diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index 1ae26673f..34f617e6e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -348,12 +348,13 @@ fun Activity.hasNavBar(): Boolean { return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0) } -fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, callback: (() -> Unit)? = null) { +fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasRescanned: Boolean = false, callback: (() -> Unit)? = null) { val BATCH_SIZE = 50 if (showToasts) { toast(R.string.fixing) } + val pathsToRescan = ArrayList() try { var didUpdateFile = false val operations = ArrayList() @@ -388,6 +389,10 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, callbac mediumDao.updateFavoriteDateTaken(path, timestamp) didUpdateFile = true + + if (!hasRescanned && getFileDateTaken(path) == 0L) { + pathsToRescan.add(path) + } } val resultSize = contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size @@ -395,12 +400,18 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, callbac didUpdateFile = false } - runOnUiThread { - if (showToasts) { - toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) - } + if (hasRescanned || pathsToRescan.isEmpty()) { + runOnUiThread { + if (showToasts) { + toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) + } - callback?.invoke() + callback?.invoke() + } + } else { + rescanPaths(pathsToRescan) { + fixDateTaken(paths, showToasts, true) + } } } } catch (e: Exception) { 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 a19e36606..34676a6a2 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 @@ -852,3 +852,27 @@ fun Context.updateDirectoryPath(path: String) { val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending, getProperFileSize) updateDBDirectory(directory, galleryDB.DirectoryDao()) } + +fun Context.getFileDateTaken(path: String): Long { + val projection = arrayOf( + MediaStore.Images.Media.DATE_TAKEN + ) + + val uri = MediaStore.Files.getContentUri("external") + val selection = "${MediaStore.Images.Media.DATA} = ?" + val selectionArgs = arrayOf(path) + + val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null) + cursor?.use { + if (cursor.moveToFirst()) { + do { + try { + return cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) + } catch (e: Exception) { + } + } while (cursor.moveToNext()) + } + } + + return 0L +}