Workaround some potential exceptions

This commit is contained in:
Naveen 2022-11-03 23:49:55 +05:30
parent 09956f809d
commit b6012f6e2b
2 changed files with 25 additions and 10 deletions

View File

@ -24,9 +24,13 @@ fun View.setupDocumentPreview(
filename.text = title filename.text = title
} }
try {
val size = context.getFileSizeFromUri(uri) val size = context.getFileSizeFromUri(uri)
file_size.beVisible() file_size.beVisible()
file_size.text = size.formatSize() file_size.text = size.formatSize()
} catch (e: Exception) {
file_size.beGone()
}
val textColor = context.getProperTextColor() val textColor = context.getProperTextColor()
val primaryColor = context.getProperPrimaryColor() val primaryColor = context.getProperPrimaryColor()
@ -82,13 +86,19 @@ fun View.setupVCardPreview(
} }
parseVCardFromUri(activity, uri) { vCards -> parseVCardFromUri(activity, uri) { vCards ->
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 title = vCards.firstOrNull()?.parseNameFromVCard()
val imageIcon = if (title != null) { val imageIcon = if (title != null) {
SimpleContactsHelper(activity).getContactLetterIcon(title) SimpleContactsHelper(activity).getContactLetterIcon(title)
} else { } else {
null null
} }
activity.runOnUiThread {
arrayOf(vcard_photo, vcard_title).forEach { arrayOf(vcard_photo, vcard_title).forEach {
it.beVisible() it.beVisible()
} }

View File

@ -8,7 +8,12 @@ import ezvcard.VCard
fun parseVCardFromUri(context: Context, uri: Uri, callback: (vCards: List<VCard>) -> Unit) { fun parseVCardFromUri(context: Context, uri: Uri, callback: (vCards: List<VCard>) -> Unit) {
ensureBackgroundThread { 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() val vCards = Ezvcard.parse(inputStream).all()
callback(vCards) callback(vCards)
} }