From b856081927af50ddd78f7285bea1feef6016da92 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Sat, 10 Oct 2020 19:01:45 -0400 Subject: [PATCH 1/3] fix language not change bug fix language not change bug --- app/src/main/java/io/legado/app/App.kt | 3 +- .../java/io/legado/app/constant/EventBus.kt | 1 + .../java/io/legado/app/help/ActivityHelp.kt | 4 + .../io/legado/app/help/storage/Restore.kt | 2 +- .../io/legado/app/ui/config/ConfigActivity.kt | 12 +++ .../app/ui/config/OtherConfigFragment.kt | 4 +- .../java/io/legado/app/utils/LanguageUtils.kt | 81 ++++++++++++++----- app/src/main/res/values/arrays.xml | 8 +- app/src/main/res/values/strings.xml | 5 +- 9 files changed, 93 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 37aea552d..64f037fa4 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -7,6 +7,7 @@ import android.content.res.Configuration import android.graphics.Color import android.os.Build import android.provider.Settings +import android.util.Log import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDexApplication @@ -46,7 +47,7 @@ class App : MultiDexApplication() { INSTANCE = this androidId = Settings.System.getString(contentResolver, Settings.Secure.ANDROID_ID) CrashHandler(this) - LanguageUtils.setConfigurationOld(this) + LanguageUtils.setConfiguration(this) db = AppDatabase.createDatabase(INSTANCE) packageManager.getPackageInfo(packageName, 0)?.let { versionCode = it.versionCode diff --git a/app/src/main/java/io/legado/app/constant/EventBus.kt b/app/src/main/java/io/legado/app/constant/EventBus.kt index dd00d8cee..8e6c2a9e6 100644 --- a/app/src/main/java/io/legado/app/constant/EventBus.kt +++ b/app/src/main/java/io/legado/app/constant/EventBus.kt @@ -3,6 +3,7 @@ package io.legado.app.constant object EventBus { const val MEDIA_BUTTON = "mediaButton" const val RECREATE = "RECREATE" + const val REOPEN = "REOPEN" const val UP_BOOK = "upBookToc" const val ALOUD_STATE = "aloud_state" const val TTS_PROGRESS = "ttsStart" diff --git a/app/src/main/java/io/legado/app/help/ActivityHelp.kt b/app/src/main/java/io/legado/app/help/ActivityHelp.kt index b14c76670..8d07a3500 100644 --- a/app/src/main/java/io/legado/app/help/ActivityHelp.kt +++ b/app/src/main/java/io/legado/app/help/ActivityHelp.kt @@ -3,6 +3,7 @@ package io.legado.app.help import android.app.Activity import android.app.Application import android.os.Bundle +import io.legado.app.utils.LanguageUtils import java.lang.ref.WeakReference import java.util.* @@ -110,5 +111,8 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { add(activity) + if (!LanguageUtils.isSameWithSetting(activity)){ + LanguageUtils.setConfiguration(activity) + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 7b0e7636d..0ba5e4367 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -225,7 +225,7 @@ object Restore { if (!BuildConfig.DEBUG) { LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon)) } - LanguageUtils.setConfigurationOld(App.INSTANCE) + LanguageUtils.setConfiguration(App.INSTANCE) App.INSTANCE.applyDayNight() postEvent(EventBus.SHOW_RSS, "") postEvent(EventBus.RECREATE, "") diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt index 6d7bfcfe7..ad6c1bea2 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt @@ -1,9 +1,14 @@ package io.legado.app.ui.config +import android.content.Intent import android.os.Bundle +import android.os.Process +import android.util.Log +import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.EventBus +import io.legado.app.ui.main.MainActivity import io.legado.app.utils.getViewModel import io.legado.app.utils.observeEvent import kotlinx.android.synthetic.main.activity_config.* @@ -54,5 +59,12 @@ class ConfigActivity : VMBaseActivity(R.layout.activity_config) observeEvent(EventBus.RECREATE) { recreate() } + observeEvent(EventBus.REOPEN) { + Log.d("h11128", "reopen") + val intent = Intent(App.INSTANCE, MainActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) + App.INSTANCE.startActivity(intent) + Process.killProcess(Process.myPid()) + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index 1230bb2b5..177731855 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -7,6 +7,7 @@ import android.content.SharedPreferences import android.content.pm.PackageManager import android.net.Uri import android.os.Bundle +import android.util.Log import android.view.View import androidx.documentfile.provider.DocumentFile import androidx.preference.ListPreference @@ -31,6 +32,7 @@ import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.utils.* import java.io.File +import java.util.* class OtherConfigFragment : BasePreferenceFragment(), @@ -123,7 +125,7 @@ class OtherConfigFragment : BasePreferenceFragment(), PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault = App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true) PreferKey.language -> { - LanguageUtils.setConfigurationOld(App.INSTANCE) + LanguageUtils.setConfiguration(App.INSTANCE) postEvent(EventBus.RECREATE, "") } } diff --git a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt index ae6c465a8..551e91a00 100644 --- a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt @@ -4,6 +4,8 @@ import android.content.Context import android.content.res.Configuration import android.content.res.Resources import android.os.Build +import android.os.LocaleList +import android.util.Log import io.legado.app.constant.PreferKey import java.util.* @@ -16,14 +18,14 @@ object LanguageUtils { fun setConfiguration(context: Context): Context { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val resources: Resources = context.resources - val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { - "zh" -> Locale.CHINESE - "tw" -> Locale.TRADITIONAL_CHINESE - "en" -> Locale.ENGLISH - else -> getSystemLocale() - } + val metrics = resources.displayMetrics val configuration: Configuration = resources.configuration + //Log.d("h11128", "set language to ${context.getPrefString(PreferKey.language)}") + val targetLocale = getSetLocale(context) configuration.setLocale(targetLocale) + configuration.setLocales(LocaleList(targetLocale)) + @Suppress("DEPRECATION") + resources.updateConfiguration(configuration, metrics) context.createConfigurationContext(configuration) } else { setConfigurationOld(context) @@ -34,18 +36,12 @@ object LanguageUtils { /** * 设置语言 */ - fun setConfigurationOld(context: Context) { + private fun setConfigurationOld(context: Context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { val resources: Resources = context.resources - val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { - "zh" -> Locale.CHINESE - "tw" -> Locale.TRADITIONAL_CHINESE - "en" -> Locale.ENGLISH - else -> getSystemLocale() - } + val targetLocale = getSetLocale(context) val configuration: Configuration = resources.configuration - @Suppress("DEPRECATION") - configuration.locale = targetLocale + configuration.setLocale(targetLocale) @Suppress("DEPRECATION") resources.updateConfiguration(configuration, resources.displayMetrics) } @@ -55,13 +51,62 @@ object LanguageUtils { * 当前系统语言 */ private fun getSystemLocale(): Locale { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 - Resources.getSystem().configuration.locales.get(0) + val locale: Locale + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 + locale = Resources.getSystem().configuration.locales.get(0) + } else { + @Suppress("DEPRECATION") + locale = Resources.getSystem().configuration.locale + } + return locale + } + + /** + * 当前App语言 + */ + private fun getAppLocale(context: Context): Locale { + val locale: Locale + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + locale = context.resources.configuration.locales[0] } else { @Suppress("DEPRECATION") - Resources.getSystem().configuration.locale + locale =context.resources.configuration.locale + } + /* + Log.d("h11128", "displayName " + locale.displayName) + Log.d("h11128", "displayCountry " + locale.displayCountry) + Log.d("h11128", "displayLanguage " + locale.displayLanguage) + Log.d("h11128", "Language " + locale.language) + Log.d("h11128", "Country " + locale.country) + + */ + return locale + + } + + /** + * 当前设置语言 + */ + private fun getSetLocale(context: Context): Locale { + return when (context.getPrefString(PreferKey.language)) { + "zh" -> Locale.SIMPLIFIED_CHINESE + "tw" -> Locale.TRADITIONAL_CHINESE + "en" -> Locale.ENGLISH + else -> getSystemLocale() } } + /** + * 判断系统语言和设置语言是否相同 + */ + fun isSameWithSetting(context: Context): Boolean { + val locale = getAppLocale(context) + val language = locale.language + val country = locale.country + val pfLocale = getSetLocale(context) + val pfLanguage = pfLocale.language + val pfCountry = pfLocale.country + return language == pfLanguage && country == pfCountry + } } \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 9a0410c81..f97f537fa 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -99,10 +99,10 @@ - 跟随系统 - 简体中文 - 繁体中文 - 英文 + Auto + Simplified_Chinese + Traditional_Chinese + English \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 39b957bf1..9186d4e20 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ + Legado @@ -693,7 +694,7 @@ Hide footer Switch Layout Text font weight switching - + Primary Accent @@ -773,4 +774,4 @@ Sort by update time Search content - \ No newline at end of file + From e8ba0801aa9c94a029a12e8dfa65efc1d2274701 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Sat, 10 Oct 2020 19:06:00 -0400 Subject: [PATCH 2/3] update --- app/src/main/java/io/legado/app/utils/LanguageUtils.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt index 551e91a00..cf1e5a159 100644 --- a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt @@ -41,7 +41,8 @@ object LanguageUtils { val resources: Resources = context.resources val targetLocale = getSetLocale(context) val configuration: Configuration = resources.configuration - configuration.setLocale(targetLocale) + @Suppress("DEPRECATION") + configuration.locale = targetLocale @Suppress("DEPRECATION") resources.updateConfiguration(configuration, resources.displayMetrics) } @@ -70,7 +71,7 @@ object LanguageUtils { locale = context.resources.configuration.locales[0] } else { @Suppress("DEPRECATION") - locale =context.resources.configuration.locale + locale = context.resources.configuration.locale } /* Log.d("h11128", "displayName " + locale.displayName) @@ -97,7 +98,7 @@ object LanguageUtils { } /** - * 判断系统语言和设置语言是否相同 + * 判断App语言和设置语言是否相同 */ fun isSameWithSetting(context: Context): Boolean { val locale = getAppLocale(context) From 9c5ac27abc1b7364f84683c20887aec9577e8811 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Sat, 10 Oct 2020 19:25:10 -0400 Subject: [PATCH 3/3] change to reopen --- app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt | 2 -- .../main/java/io/legado/app/ui/config/OtherConfigFragment.kt | 2 +- app/src/main/java/io/legado/app/utils/LanguageUtils.kt | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt index ad6c1bea2..0c2525d9c 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt @@ -3,7 +3,6 @@ package io.legado.app.ui.config import android.content.Intent import android.os.Bundle import android.os.Process -import android.util.Log import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -60,7 +59,6 @@ class ConfigActivity : VMBaseActivity(R.layout.activity_config) recreate() } observeEvent(EventBus.REOPEN) { - Log.d("h11128", "reopen") val intent = Intent(App.INSTANCE, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) App.INSTANCE.startActivity(intent) diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index 177731855..36fe3dc92 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -126,7 +126,7 @@ class OtherConfigFragment : BasePreferenceFragment(), App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true) PreferKey.language -> { LanguageUtils.setConfiguration(App.INSTANCE) - postEvent(EventBus.RECREATE, "") + postEvent(EventBus.REOPEN, "") } } } diff --git a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt index cf1e5a159..def23e1e8 100644 --- a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt @@ -5,7 +5,6 @@ import android.content.res.Configuration import android.content.res.Resources import android.os.Build import android.os.LocaleList -import android.util.Log import io.legado.app.constant.PreferKey import java.util.*