fetch nomedia files from MediaStore, use them for quick check

This commit is contained in:
tibbi 2020-10-28 20:34:30 +01:00
parent a4c590ddd3
commit eb9ca5df77
3 changed files with 38 additions and 31 deletions

View file

@ -304,6 +304,11 @@ fun Context.updateSubfolderCounts(children: ArrayList<Directory>, parentDirs: Ar
fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) { fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
ensureBackgroundThread { ensureBackgroundThread {
callback(getNoMediaFoldersSync())
}
}
fun Context.getNoMediaFoldersSync(): ArrayList<String> {
val folders = ArrayList<String>() val folders = ArrayList<String>()
val uri = Files.getContentUri("external") val uri = Files.getContentUri("external")
@ -321,7 +326,7 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
val path = cursor.getStringValue(Files.FileColumns.DATA) ?: continue val path = cursor.getStringValue(Files.FileColumns.DATA) ?: continue
val noMediaFile = File(path) val noMediaFile = File(path)
if (getDoesFilePathExist(noMediaFile.absolutePath, OTGPath) && noMediaFile.name == NOMEDIA) { if (getDoesFilePathExist(noMediaFile.absolutePath, OTGPath) && noMediaFile.name == NOMEDIA) {
folders.add("${noMediaFile.parent}/") folders.add("${noMediaFile.parent}")
} }
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
@ -329,8 +334,7 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
cursor?.close() cursor?.close()
} }
callback(folders) return folders
}
} }
fun Context.rescanFolderMedia(path: String) { fun Context.rescanFolderMedia(path: String) {
@ -519,9 +523,10 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
val excludedPaths = config.excludedFolders val excludedPaths = config.excludedFolders
val includedPaths = config.includedFolders val includedPaths = config.includedFolders
val noMediaFolders = getNoMediaFoldersSync()
val folderNomediaStatuses = HashMap<String, Boolean>() val folderNomediaStatuses = HashMap<String, Boolean>()
var filteredDirectories = directories.filter { 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 folderNomediaStatuses[path] = hasNoMedia
} }
} as ArrayList<Directory> } as ArrayList<Directory>

View file

@ -11,7 +11,8 @@ fun String.isThisOrParentExcluded(excludedPaths: MutableSet<String>) = excludedP
// cache which folders contain .nomedia files to avoid checking them over and over again // cache which folders contain .nomedia files to avoid checking them over and over again
fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean, fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean,
folderNomediaStatuses: HashMap<String, Boolean>, callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean { folderNomediaStatuses: HashMap<String, Boolean>, noMediaFolders: ArrayList<String> = ArrayList(),
callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean {
if (isEmpty()) { if (isEmpty()) {
return false return false
} }
@ -36,7 +37,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
val containsNoMedia = if (showHidden) { val containsNoMedia = if (showHidden) {
false false
} else { } else {
File(this, NOMEDIA).exists() noMediaFolders.contains(this) || File(this, NOMEDIA).exists()
} }
return if (!showHidden && containsNoMedia) { return if (!showHidden && containsNoMedia) {
@ -60,7 +61,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
break break
} }
} else { } else {
val noMediaExists = File(pathToCheck).exists() val noMediaExists = noMediaFolders.contains(pathToCheck) || File(pathToCheck).exists()
callback(pathToCheck, noMediaExists) callback(pathToCheck, noMediaExists)
if (noMediaExists) { if (noMediaExists) {
containsNoMediaOrDot = true containsNoMediaOrDot = true

View file

@ -82,8 +82,9 @@ class MediaFetcher(val context: Context) {
} }
} }
val noMediaFolders = context.getNoMediaFoldersSync()
distinctPaths.filter { distinctPaths.filter {
it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia -> it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses, noMediaFolders) { path, hasNoMedia ->
folderNomediaStatuses[path] = hasNoMedia folderNomediaStatuses[path] = hasNoMedia
} }
}.toMutableList() as ArrayList<String> }.toMutableList() as ArrayList<String>