Fix NPE in StatusBarIconView#getIcon

The code to downscale the Icon drawables does not account for
potentially `null` Icons returned from `Icon#loadDrawableAsUser`.

Fixes: 218331554
Test: manual + atest
Change-Id: Iad1ef2975a0526a1fe129aa9693b6e9fe3d046b5
This commit is contained in:
Steve Elliott
2022-02-14 17:01:36 -05:00
parent 865dc828f5
commit 497fad03ee
2 changed files with 27 additions and 10 deletions

View File

@@ -427,15 +427,17 @@ public class StatusBarIconView extends AnimatedImageView implements StatusIconDi
typedValue, true);
float scaleFactor = typedValue.getFloat();
// We downscale the loaded drawable to reasonable size to protect against applications
// using too much memory. The size can be tweaked in config.xml. Drawables
// that are already sized properly won't be touched.
boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic();
Resources res = sysuiContext.getResources();
int maxIconSize = res.getDimensionPixelSize(isLowRamDevice
? com.android.internal.R.dimen.notification_small_icon_size_low_ram
: com.android.internal.R.dimen.notification_small_icon_size);
icon = DrawableSize.downscaleToSize(res, icon, maxIconSize, maxIconSize);
if (icon != null) {
// We downscale the loaded drawable to reasonable size to protect against applications
// using too much memory. The size can be tweaked in config.xml. Drawables that are
// already sized properly won't be touched.
boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic();
Resources res = sysuiContext.getResources();
int maxIconSize = res.getDimensionPixelSize(isLowRamDevice
? com.android.internal.R.dimen.notification_small_icon_size_low_ram
: com.android.internal.R.dimen.notification_small_icon_size);
icon = DrawableSize.downscaleToSize(res, icon, maxIconSize, maxIconSize);
}
// No need to scale the icon, so return it as is.
if (scaleFactor == 1.f) {

View File

@@ -155,4 +155,19 @@ public class StatusBarIconViewTest extends SysuiTestCase {
mIconView.getIcon(largeIcon);
// no crash? good
}
}
@Test
public void testNullIcon() {
Icon mockIcon = mock(Icon.class);
when(mockIcon.loadDrawableAsUser(any(), anyInt())).thenReturn(null);
mStatusBarIcon.icon = mockIcon;
mIconView.set(mStatusBarIcon);
Bitmap bitmap = Bitmap.createBitmap(60, 60, Bitmap.Config.ARGB_8888);
Icon icon = Icon.createWithBitmap(bitmap);
StatusBarIcon largeIcon = new StatusBarIcon(UserHandle.ALL, "mockPackage",
icon, 0, 0, "");
mIconView.getIcon(largeIcon);
// No crash? good
}
}