pull/83/head
kunfei 5 years ago
parent 064f872cdb
commit d596ca600f
  1. 13
      app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt
  2. 22
      app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt
  3. 56
      app/src/main/java/io/legado/app/utils/DocumentUtils.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<ImportBookViewModel>(R.layout.activity
importBookAdapter.clearItems()
rotate_loading.show()
launch(IO) {
val docList = arrayListOf<DocumentFile>()
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<ImportBookViewModel>(R.layout.activity
btn_add_book.isEnabled = isClickable
btn_add_book.isClickable = isClickable
}
}

@ -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<DocumentFile>(context, R.layout.item_import_book) {
SimpleRecyclerAdapter<DocItem>(context, R.layout.item_import_book) {
var selectedUris = linkedSetOf<String>()
private var localUri = arrayListOf<String>()
@ -26,9 +22,9 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
notifyDataSetChanged()
}
override fun convert(holder: ItemViewHolder, item: DocumentFile, payloads: MutableList<Any>) {
override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) {
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) {

@ -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<DocItem> {
val docList = arrayListOf<DocItem>()
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
}
}
Loading…
Cancel
Save