Fix small but crashy edge case in Icon.scaleDown
Plus a regression test which throws an Exception for the old version. It
shouldn't be an issue in the real world because there's absolutely no
reason any app should be creating 1920*3px icons.
Found while porting the code somewhere else.
Test: runtest -x ./core/tests/coretests/src/android/graphics/drawable/IconTest.java
Change-Id: I1283d982507221914ddad1313e16f63af13e245a
diff --git a/core/tests/coretests/src/android/graphics/drawable/IconTest.java b/core/tests/coretests/src/android/graphics/drawable/IconTest.java
index b7a48c7..64fadc0 100644
--- a/core/tests/coretests/src/android/graphics/drawable/IconTest.java
+++ b/core/tests/coretests/src/android/graphics/drawable/IconTest.java
@@ -16,6 +16,8 @@
package android.graphics.drawable;
+import static com.google.common.truth.Truth.assertThat;
+
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Region;
@@ -108,6 +110,19 @@
}
@SmallTest
+ public void testScaleDownIfNecessary() throws Exception {
+ final Bitmap bm = Bitmap.createBitmap(4321, 78, Bitmap.Config.ARGB_8888);
+ final Icon ic = Icon.createWithBitmap(bm);
+ ic.scaleDownIfNecessary(40, 20);
+
+ assertThat(bm.getWidth()).isEqualTo(4321);
+ assertThat(bm.getHeight()).isEqualTo(78);
+
+ assertThat(ic.getBitmap().getWidth()).isLessThan(41);
+ assertThat(ic.getBitmap().getHeight()).isLessThan(21);
+ }
+
+ @SmallTest
public void testWithAdaptiveBitmap() throws Exception {
final Bitmap bm1 = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888);
diff --git a/graphics/java/android/graphics/drawable/Icon.java b/graphics/java/android/graphics/drawable/Icon.java
index c329918..749b7594 100644
--- a/graphics/java/android/graphics/drawable/Icon.java
+++ b/graphics/java/android/graphics/drawable/Icon.java
@@ -819,8 +819,10 @@
if (bitmapWidth > maxWidth || bitmapHeight > maxHeight) {
float scale = Math.min((float) maxWidth / bitmapWidth,
(float) maxHeight / bitmapHeight);
- bitmap = Bitmap.createScaledBitmap(bitmap, (int) (scale * bitmapWidth),
- (int) (scale * bitmapHeight), true /* filter */);
+ bitmap = Bitmap.createScaledBitmap(bitmap,
+ Math.max(1, (int) (scale * bitmapWidth)),
+ Math.max(1, (int) (scale * bitmapHeight)),
+ true /* filter */);
}
return bitmap;
}