mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2024-11-26 22:47:59 +01:00
improve FixDateTaken, rescan paths missing from mediastore if necessary
This commit is contained in:
parent
255448fca2
commit
28981ac870
2 changed files with 41 additions and 6 deletions
|
@ -348,12 +348,13 @@ fun Activity.hasNavBar(): Boolean {
|
|||
return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0)
|
||||
}
|
||||
|
||||
fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, callback: (() -> Unit)? = null) {
|
||||
fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, hasRescanned: Boolean = false, callback: (() -> Unit)? = null) {
|
||||
val BATCH_SIZE = 50
|
||||
if (showToasts) {
|
||||
toast(R.string.fixing)
|
||||
}
|
||||
|
||||
val pathsToRescan = ArrayList<String>()
|
||||
try {
|
||||
var didUpdateFile = false
|
||||
val operations = ArrayList<ContentProviderOperation>()
|
||||
|
@ -388,6 +389,10 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, 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<String>, 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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue