From 1013f33cf641d83c4ce69bbbf2137e56d6b08368 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 16 Oct 2022 00:58:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/JsExtensions.kt | 2 +- .../legado/app/help/storage/ImportOldData.kt | 6 +- .../io/legado/app/utils/DocumentExtensions.kt | 68 ++++++------------- .../java/io/legado/app/utils/UriExtensions.kt | 20 ++++-- 4 files changed, 39 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 7990165aa..40423239a 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -164,7 +164,7 @@ interface JsExtensions { fun importScript(path: String): String { val result = when { path.startsWith("http") -> cacheFile(path) ?: "" - path.isContentScheme() -> DocumentUtils.readText(appCtx, Uri.parse(path)) + path.isUri() -> Uri.parse(path).readText(appCtx) path.startsWith("/storage") -> FileUtils.readText(path) else -> readTxtFile(path) } diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index 414c1db19..015118ccb 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -19,7 +19,7 @@ object ImportOldData { when (doc.name) { "myBookShelf.json" -> kotlin.runCatching { - DocumentUtils.readText(context, doc.uri).let { json -> + doc.uri.readText(context).let { json -> val importCount = importOldBookshelf(json) context.toastOnUi("成功导入书籍${importCount}") } @@ -28,7 +28,7 @@ object ImportOldData { } "myBookSource.json" -> kotlin.runCatching { - DocumentUtils.readText(context, doc.uri).let { json -> + doc.uri.readText(context).let { json -> val importCount = importOldSource(json) context.toastOnUi("成功导入书源${importCount}") } @@ -37,7 +37,7 @@ object ImportOldData { } "myBookReplaceRule.json" -> kotlin.runCatching { - DocumentUtils.readText(context, doc.uri).let { json -> + doc.uri.readText(context).let { json -> val importCount = importOldReplaceRule(json) context.toastOnUi("成功导入替换规则${importCount}") } diff --git a/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt b/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt index 5e2411706..91aa16c58 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt @@ -53,44 +53,14 @@ object DocumentUtils { return parent } - @JvmStatic - @Throws(Exception::class) - fun writeText( - context: Context, - data: String, - fileUri: Uri, - charset: Charset = Charsets.UTF_8 - ): Boolean { - return writeBytes(context, data.toByteArray(charset), fileUri) - } - - @JvmStatic - @Throws(Exception::class) - fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean { - context.contentResolver.openOutputStream(fileUri)?.let { - it.write(data) - it.close() - return true - } - return false - } - - @JvmStatic - @Throws(Exception::class) - fun readText(context: Context, uri: Uri): String { - return String(readBytes(context, uri)) - } - - @JvmStatic - @Throws(Exception::class) - fun readBytes(context: Context, uri: Uri): ByteArray { - context.contentResolver.openInputStream(uri)?.let { - val len: Int = it.available() - val buffer = ByteArray(len) - it.read(buffer) - it.close() - return buffer - } ?: throw NoStackTraceException("打开文件失败\n${uri}") + private val projection by lazy { + arrayOf( + DocumentsContract.Document.COLUMN_DOCUMENT_ID, + DocumentsContract.Document.COLUMN_DISPLAY_NAME, + DocumentsContract.Document.COLUMN_LAST_MODIFIED, + DocumentsContract.Document.COLUMN_SIZE, + DocumentsContract.Document.COLUMN_MIME_TYPE + ) } @Throws(Exception::class) @@ -104,13 +74,7 @@ object DocumentUtils { var cursor: Cursor? = null try { cursor = appCtx.contentResolver.query( - childrenUri, arrayOf( - DocumentsContract.Document.COLUMN_DOCUMENT_ID, - DocumentsContract.Document.COLUMN_DISPLAY_NAME, - DocumentsContract.Document.COLUMN_LAST_MODIFIED, - DocumentsContract.Document.COLUMN_SIZE, - DocumentsContract.Document.COLUMN_MIME_TYPE - ), null, null, DocumentsContract.Document.COLUMN_DISPLAY_NAME + childrenUri, projection, null, null, DocumentsContract.Document.COLUMN_DISPLAY_NAME ) cursor?.let { val ici = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DOCUMENT_ID) @@ -206,20 +170,26 @@ data class FileDoc( @Throws(Exception::class) fun DocumentFile.writeText(context: Context, data: String, charset: Charset = Charsets.UTF_8) { - DocumentUtils.writeText(context, data, this.uri, charset) + uri.writeText(context, data, charset) } @Throws(Exception::class) fun DocumentFile.writeBytes(context: Context, data: ByteArray) { - DocumentUtils.writeBytes(context, data, this.uri) + uri.writeBytes(context, data) } @Throws(Exception::class) fun DocumentFile.readText(context: Context): String { - return DocumentUtils.readText(context, this.uri) + return String(readBytes(context)) } @Throws(Exception::class) fun DocumentFile.readBytes(context: Context): ByteArray { - return DocumentUtils.readBytes(context, this.uri) + return context.contentResolver.openInputStream(uri)?.let { + val len: Int = it.available() + val buffer = ByteArray(len) + it.read(buffer) + it.close() + return buffer + } ?: throw NoStackTraceException("打开文件失败\n${uri}") } diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index aea7beff2..e5da25e27 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -13,6 +13,7 @@ import io.legado.app.lib.permission.PermissionsCompat import java.io.File import java.io.FileInputStream import java.io.InputStream +import java.nio.charset.Charset fun Uri.isContentScheme() = this.scheme == "content" @@ -100,7 +101,13 @@ fun Fragment.readUri(uri: Uri?, success: (fileDoc: FileDoc, inputStream: InputSt @Throws(Exception::class) fun Uri.readBytes(context: Context): ByteArray { return if (this.isContentScheme()) { - DocumentUtils.readBytes(context, this) + context.contentResolver.openInputStream(this)?.let { + val len: Int = it.available() + val buffer = ByteArray(len) + it.read(buffer) + it.close() + return buffer + } ?: throw NoStackTraceException("打开文件失败\n${this}") } else { val path = RealPathUtil.getPath(context, this) if (path?.isNotEmpty() == true) { @@ -124,7 +131,12 @@ fun Uri.writeBytes( byteArray: ByteArray ): Boolean { if (this.isContentScheme()) { - return DocumentUtils.writeBytes(context, byteArray, this) + context.contentResolver.openOutputStream(this)?.let { + it.write(byteArray) + it.close() + return true + } + return false } else { val path = RealPathUtil.getPath(context, this) if (path?.isNotEmpty() == true) { @@ -136,8 +148,8 @@ fun Uri.writeBytes( } @Throws(Exception::class) -fun Uri.writeText(context: Context, text: String): Boolean { - return writeBytes(context, text.toByteArray()) +fun Uri.writeText(context: Context, text: String, charset: Charset = Charsets.UTF_8): Boolean { + return writeBytes(context, text.toByteArray(charset)) } fun Uri.writeBytes(