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