From d942aeca120d887687ae2f7435f175df36921fc2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 24 Oct 2021 14:22:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 5 ----- .../java/io/legado/app/base/AppContextWrapper.kt | 1 + app/src/main/java/io/legado/app/help/AppConfig.kt | 2 +- .../io/legado/app/utils/ConfigurationExtensions.kt | 14 ++++++++++++++ .../java/io/legado/app/utils/ContextExtensions.kt | 9 --------- 5 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/ConfigurationExtensions.kt diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 87de7cc33..466e2407b 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -7,7 +7,6 @@ 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 @@ -38,10 +37,6 @@ 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/base/AppContextWrapper.kt b/app/src/main/java/io/legado/app/base/AppContextWrapper.kt index 5fd9e7725..9f2f5f0c6 100644 --- a/app/src/main/java/io/legado/app/base/AppContextWrapper.kt +++ b/app/src/main/java/io/legado/app/base/AppContextWrapper.kt @@ -25,6 +25,7 @@ object AppContextWrapper { configuration.setLocale(targetLocale) configuration.setLocales(LocaleList(targetLocale)) } else { + @Suppress("DEPRECATION") configuration.locale = targetLocale } configuration.fontScale = fontScale diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index db5068c05..d3afd4b6c 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -57,7 +57,7 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { "1" -> false "2" -> true "3" -> false - else -> context.sysIsDarkMode() + else -> sysConfiguration.isNightMode } } diff --git a/app/src/main/java/io/legado/app/utils/ConfigurationExtensions.kt b/app/src/main/java/io/legado/app/utils/ConfigurationExtensions.kt new file mode 100644 index 000000000..186e8ae69 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/ConfigurationExtensions.kt @@ -0,0 +1,14 @@ +@file:Suppress("unused") + +package io.legado.app.utils + +import android.content.res.Configuration +import android.content.res.Resources + +val sysConfiguration: Configuration = Resources.getSystem().configuration + +val Configuration.isNightMode: Boolean + get() { + val mode = uiMode and Configuration.UI_MODE_NIGHT_MASK + return mode == Configuration.UI_MODE_NIGHT_YES + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 5af1f5ac4..00546c8ad 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -10,7 +10,6 @@ import android.app.Service import android.content.* import android.content.pm.PackageManager import android.content.res.ColorStateList -import android.content.res.Configuration import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.net.Uri @@ -250,14 +249,6 @@ fun Context.sendMail(mail: String) { } } -/** - * 系统是否暗色主题 - */ -fun Context.sysIsDarkMode(): Boolean { - val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - return mode == Configuration.UI_MODE_NIGHT_YES -} - /** * 获取电量 */