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; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 32 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.os.ParcelFileDescriptor; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 34 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | import java.io.File; |
| 37 | import java.io.FileNotFoundException; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 60 | * <li>{@link #onCreate} which is called to initialize the provider</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * <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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 68 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | */ |
| 78 | public abstract class ContentProvider implements ComponentCallbacks { |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 79 | /* |
| 80 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 81 | * MockContentProvider. |
| 82 | */ |
| 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private Context mContext = null; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 85 | private int mMyUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | private String mReadPermission; |
| 87 | private String mWritePermission; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 88 | private PathPermission[] mPathPermissions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | private Transport mTransport = new Transport(); |
| 91 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 92 | /** |
| 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 Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 107 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | /** |
| 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 Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 172 | enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | Cursor cursor = ContentProvider.this.query(uri, projection, |
| 174 | selection, selectionArgs, sortOrder); |
| 175 | if (cursor == null) { |
| 176 | return null; |
| 177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | return new CursorToBulkCursorAdaptor(cursor, observer, |
| 179 | ContentProvider.this.getClass().getName(), |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 180 | hasWritePermission(uri), window); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public Cursor query(Uri uri, String[] projection, |
| 184 | String selection, String[] selectionArgs, String sortOrder) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 185 | enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | 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 Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 196 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | return ContentProvider.this.insert(uri, initialValues); |
| 198 | } |
| 199 | |
| 200 | public int bulkInsert(Uri uri, ContentValues[] initialValues) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 201 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | return ContentProvider.this.bulkInsert(uri, initialValues); |
| 203 | } |
| 204 | |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 205 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 206 | throws OperationApplicationException { |
| 207 | for (ContentProviderOperation operation : operations) { |
| 208 | if (operation.isReadOperation()) { |
Dianne Hackborn | e3f0544 | 2009-07-09 12:15:46 -0700 | [diff] [blame] | 209 | enforceReadPermission(operation.getUri()); |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (operation.isWriteOperation()) { |
Dianne Hackborn | e3f0544 | 2009-07-09 12:15:46 -0700 | [diff] [blame] | 213 | enforceWritePermission(operation.getUri()); |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | return ContentProvider.this.applyBatch(operations); |
Fred Quintana | 6a8d533 | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 217 | } |
| 218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 220 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | return ContentProvider.this.delete(uri, selection, selectionArgs); |
| 222 | } |
| 223 | |
| 224 | public int update(Uri uri, ContentValues values, String selection, |
| 225 | String[] selectionArgs) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 226 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | return ContentProvider.this.update(uri, values, selection, selectionArgs); |
| 228 | } |
| 229 | |
| 230 | public ParcelFileDescriptor openFile(Uri uri, String mode) |
| 231 | throws FileNotFoundException { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 232 | if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri); |
| 233 | else enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | return ContentProvider.this.openFile(uri, mode); |
| 235 | } |
| 236 | |
| 237 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) |
| 238 | throws FileNotFoundException { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 239 | if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri); |
| 240 | else enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | return ContentProvider.this.openAssetFile(uri, mode); |
| 242 | } |
| 243 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 244 | /** |
| 245 | * @hide |
| 246 | */ |
| 247 | public Bundle call(String method, String request, Bundle args) { |
| 248 | return ContentProvider.this.call(method, request, args); |
| 249 | } |
| 250 | |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 251 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | final String rperm = getReadPermission(); |
| 259 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 260 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 285 | == PackageManager.PERMISSION_GRANTED) { |
| 286 | return; |
| 287 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | 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 Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 297 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | final String wperm = getWritePermission(); |
| 305 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 306 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| 331 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | private void enforceWritePermission(Uri uri) { |
| 339 | if (hasWritePermission(uri)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | return; |
| 341 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | String msg = "Permission Denial: writing " |
| 344 | + ContentProvider.this.getClass().getName() |
| 345 | + " uri " + uri + " from pid=" + Binder.getCallingPid() |
| 346 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 347 | + " requires " + getWritePermission(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | throw new SecurityException(msg); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 354 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | * 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 Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 407 | * 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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 429 | * 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 Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 441 | * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper} |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 442 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | * |
| 451 | * @return true if the provider was successfully loaded, false otherwise |
| 452 | */ |
| 453 | public abstract boolean onCreate(); |
| 454 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 455 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | public void onConfigurationChanged(Configuration newConfig) { |
| 466 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 467 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | public void onLowMemory() { |
| 477 | } |
| 478 | |
| 479 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 480 | * Implement this to handle query requests from clients. |
| 481 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | * <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 Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 488 | ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | projection, // Which columns to return. |
| 490 | null, // WHERE clause. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 491 | null, // WHERE clause value substitution |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | 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 Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 520 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | * @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 Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 527 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | * @param sortOrder How the rows in the cursor should be sorted. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 531 | * If null then the provider is free to define the sort order. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | * @return a Cursor or null. |
| 533 | */ |
| 534 | public abstract Cursor query(Uri uri, String[] projection, |
| 535 | String selection, String[] selectionArgs, String sortOrder); |
| 536 | |
Fred Quintana | 5bba632 | 2009-10-05 14:21:12 -0700 | [diff] [blame] | 537 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 538 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | * <code>vnd.android.cursor.item</code> for a single record, |
| 541 | * or <code>vnd.android.cursor.dir/</code> for multiple items. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 542 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | * <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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 552 | * Implement this to handle requests to insert a new row. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 554 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 555 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | * <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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 565 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 569 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 570 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | * <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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 587 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()} |
| 591 | * after deleting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 592 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | * <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 Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 609 | * Implement this to handle requests to update one or more rows. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 610 | * The implementation should update all rows matching the selection |
| 611 | * to set the columns according to the provided values map. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 613 | * after updating. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 614 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | * <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 Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 629 | * Override this to handle requests to open a file blob. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 630 | * The default implementation always throws {@link FileNotFoundException}. |
| 631 | * This method can be called from multiple threads, as described in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 633 | * Processes and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 634 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 635 | * <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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 637 | * returned without copying the content. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | * |
| 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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 655 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | * @see #openAssetFile(Uri, String) |
| 657 | * @see #openFileHelper(Uri, String) |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 658 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | public ParcelFileDescriptor openFile(Uri uri, String mode) |
| 660 | throws FileNotFoundException { |
| 661 | throw new FileNotFoundException("No files supported by provider at " |
| 662 | + uri); |
| 663 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 664 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | /** |
| 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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 668 | * 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 Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 674 | * file slices, either directly with |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 675 | * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 677 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 678 | * methods. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 679 | * |
| 680 | * <p class="note">If you are implementing this to return a full file, you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | * should create the AssetFileDescriptor with |
| 682 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 683 | * applications that can not handle sub-sections of files.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | * 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 Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 784 | mMyUid = Process.myUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | if (info != null) { |
| 786 | setReadPermission(info.readPermission); |
| 787 | setWritePermission(info.writePermission); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 788 | setPathPermissions(info.pathPermissions); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
| 790 | ContentProvider.this.onCreate(); |
| 791 | } |
| 792 | } |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 793 | |
| 794 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 795 | * 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 Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 801 | * 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 Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 806 | * @param operations the operations to apply |
| 807 | * @return the results of the applications |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 808 | * @throws OperationApplicationException thrown if any operation fails. |
| 809 | * @see ContentProviderOperation#apply |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 810 | */ |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 811 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 812 | throws OperationApplicationException { |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 813 | 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 Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 817 | } |
| 818 | return results; |
| 819 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 820 | |
| 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 | } |