|
1 | 1 | package org.wordpress.android.ui.prefs.language
|
2 | 2 |
|
3 | 3 | import android.content.Context
|
| 4 | +import android.content.Intent |
| 5 | +import android.net.Uri |
| 6 | +import android.provider.Settings |
4 | 7 | import androidx.appcompat.app.AppCompatDelegate
|
5 | 8 | import androidx.core.os.LocaleListCompat
|
6 | 9 | import org.wordpress.android.R
|
@@ -57,22 +60,47 @@ class LocaleHelper @Inject constructor(
|
57 | 60 | * Previously the app locale was stored in SharedPreferences, so here we migrate to AndroidX per-app language prefs
|
58 | 61 | */
|
59 | 62 | fun performMigrationIfNecessary(context: Context) {
|
60 |
| - if (isPerAppLanguagePrefsEnabled(context) && isApplicationLocaleEmpty()) { |
| 63 | + if (isPerAppLanguagePrefsEnabled() && isApplicationLocaleEmpty()) { |
61 | 64 | val languagePrefKey = context.getString(R.string.pref_key_language)
|
62 | 65 | val previousLanguage = appPrefsWrapper.prefs().getString(languagePrefKey, "")
|
63 | 66 | if (previousLanguage?.isNotEmpty() == true) {
|
64 |
| - appLogWrapper.d(AppLog.T.SETTINGS, "LocaleHelper: performing migration to AndroidX per-app language prefs") |
| 67 | + appLogWrapper.d( |
| 68 | + AppLog.T.SETTINGS, |
| 69 | + "LocaleHelper: performing migration to AndroidX per-app language prefs" |
| 70 | + ) |
65 | 71 | setCurrentLocaleByLanguageCode(previousLanguage)
|
66 | 72 | appPrefsWrapper.prefs().edit().remove(languagePrefKey).apply()
|
67 | 73 | } else {
|
68 |
| - appLogWrapper.d(AppLog.T.SETTINGS, "LocaleHelper: setting default locale") |
| 74 | + appLogWrapper.d( |
| 75 | + AppLog.T.SETTINGS, |
| 76 | + "LocaleHelper: setting default locale" |
| 77 | + ) |
69 | 78 | setCurrentLocaleByLanguageCode(Locale.getDefault().language)
|
70 | 79 | }
|
71 | 80 | }
|
72 | 81 | }
|
73 | 82 |
|
74 |
| - fun isPerAppLanguagePrefsEnabled(context: Context): Boolean { |
75 |
| - val prefKey = context.getString(R.string.experimental_per_app_language_prefs) |
76 |
| - return appPrefsWrapper.getManualFeatureConfig(prefKey) |
| 83 | + fun isPerAppLanguagePrefsEnabled(): Boolean { |
| 84 | + return appPrefsWrapper.getManualFeatureConfig(EXPERIMENTAL_PER_APP_LANGUAGE_PREF_KEY) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Open the app settings screen so the user can change the app locale - note that |
| 89 | + * the locale is only available in API 33+ |
| 90 | + */ |
| 91 | + fun openAppSettings(context: Context) { |
| 92 | + Intent().also { intent -> |
| 93 | + intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
| 94 | + intent.addCategory(Intent.CATEGORY_DEFAULT) |
| 95 | + intent.setData(Uri.parse("package:" + context.packageName)) |
| 96 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 97 | + intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) |
| 98 | + intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 99 | + context.startActivity(intent) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + companion object { |
| 104 | + const val EXPERIMENTAL_PER_APP_LANGUAGE_PREF_KEY = "experimental_per_app_language_prefs" |
77 | 105 | }
|
78 | 106 | }
|
0 commit comments