diff --git a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadEventListener.kt b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadEventListener.kt index 8e765faa9a..2be1fccf06 100644 --- a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadEventListener.kt +++ b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadEventListener.kt @@ -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 diff --git a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadExt.kt b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadExt.kt index fb8b08b15c..c30185da24 100644 --- a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadExt.kt +++ b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadExt.kt @@ -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) } } @@ -39,51 +39,51 @@ 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) } } @@ -91,58 +91,45 @@ private class ActivityLoadEventEmitter( * 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( @@ -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() diff --git a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitter.kt b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitter.kt index 550c8780c6..b0c769f361 100644 --- a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitter.kt +++ b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitter.kt @@ -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( diff --git a/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitterTest.kt b/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitterTest.kt index 8dce3dd9be..8b9ba9f044 100644 --- a/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitterTest.kt +++ b/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/capture/activity/UiLoadTraceEmitterTest.kt @@ -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 diff --git a/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeUiLoadEventListener.kt b/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeUiLoadEventListener.kt index c6c7bf871f..be58e21add 100644 --- a/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeUiLoadEventListener.kt +++ b/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeUiLoadEventListener.kt @@ -5,7 +5,7 @@ import io.embrace.android.embracesdk.internal.capture.activity.UiLoadEventListen class FakeUiLoadEventListener : UiLoadEventListener { val events = mutableListOf() - override fun abandon(instanceId: Int, activityName: String, timestampMs: Long) { + override fun exit(instanceId: Int, activityName: String, timestampMs: Long) { events.add( EventData( stage = "abandon",