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

Looper on a different thread for the SessionLifecycleClient's callbacks #5447

Merged
merged 6 commits into from
Oct 20, 2023
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 @@ -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")
jrothfeder marked this conversation as resolved.
Show resolved Hide resolved

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
Loading