Skip to content

Instantly share code, notes, and snippets.

@maxirosson
Last active July 15, 2021 00:04
Show Gist options
  • Save maxirosson/9eb722182bb3b0950d0c18c33247b2ae to your computer and use it in GitHub Desktop.
Save maxirosson/9eb722182bb3b0950d0c18c33247b2ae to your computer and use it in GitHub Desktop.
object CrashlyticsHelper {
fun sendCustomKeys(context: Context) {
val builder = CustomKeysAndValues.Builder()
builder.putString("abi", Build.SUPPORTED_ABIS[0])
builder.putString("locale", getLocale(context).toString())
builder.putBoolean("isGooglePlayServicesAvailable", isGooglePlayServicesAvailable(context))
getInstallSource(context)?.let {
builder.putString("installSource", it)
}
FirebaseCrashlytics.getInstance().setCustomKeys(builder.build())
}
/**
* Retrieve the locale information for the app.
*/
@Suppress("DEPRECATION")
private fun getLocale(context: Context): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.resources.configuration.locales[0]
} else {
context.resources.configuration.locale
}
}
private fun isGooglePlayServicesAvailable(context: Context): Boolean {
return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS
}
private fun getInstallSource(context: Context): String? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return context.packageManager.getInstallSourceInfo(context.packageName).initiatingPackageName
}
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment