blob: 1764e113d3d00cf8684b80a0f7b3304d6159d912 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package android.content;
18
19import android.content.pm.PackageManager;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070020import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.pm.ProviderInfo;
22import android.content.res.AssetFileDescriptor;
23import android.content.res.Configuration;
24import android.database.Cursor;
25import android.database.CursorToBulkCursorAdaptor;
26import android.database.CursorWindow;
27import android.database.IBulkCursor;
28import android.database.IContentObserver;
29import android.database.SQLException;
30import android.net.Uri;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070031import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Binder;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080033import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.ParcelFileDescriptor;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070035import android.os.Process;
Vasu Nori0c9e14a2010-08-04 13:31:48 -070036import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import java.io.File;
39import java.io.FileNotFoundException;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070040import java.io.IOException;
Fred Quintana03d94902009-05-22 14:23:31 -070041import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43/**
44 * Content providers are one of the primary building blocks of Android applications, providing
45 * content to applications. They encapsulate data and provide it to applications through the single
46 * {@link ContentResolver} interface. A content provider is only required if you need to share
47 * data between multiple applications. For example, the contacts data is used by multiple
48 * applications and must be stored in a content provider. If you don't need to share data amongst
49 * multiple applications you can use a database directly via
50 * {@link android.database.sqlite.SQLiteDatabase}.
51 *
52 * <p>For more information, read <a href="{@docRoot}guide/topics/providers/content-providers.html">Content
53 * Providers</a>.</p>
54 *
55 * <p>When a request is made via
56 * a {@link ContentResolver} the system inspects the authority of the given URI and passes the
57 * request to the content provider registered with the authority. The content provider can interpret
58 * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing
59 * URIs.</p>
60 *
61 * <p>The primary methods that need to be implemented are:
62 * <ul>
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070063 * <li>{@link #onCreate} which is called to initialize the provider</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 * <li>{@link #query} which returns data to the caller</li>
65 * <li>{@link #insert} which inserts new data into the content provider</li>
66 * <li>{@link #update} which updates existing data in the content provider</li>
67 * <li>{@link #delete} which deletes data from the content provider</li>
68 * <li>{@link #getType} which returns the MIME type of data in the content provider</li>
69 * </ul></p>
70 *
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070071 * <p class="caution">Data access methods (such as {@link #insert} and
72 * {@link #update}) may be called from many threads at once, and must be thread-safe.
73 * Other methods (such as {@link #onCreate}) are only called from the application
74 * main thread, and must avoid performing lengthy operations. See the method
75 * descriptions for their expected thread behavior.</p>
76 *
77 * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate
78 * ContentProvider instance, so subclasses don't have to worry about the details of
79 * cross-process calls.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 */
81public abstract class ContentProvider implements ComponentCallbacks {
Vasu Nori0c9e14a2010-08-04 13:31:48 -070082 private static final String TAG = "ContentProvider";
83
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090084 /*
85 * Note: if you add methods to ContentProvider, you must add similar methods to
86 * MockContentProvider.
87 */
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private Context mContext = null;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070090 private int mMyUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 private String mReadPermission;
92 private String mWritePermission;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070093 private PathPermission[] mPathPermissions;
Dianne Hackbornb424b632010-08-18 15:59:05 -070094 private boolean mExported;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96 private Transport mTransport = new Transport();
97
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070098 /**
99 * Construct a ContentProvider instance. Content providers must be
100 * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared
101 * in the manifest</a>, accessed with {@link ContentResolver}, and created
102 * automatically by the system, so applications usually do not create
103 * ContentProvider instances directly.
104 *
105 * <p>At construction time, the object is uninitialized, and most fields and
106 * methods are unavailable. Subclasses should initialize themselves in
107 * {@link #onCreate}, not the constructor.
108 *
109 * <p>Content providers are created on the application main thread at
110 * application launch time. The constructor must not perform lengthy
111 * operations, or application startup will be delayed.
112 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900113 public ContentProvider() {
114 }
115
116 /**
117 * Constructor just for mocking.
118 *
119 * @param context A Context object which should be some mock instance (like the
120 * instance of {@link android.test.mock.MockContext}).
121 * @param readPermission The read permision you want this instance should have in the
122 * test, which is available via {@link #getReadPermission()}.
123 * @param writePermission The write permission you want this instance should have
124 * in the test, which is available via {@link #getWritePermission()}.
125 * @param pathPermissions The PathPermissions you want this instance should have
126 * in the test, which is available via {@link #getPathPermissions()}.
127 * @hide
128 */
129 public ContentProvider(
130 Context context,
131 String readPermission,
132 String writePermission,
133 PathPermission[] pathPermissions) {
134 mContext = context;
135 mReadPermission = readPermission;
136 mWritePermission = writePermission;
137 mPathPermissions = pathPermissions;
138 }
139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
141 * Given an IContentProvider, try to coerce it back to the real
142 * ContentProvider object if it is running in the local process. This can
143 * be used if you know you are running in the same process as a provider,
144 * and want to get direct access to its implementation details. Most
145 * clients should not nor have a reason to use it.
146 *
147 * @param abstractInterface The ContentProvider interface that is to be
148 * coerced.
149 * @return If the IContentProvider is non-null and local, returns its actual
150 * ContentProvider instance. Otherwise returns null.
151 * @hide
152 */
153 public static ContentProvider coerceToLocalContentProvider(
154 IContentProvider abstractInterface) {
155 if (abstractInterface instanceof Transport) {
156 return ((Transport)abstractInterface).getContentProvider();
157 }
158 return null;
159 }
160
161 /**
162 * Binder object that deals with remoting.
163 *
164 * @hide
165 */
166 class Transport extends ContentProviderNative {
167 ContentProvider getContentProvider() {
168 return ContentProvider.this;
169 }
170
171 /**
172 * Remote version of a query, which returns an IBulkCursor. The bulk
173 * cursor should be wrapped with BulkCursorToCursorAdaptor before use.
174 */
175 public IBulkCursor bulkQuery(Uri uri, String[] projection,
176 String selection, String[] selectionArgs, String sortOrder,
177 IContentObserver observer, CursorWindow window) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700178 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 Cursor cursor = ContentProvider.this.query(uri, projection,
180 selection, selectionArgs, sortOrder);
181 if (cursor == null) {
182 return null;
183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 return new CursorToBulkCursorAdaptor(cursor, observer,
185 ContentProvider.this.getClass().getName(),
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700186 hasWritePermission(uri), window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188
189 public Cursor query(Uri uri, String[] projection,
190 String selection, String[] selectionArgs, String sortOrder) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700191 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return ContentProvider.this.query(uri, projection, selection,
193 selectionArgs, sortOrder);
194 }
195
196 public String getType(Uri uri) {
197 return ContentProvider.this.getType(uri);
198 }
199
200
201 public Uri insert(Uri uri, ContentValues initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700202 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 return ContentProvider.this.insert(uri, initialValues);
204 }
205
206 public int bulkInsert(Uri uri, ContentValues[] initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700207 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 return ContentProvider.this.bulkInsert(uri, initialValues);
209 }
210
Fred Quintana03d94902009-05-22 14:23:31 -0700211 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintana89437372009-05-15 15:10:40 -0700212 throws OperationApplicationException {
213 for (ContentProviderOperation operation : operations) {
214 if (operation.isReadOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700215 enforceReadPermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700216 }
217
218 if (operation.isWriteOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700219 enforceWritePermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700220 }
221 }
222 return ContentProvider.this.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -0700223 }
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 public int delete(Uri uri, String selection, String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700226 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 return ContentProvider.this.delete(uri, selection, selectionArgs);
228 }
229
230 public int update(Uri uri, ContentValues values, String selection,
231 String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700232 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 return ContentProvider.this.update(uri, values, selection, selectionArgs);
234 }
235
236 public ParcelFileDescriptor openFile(Uri uri, String mode)
237 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700238 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
239 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 return ContentProvider.this.openFile(uri, mode);
241 }
242
243 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
244 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700245 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
246 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 return ContentProvider.this.openAssetFile(uri, mode);
248 }
249
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800250 public Bundle call(String method, String arg, Bundle extras) {
251 return ContentProvider.this.call(method, arg, extras);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800252 }
253
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700254 @Override
255 public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
256 return ContentProvider.this.getStreamTypes(uri, mimeTypeFilter);
257 }
258
259 @Override
260 public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeType, Bundle opts)
261 throws FileNotFoundException {
262 enforceReadPermission(uri);
263 return ContentProvider.this.openTypedAssetFile(uri, mimeType, opts);
264 }
265
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700266 private void enforceReadPermission(Uri uri) {
267 final int uid = Binder.getCallingUid();
268 if (uid == mMyUid) {
269 return;
270 }
271
272 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 final String rperm = getReadPermission();
274 final int pid = Binder.getCallingPid();
Dianne Hackbornb424b632010-08-18 15:59:05 -0700275 if (mExported && (rperm == null
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700276 || context.checkPermission(rperm, pid, uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -0700277 == PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700278 return;
279 }
280
281 PathPermission[] pps = getPathPermissions();
282 if (pps != null) {
283 final String path = uri.getPath();
284 int i = pps.length;
285 while (i > 0) {
286 i--;
287 final PathPermission pp = pps[i];
288 final String pprperm = pp.getReadPermission();
289 if (pprperm != null && pp.match(path)) {
290 if (context.checkPermission(pprperm, pid, uid)
291 == PackageManager.PERMISSION_GRANTED) {
292 return;
293 }
294 }
295 }
296 }
297
298 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 Intent.FLAG_GRANT_READ_URI_PERMISSION)
300 == PackageManager.PERMISSION_GRANTED) {
301 return;
302 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 String msg = "Permission Denial: reading "
305 + ContentProvider.this.getClass().getName()
306 + " uri " + uri + " from pid=" + Binder.getCallingPid()
307 + ", uid=" + Binder.getCallingUid()
308 + " requires " + rperm;
309 throw new SecurityException(msg);
310 }
311
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700312 private boolean hasWritePermission(Uri uri) {
313 final int uid = Binder.getCallingUid();
314 if (uid == mMyUid) {
315 return true;
316 }
317
318 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 final String wperm = getWritePermission();
320 final int pid = Binder.getCallingPid();
Dianne Hackbornb424b632010-08-18 15:59:05 -0700321 if (mExported && (wperm == null
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700322 || context.checkPermission(wperm, pid, uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -0700323 == PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700324 return true;
325 }
326
327 PathPermission[] pps = getPathPermissions();
328 if (pps != null) {
329 final String path = uri.getPath();
330 int i = pps.length;
331 while (i > 0) {
332 i--;
333 final PathPermission pp = pps[i];
334 final String ppwperm = pp.getWritePermission();
335 if (ppwperm != null && pp.match(path)) {
336 if (context.checkPermission(ppwperm, pid, uid)
337 == PackageManager.PERMISSION_GRANTED) {
338 return true;
339 }
340 }
341 }
342 }
343
344 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
346 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700347 return true;
348 }
349
350 return false;
351 }
352
353 private void enforceWritePermission(Uri uri) {
354 if (hasWritePermission(uri)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 return;
356 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 String msg = "Permission Denial: writing "
359 + ContentProvider.this.getClass().getName()
360 + " uri " + uri + " from pid=" + Binder.getCallingPid()
361 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700362 + " requires " + getWritePermission();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 throw new SecurityException(msg);
364 }
365 }
366
367
368 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700369 * Retrieves the Context this provider is running in. Only available once
370 * {@link #onCreate} has been called -- this will return null in the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * constructor.
372 */
373 public final Context getContext() {
374 return mContext;
375 }
376
377 /**
378 * Change the permission required to read data from the content
379 * provider. This is normally set for you from its manifest information
380 * when the provider is first created.
381 *
382 * @param permission Name of the permission required for read-only access.
383 */
384 protected final void setReadPermission(String permission) {
385 mReadPermission = permission;
386 }
387
388 /**
389 * Return the name of the permission required for read-only access to
390 * this content provider. This method can be called from multiple
391 * threads, as described in
392 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
393 * Processes and Threads</a>.
394 */
395 public final String getReadPermission() {
396 return mReadPermission;
397 }
398
399 /**
400 * Change the permission required to read and write data in the content
401 * provider. This is normally set for you from its manifest information
402 * when the provider is first created.
403 *
404 * @param permission Name of the permission required for read/write access.
405 */
406 protected final void setWritePermission(String permission) {
407 mWritePermission = permission;
408 }
409
410 /**
411 * Return the name of the permission required for read/write access to
412 * this content provider. This method can be called from multiple
413 * threads, as described in
414 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
415 * Processes and Threads</a>.
416 */
417 public final String getWritePermission() {
418 return mWritePermission;
419 }
420
421 /**
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700422 * Change the path-based permission required to read and/or write data in
423 * the content provider. This is normally set for you from its manifest
424 * information when the provider is first created.
425 *
426 * @param permissions Array of path permission descriptions.
427 */
428 protected final void setPathPermissions(PathPermission[] permissions) {
429 mPathPermissions = permissions;
430 }
431
432 /**
433 * Return the path-based permissions required for read and/or write access to
434 * this content provider. This method can be called from multiple
435 * threads, as described in
436 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
437 * Processes and Threads</a>.
438 */
439 public final PathPermission[] getPathPermissions() {
440 return mPathPermissions;
441 }
442
443 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700444 * Implement this to initialize your content provider on startup.
445 * This method is called for all registered content providers on the
446 * application main thread at application launch time. It must not perform
447 * lengthy operations, or application startup will be delayed.
448 *
449 * <p>You should defer nontrivial initialization (such as opening,
450 * upgrading, and scanning databases) until the content provider is used
451 * (via {@link #query}, {@link #insert}, etc). Deferred initialization
452 * keeps application startup fast, avoids unnecessary work if the provider
453 * turns out not to be needed, and stops database errors (such as a full
454 * disk) from halting application launch.
455 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700456 * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper}
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700457 * is a helpful utility class that makes it easy to manage databases,
458 * and will automatically defer opening until first use. If you do use
459 * SQLiteOpenHelper, make sure to avoid calling
460 * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or
461 * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase}
462 * from this method. (Instead, override
463 * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the
464 * database when it is first opened.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 *
466 * @return true if the provider was successfully loaded, false otherwise
467 */
468 public abstract boolean onCreate();
469
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700470 /**
471 * {@inheritDoc}
472 * This method is always called on the application main thread, and must
473 * not perform lengthy operations.
474 *
475 * <p>The default content provider implementation does nothing.
476 * Override this method to take appropriate action.
477 * (Content providers do not usually care about things like screen
478 * orientation, but may want to know about locale changes.)
479 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 public void onConfigurationChanged(Configuration newConfig) {
481 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700482
483 /**
484 * {@inheritDoc}
485 * This method is always called on the application main thread, and must
486 * not perform lengthy operations.
487 *
488 * <p>The default content provider implementation does nothing.
489 * Subclasses may override this method to take appropriate action.
490 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public void onLowMemory() {
492 }
493
494 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700495 * Implement this to handle query requests from clients.
496 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
498 * Processes and Threads</a>.
499 * <p>
500 * Example client call:<p>
501 * <pre>// Request a specific record.
502 * Cursor managedCursor = managedQuery(
Alan Jones81a476f2009-05-21 12:32:17 +1000503 ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 projection, // Which columns to return.
505 null, // WHERE clause.
Alan Jones81a476f2009-05-21 12:32:17 +1000506 null, // WHERE clause value substitution
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 People.NAME + " ASC"); // Sort order.</pre>
508 * Example implementation:<p>
509 * <pre>// SQLiteQueryBuilder is a helper class that creates the
510 // proper SQL syntax for us.
511 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
512
513 // Set the table we're querying.
514 qBuilder.setTables(DATABASE_TABLE_NAME);
515
516 // If the query ends in a specific record number, we're
517 // being asked for a specific record, so set the
518 // WHERE clause in our query.
519 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
520 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
521 }
522
523 // Make the query.
524 Cursor c = qBuilder.query(mDb,
525 projection,
526 selection,
527 selectionArgs,
528 groupBy,
529 having,
530 sortOrder);
531 c.setNotificationUri(getContext().getContentResolver(), uri);
532 return c;</pre>
533 *
534 * @param uri The URI to query. This will be the full URI sent by the client;
Alan Jones81a476f2009-05-21 12:32:17 +1000535 * if the client is requesting a specific record, the URI will end in a record number
536 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
537 * that _id value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 * @param projection The list of columns to put into the cursor. If
539 * null all columns are included.
540 * @param selection A selection criteria to apply when filtering rows.
541 * If null then all rows are included.
Alan Jones81a476f2009-05-21 12:32:17 +1000542 * @param selectionArgs You may include ?s in selection, which will be replaced by
543 * the values from selectionArgs, in order that they appear in the selection.
544 * The values will be bound as Strings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 * @param sortOrder How the rows in the cursor should be sorted.
Alan Jones81a476f2009-05-21 12:32:17 +1000546 * If null then the provider is free to define the sort order.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 * @return a Cursor or null.
548 */
549 public abstract Cursor query(Uri uri, String[] projection,
550 String selection, String[] selectionArgs, String sortOrder);
551
Fred Quintana5bba6322009-10-05 14:21:12 -0700552 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700553 * Implement this to handle requests for the MIME type of the data at the
554 * given URI. The returned MIME type should start with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <code>vnd.android.cursor.item</code> for a single record,
556 * or <code>vnd.android.cursor.dir/</code> for multiple items.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700557 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
559 * Processes and Threads</a>.
560 *
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700561 * <p>Note that there are no permissions needed for an application to
562 * access this information; if your content provider requires read and/or
563 * write permissions, or is not exported, all applications can still call
564 * this method regardless of their access permissions. This allows them
565 * to retrieve the MIME type for a URI when dispatching intents.
566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 * @param uri the URI to query.
568 * @return a MIME type string, or null if there is no type.
569 */
570 public abstract String getType(Uri uri);
571
572 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700573 * Implement this to handle requests to insert a new row.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
575 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700576 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
578 * Processes and Threads</a>.
579 * @param uri The content:// URI of the insertion request.
580 * @param values A set of column_name/value pairs to add to the database.
581 * @return The URI for the newly inserted item.
582 */
583 public abstract Uri insert(Uri uri, ContentValues values);
584
585 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700586 * Override this to handle requests to insert a set of new rows, or the
587 * default implementation will iterate over the values and call
588 * {@link #insert} on each of them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
590 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700591 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
593 * Processes and Threads</a>.
594 *
595 * @param uri The content:// URI of the insertion request.
596 * @param values An array of sets of column_name/value pairs to add to the database.
597 * @return The number of values that were inserted.
598 */
599 public int bulkInsert(Uri uri, ContentValues[] values) {
600 int numValues = values.length;
601 for (int i = 0; i < numValues; i++) {
602 insert(uri, values[i]);
603 }
604 return numValues;
605 }
606
607 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700608 * Implement this to handle requests to delete one or more rows.
609 * The implementation should apply the selection clause when performing
610 * deletion, allowing the operation to affect multiple rows in a directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()}
612 * after deleting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700613 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
615 * Processes and Threads</a>.
616 *
617 * <p>The implementation is responsible for parsing out a row ID at the end
618 * of the URI, if a specific row is being deleted. That is, the client would
619 * pass in <code>content://contacts/people/22</code> and the implementation is
620 * responsible for parsing the record number (22) when creating a SQL statement.
621 *
622 * @param uri The full URI to query, including a row ID (if a specific record is requested).
623 * @param selection An optional restriction to apply to rows when deleting.
624 * @return The number of rows affected.
625 * @throws SQLException
626 */
627 public abstract int delete(Uri uri, String selection, String[] selectionArgs);
628
629 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700630 * Implement this to handle requests to update one or more rows.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700631 * The implementation should update all rows matching the selection
632 * to set the columns according to the provided values map.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
634 * after updating.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700635 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
637 * Processes and Threads</a>.
638 *
639 * @param uri The URI to query. This can potentially have a record ID if this
640 * is an update request for a specific record.
641 * @param values A Bundle mapping from column names to new column values (NULL is a
642 * valid value).
643 * @param selection An optional filter to match rows to update.
644 * @return the number of rows affected.
645 */
646 public abstract int update(Uri uri, ContentValues values, String selection,
647 String[] selectionArgs);
648
649 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700650 * Override this to handle requests to open a file blob.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700651 * The default implementation always throws {@link FileNotFoundException}.
652 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
654 * Processes and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700655 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700656 * <p>This method returns a ParcelFileDescriptor, which is returned directly
657 * to the caller. This way large data (such as images and documents) can be
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700658 * returned without copying the content.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 *
660 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
661 * their responsibility to close it when done. That is, the implementation
662 * of this method should create a new ParcelFileDescriptor for each call.
663 *
664 * @param uri The URI whose file is to be opened.
665 * @param mode Access mode for the file. May be "r" for read-only access,
666 * "rw" for read and write access, or "rwt" for read and write access
667 * that truncates any existing file.
668 *
669 * @return Returns a new ParcelFileDescriptor which you can use to access
670 * the file.
671 *
672 * @throws FileNotFoundException Throws FileNotFoundException if there is
673 * no file associated with the given URI or the mode is invalid.
674 * @throws SecurityException Throws SecurityException if the caller does
675 * not have permission to access the file.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * @see #openAssetFile(Uri, String)
678 * @see #openFileHelper(Uri, String)
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700679 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 public ParcelFileDescriptor openFile(Uri uri, String mode)
681 throws FileNotFoundException {
682 throw new FileNotFoundException("No files supported by provider at "
683 + uri);
684 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 /**
687 * This is like {@link #openFile}, but can be implemented by providers
688 * that need to be able to return sub-sections of files, often assets
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700689 * inside of their .apk.
690 * This method can be called from multiple threads, as described in
691 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
692 * Processes and Threads</a>.
693 *
694 * <p>If you implement this, your clients must be able to deal with such
Dan Egnor17876aa2010-07-28 12:28:04 -0700695 * file slices, either directly with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700696 * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
698 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
699 * methods.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700700 *
701 * <p class="note">If you are implementing this to return a full file, you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 * should create the AssetFileDescriptor with
703 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700704 * applications that can not handle sub-sections of files.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 *
706 * @param uri The URI whose file is to be opened.
707 * @param mode Access mode for the file. May be "r" for read-only access,
708 * "w" for write-only access (erasing whatever data is currently in
709 * the file), "wa" for write-only access to append to any existing data,
710 * "rw" for read and write access on any existing data, and "rwt" for read
711 * and write access that truncates any existing file.
712 *
713 * @return Returns a new AssetFileDescriptor which you can use to access
714 * the file.
715 *
716 * @throws FileNotFoundException Throws FileNotFoundException if there is
717 * no file associated with the given URI or the mode is invalid.
718 * @throws SecurityException Throws SecurityException if the caller does
719 * not have permission to access the file.
720 *
721 * @see #openFile(Uri, String)
722 * @see #openFileHelper(Uri, String)
723 */
724 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
725 throws FileNotFoundException {
726 ParcelFileDescriptor fd = openFile(uri, mode);
727 return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
728 }
729
730 /**
731 * Convenience for subclasses that wish to implement {@link #openFile}
732 * by looking up a column named "_data" at the given URI.
733 *
734 * @param uri The URI to be opened.
735 * @param mode The file mode. May be "r" for read-only access,
736 * "w" for write-only access (erasing whatever data is currently in
737 * the file), "wa" for write-only access to append to any existing data,
738 * "rw" for read and write access on any existing data, and "rwt" for read
739 * and write access that truncates any existing file.
740 *
741 * @return Returns a new ParcelFileDescriptor that can be used by the
742 * client to access the file.
743 */
744 protected final ParcelFileDescriptor openFileHelper(Uri uri,
745 String mode) throws FileNotFoundException {
746 Cursor c = query(uri, new String[]{"_data"}, null, null, null);
747 int count = (c != null) ? c.getCount() : 0;
748 if (count != 1) {
749 // If there is not exactly one result, throw an appropriate
750 // exception.
751 if (c != null) {
752 c.close();
753 }
754 if (count == 0) {
755 throw new FileNotFoundException("No entry for " + uri);
756 }
757 throw new FileNotFoundException("Multiple items at " + uri);
758 }
759
760 c.moveToFirst();
761 int i = c.getColumnIndex("_data");
762 String path = (i >= 0 ? c.getString(i) : null);
763 c.close();
764 if (path == null) {
765 throw new FileNotFoundException("Column _data not found.");
766 }
767
768 int modeBits = ContentResolver.modeToMode(uri, mode);
769 return ParcelFileDescriptor.open(new File(path), modeBits);
770 }
771
772 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700773 * Called by a client to determine the types of data streams that this
774 * content provider supports for the given URI. The default implementation
775 * returns null, meaning no types. If your content provider stores data
776 * of a particular type, return that MIME type if it matches the given
777 * mimeTypeFilter. If it can perform type conversions, return an array
778 * of all supported MIME types that match mimeTypeFilter.
779 *
780 * @param uri The data in the content provider being queried.
781 * @param mimeTypeFilter The type of data the client desires. May be
782 * a pattern, such as *\/* to retrieve all possible data types.
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700783 * @return Returns null if there are no possible data streams for the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700784 * given mimeTypeFilter. Otherwise returns an array of all available
785 * concrete MIME types.
786 *
787 * @see #getType(Uri)
788 * @see #openTypedAssetFile(Uri, String, Bundle)
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700789 * @see ClipDescription#compareMimeTypes(String, String)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700790 */
791 public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
792 return null;
793 }
794
795 /**
796 * Called by a client to open a read-only stream containing data of a
797 * particular MIME type. This is like {@link #openAssetFile(Uri, String)},
798 * except the file can only be read-only and the content provider may
799 * perform data conversions to generate data of the desired type.
800 *
801 * <p>The default implementation compares the given mimeType against the
802 * result of {@link #getType(Uri)} and, if the match, simple calls
803 * {@link #openAssetFile(Uri, String)}.
804 *
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700805 * <p>See {@link ClipData} for examples of the use and implementation
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700806 * of this method.
807 *
808 * @param uri The data in the content provider being queried.
809 * @param mimeTypeFilter The type of data the client desires. May be
810 * a pattern, such as *\/*, if the caller does not have specific type
811 * requirements; in this case the content provider will pick its best
812 * type matching the pattern.
813 * @param opts Additional options from the client. The definitions of
814 * these are specific to the content provider being called.
815 *
816 * @return Returns a new AssetFileDescriptor from which the client can
817 * read data of the desired type.
818 *
819 * @throws FileNotFoundException Throws FileNotFoundException if there is
820 * no file associated with the given URI or the mode is invalid.
821 * @throws SecurityException Throws SecurityException if the caller does
822 * not have permission to access the data.
823 * @throws IllegalArgumentException Throws IllegalArgumentException if the
824 * content provider does not support the requested MIME type.
825 *
826 * @see #getStreamTypes(Uri, String)
827 * @see #openAssetFile(Uri, String)
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700828 * @see ClipDescription#compareMimeTypes(String, String)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700829 */
830 public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts)
831 throws FileNotFoundException {
Dianne Hackborn02dfd262010-08-13 12:34:58 -0700832 if ("*/*".equals(mimeTypeFilter)) {
833 // If they can take anything, the untyped open call is good enough.
834 return openAssetFile(uri, "r");
835 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700836 String baseType = getType(uri);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700837 if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) {
Dianne Hackborn02dfd262010-08-13 12:34:58 -0700838 // Use old untyped open call if this provider has a type for this
839 // URI and it matches the request.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700840 return openAssetFile(uri, "r");
841 }
842 throw new FileNotFoundException("Can't open " + uri + " as type " + mimeTypeFilter);
843 }
844
845 /**
846 * Interface to write a stream of data to a pipe. Use with
847 * {@link ContentProvider#openPipeHelper}.
848 */
849 public interface PipeDataWriter<T> {
850 /**
851 * Called from a background thread to stream data out to a pipe.
852 * Note that the pipe is blocking, so this thread can block on
853 * writes for an arbitrary amount of time if the client is slow
854 * at reading.
855 *
856 * @param output The pipe where data should be written. This will be
857 * closed for you upon returning from this function.
858 * @param uri The URI whose data is to be written.
859 * @param mimeType The desired type of data to be written.
860 * @param opts Options supplied by caller.
861 * @param args Your own custom arguments.
862 */
863 public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType,
864 Bundle opts, T args);
865 }
866
867 /**
868 * A helper function for implementing {@link #openTypedAssetFile}, for
869 * creating a data pipe and background thread allowing you to stream
870 * generated data back to the client. This function returns a new
871 * ParcelFileDescriptor that should be returned to the caller (the caller
872 * is responsible for closing it).
873 *
874 * @param uri The URI whose data is to be written.
875 * @param mimeType The desired type of data to be written.
876 * @param opts Options supplied by caller.
877 * @param args Your own custom arguments.
878 * @param func Interface implementing the function that will actually
879 * stream the data.
880 * @return Returns a new ParcelFileDescriptor holding the read side of
881 * the pipe. This should be returned to the caller for reading; the caller
882 * is responsible for closing it when done.
883 */
884 public <T> ParcelFileDescriptor openPipeHelper(final Uri uri, final String mimeType,
885 final Bundle opts, final T args, final PipeDataWriter<T> func)
886 throws FileNotFoundException {
887 try {
888 final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
889
890 AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {
891 @Override
892 protected Object doInBackground(Object... params) {
893 func.writeDataToPipe(fds[1], uri, mimeType, opts, args);
894 try {
895 fds[1].close();
896 } catch (IOException e) {
897 Log.w(TAG, "Failure closing pipe", e);
898 }
899 return null;
900 }
901 };
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800902 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700903
904 return fds[0];
905 } catch (IOException e) {
906 throw new FileNotFoundException("failure making pipe");
907 }
908 }
909
910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 * Returns true if this instance is a temporary content provider.
912 * @return true if this instance is a temporary content provider
913 */
914 protected boolean isTemporary() {
915 return false;
916 }
917
918 /**
919 * Returns the Binder object for this provider.
920 *
921 * @return the Binder object for this provider
922 * @hide
923 */
924 public IContentProvider getIContentProvider() {
925 return mTransport;
926 }
927
928 /**
929 * After being instantiated, this is called to tell the content provider
930 * about itself.
931 *
932 * @param context The context this provider is running in
933 * @param info Registered information about this content provider
934 */
935 public void attachInfo(Context context, ProviderInfo info) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700936 /*
937 * We may be using AsyncTask from binder threads. Make it init here
938 * so its static handler is on the main thread.
939 */
940 AsyncTask.init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941
942 /*
943 * Only allow it to be set once, so after the content service gives
944 * this to us clients can't change it.
945 */
946 if (mContext == null) {
947 mContext = context;
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700948 mMyUid = Process.myUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 if (info != null) {
950 setReadPermission(info.readPermission);
951 setWritePermission(info.writePermission);
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700952 setPathPermissions(info.pathPermissions);
Dianne Hackbornb424b632010-08-18 15:59:05 -0700953 mExported = info.exported;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955 ContentProvider.this.onCreate();
956 }
957 }
Fred Quintanace31b232009-05-04 16:01:15 -0700958
959 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700960 * Override this to handle requests to perform a batch of operations, or the
961 * default implementation will iterate over the operations and call
962 * {@link ContentProviderOperation#apply} on each of them.
963 * If all calls to {@link ContentProviderOperation#apply} succeed
964 * then a {@link ContentProviderResult} array with as many
965 * elements as there were operations will be returned. If any of the calls
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700966 * fail, it is up to the implementation how many of the others take effect.
967 * This method can be called from multiple threads, as described in
968 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
969 * Processes and Threads</a>.
970 *
Fred Quintanace31b232009-05-04 16:01:15 -0700971 * @param operations the operations to apply
972 * @return the results of the applications
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700973 * @throws OperationApplicationException thrown if any operation fails.
974 * @see ContentProviderOperation#apply
Fred Quintanace31b232009-05-04 16:01:15 -0700975 */
Fred Quintana03d94902009-05-22 14:23:31 -0700976 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintanace31b232009-05-04 16:01:15 -0700977 throws OperationApplicationException {
Fred Quintana03d94902009-05-22 14:23:31 -0700978 final int numOperations = operations.size();
979 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
980 for (int i = 0; i < numOperations; i++) {
981 results[i] = operations.get(i).apply(this, results, i);
Fred Quintanace31b232009-05-04 16:01:15 -0700982 }
983 return results;
984 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800985
986 /**
Manuel Roman2c96a0c2010-08-05 16:39:49 -0700987 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800988 * interfaces that are cheaper and/or unnatural for a table-like
989 * model.
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800990 *
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800991 * @param method method name to call. Opaque to framework, but should not be null.
992 * @param arg provider-defined String argument. May be null.
993 * @param extras provider-defined Bundle argument. May be null.
994 * @return provider-defined return value. May be null. Null is also
995 * the default for providers which don't implement any call methods.
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800996 */
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800997 public Bundle call(String method, String arg, Bundle extras) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800998 return null;
999 }
Vasu Nori0c9e14a2010-08-04 13:31:48 -07001000
1001 /**
Manuel Roman2c96a0c2010-08-05 16:39:49 -07001002 * Implement this to shut down the ContentProvider instance. You can then
1003 * invoke this method in unit tests.
1004 *
Vasu Nori0c9e14a2010-08-04 13:31:48 -07001005 * <p>
Manuel Roman2c96a0c2010-08-05 16:39:49 -07001006 * Android normally handles ContentProvider startup and shutdown
1007 * automatically. You do not need to start up or shut down a
1008 * ContentProvider. When you invoke a test method on a ContentProvider,
1009 * however, a ContentProvider instance is started and keeps running after
1010 * the test finishes, even if a succeeding test instantiates another
1011 * ContentProvider. A conflict develops because the two instances are
1012 * usually running against the same underlying data source (for example, an
1013 * sqlite database).
1014 * </p>
Vasu Nori0c9e14a2010-08-04 13:31:48 -07001015 * <p>
Manuel Roman2c96a0c2010-08-05 16:39:49 -07001016 * Implementing shutDown() avoids this conflict by providing a way to
1017 * terminate the ContentProvider. This method can also prevent memory leaks
1018 * from multiple instantiations of the ContentProvider, and it can ensure
1019 * unit test isolation by allowing you to completely clean up the test
1020 * fixture before moving on to the next test.
1021 * </p>
Vasu Nori0c9e14a2010-08-04 13:31:48 -07001022 */
1023 public void shutdown() {
1024 Log.w(TAG, "implement ContentProvider shutdown() to make sure all database " +
1025 "connections are gracefully shutdown");
1026 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08001027}