Skip to content

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.

fix(settings): preserve language state across config changes and handle launch errors

Jalebi213f9a3a463d
diff --git a/app/src/main/java/com/gotcha/ui/LanguageScreen.kt b/app/src/main/java/com/gotcha/ui/LanguageScreen.kt
index d72389e..61140fd 100644
--- a/app/src/main/java/com/gotcha/ui/LanguageScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/LanguageScreen.kt
@@ -1,8 +1,10 @@
package com.gotcha.ui
+import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
+import android.widget.Toast
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenuItem
@@ -20,6 +22,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
@@ -63,11 +66,11 @@ fun LanguageScreen(
onTestVoice: suspend (Language) -> Boolean? = { null }
) {
val initial = remember { load() }
- var preferredLanguage by remember { mutableStateOf(initial.preferredLanguage) }
- var voiceLanguage by remember { mutableStateOf(initial.voiceLanguage) }
+ var preferredLanguage by rememberSaveable { mutableStateOf(initial.preferredLanguage) }
+ var voiceLanguage by rememberSaveable { mutableStateOf(initial.voiceLanguage) }
- var replyExpanded by remember { mutableStateOf(false) }
- var voiceExpanded by remember { mutableStateOf(false) }
+ var replyExpanded by rememberSaveable { mutableStateOf(false) }
+ var voiceExpanded by rememberSaveable { mutableStateOf(false) }
var testingVoice by remember { mutableStateOf(false) }
/** Last [Language] whose voice data was reported missing, or null when not shown. */
@@ -214,7 +217,13 @@ fun LanguageScreen(
android.speech.tts.TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
- } catch (_: Exception) { }
+ } catch (_: Exception) {
+ Toast.makeText(
+ localContext,
+ "Could not open text-to-speech settings.",
+ Toast.LENGTH_SHORT
+ ).show()
+ }
}
) { Text("Install") }
},
@@ -286,10 +295,10 @@ fun LanguageScreen(
/**
* Open the per-app language screen where the platform has one (API 33+), falling
* back to the device-wide language list. Both are ordinary system activities that
- * a heavily-skinned OEM build may simply not have, hence the catch — the same
- * shape as the TTS-data launch above.
+ * a heavily-skinned OEM build may simply not have; if neither activity resolves,
+ * a toast informs the user.
*/
-private fun openLanguageSettings(context: android.content.Context) {
+internal fun openLanguageSettings(context: Context) {
val intents = buildList {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
add(
@@ -305,4 +314,9 @@ private fun openLanguageSettings(context: android.content.Context) {
return
} catch (_: Exception) { }
}
+ Toast.makeText(
+ context,
+ "Could not open language settings.",
+ Toast.LENGTH_SHORT
+ ).show()
}
diff --git a/app/src/test/java/com/gotcha/ui/LanguageScreenTest.kt b/app/src/test/java/com/gotcha/ui/LanguageScreenTest.kt
new file mode 100644
index 0000000..523e8c2
--- /dev/null
+++ b/app/src/test/java/com/gotcha/ui/LanguageScreenTest.kt
@@ -0,0 +1,55 @@
+package com.gotcha.ui
+
+import android.content.Context
+import android.content.ContextWrapper
+import android.content.Intent
+import android.net.Uri
+import androidx.test.core.app.ApplicationProvider
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.Shadows
+import org.robolectric.annotation.Config
+import org.robolectric.shadows.ShadowToast
+import android.provider.Settings as AndroidSettings
+
+@RunWith(RobolectricTestRunner::class)
+class LanguageScreenTest {
+
+ private val context: Context = ApplicationProvider.getApplicationContext()
+
+ @Test
+ @Config(sdk = [34])
+ fun `openLanguageSettings targets per-app locale settings on API 34`() {
+ openLanguageSettings(context)
+ val intent = Shadows.shadowOf(context as android.app.Application).nextStartedActivity
+ assertNotNull(intent)
+ assertEquals(AndroidSettings.ACTION_APP_LOCALE_SETTINGS, intent.action)
+ assertEquals(Uri.parse("package:${context.packageName}"), intent.data)
+ assertTrue(intent.flags and Intent.FLAG_ACTIVITY_NEW_TASK != 0)
+ }
+
+ @Test
+ @Config(sdk = [30])
+ fun `openLanguageSettings falls back to device locale settings on API 30`() {
+ openLanguageSettings(context)
+ val intent = Shadows.shadowOf(context as android.app.Application).nextStartedActivity
+ assertNotNull(intent)
+ assertEquals(AndroidSettings.ACTION_LOCALE_SETTINGS, intent.action)
+ assertTrue(intent.flags and Intent.FLAG_ACTIVITY_NEW_TASK != 0)
+ }
+
+ @Test
+ fun `openLanguageSettings shows toast when activities cannot be launched`() {
+ val failingContext = object : ContextWrapper(context) {
+ override fun startActivity(intent: Intent?) {
+ throw android.content.ActivityNotFoundException("Activity not found")
+ }
+ }
+ openLanguageSettings(failingContext)
+ assertEquals("Could not open language settings.", ShadowToast.getTextOfLatestToast())
+ }
+}