The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content; |
| 18 | |
| 19 | import android.content.pm.PackageManager; |
| 20 | import android.content.pm.ProviderInfo; |
| 21 | import android.content.res.AssetFileDescriptor; |
| 22 | import android.content.res.Configuration; |
| 23 | import android.database.Cursor; |
| 24 | import android.database.CursorToBulkCursorAdaptor; |
| 25 | import android.database.CursorWindow; |
| 26 | import android.database.IBulkCursor; |
| 27 | import android.database.IContentObserver; |
| 28 | import android.database.SQLException; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Binder; |
| 31 | import android.os.ParcelFileDescriptor; |
| 32 | |
| 33 | import java.io.File; |
| 34 | import 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 | */ |
| 66 | public 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 | |
| 133 | public String getType(Uri uri) { |
| 134 | return ContentProvider.this.getType(uri); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | public Uri insert(Uri uri, ContentValues initialValues) { |
| 139 | checkWritePermission(uri); |
| 140 | return ContentProvider.this.insert(uri, initialValues); |
| 141 | } |
| 142 | |
| 143 | public int bulkInsert(Uri uri, ContentValues[] initialValues) { |
| 144 | checkWritePermission(uri); |
| 145 | return ContentProvider.this.bulkInsert(uri, initialValues); |
| 146 | } |
| 147 | |
| 148 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 149 | checkWritePermission(uri); |
| 150 | return ContentProvider.this.delete(uri, selection, selectionArgs); |
| 151 | } |
| 152 | |
| 153 | public int update(Uri uri, ContentValues values, String selection, |
| 154 | String[] selectionArgs) { |
| 155 | checkWritePermission(uri); |
| 156 | return ContentProvider.this.update(uri, values, selection, selectionArgs); |
| 157 | } |
| 158 | |
| 159 | public ParcelFileDescriptor openFile(Uri uri, String mode) |
| 160 | throws FileNotFoundException { |
| 161 | if (mode != null && mode.startsWith("rw")) checkWritePermission(uri); |
| 162 | else checkReadPermission(uri); |
| 163 | return ContentProvider.this.openFile(uri, mode); |
| 164 | } |
| 165 | |
| 166 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) |
| 167 | throws FileNotFoundException { |
| 168 | if (mode != null && mode.startsWith("rw")) checkWritePermission(uri); |
| 169 | else checkReadPermission(uri); |
| 170 | return ContentProvider.this.openAssetFile(uri, mode); |
| 171 | } |
| 172 | |
| 173 | public ISyncAdapter getSyncAdapter() { |
| 174 | checkWritePermission(null); |
Bjorn Bringert | f64aff1 | 2009-03-24 17:59:45 -0700 | [diff] [blame] | 175 | SyncAdapter sa = ContentProvider.this.getSyncAdapter(); |
| 176 | return sa != null ? sa.getISyncAdapter() : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private void checkReadPermission(Uri uri) { |
| 180 | final String rperm = getReadPermission(); |
| 181 | final int pid = Binder.getCallingPid(); |
| 182 | final int uid = Binder.getCallingUid(); |
| 183 | if (getContext().checkUriPermission(uri, rperm, null, pid, uid, |
| 184 | Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 185 | == PackageManager.PERMISSION_GRANTED) { |
| 186 | return; |
| 187 | } |
| 188 | String msg = "Permission Denial: reading " |
| 189 | + ContentProvider.this.getClass().getName() |
| 190 | + " uri " + uri + " from pid=" + Binder.getCallingPid() |
| 191 | + ", uid=" + Binder.getCallingUid() |
| 192 | + " requires " + rperm; |
| 193 | throw new SecurityException(msg); |
| 194 | } |
| 195 | |
| 196 | private void checkWritePermission(Uri uri) { |
| 197 | final String wperm = getWritePermission(); |
| 198 | final int pid = Binder.getCallingPid(); |
| 199 | final int uid = Binder.getCallingUid(); |
| 200 | if (getContext().checkUriPermission(uri, null, wperm, pid, uid, |
| 201 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| 202 | == PackageManager.PERMISSION_GRANTED) { |
| 203 | return; |
| 204 | } |
| 205 | String msg = "Permission Denial: writing " |
| 206 | + ContentProvider.this.getClass().getName() |
| 207 | + " uri " + uri + " from pid=" + Binder.getCallingPid() |
| 208 | + ", uid=" + Binder.getCallingUid() |
| 209 | + " requires " + wperm; |
| 210 | throw new SecurityException(msg); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * Retrieve the Context this provider is running in. Only available once |
| 217 | * onCreate(Map icicle) has been called -- this will be null in the |
| 218 | * constructor. |
| 219 | */ |
| 220 | public final Context getContext() { |
| 221 | return mContext; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Change the permission required to read data from the content |
| 226 | * provider. This is normally set for you from its manifest information |
| 227 | * when the provider is first created. |
| 228 | * |
| 229 | * @param permission Name of the permission required for read-only access. |
| 230 | */ |
| 231 | protected final void setReadPermission(String permission) { |
| 232 | mReadPermission = permission; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Return the name of the permission required for read-only access to |
| 237 | * this content provider. This method can be called from multiple |
| 238 | * threads, as described in |
| 239 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 240 | * Processes and Threads</a>. |
| 241 | */ |
| 242 | public final String getReadPermission() { |
| 243 | return mReadPermission; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Change the permission required to read and write data in the content |
| 248 | * provider. This is normally set for you from its manifest information |
| 249 | * when the provider is first created. |
| 250 | * |
| 251 | * @param permission Name of the permission required for read/write access. |
| 252 | */ |
| 253 | protected final void setWritePermission(String permission) { |
| 254 | mWritePermission = permission; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Return the name of the permission required for read/write access to |
| 259 | * this content provider. This method can be called from multiple |
| 260 | * threads, as described in |
| 261 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 262 | * Processes and Threads</a>. |
| 263 | */ |
| 264 | public final String getWritePermission() { |
| 265 | return mWritePermission; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Called when the provider is being started. |
| 270 | * |
| 271 | * @return true if the provider was successfully loaded, false otherwise |
| 272 | */ |
| 273 | public abstract boolean onCreate(); |
| 274 | |
| 275 | public void onConfigurationChanged(Configuration newConfig) { |
| 276 | } |
| 277 | |
| 278 | public void onLowMemory() { |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Receives a query request from a client in a local process, and |
| 283 | * returns a Cursor. This is called internally by the {@link ContentResolver}. |
| 284 | * This method can be called from multiple |
| 285 | * threads, as described in |
| 286 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 287 | * Processes and Threads</a>. |
| 288 | * <p> |
| 289 | * Example client call:<p> |
| 290 | * <pre>// Request a specific record. |
| 291 | * Cursor managedCursor = managedQuery( |
| 292 | Contacts.People.CONTENT_URI.addId(2), |
| 293 | projection, // Which columns to return. |
| 294 | null, // WHERE clause. |
| 295 | People.NAME + " ASC"); // Sort order.</pre> |
| 296 | * Example implementation:<p> |
| 297 | * <pre>// SQLiteQueryBuilder is a helper class that creates the |
| 298 | // proper SQL syntax for us. |
| 299 | SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder(); |
| 300 | |
| 301 | // Set the table we're querying. |
| 302 | qBuilder.setTables(DATABASE_TABLE_NAME); |
| 303 | |
| 304 | // If the query ends in a specific record number, we're |
| 305 | // being asked for a specific record, so set the |
| 306 | // WHERE clause in our query. |
| 307 | if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){ |
| 308 | qBuilder.appendWhere("_id=" + uri.getPathLeafId()); |
| 309 | } |
| 310 | |
| 311 | // Make the query. |
| 312 | Cursor c = qBuilder.query(mDb, |
| 313 | projection, |
| 314 | selection, |
| 315 | selectionArgs, |
| 316 | groupBy, |
| 317 | having, |
| 318 | sortOrder); |
| 319 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 320 | return c;</pre> |
| 321 | * |
| 322 | * @param uri The URI to query. This will be the full URI sent by the client; |
| 323 | * if the client is requesting a specific record, the URI will end in a record number |
| 324 | * that the implementation should parse and add to a WHERE or HAVING clause, specifying |
| 325 | * that _id value. |
| 326 | * @param projection The list of columns to put into the cursor. If |
| 327 | * null all columns are included. |
| 328 | * @param selection A selection criteria to apply when filtering rows. |
| 329 | * If null then all rows are included. |
| 330 | * @param sortOrder How the rows in the cursor should be sorted. |
| 331 | * If null then the provider is free to define the sort order. |
| 332 | * @return a Cursor or null. |
| 333 | */ |
| 334 | public abstract Cursor query(Uri uri, String[] projection, |
| 335 | String selection, String[] selectionArgs, String sortOrder); |
| 336 | |
| 337 | /** |
| 338 | * Return the MIME type of the data at the given URI. This should start with |
| 339 | * <code>vnd.android.cursor.item</code> for a single record, |
| 340 | * or <code>vnd.android.cursor.dir/</code> for multiple items. |
| 341 | * This method can be called from multiple |
| 342 | * threads, as described in |
| 343 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 344 | * Processes and Threads</a>. |
| 345 | * |
| 346 | * @param uri the URI to query. |
| 347 | * @return a MIME type string, or null if there is no type. |
| 348 | */ |
| 349 | public abstract String getType(Uri uri); |
| 350 | |
| 351 | /** |
| 352 | * Implement this to insert a new row. |
| 353 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 354 | * after inserting. |
| 355 | * This method can be called from multiple |
| 356 | * threads, as described in |
| 357 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 358 | * Processes and Threads</a>. |
| 359 | * @param uri The content:// URI of the insertion request. |
| 360 | * @param values A set of column_name/value pairs to add to the database. |
| 361 | * @return The URI for the newly inserted item. |
| 362 | */ |
| 363 | public abstract Uri insert(Uri uri, ContentValues values); |
| 364 | |
| 365 | /** |
| 366 | * Implement this to insert a set of new rows, or the default implementation will |
| 367 | * iterate over the values and call {@link #insert} on each of them. |
| 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 | * |
| 375 | * @param uri The content:// URI of the insertion request. |
| 376 | * @param values An array of sets of column_name/value pairs to add to the database. |
| 377 | * @return The number of values that were inserted. |
| 378 | */ |
| 379 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 380 | int numValues = values.length; |
| 381 | for (int i = 0; i < numValues; i++) { |
| 382 | insert(uri, values[i]); |
| 383 | } |
| 384 | return numValues; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * A request to delete one or more rows. The selection clause is applied when performing |
| 389 | * the deletion, allowing the operation to affect multiple rows in a |
| 390 | * directory. |
| 391 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()} |
| 392 | * after deleting. |
| 393 | * This method can be called from multiple |
| 394 | * threads, as described in |
| 395 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 396 | * Processes and Threads</a>. |
| 397 | * |
| 398 | * <p>The implementation is responsible for parsing out a row ID at the end |
| 399 | * of the URI, if a specific row is being deleted. That is, the client would |
| 400 | * pass in <code>content://contacts/people/22</code> and the implementation is |
| 401 | * responsible for parsing the record number (22) when creating a SQL statement. |
| 402 | * |
| 403 | * @param uri The full URI to query, including a row ID (if a specific record is requested). |
| 404 | * @param selection An optional restriction to apply to rows when deleting. |
| 405 | * @return The number of rows affected. |
| 406 | * @throws SQLException |
| 407 | */ |
| 408 | public abstract int delete(Uri uri, String selection, String[] selectionArgs); |
| 409 | |
| 410 | /** |
| 411 | * Update a content URI. All rows matching the optionally provided selection |
| 412 | * will have their columns listed as the keys in the values map with the |
| 413 | * values of those keys. |
| 414 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 415 | * after updating. |
| 416 | * This method can be called from multiple |
| 417 | * threads, as described in |
| 418 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 419 | * Processes and Threads</a>. |
| 420 | * |
| 421 | * @param uri The URI to query. This can potentially have a record ID if this |
| 422 | * is an update request for a specific record. |
| 423 | * @param values A Bundle mapping from column names to new column values (NULL is a |
| 424 | * valid value). |
| 425 | * @param selection An optional filter to match rows to update. |
| 426 | * @return the number of rows affected. |
| 427 | */ |
| 428 | public abstract int update(Uri uri, ContentValues values, String selection, |
| 429 | String[] selectionArgs); |
| 430 | |
| 431 | /** |
| 432 | * Open a file blob associated with a content URI. |
| 433 | * This method can be called from multiple |
| 434 | * threads, as described in |
| 435 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 436 | * Processes and Threads</a>. |
| 437 | * |
| 438 | * <p>Returns a |
| 439 | * ParcelFileDescriptor, from which you can obtain a |
| 440 | * {@link java.io.FileDescriptor} for use with |
| 441 | * {@link java.io.FileInputStream}, {@link java.io.FileOutputStream}, etc. |
| 442 | * This can be used to store large data (such as an image) associated with |
| 443 | * a particular piece of content. |
| 444 | * |
| 445 | * <p>The returned ParcelFileDescriptor is owned by the caller, so it is |
| 446 | * their responsibility to close it when done. That is, the implementation |
| 447 | * of this method should create a new ParcelFileDescriptor for each call. |
| 448 | * |
| 449 | * @param uri The URI whose file is to be opened. |
| 450 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 451 | * "rw" for read and write access, or "rwt" for read and write access |
| 452 | * that truncates any existing file. |
| 453 | * |
| 454 | * @return Returns a new ParcelFileDescriptor which you can use to access |
| 455 | * the file. |
| 456 | * |
| 457 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 458 | * no file associated with the given URI or the mode is invalid. |
| 459 | * @throws SecurityException Throws SecurityException if the caller does |
| 460 | * not have permission to access the file. |
| 461 | * |
| 462 | * @see #openAssetFile(Uri, String) |
| 463 | * @see #openFileHelper(Uri, String) |
| 464 | */ |
| 465 | public ParcelFileDescriptor openFile(Uri uri, String mode) |
| 466 | throws FileNotFoundException { |
| 467 | throw new FileNotFoundException("No files supported by provider at " |
| 468 | + uri); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * This is like {@link #openFile}, but can be implemented by providers |
| 473 | * that need to be able to return sub-sections of files, often assets |
| 474 | * inside of their .apk. Note that when implementing this your clients |
| 475 | * must be able to deal with such files, either directly with |
| 476 | * {@link ContentResolver#openAssetFileDescriptor |
| 477 | * ContentResolver.openAssetFileDescriptor}, or by using the higher-level |
| 478 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 479 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 480 | * methods. |
| 481 | * |
| 482 | * <p><em>Note: if you are implementing this to return a full file, you |
| 483 | * should create the AssetFileDescriptor with |
| 484 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
| 485 | * applications that can not handle sub-sections of files.</em></p> |
| 486 | * |
| 487 | * @param uri The URI whose file is to be opened. |
| 488 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 489 | * "w" for write-only access (erasing whatever data is currently in |
| 490 | * the file), "wa" for write-only access to append to any existing data, |
| 491 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 492 | * and write access that truncates any existing file. |
| 493 | * |
| 494 | * @return Returns a new AssetFileDescriptor which you can use to access |
| 495 | * the file. |
| 496 | * |
| 497 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 498 | * no file associated with the given URI or the mode is invalid. |
| 499 | * @throws SecurityException Throws SecurityException if the caller does |
| 500 | * not have permission to access the file. |
| 501 | * |
| 502 | * @see #openFile(Uri, String) |
| 503 | * @see #openFileHelper(Uri, String) |
| 504 | */ |
| 505 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) |
| 506 | throws FileNotFoundException { |
| 507 | ParcelFileDescriptor fd = openFile(uri, mode); |
| 508 | return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Convenience for subclasses that wish to implement {@link #openFile} |
| 513 | * by looking up a column named "_data" at the given URI. |
| 514 | * |
| 515 | * @param uri The URI to be opened. |
| 516 | * @param mode The file mode. May be "r" for read-only access, |
| 517 | * "w" for write-only access (erasing whatever data is currently in |
| 518 | * the file), "wa" for write-only access to append to any existing data, |
| 519 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 520 | * and write access that truncates any existing file. |
| 521 | * |
| 522 | * @return Returns a new ParcelFileDescriptor that can be used by the |
| 523 | * client to access the file. |
| 524 | */ |
| 525 | protected final ParcelFileDescriptor openFileHelper(Uri uri, |
| 526 | String mode) throws FileNotFoundException { |
| 527 | Cursor c = query(uri, new String[]{"_data"}, null, null, null); |
| 528 | int count = (c != null) ? c.getCount() : 0; |
| 529 | if (count != 1) { |
| 530 | // If there is not exactly one result, throw an appropriate |
| 531 | // exception. |
| 532 | if (c != null) { |
| 533 | c.close(); |
| 534 | } |
| 535 | if (count == 0) { |
| 536 | throw new FileNotFoundException("No entry for " + uri); |
| 537 | } |
| 538 | throw new FileNotFoundException("Multiple items at " + uri); |
| 539 | } |
| 540 | |
| 541 | c.moveToFirst(); |
| 542 | int i = c.getColumnIndex("_data"); |
| 543 | String path = (i >= 0 ? c.getString(i) : null); |
| 544 | c.close(); |
| 545 | if (path == null) { |
| 546 | throw new FileNotFoundException("Column _data not found."); |
| 547 | } |
| 548 | |
| 549 | int modeBits = ContentResolver.modeToMode(uri, mode); |
| 550 | return ParcelFileDescriptor.open(new File(path), modeBits); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Get the sync adapter that is to be used by this content provider. |
| 555 | * This is intended for use by the sync system. If null then this |
| 556 | * content provider is considered not syncable. |
| 557 | * This method can be called from multiple |
| 558 | * threads, as described in |
| 559 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 560 | * Processes and Threads</a>. |
| 561 | * |
| 562 | * @return the SyncAdapter that is to be used by this ContentProvider, or null |
| 563 | * if this ContentProvider is not syncable |
| 564 | * @hide |
| 565 | */ |
| 566 | public SyncAdapter getSyncAdapter() { |
| 567 | return null; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Returns true if this instance is a temporary content provider. |
| 572 | * @return true if this instance is a temporary content provider |
| 573 | */ |
| 574 | protected boolean isTemporary() { |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Returns the Binder object for this provider. |
| 580 | * |
| 581 | * @return the Binder object for this provider |
| 582 | * @hide |
| 583 | */ |
| 584 | public IContentProvider getIContentProvider() { |
| 585 | return mTransport; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * After being instantiated, this is called to tell the content provider |
| 590 | * about itself. |
| 591 | * |
| 592 | * @param context The context this provider is running in |
| 593 | * @param info Registered information about this content provider |
| 594 | */ |
| 595 | public void attachInfo(Context context, ProviderInfo info) { |
| 596 | |
| 597 | /* |
| 598 | * Only allow it to be set once, so after the content service gives |
| 599 | * this to us clients can't change it. |
| 600 | */ |
| 601 | if (mContext == null) { |
| 602 | mContext = context; |
| 603 | if (info != null) { |
| 604 | setReadPermission(info.readPermission); |
| 605 | setWritePermission(info.writePermission); |
| 606 | } |
| 607 | ContentProvider.this.onCreate(); |
| 608 | } |
| 609 | } |
| 610 | } |