Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align event emitter names with lifecycle methods #1786

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ interface UiLoadEventListener {
* When we no longer wish to observe the loading of the given UI instance. This may be called during its load
* or after it has loaded. Calls to this for a given instance should be idempotent.
*/
fun abandon(instanceId: Int, activityName: String, timestampMs: Long)
fun exit(instanceId: Int, activityName: String, timestampMs: Long)

/**
* When the app is no longer in a state where it is trying to open up UI. All traces should be abandoned and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ fun createActivityLoadEventEmitter(
clock: Clock,
versionChecker: VersionChecker,
): ActivityLifecycleListener {
val uiLoadEventEmitter = UiLoadEventEmitter(
val lifecycleEventEmitter = LifecycleEventEmitter(
uiLoadEventListener = uiLoadEventListener,
clock = clock,
)
return if (versionChecker.isAtLeast(VERSION_CODES.Q)) {
ActivityLoadEventEmitter(uiLoadEventEmitter)
ActivityLoadEventEmitter(lifecycleEventEmitter)
} else {
LegacyActivityLoadEventEmitter(uiLoadEventEmitter)
LegacyActivityLoadEventEmitter(lifecycleEventEmitter)
}
}

Expand All @@ -39,110 +39,97 @@ fun Activity.observeOpening() = javaClass.isAnnotationPresent(ObservedActivity::
*/
@RequiresApi(VERSION_CODES.Q)
private class ActivityLoadEventEmitter(
private val uiLoadEventEmitter: UiLoadEventEmitter
private val lifecycleEventEmitter: LifecycleEventEmitter
) : ActivityLifecycleListener {

override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
if (activity.observeOpening()) {
uiLoadEventEmitter.create(activity)
lifecycleEventEmitter.create(activity)
}
}

override fun onActivityPostCreated(activity: Activity, savedInstanceState: Bundle?) {
if (activity.observeOpening()) {
uiLoadEventEmitter.createEnd(activity)
lifecycleEventEmitter.createEnd(activity)
}
}

override fun onActivityPreStarted(activity: Activity) {
if (activity.observeOpening()) {
uiLoadEventEmitter.start(activity)
lifecycleEventEmitter.start(activity)
}
}

override fun onActivityPostStarted(activity: Activity) {
if (activity.observeOpening()) {
uiLoadEventEmitter.startEnd(activity)
lifecycleEventEmitter.startEnd(activity)
}
}

override fun onActivityPreResumed(activity: Activity) {
if (activity.observeOpening()) {
uiLoadEventEmitter.resume(activity)
lifecycleEventEmitter.resume(activity)
}
}

override fun onActivityPostResumed(activity: Activity) {
if (activity.observeOpening()) {
uiLoadEventEmitter.resumeEnd(activity)
lifecycleEventEmitter.resumeEnd(activity)
}
}

override fun onActivityPrePaused(activity: Activity) {
uiLoadEventEmitter.abandonTrace(activity)
lifecycleEventEmitter.pause(activity)
}

override fun onActivityStopped(activity: Activity) {
uiLoadEventEmitter.reset(activity)
lifecycleEventEmitter.stop(activity)
}
}

/**
* Version of [ActivityLoadEventEmitter] that works with all Android version and used for Android 9 or lower
*/
private class LegacyActivityLoadEventEmitter(
private val uiLoadEventEmitter: UiLoadEventEmitter
private val lifecycleEventEmitter: LifecycleEventEmitter
) : ActivityLifecycleListener {

override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
if (activity.observeOpening()) {
uiLoadEventEmitter.create(activity)
lifecycleEventEmitter.create(activity)
}
}

override fun onActivityStarted(activity: Activity) {
if (activity.observeOpening()) {
uiLoadEventEmitter.createEnd(activity)
uiLoadEventEmitter.start(activity)
lifecycleEventEmitter.createEnd(activity)
lifecycleEventEmitter.start(activity)
}
}

override fun onActivityResumed(activity: Activity) {
if (activity.observeOpening()) {
uiLoadEventEmitter.startEnd(activity)
uiLoadEventEmitter.resume(activity)
lifecycleEventEmitter.startEnd(activity)
lifecycleEventEmitter.resume(activity)
}
}

override fun onActivityPaused(activity: Activity) {
uiLoadEventEmitter.abandonTrace(activity)
lifecycleEventEmitter.pause(activity)
}

override fun onActivityStopped(activity: Activity) {
uiLoadEventEmitter.reset(activity)
lifecycleEventEmitter.stop(activity)
}
}

/**
* Maps an Activity instance's UI Load events the app-wide UI [UiLoadEventListener]
* Maps lifecycle events to UI Load events implemented by the given [UiLoadEventListener]
*/
private class UiLoadEventEmitter(
private class LifecycleEventEmitter(
private val uiLoadEventListener: UiLoadEventListener,
private val clock: Clock,
) {
fun abandonTrace(activity: Activity) {
uiLoadEventListener.abandon(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

fun reset(activity: Activity) {
uiLoadEventListener.reset(
lastInstanceId = traceInstanceId(activity),
)
}

fun create(activity: Activity) {
uiLoadEventListener.create(
Expand Down Expand Up @@ -191,6 +178,20 @@ private class UiLoadEventEmitter(
)
}

fun pause(activity: Activity) {
uiLoadEventListener.exit(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

fun stop(activity: Activity) {
uiLoadEventListener.reset(
lastInstanceId = traceInstanceId(activity),
)
}

private fun traceInstanceId(activity: Activity): Int = activity.hashCode()

private fun nowMs(): Long = clock.now().nanosToMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class UiLoadTraceEmitter(
}
}

override fun abandon(instanceId: Int, activityName: String, timestampMs: Long) {
override fun exit(instanceId: Int, activityName: String, timestampMs: Long) {
currentTracedInstanceId?.let { currentlyTracedInstanceId ->
if (instanceId != currentlyTracedInstanceId) {
endTrace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ internal class UiLoadTraceEmitterTest {
clock.tick(100L)

// set state of tracker to simulate that at least one activity has been opened
traceEmitter.abandon(lastInstanceId, lastActivityName, lastActivityExitMs)
traceEmitter.exit(lastInstanceId, lastActivityName, lastActivityExitMs)
when (previousState) {
PreviousState.FROM_ACTIVITY -> {
// do nothing extra
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.embrace.android.embracesdk.internal.capture.activity.UiLoadEventListen
class FakeUiLoadEventListener : UiLoadEventListener {
val events = mutableListOf<EventData>()

override fun abandon(instanceId: Int, activityName: String, timestampMs: Long) {
override fun exit(instanceId: Int, activityName: String, timestampMs: Long) {
events.add(
EventData(
stage = "abandon",
Expand Down
Loading