From 42acf6009ade4314f3cd782d68db6ab7ad4c8da3 Mon Sep 17 00:00:00 2001 From: Lucas Dupin Date: Thu, 13 Jul 2017 16:32:44 -0700 Subject: [PATCH] Don't recycle bitmaps we don't own WallpaperColors#fromBitmap should only recycle the bitmaps created by itself. Test: runtest -x tests/Internal/src/android/app/WallpaperColorsTest.java Fixes: 63758291 Change-Id: I1710bf7118b6ae871442f9606f95ac9c9dbb7d24 --- core/java/android/app/WallpaperColors.java | 11 ++++++++--- .../src/android/app/WallpaperColorsTest.java | 12 ++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/java/android/app/WallpaperColors.java b/core/java/android/app/WallpaperColors.java index b9d3e75b99436..d0791cf93469b 100644 --- a/core/java/android/app/WallpaperColors.java +++ b/core/java/android/app/WallpaperColors.java @@ -136,12 +136,12 @@ public final class WallpaperColors implements Parcelable { } final int bitmapArea = bitmap.getWidth() * bitmap.getHeight(); + boolean shouldRecycle = false; if (bitmapArea > MAX_WALLPAPER_EXTRACTION_AREA) { + shouldRecycle = true; Size optimalSize = calculateOptimalSize(bitmap.getWidth(), bitmap.getHeight()); - Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, optimalSize.getWidth(), + bitmap = Bitmap.createScaledBitmap(bitmap, optimalSize.getWidth(), optimalSize.getHeight(), true /* filter */); - bitmap.recycle(); - bitmap = scaledBitmap; } final Palette palette = Palette @@ -181,6 +181,11 @@ public final class WallpaperColors implements Parcelable { } int hints = calculateHints(bitmap); + + if (shouldRecycle) { + bitmap.recycle(); + } + return new WallpaperColors(primary, secondary, tertiary, hints); } diff --git a/tests/Internal/src/android/app/WallpaperColorsTest.java b/tests/Internal/src/android/app/WallpaperColorsTest.java index 5bbd82bea3ded..fb529b936e5ce 100644 --- a/tests/Internal/src/android/app/WallpaperColorsTest.java +++ b/tests/Internal/src/android/app/WallpaperColorsTest.java @@ -77,4 +77,16 @@ public class WallpaperColorsTest { Assert.assertFalse("Light surface shouldn't support dark text " + "when it contains dark pixels", supportsDarkText); } + + /** + * WallpaperColors should not recycle bitmaps that it didn't create. + */ + @Test + public void wallpaperRecycleBitmapTest() { + Bitmap image = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); + WallpaperColors.fromBitmap(image); + Canvas canvas = new Canvas(); + // This would crash: + canvas.drawBitmap(image, 0, 0, new Paint()); + } }