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