pull/1415/head
gedoor 3 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 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) {

@ -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
}
}

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

@ -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)
}
}

@ -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)

Loading…
Cancel
Save