From a24050c98da4f0a0dd21c533d73f809e239b6e8b Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 26 Apr 2018 13:59:24 +0200 Subject: [PATCH] make sure folder inclusion is recursive on OTG too --- .../gallery/activities/MainActivity.kt | 4 +--- .../gallery/extensions/Context.kt | 4 ++++ .../gallery/helpers/MediaFetcher.kt | 19 +++++++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 0fd59ddd8..5d27d06a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -760,7 +760,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { if (!getDoesFilePathExist(it.path)) { invalidDirs.add(it) } else { - val children = if (it.path.startsWith(OTG_PATH)) getOTGFolderChildren(it.path) else File(it.path).list()?.asList() + val children = if (it.path.startsWith(OTG_PATH)) getOTGFolderChildrenNames(it.path) else File(it.path).list()?.asList() val hasMediaFile = children?.any { it.isImageVideoGif() } ?: false if (!hasMediaFile) { invalidDirs.add(it) @@ -777,8 +777,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } } - private fun getOTGFolderChildren(path: String) = getDocumentFile(path)?.listFiles()?.map { it.name }?.toList() - private fun getCurrentlyDisplayedDirs() = getRecyclerAdapter()?.dirs ?: ArrayList() private fun getBubbleTextItem(index: Int) = getRecyclerAdapter()?.dirs?.getOrNull(index)?.getBubbleText() ?: "" diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 0f4673fa5..0cfd4d85a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -326,3 +326,7 @@ fun Context.updateDBMediaPath(oldPath: String, newPath: String) { fun Context.updateDBDirectory(directory: Directory) { galleryDB.DirectoryDao().updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) } + +fun Context.getOTGFolderChildren(path: String) = getDocumentFile(path)?.listFiles() + +fun Context.getOTGFolderChildrenNames(path: String) = getOTGFolderChildren(path)?.map { it.name }?.toList() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index 9cf121e75..41d876701 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -9,6 +9,7 @@ import com.simplemobiletools.commons.helpers.OTG_PATH import com.simplemobiletools.commons.helpers.photoExtensions import com.simplemobiletools.commons.helpers.videoExtensions import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.extensions.getOTGFolderChildren import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -143,10 +144,20 @@ class MediaFetcher(val context: Context) { private fun addFolder(curFolders: ArrayList, folder: String) { curFolders.add(folder) - val files = File(folder).listFiles() ?: return - for (file in files) { - if (file.isDirectory) { - addFolder(curFolders, file.absolutePath) + if (folder.startsWith(OTG_PATH)) { + val files = context.getOTGFolderChildren(folder) ?: return + for (file in files) { + if (file.isDirectory) { + val relativePath = file.uri.path.substringAfterLast("${context.config.OTGPartition}:") + addFolder(curFolders, "$OTG_PATH$relativePath") + } + } + } else { + val files = File(folder).listFiles() ?: return + for (file in files) { + if (file.isDirectory) { + addFolder(curFolders, file.absolutePath) + } } } }