cache all Date Taken values at once, not folder by folder

This commit is contained in:
tibbi 2020-09-25 14:00:49 +02:00
parent 44f5bb5c1e
commit 8d7a5b1771
4 changed files with 50 additions and 13 deletions

View file

@ -908,8 +908,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
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 dirPathsToRemove = ArrayList<String>() val dirPathsToRemove = ArrayList<String>()
val lastModifieds = if (isRPlus()) mLastMediaFetcher!!.getLastModifieds() else HashMap()
val lastModifieds = if (isRPlus()) mLastMediaFetcher!!.getLastModifieds() else HashMap<String, Long>() val dateTakens = mLastMediaFetcher!!.getDateTakens()
try { try {
for (directory in dirs) { for (directory in dirs) {
@ -930,7 +930,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
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, val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified,
getProperFileSize, favoritePaths, false, lastModifieds) getProperFileSize, favoritePaths, false, lastModifieds, dateTakens)
val newDir = if (curMedia.isEmpty()) { val newDir = if (curMedia.isEmpty()) {
if (directory.path != tempFolderPath) { if (directory.path != tempFolderPath) {
@ -1024,7 +1024,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
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, val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified,
getProperFileSize, favoritePaths, false, lastModifieds) getProperFileSize, favoritePaths, false, lastModifieds, dateTakens)
if (newMedia.isEmpty()) { if (newMedia.isEmpty()) {
continue continue

View file

@ -34,12 +34,14 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage
val favoritePaths = context.getFavoritePaths() val favoritePaths = context.getFavoritePaths()
val getVideoDurations = context.config.showThumbnailVideoDuration val getVideoDurations = context.config.showThumbnailVideoDuration
val lastModifieds = if (isRPlus() && getProperLastModified) mediaFetcher.getLastModifieds() else HashMap() val lastModifieds = if (isRPlus() && getProperLastModified) mediaFetcher.getLastModifieds() else HashMap()
val dateTakens = if (getProperLastModified) mediaFetcher.getDateTakens() else HashMap()
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, val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize,
favoritePaths, getVideoDurations, lastModifieds) favoritePaths, getVideoDurations, lastModifieds, dateTakens)
media.addAll(newMedia) media.addAll(newMedia)
} }
@ -47,7 +49,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage
media media
} else { } else {
mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths,
getVideoDurations, lastModifieds) getVideoDurations, lastModifieds, dateTakens)
} }
return mediaFetcher.groupMedia(media, pathToUse) return mediaFetcher.groupMedia(media, pathToUse)

View file

@ -869,7 +869,6 @@ 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,9 +888,12 @@ fun Context.updateDirectoryPath(path: String) {
grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0
val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 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 favoritePaths = getFavoritePaths()
val curMedia = mediaFetcher.getFilesFrom(path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, 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) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, getProperFileSize)
updateDBDirectory(directory) updateDBDirectory(directory)
} }

View file

@ -23,7 +23,7 @@ class MediaFetcher(val context: Context) {
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, getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean,
lastModifieds: HashMap<String, Long> = HashMap<String, Long>()): ArrayList<Medium> { lastModifieds: HashMap<String, Long>, dateTakens: 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()
@ -37,7 +37,7 @@ class MediaFetcher(val context: Context) {
} }
} else { } else {
val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize,
favoritePaths, getVideoDurations, lastModifieds) favoritePaths, getVideoDurations, lastModifieds, dateTakens)
curMedia.addAll(newMedia) 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, private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean,
getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getProperLastModified: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList<String>,
getVideoDurations: Boolean, lastModifieds: HashMap<String, Long>): ArrayList<Medium> { getVideoDurations: Boolean, lastModifieds: HashMap<String, Long>, dateTakens: 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) {
@ -240,7 +240,6 @@ class MediaFetcher(val context: Context) {
val checkFileExistence = config.fileLoadingPriority == PRIORITY_VALIDITY val checkFileExistence = config.fileLoadingPriority == PRIORITY_VALIDITY
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 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) {
@ -432,7 +431,7 @@ class MediaFetcher(val context: Context) {
return media return media
} }
private fun getFolderDateTakens(folder: String): HashMap<String, Long> { fun getFolderDateTakens(folder: String): HashMap<String, Long> {
val dateTakens = HashMap<String, Long>() val dateTakens = HashMap<String, Long>()
if (folder != FAVORITES) { if (folder != FAVORITES) {
val projection = arrayOf( val projection = arrayOf(
@ -474,6 +473,40 @@ class MediaFetcher(val context: Context) {
return dateTakens return dateTakens
} }
fun getDateTakens(): HashMap<String, Long> {
val dateTakens = HashMap<String, Long>()
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<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) {