Skip to content

Commit

Permalink
Merge pull request #26 from adaptmobile-organization/feature/update-c…
Browse files Browse the repository at this point in the history
…onductor

Fix intent actions
  • Loading branch information
GaborHartyandi authored Aug 17, 2020
2 parents a92cab6 + 7e53b52 commit 456dca1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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" }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
}
Expand All @@ -111,13 +113,16 @@ fun Context.getFontCompat(fontRes: Int): Typeface? {

fun Context?.composeEmail(addresses: Array<String>, 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" }
}
}
}
Expand All @@ -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" }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>, 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" }
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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" }
}
}
}
Expand Down

0 comments on commit 456dca1

Please sign in to comment.