From 0fe1fa207515765ef3edf3e6bc24bda2c2e93fc9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 13:53:44 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/utils/NetworkUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index d08e4a450..d5464c6aa 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -80,7 +80,7 @@ object NetworkUtils { fun getAbsoluteURL(baseURL: String?, relativePath: String?): String? { if (baseURL.isNullOrEmpty()) return relativePath if (relativePath.isNullOrEmpty()) return baseURL - if (relativePath.isAbsUrl()) return baseURL + if (relativePath.isAbsUrl()) return relativePath var relativeUrl = relativePath try { val absoluteUrl = URL(baseURL.substringBefore(",")) From faf43526d7233c6401b9a9ab2ddb90d75260147c Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 14:06:36 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/data/entities/BookChapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index 1668ef19c..d508895e7 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -65,7 +65,7 @@ data class BookChapter( val urlArray = url.split(AnalyzeUrl.splitUrlRegex) var absoluteUrl = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0])!! if (urlArray.size > 1) { - absoluteUrl += urlArray[1] + absoluteUrl += "," + urlArray[1] } return absoluteUrl } From 8202230c9342fd46f57cc325efcadf5e25f645b3 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 14:34:37 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index be988fe08..86e57fb68 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -94,6 +94,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { BookHelp.getContent(book, chapter).let { content -> val content1 = content?.let { contentProcessor.getContent(book, chapter.title, it, false) + .joinToString("\n") } stringBuilder.append("\n\n") .append(chapter.title) From 6c782107fc59b275f2c27d764453eb181d438830 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 14:52:15 +0800 Subject: [PATCH 04/10] =?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/data/entities/BookChapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index d508895e7..c9c1d63c1 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -65,7 +65,7 @@ data class BookChapter( val urlArray = url.split(AnalyzeUrl.splitUrlRegex) var absoluteUrl = NetworkUtils.getAbsoluteURL(baseUrl, urlArray[0])!! if (urlArray.size > 1) { - absoluteUrl += "," + urlArray[1] + absoluteUrl = "$absoluteUrl,${urlArray[1]}" } return absoluteUrl } From e7a0a212e2d4c579db80bfb5bb9100dbe609a69b Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 15:51:28 +0800 Subject: [PATCH 05/10] =?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/constant/PreferKey.kt | 1 - app/src/main/java/io/legado/app/help/LocalConfig.kt | 9 +++++++++ app/src/main/java/io/legado/app/help/storage/Restore.kt | 1 - app/src/main/java/io/legado/app/ui/main/MainActivity.kt | 8 +++++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 1f99d7cba..6a47308d5 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -1,7 +1,6 @@ package io.legado.app.constant object PreferKey { - const val versionCode = "versionCode" const val language = "language" const val themeMode = "themeMode" const val hideStatusBar = "hideStatusBar" diff --git a/app/src/main/java/io/legado/app/help/LocalConfig.kt b/app/src/main/java/io/legado/app/help/LocalConfig.kt index 11c9ce430..686dd93f4 100644 --- a/app/src/main/java/io/legado/app/help/LocalConfig.kt +++ b/app/src/main/java/io/legado/app/help/LocalConfig.kt @@ -5,10 +5,19 @@ import androidx.core.content.edit import io.legado.app.App object LocalConfig { + private const val versionCodeKey = "versionCode" private val localConfig = App.INSTANCE.getSharedPreferences("local", Context.MODE_PRIVATE) + var versionCode + get() = localConfig.getInt(versionCodeKey, 0) + set(value) { + localConfig.edit { + putInt(versionCodeKey, value) + } + } + val isFirstOpenApp: Boolean get() { val value = localConfig.getBoolean("firstOpen", true) 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 cd100503b..8c535926e 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 @@ -52,7 +52,6 @@ object Restore { //默认忽略keys private val ignorePrefKeys = arrayOf( - PreferKey.versionCode, PreferKey.defaultCover ) private val readPrefKeys = arrayOf( diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 66970d7cc..028506550 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -27,7 +27,9 @@ import io.legado.app.ui.main.explore.ExploreFragment import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.widget.dialog.TextDialog -import io.legado.app.utils.* +import io.legado.app.utils.getViewModel +import io.legado.app.utils.hideSoftInput +import io.legado.app.utils.observeEvent import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.toast @@ -101,8 +103,8 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } private fun upVersion() { - if (getPrefInt(PreferKey.versionCode) != App.versionCode) { - putPrefInt(PreferKey.versionCode, App.versionCode) + if (LocalConfig.versionCode != App.versionCode) { + LocalConfig.versionCode = App.versionCode if (LocalConfig.isFirstOpenApp) { val text = String(assets.open("help/appHelp.md").readBytes()) TextDialog.show(supportFragmentManager, text, TextDialog.MD) From c71a525de83cf1cda002c5f22827857dc7c7f780 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 15:56:50 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/PreferKey.kt | 18 +++++------ .../main/java/io/legado/app/help/AppConfig.kt | 20 ++++++------ .../read/config/ClickActionConfigDialog.kt | 32 +++++++++---------- .../legado/app/ui/book/read/page/PageView.kt | 18 +++++------ 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 6a47308d5..548356466 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -4,15 +4,15 @@ object PreferKey { const val language = "language" const val themeMode = "themeMode" const val hideStatusBar = "hideStatusBar" - const val clickActionTopLeft = "clickActionTopLeft" - const val clickActionTopCenter = "clickActionTopCenter" - const val clickActionTopRight = "clickActionTopRight" - const val clickActionMiddleLeft = "clickActionMiddleLeft" - const val clickActionMiddleCenter = "clickActionMiddleCenter" - const val clickActionMiddleRight = "clickActionMiddleRight" - const val clickActionBottomLeft = "clickActionBottomLeft" - const val clickActionBottomCenter = "clickActionBottomCenter" - const val clickActionBottomRight = "clickActionBottomRight" + const val clickActionTL = "clickActionTopLeft" + const val clickActionTC = "clickActionTopCenter" + const val clickActionTR = "clickActionTopRight" + const val clickActionML = "clickActionMiddleLeft" + const val clickActionMC = "clickActionMiddleCenter" + const val clickActionMR = "clickActionMiddleRight" + const val clickActionBL = "clickActionBottomLeft" + const val clickActionBC = "clickActionBottomCenter" + const val clickActionBR = "clickActionBottomRight" const val hideNavigationBar = "hideNavigationBar" const val precisionSearch = "precisionSearch" const val speakEngine = "speakEngine" 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 a6064e855..b01e1ee7f 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -137,16 +137,14 @@ object AppConfig { val importKeepName get() = App.INSTANCE.getPrefBoolean(PreferKey.importKeepName) - val clickActionTopLeft get() = App.INSTANCE.getPrefInt(PreferKey.clickActionTopLeft, 2) - val clickActionTopCenter get() = App.INSTANCE.getPrefInt(PreferKey.clickActionTopCenter, 2) - val clickActionTopRight get() = App.INSTANCE.getPrefInt(PreferKey.clickActionTopRight, 1) - val clickActionMiddleLeft get() = App.INSTANCE.getPrefInt(PreferKey.clickActionMiddleLeft, 2) - val clickActionMiddleCenter - get() = App.INSTANCE.getPrefInt(PreferKey.clickActionMiddleCenter, 0) - val clickActionMiddleRight get() = App.INSTANCE.getPrefInt(PreferKey.clickActionMiddleRight, 1) - val clickActionBottomLeft get() = App.INSTANCE.getPrefInt(PreferKey.clickActionBottomLeft, 2) - val clickActionBottomCenter - get() = App.INSTANCE.getPrefInt(PreferKey.clickActionBottomCenter, 1) - val clickActionBottomRight get() = App.INSTANCE.getPrefInt(PreferKey.clickActionBottomRight, 1) + 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/ui/book/read/config/ClickActionConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ClickActionConfigDialog.kt index 55082400e..30608a1f8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ClickActionConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ClickActionConfigDialog.kt @@ -56,14 +56,14 @@ class ClickActionConfigDialog : BaseDialogFragment() { } private fun initData() = with(AppConfig) { - tv_top_left.text = actions[clickActionTopLeft] - tv_top_center.text = actions[clickActionTopCenter] - tv_top_right.text = actions[clickActionTopRight] - tv_middle_left.text = actions[clickActionMiddleLeft] - tv_middle_right.text = actions[clickActionMiddleRight] - tv_bottom_left.text = actions[clickActionBottomLeft] - tv_bottom_center.text = actions[clickActionBottomCenter] - tv_bottom_right.text = actions[clickActionBottomRight] + tv_top_left.text = actions[clickActionTL] + tv_top_center.text = actions[clickActionTC] + tv_top_right.text = actions[clickActionTR] + tv_middle_left.text = actions[clickActionML] + tv_middle_right.text = actions[clickActionMR] + tv_bottom_left.text = actions[clickActionBL] + tv_bottom_center.text = actions[clickActionBC] + tv_bottom_right.text = actions[clickActionBR] } private fun initViewEvent() { @@ -72,49 +72,49 @@ class ClickActionConfigDialog : BaseDialogFragment() { } tv_top_left.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionTopLeft, action) + putPrefInt(PreferKey.clickActionTL, action) (it as? TextView)?.text = actions[action] } } tv_top_center.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionTopCenter, action) + putPrefInt(PreferKey.clickActionTC, action) (it as? TextView)?.text = actions[action] } } tv_top_right.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionTopRight, action) + putPrefInt(PreferKey.clickActionTR, action) (it as? TextView)?.text = actions[action] } } tv_middle_left.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionMiddleLeft, action) + putPrefInt(PreferKey.clickActionML, action) (it as? TextView)?.text = actions[action] } } tv_middle_right.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionMiddleRight, action) + putPrefInt(PreferKey.clickActionMR, action) (it as? TextView)?.text = actions[action] } } tv_bottom_left.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionBottomLeft, action) + putPrefInt(PreferKey.clickActionBL, action) (it as? TextView)?.text = actions[action] } } tv_bottom_center.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionBottomCenter, action) + putPrefInt(PreferKey.clickActionBC, action) (it as? TextView)?.text = actions[action] } } tv_bottom_right.onClick { selectAction { action -> - putPrefInt(PreferKey.clickActionBottomRight, action) + putPrefInt(PreferKey.clickActionBR, action) (it as? TextView)?.text = actions[action] } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index 0fb334989..4bd777ab8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -261,31 +261,31 @@ class PageView(context: Context, attrs: AttributeSet) : when { isTextSelected -> isTextSelected = false mcRect.contains(startX, startY) -> if (!isAbortAnim) { - click(AppConfig.clickActionMiddleCenter) + click(AppConfig.clickActionMC) } bcRect.contains(startX, startY) -> { - click(AppConfig.clickActionBottomCenter) + click(AppConfig.clickActionBC) } blRect.contains(startX, startY) -> { - click(AppConfig.clickActionBottomLeft) + click(AppConfig.clickActionBL) } brRect.contains(startX, startY) -> { - click(AppConfig.clickActionBottomRight) + click(AppConfig.clickActionBR) } mlRect.contains(startX, startY) -> { - click(AppConfig.clickActionMiddleLeft) + click(AppConfig.clickActionML) } mrRect.contains(startX, startY) -> { - click(AppConfig.clickActionMiddleRight) + click(AppConfig.clickActionMR) } tlRect.contains(startX, startY) -> { - click(AppConfig.clickActionTopLeft) + click(AppConfig.clickActionTL) } tcRect.contains(startX, startY) -> { - click(AppConfig.clickActionTopCenter) + click(AppConfig.clickActionTC) } trRect.contains(startX, startY) -> { - click(AppConfig.clickActionTopRight) + click(AppConfig.clickActionTR) } } } From cf919ee682c93131261f5afa585a5106d1805a1f Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 16:11:21 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/qrcode/QrCodeActivity.kt | 12 ++++++++---- .../app/ui/replace/edit/ReplaceEditActivity.kt | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt b/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt index 6ea1d85bd..a5f6170fa 100644 --- a/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt +++ b/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt @@ -64,17 +64,20 @@ class QrCodeActivity : BaseActivity(R.layout.activity_qrcode_capture), QRCodeVie .rationale(R.string.qr_per) .onGranted { zxingview.visibility = View.VISIBLE - zxingview.startSpotAndShowRect() // 显示扫描框,并开始识别 + //TODO 显示扫描框,并开始识别 + zxingview.startSpotAndShowRect() }.request() } override fun onStop() { - zxingview.stopCamera() // 关闭摄像头预览,并且隐藏扫描框 + //TODO 关闭摄像头预览,并且隐藏扫描框 + zxingview.stopCamera() super.onStop() } override fun onDestroy() { - zxingview.onDestroy() // 销毁二维码扫描控件 + //TODO 销毁二维码扫描控件 + zxingview.onDestroy() super.onDestroy() } @@ -96,7 +99,8 @@ class QrCodeActivity : BaseActivity(R.layout.activity_qrcode_capture), QRCodeVie override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) data?.data?.let { - zxingview.startSpotAndShowRect() // 显示扫描框,并开始识别 + //TODO 显示扫描框,并开始识别 + zxingview.startSpotAndShowRect() if (resultCode == Activity.RESULT_OK && requestCode == requestQrImage) { // 本来就用到 QRCodeView 时可直接调 QRCodeView 的方法,走通用的回调 diff --git a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt index ee07a9244..4faaa8f35 100644 --- a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt @@ -117,11 +117,13 @@ class ReplaceEditActivity : if (view is EditText) { val start = view.selectionStart val end = view.selectionEnd - val edit = view.editableText//获取EditText的文字 + //TODO 获取EditText的文字 + val edit = view.editableText if (start < 0 || start >= edit.length) { edit.append(text) } else { - edit.replace(start, end, text)//光标所在位置插入文字 + //TODO 光标所在位置插入文字 + edit.replace(start, end, text) } } } From 8a51fd77cf81bb12a6db75a6f22f9846902cf439 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 17:49:39 +0800 Subject: [PATCH 08/10] =?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 { From c0a2b69eeaafd7e2154952b532597911df71a85c Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 18:40:05 +0800 Subject: [PATCH 09/10] =?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/help/AppConfig.kt | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) 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 7e4e1ae02..3ff4035e6 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -1,6 +1,5 @@ package io.legado.app.help -import android.annotation.SuppressLint import android.content.Context import android.content.SharedPreferences import io.legado.app.App @@ -13,7 +12,6 @@ import io.legado.app.utils.* 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) @@ -104,11 +102,8 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { } } - var isShowRSS: Boolean + val isShowRSS: Boolean get() = context.getPrefBoolean(PreferKey.showRss, true) - set(value) { - context.putPrefBoolean(PreferKey.showRss, value) - } val autoRefreshBook: Boolean get() = context.getPrefBoolean(R.string.pk_auto_refresh) @@ -148,7 +143,6 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { } var elevation: Int - @SuppressLint("PrivateResource") get() = context.getPrefInt(PreferKey.barElevation, AppConst.sysElevation) set(value) { context.putPrefInt(PreferKey.barElevation, value) From 88779ee75663f47266dd38cabd99d3ce02a61038 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Nov 2020 20:00:20 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/AppConst.kt | 4 -- .../java/io/legado/app/constant/PreferKey.kt | 1 + .../io/legado/app/data/entities/BookSource.kt | 9 ++- .../io/legado/app/data/entities/RssSource.kt | 5 +- .../main/java/io/legado/app/help/AppConfig.kt | 63 +++++++++---------- .../io/legado/app/help/http/HttpHelper.kt | 7 ++- .../app/model/analyzeRule/AnalyzeUrl.kt | 4 +- .../app/ui/config/OtherConfigFragment.kt | 33 +++++++++- app/src/main/res/values/pref_key_value.xml | 7 --- app/src/main/res/xml/pref_config_other.xml | 4 ++ 10 files changed, 80 insertions(+), 57 deletions(-) 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 ba51ce764..5cbb5a48b 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -18,10 +18,6 @@ object AppConst { const val UA_NAME = "User-Agent" - val userAgent: String by lazy { - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" - } - val SCRIPT_ENGINE: ScriptEngine by lazy { ScriptEngineManager().getEngineByName("rhino") } diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 548356466..22a96517a 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -3,6 +3,7 @@ package io.legado.app.constant object PreferKey { const val language = "language" const val themeMode = "themeMode" + const val userAgent = "userAgent" const val hideStatusBar = "hideStatusBar" const val clickActionTL = "clickActionTopLeft" const val clickActionTC = "clickActionTopCenter" diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 29ffdcfd1..1169ba690 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -5,11 +5,14 @@ import android.text.TextUtils import androidx.room.* import io.legado.app.App import io.legado.app.constant.AppConst -import io.legado.app.constant.AppConst.userAgent import io.legado.app.constant.BookType import io.legado.app.data.entities.rule.* +import io.legado.app.help.AppConfig import io.legado.app.help.JsExtensions -import io.legado.app.utils.* +import io.legado.app.utils.ACache +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import io.legado.app.utils.splitNotBlank import kotlinx.android.parcel.Parcelize import javax.script.SimpleBindings @@ -51,7 +54,7 @@ data class BookSource( @Throws(Exception::class) fun getHeaderMap() = (HashMap().apply { - this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent + this[AppConst.UA_NAME] = AppConfig.userAgent header?.let { GSON.fromJsonObject>( when { diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 90418f0c0..0b40b3f13 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -4,12 +4,11 @@ import android.os.Parcelable import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey -import io.legado.app.App import io.legado.app.constant.AppConst +import io.legado.app.help.AppConfig import io.legado.app.help.JsExtensions import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject -import io.legado.app.utils.getPrefString import kotlinx.android.parcel.Parcelize import java.util.* import javax.script.SimpleBindings @@ -54,7 +53,7 @@ data class RssSource( @Throws(Exception::class) fun getHeaderMap() = HashMap().apply { - this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent + this[AppConst.UA_NAME] = AppConfig.userAgent header?.let { GSON.fromJsonObject>( when { 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 3ff4035e6..bf15b1781 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -12,6 +12,7 @@ import io.legado.app.utils.* object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { private val context get() = App.INSTANCE val isGooglePlay = context.channel == "google" + var userAgent: String = getPrefUserAgent() var replaceEnableDefault = context.getPrefBoolean(PreferKey.replaceEnableDefault, true) var isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" var clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) @@ -26,39 +27,28 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { 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) - } + 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) + PreferKey.userAgent -> userAgent = getPrefUserAgent() } } @@ -157,5 +147,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val importKeepName get() = context.getPrefBoolean(PreferKey.importKeepName) + private fun getPrefUserAgent(): String { + val ua = context.getPrefString(PreferKey.userAgent) + if (ua.isNullOrBlank()) { + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" + } + return ua + } } diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index a226b46d6..c6568f50e 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -1,6 +1,7 @@ package io.legado.app.help.http import io.legado.app.constant.AppConst +import io.legado.app.help.AppConfig import io.legado.app.help.http.api.HttpGetApi import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.suspendCancellableCoroutine @@ -41,7 +42,7 @@ object HttpHelper { fun simpleGet(url: String, encode: String? = null): String? { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> val response = getApiService(baseUrl, encode) - .get(url, mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) + .get(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) .execute() return response.body() } @@ -51,7 +52,7 @@ object HttpHelper { suspend fun simpleGetAsync(url: String, encode: String? = null): String? { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> val response = getApiService(baseUrl, encode) - .getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) + .getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) return response.body() } return null @@ -61,7 +62,7 @@ object HttpHelper { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> return getByteRetrofit(baseUrl) .create(HttpGetApi::class.java) - .getMapByteAsync(url, mapOf(), mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) + .getMapByteAsync(url, mapOf(), mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) .body() } return null diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 16d9f3d29..aa4e7ec17 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -7,11 +7,11 @@ import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.LazyHeaders import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.AppConst.UA_NAME -import io.legado.app.constant.AppConst.userAgent import io.legado.app.constant.AppPattern.EXP_PATTERN import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BookChapter +import io.legado.app.help.AppConfig import io.legado.app.help.JsExtensions import io.legado.app.help.http.* import io.legado.app.help.http.api.HttpGetApi @@ -204,7 +204,7 @@ class AnalyzeUrl( } } headerMap[UA_NAME] ?: let { - headerMap[UA_NAME] = userAgent + headerMap[UA_NAME] = AppConfig.userAgent } when (method) { RequestMethod.GET -> { 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 e3a6d7e9f..eba591739 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 @@ -1,5 +1,6 @@ package io.legado.app.ui.config +import android.annotation.SuppressLint import android.app.Activity.RESULT_OK import android.content.ComponentName import android.content.Intent @@ -29,7 +30,9 @@ import io.legado.app.service.WebService import io.legado.app.ui.main.MainActivity import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.number.NumberPickerDialog +import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.utils.* +import kotlinx.android.synthetic.main.dialog_edit_text.view.* import java.io.File @@ -48,6 +51,7 @@ class OtherConfigFragment : BasePreferenceFragment(), override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) addPreferencesFromResource(R.xml.pref_config_other) + upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent) upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) upPreferenceSummary(PreferKey.webPort, webPort.toString()) upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) @@ -66,6 +70,7 @@ class OtherConfigFragment : BasePreferenceFragment(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { + PreferKey.userAgent -> showUserAgentDialog() PreferKey.threadCount -> NumberPickerDialog(requireContext()) .setTitle(getString(R.string.threads_num_title)) .setMaxValue(999) @@ -117,8 +122,7 @@ class OtherConfigFragment : BasePreferenceFragment(), } PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "") PreferKey.defaultCover -> upPreferenceSummary( - key, - getPrefString(PreferKey.defaultCover) + key, getPrefString(PreferKey.defaultCover) ) PreferKey.language -> listView.postDelayed({ LanguageUtils.setConfiguration(App.INSTANCE) @@ -127,6 +131,9 @@ class OtherConfigFragment : BasePreferenceFragment(), App.INSTANCE.startActivity(intent) Process.killProcess(Process.myPid()) }, 1000) + PreferKey.userAgent -> listView.post { + upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent) + } } } @@ -145,6 +152,28 @@ class OtherConfigFragment : BasePreferenceFragment(), } } + @SuppressLint("InflateParams") + private fun showUserAgentDialog() { + alert("UserAgent") { + var editText: AutoCompleteTextView? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + edit_view.setText(AppConfig.userAgent) + editText = edit_view + } + } + okButton { + val userAgent = editText?.text?.toString() + if (userAgent.isNullOrBlank()) { + removePref(PreferKey.userAgent) + } else { + putPrefString(PreferKey.userAgent, userAgent) + } + } + noButton() + }.show() + } + private fun clearCache() { requireContext().alert( titleResource = R.string.clear_cache, diff --git a/app/src/main/res/values/pref_key_value.xml b/app/src/main/res/values/pref_key_value.xml index a0247482b..266c4cb51 100644 --- a/app/src/main/res/values/pref_key_value.xml +++ b/app/src/main/res/values/pref_key_value.xml @@ -2,15 +2,8 @@ auto_refresh list_screen_direction - full_screen - threads_num - user_agent bookshelf_px - read_type - expandGroupFind defaultToRead - autoDownload - checkUpdate 开源阅读 https://alanskycn.gitee.io/teachme/ diff --git a/app/src/main/res/xml/pref_config_other.xml b/app/src/main/res/xml/pref_config_other.xml index 1a1541988..7077d665b 100644 --- a/app/src/main/res/xml/pref_config_other.xml +++ b/app/src/main/res/xml/pref_config_other.xml @@ -51,6 +51,10 @@ app:iconSpaceReserved="false" app:layout="@layout/view_preference_category"> + +