Bug 1049217 - Fix CharacterValidator for API levels below 12. r=margaret, a=sledru
authorRichard Newman <rnewman@mozilla.com>
Tue, 05 Aug 2014 18:34:22 -0700
changeset 208254 fc7ce6481ea9
parent 208253 8f34703f5065
child 208255 ee74d30a8968
push id3790
push userryanvm@gmail.com
push date2014-08-07 15:21 +0000
treeherdermozilla-beta@ee74d30a8968 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, sledru
bugs1049217
milestone32.0
Bug 1049217 - Fix CharacterValidator for API levels below 12. r=margaret, a=sledru
mobile/android/base/preferences/LocaleListPreference.java
--- a/mobile/android/base/preferences/LocaleListPreference.java
+++ b/mobile/android/base/preferences/LocaleListPreference.java
@@ -53,19 +53,44 @@ public class LocaleListPreference extend
         }
 
         private Bitmap drawBitmap(String text){
             Bitmap b = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ALPHA_8);
             Canvas c = new Canvas(b);
             c.drawText(text, 0, BITMAP_HEIGHT / 2, this.paint);
             return b;
         }
-        private static byte[] getPixels(Bitmap b) {
-            ByteBuffer buffer = ByteBuffer.allocate(b.getByteCount());
-            b.copyPixelsToBuffer(buffer);
+
+        private static byte[] getPixels(final Bitmap b) {
+            final int byteCount;
+            if (android.os.Build.VERSION.SDK_INT >= 19) {
+                // TODO: when Bug 1042829 lands, do the right thing for KitKat devices.
+                // Which is:
+                // byteCount = b.getAllocationByteCount();
+                byteCount = b.getRowBytes() * b.getHeight();
+            } else {
+                // Close enough for government work.
+                // Equivalent to getByteCount, but works on <12.
+                byteCount = b.getRowBytes() * b.getHeight();
+            }
+
+            final ByteBuffer buffer = ByteBuffer.allocate(byteCount);
+            try {
+                b.copyPixelsToBuffer(buffer);
+            } catch (RuntimeException e) {
+                // Android throws this if there's not enough space in the buffer.
+                // This should never occur, but if it does, we don't
+                // really care -- we probably don't need the entire image.
+                // This is awful. I apologize.
+                if ("Buffer not large enough for pixels".equals(e.getMessage())) {
+                    return buffer.array();
+                }
+                throw e;
+            }
+
             return buffer.array();
         }
 
         public boolean characterIsMissingInFont(String ch) {
             byte[] rendered = getPixels(drawBitmap(ch));
             return Arrays.equals(rendered, missingCharacter);
         }
     }