improve the way spam folders are excluded

This commit is contained in:
tibbi 2018-12-02 13:58:57 +01:00
parent b5dc1c7d27
commit 8d92fcd07b
1 changed files with 24 additions and 53 deletions

View File

@ -1105,32 +1105,37 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
private fun excludeSpamFolders() { private fun excludeSpamFolders() {
Thread { Thread {
try { try {
val internalPath = config.internalStoragePath val internalPath = internalStoragePath
val sdPath = config.sdCardPath val checkedPaths = ArrayList<String>()
val pathParts = ArrayList<ArrayList<String>>() val oftenRepeatedPaths = ArrayList<String>()
mDirs.asSequence().map { it.path.removePrefix(internalPath).removePrefix(sdPath) }.sorted().toList().forEach { val paths = mDirs.map { it.path.removePrefix(internalPath) }.toMutableList() as ArrayList<String>
val parts = it.split("/").asSequence().filter { it.isNotEmpty() }.toMutableList() as ArrayList<String> paths.forEach {
if (parts.size > 3) { val parts = it.split("/")
pathParts.add(parts) 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) val substringToRemove = oftenRepeatedPaths.filter {
pathParts.clear() val path = it
keys.forEach { it -> it == "/" || oftenRepeatedPaths.any { it != path && it.startsWith(path) }
val parts = it.split("/").asSequence().filter { it.isNotEmpty() }.toMutableList() as ArrayList<String>
pathParts.add(parts)
} }
getLongestCommonStrings(pathParts).forEach { oftenRepeatedPaths.removeAll(substringToRemove)
var file = File("$internalPath/$it") oftenRepeatedPaths.forEach {
val file = File("$internalPath/$it")
if (file.exists()) { if (file.exists()) {
config.addExcludedFolder(file.absolutePath) config.addExcludedFolder(file.absolutePath)
} else {
file = File("$sdPath/$it")
if (file.exists()) {
config.addExcludedFolder(file.absolutePath)
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -1138,40 +1143,6 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
}.start() }.start()
} }
private fun getLongestCommonStrings(pathParts: ArrayList<ArrayList<String>>): ArrayList<String> {
val commonStrings = ArrayList<String>()
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<String>
} catch (e: Exception) {
ArrayList()
}
}
override fun refreshItems() { override fun refreshItems() {
getDirectories() getDirectories()
} }