pull/1415/head
gedoor 3 years ago
parent b594e7a24b
commit d5dad7065d
  1. 2
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt
  3. 26
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  4. 34
      app/src/main/java/io/legado/app/utils/DocumentExtensions.kt

@ -190,7 +190,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
adapter.selectedUris.clear() adapter.selectedUris.clear()
adapter.clearItems() adapter.clearItems()
launch(IO) { launch(IO) {
val docList = DocumentUtils.listFiles(this@ImportBookActivity, lastDoc.uri) val docList = DocumentUtils.listFiles(lastDoc.uri)
for (i in docList.lastIndex downTo 0) { for (i in docList.lastIndex downTo 0) {
val item = docList[i] val item = docList[i]
if (item.name.startsWith(".")) { if (item.name.startsWith(".")) {

@ -47,7 +47,7 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
find: (docItem: DocItem) -> Unit, find: (docItem: DocItem) -> Unit,
finally: (() -> Unit)? = null finally: (() -> Unit)? = null
) { ) {
val docList = DocumentUtils.listFiles(context, documentFile.uri) val docList = DocumentUtils.listFiles(documentFile.uri)
docList.forEach { docItem -> docList.forEach { docItem ->
if (docItem.isDir) { if (docItem.isDir) {
DocumentFile.fromSingleUri(context, docItem.uri)?.let { DocumentFile.fromSingleUri(context, docItem.uri)?.let {

@ -31,7 +31,7 @@ import kotlin.collections.ArrayList
class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select), class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
Toolbar.OnMenuItemClickListener, Toolbar.OnMenuItemClickListener,
FontAdapter.CallBack { FontAdapter.CallBack {
private val fontRegex = Regex(".*\\.[ot]tf") private val fontRegex = Regex("(?i).*\\.[ot]tf")
private val fontFolder by lazy { private val fontFolder by lazy {
FileUtils.createFolderIfNotExist(appCtx.filesDir, "Fonts") FileUtils.createFolderIfNotExist(appCtx.filesDir, "Fonts")
} }
@ -148,13 +148,7 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
private fun loadFontFiles(doc: DocumentFile) { private fun loadFontFiles(doc: DocumentFile) {
execute { execute {
val fontItems = arrayListOf<DocItem>() val fontItems = DocumentUtils.listFiles(doc.uri, fontRegex)
val docItems = DocumentUtils.listFiles(appCtx, doc.uri)
docItems.forEach { item ->
if (item.name.lowercase(Locale.getDefault()).matches(fontRegex)) {
fontItems.add(item)
}
}
mergeFontItems(fontItems, getLocalFonts()) mergeFontItems(fontItems, getLocalFonts())
}.onSuccess { }.onSuccess {
adapter.setItems(it) adapter.setItems(it)
@ -175,21 +169,7 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
private fun loadFontFiles(path: String) { private fun loadFontFiles(path: String) {
execute { execute {
val fontItems = arrayListOf<DocItem>() val fontItems = DocumentUtils.listFiles(path, fontRegex)
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)
)
)
}
mergeFontItems(fontItems, getLocalFonts()) mergeFontItems(fontItems, getLocalFonts())
}.onSuccess { }.onSuccess {
adapter.setItems(it) adapter.setItems(it)

@ -6,7 +6,9 @@ import android.net.Uri
import android.provider.DocumentsContract import android.provider.DocumentsContract
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.model.NoStackTraceException import io.legado.app.model.NoStackTraceException
import splitties.init.appCtx
import timber.log.Timber import timber.log.Timber
import java.io.File
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.* import java.util.*
@ -93,13 +95,13 @@ object DocumentUtils {
} ?: throw NoStackTraceException("打开文件失败\n${uri}") } ?: throw NoStackTraceException("打开文件失败\n${uri}")
} }
fun listFiles(context: Context, uri: Uri): ArrayList<DocItem> { fun listFiles(uri: Uri, regex: Regex? = null): ArrayList<DocItem> {
val docList = arrayListOf<DocItem>() val docList = arrayListOf<DocItem>()
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
val childrenUri = DocumentsContract val childrenUri = DocumentsContract
.buildChildDocumentsUriUsingTree(uri, DocumentsContract.getDocumentId(uri)) .buildChildDocumentsUriUsingTree(uri, DocumentsContract.getDocumentId(uri))
cursor = context.contentResolver.query( cursor = appCtx.contentResolver.query(
childrenUri, arrayOf( childrenUri, arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_DISPLAY_NAME,
@ -116,8 +118,10 @@ object DocumentUtils {
val dci = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED) val dci = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
val name = cursor.getString(nci)
if (regex == null || regex.matches(name)) {
val item = DocItem( val item = DocItem(
name = cursor.getString(nci), name = name,
attr = cursor.getString(mci), attr = cursor.getString(mci),
size = cursor.getLong(sci), size = cursor.getLong(sci),
date = Date(cursor.getLong(dci)), date = Date(cursor.getLong(dci)),
@ -125,6 +129,7 @@ object DocumentUtils {
.buildDocumentUriUsingTree(uri, cursor.getString(ici)) .buildDocumentUriUsingTree(uri, cursor.getString(ici))
) )
docList.add(item) docList.add(item)
}
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} }
@ -136,6 +141,29 @@ object DocumentUtils {
return docList return docList
} }
fun listFiles(path: String, regex: Regex? = null): ArrayList<DocItem> {
val docItems = arrayListOf<DocItem>()
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( data class DocItem(

Loading…
Cancel
Save