Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
feat: add notification settings for reply alerts with vibration and chime options
Durganshu Mishra234383d4418c
diff --git a/app/src/main/java/com/gotcha/MainActivity.kt b/app/src/main/java/com/gotcha/MainActivity.ktindex aa97387..0b5f3ff 100644--- a/app/src/main/java/com/gotcha/MainActivity.kt+++ b/app/src/main/java/com/gotcha/MainActivity.kt@@ -448,6 +448,15 @@ class MainActivity : ComponentActivity() {themeMode = modesettingsRepository.save(settingsRepository.load().copy(themeMode = mode))},+ onNotifyAlertChange = { vibration, chime ->+ settingsRepository.save(+ settingsRepository.load().copy(+ notifyVibrationEnabled = vibration,+ notifyChimeEnabled = chime+ )+ )+ chatViewModel.refreshSettings()+ },onRefreshAudioModels = { s ->withContext(Dispatchers.IO) {val ttsBase = s.effectiveTtsBaseUrldiff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.ktindex 0317b69..fb7c894 100644--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt@@ -8,6 +8,7 @@ import androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.gotcha.audio.AudioModelimport com.gotcha.audio.AudioProvider+import com.gotcha.audio.CompletionFeedbackimport com.gotcha.audio.SttEngineimport com.gotcha.audio.TtsEngineimport com.gotcha.data.ChatHistoryRepository@@ -148,6 +149,15 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A/** Agent mode of the session currently bound to [agentEngine]. */private var engineAgent: AgentMode = AgentMode.MONITOR+ /**+ * True when the run in flight has surfaced an error bubble (LLM failure,+ * user interruption, …). Decides whether an arriving reply gets the normal+ * alert or the error buzz, since the engine reports both outcomes through+ * the same `onAssistantReply` path.+ */+ @Volatile+ private var runHadError = false+/** True when the session the user is viewing is the one bound to the engine. */private fun viewingEngineSession(): Boolean =_uiState.value.activeSessionId == agentEngine.sessionId@@ -219,6 +229,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A}override fun onAssistantReply(text: String) {+ signalReplyArrived()val shouldRead = lastInputWasVoice ||(settings.autoReadReplies && settings.ttsProvider != AudioProvider.NONE)if (shouldRead && settings.ttsProvider != AudioProvider.NONE) {@@ -371,6 +382,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), ArunningSessionTitle = runningTitle)}+ runHadError = falsetry {agentEngine.run(engineAgent)} catch (_: CancellationException) {@@ -394,6 +406,23 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A}}+ /**+ * Buzz/chime as a reply lands. The chat screen has no spoken "I'm done"+ * unless auto-read is on, so without this a reply that arrives while the+ * user is elsewhere goes unnoticed.+ */+ private fun signalReplyArrived() {+ if (runHadError) {+ CompletionFeedback.error(getApplication())+ } else {+ CompletionFeedback.replyArrived(+ context = getApplication(),+ vibrate = settings.notifyVibrationEnabled,+ chime = settings.notifyChimeEnabled+ )+ }+ }+/*** Point [agentEngine] at [viewedId] (the session being viewed) so a run* operates on it. Only called from [sendMessage], which is gated on nothing@@ -871,6 +900,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A) {val viewing = viewingEngineSession()val id = if (viewing) nextId++ else engineNextId+++ if (kind == MessageKind.ERROR) runHadError = trueval message = UiMessage(id, kind, text, imageBase64, subAgentSteps, subAgentCollapsed = true, reasoningContent)engineTranscript = engineTranscript + messageif (viewing) {diff --git a/app/src/main/java/com/gotcha/audio/CompletionFeedback.kt b/app/src/main/java/com/gotcha/audio/CompletionFeedback.ktnew file mode 100644index 0000000..b8a119d--- /dev/null+++ b/app/src/main/java/com/gotcha/audio/CompletionFeedback.kt@@ -0,0 +1,72 @@+package com.gotcha.audio++import android.content.Context+import android.media.AudioManager+import android.media.ToneGenerator+import android.os.Handler+import android.os.Looper+import android.os.VibrationEffect+import android.os.Vibrator++/**+ * Non-verbal "a reply just arrived" signals.+ *+ * A reply can land while the user is looking at another app — or, on a voice+ * call, not looking at anything — so the buzz pattern is often the only thing+ * that says the turn ended and whether it ended well. The two patterns are+ * therefore deliberately unalike: a reply is a short rising double buzz, an+ * error is a longer, even triple buzz.+ */+object CompletionFeedback {++ /** Rising two-pulse buzz: soft, then firm. Reads as "done", not "wrong". */+ private val REPLY_TIMINGS = longArrayOf(0, 40, 70, 110)++ /**+ * Paired with [REPLY_TIMINGS]. Devices without amplitude control play+ * every non-zero entry at their default strength, so the pattern still+ * differs from the error buzz by rhythm alone.+ */+ private val REPLY_AMPLITUDES = intArrayOf(0, 110, 0, 255)++ /** Longer, even triple buzz — an error should feel insistent, not tidy. */+ private val ERROR_TIMINGS = longArrayOf(0, 100, 80, 100, 80, 100)++ private const val CHIME_VOLUME = 70+ private const val CHIME_RELEASE_MS = 1_000L++ /**+ * Signal that a reply arrived. Both channels are opt-in per [vibrate] and+ * [chime]; with both off this is a no-op.+ */+ fun replyArrived(context: Context, vibrate: Boolean, chime: Boolean) {+ if (vibrate) {+ vibrator(context)?.vibrate(+ VibrationEffect.createWaveform(REPLY_TIMINGS, REPLY_AMPLITUDES, -1)+ )+ }+ if (chime) playChime()+ }++ /** Signal that the turn ended badly. Always vibrates — errors are not opt-out. */+ fun error(context: Context) {+ vibrator(context)?.vibrate(VibrationEffect.createWaveform(ERROR_TIMINGS, -1))+ }++ private fun vibrator(context: Context): Vibrator? =+ (context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator)?.takeIf { it.hasVibrator() }++ /**+ * Two-note ascending beep on the notification stream, so it follows the+ * user's notification volume and stays silent in Do Not Disturb. The+ * generator holds a native audio resource, hence the delayed release once+ * the tone has played out.+ */+ private fun playChime() {+ val tone = runCatching {+ ToneGenerator(AudioManager.STREAM_NOTIFICATION, CHIME_VOLUME)+ }.getOrNull() ?: return+ tone.startTone(ToneGenerator.TONE_PROP_BEEP2)+ Handler(Looper.getMainLooper()).postDelayed({ tone.release() }, CHIME_RELEASE_MS)+ }+}diff --git a/app/src/main/java/com/gotcha/data/SettingsRepository.kt b/app/src/main/java/com/gotcha/data/SettingsRepository.ktindex 9012e55..5ed2898 100644--- a/app/src/main/java/com/gotcha/data/SettingsRepository.kt+++ b/app/src/main/java/com/gotcha/data/SettingsRepository.kt@@ -51,6 +51,14 @@ data class Settings(val sttApiModel: String = "",val sttLanguage: String = "",val autoReadReplies: Boolean = false,+ /**+ * Buzz when a reply arrives. On by default: a reply can land while the user+ * is in another app, and the pattern is distinct from the error buzz so it+ * says *how* the turn ended, not just that it did.+ */+ val notifyVibrationEnabled: Boolean = true,+ /** Chime when a reply arrives. Off by default — audible in a way a buzz is not. */+ val notifyChimeEnabled: Boolean = false,val assistiveBallEnabled: Boolean = false,val themeMode: ThemeMode = ThemeMode.SYSTEM,val disabledSkills: Set<String> = emptySet(),@@ -215,6 +223,8 @@ class SettingsRepository(context: Context) {sttApiModel = prefs.getString(KEY_STT_API_MODEL, "") ?: "",sttLanguage = prefs.getString(KEY_STT_LANGUAGE, "") ?: "",autoReadReplies = prefs.getBoolean(KEY_AUTO_READ, false),+ notifyVibrationEnabled = prefs.getBoolean(KEY_NOTIFY_VIBRATION, true),+ notifyChimeEnabled = prefs.getBoolean(KEY_NOTIFY_CHIME, false),assistiveBallEnabled = prefs.getBoolean(KEY_ASSISTIVE_BALL, false),themeMode = runCatching {ThemeMode.valueOf(prefs.getString(KEY_THEME_MODE, "SYSTEM") ?: "SYSTEM")@@ -260,6 +270,8 @@ class SettingsRepository(context: Context) {.putString(KEY_STT_API_MODEL, settings.sttApiModel).putString(KEY_STT_LANGUAGE, settings.sttLanguage).putBoolean(KEY_AUTO_READ, settings.autoReadReplies)+ .putBoolean(KEY_NOTIFY_VIBRATION, settings.notifyVibrationEnabled)+ .putBoolean(KEY_NOTIFY_CHIME, settings.notifyChimeEnabled).putBoolean(KEY_ASSISTIVE_BALL, settings.assistiveBallEnabled).putString(KEY_THEME_MODE, settings.themeMode.name).putStringSet(KEY_DISABLED_SKILLS, settings.disabledSkills)@@ -319,6 +331,8 @@ class SettingsRepository(context: Context) {const val KEY_STT_API_MODEL = "stt_api_model"const val KEY_STT_LANGUAGE = "stt_language"const val KEY_AUTO_READ = "auto_read"+ const val KEY_NOTIFY_VIBRATION = "notify_vibration"+ const val KEY_NOTIFY_CHIME = "notify_chime"const val KEY_ASSISTIVE_BALL = "assistive_ball_enabled"const val KEY_THEME_MODE = "theme_mode"const val KEY_DISABLED_SKILLS = "disabled_skills"diff --git a/app/src/main/java/com/gotcha/service/CallSessionController.kt b/app/src/main/java/com/gotcha/service/CallSessionController.ktindex dcb0f1c..46d0744 100644--- a/app/src/main/java/com/gotcha/service/CallSessionController.kt+++ b/app/src/main/java/com/gotcha/service/CallSessionController.kt@@ -2,8 +2,6 @@ package com.gotcha.serviceimport android.content.Contextimport android.content.pm.PackageManager-import android.os.VibrationEffect-import android.os.Vibratorimport androidx.core.content.ContextCompatimport com.gotcha.agent.AgentEngineimport com.gotcha.agent.AgentEvents@@ -12,6 +10,7 @@ import com.gotcha.agent.PendingQuestionimport com.gotcha.agent.ScreenSnapshotimport com.gotcha.agent.friendlyAgentErrorimport com.gotcha.audio.AudioProvider+import com.gotcha.audio.CompletionFeedbackimport com.gotcha.audio.SttEngineimport com.gotcha.audio.TtsEngineimport com.gotcha.data.ChatHistoryRepository@@ -572,30 +571,17 @@ class CallSessionController(return phrases.last().first}+ /** Buzz/chime the turn's end, subject to the user's notification settings. */private fun triggerEndVibration() {- val vibrator = appContext.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator- if (vibrator?.hasVibrator() == true) {- vibrator.vibrate(- VibrationEffect.createWaveform(- longArrayOf(0, 50, 100, 50),- -1- )- )- }+ val s = settingsRepository.load()+ CompletionFeedback.replyArrived(+ context = appContext,+ vibrate = s.notifyVibrationEnabled,+ chime = s.notifyChimeEnabled+ )}- /** Distinct (longer, triple-buzz) pattern so an error doesn't feel like a normal turn end. */- private fun triggerErrorVibration() {- val vibrator = appContext.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator- if (vibrator?.hasVibrator() == true) {- vibrator.vibrate(- VibrationEffect.createWaveform(- longArrayOf(0, 100, 80, 100, 80, 100),- -1- )- )- }- }+ private fun triggerErrorVibration() = CompletionFeedback.error(appContext)companion object {private const val NARRATION_THROTTLE_MS = 3_000Ldiff --git a/app/src/main/java/com/gotcha/ui/SettingsScreen.kt b/app/src/main/java/com/gotcha/ui/SettingsScreen.ktindex d92f9a1..0b84083 100644--- a/app/src/main/java/com/gotcha/ui/SettingsScreen.kt+++ b/app/src/main/java/com/gotcha/ui/SettingsScreen.kt@@ -60,6 +60,7 @@ import com.gotcha.agent.skills.Skillimport com.gotcha.agent.skills.SkillRegistryimport com.gotcha.audio.AudioModelimport com.gotcha.audio.AudioProvider+import com.gotcha.audio.CompletionFeedbackimport com.gotcha.audio.VoiceInfoimport com.gotcha.data.LlmProviderimport com.gotcha.data.Settings@@ -98,6 +99,12 @@ fun SettingsScreen(onClearDebugScreenshots: () -> Unit,onBack: () -> Unit,onThemeChange: (ThemeMode) -> Unit = {},+ /**+ * Persists the reply-alert switches on the spot. Like [onThemeChange], and+ * unlike the section Save buttons, so a toggle here can't drag half-typed+ * edits from another section into storage with it.+ */+ onNotifyAlertChange: (vibration: Boolean, chime: Boolean) -> Unit = { _, _ -> },onRefreshAudioModels: suspend (Settings) -> Pair<List<AudioModel>, List<AudioModel>> = {Pair(emptyList(),@@ -147,6 +154,8 @@ fun SettingsScreen(var sttApiModel by remember { mutableStateOf(initial.sttApiModel) }var sttLanguage by remember { mutableStateOf(initial.sttLanguage) }var autoReadReplies by remember { mutableStateOf(initial.autoReadReplies) }+ var notifyVibration by remember { mutableStateOf(initial.notifyVibrationEnabled) }+ var notifyChime by remember { mutableStateOf(initial.notifyChimeEnabled) }var themeMode by remember { mutableStateOf(initial.themeMode) }var disabledSkills by remember { mutableStateOf(initial.disabledSkills) }var proactiveEnabled by remember { mutableStateOf(initial.proactiveEnabled) }@@ -232,6 +241,8 @@ fun SettingsScreen(sttApiModel = sttApiModel.trim(),sttLanguage = sttLanguage.trim(),autoReadReplies = autoReadReplies,+ notifyVibrationEnabled = notifyVibration,+ notifyChimeEnabled = notifyChime,assistiveBallEnabled = initial.assistiveBallEnabled,themeMode = themeMode,disabledSkills = disabledSkills,@@ -356,6 +367,52 @@ fun SettingsScreen(HorizontalDivider(thickness = 1.dp)+ // ---- Notifications (always visible, applies immediately) ----+ Text(+ "Notifications",+ style = MaterialTheme.typography.titleMedium,+ fontWeight = FontWeight.Bold+ )+ Text(+ "Played as soon as a reply arrives. Turn both off for no alert.",+ style = MaterialTheme.typography.bodySmall+ )+ // Switching one on plays it once, so the user knows what to expect.+ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.SpaceBetween,+ verticalAlignment = Alignment.CenterVertically+ ) {+ Text("Vibration", style = MaterialTheme.typography.bodyLarge)+ Switch(+ checked = notifyVibration,+ onCheckedChange = {+ notifyVibration = it+ onNotifyAlertChange(it, notifyChime)+ if (it) CompletionFeedback.replyArrived(localContext, vibrate = true, chime = false)+ },+ modifier = Modifier.testTag("settings_notify_vibration")+ )+ }+ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.SpaceBetween,+ verticalAlignment = Alignment.CenterVertically+ ) {+ Text("Chime", style = MaterialTheme.typography.bodyLarge)+ Switch(+ checked = notifyChime,+ onCheckedChange = {+ notifyChime = it+ onNotifyAlertChange(notifyVibration, it)+ if (it) CompletionFeedback.replyArrived(localContext, vibrate = false, chime = true)+ },+ modifier = Modifier.testTag("settings_notify_chime")+ )+ }++ HorizontalDivider(thickness = 1.dp)+// ---- AI Configuration (collapsible, collapsed by default) ----SectionHeader(title = "AI Configuration",diff --git a/app/src/test/java/com/gotcha/audio/CompletionFeedbackTest.kt b/app/src/test/java/com/gotcha/audio/CompletionFeedbackTest.ktnew file mode 100644index 0000000..c99887e--- /dev/null+++ b/app/src/test/java/com/gotcha/audio/CompletionFeedbackTest.kt@@ -0,0 +1,88 @@+package com.gotcha.audio++import android.content.Context+import android.os.Vibrator+import androidx.test.core.app.ApplicationProvider+import com.gotcha.data.Settings+import com.gotcha.data.SettingsRepository+import com.gotcha.testsupport.FakeAndroidKeyStore+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Before+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf+import org.robolectric.annotation.Config++/**+ * The reply alert is the only signal a user gets when they aren't watching the+ * screen, so both halves matter: that it fires, and that switching it off+ * actually silences it.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class CompletionFeedbackTest {++ private val context: Context = ApplicationProvider.getApplicationContext()++ private val vibrator: Vibrator+ get() = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator++ @Before+ fun setUp() {+ FakeAndroidKeyStore.setUp()+ vibrator.cancel()+ }++ @Test+ fun `replyArrived vibrates when the vibration alert is enabled`() {+ CompletionFeedback.replyArrived(context, vibrate = true, chime = false)+ assertTrue(shadowOf(vibrator).isVibrating)+ }++ @Test+ fun `replyArrived stays silent when both alerts are disabled`() {+ CompletionFeedback.replyArrived(context, vibrate = false, chime = false)+ assertFalse(shadowOf(vibrator).isVibrating)+ }++ @Test+ fun `error always vibrates regardless of the notification settings`() {+ CompletionFeedback.error(context)+ assertTrue(shadowOf(vibrator).isVibrating)+ }++ @Test+ fun `reply and error use different vibration patterns`() {+ CompletionFeedback.replyArrived(context, vibrate = true, chime = false)+ val replyMs = shadowOf(vibrator).milliseconds+ vibrator.cancel()+ CompletionFeedback.error(context)+ // Same-length buzzes would be indistinguishable without looking at the+ // screen, which is the entire point of the signal.+ assertTrue(shadowOf(vibrator).milliseconds != replyMs)+ }++ @Test+ fun `notification alert defaults to vibration only`() {+ val defaults = Settings()+ assertTrue(defaults.notifyVibrationEnabled)+ assertFalse(defaults.notifyChimeEnabled)+ }++ @Test+ fun `notification settings survive a save and load round-trip`() {+ val repo = SettingsRepository(context)+ repo.save(+ Settings(+ notifyVibrationEnabled = false,+ notifyChimeEnabled = true+ )+ )+ val loaded = repo.load()+ assertEquals(false, loaded.notifyVibrationEnabled)+ assertEquals(true, loaded.notifyChimeEnabled)+ }+}