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.database.Cursor; 21 import android.net.Uri; 22 import android.provider.MediaStore; 23 24 import com.android.camera.Storage; 25 import com.android.camera.debug.Log; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * A set of queries for loading data from a content resolver. 32 */ 33 public class FilmstripContentQueries { 34 private static final Log.Tag TAG = new Log.Tag("LocalDataQuery"); 35 private static final String CAMERA_PATH = Storage.DIRECTORY + "%"; 36 private static final String SELECT_BY_PATH = MediaStore.MediaColumns.DATA + " LIKE ?"; 37 38 public interface CursorToFilmstripItemFactory<I extends FilmstripItem> { 39 40 /** 41 * Convert a cursor at a given location to a Local Data object. 42 * 43 * @param cursor the current cursor state. 44 * @return a LocalData object that represents the current cursor state. 45 */ 46 public I get(Cursor cursor); 47 } 48 49 /** 50 * Query the camera storage directory and convert it to local data 51 * objects. 52 * 53 * @param contentResolver to resolve content with. 54 * @param contentUri to resolve an item at 55 * @param projection the columns to extract 56 * @param minimumId the lower bound of results 57 * @param orderBy the order by clause 58 * @param factory an object that can turn a given cursor into a LocalData object. 59 * @return A list of LocalData objects that satisfy the query. 60 */ 61 public static <I extends FilmstripItem> List<I> forCameraPath(ContentResolver contentResolver, 62 Uri contentUri, String[] projection, long minimumId, String orderBy, 63 CursorToFilmstripItemFactory<I> factory) { 64 String selection = SELECT_BY_PATH + " AND " + MediaStore.MediaColumns._ID + " > ?"; 65 String[] selectionArgs = new String[] { CAMERA_PATH, Long.toString(minimumId) }; 66 67 Cursor cursor = contentResolver.query(contentUri, projection, 68 selection, selectionArgs, orderBy); 69 List<I> result = new ArrayList<>(); 70 if (cursor != null) { 71 while (cursor.moveToNext()) { 72 I item = factory.get(cursor); 73 if (item != null) { 74 result.add(item); 75 } else { 76 final int dataIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 77 Log.e(TAG, "Error loading data:" + cursor.getString(dataIndex)); 78 } 79 } 80 81 cursor.close(); 82 } 83 return result; 84 } 85 }