1/*
2 * Copyright (C) 2013 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.data;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.database.Cursor;
22import android.net.Uri;
23
24import com.android.camera.data.FilmstripContentQueries.CursorToFilmstripItemFactory;
25import com.android.camera.debug.Log;
26
27import java.util.List;
28
29public class PhotoItemFactory implements CursorToFilmstripItemFactory<PhotoItem> {
30    private static final Log.Tag TAG = new Log.Tag("PhotoItemFact");
31
32    private final Context mContext;
33    private final GlideFilmstripManager mGlideManager;
34    private final ContentResolver mContentResolver;
35    private final PhotoDataFactory mPhotoDataFactory;
36
37    public PhotoItemFactory(Context context, GlideFilmstripManager glideManager,
38          ContentResolver contentResolver, PhotoDataFactory photoDataFactory) {
39        mContext = context;
40        mGlideManager = glideManager;
41        mContentResolver = contentResolver;
42        mPhotoDataFactory = photoDataFactory;
43    }
44
45    @Override
46    public PhotoItem get(Cursor c) {
47        FilmstripItemData data = mPhotoDataFactory.fromCursor(c);
48        if (data != null) {
49            return new PhotoItem(mContext, mGlideManager, data, this);
50        } else {
51            Log.w(TAG, "skipping item with null data, returning null for item");
52            return null;
53        }
54    }
55
56    public PhotoItem get(Uri uri) {
57        PhotoItem newData = null;
58        Cursor c = mContentResolver.query(uri, PhotoDataQuery.QUERY_PROJECTION,
59              null, null, null);
60        if (c != null) {
61            if (c.moveToFirst()) {
62                newData = get(c);
63            }
64            c.close();
65        }
66
67        return newData;
68    }
69
70    /** Query for all the photo data items */
71    public List<PhotoItem> queryAll() {
72        return queryAll(PhotoDataQuery.CONTENT_URI, FilmstripItemBase.QUERY_ALL_MEDIA_ID);
73    }
74
75    /** Query for all the photo data items */
76    public List<PhotoItem> queryAll(Uri uri, long lastId) {
77        return FilmstripContentQueries
78              .forCameraPath(mContentResolver, uri, PhotoDataQuery.QUERY_PROJECTION, lastId,
79                    PhotoDataQuery.QUERY_ORDER, this);
80    }
81
82    /** Query for a single data item */
83    public PhotoItem queryContentUri(Uri uri) {
84        // TODO: Consider refactoring this, this approach may be slow.
85        List<PhotoItem> newPhotos = queryAll(uri, FilmstripItemBase.QUERY_ALL_MEDIA_ID);
86        if (newPhotos.isEmpty()) {
87            return null;
88        }
89        return newPhotos.get(0);
90    }
91}
92