pull/1238/head^2
gedoor 3 years ago
parent c43dcd1216
commit a32abdf792
  1. 38
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  2. 82
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  3. 5
      app/src/main/java/io/legado/app/ui/document/FilePickerActivity.kt
  4. 98
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -20,8 +20,6 @@ import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.permission.Permissions
import io.legado.app.lib.permission.PermissionsCompat
import io.legado.app.lib.theme.bottomBackground import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.getPrimaryTextColor import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.lib.theme.getSecondaryTextColor import io.legado.app.lib.theme.getSecondaryTextColor
@ -345,35 +343,13 @@ class BgTextConfigDialog : BaseDialogFragment() {
} }
private fun setBgFromUri(uri: Uri) { private fun setBgFromUri(uri: Uri) {
if (uri.toString().isContentScheme()) { uri.read(this) { name, bytes ->
val doc = DocumentFile.fromSingleUri(requireContext(), uri) var file = requireContext().externalFiles
doc?.name?.let { file = FileUtils.createFileIfNotExist(file, "bg", name)
val file = file.writeBytes(bytes)
FileUtils.createFileIfNotExist(requireContext().externalFiles, "bg", it) ReadBookConfig.durConfig.setCurBg(2, file.absolutePath)
kotlin.runCatching { ReadBookConfig.upBg()
DocumentUtils.readBytes(requireContext(), doc.uri) postEvent(EventBus.UP_CONFIG, false)
}.getOrNull()?.let { byteArray ->
file.writeBytes(byteArray)
ReadBookConfig.durConfig.setCurBg(2, file.absolutePath)
ReadBookConfig.upBg()
postEvent(EventBus.UP_CONFIG, false)
} ?: toastOnUi("获取文件出错")
}
} 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 ->
ReadBookConfig.durConfig.setCurBg(2, path)
ReadBookConfig.upBg()
postEvent(EventBus.UP_CONFIG, false)
}
}
.request()
} }
} }
} }

@ -10,7 +10,6 @@ import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.SeekBar import android.widget.SeekBar
import androidx.documentfile.provider.DocumentFile
import androidx.preference.Preference import androidx.preference.Preference
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BasePreferenceFragment import io.legado.app.base.BasePreferenceFragment
@ -24,15 +23,12 @@ import io.legado.app.help.LauncherIconHelp
import io.legado.app.help.ThemeConfig import io.legado.app.help.ThemeConfig
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.permission.Permissions
import io.legado.app.lib.permission.PermissionsCompat
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.ui.widget.image.CoverImageView 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.ui.widget.prefs.ColorPreference import io.legado.app.ui.widget.prefs.ColorPreference
import io.legado.app.ui.widget.seekbar.SeekBarChangeListener import io.legado.app.ui.widget.seekbar.SeekBarChangeListener
import io.legado.app.utils.* import io.legado.app.utils.*
import java.io.File
@Suppress("SameParameterValue") @Suppress("SameParameterValue")
@ -306,76 +302,22 @@ class ThemeConfigFragment : BasePreferenceFragment(),
} }
private fun setBgFromUri(uri: Uri, preferenceKey: String, success: () -> Unit) { private fun setBgFromUri(uri: Uri, preferenceKey: String, success: () -> Unit) {
if (uri.isContentScheme()) { uri.read(this) { name, bytes ->
val doc = DocumentFile.fromSingleUri(requireContext(), uri) var file = requireContext().externalFiles
doc?.name?.let { file = FileUtils.createFileIfNotExist(file, preferenceKey, name)
var file = requireContext().externalFiles file.writeBytes(bytes)
file = FileUtils.createFileIfNotExist(file, preferenceKey, it) putPrefString(preferenceKey, file.absolutePath)
kotlin.runCatching { success()
DocumentUtils.readBytes(requireContext(), doc.uri)
}.getOrNull()?.let { byteArray ->
file.writeBytes(byteArray)
putPrefString(preferenceKey, file.absolutePath)
success()
} ?: toastOnUi("获取文件出错")
}
} 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().externalFiles
file = FileUtils.createFileIfNotExist(file, preferenceKey, imgFile.name)
file.writeBytes(imgFile.readBytes())
putPrefString(preferenceKey, file.absolutePath)
success()
}
}
}
.request()
} }
} }
private fun setCoverFromUri(preferenceKey: String, uri: Uri) { private fun setCoverFromUri(preferenceKey: String, uri: Uri) {
if (uri.isContentScheme()) { uri.read(this) { name, bytes ->
val doc = DocumentFile.fromSingleUri(requireContext(), uri) var file = requireContext().externalFiles
doc?.name?.let { file = FileUtils.createFileIfNotExist(file, "covers", name)
var file = requireContext().externalFiles file.writeBytes(bytes)
file = FileUtils.createFileIfNotExist(file, "covers", it) putPrefString(preferenceKey, file.absolutePath)
kotlin.runCatching { CoverImageView.upDefaultCover()
DocumentUtils.readBytes(requireContext(), doc.uri)
}.getOrNull()?.let { byteArray ->
file.writeBytes(byteArray)
putPrefString(preferenceKey, file.absolutePath)
CoverImageView.upDefaultCover()
} ?: toastOnUi("获取文件出错")
}
} 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().externalFiles
file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name)
file.writeBytes(imgFile.readBytes())
putPrefString(PreferKey.defaultCover, file.absolutePath)
CoverImageView.upDefaultCover()
}
}
}
.request()
} }
} }

@ -31,10 +31,9 @@ class FilePickerActivity :
return@registerForActivityResult return@registerForActivityResult
} }
if (it.isContentScheme()) { if (it.isContentScheme()) {
contentResolver.takePersistableUriPermission( val modeFlags =
it,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) contentResolver.takePersistableUriPermission(it, modeFlags)
} }
onResult(Intent().setData(it)) onResult(Intent().setData(it))
} }

@ -12,55 +12,69 @@ import java.io.File
fun Uri.isContentScheme() = this.scheme == "content" fun Uri.isContentScheme() = this.scheme == "content"
@Throws(Exception::class) /**
* 读取URI
*/
fun Uri.read(activity: AppCompatActivity, success: (name: String, bytes: ByteArray) -> Unit) { fun Uri.read(activity: AppCompatActivity, success: (name: String, bytes: ByteArray) -> Unit) {
if (isContentScheme()) { try {
val doc = DocumentFile.fromSingleUri(activity, this) if (isContentScheme()) {
doc ?: error("未获取到文件") val doc = DocumentFile.fromSingleUri(activity, this)
val name = doc.name ?: error("未获取到文件名") doc ?: error("未获取到文件")
val fileBytes = DocumentUtils.readBytes(activity, doc.uri) val name = doc.name ?: error("未获取到文件名")
fileBytes ?: error("读取文件出错") val fileBytes = DocumentUtils.readBytes(activity, doc.uri)
success.invoke(name, fileBytes) fileBytes ?: error("读取文件出错")
} else { success.invoke(name, fileBytes)
PermissionsCompat.Builder(activity) } else {
.addPermissions( PermissionsCompat.Builder(activity)
Permissions.READ_EXTERNAL_STORAGE, .addPermissions(
Permissions.WRITE_EXTERNAL_STORAGE Permissions.READ_EXTERNAL_STORAGE,
) Permissions.WRITE_EXTERNAL_STORAGE
.rationale(R.string.bg_image_per) )
.onGranted { .rationale(R.string.bg_image_per)
RealPathUtil.getPath(activity, this)?.let { path -> .onGranted {
val imgFile = File(path) RealPathUtil.getPath(activity, this)?.let { path ->
success.invoke(imgFile.name, imgFile.readBytes()) val imgFile = File(path)
success.invoke(imgFile.name, imgFile.readBytes())
}
} }
} .request()
.request() }
} catch (e: Exception) {
e.printStackTrace()
activity.toastOnUi(e.localizedMessage ?: "read uri error")
} }
} }
@Throws(Exception::class) /**
* 读取URI
*/
fun Uri.read(fragment: Fragment, success: (name: String, bytes: ByteArray) -> Unit) { fun Uri.read(fragment: Fragment, success: (name: String, bytes: ByteArray) -> Unit) {
if (isContentScheme()) { try {
val doc = DocumentFile.fromSingleUri(fragment.requireContext(), this) if (isContentScheme()) {
doc ?: error("未获取到文件") val doc = DocumentFile.fromSingleUri(fragment.requireContext(), this)
val name = doc.name ?: error("未获取到文件名") doc ?: error("未获取到文件")
val fileBytes = DocumentUtils.readBytes(fragment.requireContext(), doc.uri) val name = doc.name ?: error("未获取到文件名")
fileBytes ?: error("读取文件出错") val fileBytes = DocumentUtils.readBytes(fragment.requireContext(), doc.uri)
success.invoke(name, fileBytes) fileBytes ?: error("读取文件出错")
} else { success.invoke(name, fileBytes)
PermissionsCompat.Builder(fragment) } else {
.addPermissions( PermissionsCompat.Builder(fragment)
Permissions.READ_EXTERNAL_STORAGE, .addPermissions(
Permissions.WRITE_EXTERNAL_STORAGE Permissions.READ_EXTERNAL_STORAGE,
) Permissions.WRITE_EXTERNAL_STORAGE
.rationale(R.string.bg_image_per) )
.onGranted { .rationale(R.string.bg_image_per)
RealPathUtil.getPath(fragment.requireContext(), this)?.let { path -> .onGranted {
val imgFile = File(path) RealPathUtil.getPath(fragment.requireContext(), this)?.let { path ->
success.invoke(imgFile.name, imgFile.readBytes()) val imgFile = File(path)
success.invoke(imgFile.name, imgFile.readBytes())
}
} }
} .request()
.request() }
} catch (e: Exception) {
e.printStackTrace()
fragment.toastOnUi(e.localizedMessage ?: "read uri error")
} }
} }

Loading…
Cancel
Save