From 890e1b465d0815c2d00d2a45042db5f59a6d18a9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 26 Jan 2020 14:03:28 +0800 Subject: [PATCH 01/27] up --- .../java/io/legado/app/ui/rss/read/ReadRssViewModel.kt | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 5af6841e9..03264c236 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -85,17 +85,11 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), } fun clHtml(content: String): String { - return if (content.contains("$content - """ - } + $content""" } override fun onInit(status: Int) { From 94bfc9a4d32039e1681b6a141d5d04b95016862f Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 26 Jan 2020 14:10:19 +0800 Subject: [PATCH 02/27] up --- .../java/io/legado/app/ui/rss/read/ReadRssViewModel.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 03264c236..36a487a6c 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -86,10 +86,11 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), fun clHtml(content: String): String { return """$content""" + + $content""" } override fun onInit(status: Int) { From 23a35bb42af989d333ffbe0d657c03a0ae156e6f Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 10:56:58 +0800 Subject: [PATCH 03/27] up --- app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index a8ca32fdd..894bed7a9 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -149,7 +149,7 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r webView.settings.javaScriptEnabled = true webView.evaluateJavascript("document.documentElement.outerHTML") { val html = StringEscapeUtils.unescapeJson(it) - viewModel.readAloud(Jsoup.clean(html, Whitelist())) + viewModel.readAloud(Jsoup.clean(html, Whitelist.none())) } } } From 146386d78e089890bd04cb28d148216d158b80d3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 11:07:41 +0800 Subject: [PATCH 04/27] up --- app/src/main/java/io/legado/app/App.kt | 2 +- app/src/main/java/io/legado/app/constant/Theme.kt | 2 +- app/src/main/java/io/legado/app/help/PrefExtensions.kt | 5 +++++ app/src/main/java/io/legado/app/lib/theme/ATH.kt | 4 ++-- app/src/main/java/io/legado/app/ui/book/read/Help.kt | 2 +- .../main/java/io/legado/app/ui/book/read/ReadMenu.kt | 1 + .../main/java/io/legado/app/utils/ContextExtensions.kt | 10 +++++----- 7 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index ee3e4f5f6..8565ceb9e 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -17,11 +17,11 @@ import io.legado.app.data.AppDatabase import io.legado.app.help.ActivityHelp import io.legado.app.help.CrashHandler import io.legado.app.help.ReadBookConfig +import io.legado.app.help.isNightTheme import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.utils.getCompatColor import io.legado.app.utils.getPrefInt -import io.legado.app.utils.isNightTheme @Suppress("DEPRECATION") class App : Application() { diff --git a/app/src/main/java/io/legado/app/constant/Theme.kt b/app/src/main/java/io/legado/app/constant/Theme.kt index 40467a688..975eaa802 100644 --- a/app/src/main/java/io/legado/app/constant/Theme.kt +++ b/app/src/main/java/io/legado/app/constant/Theme.kt @@ -1,7 +1,7 @@ package io.legado.app.constant import io.legado.app.App -import io.legado.app.utils.isNightTheme +import io.legado.app.help.isNightTheme enum class Theme { Dark, Light, Auto; diff --git a/app/src/main/java/io/legado/app/help/PrefExtensions.kt b/app/src/main/java/io/legado/app/help/PrefExtensions.kt index 9a41707a1..4b89939cc 100644 --- a/app/src/main/java/io/legado/app/help/PrefExtensions.kt +++ b/app/src/main/java/io/legado/app/help/PrefExtensions.kt @@ -5,6 +5,11 @@ import io.legado.app.constant.PreferKey import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefInt +val Context.isNightTheme: Boolean + get() = getPrefBoolean("isNightTheme") + +val Context.isTransparentStatusBar: Boolean + get() = getPrefBoolean("transparentStatusBar", true) val Context.isShowRSS: Boolean get() = getPrefBoolean(PreferKey.showRss, true) diff --git a/app/src/main/java/io/legado/app/lib/theme/ATH.kt b/app/src/main/java/io/legado/app/lib/theme/ATH.kt index 57c7c8403..91c7fcda2 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATH.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATH.kt @@ -16,9 +16,9 @@ import androidx.recyclerview.widget.RecyclerView import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView import io.legado.app.R +import io.legado.app.help.isNightTheme +import io.legado.app.help.isTransparentStatusBar import io.legado.app.utils.getCompatColor -import io.legado.app.utils.isNightTheme -import io.legado.app.utils.isTransparentStatusBar import kotlinx.android.synthetic.main.activity_main.view.* import org.jetbrains.anko.backgroundColor diff --git a/app/src/main/java/io/legado/app/ui/book/read/Help.kt b/app/src/main/java/io/legado/app/ui/book/read/Help.kt index 247349a60..9d1be9f0c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/Help.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/Help.kt @@ -10,10 +10,10 @@ import android.view.WindowManager import io.legado.app.App import io.legado.app.constant.PreferKey import io.legado.app.help.ReadBookConfig +import io.legado.app.help.isTransparentStatusBar import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.isTransparentStatusBar object Help { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt index 19729f20b..972f89cb7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt @@ -11,6 +11,7 @@ import androidx.core.view.isVisible import io.legado.app.App import io.legado.app.R import io.legado.app.constant.PreferKey +import io.legado.app.help.isNightTheme import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.buttonDisabledColor import io.legado.app.service.help.ReadBook 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 7b920dfa7..8d81ace88 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.res.ColorStateList +import android.content.res.Configuration import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.provider.Settings @@ -126,8 +127,7 @@ fun Context.shareWithQr(title: String, text: String) { } } -val Context.isNightTheme: Boolean - get() = getPrefBoolean("isNightTheme") - -val Context.isTransparentStatusBar: Boolean - get() = getPrefBoolean("transparentStatusBar", true) +fun Context.getDarkModeStatus(): Boolean { + val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + return mode == Configuration.UI_MODE_NIGHT_YES +} \ No newline at end of file From c88b750360680bd151abcbb11078caed95f4b1e5 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 11:10:36 +0800 Subject: [PATCH 05/27] up --- app/src/main/java/io/legado/app/utils/ContextExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8d81ace88..d5fc85fdf 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -127,7 +127,7 @@ fun Context.shareWithQr(title: String, text: String) { } } -fun Context.getDarkModeStatus(): Boolean { +fun Context.sysIsDarkMode(): Boolean { val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK return mode == Configuration.UI_MODE_NIGHT_YES } \ No newline at end of file From df0b2e3ac38883da3a3fdfbd973a5255070484cb Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 11:40:31 +0800 Subject: [PATCH 06/27] up --- app/src/main/java/io/legado/app/constant/PreferKey.kt | 1 + app/src/main/java/io/legado/app/help/PrefExtensions.kt | 5 ++++- app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/pref_config_theme.xml | 6 ++++++ 4 files changed, 13 insertions(+), 1 deletion(-) 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 80efd5925..1cb5e2edc 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -21,4 +21,5 @@ object PreferKey { const val backupPath = "backupUri" const val threadCount = "threadCount" const val keepLight = "keep_light" + const val autoDarkMode = "autoDarkMode" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/PrefExtensions.kt b/app/src/main/java/io/legado/app/help/PrefExtensions.kt index 4b89939cc..4b8daf1e4 100644 --- a/app/src/main/java/io/legado/app/help/PrefExtensions.kt +++ b/app/src/main/java/io/legado/app/help/PrefExtensions.kt @@ -15,4 +15,7 @@ val Context.isShowRSS: Boolean get() = getPrefBoolean(PreferKey.showRss, true) val Context.threadCount: Int - get() = getPrefInt(PreferKey.threadCount, 16) \ No newline at end of file + get() = getPrefInt(PreferKey.threadCount, 16) + +val Context.autoDarkMode: Boolean + get() = getPrefBoolean(PreferKey.autoDarkMode, true) \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 12ceceb6d..1e352f8e4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -582,5 +582,7 @@ 文字太多,生成二维码失败 分享RSS源 分享书源 + 自动切换夜间模式 + 夜间模式跟随系统 diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index f9f3df963..853b2fb12 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -2,6 +2,12 @@ + Date: Mon, 27 Jan 2020 11:52:43 +0800 Subject: [PATCH 07/27] up --- app/src/main/java/io/legado/app/ui/README.md | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/README.md b/app/src/main/java/io/legado/app/ui/README.md index 18c04f014..a1a7a2313 100644 --- a/app/src/main/java/io/legado/app/ui/README.md +++ b/app/src/main/java/io/legado/app/ui/README.md @@ -1 +1,24 @@ -## 放置与界面有关的类 \ No newline at end of file +## 放置与界面有关的类 + +* about 关于界面 +* audio 音频播放界面 +* book\info 书籍信息查看 +* book\read 书籍阅读界面 +* book\search 搜索书籍界面 +* book\source 搜索书源界面 +* changecover 封面换源界面 +* changesource 换源界面 +* chapterlist 目录界面 +* config 配置界面 +* download 下载界面 +* explore 发现界面 +* filechooser 文件选择界面 +* importbook 书籍导入界面 +* main 主界面 +* qrcode 二维码扫描界面 +* replacerule 替换净化界面 +* rss\article 订阅条目界面 +* rss\read 订阅阅读界面 +* rss\source 订阅源界面 +* welcome 欢迎界面 +* widget 自定义插件 \ No newline at end of file From 81c6f449eb76ad1296436a13e1dceba8c4151208 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 19:32:00 +0800 Subject: [PATCH 08/27] up --- app/src/main/java/io/legado/app/App.kt | 12 +++--- .../main/java/io/legado/app/constant/Theme.kt | 5 +-- .../main/java/io/legado/app/help/AppConfig.kt | 41 +++++++++++++++++++ .../java/io/legado/app/help/PrefExtensions.kt | 21 ---------- .../java/io/legado/app/help/ReadBookConfig.kt | 14 +++---- .../main/java/io/legado/app/lib/theme/ATH.kt | 11 +++-- .../io/legado/app/service/DownloadService.kt | 4 +- .../java/io/legado/app/ui/book/read/Help.kt | 4 +- .../io/legado/app/ui/book/read/ReadMenu.kt | 6 +-- .../app/ui/book/search/SearchViewModel.kt | 4 +- .../ui/changesource/ChangeSourceViewModel.kt | 4 +- .../app/ui/config/OtherConfigFragment.kt | 6 +-- .../io/legado/app/ui/main/MainActivity.kt | 16 ++++---- 13 files changed, 83 insertions(+), 65 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/AppConfig.kt delete mode 100644 app/src/main/java/io/legado/app/help/PrefExtensions.kt diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 8565ceb9e..fed02381c 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -15,9 +15,9 @@ import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.data.AppDatabase import io.legado.app.help.ActivityHelp +import io.legado.app.help.AppConfig import io.legado.app.help.CrashHandler import io.legado.app.help.ReadBookConfig -import io.legado.app.help.isNightTheme import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.utils.getCompatColor @@ -50,7 +50,7 @@ class App : Application() { } if (!ThemeStore.isConfigured(this, versionCode)) applyTheme() - initNightTheme() + initNightMode() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannelId() @@ -67,7 +67,7 @@ class App : Application() { * 更新主题 */ fun applyTheme() { - if (isNightTheme) { + if (AppConfig.isNightTheme) { ThemeStore.editTheme(this) .primaryColor( getPrefInt("colorPrimaryNight", getCompatColor(R.color.shine_color)) @@ -98,11 +98,11 @@ class App : Application() { fun applyDayNight() { ReadBookConfig.upBg() applyTheme() - initNightTheme() + initNightMode() } - private fun initNightTheme() { - val targetMode = if (isNightTheme) { + private fun initNightMode() { + val targetMode = if (AppConfig.isNightTheme) { AppCompatDelegate.MODE_NIGHT_YES } else { AppCompatDelegate.MODE_NIGHT_NO diff --git a/app/src/main/java/io/legado/app/constant/Theme.kt b/app/src/main/java/io/legado/app/constant/Theme.kt index 975eaa802..b8587997b 100644 --- a/app/src/main/java/io/legado/app/constant/Theme.kt +++ b/app/src/main/java/io/legado/app/constant/Theme.kt @@ -1,14 +1,13 @@ package io.legado.app.constant -import io.legado.app.App -import io.legado.app.help.isNightTheme +import io.legado.app.help.AppConfig enum class Theme { Dark, Light, Auto; companion object { fun getTheme(): Theme { - return if (App.INSTANCE.isNightTheme) { + return if (AppConfig.isNightTheme) { Dark } else Light } diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt new file mode 100644 index 000000000..8190cfd79 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -0,0 +1,41 @@ +package io.legado.app.help + +import io.legado.app.App +import io.legado.app.constant.PreferKey +import io.legado.app.utils.getPrefBoolean +import io.legado.app.utils.getPrefInt +import io.legado.app.utils.putPrefBoolean +import io.legado.app.utils.putPrefInt + +object AppConfig { + + var isNightTheme: Boolean + get() = App.INSTANCE.getPrefBoolean("isNightTheme") + set(value) { + App.INSTANCE.putPrefBoolean("isNightTheme", value) + } + + var isTransparentStatusBar: Boolean + get() = App.INSTANCE.getPrefBoolean("transparentStatusBar") + set(value) { + App.INSTANCE.putPrefBoolean("transparentStatusBar", value) + } + + var isShowRSS: Boolean + get() = App.INSTANCE.getPrefBoolean(PreferKey.showRss) + set(value) { + App.INSTANCE.putPrefBoolean(PreferKey.showRss, value) + } + + var threadCount: Int + get() = App.INSTANCE.getPrefInt(PreferKey.threadCount) + set(value) { + App.INSTANCE.putPrefInt(PreferKey.threadCount, value) + } + + var autoDarkMode: Boolean + get() = App.INSTANCE.getPrefBoolean(PreferKey.autoDarkMode) + set(value) { + App.INSTANCE.putPrefBoolean(PreferKey.autoDarkMode, value) + } +} diff --git a/app/src/main/java/io/legado/app/help/PrefExtensions.kt b/app/src/main/java/io/legado/app/help/PrefExtensions.kt deleted file mode 100644 index 4b8daf1e4..000000000 --- a/app/src/main/java/io/legado/app/help/PrefExtensions.kt +++ /dev/null @@ -1,21 +0,0 @@ -package io.legado.app.help - -import android.content.Context -import io.legado.app.constant.PreferKey -import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.getPrefInt - -val Context.isNightTheme: Boolean - get() = getPrefBoolean("isNightTheme") - -val Context.isTransparentStatusBar: Boolean - get() = getPrefBoolean("transparentStatusBar", true) - -val Context.isShowRSS: Boolean - get() = getPrefBoolean(PreferKey.showRss, true) - -val Context.threadCount: Int - get() = getPrefInt(PreferKey.threadCount, 16) - -val Context.autoDarkMode: Boolean - get() = getPrefBoolean(PreferKey.autoDarkMode, true) \ No newline at end of file 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 328433c3c..311093c13 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -111,7 +111,7 @@ object ReadBookConfig { var textSize: Int = 15 ) { fun setBg(bgType: Int, bg: String) { - if (App.INSTANCE.isNightTheme) { + if (AppConfig.isNightTheme) { bgTypeNight = bgType bgStrNight = bg } else { @@ -121,7 +121,7 @@ object ReadBookConfig { } fun setTextColor(color: Int) { - if (App.INSTANCE.isNightTheme) { + if (AppConfig.isNightTheme) { textColorNight = "#${color.hexString}" } else { textColor = "#${color.hexString}" @@ -129,7 +129,7 @@ object ReadBookConfig { } fun setStatusIconDark(isDark: Boolean) { - if (App.INSTANCE.isNightTheme) { + if (AppConfig.isNightTheme) { darkStatusIconNight = isDark } else { darkStatusIcon = isDark @@ -137,7 +137,7 @@ object ReadBookConfig { } fun statusIconDark(): Boolean { - return if (App.INSTANCE.isNightTheme) { + return if (AppConfig.isNightTheme) { darkStatusIconNight } else { darkStatusIcon @@ -145,17 +145,17 @@ object ReadBookConfig { } fun textColor(): Int { - return if (App.INSTANCE.isNightTheme) Color.parseColor(textColorNight) + return if (AppConfig.isNightTheme) Color.parseColor(textColorNight) else Color.parseColor(textColor) } fun bgStr(): String { - return if (App.INSTANCE.isNightTheme) bgStrNight + return if (AppConfig.isNightTheme) bgStrNight else bgStr } fun bgType(): Int { - return if (App.INSTANCE.isNightTheme) bgTypeNight + return if (AppConfig.isNightTheme) bgTypeNight else bgType } diff --git a/app/src/main/java/io/legado/app/lib/theme/ATH.kt b/app/src/main/java/io/legado/app/lib/theme/ATH.kt index 91c7fcda2..c625e1807 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATH.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATH.kt @@ -16,8 +16,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView import io.legado.app.R -import io.legado.app.help.isNightTheme -import io.legado.app.help.isTransparentStatusBar +import io.legado.app.help.AppConfig import io.legado.app.utils.getCompatColor import kotlinx.android.synthetic.main.activity_main.view.* import org.jetbrains.anko.backgroundColor @@ -37,7 +36,7 @@ object ATH { } fun setStatusBarColorAuto(activity: Activity, fullScreen: Boolean) { - val isTransparentStatusBar = activity.isTransparentStatusBar + val isTransparentStatusBar = AppConfig.isTransparentStatusBar setStatusBarColor( activity, ThemeStore.statusBarColor(activity, isTransparentStatusBar), @@ -131,14 +130,14 @@ object ATH { fun setTint( view: View, @ColorInt color: Int, - isDark: Boolean = view.context.isNightTheme + isDark: Boolean = AppConfig.isNightTheme ) { TintHelper.setTintAuto(view, color, false, isDark) } fun setBackgroundTint( view: View, @ColorInt color: Int, - isDark: Boolean = view.context.isNightTheme + isDark: Boolean = AppConfig.isNightTheme ) { TintHelper.setTintAuto(view, color, true, isDark) } @@ -206,7 +205,7 @@ object ATH { .setSelectedColor(ThemeStore.accentColor(bottom_navigation_view.context)).create() itemIconTintList = colorStateList itemTextColor = colorStateList - itemBackgroundResource = when(context.isNightTheme) { + itemBackgroundResource = when (AppConfig.isNightTheme) { true -> R.drawable.item_bg_dark false -> R.drawable.item_bg_light } diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index 6770f7db7..9c2e7a2f3 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -9,10 +9,10 @@ import io.legado.app.base.BaseService import io.legado.app.constant.Action import io.legado.app.constant.AppConst import io.legado.app.constant.Bus +import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.IntentHelp import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.threadCount import io.legado.app.model.WebBook import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO @@ -21,7 +21,7 @@ import java.util.concurrent.Executors class DownloadService : BaseService() { private var searchPool = - Executors.newFixedThreadPool(App.INSTANCE.threadCount).asCoroutineDispatcher() + Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() private var tasks: ArrayList> = arrayListOf() private val handler = Handler() private var runnable: Runnable = Runnable { upDownload() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/Help.kt b/app/src/main/java/io/legado/app/ui/book/read/Help.kt index 9d1be9f0c..939377cb1 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/Help.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/Help.kt @@ -9,8 +9,8 @@ import android.view.Window import android.view.WindowManager import io.legado.app.App import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.help.ReadBookConfig -import io.legado.app.help.isTransparentStatusBar import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import io.legado.app.utils.getPrefBoolean @@ -43,7 +43,7 @@ object Help { } else { ATH.setLightStatusBarAuto( activity, - ThemeStore.statusBarColor(activity, activity.isTransparentStatusBar) + ThemeStore.statusBarColor(activity, AppConfig.isTransparentStatusBar) ) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt index 972f89cb7..0583056cd 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt @@ -11,7 +11,7 @@ import androidx.core.view.isVisible import io.legado.app.App import io.legado.app.R import io.legado.app.constant.PreferKey -import io.legado.app.help.isNightTheme +import io.legado.app.help.AppConfig import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.buttonDisabledColor import io.legado.app.service.help.ReadBook @@ -42,7 +42,7 @@ class ReadMenu : FrameLayout { init { callBack = activity as? CallBack inflate(context, R.layout.view_read_menu, this) - if (context.isNightTheme) { + if (AppConfig.isNightTheme) { fabNightTheme.setImageResource(R.drawable.ic_daytime) } else { fabNightTheme.setImageResource(R.drawable.ic_brightness) @@ -145,7 +145,7 @@ class ReadMenu : FrameLayout { //夜间模式 fabNightTheme.onClick { - context.putPrefBoolean("isNightTheme", !context.isNightTheme) + context.putPrefBoolean("isNightTheme", !AppConfig.isNightTheme) App.INSTANCE.applyDayNight() } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index 00148a7fd..125204062 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -6,8 +6,8 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.PreferKey import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchKeyword +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.threadCount import io.legado.app.model.WebBook import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefString @@ -18,7 +18,7 @@ import java.util.concurrent.Executors class SearchViewModel(application: Application) : BaseViewModel(application) { private var searchPool = - Executors.newFixedThreadPool(context.threadCount).asCoroutineDispatcher() + Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() private var task: Coroutine<*>? = null var callBack: CallBack? = null var searchKey: String = "" diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 81dfcebe9..3b16a4359 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -8,8 +8,8 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.SearchBook +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.threadCount import io.legado.app.model.WebBook import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -20,7 +20,7 @@ import java.util.concurrent.Executors class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { private var searchPool = - Executors.newFixedThreadPool(context.threadCount).asCoroutineDispatcher() + Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() var callBack: CallBack? = null val searchStateData = MutableLiveData() var name: String = "" 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 07a5a641d..2645002fe 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 @@ -12,8 +12,8 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.constant.Bus import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp -import io.legado.app.help.threadCount import io.legado.app.lib.theme.ATH import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.ui.filechooser.FileChooserDialog @@ -61,7 +61,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), .setTitle(getString(R.string.threads_num_title)) .setMaxValue(999) .setMinValue(1) - .setValue(requireContext().threadCount) + .setValue(AppConfig.threadCount) .show { requireContext().putPrefInt(PreferKey.threadCount, it) findPreference(PreferKey.threadCount)?.summary = @@ -125,7 +125,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), PreferKey.downloadPath -> getPrefString(PreferKey.downloadPath) ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath - PreferKey.threadCount -> requireContext().threadCount + PreferKey.threadCount -> AppConfig.threadCount else -> getPrefString(key) ?: "" } } 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 0b47b24f6..bdaf72031 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 @@ -16,8 +16,8 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.isShowRSS import io.legado.app.help.storage.Backup import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService @@ -47,7 +47,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) - bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = isShowRSS + bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS upVersion() } @@ -68,10 +68,10 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), fragmentList.add(RssFragment()) fragmentList.add(MyFragment()) } - if (isShowRSS && fragmentList.size < 4) { + if (AppConfig.isShowRSS && fragmentList.size < 4) { fragmentList.add(2, RssFragment()) } - if (!isShowRSS && fragmentList.size == 4) { + if (!AppConfig.isShowRSS && fragmentList.size == 4) { fragmentList.removeAt(2) } } @@ -89,7 +89,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), pagePosition = position when (position) { 0, 1, 3 -> bottom_navigation_view.menu.getItem(position).isChecked = true - 2 -> if (isShowRSS) { + 2 -> if (AppConfig.isShowRSS) { bottom_navigation_view.menu.getItem(position).isChecked = true } else { bottom_navigation_view.menu.getItem(3).isChecked = true @@ -147,10 +147,10 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), recreate() } observeEvent(Bus.SHOW_RSS) { - bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = isShowRSS + bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS upFragmentList() view_pager_main.adapter?.notifyDataSetChanged() - if (isShowRSS) { + if (AppConfig.isShowRSS) { view_pager_main.setCurrentItem(3, false) } } @@ -168,7 +168,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } override fun getCount(): Int { - return if (isShowRSS) 4 else 3 + return if (AppConfig.isShowRSS) 4 else 3 } } From c1d349f17633bfa321f82e462623cd54a0981477 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 19:40:44 +0800 Subject: [PATCH 09/27] up --- app/src/main/java/io/legado/app/App.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index fed02381c..2dc763432 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -22,6 +22,7 @@ import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.utils.getCompatColor import io.legado.app.utils.getPrefInt +import io.legado.app.utils.sysIsDarkMode @Suppress("DEPRECATION") class App : Application() { @@ -102,11 +103,18 @@ class App : Application() { } private fun initNightMode() { - val targetMode = if (AppConfig.isNightTheme) { - AppCompatDelegate.MODE_NIGHT_YES - } else { - AppCompatDelegate.MODE_NIGHT_NO - } + val targetMode = + if (AppConfig.autoDarkMode) { + if (sysIsDarkMode()) { + AppCompatDelegate.MODE_NIGHT_YES + } else { + AppCompatDelegate.MODE_NIGHT_NO + } + } else if (AppConfig.isNightTheme) { + AppCompatDelegate.MODE_NIGHT_YES + } else { + AppCompatDelegate.MODE_NIGHT_NO + } AppCompatDelegate.setDefaultNightMode(targetMode) } From 32d66a88a46cd991fb812120a332f14b1d72ccaa Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 19:45:52 +0800 Subject: [PATCH 10/27] up --- app/src/main/assets/updateLog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 3302fbfff..0aa0e4916 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,6 +3,9 @@ * 旧版数据导入教程: * 先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 +**2020/01/27** +* 添加根据系统主题切换夜间模式 + **2020/01/26** * 修复bug * 未加入书架可查看目录 From 7b10b267263a7600a523c8be711287dd244b2ab8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 20:29:04 +0800 Subject: [PATCH 11/27] Merge branch 'eink' of https://github.com/FadeApp/legado into FadeApp-eink # Conflicts: # app/src/main/java/io/legado/app/lib/theme/ATH.kt # app/src/main/java/io/legado/app/utils/ContextExtensions.kt --- app/build.gradle | 24 ++++++++++++------- .../main/java/io/legado/app/lib/theme/ATH.kt | 6 ++--- .../app/ui/config/ThemeConfigFragment.kt | 20 +++++++++++++++- app/src/main/res/values/strings.xml | 3 ++- app/src/main/res/xml/pref_main.xml | 6 +++++ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7eac96051..cae1dac53 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,13 +20,15 @@ def gitCommits = Integer.parseInt('git rev-list --count HEAD'.execute([], projec android { compileSdkVersion 29 signingConfigs { - myConfig { - storeFile file(RELEASE_STORE_FILE) - storePassword RELEASE_STORE_PASSWORD - keyAlias RELEASE_KEY_ALIAS - keyPassword RELEASE_KEY_PASSWORD - v1SigningEnabled true - v2SigningEnabled true + if (project.hasProperty("RELEASE_STORE_FILE")) { + myConfig { + storeFile file(RELEASE_STORE_FILE) + storePassword RELEASE_STORE_PASSWORD + keyAlias RELEASE_KEY_ALIAS + keyPassword RELEASE_KEY_PASSWORD + v1SigningEnabled true + v2SigningEnabled true + } } } defaultConfig { @@ -49,12 +51,16 @@ android { } buildTypes { release { - signingConfig signingConfigs.myConfig + if (project.hasProperty("RELEASE_STORE_FILE")) { + signingConfig signingConfigs.myConfig + } minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } debug { - signingConfig signingConfigs.myConfig + if (project.hasProperty("RELEASE_STORE_FILE")) { + signingConfig signingConfigs.myConfig + } applicationIdSuffix '.debug' versionNameSuffix 'debug' minifyEnabled false diff --git a/app/src/main/java/io/legado/app/lib/theme/ATH.kt b/app/src/main/java/io/legado/app/lib/theme/ATH.kt index c625e1807..1b17de91e 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATH.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATH.kt @@ -5,6 +5,7 @@ import android.app.Activity import android.app.ActivityManager import android.content.Context import android.graphics.Color +import android.graphics.drawable.ColorDrawable import android.os.Build import android.view.View import android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR @@ -205,10 +206,7 @@ object ATH { .setSelectedColor(ThemeStore.accentColor(bottom_navigation_view.context)).create() itemIconTintList = colorStateList itemTextColor = colorStateList - itemBackgroundResource = when (AppConfig.isNightTheme) { - true -> R.drawable.item_bg_dark - false -> R.drawable.item_bg_light - } + itemBackground = ColorDrawable(ThemeStore.primaryColor(bottomBar.context)) } } 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 6c87ff0af..477227214 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 @@ -20,7 +20,7 @@ import io.legado.app.utils.* class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { - val items = arrayOf("极简","曜夜","经典") + val items = arrayOf("极简", "曜夜", "经典", "黑白", "A屏黑") override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) @@ -124,6 +124,24 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar putPrefInt("colorBackground", getCompatColor(R.color.md_grey_100)) putPrefBoolean("isNightTheme", false) } + 3 -> { + putPrefInt("colorPrimary", getCompatColor(R.color.white)) + putPrefInt("colorAccent", getCompatColor(R.color.black)) + putPrefInt("colorBackground", getCompatColor(R.color.white)) + putPrefBoolean("isNightTheme", false) + } + 4 -> { + putPrefInt("colorPrimaryNight", getCompatColor(R.color.black)) + putPrefInt( + "colorAccentNight", + getCompatColor(R.color.md_grey_600) + ) + putPrefInt( + "colorBackgroundNight", + getCompatColor(R.color.black) + ) + putPrefBoolean("isNightTheme", true) + } } App.INSTANCE.applyDayNight() recreateActivities() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1e352f8e4..948cf5a47 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -77,7 +77,7 @@ 共%s个Text文件 加载中… 重试 - Web服务 + Web 服务 web编辑书源 http://%s:%d 离线下载 @@ -447,6 +447,7 @@ 切换显示样式 导入本地书籍需存储权限 夜间模式 + E-Ink 模式 本软件需要存储权限来存储备份书籍信息 再按一次退出程序 导入本地书籍需存储权限 diff --git a/app/src/main/res/xml/pref_main.xml b/app/src/main/res/xml/pref_main.xml index 297298599..f59dff9f7 100644 --- a/app/src/main/res/xml/pref_main.xml +++ b/app/src/main/res/xml/pref_main.xml @@ -18,6 +18,12 @@ android:title="@string/night_theme" app:iconSpaceReserved="false" /> + + Date: Mon, 27 Jan 2020 20:35:37 +0800 Subject: [PATCH 12/27] Merge branch 'eink' of https://github.com/FadeApp/legado into FadeApp-eink # Conflicts: # app/src/main/java/io/legado/app/lib/theme/ATH.kt # app/src/main/java/io/legado/app/utils/ContextExtensions.kt --- app/src/main/java/io/legado/app/help/AppConfig.kt | 3 +++ 1 file changed, 3 insertions(+) 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 8190cfd79..afb9b88fd 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -38,4 +38,7 @@ object AppConfig { set(value) { App.INSTANCE.putPrefBoolean(PreferKey.autoDarkMode, value) } + + val isEInkMode: Boolean + get() = App.INSTANCE.getPrefBoolean("isEInkMode") } From f95c07cc3c0827b4c42c1a3e5c5947270e337985 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 20:39:11 +0800 Subject: [PATCH 13/27] Merge branch 'eink' of https://github.com/FadeApp/legado into FadeApp-eink # Conflicts: # app/src/main/java/io/legado/app/lib/theme/ATH.kt # app/src/main/java/io/legado/app/utils/ContextExtensions.kt --- app/src/main/res/xml/pref_config_theme.xml | 7 ------- app/src/main/res/xml/pref_main.xml | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index 853b2fb12..4988e08bf 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -2,13 +2,6 @@ - - + + Date: Mon, 27 Jan 2020 20:45:31 +0800 Subject: [PATCH 14/27] =?UTF-8?q?=E8=BF=98=E6=98=AF=E6=84=9F=E8=A7=89?= =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E9=A2=9C=E8=89=B2=E5=A5=BD=E7=9C=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/lib/theme/ATH.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/lib/theme/ATH.kt b/app/src/main/java/io/legado/app/lib/theme/ATH.kt index 1b17de91e..c625e1807 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATH.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATH.kt @@ -5,7 +5,6 @@ import android.app.Activity import android.app.ActivityManager import android.content.Context import android.graphics.Color -import android.graphics.drawable.ColorDrawable import android.os.Build import android.view.View import android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR @@ -206,7 +205,10 @@ object ATH { .setSelectedColor(ThemeStore.accentColor(bottom_navigation_view.context)).create() itemIconTintList = colorStateList itemTextColor = colorStateList - itemBackground = ColorDrawable(ThemeStore.primaryColor(bottomBar.context)) + itemBackgroundResource = when (AppConfig.isNightTheme) { + true -> R.drawable.item_bg_dark + false -> R.drawable.item_bg_light + } } } From 0793f81385fee512da49bcec1fadb2343ebf0f87 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 27 Jan 2020 21:11:18 +0800 Subject: [PATCH 15/27] =?UTF-8?q?=E8=BF=98=E6=98=AF=E6=84=9F=E8=A7=89?= =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E9=A2=9C=E8=89=B2=E5=A5=BD=E7=9C=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 9 +-------- .../main/java/io/legado/app/help/AppConfig.kt | 19 +++++++++++++------ .../io/legado/app/ui/book/read/ReadMenu.kt | 2 +- .../app/ui/config/ThemeConfigFragment.kt | 13 +++++++------ .../io/legado/app/ui/main/my/MyFragment.kt | 2 +- .../io/legado/app/utils/FragmentExtensions.kt | 8 -------- app/src/main/res/values/arrays.xml | 12 ++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_main.xml | 17 ++++++----------- 9 files changed, 42 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 2dc763432..64d6abf9d 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -22,7 +22,6 @@ import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.utils.getCompatColor import io.legado.app.utils.getPrefInt -import io.legado.app.utils.sysIsDarkMode @Suppress("DEPRECATION") class App : Application() { @@ -104,13 +103,7 @@ class App : Application() { private fun initNightMode() { val targetMode = - if (AppConfig.autoDarkMode) { - if (sysIsDarkMode()) { - AppCompatDelegate.MODE_NIGHT_YES - } else { - AppCompatDelegate.MODE_NIGHT_NO - } - } else if (AppConfig.isNightTheme) { + if (AppConfig.isNightTheme) { AppCompatDelegate.MODE_NIGHT_YES } else { AppCompatDelegate.MODE_NIGHT_NO 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 afb9b88fd..f51d7b479 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -2,17 +2,24 @@ package io.legado.app.help import io.legado.app.App import io.legado.app.constant.PreferKey -import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.getPrefInt -import io.legado.app.utils.putPrefBoolean -import io.legado.app.utils.putPrefInt +import io.legado.app.utils.* object AppConfig { var isNightTheme: Boolean - get() = App.INSTANCE.getPrefBoolean("isNightTheme") + get() { + return when (App.INSTANCE.getPrefString("themeMode", "0")) { + "1" -> false + "2" -> true + else -> App.INSTANCE.sysIsDarkMode() + } + } set(value) { - App.INSTANCE.putPrefBoolean("isNightTheme", value) + if (value) { + App.INSTANCE.putPrefString("themeMode", "2") + } else { + App.INSTANCE.putPrefString("themeMode", "1") + } } var isTransparentStatusBar: Boolean diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt index 0583056cd..12bbb6d8b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt @@ -145,7 +145,7 @@ class ReadMenu : FrameLayout { //夜间模式 fabNightTheme.onClick { - context.putPrefBoolean("isNightTheme", !AppConfig.isNightTheme) + AppConfig.isNightTheme = !AppConfig.isNightTheme App.INSTANCE.applyDayNight() } 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 477227214..2ee2f6621 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 @@ -10,6 +10,7 @@ import androidx.preference.PreferenceFragmentCompat import io.legado.app.App import io.legado.app.R import io.legado.app.constant.Bus +import io.legado.app.help.AppConfig import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.yesButton @@ -110,25 +111,25 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar putPrefInt("colorPrimary", getCompatColor(R.color.md_grey_100)) putPrefInt("colorAccent", getCompatColor(R.color.lightBlue_color)) putPrefInt("colorBackground", getCompatColor(R.color.md_grey_100)) - putPrefBoolean("isNightTheme", false) + AppConfig.isNightTheme = false } 1 -> { putPrefInt("colorPrimaryNight", getCompatColor(R.color.shine_color)) putPrefInt("colorAccentNight", getCompatColor(R.color.lightBlue_color)) putPrefInt("colorBackgroundNight", getCompatColor(R.color.shine_color)) - putPrefBoolean("isNightTheme", true) + AppConfig.isNightTheme = true } 2 -> { putPrefInt("colorPrimary", getCompatColor(R.color.md_light_blue_500)) putPrefInt("colorAccent", getCompatColor(R.color.md_pink_800)) putPrefInt("colorBackground", getCompatColor(R.color.md_grey_100)) - putPrefBoolean("isNightTheme", false) + AppConfig.isNightTheme = false } 3 -> { putPrefInt("colorPrimary", getCompatColor(R.color.white)) putPrefInt("colorAccent", getCompatColor(R.color.black)) putPrefInt("colorBackground", getCompatColor(R.color.white)) - putPrefBoolean("isNightTheme", false) + AppConfig.isNightTheme = false } 4 -> { putPrefInt("colorPrimaryNight", getCompatColor(R.color.black)) @@ -140,7 +141,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar "colorBackgroundNight", getCompatColor(R.color.black) ) - putPrefBoolean("isNightTheme", true) + AppConfig.isNightTheme = true } } App.INSTANCE.applyDayNight() @@ -172,7 +173,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar } private fun upTheme(isNightTheme: Boolean) { - if (this.isNightTheme == isNightTheme) { + if (AppConfig.isNightTheme == isNightTheme) { App.INSTANCE.applyTheme() recreateActivities() } diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index 6d79f55d4..d3ee247c7 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -200,7 +200,7 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { when (key) { - "isNightTheme" -> App.INSTANCE.applyDayNight() + "themeMode" -> App.INSTANCE.applyDayNight() "webService" -> { if (requireContext().getPrefBoolean("webService")) { WebService.start(requireContext()) diff --git a/app/src/main/java/io/legado/app/utils/FragmentExtensions.kt b/app/src/main/java/io/legado/app/utils/FragmentExtensions.kt index f233929fb..b2379e89a 100644 --- a/app/src/main/java/io/legado/app/utils/FragmentExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/FragmentExtensions.kt @@ -54,17 +54,9 @@ fun Fragment.getCompatDrawable(@DrawableRes id: Int): Drawable? = requireContext fun Fragment.getCompatColorStateList(@ColorRes id: Int): ColorStateList? = requireContext().getCompatColorStateList(id) -val Fragment.isNightTheme: Boolean - get() = getPrefBoolean("isNightTheme") - -val Fragment.isTransparentStatusBar: Boolean - get() = getPrefBoolean("transparentStatusBar") - - inline fun Fragment.startActivity(vararg params: Pair) = AnkoInternals.internalStartActivity(requireActivity(), T::class.java, params) - inline fun Fragment.startActivityForResult(requestCode: Int, vararg params: Pair) = startActivityForResult(AnkoInternals.createIntent(requireActivity(), T::class.java, params), requestCode) diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 67bb70836..b8cea9e3b 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -63,6 +63,18 @@ @string/jf_convert_f + + 跟随系统 + 亮色主题 + 暗色主题 + + + + 0 + 1 + 2 + + 自动 黑色 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 948cf5a47..43a0e6019 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -433,6 +433,7 @@ 扫描二维码 选中时点击可弹出菜单 主题 + 主题模式 默认主题 恢复主题为默认配色 加入QQ群 diff --git a/app/src/main/res/xml/pref_main.xml b/app/src/main/res/xml/pref_main.xml index bd17ae2ee..29e66e13e 100644 --- a/app/src/main/res/xml/pref_main.xml +++ b/app/src/main/res/xml/pref_main.xml @@ -12,17 +12,12 @@ android:title="@string/replace_purify" app:iconSpaceReserved="false" /> - - - Date: Mon, 27 Jan 2020 21:13:34 +0800 Subject: [PATCH 16/27] =?UTF-8?q?=E8=BF=98=E6=98=AF=E6=84=9F=E8=A7=89?= =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E9=A2=9C=E8=89=B2=E5=A5=BD=E7=9C=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 0aa0e4916..80066583d 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -5,6 +5,7 @@ **2020/01/27** * 添加根据系统主题切换夜间模式 +* 合并Modificator提交的代码 **2020/01/26** * 修复bug From 851a5bcb31eff46629f81de902e935adc60322bd Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 11:25:28 +0800 Subject: [PATCH 17/27] =?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/utils/ZipUtils.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/ZipUtils.kt b/app/src/main/java/io/legado/app/utils/ZipUtils.kt index eab9951c7..44d547e5c 100644 --- a/app/src/main/java/io/legado/app/utils/ZipUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ZipUtils.kt @@ -57,7 +57,7 @@ object ZipUtils { } return true } finally { - if (zos != null) { + zos?.let { zos.finish() zos.close() } @@ -89,7 +89,7 @@ object ZipUtils { } return true } finally { - if (zos != null) { + zos?.let { zos.finish() zos.close() } @@ -261,7 +261,7 @@ object ZipUtils { val files = ArrayList() val zip = ZipFile(zipFile) val entries = zip.entries() - try { + zip.use { if (isSpace(keyword)) { while (entries.hasMoreElements()) { val entry = entries.nextElement() as ZipEntry @@ -285,8 +285,6 @@ object ZipUtils { } } } - } finally { - zip.close() } return files } From 660cedea6878a93101f6dcee69c65e85e7ea1552 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 11:26:28 +0800 Subject: [PATCH 18/27] =?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/utils/ViewExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt index 81cdafe81..403f0d40c 100644 --- a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt @@ -24,7 +24,7 @@ private tailrec fun getCompatActivity(context: Context?): AppCompatActivity? { val View.activity: AppCompatActivity? get() = getCompatActivity(context) -inline fun View.hideSoftInput() = run { +fun View.hideSoftInput() = run { val imm = App.INSTANCE.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager imm?.let { imm.hideSoftInputFromWindow(this.windowToken, 0) From 93d4bba524f1121024d3ee06cebe1f5333e78185 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 11:30:20 +0800 Subject: [PATCH 19/27] =?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/utils/StringUtils.kt | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index c9f80866f..6d70b244b 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -41,16 +41,18 @@ object StringUtils { //将时间转换成日期 fun dateConvert(time: Long, pattern: String): String { val date = Date(time) - @SuppressLint("SimpleDateFormat") val format = SimpleDateFormat(pattern) + @SuppressLint("SimpleDateFormat") + val format = SimpleDateFormat(pattern) return format.format(date) } //将日期转换成昨天、今天、明天 fun dateConvert(source: String, pattern: String): String { - @SuppressLint("SimpleDateFormat") val format = SimpleDateFormat(pattern) + @SuppressLint("SimpleDateFormat") + val format = SimpleDateFormat(pattern) val calendar = Calendar.getInstance() try { - val date = format.parse(source) + val date = format.parse(source) ?: return "" val curTime = calendar.timeInMillis calendar.time = date //将MISC 转换成 sec @@ -62,13 +64,18 @@ object StringUtils { //如果没有时间 if (oldHour == 0) { //比日期:昨天今天和明天 - if (difDate == 0L) { - return "今天" - } else if (difDate < DAY_OF_YESTERDAY) { - return "昨天" - } else { - @SuppressLint("SimpleDateFormat") val convertFormat = SimpleDateFormat("yyyy-MM-dd") - return convertFormat.format(date) + return when { + difDate == 0L -> { + "今天" + } + difDate < DAY_OF_YESTERDAY -> { + "昨天" + } + else -> { + @SuppressLint("SimpleDateFormat") + val convertFormat = SimpleDateFormat("yyyy-MM-dd") + convertFormat.format(date) + } } } @@ -78,7 +85,8 @@ object StringUtils { difHour < HOUR_OF_DAY -> difHour.toString() + "小时前" difDate < DAY_OF_YESTERDAY -> "昨天" else -> { - @SuppressLint("SimpleDateFormat") val convertFormat = SimpleDateFormat("yyyy-MM-dd") + @SuppressLint("SimpleDateFormat") + val convertFormat = SimpleDateFormat("yyyy-MM-dd") convertFormat.format(date) } } @@ -254,9 +262,9 @@ object StringUtils { "(?i)<(br[\\s/]*|/*p.*?|/*div.*?)>".toRegex(), "\n" )// 替换特定标签为换行符 - .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符 - .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 - .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字 - .replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 + .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符 + .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 + .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字 + .replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 } } From 25aba898bfef6bc75c2136587cdccb2a241fba15 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 11:39:06 +0800 Subject: [PATCH 20/27] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/utils/StringExtensions.kt | 63 ++++++++++--------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 987a0fce8..19319d3ba 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -4,34 +4,41 @@ package io.legado.app.utils fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() -fun String?.isAbsUrl() = if (this.isNullOrBlank()) false else this.startsWith("http://", true) - || this.startsWith("https://", true) - -fun String?.isJson(): Boolean = this?.run { - val str = this.trim() - when { - str.startsWith("{") && str.endsWith("}") -> true - str.startsWith("[") && str.endsWith("]") -> true - else -> false - } -} ?: false - -fun String?.isJsonObject(): Boolean = this?.run { - val str = this.trim() - str.startsWith("{") && str.endsWith("}") -} ?: false - -fun String?.isJsonArray(): Boolean = this?.run { - val str = this.trim() - str.startsWith("[") && str.endsWith("]") -} ?: false - -fun String?.htmlFormat(): String = if (this.isNullOrBlank()) "" else - this.replace("(?i)<(br[\\s/]*|/*p\\b.*?|/*div\\b.*?)>".toRegex(), "\n")// 替换特定标签为换行符 - .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符 - .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 - .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字 - .replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 +fun String?.isAbsUrl() = + this?.let { + it.startsWith("http://", true) + || it.startsWith("https://", true) + } ?: false + +fun String?.isJson(): Boolean = + this?.run { + val str = this.trim() + when { + str.startsWith("{") && str.endsWith("}") -> true + str.startsWith("[") && str.endsWith("]") -> true + else -> false + } + } ?: false + +fun String?.isJsonObject(): Boolean = + this?.run { + val str = this.trim() + str.startsWith("{") && str.endsWith("}") + } ?: false + +fun String?.isJsonArray(): Boolean = + this?.run { + val str = this.trim() + str.startsWith("[") && str.endsWith("]") + } ?: false + +fun String?.htmlFormat(): String = + this?.replace("(?i)<(br[\\s/]*|/*p\\b.*?|/*div\\b.*?)>".toRegex(), "\n") + ?.replace("<[script>]*.*?>| ".toRegex(), "") + ?.replace("\\s*\\n+\\s*".toRegex(), "\n  ") + ?.replace("^[\\n\\s]+".toRegex(), "  ") + ?.replace("[\\n\\s]+$".toRegex(), "") + ?: "" fun String.splitNotBlank(vararg delimiter: String): Array = run { this.split(*delimiter).map { it.trim() }.filterNot { it.isBlank() }.toTypedArray() From 722f71c93110885b4e34f46111b5408a87830aee Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 11:47:24 +0800 Subject: [PATCH 21/27] =?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/utils/ACache.kt | 10 +++++----- app/src/main/java/io/legado/app/utils/FileUtils.kt | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/ACache.kt b/app/src/main/java/io/legado/app/utils/ACache.kt index 44f96631c..b173dc612 100644 --- a/app/src/main/java/io/legado/app/utils/ACache.kt +++ b/app/src/main/java/io/legado/app/utils/ACache.kt @@ -171,7 +171,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) * @return JSONObject数据 */ fun getAsJSONObject(key: String): JSONObject? { - val json = getAsString(key) + val json = getAsString(key) ?: return null return try { JSONObject(json) } catch (e: Exception) { @@ -311,17 +311,17 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) fun getAsObject(key: String): Any? { val data = getAsBinary(key) if (data != null) { - var bais: ByteArrayInputStream? = null + var bis: ByteArrayInputStream? = null var ois: ObjectInputStream? = null try { - bais = ByteArrayInputStream(data) - ois = ObjectInputStream(bais) + bis = ByteArrayInputStream(data) + ois = ObjectInputStream(bis) return ois.readObject() } catch (e: Exception) { e.printStackTrace() } finally { try { - bais?.close() + bis?.close() } catch (e: IOException) { e.printStackTrace() } diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 39148fe76..23cdcabab 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -43,7 +43,7 @@ object FileUtils { val storageVolumeClazz = Class.forName("android.os.storage.StorageVolume") val getPath = storageVolumeClazz.getMethod("getPath") - val invokeVolumeList = getVolumeList.invoke(storageManager) + val invokeVolumeList = getVolumeList.invoke(storageManager) ?: return null val length = Array.getLength(invokeVolumeList) val list = ArrayList() From 8aa3e29afa1c4da04ba1e47247182a501be3afa4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 19:41:15 +0800 Subject: [PATCH 22/27] =?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/ui/config/OtherConfigFragment.kt | 57 ++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) 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 2645002fe..7a0d7fb0f 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,6 +1,8 @@ package io.legado.app.ui.config +import android.app.Activity.RESULT_OK import android.content.ComponentName +import android.content.Intent import android.content.SharedPreferences import android.content.pm.PackageManager import android.os.Bundle @@ -14,6 +16,8 @@ import io.legado.app.constant.Bus import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.theme.ATH import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.ui.filechooser.FileChooserDialog @@ -26,7 +30,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener { - private val downloadPath = 25324 + private val requestCodeDownloadPath = 25324 private val packageManager = App.INSTANCE.packageManager private val componentName = ComponentName( App.INSTANCE, @@ -67,12 +71,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), findPreference(PreferKey.threadCount)?.summary = getString(R.string.threads_num, it.toString()) } - PreferKey.downloadPath -> FileChooserDialog.show( - childFragmentManager, - downloadPath, - mode = FileChooserDialog.DIRECTORY, - initPath = getPreferenceString(PreferKey.downloadPath).toString() - ) + PreferKey.downloadPath -> selectDownloadPathSys() PreferKey.cleanCache -> { BookHelp.clearCache() toast(R.string.clear_cache_success) @@ -148,7 +147,49 @@ class OtherConfigFragment : PreferenceFragmentCompat(), } } + private fun selectDownloadPathSys() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, requestCodeDownloadPath) + } catch (e: Exception) { + selectDownloadPath() + } + } + + private fun selectDownloadPath() { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + FileChooserDialog.show( + childFragmentManager, + requestCodeDownloadPath, + mode = FileChooserDialog.DIRECTORY, + initPath = getPreferenceString(PreferKey.downloadPath).toString() + ) + } + .request() + } + override fun onFilePicked(requestCode: Int, currentPath: String) { - putPrefString(PreferKey.downloadPath, currentPath) + if (requestCode == requestCodeDownloadPath) { + putPrefString(PreferKey.downloadPath, currentPath) + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + requestCodeDownloadPath -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.downloadPath, uri.toString()) + } + } + } } } \ No newline at end of file From 43d3b981df594f41f3469776be8dd869db875704 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 20:00:51 +0800 Subject: [PATCH 23/27] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index ff5ee0b7d..9225cf0ac 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -1,5 +1,6 @@ package io.legado.app.help +import android.net.Uri import io.legado.app.App import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book @@ -26,6 +27,16 @@ object BookHelp { ?: App.INSTANCE.cacheDir.absolutePath } + private val dwnloadUri get() = Uri.parse(downloadPath) + + private fun bookFolderName(book: Book): String { + return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl) + } + + private fun bookChapterName(bookChapter: BookChapter): String { + return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode(bookChapter.title)) + } + private fun getBookCachePath(): String { return "$downloadPath${File.separator}book_cache" } @@ -86,14 +97,11 @@ object BookHelp { } private fun getBookFolder(book: Book): String { - val bookFolder = formatFolderName(book.name + MD5Utils.md5Encode16(book.bookUrl)) - return "${getBookCachePath()}${File.separator}$bookFolder" + return "${getBookCachePath()}${File.separator}${bookFolderName(book)}" } private fun getChapterPath(book: Book, bookChapter: BookChapter): String { - val chapterFile = - String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode(bookChapter.title)) - return "${getBookFolder(book)}${File.separator}$chapterFile.nb" + return "${getBookFolder(book)}${File.separator}${bookChapterName(bookChapter)}.nb" } private fun formatFolderName(folderName: String): String { From 95ef2d9a0393baef7eb53b881d3ed955de7e485f Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 21:47:32 +0800 Subject: [PATCH 24/27] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 43 +++++++++++-------- .../java/io/legado/app/utils/DocumentUtils.kt | 30 +++++++++++++ .../java/io/legado/app/utils/UriExtensions.kt | 3 ++ 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 9225cf0ac..fa020e466 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -1,20 +1,19 @@ package io.legado.app.help import android.net.Uri +import androidx.documentfile.provider.DocumentFile import io.legado.app.App import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.ReplaceRule -import io.legado.app.utils.MD5Utils -import io.legado.app.utils.getPrefInt -import io.legado.app.utils.getPrefString +import io.legado.app.utils.* import org.apache.commons.text.similarity.JaccardSimilarity import java.io.File import kotlin.math.min object BookHelp { - + private const val cacheFolderName = "book_cache" private var downloadPath: String = App.INSTANCE.getPrefString(PreferKey.downloadPath) ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath @@ -27,7 +26,7 @@ object BookHelp { ?: App.INSTANCE.cacheDir.absolutePath } - private val dwnloadUri get() = Uri.parse(downloadPath) + private val downloadUri get() = Uri.parse(downloadPath) private fun bookFolderName(book: Book): String { return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl) @@ -38,7 +37,7 @@ object BookHelp { } private fun getBookCachePath(): String { - return "$downloadPath${File.separator}book_cache" + return "$downloadPath${File.separator}$cacheFolderName" } fun clearCache() { @@ -49,15 +48,25 @@ object BookHelp { @Synchronized fun saveContent(book: Book, bookChapter: BookChapter, content: String) { if (content.isEmpty()) return - FileHelp.getFolder(getBookFolder(book)).listFiles()?.forEach { - if (it.name.startsWith(String.format("%05d", bookChapter.index))) { - it.delete() - return@forEach + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + DocumentUtils.createFileIfNotExist( + it, + bookChapterName(bookChapter), + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ) } + } else { + FileHelp.getFolder(getBookFolder(book)).listFiles()?.forEach { + if (it.name.startsWith(String.format("%05d", bookChapter.index))) { + it.delete() + return@forEach + } + } + val filePath = getChapterPath(book, bookChapter) + val file = FileHelp.getFile(filePath) + file.writeText(content) } - val filePath = getChapterPath(book, bookChapter) - val file = FileHelp.getFile(filePath) - file.writeText(content) } fun getChapterCount(book: Book): Int { @@ -132,16 +141,16 @@ object BookHelp { } var newIndex = 0 - val jaccardSimilarity = JaccardSimilarity() + val jSimilarity = JaccardSimilarity() var similarity = if (chapters.size > index) { - jaccardSimilarity.apply(title, chapters[index].title) + jSimilarity.apply(title, chapters[index].title) } else 0.0 if (similarity == 1.0) { return index } else { for (i in 1..50) { if (index - i in chapters.indices) { - jaccardSimilarity.apply(title, chapters[index - i].title).let { + jSimilarity.apply(title, chapters[index - i].title).let { if (it > similarity) { similarity = it newIndex = index - i @@ -152,7 +161,7 @@ object BookHelp { } } if (index + i in chapters.indices) { - jaccardSimilarity.apply(title, chapters[index + i].title).let { + jSimilarity.apply(title, chapters[index + i].title).let { if (it > similarity) { similarity = it newIndex = index + i diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index 3929874ef..99cf03f5e 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -2,9 +2,39 @@ package io.legado.app.utils import android.content.Context import android.net.Uri +import androidx.documentfile.provider.DocumentFile object DocumentUtils { + fun createFileIfNotExist( + root: DocumentFile, + fileName: String, + mimeType: String = "", + vararg subDirs: String + ): DocumentFile? { + val parent: DocumentFile? = createFileIfNotExist(root, *subDirs) + return parent?.createFile(mimeType, fileName) + } + + fun createFileIfNotExist(root: DocumentFile, vararg subDirs: String): DocumentFile? { + var parent: DocumentFile? = root + for (subDirName in subDirs) { + val subDir = parent?.findFile(subDirName) + ?: parent?.createDirectory(subDirName) + parent = subDir + } + return parent + } + + fun getDirDocument(root: DocumentFile, vararg subDirs: String): DocumentFile? { + var parent = root + for (subDirName in subDirs) { + val subDir = parent.findFile(subDirName) + parent = subDir ?: return null + } + return parent + } + @JvmStatic @Throws(Exception::class) fun writeText(context: Context, data: String, fileUri: Uri): Boolean { diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index 297a5a612..49b76b439 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -5,6 +5,9 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import java.io.File +fun Uri.isDocumentUri(context: Context): Boolean { + return DocumentFile.isDocumentUri(context, this) +} @Throws(Exception::class) fun Uri.readBytes(context: Context): ByteArray? { From 5407cd5f3b0989f9835a9e9fc043f01e89d9c9c3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 22:33:46 +0800 Subject: [PATCH 25/27] =?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/base/BaseActivity.kt | 4 +- .../main/java/io/legado/app/help/BookHelp.kt | 10 +- .../main/java/io/legado/app/help/FileHelp.kt | 58 ---------- .../java/io/legado/app/help/storage/Backup.kt | 22 ++-- .../io/legado/app/help/storage/Restore.kt | 21 ++-- .../io/legado/app/help/storage/WebDavHelp.kt | 8 +- .../app/service/HttpReadAloudService.kt | 10 +- .../ui/book/read/config/BgTextConfigDialog.kt | 3 +- .../book/source/manage/BookSourceViewModel.kt | 3 +- .../ui/replacerule/ReplaceRuleViewModel.kt | 4 +- .../rss/source/manage/RssSourceViewModel.kt | 6 +- .../app/ui/widget/font/FontSelectDialog.kt | 15 +-- .../main/java/io/legado/app/utils/ACache.kt | 1 + .../java/io/legado/app/utils/FileUtils.kt | 106 ++++++++---------- .../main/java/io/legado/app/utils/LogUtils.kt | 5 +- .../java/io/legado/app/utils/StringUtils.kt | 2 +- 16 files changed, 101 insertions(+), 177 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/help/FileHelp.kt diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 2517cc616..048036892 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -52,8 +52,8 @@ abstract class BaseActivity( } ?: super.onCreateOptionsMenu(menu) } - override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean { - menu?.applyOpenTint(this) + override fun onMenuOpened(featureId: Int, menu: Menu): Boolean { + menu.applyOpenTint(this) return super.onMenuOpened(featureId, menu) } diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index fa020e466..bce22a823 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -41,8 +41,8 @@ object BookHelp { } fun clearCache() { - FileHelp.deleteFile(getBookCachePath()) - FileHelp.getFolder(getBookCachePath()) + FileUtils.deleteFile(getBookCachePath()) + FileUtils.getFolder(getBookCachePath()) } @Synchronized @@ -57,20 +57,20 @@ object BookHelp { ) } } else { - FileHelp.getFolder(getBookFolder(book)).listFiles()?.forEach { + FileUtils.getFolder(getBookFolder(book)).listFiles()?.forEach { if (it.name.startsWith(String.format("%05d", bookChapter.index))) { it.delete() return@forEach } } val filePath = getChapterPath(book, bookChapter) - val file = FileHelp.getFile(filePath) + val file = FileUtils.getFile(filePath) file.writeText(content) } } fun getChapterCount(book: Book): Int { - return FileHelp.getFolder(getBookFolder(book)).list()?.size ?: 0 + return FileUtils.getFolder(getBookFolder(book)).list()?.size ?: 0 } fun hasContent(book: Book, bookChapter: BookChapter): Boolean { diff --git a/app/src/main/java/io/legado/app/help/FileHelp.kt b/app/src/main/java/io/legado/app/help/FileHelp.kt deleted file mode 100644 index 5a7678563..000000000 --- a/app/src/main/java/io/legado/app/help/FileHelp.kt +++ /dev/null @@ -1,58 +0,0 @@ -package io.legado.app.help - -import io.legado.app.App -import java.io.File -import java.io.IOException - -object FileHelp { - - - //获取文件夹 - fun getFolder(filePath: String): File { - val file = File(filePath) - //如果文件夹不存在,就创建它 - if (!file.exists()) { - file.mkdirs() - } - return file - } - - //获取文件 - @Synchronized - fun getFile(filePath: String): File { - val file = File(filePath) - try { - if (!file.exists()) { - //创建父类文件夹 - getFolder(file.parent) - //创建文件 - file.createNewFile() - } - } catch (e: IOException) { - e.printStackTrace() - } - return file - } - - fun getCachePath(): String { - return App.INSTANCE.externalCacheDir?.absolutePath - ?: App.INSTANCE.cacheDir.absolutePath - } - - //递归删除文件夹下的数据 - @Synchronized - fun deleteFile(filePath: String) { - val file = File(filePath) - if (!file.exists()) return - - if (file.isDirectory) { - val files = file.listFiles() - files?.forEach { subFile -> - val path = subFile.path - deleteFile(path) - } - } - //删除文件 - file.delete() - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 90bf3b9e2..8a635bafc 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -4,7 +4,6 @@ import android.content.Context import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.App -import io.legado.app.help.FileHelp import io.legado.app.help.ReadBookConfig import io.legado.app.utils.DocumentUtils import io.legado.app.utils.FileUtils @@ -48,37 +47,40 @@ object Backup { App.db.bookDao().allBooks.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - FileHelp.getFile(backupPath + File.separator + "bookshelf.json").writeText(json) + FileUtils.getFile(backupPath + File.separator + "bookshelf.json") + .writeText(json) } } App.db.bookGroupDao().all().let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - FileHelp.getFile(backupPath + File.separator + "bookGroup.json").writeText(json) + FileUtils.getFile(backupPath + File.separator + "bookGroup.json") + .writeText(json) } } App.db.bookSourceDao().all.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - FileHelp.getFile(backupPath + File.separator + "bookSource.json") + FileUtils.getFile(backupPath + File.separator + "bookSource.json") .writeText(json) } } App.db.rssSourceDao().all.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - FileHelp.getFile(backupPath + File.separator + "rssSource.json").writeText(json) + FileUtils.getFile(backupPath + File.separator + "rssSource.json") + .writeText(json) } } App.db.replaceRuleDao().all.let { if (it.isNotEmpty()) { val json = GSON.toJson(it) - FileHelp.getFile(backupPath + File.separator + "replaceRule.json") + FileUtils.getFile(backupPath + File.separator + "replaceRule.json") .writeText(json) } } GSON.toJson(ReadBookConfig.configList)?.let { - FileHelp.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName) + FileUtils.getFile(backupPath + File.separator + ReadBookConfig.readConfigFileName) .writeText(it) } Preferences.getSharedPreferences(App.INSTANCE, backupPath, "config")?.let { sp -> @@ -111,7 +113,7 @@ object Backup { doc?.let { DocumentUtils.writeText( context, - FileHelp.getFile(backupPath + File.separator + fileName).readText(), + FileUtils.getFile(backupPath + File.separator + fileName).readText(), doc.uri ) } @@ -122,8 +124,8 @@ object Backup { private fun copyBackup() { try { for (fileName in backupFileNames) { - FileHelp.getFile(backupPath + File.separator + "bookshelf.json") - .copyTo(FileHelp.getFile(legadoPath + File.separator + "bookshelf.json"), true) + FileUtils.getFile(backupPath + File.separator + "bookshelf.json") + .copyTo(FileUtils.getFile(legadoPath + File.separator + "bookshelf.json"), true) } } catch (e: Exception) { e.printStackTrace() 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 15c14051c..6741e6830 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 @@ -11,7 +11,6 @@ import com.jayway.jsonpath.ParseContext import io.legado.app.App import io.legado.app.constant.AppConst import io.legado.app.data.entities.* -import io.legado.app.help.FileHelp import io.legado.app.help.ReadBookConfig import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO @@ -38,7 +37,7 @@ object Restore { for (fileName in Backup.backupFileNames) { if (doc.name == fileName) { DocumentUtils.readText(context, doc.uri)?.let { - FileHelp.getFile(Backup.backupPath + File.separator + fileName) + FileUtils.getFile(Backup.backupPath + File.separator + fileName) .writeText(it) } } @@ -51,7 +50,7 @@ object Restore { suspend fun restore(path: String) { withContext(IO) { try { - val file = FileHelp.getFile(path + File.separator + "bookshelf.json") + val file = FileUtils.getFile(path + File.separator + "bookshelf.json") val json = file.readText() GSON.fromJsonArray(json)?.let { App.db.bookDao().insert(*it.toTypedArray()) @@ -60,7 +59,7 @@ object Restore { e.printStackTrace() } try { - val file = FileHelp.getFile(path + File.separator + "bookGroup.json") + val file = FileUtils.getFile(path + File.separator + "bookGroup.json") val json = file.readText() GSON.fromJsonArray(json)?.let { App.db.bookGroupDao().insert(*it.toTypedArray()) @@ -69,7 +68,7 @@ object Restore { e.printStackTrace() } try { - val file = FileHelp.getFile(path + File.separator + "bookSource.json") + val file = FileUtils.getFile(path + File.separator + "bookSource.json") val json = file.readText() GSON.fromJsonArray(json)?.let { App.db.bookSourceDao().insert(*it.toTypedArray()) @@ -78,7 +77,7 @@ object Restore { e.printStackTrace() } try { - val file = FileHelp.getFile(path + File.separator + "rssSource.json") + val file = FileUtils.getFile(path + File.separator + "rssSource.json") val json = file.readText() GSON.fromJsonArray(json)?.let { App.db.rssSourceDao().insert(*it.toTypedArray()) @@ -87,7 +86,7 @@ object Restore { e.printStackTrace() } try { - val file = FileHelp.getFile(path + File.separator + "replaceRule.json") + val file = FileUtils.getFile(path + File.separator + "replaceRule.json") val json = file.readText() GSON.fromJsonArray(json)?.let { App.db.replaceRuleDao().insert(*it.toTypedArray()) @@ -97,7 +96,7 @@ object Restore { } try { val file = - FileHelp.getFile(path + File.separator + ReadBookConfig.readConfigFileName) + FileUtils.getFile(path + File.separator + ReadBookConfig.readConfigFileName) val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + ReadBookConfig.readConfigFileName) if (file.exists()) { @@ -126,7 +125,7 @@ object Restore { GlobalScope.launch(IO) { try {// 导入书架 val shelfFile = - FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") + FileUtils.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") val json = shelfFile.readText() val importCount = importOldBookshelf(json) withContext(Main) { @@ -140,7 +139,7 @@ object Restore { try {// Book source val sourceFile = - FileHelp.getFile(Backup.defaultPath + File.separator + "myBookSource.json") + FileUtils.getFile(Backup.defaultPath + File.separator + "myBookSource.json") val json = sourceFile.readText() val importCount = importOldSource(json) withContext(Main) { @@ -154,7 +153,7 @@ object Restore { try {// Replace rules val ruleFile = - FileHelp.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json") + FileUtils.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json") val json = ruleFile.readText() val importCount = importOldReplaceRule(json) withContext(Main) { diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index 7d905299d..fb2e09a15 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -2,10 +2,10 @@ package io.legado.app.help.storage import android.content.Context import io.legado.app.App -import io.legado.app.help.FileHelp import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.http.HttpAuth +import io.legado.app.utils.FileUtils import io.legado.app.utils.ZipUtils import io.legado.app.utils.getPrefString import kotlinx.coroutines.Dispatchers.IO @@ -17,9 +17,9 @@ import java.util.* import kotlin.math.min object WebDavHelp { - private val zipFilePath = FileHelp.getCachePath() + "/backup" + ".zip" + private val zipFilePath = FileUtils.getCachePath() + "/backup" + ".zip" private val unzipFilesPath by lazy { - FileHelp.getCachePath() + FileUtils.getCachePath() } private fun getWebDavUrl(): String? { @@ -86,7 +86,7 @@ object WebDavHelp { for (i in 0 until paths.size) { paths[i] = path + File.separator + paths[i] } - FileHelp.deleteFile(zipFilePath) + FileUtils.deleteFile(zipFilePath) if (ZipUtils.zipFiles(paths, zipFilePath)) { WebDav(getWebDavUrl() + "legado").makeAsDir() val putUrl = getWebDavUrl() + "legado/backup" + diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index c2467c1eb..b12fef048 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -4,14 +4,10 @@ import android.app.PendingIntent import android.media.MediaPlayer import io.legado.app.constant.Bus import io.legado.app.data.api.IHttpPostApi -import io.legado.app.help.FileHelp import io.legado.app.help.IntentHelp import io.legado.app.help.http.HttpHelper import io.legado.app.service.help.ReadBook -import io.legado.app.utils.LogUtils -import io.legado.app.utils.getPrefInt -import io.legado.app.utils.getPrefString -import io.legado.app.utils.postEvent +import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job import kotlinx.coroutines.isActive @@ -65,7 +61,7 @@ class HttpReadAloudService : BaseReadAloudService(), private fun downloadAudio() { job = launch(IO) { - FileHelp.deleteFile(ttsFolder) + FileUtils.deleteFile(ttsFolder) for (index in 0 until contentList.size) { if (isActive) { val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com") @@ -104,7 +100,7 @@ class HttpReadAloudService : BaseReadAloudService(), } private fun getSpeakFile(index: Int = nowSpeak): File { - return FileHelp.getFile("${ttsFolder}${File.separator}${index}.mp3") + return FileUtils.getFile("${ttsFolder}${File.separator}${index}.mp3") } private fun getAudioBody(content: String): Map { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 2b4a7dc4f..460e234c0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -21,7 +21,6 @@ import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.Bus -import io.legado.app.help.FileHelp import io.legado.app.help.ImageLoader import io.legado.app.help.ReadBookConfig import io.legado.app.help.permission.Permissions @@ -174,7 +173,7 @@ class BgTextConfigDialog : DialogFragment() { var file = requireContext().getExternalFilesDir(null) ?: requireContext().filesDir file = - FileHelp.getFile(file.absolutePath + File.separator + "bg" + File.separator + doc.name) + FileUtils.getFile(file.absolutePath + File.separator + "bg" + File.separator + doc.name) DocumentUtils.readBytes(requireContext(), uri)?.let { file.writeBytes(it) ReadBookConfig.getConfig().setBg(2, file.absolutePath) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 6ae3bd977..5c75187bd 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -7,7 +7,6 @@ import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.api.IHttpGetApi import io.legado.app.data.entities.BookSource -import io.legado.app.help.FileHelp import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Backup import io.legado.app.help.storage.OldRule @@ -92,7 +91,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) }.let { val json = GSON.toJson(it) val file = - FileHelp.getFile(Backup.exportPath + File.separator + "exportBookSource.json") + FileUtils.getFile(Backup.exportPath + File.separator + "exportBookSource.json") file.writeText(json) } }.onSuccess { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index ba6bc2a61..60be30fae 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -6,9 +6,9 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.ReplaceRule -import io.legado.app.help.FileHelp import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Restore +import io.legado.app.utils.FileUtils import io.legado.app.utils.GSON import io.legado.app.utils.splitNotBlank import org.jetbrains.anko.toast @@ -80,7 +80,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application }.let { val json = GSON.toJson(it) val file = - FileHelp.getFile(Backup.exportPath + File.separator + "exportReplaceRule.json") + FileUtils.getFile(Backup.exportPath + File.separator + "exportReplaceRule.json") file.writeText(json) } }.onSuccess { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index c47bb8c9d..22c8f62f4 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -4,10 +4,10 @@ import android.app.Application import android.text.TextUtils import com.jayway.jsonpath.JsonPath import io.legado.app.App +import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.api.IHttpGetApi import io.legado.app.data.entities.RssSource -import io.legado.app.help.FileHelp import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Restore.jsonPath @@ -65,7 +65,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) App.db.rssSourceDao().getRssSources(*ids.toTypedArray()).let { val json = GSON.toJson(it) val file = - FileHelp.getFile(Backup.exportPath + File.separator + "exportRssSource.json") + FileUtils.getFile(Backup.exportPath + File.separator + "exportRssSource.json") file.writeText(json) } }.onSuccess { @@ -123,6 +123,8 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) App.db.rssSourceDao().insert(*it.toTypedArray()) } } + }.onSuccess { + finally.invoke(context.getString(R.string.success)) } } diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index 420bc9233..5e57e79fc 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -5,7 +5,6 @@ import android.app.Activity.RESULT_OK import android.content.Intent import android.net.Uri import android.os.Bundle -import android.os.Environment import android.util.DisplayMetrics import android.view.LayoutInflater import android.view.MenuItem @@ -18,13 +17,9 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.constant.PreferKey -import io.legado.app.help.FileHelp import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat -import io.legado.app.utils.DocumentUtils -import io.legado.app.utils.getPrefString -import io.legado.app.utils.putPrefString -import io.legado.app.utils.toast +import io.legado.app.utils.* import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO @@ -127,11 +122,11 @@ class FontSelectDialog : DialogFragment(), @SuppressLint("DefaultLocale") private fun getFontFiles(uri: Uri) { launch(IO) { - FileHelp.deleteFile(fontCacheFolder) + FileUtils.deleteFile(fontCacheFolder) DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach { file -> if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) { DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let { - FileHelp.getFile(fontCacheFolder + file.name).writeBytes(it) + FileUtils.getFile(fontCacheFolder + file.name).writeBytes(it) } } } @@ -154,7 +149,7 @@ class FontSelectDialog : DialogFragment(), private fun getFontFilesOld() { try { val file = - File(Environment.getExternalStorageDirectory().absolutePath + File.separator + "Fonts") + File(FileUtils.getSdCardPath() + File.separator + "Fonts") file.listFiles { pathName -> pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) }?.let { @@ -167,7 +162,7 @@ class FontSelectDialog : DialogFragment(), override fun onClick(file: File) { launch(IO) { - file.copyTo(FileHelp.getFile(fontFolder + file.name), true).absolutePath.let { path -> + file.copyTo(FileUtils.getFile(fontFolder + file.name), true).absolutePath.let { path -> val cb = (parentFragment as? CallBack) ?: (activity as? CallBack) cb?.let { if (it.curFontPath != path) { diff --git a/app/src/main/java/io/legado/app/utils/ACache.kt b/app/src/main/java/io/legado/app/utils/ACache.kt index b173dc612..03a330d6a 100644 --- a/app/src/main/java/io/legado/app/utils/ACache.kt +++ b/app/src/main/java/io/legado/app/utils/ACache.kt @@ -597,6 +597,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) val w = drawable.intrinsicWidth val h = drawable.intrinsicHeight // 取 drawable 的颜色格式 + @Suppress("DEPRECATION") val config = if (drawable.opacity != PixelFormat.OPAQUE) Bitmap.Config.ARGB_8888 else diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 23cdcabab..9ebd26ea0 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -4,86 +4,76 @@ import android.content.ContentUris import android.content.Context import android.net.Uri import android.os.Environment -import android.os.storage.StorageManager import android.provider.DocumentsContract import android.provider.MediaStore -import android.util.Log -import androidx.core.content.ContextCompat +import io.legado.app.App import java.io.File import java.io.IOException -import java.lang.reflect.Array -import java.util.* object FileUtils { - fun getFileByPath(filePath: String): File? { - return if (filePath.isBlank()) null else File(filePath) + fun getCachePath(): String { + return App.INSTANCE.externalCacheDir?.absolutePath + ?: App.INSTANCE.cacheDir.absolutePath } - fun getSdCardPath(): String { - var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath - try { - sdCardDirectory = File(sdCardDirectory).canonicalPath - } catch (ioe: IOException) { - ioe.printStackTrace() + //获取文件夹 + fun getFolder(filePath: String): File { + val file = File(filePath) + //如果文件夹不存在,就创建它 + if (!file.exists()) { + file.mkdirs() } - - return sdCardDirectory + return file } - fun getStorageData(pContext: Context): ArrayList? { - - val storageManager = pContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager - + //获取文件 + @Synchronized + fun getFile(filePath: String): File { + val file = File(filePath) try { - val getVolumeList = storageManager.javaClass.getMethod("getVolumeList") - - val storageVolumeClazz = Class.forName("android.os.storage.StorageVolume") - val getPath = storageVolumeClazz.getMethod("getPath") - - val invokeVolumeList = getVolumeList.invoke(storageManager) ?: return null - val length = Array.getLength(invokeVolumeList) - - val list = ArrayList() - for (i in 0 until length) { - val storageVolume = Array.get(invokeVolumeList, i)//得到StorageVolume对象 - val path = getPath.invoke(storageVolume) as String - - list.add(path) + if (!file.exists()) { + //创建父类文件夹 + file.parent?.let { + getFolder(it) + } + //创建文件 + file.createNewFile() } - return list - } catch (e: Exception) { + } catch (e: IOException) { e.printStackTrace() } - - return null + return file } - - fun getExtSdCardPaths(con: Context): ArrayList { - val paths = ArrayList() - val files = ContextCompat.getExternalFilesDirs(con, "external") - val firstFile = files[0] - for (file in files) { - if (file != null && file != firstFile) { - val index = file.absolutePath.lastIndexOf("/Android/data") - if (index < 0) { - Log.w("", "Unexpected external file dir: " + file.absolutePath) - } else { - var path = file.absolutePath.substring(0, index) - try { - path = File(path).canonicalPath - } catch (e: IOException) { - // Keep non-canonical path. - } - - paths.add(path) - } + //递归删除文件夹下的数据 + @Synchronized + fun deleteFile(filePath: String) { + val file = File(filePath) + if (!file.exists()) return + + if (file.isDirectory) { + val files = file.listFiles() + files?.forEach { subFile -> + val path = subFile.path + deleteFile(path) } } - return paths + //删除文件 + file.delete() + } + + fun getSdCardPath(): String { + @Suppress("DEPRECATION") + var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath + try { + sdCardDirectory = File(sdCardDirectory).canonicalPath + } catch (ioe: IOException) { + ioe.printStackTrace() + } + return sdCardDirectory } fun getPath(context: Context, uri: Uri): String? { diff --git a/app/src/main/java/io/legado/app/utils/LogUtils.kt b/app/src/main/java/io/legado/app/utils/LogUtils.kt index 1662974a7..b6712ad4d 100644 --- a/app/src/main/java/io/legado/app/utils/LogUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LogUtils.kt @@ -2,7 +2,6 @@ package io.legado.app.utils import android.annotation.SuppressLint import io.legado.app.App -import io.legado.app.help.FileHelp import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -30,8 +29,8 @@ object LogUtils { } private val fileHandler by lazy { - val logFolder = FileHelp.getCachePath() + File.separator + "logs" - FileHelp.getFolder(logFolder) + val logFolder = FileUtils.getCachePath() + File.separator + "logs" + FileUtils.getFolder(logFolder) FileHandler( logFolder + File.separator + "app.log", 10240, diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index 6d70b244b..95096c087 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -250,7 +250,7 @@ object StringUtils { val m = p.matcher(data) val buf = StringBuffer(data.length) while (m.find()) { - val ch = Integer.parseInt(m.group(1), 16).toChar().toString() + val ch = Integer.parseInt(m.group(1)!!, 16).toChar().toString() m.appendReplacement(buf, Matcher.quoteReplacement(ch)) } m.appendTail(buf) From 09bbad40eda5619e8d34be2ab7d22b47f591efcb Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 22:39:49 +0800 Subject: [PATCH 26/27] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/ui/book/info/BookInfoActivity.kt | 4 ++-- .../main/java/io/legado/app/ui/book/search/SearchActivity.kt | 4 ++-- app/src/main/java/io/legado/app/utils/FileUtils.kt | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index fb689519c..57839f1c9 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -98,8 +98,8 @@ class BookInfoActivity : return super.onCompatOptionsItemSelected(item) } - override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean { - menu?.findItem(R.id.menu_can_update)?.isChecked = + override fun onMenuOpened(featureId: Int, menu: Menu): Boolean { + menu.findItem(R.id.menu_can_update)?.isChecked = viewModel.bookData.value?.canUpdate ?: true return super.onMenuOpened(featureId, menu) } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index ae44ddaad..fb250fb46 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -76,7 +76,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se !getPrefBoolean(PreferKey.precisionSearch) ) precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch) - search_view.query.toString().trim()?.let { + search_view.query?.toString()?.trim()?.let { search_view.setQuery(it, true) } } @@ -88,7 +88,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se } else { putPrefString("searchGroup", item.title.toString()) } - search_view.query.toString().trim()?.let { + search_view.query?.toString()?.trim()?.let { search_view.setQuery(it, true) } } diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 9ebd26ea0..c6061acb6 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -86,6 +86,7 @@ object FileUtils { val type = split[0] if ("primary".equals(type, ignoreCase = true)) { + @Suppress("DEPRECATION") return Environment.getExternalStorageDirectory().toString() + "/" + split[1] } @@ -135,7 +136,7 @@ object FileUtils { private fun getDataColumn( context: Context, uri: Uri, selection: String?, - selectionArgs: kotlin.Array? + selectionArgs: Array? ): String? { val column = "_data" From 5e2f4ad06b97c38a354c9b6717af64ad98eecdef Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 23:42:31 +0800 Subject: [PATCH 27/27] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 94 +++++++++++++------ .../java/io/legado/app/utils/DocumentUtils.kt | 5 + .../java/io/legado/app/utils/FileUtils.kt | 18 +++- 3 files changed, 86 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index bce22a823..2c1330b12 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -52,67 +52,101 @@ object BookHelp { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { DocumentUtils.createFileIfNotExist( it, - bookChapterName(bookChapter), + "${bookChapterName(bookChapter)}.nb", subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) - ) + )?.uri?.writeText(App.INSTANCE, content) } } else { - FileUtils.getFolder(getBookFolder(book)).listFiles()?.forEach { + FileUtils.createFileIfNotExist( + File(downloadPath), + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).listFiles()?.forEach { if (it.name.startsWith(String.format("%05d", bookChapter.index))) { it.delete() return@forEach } } - val filePath = getChapterPath(book, bookChapter) - val file = FileUtils.getFile(filePath) - file.writeText(content) + FileUtils.createFileIfNotExist( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).writeText(content) } } fun getChapterCount(book: Book): Int { - return FileUtils.getFolder(getBookFolder(book)).list()?.size ?: 0 + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + return DocumentUtils.createFileIfNotExist( + it, + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + )?.listFiles()?.size ?: 0 + } + } else { + return FileUtils.createFileIfNotExist( + File(downloadPath), + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).list()?.size ?: 0 + } + return 0 } fun hasContent(book: Book, bookChapter: BookChapter): Boolean { - val filePath = getChapterPath(book, bookChapter) - runCatching { - val file = File(filePath) - if (file.exists()) { - return true + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + return DocumentUtils.exists( + it, + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ) } + } else { + return FileUtils.exists( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ) } return false } fun getContent(book: Book, bookChapter: BookChapter): String? { - val filePath = getChapterPath(book, bookChapter) - runCatching { - val file = File(filePath) - if (file.exists()) { - return file.readText() + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + return DocumentUtils.createFileIfNotExist( + it, + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + )?.uri?.readText(App.INSTANCE) } + } else { + return FileUtils.createFileIfNotExist( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).readText() } return null } fun delContent(book: Book, bookChapter: BookChapter) { - val filePath = getChapterPath(book, bookChapter) - kotlin.runCatching { - val file = File(filePath) - if (file.exists()) { - file.delete() + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + DocumentUtils.createFileIfNotExist( + it, + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + )?.delete() } + } else { + FileUtils.createFileIfNotExist( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).delete() } } - private fun getBookFolder(book: Book): String { - return "${getBookCachePath()}${File.separator}${bookFolderName(book)}" - } - - private fun getChapterPath(book: Book, bookChapter: BookChapter): String { - return "${getBookFolder(book)}${File.separator}${bookChapterName(bookChapter)}.nb" - } - private fun formatFolderName(folderName: String): String { return folderName.replace("[\\\\/:*?\"<>|.]".toRegex(), "") } diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index 99cf03f5e..3d1f53a58 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -6,6 +6,11 @@ import androidx.documentfile.provider.DocumentFile object DocumentUtils { + fun exists(root: DocumentFile, fileName: String, vararg subDirs: String): Boolean { + val parent = getDirDocument(root, *subDirs) ?: return false + return parent.findFile(fileName)?.exists() ?: false + } + fun createFileIfNotExist( root: DocumentFile, fileName: String, diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index c6061acb6..3d9923448 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -13,12 +13,28 @@ import java.io.IOException object FileUtils { + fun exists(file: File, fileName: String, vararg subDirs: String): Boolean { + val filePath = + file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName + return File(filePath).exists() + } + + fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File { + val filePath = + file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName + return getFile(filePath) + } + + fun createFileIfNotExist(file: File, vararg subDirs: String): File { + val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator) + return getFolder(filePath) + } + fun getCachePath(): String { return App.INSTANCE.externalCacheDir?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath } - //获取文件夹 fun getFolder(filePath: String): File { val file = File(filePath)