blob: dc4e9c4e6525a05d1e9d9e39d25fdc29fc7dcd2d [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;
31import android.os.Binder;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080032import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.ParcelFileDescriptor;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070034import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import java.io.File;
37import java.io.FileNotFoundException;
Fred Quintana03d94902009-05-22 14:23:31 -070038import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40/**
41 * Content providers are one of the primary building blocks of Android applications, providing
42 * content to applications. They encapsulate data and provide it to applications through the single
43 * {@link ContentResolver} interface. A content provider is only required if you need to share
44 * data between multiple applications. For example, the contacts data is used by multiple
45 * applications and must be stored in a content provider. If you don't need to share data amongst
46 * multiple applications you can use a database directly via
47 * {@link android.database.sqlite.SQLiteDatabase}.
48 *
49 * <p>For more information, read <a href="{@docRoot}guide/topics/providers/content-providers.html">Content
50 * Providers</a>.</p>
51 *
52 * <p>When a request is made via
53 * a {@link ContentResolver} the system inspects the authority of the given URI and passes the
54 * request to the content provider registered with the authority. The content provider can interpret
55 * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing
56 * URIs.</p>
57 *
58 * <p>The primary methods that need to be implemented are:
59 * <ul>
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070060 * <li>{@link #onCreate} which is called to initialize the provider</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * <li>{@link #query} which returns data to the caller</li>
62 * <li>{@link #insert} which inserts new data into the content provider</li>
63 * <li>{@link #update} which updates existing data in the content provider</li>
64 * <li>{@link #delete} which deletes data from the content provider</li>
65 * <li>{@link #getType} which returns the MIME type of data in the content provider</li>
66 * </ul></p>
67 *
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070068 * <p class="caution">Data access methods (such as {@link #insert} and
69 * {@link #update}) may be called from many threads at once, and must be thread-safe.
70 * Other methods (such as {@link #onCreate}) are only called from the application
71 * main thread, and must avoid performing lengthy operations. See the method
72 * descriptions for their expected thread behavior.</p>
73 *
74 * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate
75 * ContentProvider instance, so subclasses don't have to worry about the details of
76 * cross-process calls.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 */
78public abstract class ContentProvider implements ComponentCallbacks {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090079 /*
80 * Note: if you add methods to ContentProvider, you must add similar methods to
81 * MockContentProvider.
82 */
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private Context mContext = null;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070085 private int mMyUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private String mReadPermission;
87 private String mWritePermission;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070088 private PathPermission[] mPathPermissions;
Dianne Hackbornb424b632010-08-18 15:59:05 -070089 private boolean mExported;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 private Transport mTransport = new Transport();
92
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070093 /**
94 * Construct a ContentProvider instance. Content providers must be
95 * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared
96 * in the manifest</a>, accessed with {@link ContentResolver}, and created
97 * automatically by the system, so applications usually do not create
98 * ContentProvider instances directly.
99 *
100 * <p>At construction time, the object is uninitialized, and most fields and
101 * methods are unavailable. Subclasses should initialize themselves in
102 * {@link #onCreate}, not the constructor.
103 *
104 * <p>Content providers are created on the application main thread at
105 * application launch time. The constructor must not perform lengthy
106 * operations, or application startup will be delayed.
107 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900108 public ContentProvider() {
109 }
110
111 /**
112 * Constructor just for mocking.
113 *
114 * @param context A Context object which should be some mock instance (like the
115 * instance of {@link android.test.mock.MockContext}).
116 * @param readPermission The read permision you want this instance should have in the
117 * test, which is available via {@link #getReadPermission()}.
118 * @param writePermission The write permission you want this instance should have
119 * in the test, which is available via {@link #getWritePermission()}.
120 * @param pathPermissions The PathPermissions you want this instance should have
121 * in the test, which is available via {@link #getPathPermissions()}.
122 * @hide
123 */
124 public ContentProvider(
125 Context context,
126 String readPermission,
127 String writePermission,
128 PathPermission[] pathPermissions) {
129 mContext = context;
130 mReadPermission = readPermission;
131 mWritePermission = writePermission;
132 mPathPermissions = pathPermissions;
133 }
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
136 * Given an IContentProvider, try to coerce it back to the real
137 * ContentProvider object if it is running in the local process. This can
138 * be used if you know you are running in the same process as a provider,
139 * and want to get direct access to its implementation details. Most
140 * clients should not nor have a reason to use it.
141 *
142 * @param abstractInterface The ContentProvider interface that is to be
143 * coerced.
144 * @return If the IContentProvider is non-null and local, returns its actual
145 * ContentProvider instance. Otherwise returns null.
146 * @hide
147 */
148 public static ContentProvider coerceToLocalContentProvider(
149 IContentProvider abstractInterface) {
150 if (abstractInterface instanceof Transport) {
151 return ((Transport)abstractInterface).getContentProvider();
152 }
153 return null;
154 }
155
156 /**
157 * Binder object that deals with remoting.
158 *
159 * @hide
160 */
161 class Transport extends ContentProviderNative {
162 ContentProvider getContentProvider() {
163 return ContentProvider.this;
164 }
165
166 /**
167 * Remote version of a query, which returns an IBulkCursor. The bulk
168 * cursor should be wrapped with BulkCursorToCursorAdaptor before use.
169 */
170 public IBulkCursor bulkQuery(Uri uri, String[] projection,
171 String selection, String[] selectionArgs, String sortOrder,
172 IContentObserver observer, CursorWindow window) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700173 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 Cursor cursor = ContentProvider.this.query(uri, projection,
175 selection, selectionArgs, sortOrder);
176 if (cursor == null) {
177 return null;
178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 return new CursorToBulkCursorAdaptor(cursor, observer,
180 ContentProvider.this.getClass().getName(),
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700181 hasWritePermission(uri), window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
183
184 public Cursor query(Uri uri, String[] projection,
185 String selection, String[] selectionArgs, String sortOrder) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700186 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 return ContentProvider.this.query(uri, projection, selection,
188 selectionArgs, sortOrder);
189 }
190
191 public String getType(Uri uri) {
192 return ContentProvider.this.getType(uri);
193 }
194
195
196 public Uri insert(Uri uri, ContentValues initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700197 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 return ContentProvider.this.insert(uri, initialValues);
199 }
200
201 public int bulkInsert(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.bulkInsert(uri, initialValues);
204 }
205
Fred Quintana03d94902009-05-22 14:23:31 -0700206 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintana89437372009-05-15 15:10:40 -0700207 throws OperationApplicationException {
208 for (ContentProviderOperation operation : operations) {
209 if (operation.isReadOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700210 enforceReadPermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700211 }
212
213 if (operation.isWriteOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700214 enforceWritePermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700215 }
216 }
217 return ContentProvider.this.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -0700218 }
219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 public int delete(Uri uri, String selection, String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700221 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 return ContentProvider.this.delete(uri, selection, selectionArgs);
223 }
224
225 public int update(Uri uri, ContentValues values, String selection,
226 String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700227 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 return ContentProvider.this.update(uri, values, selection, selectionArgs);
229 }
230
231 public ParcelFileDescriptor openFile(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.openFile(uri, mode);
236 }
237
238 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
239 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700240 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
241 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 return ContentProvider.this.openAssetFile(uri, mode);
243 }
244
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800245 /**
246 * @hide
247 */
248 public Bundle call(String method, String request, Bundle args) {
249 return ContentProvider.this.call(method, request, args);
250 }
251
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700252 private void enforceReadPermission(Uri uri) {
253 final int uid = Binder.getCallingUid();
254 if (uid == mMyUid) {
255 return;
256 }
257
258 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 final String rperm = getReadPermission();
260 final int pid = Binder.getCallingPid();
Dianne Hackbornb424b632010-08-18 15:59:05 -0700261 if (mExported && (rperm == null
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700262 || context.checkPermission(rperm, pid, uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -0700263 == PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700264 return;
265 }
266
267 PathPermission[] pps = getPathPermissions();
268 if (pps != null) {
269 final String path = uri.getPath();
270 int i = pps.length;
271 while (i > 0) {
272 i--;
273 final PathPermission pp = pps[i];
274 final String pprperm = pp.getReadPermission();
275 if (pprperm != null && pp.match(path)) {
276 if (context.checkPermission(pprperm, pid, uid)
277 == PackageManager.PERMISSION_GRANTED) {
278 return;
279 }
280 }
281 }
282 }
283
284 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 Intent.FLAG_GRANT_READ_URI_PERMISSION)
286 == PackageManager.PERMISSION_GRANTED) {
287 return;
288 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 String msg = "Permission Denial: reading "
291 + ContentProvider.this.getClass().getName()
292 + " uri " + uri + " from pid=" + Binder.getCallingPid()
293 + ", uid=" + Binder.getCallingUid()
294 + " requires " + rperm;
295 throw new SecurityException(msg);
296 }
297
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700298 private boolean hasWritePermission(Uri uri) {
299 final int uid = Binder.getCallingUid();
300 if (uid == mMyUid) {
301 return true;
302 }
303
304 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 final String wperm = getWritePermission();
306 final int pid = Binder.getCallingPid();
Dianne Hackbornb424b632010-08-18 15:59:05 -0700307 if (mExported && (wperm == null
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700308 || context.checkPermission(wperm, pid, uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -0700309 == PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700310 return true;
311 }
312
313 PathPermission[] pps = getPathPermissions();
314 if (pps != null) {
315 final String path = uri.getPath();
316 int i = pps.length;
317 while (i > 0) {
318 i--;
319 final PathPermission pp = pps[i];
320 final String ppwperm = pp.getWritePermission();
321 if (ppwperm != null && pp.match(path)) {
322 if (context.checkPermission(ppwperm, pid, uid)
323 == PackageManager.PERMISSION_GRANTED) {
324 return true;
325 }
326 }
327 }
328 }
329
330 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
332 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700333 return true;
334 }
335
336 return false;
337 }
338
339 private void enforceWritePermission(Uri uri) {
340 if (hasWritePermission(uri)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 return;
342 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 String msg = "Permission Denial: writing "
345 + ContentProvider.this.getClass().getName()
346 + " uri " + uri + " from pid=" + Binder.getCallingPid()
347 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700348 + " requires " + getWritePermission();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 throw new SecurityException(msg);
350 }
351 }
352
353
354 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700355 * Retrieves the Context this provider is running in. Only available once
356 * {@link #onCreate} has been called -- this will return null in the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * constructor.
358 */
359 public final Context getContext() {
360 return mContext;
361 }
362
363 /**
364 * Change the permission required to read data from the content
365 * provider. This is normally set for you from its manifest information
366 * when the provider is first created.
367 *
368 * @param permission Name of the permission required for read-only access.
369 */
370 protected final void setReadPermission(String permission) {
371 mReadPermission = permission;
372 }
373
374 /**
375 * Return the name of the permission required for read-only access to
376 * this content provider. This method can be called from multiple
377 * threads, as described in
378 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
379 * Processes and Threads</a>.
380 */
381 public final String getReadPermission() {
382 return mReadPermission;
383 }
384
385 /**
386 * Change the permission required to read and write data in the content
387 * provider. This is normally set for you from its manifest information
388 * when the provider is first created.
389 *
390 * @param permission Name of the permission required for read/write access.
391 */
392 protected final void setWritePermission(String permission) {
393 mWritePermission = permission;
394 }
395
396 /**
397 * Return the name of the permission required for read/write access to
398 * this content provider. This method can be called from multiple
399 * threads, as described in
400 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
401 * Processes and Threads</a>.
402 */
403 public final String getWritePermission() {
404 return mWritePermission;
405 }
406
407 /**
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700408 * Change the path-based permission required to read and/or write data in
409 * the content provider. This is normally set for you from its manifest
410 * information when the provider is first created.
411 *
412 * @param permissions Array of path permission descriptions.
413 */
414 protected final void setPathPermissions(PathPermission[] permissions) {
415 mPathPermissions = permissions;
416 }
417
418 /**
419 * Return the path-based permissions required for read and/or write access to
420 * this content provider. 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 public final PathPermission[] getPathPermissions() {
426 return mPathPermissions;
427 }
428
429 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700430 * Implement this to initialize your content provider on startup.
431 * This method is called for all registered content providers on the
432 * application main thread at application launch time. It must not perform
433 * lengthy operations, or application startup will be delayed.
434 *
435 * <p>You should defer nontrivial initialization (such as opening,
436 * upgrading, and scanning databases) until the content provider is used
437 * (via {@link #query}, {@link #insert}, etc). Deferred initialization
438 * keeps application startup fast, avoids unnecessary work if the provider
439 * turns out not to be needed, and stops database errors (such as a full
440 * disk) from halting application launch.
441 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700442 * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper}
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700443 * is a helpful utility class that makes it easy to manage databases,
444 * and will automatically defer opening until first use. If you do use
445 * SQLiteOpenHelper, make sure to avoid calling
446 * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or
447 * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase}
448 * from this method. (Instead, override
449 * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the
450 * database when it is first opened.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 *
452 * @return true if the provider was successfully loaded, false otherwise
453 */
454 public abstract boolean onCreate();
455
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700456 /**
457 * {@inheritDoc}
458 * This method is always called on the application main thread, and must
459 * not perform lengthy operations.
460 *
461 * <p>The default content provider implementation does nothing.
462 * Override this method to take appropriate action.
463 * (Content providers do not usually care about things like screen
464 * orientation, but may want to know about locale changes.)
465 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public void onConfigurationChanged(Configuration newConfig) {
467 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700468
469 /**
470 * {@inheritDoc}
471 * This method is always called on the application main thread, and must
472 * not perform lengthy operations.
473 *
474 * <p>The default content provider implementation does nothing.
475 * Subclasses may override this method to take appropriate action.
476 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 public void onLowMemory() {
478 }
479
480 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700481 * Implement this to handle query requests from clients.
482 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
484 * Processes and Threads</a>.
485 * <p>
486 * Example client call:<p>
487 * <pre>// Request a specific record.
488 * Cursor managedCursor = managedQuery(
Alan Jones81a476f2009-05-21 12:32:17 +1000489 ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 projection, // Which columns to return.
491 null, // WHERE clause.
Alan Jones81a476f2009-05-21 12:32:17 +1000492 null, // WHERE clause value substitution
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 People.NAME + " ASC"); // Sort order.</pre>
494 * Example implementation:<p>
495 * <pre>// SQLiteQueryBuilder is a helper class that creates the
496 // proper SQL syntax for us.
497 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
498
499 // Set the table we're querying.
500 qBuilder.setTables(DATABASE_TABLE_NAME);
501
502 // If the query ends in a specific record number, we're
503 // being asked for a specific record, so set the
504 // WHERE clause in our query.
505 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
506 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
507 }
508
509 // Make the query.
510 Cursor c = qBuilder.query(mDb,
511 projection,
512 selection,
513 selectionArgs,
514 groupBy,
515 having,
516 sortOrder);
517 c.setNotificationUri(getContext().getContentResolver(), uri);
518 return c;</pre>
519 *
520 * @param uri The URI to query. This will be the full URI sent by the client;
Alan Jones81a476f2009-05-21 12:32:17 +1000521 * if the client is requesting a specific record, the URI will end in a record number
522 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
523 * that _id value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * @param projection The list of columns to put into the cursor. If
525 * null all columns are included.
526 * @param selection A selection criteria to apply when filtering rows.
527 * If null then all rows are included.
Alan Jones81a476f2009-05-21 12:32:17 +1000528 * @param selectionArgs You may include ?s in selection, which will be replaced by
529 * the values from selectionArgs, in order that they appear in the selection.
530 * The values will be bound as Strings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * @param sortOrder How the rows in the cursor should be sorted.
Alan Jones81a476f2009-05-21 12:32:17 +1000532 * If null then the provider is free to define the sort order.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * @return a Cursor or null.
534 */
535 public abstract Cursor query(Uri uri, String[] projection,
536 String selection, String[] selectionArgs, String sortOrder);
537
Fred Quintana5bba6322009-10-05 14:21:12 -0700538 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700539 * Implement this to handle requests for the MIME type of the data at the
540 * given URI. The returned MIME type should start with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 * <code>vnd.android.cursor.item</code> for a single record,
542 * or <code>vnd.android.cursor.dir/</code> for multiple items.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700543 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
545 * Processes and Threads</a>.
546 *
547 * @param uri the URI to query.
548 * @return a MIME type string, or null if there is no type.
549 */
550 public abstract String getType(Uri uri);
551
552 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700553 * Implement this to handle requests to insert a new row.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
555 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700556 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
558 * Processes and Threads</a>.
559 * @param uri The content:// URI of the insertion request.
560 * @param values A set of column_name/value pairs to add to the database.
561 * @return The URI for the newly inserted item.
562 */
563 public abstract Uri insert(Uri uri, ContentValues values);
564
565 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700566 * Override this to handle requests to insert a set of new rows, or the
567 * default implementation will iterate over the values and call
568 * {@link #insert} on each of them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
570 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700571 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
573 * Processes and Threads</a>.
574 *
575 * @param uri The content:// URI of the insertion request.
576 * @param values An array of sets of column_name/value pairs to add to the database.
577 * @return The number of values that were inserted.
578 */
579 public int bulkInsert(Uri uri, ContentValues[] values) {
580 int numValues = values.length;
581 for (int i = 0; i < numValues; i++) {
582 insert(uri, values[i]);
583 }
584 return numValues;
585 }
586
587 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700588 * Implement this to handle requests to delete one or more rows.
589 * The implementation should apply the selection clause when performing
590 * deletion, allowing the operation to affect multiple rows in a directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()}
592 * after deleting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700593 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
595 * Processes and Threads</a>.
596 *
597 * <p>The implementation is responsible for parsing out a row ID at the end
598 * of the URI, if a specific row is being deleted. That is, the client would
599 * pass in <code>content://contacts/people/22</code> and the implementation is
600 * responsible for parsing the record number (22) when creating a SQL statement.
601 *
602 * @param uri The full URI to query, including a row ID (if a specific record is requested).
603 * @param selection An optional restriction to apply to rows when deleting.
604 * @return The number of rows affected.
605 * @throws SQLException
606 */
607 public abstract int delete(Uri uri, String selection, String[] selectionArgs);
608
609 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700610 * Implement this to handle requests to update one or more rows.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700611 * The implementation should update all rows matching the selection
612 * to set the columns according to the provided values map.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
614 * after updating.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700615 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
617 * Processes and Threads</a>.
618 *
619 * @param uri The URI to query. This can potentially have a record ID if this
620 * is an update request for a specific record.
621 * @param values A Bundle mapping from column names to new column values (NULL is a
622 * valid value).
623 * @param selection An optional filter to match rows to update.
624 * @return the number of rows affected.
625 */
626 public abstract int update(Uri uri, ContentValues values, String selection,
627 String[] selectionArgs);
628
629 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700630 * Override this to handle requests to open a file blob.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700631 * The default implementation always throws {@link FileNotFoundException}.
632 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
634 * Processes and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700635 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700636 * <p>This method returns a ParcelFileDescriptor, which is returned directly
637 * to the caller. This way large data (such as images and documents) can be
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700638 * returned without copying the content.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 *
640 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
641 * their responsibility to close it when done. That is, the implementation
642 * of this method should create a new ParcelFileDescriptor for each call.
643 *
644 * @param uri The URI whose file is to be opened.
645 * @param mode Access mode for the file. May be "r" for read-only access,
646 * "rw" for read and write access, or "rwt" for read and write access
647 * that truncates any existing file.
648 *
649 * @return Returns a new ParcelFileDescriptor which you can use to access
650 * the file.
651 *
652 * @throws FileNotFoundException Throws FileNotFoundException if there is
653 * no file associated with the given URI or the mode is invalid.
654 * @throws SecurityException Throws SecurityException if the caller does
655 * not have permission to access the file.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 * @see #openAssetFile(Uri, String)
658 * @see #openFileHelper(Uri, String)
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700659 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public ParcelFileDescriptor openFile(Uri uri, String mode)
661 throws FileNotFoundException {
662 throw new FileNotFoundException("No files supported by provider at "
663 + uri);
664 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 /**
667 * This is like {@link #openFile}, but can be implemented by providers
668 * that need to be able to return sub-sections of files, often assets
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700669 * inside of their .apk.
670 * This method can be called from multiple threads, as described in
671 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
672 * Processes and Threads</a>.
673 *
674 * <p>If you implement this, your clients must be able to deal with such
Dan Egnor17876aa2010-07-28 12:28:04 -0700675 * file slices, either directly with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700676 * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
678 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
679 * methods.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700680 *
681 * <p class="note">If you are implementing this to return a full file, you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 * should create the AssetFileDescriptor with
683 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700684 * applications that can not handle sub-sections of files.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 *
686 * @param uri The URI whose file is to be opened.
687 * @param mode Access mode for the file. May be "r" for read-only access,
688 * "w" for write-only access (erasing whatever data is currently in
689 * the file), "wa" for write-only access to append to any existing data,
690 * "rw" for read and write access on any existing data, and "rwt" for read
691 * and write access that truncates any existing file.
692 *
693 * @return Returns a new AssetFileDescriptor which you can use to access
694 * the file.
695 *
696 * @throws FileNotFoundException Throws FileNotFoundException if there is
697 * no file associated with the given URI or the mode is invalid.
698 * @throws SecurityException Throws SecurityException if the caller does
699 * not have permission to access the file.
700 *
701 * @see #openFile(Uri, String)
702 * @see #openFileHelper(Uri, String)
703 */
704 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
705 throws FileNotFoundException {
706 ParcelFileDescriptor fd = openFile(uri, mode);
707 return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
708 }
709
710 /**
711 * Convenience for subclasses that wish to implement {@link #openFile}
712 * by looking up a column named "_data" at the given URI.
713 *
714 * @param uri The URI to be opened.
715 * @param mode The file mode. May be "r" for read-only access,
716 * "w" for write-only access (erasing whatever data is currently in
717 * the file), "wa" for write-only access to append to any existing data,
718 * "rw" for read and write access on any existing data, and "rwt" for read
719 * and write access that truncates any existing file.
720 *
721 * @return Returns a new ParcelFileDescriptor that can be used by the
722 * client to access the file.
723 */
724 protected final ParcelFileDescriptor openFileHelper(Uri uri,
725 String mode) throws FileNotFoundException {
726 Cursor c = query(uri, new String[]{"_data"}, null, null, null);
727 int count = (c != null) ? c.getCount() : 0;
728 if (count != 1) {
729 // If there is not exactly one result, throw an appropriate
730 // exception.
731 if (c != null) {
732 c.close();
733 }
734 if (count == 0) {
735 throw new FileNotFoundException("No entry for " + uri);
736 }
737 throw new FileNotFoundException("Multiple items at " + uri);
738 }
739
740 c.moveToFirst();
741 int i = c.getColumnIndex("_data");
742 String path = (i >= 0 ? c.getString(i) : null);
743 c.close();
744 if (path == null) {
745 throw new FileNotFoundException("Column _data not found.");
746 }
747
748 int modeBits = ContentResolver.modeToMode(uri, mode);
749 return ParcelFileDescriptor.open(new File(path), modeBits);
750 }
751
752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 * Returns true if this instance is a temporary content provider.
754 * @return true if this instance is a temporary content provider
755 */
756 protected boolean isTemporary() {
757 return false;
758 }
759
760 /**
761 * Returns the Binder object for this provider.
762 *
763 * @return the Binder object for this provider
764 * @hide
765 */
766 public IContentProvider getIContentProvider() {
767 return mTransport;
768 }
769
770 /**
771 * After being instantiated, this is called to tell the content provider
772 * about itself.
773 *
774 * @param context The context this provider is running in
775 * @param info Registered information about this content provider
776 */
777 public void attachInfo(Context context, ProviderInfo info) {
778
779 /*
780 * Only allow it to be set once, so after the content service gives
781 * this to us clients can't change it.
782 */
783 if (mContext == null) {
784 mContext = context;
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700785 mMyUid = Process.myUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 if (info != null) {
787 setReadPermission(info.readPermission);
788 setWritePermission(info.writePermission);
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700789 setPathPermissions(info.pathPermissions);
Dianne Hackbornb424b632010-08-18 15:59:05 -0700790 mExported = info.exported;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792 ContentProvider.this.onCreate();
793 }
794 }
Fred Quintanace31b232009-05-04 16:01:15 -0700795
796 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700797 * Override this to handle requests to perform a batch of operations, or the
798 * default implementation will iterate over the operations and call
799 * {@link ContentProviderOperation#apply} on each of them.
800 * If all calls to {@link ContentProviderOperation#apply} succeed
801 * then a {@link ContentProviderResult} array with as many
802 * elements as there were operations will be returned. If any of the calls
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700803 * fail, it is up to the implementation how many of the others take effect.
804 * This method can be called from multiple threads, as described in
805 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
806 * Processes and Threads</a>.
807 *
Fred Quintanace31b232009-05-04 16:01:15 -0700808 * @param operations the operations to apply
809 * @return the results of the applications
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700810 * @throws OperationApplicationException thrown if any operation fails.
811 * @see ContentProviderOperation#apply
Fred Quintanace31b232009-05-04 16:01:15 -0700812 */
Fred Quintana03d94902009-05-22 14:23:31 -0700813 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintanace31b232009-05-04 16:01:15 -0700814 throws OperationApplicationException {
Fred Quintana03d94902009-05-22 14:23:31 -0700815 final int numOperations = operations.size();
816 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
817 for (int i = 0; i < numOperations; i++) {
818 results[i] = operations.get(i).apply(this, results, i);
Fred Quintanace31b232009-05-04 16:01:15 -0700819 }
820 return results;
821 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800822
823 /**
824 * @hide -- until interface has proven itself
825 *
826 * Call an provider-defined method. This can be used to implement
827 * interfaces that are cheaper than using a Cursor.
828 *
829 * @param method Method name to call. Opaque to framework.
830 * @param request Nullable String argument passed to method.
831 * @param args Nullable Bundle argument passed to method.
832 */
833 public Bundle call(String method, String request, Bundle args) {
834 return null;
835 }
836}