From 440ad903d8c78c04ba0508f8b788276c03a48b88 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 19 Feb 2020 09:47:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/read/config/BgTextConfigDialog.kt | 2 +- .../io/legado/app/ui/qrcode/QrCodeActivity.kt | 2 +- .../app/ui/widget/font/FontSelectDialog.kt | 32 +++++++++++++------ .../java/io/legado/app/utils/DocumentUtils.kt | 11 ++++--- .../java/io/legado/app/utils/FileUtils.kt | 2 +- .../java/io/legado/app/utils/UriExtensions.kt | 8 ++--- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 2ad61a9b5..8acc883ee 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -193,7 +193,7 @@ class BgTextConfigDialog : DialogFragment() { ) .rationale(R.string.bg_image_per) .onGranted { - FileUtils.getPath(requireContext(), uri)?.let { path -> + FileUtils.getRealPath(requireContext(), uri)?.let { path -> ReadBookConfig.getConfig().setBg(2, path) ReadBookConfig.upBg() postEvent(EventBus.UP_CONFIG, false) diff --git a/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt b/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt index b84cc3f6f..c9e4d4bd2 100644 --- a/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt +++ b/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt @@ -98,7 +98,7 @@ class QrCodeActivity : BaseActivity(R.layout.activity_qrcode_capture), QRCodeVie zxingview.startSpotAndShowRect() // 显示扫描框,并开始识别 if (resultCode == Activity.RESULT_OK && requestCode == requestQrImage) { - val picturePath = FileUtils.getPath(this, it) + val picturePath = FileUtils.getRealPath(this, it) // 本来就用到 QRCodeView 时可直接调 QRCodeView 的方法,走通用的回调 zxingview.decodeQRCode(picturePath) } diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index a129ae47c..636aa8a32 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -38,8 +38,9 @@ class FontSelectDialog : DialogFragment(), private val fontFolderRequestCode = 35485 private val fontFolder = App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator - private val fontCacheFolder = - App.INSTANCE.cacheDir.absolutePath + File.separator + "Fonts" + File.separator + private val fontCacheFolder by lazy { + FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts") + } override val coroutineContext: CoroutineContext get() = job + Main private var adapter: FontAdapter? = null @@ -122,18 +123,31 @@ class FontSelectDialog : DialogFragment(), @SuppressLint("DefaultLocale") private fun getFontFiles(uri: Uri) { launch(IO) { - FileUtils.deleteFile(fontCacheFolder) - DocumentUtils.listFiles(App.INSTANCE, uri).forEach { item -> + val docItems = DocumentUtils.listFiles(App.INSTANCE, uri) + fontCacheFolder.listFiles()?.forEach { fontFile -> + var contain = false + for (item in docItems) { + if (fontFile.name == item.name) { + contain = true + break + } + } + if (!contain) { + fontFile.delete() + } + } + docItems.forEach { item -> if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) { - DocumentUtils.readBytes(App.INSTANCE, item.uri)?.let { byteArray -> - FileUtils.createFileIfNotExist(fontCacheFolder + item.name) - .writeBytes(byteArray) + val fontFile = FileUtils.getFile(fontCacheFolder, item.name) + if (!fontFile.exists()) { + DocumentUtils.readBytes(App.INSTANCE, item.uri)?.let { byteArray -> + fontFile.writeBytes(byteArray) + } } } } try { - val file = File(fontCacheFolder) - file.listFiles { pathName -> + fontCacheFolder.listFiles { pathName -> pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) }?.let { withContext(Main) { diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index 2adfcddf4..e10cd8ccf 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -85,12 +85,12 @@ object DocumentUtils { fun listFiles(context: Context, uri: Uri): ArrayList { val docList = arrayListOf() - val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( - uri, - DocumentsContract.getDocumentId(uri) - ) var c: Cursor? = null try { + val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( + uri, + DocumentsContract.getDocumentId(uri) + ) c = context.contentResolver.query( childrenUri, arrayOf( DocumentsContract.Document.COLUMN_DOCUMENT_ID, @@ -118,7 +118,8 @@ object DocumentUtils { docList.add(item) } while (c.moveToNext()) } - } catch (e: java.lang.Exception) { + } catch (e: Exception) { + e.printStackTrace() } finally { c?.close() } diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 59b5c4d24..c01eff00d 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -105,7 +105,7 @@ object FileUtils { return sdCardDirectory } - fun getPath(context: Context, uri: Uri): String? { + fun getRealPath(context: Context, uri: Uri): String? { // DocumentProvider if (DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider 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 4062c74df..cd3056941 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -10,7 +10,7 @@ fun Uri.readBytes(context: Context): ByteArray? { if (DocumentFile.isDocumentUri(context, this)) { return DocumentUtils.readBytes(context, this) } else { - val path = FileUtils.getPath(context, this) + val path = FileUtils.getRealPath(context, this) if (path?.isNotEmpty() == true) { return File(path).readBytes() } @@ -23,7 +23,7 @@ fun Uri.readText(context: Context): String? { if (DocumentFile.isDocumentUri(context, this)) { return DocumentUtils.readText(context, this) } else { - val path = FileUtils.getPath(context, this) + val path = FileUtils.getRealPath(context, this) if (path?.isNotEmpty() == true) { return File(path).readText() } @@ -36,7 +36,7 @@ fun Uri.writeBytes(context: Context, byteArray: ByteArray): Boolean { if (DocumentFile.isDocumentUri(context, this)) { return DocumentUtils.writeBytes(context, byteArray, this) } else { - val path = FileUtils.getPath(context, this) + val path = FileUtils.getRealPath(context, this) if (path?.isNotEmpty() == true) { File(path).writeBytes(byteArray) return true @@ -50,7 +50,7 @@ fun Uri.writeText(context: Context, text: String): Boolean { if (DocumentFile.isDocumentUri(context, this)) { return DocumentUtils.writeText(context, text, this) } else { - val path = FileUtils.getPath(context, this) + val path = FileUtils.getRealPath(context, this) if (path?.isNotEmpty() == true) { File(path).writeText(text) return true