• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ImageCacheRequest(GalleryApp application, Path path, int type, int targetSize)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     @Override
run(JobContext jc)45     public Bitmap run(JobContext jc) {
46         String debugTag = mPath + "," +
47                  ((mType == MediaItem.TYPE_THUMBNAIL) ? "THUMB" :
48                  (mType == MediaItem.TYPE_MICROTHUMBNAIL) ? "MICROTHUMB" : "?");
49         ImageCacheService cacheService = mApplication.getImageCacheService();
50 
51         BytesBuffer buffer = MediaItem.getBytesBufferPool().get();
52         try {
53             boolean found = cacheService.getImageData(mPath, mType, buffer);
54             if (jc.isCancelled()) return null;
55             if (found) {
56                 BitmapFactory.Options options = new BitmapFactory.Options();
57                 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
58                 Bitmap bitmap;
59                 if (mType == MediaItem.TYPE_MICROTHUMBNAIL) {
60                     bitmap = MediaItem.getMicroThumbPool().decode(jc,
61                             buffer.data, buffer.offset, buffer.length, options);
62                 } else {
63                     bitmap = MediaItem.getThumbPool().decode(jc,
64                             buffer.data, buffer.offset, buffer.length, options);
65                 }
66                 if (bitmap == null && !jc.isCancelled()) {
67                     Log.w(TAG, "decode cached failed " + debugTag);
68                 }
69                 return bitmap;
70             }
71         } finally {
72             MediaItem.getBytesBufferPool().recycle(buffer);
73         }
74         Bitmap bitmap = onDecodeOriginal(jc, mType);
75         if (jc.isCancelled()) return null;
76 
77         if (bitmap == null) {
78             Log.w(TAG, "decode orig failed " + debugTag);
79             return null;
80         }
81 
82         if (mType == MediaItem.TYPE_MICROTHUMBNAIL) {
83             bitmap = BitmapUtils.resizeAndCropCenter(bitmap, mTargetSize, true);
84         } else {
85             bitmap = BitmapUtils.resizeDownBySideLength(bitmap, mTargetSize, true);
86         }
87         if (jc.isCancelled()) return null;
88 
89         byte[] array = BitmapUtils.compressToBytes(bitmap);
90         if (jc.isCancelled()) return null;
91 
92         cacheService.putImageData(mPath, mType, array);
93         return bitmap;
94     }
95 
onDecodeOriginal(JobContext jc, int targetSize)96     public abstract Bitmap onDecodeOriginal(JobContext jc, int targetSize);
97 }
98