From 8d5a47ce6c2de9f6815b7bd1fcfd94ab59a35f62 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 10 Feb 2020 11:10:46 +0100 Subject: [PATCH] get proper file last modified value only when actually needed --- .../gallery/pro/activities/MainActivity.kt | 5 +++-- .../pro/asynctasks/GetMediaAsynctask.kt | 18 +++++++++++++---- .../gallery/pro/extensions/Context.kt | 3 ++- .../gallery/pro/helpers/MediaFetcher.kt | 20 +++++++++++++------ 4 files changed, 33 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 a2c193d49..65d981fc3 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 @@ -893,6 +893,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val tempFolderPath = config.tempFolderPath val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0 val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 + val getProperLastModified = config.directorySorting and SORT_BY_DATE_MODIFIED != 0 val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 val favoritePaths = getFavoritePaths() val dirPathsToRemove = ArrayList() @@ -903,7 +904,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { return } - val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false) + val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) val newDir = if (curMedia.isEmpty()) { if (directory.path != tempFolderPath) { dirPathsToRemove.add(directory.path) @@ -976,7 +977,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { return } - val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false) + val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) 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 365eb673d..955d55df8 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 @@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.pro.asynctasks import android.content.Context 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.gallery.pro.extensions.config @@ -18,22 +19,31 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage override fun doInBackground(vararg params: Void): ArrayList { val pathToUse = if (showAll) SHOW_ALL else mPath - val getProperDateTaken = context.config.getFileSorting(pathToUse) and SORT_BY_DATE_TAKEN != 0 || context.config.getFolderGrouping(pathToUse) and GROUP_BY_DATE_TAKEN_DAILY != 0 - val getProperFileSize = context.config.getFileSorting(pathToUse) and SORT_BY_SIZE != 0 + val folderGrouping = context.config.getFolderGrouping(pathToUse) + val fileSorting = context.config.getFileSorting(pathToUse) + val getProperDateTaken = fileSorting and SORT_BY_DATE_TAKEN != 0 || + folderGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 || + folderGrouping and GROUP_BY_DATE_TAKEN_MONTHLY != 0 + + val getProperLastModified = fileSorting and SORT_BY_DATE_MODIFIED != 0 || + folderGrouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || + folderGrouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 + + val getProperFileSize = fileSorting and SORT_BY_SIZE != 0 val favoritePaths = context.getFavoritePaths() val getVideoDurations = context.config.showThumbnailVideoDuration 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, getProperFileSize, favoritePaths, getVideoDurations, false) + val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations, false) media.addAll(newMedia) } mediaFetcher.sortMedia(media, context.config.getFileSorting(SHOW_ALL)) media } else { - mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperFileSize, favoritePaths, getVideoDurations) + mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations) } 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 a3d542674..363b9cb19 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 @@ -888,9 +888,10 @@ fun Context.updateDirectoryPath(path: String) { val includedFolders = config.includedFolders val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0 val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 + val getProperLastModified = config.directorySorting and SORT_BY_DATE_MODIFIED != 0 val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 val favoritePaths = getFavoritePaths() - val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false) + val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending, 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 8f060f922..37b8207fc 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 @@ -20,8 +20,8 @@ import java.util.* class MediaFetcher(val context: Context) { var shouldStop = false - fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperFileSize: Boolean, - favoritePaths: ArrayList, getVideoDurations: Boolean, sortMedia: Boolean = true): ArrayList { + fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, + getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean, sortMedia: Boolean = true): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { return ArrayList() @@ -34,7 +34,7 @@ class MediaFetcher(val context: Context) { curMedia.addAll(newMedia) } } else { - val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperFileSize, favoritePaths, getVideoDurations) + val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations) curMedia.addAll(newMedia) } @@ -212,7 +212,7 @@ class MediaFetcher(val context: Context) { } private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean, - getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean): ArrayList { + getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean): ArrayList { val media = ArrayList() val isRecycleBin = folder == RECYCLE_BIN val deletedMedia = if (isRecycleBin) { @@ -297,12 +297,20 @@ class MediaFetcher(val context: Context) { media.add(this) } } else { - val lastModified = file.lastModified() + val lastModified = if (getProperLastModified) file.lastModified() else 0L var dateTaken = lastModified val videoDuration = if (getVideoDurations && isVideo) path.getVideoDuration() else 0 if (getProperDateTaken) { - dateTaken = dateTakens.remove(path) ?: lastModified + var newDateTaken = dateTakens.remove(path) + if (newDateTaken == null) { + newDateTaken = if (getProperLastModified) { + lastModified + } else { + file.lastModified() + } + } + dateTaken = newDateTaken } val type = when {