aboutsummaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
Diffstat (limited to 'library')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt70
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt21
-rw-r--r--library/src/main/res/values/strings.xml3
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>