From c423aa2fff4d13ab2f0c11f543936d516d1dff2a Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sun, 31 Jul 2022 21:47:06 +0200 Subject: [PATCH] Fix crash when an icon pack references an invalid resource id --- .../mm20/launcher2/icons/IconPackManager.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt b/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt index 9e2d1f84..9a915eb5 100644 --- a/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt +++ b/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt @@ -69,7 +69,11 @@ class IconPackManager( } val resId = res.getIdentifier(drawableName, "drawable", iconPack).takeIf { it != 0 } ?: return null - val drawable = ResourcesCompat.getDrawable(res, resId, context.theme) ?: return null + val drawable = try { + ResourcesCompat.getDrawable(res, resId, context.theme) ?: return null + } catch (e: Resources.NotFoundException) { + return null + } return when (drawable) { is AdaptiveIconDrawable -> { return StaticLauncherIcon( @@ -148,7 +152,11 @@ class IconPackManager( if (mask != null) { res.getIdentifier(mask, "drawable", pack).takeIf { it != 0 }?.let { paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT) - val maskDrawable = ResourcesCompat.getDrawable(res, it, null) ?: return null + val maskDrawable = try { + ResourcesCompat.getDrawable(res, it, null) ?: return null + } catch (e: Resources.NotFoundException) { + return null + } val maskBmp = maskDrawable.toBitmap(size, size) inBounds = Rect(0, 0, maskBmp.width, maskBmp.height) outBounds = Rect(0, 0, bitmap.width, bitmap.height) @@ -158,7 +166,11 @@ class IconPackManager( if (upon != null) { res.getIdentifier(upon, "drawable", pack).takeIf { it != 0 }?.let { paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OVER) - val maskDrawable = ResourcesCompat.getDrawable(res, it, null) ?: return null + val maskDrawable = try { + ResourcesCompat.getDrawable(res, it, null) ?: return null + } catch (e: Resources.NotFoundException) { + return null + } val maskBmp = maskDrawable.toBitmap(size, size) inBounds = Rect(0, 0, maskBmp.width, maskBmp.height) outBounds = Rect(0, 0, bitmap.width, bitmap.height) @@ -168,7 +180,11 @@ class IconPackManager( if (back != null) { res.getIdentifier(back, "drawable", pack).takeIf { it != 0 }?.let { paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OVER) - val maskDrawable = ResourcesCompat.getDrawable(res, it, null) ?: return null + val maskDrawable = try { + ResourcesCompat.getDrawable(res, it, null) ?: return null + } catch (e: Resources.NotFoundException) { + return null + } val maskBmp = maskDrawable.toBitmap(size, size) inBounds = Rect(0, 0, maskBmp.width, maskBmp.height) outBounds = Rect(0, 0, bitmap.width, bitmap.height)