| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test.mock; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.content.AttributionSource; |
| 22 |
import android.content.ContentProvider; |
| 23 |
import android.content.ContentProviderOperation; |
| 24 |
import android.content.ContentProviderResult; |
| 25 |
import android.content.ContentResolver; |
| 26 |
import android.content.ContentValues; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.IContentProvider; |
| 29 |
import android.content.Intent; |
| 30 |
import android.content.OperationApplicationException; |
| 31 |
import android.content.pm.PathPermission; |
| 32 |
import android.content.pm.ProviderInfo; |
| 33 |
import android.content.res.AssetFileDescriptor; |
| 34 |
import android.database.Cursor; |
| 35 |
import android.net.Uri; |
| 36 |
import android.os.AsyncTask; |
| 37 |
import android.os.Binder; |
| 38 |
import android.os.Bundle; |
| 39 |
import android.os.IBinder; |
| 40 |
import android.os.ICancellationSignal; |
| 41 |
import android.os.ParcelFileDescriptor; |
| 42 |
import android.os.RemoteCallback; |
| 43 |
import android.os.RemoteException; |
| 44 |
|
| 45 |
import java.io.FileNotFoundException; |
| 46 |
import java.util.ArrayList; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class MockContentProvider extends ContentProvider { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private class InversionIContentProvider implements IContentProvider { |
| 63 |
@Override |
| 64 |
public ContentProviderResult[] applyBatch(@NonNull AttributionSource attributionSource, |
| 65 |
String authority, ArrayList<ContentProviderOperation> operations) |
| 66 |
throws RemoteException, OperationApplicationException { |
| 67 |
return MockContentProvider.this.applyBatch(authority, operations); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public int bulkInsert(@NonNull AttributionSource attributionSource, Uri url, |
| 72 |
ContentValues[] initialValues) throws RemoteException { |
| 73 |
return MockContentProvider.this.bulkInsert(url, initialValues); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public int delete(@NonNull AttributionSource attributionSource, Uri url, |
| 78 |
Bundle extras) throws RemoteException { |
| 79 |
return MockContentProvider.this.delete(url, extras); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public String getType(Uri url) throws RemoteException { |
| 84 |
return MockContentProvider.this.getType(url); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void getTypeAsync(Uri uri, RemoteCallback callback) throws RemoteException { |
| 89 |
MockContentProvider.this.getTypeAsync(uri, callback); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public Uri insert(@NonNull AttributionSource attributionSource, Uri url, |
| 94 |
ContentValues initialValues, Bundle extras) throws RemoteException { |
| 95 |
return MockContentProvider.this.insert(url, initialValues, extras); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public AssetFileDescriptor openAssetFile(@NonNull AttributionSource attributionSource, |
| 100 |
Uri url, String mode, ICancellationSignal signal) |
| 101 |
throws RemoteException, FileNotFoundException { |
| 102 |
return MockContentProvider.this.openAssetFile(url, mode); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public ParcelFileDescriptor openFile(@NonNull AttributionSource attributionSource, |
| 107 |
Uri url, String mode, ICancellationSignal signal) |
| 108 |
throws RemoteException, FileNotFoundException { |
| 109 |
return MockContentProvider.this.openFile(url, mode); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public Cursor query(@NonNull AttributionSource attributionSource, Uri url, |
| 114 |
@Nullable String[] projection, @Nullable Bundle queryArgs, |
| 115 |
@Nullable ICancellationSignal cancellationSignal) throws RemoteException { |
| 116 |
return MockContentProvider.this.query(url, projection, queryArgs, null); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public int update(@NonNull AttributionSource attributionSource, Uri url, |
| 121 |
ContentValues values, Bundle extras) throws RemoteException { |
| 122 |
return MockContentProvider.this.update(url, values, extras); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public Bundle call(@NonNull AttributionSource attributionSource, String authority, |
| 127 |
String method, String request, Bundle args) throws RemoteException { |
| 128 |
return MockContentProvider.this.call(authority, method, request, args); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public IBinder asBinder() { |
| 133 |
return MockContentProvider.this.getIContentProviderBinder(); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { |
| 138 |
return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public AssetFileDescriptor openTypedAssetFile( |
| 143 |
@NonNull AttributionSource attributionSource, Uri url, String mimeType, |
| 144 |
Bundle opts, ICancellationSignal signal) |
| 145 |
throws RemoteException, FileNotFoundException { |
| 146 |
return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public ICancellationSignal createCancellationSignal() throws RemoteException { |
| 151 |
return null; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public Uri canonicalize(@NonNull AttributionSource attributionSource, Uri uri) |
| 156 |
throws RemoteException { |
| 157 |
return MockContentProvider.this.canonicalize(uri); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void canonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, |
| 162 |
RemoteCallback callback) { |
| 163 |
MockContentProvider.this.canonicalizeAsync(uri, callback); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public Uri uncanonicalize(@NonNull AttributionSource attributionSource, Uri uri) |
| 168 |
throws RemoteException { |
| 169 |
return MockContentProvider.this.uncanonicalize(uri); |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void uncanonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, |
| 174 |
RemoteCallback callback) { |
| 175 |
MockContentProvider.this.uncanonicalizeAsync(uri, callback); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public boolean refresh(@NonNull AttributionSource attributionSource, Uri url, |
| 180 |
Bundle args, ICancellationSignal cancellationSignal) throws RemoteException { |
| 181 |
return MockContentProvider.this.refresh(url, args); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public int checkUriPermission(@NonNull AttributionSource attributionSource, Uri uri, |
| 186 |
int uid, int modeFlags) { |
| 187 |
return MockContentProvider.this.checkUriPermission(uri, uid, modeFlags); |
| 188 |
} |
| 189 |
} |
| 190 |
private final InversionIContentProvider mIContentProvider = new InversionIContentProvider(); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
protected MockContentProvider() { |
| 196 |
super(new MockContext(), "", "", null); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public MockContentProvider(Context context) { |
| 204 |
super(context, "", "", null); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public MockContentProvider(Context context, |
| 221 |
String readPermission, |
| 222 |
String writePermission, |
| 223 |
PathPermission[] pathPermissions) { |
| 224 |
super(context, readPermission, writePermission, pathPermissions); |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 229 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public String getType(Uri uri) { |
| 234 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
@SuppressWarnings("deprecation") |
| 241 |
public void getTypeAsync(Uri uri, RemoteCallback remoteCallback) { |
| 242 |
AsyncTask.SERIAL_EXECUTOR.execute(() -> { |
| 243 |
final Bundle bundle = new Bundle(); |
| 244 |
bundle.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri)); |
| 245 |
remoteCallback.sendResult(bundle); |
| 246 |
}); |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public Uri insert(Uri uri, ContentValues values) { |
| 251 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public boolean onCreate() { |
| 256 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 261 |
String sortOrder) { |
| 262 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 267 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@Override |
| 274 |
public int bulkInsert(Uri uri, ContentValues[] values) { |
| 275 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public void attachInfo(Context context, ProviderInfo info) { |
| 280 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { |
| 285 |
throw new UnsupportedOperationException("unimplemented mock method"); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@Override |
| 292 |
public Bundle call(String method, String request, Bundle args) { |
| 293 |
throw new UnsupportedOperationException("unimplemented mock method call"); |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public String[] getStreamTypes(Uri url, String mimeTypeFilter) { |
| 298 |
throw new UnsupportedOperationException("unimplemented mock method call"); |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) { |
| 303 |
throw new UnsupportedOperationException("unimplemented mock method call"); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
@SuppressWarnings("deprecation") |
| 310 |
public void canonicalizeAsync(Uri uri, RemoteCallback callback) { |
| 311 |
AsyncTask.SERIAL_EXECUTOR.execute(() -> { |
| 312 |
final Bundle bundle = new Bundle(); |
| 313 |
bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, canonicalize(uri)); |
| 314 |
callback.sendResult(bundle); |
| 315 |
}); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
@SuppressWarnings("deprecation") |
| 322 |
public void uncanonicalizeAsync(Uri uri, RemoteCallback callback) { |
| 323 |
AsyncTask.SERIAL_EXECUTOR.execute(() -> { |
| 324 |
final Bundle bundle = new Bundle(); |
| 325 |
bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, uncanonicalize(uri)); |
| 326 |
callback.sendResult(bundle); |
| 327 |
}); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public boolean refresh(Uri url, Bundle args) { |
| 334 |
throw new UnsupportedOperationException("unimplemented mock method call"); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
@Override |
| 339 |
public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) { |
| 340 |
throw new UnsupportedOperationException("unimplemented mock method call"); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
@Override |
| 351 |
public final IContentProvider getIContentProvider() { |
| 352 |
return mIContentProvider; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public IBinder getIContentProviderBinder() { |
| 359 |
return new Binder(); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
@Deprecated |
| 374 |
public static void attachInfoForTesting( |
| 375 |
ContentProvider provider, Context context, ProviderInfo providerInfo) { |
| 376 |
provider.attachInfoForTesting(context, providerInfo); |
| 377 |
} |
| 378 |
} |