自定义默认封面

pull/276/head
gedoor 4 years ago
parent 9c20ff3ea2
commit c739301dec
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 9
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  4. 70
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  5. 22
      app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt

@ -48,6 +48,7 @@ object PreferKey {
const val autoReadSpeed = "autoReadSpeed" const val autoReadSpeed = "autoReadSpeed"
const val barElevation = "barElevation" const val barElevation = "barElevation"
const val transparentStatusBar = "transparentStatusBar" const val transparentStatusBar = "transparentStatusBar"
const val defaultCover = "defaultCover"
const val cPrimary = "colorPrimary" const val cPrimary = "colorPrimary"
const val cAccent = "colorAccent" const val cAccent = "colorAccent"

@ -104,8 +104,12 @@ object Restore {
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all?.map { Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all
?.let { map ->
val ignoreKeys = arrayOf(PreferKey.versionCode, PreferKey.defaultCover)
val edit = App.INSTANCE.defaultSharedPreferences.edit() val edit = App.INSTANCE.defaultSharedPreferences.edit()
map.forEach {
if (!ignoreKeys.contains(it.key)) {
when (val value = it.value) { when (val value = it.value) {
is Int -> edit.putInt(it.key, value) is Int -> edit.putInt(it.key, value)
is Boolean -> edit.putBoolean(it.key, value) is Boolean -> edit.putBoolean(it.key, value)
@ -114,7 +118,8 @@ object Restore {
is String -> edit.putString(it.key, value) is String -> edit.putString(it.key, value)
else -> Unit else -> Unit
} }
edit.putInt(PreferKey.versionCode, App.INSTANCE.versionCode) }
}
edit.apply() edit.apply()
} }
ReadBookConfig.apply { ReadBookConfig.apply {

@ -40,7 +40,7 @@ class BgTextConfigDialog : BaseDialogFragment() {
const val BG_COLOR = 122 const val BG_COLOR = 122
} }
private val resultSelectBg = 123 private val requestCodeBg = 123
private lateinit var adapter: BgAdapter private lateinit var adapter: BgAdapter
override fun onStart() { override fun onStart() {
@ -136,7 +136,7 @@ class BgTextConfigDialog : BaseDialogFragment() {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE) intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*" intent.type = "image/*"
startActivityForResult(intent, resultSelectBg) startActivityForResult(intent, requestCodeBg)
} }
class BgAdapter(context: Context) : class BgAdapter(context: Context) :
@ -167,15 +167,13 @@ class BgTextConfigDialog : BaseDialogFragment() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
when (requestCode) { when (requestCode) {
resultSelectBg -> { requestCodeBg -> if (resultCode == RESULT_OK) {
if (resultCode == RESULT_OK) {
data?.data?.let { uri -> data?.data?.let { uri ->
setBgFromUri(uri) setBgFromUri(uri)
} }
} }
} }
} }
}
private fun setBgFromUri(uri: Uri) { private fun setBgFromUri(uri: Uri) {
if (uri.toString().isContentPath()) { if (uri.toString().isContentPath()) {

@ -1,10 +1,14 @@
package io.legado.app.ui.config package io.legado.app.ui.config
import android.app.Activity.RESULT_OK
import android.content.ComponentName import android.content.ComponentName
import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.documentfile.provider.DocumentFile
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
@ -14,16 +18,22 @@ import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp 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.lib.theme.ATH
import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.receiver.SharedReceiverActivity
import io.legado.app.service.WebService import io.legado.app.service.WebService
import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.ui.widget.number.NumberPickerDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import java.io.File
class OtherConfigFragment : PreferenceFragmentCompat(), class OtherConfigFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
private val requestCodeCover = 231
private val packageManager = App.INSTANCE.packageManager private val packageManager = App.INSTANCE.packageManager
private val componentName = ComponentName( private val componentName = ComponentName(
App.INSTANCE, App.INSTANCE,
@ -36,6 +46,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
addPreferencesFromResource(R.xml.pref_config_other) addPreferencesFromResource(R.xml.pref_config_other)
upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString())
upPreferenceSummary(PreferKey.webPort, webPort.toString()) upPreferenceSummary(PreferKey.webPort, webPort.toString())
upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover))
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -74,8 +85,11 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
} }
toast(R.string.clear_cache_success) toast(R.string.clear_cache_success)
} }
"defaultCover" -> { PreferKey.defaultCover -> {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, requestCodeCover)
} }
} }
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
@ -98,6 +112,10 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
setProcessTextEnable(it.getBoolean(key, true)) setProcessTextEnable(it.getBoolean(key, true))
} }
PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "unused") PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "unused")
PreferKey.defaultCover -> upPreferenceSummary(
key,
getPrefString(PreferKey.defaultCover)
)
} }
} }
@ -134,4 +152,52 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
} }
} }
private fun setCoverFromUri(uri: Uri) {
if (uri.toString().isContentPath()) {
val doc = DocumentFile.fromSingleUri(requireContext(), uri)
doc?.name?.let {
var file = requireContext().externalFilesDir
file = FileUtils.createFileIfNotExist(file, it, "covers")
kotlin.runCatching {
DocumentUtils.readBytes(requireContext(), doc.uri)
}.getOrNull()?.let { byteArray ->
file.writeBytes(byteArray)
putPrefString(PreferKey.defaultCover, file.absolutePath)
CoverImageView.upDefaultCover()
} ?: 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, imgFile.name, "covers")
file.writeBytes(imgFile.readBytes())
putPrefString(PreferKey.defaultCover, file.absolutePath)
CoverImageView.upDefaultCover()
}
}
}
.request()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
requestCodeCover -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
setCoverFromUri(uri)
}
}
}
}
} }

@ -10,8 +10,11 @@ import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target import com.bumptech.glide.request.target.Target
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.utils.getPrefString
class CoverImageView : androidx.appcompat.widget.AppCompatImageView { class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
@ -131,8 +134,8 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
fun load(path: String?, name: String?, author: String?) { fun load(path: String?, name: String?, author: String?) {
setText(name, author) setText(name, author)
ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// ImageLoader.load(context, path)//Glide自动识别http://,content://和file://
.placeholder(R.drawable.image_cover_default) .placeholder(defaultDrawable)
.error(R.drawable.image_cover_default) .error(defaultDrawable)
.listener(object : RequestListener<Drawable> { .listener(object : RequestListener<Drawable> {
override fun onLoadFailed( override fun onLoadFailed(
e: GlideException?, e: GlideException?,
@ -159,4 +162,19 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
.centerCrop() .centerCrop()
.into(this) .into(this)
} }
companion object {
lateinit var defaultDrawable: Drawable
init {
upDefaultCover()
}
fun upDefaultCover() {
val path = App.INSTANCE.getPrefString(PreferKey.defaultCover)
defaultDrawable = Drawable.createFromPath(path)
?: App.INSTANCE.resources.getDrawable(R.drawable.image_cover_default, null)
}
}
} }

Loading…
Cancel
Save