summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/data/CameraDataAdapter.java
blob: 5d10953f882dba09971b61051e7c741390c0f515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.data;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.ImageColumns;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.camera.Storage;
import com.android.camera.ui.FilmStripView;

import java.util.ArrayList;
import java.util.List;

/**
 * A FilmStripDataProvider that provide data in the camera folder.
 *
 * The given view for camera preview won't be added until the preview info
 * has been set by setPreviewInfo(int, int, int)
 */
public class CameraDataAdapter implements FilmStripView.DataAdapter {
    private static final String TAG = "CamreaFilmStripDataProvider";

    private static final int DEFAULT_DECODE_SIZE = 3000;
    private static final String ORDER_CLAUSE = ImageColumns.DATE_TAKEN + " DESC, "
            + ImageColumns._ID + " DESC";
    private static final String[] CAMERA_PATH = { Storage.DIRECTORY + "%" };
    private static final int COL_ID = 0;
    private static final int COL_TITLE = 1;
    private static final int COL_MIME_TYPE = 2;
    private static final int COL_DATE_TAKEN = 3;
    private static final int COL_DATE_MODIFIED = 4;
    private static final int COL_DATA = 5;
    private static final int COL_ORIENTATION = 6;
    private static final int COL_WIDTH = 7;
    private static final int COL_HEIGHT = 8;
    private static final int COL_SIZE = 9;

    private static final String[] PROJECTION = {
        ImageColumns._ID,           // 0, int
        ImageColumns.TITLE,         // 1, string
        ImageColumns.MIME_TYPE,     // 2, tring
        ImageColumns.DATE_TAKEN,    // 3, int
        ImageColumns.DATE_MODIFIED, // 4, int
        ImageColumns.DATA,          // 5, string
        ImageColumns.ORIENTATION,   // 6, int, 0, 90, 180, 270
        ImageColumns.WIDTH,         // 7, int
        ImageColumns.HEIGHT,        // 8, int
        ImageColumns.SIZE           // 9, int
    };

    // 32K buffer.
    private static final byte[] DECODE_TEMP_STORAGE = new byte[32 * 1024];

    private List<LocalImageData> mImages;

    private FilmStripView mFilmStripView;
    private View mCameraPreviewView;
    private ColorDrawable mPlaceHolder;

    private int mSuggestedWidth = DEFAULT_DECODE_SIZE;
    private int mSuggestedHeight = DEFAULT_DECODE_SIZE;

    public CameraDataAdapter(View cameraPreviewView, int placeHolderColor) {
        mCameraPreviewView = cameraPreviewView;
        mPlaceHolder = new ColorDrawable(placeHolderColor);
    }

    public void setCameraPreviewInfo(int width, int height, int orientation) {
        addOrReplaceCameraData(buildCameraImageData(width, height, orientation));
    }

    @Override
    public int getTotalNumber() {
        return mImages.size();
    }

    @Override
    public FilmStripView.ImageData getImageData(int id) {
        if (id >= mImages.size()) return null;
        return mImages.get(id);
    }

    @Override
    public void suggestSize(int w, int h) {
        if (w <= 0 || h <= 0) {
            mSuggestedWidth  = mSuggestedHeight = DEFAULT_DECODE_SIZE;
        } else {
            mSuggestedWidth = (w < DEFAULT_DECODE_SIZE ? w : DEFAULT_DECODE_SIZE);
            mSuggestedHeight = (h < DEFAULT_DECODE_SIZE ? h : DEFAULT_DECODE_SIZE);
        }
    }

    @Override
    public void requestLoad(ContentResolver resolver) {
        QueryTask qtask = new QueryTask();
        qtask.execute(resolver);
    }

    @Override
    public View getView(Context c, int dataID) {
        if (dataID >= mImages.size() || dataID < 0) {
            return null;
        }

        LocalImageData data = mImages.get(dataID);

        if (data.isCameraData) return mCameraPreviewView;

        ImageView v = new ImageView(c);
        v.setImageDrawable(mPlaceHolder);

        v.setScaleType(ImageView.ScaleType.FIT_XY);
        LoadBitmapTask task = new LoadBitmapTask(data, v);
        task.execute();
        return v;
    }

    @Override
    public void setDataListener(FilmStripView v) {
        mFilmStripView = v;
    }

    private LocalImageData buildCameraImageData(int width, int height, int orientation) {
        LocalImageData d = new LocalImageData();
        d.width = width;
        d.height = height;
        d.orientation = orientation;
        d.isCameraData = true;
        return d;
    }

    private void addOrReplaceCameraData(LocalImageData data) {
        if (mImages == null) mImages = new ArrayList<LocalImageData>();
        if (mImages.size() == 0) {
            mImages.add(0, data);
            return;
        }

        LocalImageData first = mImages.get(0);
        if (first.isCameraData) {
            mImages.set(0, data);
        } else {
            mImages.add(0, data);
        }
    }

    private LocalImageData buildCursorImageData(Cursor c) {
        LocalImageData d = new LocalImageData();
        d.id = c.getInt(COL_ID);
        d.title = c.getString(COL_TITLE);
        d.mimeType = c.getString(COL_MIME_TYPE);
        d.path = c.getString(COL_DATA);
        d.orientation = c.getInt(COL_ORIENTATION);
        d.width = c.getInt(COL_WIDTH);
        d.height = c.getInt(COL_HEIGHT);
        if (d.width <= 0 || d.height <= 0) {
            Log.v(TAG, "warning! zero dimension for "
                    + d.path + ":" + d.width + "x" + d.height);
            Dimension dim = decodeDimension(d.path);
            if (dim != null) {
                d.width = dim.width;
                d.height = dim.height;
            } else {
                Log.v(TAG, "warning! dimension decode failed for " + d.path);
                Bitmap b = BitmapFactory.decodeFile(d.path);
                if (b == null) return null;
                d.width = b.getWidth();
                d.height = b.getHeight();
            }
        }
        if (d.orientation == 90 || d.orientation == 270) {
            int b = d.width;
            d.width = d.height;
            d.height = b;
        }
        return d;
    }

    private Dimension decodeDimension(String path) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap b = BitmapFactory.decodeFile(path, opts);
        if (b == null) return null;
        Dimension d = new Dimension();
        d.width = opts.outWidth;
        d.height = opts.outHeight;
        return d;
    }

    private class Dimension {
        public int width;
        public int height;
    }

    private class LocalImageData implements FilmStripView.ImageData {
        public boolean isCameraData;
        public int id;
        public String title;
        public String mimeType;
        public String path;
        // from MediaStore, can only be 0, 90, 180, 270;
        public int orientation;
        // width and height should be adjusted according to orientation.
        public int width;
        public int height;

        @Override
        public int getWidth() {
            return width;
        }

        @Override
        public int getHeight() {
            return height;
        }

        @Override
        public String toString() {
            return "LocalImageData:" + ",data=" + path + ",mimeType=" + mimeType
                    + "," + width + "x" + height + ",orientation=" + orientation;
        }
    }

    private class QueryTask extends AsyncTask<ContentResolver, Void, List<LocalImageData>> {
        private ContentResolver mResolver;
        private LocalImageData mCameraImageData;

        @Override
        protected List<LocalImageData> doInBackground(ContentResolver... resolver) {
            List<LocalImageData> l = null;
            Cursor c = resolver[0].query(Images.Media.EXTERNAL_CONTENT_URI, PROJECTION,
                    MediaStore.Images.Media.DATA + " like ? ", CAMERA_PATH,
                    ORDER_CLAUSE);
            if (c == null) return null;
            l = new ArrayList<LocalImageData>();
            c.moveToFirst();
            while (!c.isLast()) {
                LocalImageData data = buildCursorImageData(c);
                if (data != null) l.add(data);
                else Log.e(TAG, "Error decoding file:" + c.getString(COL_DATA));
                c.moveToNext();
            }
            c.close();
            return l;
        }

        @Override
        protected void onPostExecute(List<LocalImageData> l) {
            boolean changed = (l != mImages);
            LocalImageData first = null;
            if (mImages != null && mImages.size() > 0) {
                first = mImages.get(0);
                if (!first.isCameraData) first = null;
            }
            mImages = l;
            if (first != null) addOrReplaceCameraData(first);
            // both might be null.
            if (changed) mFilmStripView.onDataChanged();
        }
    }

    private class LoadBitmapTask extends AsyncTask<Void, Void, Bitmap> {
        private LocalImageData mData;
        private ImageView mView;

        public LoadBitmapTask(
                LocalImageData d, ImageView v) {
            mData = d;
            mView = v;
        }

        @Override
        protected Bitmap doInBackground(Void... v) {
            BitmapFactory.Options opts = null;
            Bitmap b;
            int sample = 1;
            while (mSuggestedWidth * sample < mData.width
                    || mSuggestedHeight * sample < mData.height) {
                sample *= 2;
            }
            opts = new BitmapFactory.Options();
            opts.inSampleSize = sample;
            opts.inTempStorage = DECODE_TEMP_STORAGE;
            if (isCancelled()) return null;
            b = BitmapFactory.decodeFile(mData.path, opts);
            if (mData.orientation != 0) {
                if (isCancelled()) return null;
                Matrix m = new Matrix();
                m.setRotate((float) mData.orientation);
                b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, false);
            }
            return b;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap == null) {
                Log.e(TAG, "Cannot decode bitmap file:" + mData.path);
                return;
            }
            mView.setImageBitmap(bitmap);
        }
    }
}