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