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 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 19 | import static android.Manifest.permission.INTERACT_ACROSS_USERS; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 20 | import static android.app.AppOpsManager.MODE_ALLOWED; |
Eugene Susla | 9351985 | 2018-06-13 16:44:31 -0700 | [diff] [blame] | 21 | import static android.app.AppOpsManager.MODE_DEFAULT; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 22 | import static android.app.AppOpsManager.MODE_ERRORED; |
| 23 | import static android.app.AppOpsManager.MODE_IGNORED; |
| 24 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 25 | import static android.os.Trace.TRACE_TAG_DATABASE; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 26 | |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 27 | import android.annotation.NonNull; |
Scott Kennedy | 9f78f65 | 2015-03-01 15:29:25 -0800 | [diff] [blame] | 28 | import android.annotation.Nullable; |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 29 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 30 | import android.app.AppOpsManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 31 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.pm.ProviderInfo; |
| 33 | import android.content.res.AssetFileDescriptor; |
| 34 | import android.content.res.Configuration; |
| 35 | import android.database.Cursor; |
Svet Ganov | 7271f3e | 2015-04-23 10:16:53 -0700 | [diff] [blame] | 36 | import android.database.MatrixCursor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.database.SQLException; |
| 38 | import android.net.Uri; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 39 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.Binder; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 41 | import android.os.Build; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 42 | import android.os.Bundle; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 43 | import android.os.CancellationSignal; |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 44 | import android.os.IBinder; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 45 | import android.os.ICancellationSignal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.ParcelFileDescriptor; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 47 | import android.os.Process; |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 48 | import android.os.RemoteException; |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 49 | import android.os.Trace; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
Jeff Sharkey | b31afd2 | 2017-06-12 14:17:10 -0600 | [diff] [blame] | 51 | import android.os.storage.StorageManager; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 52 | import android.text.TextUtils; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 53 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 55 | import com.android.internal.annotations.VisibleForTesting; |
| 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import java.io.File; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 58 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import java.io.FileNotFoundException; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 60 | import java.io.IOException; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 61 | import java.io.PrintWriter; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 62 | import java.util.ArrayList; |
Andreas Gampe | e6748ce | 2015-12-11 18:00:38 -0800 | [diff] [blame] | 63 | import java.util.Arrays; |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 64 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Content providers are one of the primary building blocks of Android applications, providing |
| 68 | * content to applications. They encapsulate data and provide it to applications through the single |
| 69 | * {@link ContentResolver} interface. A content provider is only required if you need to share |
| 70 | * data between multiple applications. For example, the contacts data is used by multiple |
| 71 | * applications and must be stored in a content provider. If you don't need to share data amongst |
| 72 | * multiple applications you can use a database directly via |
| 73 | * {@link android.database.sqlite.SQLiteDatabase}. |
| 74 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * <p>When a request is made via |
| 76 | * a {@link ContentResolver} the system inspects the authority of the given URI and passes the |
| 77 | * request to the content provider registered with the authority. The content provider can interpret |
| 78 | * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing |
| 79 | * URIs.</p> |
| 80 | * |
| 81 | * <p>The primary methods that need to be implemented are: |
| 82 | * <ul> |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 83 | * <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] | 84 | * <li>{@link #query} which returns data to the caller</li> |
| 85 | * <li>{@link #insert} which inserts new data into the content provider</li> |
| 86 | * <li>{@link #update} which updates existing data in the content provider</li> |
| 87 | * <li>{@link #delete} which deletes data from the content provider</li> |
| 88 | * <li>{@link #getType} which returns the MIME type of data in the content provider</li> |
| 89 | * </ul></p> |
| 90 | * |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 91 | * <p class="caution">Data access methods (such as {@link #insert} and |
| 92 | * {@link #update}) may be called from many threads at once, and must be thread-safe. |
| 93 | * Other methods (such as {@link #onCreate}) are only called from the application |
| 94 | * main thread, and must avoid performing lengthy operations. See the method |
| 95 | * descriptions for their expected thread behavior.</p> |
| 96 | * |
| 97 | * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate |
| 98 | * ContentProvider instance, so subclasses don't have to worry about the details of |
| 99 | * cross-process calls.</p> |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 100 | * |
| 101 | * <div class="special reference"> |
| 102 | * <h3>Developer Guides</h3> |
| 103 | * <p>For more information about using content providers, read the |
| 104 | * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a> |
| 105 | * developer guide.</p> |
Nicole Borrelli | 8a5f04a | 2018-09-20 14:19:14 -0700 | [diff] [blame] | 106 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 108 | public abstract class ContentProvider implements ContentInterface, ComponentCallbacks2 { |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 109 | |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 110 | private static final String TAG = "ContentProvider"; |
| 111 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 112 | /* |
| 113 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 114 | * MockContentProvider. |
| 115 | */ |
| 116 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 117 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | private Context mContext = null; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 119 | private int mMyUid; |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 120 | |
| 121 | // Since most Providers have only one authority, we keep both a String and a String[] to improve |
| 122 | // performance. |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 124 | private String mAuthority; |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 125 | @UnsupportedAppUsage |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 126 | private String[] mAuthorities; |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | private String mReadPermission; |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 129 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | private String mWritePermission; |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 131 | @UnsupportedAppUsage |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 132 | private PathPermission[] mPathPermissions; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 133 | private boolean mExported; |
Dianne Hackborn | 7e6f976 | 2013-02-26 13:35:11 -0800 | [diff] [blame] | 134 | private boolean mNoPerms; |
Amith Yamasani | a6f4d58 | 2014-08-07 17:58:39 -0700 | [diff] [blame] | 135 | private boolean mSingleUser; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
Jeff Sharkey | 497789e | 2019-02-15 19:41:30 -0700 | [diff] [blame] | 137 | private ThreadLocal<String> mCallingPackage; |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | private Transport mTransport = new Transport(); |
| 140 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Construct a ContentProvider instance. Content providers must be |
| 143 | * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared |
| 144 | * in the manifest</a>, accessed with {@link ContentResolver}, and created |
| 145 | * automatically by the system, so applications usually do not create |
| 146 | * ContentProvider instances directly. |
| 147 | * |
| 148 | * <p>At construction time, the object is uninitialized, and most fields and |
| 149 | * methods are unavailable. Subclasses should initialize themselves in |
| 150 | * {@link #onCreate}, not the constructor. |
| 151 | * |
| 152 | * <p>Content providers are created on the application main thread at |
| 153 | * application launch time. The constructor must not perform lengthy |
| 154 | * operations, or application startup will be delayed. |
| 155 | */ |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 156 | public ContentProvider() { |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Constructor just for mocking. |
| 161 | * |
| 162 | * @param context A Context object which should be some mock instance (like the |
| 163 | * instance of {@link android.test.mock.MockContext}). |
| 164 | * @param readPermission The read permision you want this instance should have in the |
| 165 | * test, which is available via {@link #getReadPermission()}. |
| 166 | * @param writePermission The write permission you want this instance should have |
| 167 | * in the test, which is available via {@link #getWritePermission()}. |
| 168 | * @param pathPermissions The PathPermissions you want this instance should have |
| 169 | * in the test, which is available via {@link #getPathPermissions()}. |
| 170 | * @hide |
| 171 | */ |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 172 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 173 | public ContentProvider( |
| 174 | Context context, |
| 175 | String readPermission, |
| 176 | String writePermission, |
| 177 | PathPermission[] pathPermissions) { |
| 178 | mContext = context; |
| 179 | mReadPermission = readPermission; |
| 180 | mWritePermission = writePermission; |
| 181 | mPathPermissions = pathPermissions; |
| 182 | } |
| 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Given an IContentProvider, try to coerce it back to the real |
| 186 | * ContentProvider object if it is running in the local process. This can |
| 187 | * be used if you know you are running in the same process as a provider, |
| 188 | * and want to get direct access to its implementation details. Most |
| 189 | * clients should not nor have a reason to use it. |
| 190 | * |
| 191 | * @param abstractInterface The ContentProvider interface that is to be |
| 192 | * coerced. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 193 | * @return If the IContentProvider is non-{@code null} and local, returns its actual |
| 194 | * ContentProvider instance. Otherwise returns {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | * @hide |
| 196 | */ |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 197 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | public static ContentProvider coerceToLocalContentProvider( |
| 199 | IContentProvider abstractInterface) { |
| 200 | if (abstractInterface instanceof Transport) { |
| 201 | return ((Transport)abstractInterface).getContentProvider(); |
| 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Binder object that deals with remoting. |
| 208 | * |
| 209 | * @hide |
| 210 | */ |
| 211 | class Transport extends ContentProviderNative { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 212 | AppOpsManager mAppOpsManager = null; |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 213 | int mReadOp = AppOpsManager.OP_NONE; |
| 214 | int mWriteOp = AppOpsManager.OP_NONE; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | ContentProvider getContentProvider() { |
| 217 | return ContentProvider.this; |
| 218 | } |
| 219 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 220 | @Override |
| 221 | public String getProviderName() { |
| 222 | return getContentProvider().getClass().getName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 225 | @Override |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 226 | public Cursor query(String callingPkg, Uri uri, @Nullable String[] projection, |
| 227 | @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 228 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 229 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 230 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Svet Ganov | 7271f3e | 2015-04-23 10:16:53 -0700 | [diff] [blame] | 231 | // The caller has no access to the data, so return an empty cursor with |
| 232 | // the columns in the requested order. The caller may ask for an invalid |
| 233 | // column and we would not catch that but this is not a problem in practice. |
| 234 | // We do not call ContentProvider#query with a modified where clause since |
| 235 | // the implementation is not guaranteed to be backed by a SQL database, hence |
| 236 | // it may not handle properly the tautology where clause we would have created. |
Svet Ganov | a2147ec | 2015-04-27 17:00:44 -0700 | [diff] [blame] | 237 | if (projection != null) { |
| 238 | return new MatrixCursor(projection, 0); |
| 239 | } |
| 240 | |
| 241 | // Null projection means all columns but we have no idea which they are. |
| 242 | // However, the caller may be expecting to access them my index. Hence, |
| 243 | // we have to execute the query as if allowed to get a cursor with the |
| 244 | // columns. We then use the column names to return an empty cursor. |
Makoto Onuki | 2cc250b | 2018-08-28 15:40:10 -0700 | [diff] [blame] | 245 | Cursor cursor; |
| 246 | final String original = setCallingPackage(callingPkg); |
| 247 | try { |
| 248 | cursor = ContentProvider.this.query( |
| 249 | uri, projection, queryArgs, |
| 250 | CancellationSignal.fromTransport(cancellationSignal)); |
| 251 | } finally { |
| 252 | setCallingPackage(original); |
| 253 | } |
Makoto Onuki | 34bdcdb | 2015-06-12 17:14:57 -0700 | [diff] [blame] | 254 | if (cursor == null) { |
| 255 | return null; |
Svet Ganov | a2147ec | 2015-04-27 17:00:44 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Return an empty cursor for all columns. |
Makoto Onuki | 34bdcdb | 2015-06-12 17:14:57 -0700 | [diff] [blame] | 259 | return new MatrixCursor(cursor.getColumnNames(), 0); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 260 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 261 | Trace.traceBegin(TRACE_TAG_DATABASE, "query"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 262 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 263 | try { |
| 264 | return ContentProvider.this.query( |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 265 | uri, projection, queryArgs, |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 266 | CancellationSignal.fromTransport(cancellationSignal)); |
| 267 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 268 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 269 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 270 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 273 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | public String getType(Uri uri) { |
Makoto Onuki | 2cc250b | 2018-08-28 15:40:10 -0700 | [diff] [blame] | 275 | // getCallingPackage() isn't available in getType(), as the javadoc states. |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 276 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 277 | uri = maybeGetUriWithoutUserId(uri); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 278 | Trace.traceBegin(TRACE_TAG_DATABASE, "getType"); |
| 279 | try { |
| 280 | return ContentProvider.this.getType(uri); |
| 281 | } finally { |
| 282 | Trace.traceEnd(TRACE_TAG_DATABASE); |
| 283 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 286 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 287 | public Uri insert(String callingPkg, Uri uri, ContentValues initialValues) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 288 | uri = validateIncomingUri(uri); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 289 | int userId = getUserIdFromUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 290 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 291 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Makoto Onuki | 2cc250b | 2018-08-28 15:40:10 -0700 | [diff] [blame] | 292 | final String original = setCallingPackage(callingPkg); |
| 293 | try { |
| 294 | return rejectInsert(uri, initialValues); |
| 295 | } finally { |
| 296 | setCallingPackage(original); |
| 297 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 298 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 299 | Trace.traceBegin(TRACE_TAG_DATABASE, "insert"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 300 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 301 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 302 | return maybeAddUserId(ContentProvider.this.insert(uri, initialValues), userId); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 303 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 304 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 305 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 309 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 310 | public int bulkInsert(String callingPkg, Uri uri, ContentValues[] initialValues) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 311 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 312 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 313 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 314 | return 0; |
| 315 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 316 | Trace.traceBegin(TRACE_TAG_DATABASE, "bulkInsert"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 317 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 318 | try { |
| 319 | return ContentProvider.this.bulkInsert(uri, initialValues); |
| 320 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 321 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 322 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 326 | @Override |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 327 | public ContentProviderResult[] applyBatch(String callingPkg, String authority, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 328 | ArrayList<ContentProviderOperation> operations) |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 329 | throws OperationApplicationException { |
Jeff Sharkey | 2de00bf | 2018-12-13 15:06:05 -0700 | [diff] [blame] | 330 | validateIncomingAuthority(authority); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 331 | int numOperations = operations.size(); |
| 332 | final int[] userIds = new int[numOperations]; |
| 333 | for (int i = 0; i < numOperations; i++) { |
| 334 | ContentProviderOperation operation = operations.get(i); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 335 | Uri uri = operation.getUri(); |
Jeff Sharkey | 9144b4d | 2018-09-26 20:15:12 -0600 | [diff] [blame] | 336 | userIds[i] = getUserIdFromUri(uri); |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 337 | uri = validateIncomingUri(uri); |
| 338 | uri = maybeGetUriWithoutUserId(uri); |
| 339 | // Rebuild operation if we changed the Uri above |
| 340 | if (!Objects.equals(operation.getUri(), uri)) { |
| 341 | operation = new ContentProviderOperation(operation, uri); |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 342 | operations.set(i, operation); |
| 343 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 344 | if (operation.isReadOperation()) { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 345 | if (enforceReadPermission(callingPkg, uri, null) |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 346 | != AppOpsManager.MODE_ALLOWED) { |
| 347 | throw new OperationApplicationException("App op not allowed", 0); |
| 348 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 349 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 350 | if (operation.isWriteOperation()) { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 351 | if (enforceWritePermission(callingPkg, uri, null) |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 352 | != AppOpsManager.MODE_ALLOWED) { |
| 353 | throw new OperationApplicationException("App op not allowed", 0); |
| 354 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 357 | Trace.traceBegin(TRACE_TAG_DATABASE, "applyBatch"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 358 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 359 | try { |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 360 | ContentProviderResult[] results = ContentProvider.this.applyBatch(authority, |
| 361 | operations); |
Jay Shrauner | ac2506c | 2014-12-15 12:28:25 -0800 | [diff] [blame] | 362 | if (results != null) { |
| 363 | for (int i = 0; i < results.length ; i++) { |
| 364 | if (userIds[i] != UserHandle.USER_CURRENT) { |
| 365 | // Adding the userId to the uri. |
| 366 | results[i] = new ContentProviderResult(results[i], userIds[i]); |
| 367 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | return results; |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 371 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 372 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 373 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 374 | } |
Fred Quintana | 6a8d533 | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 377 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 378 | public int delete(String callingPkg, Uri uri, String selection, String[] selectionArgs) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 379 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 380 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 381 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 382 | return 0; |
| 383 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 384 | Trace.traceBegin(TRACE_TAG_DATABASE, "delete"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 385 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 386 | try { |
| 387 | return ContentProvider.this.delete(uri, selection, selectionArgs); |
| 388 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 389 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 390 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 394 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 395 | 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] | 396 | String[] selectionArgs) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 397 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 398 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 399 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 400 | return 0; |
| 401 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 402 | Trace.traceBegin(TRACE_TAG_DATABASE, "update"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 403 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 404 | try { |
| 405 | return ContentProvider.this.update(uri, values, selection, selectionArgs); |
| 406 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 407 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 408 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 409 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 412 | @Override |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 413 | public ParcelFileDescriptor openFile( |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 414 | String callingPkg, Uri uri, String mode, ICancellationSignal cancellationSignal, |
| 415 | IBinder callerToken) throws FileNotFoundException { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 416 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 417 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 418 | enforceFilePermission(callingPkg, uri, mode, callerToken); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 419 | Trace.traceBegin(TRACE_TAG_DATABASE, "openFile"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 420 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 421 | try { |
| 422 | return ContentProvider.this.openFile( |
| 423 | uri, mode, CancellationSignal.fromTransport(cancellationSignal)); |
| 424 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 425 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 426 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 430 | @Override |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 431 | public AssetFileDescriptor openAssetFile( |
| 432 | String callingPkg, Uri uri, String mode, ICancellationSignal cancellationSignal) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | throws FileNotFoundException { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 434 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 435 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 436 | enforceFilePermission(callingPkg, uri, mode, null); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 437 | Trace.traceBegin(TRACE_TAG_DATABASE, "openAssetFile"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 438 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 439 | try { |
| 440 | return ContentProvider.this.openAssetFile( |
| 441 | uri, mode, CancellationSignal.fromTransport(cancellationSignal)); |
| 442 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 443 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 444 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 448 | @Override |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 449 | public Bundle call(String callingPkg, String authority, String method, @Nullable String arg, |
| 450 | @Nullable Bundle extras) { |
Jeff Sharkey | 2de00bf | 2018-12-13 15:06:05 -0700 | [diff] [blame] | 451 | validateIncomingAuthority(authority); |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 452 | Bundle.setDefusable(extras, true); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 453 | Trace.traceBegin(TRACE_TAG_DATABASE, "call"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 454 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 455 | try { |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 456 | return ContentProvider.this.call(authority, method, arg, extras); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 457 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 458 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 459 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 460 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 463 | @Override |
| 464 | public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { |
Makoto Onuki | 2cc250b | 2018-08-28 15:40:10 -0700 | [diff] [blame] | 465 | // getCallingPackage() isn't available in getType(), as the javadoc states. |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 466 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 467 | uri = maybeGetUriWithoutUserId(uri); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 468 | Trace.traceBegin(TRACE_TAG_DATABASE, "getStreamTypes"); |
| 469 | try { |
| 470 | return ContentProvider.this.getStreamTypes(uri, mimeTypeFilter); |
| 471 | } finally { |
| 472 | Trace.traceEnd(TRACE_TAG_DATABASE); |
| 473 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 477 | public AssetFileDescriptor openTypedAssetFile(String callingPkg, Uri uri, String mimeType, |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 478 | Bundle opts, ICancellationSignal cancellationSignal) throws FileNotFoundException { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 479 | Bundle.setDefusable(opts, true); |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 480 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 481 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 482 | enforceFilePermission(callingPkg, uri, "r", null); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 483 | Trace.traceBegin(TRACE_TAG_DATABASE, "openTypedAssetFile"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 484 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 485 | try { |
| 486 | return ContentProvider.this.openTypedAssetFile( |
| 487 | uri, mimeType, opts, CancellationSignal.fromTransport(cancellationSignal)); |
| 488 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 489 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 490 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 491 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 494 | @Override |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 495 | public ICancellationSignal createCancellationSignal() { |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 496 | return CancellationSignal.createTransport(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 499 | @Override |
| 500 | public Uri canonicalize(String callingPkg, Uri uri) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 501 | uri = validateIncomingUri(uri); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 502 | int userId = getUserIdFromUri(uri); |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 503 | uri = getUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 504 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 505 | return null; |
| 506 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 507 | Trace.traceBegin(TRACE_TAG_DATABASE, "canonicalize"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 508 | final String original = setCallingPackage(callingPkg); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 509 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 510 | return maybeAddUserId(ContentProvider.this.canonicalize(uri), userId); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 511 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 512 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 513 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public Uri uncanonicalize(String callingPkg, Uri uri) { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 519 | uri = validateIncomingUri(uri); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 520 | int userId = getUserIdFromUri(uri); |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 521 | uri = getUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 522 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 523 | return null; |
| 524 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 525 | Trace.traceBegin(TRACE_TAG_DATABASE, "uncanonicalize"); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 526 | final String original = setCallingPackage(callingPkg); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 527 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 528 | return maybeAddUserId(ContentProvider.this.uncanonicalize(uri), userId); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 529 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 530 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 531 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 535 | @Override |
| 536 | public boolean refresh(String callingPkg, Uri uri, Bundle args, |
| 537 | ICancellationSignal cancellationSignal) throws RemoteException { |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 538 | uri = validateIncomingUri(uri); |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 539 | uri = getUriWithoutUserId(uri); |
| 540 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
| 541 | return false; |
| 542 | } |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 543 | Trace.traceBegin(TRACE_TAG_DATABASE, "refresh"); |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 544 | final String original = setCallingPackage(callingPkg); |
| 545 | try { |
| 546 | return ContentProvider.this.refresh(uri, args, |
| 547 | CancellationSignal.fromTransport(cancellationSignal)); |
| 548 | } finally { |
| 549 | setCallingPackage(original); |
Jeff Sharkey | 9664ff5 | 2018-08-03 17:08:04 -0600 | [diff] [blame] | 550 | Trace.traceEnd(TRACE_TAG_DATABASE); |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 554 | private void enforceFilePermission(String callingPkg, Uri uri, String mode, |
| 555 | IBinder callerToken) throws FileNotFoundException, SecurityException { |
Jeff Sharkey | ba76197 | 2013-02-28 15:57:36 -0800 | [diff] [blame] | 556 | if (mode != null && mode.indexOf('w') != -1) { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 557 | if (enforceWritePermission(callingPkg, uri, callerToken) |
| 558 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 559 | throw new FileNotFoundException("App op not allowed"); |
| 560 | } |
| 561 | } else { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 562 | if (enforceReadPermission(callingPkg, uri, callerToken) |
| 563 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 564 | throw new FileNotFoundException("App op not allowed"); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 569 | private int enforceReadPermission(String callingPkg, Uri uri, IBinder callerToken) |
| 570 | throws SecurityException { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 571 | final int mode = enforceReadPermissionInner(uri, callingPkg, callerToken); |
| 572 | if (mode != MODE_ALLOWED) { |
| 573 | return mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 574 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 575 | |
Eugene Susla | 9351985 | 2018-06-13 16:44:31 -0700 | [diff] [blame] | 576 | return noteProxyOp(callingPkg, mReadOp); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 579 | private int enforceWritePermission(String callingPkg, Uri uri, IBinder callerToken) |
| 580 | throws SecurityException { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 581 | final int mode = enforceWritePermissionInner(uri, callingPkg, callerToken); |
| 582 | if (mode != MODE_ALLOWED) { |
| 583 | return mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 584 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 585 | |
Eugene Susla | 9351985 | 2018-06-13 16:44:31 -0700 | [diff] [blame] | 586 | return noteProxyOp(callingPkg, mWriteOp); |
| 587 | } |
| 588 | |
| 589 | private int noteProxyOp(String callingPkg, int op) { |
| 590 | if (op != AppOpsManager.OP_NONE) { |
| 591 | int mode = mAppOpsManager.noteProxyOp(op, callingPkg); |
Eugene Susla | b22f71e | 2018-11-30 10:17:20 -0800 | [diff] [blame] | 592 | int nonDefaultMode = mode == MODE_DEFAULT ? interpretDefaultAppOpMode(op) : mode; |
| 593 | if (mode == MODE_DEFAULT && nonDefaultMode == MODE_IGNORED) { |
Eugene Susla | aaa5427 | 2018-12-06 11:04:21 -0800 | [diff] [blame] | 594 | Log.w(TAG, "Denying access for " + callingPkg + " to " + getClass().getName() |
Eugene Susla | b22f71e | 2018-11-30 10:17:20 -0800 | [diff] [blame] | 595 | + " (" + AppOpsManager.opToName(op) |
| 596 | + " = " + AppOpsManager.opToName(mode) + ")"); |
| 597 | } |
| 598 | return mode == MODE_DEFAULT ? nonDefaultMode : mode; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 601 | return AppOpsManager.MODE_ALLOWED; |
| 602 | } |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 603 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 604 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 605 | boolean checkUser(int pid, int uid, Context context) { |
| 606 | return UserHandle.getUserId(uid) == context.getUserId() |
Amith Yamasani | a6f4d58 | 2014-08-07 17:58:39 -0700 | [diff] [blame] | 607 | || mSingleUser |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 608 | || context.checkPermission(INTERACT_ACROSS_USERS, pid, uid) |
| 609 | == PERMISSION_GRANTED; |
| 610 | } |
| 611 | |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 612 | /** |
| 613 | * Verify that calling app holds both the given permission and any app-op |
| 614 | * associated with that permission. |
| 615 | */ |
| 616 | private int checkPermissionAndAppOp(String permission, String callingPkg, |
| 617 | IBinder callerToken) { |
| 618 | if (getContext().checkPermission(permission, Binder.getCallingPid(), Binder.getCallingUid(), |
| 619 | callerToken) != PERMISSION_GRANTED) { |
| 620 | return MODE_ERRORED; |
| 621 | } |
| 622 | |
Eugene Susla | 9351985 | 2018-06-13 16:44:31 -0700 | [diff] [blame] | 623 | return mTransport.noteProxyOp(callingPkg, AppOpsManager.permissionToOpCode(permission)); |
| 624 | } |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 625 | |
Eugene Susla | 9351985 | 2018-06-13 16:44:31 -0700 | [diff] [blame] | 626 | /** |
| 627 | * Allows for custom interpretations of {@link AppOpsManager#MODE_DEFAULT} by individual |
| 628 | * content providers |
| 629 | * |
| 630 | * @hide |
| 631 | */ |
| 632 | protected int interpretDefaultAppOpMode(int op) { |
| 633 | return MODE_IGNORED; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 636 | /** {@hide} */ |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 637 | protected int enforceReadPermissionInner(Uri uri, String callingPkg, IBinder callerToken) |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 638 | throws SecurityException { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 639 | final Context context = getContext(); |
| 640 | final int pid = Binder.getCallingPid(); |
| 641 | final int uid = Binder.getCallingUid(); |
| 642 | String missingPerm = null; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 643 | int strongestMode = MODE_ALLOWED; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 644 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 645 | if (UserHandle.isSameApp(uid, mMyUid)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 646 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 649 | if (mExported && checkUser(pid, uid, context)) { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 650 | final String componentPerm = getReadPermission(); |
| 651 | if (componentPerm != null) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 652 | final int mode = checkPermissionAndAppOp(componentPerm, callingPkg, callerToken); |
| 653 | if (mode == MODE_ALLOWED) { |
| 654 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 655 | } else { |
| 656 | missingPerm = componentPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 657 | strongestMode = Math.max(strongestMode, mode); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 658 | } |
Jeff Sharkey | e5d4933 | 2012-03-13 12:13:17 -0700 | [diff] [blame] | 659 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 660 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 661 | // track if unprotected read is allowed; any denied |
| 662 | // <path-permission> below removes this ability |
| 663 | boolean allowDefaultRead = (componentPerm == null); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 664 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 665 | final PathPermission[] pps = getPathPermissions(); |
| 666 | if (pps != null) { |
| 667 | final String path = uri.getPath(); |
| 668 | for (PathPermission pp : pps) { |
| 669 | final String pathPerm = pp.getReadPermission(); |
| 670 | if (pathPerm != null && pp.match(path)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 671 | final int mode = checkPermissionAndAppOp(pathPerm, callingPkg, callerToken); |
| 672 | if (mode == MODE_ALLOWED) { |
| 673 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 674 | } else { |
| 675 | // any denied <path-permission> means we lose |
| 676 | // default <provider> access. |
| 677 | allowDefaultRead = false; |
| 678 | missingPerm = pathPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 679 | strongestMode = Math.max(strongestMode, mode); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
| 683 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 684 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 685 | // if we passed <path-permission> checks above, and no default |
| 686 | // <provider> permission, then allow access. |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 687 | if (allowDefaultRead) return MODE_ALLOWED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | } |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 689 | |
| 690 | // last chance, check against any uri grants |
Amith Yamasani | 7d2d4fd | 2014-11-05 15:46:09 -0800 | [diff] [blame] | 691 | final int callingUserId = UserHandle.getUserId(uid); |
| 692 | final Uri userUri = (mSingleUser && !UserHandle.isSameUser(mMyUid, uid)) |
| 693 | ? maybeAddUserId(uri, callingUserId) : uri; |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 694 | if (context.checkUriPermission(userUri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 695 | callerToken) == PERMISSION_GRANTED) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 696 | return MODE_ALLOWED; |
| 697 | } |
| 698 | |
| 699 | // If the worst denial we found above was ignored, then pass that |
| 700 | // ignored through; otherwise we assume it should be a real error below. |
| 701 | if (strongestMode == MODE_IGNORED) { |
| 702 | return MODE_IGNORED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Jeff Sharkey | c0cc220 | 2017-03-21 19:25:34 -0600 | [diff] [blame] | 705 | final String suffix; |
| 706 | if (android.Manifest.permission.MANAGE_DOCUMENTS.equals(mReadPermission)) { |
| 707 | suffix = " requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs"; |
| 708 | } else if (mExported) { |
| 709 | suffix = " requires " + missingPerm + ", or grantUriPermission()"; |
| 710 | } else { |
| 711 | suffix = " requires the provider be exported, or grantUriPermission()"; |
| 712 | } |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 713 | throw new SecurityException("Permission Denial: reading " |
| 714 | + ContentProvider.this.getClass().getName() + " uri " + uri + " from pid=" + pid |
Jeff Sharkey | c0cc220 | 2017-03-21 19:25:34 -0600 | [diff] [blame] | 715 | + ", uid=" + uid + suffix); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** {@hide} */ |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 719 | protected int enforceWritePermissionInner(Uri uri, String callingPkg, IBinder callerToken) |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 720 | throws SecurityException { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 721 | final Context context = getContext(); |
| 722 | final int pid = Binder.getCallingPid(); |
| 723 | final int uid = Binder.getCallingUid(); |
| 724 | String missingPerm = null; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 725 | int strongestMode = MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 726 | |
| 727 | if (UserHandle.isSameApp(uid, mMyUid)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 728 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 731 | if (mExported && checkUser(pid, uid, context)) { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 732 | final String componentPerm = getWritePermission(); |
| 733 | if (componentPerm != null) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 734 | final int mode = checkPermissionAndAppOp(componentPerm, callingPkg, callerToken); |
| 735 | if (mode == MODE_ALLOWED) { |
| 736 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 737 | } else { |
| 738 | missingPerm = componentPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 739 | strongestMode = Math.max(strongestMode, mode); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| 743 | // track if unprotected write is allowed; any denied |
| 744 | // <path-permission> below removes this ability |
| 745 | boolean allowDefaultWrite = (componentPerm == null); |
| 746 | |
| 747 | final PathPermission[] pps = getPathPermissions(); |
| 748 | if (pps != null) { |
| 749 | final String path = uri.getPath(); |
| 750 | for (PathPermission pp : pps) { |
| 751 | final String pathPerm = pp.getWritePermission(); |
| 752 | if (pathPerm != null && pp.match(path)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 753 | final int mode = checkPermissionAndAppOp(pathPerm, callingPkg, callerToken); |
| 754 | if (mode == MODE_ALLOWED) { |
| 755 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 756 | } else { |
| 757 | // any denied <path-permission> means we lose |
| 758 | // default <provider> access. |
| 759 | allowDefaultWrite = false; |
| 760 | missingPerm = pathPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 761 | strongestMode = Math.max(strongestMode, mode); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // if we passed <path-permission> checks above, and no default |
| 768 | // <provider> permission, then allow access. |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 769 | if (allowDefaultWrite) return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // last chance, check against any uri grants |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 773 | if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_WRITE_URI_PERMISSION, |
| 774 | callerToken) == PERMISSION_GRANTED) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 775 | return MODE_ALLOWED; |
| 776 | } |
| 777 | |
| 778 | // If the worst denial we found above was ignored, then pass that |
| 779 | // ignored through; otherwise we assume it should be a real error below. |
| 780 | if (strongestMode == MODE_IGNORED) { |
| 781 | return MODE_IGNORED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | final String failReason = mExported |
| 785 | ? " requires " + missingPerm + ", or grantUriPermission()" |
| 786 | : " requires the provider be exported, or grantUriPermission()"; |
| 787 | throw new SecurityException("Permission Denial: writing " |
| 788 | + ContentProvider.this.getClass().getName() + " uri " + uri + " from pid=" + pid |
| 789 | + ", uid=" + uid + failReason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | } |
| 791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 793 | * Retrieves the Context this provider is running in. Only available once |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 794 | * {@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] | 795 | * constructor. |
| 796 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 797 | public final @Nullable Context getContext() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | return mContext; |
| 799 | } |
| 800 | |
| 801 | /** |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 802 | * Set the calling package, returning the current value (or {@code null}) |
| 803 | * which can be used later to restore the previous state. |
| 804 | */ |
| 805 | private String setCallingPackage(String callingPackage) { |
| 806 | final String original = mCallingPackage.get(); |
| 807 | mCallingPackage.set(callingPackage); |
| 808 | return original; |
| 809 | } |
| 810 | |
| 811 | /** |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 812 | * Return the package name of the caller that initiated the request being |
| 813 | * processed on the current thread. The returned package will have been |
| 814 | * verified to belong to the calling UID. Returns {@code null} if not |
| 815 | * currently processing a request. |
| 816 | * <p> |
| 817 | * This will always return {@code null} when processing |
| 818 | * {@link #getType(Uri)} or {@link #getStreamTypes(Uri, String)} requests. |
| 819 | * |
| 820 | * @see Binder#getCallingUid() |
| 821 | * @see Context#grantUriPermission(String, Uri, int) |
| 822 | * @throws SecurityException if the calling package doesn't belong to the |
| 823 | * calling UID. |
| 824 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 825 | public final @Nullable String getCallingPackage() { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 826 | final String pkg = mCallingPackage.get(); |
| 827 | if (pkg != null) { |
| 828 | mTransport.mAppOpsManager.checkPackage(Binder.getCallingUid(), pkg); |
| 829 | } |
| 830 | return pkg; |
| 831 | } |
| 832 | |
| 833 | /** |
Jeff Sharkey | d2b64d7 | 2018-10-19 15:40:03 -0600 | [diff] [blame] | 834 | * Opaque token representing the identity of an incoming IPC. |
| 835 | */ |
| 836 | public final class CallingIdentity { |
| 837 | /** {@hide} */ |
| 838 | public final long binderToken; |
| 839 | /** {@hide} */ |
| 840 | public final String callingPackage; |
| 841 | |
| 842 | /** {@hide} */ |
| 843 | public CallingIdentity(long binderToken, String callingPackage) { |
| 844 | this.binderToken = binderToken; |
| 845 | this.callingPackage = callingPackage; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Reset the identity of the incoming IPC on the current thread. |
| 851 | * <p> |
| 852 | * Internally this calls {@link Binder#clearCallingIdentity()} and also |
| 853 | * clears any value stored in {@link #getCallingPackage()}. |
| 854 | * |
| 855 | * @return Returns an opaque token that can be used to restore the original |
| 856 | * calling identity by passing it to |
| 857 | * {@link #restoreCallingIdentity}. |
| 858 | */ |
| 859 | public final @NonNull CallingIdentity clearCallingIdentity() { |
| 860 | return new CallingIdentity(Binder.clearCallingIdentity(), setCallingPackage(null)); |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Restore the identity of the incoming IPC on the current thread back to a |
| 865 | * previously identity that was returned by {@link #clearCallingIdentity}. |
| 866 | * <p> |
| 867 | * Internally this calls {@link Binder#restoreCallingIdentity(long)} and |
| 868 | * also restores any value stored in {@link #getCallingPackage()}. |
| 869 | */ |
| 870 | public final void restoreCallingIdentity(@NonNull CallingIdentity identity) { |
| 871 | Binder.restoreCallingIdentity(identity.binderToken); |
| 872 | mCallingPackage.set(identity.callingPackage); |
| 873 | } |
| 874 | |
| 875 | /** |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 876 | * Change the authorities of the ContentProvider. |
| 877 | * This is normally set for you from its manifest information when the provider is first |
| 878 | * created. |
| 879 | * @hide |
| 880 | * @param authorities the semi-colon separated authorities of the ContentProvider. |
| 881 | */ |
| 882 | protected final void setAuthorities(String authorities) { |
Nicolas Prevot | 6e412ad | 2014-09-08 18:26:55 +0100 | [diff] [blame] | 883 | if (authorities != null) { |
| 884 | if (authorities.indexOf(';') == -1) { |
| 885 | mAuthority = authorities; |
| 886 | mAuthorities = null; |
| 887 | } else { |
| 888 | mAuthority = null; |
| 889 | mAuthorities = authorities.split(";"); |
| 890 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
| 894 | /** @hide */ |
| 895 | protected final boolean matchesOurAuthorities(String authority) { |
| 896 | if (mAuthority != null) { |
| 897 | return mAuthority.equals(authority); |
| 898 | } |
Nicolas Prevot | 6e412ad | 2014-09-08 18:26:55 +0100 | [diff] [blame] | 899 | if (mAuthorities != null) { |
| 900 | int length = mAuthorities.length; |
| 901 | for (int i = 0; i < length; i++) { |
| 902 | if (mAuthorities[i].equals(authority)) return true; |
| 903 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 904 | } |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | |
| 909 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | * Change the permission required to read data from the content |
| 911 | * provider. This is normally set for you from its manifest information |
| 912 | * when the provider is first created. |
| 913 | * |
| 914 | * @param permission Name of the permission required for read-only access. |
| 915 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 916 | protected final void setReadPermission(@Nullable String permission) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | mReadPermission = permission; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Return the name of the permission required for read-only access to |
| 922 | * this content provider. This method can be called from multiple |
| 923 | * threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 924 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 925 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 927 | public final @Nullable String getReadPermission() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | return mReadPermission; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Change the permission required to read and write data in the content |
| 933 | * provider. This is normally set for you from its manifest information |
| 934 | * when the provider is first created. |
| 935 | * |
| 936 | * @param permission Name of the permission required for read/write access. |
| 937 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 938 | protected final void setWritePermission(@Nullable String permission) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | mWritePermission = permission; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * Return the name of the permission required for read/write access to |
| 944 | * this content provider. This method can be called from multiple |
| 945 | * threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 946 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 947 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 949 | public final @Nullable String getWritePermission() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | return mWritePermission; |
| 951 | } |
| 952 | |
| 953 | /** |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 954 | * Change the path-based permission required to read and/or write data in |
| 955 | * the content provider. This is normally set for you from its manifest |
| 956 | * information when the provider is first created. |
| 957 | * |
| 958 | * @param permissions Array of path permission descriptions. |
| 959 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 960 | protected final void setPathPermissions(@Nullable PathPermission[] permissions) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 961 | mPathPermissions = permissions; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Return the path-based permissions required for read and/or write access to |
| 966 | * this content provider. This method can be called from multiple |
| 967 | * threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 968 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 969 | * and Threads</a>. |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 970 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 971 | public final @Nullable PathPermission[] getPathPermissions() { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 972 | return mPathPermissions; |
| 973 | } |
| 974 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 975 | /** @hide */ |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 976 | @UnsupportedAppUsage |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 977 | public final void setAppOps(int readOp, int writeOp) { |
Dianne Hackborn | 7e6f976 | 2013-02-26 13:35:11 -0800 | [diff] [blame] | 978 | if (!mNoPerms) { |
Dianne Hackborn | 7e6f976 | 2013-02-26 13:35:11 -0800 | [diff] [blame] | 979 | mTransport.mReadOp = readOp; |
| 980 | mTransport.mWriteOp = writeOp; |
| 981 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 984 | /** @hide */ |
| 985 | public AppOpsManager getAppOpsManager() { |
| 986 | return mTransport.mAppOpsManager; |
| 987 | } |
| 988 | |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 989 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 990 | * Implement this to initialize your content provider on startup. |
| 991 | * This method is called for all registered content providers on the |
| 992 | * application main thread at application launch time. It must not perform |
| 993 | * lengthy operations, or application startup will be delayed. |
| 994 | * |
| 995 | * <p>You should defer nontrivial initialization (such as opening, |
| 996 | * upgrading, and scanning databases) until the content provider is used |
| 997 | * (via {@link #query}, {@link #insert}, etc). Deferred initialization |
| 998 | * keeps application startup fast, avoids unnecessary work if the provider |
| 999 | * turns out not to be needed, and stops database errors (such as a full |
| 1000 | * disk) from halting application launch. |
| 1001 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1002 | * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper} |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1003 | * is a helpful utility class that makes it easy to manage databases, |
| 1004 | * and will automatically defer opening until first use. If you do use |
| 1005 | * SQLiteOpenHelper, make sure to avoid calling |
| 1006 | * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or |
| 1007 | * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase} |
| 1008 | * from this method. (Instead, override |
| 1009 | * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the |
| 1010 | * database when it is first opened.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | * |
| 1012 | * @return true if the provider was successfully loaded, false otherwise |
| 1013 | */ |
| 1014 | public abstract boolean onCreate(); |
| 1015 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1016 | /** |
| 1017 | * {@inheritDoc} |
| 1018 | * This method is always called on the application main thread, and must |
| 1019 | * not perform lengthy operations. |
| 1020 | * |
| 1021 | * <p>The default content provider implementation does nothing. |
| 1022 | * Override this method to take appropriate action. |
| 1023 | * (Content providers do not usually care about things like screen |
| 1024 | * orientation, but may want to know about locale changes.) |
| 1025 | */ |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1026 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | public void onConfigurationChanged(Configuration newConfig) { |
| 1028 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1029 | |
| 1030 | /** |
| 1031 | * {@inheritDoc} |
| 1032 | * This method is always called on the application main thread, and must |
| 1033 | * not perform lengthy operations. |
| 1034 | * |
| 1035 | * <p>The default content provider implementation does nothing. |
| 1036 | * Subclasses may override this method to take appropriate action. |
| 1037 | */ |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1038 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | public void onLowMemory() { |
| 1040 | } |
| 1041 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1042 | @Override |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1043 | public void onTrimMemory(int level) { |
| 1044 | } |
| 1045 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1047 | * Implement this to handle query requests from clients. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1048 | * |
| 1049 | * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher should override |
| 1050 | * {@link #query(Uri, String[], Bundle, CancellationSignal)} and provide a stub |
| 1051 | * implementation of this method. |
| 1052 | * |
| 1053 | * <p>This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1054 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1055 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | * <p> |
| 1057 | * Example client call:<p> |
| 1058 | * <pre>// Request a specific record. |
| 1059 | * Cursor managedCursor = managedQuery( |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 1060 | ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | projection, // Which columns to return. |
| 1062 | null, // WHERE clause. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 1063 | null, // WHERE clause value substitution |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | People.NAME + " ASC"); // Sort order.</pre> |
| 1065 | * Example implementation:<p> |
| 1066 | * <pre>// SQLiteQueryBuilder is a helper class that creates the |
| 1067 | // proper SQL syntax for us. |
| 1068 | SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder(); |
| 1069 | |
| 1070 | // Set the table we're querying. |
| 1071 | qBuilder.setTables(DATABASE_TABLE_NAME); |
| 1072 | |
| 1073 | // If the query ends in a specific record number, we're |
| 1074 | // being asked for a specific record, so set the |
| 1075 | // WHERE clause in our query. |
| 1076 | if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){ |
| 1077 | qBuilder.appendWhere("_id=" + uri.getPathLeafId()); |
| 1078 | } |
| 1079 | |
| 1080 | // Make the query. |
| 1081 | Cursor c = qBuilder.query(mDb, |
| 1082 | projection, |
| 1083 | selection, |
| 1084 | selectionArgs, |
| 1085 | groupBy, |
| 1086 | having, |
| 1087 | sortOrder); |
| 1088 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 1089 | return c;</pre> |
| 1090 | * |
| 1091 | * @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] | 1092 | * if the client is requesting a specific record, the URI will end in a record number |
| 1093 | * that the implementation should parse and add to a WHERE or HAVING clause, specifying |
| 1094 | * that _id value. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | * @param projection The list of columns to put into the cursor. If |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1096 | * {@code null} all columns are included. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | * @param selection A selection criteria to apply when filtering rows. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1098 | * If {@code null} then all rows are included. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 1099 | * @param selectionArgs You may include ?s in selection, which will be replaced by |
| 1100 | * the values from selectionArgs, in order that they appear in the selection. |
| 1101 | * The values will be bound as Strings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | * @param sortOrder How the rows in the cursor should be sorted. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1103 | * If {@code null} then the provider is free to define the sort order. |
| 1104 | * @return a Cursor or {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1106 | public abstract @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection, |
| 1107 | @Nullable String selection, @Nullable String[] selectionArgs, |
| 1108 | @Nullable String sortOrder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | |
Fred Quintana | 5bba632 | 2009-10-05 14:21:12 -0700 | [diff] [blame] | 1110 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 1111 | * Implement this to handle query requests from clients with support for cancellation. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1112 | * |
| 1113 | * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher should override |
| 1114 | * {@link #query(Uri, String[], Bundle, CancellationSignal)} instead of this method. |
| 1115 | * |
| 1116 | * <p>This method can be called from multiple threads, as described in |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1117 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1118 | * and Threads</a>. |
| 1119 | * <p> |
| 1120 | * Example client call:<p> |
| 1121 | * <pre>// Request a specific record. |
| 1122 | * Cursor managedCursor = managedQuery( |
| 1123 | ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2), |
| 1124 | projection, // Which columns to return. |
| 1125 | null, // WHERE clause. |
| 1126 | null, // WHERE clause value substitution |
| 1127 | People.NAME + " ASC"); // Sort order.</pre> |
| 1128 | * Example implementation:<p> |
| 1129 | * <pre>// SQLiteQueryBuilder is a helper class that creates the |
| 1130 | // proper SQL syntax for us. |
| 1131 | SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder(); |
| 1132 | |
| 1133 | // Set the table we're querying. |
| 1134 | qBuilder.setTables(DATABASE_TABLE_NAME); |
| 1135 | |
| 1136 | // If the query ends in a specific record number, we're |
| 1137 | // being asked for a specific record, so set the |
| 1138 | // WHERE clause in our query. |
| 1139 | if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){ |
| 1140 | qBuilder.appendWhere("_id=" + uri.getPathLeafId()); |
| 1141 | } |
| 1142 | |
| 1143 | // Make the query. |
| 1144 | Cursor c = qBuilder.query(mDb, |
| 1145 | projection, |
| 1146 | selection, |
| 1147 | selectionArgs, |
| 1148 | groupBy, |
| 1149 | having, |
| 1150 | sortOrder); |
| 1151 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 1152 | return c;</pre> |
| 1153 | * <p> |
| 1154 | * 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] | 1155 | * {@link #query(Uri, String[], String, String[], String)} that does not take a cancellation |
| 1156 | * signal to ensure correct operation on older versions of the Android Framework in |
| 1157 | * which the cancellation signal overload was not available. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1158 | * |
| 1159 | * @param uri The URI to query. This will be the full URI sent by the client; |
| 1160 | * if the client is requesting a specific record, the URI will end in a record number |
| 1161 | * that the implementation should parse and add to a WHERE or HAVING clause, specifying |
| 1162 | * that _id value. |
| 1163 | * @param projection The list of columns to put into the cursor. If |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1164 | * {@code null} all columns are included. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1165 | * @param selection A selection criteria to apply when filtering rows. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1166 | * If {@code null} then all rows are included. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1167 | * @param selectionArgs You may include ?s in selection, which will be replaced by |
| 1168 | * the values from selectionArgs, in order that they appear in the selection. |
| 1169 | * The values will be bound as Strings. |
| 1170 | * @param sortOrder How the rows in the cursor should be sorted. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1171 | * If {@code null} then the provider is free to define the sort order. |
| 1172 | * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if none. |
Jeff Sharkey | 67f9d50 | 2017-08-05 13:49:13 -0600 | [diff] [blame] | 1173 | * If the operation is canceled, then {@link android.os.OperationCanceledException} will be thrown |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1174 | * when the query is executed. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1175 | * @return a Cursor or {@code null}. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1176 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1177 | public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection, |
| 1178 | @Nullable String selection, @Nullable String[] selectionArgs, |
| 1179 | @Nullable String sortOrder, @Nullable CancellationSignal cancellationSignal) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1180 | return query(uri, projection, selection, selectionArgs, sortOrder); |
| 1181 | } |
| 1182 | |
| 1183 | /** |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1184 | * Implement this to handle query requests where the arguments are packed into a {@link Bundle}. |
| 1185 | * Arguments may include traditional SQL style query arguments. When present these |
| 1186 | * should be handled according to the contract established in |
Andrew Solovay | 27e4346 | 2018-12-12 15:38:06 -0800 | [diff] [blame] | 1187 | * {@link #query(Uri, String[], String, String[], String, CancellationSignal)}. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1188 | * |
| 1189 | * <p>Traditional SQL arguments can be found in the bundle using the following keys: |
Andrew Solovay | 27e4346 | 2018-12-12 15:38:06 -0800 | [diff] [blame] | 1190 | * <li>{@link android.content.ContentResolver#QUERY_ARG_SQL_SELECTION} |
| 1191 | * <li>{@link android.content.ContentResolver#QUERY_ARG_SQL_SELECTION_ARGS} |
| 1192 | * <li>{@link android.content.ContentResolver#QUERY_ARG_SQL_SORT_ORDER} |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1193 | * |
Steve McKay | 76b2770 | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 1194 | * <p>This method can be called from multiple threads, as described in |
| 1195 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1196 | * and Threads</a>. |
| 1197 | * |
| 1198 | * <p> |
| 1199 | * Example client call:<p> |
| 1200 | * <pre>// Request 20 records starting at row index 30. |
| 1201 | Bundle queryArgs = new Bundle(); |
| 1202 | queryArgs.putInt(ContentResolver.QUERY_ARG_OFFSET, 30); |
| 1203 | queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20); |
| 1204 | |
| 1205 | Cursor cursor = getContentResolver().query( |
| 1206 | contentUri, // Content Uri is specific to individual content providers. |
| 1207 | projection, // String[] describing which columns to return. |
| 1208 | queryArgs, // Query arguments. |
| 1209 | null); // Cancellation signal.</pre> |
| 1210 | * |
| 1211 | * Example implementation:<p> |
| 1212 | * <pre> |
| 1213 | |
| 1214 | int recordsetSize = 0x1000; // Actual value is implementation specific. |
| 1215 | queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY; // ensure queryArgs is non-null |
| 1216 | |
| 1217 | int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0); |
| 1218 | int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MIN_VALUE); |
| 1219 | |
| 1220 | MatrixCursor c = new MatrixCursor(PROJECTION, limit); |
| 1221 | |
| 1222 | // Calculate the number of items to include in the cursor. |
| 1223 | int numItems = MathUtils.constrain(recordsetSize - offset, 0, limit); |
| 1224 | |
| 1225 | // Build the paged result set.... |
| 1226 | for (int i = offset; i < offset + numItems; i++) { |
| 1227 | // populate row from your data. |
| 1228 | } |
| 1229 | |
| 1230 | Bundle extras = new Bundle(); |
| 1231 | c.setExtras(extras); |
| 1232 | |
| 1233 | // Any QUERY_ARG_* key may be included if honored. |
| 1234 | // In an actual implementation, include only keys that are both present in queryArgs |
| 1235 | // and reflected in the Cursor output. For example, if QUERY_ARG_OFFSET were included |
| 1236 | // in queryArgs, but was ignored because it contained an invalid value (like –273), |
| 1237 | // then QUERY_ARG_OFFSET should be omitted. |
| 1238 | extras.putStringArray(ContentResolver.EXTRA_HONORED_ARGS, new String[] { |
| 1239 | ContentResolver.QUERY_ARG_OFFSET, |
| 1240 | ContentResolver.QUERY_ARG_LIMIT |
| 1241 | }); |
| 1242 | |
| 1243 | extras.putInt(ContentResolver.EXTRA_TOTAL_COUNT, recordsetSize); |
| 1244 | |
| 1245 | cursor.setNotificationUri(getContext().getContentResolver(), uri); |
| 1246 | |
| 1247 | return cursor;</pre> |
| 1248 | * <p> |
Andrew Solovay | 27e4346 | 2018-12-12 15:38:06 -0800 | [diff] [blame] | 1249 | * See {@link #query(Uri, String[], String, String[], String, CancellationSignal)} |
| 1250 | * for implementation details. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1251 | * |
| 1252 | * @param uri The URI to query. This will be the full URI sent by the client. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1253 | * @param projection The list of columns to put into the cursor. |
| 1254 | * If {@code null} provide a default set of columns. |
| 1255 | * @param queryArgs A Bundle containing all additional information necessary for the query. |
| 1256 | * Values in the Bundle may include SQL style arguments. |
| 1257 | * @param cancellationSignal A signal to cancel the operation in progress, |
| 1258 | * or {@code null}. |
| 1259 | * @return a Cursor or {@code null}. |
| 1260 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1261 | @Override |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1262 | public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection, |
| 1263 | @Nullable Bundle queryArgs, @Nullable CancellationSignal cancellationSignal) { |
| 1264 | queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY; |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1265 | |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 1266 | // if client doesn't supply an SQL sort order argument, attempt to build one from |
| 1267 | // QUERY_ARG_SORT* arguments. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1268 | String sortClause = queryArgs.getString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER); |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1269 | if (sortClause == null && queryArgs.containsKey(ContentResolver.QUERY_ARG_SORT_COLUMNS)) { |
| 1270 | sortClause = ContentResolver.createSqlSortClause(queryArgs); |
| 1271 | } |
| 1272 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1273 | return query( |
| 1274 | uri, |
| 1275 | projection, |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1276 | queryArgs.getString(ContentResolver.QUERY_ARG_SQL_SELECTION), |
| 1277 | queryArgs.getStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS), |
| 1278 | sortClause, |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1279 | cancellationSignal); |
| 1280 | } |
| 1281 | |
| 1282 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1283 | * Implement this to handle requests for the MIME type of the data at the |
| 1284 | * given URI. The returned MIME type should start with |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | * <code>vnd.android.cursor.item</code> for a single record, |
| 1286 | * or <code>vnd.android.cursor.dir/</code> for multiple items. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1287 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1288 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1289 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | * |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1291 | * <p>Note that there are no permissions needed for an application to |
| 1292 | * access this information; if your content provider requires read and/or |
| 1293 | * write permissions, or is not exported, all applications can still call |
| 1294 | * this method regardless of their access permissions. This allows them |
| 1295 | * to retrieve the MIME type for a URI when dispatching intents. |
| 1296 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | * @param uri the URI to query. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1298 | * @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] | 1299 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1300 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1301 | public abstract @Nullable String getType(@NonNull Uri uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | |
| 1303 | /** |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1304 | * Implement this to support canonicalization of URIs that refer to your |
| 1305 | * content provider. A canonical URI is one that can be transported across |
| 1306 | * devices, backup/restore, and other contexts, and still be able to refer |
| 1307 | * to the same data item. Typically this is implemented by adding query |
| 1308 | * params to the URI allowing the content provider to verify that an incoming |
| 1309 | * canonical URI references the same data as it was originally intended for and, |
| 1310 | * if it doesn't, to find that data (if it exists) in the current environment. |
| 1311 | * |
| 1312 | * <p>For example, if the content provider holds people and a normal URI in it |
| 1313 | * is created with a row index into that people database, the cananical representation |
| 1314 | * may have an additional query param at the end which specifies the name of the |
| 1315 | * person it is intended for. Later calls into the provider with that URI will look |
| 1316 | * up the row of that URI's base index and, if it doesn't match or its entry's |
| 1317 | * name doesn't match the name in the query param, perform a query on its database |
| 1318 | * to find the correct row to operate on.</p> |
| 1319 | * |
| 1320 | * <p>If you implement support for canonical URIs, <b>all</b> incoming calls with |
| 1321 | * URIs (including this one) must perform this verification and recovery of any |
| 1322 | * canonical URIs they receive. In addition, you must also implement |
| 1323 | * {@link #uncanonicalize} to strip the canonicalization of any of these URIs.</p> |
| 1324 | * |
| 1325 | * <p>The default implementation of this method returns null, indicating that |
| 1326 | * canonical URIs are not supported.</p> |
| 1327 | * |
| 1328 | * @param url The Uri to canonicalize. |
| 1329 | * |
| 1330 | * @return Return the canonical representation of <var>url</var>, or null if |
| 1331 | * canonicalization of that Uri is not supported. |
| 1332 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1333 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1334 | public @Nullable Uri canonicalize(@NonNull Uri url) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1335 | return null; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Remove canonicalization from canonical URIs previously returned by |
| 1340 | * {@link #canonicalize}. For example, if your implementation is to add |
| 1341 | * a query param to canonicalize a URI, this method can simply trip any |
| 1342 | * query params on the URI. The default implementation always returns the |
| 1343 | * same <var>url</var> that was passed in. |
| 1344 | * |
| 1345 | * @param url The Uri to remove any canonicalization from. |
| 1346 | * |
Dianne Hackborn | b3ac67a | 2013-09-11 11:02:24 -0700 | [diff] [blame] | 1347 | * @return Return the non-canonical representation of <var>url</var>, return |
| 1348 | * the <var>url</var> as-is if there is nothing to do, or return null if |
| 1349 | * the data identified by the canonical representation can not be found in |
| 1350 | * the current environment. |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1351 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1352 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1353 | public @Nullable Uri uncanonicalize(@NonNull Uri url) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1354 | return url; |
| 1355 | } |
| 1356 | |
| 1357 | /** |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 1358 | * Implement this to support refresh of content identified by {@code uri}. By default, this |
| 1359 | * method returns false; providers who wish to implement this should return true to signal the |
| 1360 | * client that the provider has tried refreshing with its own implementation. |
| 1361 | * <p> |
| 1362 | * This allows clients to request an explicit refresh of content identified by {@code uri}. |
| 1363 | * <p> |
| 1364 | * Client code should only invoke this method when there is a strong indication (such as a user |
| 1365 | * initiated pull to refresh gesture) that the content is stale. |
| 1366 | * <p> |
| 1367 | * Remember to send {@link ContentResolver#notifyChange(Uri, android.database.ContentObserver)} |
| 1368 | * notifications when content changes. |
| 1369 | * |
| 1370 | * @param uri The Uri identifying the data to refresh. |
| 1371 | * @param args Additional options from the client. The definitions of these are specific to the |
| 1372 | * content provider being called. |
| 1373 | * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if |
| 1374 | * none. For example, if you called refresh on a particular uri, you should call |
| 1375 | * {@link CancellationSignal#throwIfCanceled()} to check whether the client has |
| 1376 | * canceled the refresh request. |
| 1377 | * @return true if the provider actually tried refreshing. |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 1378 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1379 | @Override |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 1380 | public boolean refresh(Uri uri, @Nullable Bundle args, |
| 1381 | @Nullable CancellationSignal cancellationSignal) { |
| 1382 | return false; |
| 1383 | } |
| 1384 | |
| 1385 | /** |
Dianne Hackborn | d7960d1 | 2013-01-29 18:55:48 -0800 | [diff] [blame] | 1386 | * @hide |
| 1387 | * Implementation when a caller has performed an insert on the content |
| 1388 | * provider, but that call has been rejected for the operation given |
| 1389 | * to {@link #setAppOps(int, int)}. The default implementation simply |
| 1390 | * returns a dummy URI that is the base URI with a 0 path element |
| 1391 | * appended. |
| 1392 | */ |
| 1393 | public Uri rejectInsert(Uri uri, ContentValues values) { |
| 1394 | // If not allowed, we need to return some reasonable URI. Maybe the |
| 1395 | // content provider should be responsible for this, but for now we |
| 1396 | // will just return the base URI with a dummy '0' tagged on to it. |
| 1397 | // You shouldn't be able to read if you can't write, anyway, so it |
| 1398 | // shouldn't matter much what is returned. |
| 1399 | return uri.buildUpon().appendPath("0").build(); |
| 1400 | } |
| 1401 | |
| 1402 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1403 | * 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] | 1404 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 1405 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1406 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1407 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1408 | * and Threads</a>. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1409 | * @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] | 1410 | * @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] | 1411 | * This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | * @return The URI for the newly inserted item. |
| 1413 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1414 | @Override |
Jeff Sharkey | 34796bd | 2015-06-11 21:55:32 -0700 | [diff] [blame] | 1415 | public abstract @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | |
| 1417 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1418 | * Override this to handle requests to insert a set of new rows, or the |
| 1419 | * default implementation will iterate over the values and call |
| 1420 | * {@link #insert} on each of them. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 1422 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1423 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1424 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1425 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | * |
| 1427 | * @param uri The content:// URI of the insertion request. |
| 1428 | * @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] | 1429 | * This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | * @return The number of values that were inserted. |
| 1431 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1432 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1433 | public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | int numValues = values.length; |
| 1435 | for (int i = 0; i < numValues; i++) { |
| 1436 | insert(uri, values[i]); |
| 1437 | } |
| 1438 | return numValues; |
| 1439 | } |
| 1440 | |
| 1441 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1442 | * Implement this to handle requests to delete one or more rows. |
| 1443 | * The implementation should apply the selection clause when performing |
| 1444 | * deletion, allowing the operation to affect multiple rows in a directory. |
Taeho Kim | bd88de4 | 2013-10-28 15:08:53 +0900 | [diff] [blame] | 1445 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | * after deleting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1447 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1448 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1449 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | * |
| 1451 | * <p>The implementation is responsible for parsing out a row ID at the end |
| 1452 | * of the URI, if a specific row is being deleted. That is, the client would |
| 1453 | * pass in <code>content://contacts/people/22</code> and the implementation is |
| 1454 | * responsible for parsing the record number (22) when creating a SQL statement. |
| 1455 | * |
| 1456 | * @param uri The full URI to query, including a row ID (if a specific record is requested). |
| 1457 | * @param selection An optional restriction to apply to rows when deleting. |
| 1458 | * @return The number of rows affected. |
| 1459 | * @throws SQLException |
| 1460 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1461 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1462 | public abstract int delete(@NonNull Uri uri, @Nullable String selection, |
| 1463 | @Nullable String[] selectionArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | |
| 1465 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1466 | * Implement this to handle requests to update one or more rows. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1467 | * The implementation should update all rows matching the selection |
| 1468 | * 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] | 1469 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 1470 | * after updating. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1471 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1472 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1473 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | * |
| 1475 | * @param uri The URI to query. This can potentially have a record ID if this |
| 1476 | * is an update request for a specific record. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1477 | * @param values A set of column_name/value pairs to update in the database. |
| 1478 | * This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | * @param selection An optional filter to match rows to update. |
| 1480 | * @return the number of rows affected. |
| 1481 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1482 | @Override |
Jeff Sharkey | 34796bd | 2015-06-11 21:55:32 -0700 | [diff] [blame] | 1483 | public abstract int update(@NonNull Uri uri, @Nullable ContentValues values, |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1484 | @Nullable String selection, @Nullable String[] selectionArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | |
| 1486 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1487 | * Override this to handle requests to open a file blob. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1488 | * The default implementation always throws {@link FileNotFoundException}. |
| 1489 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1490 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1491 | * and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1492 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1493 | * <p>This method returns a ParcelFileDescriptor, which is returned directly |
| 1494 | * 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] | 1495 | * returned without copying the content. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | * |
| 1497 | * <p>The returned ParcelFileDescriptor is owned by the caller, so it is |
| 1498 | * their responsibility to close it when done. That is, the implementation |
| 1499 | * of this method should create a new ParcelFileDescriptor for each call. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1500 | * <p> |
| 1501 | * If opened with the exclusive "r" or "w" modes, the returned |
| 1502 | * ParcelFileDescriptor can be a pipe or socket pair to enable streaming |
| 1503 | * of data. Opening with the "rw" or "rwt" modes implies a file on disk that |
| 1504 | * supports seeking. |
| 1505 | * <p> |
| 1506 | * If you need to detect when the returned ParcelFileDescriptor has been |
| 1507 | * closed, or if the remote process has crashed or encountered some other |
| 1508 | * error, you can use {@link ParcelFileDescriptor#open(File, int, |
| 1509 | * android.os.Handler, android.os.ParcelFileDescriptor.OnCloseListener)}, |
| 1510 | * {@link ParcelFileDescriptor#createReliablePipe()}, or |
| 1511 | * {@link ParcelFileDescriptor#createReliableSocketPair()}. |
Jeff Sharkey | b31afd2 | 2017-06-12 14:17:10 -0600 | [diff] [blame] | 1512 | * <p> |
| 1513 | * If you need to return a large file that isn't backed by a real file on |
| 1514 | * disk, such as a file on a network share or cloud storage service, |
| 1515 | * consider using |
| 1516 | * {@link StorageManager#openProxyFileDescriptor(int, android.os.ProxyFileDescriptorCallback, android.os.Handler)} |
| 1517 | * which will let you to stream the content on-demand. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1519 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1520 | * to return the appropriate MIME type for the data returned here with |
| 1521 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1522 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1523 | * |
| 1524 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1525 | * that for any URIs that can be opened, you also support queries on them |
| 1526 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1527 | * You may also want to support other common columns if you have additional meta-data |
| 1528 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1529 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1530 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | * @param uri The URI whose file is to be opened. |
| 1532 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1533 | * "rw" for read and write access, or "rwt" for read and write access |
| 1534 | * that truncates any existing file. |
| 1535 | * |
| 1536 | * @return Returns a new ParcelFileDescriptor which you can use to access |
| 1537 | * the file. |
| 1538 | * |
| 1539 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1540 | * no file associated with the given URI or the mode is invalid. |
| 1541 | * @throws SecurityException Throws SecurityException if the caller does |
| 1542 | * not have permission to access the file. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1543 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | * @see #openAssetFile(Uri, String) |
| 1545 | * @see #openFileHelper(Uri, String) |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1546 | * @see #getType(android.net.Uri) |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1547 | * @see ParcelFileDescriptor#parseMode(String) |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1548 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1549 | public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1550 | throws FileNotFoundException { |
| 1551 | throw new FileNotFoundException("No files supported by provider at " |
| 1552 | + uri); |
| 1553 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | /** |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1556 | * Override this to handle requests to open a file blob. |
| 1557 | * The default implementation always throws {@link FileNotFoundException}. |
| 1558 | * This method can be called from multiple threads, as described in |
| 1559 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1560 | * and Threads</a>. |
| 1561 | * |
| 1562 | * <p>This method returns a ParcelFileDescriptor, which is returned directly |
| 1563 | * to the caller. This way large data (such as images and documents) can be |
| 1564 | * returned without copying the content. |
| 1565 | * |
| 1566 | * <p>The returned ParcelFileDescriptor is owned by the caller, so it is |
| 1567 | * their responsibility to close it when done. That is, the implementation |
| 1568 | * of this method should create a new ParcelFileDescriptor for each call. |
| 1569 | * <p> |
| 1570 | * If opened with the exclusive "r" or "w" modes, the returned |
| 1571 | * ParcelFileDescriptor can be a pipe or socket pair to enable streaming |
| 1572 | * of data. Opening with the "rw" or "rwt" modes implies a file on disk that |
| 1573 | * supports seeking. |
| 1574 | * <p> |
| 1575 | * If you need to detect when the returned ParcelFileDescriptor has been |
| 1576 | * closed, or if the remote process has crashed or encountered some other |
| 1577 | * error, you can use {@link ParcelFileDescriptor#open(File, int, |
| 1578 | * android.os.Handler, android.os.ParcelFileDescriptor.OnCloseListener)}, |
| 1579 | * {@link ParcelFileDescriptor#createReliablePipe()}, or |
| 1580 | * {@link ParcelFileDescriptor#createReliableSocketPair()}. |
| 1581 | * |
| 1582 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1583 | * to return the appropriate MIME type for the data returned here with |
| 1584 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1585 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1586 | * |
| 1587 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1588 | * that for any URIs that can be opened, you also support queries on them |
| 1589 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1590 | * You may also want to support other common columns if you have additional meta-data |
| 1591 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1592 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1593 | * |
| 1594 | * @param uri The URI whose file is to be opened. |
| 1595 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1596 | * "w" for write-only access, "rw" for read and write access, or |
| 1597 | * "rwt" for read and write access that truncates any existing |
| 1598 | * file. |
| 1599 | * @param signal A signal to cancel the operation in progress, or |
| 1600 | * {@code null} if none. For example, if you are downloading a |
| 1601 | * file from the network to service a "rw" mode request, you |
| 1602 | * should periodically call |
| 1603 | * {@link CancellationSignal#throwIfCanceled()} to check whether |
| 1604 | * the client has canceled the request and abort the download. |
| 1605 | * |
| 1606 | * @return Returns a new ParcelFileDescriptor which you can use to access |
| 1607 | * the file. |
| 1608 | * |
| 1609 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1610 | * no file associated with the given URI or the mode is invalid. |
| 1611 | * @throws SecurityException Throws SecurityException if the caller does |
| 1612 | * not have permission to access the file. |
| 1613 | * |
| 1614 | * @see #openAssetFile(Uri, String) |
| 1615 | * @see #openFileHelper(Uri, String) |
| 1616 | * @see #getType(android.net.Uri) |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1617 | * @see ParcelFileDescriptor#parseMode(String) |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1618 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1619 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1620 | public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode, |
| 1621 | @Nullable CancellationSignal signal) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1622 | return openFile(uri, mode); |
| 1623 | } |
| 1624 | |
| 1625 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | * This is like {@link #openFile}, but can be implemented by providers |
| 1627 | * 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] | 1628 | * inside of their .apk. |
| 1629 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1630 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1631 | * and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1632 | * |
| 1633 | * <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] | 1634 | * file slices, either directly with |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1635 | * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 1637 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 1638 | * methods. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1639 | * <p> |
| 1640 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1641 | * streaming of data. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1642 | * |
| 1643 | * <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] | 1644 | * should create the AssetFileDescriptor with |
| 1645 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1646 | * applications that cannot handle sub-sections of files.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1648 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1649 | * to return the appropriate MIME type for the data returned here with |
| 1650 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1651 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1652 | * |
| 1653 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1654 | * that for any URIs that can be opened, you also support queries on them |
| 1655 | * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p> |
| 1656 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | * @param uri The URI whose file is to be opened. |
| 1658 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1659 | * "w" for write-only access (erasing whatever data is currently in |
| 1660 | * the file), "wa" for write-only access to append to any existing data, |
| 1661 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 1662 | * and write access that truncates any existing file. |
| 1663 | * |
| 1664 | * @return Returns a new AssetFileDescriptor which you can use to access |
| 1665 | * the file. |
| 1666 | * |
| 1667 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1668 | * no file associated with the given URI or the mode is invalid. |
| 1669 | * @throws SecurityException Throws SecurityException if the caller does |
| 1670 | * not have permission to access the file. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1671 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | * @see #openFile(Uri, String) |
| 1673 | * @see #openFileHelper(Uri, String) |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1674 | * @see #getType(android.net.Uri) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1675 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1676 | public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | throws FileNotFoundException { |
| 1678 | ParcelFileDescriptor fd = openFile(uri, mode); |
| 1679 | return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null; |
| 1680 | } |
| 1681 | |
| 1682 | /** |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1683 | * This is like {@link #openFile}, but can be implemented by providers |
| 1684 | * that need to be able to return sub-sections of files, often assets |
| 1685 | * inside of their .apk. |
| 1686 | * This method can be called from multiple threads, as described in |
| 1687 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1688 | * and Threads</a>. |
| 1689 | * |
| 1690 | * <p>If you implement this, your clients must be able to deal with such |
| 1691 | * file slices, either directly with |
| 1692 | * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level |
| 1693 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 1694 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 1695 | * methods. |
| 1696 | * <p> |
| 1697 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1698 | * streaming of data. |
| 1699 | * |
| 1700 | * <p class="note">If you are implementing this to return a full file, you |
| 1701 | * should create the AssetFileDescriptor with |
| 1702 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
| 1703 | * applications that cannot handle sub-sections of files.</p> |
| 1704 | * |
| 1705 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1706 | * to return the appropriate MIME type for the data returned here with |
| 1707 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1708 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1709 | * |
| 1710 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1711 | * that for any URIs that can be opened, you also support queries on them |
| 1712 | * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p> |
| 1713 | * |
| 1714 | * @param uri The URI whose file is to be opened. |
| 1715 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1716 | * "w" for write-only access (erasing whatever data is currently in |
| 1717 | * the file), "wa" for write-only access to append to any existing data, |
| 1718 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 1719 | * and write access that truncates any existing file. |
| 1720 | * @param signal A signal to cancel the operation in progress, or |
| 1721 | * {@code null} if none. For example, if you are downloading a |
| 1722 | * file from the network to service a "rw" mode request, you |
| 1723 | * should periodically call |
| 1724 | * {@link CancellationSignal#throwIfCanceled()} to check whether |
| 1725 | * the client has canceled the request and abort the download. |
| 1726 | * |
| 1727 | * @return Returns a new AssetFileDescriptor which you can use to access |
| 1728 | * the file. |
| 1729 | * |
| 1730 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1731 | * no file associated with the given URI or the mode is invalid. |
| 1732 | * @throws SecurityException Throws SecurityException if the caller does |
| 1733 | * not have permission to access the file. |
| 1734 | * |
| 1735 | * @see #openFile(Uri, String) |
| 1736 | * @see #openFileHelper(Uri, String) |
| 1737 | * @see #getType(android.net.Uri) |
| 1738 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1739 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1740 | public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode, |
| 1741 | @Nullable CancellationSignal signal) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1742 | return openAssetFile(uri, mode); |
| 1743 | } |
| 1744 | |
| 1745 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | * Convenience for subclasses that wish to implement {@link #openFile} |
| 1747 | * by looking up a column named "_data" at the given URI. |
| 1748 | * |
| 1749 | * @param uri The URI to be opened. |
| 1750 | * @param mode The file mode. May be "r" for read-only access, |
| 1751 | * "w" for write-only access (erasing whatever data is currently in |
| 1752 | * the file), "wa" for write-only access to append to any existing data, |
| 1753 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 1754 | * and write access that truncates any existing file. |
| 1755 | * |
| 1756 | * @return Returns a new ParcelFileDescriptor that can be used by the |
| 1757 | * client to access the file. |
| 1758 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1759 | protected final @NonNull ParcelFileDescriptor openFileHelper(@NonNull Uri uri, |
| 1760 | @NonNull String mode) throws FileNotFoundException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1761 | Cursor c = query(uri, new String[]{"_data"}, null, null, null); |
| 1762 | int count = (c != null) ? c.getCount() : 0; |
| 1763 | if (count != 1) { |
| 1764 | // If there is not exactly one result, throw an appropriate |
| 1765 | // exception. |
| 1766 | if (c != null) { |
| 1767 | c.close(); |
| 1768 | } |
| 1769 | if (count == 0) { |
| 1770 | throw new FileNotFoundException("No entry for " + uri); |
| 1771 | } |
| 1772 | throw new FileNotFoundException("Multiple items at " + uri); |
| 1773 | } |
| 1774 | |
| 1775 | c.moveToFirst(); |
| 1776 | int i = c.getColumnIndex("_data"); |
| 1777 | String path = (i >= 0 ? c.getString(i) : null); |
| 1778 | c.close(); |
| 1779 | if (path == null) { |
| 1780 | throw new FileNotFoundException("Column _data not found."); |
| 1781 | } |
| 1782 | |
Adam Lesinski | eb8c3f9 | 2013-09-20 14:08:25 -0700 | [diff] [blame] | 1783 | int modeBits = ParcelFileDescriptor.parseMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | return ParcelFileDescriptor.open(new File(path), modeBits); |
| 1785 | } |
| 1786 | |
| 1787 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1788 | * Called by a client to determine the types of data streams that this |
| 1789 | * content provider supports for the given URI. The default implementation |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1790 | * returns {@code null}, meaning no types. If your content provider stores data |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1791 | * of a particular type, return that MIME type if it matches the given |
| 1792 | * mimeTypeFilter. If it can perform type conversions, return an array |
| 1793 | * of all supported MIME types that match mimeTypeFilter. |
| 1794 | * |
| 1795 | * @param uri The data in the content provider being queried. |
| 1796 | * @param mimeTypeFilter The type of data the client desires. May be |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1797 | * a pattern, such as */* to retrieve all possible data types. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1798 | * @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] | 1799 | * given mimeTypeFilter. Otherwise returns an array of all available |
| 1800 | * concrete MIME types. |
| 1801 | * |
| 1802 | * @see #getType(Uri) |
| 1803 | * @see #openTypedAssetFile(Uri, String, Bundle) |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1804 | * @see ClipDescription#compareMimeTypes(String, String) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1805 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1806 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1807 | public @Nullable String[] getStreamTypes(@NonNull Uri uri, @NonNull String mimeTypeFilter) { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1808 | return null; |
| 1809 | } |
| 1810 | |
| 1811 | /** |
| 1812 | * Called by a client to open a read-only stream containing data of a |
| 1813 | * particular MIME type. This is like {@link #openAssetFile(Uri, String)}, |
| 1814 | * except the file can only be read-only and the content provider may |
| 1815 | * perform data conversions to generate data of the desired type. |
| 1816 | * |
| 1817 | * <p>The default implementation compares the given mimeType against the |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1818 | * result of {@link #getType(Uri)} and, if they match, simply calls |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1819 | * {@link #openAssetFile(Uri, String)}. |
| 1820 | * |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1821 | * <p>See {@link ClipData} for examples of the use and implementation |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1822 | * of this method. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1823 | * <p> |
| 1824 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1825 | * streaming of data. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1826 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1827 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1828 | * that for any URIs that can be opened, you also support queries on them |
| 1829 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1830 | * You may also want to support other common columns if you have additional meta-data |
| 1831 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1832 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1833 | * |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1834 | * @param uri The data in the content provider being queried. |
| 1835 | * @param mimeTypeFilter The type of data the client desires. May be |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1836 | * a pattern, such as */*, if the caller does not have specific type |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1837 | * requirements; in this case the content provider will pick its best |
| 1838 | * type matching the pattern. |
| 1839 | * @param opts Additional options from the client. The definitions of |
| 1840 | * these are specific to the content provider being called. |
| 1841 | * |
| 1842 | * @return Returns a new AssetFileDescriptor from which the client can |
| 1843 | * read data of the desired type. |
| 1844 | * |
| 1845 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1846 | * no file associated with the given URI or the mode is invalid. |
| 1847 | * @throws SecurityException Throws SecurityException if the caller does |
| 1848 | * not have permission to access the data. |
| 1849 | * @throws IllegalArgumentException Throws IllegalArgumentException if the |
| 1850 | * content provider does not support the requested MIME type. |
| 1851 | * |
| 1852 | * @see #getStreamTypes(Uri, String) |
| 1853 | * @see #openAssetFile(Uri, String) |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1854 | * @see ClipDescription#compareMimeTypes(String, String) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1855 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1856 | public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri, |
| 1857 | @NonNull String mimeTypeFilter, @Nullable Bundle opts) throws FileNotFoundException { |
Dianne Hackborn | 02dfd26 | 2010-08-13 12:34:58 -0700 | [diff] [blame] | 1858 | if ("*/*".equals(mimeTypeFilter)) { |
| 1859 | // If they can take anything, the untyped open call is good enough. |
| 1860 | return openAssetFile(uri, "r"); |
| 1861 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1862 | String baseType = getType(uri); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1863 | if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) { |
Dianne Hackborn | 02dfd26 | 2010-08-13 12:34:58 -0700 | [diff] [blame] | 1864 | // Use old untyped open call if this provider has a type for this |
| 1865 | // URI and it matches the request. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1866 | return openAssetFile(uri, "r"); |
| 1867 | } |
| 1868 | throw new FileNotFoundException("Can't open " + uri + " as type " + mimeTypeFilter); |
| 1869 | } |
| 1870 | |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1871 | |
| 1872 | /** |
| 1873 | * Called by a client to open a read-only stream containing data of a |
| 1874 | * particular MIME type. This is like {@link #openAssetFile(Uri, String)}, |
| 1875 | * except the file can only be read-only and the content provider may |
| 1876 | * perform data conversions to generate data of the desired type. |
| 1877 | * |
| 1878 | * <p>The default implementation compares the given mimeType against the |
| 1879 | * result of {@link #getType(Uri)} and, if they match, simply calls |
| 1880 | * {@link #openAssetFile(Uri, String)}. |
| 1881 | * |
| 1882 | * <p>See {@link ClipData} for examples of the use and implementation |
| 1883 | * of this method. |
| 1884 | * <p> |
| 1885 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1886 | * streaming of data. |
| 1887 | * |
| 1888 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1889 | * that for any URIs that can be opened, you also support queries on them |
| 1890 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1891 | * You may also want to support other common columns if you have additional meta-data |
| 1892 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1893 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1894 | * |
| 1895 | * @param uri The data in the content provider being queried. |
| 1896 | * @param mimeTypeFilter The type of data the client desires. May be |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1897 | * a pattern, such as */*, if the caller does not have specific type |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1898 | * requirements; in this case the content provider will pick its best |
| 1899 | * type matching the pattern. |
| 1900 | * @param opts Additional options from the client. The definitions of |
| 1901 | * these are specific to the content provider being called. |
| 1902 | * @param signal A signal to cancel the operation in progress, or |
| 1903 | * {@code null} if none. For example, if you are downloading a |
| 1904 | * file from the network to service a "rw" mode request, you |
| 1905 | * should periodically call |
| 1906 | * {@link CancellationSignal#throwIfCanceled()} to check whether |
| 1907 | * the client has canceled the request and abort the download. |
| 1908 | * |
| 1909 | * @return Returns a new AssetFileDescriptor from which the client can |
| 1910 | * read data of the desired type. |
| 1911 | * |
| 1912 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1913 | * no file associated with the given URI or the mode is invalid. |
| 1914 | * @throws SecurityException Throws SecurityException if the caller does |
| 1915 | * not have permission to access the data. |
| 1916 | * @throws IllegalArgumentException Throws IllegalArgumentException if the |
| 1917 | * content provider does not support the requested MIME type. |
| 1918 | * |
| 1919 | * @see #getStreamTypes(Uri, String) |
| 1920 | * @see #openAssetFile(Uri, String) |
| 1921 | * @see ClipDescription#compareMimeTypes(String, String) |
| 1922 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 1923 | @Override |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1924 | public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri, |
| 1925 | @NonNull String mimeTypeFilter, @Nullable Bundle opts, |
| 1926 | @Nullable CancellationSignal signal) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1927 | return openTypedAssetFile(uri, mimeTypeFilter, opts); |
| 1928 | } |
| 1929 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1930 | /** |
| 1931 | * Interface to write a stream of data to a pipe. Use with |
| 1932 | * {@link ContentProvider#openPipeHelper}. |
| 1933 | */ |
| 1934 | public interface PipeDataWriter<T> { |
| 1935 | /** |
| 1936 | * Called from a background thread to stream data out to a pipe. |
| 1937 | * Note that the pipe is blocking, so this thread can block on |
| 1938 | * writes for an arbitrary amount of time if the client is slow |
| 1939 | * at reading. |
| 1940 | * |
| 1941 | * @param output The pipe where data should be written. This will be |
| 1942 | * closed for you upon returning from this function. |
| 1943 | * @param uri The URI whose data is to be written. |
| 1944 | * @param mimeType The desired type of data to be written. |
| 1945 | * @param opts Options supplied by caller. |
| 1946 | * @param args Your own custom arguments. |
| 1947 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1948 | public void writeDataToPipe(@NonNull ParcelFileDescriptor output, @NonNull Uri uri, |
| 1949 | @NonNull String mimeType, @Nullable Bundle opts, @Nullable T args); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * A helper function for implementing {@link #openTypedAssetFile}, for |
| 1954 | * creating a data pipe and background thread allowing you to stream |
| 1955 | * generated data back to the client. This function returns a new |
| 1956 | * ParcelFileDescriptor that should be returned to the caller (the caller |
| 1957 | * is responsible for closing it). |
| 1958 | * |
| 1959 | * @param uri The URI whose data is to be written. |
| 1960 | * @param mimeType The desired type of data to be written. |
| 1961 | * @param opts Options supplied by caller. |
| 1962 | * @param args Your own custom arguments. |
| 1963 | * @param func Interface implementing the function that will actually |
| 1964 | * stream the data. |
| 1965 | * @return Returns a new ParcelFileDescriptor holding the read side of |
| 1966 | * the pipe. This should be returned to the caller for reading; the caller |
| 1967 | * is responsible for closing it when done. |
| 1968 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1969 | public @NonNull <T> ParcelFileDescriptor openPipeHelper(final @NonNull Uri uri, |
| 1970 | final @NonNull String mimeType, final @Nullable Bundle opts, final @Nullable T args, |
| 1971 | final @NonNull PipeDataWriter<T> func) throws FileNotFoundException { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1972 | try { |
| 1973 | final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); |
| 1974 | |
| 1975 | AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() { |
| 1976 | @Override |
| 1977 | protected Object doInBackground(Object... params) { |
| 1978 | func.writeDataToPipe(fds[1], uri, mimeType, opts, args); |
| 1979 | try { |
| 1980 | fds[1].close(); |
| 1981 | } catch (IOException e) { |
| 1982 | Log.w(TAG, "Failure closing pipe", e); |
| 1983 | } |
| 1984 | return null; |
| 1985 | } |
| 1986 | }; |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1987 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1988 | |
| 1989 | return fds[0]; |
| 1990 | } catch (IOException e) { |
| 1991 | throw new FileNotFoundException("failure making pipe"); |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | * Returns true if this instance is a temporary content provider. |
| 1997 | * @return true if this instance is a temporary content provider |
| 1998 | */ |
| 1999 | protected boolean isTemporary() { |
| 2000 | return false; |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * Returns the Binder object for this provider. |
| 2005 | * |
| 2006 | * @return the Binder object for this provider |
| 2007 | * @hide |
| 2008 | */ |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 2009 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | public IContentProvider getIContentProvider() { |
| 2011 | return mTransport; |
| 2012 | } |
| 2013 | |
| 2014 | /** |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 2015 | * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use |
| 2016 | * when directly instantiating the provider for testing. |
| 2017 | * @hide |
| 2018 | */ |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 2019 | @UnsupportedAppUsage |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 2020 | public void attachInfoForTesting(Context context, ProviderInfo info) { |
| 2021 | attachInfo(context, info, true); |
| 2022 | } |
| 2023 | |
| 2024 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | * After being instantiated, this is called to tell the content provider |
| 2026 | * about itself. |
| 2027 | * |
| 2028 | * @param context The context this provider is running in |
| 2029 | * @param info Registered information about this content provider |
| 2030 | */ |
| 2031 | public void attachInfo(Context context, ProviderInfo info) { |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 2032 | attachInfo(context, info, false); |
| 2033 | } |
| 2034 | |
| 2035 | private void attachInfo(Context context, ProviderInfo info, boolean testing) { |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 2036 | mNoPerms = testing; |
Jeff Sharkey | 497789e | 2019-02-15 19:41:30 -0700 | [diff] [blame] | 2037 | mCallingPackage = new ThreadLocal<>(); |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 2038 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | /* |
| 2040 | * Only allow it to be set once, so after the content service gives |
| 2041 | * this to us clients can't change it. |
| 2042 | */ |
| 2043 | if (mContext == null) { |
| 2044 | mContext = context; |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 2045 | if (context != null && mTransport != null) { |
Jeff Sharkey | 10cb312 | 2013-09-17 15:18:43 -0700 | [diff] [blame] | 2046 | mTransport.mAppOpsManager = (AppOpsManager) context.getSystemService( |
| 2047 | Context.APP_OPS_SERVICE); |
| 2048 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 2049 | mMyUid = Process.myUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2050 | if (info != null) { |
| 2051 | setReadPermission(info.readPermission); |
| 2052 | setWritePermission(info.writePermission); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 2053 | setPathPermissions(info.pathPermissions); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2054 | mExported = info.exported; |
Amith Yamasani | a6f4d58 | 2014-08-07 17:58:39 -0700 | [diff] [blame] | 2055 | mSingleUser = (info.flags & ProviderInfo.FLAG_SINGLE_USER) != 0; |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2056 | setAuthorities(info.authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2057 | } |
| 2058 | ContentProvider.this.onCreate(); |
| 2059 | } |
| 2060 | } |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 2061 | |
| 2062 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 2063 | * Override this to handle requests to perform a batch of operations, or the |
| 2064 | * default implementation will iterate over the operations and call |
| 2065 | * {@link ContentProviderOperation#apply} on each of them. |
| 2066 | * If all calls to {@link ContentProviderOperation#apply} succeed |
| 2067 | * then a {@link ContentProviderResult} array with as many |
| 2068 | * 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] | 2069 | * fail, it is up to the implementation how many of the others take effect. |
| 2070 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 2071 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 2072 | * and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 2073 | * |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 2074 | * @param operations the operations to apply |
| 2075 | * @return the results of the applications |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 2076 | * @throws OperationApplicationException thrown if any operation fails. |
| 2077 | * @see ContentProviderOperation#apply |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 2078 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 2079 | @Override |
| 2080 | public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority, |
| 2081 | @NonNull ArrayList<ContentProviderOperation> operations) |
| 2082 | throws OperationApplicationException { |
| 2083 | return applyBatch(operations); |
| 2084 | } |
| 2085 | |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2086 | public @NonNull ContentProviderResult[] applyBatch( |
| 2087 | @NonNull ArrayList<ContentProviderOperation> operations) |
| 2088 | throws OperationApplicationException { |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 2089 | final int numOperations = operations.size(); |
| 2090 | final ContentProviderResult[] results = new ContentProviderResult[numOperations]; |
| 2091 | for (int i = 0; i < numOperations; i++) { |
| 2092 | results[i] = operations.get(i).apply(this, results, i); |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 2093 | } |
| 2094 | return results; |
| 2095 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 2096 | |
| 2097 | /** |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 2098 | * Call a provider-defined method. This can be used to implement |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 2099 | * interfaces that are cheaper and/or unnatural for a table-like |
| 2100 | * model. |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 2101 | * |
Dianne Hackborn | 5d122d9 | 2013-03-12 18:37:07 -0700 | [diff] [blame] | 2102 | * <p class="note"><strong>WARNING:</strong> The framework does no permission checking |
| 2103 | * on this entry into the content provider besides the basic ability for the application |
| 2104 | * to get access to the provider at all. For example, it has no idea whether the call |
| 2105 | * being executed may read or write data in the provider, so can't enforce those |
| 2106 | * individual permissions. Any implementation of this method <strong>must</strong> |
| 2107 | * do its own permission checks on incoming calls to make sure they are allowed.</p> |
| 2108 | * |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 2109 | * @param method method name to call. Opaque to framework, but should not be {@code null}. |
| 2110 | * @param arg provider-defined String argument. May be {@code null}. |
| 2111 | * @param extras provider-defined Bundle argument. May be {@code null}. |
| 2112 | * @return provider-defined return value. May be {@code null}, which is also |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 2113 | * the default for providers which don't implement any call methods. |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 2114 | */ |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 2115 | @Override |
| 2116 | public @Nullable Bundle call(@NonNull String authority, @NonNull String method, |
| 2117 | @Nullable String arg, @Nullable Bundle extras) { |
| 2118 | return call(method, arg, extras); |
| 2119 | } |
| 2120 | |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2121 | public @Nullable Bundle call(@NonNull String method, @Nullable String arg, |
| 2122 | @Nullable Bundle extras) { |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 2123 | return null; |
| 2124 | } |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 2125 | |
| 2126 | /** |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 2127 | * Implement this to shut down the ContentProvider instance. You can then |
| 2128 | * invoke this method in unit tests. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 2129 | * |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 2130 | * <p> |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 2131 | * Android normally handles ContentProvider startup and shutdown |
| 2132 | * automatically. You do not need to start up or shut down a |
| 2133 | * ContentProvider. When you invoke a test method on a ContentProvider, |
| 2134 | * however, a ContentProvider instance is started and keeps running after |
| 2135 | * the test finishes, even if a succeeding test instantiates another |
| 2136 | * ContentProvider. A conflict develops because the two instances are |
| 2137 | * usually running against the same underlying data source (for example, an |
| 2138 | * sqlite database). |
| 2139 | * </p> |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 2140 | * <p> |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 2141 | * Implementing shutDown() avoids this conflict by providing a way to |
| 2142 | * terminate the ContentProvider. This method can also prevent memory leaks |
| 2143 | * from multiple instantiations of the ContentProvider, and it can ensure |
| 2144 | * unit test isolation by allowing you to completely clean up the test |
| 2145 | * fixture before moving on to the next test. |
| 2146 | * </p> |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 2147 | */ |
| 2148 | public void shutdown() { |
| 2149 | Log.w(TAG, "implement ContentProvider shutdown() to make sure all database " + |
| 2150 | "connections are gracefully shutdown"); |
| 2151 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2152 | |
| 2153 | /** |
| 2154 | * 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] | 2155 | * you run "adb shell dumpsys activity provider <provider_component_name>". |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2156 | * |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2157 | * @param fd The raw file descriptor that the dump is being sent to. |
| 2158 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 2159 | * closed for you after you return. |
| 2160 | * @param args additional arguments to the dump request. |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2161 | */ |
| 2162 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2163 | writer.println("nothing to dump"); |
| 2164 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2165 | |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 2166 | private void validateIncomingAuthority(String authority) throws SecurityException { |
| 2167 | if (!matchesOurAuthorities(getAuthorityWithoutUserId(authority))) { |
| 2168 | String message = "The authority " + authority + " does not match the one of the " |
| 2169 | + "contentProvider: "; |
| 2170 | if (mAuthority != null) { |
| 2171 | message += mAuthority; |
| 2172 | } else { |
| 2173 | message += Arrays.toString(mAuthorities); |
| 2174 | } |
| 2175 | throw new SecurityException(message); |
| 2176 | } |
| 2177 | } |
| 2178 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2179 | /** @hide */ |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 2180 | @VisibleForTesting |
| 2181 | public Uri validateIncomingUri(Uri uri) throws SecurityException { |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2182 | String auth = uri.getAuthority(); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 2183 | if (!mSingleUser) { |
| 2184 | int userId = getUserIdFromAuthority(auth, UserHandle.USER_CURRENT); |
| 2185 | if (userId != UserHandle.USER_CURRENT && userId != mContext.getUserId()) { |
| 2186 | throw new SecurityException("trying to query a ContentProvider in user " |
| 2187 | + mContext.getUserId() + " with a uri belonging to user " + userId); |
| 2188 | } |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2189 | } |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame] | 2190 | validateIncomingAuthority(auth); |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 2191 | |
| 2192 | // Normalize the path by removing any empty path segments, which can be |
| 2193 | // a source of security issues. |
| 2194 | final String encodedPath = uri.getEncodedPath(); |
| 2195 | if (encodedPath != null && encodedPath.indexOf("//") != -1) { |
Jeff Sharkey | 4a7b6ac | 2018-10-03 10:33:46 -0600 | [diff] [blame] | 2196 | final Uri normalized = uri.buildUpon() |
| 2197 | .encodedPath(encodedPath.replaceAll("//+", "/")).build(); |
| 2198 | Log.w(TAG, "Normalized " + uri + " to " + normalized |
| 2199 | + " to avoid possible security issues"); |
| 2200 | return normalized; |
Jeff Sharkey | c4156e0 | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 2201 | } else { |
| 2202 | return uri; |
| 2203 | } |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2204 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2205 | |
| 2206 | /** @hide */ |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 2207 | private Uri maybeGetUriWithoutUserId(Uri uri) { |
| 2208 | if (mSingleUser) { |
| 2209 | return uri; |
| 2210 | } |
| 2211 | return getUriWithoutUserId(uri); |
| 2212 | } |
| 2213 | |
| 2214 | /** @hide */ |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2215 | public static int getUserIdFromAuthority(String auth, int defaultUserId) { |
| 2216 | if (auth == null) return defaultUserId; |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2217 | int end = auth.lastIndexOf('@'); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2218 | if (end == -1) return defaultUserId; |
| 2219 | String userIdString = auth.substring(0, end); |
| 2220 | try { |
| 2221 | return Integer.parseInt(userIdString); |
| 2222 | } catch (NumberFormatException e) { |
| 2223 | Log.w(TAG, "Error parsing userId.", e); |
| 2224 | return UserHandle.USER_NULL; |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | /** @hide */ |
| 2229 | public static int getUserIdFromAuthority(String auth) { |
| 2230 | return getUserIdFromAuthority(auth, UserHandle.USER_CURRENT); |
| 2231 | } |
| 2232 | |
| 2233 | /** @hide */ |
| 2234 | public static int getUserIdFromUri(Uri uri, int defaultUserId) { |
| 2235 | if (uri == null) return defaultUserId; |
| 2236 | return getUserIdFromAuthority(uri.getAuthority(), defaultUserId); |
| 2237 | } |
| 2238 | |
| 2239 | /** @hide */ |
| 2240 | public static int getUserIdFromUri(Uri uri) { |
| 2241 | return getUserIdFromUri(uri, UserHandle.USER_CURRENT); |
| 2242 | } |
| 2243 | |
| 2244 | /** |
| 2245 | * Removes userId part from authority string. Expects format: |
| 2246 | * userId@some.authority |
| 2247 | * If there is no userId in the authority, it symply returns the argument |
| 2248 | * @hide |
| 2249 | */ |
| 2250 | public static String getAuthorityWithoutUserId(String auth) { |
| 2251 | if (auth == null) return null; |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2252 | int end = auth.lastIndexOf('@'); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2253 | return auth.substring(end+1); |
| 2254 | } |
| 2255 | |
| 2256 | /** @hide */ |
| 2257 | public static Uri getUriWithoutUserId(Uri uri) { |
| 2258 | if (uri == null) return null; |
| 2259 | Uri.Builder builder = uri.buildUpon(); |
| 2260 | builder.authority(getAuthorityWithoutUserId(uri.getAuthority())); |
| 2261 | return builder.build(); |
| 2262 | } |
| 2263 | |
| 2264 | /** @hide */ |
| 2265 | public static boolean uriHasUserId(Uri uri) { |
| 2266 | if (uri == null) return false; |
| 2267 | return !TextUtils.isEmpty(uri.getUserInfo()); |
| 2268 | } |
| 2269 | |
| 2270 | /** @hide */ |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 2271 | @UnsupportedAppUsage |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2272 | public static Uri maybeAddUserId(Uri uri, int userId) { |
| 2273 | if (uri == null) return null; |
| 2274 | if (userId != UserHandle.USER_CURRENT |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 2275 | && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2276 | if (!uriHasUserId(uri)) { |
| 2277 | //We don't add the user Id if there's already one |
| 2278 | Uri.Builder builder = uri.buildUpon(); |
| 2279 | builder.encodedAuthority("" + userId + "@" + uri.getEncodedAuthority()); |
| 2280 | return builder.build(); |
| 2281 | } |
| 2282 | } |
| 2283 | return uri; |
| 2284 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 2285 | } |