1 /*
2  * Copyright (C) 2012 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.gallery3d.filtershow.provider;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.database.Cursor;
22 import android.database.MatrixCursor;
23 import android.net.Uri;
24 import android.os.ConditionVariable;
25 import android.os.ParcelFileDescriptor;
26 import android.provider.BaseColumns;
27 import android.provider.MediaStore;
28 import android.provider.OpenableColumns;
29 
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 
33 public class SharedImageProvider extends ContentProvider {
34 
35     private static final String LOGTAG = "SharedImageProvider";
36 
37     public static final String MIME_TYPE = "image/jpeg";
38     public static final String AUTHORITY = "com.android.gallery3d.filtershow.provider.SharedImageProvider";
39     public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/image");
40     public static final String PREPARE = "prepare";
41 
42     private final String[] mMimeStreamType = {
43             MIME_TYPE
44     };
45 
46     private static ConditionVariable mImageReadyCond = new ConditionVariable(false);
47 
48     @Override
delete(Uri arg0, String arg1, String[] arg2)49     public int delete(Uri arg0, String arg1, String[] arg2) {
50         return 0;
51     }
52 
53     @Override
getType(Uri arg0)54     public String getType(Uri arg0) {
55         return MIME_TYPE;
56     }
57 
58     @Override
getStreamTypes(Uri arg0, String mimeTypeFilter)59     public String[] getStreamTypes(Uri arg0, String mimeTypeFilter) {
60         return mMimeStreamType;
61     }
62 
63     @Override
insert(Uri uri, ContentValues values)64     public Uri insert(Uri uri, ContentValues values) {
65         if (values.containsKey(PREPARE)) {
66             if (values.getAsBoolean(PREPARE)) {
67                 mImageReadyCond.close();
68             } else {
69                 mImageReadyCond.open();
70             }
71         }
72         return null;
73     }
74 
75     @Override
update(Uri arg0, ContentValues arg1, String arg2, String[] arg3)76     public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
77         return 0;
78     }
79 
80     @Override
onCreate()81     public boolean onCreate() {
82         return true;
83     }
84 
85     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)86     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
87         String uriPath = uri.getLastPathSegment();
88         if (uriPath == null) {
89             return null;
90         }
91         if (projection == null) {
92             projection = new String[] {
93                     BaseColumns._ID,
94                     MediaStore.MediaColumns.DATA,
95                     OpenableColumns.DISPLAY_NAME,
96                     OpenableColumns.SIZE
97             };
98         }
99         // If we receive a query on display name or size,
100         // we should block until the image is ready
101         mImageReadyCond.block();
102 
103         File path = new File(uriPath);
104 
105         MatrixCursor cursor = new MatrixCursor(projection);
106         Object[] columns = new Object[projection.length];
107         for (int i = 0; i < projection.length; i++) {
108             if (projection[i].equalsIgnoreCase(BaseColumns._ID)) {
109                 columns[i] = 0;
110             } else if (projection[i].equalsIgnoreCase(MediaStore.MediaColumns.DATA)) {
111                 columns[i] = uri;
112             } else if (projection[i].equalsIgnoreCase(OpenableColumns.DISPLAY_NAME)) {
113                 columns[i] = path.getName();
114             } else if (projection[i].equalsIgnoreCase(OpenableColumns.SIZE)) {
115                 columns[i] = path.length();
116             }
117         }
118         cursor.addRow(columns);
119 
120         return cursor;
121     }
122 
123     @Override
openFile(Uri uri, String mode)124     public ParcelFileDescriptor openFile(Uri uri, String mode)
125             throws FileNotFoundException {
126         String uriPath = uri.getLastPathSegment();
127         if (uriPath == null) {
128             return null;
129         }
130         // Here we need to block until the image is ready
131         mImageReadyCond.block();
132         File path = new File(uriPath);
133         int imode = 0;
134         imode |= ParcelFileDescriptor.MODE_READ_ONLY;
135         return ParcelFileDescriptor.open(path, imode);
136     }
137 }
138