OSDN Git Service

Avoid building FilmstripItems when data is null
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / data / VideoItemFactory.java
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 import android.provider.MediaStore;
24
25 import com.android.camera.data.FilmstripContentQueries.CursorToFilmstripItemFactory;
26 import com.android.camera.debug.Log;
27
28 import java.util.List;
29
30 public class VideoItemFactory implements CursorToFilmstripItemFactory<VideoItem> {
31     private static final Log.Tag TAG = new Log.Tag("VideoItemFact");
32     private static final String QUERY_ORDER = MediaStore.Video.VideoColumns.DATE_TAKEN
33           + " DESC, " + MediaStore.Video.VideoColumns._ID + " DESC";
34
35     private final Context mContext;
36     private final ContentResolver mContentResolver;
37     private final VideoDataFactory mVideoDataFactory;
38
39     public VideoItemFactory(Context context, ContentResolver contentResolver,
40           VideoDataFactory videoDataFactory) {
41         mContext = context;
42         mContentResolver = contentResolver;
43         mVideoDataFactory = videoDataFactory;
44     }
45
46     @Override
47     public VideoItem get(Cursor c) {
48         VideoItemData data = mVideoDataFactory.fromCursor(c);
49         if (data != null) {
50             return new VideoItem(mContext, data, this);
51         } else {
52             Log.w(TAG, "skipping item with null data, returning null for item");
53             return null;
54         }
55     }
56
57     /** Query for a single video data item */
58     public VideoItem get(Uri uri) {
59         VideoItem newData = null;
60         Cursor c = mContext.getContentResolver().query(uri, VideoDataQuery.QUERY_PROJECTION, null,
61               null, null);
62         if (c != null) {
63             if (c.moveToFirst()) {
64                 newData = get(c);
65             }
66             c.close();
67         }
68
69         return newData;
70     }
71
72     /** Query for all the video data items */
73     public List<VideoItem> queryAll() {
74         return queryAll(VideoDataQuery.CONTENT_URI,
75               FilmstripItemBase.QUERY_ALL_MEDIA_ID);
76     }
77
78     /** Query for all the video data items */
79     public List<VideoItem> queryAll(Uri uri, long lastId) {
80         return FilmstripContentQueries
81               .forCameraPath(mContentResolver, uri, VideoDataQuery.QUERY_PROJECTION, lastId,
82                     QUERY_ORDER, this);
83     }
84
85     /** Query for a single data item */
86     public VideoItem queryContentUri(Uri uri) {
87         // TODO: Consider refactoring this, this approach may be slow.
88         List<VideoItem> videos = queryAll(uri,
89               FilmstripItemBase.QUERY_ALL_MEDIA_ID);
90         if (videos.isEmpty()) {
91             return null;
92         }
93         return videos.get(0);
94     }
95 }