From 8d92fcd07b0faa4d4ce8d729f046197b1aa7af7c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 2 Dec 2018 13:58:57 +0100 Subject: [PATCH] improve the way spam folders are excluded --- .../gallery/pro/activities/MainActivity.kt | 77 ++++++------------- 1 file changed, 24 insertions(+), 53 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index 18f9b70d4..b508f9220 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -1105,32 +1105,37 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private fun excludeSpamFolders() { Thread { try { - val internalPath = config.internalStoragePath - val sdPath = config.sdCardPath - val pathParts = ArrayList>() - mDirs.asSequence().map { it.path.removePrefix(internalPath).removePrefix(sdPath) }.sorted().toList().forEach { - val parts = it.split("/").asSequence().filter { it.isNotEmpty() }.toMutableList() as ArrayList - if (parts.size > 3) { - pathParts.add(parts) + val internalPath = internalStoragePath + val checkedPaths = ArrayList() + val oftenRepeatedPaths = ArrayList() + val paths = mDirs.map { it.path.removePrefix(internalPath) }.toMutableList() as ArrayList + paths.forEach { + val parts = it.split("/") + var currentString = "" + for (i in 0 until parts.size) { + currentString += "${parts[i]}/" + + if (!checkedPaths.contains(currentString)) { + val cnt = paths.count { it.startsWith(currentString) } + if (cnt > 50) { + oftenRepeatedPaths.add(currentString) + } + } + + checkedPaths.add(currentString) } } - val keys = getLongestCommonStrings(pathParts) - pathParts.clear() - keys.forEach { it -> - val parts = it.split("/").asSequence().filter { it.isNotEmpty() }.toMutableList() as ArrayList - pathParts.add(parts) + val substringToRemove = oftenRepeatedPaths.filter { + val path = it + it == "/" || oftenRepeatedPaths.any { it != path && it.startsWith(path) } } - getLongestCommonStrings(pathParts).forEach { - var file = File("$internalPath/$it") + oftenRepeatedPaths.removeAll(substringToRemove) + oftenRepeatedPaths.forEach { + val file = File("$internalPath/$it") if (file.exists()) { config.addExcludedFolder(file.absolutePath) - } else { - file = File("$sdPath/$it") - if (file.exists()) { - config.addExcludedFolder(file.absolutePath) - } } } } catch (e: Exception) { @@ -1138,40 +1143,6 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { }.start() } - private fun getLongestCommonStrings(pathParts: ArrayList>): ArrayList { - val commonStrings = ArrayList() - return try { - val cnt = pathParts.size - for (i in 0..cnt) { - var longestCommonString = "" - for (j in i..cnt) { - var currentCommonString = "" - if (i != j) { - val originalParts = pathParts.getOrNull(i) - val otherParts = pathParts.getOrNull(j) - if (originalParts != null && otherParts != null) { - originalParts.forEachIndexed { index, string -> - if (string == otherParts.getOrNull(index)) { - currentCommonString += "$string/" - if (currentCommonString.length > longestCommonString.length) { - longestCommonString = currentCommonString.trimEnd('/') - } - } - } - } - } - } - - if (longestCommonString.isNotEmpty()) { - commonStrings.add(longestCommonString) - } - } - commonStrings.groupingBy { it }.eachCount().filter { it.value > 5 && it.key.length > 10 }.map { it.key }.toMutableList() as ArrayList - } catch (e: Exception) { - ArrayList() - } - } - override fun refreshItems() { getDirectories() }