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