fix language not change bug

fix language not change bug
pull/414/head
Jason Yao 4 years ago
parent f10a89dd2f
commit b856081927
  1. 3
      app/src/main/java/io/legado/app/App.kt
  2. 1
      app/src/main/java/io/legado/app/constant/EventBus.kt
  3. 4
      app/src/main/java/io/legado/app/help/ActivityHelp.kt
  4. 2
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  5. 12
      app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt
  6. 4
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  7. 81
      app/src/main/java/io/legado/app/utils/LanguageUtils.kt
  8. 8
      app/src/main/res/values/arrays.xml
  9. 1
      app/src/main/res/values/strings.xml

@ -7,6 +7,7 @@ import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
import android.os.Build import android.os.Build
import android.provider.Settings import android.provider.Settings
import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDexApplication import androidx.multidex.MultiDexApplication
@ -46,7 +47,7 @@ class App : MultiDexApplication() {
INSTANCE = this INSTANCE = this
androidId = Settings.System.getString(contentResolver, Settings.Secure.ANDROID_ID) androidId = Settings.System.getString(contentResolver, Settings.Secure.ANDROID_ID)
CrashHandler(this) CrashHandler(this)
LanguageUtils.setConfigurationOld(this) LanguageUtils.setConfiguration(this)
db = AppDatabase.createDatabase(INSTANCE) db = AppDatabase.createDatabase(INSTANCE)
packageManager.getPackageInfo(packageName, 0)?.let { packageManager.getPackageInfo(packageName, 0)?.let {
versionCode = it.versionCode versionCode = it.versionCode

@ -3,6 +3,7 @@ package io.legado.app.constant
object EventBus { object EventBus {
const val MEDIA_BUTTON = "mediaButton" const val MEDIA_BUTTON = "mediaButton"
const val RECREATE = "RECREATE" const val RECREATE = "RECREATE"
const val REOPEN = "REOPEN"
const val UP_BOOK = "upBookToc" const val UP_BOOK = "upBookToc"
const val ALOUD_STATE = "aloud_state" const val ALOUD_STATE = "aloud_state"
const val TTS_PROGRESS = "ttsStart" const val TTS_PROGRESS = "ttsStart"

@ -3,6 +3,7 @@ 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.utils.LanguageUtils
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
@ -110,5 +111,8 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
add(activity) add(activity)
if (!LanguageUtils.isSameWithSetting(activity)){
LanguageUtils.setConfiguration(activity)
}
} }
} }

@ -225,7 +225,7 @@ object Restore {
if (!BuildConfig.DEBUG) { if (!BuildConfig.DEBUG) {
LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon)) LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon))
} }
LanguageUtils.setConfigurationOld(App.INSTANCE) LanguageUtils.setConfiguration(App.INSTANCE)
App.INSTANCE.applyDayNight() App.INSTANCE.applyDayNight()
postEvent(EventBus.SHOW_RSS, "") postEvent(EventBus.SHOW_RSS, "")
postEvent(EventBus.RECREATE, "") postEvent(EventBus.RECREATE, "")

@ -1,9 +1,14 @@
package io.legado.app.ui.config package io.legado.app.ui.config
import android.content.Intent
import android.os.Bundle 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.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.ui.main.MainActivity
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.activity_config.* import kotlinx.android.synthetic.main.activity_config.*
@ -54,5 +59,12 @@ class ConfigActivity : VMBaseActivity<ConfigViewModel>(R.layout.activity_config)
observeEvent<String>(EventBus.RECREATE) { observeEvent<String>(EventBus.RECREATE) {
recreate() recreate()
} }
observeEvent<String>(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())
}
} }
} }

@ -7,6 +7,7 @@ import android.content.SharedPreferences
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.View import android.view.View
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.preference.ListPreference 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.ui.widget.number.NumberPickerDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import java.io.File import java.io.File
import java.util.*
class OtherConfigFragment : BasePreferenceFragment(), class OtherConfigFragment : BasePreferenceFragment(),
@ -123,7 +125,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault = PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault =
App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true) App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true)
PreferKey.language -> { PreferKey.language -> {
LanguageUtils.setConfigurationOld(App.INSTANCE) LanguageUtils.setConfiguration(App.INSTANCE)
postEvent(EventBus.RECREATE, "") postEvent(EventBus.RECREATE, "")
} }
} }

@ -4,6 +4,8 @@ import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
import android.os.Build import android.os.Build
import android.os.LocaleList
import android.util.Log
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import java.util.* import java.util.*
@ -16,14 +18,14 @@ object LanguageUtils {
fun setConfiguration(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 targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { val metrics = resources.displayMetrics
"zh" -> Locale.CHINESE
"tw" -> Locale.TRADITIONAL_CHINESE
"en" -> Locale.ENGLISH
else -> getSystemLocale()
}
val configuration: Configuration = resources.configuration val configuration: Configuration = resources.configuration
//Log.d("h11128", "set language to ${context.getPrefString(PreferKey.language)}")
val targetLocale = getSetLocale(context)
configuration.setLocale(targetLocale) configuration.setLocale(targetLocale)
configuration.setLocales(LocaleList(targetLocale))
@Suppress("DEPRECATION")
resources.updateConfiguration(configuration, metrics)
context.createConfigurationContext(configuration) context.createConfigurationContext(configuration)
} else { } else {
setConfigurationOld(context) 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) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
val resources: Resources = context.resources val resources: Resources = context.resources
val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { val targetLocale = getSetLocale(context)
"zh" -> Locale.CHINESE
"tw" -> Locale.TRADITIONAL_CHINESE
"en" -> Locale.ENGLISH
else -> getSystemLocale()
}
val configuration: Configuration = resources.configuration val configuration: Configuration = resources.configuration
@Suppress("DEPRECATION") configuration.setLocale(targetLocale)
configuration.locale = targetLocale
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
resources.updateConfiguration(configuration, resources.displayMetrics) resources.updateConfiguration(configuration, resources.displayMetrics)
} }
@ -55,13 +51,62 @@ object LanguageUtils {
* 当前系统语言 * 当前系统语言
*/ */
private fun getSystemLocale(): Locale { private fun getSystemLocale(): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 val locale: Locale
Resources.getSystem().configuration.locales.get(0) 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 { } else {
@Suppress("DEPRECATION") @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
}
} }

@ -99,10 +99,10 @@
</string-array> </string-array>
<string-array name="language"> <string-array name="language">
<item>跟随系统</item> <item>Auto</item>
<item>简体中文</item> <item>Simplified_Chinese</item>
<item>繁体中文</item> <item>Traditional_Chinese</item>
<item>英文</item> <item>English</item>
</string-array> </string-array>
</resources> </resources>

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<!--App--> <!--App-->
<string name="app_name">Legado</string> <string name="app_name">Legado</string>

Loading…
Cancel
Save