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.photos.data;
18 
19 import android.content.Context;
20 import android.content.CursorLoader;
21 import android.database.ContentObserver;
22 import android.database.Cursor;
23 import android.graphics.drawable.Drawable;
24 import android.net.Uri;
25 import android.provider.MediaStore;
26 import android.provider.MediaStore.Files;
27 import android.provider.MediaStore.Files.FileColumns;
28 
29 import com.android.photos.drawables.DataUriThumbnailDrawable;
30 import com.android.photos.shims.LoaderCompatShim;
31 
32 import java.util.ArrayList;
33 
34 public class PhotoSetLoader extends CursorLoader implements LoaderCompatShim<Cursor> {
35 
36     public static final String SUPPORTED_OPERATIONS = "supported_operations";
37 
38     private static final Uri CONTENT_URI = Files.getContentUri("external");
39     public static final String[] PROJECTION = new String[] {
40         FileColumns._ID,
41         FileColumns.DATA,
42         FileColumns.WIDTH,
43         FileColumns.HEIGHT,
44         FileColumns.DATE_ADDED,
45         FileColumns.MEDIA_TYPE,
46         SUPPORTED_OPERATIONS,
47     };
48 
49     private static final String SORT_ORDER = FileColumns.DATE_ADDED + " DESC";
50     private static final String SELECTION =
51             FileColumns.MEDIA_TYPE + " == " + FileColumns.MEDIA_TYPE_IMAGE
52             + " OR "
53             + FileColumns.MEDIA_TYPE + " == " + FileColumns.MEDIA_TYPE_VIDEO;
54 
55     public static final int INDEX_ID = 0;
56     public static final int INDEX_DATA = 1;
57     public static final int INDEX_WIDTH = 2;
58     public static final int INDEX_HEIGHT = 3;
59     public static final int INDEX_DATE_ADDED = 4;
60     public static final int INDEX_MEDIA_TYPE = 5;
61     public static final int INDEX_SUPPORTED_OPERATIONS = 6;
62 
63     private static final Uri GLOBAL_CONTENT_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/external/");
64     private final ContentObserver mGlobalObserver = new ForceLoadContentObserver();
65 
PhotoSetLoader(Context context)66     public PhotoSetLoader(Context context) {
67         super(context, CONTENT_URI, PROJECTION, SELECTION, null, SORT_ORDER);
68     }
69 
70     @Override
onStartLoading()71     protected void onStartLoading() {
72         super.onStartLoading();
73         getContext().getContentResolver().registerContentObserver(GLOBAL_CONTENT_URI,
74                 true, mGlobalObserver);
75     }
76 
77     @Override
onReset()78     protected void onReset() {
79         super.onReset();
80         getContext().getContentResolver().unregisterContentObserver(mGlobalObserver);
81     }
82 
83     @Override
drawableForItem(Cursor item, Drawable recycle)84     public Drawable drawableForItem(Cursor item, Drawable recycle) {
85         DataUriThumbnailDrawable drawable = null;
86         if (recycle == null || !(recycle instanceof DataUriThumbnailDrawable)) {
87             drawable = new DataUriThumbnailDrawable();
88         } else {
89             drawable = (DataUriThumbnailDrawable) recycle;
90         }
91         drawable.setImage(item.getString(INDEX_DATA),
92                 item.getInt(INDEX_WIDTH), item.getInt(INDEX_HEIGHT));
93         return drawable;
94     }
95 
96     @Override
uriForItem(Cursor item)97     public Uri uriForItem(Cursor item) {
98         return null;
99     }
100 
101     @Override
urisForSubItems(Cursor item)102     public ArrayList<Uri> urisForSubItems(Cursor item) {
103         return null;
104     }
105 
106     @Override
deleteItemWithPath(Object path)107     public void deleteItemWithPath(Object path) {
108 
109     }
110 
111     @Override
getPathForItem(Cursor item)112     public Object getPathForItem(Cursor item) {
113         return null;
114     }
115 }
116