diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index ba073f07c..10ecf9b34 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -304,35 +304,39 @@ fun Context.updateSubfolderCounts(children: ArrayList, parentDirs: Ar fun Context.getNoMediaFolders(callback: (folders: ArrayList) -> Unit) { ensureBackgroundThread { - val folders = ArrayList() - - val uri = Files.getContentUri("external") - val projection = arrayOf(Files.FileColumns.DATA) - val selection = "${Files.FileColumns.MEDIA_TYPE} = ? AND ${Files.FileColumns.TITLE} LIKE ?" - val selectionArgs = arrayOf(Files.FileColumns.MEDIA_TYPE_NONE.toString(), "%$NOMEDIA%") - val sortOrder = "${Files.FileColumns.DATE_MODIFIED} DESC" - val OTGPath = config.OTGPath - - var cursor: Cursor? = null - try { - cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) - if (cursor?.moveToFirst() == true) { - do { - val path = cursor.getStringValue(Files.FileColumns.DATA) ?: continue - val noMediaFile = File(path) - if (getDoesFilePathExist(noMediaFile.absolutePath, OTGPath) && noMediaFile.name == NOMEDIA) { - folders.add("${noMediaFile.parent}/") - } - } while (cursor.moveToNext()) - } - } finally { - cursor?.close() - } - - callback(folders) + callback(getNoMediaFoldersSync()) } } +fun Context.getNoMediaFoldersSync(): ArrayList { + val folders = ArrayList() + + val uri = Files.getContentUri("external") + val projection = arrayOf(Files.FileColumns.DATA) + val selection = "${Files.FileColumns.MEDIA_TYPE} = ? AND ${Files.FileColumns.TITLE} LIKE ?" + val selectionArgs = arrayOf(Files.FileColumns.MEDIA_TYPE_NONE.toString(), "%$NOMEDIA%") + val sortOrder = "${Files.FileColumns.DATE_MODIFIED} DESC" + val OTGPath = config.OTGPath + + var cursor: Cursor? = null + try { + cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) + if (cursor?.moveToFirst() == true) { + do { + val path = cursor.getStringValue(Files.FileColumns.DATA) ?: continue + val noMediaFile = File(path) + if (getDoesFilePathExist(noMediaFile.absolutePath, OTGPath) && noMediaFile.name == NOMEDIA) { + folders.add("${noMediaFile.parent}") + } + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + + return folders +} + fun Context.rescanFolderMedia(path: String) { ensureBackgroundThread { rescanFolderMediaSync(path) @@ -519,9 +523,10 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: val excludedPaths = config.excludedFolders val includedPaths = config.includedFolders + val noMediaFolders = getNoMediaFoldersSync() val folderNomediaStatuses = HashMap() var filteredDirectories = directories.filter { - it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia -> + it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses, noMediaFolders) { path, hasNoMedia -> folderNomediaStatuses[path] = hasNoMedia } } as ArrayList diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt index e8e19edcb..87e92ec37 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt @@ -11,7 +11,8 @@ fun String.isThisOrParentExcluded(excludedPaths: MutableSet) = excludedP // cache which folders contain .nomedia files to avoid checking them over and over again fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPaths: MutableSet, showHidden: Boolean, - folderNomediaStatuses: HashMap, callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean { + folderNomediaStatuses: HashMap, noMediaFolders: ArrayList = ArrayList(), + callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean { if (isEmpty()) { return false } @@ -36,7 +37,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPath val containsNoMedia = if (showHidden) { false } else { - File(this, NOMEDIA).exists() + noMediaFolders.contains(this) || File(this, NOMEDIA).exists() } return if (!showHidden && containsNoMedia) { @@ -60,7 +61,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPath break } } else { - val noMediaExists = File(pathToCheck).exists() + val noMediaExists = noMediaFolders.contains(pathToCheck) || File(pathToCheck).exists() callback(pathToCheck, noMediaExists) if (noMediaExists) { containsNoMediaOrDot = true diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index ef2a6c52a..bd229721d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -82,8 +82,9 @@ class MediaFetcher(val context: Context) { } } + val noMediaFolders = context.getNoMediaFoldersSync() distinctPaths.filter { - it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia -> + it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses, noMediaFolders) { path, hasNoMedia -> folderNomediaStatuses[path] = hasNoMedia } }.toMutableList() as ArrayList