add file fetching at mime type activity

This commit is contained in:
tibbi 2021-10-13 15:57:41 +02:00
parent ffc838037f
commit 8fb2f56506
2 changed files with 32 additions and 10 deletions

View File

@ -11,11 +11,13 @@ import androidx.core.view.MenuItemCompat
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.dialogs.ChangeSortingDialog import com.simplemobiletools.filemanager.pro.dialogs.ChangeSortingDialog
import com.simplemobiletools.filemanager.pro.dialogs.ChangeViewTypeDialog import com.simplemobiletools.filemanager.pro.dialogs.ChangeViewTypeDialog
import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.helpers.* import com.simplemobiletools.filemanager.pro.helpers.*
import com.simplemobiletools.filemanager.pro.models.ListItem
import java.util.* import java.util.*
class MimeTypesActivity : SimpleActivity() { class MimeTypesActivity : SimpleActivity() {
@ -44,7 +46,11 @@ class MimeTypesActivity : SimpleActivity() {
) )
ensureBackgroundThread { ensureBackgroundThread {
getProperFilePaths { paths -> getProperFileDirItems { fileDirItems ->
FileDirItem.sorting = config.getFolderSorting(currentMimeType)
fileDirItems.sort()
val listItems = getListItemsFromFileDirItems(fileDirItems)
runOnUiThread { runOnUiThread {
} }
@ -97,12 +103,15 @@ class MimeTypesActivity : SimpleActivity() {
return true return true
} }
private fun getProperFilePaths(callback: (ArrayList<String>) -> Unit) { private fun getProperFileDirItems(callback: (ArrayList<FileDirItem>) -> Unit) {
val filePaths = ArrayList<String>() val fileDirItems = ArrayList<FileDirItem>()
val uri = MediaStore.Files.getContentUri("external") val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf( val projection = arrayOf(
MediaStore.Files.FileColumns.MIME_TYPE, MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.DATA MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.SIZE,
MediaStore.Files.FileColumns.DATE_MODIFIED
) )
try { try {
@ -110,32 +119,35 @@ class MimeTypesActivity : SimpleActivity() {
try { try {
val fullMimetype = cursor.getStringValue(MediaStore.Files.FileColumns.MIME_TYPE)?.lowercase(Locale.getDefault()) ?: return@queryCursor val fullMimetype = cursor.getStringValue(MediaStore.Files.FileColumns.MIME_TYPE)?.lowercase(Locale.getDefault()) ?: return@queryCursor
val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA) val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA)
val name = cursor.getStringValue(MediaStore.Files.FileColumns.DISPLAY_NAME)
val size = cursor.getLongValue(MediaStore.Files.FileColumns.SIZE)
val lastModified = cursor.getLongValue(MediaStore.Files.FileColumns.DATE_MODIFIED) * 1000
val mimetype = fullMimetype.substringBefore("/") val mimetype = fullMimetype.substringBefore("/")
when (currentMimeType) { when (currentMimeType) {
IMAGES -> { IMAGES -> {
if (mimetype == "image") { if (mimetype == "image") {
filePaths.add(path) fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
} }
} }
VIDEOS -> { VIDEOS -> {
if (mimetype == "video") { if (mimetype == "video") {
filePaths.add(path) fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
} }
} }
AUDIO -> { AUDIO -> {
if (mimetype == "audio" || extraAudioMimeTypes.contains(fullMimetype)) { if (mimetype == "audio" || extraAudioMimeTypes.contains(fullMimetype)) {
filePaths.add(path) fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
} }
} }
DOCUMENTS -> { DOCUMENTS -> {
if (mimetype == "text" || extraDocumentMimeTypes.contains(fullMimetype)) { if (mimetype == "text" || extraDocumentMimeTypes.contains(fullMimetype)) {
filePaths.add(path) fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
} }
} }
ARCHIVES -> { ARCHIVES -> {
if (archiveMimeTypes.contains(fullMimetype)) { if (archiveMimeTypes.contains(fullMimetype)) {
filePaths.add(path) fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
} }
} }
} }
@ -146,7 +158,16 @@ class MimeTypesActivity : SimpleActivity() {
showErrorToast(e) showErrorToast(e)
} }
callback(filePaths) callback(fileDirItems)
}
private fun getListItemsFromFileDirItems(fileDirItems: ArrayList<FileDirItem>): ArrayList<ListItem> {
val listItems = ArrayList<ListItem>()
fileDirItems.forEach {
val listItem = ListItem(it.path, it.name, false, 0, it.size, it.modified, false)
listItems.add(listItem)
}
return listItems
} }
private fun setupSearch(menu: Menu) { private fun setupSearch(menu: Menu) {

View File

@ -2,5 +2,6 @@ package com.simplemobiletools.filemanager.pro.models
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
// isSectionTitle is used only at search results for showing the current folders path
data class ListItem(val mPath: String, val mName: String = "", var mIsDirectory: Boolean = false, var mChildren: Int = 0, var mSize: Long = 0L, var mModified: Long = 0L, data class ListItem(val mPath: String, val mName: String = "", var mIsDirectory: Boolean = false, var mChildren: Int = 0, var mSize: Long = 0L, var mModified: Long = 0L,
var isSectionTitle: Boolean) : FileDirItem(mPath, mName, mIsDirectory, mChildren, mSize, mModified) var isSectionTitle: Boolean) : FileDirItem(mPath, mName, mIsDirectory, mChildren, mSize, mModified)