diff --git a/app/src/main/java/com/droiddeck/launcher/App.kt b/app/src/main/java/com/droiddeck/launcher/App.kt index bbaa0a813..427195646 100644 --- a/app/src/main/java/com/droiddeck/launcher/App.kt +++ b/app/src/main/java/com/droiddeck/launcher/App.kt @@ -11,6 +11,14 @@ import com.droiddeck.launcher.session.GpuClockPin * sandboxes are started through (BwrapSpawner). */ class App : Application() { + override fun attachBaseContext(base: android.content.Context) = + super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(base)) + + override fun onConfigurationChanged(newConfig: android.content.res.Configuration) { + super.onConfigurationChanged(newConfig) + com.droiddeck.launcher.core.AppLanguage.systemChanged(newConfig) + } + override fun onCreate() { super.onCreate() CrashHandler.install(this) diff --git a/app/src/main/java/com/droiddeck/launcher/HomeActivity.kt b/app/src/main/java/com/droiddeck/launcher/HomeActivity.kt index 14921e1b3..74c14f964 100644 --- a/app/src/main/java/com/droiddeck/launcher/HomeActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/HomeActivity.kt @@ -7,6 +7,11 @@ import com.droiddeck.launcher.session.SessionService /** Receives the system Home launch and routes it without consuming KEYCODE_HOME. */ class HomeActivity : Activity() { + override fun attachBaseContext(newBase: android.content.Context) { + super.attachBaseContext(newBase) + com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) routeHome() diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index e2e82449c..2822388cd 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -81,6 +81,11 @@ import com.droiddeck.launcher.input.SecondScreenDisplays * the store, downloads, settings - is the client's own job once [SessionActivity] has it on screen. */ class MainActivity : ComponentActivity() { + override fun attachBaseContext(newBase: android.content.Context) { + super.attachBaseContext(newBase) + com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) + } + private val ui = Handler(Looper.getMainLooper()) private val drivers = DriverMenus(this, ui) private val components = ComponentsMenu(this, ui) @@ -518,6 +523,7 @@ class MainActivity : ComponentActivity() { lossless = lossless, pageKey = sm?.let { "settings:$it" } ?: if (showPerformance) "performance" else if (showProtons) "protons" else if (showComponents) "components" else if (showMapping) "controller-mapping" else null, theme = theme, + language = com.droiddeck.launcher.core.AppLanguage.chosen(this), isHomeApp = homeAppSelected, homeScreenEnabled = homeScreenEnabled, defaultHomeLabel = defaultHomeLabel, @@ -647,6 +653,13 @@ class MainActivity : ComponentActivity() { }, onPageBack = { settingsMode = null; showPerformance = false; showProtons = false; showComponents = false; showMapping = false }, onTheme = { id -> SessionPrefs.setTheme(this, id); theme = id }, + onLanguage = { tag -> + if (tag != com.droiddeck.launcher.core.AppLanguage.chosen(this)) { + com.droiddeck.launcher.core.AppLanguage.setChosen(this, tag) + // Redrawn in the new language; Setup's page and tab are saved state. + recreate() + } + }, onLauncherFullscreen = { on -> SessionPrefs.setLauncherFullscreen(this, on) launcherFullscreen = on diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index abbe696e9..3811eddfb 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -98,6 +98,11 @@ import kotlin.math.abs * the HUD line, the loading overlay, the drawer and its dialogs. */ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { + override fun attachBaseContext(newBase: android.content.Context) { + super.attachBaseContext(newBase) + com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) + } + private lateinit var pip: com.droiddeck.launcher.session.SessionPipController private var pipUi by mutableStateOf(false) private var pipAutoEnter by mutableStateOf(false) diff --git a/app/src/main/java/com/droiddeck/launcher/core/AppLanguage.kt b/app/src/main/java/com/droiddeck/launcher/core/AppLanguage.kt new file mode 100644 index 000000000..95ecdc73f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/AppLanguage.kt @@ -0,0 +1,111 @@ +package com.droiddeck.launcher.core + +import android.app.Activity +import android.content.Context +import android.content.res.Configuration +import android.os.LocaleList +import java.util.Locale + +/** + * The language the app shows itself in. + * + * Nothing chosen (the default) means the system's: Android's own language, or the per-app language + * Android 13+ lets a user set for DroidDeck in its settings, exactly as Android resolves it. Picking + * one in Setup overrides that until the user picks "System default" again. Each activity and service + * applies the choice to its own context ([wrap], [applyTo]), so it works on every Android version + * the app runs on rather than only where the platform's per-app language API exists. + * + * Steam follows the same choice: [SteamLanguage] turns [effective] into the client's language. + */ +object AppLanguage { + /** The stored value meaning "follow the system". */ + const val SYSTEM = "" + + /** The languages the app ships, as BCP 47 tags, in the order the picker lists them. */ + val supported: List = listOf("en", "es", "ja", "ko", "zh-CN", "zh-TW", "zh-HK") + + private const val PREFS = "language" + private const val KEY = "app_language" + + /** The locale Android gave the process before any choice here was applied. */ + @Volatile private var systemLocale: Locale? = null + + /** The stored choice: one of [supported], or [SYSTEM]. */ + fun chosen(context: Context): String = + context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY, SYSTEM) + ?.takeIf { it in supported } ?: SYSTEM + + fun setChosen(context: Context, tag: String) { + val value = if (tag in supported) tag else SYSTEM + context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putString(KEY, value).commit() + val locale = value.takeIf { it.isNotEmpty() }?.let(Locale::forLanguageTag) ?: system(context) + Locale.setDefault(locale) + // Code that reads strings through the application context sees the new language too. + val app = context.applicationContext + @Suppress("DEPRECATION") + app.resources.updateConfiguration( + Configuration(app.resources.configuration).apply { setLocales(LocaleList(locale)) }, + app.resources.displayMetrics, + ) + } + + /** The locale Android chose for the app, ignoring the choice made here. */ + fun system(context: Context): Locale = + systemLocale ?: context.resources.configuration.locales[0] ?: Locale.getDefault() + + /** The locale the app is shown in: the chosen language, or else the system's. */ + fun effective(context: Context): Locale = + chosen(context).takeIf { it.isNotEmpty() }?.let(Locale::forLanguageTag) ?: system(context) + + /** Remembers the system's locale; call with the untouched base context, before [wrap]. */ + fun noteSystem(base: Context) { + if (systemLocale == null) systemLocale = base.resources.configuration.locales[0] + } + + /** + * [base], showing the chosen language: for an Application's or a Service's attachBaseContext. + * Only the locale is overridden, so screen size, density and night mode keep following the + * device as they change. + */ + fun wrap(base: Context): Context { + noteSystem(base) + val tag = chosen(base).ifEmpty { return base } + val locale = Locale.forLanguageTag(tag) + Locale.setDefault(locale) + return base.createConfigurationContext(override(locale)) + } + + /** + * Applies the chosen language to an activity; call from its attachBaseContext, after super. + * applyOverrideConfiguration keeps the locale through rotation and window resizes, which an + * activity here handles itself rather than being recreated. + */ + fun applyTo(activity: Activity, base: Context) { + noteSystem(base) + val tag = chosen(base).ifEmpty { return } + val locale = Locale.forLanguageTag(tag) + Locale.setDefault(locale) + activity.applyOverrideConfiguration(override(locale)) + } + + /** Follows a change of the system's language while the process lives (Application.onConfigurationChanged). */ + fun systemChanged(config: Configuration) { + config.locales[0]?.let { systemLocale = it } + } + + // A new Configuration's fontScale is 1, which as an override would undo the user's font size; + // 0 leaves it undefined, so only the locale is changed. + private fun override(locale: Locale) = Configuration().apply { fontScale = 0f; setLocales(LocaleList(locale)) } + + /** A language's name in that language, for the picker: what a reader of it looks for. */ + fun nativeName(tag: String): String = when (tag) { + "en" -> "English" + "es" -> "Español" + "ja" -> "日本語" + "ko" -> "한국어" + "zh-CN" -> "简体中文" + "zh-TW" -> "繁體中文(台灣)" + "zh-HK" -> "繁體中文(香港)" + else -> Locale.forLanguageTag(tag).let { it.getDisplayName(it) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt b/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt index d4645962a..f4a005086 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/WirelessAdbPairingService.kt @@ -20,6 +20,9 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow class WirelessAdbPairingService : Service() { + override fun attachBaseContext(newBase: android.content.Context) = + super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(newBase)) + sealed interface Stage { data object Idle : Stage data object Waiting : Stage diff --git a/app/src/main/java/com/droiddeck/launcher/files/FileManagerActivity.kt b/app/src/main/java/com/droiddeck/launcher/files/FileManagerActivity.kt index 88c2462ed..4a08bcb51 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/FileManagerActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/FileManagerActivity.kt @@ -15,6 +15,11 @@ import java.io.File /** The File Manager as a screen of its own, from the Files tile on the main screen. */ class FileManagerActivity : ComponentActivity() { + override fun attachBaseContext(newBase: android.content.Context) { + super.attachBaseContext(newBase) + com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { diff --git a/app/src/main/java/com/droiddeck/launcher/files/FilePickerActivity.kt b/app/src/main/java/com/droiddeck/launcher/files/FilePickerActivity.kt index d892c1241..cf8b0770d 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/FilePickerActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/FilePickerActivity.kt @@ -26,6 +26,11 @@ import java.io.File * - [EXTRA_PICKER_TITLE] the header */ class FilePickerActivity : ComponentActivity() { + override fun attachBaseContext(newBase: android.content.Context) { + super.attachBaseContext(newBase) + com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt b/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt index c94b5cf9c..8b49a4a73 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt @@ -29,6 +29,11 @@ import com.droiddeck.launcher.ui.onSignal import com.droiddeck.launcher.ui.LocalPalette class ControllerEditorActivity : ComponentActivity() { + override fun attachBaseContext(newBase: android.content.Context) { + super.attachBaseContext(newBase) + com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) + } + private lateinit var controls: OnScreenControls override fun onCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt index 356dada46..9eb0ff81e 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt @@ -22,6 +22,9 @@ import com.droiddeck.launcher.R * two gets there first. */ class RuntimeInstallService : Service() { + override fun attachBaseContext(newBase: android.content.Context) = + super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(newBase)) + override fun onBind(intent: Intent?): IBinder? = null override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 584c2c380..4220f08fe 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -59,6 +59,9 @@ import java.util.Locale * The activity comes and goes on top of this; see [com.droiddeck.launcher.wayland.CompositorHost]. */ class SessionService : Service() { + override fun attachBaseContext(newBase: android.content.Context) = + super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(newBase)) + private val components = java.util.concurrent.CopyOnWriteArrayList() /** The Steam Deck controller's sysfs binds (SteamDeckPad), when this session has one. */ private var deckBinds: List = emptyList() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt index d8f8b2275..0269f0e80 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt @@ -101,6 +101,8 @@ class FrontEndState( val gameSyncFolder: String? = null, val pageKey: String? = null, val theme: String = Themes.GRAPHITE, + /** The language picked in Setup, or AppLanguage.SYSTEM to follow the system. */ + val language: String = "", val isHomeApp: Boolean = false, val homeScreenEnabled: Boolean = false, val defaultHomeLabel: String? = null, @@ -168,6 +170,7 @@ class FrontEndActions( val onOffline: () -> Unit, val onPageBack: () -> Unit = {}, val onTheme: (String) -> Unit = {}, + val onLanguage: (String) -> Unit = {}, val onLauncherFullscreen: (Boolean) -> Unit = {}, val onAnimationsEnabled: (Boolean) -> Unit = {}, val onStoreEnabled: (Boolean) -> Unit = {}, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt index c404dab6f..0fa5e6e70 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt @@ -71,6 +71,7 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.core.AppLanguage import com.droiddeck.launcher.core.DeviceSupport import com.droiddeck.launcher.core.PhantomProcessLimit import com.droiddeck.launcher.core.PhantomProcessStatus @@ -314,6 +315,7 @@ internal fun SetupPanel( } 3 -> { SettingsGroup(stringResource(R.string.setup_launcher)) { + LanguageRow(host, s.language, a.onLanguage) SettingsRow(stringResource(R.string.setup_theme), stringResource(R.string.setup_theme_hint)) { Box { ValueChip(Themes.byId(s.theme).label, host.open == "theme") { host.open = if (host.open == "theme") null else "theme" } @@ -464,3 +466,32 @@ private fun SettingCard(label: String, value: String, id: String, modifier: Modi Text(value, fontSize = 16.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) } } + +/** + * The app's language: "System default" (what Android uses, named in brackets) or one of the + * languages the app ships, each in its own script. Steam is set to the same language when it starts. + */ +@Composable +private fun LanguageRow(host: MenuHost, chosen: String, onPick: (String) -> Unit) { + val context = androidx.compose.ui.platform.LocalContext.current + val systemName = AppLanguage.system(context).let { it.getDisplayName(it) } + val systemLabel = stringResource(R.string.setup_language_system, systemName) + SettingsRow(stringResource(R.string.setup_language), stringResource(R.string.setup_language_hint)) { + Box { + ValueChip(if (chosen == AppLanguage.SYSTEM) stringResource(R.string.setup_language_system_short) else AppLanguage.nativeName(chosen), + host.open == "language") { host.open = if (host.open == "language") null else "language" } + AnchoredMenu(host.open == "language", onDismiss = { if (host.open == "language") host.open = null }, title = stringResource(R.string.setup_language)) { firstItemFocus -> + MenuItem(systemLabel, checked = chosen == AppLanguage.SYSTEM, focusRequester = firstItemFocus) { + host.open = null + onPick(AppLanguage.SYSTEM) + } + AppLanguage.supported.forEach { tag -> + MenuItem(AppLanguage.nativeName(tag), checked = chosen == tag) { + host.open = null + onPick(tag) + } + } + } + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index be062ba1b..8103d2c4b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -590,6 +590,10 @@ Offline mode Sign in to Steam first Launcher + Language + Steam uses it too, from the next time it starts. + System default (%1$s) + System default Theme Launcher appearance Use as Home screen diff --git a/app/src/main/res/xml/locales_config.xml b/app/src/main/res/xml/locales_config.xml index 65a8220b2..7c0b9212b 100644 --- a/app/src/main/res/xml/locales_config.xml +++ b/app/src/main/res/xml/locales_config.xml @@ -1,7 +1,12 @@ - + - + + + + +