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 
17 package com.android.camera.data;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.net.Uri;
23 
24 import com.android.camera.data.FilmstripContentQueries.CursorToFilmstripItemFactory;
25 import com.android.camera.debug.Log;
26 
27 import java.util.List;
28 
29 public 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 
PhotoItemFactory(Context context, GlideFilmstripManager glideManager, ContentResolver contentResolver, PhotoDataFactory photoDataFactory)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
get(Cursor c)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 
get(Uri uri)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 */
queryAll()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 */
queryAll(Uri uri, long lastId)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 */
queryContentUri(Uri uri)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