diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index 1579213dc..fe9034d49 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -190,7 +190,7 @@ class ImportBookActivity : VMBaseActivity Unit, finally: (() -> Unit)? = null ) { - val docList = DocumentUtils.listFiles(context, documentFile.uri) + val docList = DocumentUtils.listFiles(documentFile.uri) docList.forEach { docItem -> if (docItem.isDir) { DocumentFile.fromSingleUri(context, docItem.uri)?.let { 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 d1a023c88..66dc30de2 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 @@ -31,7 +31,7 @@ import kotlin.collections.ArrayList class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select), Toolbar.OnMenuItemClickListener, FontAdapter.CallBack { - private val fontRegex = Regex(".*\\.[ot]tf") + private val fontRegex = Regex("(?i).*\\.[ot]tf") private val fontFolder by lazy { FileUtils.createFolderIfNotExist(appCtx.filesDir, "Fonts") } @@ -148,13 +148,7 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select), private fun loadFontFiles(doc: DocumentFile) { execute { - val fontItems = arrayListOf() - val docItems = DocumentUtils.listFiles(appCtx, doc.uri) - docItems.forEach { item -> - if (item.name.lowercase(Locale.getDefault()).matches(fontRegex)) { - fontItems.add(item) - } - } + val fontItems = DocumentUtils.listFiles(doc.uri, fontRegex) mergeFontItems(fontItems, getLocalFonts()) }.onSuccess { adapter.setItems(it) @@ -175,21 +169,7 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select), private fun loadFontFiles(path: String) { execute { - val fontItems = arrayListOf() - val file = File(path) - file.listFiles { pathName -> - pathName.name.lowercase(Locale.getDefault()).matches(fontRegex) - }?.forEach { - fontItems.add( - DocItem( - it.name, - it.extension, - it.length(), - Date(it.lastModified()), - Uri.parse(it.absolutePath) - ) - ) - } + val fontItems = DocumentUtils.listFiles(path, fontRegex) mergeFontItems(fontItems, getLocalFonts()) }.onSuccess { adapter.setItems(it) 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 64fa913eb..fd6d709c0 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt @@ -6,7 +6,9 @@ import android.net.Uri import android.provider.DocumentsContract import androidx.documentfile.provider.DocumentFile import io.legado.app.model.NoStackTraceException +import splitties.init.appCtx import timber.log.Timber +import java.io.File import java.nio.charset.Charset import java.util.* @@ -93,13 +95,13 @@ object DocumentUtils { } ?: throw NoStackTraceException("打开文件失败\n${uri}") } - fun listFiles(context: Context, uri: Uri): ArrayList { + fun listFiles(uri: Uri, regex: Regex? = null): ArrayList { val docList = arrayListOf() var cursor: Cursor? = null try { val childrenUri = DocumentsContract .buildChildDocumentsUriUsingTree(uri, DocumentsContract.getDocumentId(uri)) - cursor = context.contentResolver.query( + cursor = appCtx.contentResolver.query( childrenUri, arrayOf( DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME, @@ -116,15 +118,18 @@ object DocumentUtils { val dci = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED) if (cursor.moveToFirst()) { do { - val item = DocItem( - name = cursor.getString(nci), - attr = cursor.getString(mci), - size = cursor.getLong(sci), - date = Date(cursor.getLong(dci)), - uri = DocumentsContract - .buildDocumentUriUsingTree(uri, cursor.getString(ici)) - ) - docList.add(item) + val name = cursor.getString(nci) + if (regex == null || regex.matches(name)) { + val item = DocItem( + name = name, + attr = cursor.getString(mci), + size = cursor.getLong(sci), + date = Date(cursor.getLong(dci)), + uri = DocumentsContract + .buildDocumentUriUsingTree(uri, cursor.getString(ici)) + ) + docList.add(item) + } } while (cursor.moveToNext()) } } @@ -136,6 +141,29 @@ object DocumentUtils { return docList } + fun listFiles(path: String, regex: Regex? = null): ArrayList { + val docItems = arrayListOf() + kotlin.runCatching { + val file = File(path) + file.listFiles { pathName -> + regex?.let { + pathName.name.matches(it) + } ?: true + }?.forEach { + docItems.add( + DocItem( + it.name, + it.extension, + it.length(), + Date(it.lastModified()), + Uri.parse(it.absolutePath) + ) + ) + } + } + return docItems + } + } data class DocItem(