From 08a720465ed95d614690d991b36bce203d89e467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Bene=C5=A1?= Date: Tue, 24 Nov 2026 14:10:27 +0000 Subject: [PATCH] A session that ends without being hidden leaves the gate shut The sheet's flag at the gate is cleared when the session is destroyed and when its service is, not only when it hides: Android may tear a session down without hiding it, and the gate would have stayed open with nothing of the app in front. Co-Authored-By: Claude Fable 5.2 --- .../java/eu/llembas/android/InstanceGate.kt | 13 ++++++++ .../llembas/android/assist/AssistService.kt | 10 ++++-- .../java/eu/llembas/android/ShownMediaTest.kt | 32 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/llembas/android/InstanceGate.kt b/app/src/main/java/eu/llembas/android/InstanceGate.kt index cbb38c0..34bd56a 100644 --- a/app/src/main/java/eu/llembas/android/InstanceGate.kt +++ b/app/src/main/java/eu/llembas/android/InstanceGate.kt @@ -137,3 +137,16 @@ suspend fun sendFrom(items: List, from: Int, turn: suspend () -> Unit, do done(i + 1) } } + +/** + * The assistant's sheet as the gate hears of it: every way the session's window can go says so. + * Android may show a sheet that is up again, hide it once, or tear the session down without + * hiding it at all -- [gone] is for each of those ends, so the sheet can never be left counted + * as the app in front when nothing of the app is. + */ +class SheetAtGate(private val gate: InstanceGate) { + fun shown() = gate.sessionShown() + + /** Hidden, destroyed, or its service gone: whichever comes, and however many of them. */ + fun gone() = gate.sessionHidden() +} diff --git a/app/src/main/java/eu/llembas/android/assist/AssistService.kt b/app/src/main/java/eu/llembas/android/assist/AssistService.kt index 0f34d7c..70e08b5 100644 --- a/app/src/main/java/eu/llembas/android/assist/AssistService.kt +++ b/app/src/main/java/eu/llembas/android/assist/AssistService.kt @@ -28,6 +28,7 @@ import androidx.savedstate.SavedStateRegistryController import androidx.savedstate.SavedStateRegistryOwner import androidx.savedstate.setViewTreeSavedStateRegistryOwner import eu.llembas.android.AppLanguage +import eu.llembas.android.SheetAtGate import eu.llembas.android.lembas import eu.llembas.android.ui.theme.LembasTheme import eu.llembas.android.wake.AssistLink @@ -79,6 +80,8 @@ class LembasAssistService : VoiceInteractionService() { override fun onDestroy() { AssistLink.destroyed(System.currentTimeMillis()) + // With the service gone no sheet of it is up, whatever its session managed to say. + lembas.gate.sessionHidden() super.onDestroy() } @@ -143,6 +146,7 @@ class LembasAssistSession(context: Context) : /** Whether this session's sheet is up, as far as the wake word's pause is concerned. */ private var up = false + private val atGate = SheetAtGate(context.lembas.gate) /** Over the lock screen: waiting for what is heard, to hand it to the sheet's activity. */ private var handing: Job? = null @@ -203,7 +207,7 @@ class LembasAssistSession(context: Context) : } // The sheet is the app in front: what it draws of the account (a reply's picture) may be // asked for, unless the phone is locked. Said each time; the gate keeps it as one. - context.lembas.gate.sessionShown() + atGate.shown() AssistLink.sheetShown(SystemClock.elapsedRealtime()) // One sheet, never two: a sheet the wake word opened as a screen of the app, because // this one was slow to come, goes now that this one is here. @@ -233,7 +237,7 @@ class LembasAssistSession(context: Context) : up = false AssistPresence.hidden() } - context.lembas.gate.sessionHidden() + atGate.gone() controller.close() scope.cancel() scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) @@ -244,6 +248,8 @@ class LembasAssistSession(context: Context) : override fun onDestroy() { super.onDestroy() + // A session torn down without being hidden must not leave the gate open behind it. + atGate.gone() controller.close() scope.cancel() registry.currentState = Lifecycle.State.DESTROYED diff --git a/app/src/test/java/eu/llembas/android/ShownMediaTest.kt b/app/src/test/java/eu/llembas/android/ShownMediaTest.kt index ad68fc3..f351594 100644 --- a/app/src/test/java/eu/llembas/android/ShownMediaTest.kt +++ b/app/src/test/java/eu/llembas/android/ShownMediaTest.kt @@ -383,6 +383,38 @@ class ShownMediaTest { assertFalse(gate.open.value) } + @Test + fun aSessionTornDownWithoutBeingHiddenLeavesTheGateShut() = runTest { + val gate = InstanceGate { false } + val sheet = SheetAtGate(gate) + // Shown, then destroyed with no hide between them: the end is said all the same. + sheet.shown() + assertTrue(gate.open.value) + sheet.gone() + assertFalse(gate.open.value) + assertFalse(gate.look()) + // Nothing asked for now starts until something of the app is in front again. + val fetched = mutableListOf() + val picture = launch { + gate.passLater() + fetched += "picture" + } + runCurrent() + assertTrue(fetched.isEmpty()) + // Hidden and then destroyed, or destroyed twice, is still one end; the next showing opens it. + sheet.gone() + sheet.gone() + assertFalse(gate.open.value) + sheet.shown() + runCurrent() + assertEquals(listOf("picture"), fetched) + picture.join() + // An activity in front is not undone by the sheet's end. + gate.activityStarted() + sheet.gone() + assertTrue(gate.open.value) + } + // --- Open with ---------------------------------------------------------------------------------- private fun bytes(vararg values: Int) = ByteArray(values.size) { values[it].toByte() }