BaseImage.java revision 7d2d5ec1e9d6c19278e41c103eacd8090be24406
1/*
2 * Copyright (C) 2009 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.camera.gallery;
18
19import com.android.camera.BitmapManager;
20import com.android.camera.Util;
21
22import android.content.ContentResolver;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.net.Uri;
26import android.os.ParcelFileDescriptor;
27import android.provider.MediaStore.Images;
28import android.util.Log;
29
30import java.io.FileNotFoundException;
31import java.io.IOException;
32import java.io.InputStream;
33
34/**
35 * Represents a particular image and provides access to the underlying bitmap
36 * and two thumbnail bitmaps as well as other information such as the id, and
37 * the path to the actual image data.
38 */
39public abstract class BaseImage implements IImage {
40    private static final String TAG = "BaseImage";
41    private static final int UNKNOWN_LENGTH = -1;
42    protected ContentResolver mContentResolver;
43
44    // Database field
45    protected Uri mUri;
46    protected long mId;
47    protected String mDataPath;
48    protected long mMiniThumbMagic;
49    protected final int mIndex;
50    protected String mMimeType;
51    private final long mDateTaken;
52    private String mTitle;
53    private final String mDisplayName;
54
55    protected BaseImageList mContainer;
56
57    private int mWidth = UNKNOWN_LENGTH;
58    private int mHeight = UNKNOWN_LENGTH;
59
60    protected BaseImage(BaseImageList container, ContentResolver cr,
61            long id, int index, Uri uri, String dataPath, long miniThumbMagic,
62            String mimeType, long dateTaken, String title, String displayName) {
63        mContainer = container;
64        mContentResolver = cr;
65        mId = id;
66        mIndex = index;
67        mUri = uri;
68        mDataPath = dataPath;
69        mMiniThumbMagic = miniThumbMagic;
70        mMimeType = mimeType;
71        mDateTaken = dateTaken;
72        mTitle = title;
73        mDisplayName = displayName;
74    }
75
76    public String getDataPath() {
77        return mDataPath;
78    }
79
80    @Override
81    public boolean equals(Object other) {
82        if (other == null || !(other instanceof Image)) return false;
83        return mUri.equals(((Image) other).mUri);
84    }
85
86    @Override
87    public int hashCode() {
88        return mUri.hashCode();
89    }
90
91    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) {
92        return fullSizeBitmap(minSideLength, maxNumberOfPixels,
93                IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE);
94    }
95
96    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
97            boolean rotateAsNeeded, boolean useNative) {
98        Uri url = mContainer.contentUri(mId);
99        if (url == null) return null;
100
101        Bitmap b = Util.makeBitmap(minSideLength, maxNumberOfPixels,
102                url, mContentResolver, useNative);
103
104        if (b != null && rotateAsNeeded) {
105            b = Util.rotate(b, getDegreesRotated());
106        }
107
108        return b;
109    }
110
111    public InputStream fullSizeImageData() {
112        try {
113            InputStream input = mContentResolver.openInputStream(mUri);
114            return input;
115        } catch (IOException ex) {
116            return null;
117        }
118    }
119
120    public Uri fullSizeImageUri() {
121        return mUri;
122    }
123
124    public IImageList getContainer() {
125        return mContainer;
126    }
127
128    public long getDateTaken() {
129        return mDateTaken;
130    }
131
132    public int getDegreesRotated() {
133        return 0;
134    }
135
136    public String getMimeType() {
137        return mMimeType;
138    }
139
140    public String getTitle() {
141        return mTitle;
142    }
143
144    public String getDisplayName() {
145        return mDisplayName;
146    }
147
148    private void setupDimension() {
149        ParcelFileDescriptor input = null;
150        try {
151            input = mContentResolver.openFileDescriptor(mUri, "r");
152            BitmapFactory.Options options = new BitmapFactory.Options();
153            options.inJustDecodeBounds = true;
154            BitmapManager.instance().decodeFileDescriptor(
155                    input.getFileDescriptor(), options);
156            mWidth = options.outWidth;
157            mHeight = options.outHeight;
158        } catch (FileNotFoundException ex) {
159            mWidth = 0;
160            mHeight = 0;
161        } finally {
162            Util.closeSilently(input);
163        }
164    }
165
166    public int getWidth() {
167        if (mWidth == UNKNOWN_LENGTH) setupDimension();
168        return mWidth;
169    }
170
171    public int getHeight() {
172        if (mHeight == UNKNOWN_LENGTH) setupDimension();
173        return mHeight;
174    }
175
176    public Bitmap miniThumbBitmap() {
177        Bitmap b = null;
178        try {
179            long id = mId;
180            b = BitmapManager.instance().getThumbnail(mContentResolver, id,
181                    Images.Thumbnails.MICRO_KIND, null, false);
182        } catch (Throwable ex) {
183            Log.e(TAG, "miniThumbBitmap got exception", ex);
184            return null;
185        }
186        if (b != null) {
187            b = Util.rotate(b, getDegreesRotated());
188        }
189        return b;
190    }
191
192    protected void onRemove() {
193    }
194
195    @Override
196    public String toString() {
197        return mUri.toString();
198    }
199}
200