pull/1415/head
gedoor 4 years ago
parent 838e079e2b
commit e711093efe
  1. 7
      app/src/main/java/io/legado/app/App.kt
  2. 26
      app/src/main/java/io/legado/app/base/AppContextWrapper.kt
  3. 2
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  4. 6
      app/src/main/java/io/legado/app/help/LifecycleHelp.kt
  5. 3
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt

@ -7,6 +7,7 @@ import android.content.res.Configuration
import android.os.Build import android.os.Build
import androidx.multidex.MultiDexApplication import androidx.multidex.MultiDexApplication
import com.jeremyliao.liveeventbus.LiveEventBus import com.jeremyliao.liveeventbus.LiveEventBus
import io.legado.app.base.AppContextWrapper
import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdDownload
import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdReadAloud
import io.legado.app.constant.AppConst.channelIdWeb 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.LifecycleHelp
import io.legado.app.help.ThemeConfig.applyDayNight import io.legado.app.help.ThemeConfig.applyDayNight
import io.legado.app.help.http.cronet.CronetLoader import io.legado.app.help.http.cronet.CronetLoader
import io.legado.app.utils.LanguageUtils
import io.legado.app.utils.defaultSharedPreferences import io.legado.app.utils.defaultSharedPreferences
import timber.log.Timber import timber.log.Timber
@ -29,7 +29,6 @@ class App : MultiDexApplication() {
} }
//预下载Cronet so //预下载Cronet so
CronetLoader.preDownload() CronetLoader.preDownload()
LanguageUtils.setConfiguration(this)
createNotificationChannels() createNotificationChannels()
applyDayNight(this) applyDayNight(this)
LiveEventBus.config() LiveEventBus.config()
@ -39,6 +38,10 @@ class App : MultiDexApplication() {
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig)
} }
override fun attachBaseContext(base: Context) {
super.attachBaseContext(AppContextWrapper.wrap(base))
}
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) { when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) {

@ -1,4 +1,4 @@
package io.legado.app.utils package io.legado.app.base
import android.content.Context import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
@ -6,43 +6,31 @@ import android.content.res.Resources
import android.os.Build import android.os.Build
import android.os.LocaleList import android.os.LocaleList
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.utils.getPrefString
import java.util.* import java.util.*
object LanguageUtils { object AppContextWrapper {
/** fun wrap(context: Context): Context {
* 设置语言
*/
fun setConfiguration(context: Context): Context {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val resources: Resources = context.resources val resources: Resources = context.resources
val metrics = resources.displayMetrics
val configuration: Configuration = resources.configuration val configuration: Configuration = resources.configuration
val targetLocale = getSetLocale(context) val targetLocale = getSetLocale(context)
configuration.setLocale(targetLocale) configuration.setLocale(targetLocale)
configuration.setLocales(LocaleList(targetLocale)) configuration.setLocales(LocaleList(targetLocale))
@Suppress("DEPRECATION") configuration.fontScale = 1f
resources.updateConfiguration(configuration, metrics)
context.createConfigurationContext(configuration) context.createConfigurationContext(configuration)
} else { } else {
setConfigurationOld(context)
context
}
}
/**
* 设置语言
*/
private fun setConfigurationOld(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
val resources: Resources = context.resources val resources: Resources = context.resources
val targetLocale = getSetLocale(context) val targetLocale = getSetLocale(context)
val configuration: Configuration = resources.configuration val configuration: Configuration = resources.configuration
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
configuration.locale = targetLocale configuration.locale = targetLocale
configuration.fontScale = 1f
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
resources.updateConfiguration(configuration, resources.displayMetrics) resources.updateConfiguration(configuration, resources.displayMetrics)
context
} }
} }

@ -48,7 +48,7 @@ abstract class BaseActivity<VB : ViewBinding>(
} }
override fun attachBaseContext(newBase: Context) { override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(LanguageUtils.setConfiguration(newBase)) super.attachBaseContext(AppContextWrapper.wrap(newBase))
} }
override fun onCreateView( override fun onCreateView(

@ -3,8 +3,8 @@ package io.legado.app.help
import android.app.Activity import android.app.Activity
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import io.legado.app.base.AppContextWrapper
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.utils.LanguageUtils
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
@ -86,8 +86,8 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
activities.add(WeakReference(activity)) activities.add(WeakReference(activity))
if (!LanguageUtils.isSameWithSetting(activity)) { if (!AppContextWrapper.isSameWithSetting(activity)) {
LanguageUtils.setConfiguration(activity) AppContextWrapper.wrap(activity)
} }
} }

@ -11,6 +11,7 @@ import android.view.View
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.AppContextWrapper
import io.legado.app.base.BasePreferenceFragment import io.legado.app.base.BasePreferenceFragment
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
@ -113,7 +114,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
} }
PreferKey.showDiscovery, PreferKey.showRss -> postEvent(EventBus.NOTIFY_MAIN, true) PreferKey.showDiscovery, PreferKey.showRss -> postEvent(EventBus.NOTIFY_MAIN, true)
PreferKey.language -> listView.postDelayed({ PreferKey.language -> listView.postDelayed({
LanguageUtils.setConfiguration(appCtx) AppContextWrapper.wrap(appCtx)
val intent = Intent(appCtx, MainActivity::class.java) val intent = Intent(appCtx, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
appCtx.startActivity(intent) appCtx.startActivity(intent)

Loading…
Cancel
Save