From 746da952ed75262a6864011143fa5b37edb4dba9 Mon Sep 17 00:00:00 2001 From: Jamie Rothfeder Date: Fri, 20 Oct 2023 15:30:39 -0400 Subject: [PATCH] Looper on a different thread for the SessionLifecycleClient's callbacks (#5447) Co-authored-by: Jamie Rothfeder --- .../sessions/SessionLifecycleClient.kt | 22 ++++++++++++++++++- .../SessionsActivityLifecycleCallbacks.kt | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionLifecycleClient.kt b/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionLifecycleClient.kt index ac818eb105c..678fec13cde 100644 --- a/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionLifecycleClient.kt +++ b/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionLifecycleClient.kt @@ -21,6 +21,7 @@ import android.content.Context import android.content.Intent import android.content.ServiceConnection import android.os.Handler +import android.os.HandlerThread import android.os.IBinder import android.os.Looper import android.os.Message @@ -54,13 +55,18 @@ internal object SessionLifecycleClient { private var serviceBound: Boolean = false private val queuedMessages = LinkedBlockingDeque(MAX_QUEUED_MESSAGES) private var curSessionId: String = "" + private var handlerThread: HandlerThread = HandlerThread("FirebaseSessionsClient_HandlerThread") + init { + handlerThread.start() + } + /** * The callback class that will be used to receive updated session events from the * [SessionLifecycleService]. */ // TODO(rothbutter) should we use the main looper or is there one available in this SDK? - internal class ClientUpdateHandler : Handler(Looper.getMainLooper()) { + internal class ClientUpdateHandler : Handler(handlerThread.looper) { override fun handleMessage(msg: Message) { when (msg.what) { SessionLifecycleService.SESSION_UPDATED -> @@ -144,6 +150,20 @@ internal object SessionLifecycleClient { sendLifecycleEvent(SessionLifecycleService.BACKGROUNDED) } + /** + * Perform initialization that requires cleanup + */ + fun started() { + if (!handlerThread.isAlive) { handlerThread.start() } + } + + /** + * Cleanup initialization + */ + fun stopped() { + handlerThread.quit() + } + /** * Sends a message to the [SessionLifecycleService] with the given event code. This will * potentially also send any messages that have been queued up but not successfully delivered to diff --git a/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionsActivityLifecycleCallbacks.kt b/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionsActivityLifecycleCallbacks.kt index c6f91d597b2..6729f902dfd 100644 --- a/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionsActivityLifecycleCallbacks.kt +++ b/firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionsActivityLifecycleCallbacks.kt @@ -31,9 +31,9 @@ internal object SessionsActivityLifecycleCallbacks : ActivityLifecycleCallbacks override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit - override fun onActivityStarted(activity: Activity) = Unit + override fun onActivityStarted(activity: Activity) = SessionLifecycleClient.started() - override fun onActivityStopped(activity: Activity) = Unit + override fun onActivityStopped(activity: Activity) = SessionLifecycleClient.stopped() override fun onActivityDestroyed(activity: Activity) = Unit