Wake word: the wake runs outside the loop it pauses; what Android says is recorded
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
Review W1 (must): wake() was a suspend function run inside the hearing job, and its own first step -- holding the microphone for the sheet -- paused the listener, which cancelled that job: no wait for the sheet, no fallback, and no "Last wake" record for a real wake. - WakeListener hands `heard` to the scope beside the listening, so a pause never cancels the answer (test: a suspending heard that pauses the plan). The service runs the wake on the app's own scope, one at a time, holds the microphone for the whole attempt, and writes the record NonCancellable. - WakeOpener holds the order of what is tried, apart from Android, with tests: wait up to 1 s for the connection when Android says the service is active; "opened" only once a sheet says it is up; a session that was not refused gets a second wait before anything else is shown (W3: never two sheets -- and the session closes a sheet the wake word opened in its place); the activity counts only where Android will start it. - The record gains isActiveService, FEATURE_VOICE_RECOGNIZERS, screen, keyguard, app visible, ms until the sheet, the service's onCreate and onDestroy times, onPrepareToShowSession / onShowSessionFailed (API 34+), and whether the connection was waited for. Settings says when the role is held but Android does not use LLeMbas as its voice assistant. - AssistLink.shutdown is guarded by instance. W2: a session counts as up once per show; the activity from onStart to onStop. W4: the activity says it is up from onStart and onNewIntent, and turns the screen on there too. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
22 files changed
+740
-97
No files matched your search
@@ -13,6 +13,7 @@ import eu.llembas.android.wake.wakeKeywords
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -140,6 +141,33 @@ class WakeListenerTest {
|
||||
job.cancelAndJoin()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun answeringTheWordIsNotCancelledByThePauseItCauses() = runTest {
|
||||
// What a real wake does: its first step pauses the listening (the sheet takes the
|
||||
// microphone), then it waits for the sheet, then it writes down what happened.
|
||||
val plans = MutableStateFlow(plan)
|
||||
var written = false
|
||||
val listener = WakeListener(
|
||||
say = { states += it },
|
||||
engine = { Detector(hearOn = 2) },
|
||||
microphone = { Mic() },
|
||||
heard = {
|
||||
plans.value = plan.copy(pause = WakePause.Assistant)
|
||||
delay(2_500)
|
||||
written = true
|
||||
},
|
||||
work = StandardTestDispatcher(testScheduler),
|
||||
)
|
||||
val job = launch { listener.follow(plans) }
|
||||
runCurrent()
|
||||
// The listening is paused, as asked...
|
||||
assertEquals(WakeState.Paused(WakePause.Assistant), states.last())
|
||||
// ...and the answer goes on to its end all the same.
|
||||
advanceTimeBy(2_501)
|
||||
assertTrue(written)
|
||||
job.cancelAndJoin()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aTapTriesAgainNowAndStartsTheWaitsOver() = runTest {
|
||||
var opens = 0
|
||||
|
||||
@@ -118,28 +118,40 @@ class WakeNotificationTest {
|
||||
listOf("Tue 10 Mar, 08:00:00 · “Hey Assistant”", "Opened the assistant."),
|
||||
words(WakeAttempt(at = at, phrase = "Hey Assistant", way = "session", outcome = WakeAttempt.OUT_OPENED, assistant = true, readyAt = at - 60_000)),
|
||||
)
|
||||
// The assistant in name, never connected in this run of the app: said step by step.
|
||||
// The assistant in name, but Android was not using it: said step by step.
|
||||
val missed = words(
|
||||
WakeAttempt(
|
||||
at = at,
|
||||
phrase = "Hey Assistant",
|
||||
way = "notification",
|
||||
outcome = WakeAttempt.OUT_NOT_CONNECTED,
|
||||
fallback = "notification",
|
||||
assistant = true,
|
||||
connectedLater =
|
||||
at + 30_000,
|
||||
),
|
||||
)
|
||||
assertEquals(
|
||||
listOf(
|
||||
"Tue 10 Mar, 08:00:00 · “Hey Assistant”",
|
||||
"Android had not connected LLeMbas as the assistant, so it could not open the sheet.",
|
||||
"Posted a notification to tap instead.",
|
||||
"LLeMbas was the digital assistant app.",
|
||||
"LLeMbas was chosen as the digital assistant app, but Android was not using it as its voice assistant.",
|
||||
"Android had not connected the assistant since the app started.",
|
||||
"Android connected it again at Tue 10 Mar, 08:00:30.",
|
||||
"screen=on locked=false visible=false created=- destroyed=- prepared=false",
|
||||
),
|
||||
words(
|
||||
WakeAttempt(
|
||||
at = at,
|
||||
phrase = "Hey Assistant",
|
||||
way = "notification",
|
||||
outcome = WakeAttempt.OUT_NOT_CONNECTED,
|
||||
fallback = "notification",
|
||||
assistant = true,
|
||||
connectedLater =
|
||||
at + 30_000,
|
||||
),
|
||||
),
|
||||
missed,
|
||||
)
|
||||
// Active and held, and still not connected: the plain "was the assistant" line, and that it was waited for.
|
||||
val waited =
|
||||
words(WakeAttempt(at = at, phrase = "Hey Assistant", outcome = WakeAttempt.OUT_NOT_CONNECTED, assistant = true, active = true, waited = true))
|
||||
assertEquals("LLeMbas was the digital assistant app.", waited[2])
|
||||
assertEquals("Waited a moment for Android to connect it.", waited[3])
|
||||
// A sheet that came says how long it took.
|
||||
assertEquals(
|
||||
"The sheet was up after 180 ms.",
|
||||
words(WakeAttempt(at = at, phrase = "Hey Assistant", way = "session", outcome = WakeAttempt.OUT_OPENED, sheetMs = 180))[2],
|
||||
)
|
||||
// Android's own words where it refused, and when it had connected and dropped the service.
|
||||
val refused = words(
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package eu.llembas.android
|
||||
|
||||
import eu.llembas.android.wake.WakeAttempt
|
||||
import eu.llembas.android.wake.WakeFacts
|
||||
import eu.llembas.android.wake.WakeOpener
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* What is tried when the phrase is heard, in which order, and what is written down about it --
|
||||
* without a phone: Android's part is a script here (connected or not, refuses or not, a sheet
|
||||
* comes on the n-th wait or never).
|
||||
*/
|
||||
class WakeOpenerTest {
|
||||
/** One wake against a scripted Android. */
|
||||
private class Phone(
|
||||
var facts: WakeFacts,
|
||||
/** The waits for a sheet, in order: how long each took, or null for "none came". */
|
||||
val sheets: MutableList<Long?> = mutableListOf(),
|
||||
val connects: Boolean = false,
|
||||
val refuses: Throwable? = null,
|
||||
val activityRefused: Throwable? = null,
|
||||
) {
|
||||
val did = mutableListOf<String>()
|
||||
|
||||
val opener = WakeOpener(
|
||||
facts = { facts },
|
||||
awaitConnected = {
|
||||
did += "wait-connect"
|
||||
connects
|
||||
},
|
||||
showSession = {
|
||||
did += "session"
|
||||
refuses?.let { throw it }
|
||||
},
|
||||
startActivity = {
|
||||
did += "activity"
|
||||
activityRefused?.let { throw it }
|
||||
},
|
||||
sheetCame = {
|
||||
did += "wait-sheet"
|
||||
if (sheets.isEmpty()) null else sheets.removeAt(0)
|
||||
},
|
||||
notify = { did += "notify" },
|
||||
now = { 1_000 },
|
||||
)
|
||||
}
|
||||
|
||||
private val assistant = WakeFacts(connected = true, active = true, role = true)
|
||||
|
||||
@Test
|
||||
fun theAssistantsSheetOpensAndIsCountedOnlyOnceItIsUp() = runTest {
|
||||
val phone = Phone(assistant, sheets = mutableListOf(180))
|
||||
val attempt = phone.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("session", "wait-sheet"), phone.did)
|
||||
assertEquals(WakeAttempt.OUT_OPENED, attempt.outcome)
|
||||
assertEquals("session", attempt.way)
|
||||
assertEquals(180L, attempt.sheetMs)
|
||||
assertEquals("", attempt.fallback)
|
||||
assertEquals("Hey Assistant", attempt.phrase)
|
||||
assertTrue(attempt.assistant && attempt.active)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSlowSessionIsWaitedForAgainAndNothingElseIsPutBesideIt() = runTest {
|
||||
// No sheet in the first wait, then it comes: one sheet, no activity, no notification.
|
||||
val phone = Phone(assistant, sheets = mutableListOf(null, 400))
|
||||
val attempt = phone.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("session", "wait-sheet", "wait-sheet"), phone.did)
|
||||
assertEquals(WakeAttempt.OUT_OPENED, attempt.outcome)
|
||||
assertEquals(WakeOpener.SHEET_WAIT_MS + 400, attempt.sheetMs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSessionThatNeverComesFallsToTheActivityThenToTheNotification() = runTest {
|
||||
// Connected, so the activity may start: it is waited for, and counts when it comes.
|
||||
val came = Phone(assistant, sheets = mutableListOf(null, null, 300))
|
||||
val opened = came.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("session", "wait-sheet", "wait-sheet", "activity", "wait-sheet"), came.did)
|
||||
assertEquals(WakeAttempt.OUT_NO_SHEET, opened.outcome)
|
||||
assertEquals("activity", opened.fallback)
|
||||
// Nothing comes at all: the notification, and it is said.
|
||||
val never = Phone(assistant)
|
||||
val told = never.opener.open("Hey Assistant")
|
||||
assertEquals("notify", never.did.last())
|
||||
assertEquals(1, never.did.count { it == "notify" })
|
||||
assertEquals("notification", told.fallback)
|
||||
assertEquals(-1L, told.sheetMs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aRefusalIsKeptInAndroidsOwnWords() = runTest {
|
||||
val phone = Phone(assistant, refuses = SecurityException("Caller is not the current voice interaction service"), sheets = mutableListOf(250))
|
||||
val attempt = phone.opener.open("Hey Assistant")
|
||||
// Refused: no waiting for a session that was never asked for; straight to the activity.
|
||||
assertEquals(listOf("session", "activity", "wait-sheet"), phone.did)
|
||||
assertEquals(WakeAttempt.OUT_REFUSED, attempt.outcome)
|
||||
assertEquals("SecurityException: Caller is not the current voice interaction service", attempt.detail)
|
||||
assertEquals("activity", attempt.fallback)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notConnectedAndNotOnScreenIsANotificationAndSaysWhy() = runTest {
|
||||
// The role held, but Android does not use the service: nothing to wait for.
|
||||
val phone = Phone(WakeFacts(connected = false, active = false, role = true, locked = true, screenOn = false))
|
||||
val attempt = phone.opener.open("Hey Assistant")
|
||||
// The activity is still tried (a phone may allow it), but it is not relied on or waited for.
|
||||
assertEquals(listOf("activity", "notify"), phone.did)
|
||||
assertEquals(WakeAttempt.OUT_NOT_CONNECTED, attempt.outcome)
|
||||
assertEquals("notification", attempt.way)
|
||||
assertEquals("notification", attempt.fallback)
|
||||
assertTrue(attempt.assistant)
|
||||
assertFalse(attempt.active)
|
||||
assertFalse(attempt.waited)
|
||||
assertTrue(attempt.locked)
|
||||
assertFalse(attempt.screenOn)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun activeButNotConnectedYetIsGivenAMomentToConnect() = runTest {
|
||||
val waiting = WakeFacts(connected = false, active = true, role = true)
|
||||
val phone = Phone(waiting, connects = true, sheets = mutableListOf(200))
|
||||
val attempt = phone.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("wait-connect", "session", "wait-sheet"), phone.did)
|
||||
assertEquals(WakeAttempt.OUT_OPENED, attempt.outcome)
|
||||
assertTrue(attempt.waited)
|
||||
// It does not connect in time: the record says that it was waited for.
|
||||
val late = Phone(waiting, connects = false)
|
||||
val missed = late.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("wait-connect", "activity", "notify"), late.did)
|
||||
assertEquals(WakeAttempt.OUT_NOT_CONNECTED, missed.outcome)
|
||||
assertTrue(missed.waited)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onScreenWithoutTheAssistantTheSheetIsTheAppsOwnScreen() = runTest {
|
||||
val phone = Phone(WakeFacts(connected = false, active = false, role = false, visible = true), sheets = mutableListOf(120))
|
||||
val attempt = phone.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("activity", "wait-sheet"), phone.did)
|
||||
assertEquals("activity", attempt.way)
|
||||
assertEquals("activity", attempt.fallback)
|
||||
assertEquals(120L, attempt.sheetMs)
|
||||
assertFalse(attempt.assistant)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun withTheScreenOffTheSheetIsTheActivityAndItIsNotStartedTwice() = runTest {
|
||||
val dark = assistant.copy(screenOn = false)
|
||||
val phone = Phone(dark, sheets = mutableListOf(350))
|
||||
assertEquals("assistant_activity", phone.opener.open("Hey Assistant").way)
|
||||
assertEquals(listOf("activity", "wait-sheet"), phone.did)
|
||||
// It does not come up: a notification, not the same activity once more.
|
||||
val never = Phone(dark)
|
||||
val told = never.opener.open("Hey Assistant")
|
||||
assertEquals(listOf("activity", "wait-sheet", "notify"), never.did)
|
||||
assertEquals(WakeAttempt.OUT_NO_SHEET, told.outcome)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theRecordCarriesWhatTheAssistantServiceHadSaid() = runTest {
|
||||
val phone = Phone(WakeFacts(connected = false, active = false, role = true, recognizers = false))
|
||||
val link = WakeAttempt(createdAt = 10, destroyedAt = 20, readyAt = 11, shutdownAt = 19)
|
||||
val attempt = phone.opener.open("OK Computer", link)
|
||||
assertEquals(1_000L, attempt.at)
|
||||
assertEquals(listOf(10L, 20L, 11L, 19L), listOf(attempt.createdAt, attempt.destroyedAt, attempt.readyAt, attempt.shutdownAt))
|
||||
assertFalse(attempt.recognizers)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import eu.llembas.android.wake.WakeWords
|
||||
import eu.llembas.android.wake.checkKeywords
|
||||
import eu.llembas.android.wake.unpackWakeModel
|
||||
import eu.llembas.android.wake.wakeBackoff
|
||||
import eu.llembas.android.wake.wakeFallback
|
||||
import eu.llembas.android.wake.wakeKeywords
|
||||
import eu.llembas.android.wake.wakeLogAdd
|
||||
import eu.llembas.android.wake.wakeLogConnected
|
||||
@@ -174,9 +173,6 @@ class WakeTest {
|
||||
assertEquals(WakeWay.Activity, wakeWay(connected = false, screenOn = true, appVisible = true))
|
||||
assertEquals(WakeWay.Notification, wakeWay(connected = false, screenOn = true, appVisible = false))
|
||||
assertEquals(WakeWay.Notification, wakeWay(connected = false, screenOn = false, appVisible = false))
|
||||
// And when the assistant's own way did not open the sheet.
|
||||
assertEquals(WakeWay.Activity, wakeFallback(appVisible = true))
|
||||
assertEquals(WakeWay.Notification, wakeFallback(appVisible = false))
|
||||
repeat(3) { AppVisibility.activityStopped() }
|
||||
assertFalse(AppVisibility.visible)
|
||||
AppVisibility.activityStarted()
|
||||
|
||||
Reference in new issue
Block a user