From 8a51fd77cf81bb12a6db75a6f22f9846902cf439 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 17:49:39 +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 | 6 +- .../java/io/legado/app/constant/AppConst.kt | 4 + .../main/java/io/legado/app/help/AppConfig.kt | 149 ++++++++++-------- .../java/io/legado/app/help/ReadBookConfig.kt | 38 ++--- .../io/legado/app/help/storage/Restore.kt | 6 +- .../app/ui/book/read/ReadBookBaseActivity.kt | 5 +- .../app/ui/config/OtherConfigFragment.kt | 1 - .../app/ui/config/ThemeConfigFragment.kt | 3 +- 8 files changed, 120 insertions(+), 92 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index a49704169..db6b657f4 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -17,6 +17,7 @@ import io.legado.app.data.AppDatabase import io.legado.app.help.* import io.legado.app.utils.LanguageUtils import io.legado.app.utils.postEvent +import org.jetbrains.anko.defaultSharedPreferences @Suppress("DEPRECATION") class App : MultiDexApplication() { @@ -33,6 +34,8 @@ class App : MultiDexApplication() { lateinit var androidId: String var versionCode = 0 var versionName = "" + + } override fun onCreate() { @@ -53,8 +56,8 @@ class App : MultiDexApplication() { .supportBroadcast(this) .lifecycleObserverAlwaysActive(true) .autoClear(false) - registerActivityLifecycleCallbacks(ActivityHelp) + defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) } override fun onConfigurationChanged(newConfig: Configuration) { @@ -66,7 +69,6 @@ class App : MultiDexApplication() { } fun applyDayNight() { - AppConfig.upEInkMode() ReadBookConfig.upBg() ThemeConfig.applyTheme(this) initNightMode() diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index ec37a11a2..ba51ce764 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -1,6 +1,8 @@ package io.legado.app.constant import android.annotation.SuppressLint +import io.legado.app.App +import io.legado.app.R import java.text.SimpleDateFormat import javax.script.ScriptEngine import javax.script.ScriptEngineManager @@ -69,4 +71,6 @@ object AppConst { "com.android.internal.view.menu.ListMenuItemView", "androidx.appcompat.view.menu.ListMenuItemView" ) + + val sysElevation = App.INSTANCE.resources.getDimension(R.dimen.design_appbar_elevation).toInt() } \ No newline at end of file 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 b01e1ee7f..7e4e1ae02 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -2,28 +2,66 @@ package io.legado.app.help import android.annotation.SuppressLint import android.content.Context +import android.content.SharedPreferences import io.legado.app.App import io.legado.app.R +import io.legado.app.constant.AppConst import io.legado.app.constant.PreferKey import io.legado.app.utils.* @Suppress("MemberVisibilityCanBePrivate") -object AppConfig { - var isEInkMode: Boolean = false - val isGooglePlay: Boolean - val isCoolApk: Boolean - var replaceEnableDefault: Boolean = true - val sysElevation = App.INSTANCE.resources.getDimension(R.dimen.design_appbar_elevation).toInt() - - init { - upConfig() - isGooglePlay = App.INSTANCE.channel == "google" - isCoolApk = App.INSTANCE.channel == "coolApk" - } - - fun upConfig() { - upEInkMode() - upReplaceEnableDefault() +object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { + private val context get() = App.INSTANCE + val isGooglePlay = context.channel == "google" + val isCoolApk = context.channel == "coolApk" + var replaceEnableDefault = context.getPrefBoolean(PreferKey.replaceEnableDefault, true) + var isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" + var clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) + var clickActionTC = context.getPrefInt(PreferKey.clickActionTC, 2) + var clickActionTR = context.getPrefInt(PreferKey.clickActionTR, 1) + var clickActionML = context.getPrefInt(PreferKey.clickActionML, 2) + var clickActionMC = context.getPrefInt(PreferKey.clickActionMC, 0) + var clickActionMR = context.getPrefInt(PreferKey.clickActionMR, 1) + var clickActionBL = context.getPrefInt(PreferKey.clickActionBL, 2) + var clickActionBC = context.getPrefInt(PreferKey.clickActionBC, 1) + var clickActionBR = context.getPrefInt(PreferKey.clickActionBR, 1) + + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { + when (key) { + PreferKey.themeMode -> { + isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" + } + PreferKey.clickActionTL -> { + clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) + } + PreferKey.clickActionTC -> { + clickActionTC = context.getPrefInt(PreferKey.clickActionTC, 2) + } + PreferKey.clickActionTR -> { + clickActionTR = context.getPrefInt(PreferKey.clickActionTR, 2) + } + PreferKey.clickActionML -> { + clickActionML = context.getPrefInt(PreferKey.clickActionML, 2) + } + PreferKey.clickActionMC -> { + clickActionMC = context.getPrefInt(PreferKey.clickActionMC, 2) + } + PreferKey.clickActionMR -> { + clickActionMR = context.getPrefInt(PreferKey.clickActionMR, 2) + } + PreferKey.clickActionBL -> { + clickActionBL = context.getPrefInt(PreferKey.clickActionBL, 2) + } + PreferKey.clickActionBC -> { + clickActionBC = context.getPrefInt(PreferKey.clickActionBC, 2) + } + PreferKey.clickActionBR -> { + clickActionBR = context.getPrefInt(PreferKey.clickActionBR, 2) + } + PreferKey.readBodyToLh -> { + ReadBookConfig.readBodyToLh = context.getPrefBoolean(PreferKey.readBodyToLh, true) + } + } } fun isNightTheme(context: Context): Boolean { @@ -36,115 +74,94 @@ object AppConfig { } var isNightTheme: Boolean - get() = isNightTheme(App.INSTANCE) + get() = isNightTheme(context) set(value) { if (isNightTheme != value) { if (value) { - App.INSTANCE.putPrefString(PreferKey.themeMode, "2") + context.putPrefString(PreferKey.themeMode, "2") } else { - App.INSTANCE.putPrefString(PreferKey.themeMode, "1") + context.putPrefString(PreferKey.themeMode, "1") } } } - fun upEInkMode() { - isEInkMode = App.INSTANCE.getPrefString(PreferKey.themeMode) == "3" - } - var isTransparentStatusBar: Boolean - get() = App.INSTANCE.getPrefBoolean(PreferKey.transparentStatusBar) + get() = context.getPrefBoolean(PreferKey.transparentStatusBar) set(value) { - App.INSTANCE.putPrefBoolean(PreferKey.transparentStatusBar, value) + context.putPrefBoolean(PreferKey.transparentStatusBar, value) } val requestedDirection: String? - get() = App.INSTANCE.getPrefString(R.string.pk_requested_direction) + get() = context.getPrefString(R.string.pk_requested_direction) var backupPath: String? - get() = App.INSTANCE.getPrefString(PreferKey.backupPath) + get() = context.getPrefString(PreferKey.backupPath) set(value) { if (value.isNullOrEmpty()) { - App.INSTANCE.removePref(PreferKey.backupPath) + context.removePref(PreferKey.backupPath) } else { - App.INSTANCE.putPrefString(PreferKey.backupPath, value) + context.putPrefString(PreferKey.backupPath, value) } } var isShowRSS: Boolean - get() = App.INSTANCE.getPrefBoolean(PreferKey.showRss, true) + get() = context.getPrefBoolean(PreferKey.showRss, true) set(value) { - App.INSTANCE.putPrefBoolean(PreferKey.showRss, value) + context.putPrefBoolean(PreferKey.showRss, value) } val autoRefreshBook: Boolean - get() = App.INSTANCE.getPrefBoolean(R.string.pk_auto_refresh) + get() = context.getPrefBoolean(R.string.pk_auto_refresh) var threadCount: Int - get() = App.INSTANCE.getPrefInt(PreferKey.threadCount, 16) + get() = context.getPrefInt(PreferKey.threadCount, 16) set(value) { - App.INSTANCE.putPrefInt(PreferKey.threadCount, value) + context.putPrefInt(PreferKey.threadCount, value) } var importBookPath: String? - get() = App.INSTANCE.getPrefString("importBookPath") + get() = context.getPrefString("importBookPath") set(value) { if (value == null) { - App.INSTANCE.removePref("importBookPath") + context.removePref("importBookPath") } else { - App.INSTANCE.putPrefString("importBookPath", value) + context.putPrefString("importBookPath", value) } } var ttsSpeechRate: Int - get() = App.INSTANCE.getPrefInt(PreferKey.ttsSpeechRate, 5) + get() = context.getPrefInt(PreferKey.ttsSpeechRate, 5) set(value) { - App.INSTANCE.putPrefInt(PreferKey.ttsSpeechRate, value) + context.putPrefInt(PreferKey.ttsSpeechRate, value) } var chineseConverterType: Int - get() = App.INSTANCE.getPrefInt(PreferKey.chineseConverterType) + get() = context.getPrefInt(PreferKey.chineseConverterType) set(value) { - App.INSTANCE.putPrefInt(PreferKey.chineseConverterType, value) + context.putPrefInt(PreferKey.chineseConverterType, value) } var systemTypefaces: Int - get() = App.INSTANCE.getPrefInt(PreferKey.systemTypefaces) + get() = context.getPrefInt(PreferKey.systemTypefaces) set(value) { - App.INSTANCE.putPrefInt(PreferKey.systemTypefaces, value) + context.putPrefInt(PreferKey.systemTypefaces, value) } var elevation: Int @SuppressLint("PrivateResource") - get() = App.INSTANCE.getPrefInt(PreferKey.barElevation, sysElevation) + get() = context.getPrefInt(PreferKey.barElevation, AppConst.sysElevation) set(value) { - App.INSTANCE.putPrefInt(PreferKey.barElevation, value) + context.putPrefInt(PreferKey.barElevation, value) } val autoChangeSource: Boolean - get() = App.INSTANCE.getPrefBoolean(PreferKey.autoChangeSource, true) - - val readBodyToLh: Boolean - get() = App.INSTANCE.getPrefBoolean(PreferKey.readBodyToLh, true) - - fun upReplaceEnableDefault() { - replaceEnableDefault = - App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true) - } + get() = context.getPrefBoolean(PreferKey.autoChangeSource, true) - val changeSourceLoadInfo get() = App.INSTANCE.getPrefBoolean(PreferKey.changeSourceLoadToc) + val changeSourceLoadInfo get() = context.getPrefBoolean(PreferKey.changeSourceLoadToc) - val changeSourceLoadToc get() = App.INSTANCE.getPrefBoolean(PreferKey.changeSourceLoadToc) + val changeSourceLoadToc get() = context.getPrefBoolean(PreferKey.changeSourceLoadToc) - val importKeepName get() = App.INSTANCE.getPrefBoolean(PreferKey.importKeepName) + val importKeepName get() = context.getPrefBoolean(PreferKey.importKeepName) - val clickActionTL get() = App.INSTANCE.getPrefInt(PreferKey.clickActionTL, 2) - val clickActionTC get() = App.INSTANCE.getPrefInt(PreferKey.clickActionTC, 2) - val clickActionTR get() = App.INSTANCE.getPrefInt(PreferKey.clickActionTR, 1) - val clickActionML get() = App.INSTANCE.getPrefInt(PreferKey.clickActionML, 2) - val clickActionMC get() = App.INSTANCE.getPrefInt(PreferKey.clickActionMC, 0) - val clickActionMR get() = App.INSTANCE.getPrefInt(PreferKey.clickActionMR, 1) - val clickActionBL get() = App.INSTANCE.getPrefInt(PreferKey.clickActionBL, 2) - val clickActionBC get() = App.INSTANCE.getPrefInt(PreferKey.clickActionBC, 1) - val clickActionBR get() = App.INSTANCE.getPrefInt(PreferKey.clickActionBR, 1) } diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index 2303c414b..9515cbcff 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -22,8 +22,9 @@ import java.io.File object ReadBookConfig { const val configFileName = "readConfig.json" const val shareConfigFileName = "shareReadConfig.json" - val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, configFileName) - val shareConfigFilePath = FileUtils.getPath(App.INSTANCE.filesDir, shareConfigFileName) + val context get() = App.INSTANCE + val configFilePath = FileUtils.getPath(context.filesDir, configFileName) + val shareConfigFilePath = FileUtils.getPath(context.filesDir, shareConfigFileName) val configList: ArrayList = arrayListOf() lateinit var shareConfig: Config var durConfig @@ -85,7 +86,7 @@ object ReadBookConfig { } fun upBg() { - val resources = App.INSTANCE.resources + val resources = context.resources val dm = resources.displayMetrics val width = dm.widthPixels val height = dm.heightPixels @@ -134,29 +135,30 @@ object ReadBookConfig { } //配置写入读取 - var autoReadSpeed = App.INSTANCE.getPrefInt(PreferKey.autoReadSpeed, 46) + var readBodyToLh = context.getPrefBoolean(PreferKey.readBodyToLh, true) + var autoReadSpeed = context.getPrefInt(PreferKey.autoReadSpeed, 46) set(value) { field = value - App.INSTANCE.putPrefInt(PreferKey.autoReadSpeed, value) + context.putPrefInt(PreferKey.autoReadSpeed, value) } - var styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) + var styleSelect = context.getPrefInt(PreferKey.readStyleSelect) set(value) { field = value - if (App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) != value) { - App.INSTANCE.putPrefInt(PreferKey.readStyleSelect, value) + if (context.getPrefInt(PreferKey.readStyleSelect) != value) { + context.putPrefInt(PreferKey.readStyleSelect, value) } } - var shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout) + var shareLayout = context.getPrefBoolean(PreferKey.shareLayout) set(value) { field = value - if (App.INSTANCE.getPrefBoolean(PreferKey.shareLayout) != value) { - App.INSTANCE.putPrefBoolean(PreferKey.shareLayout, value) + if (context.getPrefBoolean(PreferKey.shareLayout) != value) { + context.putPrefBoolean(PreferKey.shareLayout, value) } } - val textFullJustify get() = App.INSTANCE.getPrefBoolean(PreferKey.textFullJustify, true) - val textBottomJustify get() = App.INSTANCE.getPrefBoolean(PreferKey.textBottomJustify, true) - var hideStatusBar = App.INSTANCE.getPrefBoolean(PreferKey.hideStatusBar) - var hideNavigationBar = App.INSTANCE.getPrefBoolean(PreferKey.hideNavigationBar) + val textFullJustify get() = context.getPrefBoolean(PreferKey.textFullJustify, true) + val textBottomJustify get() = context.getPrefBoolean(PreferKey.textBottomJustify, true) + var hideStatusBar = context.getPrefBoolean(PreferKey.hideStatusBar) + var hideNavigationBar = context.getPrefBoolean(PreferKey.hideNavigationBar) val config get() = if (shareLayout) shareConfig else durConfig @@ -489,7 +491,7 @@ object ReadBookConfig { fun curBgDrawable(width: Int, height: Int): Drawable { var bgDrawable: Drawable? = null - val resources = App.INSTANCE.resources + val resources = context.resources try { bgDrawable = when (curBgType()) { 0 -> ColorDrawable(Color.parseColor(curBgStr())) @@ -497,7 +499,7 @@ object ReadBookConfig { BitmapDrawable( resources, BitmapUtils.decodeAssetsBitmap( - App.INSTANCE, + context, "bg" + File.separator + curBgStr(), width, height @@ -512,7 +514,7 @@ object ReadBookConfig { } catch (e: Exception) { e.printStackTrace() } - return bgDrawable ?: ColorDrawable(App.INSTANCE.getCompatColor(R.color.background)) + return bgDrawable ?: ColorDrawable(context.getCompatColor(R.color.background)) } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 8c535926e..eab9671d6 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -13,7 +13,10 @@ import io.legado.app.R import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.entities.* -import io.legado.app.help.* +import io.legado.app.help.DefaultData +import io.legado.app.help.LauncherIconHelp +import io.legado.app.help.ReadBookConfig +import io.legado.app.help.ThemeConfig import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.utils.* @@ -206,7 +209,6 @@ object Restore { } } edit.apply() - AppConfig.upConfig() } ReadBookConfig.apply { styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt index dbfdc8807..1e68e29ff 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt @@ -21,7 +21,8 @@ import io.legado.app.help.AppConfig import io.legado.app.help.LocalConfig import io.legado.app.help.ReadBookConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.lib.dialogs.* +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.backgroundColor @@ -208,7 +209,7 @@ abstract class ReadBookBaseActivity : * 适配刘海 */ private fun upLayoutInDisplayCutoutMode() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && AppConfig.readBodyToLh) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && ReadBookConfig.readBodyToLh) { window.attributes = window.attributes.apply { layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 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 5b3ab31f5..e3a6d7e9f 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 @@ -120,7 +120,6 @@ class OtherConfigFragment : BasePreferenceFragment(), key, getPrefString(PreferKey.defaultCover) ) - PreferKey.replaceEnableDefault -> AppConfig.upReplaceEnableDefault() PreferKey.language -> listView.postDelayed({ LanguageUtils.setConfiguration(App.INSTANCE) val intent = Intent(App.INSTANCE, MainActivity::class.java) diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 045ddee39..3c85d33fc 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -12,6 +12,7 @@ import androidx.preference.Preference import io.legado.app.App import io.legado.app.R import io.legado.app.base.BasePreferenceFragment +import io.legado.app.constant.AppConst import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig @@ -158,7 +159,7 @@ class ThemeConfigFragment : BasePreferenceFragment(), .setMinValue(0) .setValue(AppConfig.elevation) .setCustomButton((R.string.btn_default_s)) { - AppConfig.elevation = AppConfig.sysElevation + AppConfig.elevation = AppConst.sysElevation recreateActivities() } .show {