Wake word, from the review: a heard word always shows, holds end, stop and retry
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
1. Heard, but not the assistant and not on screen: Android blocks the activity start silently and runCatching called it a success, so nothing opened and nothing was posted. The opening is decided from what Android allows (wakeOpening: session, activity while an activity of the app is started, else a notification); "Heard you. Tap to ask." goes on a channel of its own (high importance, fixed words, times out after a minute) and the sheet cancels it. Settings says that only the digital assistant app opens by voice over other apps, with a row to that system setting. 2. The hold after the word is apart from the sheets' count and ends on a process scope in a finally; a starting service clears it. 3. A tap on the state in Settings retries now (WakeStatus.retryNow -> WakeListener.retryNow) and restarts the back-off; thirty seconds of samples taken in also resets the count of failures. 4. State writes carry the instance's token (WakeStatus.begin): an older instance's last "off" no longer overwrites a newer one. Engine.create is synchronized and writes the keywords to a file of its own. 5. numTrailingBlanks = 1 means more than one blank frame after the phrase (about 80 ms): the comment is corrected, and Settings and the test row say to pause after the phrase. 6. Stop no longer calls stopService or stopSelf() under a possible pending foreground start: the setting goes off and the loop ends the service with stopSelf(lastStartId); a start that arrives after the loop ended begins it again. 7. The "tap to listen again" notification is cancelled when the service starts. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
24 files changed
+615
-104
No files matched your search
@@ -140,6 +140,44 @@ class WakeListenerTest {
|
||||
job.cancelAndJoin()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aTapTriesAgainNowAndStartsTheWaitsOver() = runTest {
|
||||
var opens = 0
|
||||
val listener = listener(engine = { Detector() }, microphone = {
|
||||
opens++
|
||||
throw IllegalStateException("recording did not start")
|
||||
})
|
||||
val job = launch { listener.follow(MutableStateFlow(plan)) }
|
||||
advanceTimeBy(2_001 + 5_001) // two waits gone by: the third would be fifteen seconds
|
||||
assertEquals(3, opens)
|
||||
listener.retryNow()
|
||||
runCurrent()
|
||||
assertEquals(4, opens)
|
||||
// And the count of failures starts over: two seconds again, not a minute.
|
||||
advanceTimeBy(2_001)
|
||||
assertEquals(5, opens)
|
||||
job.cancelAndJoin()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun troubleAfterListeningWellIsNewTroubleNotTheOldOneAgain() = runTest {
|
||||
var opens = 0
|
||||
// Each microphone works for forty seconds (a tenth of a second a chunk), then stops.
|
||||
val listener = listener(engine = { Detector() }, microphone = {
|
||||
opens++
|
||||
Mic(script = List(400) { true }, after = -3)
|
||||
})
|
||||
val job = launch { listener.follow(MutableStateFlow(plan)) }
|
||||
runCurrent()
|
||||
assertEquals(1, opens)
|
||||
// Every wait is the first, two seconds, however many times it has happened.
|
||||
repeat(4) {
|
||||
advanceTimeBy(2_001)
|
||||
assertEquals(it + 2, opens)
|
||||
}
|
||||
job.cancelAndJoin()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aMicrophoneThatStopsIsAFailureAndIsClosed() = runTest {
|
||||
val mic = Mic(script = listOf(true, true), after = -3)
|
||||
|
||||
@@ -2,13 +2,17 @@ package eu.llembas.android
|
||||
|
||||
import android.app.Application
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import eu.llembas.android.wake.WakeFailure
|
||||
import eu.llembas.android.wake.WakePause
|
||||
import eu.llembas.android.wake.WakeState
|
||||
import eu.llembas.android.wake.WakeWordService
|
||||
import eu.llembas.android.wake.wakeHeardNotification
|
||||
import eu.llembas.android.wake.wakeNotification
|
||||
import eu.llembas.android.wake.wakeTitle
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
@@ -75,4 +79,20 @@ class WakeNotificationTest {
|
||||
assertEquals(Notification.FLAG_ONGOING_EVENT, notification.flags and Notification.FLAG_ONGOING_EVENT)
|
||||
assertEquals(1, notification.actions.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun heardButUnableToOpenIsANotificationThatComesUp() {
|
||||
val heard = wakeHeardNotification(app)
|
||||
assertEquals("Heard you. Tap to ask.", heard.extras.getCharSequence(Notification.EXTRA_TITLE).toString())
|
||||
// Its own channel, which may come up over what is on screen; the listening one is quiet.
|
||||
assertEquals(WakeWordService.CHANNEL_HEARD, heard.channelId)
|
||||
WakeWordService.channel(app)
|
||||
val manager = app.getSystemService(NotificationManager::class.java)
|
||||
assertEquals(NotificationManager.IMPORTANCE_HIGH, manager.getNotificationChannel(WakeWordService.CHANNEL_HEARD).importance)
|
||||
assertEquals(NotificationManager.IMPORTANCE_LOW, manager.getNotificationChannel(WakeWordService.CHANNEL).importance)
|
||||
// Fixed words, nothing from the account: it may show on the lock screen. The tap asks.
|
||||
assertEquals(Notification.VISIBILITY_PUBLIC, heard.visibility)
|
||||
assertEquals(Notification.FLAG_AUTO_CANCEL, heard.flags and Notification.FLAG_AUTO_CANCEL)
|
||||
assertNotNull(heard.contentIntent)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,10 @@ package eu.llembas.android
|
||||
import android.Manifest
|
||||
import android.app.Application
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import eu.llembas.android.wake.AssistPresence
|
||||
import eu.llembas.android.wake.WakeBootReceiver
|
||||
import eu.llembas.android.wake.WakePause
|
||||
import eu.llembas.android.wake.WakeState
|
||||
import eu.llembas.android.wake.WakeStatus
|
||||
@@ -40,17 +44,38 @@ class WakeServiceTest {
|
||||
// The first start: nothing is known yet, so it says it is starting -- no phrase.
|
||||
service.startCommand(0, 1)
|
||||
assertEquals("Starting the wake word…", title(shadowOf(service.get()).lastForegroundNotification))
|
||||
// The app is opened again while it listens for the chosen phrase: the start says the same.
|
||||
WakeStatus.set(WakeState.Listening("OK Google"))
|
||||
service.startCommand(0, 2)
|
||||
assertEquals("Listening for “OK Google”", title(shadowOf(service.get()).lastForegroundNotification))
|
||||
// And while it is paused, a start does not turn the notification into "listening".
|
||||
WakeStatus.set(WakeState.Paused(WakePause.BatterySaver))
|
||||
service.startCommand(0, 3)
|
||||
assertEquals("Wake word paused: battery saver is on", title(shadowOf(service.get()).lastForegroundNotification))
|
||||
service.destroy()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aStartClearsWhatAnEarlierInstanceLeftBehind() {
|
||||
shadowOf(app).grantPermissions(Manifest.permission.RECORD_AUDIO)
|
||||
val manager = app.getSystemService(NotificationManager::class.java)
|
||||
WakeWordService.channel(app)
|
||||
// "Tap to listen again" from a restart, and a hold of the microphone nobody ended.
|
||||
manager.notify(WakeBootReceiver.RESUME_ID, NotificationCompat.Builder(app, WakeWordService.CHANNEL).setSmallIcon(R.drawable.ic_notification).build())
|
||||
AssistPresence.release()
|
||||
val before = AssistPresence.open.value
|
||||
AssistPresence.holdFor(600_000)
|
||||
assertEquals(before + 1, AssistPresence.open.value)
|
||||
val service = Robolectric.buildService(WakeWordService::class.java).create()
|
||||
service.startCommand(0, 1)
|
||||
assertNull(shadowOf(manager).getNotification(WakeBootReceiver.RESUME_ID))
|
||||
assertEquals(before, AssistPresence.open.value)
|
||||
service.destroy()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stopIsTheSettingGoingOffNotTheServiceStoppedUnderAStart() {
|
||||
shadowOf(app).grantPermissions(Manifest.permission.RECORD_AUDIO)
|
||||
WakeStatus.set(WakeState.Listening("Hey Assistant"))
|
||||
while (shadowOf(app).nextStoppedService != null) Unit
|
||||
WakeWordService.stop()
|
||||
// Nobody calls stopService: the service ends itself once it has shown its notification.
|
||||
assertNull(shadowOf(app).nextStoppedService)
|
||||
assertEquals(WakeState.Off, WakeStatus.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun withoutTheMicrophonePermissionItIsNotStartedAndSaysWhy() {
|
||||
shadowOf(app).denyPermissions(Manifest.permission.RECORD_AUDIO)
|
||||
|
||||
@@ -2,18 +2,31 @@ package eu.llembas.android
|
||||
|
||||
import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.auth.Prefs
|
||||
import eu.llembas.android.wake.AppVisibility
|
||||
import eu.llembas.android.wake.AssistPresence
|
||||
import eu.llembas.android.wake.WakeError
|
||||
import eu.llembas.android.wake.WakeFailure
|
||||
import eu.llembas.android.wake.WakeOpening
|
||||
import eu.llembas.android.wake.WakePause
|
||||
import eu.llembas.android.wake.WakePhrases
|
||||
import eu.llembas.android.wake.WakeSensitivity
|
||||
import eu.llembas.android.wake.WakeState
|
||||
import eu.llembas.android.wake.WakeStatus
|
||||
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.wakeKeywords
|
||||
import eu.llembas.android.wake.wakeOpening
|
||||
import eu.llembas.android.wake.wakePause
|
||||
import eu.llembas.android.wake.wakeStateAtStart
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.test.advanceTimeBy
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertThrows
|
||||
@@ -26,6 +39,7 @@ import java.io.FileNotFoundException
|
||||
import java.io.InputStream
|
||||
import java.util.Locale
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class WakeTest {
|
||||
@get:Rule
|
||||
val folder = TemporaryFolder()
|
||||
@@ -144,6 +158,82 @@ class WakeTest {
|
||||
assertEquals(0, WakeStatus.trial.value.heard)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theAssistantIsOpenedOnlyTheWayAndroidAllows() {
|
||||
// The phone's assistant: its own service shows the sheet, on screen or not.
|
||||
assertEquals(WakeOpening.Session, wakeOpening(isAssistant = true, appVisible = false))
|
||||
assertEquals(WakeOpening.Session, wakeOpening(isAssistant = true, appVisible = true))
|
||||
// Not the assistant, but on screen: the sheet's activity may start.
|
||||
assertEquals(WakeOpening.Activity, wakeOpening(isAssistant = false, appVisible = true))
|
||||
// Neither: an activity start is blocked without a word, so the notification is what is relied on.
|
||||
assertEquals(WakeOpening.Notification, wakeOpening(isAssistant = false, appVisible = false))
|
||||
repeat(3) { AppVisibility.activityStopped() }
|
||||
assertFalse(AppVisibility.visible)
|
||||
AppVisibility.activityStarted()
|
||||
AppVisibility.activityStarted()
|
||||
AppVisibility.activityStopped()
|
||||
assertTrue(AppVisibility.visible)
|
||||
AppVisibility.activityStopped()
|
||||
assertFalse(AppVisibility.visible)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theHoldAfterTheWordEndsEvenIfWhoeverStartedItIsGone() = runTest {
|
||||
AssistPresence.release()
|
||||
val before = AssistPresence.open.value
|
||||
// The service that heard the word is destroyed before its three seconds are up.
|
||||
val service = CoroutineScope(coroutineContext + Job())
|
||||
AssistPresence.holdFor(3_000, service)
|
||||
assertEquals(before + 1, AssistPresence.open.value)
|
||||
runCurrent()
|
||||
service.cancel()
|
||||
runCurrent()
|
||||
assertEquals(before, AssistPresence.open.value)
|
||||
// Left to itself it ends after its time; a second word meanwhile holds on from then.
|
||||
AssistPresence.holdFor(3_000, this)
|
||||
advanceTimeBy(2_000)
|
||||
AssistPresence.holdFor(3_000, this)
|
||||
advanceTimeBy(2_000)
|
||||
assertEquals(before + 1, AssistPresence.open.value)
|
||||
advanceTimeBy(1_001)
|
||||
assertEquals(before, AssistPresence.open.value)
|
||||
// A sheet that is up is counted apart, and a starting service clears only the hold.
|
||||
AssistPresence.shown()
|
||||
AssistPresence.holdFor(60_000, this)
|
||||
AssistPresence.release()
|
||||
assertEquals(before + 1, AssistPresence.open.value)
|
||||
AssistPresence.hidden()
|
||||
assertEquals(before, AssistPresence.open.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aStartSaysWhatIsTrueAndNeverAPhraseByDefault() {
|
||||
// A service that has only just begun knows no phrase.
|
||||
assertEquals(WakeState.Starting, wakeStateAtStart(running = false, current = WakeState.Off))
|
||||
// Whatever an earlier instance left behind is not this one's to repeat.
|
||||
assertEquals(WakeState.Starting, wakeStateAtStart(running = false, current = WakeState.Listening("Hey Assistant")))
|
||||
// The app is opened again while it runs: the start says the same as before.
|
||||
val listening = WakeState.Listening("OK Google")
|
||||
assertEquals(listening, wakeStateAtStart(running = true, current = listening))
|
||||
val paused = WakeState.Paused(WakePause.BatterySaver)
|
||||
assertEquals(paused, wakeStateAtStart(running = true, current = paused))
|
||||
assertEquals(WakeState.Starting, wakeStateAtStart(running = true, current = WakeState.Off))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onlyTheNewestServiceWritesTheState() {
|
||||
val old = WakeStatus.begin()
|
||||
WakeStatus.set(old, WakeState.Listening("Hey Assistant"))
|
||||
assertEquals(WakeState.Listening("Hey Assistant"), WakeStatus.state.value)
|
||||
// Turned off and on again: a new instance takes over while the old one is still ending.
|
||||
val new = WakeStatus.begin()
|
||||
WakeStatus.set(new, WakeState.Starting)
|
||||
WakeStatus.set(old, WakeState.Off)
|
||||
assertEquals(WakeState.Starting, WakeStatus.state.value)
|
||||
WakeStatus.set(new, WakeState.Off)
|
||||
assertEquals(WakeState.Off, WakeStatus.state.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun retriesBackOff() {
|
||||
assertEquals(listOf(2_000L, 5_000L, 15_000L, 60_000L, 300_000L, 300_000L), (0..5).map(::wakeBackoff))
|
||||
|
||||
@@ -379,7 +379,7 @@ object Fakes {
|
||||
)
|
||||
|
||||
/** A standard build: the test heard the phrase twice, and notification summaries are in the other build. */
|
||||
val settingsWakeTested = settings.copy(wakeTest = WakeTestShown(running = true, heard = 2), notificationsBuild = false)
|
||||
val settingsWakeTested = settings.copy(wakeTest = WakeTestShown(running = true, heard = 2), notificationsBuild = false, isAssistant = false)
|
||||
|
||||
// The assistant's sheet, as it opens and as it answers.
|
||||
private val assistBase = AssistUiState(
|
||||
|
||||
Reference in new issue
Block a user