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