Skip to content

Instantly share code, notes, and snippets.

@zmarkan
Created November 22, 2017 14:53
Show Gist options
  • Save zmarkan/8d2f1f76985949758ff333b8b123bb1a to your computer and use it in GitHub Desktop.
Save zmarkan/8d2f1f76985949758ff333b8b123bb1a to your computer and use it in GitHub Desktop.
Code to cause a preventive crash if using Adaptive Icons with Android Oreo, in order to avoid a crash loop
private fun hasDefaultFCMIconInMetadata(context: Context): Boolean {
try {
val appInfo = context.packageManager.getApplicationInfo(
context.packageName, PackageManager.GET_META_DATA)
if (appInfo.metaData != null) {
return appInfo.metaData.getInt(META_DATA_FIREBASE_NOTIFICATION_IMAGE, -1) != -1
}
} catch (_: PackageManager.NameNotFoundException) {
}
return false
}
private fun targetIsBelowOreo(context: Context): Boolean =
context.packageManager.getApplicationInfo(context.packageName, 0).targetSdkVersion < Build.VERSION_CODES.O
private fun canCreateIconDrawable(context: Context): Boolean {
try {
val possibleBitmap = AdaptiveIconDrawable.createFromStream(
context.resources.openRawResource(context.applicationInfo.icon),
"applicationInfo.icon")
if (possibleBitmap != null) {
return true
}
} catch (_: Exception) {
}
return false
}
fun validateApplicationIcon(context: Context) {
if (targetIsBelowOreo(context)) {
return
}
if (canCreateIconDrawable(context)) {
return
}
if (hasDefaultFCMIconInMetadata(context)) {
return
}
throw IllegalStateException(
"You are targetting Android Oreo and using adaptive icons without having a fallback drawable set for FCM notifications. \n" +
"This can cause a irreversible crash on devices using Oreo. \n " +
"To learn more about this issue check: https://issuetracker.google.com/issues/68716460"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment