1 /* 2 * Copyright (C) 2010 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 */ 16 17 package com.android.gallery3d.data; 18 19 import android.graphics.Bitmap; 20 import android.graphics.BitmapFactory; 21 22 import com.android.gallery3d.app.GalleryApp; 23 import com.android.gallery3d.common.BitmapUtils; 24 import com.android.gallery3d.data.BytesBufferPool.BytesBuffer; 25 import com.android.gallery3d.util.ThreadPool.Job; 26 import com.android.gallery3d.util.ThreadPool.JobContext; 27 28 abstract class ImageCacheRequest implements Job<Bitmap> { 29 private static final String TAG = "ImageCacheRequest"; 30 31 protected GalleryApp mApplication; 32 private Path mPath; 33 private int mType; 34 private int mTargetSize; 35 36 public ImageCacheRequest(GalleryApp application, 37 Path path, int type, int targetSize) { 38 mApplication = application; 39 mPath = path; 40 mType = type; 41 mTargetSize = targetSize; 42 } 43 44 private String debugTag() { 45 return mPath + "," + 46 ((mType == MediaItem.TYPE_THUMBNAIL) ? "THUMB" : 47 (mType == MediaItem.TYPE_MICROTHUMBNAIL) ? "MICROTHUMB" : "?"); 48 } 49 50 @Override 51 public Bitmap run(JobContext jc) { 52 ImageCacheService cacheService = mApplication.getImageCacheService(); 53 54 BytesBuffer buffer = MediaItem.getBytesBufferPool().get(); 55 try { 56 boolean found = cacheService.getImageData(mPath, mType, buffer); 57 if (jc.isCancelled()) return null; 58 if (found) { 59 BitmapFactory.Options options = new BitmapFactory.Options(); 60 options.inPreferredConfig = Bitmap.Config.ARGB_8888; 61 Bitmap bitmap; 62 if (mType == MediaItem.TYPE_MICROTHUMBNAIL) { 63 bitmap = DecodeUtils.decodeUsingPool(jc, 64 buffer.data, buffer.offset, buffer.length, options); 65 } else { 66 bitmap = DecodeUtils.decodeUsingPool(jc, 67 buffer.data, buffer.offset, buffer.length, options); 68 } 69 if (bitmap == null && !jc.isCancelled()) { 70 Log.w(TAG, "decode cached failed " + debugTag()); 71 } 72 return bitmap; 73 } 74 } finally { 75 MediaItem.getBytesBufferPool().recycle(buffer); 76 } 77 Bitmap bitmap = onDecodeOriginal(jc, mType); 78 if (jc.isCancelled()) return null; 79 80 if (bitmap == null) { 81 Log.w(TAG, "decode orig failed " + debugTag()); 82 return null; 83 } 84 85 if (mType == MediaItem.TYPE_MICROTHUMBNAIL) { 86 bitmap = BitmapUtils.resizeAndCropCenter(bitmap, mTargetSize, true); 87 } else { 88 bitmap = BitmapUtils.resizeDownBySideLength(bitmap, mTargetSize, true); 89 } 90 if (jc.isCancelled()) return null; 91 92 byte[] array = BitmapUtils.compressToBytes(bitmap); 93 if (jc.isCancelled()) return null; 94 95 cacheService.putImageData(mPath, mType, array); 96 return bitmap; 97 } 98 99 public abstract Bitmap onDecodeOriginal(JobContext jc, int targetSize); 100 } 101