Created
February 16, 2020 21:43
-
-
Save lawloretienne/d1d0294ee1bebf4e3578227f4ac2693f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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