From 8aa2d8601f6a97af4a329cfc3bfd5fb5fb5bcbf8 Mon Sep 17 00:00:00 2001 From: Mark Wei Date: Tue, 21 Jan 2014 14:58:19 -0800 Subject: Check both ReusableBitmap and ReusableBitmap#bmp for null to avoid NPE. Bug: 12617030 Change-Id: I5882be1f02413dd55f44bf09b68257e9690abb6c --- src/com/android/bitmap/drawable/BasicBitmapDrawable.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java index 8fe1ff4..684d322 100644 --- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java +++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java @@ -204,7 +204,7 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, * Should only be overriden, not called. */ protected void setBitmap(ReusableBitmap bmp) { - if (mBitmap != null && mBitmap != bmp) { + if (hasBitmap()) { mBitmap.releaseReference(); } mBitmap = bmp; @@ -293,7 +293,7 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, return; } - if (mBitmap != null && mBitmap.bmp != null) { + if (hasBitmap()) { BitmapUtils.calculateCroppedSrcRect( mBitmap.getLogicalWidth(), mBitmap.getLogicalHeight(), bounds.width(), bounds.height(), @@ -321,12 +321,18 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, } } + protected boolean hasBitmap() { + return mBitmap != null && mBitmap.bmp != null; + } + /** * Override this method to customize how to draw the bitmap to the canvas for the given bounds. * The bitmap to be drawn can be found at {@link #getBitmap()}. */ protected void onDrawBitmap(final Canvas canvas, final Rect src, final Rect dst) { - canvas.drawBitmap(mBitmap.bmp, src, dst, mPaint); + if (hasBitmap()) { + canvas.drawBitmap(mBitmap.bmp, src, dst, mPaint); + } } @Override @@ -346,7 +352,7 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, @Override public int getOpacity() { - return (mBitmap != null && (mBitmap.bmp.hasAlpha() || mPaint.getAlpha() < 255)) ? + return (hasBitmap() && (mBitmap.bmp.hasAlpha() || mPaint.getAlpha() < 255)) ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE; } -- cgit v1.2.3