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