blob: 3a8de6ec922b1e19c55bc93d517457ec3bb04708 [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;
20import android.content.pm.ProviderInfo;
21import android.content.res.AssetFileDescriptor;
22import android.content.res.Configuration;
23import android.database.Cursor;
24import android.database.CursorToBulkCursorAdaptor;
25import android.database.CursorWindow;
26import android.database.IBulkCursor;
27import android.database.IContentObserver;
28import android.database.SQLException;
29import android.net.Uri;
30import android.os.Binder;
31import android.os.ParcelFileDescriptor;
32
33import java.io.File;
34import java.io.FileNotFoundException;
35
36/**
37 * Content providers are one of the primary building blocks of Android applications, providing
38 * content to applications. They encapsulate data and provide it to applications through the single
39 * {@link ContentResolver} interface. A content provider is only required if you need to share
40 * data between multiple applications. For example, the contacts data is used by multiple
41 * applications and must be stored in a content provider. If you don't need to share data amongst
42 * multiple applications you can use a database directly via
43 * {@link android.database.sqlite.SQLiteDatabase}.
44 *
45 * <p>For more information, read <a href="{@docRoot}guide/topics/providers/content-providers.html">Content
46 * Providers</a>.</p>
47 *
48 * <p>When a request is made via
49 * a {@link ContentResolver} the system inspects the authority of the given URI and passes the
50 * request to the content provider registered with the authority. The content provider can interpret
51 * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing
52 * URIs.</p>
53 *
54 * <p>The primary methods that need to be implemented are:
55 * <ul>
56 * <li>{@link #query} which returns data to the caller</li>
57 * <li>{@link #insert} which inserts new data into the content provider</li>
58 * <li>{@link #update} which updates existing data in the content provider</li>
59 * <li>{@link #delete} which deletes data from the content provider</li>
60 * <li>{@link #getType} which returns the MIME type of data in the content provider</li>
61 * </ul></p>
62 *
63 * <p>This class takes care of cross process calls so subclasses don't have to worry about which
64 * process a request is coming from.</p>
65 */
66public abstract class ContentProvider implements ComponentCallbacks {
67 private Context mContext = null;
68 private String mReadPermission;
69 private String mWritePermission;
70
71 private Transport mTransport = new Transport();
72
73 /**
74 * Given an IContentProvider, try to coerce it back to the real
75 * ContentProvider object if it is running in the local process. This can
76 * be used if you know you are running in the same process as a provider,
77 * and want to get direct access to its implementation details. Most
78 * clients should not nor have a reason to use it.
79 *
80 * @param abstractInterface The ContentProvider interface that is to be
81 * coerced.
82 * @return If the IContentProvider is non-null and local, returns its actual
83 * ContentProvider instance. Otherwise returns null.
84 * @hide
85 */
86 public static ContentProvider coerceToLocalContentProvider(
87 IContentProvider abstractInterface) {
88 if (abstractInterface instanceof Transport) {
89 return ((Transport)abstractInterface).getContentProvider();
90 }
91 return null;
92 }
93
94 /**
95 * Binder object that deals with remoting.
96 *
97 * @hide
98 */
99 class Transport extends ContentProviderNative {
100 ContentProvider getContentProvider() {
101 return ContentProvider.this;
102 }
103
104 /**
105 * Remote version of a query, which returns an IBulkCursor. The bulk
106 * cursor should be wrapped with BulkCursorToCursorAdaptor before use.
107 */
108 public IBulkCursor bulkQuery(Uri uri, String[] projection,
109 String selection, String[] selectionArgs, String sortOrder,
110 IContentObserver observer, CursorWindow window) {
111 checkReadPermission(uri);
112 Cursor cursor = ContentProvider.this.query(uri, projection,
113 selection, selectionArgs, sortOrder);
114 if (cursor == null) {
115 return null;
116 }
117 String wperm = getWritePermission();
118 return new CursorToBulkCursorAdaptor(cursor, observer,
119 ContentProvider.this.getClass().getName(),
120 wperm == null ||
121 getContext().checkCallingOrSelfPermission(getWritePermission())
122 == PackageManager.PERMISSION_GRANTED,
123 window);
124 }
125
126 public Cursor query(Uri uri, String[] projection,
127 String selection, String[] selectionArgs, String sortOrder) {
128 checkReadPermission(uri);
129 return ContentProvider.this.query(uri, projection, selection,
130 selectionArgs, sortOrder);
131 }
132
Fred Quintana6a8d5332009-05-07 17:35:38 -0700133 public EntityIterator queryEntities(Uri uri, String selection, String[] selectionArgs,
134 String sortOrder) {
135 checkReadPermission(uri);
136 return ContentProvider.this.queryEntities(uri, selection, selectionArgs, sortOrder);
137 }
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public String getType(Uri uri) {
140 return ContentProvider.this.getType(uri);
141 }
142
143
144 public Uri insert(Uri uri, ContentValues initialValues) {
145 checkWritePermission(uri);
146 return ContentProvider.this.insert(uri, initialValues);
147 }
148
149 public int bulkInsert(Uri uri, ContentValues[] initialValues) {
150 checkWritePermission(uri);
151 return ContentProvider.this.bulkInsert(uri, initialValues);
152 }
153
Fred Quintana6a8d5332009-05-07 17:35:38 -0700154 public Uri[] bulkInsertEntities(Uri uri, Entity[] entities) {
155 checkWritePermission(uri);
156 return ContentProvider.this.bulkInsertEntities(uri, entities);
157 }
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public int delete(Uri uri, String selection, String[] selectionArgs) {
160 checkWritePermission(uri);
161 return ContentProvider.this.delete(uri, selection, selectionArgs);
162 }
163
164 public int update(Uri uri, ContentValues values, String selection,
165 String[] selectionArgs) {
166 checkWritePermission(uri);
167 return ContentProvider.this.update(uri, values, selection, selectionArgs);
168 }
169
Fred Quintana6a8d5332009-05-07 17:35:38 -0700170 public int[] bulkUpdateEntities(Uri uri, Entity[] entities) {
171 checkWritePermission(uri);
172 return ContentProvider.this.bulkUpdateEntities(uri, entities);
173 }
174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public ParcelFileDescriptor openFile(Uri uri, String mode)
176 throws FileNotFoundException {
177 if (mode != null && mode.startsWith("rw")) checkWritePermission(uri);
178 else checkReadPermission(uri);
179 return ContentProvider.this.openFile(uri, mode);
180 }
181
182 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
183 throws FileNotFoundException {
184 if (mode != null && mode.startsWith("rw")) checkWritePermission(uri);
185 else checkReadPermission(uri);
186 return ContentProvider.this.openAssetFile(uri, mode);
187 }
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 private void checkReadPermission(Uri uri) {
190 final String rperm = getReadPermission();
191 final int pid = Binder.getCallingPid();
192 final int uid = Binder.getCallingUid();
193 if (getContext().checkUriPermission(uri, rperm, null, pid, uid,
194 Intent.FLAG_GRANT_READ_URI_PERMISSION)
195 == PackageManager.PERMISSION_GRANTED) {
196 return;
197 }
198 String msg = "Permission Denial: reading "
199 + ContentProvider.this.getClass().getName()
200 + " uri " + uri + " from pid=" + Binder.getCallingPid()
201 + ", uid=" + Binder.getCallingUid()
202 + " requires " + rperm;
203 throw new SecurityException(msg);
204 }
205
206 private void checkWritePermission(Uri uri) {
207 final String wperm = getWritePermission();
208 final int pid = Binder.getCallingPid();
209 final int uid = Binder.getCallingUid();
210 if (getContext().checkUriPermission(uri, null, wperm, pid, uid,
211 Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
212 == PackageManager.PERMISSION_GRANTED) {
213 return;
214 }
215 String msg = "Permission Denial: writing "
216 + ContentProvider.this.getClass().getName()
217 + " uri " + uri + " from pid=" + Binder.getCallingPid()
218 + ", uid=" + Binder.getCallingUid()
219 + " requires " + wperm;
220 throw new SecurityException(msg);
221 }
222 }
223
224
225 /**
226 * Retrieve the Context this provider is running in. Only available once
227 * onCreate(Map icicle) has been called -- this will be null in the
228 * constructor.
229 */
230 public final Context getContext() {
231 return mContext;
232 }
233
234 /**
235 * Change the permission required to read data from the content
236 * provider. This is normally set for you from its manifest information
237 * when the provider is first created.
238 *
239 * @param permission Name of the permission required for read-only access.
240 */
241 protected final void setReadPermission(String permission) {
242 mReadPermission = permission;
243 }
244
245 /**
246 * Return the name of the permission required for read-only access to
247 * this content provider. This method can be called from multiple
248 * threads, as described in
249 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
250 * Processes and Threads</a>.
251 */
252 public final String getReadPermission() {
253 return mReadPermission;
254 }
255
256 /**
257 * Change the permission required to read and write data in the content
258 * provider. This is normally set for you from its manifest information
259 * when the provider is first created.
260 *
261 * @param permission Name of the permission required for read/write access.
262 */
263 protected final void setWritePermission(String permission) {
264 mWritePermission = permission;
265 }
266
267 /**
268 * Return the name of the permission required for read/write access to
269 * this content provider. This method can be called from multiple
270 * threads, as described in
271 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
272 * Processes and Threads</a>.
273 */
274 public final String getWritePermission() {
275 return mWritePermission;
276 }
277
278 /**
279 * Called when the provider is being started.
280 *
281 * @return true if the provider was successfully loaded, false otherwise
282 */
283 public abstract boolean onCreate();
284
285 public void onConfigurationChanged(Configuration newConfig) {
286 }
287
288 public void onLowMemory() {
289 }
290
291 /**
292 * Receives a query request from a client in a local process, and
293 * returns a Cursor. This is called internally by the {@link ContentResolver}.
294 * This method can be called from multiple
295 * threads, as described in
296 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
297 * Processes and Threads</a>.
298 * <p>
299 * Example client call:<p>
300 * <pre>// Request a specific record.
301 * Cursor managedCursor = managedQuery(
302 Contacts.People.CONTENT_URI.addId(2),
303 projection, // Which columns to return.
304 null, // WHERE clause.
305 People.NAME + " ASC"); // Sort order.</pre>
306 * Example implementation:<p>
307 * <pre>// SQLiteQueryBuilder is a helper class that creates the
308 // proper SQL syntax for us.
309 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
310
311 // Set the table we're querying.
312 qBuilder.setTables(DATABASE_TABLE_NAME);
313
314 // If the query ends in a specific record number, we're
315 // being asked for a specific record, so set the
316 // WHERE clause in our query.
317 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
318 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
319 }
320
321 // Make the query.
322 Cursor c = qBuilder.query(mDb,
323 projection,
324 selection,
325 selectionArgs,
326 groupBy,
327 having,
328 sortOrder);
329 c.setNotificationUri(getContext().getContentResolver(), uri);
330 return c;</pre>
331 *
332 * @param uri The URI to query. This will be the full URI sent by the client;
333 * if the client is requesting a specific record, the URI will end in a record number
334 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
335 * that _id value.
336 * @param projection The list of columns to put into the cursor. If
337 * null all columns are included.
338 * @param selection A selection criteria to apply when filtering rows.
339 * If null then all rows are included.
340 * @param sortOrder How the rows in the cursor should be sorted.
341 * If null then the provider is free to define the sort order.
342 * @return a Cursor or null.
343 */
344 public abstract Cursor query(Uri uri, String[] projection,
345 String selection, String[] selectionArgs, String sortOrder);
346
Fred Quintana6a8d5332009-05-07 17:35:38 -0700347 public EntityIterator queryEntities(Uri uri, String selection, String[] selectionArgs,
348 String sortOrder) {
349 throw new UnsupportedOperationException();
350 }
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * Return the MIME type of the data at the given URI. This should start with
354 * <code>vnd.android.cursor.item</code> for a single record,
355 * or <code>vnd.android.cursor.dir/</code> for multiple items.
356 * This method can be called from multiple
357 * threads, as described in
358 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
359 * Processes and Threads</a>.
360 *
361 * @param uri the URI to query.
362 * @return a MIME type string, or null if there is no type.
363 */
364 public abstract String getType(Uri uri);
365
366 /**
367 * Implement this to insert a new row.
368 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
369 * after inserting.
370 * This method can be called from multiple
371 * threads, as described in
372 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
373 * Processes and Threads</a>.
374 * @param uri The content:// URI of the insertion request.
375 * @param values A set of column_name/value pairs to add to the database.
376 * @return The URI for the newly inserted item.
377 */
378 public abstract Uri insert(Uri uri, ContentValues values);
379
380 /**
381 * Implement this to insert a set of new rows, or the default implementation will
382 * iterate over the values and call {@link #insert} on each of them.
383 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
384 * after inserting.
385 * This method can be called from multiple
386 * threads, as described in
387 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
388 * Processes and Threads</a>.
389 *
390 * @param uri The content:// URI of the insertion request.
391 * @param values An array of sets of column_name/value pairs to add to the database.
392 * @return The number of values that were inserted.
393 */
394 public int bulkInsert(Uri uri, ContentValues[] values) {
395 int numValues = values.length;
396 for (int i = 0; i < numValues; i++) {
397 insert(uri, values[i]);
398 }
399 return numValues;
400 }
401
Fred Quintana6a8d5332009-05-07 17:35:38 -0700402 public Uri insertEntity(Uri uri, Entity entity) {
403 throw new UnsupportedOperationException();
404 }
405
406 public Uri[] bulkInsertEntities(Uri uri, Entity[] entities) {
407 Uri[] result = new Uri[entities.length];
408 for (int i = 0; i < entities.length; i++) {
409 result[i] = insertEntity(uri, entities[i]);
410 }
411 return result;
412 }
413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * A request to delete one or more rows. The selection clause is applied when performing
416 * the deletion, allowing the operation to affect multiple rows in a
417 * directory.
418 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()}
419 * after deleting.
420 * This method can be called from multiple
421 * threads, as described in
422 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
423 * Processes and Threads</a>.
424 *
425 * <p>The implementation is responsible for parsing out a row ID at the end
426 * of the URI, if a specific row is being deleted. That is, the client would
427 * pass in <code>content://contacts/people/22</code> and the implementation is
428 * responsible for parsing the record number (22) when creating a SQL statement.
429 *
430 * @param uri The full URI to query, including a row ID (if a specific record is requested).
431 * @param selection An optional restriction to apply to rows when deleting.
432 * @return The number of rows affected.
433 * @throws SQLException
434 */
435 public abstract int delete(Uri uri, String selection, String[] selectionArgs);
436
437 /**
438 * Update a content URI. All rows matching the optionally provided selection
439 * will have their columns listed as the keys in the values map with the
440 * values of those keys.
441 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
442 * after updating.
443 * This method can be called from multiple
444 * threads, as described in
445 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
446 * Processes and Threads</a>.
447 *
448 * @param uri The URI to query. This can potentially have a record ID if this
449 * is an update request for a specific record.
450 * @param values A Bundle mapping from column names to new column values (NULL is a
451 * valid value).
452 * @param selection An optional filter to match rows to update.
453 * @return the number of rows affected.
454 */
455 public abstract int update(Uri uri, ContentValues values, String selection,
456 String[] selectionArgs);
457
Fred Quintana6a8d5332009-05-07 17:35:38 -0700458 public int updateEntity(Uri uri, Entity entity) {
459 throw new UnsupportedOperationException();
460 }
461
462 public int[] bulkUpdateEntities(Uri uri, Entity[] entities) {
463 int[] result = new int[entities.length];
464 for (int i = 0; i < entities.length; i++) {
465 result[i] = updateEntity(uri, entities[i]);
466 }
467 return result;
468 }
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 /**
471 * Open a file blob associated with a content URI.
472 * This method can be called from multiple
473 * threads, as described in
474 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
475 * Processes and Threads</a>.
476 *
477 * <p>Returns a
478 * ParcelFileDescriptor, from which you can obtain a
479 * {@link java.io.FileDescriptor} for use with
480 * {@link java.io.FileInputStream}, {@link java.io.FileOutputStream}, etc.
481 * This can be used to store large data (such as an image) associated with
482 * a particular piece of content.
483 *
484 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
485 * their responsibility to close it when done. That is, the implementation
486 * of this method should create a new ParcelFileDescriptor for each call.
487 *
488 * @param uri The URI whose file is to be opened.
489 * @param mode Access mode for the file. May be "r" for read-only access,
490 * "rw" for read and write access, or "rwt" for read and write access
491 * that truncates any existing file.
492 *
493 * @return Returns a new ParcelFileDescriptor which you can use to access
494 * the file.
495 *
496 * @throws FileNotFoundException Throws FileNotFoundException if there is
497 * no file associated with the given URI or the mode is invalid.
498 * @throws SecurityException Throws SecurityException if the caller does
499 * not have permission to access the file.
500 *
501 * @see #openAssetFile(Uri, String)
502 * @see #openFileHelper(Uri, String)
503 */
504 public ParcelFileDescriptor openFile(Uri uri, String mode)
505 throws FileNotFoundException {
506 throw new FileNotFoundException("No files supported by provider at "
507 + uri);
508 }
509
510 /**
511 * This is like {@link #openFile}, but can be implemented by providers
512 * that need to be able to return sub-sections of files, often assets
513 * inside of their .apk. Note that when implementing this your clients
514 * must be able to deal with such files, either directly with
515 * {@link ContentResolver#openAssetFileDescriptor
516 * ContentResolver.openAssetFileDescriptor}, or by using the higher-level
517 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
518 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
519 * methods.
520 *
521 * <p><em>Note: if you are implementing this to return a full file, you
522 * should create the AssetFileDescriptor with
523 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
524 * applications that can not handle sub-sections of files.</em></p>
525 *
526 * @param uri The URI whose file is to be opened.
527 * @param mode Access mode for the file. May be "r" for read-only access,
528 * "w" for write-only access (erasing whatever data is currently in
529 * the file), "wa" for write-only access to append to any existing data,
530 * "rw" for read and write access on any existing data, and "rwt" for read
531 * and write access that truncates any existing file.
532 *
533 * @return Returns a new AssetFileDescriptor which you can use to access
534 * the file.
535 *
536 * @throws FileNotFoundException Throws FileNotFoundException if there is
537 * no file associated with the given URI or the mode is invalid.
538 * @throws SecurityException Throws SecurityException if the caller does
539 * not have permission to access the file.
540 *
541 * @see #openFile(Uri, String)
542 * @see #openFileHelper(Uri, String)
543 */
544 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
545 throws FileNotFoundException {
546 ParcelFileDescriptor fd = openFile(uri, mode);
547 return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
548 }
549
550 /**
551 * Convenience for subclasses that wish to implement {@link #openFile}
552 * by looking up a column named "_data" at the given URI.
553 *
554 * @param uri The URI to be opened.
555 * @param mode The file mode. May be "r" for read-only access,
556 * "w" for write-only access (erasing whatever data is currently in
557 * the file), "wa" for write-only access to append to any existing data,
558 * "rw" for read and write access on any existing data, and "rwt" for read
559 * and write access that truncates any existing file.
560 *
561 * @return Returns a new ParcelFileDescriptor that can be used by the
562 * client to access the file.
563 */
564 protected final ParcelFileDescriptor openFileHelper(Uri uri,
565 String mode) throws FileNotFoundException {
566 Cursor c = query(uri, new String[]{"_data"}, null, null, null);
567 int count = (c != null) ? c.getCount() : 0;
568 if (count != 1) {
569 // If there is not exactly one result, throw an appropriate
570 // exception.
571 if (c != null) {
572 c.close();
573 }
574 if (count == 0) {
575 throw new FileNotFoundException("No entry for " + uri);
576 }
577 throw new FileNotFoundException("Multiple items at " + uri);
578 }
579
580 c.moveToFirst();
581 int i = c.getColumnIndex("_data");
582 String path = (i >= 0 ? c.getString(i) : null);
583 c.close();
584 if (path == null) {
585 throw new FileNotFoundException("Column _data not found.");
586 }
587
588 int modeBits = ContentResolver.modeToMode(uri, mode);
589 return ParcelFileDescriptor.open(new File(path), modeBits);
590 }
591
592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * Returns true if this instance is a temporary content provider.
594 * @return true if this instance is a temporary content provider
595 */
596 protected boolean isTemporary() {
597 return false;
598 }
599
600 /**
601 * Returns the Binder object for this provider.
602 *
603 * @return the Binder object for this provider
604 * @hide
605 */
606 public IContentProvider getIContentProvider() {
607 return mTransport;
608 }
609
610 /**
611 * After being instantiated, this is called to tell the content provider
612 * about itself.
613 *
614 * @param context The context this provider is running in
615 * @param info Registered information about this content provider
616 */
617 public void attachInfo(Context context, ProviderInfo info) {
618
619 /*
620 * Only allow it to be set once, so after the content service gives
621 * this to us clients can't change it.
622 */
623 if (mContext == null) {
624 mContext = context;
625 if (info != null) {
626 setReadPermission(info.readPermission);
627 setWritePermission(info.writePermission);
628 }
629 ContentProvider.this.onCreate();
630 }
631 }
632}