|
1 | 1 | package com.x8bit.bitwarden
|
2 | 2 |
|
3 | 3 | import android.content.Intent
|
| 4 | +import android.os.Build |
4 | 5 | import android.os.Bundle
|
5 | 6 | import android.view.KeyEvent
|
6 | 7 | import android.view.MotionEvent
|
@@ -28,12 +29,14 @@ import com.x8bit.bitwarden.ui.platform.feature.debugmenu.manager.DebugMenuLaunch
|
28 | 29 | import com.x8bit.bitwarden.ui.platform.feature.debugmenu.navigateToDebugMenuScreen
|
29 | 30 | import com.x8bit.bitwarden.ui.platform.feature.rootnav.RootNavScreen
|
30 | 31 | import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
| 32 | +import com.x8bit.bitwarden.ui.platform.util.appLanguage |
31 | 33 | import dagger.hilt.android.AndroidEntryPoint
|
32 | 34 | import javax.inject.Inject
|
33 | 35 |
|
34 | 36 | /**
|
35 | 37 | * Primary entry point for the application.
|
36 | 38 | */
|
| 39 | +@Suppress("TooManyFunctions") |
37 | 40 | @OmitFromCoverage
|
38 | 41 | @AndroidEntryPoint
|
39 | 42 | class MainActivity : AppCompatActivity() {
|
@@ -69,13 +72,9 @@ class MainActivity : AppCompatActivity() {
|
69 | 72 | )
|
70 | 73 | }
|
71 | 74 |
|
72 |
| - // Within the app the language and theme will change dynamically and will be managed by the |
| 75 | + // Within the app the theme will change dynamically and will be managed by the |
73 | 76 | // OS, but we need to ensure we properly set the values when upgrading from older versions
|
74 | 77 | // that handle this differently or when the activity restarts.
|
75 |
| - settingsRepository.appLanguage.localeName?.let { localeName -> |
76 |
| - val localeList = LocaleListCompat.forLanguageTags(localeName) |
77 |
| - AppCompatDelegate.setApplicationLocales(localeList) |
78 |
| - } |
79 | 78 | AppCompatDelegate.setDefaultNightMode(settingsRepository.appTheme.osValue)
|
80 | 79 | setContent {
|
81 | 80 | val state by mainViewModel.stateFlow.collectAsStateWithLifecycle()
|
@@ -140,6 +139,31 @@ class MainActivity : AppCompatActivity() {
|
140 | 139 | )
|
141 | 140 | }
|
142 | 141 |
|
| 142 | + override fun onResume() { |
| 143 | + super.onResume() |
| 144 | + // When the app resumes check for any app specific language which may have been |
| 145 | + // set via the device settings. Similar to the theme setting in onCreate this |
| 146 | + // ensures we properly set the values when upgrading from older versions |
| 147 | + // that handle this differently or when the activity restarts. |
| 148 | + val appSpecificLanguage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 149 | + val locales: LocaleListCompat = AppCompatDelegate.getApplicationLocales() |
| 150 | + if (locales.isEmpty) { |
| 151 | + // App is using the system language |
| 152 | + null |
| 153 | + } else { |
| 154 | + // App has specific language settings |
| 155 | + locales.get(0)?.appLanguage |
| 156 | + } |
| 157 | + } else { |
| 158 | + // For older versions, use what ever language is available from the repository. |
| 159 | + settingsRepository.appLanguage |
| 160 | + } |
| 161 | + |
| 162 | + appSpecificLanguage?.let { |
| 163 | + mainViewModel.trySendAction(MainAction.AppSpecificLanguageUpdate(it)) |
| 164 | + } |
| 165 | + } |
| 166 | + |
143 | 167 | override fun onStop() {
|
144 | 168 | super.onStop()
|
145 | 169 | // In some scenarios on an emulator the Activity can leak when recreated
|
|
0 commit comments