A session that ends without being hidden leaves the gate shut
CI / check (push) Canceled after 0s

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 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-24 14:10:27 +00:00
1 parent bac89b5e68
commit 08a720465e
3 files changed
+53 -2

No files matched your search

@@ -137,3 +137,16 @@ suspend fun <T> sendFrom(items: List<T>, 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()
}
@@ -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
@@ -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<String>()
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() }