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