blob: 9b9f796a1607860900ba56d3594c01cffee5cc7c [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 private Transport mTransport = new Transport();
91
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070092 /**
93 * Construct a ContentProvider instance. Content providers must be
94 * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared
95 * in the manifest</a>, accessed with {@link ContentResolver}, and created
96 * automatically by the system, so applications usually do not create
97 * ContentProvider instances directly.
98 *
99 * <p>At construction time, the object is uninitialized, and most fields and
100 * methods are unavailable. Subclasses should initialize themselves in
101 * {@link #onCreate}, not the constructor.
102 *
103 * <p>Content providers are created on the application main thread at
104 * application launch time. The constructor must not perform lengthy
105 * operations, or application startup will be delayed.
106 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900107 public ContentProvider() {
108 }
109
110 /**
111 * Constructor just for mocking.
112 *
113 * @param context A Context object which should be some mock instance (like the
114 * instance of {@link android.test.mock.MockContext}).
115 * @param readPermission The read permision you want this instance should have in the
116 * test, which is available via {@link #getReadPermission()}.
117 * @param writePermission The write permission you want this instance should have
118 * in the test, which is available via {@link #getWritePermission()}.
119 * @param pathPermissions The PathPermissions you want this instance should have
120 * in the test, which is available via {@link #getPathPermissions()}.
121 * @hide
122 */
123 public ContentProvider(
124 Context context,
125 String readPermission,
126 String writePermission,
127 PathPermission[] pathPermissions) {
128 mContext = context;
129 mReadPermission = readPermission;
130 mWritePermission = writePermission;
131 mPathPermissions = pathPermissions;
132 }
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
135 * Given an IContentProvider, try to coerce it back to the real
136 * ContentProvider object if it is running in the local process. This can
137 * be used if you know you are running in the same process as a provider,
138 * and want to get direct access to its implementation details. Most
139 * clients should not nor have a reason to use it.
140 *
141 * @param abstractInterface The ContentProvider interface that is to be
142 * coerced.
143 * @return If the IContentProvider is non-null and local, returns its actual
144 * ContentProvider instance. Otherwise returns null.
145 * @hide
146 */
147 public static ContentProvider coerceToLocalContentProvider(
148 IContentProvider abstractInterface) {
149 if (abstractInterface instanceof Transport) {
150 return ((Transport)abstractInterface).getContentProvider();
151 }
152 return null;
153 }
154
155 /**
156 * Binder object that deals with remoting.
157 *
158 * @hide
159 */
160 class Transport extends ContentProviderNative {
161 ContentProvider getContentProvider() {
162 return ContentProvider.this;
163 }
164
165 /**
166 * Remote version of a query, which returns an IBulkCursor. The bulk
167 * cursor should be wrapped with BulkCursorToCursorAdaptor before use.
168 */
169 public IBulkCursor bulkQuery(Uri uri, String[] projection,
170 String selection, String[] selectionArgs, String sortOrder,
171 IContentObserver observer, CursorWindow window) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700172 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 Cursor cursor = ContentProvider.this.query(uri, projection,
174 selection, selectionArgs, sortOrder);
175 if (cursor == null) {
176 return null;
177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 return new CursorToBulkCursorAdaptor(cursor, observer,
179 ContentProvider.this.getClass().getName(),
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700180 hasWritePermission(uri), window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 }
182
183 public Cursor query(Uri uri, String[] projection,
184 String selection, String[] selectionArgs, String sortOrder) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700185 enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 return ContentProvider.this.query(uri, projection, selection,
187 selectionArgs, sortOrder);
188 }
189
190 public String getType(Uri uri) {
191 return ContentProvider.this.getType(uri);
192 }
193
194
195 public Uri insert(Uri uri, ContentValues initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700196 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 return ContentProvider.this.insert(uri, initialValues);
198 }
199
200 public int bulkInsert(Uri uri, ContentValues[] initialValues) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700201 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 return ContentProvider.this.bulkInsert(uri, initialValues);
203 }
204
Fred Quintana03d94902009-05-22 14:23:31 -0700205 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintana89437372009-05-15 15:10:40 -0700206 throws OperationApplicationException {
207 for (ContentProviderOperation operation : operations) {
208 if (operation.isReadOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700209 enforceReadPermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700210 }
211
212 if (operation.isWriteOperation()) {
Dianne Hackborne3f05442009-07-09 12:15:46 -0700213 enforceWritePermission(operation.getUri());
Fred Quintana89437372009-05-15 15:10:40 -0700214 }
215 }
216 return ContentProvider.this.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -0700217 }
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public int delete(Uri uri, String selection, 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.delete(uri, selection, selectionArgs);
222 }
223
224 public int update(Uri uri, ContentValues values, String selection,
225 String[] selectionArgs) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700226 enforceWritePermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 return ContentProvider.this.update(uri, values, selection, selectionArgs);
228 }
229
230 public ParcelFileDescriptor openFile(Uri uri, String mode)
231 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700232 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
233 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 return ContentProvider.this.openFile(uri, mode);
235 }
236
237 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
238 throws FileNotFoundException {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700239 if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri);
240 else enforceReadPermission(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 return ContentProvider.this.openAssetFile(uri, mode);
242 }
243
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800244 /**
245 * @hide
246 */
247 public Bundle call(String method, String request, Bundle args) {
248 return ContentProvider.this.call(method, request, args);
249 }
250
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700251 private void enforceReadPermission(Uri uri) {
252 final int uid = Binder.getCallingUid();
253 if (uid == mMyUid) {
254 return;
255 }
256
257 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final String rperm = getReadPermission();
259 final int pid = Binder.getCallingPid();
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700260 if (rperm == null
261 || context.checkPermission(rperm, pid, uid)
262 == PackageManager.PERMISSION_GRANTED) {
263 return;
264 }
265
266 PathPermission[] pps = getPathPermissions();
267 if (pps != null) {
268 final String path = uri.getPath();
269 int i = pps.length;
270 while (i > 0) {
271 i--;
272 final PathPermission pp = pps[i];
273 final String pprperm = pp.getReadPermission();
274 if (pprperm != null && pp.match(path)) {
275 if (context.checkPermission(pprperm, pid, uid)
276 == PackageManager.PERMISSION_GRANTED) {
277 return;
278 }
279 }
280 }
281 }
282
283 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 Intent.FLAG_GRANT_READ_URI_PERMISSION)
285 == PackageManager.PERMISSION_GRANTED) {
286 return;
287 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 String msg = "Permission Denial: reading "
290 + ContentProvider.this.getClass().getName()
291 + " uri " + uri + " from pid=" + Binder.getCallingPid()
292 + ", uid=" + Binder.getCallingUid()
293 + " requires " + rperm;
294 throw new SecurityException(msg);
295 }
296
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700297 private boolean hasWritePermission(Uri uri) {
298 final int uid = Binder.getCallingUid();
299 if (uid == mMyUid) {
300 return true;
301 }
302
303 final Context context = getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 final String wperm = getWritePermission();
305 final int pid = Binder.getCallingPid();
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700306 if (wperm == null
307 || context.checkPermission(wperm, pid, uid)
308 == PackageManager.PERMISSION_GRANTED) {
309 return true;
310 }
311
312 PathPermission[] pps = getPathPermissions();
313 if (pps != null) {
314 final String path = uri.getPath();
315 int i = pps.length;
316 while (i > 0) {
317 i--;
318 final PathPermission pp = pps[i];
319 final String ppwperm = pp.getWritePermission();
320 if (ppwperm != null && pp.match(path)) {
321 if (context.checkPermission(ppwperm, pid, uid)
322 == PackageManager.PERMISSION_GRANTED) {
323 return true;
324 }
325 }
326 }
327 }
328
329 if (context.checkUriPermission(uri, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
331 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700332 return true;
333 }
334
335 return false;
336 }
337
338 private void enforceWritePermission(Uri uri) {
339 if (hasWritePermission(uri)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 return;
341 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 String msg = "Permission Denial: writing "
344 + ContentProvider.this.getClass().getName()
345 + " uri " + uri + " from pid=" + Binder.getCallingPid()
346 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700347 + " requires " + getWritePermission();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 throw new SecurityException(msg);
349 }
350 }
351
352
353 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700354 * Retrieves the Context this provider is running in. Only available once
355 * {@link #onCreate} has been called -- this will return null in the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * constructor.
357 */
358 public final Context getContext() {
359 return mContext;
360 }
361
362 /**
363 * Change the permission required to read data from the content
364 * provider. This is normally set for you from its manifest information
365 * when the provider is first created.
366 *
367 * @param permission Name of the permission required for read-only access.
368 */
369 protected final void setReadPermission(String permission) {
370 mReadPermission = permission;
371 }
372
373 /**
374 * Return the name of the permission required for read-only access to
375 * this content provider. This method can be called from multiple
376 * threads, as described in
377 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
378 * Processes and Threads</a>.
379 */
380 public final String getReadPermission() {
381 return mReadPermission;
382 }
383
384 /**
385 * Change the permission required to read and write data in the content
386 * provider. This is normally set for you from its manifest information
387 * when the provider is first created.
388 *
389 * @param permission Name of the permission required for read/write access.
390 */
391 protected final void setWritePermission(String permission) {
392 mWritePermission = permission;
393 }
394
395 /**
396 * Return the name of the permission required for read/write access to
397 * this content provider. This method can be called from multiple
398 * threads, as described in
399 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
400 * Processes and Threads</a>.
401 */
402 public final String getWritePermission() {
403 return mWritePermission;
404 }
405
406 /**
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700407 * Change the path-based permission required to read and/or write data in
408 * the content provider. This is normally set for you from its manifest
409 * information when the provider is first created.
410 *
411 * @param permissions Array of path permission descriptions.
412 */
413 protected final void setPathPermissions(PathPermission[] permissions) {
414 mPathPermissions = permissions;
415 }
416
417 /**
418 * Return the path-based permissions required for read and/or write access to
419 * this content provider. This method can be called from multiple
420 * threads, as described in
421 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
422 * Processes and Threads</a>.
423 */
424 public final PathPermission[] getPathPermissions() {
425 return mPathPermissions;
426 }
427
428 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700429 * Implement this to initialize your content provider on startup.
430 * This method is called for all registered content providers on the
431 * application main thread at application launch time. It must not perform
432 * lengthy operations, or application startup will be delayed.
433 *
434 * <p>You should defer nontrivial initialization (such as opening,
435 * upgrading, and scanning databases) until the content provider is used
436 * (via {@link #query}, {@link #insert}, etc). Deferred initialization
437 * keeps application startup fast, avoids unnecessary work if the provider
438 * turns out not to be needed, and stops database errors (such as a full
439 * disk) from halting application launch.
440 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700441 * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper}
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700442 * is a helpful utility class that makes it easy to manage databases,
443 * and will automatically defer opening until first use. If you do use
444 * SQLiteOpenHelper, make sure to avoid calling
445 * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or
446 * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase}
447 * from this method. (Instead, override
448 * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the
449 * database when it is first opened.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 *
451 * @return true if the provider was successfully loaded, false otherwise
452 */
453 public abstract boolean onCreate();
454
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700455 /**
456 * {@inheritDoc}
457 * This method is always called on the application main thread, and must
458 * not perform lengthy operations.
459 *
460 * <p>The default content provider implementation does nothing.
461 * Override this method to take appropriate action.
462 * (Content providers do not usually care about things like screen
463 * orientation, but may want to know about locale changes.)
464 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 public void onConfigurationChanged(Configuration newConfig) {
466 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700467
468 /**
469 * {@inheritDoc}
470 * This method is always called on the application main thread, and must
471 * not perform lengthy operations.
472 *
473 * <p>The default content provider implementation does nothing.
474 * Subclasses may override this method to take appropriate action.
475 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 public void onLowMemory() {
477 }
478
479 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700480 * Implement this to handle query requests from clients.
481 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
483 * Processes and Threads</a>.
484 * <p>
485 * Example client call:<p>
486 * <pre>// Request a specific record.
487 * Cursor managedCursor = managedQuery(
Alan Jones81a476f2009-05-21 12:32:17 +1000488 ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 projection, // Which columns to return.
490 null, // WHERE clause.
Alan Jones81a476f2009-05-21 12:32:17 +1000491 null, // WHERE clause value substitution
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 People.NAME + " ASC"); // Sort order.</pre>
493 * Example implementation:<p>
494 * <pre>// SQLiteQueryBuilder is a helper class that creates the
495 // proper SQL syntax for us.
496 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
497
498 // Set the table we're querying.
499 qBuilder.setTables(DATABASE_TABLE_NAME);
500
501 // If the query ends in a specific record number, we're
502 // being asked for a specific record, so set the
503 // WHERE clause in our query.
504 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
505 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
506 }
507
508 // Make the query.
509 Cursor c = qBuilder.query(mDb,
510 projection,
511 selection,
512 selectionArgs,
513 groupBy,
514 having,
515 sortOrder);
516 c.setNotificationUri(getContext().getContentResolver(), uri);
517 return c;</pre>
518 *
519 * @param uri The URI to query. This will be the full URI sent by the client;
Alan Jones81a476f2009-05-21 12:32:17 +1000520 * if the client is requesting a specific record, the URI will end in a record number
521 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
522 * that _id value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * @param projection The list of columns to put into the cursor. If
524 * null all columns are included.
525 * @param selection A selection criteria to apply when filtering rows.
526 * If null then all rows are included.
Alan Jones81a476f2009-05-21 12:32:17 +1000527 * @param selectionArgs You may include ?s in selection, which will be replaced by
528 * the values from selectionArgs, in order that they appear in the selection.
529 * The values will be bound as Strings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * @param sortOrder How the rows in the cursor should be sorted.
Alan Jones81a476f2009-05-21 12:32:17 +1000531 * If null then the provider is free to define the sort order.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * @return a Cursor or null.
533 */
534 public abstract Cursor query(Uri uri, String[] projection,
535 String selection, String[] selectionArgs, String sortOrder);
536
Fred Quintana5bba6322009-10-05 14:21:12 -0700537 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700538 * Implement this to handle requests for the MIME type of the data at the
539 * given URI. The returned MIME type should start with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 * <code>vnd.android.cursor.item</code> for a single record,
541 * or <code>vnd.android.cursor.dir/</code> for multiple items.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700542 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
544 * Processes and Threads</a>.
545 *
546 * @param uri the URI to query.
547 * @return a MIME type string, or null if there is no type.
548 */
549 public abstract String getType(Uri uri);
550
551 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700552 * Implement this to handle requests to insert a new row.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
554 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700555 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
557 * Processes and Threads</a>.
558 * @param uri The content:// URI of the insertion request.
559 * @param values A set of column_name/value pairs to add to the database.
560 * @return The URI for the newly inserted item.
561 */
562 public abstract Uri insert(Uri uri, ContentValues values);
563
564 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700565 * Override this to handle requests to insert a set of new rows, or the
566 * default implementation will iterate over the values and call
567 * {@link #insert} on each of them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
569 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700570 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
572 * Processes and Threads</a>.
573 *
574 * @param uri The content:// URI of the insertion request.
575 * @param values An array of sets of column_name/value pairs to add to the database.
576 * @return The number of values that were inserted.
577 */
578 public int bulkInsert(Uri uri, ContentValues[] values) {
579 int numValues = values.length;
580 for (int i = 0; i < numValues; i++) {
581 insert(uri, values[i]);
582 }
583 return numValues;
584 }
585
586 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700587 * Implement this to handle requests to delete one or more rows.
588 * The implementation should apply the selection clause when performing
589 * deletion, allowing the operation to affect multiple rows in a directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()}
591 * after deleting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700592 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
594 * Processes and Threads</a>.
595 *
596 * <p>The implementation is responsible for parsing out a row ID at the end
597 * of the URI, if a specific row is being deleted. That is, the client would
598 * pass in <code>content://contacts/people/22</code> and the implementation is
599 * responsible for parsing the record number (22) when creating a SQL statement.
600 *
601 * @param uri The full URI to query, including a row ID (if a specific record is requested).
602 * @param selection An optional restriction to apply to rows when deleting.
603 * @return The number of rows affected.
604 * @throws SQLException
605 */
606 public abstract int delete(Uri uri, String selection, String[] selectionArgs);
607
608 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700609 * Implement this to handle requests to update one or more rows.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700610 * The implementation should update all rows matching the selection
611 * to set the columns according to the provided values map.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
613 * after updating.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700614 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
616 * Processes and Threads</a>.
617 *
618 * @param uri The URI to query. This can potentially have a record ID if this
619 * is an update request for a specific record.
620 * @param values A Bundle mapping from column names to new column values (NULL is a
621 * valid value).
622 * @param selection An optional filter to match rows to update.
623 * @return the number of rows affected.
624 */
625 public abstract int update(Uri uri, ContentValues values, String selection,
626 String[] selectionArgs);
627
628 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700629 * Override this to handle requests to open a file blob.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700630 * The default implementation always throws {@link FileNotFoundException}.
631 * This method can be called from multiple threads, as described in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
633 * Processes and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700634 *
Dan Egnor17876aa2010-07-28 12:28:04 -0700635 * <p>This method returns a ParcelFileDescriptor, which is returned directly
636 * to the caller. This way large data (such as images and documents) can be
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700637 * returned without copying the content.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
640 * their responsibility to close it when done. That is, the implementation
641 * of this method should create a new ParcelFileDescriptor for each call.
642 *
643 * @param uri The URI whose file is to be opened.
644 * @param mode Access mode for the file. May be "r" for read-only access,
645 * "rw" for read and write access, or "rwt" for read and write access
646 * that truncates any existing file.
647 *
648 * @return Returns a new ParcelFileDescriptor which you can use to access
649 * the file.
650 *
651 * @throws FileNotFoundException Throws FileNotFoundException if there is
652 * no file associated with the given URI or the mode is invalid.
653 * @throws SecurityException Throws SecurityException if the caller does
654 * not have permission to access the file.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * @see #openAssetFile(Uri, String)
657 * @see #openFileHelper(Uri, String)
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700658 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 public ParcelFileDescriptor openFile(Uri uri, String mode)
660 throws FileNotFoundException {
661 throw new FileNotFoundException("No files supported by provider at "
662 + uri);
663 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 /**
666 * This is like {@link #openFile}, but can be implemented by providers
667 * that need to be able to return sub-sections of files, often assets
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700668 * inside of their .apk.
669 * This method can be called from multiple threads, as described in
670 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
671 * Processes and Threads</a>.
672 *
673 * <p>If you implement this, your clients must be able to deal with such
Dan Egnor17876aa2010-07-28 12:28:04 -0700674 * file slices, either directly with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700675 * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
677 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
678 * methods.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700679 *
680 * <p class="note">If you are implementing this to return a full file, you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * should create the AssetFileDescriptor with
682 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700683 * applications that can not handle sub-sections of files.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 *
685 * @param uri The URI whose file is to be opened.
686 * @param mode Access mode for the file. May be "r" for read-only access,
687 * "w" for write-only access (erasing whatever data is currently in
688 * the file), "wa" for write-only access to append to any existing data,
689 * "rw" for read and write access on any existing data, and "rwt" for read
690 * and write access that truncates any existing file.
691 *
692 * @return Returns a new AssetFileDescriptor which you can use to access
693 * the file.
694 *
695 * @throws FileNotFoundException Throws FileNotFoundException if there is
696 * no file associated with the given URI or the mode is invalid.
697 * @throws SecurityException Throws SecurityException if the caller does
698 * not have permission to access the file.
699 *
700 * @see #openFile(Uri, String)
701 * @see #openFileHelper(Uri, String)
702 */
703 public AssetFileDescriptor openAssetFile(Uri uri, String mode)
704 throws FileNotFoundException {
705 ParcelFileDescriptor fd = openFile(uri, mode);
706 return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
707 }
708
709 /**
710 * Convenience for subclasses that wish to implement {@link #openFile}
711 * by looking up a column named "_data" at the given URI.
712 *
713 * @param uri The URI to be opened.
714 * @param mode The file mode. May be "r" for read-only access,
715 * "w" for write-only access (erasing whatever data is currently in
716 * the file), "wa" for write-only access to append to any existing data,
717 * "rw" for read and write access on any existing data, and "rwt" for read
718 * and write access that truncates any existing file.
719 *
720 * @return Returns a new ParcelFileDescriptor that can be used by the
721 * client to access the file.
722 */
723 protected final ParcelFileDescriptor openFileHelper(Uri uri,
724 String mode) throws FileNotFoundException {
725 Cursor c = query(uri, new String[]{"_data"}, null, null, null);
726 int count = (c != null) ? c.getCount() : 0;
727 if (count != 1) {
728 // If there is not exactly one result, throw an appropriate
729 // exception.
730 if (c != null) {
731 c.close();
732 }
733 if (count == 0) {
734 throw new FileNotFoundException("No entry for " + uri);
735 }
736 throw new FileNotFoundException("Multiple items at " + uri);
737 }
738
739 c.moveToFirst();
740 int i = c.getColumnIndex("_data");
741 String path = (i >= 0 ? c.getString(i) : null);
742 c.close();
743 if (path == null) {
744 throw new FileNotFoundException("Column _data not found.");
745 }
746
747 int modeBits = ContentResolver.modeToMode(uri, mode);
748 return ParcelFileDescriptor.open(new File(path), modeBits);
749 }
750
751 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 * Returns true if this instance is a temporary content provider.
753 * @return true if this instance is a temporary content provider
754 */
755 protected boolean isTemporary() {
756 return false;
757 }
758
759 /**
760 * Returns the Binder object for this provider.
761 *
762 * @return the Binder object for this provider
763 * @hide
764 */
765 public IContentProvider getIContentProvider() {
766 return mTransport;
767 }
768
769 /**
770 * After being instantiated, this is called to tell the content provider
771 * about itself.
772 *
773 * @param context The context this provider is running in
774 * @param info Registered information about this content provider
775 */
776 public void attachInfo(Context context, ProviderInfo info) {
777
778 /*
779 * Only allow it to be set once, so after the content service gives
780 * this to us clients can't change it.
781 */
782 if (mContext == null) {
783 mContext = context;
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700784 mMyUid = Process.myUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 if (info != null) {
786 setReadPermission(info.readPermission);
787 setWritePermission(info.writePermission);
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700788 setPathPermissions(info.pathPermissions);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790 ContentProvider.this.onCreate();
791 }
792 }
Fred Quintanace31b232009-05-04 16:01:15 -0700793
794 /**
Dan Egnor17876aa2010-07-28 12:28:04 -0700795 * Override this to handle requests to perform a batch of operations, or the
796 * default implementation will iterate over the operations and call
797 * {@link ContentProviderOperation#apply} on each of them.
798 * If all calls to {@link ContentProviderOperation#apply} succeed
799 * then a {@link ContentProviderResult} array with as many
800 * elements as there were operations will be returned. If any of the calls
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700801 * fail, it is up to the implementation how many of the others take effect.
802 * This method can be called from multiple threads, as described in
803 * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals:
804 * Processes and Threads</a>.
805 *
Fred Quintanace31b232009-05-04 16:01:15 -0700806 * @param operations the operations to apply
807 * @return the results of the applications
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700808 * @throws OperationApplicationException thrown if any operation fails.
809 * @see ContentProviderOperation#apply
Fred Quintanace31b232009-05-04 16:01:15 -0700810 */
Fred Quintana03d94902009-05-22 14:23:31 -0700811 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
Fred Quintanace31b232009-05-04 16:01:15 -0700812 throws OperationApplicationException {
Fred Quintana03d94902009-05-22 14:23:31 -0700813 final int numOperations = operations.size();
814 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
815 for (int i = 0; i < numOperations; i++) {
816 results[i] = operations.get(i).apply(this, results, i);
Fred Quintanace31b232009-05-04 16:01:15 -0700817 }
818 return results;
819 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800820
821 /**
822 * @hide -- until interface has proven itself
823 *
824 * Call an provider-defined method. This can be used to implement
825 * interfaces that are cheaper than using a Cursor.
826 *
827 * @param method Method name to call. Opaque to framework.
828 * @param request Nullable String argument passed to method.
829 * @param args Nullable Bundle argument passed to method.
830 */
831 public Bundle call(String method, String request, Bundle args) {
832 return null;
833 }
834}