optimize .nomedia checks, avoid doing duplicate work

This commit is contained in:
tibbi 2021-01-18 18:53:51 +01:00
parent a2decb1cd0
commit 83b0d7347f
5 changed files with 28 additions and 17 deletions

View file

@ -77,7 +77,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.33.0'
implementation 'com.simplemobiletools:commons:5.33.6'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
implementation 'it.sephiroth.android.exif:library:1.0.1'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'

View file

@ -156,8 +156,8 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
}
private fun checkHideBtnVisibility(menu: Menu, selectedPaths: ArrayList<String>) {
menu.findItem(R.id.cab_hide).isVisible = selectedPaths.any { !it.doesThisOrParentHaveNoMedia() }
menu.findItem(R.id.cab_unhide).isVisible = selectedPaths.any { it.doesThisOrParentHaveNoMedia() }
menu.findItem(R.id.cab_hide).isVisible = selectedPaths.any { !it.doesThisOrParentHaveNoMedia(HashMap(), null) }
menu.findItem(R.id.cab_unhide).isVisible = selectedPaths.any { it.doesThisOrParentHaveNoMedia(HashMap(), null) }
}
private fun checkPinBtnVisibility(menu: Menu, selectedPaths: ArrayList<String>) {
@ -327,7 +327,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
val affectedPositions = ArrayList<Int>()
val includedFolders = config.includedFolders
val newDirs = dirs.filterIndexed { index, directory ->
val removeDir = directory.path.doesThisOrParentHaveNoMedia() && !includedFolders.contains(directory.path)
val removeDir = directory.path.doesThisOrParentHaveNoMedia(HashMap(), null) && !includedFolders.contains(directory.path)
if (removeDir) {
affectedPositions.add(index)
}

View file

@ -380,7 +380,12 @@ fun Context.storeDirectoryItems(items: ArrayList<Directory>) {
fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet<String>, noMediaFolders: ArrayList<String>): String {
val dirName = getFolderNameFromPath(path)
return if (path.doesThisOrParentHaveNoMedia(noMediaFolders) && !path.isThisOrParentIncluded(includedFolders)) {
val folderNoMediaStatuses = java.util.HashMap<String, Boolean>()
noMediaFolders.forEach { folder ->
folderNoMediaStatuses["$folder/$NOMEDIA"] = true
}
return if (path.doesThisOrParentHaveNoMedia(folderNoMediaStatuses, null) && !path.isThisOrParentIncluded(includedFolders)) {
"$dirName $hidden"
} else {
dirName
@ -556,11 +561,11 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
val folderNoMediaStatuses = HashMap<String, Boolean>()
val noMediaFolders = getNoMediaFoldersSync()
noMediaFolders.forEach { folder ->
folderNoMediaStatuses["$folder/.nomedia"] = true
folderNoMediaStatuses["$folder/$NOMEDIA"] = true
}
var filteredDirectories = directories.filter {
it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses, noMediaFolders) { path, hasNoMedia ->
it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses) { path, hasNoMedia ->
folderNoMediaStatuses[path] = hasNoMedia
}
} as ArrayList<Directory>
@ -582,11 +587,14 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
if (shouldShowHidden) {
val hiddenString = resources.getString(R.string.hidden)
filteredDirectories.forEach {
val noMediaPath = "${it.path}/.nomedia"
val noMediaPath = "${it.path}/$NOMEDIA"
val hasNoMedia = if (folderNoMediaStatuses.keys.contains(noMediaPath)) {
folderNoMediaStatuses[noMediaPath]!!
} else {
it.path.doesThisOrParentHaveNoMedia(noMediaFolders)
it.path.doesThisOrParentHaveNoMedia(folderNoMediaStatuses) { path, hasNoMedia ->
val newPath = "$path/$NOMEDIA"
folderNoMediaStatuses[newPath] = hasNoMedia
}
}
it.name = if (hasNoMedia && !it.path.isThisOrParentIncluded(includedPaths)) {

View file

@ -11,8 +11,7 @@ fun String.isThisOrParentExcluded(excludedPaths: MutableSet<String>) = excludedP
// 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>, noMediaFolders: ArrayList<String> = ArrayList(),
callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean {
folderNoMediaStatuses: HashMap<String, Boolean>, callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean {
if (isEmpty()) {
return false
}
@ -37,7 +36,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
val containsNoMedia = if (showHidden) {
false
} else {
noMediaFolders.contains(this) || File(this, NOMEDIA).exists()
folderNoMediaStatuses.getOrElse("$this/$NOMEDIA", { false }) || File(this, NOMEDIA).exists()
}
return if (!showHidden && containsNoMedia) {
@ -54,14 +53,14 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
var curPath = this
for (i in 0 until count { it == '/' } - 1) {
curPath = curPath.substringBeforeLast('/')
val pathToCheck = "$curPath/${NOMEDIA}"
val pathToCheck = "$curPath/$NOMEDIA"
if (folderNoMediaStatuses.contains(pathToCheck)) {
if (folderNoMediaStatuses[pathToCheck] == true) {
containsNoMediaOrDot = true
break
}
} else {
val noMediaExists = noMediaFolders.contains(pathToCheck) || File(pathToCheck).exists()
val noMediaExists = folderNoMediaStatuses.getOrElse(pathToCheck, { false }) || File(pathToCheck).exists()
callback(pathToCheck, noMediaExists)
if (noMediaExists) {
containsNoMediaOrDot = true

View file

@ -69,7 +69,7 @@ class MediaFetcher(val context: Context) {
val excludedPaths = config.excludedFolders
val includedPaths = config.includedFolders
val folderNomediaStatuses = HashMap<String, Boolean>()
val folderNoMediaStatuses = HashMap<String, Boolean>()
val distinctPathsMap = HashMap<String, String>()
val distinctPaths = folders.distinctBy {
when {
@ -83,9 +83,13 @@ class MediaFetcher(val context: Context) {
}
val noMediaFolders = context.getNoMediaFoldersSync()
noMediaFolders.forEach { folder ->
folderNoMediaStatuses["$folder/$NOMEDIA"] = true
}
distinctPaths.filter {
it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses, noMediaFolders) { path, hasNoMedia ->
folderNomediaStatuses[path] = hasNoMedia
it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses) { path, hasNoMedia ->
folderNoMediaStatuses[path] = hasNoMedia
}
}.toMutableList() as ArrayList<String>
} catch (e: Exception) {