Skip to content

Instantly share code, notes, and snippets.

@lawloretienne
Created February 16, 2020 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawloretienne/d1d0294ee1bebf4e3578227f4ac2693f to your computer and use it in GitHub Desktop.
Save lawloretienne/d1d0294ee1bebf4e3578227f4ac2693f to your computer and use it in GitHub Desktop.
object EmailUtils {
fun getEmailIntent(context: Context): Intent {
val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
val bodyText = getEmailBody(context)
val emailAddy = EMAIL_SUPPORT
val subject = context.resources.getString(R.string.email_subject)
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(emailAddy))
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
intent.putExtra(Intent.EXTRA_TEXT, bodyText)
return intent
}
private fun getEmailBody(context: Context): String {
val appName = getAppName(context)
val appVesion = getAppVersion(context)
val appPackageName = getAppPackageName(context)
val device = getDevice()
val osVersion = getOSVersion()
var emailBody = ""
if (appName.isNotEmpty()) {
emailBody += "App Name: $appName\n"
}
if (appVesion.isNotEmpty()) {
emailBody += "App Version: $appVesion\n"
}
if (!appPackageName.isNullOrEmpty()) {
emailBody += "App ID: $appPackageName\n"
}
if (!device.isNullOrEmpty()) {
emailBody += "Device: $device\n"
}
if (osVersion.isNotEmpty()) {
emailBody += "OS Version: $osVersion\n"
}
return emailBody
}
private fun getAppName(context: Context): String {
var appName = ""
val packageName = context.packageName
val pm = context.packageManager
try {
val pi = pm.getPackageInfo(packageName, 0)
appName = context.resources.getString(pi.applicationInfo.labelRes)
} catch (e: Exception) {
Timber.e(e)
}
return appName
}
private fun getAppVersion(context: Context): String {
var version = ""
try {
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
version = pInfo.versionName + " (" + pInfo.versionCode + ")"
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return version
}
private fun getAppPackageName(context: Context) = context.packageName
private fun getDevice() = Build.DEVICE
private fun getOSVersion(): String = Build.VERSION.SDK_INT.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment