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.clearItems()
launch(IO) {
val docList = DocumentUtils.listFiles(this@ImportBookActivity, lastDoc.uri)
val docList = DocumentUtils.listFiles(lastDoc.uri)
for (i in docList.lastIndex downTo 0) {
val item = docList[i]
if (item.name.startsWith(".")) {

@ -47,7 +47,7 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
find: (docItem: DocItem) -> 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 {

@ -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<DocItem>()
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<DocItem>()
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)

@ -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<DocItem> {
fun listFiles(uri: Uri, regex: Regex? = null): ArrayList<DocItem> {
val docList = arrayListOf<DocItem>()
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,8 +118,10 @@ object DocumentUtils {
val dci = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
if (cursor.moveToFirst()) {
do {
val name = cursor.getString(nci)
if (regex == null || regex.matches(name)) {
val item = DocItem(
name = cursor.getString(nci),
name = name,
attr = cursor.getString(mci),
size = cursor.getLong(sci),
date = Date(cursor.getLong(dci)),
@ -125,6 +129,7 @@ object DocumentUtils {
.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<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(

Loading…
Cancel
Save