blob: 116ca48587747651c22c50cd1c8717dc04d09ec1 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.database.SQLException;
26import android.net.Uri;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070027import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Binder;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080029import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.ParcelFileDescriptor;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070031import android.os.Process;
Vasu Nori0c9e14a2010-08-04 13:31:48 -070032import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34import java.io.File;
Marco Nelissen18cb2872011-11-15 11:19:53 -080035import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import java.io.FileNotFoundException;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070037import java.io.IOException;
Marco Nelissen18cb2872011-11-15 11:19:53 -080038import java.io.PrintWriter;
Fred Quintana03d94902009-05-22 14:23:31 -070039import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41/**
42 * Content providers are one of the primary building blocks of Android applications, providing
43 * content to applications. They encapsulate data and provide it to applications through the single
44 * {@link ContentResolver} interface. A content provider is only required if you need to share
45 * data between multiple applications. For example, the contacts data is used by multiple
46 * applications and must be stored in a content provider. If you don't need to share data amongst
47 * multiple applications you can use a database directly via
48 * {@link android.database.sqlite.SQLiteDatabase}.
49 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * <p>When a request is made via
51 * a {@link ContentResolver} the system inspects the authority of the given URI and passes the
52 * request to the content provider registered with the authority. The content provider can interpret
53 * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing
54 * URIs.</p>
55 *
56 * <p>The primary methods that need to be implemented are:
57 * <ul>
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070058 * <li>{@link #onCreate} which is called to initialize the provider</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 * <li>{@link #query} which returns data to the caller</li>
60 * <li>{@link #insert} which inserts new data into the content provider</li>
61 * <li>{@link #update} which updates existing data in the content provider</li>
62 * <li>{@link #delete} which deletes data from the content provider</li>
63 * <li>{@link #getType} which returns the MIME type of data in the content provider</li>
64 * </ul></p>
65 *
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070066 * <p class="caution">Data access methods (such as {@link #insert} and
67 * {@link #update}) may be called from many threads at once, and must be thread-safe.
68 * Other methods (such as {@link #onCreate}) are only called from the application
69 * main thread, and must avoid performing lengthy operations. See the method
70 * descriptions for their expected thread behavior.</p>
71 *
72 * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate
73 * ContentProvider instance, so subclasses don't have to worry about the details of
74 * cross-process calls.</p>
Joe Fernandez558459f2011-10-13 16:47:36 -070075 *
76 * <div class="special reference">
77 * <h3>Developer Guides</h3>
78 * <p>For more information about using content providers, read the
79 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
80 * developer guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 */
Dianne Hackbornc68c9132011-07-29 01:25:18 -070082public abstract class ContentProvider implements ComponentCallbacks2 {
Vasu Nori0c9e14a2010-08-04 13:31:48 -070083 private static final String TAG = "ContentProvider";
84
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090085 /*
86 * Note: if you add methods to ContentProvider, you must add similar methods to
87 * MockContentProvider.
88 */
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 private Context mContext = null;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070091 private int mMyUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private String mReadPermission;
93 private String mWritePermission;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070094 private PathPermission[] mPathPermissions;
Dianne Hackbornb424b632010-08-18 15:59:05 -070095 private boolean mExported;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 private Transport mTransport = new Transport();
98
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070099 /**
100 * Construct a ContentProvider instance. Content providers must be
101 * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared
102 * in the manifest</a>, accessed with {@link ContentResolver}, and created
103 * automatically by the system, so applications usually do not create
104 * ContentProvider instances directly.
105 *
106 * <p>At construction time, the object is uninitialized, and most fields and
107 * methods are unavailable. Subclasses should initialize themselves in
108 * {@link #onCreate}, not the constructor.
109 *
110 * <p>Content providers are created on the application main thread at
111 * application launch time. The constructor must not perform lengthy
112 * operations, or application startup will be delayed.
113 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900114 public ContentProvider() {
115 }
116
117 /**
118 * Constructor just for mocking.
119 *
120 * @param context A Context object which should be some mock instance (like the
121 * instance of {@link android.test.mock.MockContext}).
122 * @param readPermission The read permision you want this instance should have in the
123 * test, which is available via {@link #getReadPermission()}.
124 * @param writePermission The write permission you want this instance should have
125 * in the test, which is available via {@link #getWritePermission()}.
126 * @param pathPermissions The PathPermissions you want this instance should have
127 * in the test, which is available via {@link #getPathPermissions()}.
128 * @hide
129 */
130 public ContentProvider(
131 Context context,
132 String readPermission,
133 String writePermission,
134 PathPermission[] pathPermissions) {
135 mContext = context;
136 mReadPermission = readPermission;
137 mWritePermission = writePermission;
138 mPathPermissions = pathPermissions;
139 }
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
142 * Given an IContentProvider, try to coerce it back to the real
143 * ContentProvider object if it is running in the local process. This can
144 * be used if you know you are running in the same process as a provider,
145 * and want to get direct access to its implementation details. Most
146 * clients should not nor have a reason to use it.
147 *
148 * @param abstractInterface The ContentProvider interface that is to be
149 * coerced.
150 * @return If the IContentProvider is non-null and local, returns its actual
151 * ContentProvider instance. Otherwise returns null.
152 * @hide
153 */
154 public static ContentProvider coerceToLocalContentProvider(
155 IContentProvider abstractInterface) {
156 if (abstractInterface instanceof Transport) {
157 return ((Transport)abstractInterface).getContentProvider();
158 }
159 return null;
160 }
161
162 /**
163 * Binder object that deals with remoting.
164 *
165 * @hide
166 */
167 class Transport extends ContentProviderNative {
168 ContentProvider getContentProvider() {
169 return ContentProvider.this;
170 }
171
Jeff Brownd2183652011-10-09 12:39:53 -0700172 @Override
173 public String getProviderName() {
174 return getContentProvider().getClass().getName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 }
176
177 public Cursor query(Uri uri, String[] projection,
178 String selection, String[] selectionArgs, String sortOrder) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700179 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 return ContentProvider.this.query(uri, projection, selection,
181 selectionArgs, sortOrder);
182 }
183
184 public String getType(Uri uri) {
185 return ContentProvider.this.getType(uri);
186 }
187
188
189 public Uri insert(Uri uri, ContentValues initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700190 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return ContentProvider.this.insert(uri, initialValues);
192 }
193
194 public int bulkInsert(Uri uri, ContentValues[] initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700195 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 return ContentProvider.this.bulkInsert(uri, initialValues);
197 }
198
Fred Quintana03d94902009-05-22 14:23:31 -0700199 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintana89437372009-05-15 15:10:40 -0700200 throws OperationApplicationException {
201 for (ContentProviderOperation operation : operations) {
202 if (operation.isReadOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700203 enforceReadPermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700204 }
205
206 if (operation.isWriteOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700207 enforceWritePermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700208 }
209 }
210 return ContentProvider.this.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -0700211 }
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public int delete(Uri uri, String selection, String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700214 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 return ContentProvider.this.delete(uri, selection, selectionArgs);
216 }
217
218 public int update(Uri uri, ContentValues values, String selection,
219 String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700220 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 return ContentProvider.this.update(uri, values, selection, selectionArgs);
222 }
223
224 public ParcelFileDescriptor openFile(Uri uri, String mode)
225 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700226 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
227 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 return ContentProvider.this.openFile(uri, mode);
229 }
230
231 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
232 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700233 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
234 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 return ContentProvider.this.openAssetFile(uri, mode);
236 }
237
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800238 public Bundle call(String method, String arg, Bundle extras) {
239 return ContentProvider.this.call(method, arg, extras);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800240 }
241
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700242 @Override
243 public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
244 return ContentProvider.this.getStreamTypes(uri, mimeTypeFilter);
245 }
246
247 @Override
248 public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeType, Bundle opts)
249 throws FileNotFoundException {
250 enforceReadPermission(uri);
251 return ContentProvider.this.openTypedAssetFile(uri, mimeType, opts);
252 }
253
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700254 private void enforceReadPermission(Uri uri) {
255 final int uid = Binder.getCallingUid();
256 if (uid == mMyUid) {
257 return;
258 }
259
260 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 final String rperm = getReadPermission();
262 final int pid = Binder.getCallingPid();
Dianne Hackbornb424b632010-08-18 15:59:05 -0700263 if (mExported && (rperm == null
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700264 || context.checkPermission(rperm, pid, uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -0700265 == PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700266 return;
267 }
268
269 PathPermission[] pps = getPathPermissions();
270 if (pps != null) {
271 final String path = uri.getPath();
272 int i = pps.length;
273 while (i > 0) {
274 i--;
275 final PathPermission pp = pps[i];
276 final String pprperm = pp.getReadPermission();
277 if (pprperm != null && pp.match(path)) {
278 if (context.checkPermission(pprperm, pid, uid)
279 == PackageManager.PERMISSION_GRANTED) {
280 return;
281 }
282 }
283 }
284 }
285
286 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 Intent.FLAG_GRANT_READ_URI_PERMISSION)
288 == PackageManager.PERMISSION_GRANTED) {
289 return;
290 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 String msg = "Permission Denial: reading "
293 + ContentProvider.this.getClass().getName()
294 + " uri " + uri + " from pid=" + Binder.getCallingPid()
295 + ", uid=" + Binder.getCallingUid()
296 + " requires " + rperm;
297 throw new SecurityException(msg);
298 }
299
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700300 private boolean hasWritePermission(Uri uri) {
301 final int uid = Binder.getCallingUid();
302 if (uid == mMyUid) {
303 return true;
304 }
305
306 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 final String wperm = getWritePermission();
308 final int pid = Binder.getCallingPid();
Dianne Hackbornb424b632010-08-18 15:59:05 -0700309 if (mExported && (wperm == null
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700310 || context.checkPermission(wperm, pid, uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -0700311 == PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700312 return true;
313 }
314
315 PathPermission[] pps = getPathPermissions();
316 if (pps != null) {
317 final String path = uri.getPath();
318 int i = pps.length;
319 while (i > 0) {
320 i--;
321 final PathPermission pp = pps[i];
322 final String ppwperm = pp.getWritePermission();
323 if (ppwperm != null && pp.match(path)) {
324 if (context.checkPermission(ppwperm, pid, uid)
325 == PackageManager.PERMISSION_GRANTED) {
326 return true;
327 }
328 }
329 }
330 }
331
332 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
334 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700335 return true;
336 }
337
338 return false;
339 }
340
341 private void enforceWritePermission(Uri uri) {
342 if (hasWritePermission(uri)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 return;
344 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 String msg = "Permission Denial: writing "
347 + ContentProvider.this.getClass().getName()
348 + " uri " + uri + " from pid=" + Binder.getCallingPid()
349 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700350 + " requires " + getWritePermission();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 throw new SecurityException(msg);
352 }
353 }
354
355
356 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700357 * Retrieves the Context this provider is running in. Only available once
358 * {@link #onCreate} has been called -- this will return null in the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * constructor.
360 */
361 public final Context getContext() {
362 return mContext;
363 }
364
365 /**
366 * Change the permission required to read data from the content
367 * provider. This is normally set for you from its manifest information
368 * when the provider is first created.
369 *
370 * @param permission Name of the permission required for read-only access.
371 */
372 protected final void setReadPermission(String permission) {
373 mReadPermission = permission;
374 }
375
376 /**
377 * Return the name of the permission required for read-only access to
378 * this content provider. This method can be called from multiple
379 * threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800380 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
381 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
383 public final String getReadPermission() {
384 return mReadPermission;
385 }
386
387 /**
388 * Change the permission required to read and write data in the content
389 * provider. This is normally set for you from its manifest information
390 * when the provider is first created.
391 *
392 * @param permission Name of the permission required for read/write access.
393 */
394 protected final void setWritePermission(String permission) {
395 mWritePermission = permission;
396 }
397
398 /**
399 * Return the name of the permission required for read/write access to
400 * this content provider. This method can be called from multiple
401 * threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800402 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
403 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 */
405 public final String getWritePermission() {
406 return mWritePermission;
407 }
408
409 /**
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700410 * Change the path-based permission required to read and/or write data in
411 * the content provider. This is normally set for you from its manifest
412 * information when the provider is first created.
413 *
414 * @param permissions Array of path permission descriptions.
415 */
416 protected final void setPathPermissions(PathPermission[] permissions) {
417 mPathPermissions = permissions;
418 }
419
420 /**
421 * Return the path-based permissions required for read and/or write access to
422 * this content provider. This method can be called from multiple
423 * threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800424 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
425 * and Threads</a>.
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700426 */
427 public final PathPermission[] getPathPermissions() {
428 return mPathPermissions;
429 }
430
431 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700432 * Implement this to initialize your content provider on startup.
433 * This method is called for all registered content providers on the
434 * application main thread at application launch time. It must not perform
435 * lengthy operations, or application startup will be delayed.
436 *
437 * <p>You should defer nontrivial initialization (such as opening,
438 * upgrading, and scanning databases) until the content provider is used
439 * (via {@link #query}, {@link #insert}, etc). Deferred initialization
440 * keeps application startup fast, avoids unnecessary work if the provider
441 * turns out not to be needed, and stops database errors (such as a full
442 * disk) from halting application launch.
443 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700444 * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper}
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700445 * is a helpful utility class that makes it easy to manage databases,
446 * and will automatically defer opening until first use. If you do use
447 * SQLiteOpenHelper, make sure to avoid calling
448 * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or
449 * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase}
450 * from this method. (Instead, override
451 * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the
452 * database when it is first opened.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 *
454 * @return true if the provider was successfully loaded, false otherwise
455 */
456 public abstract boolean onCreate();
457
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700458 /**
459 * {@inheritDoc}
460 * This method is always called on the application main thread, and must
461 * not perform lengthy operations.
462 *
463 * <p>The default content provider implementation does nothing.
464 * Override this method to take appropriate action.
465 * (Content providers do not usually care about things like screen
466 * orientation, but may want to know about locale changes.)
467 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 public void onConfigurationChanged(Configuration newConfig) {
469 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700470
471 /**
472 * {@inheritDoc}
473 * This method is always called on the application main thread, and must
474 * not perform lengthy operations.
475 *
476 * <p>The default content provider implementation does nothing.
477 * Subclasses may override this method to take appropriate action.
478 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 public void onLowMemory() {
480 }
481
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700482 public void onTrimMemory(int level) {
483 }
484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700486 * Implement this to handle query requests from clients.
487 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800488 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
489 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 * <p>
491 * Example client call:<p>
492 * <pre>// Request a specific record.
493 * Cursor managedCursor = managedQuery(
Alan Jones81a476f2009-05-21 12:32:17 +1000494 ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 projection, // Which columns to return.
496 null, // WHERE clause.
Alan Jones81a476f2009-05-21 12:32:17 +1000497 null, // WHERE clause value substitution
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 People.NAME + " ASC"); // Sort order.</pre>
499 * Example implementation:<p>
500 * <pre>// SQLiteQueryBuilder is a helper class that creates the
501 // proper SQL syntax for us.
502 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
503
504 // Set the table we're querying.
505 qBuilder.setTables(DATABASE_TABLE_NAME);
506
507 // If the query ends in a specific record number, we're
508 // being asked for a specific record, so set the
509 // WHERE clause in our query.
510 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
511 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
512 }
513
514 // Make the query.
515 Cursor c = qBuilder.query(mDb,
516 projection,
517 selection,
518 selectionArgs,
519 groupBy,
520 having,
521 sortOrder);
522 c.setNotificationUri(getContext().getContentResolver(), uri);
523 return c;</pre>
524 *
525 * @param uri The URI to query. This will be the full URI sent by the client;
Alan Jones81a476f2009-05-21 12:32:17 +1000526 * if the client is requesting a specific record, the URI will end in a record number
527 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
528 * that _id value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 * @param projection The list of columns to put into the cursor. If
530 * null all columns are included.
531 * @param selection A selection criteria to apply when filtering rows.
532 * If null then all rows are included.
Alan Jones81a476f2009-05-21 12:32:17 +1000533 * @param selectionArgs You may include ?s in selection, which will be replaced by
534 * the values from selectionArgs, in order that they appear in the selection.
535 * The values will be bound as Strings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 * @param sortOrder How the rows in the cursor should be sorted.
Alan Jones81a476f2009-05-21 12:32:17 +1000537 * If null then the provider is free to define the sort order.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 * @return a Cursor or null.
539 */
540 public abstract Cursor query(Uri uri, String[] projection,
541 String selection, String[] selectionArgs, String sortOrder);
542
Fred Quintana5bba6322009-10-05 14:21:12 -0700543 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700544 * Implement this to handle requests for the MIME type of the data at the
545 * given URI. The returned MIME type should start with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 * <code>vnd.android.cursor.item</code> for a single record,
547 * or <code>vnd.android.cursor.dir/</code> for multiple items.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700548 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800549 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
550 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700552 * <p>Note that there are no permissions needed for an application to
553 * access this information; if your content provider requires read and/or
554 * write permissions, or is not exported, all applications can still call
555 * this method regardless of their access permissions. This allows them
556 * to retrieve the MIME type for a URI when dispatching intents.
557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * @param uri the URI to query.
559 * @return a MIME type string, or null if there is no type.
560 */
561 public abstract String getType(Uri uri);
562
563 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700564 * Implement this to handle requests to insert a new row.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
566 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700567 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800568 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
569 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 * @param uri The content:// URI of the insertion request.
571 * @param values A set of column_name/value pairs to add to the database.
572 * @return The URI for the newly inserted item.
573 */
574 public abstract Uri insert(Uri uri, ContentValues values);
575
576 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700577 * Override this to handle requests to insert a set of new rows, or the
578 * default implementation will iterate over the values and call
579 * {@link #insert} on each of them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
581 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700582 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800583 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
584 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 *
586 * @param uri The content:// URI of the insertion request.
587 * @param values An array of sets of column_name/value pairs to add to the database.
588 * @return The number of values that were inserted.
589 */
590 public int bulkInsert(Uri uri, ContentValues[] values) {
591 int numValues = values.length;
592 for (int i = 0; i < numValues; i++) {
593 insert(uri, values[i]);
594 }
595 return numValues;
596 }
597
598 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700599 * Implement this to handle requests to delete one or more rows.
600 * The implementation should apply the selection clause when performing
601 * deletion, allowing the operation to affect multiple rows in a directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()}
603 * after deleting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700604 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800605 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
606 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 *
608 * <p>The implementation is responsible for parsing out a row ID at the end
609 * of the URI, if a specific row is being deleted. That is, the client would
610 * pass in <code>content://contacts/people/22</code> and the implementation is
611 * responsible for parsing the record number (22) when creating a SQL statement.
612 *
613 * @param uri The full URI to query, including a row ID (if a specific record is requested).
614 * @param selection An optional restriction to apply to rows when deleting.
615 * @return The number of rows affected.
616 * @throws SQLException
617 */
618 public abstract int delete(Uri uri, String selection, String[] selectionArgs);
619
620 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700621 * Implement this to handle requests to update one or more rows.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700622 * The implementation should update all rows matching the selection
623 * to set the columns according to the provided values map.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
625 * after updating.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700626 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800627 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
628 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 *
630 * @param uri The URI to query. This can potentially have a record ID if this
631 * is an update request for a specific record.
632 * @param values A Bundle mapping from column names to new column values (NULL is a
633 * valid value).
634 * @param selection An optional filter to match rows to update.
635 * @return the number of rows affected.
636 */
637 public abstract int update(Uri uri, ContentValues values, String selection,
638 String[] selectionArgs);
639
640 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700641 * Override this to handle requests to open a file blob.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700642 * The default implementation always throws {@link FileNotFoundException}.
643 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800644 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
645 * and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700646 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700647 * <p>This method returns a ParcelFileDescriptor, which is returned directly
648 * to the caller. This way large data (such as images and documents) can be
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700649 * returned without copying the content.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 *
651 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
652 * their responsibility to close it when done. That is, the implementation
653 * of this method should create a new ParcelFileDescriptor for each call.
654 *
655 * @param uri The URI whose file is to be opened.
656 * @param mode Access mode for the file. May be "r" for read-only access,
657 * "rw" for read and write access, or "rwt" for read and write access
658 * that truncates any existing file.
659 *
660 * @return Returns a new ParcelFileDescriptor which you can use to access
661 * the file.
662 *
663 * @throws FileNotFoundException Throws FileNotFoundException if there is
664 * no file associated with the given URI or the mode is invalid.
665 * @throws SecurityException Throws SecurityException if the caller does
666 * not have permission to access the file.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 * @see #openAssetFile(Uri, String)
669 * @see #openFileHelper(Uri, String)
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700670 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 public ParcelFileDescriptor openFile(Uri uri, String mode)
672 throws FileNotFoundException {
673 throw new FileNotFoundException("No files supported by provider at "
674 + uri);
675 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 /**
678 * This is like {@link #openFile}, but can be implemented by providers
679 * that need to be able to return sub-sections of files, often assets
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700680 * inside of their .apk.
681 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800682 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
683 * and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700684 *
685 * <p>If you implement this, your clients must be able to deal with such
Dan Egnor17876aa2010-07-28 12:28:04 -0700686 * file slices, either directly with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700687 * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
689 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
690 * methods.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700691 *
692 * <p class="note">If you are implementing this to return a full file, you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 * should create the AssetFileDescriptor with
694 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700695 * applications that can not handle sub-sections of files.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 *
697 * @param uri The URI whose file is to be opened.
698 * @param mode Access mode for the file. May be "r" for read-only access,
699 * "w" for write-only access (erasing whatever data is currently in
700 * the file), "wa" for write-only access to append to any existing data,
701 * "rw" for read and write access on any existing data, and "rwt" for read
702 * and write access that truncates any existing file.
703 *
704 * @return Returns a new AssetFileDescriptor which you can use to access
705 * the file.
706 *
707 * @throws FileNotFoundException Throws FileNotFoundException if there is
708 * no file associated with the given URI or the mode is invalid.
709 * @throws SecurityException Throws SecurityException if the caller does
710 * not have permission to access the file.
711 *
712 * @see #openFile(Uri, String)
713 * @see #openFileHelper(Uri, String)
714 */
715 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
716 throws FileNotFoundException {
717 ParcelFileDescriptor fd = openFile(uri, mode);
718 return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
719 }
720
721 /**
722 * Convenience for subclasses that wish to implement {@link #openFile}
723 * by looking up a column named "_data" at the given URI.
724 *
725 * @param uri The URI to be opened.
726 * @param mode The file mode. May be "r" for read-only access,
727 * "w" for write-only access (erasing whatever data is currently in
728 * the file), "wa" for write-only access to append to any existing data,
729 * "rw" for read and write access on any existing data, and "rwt" for read
730 * and write access that truncates any existing file.
731 *
732 * @return Returns a new ParcelFileDescriptor that can be used by the
733 * client to access the file.
734 */
735 protected final ParcelFileDescriptor openFileHelper(Uri uri,
736 String mode) throws FileNotFoundException {
737 Cursor c = query(uri, new String[]{"_data"}, null, null, null);
738 int count = (c != null) ? c.getCount() : 0;
739 if (count != 1) {
740 // If there is not exactly one result, throw an appropriate
741 // exception.
742 if (c != null) {
743 c.close();
744 }
745 if (count == 0) {
746 throw new FileNotFoundException("No entry for " + uri);
747 }
748 throw new FileNotFoundException("Multiple items at " + uri);
749 }
750
751 c.moveToFirst();
752 int i = c.getColumnIndex("_data");
753 String path = (i >= 0 ? c.getString(i) : null);
754 c.close();
755 if (path == null) {
756 throw new FileNotFoundException("Column _data not found.");
757 }
758
759 int modeBits = ContentResolver.modeToMode(uri, mode);
760 return ParcelFileDescriptor.open(new File(path), modeBits);
761 }
762
763 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700764 * Called by a client to determine the types of data streams that this
765 * content provider supports for the given URI. The default implementation
766 * returns null, meaning no types. If your content provider stores data
767 * of a particular type, return that MIME type if it matches the given
768 * mimeTypeFilter. If it can perform type conversions, return an array
769 * of all supported MIME types that match mimeTypeFilter.
770 *
771 * @param uri The data in the content provider being queried.
772 * @param mimeTypeFilter The type of data the client desires. May be
773 * a pattern, such as *\/* to retrieve all possible data types.
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700774 * @return Returns null if there are no possible data streams for the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700775 * given mimeTypeFilter. Otherwise returns an array of all available
776 * concrete MIME types.
777 *
778 * @see #getType(Uri)
779 * @see #openTypedAssetFile(Uri, String, Bundle)
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700780 * @see ClipDescription#compareMimeTypes(String, String)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700781 */
782 public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
783 return null;
784 }
785
786 /**
787 * Called by a client to open a read-only stream containing data of a
788 * particular MIME type. This is like {@link #openAssetFile(Uri, String)},
789 * except the file can only be read-only and the content provider may
790 * perform data conversions to generate data of the desired type.
791 *
792 * <p>The default implementation compares the given mimeType against the
793 * result of {@link #getType(Uri)} and, if the match, simple calls
794 * {@link #openAssetFile(Uri, String)}.
795 *
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700796 * <p>See {@link ClipData} for examples of the use and implementation
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700797 * of this method.
798 *
799 * @param uri The data in the content provider being queried.
800 * @param mimeTypeFilter The type of data the client desires. May be
801 * a pattern, such as *\/*, if the caller does not have specific type
802 * requirements; in this case the content provider will pick its best
803 * type matching the pattern.
804 * @param opts Additional options from the client. The definitions of
805 * these are specific to the content provider being called.
806 *
807 * @return Returns a new AssetFileDescriptor from which the client can
808 * read data of the desired type.
809 *
810 * @throws FileNotFoundException Throws FileNotFoundException if there is
811 * no file associated with the given URI or the mode is invalid.
812 * @throws SecurityException Throws SecurityException if the caller does
813 * not have permission to access the data.
814 * @throws IllegalArgumentException Throws IllegalArgumentException if the
815 * content provider does not support the requested MIME type.
816 *
817 * @see #getStreamTypes(Uri, String)
818 * @see #openAssetFile(Uri, String)
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700819 * @see ClipDescription#compareMimeTypes(String, String)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700820 */
821 public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts)
822 throws FileNotFoundException {
Dianne Hackborn02dfd262010-08-13 12:34:58 -0700823 if ("*/*".equals(mimeTypeFilter)) {
824 // If they can take anything, the untyped open call is good enough.
825 return openAssetFile(uri, "r");
826 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700827 String baseType = getType(uri);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700828 if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) {
Dianne Hackborn02dfd262010-08-13 12:34:58 -0700829 // Use old untyped open call if this provider has a type for this
830 // URI and it matches the request.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700831 return openAssetFile(uri, "r");
832 }
833 throw new FileNotFoundException("Can't open " + uri + " as type " + mimeTypeFilter);
834 }
835
836 /**
837 * Interface to write a stream of data to a pipe. Use with
838 * {@link ContentProvider#openPipeHelper}.
839 */
840 public interface PipeDataWriter<T> {
841 /**
842 * Called from a background thread to stream data out to a pipe.
843 * Note that the pipe is blocking, so this thread can block on
844 * writes for an arbitrary amount of time if the client is slow
845 * at reading.
846 *
847 * @param output The pipe where data should be written. This will be
848 * closed for you upon returning from this function.
849 * @param uri The URI whose data is to be written.
850 * @param mimeType The desired type of data to be written.
851 * @param opts Options supplied by caller.
852 * @param args Your own custom arguments.
853 */
854 public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType,
855 Bundle opts, T args);
856 }
857
858 /**
859 * A helper function for implementing {@link #openTypedAssetFile}, for
860 * creating a data pipe and background thread allowing you to stream
861 * generated data back to the client. This function returns a new
862 * ParcelFileDescriptor that should be returned to the caller (the caller
863 * is responsible for closing it).
864 *
865 * @param uri The URI whose data is to be written.
866 * @param mimeType The desired type of data to be written.
867 * @param opts Options supplied by caller.
868 * @param args Your own custom arguments.
869 * @param func Interface implementing the function that will actually
870 * stream the data.
871 * @return Returns a new ParcelFileDescriptor holding the read side of
872 * the pipe. This should be returned to the caller for reading; the caller
873 * is responsible for closing it when done.
874 */
875 public <T> ParcelFileDescriptor openPipeHelper(final Uri uri, final String mimeType,
876 final Bundle opts, final T args, final PipeDataWriter<T> func)
877 throws FileNotFoundException {
878 try {
879 final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
880
881 AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {
882 @Override
883 protected Object doInBackground(Object... params) {
884 func.writeDataToPipe(fds[1], uri, mimeType, opts, args);
885 try {
886 fds[1].close();
887 } catch (IOException e) {
888 Log.w(TAG, "Failure closing pipe", e);
889 }
890 return null;
891 }
892 };
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800893 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700894
895 return fds[0];
896 } catch (IOException e) {
897 throw new FileNotFoundException("failure making pipe");
898 }
899 }
900
901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 * Returns true if this instance is a temporary content provider.
903 * @return true if this instance is a temporary content provider
904 */
905 protected boolean isTemporary() {
906 return false;
907 }
908
909 /**
910 * Returns the Binder object for this provider.
911 *
912 * @return the Binder object for this provider
913 * @hide
914 */
915 public IContentProvider getIContentProvider() {
916 return mTransport;
917 }
918
919 /**
920 * After being instantiated, this is called to tell the content provider
921 * about itself.
922 *
923 * @param context The context this provider is running in
924 * @param info Registered information about this content provider
925 */
926 public void attachInfo(Context context, ProviderInfo info) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700927 /*
928 * We may be using AsyncTask from binder threads. Make it init here
929 * so its static handler is on the main thread.
930 */
931 AsyncTask.init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
933 /*
934 * Only allow it to be set once, so after the content service gives
935 * this to us clients can't change it.
936 */
937 if (mContext == null) {
938 mContext = context;
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700939 mMyUid = Process.myUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 if (info != null) {
941 setReadPermission(info.readPermission);
942 setWritePermission(info.writePermission);
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700943 setPathPermissions(info.pathPermissions);
Dianne Hackbornb424b632010-08-18 15:59:05 -0700944 mExported = info.exported;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
946 ContentProvider.this.onCreate();
947 }
948 }
Fred Quintanace31b232009-05-04 16:01:15 -0700949
950 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700951 * Override this to handle requests to perform a batch of operations, or the
952 * default implementation will iterate over the operations and call
953 * {@link ContentProviderOperation#apply} on each of them.
954 * If all calls to {@link ContentProviderOperation#apply} succeed
955 * then a {@link ContentProviderResult} array with as many
956 * elements as there were operations will be returned. If any of the calls
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700957 * fail, it is up to the implementation how many of the others take effect.
958 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -0800959 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
960 * and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700961 *
Fred Quintanace31b232009-05-04 16:01:15 -0700962 * @param operations the operations to apply
963 * @return the results of the applications
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700964 * @throws OperationApplicationException thrown if any operation fails.
965 * @see ContentProviderOperation#apply
Fred Quintanace31b232009-05-04 16:01:15 -0700966 */
Fred Quintana03d94902009-05-22 14:23:31 -0700967 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintanace31b232009-05-04 16:01:15 -0700968 throws OperationApplicationException {
Fred Quintana03d94902009-05-22 14:23:31 -0700969 final int numOperations = operations.size();
970 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
971 for (int i = 0; i < numOperations; i++) {
972 results[i] = operations.get(i).apply(this, results, i);
Fred Quintanace31b232009-05-04 16:01:15 -0700973 }
974 return results;
975 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800976
977 /**
Manuel Roman2c96a0c2010-08-05 16:39:49 -0700978 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800979 * interfaces that are cheaper and/or unnatural for a table-like
980 * model.
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800981 *
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800982 * @param method method name to call. Opaque to framework, but should not be null.
983 * @param arg provider-defined String argument. May be null.
984 * @param extras provider-defined Bundle argument. May be null.
985 * @return provider-defined return value. May be null. Null is also
986 * the default for providers which don't implement any call methods.
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800987 */
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800988 public Bundle call(String method, String arg, Bundle extras) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800989 return null;
990 }
Vasu Nori0c9e14a2010-08-04 13:31:48 -0700991
992 /**
Manuel Roman2c96a0c2010-08-05 16:39:49 -0700993 * Implement this to shut down the ContentProvider instance. You can then
994 * invoke this method in unit tests.
995 *
Vasu Nori0c9e14a2010-08-04 13:31:48 -0700996 * <p>
Manuel Roman2c96a0c2010-08-05 16:39:49 -0700997 * Android normally handles ContentProvider startup and shutdown
998 * automatically. You do not need to start up or shut down a
999 * ContentProvider. When you invoke a test method on a ContentProvider,
1000 * however, a ContentProvider instance is started and keeps running after
1001 * the test finishes, even if a succeeding test instantiates another
1002 * ContentProvider. A conflict develops because the two instances are
1003 * usually running against the same underlying data source (for example, an
1004 * sqlite database).
1005 * </p>
Vasu Nori0c9e14a2010-08-04 13:31:48 -07001006 * <p>
Manuel Roman2c96a0c2010-08-05 16:39:49 -07001007 * Implementing shutDown() avoids this conflict by providing a way to
1008 * terminate the ContentProvider. This method can also prevent memory leaks
1009 * from multiple instantiations of the ContentProvider, and it can ensure
1010 * unit test isolation by allowing you to completely clean up the test
1011 * fixture before moving on to the next test.
1012 * </p>
Vasu Nori0c9e14a2010-08-04 13:31:48 -07001013 */
1014 public void shutdown() {
1015 Log.w(TAG, "implement ContentProvider shutdown() to make sure all database " +
1016 "connections are gracefully shutdown");
1017 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001018
1019 /**
1020 * Print the Provider's state into the given stream. This gets invoked if
1021 * you run "adb shell dumpsys activity provider <provider_component_name>".
1022 *
1023 * @param prefix Desired prefix to prepend at each line of output.
1024 * @param fd The raw file descriptor that the dump is being sent to.
1025 * @param writer The PrintWriter to which you should dump your state. This will be
1026 * closed for you after you return.
1027 * @param args additional arguments to the dump request.
1028 * @hide
1029 */
1030 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
1031 writer.println("nothing to dump");
1032 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08001033}