diff --git a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ActivityExtensions.kt b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ActivityExtensions.kt index 7e16bfa..7b1925d 100644 --- a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ActivityExtensions.kt +++ b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ActivityExtensions.kt @@ -1,6 +1,7 @@ package dk.adaptmobile.amkotlinutil.extensions import android.app.Activity +import android.content.ActivityNotFoundException import android.content.Context import android.content.ContextWrapper import android.content.Intent @@ -96,11 +97,14 @@ fun Activity?.openNativeShareDialog(url: String, subject: String = "", chooserTe this ?: return val intent = Intent(Intent.ACTION_SEND).apply { type = "text/plain" - flags = Intent.FLAG_ACTIVITY_NEW_TASK putExtra(Intent.EXTRA_TEXT, url) putExtra(Intent.EXTRA_SUBJECT, subject) } - this.startActivity(Intent.createChooser(intent, chooserText)) + try { + this.startActivity(Intent.createChooser(intent, chooserText)) + } catch (e: ActivityNotFoundException) { + e { "Unable to find market app: $e" } + } } diff --git a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ContextExtensions.kt b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ContextExtensions.kt index a9574fd..56272cf 100644 --- a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ContextExtensions.kt +++ b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ContextExtensions.kt @@ -99,8 +99,10 @@ fun Context?.openInBrowser(url: String?) { if (url != null && url.isNotEmpty()) { val page = Uri.parse(url) val intent = Intent(Intent.ACTION_VIEW, page) - if (intent.resolveActivity(this?.packageManager) != null) { + try { this?.startActivity(intent) + } catch (e: ActivityNotFoundException) { + e { "Unable to find market app: $e" } } } } @@ -111,13 +113,16 @@ fun Context.getFontCompat(fontRes: Int): Typeface? { fun Context?.composeEmail(addresses: Array, subject: String, text: String) { this?.let { - val intent = Intent(Intent.ACTION_SENDTO) - intent.data = Uri.parse("mailto:") // only email apps should handle this - intent.putExtra(Intent.EXTRA_EMAIL, addresses) - intent.putExtra(Intent.EXTRA_SUBJECT, subject) - intent.putExtra(Intent.EXTRA_TEXT, text) - if (intent.resolveActivity(packageManager) != null) { + val intent = Intent(Intent.ACTION_SENDTO).apply { + data = Uri.parse("mailto:") // only email apps should handle this + putExtra(Intent.EXTRA_EMAIL, addresses) + putExtra(Intent.EXTRA_SUBJECT, subject) + putExtra(Intent.EXTRA_TEXT, text) + } + try { startActivity(intent) + } catch (e: ActivityNotFoundException) { + e { "Unable to find market app: $e" } } } } @@ -144,17 +149,25 @@ fun Context.getProductionApplicationId(): String { fun Context?.dial(number: String) { this?.let { val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$number")) - this.startActivity(intent) + try { + startActivity(intent) + } catch (ex: ActivityNotFoundException) { + e { "Unable to find market app: $ex" } + } } } fun Context?.openGoogleMaps(query: String, placeId: String) { val queryEncoded = Uri.encode(query) val gmmIntentUri = Uri.parse("https://www.google.com/maps/search/?api=1&query=$queryEncoded&query_place_id=$placeId") - val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri) - mapIntent.setPackage("com.google.android.apps.maps") - if (mapIntent.resolveActivity(this?.packageManager) != null) { + val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri).apply { + setPackage("com.google.android.apps.maps") + } + + try { this?.startActivity(mapIntent) + } catch (ex: ActivityNotFoundException) { + e { "Unable to find market app: $ex" } } } diff --git a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ControllerExtensions.kt b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ControllerExtensions.kt index 55ec2a2..1a26ee4 100644 --- a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ControllerExtensions.kt +++ b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/ControllerExtensions.kt @@ -1,18 +1,22 @@ package dk.adaptmobile.amkotlinutil.extensions +import android.content.ActivityNotFoundException import android.content.Intent import android.net.Uri import com.bluelinelabs.conductor.Controller +import com.github.ajalt.timberkt.e fun Controller.composeEmail(addresses: Array, subject: String, emailText: String, chooserText: String) { val intent = Intent(Intent.ACTION_SENDTO).apply { type = "text/html" - flags = Intent.FLAG_ACTIVITY_NEW_TASK data = Uri.parse("mailto:") putExtra(Intent.EXTRA_EMAIL, addresses) putExtra(Intent.EXTRA_SUBJECT, subject) putExtra(Intent.EXTRA_TEXT, emailText) } - - this.startActivity(Intent.createChooser(intent, chooserText)) + try { + this.startActivity(Intent.createChooser(intent, chooserText)) + } catch (ex: ActivityNotFoundException) { + e { "Unable to find market app: $ex" } + } } \ No newline at end of file diff --git a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/StringExtensions.kt b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/StringExtensions.kt index 7bc7e8a..3f4abce 100644 --- a/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/StringExtensions.kt +++ b/amkotlinutil/src/main/java/dk/adaptmobile/amkotlinutil/extensions/StringExtensions.kt @@ -1,5 +1,6 @@ package dk.adaptmobile.amkotlinutil.extensions +import android.content.ActivityNotFoundException import android.content.Context import android.content.Intent import android.graphics.Color @@ -38,8 +39,10 @@ fun String?.openInBrowser(context: Context?) { val page = Uri.parse(this) val intent = Intent(Intent.ACTION_VIEW, page) - if (intent.resolveActivity(context?.packageManager) != null) { + try { context?.startActivity(intent) + } catch (e: ActivityNotFoundException) { + com.github.ajalt.timberkt.e { "Unable to find market app: $e" } } } }