From 461bb50d294e5876c29ddba62c307cda0f25fb87 Mon Sep 17 00:00:00 2001 From: SmartbearYing <129767130+SmartbearYing@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:30:07 +0100 Subject: [PATCH] feature(exitInfo) addPreOnSend callBack test (#1902) --- .../main/java/com/bugsnag/android/CallbackState.kt | 7 ++++++- .../src/main/java/com/bugsnag/android/Client.java | 4 ++++ .../java/com/bugsnag/android/CallbackStateTest.kt | 12 ++++++++++++ .../api/bugsnag-plugin-android-exitinfo.api | 3 +-- .../com/bugsnag/android/BugsnagExitInfoPlugin.kt | 10 ++++------ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/CallbackState.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/CallbackState.kt index facd203e80..2625130448 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/CallbackState.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/CallbackState.kt @@ -8,7 +8,7 @@ internal data class CallbackState( val onErrorTasks: MutableCollection = CopyOnWriteArrayList(), val onBreadcrumbTasks: MutableCollection = CopyOnWriteArrayList(), val onSessionTasks: MutableCollection = CopyOnWriteArrayList(), - val onSendTasks: MutableCollection = CopyOnWriteArrayList() + val onSendTasks: MutableList = CopyOnWriteArrayList() ) : CallbackAware { private var internalMetrics: InternalMetrics = InternalMetricsNoop() @@ -67,6 +67,11 @@ internal data class CallbackState( } } + fun addPreOnSend(onSend: OnSendCallback) { + onSendTasks.add(0, onSend) + internalMetrics.notifyAddCallback(onSendName) + } + fun removeOnSend(onSend: OnSendCallback) { if (onSendTasks.remove(onSend)) { internalMetrics.notifyRemoveCallback(onSendName) diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java b/bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java index 561aa383e5..ab893735d4 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java @@ -1187,4 +1187,8 @@ void setAutoNotify(boolean autoNotify) { void setAutoDetectAnrs(boolean autoDetectAnrs) { pluginClient.setAutoDetectAnrs(this, autoDetectAnrs); } + + void addOnSend(OnSendCallback callback) { + callbackState.addPreOnSend(callback); + } } diff --git a/bugsnag-android-core/src/test/java/com/bugsnag/android/CallbackStateTest.kt b/bugsnag-android-core/src/test/java/com/bugsnag/android/CallbackStateTest.kt index 2eaddc9f56..8ba92607a5 100644 --- a/bugsnag-android-core/src/test/java/com/bugsnag/android/CallbackStateTest.kt +++ b/bugsnag-android-core/src/test/java/com/bugsnag/android/CallbackStateTest.kt @@ -152,4 +152,16 @@ class CallbackStateTest { assertFalse(state.runOnSendTasks({ event }, NoopLogger)) assertEquals(0, count) } + + @Test + fun testOnPreSend() { + val state = CallbackState() + state.addOnSend(OnSendCallback { true }) + val onSendCallBack = state.onSendTasks.first() + state.addPreOnSend(OnSendCallback { true }) + val expectedOnSendCallBackPosition = state.onSendTasks.indexOf(onSendCallBack) + + assertEquals(2, state.onSendTasks.size) + assertEquals(1, expectedOnSendCallBackPosition) + } } diff --git a/bugsnag-plugin-android-exitinfo/api/bugsnag-plugin-android-exitinfo.api b/bugsnag-plugin-android-exitinfo/api/bugsnag-plugin-android-exitinfo.api index eb44b168bc..9d055a22c5 100644 --- a/bugsnag-plugin-android-exitinfo/api/bugsnag-plugin-android-exitinfo.api +++ b/bugsnag-plugin-android-exitinfo/api/bugsnag-plugin-android-exitinfo.api @@ -1,11 +1,10 @@ -public final class com/bugsnag/android/BugsnagExitInfoPlugin : com/bugsnag/android/OnSendCallback, com/bugsnag/android/Plugin { +public final class com/bugsnag/android/BugsnagExitInfoPlugin : com/bugsnag/android/Plugin { public fun ()V public fun (Z)V public fun (ZZ)V public fun (ZZZ)V public synthetic fun (ZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun load (Lcom/bugsnag/android/Client;)V - public fun onSend (Lcom/bugsnag/android/Event;)Z public fun unload ()V } diff --git a/bugsnag-plugin-android-exitinfo/src/main/java/com/bugsnag/android/BugsnagExitInfoPlugin.kt b/bugsnag-plugin-android-exitinfo/src/main/java/com/bugsnag/android/BugsnagExitInfoPlugin.kt index 56de08efc5..b7fab82586 100644 --- a/bugsnag-plugin-android-exitinfo/src/main/java/com/bugsnag/android/BugsnagExitInfoPlugin.kt +++ b/bugsnag-plugin-android-exitinfo/src/main/java/com/bugsnag/android/BugsnagExitInfoPlugin.kt @@ -24,19 +24,16 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor( */ private val disableProcessStateSummaryOverride: Boolean = false -) : Plugin, OnSendCallback { +) : Plugin { private var exitInfoCallback: ExitInfoCallback? = null - override fun onSend(event: Event): Boolean { - return exitInfoCallback?.onSend(event) ?: true - } - override fun load(client: Client) { if (!disableProcessStateSummaryOverride) { client.addOnSession( OnSessionCallback { session: Session -> - val am = client.appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val am = + client.appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager am.setProcessStateSummary(session.id.toByteArray()) return@OnSessionCallback true } @@ -47,6 +44,7 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor( TombstoneEventEnhancer(client.logger), TraceEventEnhancer(client.logger, client.immutableConfig.projectPackages) ) + client.addOnSend(exitInfoCallback) } override fun unload() {