Skip to content

armgit / andee

Public Imported from GitHub
0

Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。

fix(android): tapping the ball mid-task no longer stops listening at once

heycharging1652a6320d71
diff --git a/CLAUDE.md b/CLAUDE.md
index 560a7b9..e75638d 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -202,6 +202,8 @@ Non-obvious, and now cross-provider: `AsrController.stopImpl` blocks on `waitFin
`VoiceConfig.voiceProvider` is **anything that is not exactly `google` reads as 火山**, the same fail-closed direction `brainConfig` and `groundingEnabled` use, and 火山 is the leftmost option in the picker for the same reason. Google's defaults are real values rather than blanks (`asia-northeast1`, two model names) and every hint on its settings rows is what a blank row resolves to. **`GOOGLE_VOICE_KEY` non-empty at package time is what makes Google the default provider** — a build is packaged *for* whoever it is packaged for. Note the consequence for anything that reports on the voice path: `SelfCheck.voiceKey` and `voiceReach` both follow the selected provider now, and the rows deliberately name neither provider, because a green tick earned by the 火山 key while the device is talking to Google is the lie that having one checklist instead of two exists to prevent.
+**Listening does not end on every focus loss; speaking does** (`AsrController.lossEndsTurn`, `AudioFocusGate.acquire(onLost, decide)`). The gate's rule — any loss, duck included, ends the turn — is right for TTS and was wrong for the microphone, which makes no sound: reported as "tap the ball mid-task, it starts listening and stops at once", because the app being driven is in front then and its autoplay, message tone or voice clip is a focus request. Now a duck request is ignored, a call (`CallState` or the audio mode, which flips on ring before any notification) ends the turn, and any other taker is asked to give the floor back (`retake`, at most `MAX_REGRABS` = 2 a turn) while listening carries on. This was fixed from the code, not from a capture — the Huawei LIO keeps no app logs — so if the symptom survives, get a logcat (`Asr`, `AudioFocus`) from a device that keeps them before believing another theory.
+
### The ball wears the face the model picked (`brain/SpeechMood.kt`)
`Mood`'s own KDoc has called CALM..CONCERNED *"emotions the model picks from"* since it was written, and nothing ever picked one — `HAPPY`, `CURIOUS`, `TENSE` and `ANXIOUS` were unreachable, and the ball wore CALM through every answer it ever gave. The channel is **inline markers in the model's own text**: `[happy] 找到了,在第三页。[calm] 要我打开吗?`. It is text rather than a tool call or a JSON field because the one thing a model reliably does is write the next token, and a face is worth nothing if it arrives a round trip after the sentence it belonged to. `LocalPrompt` §10 states the rule; `SpeechMood.parse` turns a raw reply into a clean string plus `Cue(at, mood)` offsets *into that clean string*.
diff --git a/app/src/main/java/net/kuafuai/andee/asr/AsrController.kt b/app/src/main/java/net/kuafuai/andee/asr/AsrController.kt
index 08d8408..6c1622e 100644
--- a/app/src/main/java/net/kuafuai/andee/asr/AsrController.kt
+++ b/app/src/main/java/net/kuafuai/andee/asr/AsrController.kt
@@ -159,7 +159,8 @@ class AsrController(
// stop in — asking and then opening the mic in the same breath would
// still catch the tail of whatever was playing. A phone call takes the
// floor back: end the turn rather than transcribe one side of a call.
- focus.acquire { stop() }
+ regrabs = 0
+ focus.acquire(onLost = { stop() }, decide = ::lossEndsTurn)
val listener = object : AsrEngine.Listener {
override fun onReady() {
@@ -276,6 +277,36 @@ class AsrController(
* two-handed exercise — and now that talking happens from the corner ball,
* with no card in the way, "I'm done" has to be inferable from silence.
*/
+ /** How many times this turn has taken the floor back from another app. See [lossEndsTurn]. */
+ @Volatile
+ private var regrabs = 0
+
+ /**
+ * Whether losing audio focus ends this listening turn. It used to, always — the rule the speaking side
+ * needs (half an answer at a quarter volume is no answer) applied to the side that makes no sound.
+ * The cost showed up as "tap the ball mid-task, it starts listening and stops at once": the app being
+ * driven is in front then, and a video that autoplays, a chat's message tone or a voice clip the last
+ * tap opened is a focus request that took the user's sentence with it.
+ *
+ * Now only a call ends it — the call notification ([CallState]) or the audio mode, which changes the
+ * moment it rings, before any notification. A duck request means nothing to a microphone. Any other
+ * taker is asked to give the floor back, twice a turn at most so two apps cannot fight over it; after
+ * that we keep listening without it rather than drop what the user is saying.
+ */
+ private fun lossEndsTurn(change: Int): Boolean {
+ if (change == android.media.AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) return false
+ val mode = (context.getSystemService(Context.AUDIO_SERVICE) as android.media.AudioManager).mode
+ if (net.kuafuai.andee.device.CallState.isActive || mode != android.media.AudioManager.MODE_NORMAL) {
+ Log.i(TAG, "focus lost to a call (mode=$mode) — ending the turn")
+ return true
+ }
+ if (regrabs < MAX_REGRABS) {
+ regrabs++
+ focus.retake()
+ }
+ return false
+ }
+
private fun armSilenceWatchdog() {
lastSpeechAt = SystemClock.uptimeMillis()
lastPartial = ""
@@ -383,6 +414,9 @@ class AsrController(
*/
private const val MAX_TURN_MS = 60_000L
+ /** See [lossEndsTurn]. */
+ private const val MAX_REGRABS = 2
+
/**
* How long a turn may run having heard nothing at all before the
* silence rule is allowed to end it. See [checkSilence].
diff --git a/app/src/main/java/net/kuafuai/andee/audio/AudioFocusGate.kt b/app/src/main/java/net/kuafuai/andee/audio/AudioFocusGate.kt
index b2582b5..ce574c7 100644
--- a/app/src/main/java/net/kuafuai/andee/audio/AudioFocusGate.kt
+++ b/app/src/main/java/net/kuafuai/andee/audio/AudioFocusGate.kt
@@ -47,6 +47,10 @@ class AudioFocusGate(
@Volatile
private var held = false
+ /** Set by the [acquire] overload that wants a say; see there. */
+ @Volatile
+ private var decide: ((change: Int) -> Boolean)? = null
+
private val listener = AudioManager.OnAudioFocusChangeListener { change ->
when (change) {
// All three are "stop": a phone call (LOSS_TRANSIENT), something
@@ -58,6 +62,10 @@ class AudioFocusGate(
AudioManager.AUDIOFOCUS_LOSS,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
+ if (onLost != null && decide?.invoke(change) == false) {
+ Log.i(TAG, "focus lost ($change) — not the end of this turn")
+ return@OnAudioFocusChangeListener
+ }
val cb = onLost
onLost = null
held = false
@@ -88,6 +96,30 @@ class AudioFocusGate(
* mid-turn. Fires at most once per [acquire].
*/
fun acquire(onLost: () -> Unit): Boolean {
+ decide = null
+ return take(onLost)
+ }
+
+ /**
+ * [acquire], for a holder for whom not every loss means the end. [decide] sees which loss it was and
+ * returns true to end (then [onLost] fires, once, as usual) or false to carry on — the callback stays
+ * armed for the next one. Listening is that holder: it makes no sound, so a request to duck means nothing
+ * to it, and an app that grabbed the floor can be asked to give it back ([retake]).
+ */
+ fun acquire(onLost: () -> Unit, decide: (change: Int) -> Boolean): Boolean {
+ this.decide = decide
+ return take(onLost)
+ }
+
+ /** Ask for the floor again after declining a loss, so the app that took it pauses once more. */
+ fun retake(): Boolean {
+ val ok = am.requestAudioFocus(request) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
+ held = ok
+ Log.i(TAG, "focus retaken: $ok")
+ return ok
+ }
+
+ private fun take(onLost: () -> Unit): Boolean {
this.onLost = onLost
val ok = am.requestAudioFocus(request) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
held = ok
@@ -101,6 +133,7 @@ class AudioFocusGate(
/** Give the floor back so the media app can resume. Safe to call twice. */
fun release() {
onLost = null
+ decide = null
if (!held) return
held = false
runCatching { am.abandonAudioFocusRequest(request) }