fix(session): keep the session awake while a browser hand-off is pending (#2615)

* fix(session): keep the session awake while a browser hand-off is pending

When a program in the session opens a link in the Android browser, the
session view goes to the background and auto or manual suspend froze the
session within a couple of seconds. Programs that wait for the browser to
call back to a local listener (external sign-in flows) were frozen before
the callback arrived, so the sign-in never completed, and the session then
needed a tap to continue.

An open-url request now starts a browser hand-off that counts as a visible
surface until the session view comes back to the front or ten minutes pass.
Ordinary backgrounding still suspends as before.

* fix(session): hold through a display power change and only after the browser opens
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-14 10:58:06 -05:00
1 parent 50fbcf8b2a
commit 84b7295f11
4 files changed
+114 -1

No files matched your search

@@ -0,0 +1,26 @@
package com.droiddeck.launcher.session
internal class BrowserHandoff(private val timeoutMs: Long) {
private var startedAt = -1L
private var sessionLeft = false
fun begin(now: Long) {
startedAt = now
sessionLeft = false
}
fun sessionHidden() {
if (startedAt >= 0) sessionLeft = true
}
fun sessionShown() {
if (sessionLeft) clear()
}
fun clear() {
startedAt = -1L
sessionLeft = false
}
fun active(now: Long): Boolean = startedAt >= 0 && now - startedAt < timeoutMs
}
@@ -23,6 +23,7 @@ import android.os.IBinder
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.os.SystemClock
import android.hardware.display.DisplayManager
import android.view.Display
import android.util.Log
@@ -85,7 +86,12 @@ class SessionService : Service() {
private var activityDisplayId = Display.DEFAULT_DISPLAY
private var xrActivityVisible = false
private var signInHold = false
private val hasVisibleSurface get() = !SessionVisibilityPolicy.isHidden(
private val browserHandoff = BrowserHandoff(BROWSER_HANDOFF_TIMEOUT_MS)
private val browserHandoffExpired = Runnable {
suspendAttemptFailed = false
updateSuspendPolicy()
}
private val hasVisibleSurface get() = browserHandoff.active(SystemClock.elapsedRealtime()) || !SessionVisibilityPolicy.isHidden(
activityStarted = activityVisible || signInHold,
defaultDisplayOn = (getSystemService(Context.POWER_SERVICE) as? PowerManager)?.isInteractive ?: screenOn,
activityOnDefaultDisplay = activityDisplayId == Display.DEFAULT_DISPLAY,
@@ -235,6 +241,7 @@ class SessionService : Service() {
ACTION_ACTIVITY_VISIBLE -> {
if (!SessionState.pipActive) pipTask = false
activityVisible = true
browserHandoff.sessionShown()
activityDisplayId = intent.getIntExtra(EXTRA_ACTIVITY_DISPLAY_ID, activityDisplayId)
sessionDisplayId = intent.getIntExtra(EXTRA_DISPLAY_ID, sessionDisplayId)
screenOn = readScreenOn()
@@ -258,6 +265,7 @@ class SessionService : Service() {
}
ACTION_ACTIVITY_HIDDEN -> {
activityVisible = false
browserHandoff.sessionHidden()
activityDisplayId = intent.getIntExtra(EXTRA_ACTIVITY_DISPLAY_ID, activityDisplayId)
suspendAttemptFailed = false
updateSuspendPolicy()
@@ -287,6 +295,17 @@ class SessionService : Service() {
updateSuspendPolicy()
return START_NOT_STICKY
}
ACTION_BROWSER_HANDOFF -> {
if (!SessionState.running) return START_NOT_STICKY
browserHandoff.begin(SystemClock.elapsedRealtime())
if (!activityVisible) browserHandoff.sessionHidden()
mainHandler.removeCallbacks(browserHandoffExpired)
mainHandler.postDelayed(browserHandoffExpired, BROWSER_HANDOFF_TIMEOUT_MS)
if (!manualHold) manualPauseRequested = false
suspendAttemptFailed = false
updateSuspendPolicy()
return START_NOT_STICKY
}
ACTION_MIC_CHANGED -> {
if (SessionState.running && SessionState.micAvailable && !SessionState.suspended) {
val enabled = intent.getBooleanExtra("enabled", false)
@@ -343,6 +362,8 @@ class SessionService : Service() {
activityVisible = true
xrActivityVisible = false
signInHold = false
browserHandoff.clear()
mainHandler.removeCallbacks(browserHandoffExpired)
sessionDisplayId = intent?.getIntExtra(EXTRA_DISPLAY_ID, Display.DEFAULT_DISPLAY) ?: Display.DEFAULT_DISPLAY
activityDisplayId = intent?.getIntExtra(EXTRA_ACTIVITY_DISPLAY_ID, sessionDisplayId) ?: sessionDisplayId
screenOn = readScreenOn()
@@ -2409,6 +2430,8 @@ class SessionService : Service() {
private const val ACTION_XR_ACTIVITY_HIDDEN = "com.droiddeck.launcher.XR_ACTIVITY_HIDDEN"
private const val ACTION_SIGN_IN_HOLD = "com.droiddeck.launcher.SIGN_IN_HOLD"
private const val EXTRA_HOLD = "hold"
private const val ACTION_BROWSER_HANDOFF = "com.droiddeck.launcher.BROWSER_HANDOFF"
private const val BROWSER_HANDOFF_TIMEOUT_MS = 10 * 60_000L
private const val ACTION_TRACK_AUXILIARY = "com.droiddeck.launcher.TRACK_AUXILIARY"
private const val ACTION_STOP_AUXILIARY = "com.droiddeck.launcher.STOP_AUXILIARY"
private const val ACTION_AUXILIARY_EXITED = "com.droiddeck.launcher.AUXILIARY_EXITED"
@@ -2511,6 +2534,11 @@ class SessionService : Service() {
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_SIGN_IN_HOLD).putExtra(EXTRA_HOLD, hold))
}
fun holdForBrowserHandoff(context: Context) {
if (!SessionState.running) return
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_BROWSER_HANDOFF))
}
fun pause(context: Context) {
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_PAUSE_NOW))
}
@@ -114,6 +114,7 @@ object StoreLaunchRequests {
if (!(url.startsWith("https://") || url.startsWith("http://"))) JSONObject().put("ok", false).put("reason", "not-http")
else try {
app.startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url)).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK))
com.droiddeck.launcher.session.SessionService.holdForBrowserHandoff(app)
Log.i(TAG, "open-url: " + StoreLog.redactUrl(url))
JSONObject().put("ok", true)
} catch (e: Exception) {