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; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 31 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.Binder; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 33 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.ParcelFileDescriptor; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 35 | import android.os.Process; |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 36 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | import java.io.File; |
| 39 | import java.io.FileNotFoundException; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 40 | import java.io.IOException; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Content providers are one of the primary building blocks of Android applications, providing |
| 45 | * content to applications. They encapsulate data and provide it to applications through the single |
| 46 | * {@link ContentResolver} interface. A content provider is only required if you need to share |
| 47 | * data between multiple applications. For example, the contacts data is used by multiple |
| 48 | * applications and must be stored in a content provider. If you don't need to share data amongst |
| 49 | * multiple applications you can use a database directly via |
| 50 | * {@link android.database.sqlite.SQLiteDatabase}. |
| 51 | * |
| 52 | * <p>For more information, read <a href="{@docRoot}guide/topics/providers/content-providers.html">Content |
| 53 | * Providers</a>.</p> |
| 54 | * |
| 55 | * <p>When a request is made via |
| 56 | * a {@link ContentResolver} the system inspects the authority of the given URI and passes the |
| 57 | * request to the content provider registered with the authority. The content provider can interpret |
| 58 | * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing |
| 59 | * URIs.</p> |
| 60 | * |
| 61 | * <p>The primary methods that need to be implemented are: |
| 62 | * <ul> |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 63 | * <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] | 64 | * <li>{@link #query} which returns data to the caller</li> |
| 65 | * <li>{@link #insert} which inserts new data into the content provider</li> |
| 66 | * <li>{@link #update} which updates existing data in the content provider</li> |
| 67 | * <li>{@link #delete} which deletes data from the content provider</li> |
| 68 | * <li>{@link #getType} which returns the MIME type of data in the content provider</li> |
| 69 | * </ul></p> |
| 70 | * |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 71 | * <p class="caution">Data access methods (such as {@link #insert} and |
| 72 | * {@link #update}) may be called from many threads at once, and must be thread-safe. |
| 73 | * Other methods (such as {@link #onCreate}) are only called from the application |
| 74 | * main thread, and must avoid performing lengthy operations. See the method |
| 75 | * descriptions for their expected thread behavior.</p> |
| 76 | * |
| 77 | * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate |
| 78 | * ContentProvider instance, so subclasses don't have to worry about the details of |
| 79 | * cross-process calls.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | */ |
| 81 | public abstract class ContentProvider implements ComponentCallbacks { |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 82 | private static final String TAG = "ContentProvider"; |
| 83 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 84 | /* |
| 85 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 86 | * MockContentProvider. |
| 87 | */ |
| 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | private Context mContext = null; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 90 | private int mMyUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | private String mReadPermission; |
| 92 | private String mWritePermission; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 93 | private PathPermission[] mPathPermissions; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 94 | private boolean mExported; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
| 96 | private Transport mTransport = new Transport(); |
| 97 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Construct a ContentProvider instance. Content providers must be |
| 100 | * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared |
| 101 | * in the manifest</a>, accessed with {@link ContentResolver}, and created |
| 102 | * automatically by the system, so applications usually do not create |
| 103 | * ContentProvider instances directly. |
| 104 | * |
| 105 | * <p>At construction time, the object is uninitialized, and most fields and |
| 106 | * methods are unavailable. Subclasses should initialize themselves in |
| 107 | * {@link #onCreate}, not the constructor. |
| 108 | * |
| 109 | * <p>Content providers are created on the application main thread at |
| 110 | * application launch time. The constructor must not perform lengthy |
| 111 | * operations, or application startup will be delayed. |
| 112 | */ |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 113 | public ContentProvider() { |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Constructor just for mocking. |
| 118 | * |
| 119 | * @param context A Context object which should be some mock instance (like the |
| 120 | * instance of {@link android.test.mock.MockContext}). |
| 121 | * @param readPermission The read permision you want this instance should have in the |
| 122 | * test, which is available via {@link #getReadPermission()}. |
| 123 | * @param writePermission The write permission you want this instance should have |
| 124 | * in the test, which is available via {@link #getWritePermission()}. |
| 125 | * @param pathPermissions The PathPermissions you want this instance should have |
| 126 | * in the test, which is available via {@link #getPathPermissions()}. |
| 127 | * @hide |
| 128 | */ |
| 129 | public ContentProvider( |
| 130 | Context context, |
| 131 | String readPermission, |
| 132 | String writePermission, |
| 133 | PathPermission[] pathPermissions) { |
| 134 | mContext = context; |
| 135 | mReadPermission = readPermission; |
| 136 | mWritePermission = writePermission; |
| 137 | mPathPermissions = pathPermissions; |
| 138 | } |
| 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Given an IContentProvider, try to coerce it back to the real |
| 142 | * ContentProvider object if it is running in the local process. This can |
| 143 | * be used if you know you are running in the same process as a provider, |
| 144 | * and want to get direct access to its implementation details. Most |
| 145 | * clients should not nor have a reason to use it. |
| 146 | * |
| 147 | * @param abstractInterface The ContentProvider interface that is to be |
| 148 | * coerced. |
| 149 | * @return If the IContentProvider is non-null and local, returns its actual |
| 150 | * ContentProvider instance. Otherwise returns null. |
| 151 | * @hide |
| 152 | */ |
| 153 | public static ContentProvider coerceToLocalContentProvider( |
| 154 | IContentProvider abstractInterface) { |
| 155 | if (abstractInterface instanceof Transport) { |
| 156 | return ((Transport)abstractInterface).getContentProvider(); |
| 157 | } |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Binder object that deals with remoting. |
| 163 | * |
| 164 | * @hide |
| 165 | */ |
| 166 | class Transport extends ContentProviderNative { |
| 167 | ContentProvider getContentProvider() { |
| 168 | return ContentProvider.this; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Remote version of a query, which returns an IBulkCursor. The bulk |
| 173 | * cursor should be wrapped with BulkCursorToCursorAdaptor before use. |
| 174 | */ |
| 175 | public IBulkCursor bulkQuery(Uri uri, String[] projection, |
| 176 | String selection, String[] selectionArgs, String sortOrder, |
| 177 | IContentObserver observer, CursorWindow window) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 178 | enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | Cursor cursor = ContentProvider.this.query(uri, projection, |
| 180 | selection, selectionArgs, sortOrder); |
| 181 | if (cursor == null) { |
| 182 | return null; |
| 183 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | return new CursorToBulkCursorAdaptor(cursor, observer, |
| 185 | ContentProvider.this.getClass().getName(), |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 186 | hasWritePermission(uri), window); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | public Cursor query(Uri uri, String[] projection, |
| 190 | String selection, String[] selectionArgs, String sortOrder) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 191 | enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | return ContentProvider.this.query(uri, projection, selection, |
| 193 | selectionArgs, sortOrder); |
| 194 | } |
| 195 | |
| 196 | public String getType(Uri uri) { |
| 197 | return ContentProvider.this.getType(uri); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | public Uri insert(Uri uri, ContentValues initialValues) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 202 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | return ContentProvider.this.insert(uri, initialValues); |
| 204 | } |
| 205 | |
| 206 | public int bulkInsert(Uri uri, ContentValues[] initialValues) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 207 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | return ContentProvider.this.bulkInsert(uri, initialValues); |
| 209 | } |
| 210 | |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 211 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 212 | throws OperationApplicationException { |
| 213 | for (ContentProviderOperation operation : operations) { |
| 214 | if (operation.isReadOperation()) { |
Dianne Hackborn | e3f0544 | 2009-07-09 12:15:46 -0700 | [diff] [blame] | 215 | enforceReadPermission(operation.getUri()); |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (operation.isWriteOperation()) { |
Dianne Hackborn | e3f0544 | 2009-07-09 12:15:46 -0700 | [diff] [blame] | 219 | enforceWritePermission(operation.getUri()); |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | return ContentProvider.this.applyBatch(operations); |
Fred Quintana | 6a8d533 | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 223 | } |
| 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | public int delete(Uri uri, String selection, 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.delete(uri, selection, selectionArgs); |
| 228 | } |
| 229 | |
| 230 | public int update(Uri uri, ContentValues values, String selection, |
| 231 | String[] selectionArgs) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 232 | enforceWritePermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | return ContentProvider.this.update(uri, values, selection, selectionArgs); |
| 234 | } |
| 235 | |
| 236 | public ParcelFileDescriptor openFile(Uri uri, String mode) |
| 237 | throws FileNotFoundException { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 238 | if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri); |
| 239 | else enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | return ContentProvider.this.openFile(uri, mode); |
| 241 | } |
| 242 | |
| 243 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) |
| 244 | throws FileNotFoundException { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 245 | if (mode != null && mode.startsWith("rw")) enforceWritePermission(uri); |
| 246 | else enforceReadPermission(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | return ContentProvider.this.openAssetFile(uri, mode); |
| 248 | } |
| 249 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 250 | /** |
| 251 | * @hide |
| 252 | */ |
| 253 | public Bundle call(String method, String request, Bundle args) { |
| 254 | return ContentProvider.this.call(method, request, args); |
| 255 | } |
| 256 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { |
| 259 | return ContentProvider.this.getStreamTypes(uri, mimeTypeFilter); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeType, Bundle opts) |
| 264 | throws FileNotFoundException { |
| 265 | enforceReadPermission(uri); |
| 266 | return ContentProvider.this.openTypedAssetFile(uri, mimeType, opts); |
| 267 | } |
| 268 | |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 269 | private void enforceReadPermission(Uri uri) { |
| 270 | final int uid = Binder.getCallingUid(); |
| 271 | if (uid == mMyUid) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | final Context context = getContext(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | final String rperm = getReadPermission(); |
| 277 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 278 | if (mExported && (rperm == null |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 279 | || context.checkPermission(rperm, pid, uid) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 280 | == PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
| 284 | PathPermission[] pps = getPathPermissions(); |
| 285 | if (pps != null) { |
| 286 | final String path = uri.getPath(); |
| 287 | int i = pps.length; |
| 288 | while (i > 0) { |
| 289 | i--; |
| 290 | final PathPermission pp = pps[i]; |
| 291 | final String pprperm = pp.getReadPermission(); |
| 292 | if (pprperm != null && pp.match(path)) { |
| 293 | if (context.checkPermission(pprperm, pid, uid) |
| 294 | == PackageManager.PERMISSION_GRANTED) { |
| 295 | return; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (context.checkUriPermission(uri, pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 303 | == PackageManager.PERMISSION_GRANTED) { |
| 304 | return; |
| 305 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | String msg = "Permission Denial: reading " |
| 308 | + ContentProvider.this.getClass().getName() |
| 309 | + " uri " + uri + " from pid=" + Binder.getCallingPid() |
| 310 | + ", uid=" + Binder.getCallingUid() |
| 311 | + " requires " + rperm; |
| 312 | throw new SecurityException(msg); |
| 313 | } |
| 314 | |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 315 | private boolean hasWritePermission(Uri uri) { |
| 316 | final int uid = Binder.getCallingUid(); |
| 317 | if (uid == mMyUid) { |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | final Context context = getContext(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | final String wperm = getWritePermission(); |
| 323 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 324 | if (mExported && (wperm == null |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 325 | || context.checkPermission(wperm, pid, uid) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 326 | == PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
| 330 | PathPermission[] pps = getPathPermissions(); |
| 331 | if (pps != null) { |
| 332 | final String path = uri.getPath(); |
| 333 | int i = pps.length; |
| 334 | while (i > 0) { |
| 335 | i--; |
| 336 | final PathPermission pp = pps[i]; |
| 337 | final String ppwperm = pp.getWritePermission(); |
| 338 | if (ppwperm != null && pp.match(path)) { |
| 339 | if (context.checkPermission(ppwperm, pid, uid) |
| 340 | == PackageManager.PERMISSION_GRANTED) { |
| 341 | return true; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (context.checkUriPermission(uri, pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| 349 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | private void enforceWritePermission(Uri uri) { |
| 357 | if (hasWritePermission(uri)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | return; |
| 359 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | String msg = "Permission Denial: writing " |
| 362 | + ContentProvider.this.getClass().getName() |
| 363 | + " uri " + uri + " from pid=" + Binder.getCallingPid() |
| 364 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 365 | + " requires " + getWritePermission(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | throw new SecurityException(msg); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 372 | * Retrieves the Context this provider is running in. Only available once |
| 373 | * {@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] | 374 | * constructor. |
| 375 | */ |
| 376 | public final Context getContext() { |
| 377 | return mContext; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Change the permission required to read data from the content |
| 382 | * provider. This is normally set for you from its manifest information |
| 383 | * when the provider is first created. |
| 384 | * |
| 385 | * @param permission Name of the permission required for read-only access. |
| 386 | */ |
| 387 | protected final void setReadPermission(String permission) { |
| 388 | mReadPermission = permission; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Return the name of the permission required for read-only access to |
| 393 | * this content provider. 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 | public final String getReadPermission() { |
| 399 | return mReadPermission; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Change the permission required to read and write data in the content |
| 404 | * provider. This is normally set for you from its manifest information |
| 405 | * when the provider is first created. |
| 406 | * |
| 407 | * @param permission Name of the permission required for read/write access. |
| 408 | */ |
| 409 | protected final void setWritePermission(String permission) { |
| 410 | mWritePermission = permission; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Return the name of the permission required for read/write access to |
| 415 | * this content provider. This method can be called from multiple |
| 416 | * threads, as described in |
| 417 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 418 | * Processes and Threads</a>. |
| 419 | */ |
| 420 | public final String getWritePermission() { |
| 421 | return mWritePermission; |
| 422 | } |
| 423 | |
| 424 | /** |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 425 | * Change the path-based permission required to read and/or write data in |
| 426 | * the content provider. This is normally set for you from its manifest |
| 427 | * information when the provider is first created. |
| 428 | * |
| 429 | * @param permissions Array of path permission descriptions. |
| 430 | */ |
| 431 | protected final void setPathPermissions(PathPermission[] permissions) { |
| 432 | mPathPermissions = permissions; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Return the path-based permissions required for read and/or write access to |
| 437 | * this content provider. This method can be called from multiple |
| 438 | * threads, as described in |
| 439 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 440 | * Processes and Threads</a>. |
| 441 | */ |
| 442 | public final PathPermission[] getPathPermissions() { |
| 443 | return mPathPermissions; |
| 444 | } |
| 445 | |
| 446 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 447 | * Implement this to initialize your content provider on startup. |
| 448 | * This method is called for all registered content providers on the |
| 449 | * application main thread at application launch time. It must not perform |
| 450 | * lengthy operations, or application startup will be delayed. |
| 451 | * |
| 452 | * <p>You should defer nontrivial initialization (such as opening, |
| 453 | * upgrading, and scanning databases) until the content provider is used |
| 454 | * (via {@link #query}, {@link #insert}, etc). Deferred initialization |
| 455 | * keeps application startup fast, avoids unnecessary work if the provider |
| 456 | * turns out not to be needed, and stops database errors (such as a full |
| 457 | * disk) from halting application launch. |
| 458 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 459 | * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper} |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 460 | * is a helpful utility class that makes it easy to manage databases, |
| 461 | * and will automatically defer opening until first use. If you do use |
| 462 | * SQLiteOpenHelper, make sure to avoid calling |
| 463 | * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or |
| 464 | * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase} |
| 465 | * from this method. (Instead, override |
| 466 | * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the |
| 467 | * database when it is first opened.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | * |
| 469 | * @return true if the provider was successfully loaded, false otherwise |
| 470 | */ |
| 471 | public abstract boolean onCreate(); |
| 472 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 473 | /** |
| 474 | * {@inheritDoc} |
| 475 | * This method is always called on the application main thread, and must |
| 476 | * not perform lengthy operations. |
| 477 | * |
| 478 | * <p>The default content provider implementation does nothing. |
| 479 | * Override this method to take appropriate action. |
| 480 | * (Content providers do not usually care about things like screen |
| 481 | * orientation, but may want to know about locale changes.) |
| 482 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | public void onConfigurationChanged(Configuration newConfig) { |
| 484 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 485 | |
| 486 | /** |
| 487 | * {@inheritDoc} |
| 488 | * This method is always called on the application main thread, and must |
| 489 | * not perform lengthy operations. |
| 490 | * |
| 491 | * <p>The default content provider implementation does nothing. |
| 492 | * Subclasses may override this method to take appropriate action. |
| 493 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | public void onLowMemory() { |
| 495 | } |
| 496 | |
| 497 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 498 | * Implement this to handle query requests from clients. |
| 499 | * 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] | 500 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 501 | * Processes and Threads</a>. |
| 502 | * <p> |
| 503 | * Example client call:<p> |
| 504 | * <pre>// Request a specific record. |
| 505 | * Cursor managedCursor = managedQuery( |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 506 | ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | projection, // Which columns to return. |
| 508 | null, // WHERE clause. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 509 | null, // WHERE clause value substitution |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | People.NAME + " ASC"); // Sort order.</pre> |
| 511 | * Example implementation:<p> |
| 512 | * <pre>// SQLiteQueryBuilder is a helper class that creates the |
| 513 | // proper SQL syntax for us. |
| 514 | SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder(); |
| 515 | |
| 516 | // Set the table we're querying. |
| 517 | qBuilder.setTables(DATABASE_TABLE_NAME); |
| 518 | |
| 519 | // If the query ends in a specific record number, we're |
| 520 | // being asked for a specific record, so set the |
| 521 | // WHERE clause in our query. |
| 522 | if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){ |
| 523 | qBuilder.appendWhere("_id=" + uri.getPathLeafId()); |
| 524 | } |
| 525 | |
| 526 | // Make the query. |
| 527 | Cursor c = qBuilder.query(mDb, |
| 528 | projection, |
| 529 | selection, |
| 530 | selectionArgs, |
| 531 | groupBy, |
| 532 | having, |
| 533 | sortOrder); |
| 534 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 535 | return c;</pre> |
| 536 | * |
| 537 | * @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] | 538 | * if the client is requesting a specific record, the URI will end in a record number |
| 539 | * that the implementation should parse and add to a WHERE or HAVING clause, specifying |
| 540 | * that _id value. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | * @param projection The list of columns to put into the cursor. If |
| 542 | * null all columns are included. |
| 543 | * @param selection A selection criteria to apply when filtering rows. |
| 544 | * If null then all rows are included. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 545 | * @param selectionArgs You may include ?s in selection, which will be replaced by |
| 546 | * the values from selectionArgs, in order that they appear in the selection. |
| 547 | * The values will be bound as Strings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | * @param sortOrder How the rows in the cursor should be sorted. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 549 | * 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] | 550 | * @return a Cursor or null. |
| 551 | */ |
| 552 | public abstract Cursor query(Uri uri, String[] projection, |
| 553 | String selection, String[] selectionArgs, String sortOrder); |
| 554 | |
Fred Quintana | 5bba632 | 2009-10-05 14:21:12 -0700 | [diff] [blame] | 555 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 556 | * Implement this to handle requests for the MIME type of the data at the |
| 557 | * given URI. The returned MIME type should start with |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | * <code>vnd.android.cursor.item</code> for a single record, |
| 559 | * or <code>vnd.android.cursor.dir/</code> for multiple items. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 560 | * 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] | 561 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 562 | * Processes and Threads</a>. |
| 563 | * |
| 564 | * @param uri the URI to query. |
| 565 | * @return a MIME type string, or null if there is no type. |
| 566 | */ |
| 567 | public abstract String getType(Uri uri); |
| 568 | |
| 569 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 570 | * 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] | 571 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 572 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 573 | * 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] | 574 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 575 | * Processes and Threads</a>. |
| 576 | * @param uri The content:// URI of the insertion request. |
| 577 | * @param values A set of column_name/value pairs to add to the database. |
| 578 | * @return The URI for the newly inserted item. |
| 579 | */ |
| 580 | public abstract Uri insert(Uri uri, ContentValues values); |
| 581 | |
| 582 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 583 | * Override this to handle requests to insert a set of new rows, or the |
| 584 | * default implementation will iterate over the values and call |
| 585 | * {@link #insert} on each of them. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 587 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 588 | * 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] | 589 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 590 | * Processes and Threads</a>. |
| 591 | * |
| 592 | * @param uri The content:// URI of the insertion request. |
| 593 | * @param values An array of sets of column_name/value pairs to add to the database. |
| 594 | * @return The number of values that were inserted. |
| 595 | */ |
| 596 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 597 | int numValues = values.length; |
| 598 | for (int i = 0; i < numValues; i++) { |
| 599 | insert(uri, values[i]); |
| 600 | } |
| 601 | return numValues; |
| 602 | } |
| 603 | |
| 604 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 605 | * Implement this to handle requests to delete one or more rows. |
| 606 | * The implementation should apply the selection clause when performing |
| 607 | * 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] | 608 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyDelete()} |
| 609 | * after deleting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 610 | * 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] | 611 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 612 | * Processes and Threads</a>. |
| 613 | * |
| 614 | * <p>The implementation is responsible for parsing out a row ID at the end |
| 615 | * of the URI, if a specific row is being deleted. That is, the client would |
| 616 | * pass in <code>content://contacts/people/22</code> and the implementation is |
| 617 | * responsible for parsing the record number (22) when creating a SQL statement. |
| 618 | * |
| 619 | * @param uri The full URI to query, including a row ID (if a specific record is requested). |
| 620 | * @param selection An optional restriction to apply to rows when deleting. |
| 621 | * @return The number of rows affected. |
| 622 | * @throws SQLException |
| 623 | */ |
| 624 | public abstract int delete(Uri uri, String selection, String[] selectionArgs); |
| 625 | |
| 626 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 627 | * Implement this to handle requests to update one or more rows. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 628 | * The implementation should update all rows matching the selection |
| 629 | * 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] | 630 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 631 | * after updating. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 632 | * 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] | 633 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 634 | * Processes and Threads</a>. |
| 635 | * |
| 636 | * @param uri The URI to query. This can potentially have a record ID if this |
| 637 | * is an update request for a specific record. |
| 638 | * @param values A Bundle mapping from column names to new column values (NULL is a |
| 639 | * valid value). |
| 640 | * @param selection An optional filter to match rows to update. |
| 641 | * @return the number of rows affected. |
| 642 | */ |
| 643 | public abstract int update(Uri uri, ContentValues values, String selection, |
| 644 | String[] selectionArgs); |
| 645 | |
| 646 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 647 | * Override this to handle requests to open a file blob. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 648 | * The default implementation always throws {@link FileNotFoundException}. |
| 649 | * 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] | 650 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 651 | * Processes and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 652 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 653 | * <p>This method returns a ParcelFileDescriptor, which is returned directly |
| 654 | * 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] | 655 | * returned without copying the content. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | * |
| 657 | * <p>The returned ParcelFileDescriptor is owned by the caller, so it is |
| 658 | * their responsibility to close it when done. That is, the implementation |
| 659 | * of this method should create a new ParcelFileDescriptor for each call. |
| 660 | * |
| 661 | * @param uri The URI whose file is to be opened. |
| 662 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 663 | * "rw" for read and write access, or "rwt" for read and write access |
| 664 | * that truncates any existing file. |
| 665 | * |
| 666 | * @return Returns a new ParcelFileDescriptor which you can use to access |
| 667 | * the file. |
| 668 | * |
| 669 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 670 | * no file associated with the given URI or the mode is invalid. |
| 671 | * @throws SecurityException Throws SecurityException if the caller does |
| 672 | * not have permission to access the file. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 673 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | * @see #openAssetFile(Uri, String) |
| 675 | * @see #openFileHelper(Uri, String) |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 676 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | public ParcelFileDescriptor openFile(Uri uri, String mode) |
| 678 | throws FileNotFoundException { |
| 679 | throw new FileNotFoundException("No files supported by provider at " |
| 680 | + uri); |
| 681 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | /** |
| 684 | * This is like {@link #openFile}, but can be implemented by providers |
| 685 | * 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] | 686 | * inside of their .apk. |
| 687 | * This method can be called from multiple threads, as described in |
| 688 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 689 | * Processes and Threads</a>. |
| 690 | * |
| 691 | * <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] | 692 | * file slices, either directly with |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 693 | * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 695 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 696 | * methods. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 697 | * |
| 698 | * <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] | 699 | * should create the AssetFileDescriptor with |
| 700 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 701 | * 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] | 702 | * |
| 703 | * @param uri The URI whose file is to be opened. |
| 704 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 705 | * "w" for write-only access (erasing whatever data is currently in |
| 706 | * the file), "wa" for write-only access to append to any existing data, |
| 707 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 708 | * and write access that truncates any existing file. |
| 709 | * |
| 710 | * @return Returns a new AssetFileDescriptor which you can use to access |
| 711 | * the file. |
| 712 | * |
| 713 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 714 | * no file associated with the given URI or the mode is invalid. |
| 715 | * @throws SecurityException Throws SecurityException if the caller does |
| 716 | * not have permission to access the file. |
| 717 | * |
| 718 | * @see #openFile(Uri, String) |
| 719 | * @see #openFileHelper(Uri, String) |
| 720 | */ |
| 721 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) |
| 722 | throws FileNotFoundException { |
| 723 | ParcelFileDescriptor fd = openFile(uri, mode); |
| 724 | return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Convenience for subclasses that wish to implement {@link #openFile} |
| 729 | * by looking up a column named "_data" at the given URI. |
| 730 | * |
| 731 | * @param uri The URI to be opened. |
| 732 | * @param mode The file mode. May be "r" for read-only access, |
| 733 | * "w" for write-only access (erasing whatever data is currently in |
| 734 | * the file), "wa" for write-only access to append to any existing data, |
| 735 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 736 | * and write access that truncates any existing file. |
| 737 | * |
| 738 | * @return Returns a new ParcelFileDescriptor that can be used by the |
| 739 | * client to access the file. |
| 740 | */ |
| 741 | protected final ParcelFileDescriptor openFileHelper(Uri uri, |
| 742 | String mode) throws FileNotFoundException { |
| 743 | Cursor c = query(uri, new String[]{"_data"}, null, null, null); |
| 744 | int count = (c != null) ? c.getCount() : 0; |
| 745 | if (count != 1) { |
| 746 | // If there is not exactly one result, throw an appropriate |
| 747 | // exception. |
| 748 | if (c != null) { |
| 749 | c.close(); |
| 750 | } |
| 751 | if (count == 0) { |
| 752 | throw new FileNotFoundException("No entry for " + uri); |
| 753 | } |
| 754 | throw new FileNotFoundException("Multiple items at " + uri); |
| 755 | } |
| 756 | |
| 757 | c.moveToFirst(); |
| 758 | int i = c.getColumnIndex("_data"); |
| 759 | String path = (i >= 0 ? c.getString(i) : null); |
| 760 | c.close(); |
| 761 | if (path == null) { |
| 762 | throw new FileNotFoundException("Column _data not found."); |
| 763 | } |
| 764 | |
| 765 | int modeBits = ContentResolver.modeToMode(uri, mode); |
| 766 | return ParcelFileDescriptor.open(new File(path), modeBits); |
| 767 | } |
| 768 | |
| 769 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 770 | * Called by a client to determine the types of data streams that this |
| 771 | * content provider supports for the given URI. The default implementation |
| 772 | * returns null, meaning no types. If your content provider stores data |
| 773 | * of a particular type, return that MIME type if it matches the given |
| 774 | * mimeTypeFilter. If it can perform type conversions, return an array |
| 775 | * of all supported MIME types that match mimeTypeFilter. |
| 776 | * |
| 777 | * @param uri The data in the content provider being queried. |
| 778 | * @param mimeTypeFilter The type of data the client desires. May be |
| 779 | * a pattern, such as *\/* to retrieve all possible data types. |
Dianne Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 780 | * @return Returns null if there are no possible data streams for the |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 781 | * given mimeTypeFilter. Otherwise returns an array of all available |
| 782 | * concrete MIME types. |
| 783 | * |
| 784 | * @see #getType(Uri) |
| 785 | * @see #openTypedAssetFile(Uri, String, Bundle) |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame^] | 786 | * @see ClipDescription#compareMimeTypes(String, String) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 787 | */ |
| 788 | public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { |
| 789 | return null; |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Called by a client to open a read-only stream containing data of a |
| 794 | * particular MIME type. This is like {@link #openAssetFile(Uri, String)}, |
| 795 | * except the file can only be read-only and the content provider may |
| 796 | * perform data conversions to generate data of the desired type. |
| 797 | * |
| 798 | * <p>The default implementation compares the given mimeType against the |
| 799 | * result of {@link #getType(Uri)} and, if the match, simple calls |
| 800 | * {@link #openAssetFile(Uri, String)}. |
| 801 | * |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame^] | 802 | * <p>See {@link ClipData} for examples of the use and implementation |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 803 | * of this method. |
| 804 | * |
| 805 | * @param uri The data in the content provider being queried. |
| 806 | * @param mimeTypeFilter The type of data the client desires. May be |
| 807 | * a pattern, such as *\/*, if the caller does not have specific type |
| 808 | * requirements; in this case the content provider will pick its best |
| 809 | * type matching the pattern. |
| 810 | * @param opts Additional options from the client. The definitions of |
| 811 | * these are specific to the content provider being called. |
| 812 | * |
| 813 | * @return Returns a new AssetFileDescriptor from which the client can |
| 814 | * read data of the desired type. |
| 815 | * |
| 816 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 817 | * no file associated with the given URI or the mode is invalid. |
| 818 | * @throws SecurityException Throws SecurityException if the caller does |
| 819 | * not have permission to access the data. |
| 820 | * @throws IllegalArgumentException Throws IllegalArgumentException if the |
| 821 | * content provider does not support the requested MIME type. |
| 822 | * |
| 823 | * @see #getStreamTypes(Uri, String) |
| 824 | * @see #openAssetFile(Uri, String) |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame^] | 825 | * @see ClipDescription#compareMimeTypes(String, String) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 826 | */ |
| 827 | public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts) |
| 828 | throws FileNotFoundException { |
Dianne Hackborn | 02dfd26 | 2010-08-13 12:34:58 -0700 | [diff] [blame] | 829 | if ("*/*".equals(mimeTypeFilter)) { |
| 830 | // If they can take anything, the untyped open call is good enough. |
| 831 | return openAssetFile(uri, "r"); |
| 832 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 833 | String baseType = getType(uri); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame^] | 834 | if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) { |
Dianne Hackborn | 02dfd26 | 2010-08-13 12:34:58 -0700 | [diff] [blame] | 835 | // Use old untyped open call if this provider has a type for this |
| 836 | // URI and it matches the request. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 837 | return openAssetFile(uri, "r"); |
| 838 | } |
| 839 | throw new FileNotFoundException("Can't open " + uri + " as type " + mimeTypeFilter); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Interface to write a stream of data to a pipe. Use with |
| 844 | * {@link ContentProvider#openPipeHelper}. |
| 845 | */ |
| 846 | public interface PipeDataWriter<T> { |
| 847 | /** |
| 848 | * Called from a background thread to stream data out to a pipe. |
| 849 | * Note that the pipe is blocking, so this thread can block on |
| 850 | * writes for an arbitrary amount of time if the client is slow |
| 851 | * at reading. |
| 852 | * |
| 853 | * @param output The pipe where data should be written. This will be |
| 854 | * closed for you upon returning from this function. |
| 855 | * @param uri The URI whose data is to be written. |
| 856 | * @param mimeType The desired type of data to be written. |
| 857 | * @param opts Options supplied by caller. |
| 858 | * @param args Your own custom arguments. |
| 859 | */ |
| 860 | public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType, |
| 861 | Bundle opts, T args); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * A helper function for implementing {@link #openTypedAssetFile}, for |
| 866 | * creating a data pipe and background thread allowing you to stream |
| 867 | * generated data back to the client. This function returns a new |
| 868 | * ParcelFileDescriptor that should be returned to the caller (the caller |
| 869 | * is responsible for closing it). |
| 870 | * |
| 871 | * @param uri The URI whose data is to be written. |
| 872 | * @param mimeType The desired type of data to be written. |
| 873 | * @param opts Options supplied by caller. |
| 874 | * @param args Your own custom arguments. |
| 875 | * @param func Interface implementing the function that will actually |
| 876 | * stream the data. |
| 877 | * @return Returns a new ParcelFileDescriptor holding the read side of |
| 878 | * the pipe. This should be returned to the caller for reading; the caller |
| 879 | * is responsible for closing it when done. |
| 880 | */ |
| 881 | public <T> ParcelFileDescriptor openPipeHelper(final Uri uri, final String mimeType, |
| 882 | final Bundle opts, final T args, final PipeDataWriter<T> func) |
| 883 | throws FileNotFoundException { |
| 884 | try { |
| 885 | final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); |
| 886 | |
| 887 | AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() { |
| 888 | @Override |
| 889 | protected Object doInBackground(Object... params) { |
| 890 | func.writeDataToPipe(fds[1], uri, mimeType, opts, args); |
| 891 | try { |
| 892 | fds[1].close(); |
| 893 | } catch (IOException e) { |
| 894 | Log.w(TAG, "Failure closing pipe", e); |
| 895 | } |
| 896 | return null; |
| 897 | } |
| 898 | }; |
| 899 | task.execute((Object[])null); |
| 900 | |
| 901 | return fds[0]; |
| 902 | } catch (IOException e) { |
| 903 | throw new FileNotFoundException("failure making pipe"); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | * Returns true if this instance is a temporary content provider. |
| 909 | * @return true if this instance is a temporary content provider |
| 910 | */ |
| 911 | protected boolean isTemporary() { |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Returns the Binder object for this provider. |
| 917 | * |
| 918 | * @return the Binder object for this provider |
| 919 | * @hide |
| 920 | */ |
| 921 | public IContentProvider getIContentProvider() { |
| 922 | return mTransport; |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * After being instantiated, this is called to tell the content provider |
| 927 | * about itself. |
| 928 | * |
| 929 | * @param context The context this provider is running in |
| 930 | * @param info Registered information about this content provider |
| 931 | */ |
| 932 | public void attachInfo(Context context, ProviderInfo info) { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 933 | /* |
| 934 | * We may be using AsyncTask from binder threads. Make it init here |
| 935 | * so its static handler is on the main thread. |
| 936 | */ |
| 937 | AsyncTask.init(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | |
| 939 | /* |
| 940 | * Only allow it to be set once, so after the content service gives |
| 941 | * this to us clients can't change it. |
| 942 | */ |
| 943 | if (mContext == null) { |
| 944 | mContext = context; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 945 | mMyUid = Process.myUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | if (info != null) { |
| 947 | setReadPermission(info.readPermission); |
| 948 | setWritePermission(info.writePermission); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 949 | setPathPermissions(info.pathPermissions); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 950 | mExported = info.exported; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | } |
| 952 | ContentProvider.this.onCreate(); |
| 953 | } |
| 954 | } |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 955 | |
| 956 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 957 | * Override this to handle requests to perform a batch of operations, or the |
| 958 | * default implementation will iterate over the operations and call |
| 959 | * {@link ContentProviderOperation#apply} on each of them. |
| 960 | * If all calls to {@link ContentProviderOperation#apply} succeed |
| 961 | * then a {@link ContentProviderResult} array with as many |
| 962 | * 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] | 963 | * fail, it is up to the implementation how many of the others take effect. |
| 964 | * This method can be called from multiple threads, as described in |
| 965 | * <a href="{@docRoot}guide/topics/fundamentals.html#procthread">Application Fundamentals: |
| 966 | * Processes and Threads</a>. |
| 967 | * |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 968 | * @param operations the operations to apply |
| 969 | * @return the results of the applications |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 970 | * @throws OperationApplicationException thrown if any operation fails. |
| 971 | * @see ContentProviderOperation#apply |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 972 | */ |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 973 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 974 | throws OperationApplicationException { |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 975 | final int numOperations = operations.size(); |
| 976 | final ContentProviderResult[] results = new ContentProviderResult[numOperations]; |
| 977 | for (int i = 0; i < numOperations; i++) { |
| 978 | results[i] = operations.get(i).apply(this, results, i); |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 979 | } |
| 980 | return results; |
| 981 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 982 | |
| 983 | /** |
| 984 | * @hide -- until interface has proven itself |
| 985 | * |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 986 | * Call a provider-defined method. This can be used to implement |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 987 | * interfaces that are cheaper than using a Cursor. |
| 988 | * |
| 989 | * @param method Method name to call. Opaque to framework. |
| 990 | * @param request Nullable String argument passed to method. |
| 991 | * @param args Nullable Bundle argument passed to method. |
| 992 | */ |
| 993 | public Bundle call(String method, String request, Bundle args) { |
| 994 | return null; |
| 995 | } |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 996 | |
| 997 | /** |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 998 | * Implement this to shut down the ContentProvider instance. You can then |
| 999 | * invoke this method in unit tests. |
| 1000 | * |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 1001 | * <p> |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 1002 | * Android normally handles ContentProvider startup and shutdown |
| 1003 | * automatically. You do not need to start up or shut down a |
| 1004 | * ContentProvider. When you invoke a test method on a ContentProvider, |
| 1005 | * however, a ContentProvider instance is started and keeps running after |
| 1006 | * the test finishes, even if a succeeding test instantiates another |
| 1007 | * ContentProvider. A conflict develops because the two instances are |
| 1008 | * usually running against the same underlying data source (for example, an |
| 1009 | * sqlite database). |
| 1010 | * </p> |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 1011 | * <p> |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 1012 | * Implementing shutDown() avoids this conflict by providing a way to |
| 1013 | * terminate the ContentProvider. This method can also prevent memory leaks |
| 1014 | * from multiple instantiations of the ContentProvider, and it can ensure |
| 1015 | * unit test isolation by allowing you to completely clean up the test |
| 1016 | * fixture before moving on to the next test. |
| 1017 | * </p> |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 1018 | */ |
| 1019 | public void shutdown() { |
| 1020 | Log.w(TAG, "implement ContentProvider shutdown() to make sure all database " + |
| 1021 | "connections are gracefully shutdown"); |
| 1022 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 1023 | } |