Skip to content

Commit

Permalink
Looper on a different thread for the SessionLifecycleClient's callbac…
Browse files Browse the repository at this point in the history
…ks (#5447)

Co-authored-by: Jamie Rothfeder <[email protected]>
  • Loading branch information
jrothfeder and Jamie Rothfeder authored Oct 20, 2023
1 parent f75bbf2 commit 746da95
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,13 +55,18 @@ internal object SessionLifecycleClient {
private var serviceBound: Boolean = false
private val queuedMessages = LinkedBlockingDeque<Message>(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 ->
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 746da95

Please sign in to comment.