diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 879463ae8..195f66aa5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -26,9 +26,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val media = ArrayList() val showMedia = config.showMedia val fileSorting = config.fileSorting - val parents = context.getParents(isPickImage, isPickVideo) + val parents = context.getParents() - parents.map { File(it).listFiles() } + parents.mapNotNull { File(it).listFiles() } .forEach { for (file in it) { val isImage = file.isImageFast() || file.isGif() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 5bfdf5075..688c1f099 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -30,7 +30,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo val media = ArrayList() if (showAll) { - val parents = context.getParents(isPickImage, isPickVideo) + val parents = context.getParents() for (parent in parents) { media.addAll(getFilesFrom(parent)) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index 071537b15..a21f897fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -11,9 +11,7 @@ import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.helpers.Config -import com.simplemobiletools.gallery.helpers.IMAGES import com.simplemobiletools.gallery.helpers.NOMEDIA -import com.simplemobiletools.gallery.helpers.VIDEOS import java.io.File import java.util.* @@ -51,55 +49,34 @@ fun Context.launchSettings() { startActivity(Intent(this, SettingsActivity::class.java)) } -fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList { +fun Context.getParents(): ArrayList { val uri = MediaStore.Files.getContentUri("external") - val where = "${getWhereCondition(isPickImage, isPickVideo)} GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} " - val args = getArgs(isPickImage, isPickVideo) - val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA) + val columns = arrayOf(MediaStore.Images.Media.DATA) var cursor: Cursor? = null val parents = ArrayList() + val parentsSet = HashSet() try { - cursor = contentResolver.query(uri, columns, where, args, null) + cursor = contentResolver.query(uri, columns, null, null, null) if (cursor?.moveToFirst() == true) { do { - val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: "" - parents.add(File(curPath).parent) + val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue + val parent = File(curPath).parent ?: continue + parentsSet.add(parent) } while (cursor.moveToNext()) } } finally { cursor?.close() } - val filtered = ArrayList() - parents.mapNotNullTo(filtered, { it }) + parentsSet.filterTo(parents, { hasImageVideoGif(File(it)) }) if (config.showHiddenFolders) { - filtered.addAll(getNoMediaFolders()) + parents.addAll(getNoMediaFolders()) } else { - removeNoMediaFolders(filtered) - } - return filtered -} - -fun Context.getWhereCondition(isPickImage: Boolean, isPickVideo: Boolean): String { - val showMedia = config.showMedia - return if ((isPickImage || showMedia == IMAGES) || (isPickVideo || showMedia == VIDEOS)) { - "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)" - } else { - "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? OR ${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)" - } -} - -fun Context.getArgs(isPickImage: Boolean, isPickVideo: Boolean): Array { - val showMedia = config.showMedia - return if (isPickImage || showMedia == IMAGES) { - arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString()) - } else if (isPickVideo || showMedia == VIDEOS) { - arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString()) - } else { - arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(), MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString()) + removeNoMediaFolders(parents) } + return parents } private fun removeNoMediaFolders(paths: MutableList) { @@ -146,9 +123,7 @@ fun Context.getNoMediaFolders(): ArrayList { fun hasImageVideoGif(dir: File): Boolean { if (dir.isDirectory) { - dir.listFiles() - .filter(File::isImageVideoGif) - .forEach { return true } + dir.listFiles()?.filter(File::isImageVideoGif)?.any { return true } } return false }