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): "Open Android language settings" opens the device list (#112)
Durganshu78329dc915bb
diff --git a/app/src/main/java/com/gotcha/ui/LanguageScreen.kt b/app/src/main/java/com/gotcha/ui/LanguageScreen.ktindex 7f9ad3b..1293d18 100644--- a/app/src/main/java/com/gotcha/ui/LanguageScreen.kt+++ b/app/src/main/java/com/gotcha/ui/LanguageScreen.kt@@ -1,5 +1,6 @@package com.gotcha.ui+import android.app.LocaleConfigimport android.content.Contextimport android.content.Intentimport android.net.Uri@@ -293,14 +294,22 @@ 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; if neither activity resolves,- * a toast informs the user.+ * Open the per-app language screen where it can work, otherwise the device-wide+ * language list. If neither activity can be started, a toast says so.+ *+ * Android 13+ shows the per-app screen only for apps that declare their locales+ * (a `LocaleConfig`). Gotcha declares none, because its UI is English only, so+ * Settings refuses the screen. Stock builds still draw a picker whose choices do+ * nothing, and some OEM builds (Nothing OS) close it at once: a blank flash, and+ * no exception for us to fall back on (issue #112). So the per-app screen is only+ * tried when [appHasLocales] says the app declares more than one locale.*/-internal fun openLanguageSettings(context: Context) {+internal fun openLanguageSettings(+ context: Context,+ appHasLocales: Boolean = declaresAppLocales(context)+) {val intents = buildList {- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && appHasLocales) {add(Intent(AndroidSettings.ACTION_APP_LOCALE_SETTINGS).setData(Uri.fromParts("package", context.packageName, null))@@ -320,3 +329,13 @@ internal fun openLanguageSettings(context: Context) {Toast.LENGTH_SHORT).show()}++/** True when the app's `LocaleConfig` lists more than one locale (API 33+). */+private fun declaresAppLocales(context: Context): Boolean {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return false+ return try {+ (LocaleConfig(context).supportedLocales?.size() ?: 0) > 1+ } catch (_: Exception) {+ false+ }+}diff --git a/app/src/test/java/com/gotcha/ui/LanguageScreenTest.kt b/app/src/test/java/com/gotcha/ui/LanguageScreenTest.ktindex 523e8c2..c80bfb2 100644--- a/app/src/test/java/com/gotcha/ui/LanguageScreenTest.kt+++ b/app/src/test/java/com/gotcha/ui/LanguageScreenTest.kt@@ -23,8 +23,8 @@ class LanguageScreenTest {@Test@Config(sdk = [34])- fun `openLanguageSettings targets per-app locale settings on API 34`() {- openLanguageSettings(context)+ fun `openLanguageSettings targets per-app locale settings on API 34 when the app declares locales`() {+ openLanguageSettings(context, appHasLocales = true)val intent = Shadows.shadowOf(context as android.app.Application).nextStartedActivityassertNotNull(intent)assertEquals(AndroidSettings.ACTION_APP_LOCALE_SETTINGS, intent.action)@@ -32,10 +32,29 @@ class LanguageScreenTest {assertTrue(intent.flags and Intent.FLAG_ACTIVITY_NEW_TASK != 0)}+ @Test+ @Config(sdk = [34])+ fun `openLanguageSettings opens device locale settings on API 34 when the app declares no locales`() {+ // Issue #112: without a LocaleConfig the per-app screen closes at once on some OEM builds.+ openLanguageSettings(context, appHasLocales = false)+ val intent = Shadows.shadowOf(context as android.app.Application).nextStartedActivity+ assertNotNull(intent)+ assertEquals(AndroidSettings.ACTION_LOCALE_SETTINGS, intent.action)+ }++ @Test+ @Config(sdk = [34])+ fun `openLanguageSettings defaults to device locale settings for Gotcha`() {+ openLanguageSettings(context)+ val intent = Shadows.shadowOf(context as android.app.Application).nextStartedActivity+ assertNotNull(intent)+ assertEquals(AndroidSettings.ACTION_LOCALE_SETTINGS, intent.action)+ }+@Test@Config(sdk = [30])fun `openLanguageSettings falls back to device locale settings on API 30`() {- openLanguageSettings(context)+ openLanguageSettings(context, appHasLocales = true)val intent = Shadows.shadowOf(context as android.app.Application).nextStartedActivityassertNotNull(intent)assertEquals(AndroidSettings.ACTION_LOCALE_SETTINGS, intent.action)@@ -49,7 +68,7 @@ class LanguageScreenTest {throw android.content.ActivityNotFoundException("Activity not found")}}- openLanguageSettings(failingContext)+ openLanguageSettings(failingContext, appHasLocales = true)assertEquals("Could not open language settings.", ShadowToast.getTextOfLatestToast())}}