Home | History | Annotate | Download | only in data
      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 GlideFilmstripManager mGlideManager;
     37     private final ContentResolver mContentResolver;
     38     private final VideoDataFactory mVideoDataFactory;
     39 
     40     public VideoItemFactory(Context context, GlideFilmstripManager glideManager,
     41           ContentResolver contentResolver, VideoDataFactory videoDataFactory) {
     42         mContext = context;
     43         mGlideManager = glideManager;
     44         mContentResolver = contentResolver;
     45         mVideoDataFactory = videoDataFactory;
     46     }
     47 
     48     @Override
     49     public VideoItem get(Cursor c) {
     50         VideoItemData data = mVideoDataFactory.fromCursor(c);
     51         if (data != null) {
     52             return new VideoItem(mContext, mGlideManager, data, this);
     53         } else {
     54             Log.w(TAG, "skipping item with null data, returning null for item");
     55             return null;
     56         }
     57     }
     58 
     59     /** Query for a single video data item */
     60     public VideoItem get(Uri uri) {
     61         VideoItem newData = null;
     62         Cursor c = mContext.getContentResolver().query(uri, VideoDataQuery.QUERY_PROJECTION, null,
     63               null, null);
     64         if (c != null) {
     65             if (c.moveToFirst()) {
     66                 newData = get(c);
     67             }
     68             c.close();
     69         }
     70 
     71         return newData;
     72     }
     73 
     74     /** Query for all the video data items */
     75     public List<VideoItem> queryAll() {
     76         return queryAll(VideoDataQuery.CONTENT_URI,
     77               FilmstripItemBase.QUERY_ALL_MEDIA_ID);
     78     }
     79 
     80     /** Query for all the video data items */
     81     public List<VideoItem> queryAll(Uri uri, long lastId) {
     82         return FilmstripContentQueries
     83               .forCameraPath(mContentResolver, uri, VideoDataQuery.QUERY_PROJECTION, lastId,
     84                     QUERY_ORDER, this);
     85     }
     86 
     87     /** Query for a single data item */
     88     public VideoItem queryContentUri(Uri uri) {
     89         // TODO: Consider refactoring this, this approach may be slow.
     90         List<VideoItem> videos = queryAll(uri,
     91               FilmstripItemBase.QUERY_ALL_MEDIA_ID);
     92         if (videos.isEmpty()) {
     93             return null;
     94         }
     95         return videos.get(0);
     96     }
     97 }
     98