From e3216921a3334b522815e6ff4e214ddf8999dcc4 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 3 Aug 2021 11:53:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E9=9D=A2=E8=AE=BE=E7=BD=AE=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=E4=B8=BB=E9=A2=98=E9=87=8C=E9=9D=A2,=E7=99=BD?= =?UTF-8?q?=E5=A4=A9=E5=92=8C=E5=A4=9C=E9=97=B4=E5=8F=AF=E5=88=86=E5=88=AB?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + .../java/io/legado/app/constant/PreferKey.kt | 1 + .../io/legado/app/help/storage/Restore.kt | 3 +- .../app/ui/config/OtherConfigFragment.kt | 70 +---------- .../app/ui/config/ThemeConfigFragment.kt | 117 +++++++++++++++--- .../app/ui/widget/image/CoverImageView.kt | 5 +- .../app/utils/ActivityResultContractUtils.kt | 32 +++++ app/src/main/res/xml/pref_config_other.xml | 14 --- app/src/main/res/xml/pref_config_theme.xml | 23 ++++ 9 files changed, 161 insertions(+), 105 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 86ed22c58..822f304e9 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -17,6 +17,7 @@ 1. 换源界面功能添加:置顶,置底,删除 by h11128 2. Cronet:优化 by ag2s20150909 3. 优化自动翻页 by jiuZhouWorlds +4. 封面设置移到主题里面,白天和夜间可分别设置 **2021/08/01** 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 3baa9f98f..b92ad091c 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -66,6 +66,7 @@ object PreferKey { const val transparentStatusBar = "transparentStatusBar" const val immNavigationBar = "immNavigationBar" const val defaultCover = "defaultCover" + const val defaultCoverDark = "defaultCoverDark" const val replaceEnableDefault = "replaceEnableDefault" const val showBrightnessView = "showBrightnessView" const val autoClearExpired = "autoClearExpired" 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 f2be6b7b5..2d797472b 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 @@ -58,7 +58,8 @@ object Restore { //默认忽略keys private val ignorePrefKeys = arrayOf( - PreferKey.defaultCover + PreferKey.defaultCover, + PreferKey.defaultCoverDark ) private val readPrefKeys = arrayOf( PreferKey.readStyleSelect, 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 5547b76db..93ec2b45b 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 @@ -5,12 +5,9 @@ import android.content.ComponentName import android.content.Intent import android.content.SharedPreferences import android.content.pm.PackageManager -import android.net.Uri import android.os.Bundle import android.os.Process import android.view.View -import androidx.activity.result.contract.ActivityResultContracts -import androidx.documentfile.provider.DocumentFile import androidx.preference.ListPreference import androidx.preference.Preference import io.legado.app.R @@ -21,18 +18,13 @@ import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.lib.dialogs.alert -import io.legado.app.lib.dialogs.selector -import io.legado.app.lib.permission.Permissions -import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.lib.theme.ATH import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.service.WebService import io.legado.app.ui.main.MainActivity -import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.utils.* import splitties.init.appCtx -import java.io.File class OtherConfigFragment : BasePreferenceFragment(), @@ -44,10 +36,6 @@ class OtherConfigFragment : BasePreferenceFragment(), SharedReceiverActivity::class.java.name ) private val webPort get() = getPrefInt(PreferKey.webPort, 1122) - private val selectCoverImage = registerForActivityResult(ActivityResultContracts.GetContent()) { - it ?: return@registerForActivityResult - setCoverFromUri(it) - } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) @@ -56,7 +44,6 @@ class OtherConfigFragment : BasePreferenceFragment(), upPreferenceSummary(PreferKey.preDownloadNum, AppConfig.preDownloadNum.toString()) upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) upPreferenceSummary(PreferKey.webPort, webPort.toString()) - upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -98,17 +85,7 @@ class OtherConfigFragment : BasePreferenceFragment(), putPrefInt(PreferKey.webPort, it) } PreferKey.cleanCache -> clearCache() - PreferKey.defaultCover -> if (getPrefString(PreferKey.defaultCover).isNullOrEmpty()) { - selectCoverImage.launch("image/*") - } else { - selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> - if (i == 0) { - removePref(PreferKey.defaultCover) - } else { - selectCoverImage.launch("image/*") - } - } - } + } return super.onPreferenceTreeClick(preference) } @@ -134,9 +111,6 @@ class OtherConfigFragment : BasePreferenceFragment(), setProcessTextEnable(it.getBoolean(key, true)) } PreferKey.showDiscovery, PreferKey.showRss -> postEvent(EventBus.NOTIFY_MAIN, true) - PreferKey.defaultCover -> upPreferenceSummary( - key, getPrefString(PreferKey.defaultCover) - ) PreferKey.language -> listView.postDelayed({ LanguageUtils.setConfiguration(appCtx) val intent = Intent(appCtx, MainActivity::class.java) @@ -157,11 +131,6 @@ class OtherConfigFragment : BasePreferenceFragment(), getString(R.string.pre_download_s, value) PreferKey.threadCount -> preference.summary = getString(R.string.threads_num, value) PreferKey.webPort -> preference.summary = getString(R.string.web_port_summary, value) - PreferKey.defaultCover -> preference.summary = if (value.isNullOrBlank()) { - getString(R.string.select_image) - } else { - value - } else -> if (preference is ListPreference) { val index = preference.findIndexOfValue(value) // Set the summary to reflect the new value. @@ -222,41 +191,4 @@ class OtherConfigFragment : BasePreferenceFragment(), } } - private fun setCoverFromUri(uri: Uri) { - if (uri.isContentScheme()) { - val doc = DocumentFile.fromSingleUri(requireContext(), uri) - doc?.name?.let { - var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", it) - kotlin.runCatching { - DocumentUtils.readBytes(requireContext(), doc.uri) - }.getOrNull()?.let { byteArray -> - file.writeBytes(byteArray) - putPrefString(PreferKey.defaultCover, file.absolutePath) - CoverImageView.upDefaultCover() - } ?: toastOnUi("获取文件出错") - } - } else { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - RealPathUtil.getPath(requireContext(), uri)?.let { path -> - val imgFile = File(path) - if (imgFile.exists()) { - var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) - file.writeBytes(imgFile.readBytes()) - putPrefString(PreferKey.defaultCover, file.absolutePath) - CoverImageView.upDefaultCover() - } - } - } - .request() - } - } - } \ No newline at end of file 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 94eb8987b..da4d47c6f 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 @@ -9,7 +9,6 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View -import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import androidx.preference.Preference import io.legado.app.R @@ -26,6 +25,7 @@ import io.legado.app.lib.dialogs.selector import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.lib.theme.ATH +import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.ui.widget.prefs.ColorPreference import io.legado.app.utils.* @@ -35,17 +35,21 @@ import java.io.File @Suppress("SameParameterValue") class ThemeConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { - - private val selectLightBg = registerForActivityResult(ActivityResultContracts.GetContent()) { - it ?: return@registerForActivityResult - setBgFromUri(it, PreferKey.bgImage) { - upTheme(false) - } - } - private val selectDarkBg = registerForActivityResult(ActivityResultContracts.GetContent()) { - it ?: return@registerForActivityResult - setBgFromUri(it, PreferKey.bgImageN) { - upTheme(true) + private val requestCodeCover = 111 + private val requestCodeCoverDark = 112 + private val requestCodeBgLight = 121 + private val requestCodeBgDark = 122 + private val selectImage = registerForActivityResult(ActivityResultContractUtils.SelectImage()) { + val uri = it?.second ?: return@registerForActivityResult + when (it.first) { + requestCodeCover -> setCoverFromUri(PreferKey.defaultCover, uri) + requestCodeCoverDark -> setCoverFromUri(PreferKey.defaultCoverDark, uri) + requestCodeBgLight -> setBgFromUri(uri, PreferKey.bgImage) { + upTheme(false) + } + requestCodeBgDark -> setBgFromUri(uri, PreferKey.bgImageN) { + upTheme(true) + } } } @@ -57,6 +61,8 @@ class ThemeConfigFragment : BasePreferenceFragment(), upPreferenceSummary(PreferKey.bgImage, getPrefString(PreferKey.bgImage)) upPreferenceSummary(PreferKey.bgImageN, getPrefString(PreferKey.bgImageN)) upPreferenceSummary(PreferKey.barElevation, AppConfig.elevation.toString()) + upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) + upPreferenceSummary(PreferKey.defaultCoverDark, getPrefString(PreferKey.defaultCoverDark)) findPreference(PreferKey.cBackground)?.let { it.onSaveColor = { color -> if (!ColorUtils.isColorLight(color)) { @@ -85,14 +91,14 @@ class ThemeConfigFragment : BasePreferenceFragment(), setHasOptionsMenu(true) } - override fun onResume() { - super.onResume() + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) } - override fun onPause() { + override fun onDestroy() { + super.onDestroy() preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this) - super.onPause() } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { @@ -128,6 +134,9 @@ class ThemeConfigFragment : BasePreferenceFragment(), PreferKey.cNBBackground -> { upTheme(true) } + PreferKey.defaultCover, PreferKey.defaultCoverDark -> { + upPreferenceSummary(key, getPrefString(key)) + } } } @@ -151,29 +160,52 @@ class ThemeConfigFragment : BasePreferenceFragment(), "themeList" -> ThemeListDialog().show(childFragmentManager, "themeList") "saveDayTheme", "saveNightTheme" -> saveThemeAlert(key) PreferKey.bgImage -> if (getPrefString(PreferKey.bgImage).isNullOrEmpty()) { - selectLightBg.launch("image/*") + selectImage.launch(requestCodeBgLight) } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImage) upTheme(false) } else { - selectLightBg.launch("image/*") + selectImage.launch(requestCodeBgLight) } } } PreferKey.bgImageN -> if (getPrefString(PreferKey.bgImageN).isNullOrEmpty()) { - selectDarkBg.launch("image/*") + selectImage.launch(requestCodeBgDark) } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImageN) upTheme(true) } else { - selectDarkBg.launch("image/*") + selectImage.launch(requestCodeBgDark) + } + } + } + PreferKey.defaultCover -> if (getPrefString(PreferKey.defaultCover).isNullOrEmpty()) { + selectImage.launch(requestCodeCover) + } else { + selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> + if (i == 0) { + removePref(PreferKey.defaultCover) + } else { + selectImage.launch(requestCodeCover) } } } + PreferKey.defaultCoverDark -> + if (getPrefString(PreferKey.defaultCoverDark).isNullOrEmpty()) { + selectImage.launch(requestCodeCoverDark) + } else { + selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> + if (i == 0) { + removePref(PreferKey.defaultCoverDark) + } else { + selectImage.launch(requestCodeCoverDark) + } + } + } } return super.onPreferenceTreeClick(preference) } @@ -217,6 +249,14 @@ class ThemeConfigFragment : BasePreferenceFragment(), when (preferenceKey) { PreferKey.barElevation -> preference.summary = getString(R.string.bar_elevation_s, value) + PreferKey.bgImage, + PreferKey.bgImageN, + PreferKey.defaultCover, + PreferKey.defaultCoverDark -> preference.summary = if (value.isNullOrBlank()) { + getString(R.string.select_image) + } else { + value + } else -> preference.summary = value } } @@ -260,4 +300,41 @@ class ThemeConfigFragment : BasePreferenceFragment(), } } + private fun setCoverFromUri(preferenceKey: String, uri: Uri) { + if (uri.isContentScheme()) { + val doc = DocumentFile.fromSingleUri(requireContext(), uri) + doc?.name?.let { + var file = requireContext().externalFiles + file = FileUtils.createFileIfNotExist(file, "covers", it) + kotlin.runCatching { + DocumentUtils.readBytes(requireContext(), doc.uri) + }.getOrNull()?.let { byteArray -> + file.writeBytes(byteArray) + putPrefString(preferenceKey, file.absolutePath) + CoverImageView.upDefaultCover() + } ?: toastOnUi("获取文件出错") + } + } else { + PermissionsCompat.Builder(this) + .addPermissions( + Permissions.READ_EXTERNAL_STORAGE, + Permissions.WRITE_EXTERNAL_STORAGE + ) + .rationale(R.string.bg_image_per) + .onGranted { + RealPathUtil.getPath(requireContext(), uri)?.let { path -> + val imgFile = File(path) + if (imgFile.exists()) { + var file = requireContext().externalFiles + file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) + file.writeBytes(imgFile.readBytes()) + putPrefString(PreferKey.defaultCover, file.absolutePath) + CoverImageView.upDefaultCover() + } + } + } + .request() + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt index ac4b6159d..3e823829c 100644 --- a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt @@ -179,7 +179,10 @@ class CoverImageView @JvmOverloads constructor( @SuppressLint("UseCompatLoadingForDrawables") fun upDefaultCover() { - val path = appCtx.getPrefString(PreferKey.defaultCover) + val preferKey = + if (AppConfig.isNightTheme) PreferKey.defaultCoverDark + else PreferKey.defaultCover + val path = appCtx.getPrefString(preferKey) var dw = Drawable.createFromPath(path) if (dw == null) { showBookName = true diff --git a/app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt b/app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt new file mode 100644 index 000000000..3f0198023 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt @@ -0,0 +1,32 @@ +package io.legado.app.utils + +import android.app.Activity.RESULT_OK +import android.content.Context +import android.content.Intent +import android.net.Uri +import androidx.activity.result.contract.ActivityResultContract + +object ActivityResultContractUtils { + + class SelectImage : ActivityResultContract?>() { + + var requestCode: Int? = null + + override fun createIntent(context: Context, input: Int?): Intent { + requestCode = input + return Intent(Intent.ACTION_GET_CONTENT) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("image/*") + } + + override fun parseResult(resultCode: Int, intent: Intent?): Pair? { + if (resultCode == RESULT_OK) { + return Pair(requestCode, intent?.data) + } + return null + } + + } + + +} \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_other.xml b/app/src/main/res/xml/pref_config_other.xml index 217fc4156..28360973f 100644 --- a/app/src/main/res/xml/pref_config_other.xml +++ b/app/src/main/res/xml/pref_config_other.xml @@ -40,20 +40,6 @@ android:defaultValue="true" android:key="showRss" android:title="@string/show_rss" - app:iconSpaceReserved="false" /> - - - - diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index ec131ec7a..8fb8143be 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -33,6 +33,15 @@ android:title="@string/bar_elevation" app:iconSpaceReserved="false" /> + + + + + +