blob: 60ecf64dd0a9352e6c5ce3ce9fb26c3fb0476813 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.content;
18
Nicolas Prevot504d78e2014-06-26 10:07:33 +010019import static android.Manifest.permission.INTERACT_ACROSS_USERS;
Varun Shahaf8cfe32019-08-16 16:11:24 -070020import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
Jeff Sharkey0e621c32015-07-24 15:10:20 -070021import static android.app.AppOpsManager.MODE_ALLOWED;
Eugene Susla93519852018-06-13 16:44:31 -070022import static android.app.AppOpsManager.MODE_DEFAULT;
Jeff Sharkey0e621c32015-07-24 15:10:20 -070023import static android.app.AppOpsManager.MODE_ERRORED;
24import static android.app.AppOpsManager.MODE_IGNORED;
25import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Jeff Sharkey9664ff52018-08-03 17:08:04 -060026import static android.os.Trace.TRACE_TAG_DATABASE;
Jeff Sharkey110a6b62012-03-12 11:12:41 -070027
Jeff Sharkey673db442015-06-11 19:30:57 -070028import android.annotation.NonNull;
Scott Kennedy9f78f652015-03-01 15:29:25 -080029import android.annotation.Nullable;
Jeff Sharkey197fe1f2020-01-07 22:06:37 -070030import android.annotation.SystemApi;
Alex Kershawba014ac2020-01-20 22:25:02 +000031import android.annotation.TestApi;
Dianne Hackborn35654b62013-01-14 17:38:02 -080032import android.app.AppOpsManager;
Artur Satayeve23a0eb2019-12-10 17:47:52 +000033import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey9edef252019-05-20 14:00:17 -060034import android.content.pm.PackageManager;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070035import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.ProviderInfo;
37import android.content.res.AssetFileDescriptor;
38import android.content.res.Configuration;
39import android.database.Cursor;
Svet Ganov7271f3e2015-04-23 10:16:53 -070040import android.database.MatrixCursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.database.SQLException;
42import android.net.Uri;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070043import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Binder;
Mathew Inwood8c854f82018-09-14 12:35:36 +010045import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080046import android.os.Bundle;
Jeff Browna7771df2012-05-07 20:06:46 -070047import android.os.CancellationSignal;
Dianne Hackbornff170242014-11-19 10:59:01 -080048import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070049import android.os.ICancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.ParcelFileDescriptor;
Dmitri Plotnikove7f48d92020-03-27 10:05:46 -070051import android.os.ParcelableException;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070052import android.os.Process;
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -080053import android.os.RemoteCallback;
Ben Lin1cf454f2016-11-10 13:50:54 -080054import android.os.RemoteException;
Jeff Sharkey9664ff52018-08-03 17:08:04 -060055import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070056import android.os.UserHandle;
Jeff Sharkeyb31afd22017-06-12 14:17:10 -060057import android.os.storage.StorageManager;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010058import android.text.TextUtils;
Jeff Sharkey0e621c32015-07-24 15:10:20 -070059import android.util.Log;
Philip P. Moltmann128b7032019-09-27 08:44:12 -070060import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
Jeff Sharkeyc4156e02018-09-24 13:23:57 -060062import com.android.internal.annotations.VisibleForTesting;
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import java.io.File;
Marco Nelissen18cb2872011-11-15 11:19:53 -080065import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.FileNotFoundException;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070067import java.io.IOException;
Marco Nelissen18cb2872011-11-15 11:19:53 -080068import java.io.PrintWriter;
Fred Quintana03d94902009-05-22 14:23:31 -070069import java.util.ArrayList;
Andreas Gampee6748ce2015-12-11 18:00:38 -080070import java.util.Arrays;
Jeff Sharkeyc4156e02018-09-24 13:23:57 -060071import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73/**
74 * Content providers are one of the primary building blocks of Android applications, providing
75 * content to applications. They encapsulate data and provide it to applications through the single
76 * {@link ContentResolver} interface. A content provider is only required if you need to share
77 * data between multiple applications. For example, the contacts data is used by multiple
78 * applications and must be stored in a content provider. If you don't need to share data amongst
79 * multiple applications you can use a database directly via
80 * {@link android.database.sqlite.SQLiteDatabase}.
81 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * <p>When a request is made via
83 * a {@link ContentResolver} the system inspects the authority of the given URI and passes the
84 * request to the content provider registered with the authority. The content provider can interpret
85 * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing
86 * URIs.</p>
87 *
88 * <p>The primary methods that need to be implemented are:
89 * <ul>
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070090 * <li>{@link #onCreate} which is called to initialize the provider</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * <li>{@link #query} which returns data to the caller</li>
92 * <li>{@link #insert} which inserts new data into the content provider</li>
93 * <li>{@link #update} which updates existing data in the content provider</li>
94 * <li>{@link #delete} which deletes data from the content provider</li>
95 * <li>{@link #getType} which returns the MIME type of data in the content provider</li>
96 * </ul></p>
97 *
Dan Egnor6fcc0f0732010-07-27 16:32:17 -070098 * <p class="caution">Data access methods (such as {@link #insert} and
99 * {@link #update}) may be called from many threads at once, and must be thread-safe.
100 * Other methods (such as {@link #onCreate}) are only called from the application
101 * main thread, and must avoid performing lengthy operations. See the method
102 * descriptions for their expected thread behavior.</p>
103 *
104 * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate
105 * ContentProvider instance, so subclasses don't have to worry about the details of
106 * cross-process calls.</p>
Joe Fernandez558459f2011-10-13 16:47:36 -0700107 *
108 * <div class="special reference">
109 * <h3>Developer Guides</h3>
110 * <p>For more information about using content providers, read the
111 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
112 * developer guide.</p>
Nicole Borrelli8a5f04a2018-09-20 14:19:14 -0700113 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700115public abstract class ContentProvider implements ContentInterface, ComponentCallbacks2 {
Steve McKayea93fe72016-12-02 11:35:35 -0800116
Vasu Nori0c9e14a2010-08-04 13:31:48 -0700117 private static final String TAG = "ContentProvider";
118
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900119 /*
120 * Note: if you add methods to ContentProvider, you must add similar methods to
121 * MockContentProvider.
122 */
123
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100124 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 private Context mContext = null;
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700126 private int mMyUid;
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100127
128 // Since most Providers have only one authority, we keep both a String and a String[] to improve
129 // performance.
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100130 @UnsupportedAppUsage
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100131 private String mAuthority;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100132 @UnsupportedAppUsage
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100133 private String[] mAuthorities;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100134 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 private String mReadPermission;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100136 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private String mWritePermission;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100138 @UnsupportedAppUsage
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700139 private PathPermission[] mPathPermissions;
Dianne Hackbornb424b632010-08-18 15:59:05 -0700140 private boolean mExported;
Dianne Hackborn7e6f9762013-02-26 13:35:11 -0800141 private boolean mNoPerms;
Amith Yamasania6f4d582014-08-07 17:58:39 -0700142 private boolean mSingleUser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700144 private ThreadLocal<Pair<String, String>> mCallingPackage;
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 private Transport mTransport = new Transport();
147
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700148 /**
149 * Construct a ContentProvider instance. Content providers must be
150 * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared
151 * in the manifest</a>, accessed with {@link ContentResolver}, and created
152 * automatically by the system, so applications usually do not create
153 * ContentProvider instances directly.
154 *
155 * <p>At construction time, the object is uninitialized, and most fields and
156 * methods are unavailable. Subclasses should initialize themselves in
157 * {@link #onCreate}, not the constructor.
158 *
159 * <p>Content providers are created on the application main thread at
160 * application launch time. The constructor must not perform lengthy
161 * operations, or application startup will be delayed.
162 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900163 public ContentProvider() {
164 }
165
166 /**
167 * Constructor just for mocking.
168 *
169 * @param context A Context object which should be some mock instance (like the
170 * instance of {@link android.test.mock.MockContext}).
171 * @param readPermission The read permision you want this instance should have in the
172 * test, which is available via {@link #getReadPermission()}.
173 * @param writePermission The write permission you want this instance should have
174 * in the test, which is available via {@link #getWritePermission()}.
175 * @param pathPermissions The PathPermissions you want this instance should have
176 * in the test, which is available via {@link #getPathPermissions()}.
177 * @hide
178 */
Mathew Inwood8c854f82018-09-14 12:35:36 +0100179 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900180 public ContentProvider(
181 Context context,
182 String readPermission,
183 String writePermission,
184 PathPermission[] pathPermissions) {
185 mContext = context;
186 mReadPermission = readPermission;
187 mWritePermission = writePermission;
188 mPathPermissions = pathPermissions;
189 }
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
192 * Given an IContentProvider, try to coerce it back to the real
193 * ContentProvider object if it is running in the local process. This can
194 * be used if you know you are running in the same process as a provider,
195 * and want to get direct access to its implementation details. Most
196 * clients should not nor have a reason to use it.
197 *
198 * @param abstractInterface The ContentProvider interface that is to be
199 * coerced.
Christopher Tate2bc6eb82013-01-03 12:04:08 -0800200 * @return If the IContentProvider is non-{@code null} and local, returns its actual
201 * ContentProvider instance. Otherwise returns {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * @hide
203 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100204 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static ContentProvider coerceToLocalContentProvider(
206 IContentProvider abstractInterface) {
207 if (abstractInterface instanceof Transport) {
208 return ((Transport)abstractInterface).getContentProvider();
209 }
210 return null;
211 }
212
213 /**
214 * Binder object that deals with remoting.
215 *
216 * @hide
217 */
218 class Transport extends ContentProviderNative {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700219 volatile AppOpsManager mAppOpsManager = null;
220 volatile int mReadOp = AppOpsManager.OP_NONE;
221 volatile int mWriteOp = AppOpsManager.OP_NONE;
222 volatile ContentInterface mInterface = ContentProvider.this;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 ContentProvider getContentProvider() {
225 return ContentProvider.this;
226 }
227
Jeff Brownd2183652011-10-09 12:39:53 -0700228 @Override
229 public String getProviderName() {
230 return getContentProvider().getClass().getName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232
Jeff Brown75ea64f2012-01-25 19:37:13 -0800233 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800234 public Cursor query(String callingPkg, @Nullable String attributionTag, Uri uri,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700235 @Nullable String[] projection, @Nullable Bundle queryArgs,
236 @Nullable ICancellationSignal cancellationSignal) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600237 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100238 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800239 if (enforceReadPermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700240 != AppOpsManager.MODE_ALLOWED) {
Svet Ganov7271f3e2015-04-23 10:16:53 -0700241 // The caller has no access to the data, so return an empty cursor with
242 // the columns in the requested order. The caller may ask for an invalid
243 // column and we would not catch that but this is not a problem in practice.
244 // We do not call ContentProvider#query with a modified where clause since
245 // the implementation is not guaranteed to be backed by a SQL database, hence
246 // it may not handle properly the tautology where clause we would have created.
Svet Ganova2147ec2015-04-27 17:00:44 -0700247 if (projection != null) {
248 return new MatrixCursor(projection, 0);
249 }
250
251 // Null projection means all columns but we have no idea which they are.
252 // However, the caller may be expecting to access them my index. Hence,
253 // we have to execute the query as if allowed to get a cursor with the
254 // columns. We then use the column names to return an empty cursor.
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700255 Cursor cursor;
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700256 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800257 new Pair<>(callingPkg, attributionTag));
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700258 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700259 cursor = mInterface.query(
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700260 uri, projection, queryArgs,
261 CancellationSignal.fromTransport(cancellationSignal));
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700262 } catch (RemoteException e) {
263 throw e.rethrowAsRuntimeException();
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700264 } finally {
265 setCallingPackage(original);
266 }
Makoto Onuki34bdcdb2015-06-12 17:14:57 -0700267 if (cursor == null) {
268 return null;
Svet Ganova2147ec2015-04-27 17:00:44 -0700269 }
270
271 // Return an empty cursor for all columns.
Makoto Onuki34bdcdb2015-06-12 17:14:57 -0700272 return new MatrixCursor(cursor.getColumnNames(), 0);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800273 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600274 Trace.traceBegin(TRACE_TAG_DATABASE, "query");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700275 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800276 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700277 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700278 return mInterface.query(
Steve McKayea93fe72016-12-02 11:35:35 -0800279 uri, projection, queryArgs,
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700280 CancellationSignal.fromTransport(cancellationSignal));
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700281 } catch (RemoteException e) {
282 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700283 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700284 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600285 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288
Jeff Brown75ea64f2012-01-25 19:37:13 -0800289 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 public String getType(Uri uri) {
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700291 // getCallingPackage() isn't available in getType(), as the javadoc states.
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600292 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100293 uri = maybeGetUriWithoutUserId(uri);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600294 Trace.traceBegin(TRACE_TAG_DATABASE, "getType");
295 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700296 return mInterface.getType(uri);
297 } catch (RemoteException e) {
298 throw e.rethrowAsRuntimeException();
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600299 } finally {
300 Trace.traceEnd(TRACE_TAG_DATABASE);
301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303
Jeff Brown75ea64f2012-01-25 19:37:13 -0800304 @Override
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800305 public void getTypeAsync(Uri uri, RemoteCallback callback) {
306 final Bundle result = new Bundle();
Dmitri Plotnikovbe61c442020-03-24 15:43:57 -0700307 try {
308 result.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri));
309 } catch (Exception e) {
Dmitri Plotnikove7f48d92020-03-27 10:05:46 -0700310 result.putParcelable(ContentResolver.REMOTE_CALLBACK_ERROR,
311 new ParcelableException(e));
Dmitri Plotnikovbe61c442020-03-24 15:43:57 -0700312 }
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800313 callback.sendResult(result);
314 }
315
316 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800317 public Uri insert(String callingPkg, @Nullable String attributionTag, Uri uri,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700318 ContentValues initialValues, Bundle extras) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600319 uri = validateIncomingUri(uri);
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100320 int userId = getUserIdFromUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100321 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800322 if (enforceWritePermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700323 != AppOpsManager.MODE_ALLOWED) {
324 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800325 new Pair<>(callingPkg, attributionTag));
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700326 try {
327 return rejectInsert(uri, initialValues);
328 } finally {
329 setCallingPackage(original);
330 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800331 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600332 Trace.traceBegin(TRACE_TAG_DATABASE, "insert");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700333 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800334 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700335 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700336 return maybeAddUserId(mInterface.insert(uri, initialValues, extras), userId);
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700337 } catch (RemoteException e) {
338 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700339 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700340 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600341 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344
Jeff Brown75ea64f2012-01-25 19:37:13 -0800345 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800346 public int bulkInsert(String callingPkg, @Nullable String attributionTag, Uri uri,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700347 ContentValues[] initialValues) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600348 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100349 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800350 if (enforceWritePermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700351 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800352 return 0;
353 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600354 Trace.traceBegin(TRACE_TAG_DATABASE, "bulkInsert");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700355 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800356 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700357 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700358 return mInterface.bulkInsert(uri, initialValues);
359 } catch (RemoteException e) {
360 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700361 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700362 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600363 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
366
Jeff Brown75ea64f2012-01-25 19:37:13 -0800367 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800368 public ContentProviderResult[] applyBatch(String callingPkg,
369 @Nullable String attributionTag, String authority,
370 ArrayList<ContentProviderOperation> operations)
Fred Quintana89437372009-05-15 15:10:40 -0700371 throws OperationApplicationException {
Jeff Sharkey2de00bf2018-12-13 15:06:05 -0700372 validateIncomingAuthority(authority);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100373 int numOperations = operations.size();
374 final int[] userIds = new int[numOperations];
375 for (int i = 0; i < numOperations; i++) {
376 ContentProviderOperation operation = operations.get(i);
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100377 Uri uri = operation.getUri();
Jeff Sharkey9144b4d2018-09-26 20:15:12 -0600378 userIds[i] = getUserIdFromUri(uri);
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600379 uri = validateIncomingUri(uri);
380 uri = maybeGetUriWithoutUserId(uri);
381 // Rebuild operation if we changed the Uri above
382 if (!Objects.equals(operation.getUri(), uri)) {
383 operation = new ContentProviderOperation(operation, uri);
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100384 operations.set(i, operation);
385 }
Fred Quintana89437372009-05-15 15:10:40 -0700386 if (operation.isReadOperation()) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800387 if (enforceReadPermission(callingPkg, attributionTag, uri, null)
Dianne Hackborn35654b62013-01-14 17:38:02 -0800388 != AppOpsManager.MODE_ALLOWED) {
389 throw new OperationApplicationException("App op not allowed", 0);
390 }
Fred Quintana89437372009-05-15 15:10:40 -0700391 }
Fred Quintana89437372009-05-15 15:10:40 -0700392 if (operation.isWriteOperation()) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800393 if (enforceWritePermission(callingPkg, attributionTag, uri, null)
Dianne Hackborn35654b62013-01-14 17:38:02 -0800394 != AppOpsManager.MODE_ALLOWED) {
395 throw new OperationApplicationException("App op not allowed", 0);
396 }
Fred Quintana89437372009-05-15 15:10:40 -0700397 }
398 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600399 Trace.traceBegin(TRACE_TAG_DATABASE, "applyBatch");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700400 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800401 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700402 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700403 ContentProviderResult[] results = mInterface.applyBatch(authority,
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700404 operations);
Jay Shraunerac2506c2014-12-15 12:28:25 -0800405 if (results != null) {
406 for (int i = 0; i < results.length ; i++) {
407 if (userIds[i] != UserHandle.USER_CURRENT) {
408 // Adding the userId to the uri.
409 results[i] = new ContentProviderResult(results[i], userIds[i]);
410 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100411 }
412 }
413 return results;
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700414 } catch (RemoteException e) {
415 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700416 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700417 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600418 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700419 }
Fred Quintana6a8d5332009-05-07 17:35:38 -0700420 }
421
Jeff Brown75ea64f2012-01-25 19:37:13 -0800422 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800423 public int delete(String callingPkg, @Nullable String attributionTag, Uri uri,
424 Bundle extras) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600425 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100426 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800427 if (enforceWritePermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700428 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800429 return 0;
430 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600431 Trace.traceBegin(TRACE_TAG_DATABASE, "delete");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700432 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800433 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700434 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700435 return mInterface.delete(uri, extras);
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700436 } catch (RemoteException e) {
437 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700438 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700439 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600440 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443
Jeff Brown75ea64f2012-01-25 19:37:13 -0800444 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800445 public int update(String callingPkg, @Nullable String attributionTag, Uri uri,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700446 ContentValues values, Bundle extras) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600447 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100448 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800449 if (enforceWritePermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700450 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800451 return 0;
452 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600453 Trace.traceBegin(TRACE_TAG_DATABASE, "update");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700454 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800455 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700456 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700457 return mInterface.update(uri, values, extras);
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700458 } catch (RemoteException e) {
459 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700460 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700461 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600462 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
Jeff Brown75ea64f2012-01-25 19:37:13 -0800466 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800467 public ParcelFileDescriptor openFile(String callingPkg, @Nullable String attributionTag,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700468 Uri uri, String mode, ICancellationSignal cancellationSignal, IBinder callerToken)
469 throws FileNotFoundException {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600470 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100471 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800472 enforceFilePermission(callingPkg, attributionTag, uri, mode, callerToken);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600473 Trace.traceBegin(TRACE_TAG_DATABASE, "openFile");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700474 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800475 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700476 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700477 return mInterface.openFile(
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700478 uri, mode, CancellationSignal.fromTransport(cancellationSignal));
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700479 } catch (RemoteException e) {
480 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700481 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700482 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600483 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
486
Jeff Brown75ea64f2012-01-25 19:37:13 -0800487 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800488 public AssetFileDescriptor openAssetFile(String callingPkg, @Nullable String attributionTag,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700489 Uri uri, String mode, ICancellationSignal cancellationSignal)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 throws FileNotFoundException {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600491 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100492 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800493 enforceFilePermission(callingPkg, attributionTag, uri, mode, null);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600494 Trace.traceBegin(TRACE_TAG_DATABASE, "openAssetFile");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700495 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800496 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700497 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700498 return mInterface.openAssetFile(
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700499 uri, mode, CancellationSignal.fromTransport(cancellationSignal));
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700500 } catch (RemoteException e) {
501 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700502 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700503 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600504 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507
Jeff Brown75ea64f2012-01-25 19:37:13 -0800508 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800509 public Bundle call(String callingPkg, @Nullable String attributionTag, String authority,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700510 String method, @Nullable String arg, @Nullable Bundle extras) {
Jeff Sharkey2de00bf2018-12-13 15:06:05 -0700511 validateIncomingAuthority(authority);
Jeff Sharkeya04c7a72016-03-18 12:20:36 -0600512 Bundle.setDefusable(extras, true);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600513 Trace.traceBegin(TRACE_TAG_DATABASE, "call");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700514 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800515 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700516 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700517 return mInterface.call(authority, method, arg, extras);
518 } catch (RemoteException e) {
519 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700520 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700521 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600522 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700523 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800524 }
525
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700526 @Override
527 public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
Makoto Onuki2cc250b2018-08-28 15:40:10 -0700528 // getCallingPackage() isn't available in getType(), as the javadoc states.
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600529 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100530 uri = maybeGetUriWithoutUserId(uri);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600531 Trace.traceBegin(TRACE_TAG_DATABASE, "getStreamTypes");
532 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700533 return mInterface.getStreamTypes(uri, mimeTypeFilter);
534 } catch (RemoteException e) {
535 throw e.rethrowAsRuntimeException();
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600536 } finally {
537 Trace.traceEnd(TRACE_TAG_DATABASE);
538 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700539 }
540
541 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700542 public AssetFileDescriptor openTypedAssetFile(String callingPkg,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800543 @Nullable String attributionTag, Uri uri, String mimeType, Bundle opts,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700544 ICancellationSignal cancellationSignal) throws FileNotFoundException {
Jeff Sharkeya04c7a72016-03-18 12:20:36 -0600545 Bundle.setDefusable(opts, true);
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600546 uri = validateIncomingUri(uri);
Robin Lee2ab02e22016-07-28 18:41:23 +0100547 uri = maybeGetUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800548 enforceFilePermission(callingPkg, attributionTag, uri, "r", null);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600549 Trace.traceBegin(TRACE_TAG_DATABASE, "openTypedAssetFile");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700550 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800551 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700552 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700553 return mInterface.openTypedAssetFile(
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700554 uri, mimeType, opts, CancellationSignal.fromTransport(cancellationSignal));
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700555 } catch (RemoteException e) {
556 throw e.rethrowAsRuntimeException();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700557 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700558 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600559 Trace.traceEnd(TRACE_TAG_DATABASE);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700560 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700561 }
562
Jeff Brown75ea64f2012-01-25 19:37:13 -0800563 @Override
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700564 public ICancellationSignal createCancellationSignal() {
Jeff Brown4c1241d2012-02-02 17:05:00 -0800565 return CancellationSignal.createTransport();
Jeff Brown75ea64f2012-01-25 19:37:13 -0800566 }
567
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700568 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800569 public Uri canonicalize(String callingPkg, @Nullable String attributionTag, Uri uri) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600570 uri = validateIncomingUri(uri);
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100571 int userId = getUserIdFromUri(uri);
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100572 uri = getUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800573 if (enforceReadPermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700574 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700575 return null;
576 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600577 Trace.traceBegin(TRACE_TAG_DATABASE, "canonicalize");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700578 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800579 new Pair<>(callingPkg, attributionTag));
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700580 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700581 return maybeAddUserId(mInterface.canonicalize(uri), userId);
582 } catch (RemoteException e) {
583 throw e.rethrowAsRuntimeException();
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700584 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700585 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600586 Trace.traceEnd(TRACE_TAG_DATABASE);
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700587 }
588 }
589
590 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800591 public void canonicalizeAsync(String callingPkg, @Nullable String attributionTag, Uri uri,
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800592 RemoteCallback callback) {
593 final Bundle result = new Bundle();
Dmitri Plotnikovda4f3c82020-03-25 14:08:20 -0700594 try {
595 result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
596 canonicalize(callingPkg, attributionTag, uri));
597 } catch (Exception e) {
Dmitri Plotnikove7f48d92020-03-27 10:05:46 -0700598 result.putParcelable(ContentResolver.REMOTE_CALLBACK_ERROR,
599 new ParcelableException(e));
Dmitri Plotnikovda4f3c82020-03-25 14:08:20 -0700600 }
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800601 callback.sendResult(result);
602 }
603
604 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800605 public Uri uncanonicalize(String callingPkg, String attributionTag, Uri uri) {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600606 uri = validateIncomingUri(uri);
Nicolas Prevotf300bab2014-08-07 19:23:17 +0100607 int userId = getUserIdFromUri(uri);
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100608 uri = getUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800609 if (enforceReadPermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700610 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700611 return null;
612 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600613 Trace.traceBegin(TRACE_TAG_DATABASE, "uncanonicalize");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700614 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800615 new Pair<>(callingPkg, attributionTag));
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700616 try {
Jeff Sharkeybffd2502019-02-28 16:39:12 -0700617 return maybeAddUserId(mInterface.uncanonicalize(uri), userId);
618 } catch (RemoteException e) {
619 throw e.rethrowAsRuntimeException();
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700620 } finally {
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700621 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600622 Trace.traceEnd(TRACE_TAG_DATABASE);
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700623 }
624 }
625
Ben Lin1cf454f2016-11-10 13:50:54 -0800626 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800627 public boolean refresh(String callingPkg, String attributionTag, Uri uri, Bundle extras,
Ben Lin1cf454f2016-11-10 13:50:54 -0800628 ICancellationSignal cancellationSignal) throws RemoteException {
Jeff Sharkeyc4156e02018-09-24 13:23:57 -0600629 uri = validateIncomingUri(uri);
Ben Lin1cf454f2016-11-10 13:50:54 -0800630 uri = getUriWithoutUserId(uri);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800631 if (enforceReadPermission(callingPkg, attributionTag, uri, null)
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700632 != AppOpsManager.MODE_ALLOWED) {
Ben Lin1cf454f2016-11-10 13:50:54 -0800633 return false;
634 }
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600635 Trace.traceBegin(TRACE_TAG_DATABASE, "refresh");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700636 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800637 new Pair<>(callingPkg, attributionTag));
Ben Lin1cf454f2016-11-10 13:50:54 -0800638 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700639 return mInterface.refresh(uri, extras,
Ben Lin1cf454f2016-11-10 13:50:54 -0800640 CancellationSignal.fromTransport(cancellationSignal));
641 } finally {
642 setCallingPackage(original);
Jeff Sharkey9664ff52018-08-03 17:08:04 -0600643 Trace.traceEnd(TRACE_TAG_DATABASE);
Ben Lin1cf454f2016-11-10 13:50:54 -0800644 }
645 }
646
Jeff Sharkey9edef252019-05-20 14:00:17 -0600647 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800648 public int checkUriPermission(String callingPkg, @Nullable String attributionTag, Uri uri,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700649 int uid, int modeFlags) {
Jeff Sharkey9edef252019-05-20 14:00:17 -0600650 uri = validateIncomingUri(uri);
651 uri = maybeGetUriWithoutUserId(uri);
652 Trace.traceBegin(TRACE_TAG_DATABASE, "checkUriPermission");
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700653 final Pair<String, String> original = setCallingPackage(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800654 new Pair<>(callingPkg, attributionTag));
Jeff Sharkey9edef252019-05-20 14:00:17 -0600655 try {
656 return mInterface.checkUriPermission(uri, uid, modeFlags);
657 } catch (RemoteException e) {
658 throw e.rethrowAsRuntimeException();
659 } finally {
660 setCallingPackage(original);
661 Trace.traceEnd(TRACE_TAG_DATABASE);
662 }
663 }
664
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800665 private void enforceFilePermission(String callingPkg, @Nullable String attributionTag,
666 Uri uri, String mode, IBinder callerToken)
667 throws FileNotFoundException, SecurityException {
Jeff Sharkeyba761972013-02-28 15:57:36 -0800668 if (mode != null && mode.indexOf('w') != -1) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800669 if (enforceWritePermission(callingPkg, attributionTag, uri, callerToken)
Dianne Hackbornff170242014-11-19 10:59:01 -0800670 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800671 throw new FileNotFoundException("App op not allowed");
672 }
673 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800674 if (enforceReadPermission(callingPkg, attributionTag, uri, callerToken)
Dianne Hackbornff170242014-11-19 10:59:01 -0800675 != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800676 throw new FileNotFoundException("App op not allowed");
677 }
678 }
679 }
680
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800681 private int enforceReadPermission(String callingPkg, @Nullable String attributionTag,
682 Uri uri, IBinder callerToken)
Dianne Hackbornff170242014-11-19 10:59:01 -0800683 throws SecurityException {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800684 final int mode = enforceReadPermissionInner(uri, callingPkg, attributionTag,
685 callerToken);
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700686 if (mode != MODE_ALLOWED) {
687 return mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800688 }
Svet Ganov99b60432015-06-27 13:15:22 -0700689
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800690 return noteProxyOp(callingPkg, attributionTag, mReadOp);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800691 }
692
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800693 private int enforceWritePermission(String callingPkg, String attributionTag, Uri uri,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700694 IBinder callerToken)
Dianne Hackbornff170242014-11-19 10:59:01 -0800695 throws SecurityException {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800696 final int mode = enforceWritePermissionInner(uri, callingPkg, attributionTag,
697 callerToken);
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700698 if (mode != MODE_ALLOWED) {
699 return mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800700 }
Svet Ganov99b60432015-06-27 13:15:22 -0700701
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800702 return noteProxyOp(callingPkg, attributionTag, mWriteOp);
Eugene Susla93519852018-06-13 16:44:31 -0700703 }
704
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800705 private int noteProxyOp(String callingPkg, String attributionTag, int op) {
Eugene Susla93519852018-06-13 16:44:31 -0700706 if (op != AppOpsManager.OP_NONE) {
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700707 int mode = mAppOpsManager.noteProxyOp(op, callingPkg, Binder.getCallingUid(),
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800708 attributionTag, null);
Svet Ganovd8eb8b22019-04-05 18:52:08 -0700709 return mode == MODE_DEFAULT ? MODE_IGNORED : mode;
Svet Ganov99b60432015-06-27 13:15:22 -0700710 }
711
Dianne Hackborn35654b62013-01-14 17:38:02 -0800712 return AppOpsManager.MODE_ALLOWED;
713 }
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700714 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800715
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100716 boolean checkUser(int pid, int uid, Context context) {
Varun Shahaf8cfe32019-08-16 16:11:24 -0700717 if (UserHandle.getUserId(uid) == context.getUserId() || mSingleUser) {
718 return true;
719 }
720 return context.checkPermission(INTERACT_ACROSS_USERS, pid, uid) == PERMISSION_GRANTED
721 || context.checkPermission(INTERACT_ACROSS_USERS_FULL, pid, uid)
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100722 == PERMISSION_GRANTED;
723 }
724
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700725 /**
726 * Verify that calling app holds both the given permission and any app-op
727 * associated with that permission.
728 */
729 private int checkPermissionAndAppOp(String permission, String callingPkg,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800730 @Nullable String attributionTag, IBinder callerToken) {
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700731 if (getContext().checkPermission(permission, Binder.getCallingPid(), Binder.getCallingUid(),
732 callerToken) != PERMISSION_GRANTED) {
733 return MODE_ERRORED;
734 }
735
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800736 return mTransport.noteProxyOp(callingPkg, attributionTag,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700737 AppOpsManager.permissionToOpCode(permission));
Eugene Susla93519852018-06-13 16:44:31 -0700738 }
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700739
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700740 /** {@hide} */
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700741 protected int enforceReadPermissionInner(Uri uri, String callingPkg,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800742 @Nullable String attributionTag, IBinder callerToken) throws SecurityException {
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700743 final Context context = getContext();
744 final int pid = Binder.getCallingPid();
745 final int uid = Binder.getCallingUid();
746 String missingPerm = null;
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700747 int strongestMode = MODE_ALLOWED;
Jeff Sharkey110a6b62012-03-12 11:12:41 -0700748
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700749 if (UserHandle.isSameApp(uid, mMyUid)) {
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700750 return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700751 }
752
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100753 if (mExported && checkUser(pid, uid, context)) {
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700754 final String componentPerm = getReadPermission();
755 if (componentPerm != null) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800756 final int mode = checkPermissionAndAppOp(componentPerm, callingPkg, attributionTag,
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700757 callerToken);
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700758 if (mode == MODE_ALLOWED) {
759 return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700760 } else {
761 missingPerm = componentPerm;
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700762 strongestMode = Math.max(strongestMode, mode);
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700763 }
Jeff Sharkeye5d49332012-03-13 12:13:17 -0700764 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -0700765
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700766 // track if unprotected read is allowed; any denied
767 // <path-permission> below removes this ability
768 boolean allowDefaultRead = (componentPerm == null);
Jeff Sharkey110a6b62012-03-12 11:12:41 -0700769
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700770 final PathPermission[] pps = getPathPermissions();
771 if (pps != null) {
772 final String path = uri.getPath();
773 for (PathPermission pp : pps) {
774 final String pathPerm = pp.getReadPermission();
775 if (pathPerm != null && pp.match(path)) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800776 final int mode = checkPermissionAndAppOp(pathPerm, callingPkg,
777 attributionTag, callerToken);
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700778 if (mode == MODE_ALLOWED) {
779 return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700780 } else {
781 // any denied <path-permission> means we lose
782 // default <provider> access.
783 allowDefaultRead = false;
784 missingPerm = pathPerm;
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700785 strongestMode = Math.max(strongestMode, mode);
Dianne Hackborn2af632f2009-07-08 14:56:37 -0700786 }
787 }
788 }
789 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -0700790
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700791 // if we passed <path-permission> checks above, and no default
792 // <provider> permission, then allow access.
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700793 if (allowDefaultRead) return MODE_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700795
796 // last chance, check against any uri grants
Amith Yamasani7d2d4fd2014-11-05 15:46:09 -0800797 final int callingUserId = UserHandle.getUserId(uid);
798 final Uri userUri = (mSingleUser && !UserHandle.isSameUser(mMyUid, uid))
799 ? maybeAddUserId(uri, callingUserId) : uri;
Dianne Hackbornff170242014-11-19 10:59:01 -0800800 if (context.checkUriPermission(userUri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION,
801 callerToken) == PERMISSION_GRANTED) {
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700802 return MODE_ALLOWED;
803 }
804
805 // If the worst denial we found above was ignored, then pass that
806 // ignored through; otherwise we assume it should be a real error below.
807 if (strongestMode == MODE_IGNORED) {
808 return MODE_IGNORED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700809 }
810
Jeff Sharkeyc0cc2202017-03-21 19:25:34 -0600811 final String suffix;
812 if (android.Manifest.permission.MANAGE_DOCUMENTS.equals(mReadPermission)) {
813 suffix = " requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs";
814 } else if (mExported) {
815 suffix = " requires " + missingPerm + ", or grantUriPermission()";
816 } else {
817 suffix = " requires the provider be exported, or grantUriPermission()";
818 }
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700819 throw new SecurityException("Permission Denial: reading "
820 + ContentProvider.this.getClass().getName() + " uri " + uri + " from pid=" + pid
Jeff Sharkeyc0cc2202017-03-21 19:25:34 -0600821 + ", uid=" + uid + suffix);
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700822 }
823
824 /** {@hide} */
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700825 protected int enforceWritePermissionInner(Uri uri, String callingPkg,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800826 @Nullable String attributionTag, IBinder callerToken) throws SecurityException {
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700827 final Context context = getContext();
828 final int pid = Binder.getCallingPid();
829 final int uid = Binder.getCallingUid();
830 String missingPerm = null;
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700831 int strongestMode = MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700832
833 if (UserHandle.isSameApp(uid, mMyUid)) {
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700834 return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700835 }
836
Nicolas Prevot504d78e2014-06-26 10:07:33 +0100837 if (mExported && checkUser(pid, uid, context)) {
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700838 final String componentPerm = getWritePermission();
839 if (componentPerm != null) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800840 final int mode = checkPermissionAndAppOp(componentPerm, callingPkg,
841 attributionTag, callerToken);
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700842 if (mode == MODE_ALLOWED) {
843 return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700844 } else {
845 missingPerm = componentPerm;
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700846 strongestMode = Math.max(strongestMode, mode);
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700847 }
848 }
849
850 // track if unprotected write is allowed; any denied
851 // <path-permission> below removes this ability
852 boolean allowDefaultWrite = (componentPerm == null);
853
854 final PathPermission[] pps = getPathPermissions();
855 if (pps != null) {
856 final String path = uri.getPath();
857 for (PathPermission pp : pps) {
858 final String pathPerm = pp.getWritePermission();
859 if (pathPerm != null && pp.match(path)) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800860 final int mode = checkPermissionAndAppOp(pathPerm, callingPkg,
861 attributionTag, callerToken);
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700862 if (mode == MODE_ALLOWED) {
863 return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700864 } else {
865 // any denied <path-permission> means we lose
866 // default <provider> access.
867 allowDefaultWrite = false;
868 missingPerm = pathPerm;
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700869 strongestMode = Math.max(strongestMode, mode);
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700870 }
871 }
872 }
873 }
874
875 // if we passed <path-permission> checks above, and no default
876 // <provider> permission, then allow access.
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700877 if (allowDefaultWrite) return MODE_ALLOWED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700878 }
879
880 // last chance, check against any uri grants
Dianne Hackbornff170242014-11-19 10:59:01 -0800881 if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_WRITE_URI_PERMISSION,
882 callerToken) == PERMISSION_GRANTED) {
Jeff Sharkey0e621c32015-07-24 15:10:20 -0700883 return MODE_ALLOWED;
884 }
885
886 // If the worst denial we found above was ignored, then pass that
887 // ignored through; otherwise we assume it should be a real error below.
888 if (strongestMode == MODE_IGNORED) {
889 return MODE_IGNORED;
Jeff Sharkey8a2998e2013-10-31 14:55:44 -0700890 }
891
892 final String failReason = mExported
893 ? " requires " + missingPerm + ", or grantUriPermission()"
894 : " requires the provider be exported, or grantUriPermission()";
895 throw new SecurityException("Permission Denial: writing "
896 + ContentProvider.this.getClass().getName() + " uri " + uri + " from pid=" + pid
897 + ", uid=" + uid + failReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -0700901 * Retrieves the Context this provider is running in. Only available once
Christopher Tate2bc6eb82013-01-03 12:04:08 -0800902 * {@link #onCreate} has been called -- this will return {@code null} in the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 * constructor.
904 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700905 public final @Nullable Context getContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 return mContext;
907 }
908
909 /**
Pinyao Ting6dcd1132019-10-16 17:13:34 -0700910 * Retrieves a Non-Nullable Context this provider is running in, this is intended to be called
911 * after {@link #onCreate}. When called before context was created, an IllegalStateException
912 * will be thrown.
913 * <p>
914 * Note A provider must be declared in the manifest and created automatically by the system,
915 * and context is only available after {@link #onCreate} is called.
916 */
917 @NonNull
918 public final Context requireContext() {
919 final Context ctx = getContext();
920 if (ctx == null) {
921 throw new IllegalStateException("Cannot find context from the provider.");
922 }
923 return ctx;
924 }
925
926 /**
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700927 * Set the calling package/feature, returning the current value (or {@code null})
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700928 * which can be used later to restore the previous state.
929 */
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700930 private Pair<String, String> setCallingPackage(Pair<String, String> callingPackage) {
931 final Pair<String, String> original = mCallingPackage.get();
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700932 mCallingPackage.set(callingPackage);
Jeff Sharkey951f99b2019-05-15 19:19:59 -0600933 onCallingPackageChanged();
Jeff Sharkey72e2e352013-09-09 18:52:48 -0700934 return original;
935 }
936
937 /**
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700938 * Return the package name of the caller that initiated the request being
939 * processed on the current thread. The returned package will have been
940 * verified to belong to the calling UID. Returns {@code null} if not
941 * currently processing a request.
942 * <p>
943 * This will always return {@code null} when processing
944 * {@link #getType(Uri)} or {@link #getStreamTypes(Uri, String)} requests.
945 *
946 * @see Binder#getCallingUid()
947 * @see Context#grantUriPermission(String, Uri, int)
948 * @throws SecurityException if the calling package doesn't belong to the
949 * calling UID.
950 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700951 public final @Nullable String getCallingPackage() {
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700952 final Pair<String, String> pkg = mCallingPackage.get();
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700953 if (pkg != null) {
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700954 mTransport.mAppOpsManager.checkPackage(Binder.getCallingUid(), pkg.first);
955 return pkg.first;
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700956 }
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700957
958 return null;
959 }
960
961 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800962 * Return the attribution tag of the caller that initiated the request being
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700963 * processed on the current thread. Returns {@code null} if not currently processing
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800964 * a request of the request is for the default attribution.
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700965 * <p>
966 * This will always return {@code null} when processing
967 * {@link #getType(Uri)} or {@link #getStreamTypes(Uri, String)} requests.
968 *
969 * @see #getCallingPackage
970 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800971 public final @Nullable String getCallingAttributionTag() {
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700972 final Pair<String, String> pkg = mCallingPackage.get();
973 if (pkg != null) {
974 return pkg.second;
975 }
976
977 return null;
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700978 }
979
Jeff Sharkey197fe1f2020-01-07 22:06:37 -0700980 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800981 * @removed
982 */
983 @Deprecated
984 public final @Nullable String getCallingFeatureId() {
985 return getCallingAttributionTag();
986 }
987
988 /**
Jeff Sharkey197fe1f2020-01-07 22:06:37 -0700989 * Return the package name of the caller that initiated the request being
990 * processed on the current thread. The returned package will have
991 * <em>not</em> been verified to belong to the calling UID. Returns
992 * {@code null} if not currently processing a request.
993 * <p>
994 * This will always return {@code null} when processing
995 * {@link #getType(Uri)} or {@link #getStreamTypes(Uri, String)} requests.
996 *
997 * @see Binder#getCallingUid()
998 * @see Context#grantUriPermission(String, Uri, int)
999 */
Jeff Sharkey951f99b2019-05-15 19:19:59 -06001000 public final @Nullable String getCallingPackageUnchecked() {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001001 final Pair<String, String> pkg = mCallingPackage.get();
1002 if (pkg != null) {
1003 return pkg.first;
1004 }
1005
1006 return null;
Jeff Sharkey951f99b2019-05-15 19:19:59 -06001007 }
1008
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07001009 /**
1010 * Called whenever the value of {@link #getCallingPackage()} changes, giving
1011 * the provider an opportunity to invalidate any security related caching it
1012 * may be performing.
1013 * <p>
1014 * This typically happens when a {@link ContentProvider} makes a nested call
1015 * back into itself when already processing a call from a remote process.
1016 */
Jeff Sharkey951f99b2019-05-15 19:19:59 -06001017 public void onCallingPackageChanged() {
1018 }
1019
Jeff Sharkey911d7f42013-09-05 18:11:45 -07001020 /**
Jeff Sharkeyd2b64d72018-10-19 15:40:03 -06001021 * Opaque token representing the identity of an incoming IPC.
1022 */
1023 public final class CallingIdentity {
1024 /** {@hide} */
1025 public final long binderToken;
1026 /** {@hide} */
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001027 public final Pair<String, String> callingPackage;
Jeff Sharkeyd2b64d72018-10-19 15:40:03 -06001028
1029 /** {@hide} */
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001030 public CallingIdentity(long binderToken, Pair<String, String> callingPackage) {
Jeff Sharkeyd2b64d72018-10-19 15:40:03 -06001031 this.binderToken = binderToken;
1032 this.callingPackage = callingPackage;
1033 }
1034 }
1035
1036 /**
1037 * Reset the identity of the incoming IPC on the current thread.
1038 * <p>
1039 * Internally this calls {@link Binder#clearCallingIdentity()} and also
1040 * clears any value stored in {@link #getCallingPackage()}.
1041 *
1042 * @return Returns an opaque token that can be used to restore the original
1043 * calling identity by passing it to
1044 * {@link #restoreCallingIdentity}.
1045 */
1046 public final @NonNull CallingIdentity clearCallingIdentity() {
1047 return new CallingIdentity(Binder.clearCallingIdentity(), setCallingPackage(null));
1048 }
1049
1050 /**
1051 * Restore the identity of the incoming IPC on the current thread back to a
1052 * previously identity that was returned by {@link #clearCallingIdentity}.
1053 * <p>
1054 * Internally this calls {@link Binder#restoreCallingIdentity(long)} and
1055 * also restores any value stored in {@link #getCallingPackage()}.
1056 */
1057 public final void restoreCallingIdentity(@NonNull CallingIdentity identity) {
1058 Binder.restoreCallingIdentity(identity.binderToken);
1059 mCallingPackage.set(identity.callingPackage);
1060 }
1061
1062 /**
Nicolas Prevotf300bab2014-08-07 19:23:17 +01001063 * Change the authorities of the ContentProvider.
1064 * This is normally set for you from its manifest information when the provider is first
1065 * created.
1066 * @hide
1067 * @param authorities the semi-colon separated authorities of the ContentProvider.
1068 */
1069 protected final void setAuthorities(String authorities) {
Nicolas Prevot6e412ad2014-09-08 18:26:55 +01001070 if (authorities != null) {
1071 if (authorities.indexOf(';') == -1) {
1072 mAuthority = authorities;
1073 mAuthorities = null;
1074 } else {
1075 mAuthority = null;
1076 mAuthorities = authorities.split(";");
1077 }
Nicolas Prevotf300bab2014-08-07 19:23:17 +01001078 }
1079 }
1080
1081 /** @hide */
1082 protected final boolean matchesOurAuthorities(String authority) {
1083 if (mAuthority != null) {
1084 return mAuthority.equals(authority);
1085 }
Nicolas Prevot6e412ad2014-09-08 18:26:55 +01001086 if (mAuthorities != null) {
1087 int length = mAuthorities.length;
1088 for (int i = 0; i < length; i++) {
1089 if (mAuthorities[i].equals(authority)) return true;
1090 }
Nicolas Prevotf300bab2014-08-07 19:23:17 +01001091 }
1092 return false;
1093 }
1094
1095
1096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 * Change the permission required to read data from the content
1098 * provider. This is normally set for you from its manifest information
1099 * when the provider is first created.
1100 *
1101 * @param permission Name of the permission required for read-only access.
1102 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001103 protected final void setReadPermission(@Nullable String permission) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 mReadPermission = permission;
1105 }
1106
1107 /**
1108 * Return the name of the permission required for read-only access to
1109 * this content provider. This method can be called from multiple
1110 * threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001111 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1112 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001114 public final @Nullable String getReadPermission() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 return mReadPermission;
1116 }
1117
1118 /**
1119 * Change the permission required to read and write data in the content
1120 * provider. This is normally set for you from its manifest information
1121 * when the provider is first created.
1122 *
1123 * @param permission Name of the permission required for read/write access.
1124 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001125 protected final void setWritePermission(@Nullable String permission) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 mWritePermission = permission;
1127 }
1128
1129 /**
1130 * Return the name of the permission required for read/write access to
1131 * this content provider. This method can be called from multiple
1132 * threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001133 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1134 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001136 public final @Nullable String getWritePermission() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 return mWritePermission;
1138 }
1139
1140 /**
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001141 * Change the path-based permission required to read and/or write data in
1142 * the content provider. This is normally set for you from its manifest
1143 * information when the provider is first created.
1144 *
1145 * @param permissions Array of path permission descriptions.
1146 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001147 protected final void setPathPermissions(@Nullable PathPermission[] permissions) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001148 mPathPermissions = permissions;
1149 }
1150
1151 /**
1152 * Return the path-based permissions required for read and/or write access to
1153 * this content provider. This method can be called from multiple
1154 * threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001155 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1156 * and Threads</a>.
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001157 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001158 public final @Nullable PathPermission[] getPathPermissions() {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001159 return mPathPermissions;
1160 }
1161
Dianne Hackborn35654b62013-01-14 17:38:02 -08001162 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001163 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08001164 public final void setAppOps(int readOp, int writeOp) {
Dianne Hackborn7e6f9762013-02-26 13:35:11 -08001165 if (!mNoPerms) {
Dianne Hackborn7e6f9762013-02-26 13:35:11 -08001166 mTransport.mReadOp = readOp;
1167 mTransport.mWriteOp = writeOp;
1168 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001169 }
1170
Dianne Hackborn961321f2013-02-05 17:22:41 -08001171 /** @hide */
1172 public AppOpsManager getAppOpsManager() {
1173 return mTransport.mAppOpsManager;
1174 }
1175
Jeff Sharkeybffd2502019-02-28 16:39:12 -07001176 /** @hide */
1177 public final void setTransportLoggingEnabled(boolean enabled) {
Varun Shahd5395532019-08-26 18:07:48 -07001178 if (mTransport == null) {
1179 return;
1180 }
1181 if (enabled) {
1182 mTransport.mInterface = new LoggingContentInterface(getClass().getSimpleName(), this);
1183 } else {
1184 mTransport.mInterface = this;
Jeff Sharkeybffd2502019-02-28 16:39:12 -07001185 }
1186 }
1187
Dianne Hackborn2af632f2009-07-08 14:56:37 -07001188 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001189 * Implement this to initialize your content provider on startup.
1190 * This method is called for all registered content providers on the
1191 * application main thread at application launch time. It must not perform
1192 * lengthy operations, or application startup will be delayed.
1193 *
1194 * <p>You should defer nontrivial initialization (such as opening,
1195 * upgrading, and scanning databases) until the content provider is used
1196 * (via {@link #query}, {@link #insert}, etc). Deferred initialization
1197 * keeps application startup fast, avoids unnecessary work if the provider
1198 * turns out not to be needed, and stops database errors (such as a full
1199 * disk) from halting application launch.
1200 *
Dan Egnor17876aa2010-07-28 12:28:04 -07001201 * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper}
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001202 * is a helpful utility class that makes it easy to manage databases,
1203 * and will automatically defer opening until first use. If you do use
1204 * SQLiteOpenHelper, make sure to avoid calling
1205 * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or
1206 * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase}
1207 * from this method. (Instead, override
1208 * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the
1209 * database when it is first opened.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 *
1211 * @return true if the provider was successfully loaded, false otherwise
1212 */
1213 public abstract boolean onCreate();
1214
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001215 /**
1216 * {@inheritDoc}
1217 * This method is always called on the application main thread, and must
1218 * not perform lengthy operations.
1219 *
1220 * <p>The default content provider implementation does nothing.
1221 * Override this method to take appropriate action.
1222 * (Content providers do not usually care about things like screen
1223 * orientation, but may want to know about locale changes.)
1224 */
Steve McKayea93fe72016-12-02 11:35:35 -08001225 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 public void onConfigurationChanged(Configuration newConfig) {
1227 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001228
1229 /**
1230 * {@inheritDoc}
1231 * This method is always called on the application main thread, and must
1232 * not perform lengthy operations.
1233 *
1234 * <p>The default content provider implementation does nothing.
1235 * Subclasses may override this method to take appropriate action.
1236 */
Steve McKayea93fe72016-12-02 11:35:35 -08001237 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 public void onLowMemory() {
1239 }
1240
Steve McKayea93fe72016-12-02 11:35:35 -08001241 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001242 public void onTrimMemory(int level) {
1243 }
1244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001246 * Implement this to handle query requests from clients.
Steve McKay29c3f682016-12-16 14:52:59 -08001247 *
1248 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher should override
1249 * {@link #query(Uri, String[], Bundle, CancellationSignal)} and provide a stub
1250 * implementation of this method.
1251 *
1252 * <p>This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001253 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1254 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 * <p>
1256 * Example client call:<p>
1257 * <pre>// Request a specific record.
1258 * Cursor managedCursor = managedQuery(
Alan Jones81a476f2009-05-21 12:32:17 +10001259 ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 projection, // Which columns to return.
1261 null, // WHERE clause.
Alan Jones81a476f2009-05-21 12:32:17 +10001262 null, // WHERE clause value substitution
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 People.NAME + " ASC"); // Sort order.</pre>
1264 * Example implementation:<p>
1265 * <pre>// SQLiteQueryBuilder is a helper class that creates the
1266 // proper SQL syntax for us.
1267 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
1268
1269 // Set the table we're querying.
1270 qBuilder.setTables(DATABASE_TABLE_NAME);
1271
1272 // If the query ends in a specific record number, we're
1273 // being asked for a specific record, so set the
1274 // WHERE clause in our query.
1275 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
1276 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
1277 }
1278
1279 // Make the query.
1280 Cursor c = qBuilder.query(mDb,
1281 projection,
1282 selection,
1283 selectionArgs,
1284 groupBy,
1285 having,
1286 sortOrder);
1287 c.setNotificationUri(getContext().getContentResolver(), uri);
1288 return c;</pre>
1289 *
1290 * @param uri The URI to query. This will be the full URI sent by the client;
Alan Jones81a476f2009-05-21 12:32:17 +10001291 * if the client is requesting a specific record, the URI will end in a record number
1292 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
1293 * that _id value.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 * @param projection The list of columns to put into the cursor. If
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001295 * {@code null} all columns are included.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 * @param selection A selection criteria to apply when filtering rows.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001297 * If {@code null} then all rows are included.
Alan Jones81a476f2009-05-21 12:32:17 +10001298 * @param selectionArgs You may include ?s in selection, which will be replaced by
1299 * the values from selectionArgs, in order that they appear in the selection.
1300 * The values will be bound as Strings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 * @param sortOrder How the rows in the cursor should be sorted.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001302 * If {@code null} then the provider is free to define the sort order.
1303 * @return a Cursor or {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001305 public abstract @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection,
1306 @Nullable String selection, @Nullable String[] selectionArgs,
1307 @Nullable String sortOrder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
Fred Quintana5bba6322009-10-05 14:21:12 -07001309 /**
Jeff Brown4c1241d2012-02-02 17:05:00 -08001310 * Implement this to handle query requests from clients with support for cancellation.
Steve McKay29c3f682016-12-16 14:52:59 -08001311 *
1312 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher should override
1313 * {@link #query(Uri, String[], Bundle, CancellationSignal)} instead of this method.
1314 *
1315 * <p>This method can be called from multiple threads, as described in
Jeff Brown75ea64f2012-01-25 19:37:13 -08001316 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1317 * and Threads</a>.
1318 * <p>
1319 * Example client call:<p>
1320 * <pre>// Request a specific record.
1321 * Cursor managedCursor = managedQuery(
1322 ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
1323 projection, // Which columns to return.
1324 null, // WHERE clause.
1325 null, // WHERE clause value substitution
1326 People.NAME + " ASC"); // Sort order.</pre>
1327 * Example implementation:<p>
1328 * <pre>// SQLiteQueryBuilder is a helper class that creates the
1329 // proper SQL syntax for us.
1330 SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
1331
1332 // Set the table we're querying.
1333 qBuilder.setTables(DATABASE_TABLE_NAME);
1334
1335 // If the query ends in a specific record number, we're
1336 // being asked for a specific record, so set the
1337 // WHERE clause in our query.
1338 if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
1339 qBuilder.appendWhere("_id=" + uri.getPathLeafId());
1340 }
1341
1342 // Make the query.
1343 Cursor c = qBuilder.query(mDb,
1344 projection,
1345 selection,
1346 selectionArgs,
1347 groupBy,
1348 having,
1349 sortOrder);
1350 c.setNotificationUri(getContext().getContentResolver(), uri);
1351 return c;</pre>
1352 * <p>
1353 * If you implement this method then you must also implement the version of
Jeff Brown4c1241d2012-02-02 17:05:00 -08001354 * {@link #query(Uri, String[], String, String[], String)} that does not take a cancellation
1355 * signal to ensure correct operation on older versions of the Android Framework in
1356 * which the cancellation signal overload was not available.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001357 *
1358 * @param uri The URI to query. This will be the full URI sent by the client;
1359 * if the client is requesting a specific record, the URI will end in a record number
1360 * that the implementation should parse and add to a WHERE or HAVING clause, specifying
1361 * that _id value.
1362 * @param projection The list of columns to put into the cursor. If
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001363 * {@code null} all columns are included.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001364 * @param selection A selection criteria to apply when filtering rows.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001365 * If {@code null} then all rows are included.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001366 * @param selectionArgs You may include ?s in selection, which will be replaced by
1367 * the values from selectionArgs, in order that they appear in the selection.
1368 * The values will be bound as Strings.
1369 * @param sortOrder How the rows in the cursor should be sorted.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001370 * If {@code null} then the provider is free to define the sort order.
1371 * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if none.
Jeff Sharkey67f9d502017-08-05 13:49:13 -06001372 * If the operation is canceled, then {@link android.os.OperationCanceledException} will be thrown
Jeff Brown75ea64f2012-01-25 19:37:13 -08001373 * when the query is executed.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001374 * @return a Cursor or {@code null}.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001375 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001376 public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection,
1377 @Nullable String selection, @Nullable String[] selectionArgs,
1378 @Nullable String sortOrder, @Nullable CancellationSignal cancellationSignal) {
Jeff Brown75ea64f2012-01-25 19:37:13 -08001379 return query(uri, projection, selection, selectionArgs, sortOrder);
1380 }
1381
1382 /**
Steve McKayea93fe72016-12-02 11:35:35 -08001383 * Implement this to handle query requests where the arguments are packed into a {@link Bundle}.
1384 * Arguments may include traditional SQL style query arguments. When present these
1385 * should be handled according to the contract established in
Andrew Solovay27e43462018-12-12 15:38:06 -08001386 * {@link #query(Uri, String[], String, String[], String, CancellationSignal)}.
Steve McKayea93fe72016-12-02 11:35:35 -08001387 *
1388 * <p>Traditional SQL arguments can be found in the bundle using the following keys:
Andrew Solovay27e43462018-12-12 15:38:06 -08001389 * <li>{@link android.content.ContentResolver#QUERY_ARG_SQL_SELECTION}
1390 * <li>{@link android.content.ContentResolver#QUERY_ARG_SQL_SELECTION_ARGS}
1391 * <li>{@link android.content.ContentResolver#QUERY_ARG_SQL_SORT_ORDER}
Steve McKayea93fe72016-12-02 11:35:35 -08001392 *
Steve McKay76b27702017-04-24 12:07:53 -07001393 * <p>This method can be called from multiple threads, as described in
1394 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1395 * and Threads</a>.
1396 *
1397 * <p>
1398 * Example client call:<p>
1399 * <pre>// Request 20 records starting at row index 30.
1400 Bundle queryArgs = new Bundle();
1401 queryArgs.putInt(ContentResolver.QUERY_ARG_OFFSET, 30);
1402 queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20);
1403
1404 Cursor cursor = getContentResolver().query(
1405 contentUri, // Content Uri is specific to individual content providers.
1406 projection, // String[] describing which columns to return.
1407 queryArgs, // Query arguments.
1408 null); // Cancellation signal.</pre>
1409 *
1410 * Example implementation:<p>
1411 * <pre>
1412
1413 int recordsetSize = 0x1000; // Actual value is implementation specific.
1414 queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY; // ensure queryArgs is non-null
1415
1416 int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0);
1417 int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MIN_VALUE);
1418
1419 MatrixCursor c = new MatrixCursor(PROJECTION, limit);
1420
1421 // Calculate the number of items to include in the cursor.
1422 int numItems = MathUtils.constrain(recordsetSize - offset, 0, limit);
1423
1424 // Build the paged result set....
1425 for (int i = offset; i < offset + numItems; i++) {
1426 // populate row from your data.
1427 }
1428
1429 Bundle extras = new Bundle();
1430 c.setExtras(extras);
1431
1432 // Any QUERY_ARG_* key may be included if honored.
1433 // In an actual implementation, include only keys that are both present in queryArgs
1434 // and reflected in the Cursor output. For example, if QUERY_ARG_OFFSET were included
1435 // in queryArgs, but was ignored because it contained an invalid value (like –273),
1436 // then QUERY_ARG_OFFSET should be omitted.
1437 extras.putStringArray(ContentResolver.EXTRA_HONORED_ARGS, new String[] {
1438 ContentResolver.QUERY_ARG_OFFSET,
1439 ContentResolver.QUERY_ARG_LIMIT
1440 });
1441
1442 extras.putInt(ContentResolver.EXTRA_TOTAL_COUNT, recordsetSize);
1443
1444 cursor.setNotificationUri(getContext().getContentResolver(), uri);
1445
1446 return cursor;</pre>
1447 * <p>
Andrew Solovay27e43462018-12-12 15:38:06 -08001448 * See {@link #query(Uri, String[], String, String[], String, CancellationSignal)}
1449 * for implementation details.
Steve McKayea93fe72016-12-02 11:35:35 -08001450 *
1451 * @param uri The URI to query. This will be the full URI sent by the client.
Steve McKayea93fe72016-12-02 11:35:35 -08001452 * @param projection The list of columns to put into the cursor.
1453 * If {@code null} provide a default set of columns.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001454 * @param queryArgs A Bundle containing additional information necessary for
1455 * the operation. Arguments may include SQL style arguments, such
1456 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1457 * the documentation for each individual provider will indicate
1458 * which arguments they support.
Steve McKayea93fe72016-12-02 11:35:35 -08001459 * @param cancellationSignal A signal to cancel the operation in progress,
1460 * or {@code null}.
1461 * @return a Cursor or {@code null}.
1462 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001463 @Override
Steve McKayea93fe72016-12-02 11:35:35 -08001464 public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection,
1465 @Nullable Bundle queryArgs, @Nullable CancellationSignal cancellationSignal) {
1466 queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY;
Steve McKay29c3f682016-12-16 14:52:59 -08001467
Steve McKayd7ece9f2017-01-12 16:59:59 -08001468 // if client doesn't supply an SQL sort order argument, attempt to build one from
1469 // QUERY_ARG_SORT* arguments.
Steve McKay29c3f682016-12-16 14:52:59 -08001470 String sortClause = queryArgs.getString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER);
Steve McKay29c3f682016-12-16 14:52:59 -08001471 if (sortClause == null && queryArgs.containsKey(ContentResolver.QUERY_ARG_SORT_COLUMNS)) {
1472 sortClause = ContentResolver.createSqlSortClause(queryArgs);
1473 }
1474
Steve McKayea93fe72016-12-02 11:35:35 -08001475 return query(
1476 uri,
1477 projection,
Steve McKay29c3f682016-12-16 14:52:59 -08001478 queryArgs.getString(ContentResolver.QUERY_ARG_SQL_SELECTION),
1479 queryArgs.getStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS),
1480 sortClause,
Steve McKayea93fe72016-12-02 11:35:35 -08001481 cancellationSignal);
1482 }
1483
1484 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001485 * Implement this to handle requests for the MIME type of the data at the
1486 * given URI. The returned MIME type should start with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 * <code>vnd.android.cursor.item</code> for a single record,
1488 * or <code>vnd.android.cursor.dir/</code> for multiple items.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001489 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001490 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1491 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 *
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001493 * <p>Note that there are no permissions needed for an application to
1494 * access this information; if your content provider requires read and/or
1495 * write permissions, or is not exported, all applications can still call
1496 * this method regardless of their access permissions. This allows them
1497 * to retrieve the MIME type for a URI when dispatching intents.
1498 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * @param uri the URI to query.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001500 * @return a MIME type string, or {@code null} if there is no type.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001502 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001503 public abstract @Nullable String getType(@NonNull Uri uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504
1505 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001506 * Implement this to support canonicalization of URIs that refer to your
1507 * content provider. A canonical URI is one that can be transported across
1508 * devices, backup/restore, and other contexts, and still be able to refer
1509 * to the same data item. Typically this is implemented by adding query
1510 * params to the URI allowing the content provider to verify that an incoming
1511 * canonical URI references the same data as it was originally intended for and,
1512 * if it doesn't, to find that data (if it exists) in the current environment.
1513 *
1514 * <p>For example, if the content provider holds people and a normal URI in it
1515 * is created with a row index into that people database, the cananical representation
1516 * may have an additional query param at the end which specifies the name of the
1517 * person it is intended for. Later calls into the provider with that URI will look
1518 * up the row of that URI's base index and, if it doesn't match or its entry's
1519 * name doesn't match the name in the query param, perform a query on its database
1520 * to find the correct row to operate on.</p>
1521 *
1522 * <p>If you implement support for canonical URIs, <b>all</b> incoming calls with
1523 * URIs (including this one) must perform this verification and recovery of any
1524 * canonical URIs they receive. In addition, you must also implement
1525 * {@link #uncanonicalize} to strip the canonicalization of any of these URIs.</p>
1526 *
1527 * <p>The default implementation of this method returns null, indicating that
1528 * canonical URIs are not supported.</p>
1529 *
1530 * @param url The Uri to canonicalize.
1531 *
1532 * @return Return the canonical representation of <var>url</var>, or null if
1533 * canonicalization of that Uri is not supported.
1534 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001535 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001536 public @Nullable Uri canonicalize(@NonNull Uri url) {
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001537 return null;
1538 }
1539
1540 /**
1541 * Remove canonicalization from canonical URIs previously returned by
1542 * {@link #canonicalize}. For example, if your implementation is to add
1543 * a query param to canonicalize a URI, this method can simply trip any
1544 * query params on the URI. The default implementation always returns the
1545 * same <var>url</var> that was passed in.
1546 *
1547 * @param url The Uri to remove any canonicalization from.
1548 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001549 * @return Return the non-canonical representation of <var>url</var>, return
1550 * the <var>url</var> as-is if there is nothing to do, or return null if
1551 * the data identified by the canonical representation can not be found in
1552 * the current environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001553 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001554 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001555 public @Nullable Uri uncanonicalize(@NonNull Uri url) {
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001556 return url;
1557 }
1558
1559 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001560 * Implement this to support refresh of content identified by {@code uri}.
1561 * By default, this method returns false; providers who wish to implement
1562 * this should return true to signal the client that the provider has tried
1563 * refreshing with its own implementation.
Ben Lin1cf454f2016-11-10 13:50:54 -08001564 * <p>
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001565 * This allows clients to request an explicit refresh of content identified
1566 * by {@code uri}.
Ben Lin1cf454f2016-11-10 13:50:54 -08001567 * <p>
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001568 * Client code should only invoke this method when there is a strong
1569 * indication (such as a user initiated pull to refresh gesture) that the
1570 * content is stale.
Ben Lin1cf454f2016-11-10 13:50:54 -08001571 * <p>
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001572 * Remember to send
1573 * {@link ContentResolver#notifyChange(Uri, android.database.ContentObserver)}
Ben Lin1cf454f2016-11-10 13:50:54 -08001574 * notifications when content changes.
1575 *
1576 * @param uri The Uri identifying the data to refresh.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001577 * @param extras Additional options from the client. The definitions of
1578 * these are specific to the content provider being called.
1579 * @param cancellationSignal A signal to cancel the operation in progress,
1580 * or {@code null} if none. For example, if you called refresh on
1581 * a particular uri, you should call
1582 * {@link CancellationSignal#throwIfCanceled()} to check whether
1583 * the client has canceled the refresh request.
Ben Lin1cf454f2016-11-10 13:50:54 -08001584 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -08001585 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001586 @Override
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001587 public boolean refresh(Uri uri, @Nullable Bundle extras,
Ben Lin1cf454f2016-11-10 13:50:54 -08001588 @Nullable CancellationSignal cancellationSignal) {
1589 return false;
1590 }
1591
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07001592 /**
1593 * Perform a detailed internal check on a {@link Uri} to determine if a UID
1594 * is able to access it with specific mode flags.
1595 * <p>
1596 * This method is typically used when the provider implements more dynamic
1597 * access controls that cannot be expressed with {@code <path-permission>}
1598 * style static rules.
Jeff Sharkeyce8a353b2020-03-19 21:41:57 -06001599 * <p>
1600 * Because validation of these dynamic access controls has significant
1601 * system health impact, this feature is only available to providers that
1602 * are built into the system.
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07001603 *
1604 * @param uri the {@link Uri} to perform an access check on.
1605 * @param uid the UID to check the permission for.
1606 * @param modeFlags the access flags to use for the access check, such as
1607 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION}.
1608 * @return {@link PackageManager#PERMISSION_GRANTED} if access is allowed,
1609 * otherwise {@link PackageManager#PERMISSION_DENIED}.
1610 * @hide
1611 */
Jeff Sharkey9edef252019-05-20 14:00:17 -06001612 @Override
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07001613 @SystemApi
Jeff Sharkey9edef252019-05-20 14:00:17 -06001614 public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) {
1615 return PackageManager.PERMISSION_DENIED;
1616 }
1617
Ben Lin1cf454f2016-11-10 13:50:54 -08001618 /**
Dianne Hackbornd7960d12013-01-29 18:55:48 -08001619 * @hide
1620 * Implementation when a caller has performed an insert on the content
1621 * provider, but that call has been rejected for the operation given
1622 * to {@link #setAppOps(int, int)}. The default implementation simply
1623 * returns a dummy URI that is the base URI with a 0 path element
1624 * appended.
1625 */
1626 public Uri rejectInsert(Uri uri, ContentValues values) {
1627 // If not allowed, we need to return some reasonable URI. Maybe the
1628 // content provider should be responsible for this, but for now we
1629 // will just return the base URI with a dummy '0' tagged on to it.
1630 // You shouldn't be able to read if you can't write, anyway, so it
1631 // shouldn't matter much what is returned.
1632 return uri.buildUpon().appendPath("0").build();
1633 }
1634
1635 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001636 * Implement this to handle requests to insert a new row. As a courtesy,
1637 * call
1638 * {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver)
1639 * notifyChange()} after inserting. This method can be called from multiple
1640 * threads, as described in <a href="
1641 * {@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
Scott Main7aee61f2011-02-08 11:25:01 -08001642 * and Threads</a>.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001643 *
Varun Shah8c80a3f2019-10-16 12:21:19 -07001644 * @param uri The content:// URI of the insertion request.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 * @param values A set of column_name/value pairs to add to the database.
1646 * @return The URI for the newly inserted item.
1647 */
Jeff Sharkey34796bd2015-06-11 21:55:32 -07001648 public abstract @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649
1650 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001651 * Implement this to handle requests to insert a new row. As a courtesy,
1652 * call
1653 * {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver)
1654 * notifyChange()} after inserting. This method can be called from multiple
1655 * threads, as described in <a href="
1656 * {@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1657 * and Threads</a>.
1658 *
1659 * @param uri The content:// URI of the insertion request.
1660 * @param values A set of column_name/value pairs to add to the database.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001661 * @param extras A Bundle containing additional information necessary for
1662 * the operation. Arguments may include SQL style arguments, such
1663 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1664 * the documentation for each individual provider will indicate
1665 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001666 * @return The URI for the newly inserted item.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001667 * @throws IllegalArgumentException if the provider doesn't support one of
1668 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001669 */
1670 @Override
1671 public @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values,
1672 @Nullable Bundle extras) {
1673 return insert(uri, values);
1674 }
1675
1676 /**
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001677 * Override this to handle requests to insert a set of new rows, or the
1678 * default implementation will iterate over the values and call
1679 * {@link #insert} on each of them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()}
1681 * after inserting.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001682 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001683 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1684 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 *
1686 * @param uri The content:// URI of the insertion request.
1687 * @param values An array of sets of column_name/value pairs to add to the database.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001688 * This must not be {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 * @return The number of values that were inserted.
1690 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001691 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001692 public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 int numValues = values.length;
1694 for (int i = 0; i < numValues; i++) {
1695 insert(uri, values[i]);
1696 }
1697 return numValues;
1698 }
1699
1700 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001701 * Implement this to handle requests to delete one or more rows. The
1702 * implementation should apply the selection clause when performing
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001703 * deletion, allowing the operation to affect multiple rows in a directory.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001704 * As a courtesy, call
1705 * {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver)
1706 * notifyChange()} after deleting. This method can be called from multiple
1707 * threads, as described in <a href="
1708 * {@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
Scott Main7aee61f2011-02-08 11:25:01 -08001709 * and Threads</a>.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001710 * <p>
1711 * The implementation is responsible for parsing out a row ID at the end of
1712 * the URI, if a specific row is being deleted. That is, the client would
1713 * pass in <code>content://contacts/people/22</code> and the implementation
1714 * is responsible for parsing the record number (22) when creating a SQL
1715 * statement.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 *
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001717 * @param uri The full URI to query, including a row ID (if a specific
1718 * record is requested).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 * @param selection An optional restriction to apply to rows when deleting.
1720 * @return The number of rows affected.
1721 * @throws SQLException
1722 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001723 public abstract int delete(@NonNull Uri uri, @Nullable String selection,
1724 @Nullable String[] selectionArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725
1726 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001727 * Implement this to handle requests to delete one or more rows. The
1728 * implementation should apply the selection clause when performing
1729 * deletion, allowing the operation to affect multiple rows in a directory.
1730 * As a courtesy, call
1731 * {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver)
1732 * notifyChange()} after deleting. This method can be called from multiple
1733 * threads, as described in <a href="
1734 * {@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1735 * and Threads</a>.
1736 * <p>
1737 * The implementation is responsible for parsing out a row ID at the end of
1738 * the URI, if a specific row is being deleted. That is, the client would
1739 * pass in <code>content://contacts/people/22</code> and the implementation
1740 * is responsible for parsing the record number (22) when creating a SQL
1741 * statement.
1742 *
1743 * @param uri The full URI to query, including a row ID (if a specific
1744 * record is requested).
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001745 * @param extras A Bundle containing additional information necessary for
1746 * the operation. Arguments may include SQL style arguments, such
1747 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1748 * the documentation for each individual provider will indicate
1749 * which arguments they support.
1750 * @throws IllegalArgumentException if the provider doesn't support one of
1751 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001752 * @throws SQLException
1753 */
1754 @Override
1755 public int delete(@NonNull Uri uri, @Nullable Bundle extras) {
1756 extras = (extras != null) ? extras : Bundle.EMPTY;
1757 return delete(uri,
1758 extras.getString(ContentResolver.QUERY_ARG_SQL_SELECTION),
1759 extras.getStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS));
1760 }
1761
1762 /**
1763 * Implement this to handle requests to update one or more rows. The
1764 * implementation should update all rows matching the selection to set the
1765 * columns according to the provided values map. As a courtesy, call
1766 * {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver)
1767 * notifyChange()} after updating. This method can be called from multiple
1768 * threads, as described in <a href="
1769 * {@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
Scott Main7aee61f2011-02-08 11:25:01 -08001770 * and Threads</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 *
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001772 * @param uri The URI to query. This can potentially have a record ID if
1773 * this is an update request for a specific record.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08001774 * @param values A set of column_name/value pairs to update in the database.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 * @param selection An optional filter to match rows to update.
1776 * @return the number of rows affected.
1777 */
Jeff Sharkey34796bd2015-06-11 21:55:32 -07001778 public abstract int update(@NonNull Uri uri, @Nullable ContentValues values,
Jeff Sharkey673db442015-06-11 19:30:57 -07001779 @Nullable String selection, @Nullable String[] selectionArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780
1781 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001782 * Implement this to handle requests to update one or more rows. The
1783 * implementation should update all rows matching the selection to set the
1784 * columns according to the provided values map. As a courtesy, call
1785 * {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver)
1786 * notifyChange()} after updating. This method can be called from multiple
1787 * threads, as described in <a href="
1788 * {@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1789 * and Threads</a>.
1790 *
1791 * @param uri The URI to query. This can potentially have a record ID if
1792 * this is an update request for a specific record.
1793 * @param values A set of column_name/value pairs to update in the database.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001794 * @param extras A Bundle containing additional information necessary for
1795 * the operation. Arguments may include SQL style arguments, such
1796 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1797 * the documentation for each individual provider will indicate
1798 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001799 * @return the number of rows affected.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001800 * @throws IllegalArgumentException if the provider doesn't support one of
1801 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001802 */
1803 @Override
1804 public int update(@NonNull Uri uri, @Nullable ContentValues values,
1805 @Nullable Bundle extras) {
1806 extras = (extras != null) ? extras : Bundle.EMPTY;
1807 return update(uri, values,
1808 extras.getString(ContentResolver.QUERY_ARG_SQL_SELECTION),
1809 extras.getStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS));
1810 }
1811
1812 /**
Dan Egnor17876aa2010-07-28 12:28:04 -07001813 * Override this to handle requests to open a file blob.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001814 * The default implementation always throws {@link FileNotFoundException}.
1815 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001816 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1817 * and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001818 *
Dan Egnor17876aa2010-07-28 12:28:04 -07001819 * <p>This method returns a ParcelFileDescriptor, which is returned directly
1820 * to the caller. This way large data (such as images and documents) can be
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001821 * returned without copying the content.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 *
1823 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
1824 * their responsibility to close it when done. That is, the implementation
1825 * of this method should create a new ParcelFileDescriptor for each call.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001826 * <p>
1827 * If opened with the exclusive "r" or "w" modes, the returned
1828 * ParcelFileDescriptor can be a pipe or socket pair to enable streaming
1829 * of data. Opening with the "rw" or "rwt" modes implies a file on disk that
1830 * supports seeking.
1831 * <p>
1832 * If you need to detect when the returned ParcelFileDescriptor has been
1833 * closed, or if the remote process has crashed or encountered some other
1834 * error, you can use {@link ParcelFileDescriptor#open(File, int,
1835 * android.os.Handler, android.os.ParcelFileDescriptor.OnCloseListener)},
1836 * {@link ParcelFileDescriptor#createReliablePipe()}, or
1837 * {@link ParcelFileDescriptor#createReliableSocketPair()}.
Jeff Sharkeyb31afd22017-06-12 14:17:10 -06001838 * <p>
1839 * If you need to return a large file that isn't backed by a real file on
1840 * disk, such as a file on a network share or cloud storage service,
1841 * consider using
1842 * {@link StorageManager#openProxyFileDescriptor(int, android.os.ProxyFileDescriptorCallback, android.os.Handler)}
1843 * which will let you to stream the content on-demand.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 *
Dianne Hackborna53ee352013-02-20 12:47:02 -08001845 * <p class="note">For use in Intents, you will want to implement {@link #getType}
1846 * to return the appropriate MIME type for the data returned here with
1847 * the same URI. This will allow intent resolution to automatically determine the data MIME
1848 * type and select the appropriate matching targets as part of its operation.</p>
1849 *
1850 * <p class="note">For better interoperability with other applications, it is recommended
1851 * that for any URIs that can be opened, you also support queries on them
1852 * containing at least the columns specified by {@link android.provider.OpenableColumns}.
1853 * You may also want to support other common columns if you have additional meta-data
1854 * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED}
1855 * in {@link android.provider.MediaStore.MediaColumns}.</p>
1856 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 * @param uri The URI whose file is to be opened.
1858 * @param mode Access mode for the file. May be "r" for read-only access,
1859 * "rw" for read and write access, or "rwt" for read and write access
1860 * that truncates any existing file.
1861 *
1862 * @return Returns a new ParcelFileDescriptor which you can use to access
1863 * the file.
1864 *
1865 * @throws FileNotFoundException Throws FileNotFoundException if there is
1866 * no file associated with the given URI or the mode is invalid.
1867 * @throws SecurityException Throws SecurityException if the caller does
1868 * not have permission to access the file.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001869 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 * @see #openAssetFile(Uri, String)
1871 * @see #openFileHelper(Uri, String)
Dianne Hackborna53ee352013-02-20 12:47:02 -08001872 * @see #getType(android.net.Uri)
Jeff Sharkeye8c00d82013-10-15 15:46:10 -07001873 * @see ParcelFileDescriptor#parseMode(String)
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001874 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001875 public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 throws FileNotFoundException {
1877 throw new FileNotFoundException("No files supported by provider at "
1878 + uri);
1879 }
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 /**
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001882 * Override this to handle requests to open a file blob.
1883 * The default implementation always throws {@link FileNotFoundException}.
1884 * This method can be called from multiple threads, as described in
1885 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1886 * and Threads</a>.
1887 *
1888 * <p>This method returns a ParcelFileDescriptor, which is returned directly
1889 * to the caller. This way large data (such as images and documents) can be
1890 * returned without copying the content.
1891 *
1892 * <p>The returned ParcelFileDescriptor is owned by the caller, so it is
1893 * their responsibility to close it when done. That is, the implementation
1894 * of this method should create a new ParcelFileDescriptor for each call.
1895 * <p>
1896 * If opened with the exclusive "r" or "w" modes, the returned
1897 * ParcelFileDescriptor can be a pipe or socket pair to enable streaming
1898 * of data. Opening with the "rw" or "rwt" modes implies a file on disk that
1899 * supports seeking.
1900 * <p>
1901 * If you need to detect when the returned ParcelFileDescriptor has been
1902 * closed, or if the remote process has crashed or encountered some other
1903 * error, you can use {@link ParcelFileDescriptor#open(File, int,
1904 * android.os.Handler, android.os.ParcelFileDescriptor.OnCloseListener)},
1905 * {@link ParcelFileDescriptor#createReliablePipe()}, or
1906 * {@link ParcelFileDescriptor#createReliableSocketPair()}.
1907 *
1908 * <p class="note">For use in Intents, you will want to implement {@link #getType}
1909 * to return the appropriate MIME type for the data returned here with
1910 * the same URI. This will allow intent resolution to automatically determine the data MIME
1911 * type and select the appropriate matching targets as part of its operation.</p>
1912 *
1913 * <p class="note">For better interoperability with other applications, it is recommended
1914 * that for any URIs that can be opened, you also support queries on them
1915 * containing at least the columns specified by {@link android.provider.OpenableColumns}.
1916 * You may also want to support other common columns if you have additional meta-data
1917 * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED}
1918 * in {@link android.provider.MediaStore.MediaColumns}.</p>
1919 *
1920 * @param uri The URI whose file is to be opened.
1921 * @param mode Access mode for the file. May be "r" for read-only access,
1922 * "w" for write-only access, "rw" for read and write access, or
1923 * "rwt" for read and write access that truncates any existing
1924 * file.
1925 * @param signal A signal to cancel the operation in progress, or
1926 * {@code null} if none. For example, if you are downloading a
1927 * file from the network to service a "rw" mode request, you
1928 * should periodically call
1929 * {@link CancellationSignal#throwIfCanceled()} to check whether
1930 * the client has canceled the request and abort the download.
1931 *
1932 * @return Returns a new ParcelFileDescriptor which you can use to access
1933 * the file.
1934 *
1935 * @throws FileNotFoundException Throws FileNotFoundException if there is
1936 * no file associated with the given URI or the mode is invalid.
1937 * @throws SecurityException Throws SecurityException if the caller does
1938 * not have permission to access the file.
1939 *
1940 * @see #openAssetFile(Uri, String)
1941 * @see #openFileHelper(Uri, String)
1942 * @see #getType(android.net.Uri)
Jeff Sharkeye8c00d82013-10-15 15:46:10 -07001943 * @see ParcelFileDescriptor#parseMode(String)
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001944 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001945 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001946 public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
1947 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001948 return openFile(uri, mode);
1949 }
1950
1951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 * This is like {@link #openFile}, but can be implemented by providers
1953 * that need to be able to return sub-sections of files, often assets
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001954 * inside of their .apk.
1955 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08001956 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
1957 * and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001958 *
1959 * <p>If you implement this, your clients must be able to deal with such
Dan Egnor17876aa2010-07-28 12:28:04 -07001960 * file slices, either directly with
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001961 * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
1963 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
1964 * methods.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001965 * <p>
1966 * The returned AssetFileDescriptor can be a pipe or socket pair to enable
1967 * streaming of data.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07001968 *
1969 * <p class="note">If you are implementing this to return a full file, you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 * should create the AssetFileDescriptor with
1971 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001972 * applications that cannot handle sub-sections of files.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 *
Dianne Hackborna53ee352013-02-20 12:47:02 -08001974 * <p class="note">For use in Intents, you will want to implement {@link #getType}
1975 * to return the appropriate MIME type for the data returned here with
1976 * the same URI. This will allow intent resolution to automatically determine the data MIME
1977 * type and select the appropriate matching targets as part of its operation.</p>
1978 *
1979 * <p class="note">For better interoperability with other applications, it is recommended
1980 * that for any URIs that can be opened, you also support queries on them
1981 * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p>
1982 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 * @param uri The URI whose file is to be opened.
1984 * @param mode Access mode for the file. May be "r" for read-only access,
1985 * "w" for write-only access (erasing whatever data is currently in
1986 * the file), "wa" for write-only access to append to any existing data,
1987 * "rw" for read and write access on any existing data, and "rwt" for read
1988 * and write access that truncates any existing file.
1989 *
1990 * @return Returns a new AssetFileDescriptor which you can use to access
1991 * the file.
1992 *
1993 * @throws FileNotFoundException Throws FileNotFoundException if there is
1994 * no file associated with the given URI or the mode is invalid.
1995 * @throws SecurityException Throws SecurityException if the caller does
1996 * not have permission to access the file.
Steve McKayea93fe72016-12-02 11:35:35 -08001997 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 * @see #openFile(Uri, String)
1999 * @see #openFileHelper(Uri, String)
Dianne Hackborna53ee352013-02-20 12:47:02 -08002000 * @see #getType(android.net.Uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002002 public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 throws FileNotFoundException {
2004 ParcelFileDescriptor fd = openFile(uri, mode);
2005 return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
2006 }
2007
2008 /**
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07002009 * This is like {@link #openFile}, but can be implemented by providers
2010 * that need to be able to return sub-sections of files, often assets
2011 * inside of their .apk.
2012 * This method can be called from multiple threads, as described in
2013 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
2014 * and Threads</a>.
2015 *
2016 * <p>If you implement this, your clients must be able to deal with such
2017 * file slices, either directly with
2018 * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level
2019 * {@link ContentResolver#openInputStream ContentResolver.openInputStream}
2020 * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream}
2021 * methods.
2022 * <p>
2023 * The returned AssetFileDescriptor can be a pipe or socket pair to enable
2024 * streaming of data.
2025 *
2026 * <p class="note">If you are implementing this to return a full file, you
2027 * should create the AssetFileDescriptor with
2028 * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
2029 * applications that cannot handle sub-sections of files.</p>
2030 *
2031 * <p class="note">For use in Intents, you will want to implement {@link #getType}
2032 * to return the appropriate MIME type for the data returned here with
2033 * the same URI. This will allow intent resolution to automatically determine the data MIME
2034 * type and select the appropriate matching targets as part of its operation.</p>
2035 *
2036 * <p class="note">For better interoperability with other applications, it is recommended
2037 * that for any URIs that can be opened, you also support queries on them
2038 * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p>
2039 *
2040 * @param uri The URI whose file is to be opened.
2041 * @param mode Access mode for the file. May be "r" for read-only access,
2042 * "w" for write-only access (erasing whatever data is currently in
2043 * the file), "wa" for write-only access to append to any existing data,
2044 * "rw" for read and write access on any existing data, and "rwt" for read
2045 * and write access that truncates any existing file.
2046 * @param signal A signal to cancel the operation in progress, or
2047 * {@code null} if none. For example, if you are downloading a
2048 * file from the network to service a "rw" mode request, you
2049 * should periodically call
2050 * {@link CancellationSignal#throwIfCanceled()} to check whether
2051 * the client has canceled the request and abort the download.
2052 *
2053 * @return Returns a new AssetFileDescriptor which you can use to access
2054 * the file.
2055 *
2056 * @throws FileNotFoundException Throws FileNotFoundException if there is
2057 * no file associated with the given URI or the mode is invalid.
2058 * @throws SecurityException Throws SecurityException if the caller does
2059 * not have permission to access the file.
2060 *
2061 * @see #openFile(Uri, String)
2062 * @see #openFileHelper(Uri, String)
2063 * @see #getType(android.net.Uri)
2064 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002065 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07002066 public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
2067 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07002068 return openAssetFile(uri, mode);
2069 }
2070
2071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 * Convenience for subclasses that wish to implement {@link #openFile}
2073 * by looking up a column named "_data" at the given URI.
2074 *
2075 * @param uri The URI to be opened.
2076 * @param mode The file mode. May be "r" for read-only access,
2077 * "w" for write-only access (erasing whatever data is currently in
2078 * the file), "wa" for write-only access to append to any existing data,
2079 * "rw" for read and write access on any existing data, and "rwt" for read
2080 * and write access that truncates any existing file.
2081 *
2082 * @return Returns a new ParcelFileDescriptor that can be used by the
2083 * client to access the file.
2084 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002085 protected final @NonNull ParcelFileDescriptor openFileHelper(@NonNull Uri uri,
2086 @NonNull String mode) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 Cursor c = query(uri, new String[]{"_data"}, null, null, null);
2088 int count = (c != null) ? c.getCount() : 0;
2089 if (count != 1) {
2090 // If there is not exactly one result, throw an appropriate
2091 // exception.
2092 if (c != null) {
2093 c.close();
2094 }
2095 if (count == 0) {
2096 throw new FileNotFoundException("No entry for " + uri);
2097 }
2098 throw new FileNotFoundException("Multiple items at " + uri);
2099 }
2100
2101 c.moveToFirst();
2102 int i = c.getColumnIndex("_data");
2103 String path = (i >= 0 ? c.getString(i) : null);
2104 c.close();
2105 if (path == null) {
2106 throw new FileNotFoundException("Column _data not found.");
2107 }
2108
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07002109 int modeBits = ParcelFileDescriptor.parseMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 return ParcelFileDescriptor.open(new File(path), modeBits);
2111 }
2112
2113 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002114 * Called by a client to determine the types of data streams that this
2115 * content provider supports for the given URI. The default implementation
Christopher Tate2bc6eb82013-01-03 12:04:08 -08002116 * returns {@code null}, meaning no types. If your content provider stores data
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002117 * of a particular type, return that MIME type if it matches the given
2118 * mimeTypeFilter. If it can perform type conversions, return an array
2119 * of all supported MIME types that match mimeTypeFilter.
2120 *
2121 * @param uri The data in the content provider being queried.
2122 * @param mimeTypeFilter The type of data the client desires. May be
John Spurlock33900182014-01-02 11:04:18 -05002123 * a pattern, such as *&#47;* to retrieve all possible data types.
Christopher Tate2bc6eb82013-01-03 12:04:08 -08002124 * @return Returns {@code null} if there are no possible data streams for the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002125 * given mimeTypeFilter. Otherwise returns an array of all available
2126 * concrete MIME types.
2127 *
2128 * @see #getType(Uri)
2129 * @see #openTypedAssetFile(Uri, String, Bundle)
Dianne Hackborn1040dc42010-08-26 22:11:06 -07002130 * @see ClipDescription#compareMimeTypes(String, String)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002131 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002132 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07002133 public @Nullable String[] getStreamTypes(@NonNull Uri uri, @NonNull String mimeTypeFilter) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002134 return null;
2135 }
2136
2137 /**
2138 * Called by a client to open a read-only stream containing data of a
2139 * particular MIME type. This is like {@link #openAssetFile(Uri, String)},
2140 * except the file can only be read-only and the content provider may
2141 * perform data conversions to generate data of the desired type.
2142 *
2143 * <p>The default implementation compares the given mimeType against the
Dianne Hackborna53ee352013-02-20 12:47:02 -08002144 * result of {@link #getType(Uri)} and, if they match, simply calls
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002145 * {@link #openAssetFile(Uri, String)}.
2146 *
Dianne Hackborn1040dc42010-08-26 22:11:06 -07002147 * <p>See {@link ClipData} for examples of the use and implementation
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002148 * of this method.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07002149 * <p>
2150 * The returned AssetFileDescriptor can be a pipe or socket pair to enable
2151 * streaming of data.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002152 *
Dianne Hackborna53ee352013-02-20 12:47:02 -08002153 * <p class="note">For better interoperability with other applications, it is recommended
2154 * that for any URIs that can be opened, you also support queries on them
2155 * containing at least the columns specified by {@link android.provider.OpenableColumns}.
2156 * You may also want to support other common columns if you have additional meta-data
2157 * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED}
2158 * in {@link android.provider.MediaStore.MediaColumns}.</p>
2159 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002160 * @param uri The data in the content provider being queried.
2161 * @param mimeTypeFilter The type of data the client desires. May be
John Spurlock33900182014-01-02 11:04:18 -05002162 * a pattern, such as *&#47;*, if the caller does not have specific type
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002163 * requirements; in this case the content provider will pick its best
2164 * type matching the pattern.
2165 * @param opts Additional options from the client. The definitions of
2166 * these are specific to the content provider being called.
2167 *
2168 * @return Returns a new AssetFileDescriptor from which the client can
2169 * read data of the desired type.
2170 *
2171 * @throws FileNotFoundException Throws FileNotFoundException if there is
2172 * no file associated with the given URI or the mode is invalid.
2173 * @throws SecurityException Throws SecurityException if the caller does
2174 * not have permission to access the data.
2175 * @throws IllegalArgumentException Throws IllegalArgumentException if the
2176 * content provider does not support the requested MIME type.
2177 *
2178 * @see #getStreamTypes(Uri, String)
2179 * @see #openAssetFile(Uri, String)
Dianne Hackborn1040dc42010-08-26 22:11:06 -07002180 * @see ClipDescription#compareMimeTypes(String, String)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002181 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002182 public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
2183 @NonNull String mimeTypeFilter, @Nullable Bundle opts) throws FileNotFoundException {
Dianne Hackborn02dfd262010-08-13 12:34:58 -07002184 if ("*/*".equals(mimeTypeFilter)) {
2185 // If they can take anything, the untyped open call is good enough.
2186 return openAssetFile(uri, "r");
2187 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002188 String baseType = getType(uri);
Dianne Hackborn1040dc42010-08-26 22:11:06 -07002189 if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) {
Dianne Hackborn02dfd262010-08-13 12:34:58 -07002190 // Use old untyped open call if this provider has a type for this
2191 // URI and it matches the request.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002192 return openAssetFile(uri, "r");
2193 }
2194 throw new FileNotFoundException("Can't open " + uri + " as type " + mimeTypeFilter);
2195 }
2196
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07002197
2198 /**
2199 * Called by a client to open a read-only stream containing data of a
2200 * particular MIME type. This is like {@link #openAssetFile(Uri, String)},
2201 * except the file can only be read-only and the content provider may
2202 * perform data conversions to generate data of the desired type.
2203 *
2204 * <p>The default implementation compares the given mimeType against the
2205 * result of {@link #getType(Uri)} and, if they match, simply calls
2206 * {@link #openAssetFile(Uri, String)}.
2207 *
2208 * <p>See {@link ClipData} for examples of the use and implementation
2209 * of this method.
2210 * <p>
2211 * The returned AssetFileDescriptor can be a pipe or socket pair to enable
2212 * streaming of data.
2213 *
2214 * <p class="note">For better interoperability with other applications, it is recommended
2215 * that for any URIs that can be opened, you also support queries on them
2216 * containing at least the columns specified by {@link android.provider.OpenableColumns}.
2217 * You may also want to support other common columns if you have additional meta-data
2218 * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED}
2219 * in {@link android.provider.MediaStore.MediaColumns}.</p>
2220 *
2221 * @param uri The data in the content provider being queried.
2222 * @param mimeTypeFilter The type of data the client desires. May be
John Spurlock33900182014-01-02 11:04:18 -05002223 * a pattern, such as *&#47;*, if the caller does not have specific type
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07002224 * requirements; in this case the content provider will pick its best
2225 * type matching the pattern.
2226 * @param opts Additional options from the client. The definitions of
2227 * these are specific to the content provider being called.
2228 * @param signal A signal to cancel the operation in progress, or
2229 * {@code null} if none. For example, if you are downloading a
2230 * file from the network to service a "rw" mode request, you
2231 * should periodically call
2232 * {@link CancellationSignal#throwIfCanceled()} to check whether
2233 * the client has canceled the request and abort the download.
2234 *
2235 * @return Returns a new AssetFileDescriptor from which the client can
2236 * read data of the desired type.
2237 *
2238 * @throws FileNotFoundException Throws FileNotFoundException if there is
2239 * no file associated with the given URI or the mode is invalid.
2240 * @throws SecurityException Throws SecurityException if the caller does
2241 * not have permission to access the data.
2242 * @throws IllegalArgumentException Throws IllegalArgumentException if the
2243 * content provider does not support the requested MIME type.
2244 *
2245 * @see #getStreamTypes(Uri, String)
2246 * @see #openAssetFile(Uri, String)
2247 * @see ClipDescription#compareMimeTypes(String, String)
2248 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002249 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07002250 public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
2251 @NonNull String mimeTypeFilter, @Nullable Bundle opts,
2252 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07002253 return openTypedAssetFile(uri, mimeTypeFilter, opts);
2254 }
2255
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002256 /**
2257 * Interface to write a stream of data to a pipe. Use with
2258 * {@link ContentProvider#openPipeHelper}.
2259 */
2260 public interface PipeDataWriter<T> {
2261 /**
2262 * Called from a background thread to stream data out to a pipe.
2263 * Note that the pipe is blocking, so this thread can block on
2264 * writes for an arbitrary amount of time if the client is slow
2265 * at reading.
2266 *
2267 * @param output The pipe where data should be written. This will be
2268 * closed for you upon returning from this function.
2269 * @param uri The URI whose data is to be written.
2270 * @param mimeType The desired type of data to be written.
2271 * @param opts Options supplied by caller.
2272 * @param args Your own custom arguments.
2273 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002274 public void writeDataToPipe(@NonNull ParcelFileDescriptor output, @NonNull Uri uri,
2275 @NonNull String mimeType, @Nullable Bundle opts, @Nullable T args);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002276 }
2277
2278 /**
2279 * A helper function for implementing {@link #openTypedAssetFile}, for
2280 * creating a data pipe and background thread allowing you to stream
2281 * generated data back to the client. This function returns a new
2282 * ParcelFileDescriptor that should be returned to the caller (the caller
2283 * is responsible for closing it).
2284 *
2285 * @param uri The URI whose data is to be written.
2286 * @param mimeType The desired type of data to be written.
2287 * @param opts Options supplied by caller.
2288 * @param args Your own custom arguments.
2289 * @param func Interface implementing the function that will actually
2290 * stream the data.
2291 * @return Returns a new ParcelFileDescriptor holding the read side of
2292 * the pipe. This should be returned to the caller for reading; the caller
2293 * is responsible for closing it when done.
2294 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002295 public @NonNull <T> ParcelFileDescriptor openPipeHelper(final @NonNull Uri uri,
2296 final @NonNull String mimeType, final @Nullable Bundle opts, final @Nullable T args,
2297 final @NonNull PipeDataWriter<T> func) throws FileNotFoundException {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002298 try {
2299 final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
2300
2301 AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {
2302 @Override
2303 protected Object doInBackground(Object... params) {
2304 func.writeDataToPipe(fds[1], uri, mimeType, opts, args);
2305 try {
2306 fds[1].close();
2307 } catch (IOException e) {
2308 Log.w(TAG, "Failure closing pipe", e);
2309 }
2310 return null;
2311 }
2312 };
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002313 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07002314
2315 return fds[0];
2316 } catch (IOException e) {
2317 throw new FileNotFoundException("failure making pipe");
2318 }
2319 }
2320
2321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 * Returns true if this instance is a temporary content provider.
2323 * @return true if this instance is a temporary content provider
2324 */
2325 protected boolean isTemporary() {
2326 return false;
2327 }
2328
2329 /**
2330 * Returns the Binder object for this provider.
2331 *
2332 * @return the Binder object for this provider
2333 * @hide
2334 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002335 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 public IContentProvider getIContentProvider() {
2337 return mTransport;
2338 }
2339
2340 /**
Dianne Hackborn334d9ae2013-02-26 15:02:06 -08002341 * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use
2342 * when directly instantiating the provider for testing.
2343 * @hide
2344 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002345 @UnsupportedAppUsage
Dianne Hackborn334d9ae2013-02-26 15:02:06 -08002346 public void attachInfoForTesting(Context context, ProviderInfo info) {
2347 attachInfo(context, info, true);
2348 }
2349
2350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 * After being instantiated, this is called to tell the content provider
2352 * about itself.
2353 *
2354 * @param context The context this provider is running in
2355 * @param info Registered information about this content provider
2356 */
2357 public void attachInfo(Context context, ProviderInfo info) {
Dianne Hackborn334d9ae2013-02-26 15:02:06 -08002358 attachInfo(context, info, false);
2359 }
2360
2361 private void attachInfo(Context context, ProviderInfo info, boolean testing) {
Dianne Hackborn334d9ae2013-02-26 15:02:06 -08002362 mNoPerms = testing;
Jeff Sharkey497789e2019-02-15 19:41:30 -07002363 mCallingPackage = new ThreadLocal<>();
Dianne Hackborn334d9ae2013-02-26 15:02:06 -08002364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 /*
2366 * Only allow it to be set once, so after the content service gives
2367 * this to us clients can't change it.
2368 */
2369 if (mContext == null) {
2370 mContext = context;
Jeff Sharkeyc4156e02018-09-24 13:23:57 -06002371 if (context != null && mTransport != null) {
Jeff Sharkey10cb3122013-09-17 15:18:43 -07002372 mTransport.mAppOpsManager = (AppOpsManager) context.getSystemService(
2373 Context.APP_OPS_SERVICE);
2374 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002375 mMyUid = Process.myUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 if (info != null) {
2377 setReadPermission(info.readPermission);
2378 setWritePermission(info.writePermission);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002379 setPathPermissions(info.pathPermissions);
Dianne Hackbornb424b632010-08-18 15:59:05 -07002380 mExported = info.exported;
Amith Yamasania6f4d582014-08-07 17:58:39 -07002381 mSingleUser = (info.flags & ProviderInfo.FLAG_SINGLE_USER) != 0;
Nicolas Prevotf300bab2014-08-07 19:23:17 +01002382 setAuthorities(info.authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
Jeff Sharkey22e834f2019-08-08 15:21:33 -06002384 if (Build.IS_DEBUGGABLE) {
2385 setTransportLoggingEnabled(Log.isLoggable(getClass().getSimpleName(),
2386 Log.VERBOSE));
2387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 ContentProvider.this.onCreate();
2389 }
2390 }
Fred Quintanace31b232009-05-04 16:01:15 -07002391
2392 /**
Dan Egnor17876aa2010-07-28 12:28:04 -07002393 * Override this to handle requests to perform a batch of operations, or the
2394 * default implementation will iterate over the operations and call
2395 * {@link ContentProviderOperation#apply} on each of them.
2396 * If all calls to {@link ContentProviderOperation#apply} succeed
2397 * then a {@link ContentProviderResult} array with as many
2398 * elements as there were operations will be returned. If any of the calls
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07002399 * fail, it is up to the implementation how many of the others take effect.
2400 * This method can be called from multiple threads, as described in
Scott Main7aee61f2011-02-08 11:25:01 -08002401 * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
2402 * and Threads</a>.
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07002403 *
Fred Quintanace31b232009-05-04 16:01:15 -07002404 * @param operations the operations to apply
2405 * @return the results of the applications
Dan Egnor6fcc0f0732010-07-27 16:32:17 -07002406 * @throws OperationApplicationException thrown if any operation fails.
2407 * @see ContentProviderOperation#apply
Fred Quintanace31b232009-05-04 16:01:15 -07002408 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002409 @Override
2410 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
2411 @NonNull ArrayList<ContentProviderOperation> operations)
2412 throws OperationApplicationException {
2413 return applyBatch(operations);
2414 }
2415
Jeff Sharkey673db442015-06-11 19:30:57 -07002416 public @NonNull ContentProviderResult[] applyBatch(
2417 @NonNull ArrayList<ContentProviderOperation> operations)
2418 throws OperationApplicationException {
Fred Quintana03d94902009-05-22 14:23:31 -07002419 final int numOperations = operations.size();
2420 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
2421 for (int i = 0; i < numOperations; i++) {
2422 results[i] = operations.get(i).apply(this, results, i);
Fred Quintanace31b232009-05-04 16:01:15 -07002423 }
2424 return results;
2425 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002426
2427 /**
Manuel Roman2c96a0c2010-08-05 16:39:49 -07002428 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002429 * interfaces that are cheaper and/or unnatural for a table-like
2430 * model.
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002431 *
Dianne Hackborn5d122d92013-03-12 18:37:07 -07002432 * <p class="note"><strong>WARNING:</strong> The framework does no permission checking
2433 * on this entry into the content provider besides the basic ability for the application
2434 * to get access to the provider at all. For example, it has no idea whether the call
2435 * being executed may read or write data in the provider, so can't enforce those
2436 * individual permissions. Any implementation of this method <strong>must</strong>
2437 * do its own permission checks on incoming calls to make sure they are allowed.</p>
2438 *
Christopher Tate2bc6eb82013-01-03 12:04:08 -08002439 * @param method method name to call. Opaque to framework, but should not be {@code null}.
2440 * @param arg provider-defined String argument. May be {@code null}.
2441 * @param extras provider-defined Bundle argument. May be {@code null}.
2442 * @return provider-defined return value. May be {@code null}, which is also
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002443 * the default for providers which don't implement any call methods.
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002444 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002445 @Override
2446 public @Nullable Bundle call(@NonNull String authority, @NonNull String method,
2447 @Nullable String arg, @Nullable Bundle extras) {
2448 return call(method, arg, extras);
2449 }
2450
Jeff Sharkey673db442015-06-11 19:30:57 -07002451 public @Nullable Bundle call(@NonNull String method, @Nullable String arg,
2452 @Nullable Bundle extras) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002453 return null;
2454 }
Vasu Nori0c9e14a2010-08-04 13:31:48 -07002455
2456 /**
Manuel Roman2c96a0c2010-08-05 16:39:49 -07002457 * Implement this to shut down the ContentProvider instance. You can then
2458 * invoke this method in unit tests.
Steve McKayea93fe72016-12-02 11:35:35 -08002459 *
Vasu Nori0c9e14a2010-08-04 13:31:48 -07002460 * <p>
Manuel Roman2c96a0c2010-08-05 16:39:49 -07002461 * Android normally handles ContentProvider startup and shutdown
2462 * automatically. You do not need to start up or shut down a
2463 * ContentProvider. When you invoke a test method on a ContentProvider,
2464 * however, a ContentProvider instance is started and keeps running after
2465 * the test finishes, even if a succeeding test instantiates another
2466 * ContentProvider. A conflict develops because the two instances are
2467 * usually running against the same underlying data source (for example, an
2468 * sqlite database).
2469 * </p>
Vasu Nori0c9e14a2010-08-04 13:31:48 -07002470 * <p>
Manuel Roman2c96a0c2010-08-05 16:39:49 -07002471 * Implementing shutDown() avoids this conflict by providing a way to
2472 * terminate the ContentProvider. This method can also prevent memory leaks
2473 * from multiple instantiations of the ContentProvider, and it can ensure
2474 * unit test isolation by allowing you to completely clean up the test
2475 * fixture before moving on to the next test.
2476 * </p>
Vasu Nori0c9e14a2010-08-04 13:31:48 -07002477 */
2478 public void shutdown() {
2479 Log.w(TAG, "implement ContentProvider shutdown() to make sure all database " +
2480 "connections are gracefully shutdown");
2481 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08002482
2483 /**
2484 * Print the Provider's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07002485 * you run "adb shell dumpsys activity provider &lt;provider_component_name&gt;".
Marco Nelissen18cb2872011-11-15 11:19:53 -08002486 *
Marco Nelissen18cb2872011-11-15 11:19:53 -08002487 * @param fd The raw file descriptor that the dump is being sent to.
2488 * @param writer The PrintWriter to which you should dump your state. This will be
2489 * closed for you after you return.
2490 * @param args additional arguments to the dump request.
Marco Nelissen18cb2872011-11-15 11:19:53 -08002491 */
2492 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
2493 writer.println("nothing to dump");
2494 }
Nicolas Prevotf300bab2014-08-07 19:23:17 +01002495
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002496 private void validateIncomingAuthority(String authority) throws SecurityException {
2497 if (!matchesOurAuthorities(getAuthorityWithoutUserId(authority))) {
2498 String message = "The authority " + authority + " does not match the one of the "
2499 + "contentProvider: ";
2500 if (mAuthority != null) {
2501 message += mAuthority;
2502 } else {
2503 message += Arrays.toString(mAuthorities);
2504 }
2505 throw new SecurityException(message);
2506 }
2507 }
2508
Nicolas Prevot504d78e2014-06-26 10:07:33 +01002509 /** @hide */
Jeff Sharkeyc4156e02018-09-24 13:23:57 -06002510 @VisibleForTesting
2511 public Uri validateIncomingUri(Uri uri) throws SecurityException {
Nicolas Prevotf300bab2014-08-07 19:23:17 +01002512 String auth = uri.getAuthority();
Robin Lee2ab02e22016-07-28 18:41:23 +01002513 if (!mSingleUser) {
2514 int userId = getUserIdFromAuthority(auth, UserHandle.USER_CURRENT);
2515 if (userId != UserHandle.USER_CURRENT && userId != mContext.getUserId()) {
2516 throw new SecurityException("trying to query a ContentProvider in user "
2517 + mContext.getUserId() + " with a uri belonging to user " + userId);
2518 }
Nicolas Prevot504d78e2014-06-26 10:07:33 +01002519 }
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002520 validateIncomingAuthority(auth);
Jeff Sharkeyc4156e02018-09-24 13:23:57 -06002521
2522 // Normalize the path by removing any empty path segments, which can be
2523 // a source of security issues.
2524 final String encodedPath = uri.getEncodedPath();
2525 if (encodedPath != null && encodedPath.indexOf("//") != -1) {
Jeff Sharkey4a7b6ac2018-10-03 10:33:46 -06002526 final Uri normalized = uri.buildUpon()
2527 .encodedPath(encodedPath.replaceAll("//+", "/")).build();
2528 Log.w(TAG, "Normalized " + uri + " to " + normalized
2529 + " to avoid possible security issues");
2530 return normalized;
Jeff Sharkeyc4156e02018-09-24 13:23:57 -06002531 } else {
2532 return uri;
2533 }
Nicolas Prevot504d78e2014-06-26 10:07:33 +01002534 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002535
2536 /** @hide */
Robin Lee2ab02e22016-07-28 18:41:23 +01002537 private Uri maybeGetUriWithoutUserId(Uri uri) {
2538 if (mSingleUser) {
2539 return uri;
2540 }
2541 return getUriWithoutUserId(uri);
2542 }
2543
2544 /** @hide */
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002545 public static int getUserIdFromAuthority(String auth, int defaultUserId) {
2546 if (auth == null) return defaultUserId;
Nicolas Prevot504d78e2014-06-26 10:07:33 +01002547 int end = auth.lastIndexOf('@');
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002548 if (end == -1) return defaultUserId;
2549 String userIdString = auth.substring(0, end);
2550 try {
2551 return Integer.parseInt(userIdString);
2552 } catch (NumberFormatException e) {
2553 Log.w(TAG, "Error parsing userId.", e);
2554 return UserHandle.USER_NULL;
2555 }
2556 }
2557
2558 /** @hide */
2559 public static int getUserIdFromAuthority(String auth) {
2560 return getUserIdFromAuthority(auth, UserHandle.USER_CURRENT);
2561 }
2562
2563 /** @hide */
2564 public static int getUserIdFromUri(Uri uri, int defaultUserId) {
2565 if (uri == null) return defaultUserId;
2566 return getUserIdFromAuthority(uri.getAuthority(), defaultUserId);
2567 }
2568
2569 /** @hide */
2570 public static int getUserIdFromUri(Uri uri) {
2571 return getUserIdFromUri(uri, UserHandle.USER_CURRENT);
2572 }
2573
2574 /**
Alex Kershawba014ac2020-01-20 22:25:02 +00002575 * Returns the user associated with the given URI.
2576 *
2577 * @hide
2578 */
2579 @TestApi
2580 public @NonNull static UserHandle getUserHandleFromUri(@NonNull Uri uri) {
2581 return UserHandle.of(getUserIdFromUri(uri, Process.myUserHandle().getIdentifier()));
2582 }
2583
2584 /**
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002585 * Removes userId part from authority string. Expects format:
2586 * userId@some.authority
2587 * If there is no userId in the authority, it symply returns the argument
2588 * @hide
2589 */
2590 public static String getAuthorityWithoutUserId(String auth) {
2591 if (auth == null) return null;
Nicolas Prevot504d78e2014-06-26 10:07:33 +01002592 int end = auth.lastIndexOf('@');
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002593 return auth.substring(end+1);
2594 }
2595
2596 /** @hide */
2597 public static Uri getUriWithoutUserId(Uri uri) {
2598 if (uri == null) return null;
2599 Uri.Builder builder = uri.buildUpon();
2600 builder.authority(getAuthorityWithoutUserId(uri.getAuthority()));
2601 return builder.build();
2602 }
2603
2604 /** @hide */
2605 public static boolean uriHasUserId(Uri uri) {
2606 if (uri == null) return false;
2607 return !TextUtils.isEmpty(uri.getUserInfo());
2608 }
2609
2610 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002611 @UnsupportedAppUsage
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002612 public static Uri maybeAddUserId(Uri uri, int userId) {
2613 if (uri == null) return null;
2614 if (userId != UserHandle.USER_CURRENT
Jason Monkd18651f2017-10-05 14:18:49 -04002615 && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002616 if (!uriHasUserId(uri)) {
2617 //We don't add the user Id if there's already one
2618 Uri.Builder builder = uri.buildUpon();
2619 builder.encodedAuthority("" + userId + "@" + uri.getEncodedAuthority());
2620 return builder.build();
2621 }
2622 }
2623 return uri;
2624 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002625}