diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index aadce2b89..500ff30a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -147,17 +147,22 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun hideFolder() { - addNoMedia(mPath) - - if (!config.showHiddenFolders) - finish() - else - invalidateOptionsMenu() + addNoMedia(mPath) { + runOnUiThread { + if (!config.showHiddenFolders) + finish() + else + invalidateOptionsMenu() + } + } } private fun unhideFolder() { - removeNoMedia(mPath) - invalidateOptionsMenu() + removeNoMedia(mPath) { + runOnUiThread { + invalidateOptionsMenu() + } + } } private fun deleteDirectoryIfEmpty() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 795544ffe..c30a1955a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -173,14 +173,22 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList Unit) { val file = File(path, NOMEDIA) if (file.exists()) return @@ -155,10 +155,12 @@ fun SimpleActivity.addNoMedia(path: String) { } else { file.createNewFile() } - scanFile(file) {} + scanFile(file) { + callback.invoke() + } } -fun SimpleActivity.removeNoMedia(path: String) { +fun SimpleActivity.removeNoMedia(path: String, callback: () -> Unit) { val file = File(path, NOMEDIA) if (!file.exists()) return @@ -173,6 +175,8 @@ fun SimpleActivity.removeNoMedia(path: String) { } } } - scanFile(file) {} + } + scanFile(file) { + callback.invoke() } } 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 843c9599c..071537b15 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -6,11 +6,13 @@ import android.database.Cursor import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.humanizePath +import com.simplemobiletools.commons.extensions.isImageVideoGif 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.* @@ -69,10 +71,15 @@ fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList() - parents.mapNotNullTo(notNull, { it }) - filterDirectories(notNull) - return notNull + val filtered = ArrayList() + parents.mapNotNullTo(filtered, { it }) + + if (config.showHiddenFolders) { + filtered.addAll(getNoMediaFolders()) + } else { + removeNoMediaFolders(filtered) + } + return filtered } fun Context.getWhereCondition(isPickImage: Boolean, isPickVideo: Boolean): String { @@ -95,18 +102,12 @@ fun Context.getArgs(isPickImage: Boolean, isPickVideo: Boolean): Array { } } -fun Context.filterDirectories(dirs: ArrayList) { - if (!config.showHiddenFolders) { - removeNoMediaFolders(dirs) - } -} - private fun removeNoMediaFolders(paths: MutableList) { val ignorePaths = ArrayList() for (path in paths) { val dir = File(path) if (dir.exists() && dir.isDirectory) { - val res = dir.list { file, filename -> filename == ".nomedia" } + val res = dir.list { file, filename -> filename == NOMEDIA } if (res?.isNotEmpty() == true) ignorePaths.add(path) } @@ -115,4 +116,41 @@ private fun removeNoMediaFolders(paths: MutableList) { paths.removeAll(ignorePaths) } +fun Context.getNoMediaFolders(): ArrayList { + val folders = ArrayList() + val noMediaCondition = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ${MediaStore.Files.FileColumns.MEDIA_TYPE_NONE}" + + val uri = MediaStore.Files.getContentUri("external") + val columns = arrayOf(MediaStore.Files.FileColumns.DATA) + val where = "$noMediaCondition AND ${MediaStore.Files.FileColumns.TITLE} LIKE ?" + val args = arrayOf("%$NOMEDIA%") + var cursor: Cursor? = null + + try { + cursor = contentResolver.query(uri, columns, where, args, null) + if (cursor?.moveToFirst() == true) { + do { + val path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)) ?: continue + val parent = File(path).parentFile + if (hasImageVideoGif(parent)) { + folders.add(parent.absolutePath) + } + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + + return folders +} + +fun hasImageVideoGif(dir: File): Boolean { + if (dir.isDirectory) { + dir.listFiles() + .filter(File::isImageVideoGif) + .forEach { return true } + } + return false +} + val Context.config: Config get() = Config.newInstance(this)