From 84ebb59482e5a4bbc1a52fa0fc8858def8840031 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Jan 2019 15:44:04 +0100 Subject: [PATCH] restore proper file paths --- .../gallery/pro/adapters/MediaAdapter.kt | 10 ++- .../gallery/pro/extensions/Activity.kt | 88 +++++++++---------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index 606cfd2a4..de3601c4c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -307,10 +307,12 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList Unit) fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) { Thread { + val newPaths = ArrayList() paths.forEach { val source = it val destination = it.removePrefix(recycleBinPath) @@ -250,6 +250,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDa if (File(source).length() == File(destination).length()) { mediumDao.updateDeleted(destination.removePrefix(recycleBinPath), 0, "$RECYCLE_BIN$destination") } + newPaths.add(destination) } catch (e: Exception) { showErrorToast(e) } finally { @@ -262,7 +263,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDa callback() } - fixDateTaken(paths) + fixDateTaken(newPaths) }.start() } @@ -315,54 +316,51 @@ fun Activity.hasNavBar(): Boolean { fun Activity.fixDateTaken(paths: ArrayList, callback: (() -> Unit)? = null) { val BATCH_SIZE = 50 toast(R.string.fixing) - Thread { - try { - var didUpdateFile = false - val operations = ArrayList() - val mediumDao = galleryDB.MediumDao() - for (path in paths) { - val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL) - ?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue + try { + var didUpdateFile = false + val operations = ArrayList() + val mediumDao = galleryDB.MediumDao() + for (path in paths) { + val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL) + ?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue - // some formats contain a "T" in the middle, some don't - // sample dates: 2015-07-26T14:55:23, 2018:09:05 15:09:05 - val t = if (dateTime.substring(10, 11) == "T") "\'T\'" else " " - val separator = dateTime.substring(4, 5) - val format = "yyyy${separator}MM${separator}dd${t}kk:mm:ss" - val formatter = SimpleDateFormat(format, Locale.getDefault()) - val timestamp = formatter.parse(dateTime).time + // some formats contain a "T" in the middle, some don't + // sample dates: 2015-07-26T14:55:23, 2018:09:05 15:09:05 + val t = if (dateTime.substring(10, 11) == "T") "\'T\'" else " " + val separator = dateTime.substring(4, 5) + val format = "yyyy${separator}MM${separator}dd${t}kk:mm:ss" + val formatter = SimpleDateFormat(format, Locale.getDefault()) + val timestamp = formatter.parse(dateTime).time - val uri = getFileUri(path) - ContentProviderOperation.newUpdate(uri).apply { - val selection = "${MediaStore.Images.Media.DATA} = ?" - val selectionArgs = arrayOf(path) - withSelection(selection, selectionArgs) - withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp) - operations.add(build()) - } - - if (operations.size % BATCH_SIZE == 0) { - contentResolver.applyBatch(MediaStore.AUTHORITY, operations) - operations.clear() - } - - Log.e("DEBUG", "restoring $path") - mediumDao.updateFavoriteDateTaken(path, timestamp) - didUpdateFile = true + val uri = getFileUri(path) + ContentProviderOperation.newUpdate(uri).apply { + val selection = "${MediaStore.Images.Media.DATA} = ?" + val selectionArgs = arrayOf(path) + withSelection(selection, selectionArgs) + withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp) + operations.add(build()) } - val resultSize = contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size - if (resultSize == 0) { - didUpdateFile = false - rescanPaths(paths) + if (operations.size % BATCH_SIZE == 0) { + contentResolver.applyBatch(MediaStore.AUTHORITY, operations) + operations.clear() } - toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) - runOnUiThread { - callback?.invoke() - } - } catch (e: Exception) { - showErrorToast(e) + mediumDao.updateFavoriteDateTaken(path, timestamp) + didUpdateFile = true } - }.start() + + val resultSize = contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size + if (resultSize == 0) { + didUpdateFile = false + rescanPaths(paths) + } + + toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) + runOnUiThread { + callback?.invoke() + } + } catch (e: Exception) { + showErrorToast(e) + } }