pull/464/head
Robot 4 years ago
commit d4bf3a033a
  1. 6
      app/src/main/java/io/legado/app/help/ImageLoader.kt
  2. 2
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 4
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  4. 2
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  5. 2
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  6. 2
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  7. 4
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  8. 2
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  9. 4
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt
  10. 2
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt
  11. 2
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  12. 2
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  13. 2
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  14. 6
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  15. 3
      app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt
  16. 4
      app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt
  17. 4
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  18. 4
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  19. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  20. 12
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt
  21. 2
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  22. 2
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  23. 6
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt
  24. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  25. 4
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  26. 2
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt
  27. 4
      app/src/main/java/io/legado/app/utils/StringExtensions.kt
  28. 6
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -7,9 +7,9 @@ import android.net.Uri
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isContentPath
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isContentScheme
import java.io.File
object ImageLoader {
@ -21,7 +21,7 @@ object ImageLoader {
return when {
path.isNullOrEmpty() -> Glide.with(context).load(path)
path.isAbsUrl() -> Glide.with(context).load(AnalyzeUrl(path).getGlideUrl())
path.isContentPath() -> Glide.with(context).load(Uri.parse(path))
path.isContentScheme() -> Glide.with(context).load(Uri.parse(path))
else -> try {
Glide.with(context).load(File(path))
} catch (e: Exception) {

@ -92,7 +92,7 @@ object Backup {
edit.commit()
}
WebDavHelp.backUpWebDav(backupPath)
if (path.isContentPath()) {
if (path.isContentScheme()) {
copyBackup(context, Uri.parse(path), isAuto)
} else {
if (path.isEmpty()) {

@ -7,14 +7,14 @@ import io.legado.app.App
import io.legado.app.data.entities.BookSource
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentPath
import io.legado.app.utils.isContentScheme
import org.jetbrains.anko.toast
import java.io.File
object ImportOldData {
fun importUri(context: Context, uri: Uri) {
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(context, uri)?.listFiles()?.forEach {
when (it.name) {
"myBookShelf.json" ->

@ -73,7 +73,7 @@ object Restore {
suspend fun restore(context: Context, path: String) {
withContext(IO) {
if (path.isContentPath()) {
if (path.isContentScheme()) {
DocumentFile.fromTreeUri(context, Uri.parse(path))?.listFiles()?.forEach { doc ->
for (fileName in Backup.backupFileNames) {
if (doc.name == fileName) {

@ -261,7 +261,7 @@ class AnalyzeTxtFile {
}
private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentPath()) {
if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(LocalBook.cacheFolder, book.originName)
if (!bookFile.exists()) {

@ -57,7 +57,7 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) {
init {
try {
val epubReader = EpubReader()
val inputStream = if (book.bookUrl.isContentPath()) {
val inputStream = if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl)
App.INSTANCE.contentResolver.openInputStream(uri)
} else {

@ -37,7 +37,7 @@ object LocalBook {
fun importFile(uri: Uri): Book {
val path: String
val fileName = if (uri.isContentPath()) {
val fileName = if (uri.isContentScheme()) {
path = uri.toString()
val doc = DocumentFile.fromSingleUri(App.INSTANCE, uri)
doc?.let {
@ -94,7 +94,7 @@ object LocalBook {
}
if (deleteOriginal) {
if (book.bookUrl.isContentPath()) {
if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl)
DocumentFile.fromSingleUri(App.INSTANCE, uri)?.delete()
} else {

@ -26,7 +26,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
fun importSourceFromFilePath(path: String) {
execute {
val content = if (path.isContentPath()) {
val content = if (path.isContentScheme()) {
//在前面被解码了,如果不进行编码,中文会无法识别
val newPath = Uri.encode(path, ":/.")
DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context)

@ -10,7 +10,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.OldReplace
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isContentPath
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.readText
import java.io.File
@ -22,7 +22,7 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
fun importFromFilePath(path: String) {
execute {
val content = if (path.isContentPath()) {
val content = if (path.isContentScheme()) {
//在前面被解码了,如果不进行编码,中文会无法识别
val newPath = Uri.encode(path, ":/.")
DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context)

@ -26,7 +26,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
fun importSourceFromFilePath(path: String) {
execute {
val content = if (path.isContentPath()) {
val content = if (path.isContentScheme()) {
//在前面被解码了,如果不进行编码,中文会无法识别
val newPath = Uri.encode(path, ":/.")
DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context)

@ -207,7 +207,7 @@ class CacheActivity : VMBaseActivity<CacheViewModel>(R.layout.activity_download)
when (requestCode) {
exportRequestCode -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION

@ -20,7 +20,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
fun export(path: String, book: Book, finally: (msg: String) -> Unit) {
execute {
if (path.isContentPath()) {
if (path.isContentScheme()) {
val uri = Uri.parse(path)
DocumentFile.fromTreeUri(context, uri)?.let {
export(it, book)

@ -107,7 +107,7 @@ class BookInfoEditActivity :
}
private fun coverChangeTo(uri: Uri) {
if (uri.toString().isContentPath()) {
if (uri.isContentScheme()) {
val doc = DocumentFile.fromSingleUri(this, uri)
doc?.name?.let {
var file = this.externalFilesDir

@ -128,7 +128,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
tv_empty_msg.visible()
FilePicker.selectFolder(this, requestCodeSelectFolder)
}
lastPath.isContentPath() -> {
lastPath.isContentScheme() -> {
val rootUri = Uri.parse(lastPath)
rootDoc = DocumentFile.fromTreeUri(this, rootUri)
if (rootDoc == null) {
@ -276,7 +276,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
when (requestCode) {
requestCodeSelectFolder -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
@ -296,7 +296,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
@Synchronized
override fun nextDoc(uri: Uri) {
if (uri.toString().isContentPath()) {
if (uri.toString().isContentScheme()) {
subDocs.add(DocumentFile.fromSingleUri(this, uri)!!)
} else {
path = uri.path.toString()

@ -84,7 +84,8 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
ll_brief.gone()
cb_select.isChecked = false
} else {
val path = if (item.uri.isContentPath()) item.uri.toString() else item.uri.path
val path =
if (item.uri.isContentScheme()) item.uri.toString() else item.uri.path
if (bookPaths.contains(path)) {
iv_icon.setImageResource(R.drawable.ic_book_has)
iv_icon.visible()

@ -7,7 +7,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.DocItem
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.isContentPath
import io.legado.app.utils.isContentScheme
import java.io.File
import java.util.*
@ -28,7 +28,7 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
execute {
uriList.forEach {
val uri = Uri.parse(it)
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
DocumentFile.fromSingleUri(context, uri)?.delete()
} else {
uri.path?.let { path ->

@ -248,7 +248,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack {
}
val configZipPath = FileUtils.getPath(requireContext().eCacheDir, configFileName)
if (ZipUtils.zipFiles(exportFiles, File(configZipPath))) {
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc ->
treeDoc.findFile(configFileName)?.delete()
treeDoc.createFile("", configFileName)
@ -391,7 +391,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack {
}
private fun setBgFromUri(uri: Uri) {
if (uri.toString().isContentPath()) {
if (uri.toString().isContentScheme()) {
val doc = DocumentFile.fromSingleUri(requireContext(), uri)
doc?.name?.let {
val file =

@ -381,13 +381,13 @@ object ChapterProvider {
typeface = try {
val fontPath = ReadBookConfig.textFont
when {
fontPath.isContentPath() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
fontPath.isContentScheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
val fd = App.INSTANCE.contentResolver
.openFileDescriptor(Uri.parse(fontPath), "r")!!
.fileDescriptor
Typeface.Builder(fd).build()
}
fontPath.isContentPath() -> {
fontPath.isContentScheme() -> {
Typeface.createFromFile(RealPathUtil.getPath(App.INSTANCE, Uri.parse(fontPath)))
}
fontPath.isNotEmpty() -> Typeface.createFromFile(fontPath)

@ -477,7 +477,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
}
exportRequestCode -> {
data?.data?.let { uri ->
if (uri.toString().isContentPath()) {
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(this, uri)?.let {
viewModel.exportSelection(adapter.getSelection(), it)
}

@ -18,7 +18,7 @@ import io.legado.app.help.storage.Restore
import io.legado.app.help.storage.WebDavHelp
import io.legado.app.ui.filepicker.FilePicker
import io.legado.app.utils.getPrefString
import io.legado.app.utils.isContentPath
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.longToast
import io.legado.app.utils.toast
import kotlinx.coroutines.Dispatchers.Main
@ -35,7 +35,7 @@ object BackupRestoreUi {
if (backupPath.isNullOrEmpty()) {
selectBackupFolder(fragment, backupSelectRequestCode)
} else {
if (backupPath.isContentPath()) {
if (backupPath.isContentScheme()) {
val uri = Uri.parse(backupPath)
val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri)
if (doc?.canWrite() == true) {
@ -82,7 +82,7 @@ object BackupRestoreUi {
fragment.longToast("WebDavError:${it.localizedMessage}\n将从本地备份恢复。")
val backupPath = fragment.getPrefString(PreferKey.backupPath)
if (backupPath?.isNotEmpty() == true) {
if (backupPath.isContentPath()) {
if (backupPath.isContentScheme()) {
val uri = Uri.parse(backupPath)
val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri)
if (doc?.canWrite() == true) {
@ -125,7 +125,7 @@ object BackupRestoreUi {
when (requestCode) {
backupSelectRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
@ -151,7 +151,7 @@ object BackupRestoreUi {
}
restoreSelectRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
@ -173,7 +173,7 @@ object BackupRestoreUi {
}
selectFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
if (uri.isContentScheme()) {
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION

@ -186,7 +186,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
}
private fun setCoverFromUri(uri: Uri) {
if (uri.toString().isContentPath()) {
if (uri.isContentScheme()) {
val doc = DocumentFile.fromSingleUri(requireContext(), uri)
doc?.name?.let {
var file = requireContext().externalFilesDir

@ -262,7 +262,7 @@ class ReplaceRuleActivity :
}
exportRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.toString().isContentPath()) {
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(this, uri)?.let {
viewModel.exportSelection(adapter.getSelection(), it)
}

@ -17,11 +17,11 @@ import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource
import io.legado.app.data.entities.RssStar
import io.legado.app.help.http.HttpHelper
import io.legado.app.model.rss.Rss
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.rss.Rss
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentPath
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.writeBytes
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
@ -111,7 +111,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
execute {
val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg"
webData2bitmap(webPic)?.let { biteArray ->
if (path.isContentPath()) {
if (path.isContentScheme()) {
val uri = Uri.parse(path)
DocumentFile.fromTreeUri(context, uri)?.let { doc ->
DocumentUtils.createFileIfNotExist(doc, fileName)

@ -268,7 +268,7 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
}
exportRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.toString().isContentPath()) {
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(this, uri)?.let {
viewModel.exportSelection(adapter.getSelection(), it)
}

@ -68,7 +68,7 @@ class FontSelectDialog : BaseDialogFragment(),
if (fontPath.isNullOrEmpty()) {
openFolder()
} else {
if (fontPath.isContentPath()) {
if (fontPath.isContentScheme()) {
val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath))
if (doc?.canRead() == true) {
loadFontFiles(doc)
@ -209,7 +209,7 @@ class FontSelectDialog : BaseDialogFragment(),
when (requestCode) {
fontFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.toString().isContentPath()) {
if (uri.toString().isContentScheme()) {
putPrefString(PreferKey.fontFolder, uri.toString())
val doc = DocumentFile.fromTreeUri(requireContext(), uri)
if (doc != null) {

@ -139,7 +139,7 @@ data class DocItem(
DocumentsContract.Document.MIME_TYPE_DIR == attr
}
val isContentPath get() = uri.toString().isContentPath()
val isContentPath get() = uri.isContentScheme()
}
@Throws(Exception::class)

@ -11,10 +11,10 @@ val notImgHtmlRegex = "</?(?!img)\\w+[^>]*>".toRegex()
fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim()
fun String?.isContentPath(): Boolean = this?.startsWith("content://") == true
fun String?.isContentScheme(): Boolean = this?.startsWith("content://") == true
fun String.parseToUri(): Uri {
return if (isContentPath()) {
return if (isContentScheme()) {
Uri.parse(this)
} else {
Uri.fromFile(File(this))

@ -4,11 +4,11 @@ import android.content.Context
import android.net.Uri
import java.io.File
fun Uri.isContentPath() = this.toString().isContentPath()
fun Uri.isContentScheme() = this.scheme == "content"
@Throws(Exception::class)
fun Uri.readBytes(context: Context): ByteArray? {
if (this.toString().isContentPath()) {
if (this.toString().isContentScheme()) {
return DocumentUtils.readBytes(context, this)
} else {
val path = RealPathUtil.getPath(context, this)
@ -32,7 +32,7 @@ fun Uri.writeBytes(
context: Context,
byteArray: ByteArray
): Boolean {
if (this.toString().isContentPath()) {
if (this.toString().isContentScheme()) {
return DocumentUtils.writeBytes(context, byteArray, this)
} else {
val path = RealPathUtil.getPath(context, this)

Loading…
Cancel
Save