ImageCacheRequest.java revision b8be1e0ad76b6abc0da7ead39f7a9811195d001e
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
17package com.android.gallery3d.data;
18
19import android.graphics.Bitmap;
20import android.graphics.BitmapFactory;
21
22import com.android.gallery3d.app.GalleryApp;
23import com.android.gallery3d.common.BitmapUtils;
24import com.android.gallery3d.data.BytesBufferPool.BytesBuffer;
25import com.android.gallery3d.util.ThreadPool.Job;
26import com.android.gallery3d.util.ThreadPool.JobContext;
27
28abstract 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    @Override
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
96    public abstract Bitmap onDecodeOriginal(JobContext jc, int targetSize);
97}
98