From 8d7a5b177180bd2e15de096bb00a72c1015f0568 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 25 Sep 2020 14:00:49 +0200 Subject: [PATCH] cache all Date Taken values at once, not folder by folder --- .../gallery/pro/activities/MainActivity.kt | 8 ++-- .../pro/asynctasks/GetMediaAsynctask.kt | 6 ++- .../gallery/pro/extensions/Context.kt | 6 ++- .../gallery/pro/helpers/MediaFetcher.kt | 43 ++++++++++++++++--- 4 files changed, 50 insertions(+), 13 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 83463e5d7..3de7e5fb3 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 @@ -908,8 +908,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 val favoritePaths = getFavoritePaths() val dirPathsToRemove = ArrayList() - - val lastModifieds = if (isRPlus()) mLastMediaFetcher!!.getLastModifieds() else HashMap() + val lastModifieds = if (isRPlus()) mLastMediaFetcher!!.getLastModifieds() else HashMap() + val dateTakens = mLastMediaFetcher!!.getDateTakens() try { for (directory in dirs) { @@ -930,7 +930,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, - getProperFileSize, favoritePaths, false, lastModifieds) + getProperFileSize, favoritePaths, false, lastModifieds, dateTakens) val newDir = if (curMedia.isEmpty()) { if (directory.path != tempFolderPath) { @@ -1024,7 +1024,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, - getProperFileSize, favoritePaths, false, lastModifieds) + getProperFileSize, favoritePaths, false, lastModifieds, dateTakens) 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 16e3f251d..c15eaf30d 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 @@ -34,12 +34,14 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage val favoritePaths = context.getFavoritePaths() val getVideoDurations = context.config.showThumbnailVideoDuration val lastModifieds = if (isRPlus() && getProperLastModified) mediaFetcher.getLastModifieds() else HashMap() + val dateTakens = if (getProperLastModified) mediaFetcher.getDateTakens() 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, lastModifieds) + favoritePaths, getVideoDurations, lastModifieds, dateTakens) media.addAll(newMedia) } @@ -47,7 +49,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage media } else { mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, - getVideoDurations, lastModifieds) + getVideoDurations, lastModifieds, dateTakens) } 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 f52fe4937..ba073f07c 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,7 +869,6 @@ 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,9 +888,12 @@ fun Context.updateDirectoryPath(path: String) { grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 + + val lastModifieds = if (isRPlus() && getProperLastModified) mediaFetcher.getFolderLastModifieds(path) else HashMap() + val dateTakens = mediaFetcher.getFolderDateTakens(path) val favoritePaths = getFavoritePaths() val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, - favoritePaths, false, lastModifieds) + favoritePaths, false, lastModifieds, dateTakens) 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 ce69006ac..bfcb01f5d 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 @@ -23,7 +23,7 @@ class MediaFetcher(val context: Context) { fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean, - lastModifieds: HashMap = HashMap()): ArrayList { + lastModifieds: HashMap, dateTakens: HashMap): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { return ArrayList() @@ -37,7 +37,7 @@ class MediaFetcher(val context: Context) { } } else { val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, - favoritePaths, getVideoDurations, lastModifieds) + favoritePaths, getVideoDurations, lastModifieds, dateTakens) curMedia.addAll(newMedia) } @@ -226,7 +226,7 @@ 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, lastModifieds: HashMap): ArrayList { + getVideoDurations: Boolean, lastModifieds: HashMap, dateTakens: HashMap): ArrayList { val media = ArrayList() val isRecycleBin = folder == RECYCLE_BIN val deletedMedia = if (isRecycleBin) { @@ -240,7 +240,6 @@ class MediaFetcher(val context: Context) { val checkFileExistence = config.fileLoadingPriority == PRIORITY_VALIDITY val showHidden = config.shouldShowHidden val showPortraits = filterMedia and TYPE_PORTRAITS != 0 - val dateTakens = if (getProperDateTaken && !isRecycleBin) getFolderDateTakens(folder) else HashMap() val fileSizes = if (checkProperFileSize || checkFileExistence) getFolderSizes(folder) else HashMap() val files = when (folder) { @@ -432,7 +431,7 @@ class MediaFetcher(val context: Context) { return media } - private fun getFolderDateTakens(folder: String): HashMap { + fun getFolderDateTakens(folder: String): HashMap { val dateTakens = HashMap() if (folder != FAVORITES) { val projection = arrayOf( @@ -474,6 +473,40 @@ class MediaFetcher(val context: Context) { return dateTakens } + fun getDateTakens(): HashMap { + val dateTakens = HashMap() + val projection = arrayOf( + Images.Media.DATA, + Images.Media.DATE_TAKEN + ) + + val uri = Files.getContentUri("external") + + val cursor = context.contentResolver.query(uri, projection, null, null, null) + cursor?.use { + if (cursor.moveToFirst()) { + do { + try { + val dateTaken = cursor.getLongValue(Images.Media.DATE_TAKEN) + if (dateTaken != 0L) { + val path = cursor.getStringValue(Images.Media.DATA) + dateTakens[path] = dateTaken + } + } catch (e: Exception) { + } + } while (cursor.moveToNext()) + } + } + + val dateTakenValues = context.dateTakensDB.getAllDateTakens() + + dateTakenValues.forEach { + dateTakens[it.fullPath] = it.taken + } + + return dateTakens + } + fun getFolderLastModifieds(folder: String): HashMap { val lastModifieds = HashMap() if (folder != FAVORITES) {