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..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 @@ -1,9 +1,13 @@ package io.legado.app.ui.config +import android.content.Intent import android.os.Bundle +import android.os.Process +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 +58,11 @@ class ConfigActivity : VMBaseActivity(R.layout.activity_config) observeEvent(EventBus.RECREATE) { recreate() } + observeEvent(EventBus.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..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 @@ -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,8 +125,8 @@ class OtherConfigFragment : BasePreferenceFragment(), PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault = App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true) PreferKey.language -> { - LanguageUtils.setConfigurationOld(App.INSTANCE) - postEvent(EventBus.RECREATE, "") + LanguageUtils.setConfiguration(App.INSTANCE) + 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 ae6c465a8..def23e1e8 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,7 @@ import android.content.Context import android.content.res.Configuration import android.content.res.Resources import android.os.Build +import android.os.LocaleList import io.legado.app.constant.PreferKey import java.util.* @@ -16,14 +17,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,15 +35,10 @@ 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 @@ -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") - Resources.getSystem().configuration.locale + 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") + 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() } } + /** + * 判断App语言和设置语言是否相同 + */ + 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 +