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 3b6059ae9..c6f1a70fb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -1,15 +1,13 @@ package com.simplemobiletools.gallery.asynctasks import android.content.Context -import android.database.Cursor import android.os.AsyncTask -import android.provider.MediaStore import com.simplemobiletools.commons.extensions.isGif import com.simplemobiletools.commons.extensions.isImageFast import com.simplemobiletools.commons.extensions.isVideoFast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.getHumanizedFilename -import com.simplemobiletools.gallery.extensions.getStringValue +import com.simplemobiletools.gallery.extensions.getParents import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.IMAGES import com.simplemobiletools.gallery.helpers.SORT_BY_DATE_MODIFIED @@ -28,54 +26,12 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va mConfig = Config.newInstance(context) } - private fun getWhereCondition(): String { - val showMedia = mConfig.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} = ?)" - } - } - - private fun getArgs(): Array { - val showMedia = mConfig.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()) - } - } - - private fun getParents(): ArrayList { - val uri = MediaStore.Files.getContentUri("external") - val where = "${getWhereCondition()} GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} " - val args = getArgs() - val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA) - var cursor: Cursor? = null - val parents = ArrayList() - - try { - cursor = context.contentResolver.query(uri, columns, where, args, null) - if (cursor?.moveToFirst() == true) { - do { - val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) - parents.add(File(curPath).parent) - } while (cursor.moveToNext()) - } - } finally { - cursor?.close() - } - return parents - } - override fun doInBackground(vararg params: Void): ArrayList { val directories = LinkedHashMap() val media = ArrayList() val showMedia = mConfig.showMedia val fileSorting = mConfig.fileSorting - val parents = getParents() + val parents = context.getParents(isPickImage, isPickVideo) parents.mapNotNull { File(it).listFiles() } .forEach { 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 f6e33dc14..ffb7f1564 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -7,6 +7,11 @@ import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.humanizePath import com.simplemobiletools.gallery.activities.SettingsActivity +import com.simplemobiletools.gallery.helpers.Config +import com.simplemobiletools.gallery.helpers.IMAGES +import com.simplemobiletools.gallery.helpers.VIDEOS +import java.io.File +import java.util.* fun Context.getRealPathFromURI(uri: Uri): String? { var cursor: Cursor? = null @@ -36,3 +41,45 @@ fun Context.launchCamera() { fun Context.launchSettings() { startActivity(Intent(this, SettingsActivity::class.java)) } + +fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList { + val uri = MediaStore.Files.getContentUri("external") + val where = "${getWhereCondition(this, isPickImage, isPickVideo)} GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} " + val args = getArgs(this, isPickImage, isPickVideo) + val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA) + var cursor: Cursor? = null + val parents = ArrayList() + + try { + cursor = contentResolver.query(uri, columns, where, args, null) + if (cursor?.moveToFirst() == true) { + do { + val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) + parents.add(File(curPath).parent) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + return parents +} + +private fun getWhereCondition(context: Context, isPickImage: Boolean, isPickVideo: Boolean): String { + val showMedia = Config.newInstance(context).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} = ?)" + } +} + +private fun getArgs(context: Context, isPickImage: Boolean, isPickVideo: Boolean): Array { + val showMedia = Config.newInstance(context).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()) + } +}