diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index a156f1b41..87de7cc33 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.os.Build import androidx.multidex.MultiDexApplication import com.jeremyliao.liveeventbus.LiveEventBus +import io.legado.app.base.AppContextWrapper import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb @@ -15,7 +16,6 @@ import io.legado.app.help.CrashHandler import io.legado.app.help.LifecycleHelp import io.legado.app.help.ThemeConfig.applyDayNight import io.legado.app.help.http.cronet.CronetLoader -import io.legado.app.utils.LanguageUtils import io.legado.app.utils.defaultSharedPreferences import timber.log.Timber @@ -29,7 +29,6 @@ class App : MultiDexApplication() { } //预下载Cronet so CronetLoader.preDownload() - LanguageUtils.setConfiguration(this) createNotificationChannels() applyDayNight(this) LiveEventBus.config() @@ -39,6 +38,10 @@ class App : MultiDexApplication() { defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) } + override fun attachBaseContext(base: Context) { + super.attachBaseContext(AppContextWrapper.wrap(base)) + } + override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) { diff --git a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt b/app/src/main/java/io/legado/app/base/AppContextWrapper.kt similarity index 84% rename from app/src/main/java/io/legado/app/utils/LanguageUtils.kt rename to app/src/main/java/io/legado/app/base/AppContextWrapper.kt index 0eed85d6f..ae1aed567 100644 --- a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt +++ b/app/src/main/java/io/legado/app/base/AppContextWrapper.kt @@ -1,4 +1,4 @@ -package io.legado.app.utils +package io.legado.app.base import android.content.Context import android.content.res.Configuration @@ -6,43 +6,31 @@ import android.content.res.Resources import android.os.Build import android.os.LocaleList import io.legado.app.constant.PreferKey +import io.legado.app.utils.getPrefString import java.util.* -object LanguageUtils { +object AppContextWrapper { - /** - * 设置语言 - */ - fun setConfiguration(context: Context): Context { + fun wrap(context: Context): Context { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val resources: Resources = context.resources - val metrics = resources.displayMetrics val configuration: Configuration = resources.configuration val targetLocale = getSetLocale(context) configuration.setLocale(targetLocale) configuration.setLocales(LocaleList(targetLocale)) - @Suppress("DEPRECATION") - resources.updateConfiguration(configuration, metrics) + configuration.fontScale = 1f context.createConfigurationContext(configuration) } else { - setConfigurationOld(context) - context - } - } - - /** - * 设置语言 - */ - private fun setConfigurationOld(context: Context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { val resources: Resources = context.resources val targetLocale = getSetLocale(context) val configuration: Configuration = resources.configuration @Suppress("DEPRECATION") configuration.locale = targetLocale + configuration.fontScale = 1f @Suppress("DEPRECATION") resources.updateConfiguration(configuration, resources.displayMetrics) + context } } diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index a757fdd8c..469cc6e40 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -48,7 +48,7 @@ abstract class BaseActivity( } override fun attachBaseContext(newBase: Context) { - super.attachBaseContext(LanguageUtils.setConfiguration(newBase)) + super.attachBaseContext(AppContextWrapper.wrap(newBase)) } override fun onCreateView( diff --git a/app/src/main/java/io/legado/app/help/LifecycleHelp.kt b/app/src/main/java/io/legado/app/help/LifecycleHelp.kt index 0a9e50826..387e37d89 100644 --- a/app/src/main/java/io/legado/app/help/LifecycleHelp.kt +++ b/app/src/main/java/io/legado/app/help/LifecycleHelp.kt @@ -3,8 +3,8 @@ package io.legado.app.help import android.app.Activity import android.app.Application import android.os.Bundle +import io.legado.app.base.AppContextWrapper import io.legado.app.base.BaseService -import io.legado.app.utils.LanguageUtils import java.lang.ref.WeakReference import java.util.* @@ -86,8 +86,8 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { activities.add(WeakReference(activity)) - if (!LanguageUtils.isSameWithSetting(activity)) { - LanguageUtils.setConfiguration(activity) + if (!AppContextWrapper.isSameWithSetting(activity)) { + AppContextWrapper.wrap(activity) } } 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 abd86176d..5c6bed4d9 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 @@ -11,6 +11,7 @@ import android.view.View import androidx.preference.ListPreference import androidx.preference.Preference import io.legado.app.R +import io.legado.app.base.AppContextWrapper import io.legado.app.base.BasePreferenceFragment import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey @@ -113,7 +114,7 @@ class OtherConfigFragment : BasePreferenceFragment(), } PreferKey.showDiscovery, PreferKey.showRss -> postEvent(EventBus.NOTIFY_MAIN, true) PreferKey.language -> listView.postDelayed({ - LanguageUtils.setConfiguration(appCtx) + AppContextWrapper.wrap(appCtx) val intent = Intent(appCtx, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) appCtx.startActivity(intent)