From d596ca600fb221d0c41d5b02ab43d80329c98f64 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 31 Jan 2020 15:21:42 +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 --- .../app/ui/importbook/ImportBookActivity.kt | 13 ++--- .../app/ui/importbook/ImportBookAdapter.kt | 22 +++----- .../java/io/legado/app/utils/DocumentUtils.kt | 56 +++++++++++++++++++ 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt index 1352268aa..7fe292b31 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt @@ -16,6 +16,7 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.help.AppConfig import io.legado.app.lib.theme.accentColor +import io.legado.app.utils.DocumentUtils import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_import_book.* import kotlinx.coroutines.Dispatchers.IO @@ -107,15 +108,8 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity importBookAdapter.clearItems() rotate_loading.show() launch(IO) { - val docList = arrayListOf() - lastDoc.listFiles().forEach { - if (it.isDirectory && it.name?.startsWith(".") == false) { - docList.add(it) - } else if (it.name?.endsWith(".txt", true) == true) { - docList.add(it) - } - } - docList.sortWith(compareBy({ !it.isDirectory }, { it.name })) + val docList = DocumentUtils.listFiles(this@ImportBookActivity, lastDoc.uri) + docList.sortWith(compareBy({ !it.isDir }, { it.name })) withContext(Main) { rotate_loading.hide() importBookAdapter.setItems(docList) @@ -194,4 +188,5 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity btn_add_book.isEnabled = isClickable btn_add_book.isClickable = isClickable } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt index 34ebd4e36..14fe2d5a5 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt @@ -6,17 +6,13 @@ import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.AppConst -import io.legado.app.utils.StringUtils -import io.legado.app.utils.gone -import io.legado.app.utils.invisible -import io.legado.app.utils.visible +import io.legado.app.utils.* import kotlinx.android.synthetic.main.item_import_book.view.* import org.jetbrains.anko.sdk27.listeners.onClick -import java.util.* class ImportBookAdapter(context: Context, val callBack: CallBack) : - SimpleRecyclerAdapter(context, R.layout.item_import_book) { + SimpleRecyclerAdapter(context, R.layout.item_import_book) { var selectedUris = linkedSetOf() private var localUri = arrayListOf() @@ -26,9 +22,9 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) : notifyDataSetChanged() } - override fun convert(holder: ItemViewHolder, item: DocumentFile, payloads: MutableList) { + override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList) { holder.itemView.apply { - if (item.isDirectory) { + if (item.isDir) { iv_icon.setImageResource(R.drawable.ic_folder) iv_icon.visible() cb_select.invisible() @@ -43,15 +39,15 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) : cb_select.visible() } ll_brief.visible() - tv_tag.text = item.name?.substringAfterLast(".") - tv_size.text = StringUtils.toSize(item.length()) - tv_date.text = AppConst.DATE_FORMAT.format(Date(item.lastModified())) + tv_tag.text = item.name.substringAfterLast(".") + tv_size.text = StringUtils.toSize(item.size) + tv_date.text = AppConst.DATE_FORMAT.format(item.date) } tv_name.text = item.name cb_select.isChecked = selectedUris.contains(item.uri.toString()) onClick { - if (item.isDirectory) { - callBack.nextDoc(item) + if (item.isDir) { + callBack.nextDoc(DocumentFile.fromTreeUri(context, item.uri)!!) } else { cb_select.isChecked = !cb_select.isChecked if (cb_select.isChecked) { 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 8e74c63e0..15b1a7754 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -1,8 +1,11 @@ package io.legado.app.utils import android.content.Context +import android.database.Cursor import android.net.Uri +import android.provider.DocumentsContract import androidx.documentfile.provider.DocumentFile +import java.util.* object DocumentUtils { @@ -79,5 +82,58 @@ object DocumentUtils { return null } + fun listFiles(context: Context, uri: Uri): ArrayList { + val docList = arrayListOf() + val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( + uri, + DocumentsContract.getDocumentId(uri) + ) + var c: Cursor? = null + try { + c = context.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, null + ) + c?.let { + val ici = c.getColumnIndex(DocumentsContract.Document.COLUMN_DOCUMENT_ID) + val nci = c.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME) + val sci = c.getColumnIndex(DocumentsContract.Document.COLUMN_SIZE) + val mci = c.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE) + val dci = c.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED) + c.moveToFirst() + do { + val item = DocItem( + name = c.getString(nci), + attr = c.getString(mci), + size = c.getLong(sci), + date = Date(c.getLong(dci)), + uri = DocumentsContract.buildDocumentUriUsingTree(uri, c.getString(ici)) + ) + docList.add(item) + } while (c.moveToNext()) + } + } catch (e: java.lang.Exception) { + } finally { + c?.close() + } + return docList + } +} + +data class DocItem( + val name: String, + val attr: String, + val size: Long, + val date: Date, + val uri: Uri +) { + val isDir: Boolean by lazy { + DocumentsContract.Document.MIME_TYPE_DIR == attr + } } \ No newline at end of file