设置图片背景

pull/823/head
gedoor 4 years ago
parent 5fd83e87c8
commit 0c0862673c
  1. 2
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 1
      app/src/main/java/io/legado/app/help/ThemeConfig.kt
  3. 9
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  4. 97
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  5. 1
      app/src/main/res/values-zh-rHK/strings.xml
  6. 1
      app/src/main/res/values-zh-rTW/strings.xml
  7. 1
      app/src/main/res/values-zh/strings.xml
  8. 1
      app/src/main/res/values/strings.xml
  9. 8
      app/src/main/res/xml/pref_config_theme.xml

@ -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"
}

@ -204,6 +204,7 @@ object ThemeConfig {
var primaryColor: String,
var accentColor: String,
var backgroundColor: String,
var backgroundImage: String? = null,
var bottomBackground: String
)

@ -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 {

@ -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<ColorPreference>(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)
}
}
}
}
}

@ -800,5 +800,6 @@
<string name="pre_download">预下载</string>
<string name="pre_download_s">预先下载10章正文</string>
<string name="sort_enable">启用排序</string>
<string name="background_image">背景图片</string>
</resources>

@ -804,5 +804,6 @@
<string name="pre_download">预下载</string>
<string name="pre_download_s">预先下载10章正文</string>
<string name="sort_enable">启用排序</string>
<string name="background_image">背景图片</string>
</resources>

@ -804,5 +804,6 @@
<string name="pre_download">预下载</string>
<string name="pre_download_s">预先下载10章正文</string>
<string name="sort_enable">启用排序</string>
<string name="background_image">背景图片</string>
</resources>

@ -807,5 +807,6 @@
<string name="pre_download">预下载</string>
<string name="pre_download_s">预先下载10章正文</string>
<string name="sort_enable">启用排序</string>
<string name="background_image">背景图片</string>
</resources>

@ -72,6 +72,10 @@
app:cpv_dialogType="preset"
app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.Preference
android:key="backgroundImage"
android:title="@string/background_image" />
<io.legado.app.ui.widget.prefs.ColorPreference
android:defaultValue="@color/md_grey_200"
android:key="colorBottomBackground"
@ -123,6 +127,10 @@
app:cpv_dialogType="preset"
app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.Preference
android:key="backgroundImageNight"
android:title="@string/background_image" />
<io.legado.app.ui.widget.prefs.ColorPreference
android:defaultValue="@color/md_grey_800"
android:key="colorBottomBackgroundNight"

Loading…
Cancel
Save