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 | |
parent | f41eb8b14f9bb076fc95a059e6bb45e003656169 (diff) | |
download | kau-eef0a98de4add1f55b6ec61318db4951f44b4df2.tar.gz kau-eef0a98de4add1f55b6ec61318db4951f44b4df2.tar.bz2 kau-eef0a98de4add1f55b6ec61318db4951f44b4df2.zip |
Add email builder and other utils
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | docs/Changelog.md | 1 | ||||
-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 | ||||
-rw-r--r-- | sample/build.gradle | 4 | ||||
-rw-r--r-- | sample/src/main/res/xml/changelog.xml | 6 |
7 files changed, 112 insertions, 4 deletions
@@ -43,6 +43,7 @@ dependencies { * [Ripple Canvas](#ripple-canvas) * [Timber Logger](#timber-logger) * [Extensions](#extensions) +* [Email Builder](#email-builder) <a name="kprefs"></a> ## KPrefs @@ -230,10 +231,18 @@ The canvas also supports color fading and direct color setting so it can effecti ### Utils [Misc] > Extends Int * dpToPx & pxToDp conversions +* Check sdk version +* Check if app is installed ### ViewUtils > Extends View * `visible()`, `invisible()`, `gone()`, `isVisible()`, `isInvisible()`, `isGone()` methods * matchParent method to set the layout params to match_parent * Create snackbar directly -* Set IIcon into ImageView directly
\ No newline at end of file +* Set IIcon into ImageView directly + +<a name="email-builder"></a> +## Email Builder + +Easily send an email through `Context.sendEmail`. +Include your email and subject, along with other optional configurations such as retrieving device info.
\ No newline at end of file diff --git a/docs/Changelog.md b/docs/Changelog.md index 8f7bfb0..6bebb58 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -2,6 +2,7 @@ ## v1.2 * Fix title attribute in changelog +* Update support libs ## v1.1 * Created kpref items 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> diff --git a/sample/build.gradle b/sample/build.gradle index 66f87dd..78a3802 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -34,4 +34,8 @@ dependencies { }) testCompile 'junit:junit:4.12' compile "com.mikepenz:google-material-typeface:${IICON_GOOGLE}.original@aar" + compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/sample/src/main/res/xml/changelog.xml b/sample/src/main/res/xml/changelog.xml index 015171f..76d2563 100644 --- a/sample/src/main/res/xml/changelog.xml +++ b/sample/src/main/res/xml/changelog.xml @@ -8,9 +8,9 @@ <version title="v1.2"/> <item text="Fix title attribute in changelog" /> - <item text="" /> - <item text="" /> - <item text="" /> + <item text="Update support libs" /> + <item text="Add is app installed utils" /> + <item text="Add email builder" /> <item text="" /> <item text="" /> <item text="" /> |