prefetch all Last Modified values, not folder by folder

This commit is contained in:
tibbi 2020-09-25 12:07:40 +02:00
parent ff2cbbf533
commit f815c626fc
4 changed files with 49 additions and 18 deletions

View file

@ -909,6 +909,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val favoritePaths = getFavoritePaths() val favoritePaths = getFavoritePaths()
val dirPathsToRemove = ArrayList<String>() val dirPathsToRemove = ArrayList<String>()
val lastModifieds = if (isRPlus()) mLastMediaFetcher!!.getLastModifieds() else HashMap<String, Long>()
try { try {
for (directory in dirs) { for (directory in dirs) {
if (mShouldStopFetching || isDestroyed || isFinishing) { 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_DAILY != 0 ||
grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 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()) { val newDir = if (curMedia.isEmpty()) {
if (directory.path != tempFolderPath) { if (directory.path != tempFolderPath) {
dirPathsToRemove.add(directory.path) 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_DAILY != 0 ||
grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 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()) { if (newMedia.isEmpty()) {
continue continue
} }

View file

@ -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_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE 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.config
import com.simplemobiletools.gallery.pro.extensions.getFavoritePaths import com.simplemobiletools.gallery.pro.extensions.getFavoritePaths
import com.simplemobiletools.gallery.pro.helpers.* 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 getProperFileSize = fileSorting and SORT_BY_SIZE != 0
val favoritePaths = context.getFavoritePaths() val favoritePaths = context.getFavoritePaths()
val getVideoDurations = context.config.showThumbnailVideoDuration val getVideoDurations = context.config.showThumbnailVideoDuration
val lastModifieds = if (isRPlus()) mediaFetcher.getLastModifieds() else HashMap<String, Long>()
val media = if (showAll) { val media = if (showAll) {
val foldersToScan = mediaFetcher.getFoldersToScan().filter { it != RECYCLE_BIN && it != FAVORITES && !context.config.isFolderProtected(it) } val foldersToScan = mediaFetcher.getFoldersToScan().filter { it != RECYCLE_BIN && it != FAVORITES && !context.config.isFolderProtected(it) }
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
foldersToScan.forEach { 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) media.addAll(newMedia)
} }
mediaFetcher.sortMedia(media, context.config.getFolderSorting(SHOW_ALL)) mediaFetcher.sortMedia(media, context.config.getFolderSorting(SHOW_ALL))
media media
} else { } 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) return mediaFetcher.groupMedia(media, pathToUse)
} }

View file

@ -869,6 +869,7 @@ fun Context.getDirectorySortingValue(media: ArrayList<Medium>, path: String, nam
fun Context.updateDirectoryPath(path: String) { fun Context.updateDirectoryPath(path: String) {
val mediaFetcher = MediaFetcher(applicationContext) val mediaFetcher = MediaFetcher(applicationContext)
val lastModifieds = mediaFetcher.getFolderLastModifieds(path)
val getImagesOnly = false val getImagesOnly = false
val getVideosOnly = false val getVideosOnly = false
val hiddenString = getString(R.string.hidden) 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 getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0
val favoritePaths = getFavoritePaths() 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) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, getProperFileSize)
updateDBDirectory(directory) updateDBDirectory(directory)
} }

View file

@ -22,7 +22,8 @@ class MediaFetcher(val context: Context) {
var shouldStop = false var shouldStop = false
fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean,
getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean): ArrayList<Medium> { getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean,
lastModifieds: HashMap<String, Long> = HashMap<String, Long>()): ArrayList<Medium> {
val filterMedia = context.config.filterMedia val filterMedia = context.config.filterMedia
if (filterMedia == 0) { if (filterMedia == 0) {
return ArrayList() return ArrayList()
@ -35,7 +36,8 @@ class MediaFetcher(val context: Context) {
curMedia.addAll(newMedia) curMedia.addAll(newMedia)
} }
} else { } 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) 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, private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean,
getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean): ArrayList<Medium> { getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList<String>,
getVideoDurations: Boolean, lastModifieds: HashMap<String, Long>): ArrayList<Medium> {
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
val isRecycleBin = folder == RECYCLE_BIN val isRecycleBin = folder == RECYCLE_BIN
val deletedMedia = if (isRecycleBin) { val deletedMedia = if (isRecycleBin) {
@ -238,7 +241,6 @@ class MediaFetcher(val context: Context) {
val showHidden = config.shouldShowHidden val showHidden = config.shouldShowHidden
val showPortraits = filterMedia and TYPE_PORTRAITS != 0 val showPortraits = filterMedia and TYPE_PORTRAITS != 0
val dateTakens = if (getProperDateTaken && !isRecycleBin) getFolderDateTakens(folder) else HashMap() 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 fileSizes = if (checkProperFileSize || checkFileExistence) getFolderSizes(folder) else HashMap()
val files = when (folder) { val files = when (folder) {
@ -472,7 +474,7 @@ class MediaFetcher(val context: Context) {
return dateTakens return dateTakens
} }
private fun getFolderLastModifieds(folder: String): HashMap<String, Long> { fun getFolderLastModifieds(folder: String): HashMap<String, Long> {
val lastModifieds = HashMap<String, Long>() val lastModifieds = HashMap<String, Long>()
if (folder != FAVORITES) { if (folder != FAVORITES) {
val projection = arrayOf( val projection = arrayOf(
@ -501,15 +503,31 @@ class MediaFetcher(val context: Context) {
} }
} }
val lastModifiedValues = if (folder == FAVORITES) { return lastModifieds
context.dateTakensDB.getAllDateTakens()
} else {
context.dateTakensDB.getDateTakensFromPath(folder)
} }
lastModifiedValues.forEach { fun getLastModifieds(): HashMap<String, Long> {
if (!lastModifieds.containsKey(it.fullPath)) { val lastModifieds = HashMap<String, Long>()
lastModifieds[it.fullPath] = it.lastModified 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())
} }
} }