1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; 4 5 import android.content.ContentProvider; 6 import android.content.ContentProviderClient; 7 import android.content.ContentProviderOperation; 8 import android.content.ContentProviderResult; 9 import android.content.ContentValues; 10 import android.content.OperationApplicationException; 11 import android.content.res.AssetFileDescriptor; 12 import android.database.Cursor; 13 import android.net.Uri; 14 import android.os.Bundle; 15 import android.os.CancellationSignal; 16 import android.os.ParcelFileDescriptor; 17 import android.os.RemoteException; 18 import java.io.FileNotFoundException; 19 import java.util.ArrayList; 20 import org.robolectric.annotation.Implementation; 21 import org.robolectric.annotation.Implements; 22 import org.robolectric.annotation.RealObject; 23 import org.robolectric.util.ReflectionHelpers; 24 25 @Implements(ContentProviderClient.class) 26 public class ShadowContentProviderClient { 27 @RealObject private ContentProviderClient realContentProviderClient; 28 29 private boolean released; 30 private ContentProvider provider; 31 32 @Implementation(minSdk = JELLY_BEAN_MR1) call(String method, String arg, Bundle extras)33 protected Bundle call(String method, String arg, Bundle extras) throws RemoteException { 34 return provider.call(method, arg, extras); 35 } 36 37 @Implementation getType(Uri uri)38 protected String getType(Uri uri) throws RemoteException { 39 return provider.getType(uri); 40 } 41 42 @Implementation getStreamTypes(Uri uri, String mimeTypeFilter)43 protected String[] getStreamTypes(Uri uri, String mimeTypeFilter) { 44 return provider.getStreamTypes(uri, mimeTypeFilter); 45 } 46 47 @Implementation query( Uri url, String[] projection, String selection, String[] selectionArgs, String sortOrder)48 protected Cursor query( 49 Uri url, String[] projection, String selection, String[] selectionArgs, String sortOrder) 50 throws RemoteException { 51 return provider.query(url, projection, selection, selectionArgs, sortOrder); 52 } 53 54 @Implementation query( Uri url, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)55 protected Cursor query( 56 Uri url, 57 String[] projection, 58 String selection, 59 String[] selectionArgs, 60 String sortOrder, 61 CancellationSignal cancellationSignal) 62 throws RemoteException { 63 return provider.query(url, projection, selection, selectionArgs, sortOrder, cancellationSignal); 64 } 65 66 @Implementation insert(Uri url, ContentValues initialValues)67 protected Uri insert(Uri url, ContentValues initialValues) throws RemoteException { 68 return provider.insert(url, initialValues); 69 } 70 71 @Implementation bulkInsert(Uri url, ContentValues[] initialValues)72 protected int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException { 73 return provider.bulkInsert(url, initialValues); 74 } 75 76 @Implementation delete(Uri url, String selection, String[] selectionArgs)77 protected int delete(Uri url, String selection, String[] selectionArgs) throws RemoteException { 78 return provider.delete(url, selection, selectionArgs); 79 } 80 81 @Implementation update(Uri url, ContentValues values, String selection, String[] selectionArgs)82 protected int update(Uri url, ContentValues values, String selection, String[] selectionArgs) 83 throws RemoteException { 84 return provider.update(url, values, selection, selectionArgs); 85 } 86 87 @Implementation openFile(Uri url, String mode)88 protected ParcelFileDescriptor openFile(Uri url, String mode) 89 throws RemoteException, FileNotFoundException { 90 return provider.openFile(url, mode); 91 } 92 93 @Implementation openAssetFile(Uri url, String mode)94 protected AssetFileDescriptor openAssetFile(Uri url, String mode) 95 throws RemoteException, FileNotFoundException { 96 return provider.openAssetFile(url, mode); 97 } 98 99 @Implementation openTypedAssetFileDescriptor( Uri uri, String mimeType, Bundle opts)100 protected final AssetFileDescriptor openTypedAssetFileDescriptor( 101 Uri uri, String mimeType, Bundle opts) throws RemoteException, FileNotFoundException { 102 return provider.openTypedAssetFile(uri, mimeType, opts); 103 } 104 105 @Implementation applyBatch(ArrayList<ContentProviderOperation> operations)106 protected ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) 107 throws RemoteException, OperationApplicationException { 108 return provider.applyBatch(operations); 109 } 110 111 @Implementation release()112 protected boolean release() { 113 synchronized (this) { 114 if (released) { 115 throw new IllegalStateException("Already released"); 116 } 117 released = true; 118 } 119 return true; 120 } 121 122 @Implementation getLocalContentProvider()123 protected ContentProvider getLocalContentProvider() { 124 return ContentProvider.coerceToLocalContentProvider(provider.getIContentProvider()); 125 } 126 isStable()127 public boolean isStable() { 128 return ReflectionHelpers.getField(realContentProviderClient, "mStable"); 129 } 130 isReleased()131 public boolean isReleased() { 132 return released; 133 } 134 setContentProvider(ContentProvider provider)135 void setContentProvider(ContentProvider provider) { 136 this.provider = provider; 137 } 138 } 139