From dae13c0261098d2cce050f9e75a0f6a2a492caef Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 4 Dec 2021 22:24:50 +0100 Subject: [PATCH] improve the performance of fetching Android 11+ files, use the MediaStore --- .../gallery/pro/activities/MainActivity.kt | 11 ++----- .../pro/asynctasks/GetMediaAsynctask.kt | 4 +-- .../gallery/pro/extensions/Context.kt | 2 +- .../gallery/pro/helpers/MediaFetcher.kt | 30 +++++++++++++++---- 4 files changed, 30 insertions(+), 17 deletions(-) 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 19f89a985..553e5db6d 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 @@ -873,12 +873,6 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent val favoritePaths = getFavoritePaths() - - /*if (isRPlus()) { - mLastMediaFetcher!!.getAndroid11FolderMedia(getImagesOnly, getVideosOnly, favoritePaths) - return - }*/ - val hiddenString = getString(R.string.hidden) val albumCovers = config.parseAlbumCovers() val includedFolders = config.includedFolders @@ -913,6 +907,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } } + val android11Files = mLastMediaFetcher?.getAndroid11FolderMedia(getImagesOnly, getVideosOnly, favoritePaths) try { for (directory in dirs) { if (mShouldStopFetching || isDestroyed || isFinishing) { @@ -933,7 +928,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val curMedia = mLastMediaFetcher!!.getFilesFrom( directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, - getProperFileSize, favoritePaths, false, lastModifieds, dateTakens + getProperFileSize, favoritePaths, false, lastModifieds, dateTakens, android11Files ) val newDir = if (curMedia.isEmpty()) { @@ -1040,7 +1035,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val newMedia = mLastMediaFetcher!!.getFilesFrom( folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, - getProperFileSize, favoritePaths, false, lastModifieds, dateTakens + getProperFileSize, favoritePaths, false, lastModifieds, dateTakens, android11Files ) if (newMedia.isEmpty()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt index 07cfd49f1..aa523e911 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt @@ -38,7 +38,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage val media = ArrayList() foldersToScan.forEach { val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, - favoritePaths, getVideoDurations, lastModifieds, dateTakens) + favoritePaths, getVideoDurations, lastModifieds, dateTakens, null) media.addAll(newMedia) } @@ -46,7 +46,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage media } else { mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, - getVideoDurations, lastModifieds, dateTakens) + getVideoDurations, lastModifieds, dateTakens, null) } return mediaFetcher.groupMedia(media, pathToUse) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 9f24504b3..c51ccaeeb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -1071,7 +1071,7 @@ fun Context.updateDirectoryPath(path: String) { val favoritePaths = getFavoritePaths() val curMedia = mediaFetcher.getFilesFrom( path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, - favoritePaths, false, lastModifieds, dateTakens + favoritePaths, false, lastModifieds, dateTakens, null ) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, getProperFileSize, noMediaFolders) updateDBDirectory(directory) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 7491d6e85..0022171b0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -25,10 +25,11 @@ import java.util.* class MediaFetcher(val context: Context) { var shouldStop = false + // on Android 11 we fetch all files at once from MediaStore and have it split by folder, use it if available fun getFilesFrom( curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean, - lastModifieds: HashMap, dateTakens: HashMap + lastModifieds: HashMap, dateTakens: HashMap, android11Files: HashMap>? ): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { @@ -42,11 +43,24 @@ class MediaFetcher(val context: Context) { curMedia.addAll(newMedia) } } else { - val newMedia = getMediaInFolder( - curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, - favoritePaths, getVideoDurations, lastModifieds.clone() as HashMap, dateTakens.clone() as HashMap - ) - curMedia.addAll(newMedia) + if (isRPlus() && curPath != FAVORITES && curPath != RECYCLE_BIN) { + if (android11Files?.containsKey(curPath.toLowerCase()) == true) { + curMedia.addAll(android11Files[curPath.toLowerCase()]!!) + } else { + val files = getAndroid11FolderMedia(isPickImage, isPickVideo, favoritePaths) + if (files.containsKey(curPath.toLowerCase())) { + curMedia.addAll(files[curPath.toLowerCase()]!!) + } + } + } + + if (curMedia.isEmpty()) { + val newMedia = getMediaInFolder( + curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, + favoritePaths, getVideoDurations, lastModifieds.clone() as HashMap, dateTakens.clone() as HashMap + ) + curMedia.addAll(newMedia) + } } sortMedia(curMedia, context.config.getFolderSorting(curPath)) @@ -399,6 +413,10 @@ class MediaFetcher(val context: Context) { isPickImage: Boolean, isPickVideo: Boolean, favoritePaths: ArrayList ): HashMap> { val media = HashMap>() + if (!isRPlus()) { + return media + } + val filterMedia = context.config.filterMedia val showHidden = context.config.shouldShowHidden