From f815c626fc304d604216525cd67d4ed109aa7146 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 25 Sep 2020 12:07:40 +0200 Subject: [PATCH] prefetch all Last Modified values, not folder by folder --- .../gallery/pro/activities/MainActivity.kt | 10 ++++- .../pro/asynctasks/GetMediaAsynctask.kt | 9 +++- .../gallery/pro/extensions/Context.kt | 4 +- .../gallery/pro/helpers/MediaFetcher.kt | 44 +++++++++++++------ 4 files changed, 49 insertions(+), 18 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 3378f1587..83463e5d7 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 @@ -909,6 +909,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val favoritePaths = getFavoritePaths() val dirPathsToRemove = ArrayList() + val lastModifieds = if (isRPlus()) mLastMediaFetcher!!.getLastModifieds() else HashMap() + try { for (directory in dirs) { if (mShouldStopFetching || isDestroyed || isFinishing) { @@ -927,7 +929,9 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { grouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 - val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) + val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, + getProperFileSize, favoritePaths, false, lastModifieds) + val newDir = if (curMedia.isEmpty()) { if (directory.path != tempFolderPath) { dirPathsToRemove.add(directory.path) @@ -1019,7 +1023,9 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { grouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 - val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) + val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, + getProperFileSize, favoritePaths, false, lastModifieds) + if (newMedia.isEmpty()) { continue } 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 5cbd156d6..141f5236c 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 @@ -5,6 +5,7 @@ import android.os.AsyncTask import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.commons.helpers.SORT_BY_SIZE +import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.getFavoritePaths import com.simplemobiletools.gallery.pro.helpers.* @@ -32,19 +33,23 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage val getProperFileSize = fileSorting and SORT_BY_SIZE != 0 val favoritePaths = context.getFavoritePaths() val getVideoDurations = context.config.showThumbnailVideoDuration + val lastModifieds = if (isRPlus()) mediaFetcher.getLastModifieds() else HashMap() val media = if (showAll) { val foldersToScan = mediaFetcher.getFoldersToScan().filter { it != RECYCLE_BIN && it != FAVORITES && !context.config.isFolderProtected(it) } val media = ArrayList() foldersToScan.forEach { - val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations) + val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, + favoritePaths, getVideoDurations, lastModifieds) media.addAll(newMedia) } mediaFetcher.sortMedia(media, context.config.getFolderSorting(SHOW_ALL)) media } else { - mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations) + mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, + getVideoDurations, lastModifieds) } + 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 6d508b268..f52fe4937 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 @@ -869,6 +869,7 @@ fun Context.getDirectorySortingValue(media: ArrayList, path: String, nam fun Context.updateDirectoryPath(path: String) { val mediaFetcher = MediaFetcher(applicationContext) + val lastModifieds = mediaFetcher.getFolderLastModifieds(path) val getImagesOnly = false val getVideosOnly = false val hiddenString = getString(R.string.hidden) @@ -889,7 +890,8 @@ fun Context.updateDirectoryPath(path: String) { val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 val favoritePaths = getFavoritePaths() - val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) + val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, + favoritePaths, false, lastModifieds) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, getProperFileSize) 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 de905e3f8..ce69006ac 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 @@ -22,7 +22,8 @@ class MediaFetcher(val context: Context) { var shouldStop = false fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, - getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean): ArrayList { + getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean, + lastModifieds: HashMap = HashMap()): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { return ArrayList() @@ -35,7 +36,8 @@ class MediaFetcher(val context: Context) { curMedia.addAll(newMedia) } } else { - val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations) + val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, + favoritePaths, getVideoDurations, lastModifieds) curMedia.addAll(newMedia) } @@ -223,7 +225,8 @@ class MediaFetcher(val context: Context) { } private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean, - getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean): ArrayList { + getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList, + getVideoDurations: Boolean, lastModifieds: HashMap): ArrayList { val media = ArrayList() val isRecycleBin = folder == RECYCLE_BIN val deletedMedia = if (isRecycleBin) { @@ -238,7 +241,6 @@ class MediaFetcher(val context: Context) { val showHidden = config.shouldShowHidden val showPortraits = filterMedia and TYPE_PORTRAITS != 0 val dateTakens = if (getProperDateTaken && !isRecycleBin) getFolderDateTakens(folder) else HashMap() - val lastModifieds = if (getProperLastModified && !isRecycleBin && isRPlus()) getFolderLastModifieds(folder) else HashMap() val fileSizes = if (checkProperFileSize || checkFileExistence) getFolderSizes(folder) else HashMap() val files = when (folder) { @@ -472,7 +474,7 @@ class MediaFetcher(val context: Context) { return dateTakens } - private fun getFolderLastModifieds(folder: String): HashMap { + fun getFolderLastModifieds(folder: String): HashMap { val lastModifieds = HashMap() if (folder != FAVORITES) { val projection = arrayOf( @@ -501,15 +503,31 @@ class MediaFetcher(val context: Context) { } } - val lastModifiedValues = if (folder == FAVORITES) { - context.dateTakensDB.getAllDateTakens() - } else { - context.dateTakensDB.getDateTakensFromPath(folder) - } + return lastModifieds + } - lastModifiedValues.forEach { - if (!lastModifieds.containsKey(it.fullPath)) { - lastModifieds[it.fullPath] = it.lastModified + fun getLastModifieds(): HashMap { + val lastModifieds = HashMap() + val projection = arrayOf( + Images.Media.DATA, + Images.Media.DATE_MODIFIED + ) + + val uri = Files.getContentUri("external") + + val cursor = context.contentResolver.query(uri, projection, null, null, null) + cursor?.use { + if (cursor.moveToFirst()) { + do { + try { + val lastModified = cursor.getLongValue(Images.Media.DATE_MODIFIED) * 1000 + if (lastModified != 0L) { + val path = cursor.getStringValue(Images.Media.DATA) + lastModifieds[path] = lastModified + } + } catch (e: Exception) { + } + } while (cursor.moveToNext()) } }