From 6f25913558683480d0381c21fd28576280e19c82 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 7 Jun 2020 17:31:36 +0200 Subject: [PATCH] scan the internal storage from time to time for new folders --- .../gallery/pro/activities/MainActivity.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 55bb5e20b..5b5e24d7e 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 @@ -1078,6 +1078,11 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { }.mapTo(everShownFolders) { it.path } try { + // scan the internal storage from time to time for new folders + if (config.appRunCount == 1 || config.appRunCount % 30 == 0) { + everShownFolders.addAll(getFoldersWithMedia(config.internalStoragePath)) + } + // catch some extreme exceptions like too many everShownFolders for storing, shouldnt really happen config.everShownFolders = everShownFolders } catch (e: Exception) { @@ -1327,6 +1332,27 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } } + private fun getFoldersWithMedia(path: String): HashSet { + val folders = HashSet() + try { + val files = File(path).listFiles() + if (files != null) { + files.sortBy { !it.isDirectory } + for (file in files) { + if (file.isDirectory && !file.startsWith("${config.internalStoragePath}/Android")) { + folders.addAll(getFoldersWithMedia(file.absolutePath)) + } else if (file.isFile && file.isMediaFile()) { + folders.add(file.parent ?: "") + break + } + } + } + } catch (e: Exception) { + } + + return folders + } + override fun refreshItems() { getDirectories() }