自定义默认封面

pull/276/head
gedoor 4 years ago
parent 9c20ff3ea2
commit c739301dec
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 29
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 12
      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 barElevation = "barElevation"
const val transparentStatusBar = "transparentStatusBar"
const val defaultCover = "defaultCover"
const val cPrimary = "colorPrimary"
const val cAccent = "colorAccent"

@ -104,19 +104,24 @@ object Restore {
} catch (e: Exception) {
e.printStackTrace()
}
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all?.map {
val edit = App.INSTANCE.defaultSharedPreferences.edit()
when (val value = it.value) {
is Int -> edit.putInt(it.key, value)
is Boolean -> edit.putBoolean(it.key, value)
is Long -> edit.putLong(it.key, value)
is Float -> edit.putFloat(it.key, value)
is String -> edit.putString(it.key, value)
else -> Unit
Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all
?.let { map ->
val ignoreKeys = arrayOf(PreferKey.versionCode, PreferKey.defaultCover)
val edit = App.INSTANCE.defaultSharedPreferences.edit()
map.forEach {
if (!ignoreKeys.contains(it.key)) {
when (val value = it.value) {
is Int -> edit.putInt(it.key, value)
is Boolean -> edit.putBoolean(it.key, value)
is Long -> edit.putLong(it.key, value)
is Float -> edit.putFloat(it.key, value)
is String -> edit.putString(it.key, value)
else -> Unit
}
}
}
edit.apply()
}
edit.putInt(PreferKey.versionCode, App.INSTANCE.versionCode)
edit.apply()
}
ReadBookConfig.apply {
styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect)
shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout)

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

@ -1,10 +1,14 @@
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.net.Uri
import android.os.Bundle
import android.view.View
import androidx.documentfile.provider.DocumentFile
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
@ -14,16 +18,22 @@ import io.legado.app.constant.EventBus
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.service.WebService
import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.ui.widget.number.NumberPickerDialog
import io.legado.app.utils.*
import java.io.File
class OtherConfigFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
private val requestCodeCover = 231
private val packageManager = App.INSTANCE.packageManager
private val componentName = ComponentName(
App.INSTANCE,
@ -36,6 +46,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
addPreferencesFromResource(R.xml.pref_config_other)
upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString())
upPreferenceSummary(PreferKey.webPort, webPort.toString())
upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover))
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -74,8 +85,11 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
}
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)
@ -98,6 +112,10 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
setProcessTextEnable(it.getBoolean(key, true))
}
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.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.legado.app.App
import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.ImageLoader
import io.legado.app.utils.getPrefString
class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
@ -131,8 +134,8 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
fun load(path: String?, name: String?, author: String?) {
setText(name, author)
ImageLoader.load(context, path)//Glide自动识别http://,content://和file://
.placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default)
.placeholder(defaultDrawable)
.error(defaultDrawable)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
@ -159,4 +162,19 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
.centerCrop()
.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