From 4e58f1e6c1f67b54f331583c5e0869918ca30bd9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 24 May 2018 23:29:48 +0200 Subject: [PATCH] fix #770, reimplement sorting by date taken --- .../gallery/helpers/MediaFetcher.kt | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index a1a3ceb03..31bb19c25 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -6,6 +6,7 @@ import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.OTG_PATH +import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.commons.helpers.photoExtensions import com.simplemobiletools.commons.helpers.videoExtensions import com.simplemobiletools.gallery.extensions.config @@ -168,6 +169,8 @@ class MediaFetcher(val context: Context) { val files = File(folder).listFiles() ?: return media val doExtraCheck = context.config.doExtraCheck val showHidden = context.config.shouldShowHidden + val sorting = context.config.getFileSorting(folder) + val dateTakens = if (sorting and SORT_BY_DATE_TAKEN != 0) getFolderDateTakens(folder) else HashMap() for (file in files) { if (shouldStop) { @@ -198,8 +201,12 @@ class MediaFetcher(val context: Context) { if (size <= 0L || (doExtraCheck && !file.exists())) continue - val dateTaken = file.lastModified() - val dateModified = file.lastModified() + val lastModified = file.lastModified() + var dateTaken = lastModified + + if (sorting and SORT_BY_DATE_TAKEN != 0) { + dateTaken = dateTakens.remove(filename) ?: lastModified + } val type = when { isImage -> TYPE_IMAGES @@ -207,7 +214,7 @@ class MediaFetcher(val context: Context) { else -> TYPE_GIFS } - val medium = Medium(null, filename, file.absolutePath, folder, dateModified, dateTaken, size, type) + val medium = Medium(null, filename, file.absolutePath, folder, lastModified, dateTaken, size, type) media.add(medium) } return media @@ -264,4 +271,32 @@ class MediaFetcher(val context: Context) { return media } + + private fun getFolderDateTakens(folder: String): HashMap { + val projection = arrayOf( + MediaStore.Images.Media.DISPLAY_NAME, + MediaStore.Images.Media.DATE_TAKEN + ) + + val uri = MediaStore.Files.getContentUri("external") + val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?" + val selectionArgs = arrayOf("$folder/%", "$folder/%/%") + + val dateTakens = HashMap() + val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) + cursor.use { + if (cursor.moveToFirst()) { + do { + try { + val path = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) + val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) + dateTakens[path] = dateTaken + } catch (e: Exception) { + } + } while (cursor.moveToNext()) + } + } + + return dateTakens + } }