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 f4e902ba0..b8cbedf1c 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -70,10 +70,12 @@ object PreferKey { const val cAccent = "colorAccent" const val cBackground = "colorBackground" const val cBBackground = "colorBottomBackground" + const val bgImage = "backgroundImage" const val cNPrimary = "colorPrimaryNight" const val cNAccent = "colorAccentNight" const val cNBackground = "colorBackgroundNight" const val cNBBackground = "colorBottomBackgroundNight" + const val bgImageN = "backgroundImageNight" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/ThemeConfig.kt b/app/src/main/java/io/legado/app/help/ThemeConfig.kt index 9de5ceade..0b0298e91 100644 --- a/app/src/main/java/io/legado/app/help/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/ThemeConfig.kt @@ -204,6 +204,7 @@ object ThemeConfig { var primaryColor: String, var accentColor: String, var backgroundColor: String, + var backgroundImage: String? = null, var bottomBackground: 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 3adaa20f0..42945d48f 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 @@ -88,13 +88,13 @@ class OtherConfigFragment : BasePreferenceFragment(), } PreferKey.cleanCache -> clearCache() PreferKey.defaultCover -> if (getPrefString(PreferKey.defaultCover).isNullOrEmpty()) { - selectDefaultCover() + selectImage(requestCodeCover) } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.defaultCover) } else { - selectDefaultCover() + selectImage(requestCodeCover) } } } @@ -183,11 +183,12 @@ class OtherConfigFragment : BasePreferenceFragment(), }.show() } - private fun selectDefaultCover() { + @Suppress("SameParameterValue") + private fun selectImage(requestCode: Int) { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.addCategory(Intent.CATEGORY_OPENABLE) intent.type = "image/*" - startActivityForResult(intent, requestCodeCover) + startActivityForResult(intent, requestCode) } private fun isProcessTextEnabled(): Boolean { 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 b7a29434e..dbe344a53 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 @@ -1,13 +1,17 @@ package io.legado.app.ui.config import android.annotation.SuppressLint +import android.app.Activity +import android.content.Intent import android.content.SharedPreferences +import android.net.Uri import android.os.Build import android.os.Bundle import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View +import androidx.documentfile.provider.DocumentFile import androidx.preference.Preference import io.legado.app.App import io.legado.app.R @@ -19,18 +23,25 @@ import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig import io.legado.app.help.LauncherIconHelp import io.legado.app.help.ThemeConfig +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.ATH import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.ui.widget.prefs.ColorPreference import io.legado.app.ui.widget.prefs.IconListPreference import io.legado.app.utils.* +import java.io.File @Suppress("SameParameterValue") class ThemeConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { + private val requestCodeBgImage = 234 + private val requestCodeBgImageN = 342 + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) if (Build.VERSION.SDK_INT < 26) { @@ -39,6 +50,8 @@ class ThemeConfigFragment : BasePreferenceFragment(), } } upPreferenceSummary(PreferKey.barElevation, AppConfig.elevation.toString()) + upPreferenceSummary(PreferKey.bgImage, getPrefString(PreferKey.bgImage)) + upPreferenceSummary(PreferKey.bgImageN, getPrefString(PreferKey.bgImageN)) findPreference(PreferKey.cBackground)?.let { it.onSaveColor = { color -> if (!ColorUtils.isColorLight(color)) { @@ -168,10 +181,39 @@ class ThemeConfigFragment : BasePreferenceFragment(), } "themeList" -> ThemeListDialog().show(childFragmentManager, "themeList") "saveDayTheme", "saveNightTheme" -> saveThemeAlert(key) + PreferKey.bgImage -> if (getPrefString(PreferKey.bgImage).isNullOrEmpty()) { + selectImage(requestCodeBgImage) + } else { + selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> + if (i == 0) { + removePref(PreferKey.bgImage) + } else { + selectImage(requestCodeBgImage) + } + } + } + PreferKey.bgImageN -> if (getPrefString(PreferKey.bgImageN).isNullOrEmpty()) { + selectImage(requestCodeBgImageN) + } else { + selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> + if (i == 0) { + removePref(PreferKey.bgImageN) + } else { + selectImage(requestCodeBgImageN) + } + } + } } return super.onPreferenceTreeClick(preference) } + private fun selectImage(requestCode: Int) { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.type = "image/*" + startActivityForResult(intent, requestCode) + } + @SuppressLint("InflateParams") private fun saveThemeAlert(key: String) { alert(R.string.theme_name) { @@ -211,6 +253,61 @@ class ThemeConfigFragment : BasePreferenceFragment(), when (preferenceKey) { PreferKey.barElevation -> preference.summary = getString(R.string.bar_elevation_s, value) + else -> preference.summary = value + } + } + + private fun setBgFromUri(uri: Uri, preferenceKey: String) { + if (uri.isContentScheme()) { + val doc = DocumentFile.fromSingleUri(requireContext(), uri) + doc?.name?.let { + var file = requireContext().externalFilesDir + file = FileUtils.createFileIfNotExist(file, preferenceKey, it) + kotlin.runCatching { + DocumentUtils.readBytes(requireContext(), doc.uri) + }.getOrNull()?.let { byteArray -> + file.writeBytes(byteArray) + putPrefString(preferenceKey, file.absolutePath) + + } ?: toast("获取文件出错") + } + } 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().externalFilesDir + file = FileUtils.createFileIfNotExist(file, preferenceKey, imgFile.name) + file.writeBytes(imgFile.readBytes()) + putPrefString(preferenceKey, file.absolutePath) + + } + } + } + .request() } } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + requestCodeBgImage -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + setBgFromUri(uri, PreferKey.bgImage) + } + } + requestCodeBgImageN -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + setBgFromUri(uri, PreferKey.bgImageN) + } + } + } + } + } \ No newline at end of file diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 3192e781d..e4336c98a 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -800,5 +800,6 @@ 预下载 预先下载10章正文 启用排序 + 背景图片 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 836f1c4de..73f4ea493 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -804,5 +804,6 @@ 预下载 预先下载10章正文 启用排序 + 背景图片 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index ad6bfbce7..6ff4b0be6 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -804,5 +804,6 @@ 预下载 预先下载10章正文 启用排序 + 背景图片 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c38246704..2472dbe3c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -807,5 +807,6 @@ 预下载 预先下载10章正文 启用排序 + 背景图片 diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index 081c49b37..f5aae055a 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -72,6 +72,10 @@ app:cpv_dialogType="preset" app:iconSpaceReserved="false" /> + + + +