From 036c96e56cfe7b5a27b83f960d38d64f7fe1dd11 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 3 Apr 2021 23:17:06 +0200 Subject: [PATCH] adding a helper function for launching Android 11 intents --- .../commons/extensions/Activity.kt | 35 +++---------------- .../commons/extensions/Context.kt | 10 ++++++ 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt index 6d2fd6211..0ba891cc6 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -180,11 +180,7 @@ fun Activity.launchViewIntent(id: Int) = launchViewIntent(getString(id)) fun Activity.launchViewIntent(url: String) { ensureBackgroundThread { Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply { - if (resolveActivity(packageManager) != null) { - startActivity(this) - } else { - toast(R.string.no_app_found) - } + launchActivityIntent(this) } } } @@ -382,15 +378,7 @@ fun Activity.launchViewContactIntent(uri: Uri) { Intent().apply { action = ContactsContract.QuickContact.ACTION_QUICK_CONTACT data = uri - if (resolveActivity(packageManager) != null) { - try { - startActivity(this) - } catch (e: Exception) { - showErrorToast(e) - } - } else { - toast(R.string.no_app_found) - } + launchActivityIntent(this) } } @@ -404,11 +392,7 @@ fun BaseSimpleActivity.launchCallIntent(recipient: String, handle: PhoneAccountH putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle) } - if (resolveActivity(packageManager) != null) { - startActivity(this) - } else { - toast(R.string.no_app_found) - } + launchActivityIntent(this) } } } @@ -416,11 +400,7 @@ fun BaseSimpleActivity.launchCallIntent(recipient: String, handle: PhoneAccountH fun Activity.launchSendSMSIntent(recipient: String) { Intent(Intent.ACTION_SENDTO).apply { data = Uri.fromParts("smsto", recipient, null) - if (resolveActivity(packageManager) != null) { - startActivity(this) - } else { - toast(R.string.no_app_found) - } + launchActivityIntent(this) } } @@ -429,12 +409,7 @@ fun Activity.showLocationOnMap(coordinates: String) { val encodedQuery = Uri.encode(coordinates) val uriString = "$uriBegin?q=$encodedQuery&z=16" val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uriString)) - val packageManager = packageManager - if (intent.resolveActivity(packageManager) != null) { - startActivity(intent) - } else { - toast(R.string.no_app_found) - } + launchActivityIntent(intent) } fun Activity.getFinalUriFromPath(path: String, applicationId: String): Uri? { diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt index 8178fe7c8..761f0321d 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt @@ -270,6 +270,16 @@ fun Context.getPermissionString(id: Int) = when (id) { else -> "" } +fun Context.launchActivityIntent(intent: Intent) { + try { + startActivity(intent) + } catch (e: ActivityNotFoundException) { + toast(R.string.no_app_found) + } catch (e: Exception) { + showErrorToast(e) + } +} + fun Context.getFilePublicUri(file: File, applicationId: String): Uri { // for images/videos/gifs try getting a media content uri first, like content://media/external/images/media/438 // if media content uri is null, get our custom uri like content://com.simplemobiletools.gallery.provider/external_files/emulated/0/DCIM/IMG_20171104_233915.jpg