From b6012f6e2b966ae3f873ea84b2be1201f1cf6527 Mon Sep 17 00:00:00 2001 From: Naveen Date: Thu, 3 Nov 2022 23:49:55 +0530 Subject: [PATCH] Workaround some potential exceptions --- .../helpers/AttachmentPreviews.kt | 28 +++++++++++++------ .../smsmessenger/helpers/VCardParser.kt | 7 ++++- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentPreviews.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentPreviews.kt index 03efe2f0..222ad3cf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentPreviews.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentPreviews.kt @@ -24,9 +24,13 @@ fun View.setupDocumentPreview( filename.text = title } - val size = context.getFileSizeFromUri(uri) - file_size.beVisible() - file_size.text = size.formatSize() + try { + val size = context.getFileSizeFromUri(uri) + file_size.beVisible() + file_size.text = size.formatSize() + } catch (e: Exception) { + file_size.beGone() + } val textColor = context.getProperTextColor() val primaryColor = context.getProperPrimaryColor() @@ -82,13 +86,19 @@ fun View.setupVCardPreview( } parseVCardFromUri(activity, uri) { vCards -> - val title = vCards.firstOrNull()?.parseNameFromVCard() - val imageIcon = if (title != null) { - SimpleContactsHelper(activity).getContactLetterIcon(title) - } else { - null - } activity.runOnUiThread { + if (vCards.isEmpty()) { + vcard_title.beVisible() + vcard_title.text = context.getString(R.string.unknown_error_occurred) + return@runOnUiThread + } + val title = vCards.firstOrNull()?.parseNameFromVCard() + val imageIcon = if (title != null) { + SimpleContactsHelper(activity).getContactLetterIcon(title) + } else { + null + } + arrayOf(vcard_photo, vcard_title).forEach { it.beVisible() } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/VCardParser.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/VCardParser.kt index 3456c080..d93c36ac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/VCardParser.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/VCardParser.kt @@ -8,7 +8,12 @@ import ezvcard.VCard fun parseVCardFromUri(context: Context, uri: Uri, callback: (vCards: List) -> Unit) { ensureBackgroundThread { - val inputStream = context.contentResolver.openInputStream(uri) + val inputStream = try { + context.contentResolver.openInputStream(uri) + } catch (e: Exception) { + callback(emptyList()) + return@ensureBackgroundThread + } val vCards = Ezvcard.parse(inputStream).all() callback(vCards) }