From 21305a6d6d199a3b3d261b1acc8a57e9771257c7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 9 Sep 2021 15:37:26 +0200 Subject: [PATCH] catch exceptions thrown at fixing date taken values --- .../gallery/pro/extensions/Activity.kt | 75 +++++++++++-------- 1 file changed, 45 insertions(+), 30 deletions(-) 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 d7a1ee3d4..884bacb65 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 @@ -102,7 +102,8 @@ fun SimpleActivity.launchAbout() { FAQItem(R.string.faq_6_title_commons, R.string.faq_6_text_commons), FAQItem(R.string.faq_7_title_commons, R.string.faq_7_text_commons), FAQItem(R.string.faq_9_title_commons, R.string.faq_9_text_commons), - FAQItem(R.string.faq_10_title_commons, R.string.faq_10_text_commons)) + FAQItem(R.string.faq_10_title_commons, R.string.faq_10_text_commons) + ) startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true) } @@ -240,8 +241,10 @@ fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList, } } -fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, deleteFromDatabase: Boolean, - callback: ((wasSuccess: Boolean) -> Unit)? = null) { +fun BaseSimpleActivity.tryDeleteFileDirItem( + fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, deleteFromDatabase: Boolean, + callback: ((wasSuccess: Boolean) -> Unit)? = null +) { deleteFile(fileDirItem, allowDeleteFolder) { if (deleteFromDatabase) { ensureBackgroundThread { @@ -445,38 +448,50 @@ fun AppCompatActivity.fixDateTaken( val dateTakens = ArrayList() for (path in paths) { - val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL) - ?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue + try { + val dateTime: String = 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 = "${Images.Media.DATA} = ?" - val selectionArgs = arrayOf(path) - withSelection(selection, selectionArgs) - withValue(Images.Media.DATE_TAKEN, timestamp) - operations.add(build()) - } + val uri = getFileUri(path) + ContentProviderOperation.newUpdate(uri).apply { + val selection = "${Images.Media.DATA} = ?" + val selectionArgs = arrayOf(path) + withSelection(selection, selectionArgs) + withValue(Images.Media.DATE_TAKEN, timestamp) + operations.add(build()) + } - if (operations.size % BATCH_SIZE == 0) { - contentResolver.applyBatch(MediaStore.AUTHORITY, operations) - operations.clear() - } + if (operations.size % BATCH_SIZE == 0) { + contentResolver.applyBatch(MediaStore.AUTHORITY, operations) + operations.clear() + } - mediaDB.updateFavoriteDateTaken(path, timestamp) - didUpdateFile = true + mediaDB.updateFavoriteDateTaken(path, timestamp) + didUpdateFile = true - val dateTaken = DateTaken(null, path, path.getFilenameFromPath(), path.getParentPath(), timestamp, (System.currentTimeMillis() / 1000).toInt(), File(path).lastModified()) - dateTakens.add(dateTaken) - if (!hasRescanned && getFileDateTaken(path) == 0L) { - pathsToRescan.add(path) + val dateTaken = DateTaken( + null, + path, + path.getFilenameFromPath(), + path.getParentPath(), + timestamp, + (System.currentTimeMillis() / 1000).toInt(), + File(path).lastModified() + ) + dateTakens.add(dateTaken) + if (!hasRescanned && getFileDateTaken(path) == 0L) { + pathsToRescan.add(path) + } + } catch (e: Exception) { + continue } }