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.BitmapRegionDecoder;
21
22import com.android.gallery3d.common.ApiHelper;
23import com.android.gallery3d.ui.ScreenNail;
24import com.android.gallery3d.util.ThreadPool.Job;
25
26// MediaItem represents an image or a video item.
27public abstract class MediaItem extends MediaObject {
28    // NOTE: These type numbers are stored in the image cache, so it should not
29    // not be changed without resetting the cache.
30    public static final int TYPE_THUMBNAIL = 1;
31    public static final int TYPE_MICROTHUMBNAIL = 2;
32
33    public static final int CACHED_IMAGE_QUALITY = 95;
34
35    public static final int IMAGE_READY = 0;
36    public static final int IMAGE_WAIT = 1;
37    public static final int IMAGE_ERROR = -1;
38
39    public static final String MIME_TYPE_JPEG = "image/jpeg";
40
41    private static final int BYTESBUFFE_POOL_SIZE = 4;
42    private static final int BYTESBUFFER_SIZE = 200 * 1024;
43
44    private static int sMicrothumbnailTargetSize = 200;
45    private static BitmapPool sMicroThumbPool;
46    private static final BytesBufferPool sMicroThumbBufferPool =
47            new BytesBufferPool(BYTESBUFFE_POOL_SIZE, BYTESBUFFER_SIZE);
48
49    private static int sThumbnailTargetSize = 640;
50    private static final BitmapPool sThumbPool =
51            ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY
52            ? new BitmapPool(4)
53            : null;
54
55    // TODO: fix default value for latlng and change this.
56    public static final double INVALID_LATLNG = 0f;
57
58    public abstract Job<Bitmap> requestImage(int type);
59    public abstract Job<BitmapRegionDecoder> requestLargeImage();
60
61    public MediaItem(Path path, long version) {
62        super(path, version);
63    }
64
65    public long getDateInMs() {
66        return 0;
67    }
68
69    public String getName() {
70        return null;
71    }
72
73    public void getLatLong(double[] latLong) {
74        latLong[0] = INVALID_LATLNG;
75        latLong[1] = INVALID_LATLNG;
76    }
77
78    public String[] getTags() {
79        return null;
80    }
81
82    public Face[] getFaces() {
83        return null;
84    }
85
86    // The rotation of the full-resolution image. By default, it returns the value of
87    // getRotation().
88    public int getFullImageRotation() {
89        return getRotation();
90    }
91
92    public int getRotation() {
93        return 0;
94    }
95
96    public long getSize() {
97        return 0;
98    }
99
100    public abstract String getMimeType();
101
102    public String getFilePath() {
103        return "";
104    }
105
106    // Returns width and height of the media item.
107    // Returns 0, 0 if the information is not available.
108    public abstract int getWidth();
109    public abstract int getHeight();
110
111    // This is an alternative for requestImage() in PhotoPage. If this
112    // is implemented, you don't need to implement requestImage().
113    public ScreenNail getScreenNail() {
114        return null;
115    }
116
117    public static int getTargetSize(int type) {
118        switch (type) {
119            case TYPE_THUMBNAIL:
120                return sThumbnailTargetSize;
121            case TYPE_MICROTHUMBNAIL:
122                return sMicrothumbnailTargetSize;
123            default:
124                throw new RuntimeException(
125                    "should only request thumb/microthumb from cache");
126        }
127    }
128
129    public static BitmapPool getMicroThumbPool() {
130        if (ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY && sMicroThumbPool == null) {
131            initializeMicroThumbPool();
132        }
133        return sMicroThumbPool;
134    }
135
136    public static BitmapPool getThumbPool() {
137        return sThumbPool;
138    }
139
140    public static BytesBufferPool getBytesBufferPool() {
141        return sMicroThumbBufferPool;
142    }
143
144    private static void initializeMicroThumbPool() {
145        sMicroThumbPool =
146                ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY
147                ? new BitmapPool(sMicrothumbnailTargetSize, sMicrothumbnailTargetSize, 16)
148                : null;
149    }
150
151    public static void setThumbnailSizes(int size, int microSize) {
152        sThumbnailTargetSize = size;
153        if (sMicrothumbnailTargetSize != microSize) {
154            sMicrothumbnailTargetSize = microSize;
155            initializeMicroThumbPool();
156        }
157    }
158}
159