mirror of
				https://github.com/SimpleMobileTools/Simple-Gallery.git
				synced 2025-06-05 21:59:19 +02:00 
			
		
		
		
	improve the way spam folders are excluded
This commit is contained in:
		| @@ -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) | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
|                 val keys = getLongestCommonStrings(pathParts) |                         checkedPaths.add(currentString) | ||||||
|                 pathParts.clear() |                     } | ||||||
|                 keys.forEach { it -> |  | ||||||
|                     val parts = it.split("/").asSequence().filter { it.isNotEmpty() }.toMutableList() as ArrayList<String> |  | ||||||
|                     pathParts.add(parts) |  | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 getLongestCommonStrings(pathParts).forEach { |                 val substringToRemove = oftenRepeatedPaths.filter { | ||||||
|                     var file = File("$internalPath/$it") |                     val path = it | ||||||
|                     if (file.exists()) { |                     it == "/" || oftenRepeatedPaths.any { it != path && it.startsWith(path) } | ||||||
|                         config.addExcludedFolder(file.absolutePath) |  | ||||||
|                     } else { |  | ||||||
|                         file = File("$sdPath/$it") |  | ||||||
|                         if (file.exists()) { |  | ||||||
|                             config.addExcludedFolder(file.absolutePath) |  | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|  |                 oftenRepeatedPaths.removeAll(substringToRemove) | ||||||
|  |                 oftenRepeatedPaths.forEach { | ||||||
|  |                     val file = File("$internalPath/$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() | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user