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