1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.support.v4.graphics; 17 18import android.graphics.Bitmap; 19 20/** 21 * Helper for accessing features in {@link android.graphics.Bitmap} 22 * introduced after API level 4 in a backwards compatible fashion. 23 */ 24public final class BitmapCompat { 25 /** 26 * Interface for the full API. 27 */ 28 interface BitmapImpl { 29 public boolean hasMipMap(Bitmap bitmap); 30 public void setHasMipMap(Bitmap bitmap, boolean hasMipMap); 31 public int getAllocationByteCount(Bitmap bitmap); 32 } 33 34 static class BaseBitmapImpl implements BitmapImpl { 35 @Override 36 public boolean hasMipMap(Bitmap bitmap) { 37 return false; 38 } 39 40 @Override 41 public void setHasMipMap(Bitmap bitmap, boolean hasMipMap) { 42 } 43 44 @Override 45 public int getAllocationByteCount(Bitmap bitmap) { 46 return bitmap.getRowBytes() * bitmap.getHeight(); 47 } 48 } 49 50 static class HcMr1BitmapCompatImpl extends BaseBitmapImpl { 51 @Override 52 public int getAllocationByteCount(Bitmap bitmap) { 53 return BitmapCompatHoneycombMr1.getAllocationByteCount(bitmap); 54 } 55 } 56 57 static class JbMr2BitmapCompatImpl extends HcMr1BitmapCompatImpl { 58 @Override 59 public boolean hasMipMap(Bitmap bitmap){ 60 return BitmapCompatJellybeanMR2.hasMipMap(bitmap); 61 } 62 63 @Override 64 public void setHasMipMap(Bitmap bitmap, boolean hasMipMap) { 65 BitmapCompatJellybeanMR2.setHasMipMap(bitmap, hasMipMap); 66 } 67 } 68 69 static class KitKatBitmapCompatImpl extends JbMr2BitmapCompatImpl { 70 @Override 71 public int getAllocationByteCount(Bitmap bitmap) { 72 return BitmapCompatKitKat.getAllocationByteCount(bitmap); 73 } 74 } 75 76 /** 77 * Select the correct implementation to use for the current platform. 78 */ 79 static final BitmapImpl IMPL; 80 static { 81 final int version = android.os.Build.VERSION.SDK_INT; 82 if (version >= 19) { 83 IMPL = new KitKatBitmapCompatImpl(); 84 } else if (version >= 18) { 85 IMPL = new JbMr2BitmapCompatImpl(); 86 } else if (version >= 12) { 87 IMPL = new HcMr1BitmapCompatImpl(); 88 } else { 89 IMPL = new BaseBitmapImpl(); 90 } 91 } 92 93 public static boolean hasMipMap(Bitmap bitmap) { 94 return IMPL.hasMipMap(bitmap); 95 } 96 97 public static void setHasMipMap(Bitmap bitmap, boolean hasMipMap) { 98 IMPL.setHasMipMap(bitmap, hasMipMap); 99 } 100 101 /** 102 * Returns the size of the allocated memory used to store this bitmap's pixels in a backwards 103 * compatible way. 104 * 105 * @param bitmap the bitmap in which to return it's allocation size 106 * @return the allocation size in bytes 107 */ 108 public static int getAllocationByteCount(Bitmap bitmap) { 109 return IMPL.getAllocationByteCount(bitmap); 110 } 111 112 private BitmapCompat() {} 113}