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,35 +304,39 @@ 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 {
val folders = ArrayList<String>() callback(getNoMediaFoldersSync())
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)
} }
} }
fun Context.getNoMediaFoldersSync(): ArrayList<String> {
val folders = ArrayList<String>()
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) { fun Context.rescanFolderMedia(path: String) {
ensureBackgroundThread { ensureBackgroundThread {
rescanFolderMediaSync(path) rescanFolderMediaSync(path)
@ -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>