From 8f58de5126701d9a2d8f8ecbdff799b5f64d182d Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 23 Sep 2020 23:17:44 +0200 Subject: [PATCH] cache which folder has nomedia file, avoid checking it over and over --- .../gallery/pro/extensions/Context.kt | 10 +++++++- .../gallery/pro/extensions/String.kt | 25 ++++++++++++++++--- .../gallery/pro/helpers/MediaFetcher.kt | 9 +++++-- 3 files changed, 38 insertions(+), 6 deletions(-) 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 bffcdcfc6..6d508b268 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 @@ -36,6 +36,8 @@ import java.util.HashSet import java.util.LinkedHashSet import kotlin.Comparator import kotlin.collections.ArrayList +import kotlin.collections.HashMap +import kotlin.collections.set 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 excludedPaths = config.excludedFolders val includedPaths = config.includedFolders - var filteredDirectories = directories.filter { it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) } as ArrayList + + val folderNomediaStatuses = HashMap() + var filteredDirectories = directories.filter { + it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia -> + folderNomediaStatuses[path] = hasNoMedia + } + } as ArrayList val filterMedia = config.filterMedia filteredDirectories = (when { 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 cf5260e4a..1ac57208f 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 @@ -2,7 +2,7 @@ package com.simplemobiletools.gallery.pro.extensions import android.os.Environment 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.IOException @@ -10,7 +10,9 @@ fun String.isThisOrParentIncluded(includedPaths: MutableSet) = includedP fun String.isThisOrParentExcluded(excludedPaths: MutableSet) = excludedPaths.any { equals(it, true) } || excludedPaths.any { "$this/".startsWith("$it/", true) } -fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPaths: MutableSet, showHidden: Boolean): Boolean { +// 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 { if (isEmpty()) { return false } @@ -48,7 +50,24 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPath } else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) { var containsNoMediaOrDot = containsNoMedia || contains("/.") 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 } else { 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 80ebe8ed6..0a30ae525 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 @@ -66,8 +66,13 @@ class MediaFetcher(val context: Context) { val shouldShowHidden = config.shouldShowHidden val excludedPaths = config.excludedFolders val includedPaths = config.includedFolders - folders.distinctBy { it.getDistinctPath() } - .filter { it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) }.toMutableList() as ArrayList + + val folderNomediaStatuses = HashMap() + folders.distinctBy { it.getDistinctPath() }.filter { + it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses) { path, hasNoMedia -> + folderNomediaStatuses[path] = hasNoMedia + } + }.toMutableList() as ArrayList } catch (e: Exception) { ArrayList() }