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(chat): tell the user they can leave Gotcha while a run works (#96)
Durganshubce4affa9172
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.ktindex 3a2e795..05b4e56 100644--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt@@ -121,6 +121,22 @@ sealed class ComposerAttachment {*/internal val ATTACHMENT_PLACEHOLDERS = setOf("(image attached)", "(document attached)", "(files attached)")+/**+ * The "you can leave Gotcha" hint shown while a run works. It only promises the+ * signal the user will actually get: the buzz and chime are opt-in, and there is+ * no system notification yet (issue #103).+ */+internal fun backgroundHintText(vibrate: Boolean, chime: Boolean): String {+ val base = "Gotcha is working in the background. You can use another app while it works"+ val signal = when {+ vibrate && chime -> "your phone will buzz and chime when it's done"+ vibrate -> "your phone will buzz when it's done"+ chime -> "your phone will chime when it's done"+ else -> null+ }+ return if (signal == null) "$base." else "$base — $signal."+}+@kotlinx.serialization.Serializabledata class UiMessage(val id: Long,@@ -174,6 +190,12 @@ data class ChatUiState(val runningSessionId: String? = null,/** Title of the running session, for the "return to running chat" banner. */val runningSessionTitle: String? = null,+ /**+ * Informational hint that the user may leave Gotcha while the run works+ * (issue #96), or null when nothing is running. Lives only as long as the+ * run, never in the transcript, so returning to the app can't repeat it.+ */+ val backgroundHint: String? = null,val contextUsagePercent: Float = 0f,val tokenCount: Int = 0,val maxContextTokens: Int = 0,@@ -660,7 +682,11 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), Ait.copy(isBusy = true,runningSessionId = runningId,- runningSessionTitle = runningTitle+ runningSessionTitle = runningTitle,+ backgroundHint = backgroundHintText(+ vibrate = settings.notifyVibrationEnabled,+ chime = settings.notifyChimeEnabled+ ))}runHadError = false@@ -687,6 +713,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), AisBusy = false,runningSessionId = null,runningSessionTitle = null,+ backgroundHint = null,activity = if (viewingEngineSession()) null else it.activity,subAgentRunning = if (viewingEngineSession()) null else it.subAgentRunning,subAgentCurrentAction = if (viewingEngineSession()) null else it.subAgentCurrentActiondiff --git a/app/src/main/java/com/gotcha/ui/ChatScreen.kt b/app/src/main/java/com/gotcha/ui/ChatScreen.ktindex 8f97eb4..40c7ce9 100644--- a/app/src/main/java/com/gotcha/ui/ChatScreen.kt+++ b/app/src/main/java/com/gotcha/ui/ChatScreen.kt@@ -38,6 +38,7 @@ import androidx.compose.material.icons.filled.Micimport androidx.compose.material.icons.filled.Shareimport androidx.compose.material.icons.filled.Stopimport androidx.compose.material.icons.filled.TouchApp+import androidx.compose.material.icons.outlined.Infoimport androidx.compose.material.icons.outlined.Visibilityimport androidx.compose.material3.Buttonimport androidx.compose.material3.ButtonDefaults@@ -407,6 +408,28 @@ fun ChatScreen(}}+ // Once per run: the user may leave Gotcha while it works (issue #96).+ // From another chat the banner below says so instead.+ if (state.backgroundHint != null && !otherChatRunning) {+ Row(+ modifier = Modifier.padding(horizontal = 18.dp, vertical = 4.dp),+ verticalAlignment = Alignment.CenterVertically+ ) {+ Icon(+ Icons.Outlined.Info,+ contentDescription = null,+ modifier = Modifier.size(16.dp),+ tint = MaterialTheme.colorScheme.onSurfaceVariant+ )+ Spacer(modifier = Modifier.width(10.dp))+ Text(+ state.backgroundHint,+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant+ )+ }+ }+if (state.subAgentRunning != null) {Column(modifier = Modifier@@ -469,7 +492,7 @@ fun ChatScreen()Spacer(modifier = Modifier.width(8.dp))Text(- "⚡ Agent working in “${state.runningSessionTitle ?: "another chat"}” — tap to return",+ "⚡ Agent working in “${state.runningSessionTitle ?: "another chat"}” — you can leave Gotcha; tap to return",style = MaterialTheme.typography.bodySmall,color = MaterialTheme.colorScheme.onSecondaryContainer,maxLines = 1,diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.ktnew file mode 100644index 0000000..b2d7953--- /dev/null+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.kt@@ -0,0 +1,127 @@+package com.gotcha.agent++import android.app.Application+import androidx.test.core.app.ApplicationProvider+import com.gotcha.data.LlmProvider+import com.gotcha.data.Settings+import com.gotcha.data.SettingsRepository+import com.gotcha.llm.ChatMessage+import com.gotcha.testsupport.FakeAndroidKeyStore+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.Job+import kotlinx.coroutines.cancel+import kotlinx.coroutines.launch+import org.junit.After+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertNull+import org.junit.Before+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import org.robolectric.shadows.ShadowLooper++/**+ * The "you can leave Gotcha" hint (issue #96): shown once per submitted request,+ * gone when the run ends, and never part of the transcript or the LLM history.+ * The LLM base URL refuses connections so each run fails fast and stays hermetic.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class ChatViewModelBackgroundHintTest {++ private val application: Application = ApplicationProvider.getApplicationContext()+ private lateinit var viewModel: ChatViewModel+ private val scope = CoroutineScope(Dispatchers.Unconfined + Job())++ /** Every hint value the state published, in order, with repeats collapsed. */+ private val hints = mutableListOf<String?>()++ @Before+ fun setUp() {+ FakeAndroidKeyStore.setUp()+ SettingsRepository(application).save(+ Settings(+ provider = LlmProvider.OPENAI_COMPATIBLE,+ apiKey = "test-key",+ baseUrl = "http://127.0.0.1:1/v1",+ notifyVibrationEnabled = true,+ notifyChimeEnabled = false+ )+ )+ viewModel = ChatViewModel(application)+ ShadowLooper.idleMainLooper()+ scope.launch {+ viewModel.uiState.collect { state ->+ if (hints.isEmpty() || hints.last() != state.backgroundHint) hints += state.backgroundHint+ }+ }+ }++ @After+ fun tearDown() = scope.cancel()++ private fun waitForRunToFinish() {+ val deadline = System.currentTimeMillis() + 5_000+ ShadowLooper.idleMainLooper()+ while (System.currentTimeMillis() < deadline && viewModel.uiState.value.isBusy) {+ ShadowLooper.idleMainLooper()+ Thread.sleep(10)+ }+ ShadowLooper.idleMainLooper()+ }++ private fun engineHistory(): List<ChatMessage> {+ val field = ChatViewModel::class.java.getDeclaredField("agentEngine")+ .apply { isAccessible = true }+ return (field.get(viewModel) as AgentEngine).history.toList()+ }++ private val expectedHint = backgroundHintText(vibrate = true, chime = false)++ @Test+ fun `hint promises only the signals the user has enabled`() {+ val base = "Gotcha is working in the background. You can use another app while it works"+ assertEquals("$base — your phone will buzz when it's done.", backgroundHintText(vibrate = true, chime = false))+ assertEquals("$base — your phone will chime when it's done.", backgroundHintText(vibrate = false, chime = true))+ assertEquals(+ "$base — your phone will buzz and chime when it's done.",+ backgroundHintText(vibrate = true, chime = true)+ )+ assertEquals("$base.", backgroundHintText(vibrate = false, chime = false))+ }++ @Test+ fun `hint shows while a run works and clears when it ends`() {+ viewModel.sendMessage("Hello")+ waitForRunToFinish()++ assertEquals(listOf(null, expectedHint, null), hints)+ assertNull(viewModel.uiState.value.backgroundHint)+ }++ @Test+ fun `hint appears once per submitted request`() {+ viewModel.sendMessage("First")+ waitForRunToFinish()+ viewModel.sendMessage("Second")+ waitForRunToFinish()++ assertEquals(2, hints.count { it == expectedHint })+ }++ @Test+ fun `hint never enters the transcript or the LLM history`() {+ viewModel.sendMessage("Hello")+ waitForRunToFinish()+ // Leaving and returning to the app must not add anything either.+ viewModel.setForeground(false)+ viewModel.setForeground(true)+ ShadowLooper.idleMainLooper()++ assertFalse(viewModel.uiState.value.messages.any { it.text.contains("working in the background") })+ assertFalse(engineHistory().any { it.textContent.contains("working in the background") })+ }+}