diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-20 23:27:19 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-20 23:27:19 -0700 |
commit | eef0a98de4add1f55b6ec61318db4951f44b4df2 (patch) | |
tree | 0ab9bae7918925b8ef4500f3bb2af8e32d0f32f1 /library/src | |
parent | f41eb8b14f9bb076fc95a059e6bb45e003656169 (diff) | |
download | kau-eef0a98de4add1f55b6ec61318db4951f44b4df2.tar.gz kau-eef0a98de4add1f55b6ec61318db4951f44b4df2.tar.bz2 kau-eef0a98de4add1f55b6ec61318db4951f44b4df2.zip |
Add email builder and other utils
Diffstat (limited to 'library/src')
-rw-r--r-- | library/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt | 70 | ||||
-rw-r--r-- | library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt | 21 | ||||
-rw-r--r-- | library/src/main/res/values/strings.xml | 3 |
3 files changed, 94 insertions, 0 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt b/library/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt new file mode 100644 index 0000000..1553757 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt @@ -0,0 +1,70 @@ +package ca.allanwang.kau.email + +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Build +import android.support.annotation.StringRes +import ca.allanwang.kau.R +import ca.allanwang.kau.logging.KL +import ca.allanwang.kau.utils.isAppInstalled +import ca.allanwang.kau.utils.string + + +/** + * Created by Allan Wang on 2017-06-20. + */ +class EmailBuilder(@StringRes val emailId: Int, @StringRes val subjectId: Int) { + var message: String = "Write here." + var deviceDetails: Boolean = true + var appInfo: Boolean = true + var footer: String? = null + private val packages: MutableList<Package> = mutableListOf() + + fun checkPackage(packageName: String, appName: String) = packages.add(Package(packageName, appName)) + + data class Package(val packageName: String, val appName: String) + + fun execute(context: Context) { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:" + context.string(emailId))) + intent.putExtra(Intent.EXTRA_SUBJECT, context.string(subjectId)) + val emailBuilder = StringBuilder() + emailBuilder.append(message).append("\n\n") + if (deviceDetails) + emailBuilder.append("\nOS Version: ").append(System.getProperty("os.version")).append("(").append(Build.VERSION.INCREMENTAL).append(")") + .append("\nOS API Level: ").append(Build.VERSION.SDK_INT) + .append("\nDevice: ").append(Build.DEVICE) + .append("\nManufacturer: ").append(Build.MANUFACTURER) + .append("\nModel (and Product): ").append(Build.MODEL).append(" (").append(Build.PRODUCT).append(")") + .append("\n") + + if (appInfo) { + try { + val appInfo = context.packageManager.getPackageInfo(context.packageName, 0) + emailBuilder.append("\nApp Version Name: ").append(appInfo.versionName) + .append("\nApp Version Code: ").append(appInfo.versionCode).append("\n") + } catch (e: PackageManager.NameNotFoundException) { + KL.e("EmailBuilder packageInfo not found") + } + } + + packages.forEach { + if (context.isAppInstalled(it.packageName)) + emailBuilder.append(String.format("\n%s is installed", it.appName)) + } + + if (footer != null) + emailBuilder.append("\n\n").append(footer) + + intent.putExtra(Intent.EXTRA_TEXT, emailBuilder.toString()) + context.startActivity(Intent.createChooser(intent, context.resources.getString(R.string.kau_send_via))) + } +} + +fun Context.sendEmail(@StringRes emailId: Int, @StringRes subjectId: Int, builder: EmailBuilder.() -> Unit = {}) { + EmailBuilder(emailId, subjectId).apply { + builder() + execute(this@sendEmail) + } +}
\ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt index 642c29c..8aaadaf 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt @@ -1,9 +1,13 @@ package ca.allanwang.kau.utils +import android.content.Context import android.content.res.Resources import android.os.Build import android.os.Looper import ca.allanwang.kau.logging.KL +import android.content.pm.PackageManager + + /** * Created by Allan Wang on 2017-05-28. @@ -27,4 +31,21 @@ val buildIsMarshmallowAndUp: Boolean fun checkThread(id: Int) { val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not" KL.d("$id $status in the main thread") +} + +/** + * Checks if a given package is installed + * @param packageName packageId + * @return true if installed with activity, false otherwise + */ +fun Context.isAppInstalled(packageName: String): Boolean { + val pm = packageManager + var installed: Boolean + try { + pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES) + installed = true + } catch (e: PackageManager.NameNotFoundException) { + installed = false + } + return installed }
\ No newline at end of file diff --git a/library/src/main/res/values/strings.xml b/library/src/main/res/values/strings.xml index ed939fa..c6f0f44 100644 --- a/library/src/main/res/values/strings.xml +++ b/library/src/main/res/values/strings.xml @@ -15,4 +15,7 @@ <string name="kau_kpref_title_placeholder">Title Placeholder</string> <string name="kau_pref_icon">Pref Icon</string> + + <string name="kau_send_via">Send via</string> + </resources> |