cache which folder has nomedia file, avoid checking it over and over

This commit is contained in:
tibbi 2020-09-23 23:17:44 +02:00
parent 7c31fcb818
commit 8f58de5126
3 changed files with 38 additions and 6 deletions

View file

@ -36,6 +36,8 @@ import java.util.HashSet
import java.util.LinkedHashSet import java.util.LinkedHashSet
import kotlin.Comparator import kotlin.Comparator
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.set
val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager
@ -516,7 +518,13 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
val shouldShowHidden = config.shouldShowHidden || forceShowHidden val shouldShowHidden = config.shouldShowHidden || forceShowHidden
val excludedPaths = config.excludedFolders val excludedPaths = config.excludedFolders
val includedPaths = config.includedFolders val includedPaths = config.includedFolders
var filteredDirectories = directories.filter { it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) } as ArrayList<Directory>
val folderNomediaStatuses = HashMap<String, Boolean>()
var filteredDirectories = directories.filter {
it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia ->
folderNomediaStatuses[path] = hasNoMedia
}
} as ArrayList<Directory>
val filterMedia = config.filterMedia val filterMedia = config.filterMedia
filteredDirectories = (when { filteredDirectories = (when {

View file

@ -2,7 +2,7 @@ package com.simplemobiletools.gallery.pro.extensions
import android.os.Environment import android.os.Environment
import com.simplemobiletools.commons.extensions.containsNoMedia import com.simplemobiletools.commons.extensions.containsNoMedia
import com.simplemobiletools.commons.extensions.doesParentHaveNoMedia import com.simplemobiletools.commons.helpers.NOMEDIA
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@ -10,7 +10,9 @@ fun String.isThisOrParentIncluded(includedPaths: MutableSet<String>) = includedP
fun String.isThisOrParentExcluded(excludedPaths: MutableSet<String>) = excludedPaths.any { equals(it, true) } || excludedPaths.any { "$this/".startsWith("$it/", true) } fun String.isThisOrParentExcluded(excludedPaths: MutableSet<String>) = excludedPaths.any { equals(it, true) } || excludedPaths.any { "$this/".startsWith("$it/", true) }
fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean): Boolean { // 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,
folderNomediaStatuses: HashMap<String, Boolean>, callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean {
if (isEmpty()) { if (isEmpty()) {
return false return false
} }
@ -48,7 +50,24 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
} else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) { } else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) {
var containsNoMediaOrDot = containsNoMedia || contains("/.") var containsNoMediaOrDot = containsNoMedia || contains("/.")
if (!containsNoMediaOrDot) { if (!containsNoMediaOrDot) {
containsNoMediaOrDot = file.doesParentHaveNoMedia() var curPath = this
for (i in 0 until count { it == '/' } - 1) {
curPath = curPath.substringBeforeLast('/')
val pathToCheck = "$curPath/${NOMEDIA}"
if (folderNomediaStatuses.contains(pathToCheck)) {
if (folderNomediaStatuses[pathToCheck] == true) {
containsNoMediaOrDot = true
break
}
} else {
val noMediaExists = File(pathToCheck).exists()
callback(pathToCheck, noMediaExists)
if (noMediaExists) {
containsNoMediaOrDot = true
break
}
}
}
} }
!containsNoMediaOrDot !containsNoMediaOrDot
} else { } else {

View file

@ -66,8 +66,13 @@ class MediaFetcher(val context: Context) {
val shouldShowHidden = config.shouldShowHidden val shouldShowHidden = config.shouldShowHidden
val excludedPaths = config.excludedFolders val excludedPaths = config.excludedFolders
val includedPaths = config.includedFolders val includedPaths = config.includedFolders
folders.distinctBy { it.getDistinctPath() }
.filter { it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) }.toMutableList() as ArrayList<String> val folderNomediaStatuses = HashMap<String, Boolean>()
folders.distinctBy { it.getDistinctPath() }.filter {
it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia ->
folderNomediaStatuses[path] = hasNoMedia
}
}.toMutableList() as ArrayList<String>
} catch (e: Exception) { } catch (e: Exception) {
ArrayList() ArrayList()
} }