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