From 8399b4fccdd035e1c1c716c5fbedecc4f86bd234 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 23 Apr 2018 16:50:17 +0200 Subject: [PATCH] refactoring MediaFetcher once again --- .../gallery/helpers/MediaFetcher.kt | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) 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 2b093ec75..82109e20c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -30,32 +30,35 @@ class MediaFetcher(val context: Context) { return ArrayList() } + val curMedia = ArrayList() if (curPath.startsWith(OTG_PATH)) { - val curMedia = ArrayList() getMediaOnOTG(curPath, curMedia, isPickImage, isPickVideo, filterMedia) - return curMedia } else { - val projection = arrayOf(MediaStore.Images.Media.DATA) - val uri = MediaStore.Files.getContentUri("external") - - val selection = "${getSelectionQuery(curPath, filterMedia)} ${MediaStore.Images.ImageColumns.BUCKET_ID} IS NOT NULL) GROUP BY (${MediaStore.Images.ImageColumns.BUCKET_ID}" - val selectionArgs = getSelectionArgsQuery(curPath, filterMedia).toTypedArray() - - return try { - val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) - val curMedia = ArrayList() - val foldersToScan = getFoldersToScan(context, cursor, curPath) - foldersToScan.forEach { - fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia) - } - - Medium.sorting = context.config.getFileSorting(curPath) - curMedia.sort() - curMedia - } catch (e: Exception) { - ArrayList() + val foldersToScan = getFoldersToScan(curPath) + foldersToScan.forEach { + fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia) } } + + Medium.sorting = context.config.getFileSorting(curPath) + curMedia.sort() + return curMedia + } + + private fun getFoldersToScan(path: String): ArrayList { + val filterMedia = context.config.filterMedia + val projection = arrayOf(MediaStore.Images.Media.DATA) + val uri = MediaStore.Files.getContentUri("external") + + val selection = "${getSelectionQuery(path, filterMedia)} ${MediaStore.Images.ImageColumns.BUCKET_ID} IS NOT NULL) GROUP BY (${MediaStore.Images.ImageColumns.BUCKET_ID}" + val selectionArgs = getSelectionArgsQuery(path, filterMedia).toTypedArray() + + return try { + val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) + parseCursor(cursor, path) + } catch (e: Exception) { + ArrayList() + } } private fun getSelectionQuery(path: String, filterMedia: Int): String { @@ -112,7 +115,7 @@ class MediaFetcher(val context: Context) { return args } - private fun getFoldersToScan(context: Context, cursor: Cursor, curPath: String): ArrayList { + private fun parseCursor(cursor: Cursor, curPath: String): ArrayList { val config = context.config val includedFolders = config.includedFolders var foldersToScan = ArrayList()