diff options
author | Allan Wang <me@allanwang.ca> | 2018-01-18 23:50:07 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-18 23:50:07 -0500 |
commit | 6058759bbd615506cb7f110acaa21c51b82188f9 (patch) | |
tree | 6970a421f1af4becf7f1771942b3e259aa85812d /core/src/main/kotlin | |
parent | 6843e83623d2fb9bb7f42d0d9f66ab90a5a47348 (diff) | |
download | kau-6058759bbd615506cb7f110acaa21c51b82188f9.tar.gz kau-6058759bbd615506cb7f110acaa21c51b82188f9.tar.bz2 kau-6058759bbd615506cb7f110acaa21c51b82188f9.zip |
Misc (#126)
* Add auto uri read grant
* Add full attachment support
* Revert back to single attachment
* Try send with email
* Add email type
* Pass null instead of empty bundle
* Update changelog
Diffstat (limited to 'core/src/main/kotlin')
3 files changed, 31 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() } |