From b8b118c62c4e14066ce98ec07149e3707eb3b571 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 10 Feb 2018 16:49:50 +0100 Subject: [PATCH] catch exceptions thrown at looking up contacts --- .../simplemobiletools/contacts/extensions/Context.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Context.kt index 86d978a5..1e310640 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Context.kt @@ -55,7 +55,9 @@ fun Context.getLookupUriRawId(dataUri: Uri): Int { val lookupKey = getLookupKeyFromUri(dataUri) if (lookupKey != null && isLollipopPlus()) { val uri = lookupContactUri(lookupKey, this) - return getContactUriRawId(uri) + if (uri != null) { + getContactUriRawId(uri) + } } return -1 } @@ -94,9 +96,13 @@ fun isEncodedContactUri(uri: Uri?): Boolean { return lastPathSegment == "encoded" } -fun lookupContactUri(lookup: String, context: Context): Uri { +fun lookupContactUri(lookup: String, context: Context): Uri? { val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup) - return ContactsContract.Contacts.lookupContact(context.contentResolver, lookupUri) + return try { + ContactsContract.Contacts.lookupContact(context.contentResolver, lookupUri) + } catch (e: Exception) { + null + } } fun Context.getCachePhoto(): File {