aboutsummaryrefslogtreecommitdiff
path: root/core/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt34
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt3
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt3
-rw-r--r--core/src/main/res/values/strings.xml4
4 files changed, 35 insertions, 9 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
index 8277c1d..b75ff4a 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
@@ -26,16 +26,25 @@ class EmailBuilder(val email: String, val subject: String) {
var footer: String? = null
private val pairs: MutableMap<String, String> = mutableMapOf()
private val packages: MutableList<Package> = mutableListOf()
+ private var attachment: Uri? = null
fun checkPackage(packageName: String, appName: String) = packages.add(Package(packageName, appName))
fun addItem(key: String, value: String) = pairs.put(key, value)
+ fun addAttachment(uri: Uri) {
+ attachment = uri
+ }
+
+ var extras: Intent.() -> Unit = {}
+
data class Package(val packageName: String, val appName: String)
fun getIntent(context: Context): Intent {
- val intent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:$email"))
- intent.putExtra(Intent.EXTRA_SUBJECT, subject)
+ val intent = Intent(Intent.ACTION_SEND)
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ .putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
+ .putExtra(Intent.EXTRA_SUBJECT, subject)
val emailBuilder = StringBuilder()
emailBuilder.append(message).append("\n\n")
if (deviceDetails) {
@@ -77,16 +86,27 @@ class EmailBuilder(val email: String, val subject: String) {
emailBuilder.append("\n").append(footer)
intent.putExtra(Intent.EXTRA_TEXT, emailBuilder.toString())
+ intent.type = "text/plain"
return intent
}
- inline fun execute(context: Context, extras: Intent.() -> Unit = {}) {
+ /**
+ * Create the intent and send the request when possible
+ * If a stream uri is added, it will automatically be flagged to pass on read permissions
+ */
+ fun execute(context: Context) {
val intent = getIntent(context)
intent.extras()
- if (intent.resolveActivity(context.packageManager) != null)
- context.startActivity(Intent.createChooser(intent, context.string(R.string.kau_send_via)))
- else
- context.toast("Cannot resolve email activity", log = true)
+ val packageName = intent.resolveActivity(context.packageManager)?.packageName
+ ?: return context.toast(R.string.kau_error_no_email, log = true)
+
+ val attachment = this.attachment
+ if (attachment != null) {
+ context.grantUriPermission(packageName, attachment, Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ intent.putExtra(Intent.EXTRA_STREAM, attachment)
+ }
+
+ context.startActivity(Intent.createChooser(intent, context.string(R.string.kau_send_via)))
}
}
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
index f8643db..fb88e61 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
@@ -28,6 +28,7 @@ annotation class KauActivity
* Counterpart of [Activity.startActivityForResult]
* For starting activities without result, see [startActivity]
*/
+@Suppress("DEPRECATION")
inline fun <reified T : Activity> Activity.startActivityForResult(
requestCode: Int,
bundleBuilder: Bundle.() -> Unit = {},
@@ -46,7 +47,7 @@ inline fun <T : Activity> Activity.startActivityForResult(
intent.intentBuilder()
val bundle = Bundle()
bundle.bundleBuilder()
- startActivityForResult(intent, requestCode, bundle)
+ startActivityForResult(intent, requestCode, if (bundle.isEmpty) null else bundle)
}
/**
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
index 0e4b5f1..89ad5f4 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -31,6 +31,7 @@ import com.afollestad.materialdialogs.MaterialDialog
* Counterpart of [Context.startActivity]
* For starting activities for results, see [startActivityForResult]
*/
+@Suppress("DEPRECATION")
inline fun <reified T : Activity> Context.startActivity(
clearStack: Boolean = false,
bundleBuilder: Bundle.() -> Unit = {},
@@ -50,7 +51,7 @@ inline fun <T : Activity> Context.startActivity(
intent.intentBuilder()
val bundle = Bundle()
bundle.bundleBuilder()
- startActivity(intent, bundle)
+ startActivity(intent, if (bundle.isEmpty) null else bundle)
if (clearStack && this is Activity) finish()
}
diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml
new file mode 100644
index 0000000..7832e12
--- /dev/null
+++ b/core/src/main/res/values/strings.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="kau_error_no_email">Cannot resolve email activity</string>
+</resources> \ No newline at end of file