diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index 4c67041e8b..6bc7e91abd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -34,7 +34,7 @@ open class App : Application() { setupAcra() setupJobManager() - LocaleHelper.updateCfg(this, baseContext.resources.configuration) + LocaleHelper.updateConfiguration(this, resources.configuration) } override fun attachBaseContext(base: Context) { @@ -46,7 +46,7 @@ open class App : Application() { override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - LocaleHelper.updateCfg(this, newConfig) + LocaleHelper.updateConfiguration(this, newConfig, true) } protected open fun setupAcra() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index c9acddc716..6282d010fa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.util.LocaleHelper abstract class BaseActivity : AppCompatActivity(), ActivityMixin { init { - LocaleHelper.updateCfg(this) + LocaleHelper.updateConfiguration(this) } override fun getActivity() = this diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt index a68cddb423..03420a510b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt @@ -9,7 +9,7 @@ import nucleus.view.NucleusAppCompatActivity abstract class BaseRxActivity
> : NucleusAppCompatActivity
(), ActivityMixin { init { - LocaleHelper.updateCfg(this) + LocaleHelper.updateConfiguration(this) } override fun onCreate(savedState: Bundle?) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt index 78fddd26f7..7dbb3eb446 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt @@ -115,7 +115,8 @@ class SettingsGeneralFragment : SettingsFragment(), langPreference.setOnPreferenceChangeListener { preference, newValue -> (activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_LANG_CHANGED LocaleHelper.changeLocale(newValue.toString()) - LocaleHelper.updateCfg(activity.application, activity.baseContext.resources.configuration) + val app = activity.application + LocaleHelper.updateConfiguration(app, app.resources.configuration) activity.recreate() true } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt index e96e566695..4a2995bb17 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.util import android.app.Application import android.content.res.Configuration import android.os.Build +import android.os.LocaleList import android.view.ContextThemeWrapper import eu.kanade.tachiyomi.data.preference.PreferencesHelper import uy.kohesive.injekt.injectLazy @@ -20,12 +21,9 @@ object LocaleHelper { private val preferences: PreferencesHelper by injectLazy() /** - * In API 16 and below the application's configuration has to be changed, so we need a copy of - * the initial locale. The only problem is that if the system locale changes while the app is - * running, it won't change until an application restart. + * The system's locale. */ - private var v16SystemLocale = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) - preferences.context.resources.configuration.locale else null + private var systemLocale: Locale? = null /** * The application's locale. When it's null, the system locale is used. @@ -57,9 +55,9 @@ object LocaleHelper { } /** - * Updates the app's language from API 17. + * Updates the app's language to an activity. */ - fun updateCfg(wrapper: ContextThemeWrapper) { + fun updateConfiguration(wrapper: ContextThemeWrapper) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && appLocale != null) { val config = Configuration(preferences.context.resources.configuration) config.setLocale(appLocale) @@ -68,16 +66,36 @@ object LocaleHelper { } /** - * Updates the app's language for API 16 and lower. + * Updates the app's language to the application. */ - fun updateCfg(app: Application, config: Configuration) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { - val configCopy = Configuration(config) - val displayMetrics = app.baseContext.resources.displayMetrics - configCopy.locale = appLocale ?: v16SystemLocale - app.baseContext.resources.updateConfiguration(configCopy, displayMetrics) + fun updateConfiguration(app: Application, config: Configuration, configChange: Boolean = false) { + if (systemLocale == null) { + systemLocale = getConfigLocale(config) + } + // In API 16 and lower the system locale can't be changed. + if (configChange && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + val newLocale = getConfigLocale(config) + if (systemLocale == newLocale) { + return + } + systemLocale = newLocale + } + val newConfig = Configuration(config) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + newConfig.locale = appLocale ?: systemLocale + } else { + newConfig.locales = LocaleList(appLocale ?: systemLocale) + } + val resources = app.resources + resources.updateConfiguration(newConfig, resources.displayMetrics) + } + + private fun getConfigLocale(config: Configuration): Locale { + return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + config.locale + } else { + config.locales[0] } } - }