Created
November 22, 2017 14:53
-
-
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
This file contains hidden or 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
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