blob: f32a4ab433575b377ec8e79e226471a22a7c0d92 [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
Ivan Chiang6da7b0a2019-04-11 20:01:17 +080019import static android.provider.DocumentsContract.EXTRA_ORIENTATION;
20
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080021import android.accounts.Account;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070022import android.annotation.IntDef;
Jeff Sharkey673db442015-06-11 19:30:57 -070023import android.annotation.NonNull;
Scott Kennedy9f78f652015-03-01 15:29:25 -080024import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070025import android.annotation.RequiresPermission;
Ivan Chiangfd3415c2018-12-07 18:22:50 +080026import android.annotation.SystemApi;
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -070027import android.annotation.TestApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070028import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080029import android.app.ActivityManager;
Jeff Sharkey66a017b2013-01-17 18:18:22 -080030import android.app.ActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070031import android.app.AppGlobals;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070032import android.app.UriGrantsManager;
Artur Satayeve23a0eb2019-12-10 17:47:52 +000033import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey9edef252019-05-20 14:00:17 -060034import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager.NameNotFoundException;
36import android.content.res.AssetFileDescriptor;
37import android.content.res.Resources;
38import android.database.ContentObserver;
Jeff Brown825c5132011-10-12 16:11:30 -070039import android.database.CrossProcessCursorWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.database.IContentObserver;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060042import android.graphics.Bitmap;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060043import android.graphics.ImageDecoder;
44import android.graphics.ImageDecoder.ImageInfo;
45import android.graphics.ImageDecoder.Source;
Ivan Chiang6da7b0a2019-04-11 20:01:17 +080046import android.graphics.Matrix;
Jeff Sharkey5b836f22014-08-27 14:46:32 -070047import android.graphics.Point;
Daniel Nishic29d2b02016-06-30 12:20:41 -070048import android.graphics.drawable.Drawable;
Jeff Sharkey806bece2019-02-19 14:42:55 -070049import android.graphics.drawable.Icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
51import android.os.Bundle;
Jeff Browna7771df2012-05-07 20:06:46 -070052import android.os.CancellationSignal;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070053import android.os.DeadObjectException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070054import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070055import android.os.ICancellationSignal;
56import android.os.OperationCanceledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.ParcelFileDescriptor;
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -080058import android.os.RemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.RemoteException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070060import android.os.ServiceManager;
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -080061import android.os.SystemClock;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070062import android.os.UserHandle;
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060063import android.os.storage.StorageManager;
Ivan Chiang6da7b0a2019-04-11 20:01:17 +080064import android.system.Int32Ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080066import android.util.EventLog;
Dianne Hackborn231cc602009-04-27 17:10:36 -070067import android.util.Log;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060068import android.util.Size;
Jeff Sharkey1307f422019-11-13 13:03:10 -070069import android.util.SparseArray;
Jeff Sharkey08da7a12013-08-11 20:53:18 -070070
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -080071import com.android.internal.annotations.GuardedBy;
Daniel Nishic29d2b02016-06-30 12:20:41 -070072import com.android.internal.util.MimeIconUtils;
Jeff Sharkey673db442015-06-11 19:30:57 -070073
Jeff Sharkey60cfad82016-01-05 17:30:57 -070074import dalvik.system.CloseGuard;
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.io.File;
77import java.io.FileInputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.InputStream;
81import java.io.OutputStream;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070082import java.lang.annotation.Retention;
83import java.lang.annotation.RetentionPolicy;
Gilles Debunne03f02922010-06-09 14:11:45 -070084import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.List;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060086import java.util.Objects;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080087import java.util.Random;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070088import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090/**
91 * This class provides applications access to the content model.
Joe Fernandez558459f2011-10-13 16:47:36 -070092 *
93 * <div class="special reference">
94 * <h3>Developer Guides</h3>
95 * <p>For more information about using a ContentResolver with content providers, read the
96 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
97 * developer guide.</p>
Ng Zhi Anb837d0b2019-01-24 21:39:24 -080098 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700100public abstract class ContentResolver implements ContentInterface {
Fred Quintanaac9385e2009-06-22 18:00:59 -0700101 /**
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600102 * Enables logic that supports deprecation of {@code _data} columns,
103 * typically by replacing values with fake paths that the OS then offers to
104 * redirect to {@link #openFileDescriptor(Uri, String)}, which developers
105 * should be using directly.
106 *
107 * @hide
108 */
Jeff Sharkey10ec9d82018-11-28 14:52:45 -0700109 public static final boolean DEPRECATE_DATA_COLUMNS = StorageManager.hasIsolatedStorage();
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600110
111 /**
112 * Special filesystem path prefix which indicates that a path should be
113 * treated as a {@code content://} {@link Uri} when
114 * {@link #DEPRECATE_DATA_COLUMNS} is enabled.
115 * <p>
116 * The remainder of the path after this prefix is a
117 * {@link Uri#getSchemeSpecificPart()} value, which includes authority, path
118 * segments, and query parameters.
119 *
120 * @hide
121 */
122 public static final String DEPRECATE_DATA_PREFIX = "/mnt/content/";
123
124 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700125 * @deprecated instead use
126 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
127 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700128 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -0700129 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700130
131 /**
132 * If this extra is set to true, the sync request will be scheduled
133 * at the front of the sync request queue and without any delay
134 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700136
Fred Quintanaac9385e2009-06-22 18:00:59 -0700137 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000138 * If this extra is set to true, the sync request will be scheduled
139 * only when the device is plugged in. This is equivalent to calling
140 * setRequiresCharging(true) on {@link SyncRequest}.
141 */
142 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
143
144 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700145 * @deprecated instead use
146 * {@link #SYNC_EXTRAS_MANUAL}
147 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700148 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800150
151 /**
152 * If this extra is set to true then the sync settings (like getSyncAutomatically())
153 * are ignored by the sync scheduler.
154 */
155 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
156
157 /**
158 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
159 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
160 * retries will still honor the backoff.
161 */
162 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
163
164 /**
165 * If this extra is set to true then the request will not be retried if it fails.
166 */
167 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
168
169 /**
170 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
171 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
172 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700173 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800174
Georgi Nikolovb3395572013-06-18 18:27:31 -0700175 /**
176 * Indicates that this sync is intended to only upload local changes to the server.
177 * For example, this will be set to true if the sync is initiated by a call to
178 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
179 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700181
182 /**
183 * Indicates that the sync adapter should proceed with the delete operations,
184 * even if it determines that there are too many.
185 * See {@link SyncResult#tooManyDeletions}
186 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700188
189 /**
190 * Indicates that the sync adapter should not proceed with the delete operations,
191 * if it determines that there are too many.
192 * See {@link SyncResult#tooManyDeletions}
193 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
195
Matthew Williamsfa774182013-06-18 15:44:11 -0700196 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
197 /** {@hide} User-specified flag for expected upload size. */
198 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
199
200 /** {@hide} User-specified flag for expected download size. */
201 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
202
203 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
204 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
205
206 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700207 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700208
Fred Quintana4a6679b2009-08-17 13:05:39 -0700209 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700210 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800211 *
212 * Only the system and the shell user can set it.
213 *
214 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
215 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700216 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800217
218 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700219 * Set by the SyncManager to request that the SyncAdapter initialize itself for
220 * the given account/authority pair. One required initialization step is to
221 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
222 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
223 * do a full sync, though it is allowed to do so.
224 */
225 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
226
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800227 /** @hide */
228 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
229 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public static final String SCHEME_CONTENT = "content";
232 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
233 public static final String SCHEME_FILE = "file";
234
235 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700236 * An extra {@link Point} describing the optimal size for a requested image
237 * resource, in pixels. If a provider has multiple sizes of the image, it
238 * should return the image closest to this size.
239 *
240 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
241 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
242 * CancellationSignal)
243 */
244 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
245
246 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800247 * An extra boolean describing whether a particular provider supports refresh
248 * or not. If a provider supports refresh, it should include this key in its
249 * returned Cursor as part of its query call.
250 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800251 */
252 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
253
254 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800255 * Key for an SQL style selection string that may be present in the query Bundle argument
256 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
257 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800258 *
259 * <p>Clients should never include user supplied values directly in the selection string,
260 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
261 * should use standard placeholder notation to represent values in a selection string,
262 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
263 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800264 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
265 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700266 *
267 * @see #QUERY_ARG_SORT_COLUMNS
268 * @see #QUERY_ARG_SORT_DIRECTION
269 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600270 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800271 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700272 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800273
274 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800275 * Key for SQL selection string arguments list.
276 *
277 * <p>Clients should never include user supplied values directly in the selection string,
278 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
279 * should use standard placeholder notation to represent values in a selection string,
280 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
281 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800282 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
283 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700284 *
285 * @see #QUERY_ARG_SORT_COLUMNS
286 * @see #QUERY_ARG_SORT_DIRECTION
287 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600288 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800289 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700290 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
291 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800292
293 /**
294 * Key for an SQL style sort string that may be present in the query Bundle argument
295 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
296 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800297 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800298 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
299 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700300 *
301 * @see #QUERY_ARG_SORT_COLUMNS
302 * @see #QUERY_ARG_SORT_DIRECTION
303 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600304 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800305 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700306 public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800307
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700308 /**
309 * Key for an SQL style {@code GROUP BY} string that may be present in the
310 * query Bundle argument passed to
311 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
312 *
313 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
314 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
315 *
316 * @see #QUERY_ARG_GROUP_COLUMNS
317 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600318 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700319
320 /**
321 * Key for an SQL style {@code HAVING} string that may be present in the
322 * query Bundle argument passed to
323 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
324 *
325 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
326 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
327 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600328 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700329
330 /**
331 * Key for an SQL style {@code LIMIT} string that may be present in the
332 * query Bundle argument passed to
333 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
334 *
335 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
336 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
337 *
338 * @see #QUERY_ARG_LIMIT
339 * @see #QUERY_ARG_OFFSET
340 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600341 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
342
Steve McKay29c3f682016-12-16 14:52:59 -0800343 /**
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700344 * Specifies the list of columns (stored as a {@code String[]}) against
345 * which to sort results. When first column values are identical, records
346 * are then sorted based on second column values, and so on.
347 * <p>
348 * Columns present in this list must also be included in the projection
349 * supplied to
350 * {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
351 * <p>
352 * Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKayd74a3bd2017-04-24 12:07:53 -0700353 * <li>{@link ContentProvider} implementations: When preparing data in
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700354 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
355 * if sort columns is reflected in the returned Cursor, it is strongly
356 * recommended that {@link #QUERY_ARG_SORT_COLUMNS} then be included in the
357 * array of honored arguments reflected in {@link Cursor} extras
358 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
359 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in
360 * the arguments {@link Bundle}, the Content framework will attempt to
361 * synthesize an QUERY_ARG_SQL* argument using the corresponding
362 * QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800363 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700364 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800365
366 /**
367 * Specifies desired sort order. When unspecified a provider may provide a default
368 * sort direction, or choose to return unsorted results.
369 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800370 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
371 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700372 * <li>{@link ContentProvider} implementations: When preparing data in
373 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
374 * is reflected in the returned Cursor, it is strongly recommended that
375 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
376 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800377 *
378 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
379 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700380 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800381 *
382 * @see #QUERY_SORT_DIRECTION_ASCENDING
383 * @see #QUERY_SORT_DIRECTION_DESCENDING
384 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700385 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800386
387 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700388 * Allows client to specify a hint to the provider declaring which collation
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600389 * to use when sorting values.
390 * <p>
391 * Providers may support custom collators. When specifying a custom collator
Steve McKayd74a3bd2017-04-24 12:07:53 -0700392 * the value is determined by the Provider.
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600393 * <p>
394 * {@link ContentProvider} implementations: When preparing data in
395 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
396 * if sort collation is reflected in the returned Cursor, it is strongly
397 * recommended that {@link #QUERY_ARG_SORT_COLLATION} then be included in
398 * the array of honored arguments reflected in {@link Cursor} extras
399 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
400 * <p>
401 * When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
402 * arguments {@link Bundle}, the Content framework will attempt to
403 * synthesize a QUERY_ARG_SQL* argument using the corresponding
404 * QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800405 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700406 * @see java.text.Collator#PRIMARY
407 * @see java.text.Collator#SECONDARY
408 * @see java.text.Collator#TERTIARY
409 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800410 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700411 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800412
Steve McKay415f41b2017-02-01 13:38:25 -0800413 /**
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600414 * Allows client to specify a hint to the provider declaring which locale to
415 * use when sorting values.
416 * <p>
417 * The value is defined as a RFC 3066 locale ID followed by an optional
418 * keyword list, which is the locale format used to configure ICU through
419 * classes like {@link android.icu.util.ULocale}. This supports requesting
420 * advanced sorting options, such as {@code de@collation=phonebook},
421 * {@code zh@collation=pinyin}, etc.
422 * <p>
423 * {@link ContentProvider} implementations: When preparing data in
424 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
425 * if sort locale is reflected in the returned Cursor, it is strongly
426 * recommended that {@link #QUERY_ARG_SORT_LOCALE} then be included in the
427 * array of honored arguments reflected in {@link Cursor} extras
428 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
429 *
430 * @see java.util.Locale#Locale(String)
431 * @see android.icu.util.ULocale#ULocale(String)
432 */
433 public static final String QUERY_ARG_SORT_LOCALE = "android:query-arg-sort-locale";
434
435 /**
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700436 * Specifies the list of columns (stored as a {@code String[]}) against
437 * which to group results. When column values are identical, multiple
438 * records are collapsed together into a single record.
439 * <p>
440 * Columns present in this list must also be included in the projection
441 * supplied to
442 * {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
443 * <p>
444 * Apps targeting {@link android.os.Build.VERSION_CODES#R} or higher:
445 * <li>{@link ContentProvider} implementations: When preparing data in
446 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
447 * if group columns is reflected in the returned Cursor, it is strongly
448 * recommended that {@link #QUERY_ARG_SORT_COLUMNS} then be included in the
449 * array of honored arguments reflected in {@link Cursor} extras
450 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
451 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in
452 * the arguments {@link Bundle}, the Content framework will attempt to
453 * synthesize an QUERY_ARG_SQL* argument using the corresponding
454 * QUERY_ARG_SORT* values.
455 */
456 public static final String QUERY_ARG_GROUP_COLUMNS = "android:query-arg-group-columns";
457
458 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700459 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800460 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700461 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800462 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
463 * when any QUERY_ARG_SORT* value was honored during the preparation of the
464 * results {@link Cursor}.
465 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700466 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
467 *
468 * @see #QUERY_ARG_SORT_COLUMNS
469 * @see #QUERY_ARG_SORT_DIRECTION
470 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600471 * @see #QUERY_ARG_SORT_LOCALE
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700472 * @see #QUERY_ARG_GROUP_COLUMNS
Steve McKay415f41b2017-02-01 13:38:25 -0800473 */
474 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
475
Steve McKay29c3f682016-12-16 14:52:59 -0800476 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700477 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800478 QUERY_SORT_DIRECTION_ASCENDING,
479 QUERY_SORT_DIRECTION_DESCENDING
480 })
481 @Retention(RetentionPolicy.SOURCE)
482 public @interface SortDirection {}
483 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
484 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
485
486 /**
487 * @see {@link java.text.Collector} for details on respective collation strength.
488 * @hide
489 */
490 @IntDef(flag = false, value = {
491 java.text.Collator.PRIMARY,
492 java.text.Collator.SECONDARY,
493 java.text.Collator.TERTIARY,
494 java.text.Collator.IDENTICAL
495 })
496 @Retention(RetentionPolicy.SOURCE)
497 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800498
499 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700500 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800501 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700502 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800503
504 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700505 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800506 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700507 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800508
509 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700510 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
511 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800512 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800513 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700514 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800515 *
516 * <p>Providers having returned -1 in a previous query are recommended to
517 * send content change notification once (if) full recordset size becomes
518 * known.
519 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700520 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800521
522 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * This is the Android platform's base MIME type for a content: URI
524 * containing a Cursor of a single item. Applications should use this
525 * as the base type along with their own sub-type of their content: URIs
526 * that represent a particular item. For example, hypothetical IMAP email
527 * client may have a URI
528 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
529 * message in the inbox, whose MIME type would be reported as
530 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800531 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
533 */
534 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 /**
537 * This is the Android platform's base MIME type for a content: URI
538 * containing a Cursor of zero or more items. Applications should use this
539 * as the base type along with their own sub-type of their content: URIs
540 * that represent a directory of items. For example, hypothetical IMAP email
541 * client may have a URI
542 * <code>content://com.company.provider.imap/inbox</code> for all of the
543 * messages in its inbox, whose MIME type would be reported as
544 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 * <p>Note how the base MIME type varies between this and
547 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
548 * one single item or multiple items in the data set, while the sub-type
549 * remains the same because in either case the data structure contained
550 * in the cursor is the same.
551 */
552 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700553
Matt Caseybd7bcf02014-02-05 15:51:39 -0800554 /**
555 * This is the Android platform's generic MIME type to match any MIME
556 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
557 * {@code SUB_TYPE} is the sub-type of the application-dependent
558 * content, e.g., "audio", "video", "playlist".
559 */
560 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
561
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600562 /** {@hide} */
563 @Deprecated
564 public static final String MIME_TYPE_DEFAULT = ClipDescription.MIMETYPE_UNKNOWN;
Jeff Sharkey91e3cd42018-08-27 18:03:33 -0600565
Fred Quintanaac9385e2009-06-22 18:00:59 -0700566 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100567 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700568 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
569 /** @hide */
570 public static final int SYNC_ERROR_AUTHENTICATION = 2;
571 /** @hide */
572 public static final int SYNC_ERROR_IO = 3;
573 /** @hide */
574 public static final int SYNC_ERROR_PARSE = 4;
575 /** @hide */
576 public static final int SYNC_ERROR_CONFLICT = 5;
577 /** @hide */
578 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
579 /** @hide */
580 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
581 /** @hide */
582 public static final int SYNC_ERROR_INTERNAL = 8;
583
Alon Albert57286f92012-10-09 14:21:38 -0700584 private static final String[] SYNC_ERROR_NAMES = new String[] {
585 "already-in-progress",
586 "authentication-error",
587 "io-error",
588 "parse-error",
589 "conflict",
590 "too-many-deletions",
591 "too-many-retries",
592 "internal-error",
593 };
594
595 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800596 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700597 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
598 return String.valueOf(error);
599 }
600 return SYNC_ERROR_NAMES[error - 1];
601 }
602
Alon Albert5c113fa2013-02-07 08:07:32 -0800603 /** @hide */
604 public static int syncErrorStringToInt(String error) {
605 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
606 if (SYNC_ERROR_NAMES[i].equals(error)) {
607 return i + 1;
608 }
609 }
610 if (error != null) {
611 try {
612 return Integer.parseInt(error);
613 } catch (NumberFormatException e) {
614 Log.d(TAG, "error parsing sync error: " + error);
615 }
616 }
617 return 0;
618 }
619
Fred Quintanaac9385e2009-06-22 18:00:59 -0700620 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700621 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700622 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
623 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100624 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700625 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
626 /** @hide */
627 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
628
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700629 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700630 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
631 NOTIFY_SYNC_TO_NETWORK,
632 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS
633 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700634 @Retention(RetentionPolicy.SOURCE)
635 public @interface NotifyFlags {}
636
637 /**
638 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
639 * to the network.
640 */
641 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
642
643 /**
644 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
645 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
646 * using "notify for descendants." The purpose of this is to allow the provide to send
647 * a general notification of "something under X" changed that observers of that specific
648 * URI can receive, while also sending a specific URI under X. It would use this flag
649 * when sending the former, so that observers of "X and descendants" only see the latter.
650 */
651 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
652
Makoto Onuki75ad2492018-03-28 14:42:42 -0700653 /**
654 * No exception, throttled by app standby normally.
655 * @hide
656 */
657 public static final int SYNC_EXEMPTION_NONE = 0;
658
659 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700660 * Exemption given to a sync request made by a foreground app (including
661 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700662 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700663 * At the schedule time, we promote the sync adapter app for a higher bucket:
664 * - If the device is not dozing (so the sync will start right away)
665 * promote to ACTIVE for 1 hour.
666 * - If the device is dozing (so the sync *won't* start right away),
667 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
668 * device comes out of doze.
669 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
670 * so it can schedule and start more syncs without getting throttled, even when the first
671 * operation was canceled and now we're retrying.
672 *
673 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700674 * @hide
675 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700676 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700677
678 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700679 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700680 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
681 * @hide
682 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700683 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700684
685 /** @hide */
686 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
687 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700688 SYNC_EXEMPTION_PROMOTE_BUCKET,
689 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700690 })
691 @Retention(RetentionPolicy.SOURCE)
692 public @interface SyncExemption {}
693
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800694 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800695 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700696 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800697 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800698 private final Random mRandom = new Random(); // guarded by itself
699
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800700 /** @hide */
701 public static final String REMOTE_CALLBACK_RESULT = "result";
702
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700703 public ContentResolver(@Nullable Context context) {
704 this(context, null);
705 }
706
707 /** {@hide} */
708 public ContentResolver(@Nullable Context context, @Nullable ContentInterface wrapped) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800709 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700710 mPackageName = mContext.getOpPackageName();
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700711 mFeatureId = mContext.getFeatureId();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000712 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700713 mWrapped = wrapped;
714 }
715
716 /** {@hide} */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700717 public static @NonNull ContentResolver wrap(@NonNull ContentInterface wrapped) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000718 Objects.requireNonNull(wrapped);
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700719
720 return new ContentResolver(null, wrapped) {
721 @Override
722 public void unstableProviderDied(IContentProvider icp) {
723 throw new UnsupportedOperationException();
724 }
725 @Override
726 public boolean releaseUnstableProvider(IContentProvider icp) {
727 throw new UnsupportedOperationException();
728 }
729 @Override
730 public boolean releaseProvider(IContentProvider icp) {
731 throw new UnsupportedOperationException();
732 }
733 @Override
734 protected IContentProvider acquireUnstableProvider(Context c, String name) {
735 throw new UnsupportedOperationException();
736 }
737 @Override
738 protected IContentProvider acquireProvider(Context c, String name) {
739 throw new UnsupportedOperationException();
740 }
741 };
742 }
743
744 /**
745 * Create a {@link ContentResolver} instance that redirects all its methods
746 * to the given {@link ContentProvider}.
747 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700748 public static @NonNull ContentResolver wrap(@NonNull ContentProvider wrapped) {
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700749 return wrap((ContentInterface) wrapped);
750 }
751
752 /**
753 * Create a {@link ContentResolver} instance that redirects all its methods
754 * to the given {@link ContentProviderClient}.
755 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700756 public static @NonNull ContentResolver wrap(@NonNull ContentProviderClient wrapped) {
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700757 return wrap((ContentInterface) wrapped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100761 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700763
764 /**
765 * Providing a default implementation of this, to avoid having to change a
766 * lot of other things, but implementations of ContentResolver should
767 * implement it.
768 *
769 * @hide
770 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100771 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700772 protected IContentProvider acquireExistingProvider(Context c, String name) {
773 return acquireProvider(c, name);
774 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100777 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700779 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100780 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700781 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
782 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100783 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700784 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700785 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100786 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700787 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700789 /** @hide */
790 public void appNotRespondingViaProvider(IContentProvider icp) {
791 throw new UnsupportedOperationException("appNotRespondingViaProvider");
792 }
793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 /**
795 * Return the MIME type of the given content URL.
796 *
797 * @param url A Uri identifying content (either a list or specific type),
798 * using the content:// scheme.
799 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
800 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700801 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700802 public final @Nullable String getType(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000803 Objects.requireNonNull(url, "url");
Jeff Sharkey673db442015-06-11 19:30:57 -0700804
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700805 try {
806 if (mWrapped != null) return mWrapped.getType(url);
807 } catch (RemoteException e) {
808 return null;
809 }
810
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700811 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700812 IContentProvider provider = acquireExistingProvider(url);
813 if (provider != null) {
814 try {
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800815 final GetTypeResultListener resultListener = new GetTypeResultListener();
816 provider.getTypeAsync(url, new RemoteCallback(resultListener));
817 resultListener.waitForResult();
818 return resultListener.type;
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700819 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600820 // Arbitrary and not worth documenting, as Activity
821 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700822 return null;
823 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100824 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700825 return null;
826 } finally {
827 releaseProvider(provider);
828 }
829 }
830
831 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 return null;
833 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 try {
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800836 GetTypeResultListener resultListener = new GetTypeResultListener();
837 ActivityManager.getService().getProviderMimeTypeAsync(
838 ContentProvider.getUriWithoutUserId(url),
839 resolveUserId(url),
840 new RemoteCallback(resultListener));
841 resultListener.waitForResult();
842 return resultListener.type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 } catch (RemoteException e) {
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800844 // We just failed to send a oneway request to the System Server. Nothing to do.
845 return null;
Ola Olsson145e6c42010-12-20 16:45:35 +0100846 } catch (java.lang.Exception e) {
847 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
848 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850 }
851
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800852 private static final int GET_TYPE_TIMEOUT_MILLIS = 3000;
853
854 private static class GetTypeResultListener implements RemoteCallback.OnResultListener {
855 @GuardedBy("this")
856 public boolean done;
857
858 @GuardedBy("this")
859 public String type;
860
861 @Override
862 public void onResult(Bundle result) {
863 synchronized (this) {
864 type = result.getString(REMOTE_CALLBACK_RESULT);
865 done = true;
866 notifyAll();
867 }
868 }
869
870 public void waitForResult() {
871 synchronized (this) {
872 if (!done) {
873 try {
874 wait(GET_TYPE_TIMEOUT_MILLIS);
875 } catch (InterruptedException e) {
876 // Ignore
877 }
878 }
879 }
880 }
881 }
882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700884 * Query for the possible MIME types for the representations the given
885 * content URL can be returned when opened as as stream with
886 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
887 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700888 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700889 * data that they contain.
890 *
891 * @param url A Uri identifying content (either a list or specific type),
892 * using the content:// scheme.
893 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500894 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700895 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700896 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700897 * data streams that match the given mimeTypeFilter. If there are none,
898 * null is returned.
899 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700900 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700901 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000902 Objects.requireNonNull(url, "url");
903 Objects.requireNonNull(mimeTypeFilter, "mimeTypeFilter");
Jeff Sharkey673db442015-06-11 19:30:57 -0700904
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700905 try {
906 if (mWrapped != null) return mWrapped.getStreamTypes(url, mimeTypeFilter);
907 } catch (RemoteException e) {
908 return null;
909 }
910
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700911 IContentProvider provider = acquireProvider(url);
912 if (provider == null) {
913 return null;
914 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700915
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700916 try {
917 return provider.getStreamTypes(url, mimeTypeFilter);
918 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800919 // Arbitrary and not worth documenting, as Activity
920 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700921 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700922 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800923 releaseProvider(provider);
924 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700925 }
926
927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800929 * <p>
930 * For best performance, the caller should follow these guidelines:
931 * <ul>
932 * <li>Provide an explicit projection, to prevent
933 * reading data from storage that aren't going to be used.</li>
934 * <li>Use question mark parameter markers such as 'phone=?' instead of
935 * explicit values in the {@code selection} parameter, so that queries
936 * that differ only by those values will be recognized as the same
937 * for caching purposes.</li>
938 * </ul>
939 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 *
941 * @param uri The URI, using the content:// scheme, for the content to
942 * retrieve.
943 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800944 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 * @param selection A filter declaring which rows to return, formatted as an
946 * SQL WHERE clause (excluding the WHERE itself). Passing null will
947 * return all rows for the given URI.
948 * @param selectionArgs You may include ?s in selection, which will be
949 * replaced by the values from selectionArgs, in the order that they
950 * appear in the selection. The values will be bound as Strings.
951 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
952 * clause (excluding the ORDER BY itself). Passing null will use the
953 * default sort order, which may be unordered.
Varun Shahc4fdfa32019-05-10 14:10:08 -0700954 * @return A Cursor object, which is positioned before the first entry. May return
955 * <code>null</code> if the underlying content provider returns <code>null</code>,
956 * or if it crashes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 * @see Cursor
958 */
Tor Norbye788fc2b2015-07-05 16:10:42 -0700959 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
960 @Nullable String[] projection, @Nullable String selection,
961 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800962 return query(uri, projection, selection, selectionArgs, sortOrder, null);
963 }
964
965 /**
Jeff Brownc64ff372013-10-09 18:50:56 -0700966 * Query the given URI, returning a {@link Cursor} over the result set
967 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800968 * <p>
969 * For best performance, the caller should follow these guidelines:
970 * <ul>
971 * <li>Provide an explicit projection, to prevent
972 * reading data from storage that aren't going to be used.</li>
973 * <li>Use question mark parameter markers such as 'phone=?' instead of
974 * explicit values in the {@code selection} parameter, so that queries
975 * that differ only by those values will be recognized as the same
976 * for caching purposes.</li>
977 * </ul>
978 * </p>
979 *
980 * @param uri The URI, using the content:// scheme, for the content to
981 * retrieve.
982 * @param projection A list of which columns to return. Passing null will
983 * return all columns, which is inefficient.
984 * @param selection A filter declaring which rows to return, formatted as an
985 * SQL WHERE clause (excluding the WHERE itself). Passing null will
986 * return all rows for the given URI.
987 * @param selectionArgs You may include ?s in selection, which will be
988 * replaced by the values from selectionArgs, in the order that they
989 * appear in the selection. The values will be bound as Strings.
990 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
991 * clause (excluding the ORDER BY itself). Passing null will use the
992 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -0800993 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800994 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
995 * when the query is executed.
Varun Shahc4fdfa32019-05-10 14:10:08 -0700996 * @return A Cursor object, which is positioned before the first entry. May return
997 * <code>null</code> if the underlying content provider returns <code>null</code>,
998 * or if it crashes.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800999 * @see Cursor
1000 */
Steve McKayea93fe72016-12-02 11:35:35 -08001001 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -07001002 @Nullable String[] projection, @Nullable String selection,
1003 @Nullable String[] selectionArgs, @Nullable String sortOrder,
1004 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -08001005 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
1006 return query(uri, projection, queryArgs, cancellationSignal);
1007 }
1008
1009 /**
1010 * Query the given URI, returning a {@link Cursor} over the result set
1011 * with support for cancellation.
1012 *
1013 * <p>For best performance, the caller should follow these guidelines:
1014 *
1015 * <li>Provide an explicit projection, to prevent reading data from storage
1016 * that aren't going to be used.
1017 *
Steve McKay415f41b2017-02-01 13:38:25 -08001018 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
1019 * the preparation of the result set by including the respective argument keys
1020 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
1021 * for details.
1022 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07001023 * @see #QUERY_ARG_SORT_COLUMNS
1024 * @see #QUERY_ARG_SORT_DIRECTION
1025 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -08001026 *
Steve McKayea93fe72016-12-02 11:35:35 -08001027 * @param uri The URI, using the content:// scheme, for the content to
1028 * retrieve.
1029 * @param projection A list of which columns to return. Passing null will
1030 * return all columns, which is inefficient.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001031 * @param queryArgs A Bundle containing additional information necessary for
1032 * the operation. Arguments may include SQL style arguments, such
1033 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1034 * the documentation for each individual provider will indicate
1035 * which arguments they support.
Steve McKayea93fe72016-12-02 11:35:35 -08001036 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
1037 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
1038 * when the query is executed.
Varun Shahc4fdfa32019-05-10 14:10:08 -07001039 * @return A Cursor object, which is positioned before the first entry. May return
1040 * <code>null</code> if the underlying content provider returns <code>null</code>,
1041 * or if it crashes.
Steve McKayea93fe72016-12-02 11:35:35 -08001042 * @see Cursor
1043 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001044 @Override
Steve McKayea93fe72016-12-02 11:35:35 -08001045 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
1046 @Nullable String[] projection, @Nullable Bundle queryArgs,
1047 @Nullable CancellationSignal cancellationSignal) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001048 Objects.requireNonNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001049
1050 try {
1051 if (mWrapped != null) {
1052 return mWrapped.query(uri, projection, queryArgs, cancellationSignal);
1053 }
1054 } catch (RemoteException e) {
1055 return null;
1056 }
1057
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001058 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1059 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 return null;
1061 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001062 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -08001063 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001065 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -08001066
Jeff Brown4c1241d2012-02-02 17:05:00 -08001067 ICancellationSignal remoteCancellationSignal = null;
1068 if (cancellationSignal != null) {
1069 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001070 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -08001071 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -08001072 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001073 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001074 qCursor = unstableProvider.query(mPackageName, mFeatureId, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -08001075 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001076 } catch (DeadObjectException e) {
1077 // The remote process has died... but we only hold an unstable
1078 // reference though, so we might recover!!! Let's try!!!!
1079 // This is exciting!!1!!1!!!!1
1080 unstableProviderDied(unstableProvider);
1081 stableProvider = acquireProvider(uri);
1082 if (stableProvider == null) {
1083 return null;
1084 }
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001085 qCursor = stableProvider.query(mPackageName, mFeatureId, uri, projection,
1086 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001087 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001088 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 return null;
1090 }
Jeff Brownc21b5a02013-01-07 17:15:12 -08001091
1092 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -07001093 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001094 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -08001095 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -08001096
1097 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001098 final IContentProvider provider = (stableProvider != null) ? stableProvider
1099 : acquireProvider(uri);
1100 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001101 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -08001102 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001103 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001105 // Arbitrary and not worth documenting, as Activity
1106 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001108 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -08001109 if (qCursor != null) {
1110 qCursor.close();
1111 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001112 if (cancellationSignal != null) {
1113 cancellationSignal.setRemote(null);
1114 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001115 if (unstableProvider != null) {
1116 releaseUnstableProvider(unstableProvider);
1117 }
1118 if (stableProvider != null) {
1119 releaseProvider(stableProvider);
1120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122 }
1123
Jeff Sharkey63784022019-04-17 10:39:42 -06001124 /** {@hide} */
1125 public final @NonNull Uri canonicalizeOrElse(@NonNull Uri uri) {
1126 final Uri res = canonicalize(uri);
1127 return (res != null) ? res : uri;
1128 }
1129
Fred Quintana89437372009-05-15 15:10:40 -07001130 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001131 * Transform the given <var>url</var> to a canonical representation of
1132 * its referenced resource, which can be used across devices, persisted,
1133 * backed up and restored, etc. The returned Uri is still a fully capable
1134 * Uri for use with its content provider, allowing you to do all of the
1135 * same content provider operations as with the original Uri --
1136 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
1137 * only difference in behavior between the original and new Uris is that
1138 * the content provider may need to do some additional work at each call
1139 * using it to resolve it to the correct resource, especially if the
1140 * canonical Uri has been moved to a different environment.
1141 *
1142 * <p>If you are moving a canonical Uri between environments, you should
1143 * perform another call to {@link #canonicalize} with that original Uri to
1144 * re-canonicalize it for the current environment. Alternatively, you may
1145 * want to use {@link #uncanonicalize} to transform it to a non-canonical
1146 * Uri that works only in the current environment but potentially more
1147 * efficiently than the canonical representation.</p>
1148 *
1149 * @param url The {@link Uri} that is to be transformed to a canonical
1150 * representation. Like all resolver calls, the input can be either
1151 * a non-canonical or canonical Uri.
1152 *
1153 * @return Returns the official canonical representation of <var>url</var>,
1154 * or null if the content provider does not support a canonical representation
1155 * of the given Uri. Many providers may not support canonicalization of some
1156 * or all of their Uris.
1157 *
1158 * @see #uncanonicalize
1159 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001160 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001161 public final @Nullable Uri canonicalize(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001162 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001163
1164 try {
1165 if (mWrapped != null) return mWrapped.canonicalize(url);
1166 } catch (RemoteException e) {
1167 return null;
1168 }
1169
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001170 IContentProvider provider = acquireProvider(url);
1171 if (provider == null) {
1172 return null;
1173 }
1174
1175 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001176 return provider.canonicalize(mPackageName, mFeatureId, url);
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001177 } catch (RemoteException e) {
1178 // Arbitrary and not worth documenting, as Activity
1179 // Manager will kill this process shortly anyway.
1180 return null;
1181 } finally {
1182 releaseProvider(provider);
1183 }
1184 }
1185
1186 /**
1187 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
1188 * it to its local non-canonical form. This can be useful in some cases where
1189 * you know that you will only be using the Uri in the current environment and
1190 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001191 * provider or want to verify that the referenced data exists at all in the
1192 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001193 *
1194 * @param url The canonical {@link Uri} that is to be convered back to its
1195 * non-canonical form.
1196 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001197 * @return Returns the non-canonical representation of <var>url</var>. This will
1198 * return null if data identified by the canonical Uri can not be found in
1199 * the current environment; callers must always check for null and deal with
1200 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001201 *
1202 * @see #canonicalize
1203 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001204 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001205 public final @Nullable Uri uncanonicalize(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001206 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001207
1208 try {
1209 if (mWrapped != null) return mWrapped.uncanonicalize(url);
1210 } catch (RemoteException e) {
1211 return null;
1212 }
1213
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001214 IContentProvider provider = acquireProvider(url);
1215 if (provider == null) {
1216 return null;
1217 }
1218
1219 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001220 return provider.uncanonicalize(mPackageName, mFeatureId, url);
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001221 } catch (RemoteException e) {
1222 // Arbitrary and not worth documenting, as Activity
1223 // Manager will kill this process shortly anyway.
1224 return null;
1225 } finally {
1226 releaseProvider(provider);
1227 }
1228 }
1229
1230 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001231 * This allows clients to request an explicit refresh of content identified
1232 * by {@code uri}.
Ben Lin1cf454f2016-11-10 13:50:54 -08001233 * <p>
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001234 * Client code should only invoke this method when there is a strong
1235 * indication (such as a user initiated pull to refresh gesture) that the
1236 * content is stale.
Ben Lin1cf454f2016-11-10 13:50:54 -08001237 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -08001238 *
Ben Lin2b64a882016-11-11 15:24:58 -08001239 * @param url The Uri identifying the data to refresh.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001240 * @param extras Additional options from the client. The definitions of
1241 * these are specific to the content provider being called.
1242 * @param cancellationSignal A signal to cancel the operation in progress,
1243 * or {@code null} if none. For example, if you called refresh on
1244 * a particular uri, you should call
1245 * {@link CancellationSignal#throwIfCanceled()} to check whether
1246 * the client has canceled the refresh request.
Ben Lin1cf454f2016-11-10 13:50:54 -08001247 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -08001248 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001249 @Override
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001250 public final boolean refresh(@NonNull Uri url, @Nullable Bundle extras,
Ben Lin1cf454f2016-11-10 13:50:54 -08001251 @Nullable CancellationSignal cancellationSignal) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001252 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001253
1254 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001255 if (mWrapped != null) return mWrapped.refresh(url, extras, cancellationSignal);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001256 } catch (RemoteException e) {
1257 return false;
1258 }
1259
Ben Lin1cf454f2016-11-10 13:50:54 -08001260 IContentProvider provider = acquireProvider(url);
1261 if (provider == null) {
1262 return false;
1263 }
1264
1265 try {
1266 ICancellationSignal remoteCancellationSignal = null;
1267 if (cancellationSignal != null) {
1268 cancellationSignal.throwIfCanceled();
1269 remoteCancellationSignal = provider.createCancellationSignal();
1270 cancellationSignal.setRemote(remoteCancellationSignal);
1271 }
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001272 return provider.refresh(mPackageName, mFeatureId, url, extras,
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001273 remoteCancellationSignal);
Ben Lin1cf454f2016-11-10 13:50:54 -08001274 } catch (RemoteException e) {
1275 // Arbitrary and not worth documenting, as Activity
1276 // Manager will kill this process shortly anyway.
1277 return false;
1278 } finally {
1279 releaseProvider(provider);
1280 }
1281 }
1282
Jeff Sharkey9edef252019-05-20 14:00:17 -06001283 /** {@hide} */
1284 @Override
1285 public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001286 Objects.requireNonNull(uri, "uri");
Jeff Sharkey9edef252019-05-20 14:00:17 -06001287
1288 try {
1289 if (mWrapped != null) return mWrapped.checkUriPermission(uri, uid, modeFlags);
1290 } catch (RemoteException e) {
1291 return PackageManager.PERMISSION_DENIED;
1292 }
1293
1294 try (ContentProviderClient client = acquireUnstableContentProviderClient(uri)) {
1295 return client.checkUriPermission(uri, uid, modeFlags);
1296 } catch (RemoteException e) {
1297 return PackageManager.PERMISSION_DENIED;
1298 }
1299 }
1300
Ben Lin1cf454f2016-11-10 13:50:54 -08001301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 * Open a stream on to the content associated with a content URI. If there
1303 * is no data associated with the URI, FileNotFoundException is thrown.
1304 *
1305 * <h5>Accepts the following URI schemes:</h5>
1306 * <ul>
1307 * <li>content ({@link #SCHEME_CONTENT})</li>
1308 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1309 * <li>file ({@link #SCHEME_FILE})</li>
1310 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001311 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1313 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 * @param uri The desired URI.
1316 * @return InputStream
1317 * @throws FileNotFoundException if the provided URI could not be opened.
1318 * @see #openAssetFileDescriptor(Uri, String)
1319 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001320 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001322 Objects.requireNonNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 String scheme = uri.getScheme();
1324 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1325 // Note: left here to avoid breaking compatibility. May be removed
1326 // with sufficient testing.
1327 OpenResourceIdResult r = getResourceId(uri);
1328 try {
1329 InputStream stream = r.r.openRawResource(r.id);
1330 return stream;
1331 } catch (Resources.NotFoundException ex) {
1332 throw new FileNotFoundException("Resource does not exist: " + uri);
1333 }
1334 } else if (SCHEME_FILE.equals(scheme)) {
1335 // Note: left here to avoid breaking compatibility. May be removed
1336 // with sufficient testing.
1337 return new FileInputStream(uri.getPath());
1338 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001339 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 try {
1341 return fd != null ? fd.createInputStream() : null;
1342 } catch (IOException e) {
1343 throw new FileNotFoundException("Unable to create stream");
1344 }
1345 }
1346 }
1347
1348 /**
1349 * Synonym for {@link #openOutputStream(Uri, String)
1350 * openOutputStream(uri, "w")}.
1351 * @throws FileNotFoundException if the provided URI could not be opened.
1352 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001353 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 throws FileNotFoundException {
1355 return openOutputStream(uri, "w");
1356 }
1357
1358 /**
1359 * Open a stream on to the content associated with a content URI. If there
1360 * is no data associated with the URI, FileNotFoundException is thrown.
1361 *
1362 * <h5>Accepts the following URI schemes:</h5>
1363 * <ul>
1364 * <li>content ({@link #SCHEME_CONTENT})</li>
1365 * <li>file ({@link #SCHEME_FILE})</li>
1366 * </ul>
1367 *
1368 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1369 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 * @param uri The desired URI.
1372 * @param mode May be "w", "wa", "rw", or "rwt".
1373 * @return OutputStream
1374 * @throws FileNotFoundException if the provided URI could not be opened.
1375 * @see #openAssetFileDescriptor(Uri, String)
1376 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001377 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001379 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 try {
1381 return fd != null ? fd.createOutputStream() : null;
1382 } catch (IOException e) {
1383 throw new FileNotFoundException("Unable to create stream");
1384 }
1385 }
1386
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001387 @Override
1388 public final @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
1389 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001390 try {
1391 if (mWrapped != null) return mWrapped.openFile(uri, mode, signal);
1392 } catch (RemoteException e) {
1393 return null;
1394 }
1395
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001396 return openFileDescriptor(uri, mode, signal);
1397 }
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001400 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1402 * underlying {@link ContentProvider#openFile}
1403 * ContentProvider.openFile()} method, so will <em>not</em> work with
1404 * providers that return sub-sections of files. If at all possible,
1405 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1406 * will receive a FileNotFoundException exception if the provider returns a
1407 * sub-section of a file.
1408 *
1409 * <h5>Accepts the following URI schemes:</h5>
1410 * <ul>
1411 * <li>content ({@link #SCHEME_CONTENT})</li>
1412 * <li>file ({@link #SCHEME_FILE})</li>
1413 * </ul>
1414 *
1415 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1416 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001417 * <p>
1418 * If opening with the exclusive "r" or "w" modes, the returned
1419 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1420 * of data. Opening with the "rw" mode implies a file on disk that supports
1421 * seeking. If possible, always use an exclusive mode to give the underlying
1422 * {@link ContentProvider} the most flexibility.
1423 * <p>
1424 * If you are writing a file, and need to communicate an error to the
1425 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 * @param uri The desired URI to open.
1428 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1429 * ContentProvider.openFile}.
1430 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1431 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001432 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 * file exists under the URI or the mode is invalid.
1434 * @see #openAssetFileDescriptor(Uri, String)
1435 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001436 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1437 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001438 return openFileDescriptor(uri, mode, null);
1439 }
1440
1441 /**
1442 * Open a raw file descriptor to access data under a URI. This
1443 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1444 * underlying {@link ContentProvider#openFile}
1445 * ContentProvider.openFile()} method, so will <em>not</em> work with
1446 * providers that return sub-sections of files. If at all possible,
1447 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1448 * will receive a FileNotFoundException exception if the provider returns a
1449 * sub-section of a file.
1450 *
1451 * <h5>Accepts the following URI schemes:</h5>
1452 * <ul>
1453 * <li>content ({@link #SCHEME_CONTENT})</li>
1454 * <li>file ({@link #SCHEME_FILE})</li>
1455 * </ul>
1456 *
1457 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1458 * on these schemes.
1459 * <p>
1460 * If opening with the exclusive "r" or "w" modes, the returned
1461 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1462 * of data. Opening with the "rw" mode implies a file on disk that supports
1463 * seeking. If possible, always use an exclusive mode to give the underlying
1464 * {@link ContentProvider} the most flexibility.
1465 * <p>
1466 * If you are writing a file, and need to communicate an error to the
1467 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1468 *
1469 * @param uri The desired URI to open.
1470 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1471 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001472 * @param cancellationSignal A signal to cancel the operation in progress,
1473 * or null if none. If the operation is canceled, then
1474 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001475 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1476 * own this descriptor and are responsible for closing it when done.
1477 * @throws FileNotFoundException Throws FileNotFoundException if no
1478 * file exists under the URI or the mode is invalid.
1479 * @see #openAssetFileDescriptor(Uri, String)
1480 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001481 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1482 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1483 throws FileNotFoundException {
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001484 try {
1485 if (mWrapped != null) return mWrapped.openFile(uri, mode, cancellationSignal);
1486 } catch (RemoteException e) {
1487 return null;
1488 }
1489
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001490 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 if (afd == null) {
1492 return null;
1493 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 if (afd.getDeclaredLength() < 0) {
1496 // This is a full file!
1497 return afd.getParcelFileDescriptor();
1498 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 // Client can't handle a sub-section of a file, so close what
1501 // we got and bail with an exception.
1502 try {
1503 afd.close();
1504 } catch (IOException e) {
1505 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 throw new FileNotFoundException("Not a whole file");
1508 }
1509
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001510 @Override
1511 public final @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
1512 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001513 try {
1514 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, signal);
1515 } catch (RemoteException e) {
1516 return null;
1517 }
1518
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001519 return openAssetFileDescriptor(uri, mode, signal);
1520 }
1521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001523 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001525 * method of the provider associated with the given URI, to retrieve any file stored there.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 *
1527 * <h5>Accepts the following URI schemes:</h5>
1528 * <ul>
1529 * <li>content ({@link #SCHEME_CONTENT})</li>
1530 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1531 * <li>file ({@link #SCHEME_FILE})</li>
1532 * </ul>
1533 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1534 * <p>
1535 * A Uri object can be used to reference a resource in an APK file. The
1536 * Uri should be one of the following formats:
1537 * <ul>
1538 * <li><code>android.resource://package_name/id_number</code><br/>
1539 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1540 * For example <code>com.example.myapp</code><br/>
1541 * <code>id_number</code> is the int form of the ID.<br/>
1542 * The easiest way to construct this form is
1543 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1544 * </li>
1545 * <li><code>android.resource://package_name/type/name</code><br/>
1546 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1547 * For example <code>com.example.myapp</code><br/>
1548 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1549 * or <code>drawable</code>.
1550 * <code>name</code> is the string form of the resource name. That is, whatever the file
1551 * name was in your res directory, without the type extension.
1552 * The easiest way to construct this form is
1553 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1554 * </li>
1555 * </ul>
1556 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001557 * <p>Note that if this function is called for read-only input (mode is "r")
1558 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001559 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001560 * from any built-in data conversion that a provider implements.
1561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * @param uri The desired URI to open.
1563 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1564 * ContentProvider.openAssetFile}.
1565 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1566 * own this descriptor and are responsible for closing it when done.
1567 * @throws FileNotFoundException Throws FileNotFoundException of no
1568 * file exists under the URI or the mode is invalid.
1569 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001570 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1571 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001572 return openAssetFileDescriptor(uri, mode, null);
1573 }
1574
1575 /**
1576 * Open a raw file descriptor to access data under a URI. This
1577 * interacts with the underlying {@link ContentProvider#openAssetFile}
1578 * method of the provider associated with the given URI, to retrieve any file stored there.
1579 *
1580 * <h5>Accepts the following URI schemes:</h5>
1581 * <ul>
1582 * <li>content ({@link #SCHEME_CONTENT})</li>
1583 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1584 * <li>file ({@link #SCHEME_FILE})</li>
1585 * </ul>
1586 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1587 * <p>
1588 * A Uri object can be used to reference a resource in an APK file. The
1589 * Uri should be one of the following formats:
1590 * <ul>
1591 * <li><code>android.resource://package_name/id_number</code><br/>
1592 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1593 * For example <code>com.example.myapp</code><br/>
1594 * <code>id_number</code> is the int form of the ID.<br/>
1595 * The easiest way to construct this form is
1596 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1597 * </li>
1598 * <li><code>android.resource://package_name/type/name</code><br/>
1599 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1600 * For example <code>com.example.myapp</code><br/>
1601 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1602 * or <code>drawable</code>.
1603 * <code>name</code> is the string form of the resource name. That is, whatever the file
1604 * name was in your res directory, without the type extension.
1605 * The easiest way to construct this form is
1606 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1607 * </li>
1608 * </ul>
1609 *
1610 * <p>Note that if this function is called for read-only input (mode is "r")
1611 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001612 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001613 * from any built-in data conversion that a provider implements.
1614 *
1615 * @param uri The desired URI to open.
1616 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1617 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001618 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001619 * none. If the operation is canceled, then
1620 * {@link OperationCanceledException} will be thrown.
1621 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1622 * own this descriptor and are responsible for closing it when done.
1623 * @throws FileNotFoundException Throws FileNotFoundException of no
1624 * file exists under the URI or the mode is invalid.
1625 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001626 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1627 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1628 throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001629 Objects.requireNonNull(uri, "uri");
1630 Objects.requireNonNull(mode, "mode");
Jeff Sharkey673db442015-06-11 19:30:57 -07001631
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001632 try {
1633 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, cancellationSignal);
1634 } catch (RemoteException e) {
1635 return null;
1636 }
1637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 String scheme = uri.getScheme();
1639 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1640 if (!"r".equals(mode)) {
1641 throw new FileNotFoundException("Can't write resources: " + uri);
1642 }
1643 OpenResourceIdResult r = getResourceId(uri);
1644 try {
1645 return r.r.openRawResourceFd(r.id);
1646 } catch (Resources.NotFoundException ex) {
1647 throw new FileNotFoundException("Resource does not exist: " + uri);
1648 }
1649 } else if (SCHEME_FILE.equals(scheme)) {
1650 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001651 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 return new AssetFileDescriptor(pfd, 0, -1);
1653 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001654 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001655 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001656 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001657 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1658 if (unstableProvider == null) {
1659 throw new FileNotFoundException("No content provider: " + uri);
1660 }
1661 IContentProvider stableProvider = null;
1662 AssetFileDescriptor fd = null;
1663
1664 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001665 ICancellationSignal remoteCancellationSignal = null;
1666 if (cancellationSignal != null) {
1667 cancellationSignal.throwIfCanceled();
1668 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1669 cancellationSignal.setRemote(remoteCancellationSignal);
1670 }
1671
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001672 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001673 fd = unstableProvider.openAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001674 mPackageName, mFeatureId, uri, mode, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001675 if (fd == null) {
1676 // The provider will be released by the finally{} clause
1677 return null;
1678 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001679 } catch (DeadObjectException e) {
1680 // The remote process has died... but we only hold an unstable
1681 // reference though, so we might recover!!! Let's try!!!!
1682 // This is exciting!!1!!1!!!!1
1683 unstableProviderDied(unstableProvider);
1684 stableProvider = acquireProvider(uri);
1685 if (stableProvider == null) {
1686 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001687 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001688 fd = stableProvider.openAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001689 mPackageName, mFeatureId, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001690 if (fd == null) {
1691 // The provider will be released by the finally{} clause
1692 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001693 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001694 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001695
1696 if (stableProvider == null) {
1697 stableProvider = acquireProvider(uri);
1698 }
1699 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001700 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001701 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1702 fd.getParcelFileDescriptor(), stableProvider);
1703
1704 // Success! Don't release the provider when exiting, let
1705 // ParcelFileDescriptorInner do that when it is closed.
1706 stableProvider = null;
1707
1708 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1709 fd.getDeclaredLength());
1710
1711 } catch (RemoteException e) {
1712 // Whatever, whatever, we'll go away.
1713 throw new FileNotFoundException(
1714 "Failed opening content provider: " + uri);
1715 } catch (FileNotFoundException e) {
1716 throw e;
1717 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001718 if (cancellationSignal != null) {
1719 cancellationSignal.setRemote(null);
1720 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001721 if (stableProvider != null) {
1722 releaseProvider(stableProvider);
1723 }
1724 if (unstableProvider != null) {
1725 releaseUnstableProvider(unstableProvider);
1726 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001727 }
1728 }
1729 }
1730 }
1731
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001732 @Override
1733 public final @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
1734 @NonNull String mimeTypeFilter, @Nullable Bundle opts,
1735 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001736 try {
1737 if (mWrapped != null) {
1738 return mWrapped.openTypedAssetFile(uri, mimeTypeFilter, opts, signal);
1739 }
1740 } catch (RemoteException e) {
1741 return null;
1742 }
1743
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001744 return openTypedAssetFileDescriptor(uri, mimeTypeFilter, opts, signal);
1745 }
1746
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001747 /**
1748 * Open a raw file descriptor to access (potentially type transformed)
1749 * data from a "content:" URI. This interacts with the underlying
1750 * {@link ContentProvider#openTypedAssetFile} method of the provider
1751 * associated with the given URI, to retrieve retrieve any appropriate
1752 * data stream for the data stored there.
1753 *
1754 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1755 * with "content:" URIs, because content providers are the only facility
1756 * with an associated MIME type to ensure that the returned data stream
1757 * is of the desired type.
1758 *
1759 * <p>All text/* streams are encoded in UTF-8.
1760 *
1761 * @param uri The desired URI to open.
1762 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001763 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001764 * select a type, though there is no way for you to determine what type
1765 * it is returning.
1766 * @param opts Additional provider-dependent options.
1767 * @return Returns a new ParcelFileDescriptor from which you can read the
1768 * data stream from the provider. Note that this may be a pipe, meaning
1769 * you can't seek in it. The only seek you should do is if the
1770 * AssetFileDescriptor contains an offset, to move to that offset before
1771 * reading. You own this descriptor and are responsible for closing it when done.
1772 * @throws FileNotFoundException Throws FileNotFoundException of no
1773 * data of the desired type exists under the URI.
1774 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001775 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1776 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001777 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1778 }
1779
1780 /**
1781 * Open a raw file descriptor to access (potentially type transformed)
1782 * data from a "content:" URI. This interacts with the underlying
1783 * {@link ContentProvider#openTypedAssetFile} method of the provider
1784 * associated with the given URI, to retrieve retrieve any appropriate
1785 * data stream for the data stored there.
1786 *
1787 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1788 * with "content:" URIs, because content providers are the only facility
1789 * with an associated MIME type to ensure that the returned data stream
1790 * is of the desired type.
1791 *
1792 * <p>All text/* streams are encoded in UTF-8.
1793 *
1794 * @param uri The desired URI to open.
1795 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001796 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001797 * select a type, though there is no way for you to determine what type
1798 * it is returning.
1799 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001800 * @param cancellationSignal A signal to cancel the operation in progress,
1801 * or null if none. If the operation is canceled, then
1802 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001803 * @return Returns a new ParcelFileDescriptor from which you can read the
1804 * data stream from the provider. Note that this may be a pipe, meaning
1805 * you can't seek in it. The only seek you should do is if the
1806 * AssetFileDescriptor contains an offset, to move to that offset before
1807 * reading. You own this descriptor and are responsible for closing it when done.
1808 * @throws FileNotFoundException Throws FileNotFoundException of no
1809 * data of the desired type exists under the URI.
1810 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001811 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1812 @NonNull String mimeType, @Nullable Bundle opts,
1813 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001814 Objects.requireNonNull(uri, "uri");
1815 Objects.requireNonNull(mimeType, "mimeType");
Jeff Sharkey673db442015-06-11 19:30:57 -07001816
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001817 try {
1818 if (mWrapped != null) return mWrapped.openTypedAssetFile(uri, mimeType, opts, cancellationSignal);
1819 } catch (RemoteException e) {
1820 return null;
1821 }
1822
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001823 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1824 if (unstableProvider == null) {
1825 throw new FileNotFoundException("No content provider: " + uri);
1826 }
1827 IContentProvider stableProvider = null;
1828 AssetFileDescriptor fd = null;
1829
1830 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001831 ICancellationSignal remoteCancellationSignal = null;
1832 if (cancellationSignal != null) {
1833 cancellationSignal.throwIfCanceled();
1834 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1835 cancellationSignal.setRemote(remoteCancellationSignal);
1836 }
1837
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001838 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001839 fd = unstableProvider.openTypedAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001840 mPackageName, mFeatureId, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001841 if (fd == null) {
1842 // The provider will be released by the finally{} clause
1843 return null;
1844 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001845 } catch (DeadObjectException e) {
1846 // The remote process has died... but we only hold an unstable
1847 // reference though, so we might recover!!! Let's try!!!!
1848 // This is exciting!!1!!1!!!!1
1849 unstableProviderDied(unstableProvider);
1850 stableProvider = acquireProvider(uri);
1851 if (stableProvider == null) {
1852 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001853 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001854 fd = stableProvider.openTypedAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001855 mPackageName, mFeatureId, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001856 if (fd == null) {
1857 // The provider will be released by the finally{} clause
1858 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001861
1862 if (stableProvider == null) {
1863 stableProvider = acquireProvider(uri);
1864 }
1865 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001866 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001867 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1868 fd.getParcelFileDescriptor(), stableProvider);
1869
1870 // Success! Don't release the provider when exiting, let
1871 // ParcelFileDescriptorInner do that when it is closed.
1872 stableProvider = null;
1873
1874 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
Varun Shah840a9c42019-10-17 16:07:53 -07001875 fd.getDeclaredLength(), fd.getExtras());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001876
1877 } catch (RemoteException e) {
1878 // Whatever, whatever, we'll go away.
1879 throw new FileNotFoundException(
1880 "Failed opening content provider: " + uri);
1881 } catch (FileNotFoundException e) {
1882 throw e;
1883 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001884 if (cancellationSignal != null) {
1885 cancellationSignal.setRemote(null);
1886 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001887 if (stableProvider != null) {
1888 releaseProvider(stableProvider);
1889 }
1890 if (unstableProvider != null) {
1891 releaseUnstableProvider(unstableProvider);
1892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894 }
1895
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001896 /**
1897 * A resource identified by the {@link Resources} that contains it, and a resource id.
1898 *
1899 * @hide
1900 */
1901 public class OpenResourceIdResult {
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001902 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001903 public Resources r;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001904 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001905 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001907
1908 /**
1909 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1910 *
1911 * @hide
1912 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001913 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001914 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 String authority = uri.getAuthority();
1916 Resources r;
1917 if (TextUtils.isEmpty(authority)) {
1918 throw new FileNotFoundException("No authority: " + uri);
1919 } else {
1920 try {
1921 r = mContext.getPackageManager().getResourcesForApplication(authority);
1922 } catch (NameNotFoundException ex) {
1923 throw new FileNotFoundException("No package found for authority: " + uri);
1924 }
1925 }
1926 List<String> path = uri.getPathSegments();
1927 if (path == null) {
1928 throw new FileNotFoundException("No path: " + uri);
1929 }
1930 int len = path.size();
1931 int id;
1932 if (len == 1) {
1933 try {
1934 id = Integer.parseInt(path.get(0));
1935 } catch (NumberFormatException e) {
1936 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
1937 }
1938 } else if (len == 2) {
1939 id = r.getIdentifier(path.get(1), path.get(0), authority);
1940 } else {
1941 throw new FileNotFoundException("More than two path segments: " + uri);
1942 }
1943 if (id == 0) {
1944 throw new FileNotFoundException("No resource found for: " + uri);
1945 }
1946 OpenResourceIdResult res = new OpenResourceIdResult();
1947 res.r = r;
1948 res.id = id;
1949 return res;
1950 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 /**
1953 * Inserts a row into a table at the given URL.
1954 *
1955 * If the content provider supports transactions the insertion will be atomic.
1956 *
1957 * @param url The URL of the table to insert into.
1958 * @param values The initial values for the newly inserted row. The key is the column name for
1959 * the field. Passing an empty ContentValues will create an empty row.
Varun Shahc4fdfa32019-05-10 14:10:08 -07001960 * @return the URL of the newly created row. May return <code>null</code> if the underlying
1961 * content provider returns <code>null</code>, or if it crashes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001963 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1964 @Nullable ContentValues values) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001965 return insert(url, values, null);
1966 }
1967
1968 /**
1969 * Inserts a row into a table at the given URL.
1970 *
1971 * If the content provider supports transactions the insertion will be atomic.
1972 *
1973 * @param url The URL of the table to insert into.
1974 * @param values The initial values for the newly inserted row. The key is the column name for
1975 * the field. Passing an empty ContentValues will create an empty row.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001976 * @param extras A Bundle containing additional information necessary for
1977 * the operation. Arguments may include SQL style arguments, such
1978 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1979 * the documentation for each individual provider will indicate
1980 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001981 * @return the URL of the newly created row. May return <code>null</code> if the underlying
1982 * content provider returns <code>null</code>, or if it crashes.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001983 * @throws IllegalArgumentException if the provider doesn't support one of
1984 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001985 */
1986 @Override
1987 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1988 @Nullable ContentValues values, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001989 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001990
1991 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001992 if (mWrapped != null) return mWrapped.insert(url, values, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001993 } catch (RemoteException e) {
1994 return null;
1995 }
1996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 IContentProvider provider = acquireProvider(url);
1998 if (provider == null) {
1999 throw new IllegalArgumentException("Unknown URL " + url);
2000 }
2001 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002002 long startTime = SystemClock.uptimeMillis();
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002003 Uri createdRow = provider.insert(mPackageName, mFeatureId, url, values, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002004 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002005 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
2006 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002008 // Arbitrary and not worth documenting, as Activity
2009 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 return null;
2011 } finally {
2012 releaseProvider(provider);
2013 }
2014 }
2015
Fred Quintana89437372009-05-15 15:10:40 -07002016 /**
Fred Quintana89437372009-05-15 15:10:40 -07002017 * Applies each of the {@link ContentProviderOperation} objects and returns an array
2018 * of their results. Passes through OperationApplicationException, which may be thrown
2019 * by the call to {@link ContentProviderOperation#apply}.
2020 * If all the applications succeed then a {@link ContentProviderResult} array with the
2021 * same number of elements as the operations will be returned. It is implementation-specific
2022 * how many, if any, operations will have been successfully applied if a call to
2023 * apply results in a {@link OperationApplicationException}.
2024 * @param authority the authority of the ContentProvider to which this batch should be applied
2025 * @param operations the operations to apply
2026 * @return the results of the applications
2027 * @throws OperationApplicationException thrown if an application fails.
2028 * See {@link ContentProviderOperation#apply} for more information.
2029 * @throws RemoteException thrown if a RemoteException is encountered while attempting
2030 * to communicate with a remote provider.
2031 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002032 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07002033 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
2034 @NonNull ArrayList<ContentProviderOperation> operations)
2035 throws RemoteException, OperationApplicationException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002036 Objects.requireNonNull(authority, "authority");
2037 Objects.requireNonNull(operations, "operations");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002038
2039 try {
2040 if (mWrapped != null) return mWrapped.applyBatch(authority, operations);
2041 } catch (RemoteException e) {
2042 return null;
2043 }
2044
Fred Quintana89437372009-05-15 15:10:40 -07002045 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002046 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07002047 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002048 }
2049 try {
Fred Quintana89437372009-05-15 15:10:40 -07002050 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002051 } finally {
2052 provider.release();
2053 }
2054 }
2055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 /**
2057 * Inserts multiple rows into a table at the given URL.
2058 *
2059 * This function make no guarantees about the atomicity of the insertions.
2060 *
2061 * @param url The URL of the table to insert into.
2062 * @param values The initial values for the newly inserted rows. The key is the column name for
2063 * the field. Passing null will create an empty row.
2064 * @return the number of newly created rows.
2065 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002066 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07002067 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
2068 @NonNull ContentValues[] values) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002069 Objects.requireNonNull(url, "url");
2070 Objects.requireNonNull(values, "values");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002071
2072 try {
2073 if (mWrapped != null) return mWrapped.bulkInsert(url, values);
2074 } catch (RemoteException e) {
2075 return 0;
2076 }
2077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 IContentProvider provider = acquireProvider(url);
2079 if (provider == null) {
2080 throw new IllegalArgumentException("Unknown URL " + url);
2081 }
2082 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002083 long startTime = SystemClock.uptimeMillis();
Philip P. Moltmann128b7032019-09-27 08:44:12 -07002084 int rowsCreated = provider.bulkInsert(mPackageName, mFeatureId, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002085 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002086 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
2087 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002089 // Arbitrary and not worth documenting, as Activity
2090 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 return 0;
2092 } finally {
2093 releaseProvider(provider);
2094 }
2095 }
2096
2097 /**
2098 * Deletes row(s) specified by a content URI.
2099 *
2100 * If the content provider supports transactions, the deletion will be atomic.
2101 *
2102 * @param url The URL of the row to delete.
2103 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
2104 (excluding the WHERE itself).
2105 * @return The number of rows deleted.
2106 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002107 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07002108 @Nullable String[] selectionArgs) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002109 return delete(url, createSqlQueryBundle(where, selectionArgs));
2110 }
2111
2112 /**
2113 * Deletes row(s) specified by a content URI.
2114 *
2115 * If the content provider supports transactions, the deletion will be atomic.
2116 *
2117 * @param url The URL of the row to delete.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002118 * @param extras A Bundle containing additional information necessary for
2119 * the operation. Arguments may include SQL style arguments, such
2120 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2121 * the documentation for each individual provider will indicate
2122 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002123 * @return The number of rows deleted.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002124 * @throws IllegalArgumentException if the provider doesn't support one of
2125 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002126 */
2127 @Override
2128 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002129 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002130
2131 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002132 if (mWrapped != null) return mWrapped.delete(url, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002133 } catch (RemoteException e) {
2134 return 0;
2135 }
2136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 IContentProvider provider = acquireProvider(url);
2138 if (provider == null) {
2139 throw new IllegalArgumentException("Unknown URL " + url);
2140 }
2141 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002142 long startTime = SystemClock.uptimeMillis();
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002143 int rowsDeleted = provider.delete(mPackageName, mFeatureId, url, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002144 long durationMillis = SystemClock.uptimeMillis() - startTime;
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002145 maybeLogUpdateToEventLog(durationMillis, url, "delete", null);
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002146 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002148 // Arbitrary and not worth documenting, as Activity
2149 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 return -1;
2151 } finally {
2152 releaseProvider(provider);
2153 }
2154 }
2155
2156 /**
2157 * Update row(s) in a content URI.
2158 *
2159 * If the content provider supports transactions the update will be atomic.
2160 *
2161 * @param uri The URI to modify.
2162 * @param values The new field values. The key is the column name for the field.
2163 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08002164 * @param where A filter to apply to rows before updating, formatted as an SQL WHERE clause
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002166 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 * @throws NullPointerException if uri or values are null
2168 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002169 public final int update(@RequiresPermission.Write @NonNull Uri uri,
2170 @Nullable ContentValues values, @Nullable String where,
2171 @Nullable String[] selectionArgs) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002172 return update(uri, values, createSqlQueryBundle(where, selectionArgs));
2173 }
2174
2175 /**
2176 * Update row(s) in a content URI.
2177 *
2178 * If the content provider supports transactions the update will be atomic.
2179 *
2180 * @param uri The URI to modify.
2181 * @param values The new field values. The key is the column name for the field.
2182 A null value will remove an existing field value.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002183 * @param extras A Bundle containing additional information necessary for
2184 * the operation. Arguments may include SQL style arguments, such
2185 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2186 * the documentation for each individual provider will indicate
2187 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002188 * @return the number of rows updated.
2189 * @throws NullPointerException if uri or values are null
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002190 * @throws IllegalArgumentException if the provider doesn't support one of
2191 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002192 */
2193 @Override
2194 public final int update(@RequiresPermission.Write @NonNull Uri uri,
2195 @Nullable ContentValues values, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002196 Objects.requireNonNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002197
2198 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002199 if (mWrapped != null) return mWrapped.update(uri, values, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002200 } catch (RemoteException e) {
2201 return 0;
2202 }
2203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 IContentProvider provider = acquireProvider(uri);
2205 if (provider == null) {
2206 throw new IllegalArgumentException("Unknown URI " + uri);
2207 }
2208 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002209 long startTime = SystemClock.uptimeMillis();
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002210 int rowsUpdated = provider.update(mPackageName, mFeatureId, uri, values, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002211 long durationMillis = SystemClock.uptimeMillis() - startTime;
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002212 maybeLogUpdateToEventLog(durationMillis, uri, "update", null);
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002213 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002215 // Arbitrary and not worth documenting, as Activity
2216 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 return -1;
2218 } finally {
2219 releaseProvider(provider);
2220 }
2221 }
2222
2223 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09002224 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002225 * read or write interfaces which are cheaper than using a Cursor and/or
2226 * do not fit into the traditional table model.
2227 *
2228 * @param method provider-defined method name to call. Opaque to
2229 * framework, but must be non-null.
2230 * @param arg provider-defined String argument. May be null.
2231 * @param extras provider-defined Bundle argument. May be null.
2232 * @return a result Bundle, possibly null. Will be null if the ContentProvider
2233 * does not implement call.
2234 * @throws NullPointerException if uri or method is null
2235 * @throws IllegalArgumentException if uri is not known
2236 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002237 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
2238 @Nullable String arg, @Nullable Bundle extras) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002239 return call(uri.getAuthority(), method, arg, extras);
2240 }
2241
2242 @Override
2243 public final @Nullable Bundle call(@NonNull String authority, @NonNull String method,
2244 @Nullable String arg, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002245 Objects.requireNonNull(authority, "authority");
2246 Objects.requireNonNull(method, "method");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002247
2248 try {
2249 if (mWrapped != null) return mWrapped.call(authority, method, arg, extras);
2250 } catch (RemoteException e) {
2251 return null;
2252 }
2253
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002254 IContentProvider provider = acquireProvider(authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002255 if (provider == null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002256 throw new IllegalArgumentException("Unknown authority " + authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002257 }
2258 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07002259 final Bundle res = provider.call(mPackageName, mFeatureId, authority, method, arg,
2260 extras);
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002261 Bundle.setDefusable(res, true);
2262 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002263 } catch (RemoteException e) {
2264 // Arbitrary and not worth documenting, as Activity
2265 // Manager will kill this process shortly anyway.
2266 return null;
2267 } finally {
2268 releaseProvider(provider);
2269 }
2270 }
2271
2272 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002273 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 *
2275 * @param uri The URI to a content provider
2276 * @return The ContentProvider for the given URI, or null if no content provider is found.
2277 * @hide
2278 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002279 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002280 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04002281 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 return null;
2283 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002284 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002286 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
2288 return null;
2289 }
2290
2291 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002292 * Returns the content provider for the given content URI if the process
2293 * already has a reference on it.
2294 *
2295 * @param uri The URI to a content provider
2296 * @return The ContentProvider for the given URI, or null if no content provider is found.
2297 * @hide
2298 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002299 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002300 public final IContentProvider acquireExistingProvider(Uri uri) {
2301 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2302 return null;
2303 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002304 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002305 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002306 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002307 }
2308 return null;
2309 }
2310
2311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 * @hide
2313 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002314 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002316 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 return null;
2318 }
2319 return acquireProvider(mContext, name);
2320 }
2321
2322 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002323 * Returns the content provider for the given content URI.
2324 *
2325 * @param uri The URI to a content provider
2326 * @return The ContentProvider for the given URI, or null if no content provider is found.
2327 * @hide
2328 */
2329 public final IContentProvider acquireUnstableProvider(Uri uri) {
2330 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2331 return null;
2332 }
2333 String auth = uri.getAuthority();
2334 if (auth != null) {
2335 return acquireUnstableProvider(mContext, uri.getAuthority());
2336 }
2337 return null;
2338 }
2339
2340 /**
2341 * @hide
2342 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002343 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002344 public final IContentProvider acquireUnstableProvider(String name) {
2345 if (name == null) {
2346 return null;
2347 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002348 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002349 }
2350
2351 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07002352 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2353 * that services the content at uri, starting the provider if necessary. Returns
2354 * null if there is no provider associated wih the uri. The caller must indicate that they are
2355 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002356 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002357 * keeping it active.
2358 * @param uri specifies which provider should be acquired
2359 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2360 * that services the content at uri or null if there isn't one.
2361 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002362 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002363 Objects.requireNonNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07002364 IContentProvider provider = acquireProvider(uri);
2365 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002366 return new ContentProviderClient(this, provider, uri.getAuthority(), true);
Fred Quintana718d8a22009-04-29 17:53:20 -07002367 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002368 return null;
2369 }
2370
2371 /**
2372 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2373 * with the authority of name, starting the provider if necessary. Returns
2374 * null if there is no provider associated wih the uri. The caller must indicate that they are
2375 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002376 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002377 * keeping it active.
2378 * @param name specifies which provider should be acquired
2379 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2380 * with the authority of name or null if there isn't one.
2381 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002382 public final @Nullable ContentProviderClient acquireContentProviderClient(
2383 @NonNull String name) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002384 Objects.requireNonNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07002385 IContentProvider provider = acquireProvider(name);
2386 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002387 return new ContentProviderClient(this, provider, name, true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002388 }
2389
2390 return null;
2391 }
2392
2393 /**
2394 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
2395 * not trust the stability of the target content provider. This turns off
2396 * the mechanism in the platform clean up processes that are dependent on
2397 * a content provider if that content provider's process goes away. Normally
2398 * you can safely assume that once you have acquired a provider, you can freely
2399 * use it as needed and it won't disappear, even if your process is in the
2400 * background. If using this method, you need to take care to deal with any
2401 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002402 * so that it can be re-opened later. In particular, catching a
2403 * {@link android.os.DeadObjectException} from the calls there will let you
2404 * know that the content provider has gone away; at that point the current
2405 * ContentProviderClient object is invalid, and you should release it. You
2406 * can acquire a new one if you would like to try to restart the provider
2407 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002408 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002409 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2410 @NonNull Uri uri) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002411 Objects.requireNonNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002412 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002413 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002414 return new ContentProviderClient(this, provider, uri.getAuthority(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002415 }
2416
2417 return null;
2418 }
2419
2420 /**
2421 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
2422 * not trust the stability of the target content provider. This turns off
2423 * the mechanism in the platform clean up processes that are dependent on
2424 * a content provider if that content provider's process goes away. Normally
2425 * you can safely assume that once you have acquired a provider, you can freely
2426 * use it as needed and it won't disappear, even if your process is in the
2427 * background. If using this method, you need to take care to deal with any
2428 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002429 * so that it can be re-opened later. In particular, catching a
2430 * {@link android.os.DeadObjectException} from the calls there will let you
2431 * know that the content provider has gone away; at that point the current
2432 * ContentProviderClient object is invalid, and you should release it. You
2433 * can acquire a new one if you would like to try to restart the provider
2434 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002435 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002436 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2437 @NonNull String name) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002438 Objects.requireNonNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002439 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002440 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002441 return new ContentProviderClient(this, provider, name, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07002442 }
2443
2444 return null;
2445 }
2446
2447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 * Register an observer class that gets callbacks when data identified by a
2449 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002450 * <p>
2451 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2452 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002454 * @param uri The URI to watch for changes. This can be a specific row URI,
2455 * or a base URI for a whole class of content.
2456 * @param notifyForDescendants When false, the observer will be notified
2457 * whenever a change occurs to the exact URI specified by
2458 * <code>uri</code> or to one of the URI's ancestors in the path
2459 * hierarchy. When true, the observer will also be notified
2460 * whenever a change occurs to the URI's descendants in the path
2461 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 * @param observer The object that receives callbacks when changes occur.
2463 * @see #unregisterContentObserver
2464 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002465 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07002466 @NonNull ContentObserver observer) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002467 Objects.requireNonNull(uri, "uri");
2468 Objects.requireNonNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01002469 registerContentObserver(
2470 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002471 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01002472 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002473 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002474 }
2475
2476 /** @hide - designated user version */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002477 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07002478 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002479 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002481 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002482 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002484 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 }
2486 }
2487
2488 /**
2489 * Unregisters a change observer.
2490 *
2491 * @param observer The previously registered observer that is no longer needed.
2492 * @see #registerContentObserver
2493 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002494 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002495 Objects.requireNonNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 try {
2497 IContentObserver contentObserver = observer.releaseContentObserver();
2498 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002499 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 contentObserver);
2501 }
2502 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002503 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 }
2505 }
2506
2507 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002508 * Notify registered observers that a row was updated and attempt to sync
2509 * changes to the network.
2510 * <p>
2511 * To observe events sent through this call, use
2512 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2513 * <p>
2514 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2515 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 *
Jeff Brown86de0592012-01-23 13:01:18 -08002517 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002518 * @param observer The observer that originated the change, may be
2519 * <code>null</null>. The observer that originated the change
2520 * will only receive the notification if it has requested to
2521 * receive self-change notifications by implementing
2522 * {@link ContentObserver#deliverSelfNotifications()} to return
2523 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002525 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 notifyChange(uri, observer, true /* sync to network */);
2527 }
2528
2529 /**
2530 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002531 * <p>
2532 * To observe events sent through this call, use
2533 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2534 * <p>
2535 * If syncToNetwork is true, this will attempt to schedule a local sync
2536 * using the sync adapter that's registered for the authority of the
2537 * provided uri. No account will be passed to the sync adapter, so all
2538 * matching accounts will be synchronized.
2539 * <p>
2540 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2541 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 *
Jeff Brown86de0592012-01-23 13:01:18 -08002543 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002544 * @param observer The observer that originated the change, may be
2545 * <code>null</null>. The observer that originated the change
2546 * will only receive the notification if it has requested to
2547 * receive self-change notifications by implementing
2548 * {@link ContentObserver#deliverSelfNotifications()} to return
2549 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002550 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002551 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
Jeff Sharkey1307f422019-11-13 13:03:10 -07002552 * @deprecated callers should consider migrating to
2553 * {@link #notifyChange(Uri, ContentObserver, int)}, as it
2554 * offers support for many more options than just
2555 * {@link #NOTIFY_SYNC_TO_NETWORK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002557 @Deprecated
Jeff Sharkey673db442015-06-11 19:30:57 -07002558 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2559 boolean syncToNetwork) {
Jeff Sharkey1307f422019-11-13 13:03:10 -07002560 notifyChange(uri, observer, syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0);
Christopher Tate16aa9732012-09-17 16:23:44 -07002561 }
2562
2563 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002564 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002565 * <p>
2566 * To observe events sent through this call, use
2567 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2568 * <p>
Jeff Sharkey1307f422019-11-13 13:03:10 -07002569 * If {@link #NOTIFY_SYNC_TO_NETWORK} is set, this will attempt to schedule
2570 * a local sync using the sync adapter that's registered for the authority
2571 * of the provided uri. No account will be passed to the sync adapter, so
2572 * all matching accounts will be synchronized.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002573 * <p>
2574 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2575 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002576 *
2577 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002578 * @param observer The observer that originated the change, may be
2579 * <code>null</null>. The observer that originated the change
2580 * will only receive the notification if it has requested to
2581 * receive self-change notifications by implementing
2582 * {@link ContentObserver#deliverSelfNotifications()} to return
2583 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002584 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2585 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2586 */
2587 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2588 @NotifyFlags int flags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002589 Objects.requireNonNull(uri, "uri");
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002590 notifyChange(
2591 ContentProvider.getUriWithoutUserId(uri),
2592 observer,
2593 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002594 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002595 }
2596
2597 /**
Jeff Sharkey1307f422019-11-13 13:03:10 -07002598 * Notify registered observers that several rows have been updated.
2599 * <p>
2600 * To observe events sent through this call, use
2601 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2602 * <p>
2603 * If {@link #NOTIFY_SYNC_TO_NETWORK} is set, this will attempt to schedule
2604 * a local sync using the sync adapter that's registered for the authority
2605 * of the provided uri. No account will be passed to the sync adapter, so
2606 * all matching accounts will be synchronized.
2607 * <p>
2608 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2609 * notifications must be backed by a valid {@link ContentProvider}.
2610 *
2611 * @param uris The uris of the content that was changed.
2612 * @param observer The observer that originated the change, may be
2613 * <code>null</null>. The observer that originated the change
2614 * will only receive the notification if it has requested to
2615 * receive self-change notifications by implementing
2616 * {@link ContentObserver#deliverSelfNotifications()} to return
2617 * true.
2618 * @param flags Flags such as {@link #NOTIFY_SYNC_TO_NETWORK} or
2619 * {@link #NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS}.
2620 */
2621 public void notifyChange(@NonNull Iterable<Uri> uris, @Nullable ContentObserver observer,
2622 @NotifyFlags int flags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002623 Objects.requireNonNull(uris, "uris");
Jeff Sharkey1307f422019-11-13 13:03:10 -07002624
2625 // Cluster based on user ID
2626 final SparseArray<ArrayList<Uri>> clusteredByUser = new SparseArray<>();
2627 for (Uri uri : uris) {
2628 final int userId = ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
2629 ArrayList<Uri> list = clusteredByUser.get(userId);
2630 if (list == null) {
2631 list = new ArrayList<>();
2632 clusteredByUser.put(userId, list);
2633 }
2634 list.add(ContentProvider.getUriWithoutUserId(uri));
2635 }
2636
2637 for (int i = 0; i < clusteredByUser.size(); i++) {
2638 final int userId = clusteredByUser.keyAt(i);
2639 final ArrayList<Uri> list = clusteredByUser.valueAt(i);
2640 notifyChange(list.toArray(new Uri[list.size()]), observer, flags, userId);
2641 }
2642 }
2643
2644 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002645 * Notify registered observers within the designated user(s) that a row was updated.
2646 *
Jeff Sharkey1307f422019-11-13 13:03:10 -07002647 * @deprecated callers should consider migrating to
2648 * {@link #notifyChange(Uri, ContentObserver, int)}, as it
2649 * offers support for many more options than just
2650 * {@link #NOTIFY_SYNC_TO_NETWORK}.
Christopher Tate16aa9732012-09-17 16:23:44 -07002651 * @hide
2652 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002653 @Deprecated
Dianne Hackborne7617772016-04-27 17:03:52 -07002654 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002655 @UserIdInt int userHandle) {
Jeff Sharkey1307f422019-11-13 13:03:10 -07002656 notifyChange(uri, observer, syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0, userHandle);
2657 }
2658
2659 /** {@hide} */
2660 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
2661 @UserIdInt int userHandle) {
2662 notifyChange(new Uri[] { uri }, observer, flags, userHandle);
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002663 }
2664
2665 /**
2666 * Notify registered observers within the designated user(s) that a row was updated.
2667 *
2668 * @hide
2669 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002670 public void notifyChange(@NonNull Uri[] uris, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002671 @UserIdInt int userHandle) {
2672 try {
2673 getContentService().notifyChange(
Jeff Sharkey1307f422019-11-13 13:03:10 -07002674 uris, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002675 observer != null && observer.deliverSelfNotifications(), flags,
Makoto Onukie183a402018-08-29 11:46:41 -07002676 userHandle, mTargetSdkVersion, mContext.getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002678 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 }
2680 }
2681
2682 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002683 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002684 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002685 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002686 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2687 * the grant has already been persisted, taking it again will touch
2688 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002689 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002690 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002691 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002692 public void takePersistableUriPermission(@NonNull Uri uri,
2693 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002694 Objects.requireNonNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002695 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002696 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002697 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2698 resolveUserId(uri));
2699 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002700 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002701 }
2702 }
2703
2704 /**
2705 * @hide
2706 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002707 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002708 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2709 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002710 Objects.requireNonNull(toPackage, "toPackage");
2711 Objects.requireNonNull(uri, "uri");
Felipe Leme988234a2018-02-14 12:00:29 -08002712 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002713 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002714 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2715 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002716 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002717 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002718 }
2719 }
2720
2721 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002722 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002723 * previously made persistent with
2724 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2725 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002726 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002727 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002728 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002729 public void releasePersistableUriPermission(@NonNull Uri uri,
2730 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002731 Objects.requireNonNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002732 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002733 UriGrantsManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002734 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2735 resolveUserId(uri));
2736 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002737 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002738 }
2739 }
2740
2741 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002742 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002743 * calling app. That is, the returned permissions have been granted
2744 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002745 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002746 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002747 *
2748 * @see #takePersistableUriPermission(Uri, int)
2749 * @see #releasePersistableUriPermission(Uri, int)
2750 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002751 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002752 try {
Sudheer Shanka1b817f62019-05-20 16:54:59 -07002753 return UriGrantsManager.getService().getUriPermissions(
2754 mPackageName, true /* incoming */, true /* persistedOnly */).getList();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002755 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002756 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002757 }
2758 }
2759
2760 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002761 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002762 * calling app. That is, the returned permissions have been granted
2763 * <em>from</em> the calling app. Only grants taken with
2764 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002765 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002766 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002767 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002768 try {
Sudheer Shanka1b817f62019-05-20 16:54:59 -07002769 return UriGrantsManager.getService().getUriPermissions(
2770 mPackageName, false /* incoming */, true /* persistedOnly */).getList();
2771 } catch (RemoteException e) {
2772 throw e.rethrowFromSystemServer();
2773 }
2774 }
2775
2776 /** @hide */
2777 public @NonNull List<UriPermission> getOutgoingUriPermissions() {
2778 try {
2779 return UriGrantsManager.getService().getUriPermissions(
2780 mPackageName, false /* incoming */, false /* persistedOnly */).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002781 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002782 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002783 }
2784 }
2785
2786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 * Start an asynchronous sync operation. If you want to monitor the progress
2788 * of the sync you may register a SyncObserver. Only values of the following
2789 * types may be used in the extras bundle:
2790 * <ul>
2791 * <li>Integer</li>
2792 * <li>Long</li>
2793 * <li>Boolean</li>
2794 * <li>Float</li>
2795 * <li>Double</li>
2796 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002797 * <li>Account</li>
2798 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 * </ul>
2800 *
2801 * @param uri the uri of the provider to sync or null to sync all providers.
2802 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002803 * @deprecated instead use
2804 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002806 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002808 Account account = null;
2809 if (extras != null) {
2810 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2811 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002812 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002813 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002814 }
2815 extras.remove(SYNC_EXTRAS_ACCOUNT);
2816 }
2817 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2818 }
2819
2820 /**
2821 * Start an asynchronous sync operation. If you want to monitor the progress
2822 * of the sync you may register a SyncObserver. Only values of the following
2823 * types may be used in the extras bundle:
2824 * <ul>
2825 * <li>Integer</li>
2826 * <li>Long</li>
2827 * <li>Boolean</li>
2828 * <li>Float</li>
2829 * <li>Double</li>
2830 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002831 * <li>Account</li>
2832 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002833 * </ul>
2834 *
2835 * @param account which account should be synced
2836 * @param authority which authority should be synced
2837 * @param extras any extras to pass to the SyncAdapter.
2838 */
2839 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002840 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002841 }
2842
2843 /**
2844 * @see #requestSync(Account, String, Bundle)
2845 * @hide
2846 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002847 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002848 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002849 if (extras == null) {
2850 throw new IllegalArgumentException("Must specify extras.");
2851 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002852 SyncRequest request =
2853 new SyncRequest.Builder()
2854 .setSyncAdapter(account, authority)
2855 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002856 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002857 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002858 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002859 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2860 // case, but it's only for debugging.
2861 getContentService().syncAsUser(request, userId, ActivityThread.currentPackageName());
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002862 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002863 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002864 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002865 }
2866
2867 /**
2868 * Register a sync with the SyncManager. These requests are built using the
2869 * {@link SyncRequest.Builder}.
2870 */
2871 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002873 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2874 // case, but it's only for debugging.
2875 getContentService().sync(request, ActivityThread.currentPackageName());
Matthew Williamsfa774182013-06-18 15:44:11 -07002876 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002877 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 }
2879 }
2880
2881 /**
2882 * Check that only values of the following types are in the Bundle:
2883 * <ul>
2884 * <li>Integer</li>
2885 * <li>Long</li>
2886 * <li>Boolean</li>
2887 * <li>Float</li>
2888 * <li>Double</li>
2889 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002890 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 * <li>null</li>
2892 * </ul>
2893 * @param extras the Bundle to check
2894 */
2895 public static void validateSyncExtrasBundle(Bundle extras) {
2896 try {
2897 for (String key : extras.keySet()) {
2898 Object value = extras.get(key);
2899 if (value == null) continue;
2900 if (value instanceof Long) continue;
2901 if (value instanceof Integer) continue;
2902 if (value instanceof Boolean) continue;
2903 if (value instanceof Float) continue;
2904 if (value instanceof Double) continue;
2905 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002906 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 throw new IllegalArgumentException("unexpected value type: "
2908 + value.getClass().getName());
2909 }
2910 } catch (IllegalArgumentException e) {
2911 throw e;
2912 } catch (RuntimeException exc) {
2913 throw new IllegalArgumentException("error unparceling Bundle", exc);
2914 }
2915 }
2916
Fred Quintanaac9385e2009-06-22 18:00:59 -07002917 /**
2918 * Cancel any active or pending syncs that match the Uri. If the uri is null then
2919 * all syncs will be canceled.
2920 *
2921 * @param uri the uri of the provider to sync or null to sync all providers.
2922 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
2923 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002924 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002926 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
2927 }
2928
2929 /**
2930 * Cancel any active or pending syncs that match account and authority. The account and
2931 * authority can each independently be set to null, which means that syncs with any account
2932 * or authority, respectively, will match.
2933 *
2934 * @param account filters the syncs that match by this account
2935 * @param authority filters the syncs that match by this authority
2936 */
2937 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002939 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002941 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 }
2943 }
2944
Fred Quintanaac9385e2009-06-22 18:00:59 -07002945 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002946 * @see #cancelSync(Account, String)
2947 * @hide
2948 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002949 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002950 try {
2951 getContentService().cancelSyncAsUser(account, authority, null, userId);
2952 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002953 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002954 }
2955 }
2956
2957 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002958 * Get information about the SyncAdapters that are known to the system.
2959 * @return an array of SyncAdapters that have registered with the system
2960 */
2961 public static SyncAdapterType[] getSyncAdapterTypes() {
2962 try {
2963 return getContentService().getSyncAdapterTypes();
2964 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002965 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002966 }
2967 }
2968
2969 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002970 * @see #getSyncAdapterTypes()
2971 * @hide
2972 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002973 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002974 try {
2975 return getContentService().getSyncAdapterTypesAsUser(userId);
2976 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002977 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002978 }
2979 }
2980
2981 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07002982 * @hide
2983 * Returns the package names of syncadapters that match a given user and authority.
2984 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07002985 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07002986 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002987 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002988 try {
2989 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
2990 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002991 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07002992 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07002993 }
2994
2995 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002996 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002997 * <p>This method requires the caller to hold the permission
2998 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002999 *
3000 * @param account the account whose setting we are querying
3001 * @param authority the provider whose setting we are querying
3002 * @return true if the provider should be synced when a network tickle is received
3003 */
3004 public static boolean getSyncAutomatically(Account account, String authority) {
3005 try {
3006 return getContentService().getSyncAutomatically(account, authority);
3007 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003008 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003009 }
3010 }
3011
3012 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003013 * @see #getSyncAutomatically(Account, String)
3014 * @hide
3015 */
3016 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003017 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003018 try {
3019 return getContentService().getSyncAutomaticallyAsUser(account, authority, userId);
3020 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003021 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003022 }
3023 }
3024
3025 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003026 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003027 * <p>This method requires the caller to hold the permission
3028 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003029 *
3030 * @param account the account whose setting we are querying
3031 * @param authority the provider whose behavior is being controlled
3032 * @param sync true if the provider should be synced when tickles are received for it
3033 */
3034 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003035 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003036 }
3037
3038 /**
3039 * @see #setSyncAutomatically(Account, String, boolean)
3040 * @hide
3041 */
3042 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003043 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003044 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003045 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003046 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003047 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003048 }
3049 }
3050
3051 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003052 * Specifies that a sync should be requested with the specified the account, authority,
3053 * and extras at the given frequency. If there is already another periodic sync scheduled
3054 * with the account, authority and extras then a new periodic sync won't be added, instead
3055 * the frequency of the previous one will be updated.
3056 * <p>
3057 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
3058 * Although these sync are scheduled at the specified frequency, it may take longer for it to
3059 * actually be started if other syncs are ahead of it in the sync operation queue. This means
3060 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08003061 * <p>
3062 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
3063 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
3064 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
3065 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
3066 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003067 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003068 * <p>This method requires the caller to hold the permission
3069 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003070 * <p>The bundle for a periodic sync can be queried by applications with the correct
3071 * permissions using
3072 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
3073 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003074 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003075 * @param account the account to specify in the sync
3076 * @param authority the provider to specify in the sync request
3077 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07003078 * @param pollFrequency how frequently the sync should be performed, in seconds.
3079 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
3080 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08003081 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
3082 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003083 */
3084 public static void addPeriodicSync(Account account, String authority, Bundle extras,
3085 long pollFrequency) {
3086 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08003087 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08003088 throw new IllegalArgumentException("illegal extras were set");
3089 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003090 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07003091 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003092 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003093 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003094 }
3095 }
3096
3097 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003098 * {@hide}
3099 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
3100 * extras were set for a periodic sync.
3101 *
3102 * @param extras bundle to validate.
3103 */
3104 public static boolean invalidPeriodicExtras(Bundle extras) {
3105 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
3106 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
3107 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
3108 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
3109 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
3110 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
3111 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
3112 return true;
3113 }
3114 return false;
3115 }
3116
3117 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003118 * Remove a periodic sync. Has no affect if account, authority and extras don't match
3119 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003120 * <p>This method requires the caller to hold the permission
3121 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003122 *
3123 * @param account the account of the periodic sync to remove
3124 * @param authority the provider of the periodic sync to remove
3125 * @param extras the extras of the periodic sync to remove
3126 */
3127 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
3128 validateSyncExtrasBundle(extras);
3129 try {
3130 getContentService().removePeriodicSync(account, authority, extras);
3131 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003132 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003133 }
3134 }
3135
3136 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003137 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
3138 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07003139 * <p>
3140 * If a periodic sync is specified, the caller must hold the permission
3141 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
3142 *</p>
3143 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003144 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07003145 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07003146 *
3147 * @param request SyncRequest object containing information about sync to cancel.
3148 */
3149 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003150 if (request == null) {
3151 throw new IllegalArgumentException("request cannot be null");
3152 }
3153 try {
3154 getContentService().cancelRequest(request);
3155 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003156 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003157 }
Matthew Williamsfa774182013-06-18 15:44:11 -07003158 }
3159
3160 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003161 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003162 * <p>This method requires the caller to hold the permission
3163 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003164 *
3165 * @param account the account whose periodic syncs we are querying
3166 * @param authority the provider whose periodic syncs we are querying
3167 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
3168 */
3169 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
3170 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003171 return getContentService().getPeriodicSyncs(account, authority, null);
3172 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003173 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003174 }
3175 }
3176
3177 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07003178 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003179 * <p>This method requires the caller to hold the permission
3180 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07003181 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
3182 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07003183 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07003184 try {
3185 return getContentService().getIsSyncable(account, authority);
3186 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003187 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07003188 }
3189 }
3190
3191 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003192 * @see #getIsSyncable(Account, String)
3193 * @hide
3194 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003195 public static int getIsSyncableAsUser(Account account, String authority,
3196 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003197 try {
3198 return getContentService().getIsSyncableAsUser(account, authority, userId);
3199 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003200 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003201 }
3202 }
3203
3204 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07003205 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003206 * <p>This method requires the caller to hold the permission
3207 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07003208 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07003209 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07003210 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07003211 try {
3212 getContentService().setIsSyncable(account, authority, syncable);
3213 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003214 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07003215 }
3216 }
3217
3218 /**
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +00003219 * @see #setIsSyncable(Account, String, int)
3220 * @hide
3221 */
3222 public static void setIsSyncableAsUser(Account account, String authority, int syncable,
3223 int userId) {
3224 try {
3225 getContentService().setIsSyncableAsUser(account, authority, syncable, userId);
3226 } catch (RemoteException e) {
3227 throw e.rethrowFromSystemServer();
3228 }
3229 }
3230
3231 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003232 * Gets the master auto-sync setting that applies to all the providers and accounts.
3233 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003234 * <p>This method requires the caller to hold the permission
3235 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003236 *
3237 * @return the master auto-sync setting that applies to all the providers and accounts
3238 */
3239 public static boolean getMasterSyncAutomatically() {
3240 try {
3241 return getContentService().getMasterSyncAutomatically();
3242 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003243 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003244 }
3245 }
3246
3247 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003248 * @see #getMasterSyncAutomatically()
3249 * @hide
3250 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003251 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003252 try {
3253 return getContentService().getMasterSyncAutomaticallyAsUser(userId);
3254 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003255 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003256 }
3257 }
3258
3259 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003260 * Sets the master auto-sync setting that applies to all the providers and accounts.
3261 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003262 * <p>This method requires the caller to hold the permission
3263 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003264 *
3265 * @param sync the master auto-sync setting that applies to all the providers and accounts
3266 */
3267 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003268 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01003269 }
3270
3271 /**
3272 * @see #setMasterSyncAutomatically(boolean)
3273 * @hide
3274 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003275 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003276 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01003277 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003278 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003279 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003280 }
3281 }
3282
3283 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003284 * Returns true if there is currently a sync operation for the given account or authority
3285 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003286 * <p>This method requires the caller to hold the permission
3287 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003288 * @param account the account whose setting we are querying
3289 * @param authority the provider whose behavior is being queried
3290 * @return true if a sync is active for the given account or authority.
3291 */
3292 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003293 if (account == null) {
3294 throw new IllegalArgumentException("account must not be null");
3295 }
3296 if (authority == null) {
3297 throw new IllegalArgumentException("authority must not be null");
3298 }
3299
Fred Quintanaac9385e2009-06-22 18:00:59 -07003300 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003301 return getContentService().isSyncActive(account, authority, null);
3302 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003303 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003304 }
3305 }
3306
Fred Quintanaac9385e2009-06-22 18:00:59 -07003307 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07003308 * If a sync is active returns the information about it, otherwise returns null.
3309 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003310 * This method requires the caller to hold the permission
3311 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3312 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003313 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07003314 * @deprecated
3315 * Since multiple concurrent syncs are now supported you should use
3316 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
3317 * This method returns the first item from the list of current syncs
3318 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003319 */
Fred Quintanac6a69552010-09-27 17:05:04 -07003320 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003321 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003322 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07003323 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
3324 if (syncs.isEmpty()) {
3325 return null;
3326 }
3327 return syncs.get(0);
3328 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003329 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07003330 }
3331 }
3332
3333 /**
3334 * Returns a list with information about all the active syncs. This list will be empty
3335 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003336 * <p>
3337 * This method requires the caller to hold the permission
3338 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3339 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07003340 * @return a List of SyncInfo objects for the currently active syncs.
3341 */
3342 public static List<SyncInfo> getCurrentSyncs() {
3343 try {
3344 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003345 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003346 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003347 }
3348 }
3349
3350 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003351 * @see #getCurrentSyncs()
3352 * @hide
3353 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003354 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003355 try {
3356 return getContentService().getCurrentSyncsAsUser(userId);
3357 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003358 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003359 }
3360 }
3361
3362 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07003363 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003364 * @param account the account whose setting we are querying
3365 * @param authority the provider whose behavior is being queried
3366 * @return the SyncStatusInfo for the authority, or null if none exists
3367 * @hide
3368 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003369 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07003370 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
3371 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003372 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003373 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003374 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003375 }
3376 }
3377
3378 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003379 * @see #getSyncStatus(Account, String)
3380 * @hide
3381 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003382 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003383 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003384 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003385 try {
3386 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
3387 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003388 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003389 }
3390 }
3391
3392 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003393 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003394 * <p>This method requires the caller to hold the permission
3395 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003396 * @param account the account whose setting we are querying
3397 * @param authority the provider whose behavior is being queried
3398 * @return true if there is a pending sync with the matching account and authority
3399 */
3400 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003401 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003402 }
3403
3404 /**
3405 * @see #requestSync(Account, String, Bundle)
3406 * @hide
3407 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003408 public static boolean isSyncPendingAsUser(Account account, String authority,
3409 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003410 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003411 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003412 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003413 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003414 }
3415 }
3416
Fred Quintana1b487ec2010-02-26 10:57:55 -08003417 /**
3418 * Request notifications when the different aspects of the SyncManager change. The
3419 * different items that can be requested are:
3420 * <ul>
3421 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
3422 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
3423 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
3424 * </ul>
3425 * The caller can set one or more of the status types in the mask for any
3426 * given listener registration.
3427 * @param mask the status change types that will cause the callback to be invoked
3428 * @param callback observer to be invoked when the status changes
3429 * @return a handle that can be used to remove the listener at a later time
3430 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003431 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003432 if (callback == null) {
3433 throw new IllegalArgumentException("you passed in a null callback");
3434 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003435 try {
3436 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08003437 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07003438 public void onStatusChanged(int which) throws RemoteException {
3439 callback.onStatusChanged(which);
3440 }
3441 };
3442 getContentService().addStatusChangeListener(mask, observer);
3443 return observer;
3444 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003445 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003446 }
3447 }
3448
Fred Quintana1b487ec2010-02-26 10:57:55 -08003449 /**
3450 * Remove a previously registered status change listener.
3451 * @param handle the handle that was returned by {@link #addStatusChangeListener}
3452 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003453 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003454 if (handle == null) {
3455 throw new IllegalArgumentException("you passed in a null handle");
3456 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003457 try {
3458 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
3459 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003460 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003461 }
3462 }
3463
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003464 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003465 * Store the given {@link Bundle} as a long-lived cached object within the
3466 * system. This can be useful to avoid expensive re-parsing when apps are
3467 * restarted multiple times on low-RAM devices.
3468 * <p>
3469 * The {@link Bundle} is automatically invalidated when a
3470 * {@link #notifyChange(Uri, ContentObserver)} event applies to the key.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003471 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003472 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003473 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003474 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003475 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003476 public void putCache(@NonNull Uri key, @Nullable Bundle value) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003477 try {
3478 getContentService().putCache(mContext.getPackageName(), key, value,
3479 mContext.getUserId());
3480 } catch (RemoteException e) {
3481 throw e.rethrowFromSystemServer();
3482 }
3483 }
3484
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003485 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003486 * Retrieve the last {@link Bundle} stored as a long-lived cached object
3487 * within the system.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003488 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003489 * @return {@code null} if no cached object has been stored, or if the
3490 * stored object has been invalidated due to a
3491 * {@link #notifyChange(Uri, ContentObserver)} event.
3492 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003493 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003494 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003495 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003496 public @Nullable Bundle getCache(@NonNull Uri key) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003497 try {
3498 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
3499 mContext.getUserId());
3500 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
3501 return bundle;
3502 } catch (RemoteException e) {
3503 throw e.rethrowFromSystemServer();
3504 }
3505 }
3506
Ben Lin8ea82002017-03-08 17:30:16 -08003507 /** {@hide} */
3508 public int getTargetSdkVersion() {
3509 return mTargetSdkVersion;
3510 }
3511
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003512 /**
3513 * Returns sampling percentage for a given duration.
3514 *
3515 * Always returns at least 1%.
3516 */
3517 private int samplePercentForDuration(long durationMillis) {
3518 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
3519 return 100;
3520 }
3521 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
3522 }
3523
Steve McKayea93fe72016-12-02 11:35:35 -08003524 private void maybeLogQueryToEventLog(
3525 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003526 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003527 int samplePercent = samplePercentForDuration(durationMillis);
3528 if (samplePercent < 100) {
3529 synchronized (mRandom) {
3530 if (mRandom.nextInt(100) >= samplePercent) {
3531 return;
3532 }
3533 }
3534 }
3535
Steve McKayea93fe72016-12-02 11:35:35 -08003536 // Ensure a non-null bundle.
3537 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
3538
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003539 StringBuilder projectionBuffer = new StringBuilder(100);
3540 if (projection != null) {
3541 for (int i = 0; i < projection.length; ++i) {
3542 // Note: not using a comma delimiter here, as the
3543 // multiple arguments to EventLog.writeEvent later
3544 // stringify with a comma delimiter, which would make
3545 // parsing uglier later.
3546 if (i != 0) projectionBuffer.append('/');
3547 projectionBuffer.append(projection[i]);
3548 }
3549 }
3550
3551 // ActivityThread.currentPackageName() only returns non-null if the
3552 // current thread is an application main thread. This parameter tells
3553 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003554 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003555
3556 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003557 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003558 uri.toString(),
3559 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08003560 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
3561 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003562 durationMillis,
3563 blockingPackage != null ? blockingPackage : "",
3564 samplePercent);
3565 }
3566
3567 private void maybeLogUpdateToEventLog(
3568 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003569 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003570 int samplePercent = samplePercentForDuration(durationMillis);
3571 if (samplePercent < 100) {
3572 synchronized (mRandom) {
3573 if (mRandom.nextInt(100) >= samplePercent) {
3574 return;
3575 }
3576 }
3577 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003578 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003579 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003580 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003581 uri.toString(),
3582 operation,
3583 selection != null ? selection : "",
3584 durationMillis,
3585 blockingPackage != null ? blockingPackage : "",
3586 samplePercent);
3587 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003588
Jeff Brown825c5132011-10-12 16:11:30 -07003589 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003590 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003591 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003592
3593 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003595 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003597 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003598 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
3600
3601 @Override
3602 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003603 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003605
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003606 if (mProviderReleased.compareAndSet(false, true)) {
3607 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 }
3610
3611 @Override
3612 protected void finalize() throws Throwable {
3613 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003614 if (mCloseGuard != null) {
3615 mCloseGuard.warnIfOpen();
3616 }
3617
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003618 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 } finally {
3620 super.finalize();
3621 }
3622 }
3623 }
3624
3625 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003626 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003627 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628
3629 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3630 super(pfd);
3631 mContentProvider = icp;
3632 }
3633
3634 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003635 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003636 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003637 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 }
3639 }
3640 }
3641
Dianne Hackborn231cc602009-04-27 17:10:36 -07003642 /** @hide */
3643 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003644
Dianne Hackborn231cc602009-04-27 17:10:36 -07003645 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003646 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003647 public static IContentService getContentService() {
3648 if (sContentService != null) {
3649 return sContentService;
3650 }
3651 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003652 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003653 return sContentService;
3654 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003655
Dianne Hackborn35654b62013-01-14 17:38:02 -08003656 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003657 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003658 public String getPackageName() {
3659 return mPackageName;
3660 }
3661
Philip P. Moltmann128b7032019-09-27 08:44:12 -07003662 /** @hide */
3663 public @Nullable String getFeatureId() {
3664 return mFeatureId;
3665 }
3666
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003667 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003668 private static volatile IContentService sContentService;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003669 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003671
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003672 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003673 final String mPackageName;
Philip P. Moltmann128b7032019-09-27 08:44:12 -07003674 final @Nullable String mFeatureId;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003675 final int mTargetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -07003676 final ContentInterface mWrapped;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003679
3680 /** @hide */
3681 public int resolveUserId(Uri uri) {
3682 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3683 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003684
3685 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003686 public int getUserId() {
3687 return mContext.getUserId();
3688 }
3689
Jeff Sharkey806bece2019-02-19 14:42:55 -07003690 /** {@hide} */
3691 @Deprecated
Daniel Nishic29d2b02016-06-30 12:20:41 -07003692 public Drawable getTypeDrawable(String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003693 return getTypeInfo(mimeType).getIcon().loadDrawable(mContext);
3694 }
3695
3696 /**
3697 * Return a detailed description of the given MIME type, including an icon
3698 * and label that describe the type.
3699 *
3700 * @param mimeType Valid, concrete MIME type.
3701 */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003702 public final @NonNull MimeTypeInfo getTypeInfo(@NonNull String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003703 Objects.requireNonNull(mimeType);
3704 return MimeIconUtils.getTypeInfo(mimeType);
3705 }
3706
3707 /**
3708 * Detailed description of a specific MIME type, including an icon and label
3709 * that describe the type.
3710 */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003711 public static final class MimeTypeInfo {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003712 private final Icon mIcon;
3713 private final CharSequence mLabel;
3714 private final CharSequence mContentDescription;
3715
3716 /** {@hide} */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003717 public MimeTypeInfo(@NonNull Icon icon, @NonNull CharSequence label,
Jeff Sharkey806bece2019-02-19 14:42:55 -07003718 @NonNull CharSequence contentDescription) {
3719 mIcon = Objects.requireNonNull(icon);
3720 mLabel = Objects.requireNonNull(label);
3721 mContentDescription = Objects.requireNonNull(contentDescription);
3722 }
3723
3724 /**
3725 * Return a visual representation of this MIME type. This can be styled
3726 * using {@link Icon#setTint(int)} to match surrounding UI.
3727 *
3728 * @see Icon#loadDrawable(Context)
3729 * @see android.widget.ImageView#setImageDrawable(Drawable)
3730 */
3731 public @NonNull Icon getIcon() {
3732 return mIcon;
3733 }
3734
3735 /**
3736 * Return a textual representation of this MIME type.
3737 *
3738 * @see android.widget.TextView#setText(CharSequence)
3739 */
3740 public @NonNull CharSequence getLabel() {
3741 return mLabel;
3742 }
3743
3744 /**
3745 * Return a content description for this MIME type.
3746 *
3747 * @see android.view.View#setContentDescription(CharSequence)
3748 */
3749 public @NonNull CharSequence getContentDescription() {
3750 return mContentDescription;
3751 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003752 }
Steve McKayea93fe72016-12-02 11:35:35 -08003753
3754 /**
3755 * @hide
3756 */
3757 public static @Nullable Bundle createSqlQueryBundle(
3758 @Nullable String selection,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07003759 @Nullable String[] selectionArgs) {
3760 return createSqlQueryBundle(selection, selectionArgs, null);
3761 }
3762
3763 /**
3764 * @hide
3765 */
3766 public static @Nullable Bundle createSqlQueryBundle(
3767 @Nullable String selection,
Steve McKayea93fe72016-12-02 11:35:35 -08003768 @Nullable String[] selectionArgs,
3769 @Nullable String sortOrder) {
3770
3771 if (selection == null && selectionArgs == null && sortOrder == null) {
3772 return null;
3773 }
3774
3775 Bundle queryArgs = new Bundle();
3776 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003777 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003778 }
3779 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003780 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003781 }
3782 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003783 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003784 }
3785 return queryArgs;
3786 }
Steve McKay29c3f682016-12-16 14:52:59 -08003787
3788 /**
3789 * Returns structured sort args formatted as an SQL sort clause.
3790 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003791 * NOTE: Collator clauses are suitable for use with non text fields. We might
3792 * choose to omit any collation clause since we don't know the underlying
3793 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3794 * appear to care much about the presence of collate clauses in queries
3795 * when ordering by numeric fields. For this reason we include collate
3796 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3797 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003798 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003799 * TODO: Would be nice to explicitly validate that colums referenced in
3800 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003801 *
3802 * @hide
3803 */
3804 public static String createSqlSortClause(Bundle queryArgs) {
3805 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3806 if (columns == null || columns.length == 0) {
3807 throw new IllegalArgumentException("Can't create sort clause without columns.");
3808 }
3809
3810 String query = TextUtils.join(", ", columns);
3811
Steve McKayd7ece9f2017-01-12 16:59:59 -08003812 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3813 // on their javadoc descriptions.
3814 int collation = queryArgs.getInt(
3815 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3816 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3817 query += " COLLATE NOCASE";
3818 }
3819
Steve McKay415f41b2017-02-01 13:38:25 -08003820 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3821 if (sortDir != Integer.MIN_VALUE) {
3822 switch (sortDir) {
3823 case QUERY_SORT_DIRECTION_ASCENDING:
3824 query += " ASC";
3825 break;
3826 case QUERY_SORT_DIRECTION_DESCENDING:
3827 query += " DESC";
3828 break;
3829 default:
3830 throw new IllegalArgumentException("Unsupported sort direction value."
3831 + " See ContentResolver documentation for details.");
3832 }
Steve McKay29c3f682016-12-16 14:52:59 -08003833 }
Steve McKay29c3f682016-12-16 14:52:59 -08003834 return query;
3835 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003836
3837 /**
3838 * Convenience method that efficiently loads a visual thumbnail for the
3839 * given {@link Uri}. Internally calls
3840 * {@link ContentProvider#openTypedAssetFile} on the remote provider, but
3841 * also defensively resizes any returned content to match the requested
3842 * target size.
3843 *
3844 * @param uri The item that should be visualized as a thumbnail.
3845 * @param size The target area on the screen where this thumbnail will be
3846 * shown. This is passed to the provider as {@link #EXTRA_SIZE}
3847 * to help it avoid downloading or generating heavy resources.
3848 * @param signal A signal to cancel the operation in progress.
3849 * @return Valid {@link Bitmap} which is a visual thumbnail.
3850 * @throws IOException If any trouble was encountered while generating or
3851 * loading the thumbnail, or if
3852 * {@link CancellationSignal#cancel()} was invoked.
3853 */
3854 public @NonNull Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size,
3855 @Nullable CancellationSignal signal) throws IOException {
Jeff Sharkey448c1ea2019-03-29 18:10:57 -06003856 return loadThumbnail(this, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003857 }
3858
3859 /** {@hide} */
Jeff Sharkeye770d222018-12-07 15:15:59 -07003860 public static Bitmap loadThumbnail(@NonNull ContentInterface content, @NonNull Uri uri,
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003861 @NonNull Size size, @Nullable CancellationSignal signal, int allocator)
3862 throws IOException {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003863 Objects.requireNonNull(content);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003864 Objects.requireNonNull(uri);
3865 Objects.requireNonNull(size);
3866
3867 // Convert to Point, since that's what the API is defined as
3868 final Bundle opts = new Bundle();
3869 opts.putParcelable(EXTRA_SIZE, Point.convert(size));
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003870 final Int32Ref orientation = new Int32Ref(0);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003871
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003872 Bitmap bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(() -> {
3873 final AssetFileDescriptor afd = content.openTypedAssetFile(uri, "image/*", opts,
3874 signal);
3875 final Bundle extras = afd.getExtras();
3876 orientation.value = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
3877 return afd;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003878 }), (ImageDecoder decoder, ImageInfo info, Source source) -> {
Tony Huangde421372019-10-25 18:15:24 +08003879 decoder.setAllocator(allocator);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003880
Tony Huangde421372019-10-25 18:15:24 +08003881 // One last-ditch check to see if we've been canceled.
3882 if (signal != null) signal.throwIfCanceled();
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003883
Tony Huangde421372019-10-25 18:15:24 +08003884 // We requested a rough thumbnail size, but the remote size may have
3885 // returned something giant, so defensively scale down as needed.
3886 final int widthSample = info.getSize().getWidth() / size.getWidth();
3887 final int heightSample = info.getSize().getHeight() / size.getHeight();
3888 final int sample = Math.max(widthSample, heightSample);
3889 if (sample > 1) {
3890 decoder.setTargetSampleSize(sample);
3891 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003892 });
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003893
3894 // Transform the bitmap if requested. We use a side-channel to
3895 // communicate the orientation, since EXIF thumbnails don't contain
3896 // the rotation flags of the original image.
3897 if (orientation.value != 0) {
3898 final int width = bitmap.getWidth();
3899 final int height = bitmap.getHeight();
3900
3901 final Matrix m = new Matrix();
3902 m.setRotate(orientation.value, width / 2, height / 2);
3903 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
3904 }
3905
3906 return bitmap;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003907 }
Makoto Onukiee93ad22018-10-18 16:24:13 -07003908
3909 /** {@hide} */
3910 public static void onDbCorruption(String tag, String message, Throwable stacktrace) {
3911 try {
3912 getContentService().onDbCorruption(tag, message, Log.getStackTraceString(stacktrace));
3913 } catch (RemoteException e) {
3914 e.rethrowFromSystemServer();
3915 }
3916 }
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003917
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07003918 /**
3919 * Decode a path generated by {@link #encodeToFile(Uri)} back into
3920 * the original {@link Uri}.
3921 * <p>
3922 * This is used to offer a way to intercept filesystem calls in
3923 * {@link ContentProvider} unaware code and redirect them to a
3924 * {@link ContentProvider} when they attempt to use {@code _DATA} columns
3925 * that are otherwise deprecated.
3926 *
3927 * @hide
3928 */
3929 @SystemApi
3930 public static @NonNull Uri decodeFromFile(@NonNull File file) {
3931 return translateDeprecatedDataPath(file.getAbsolutePath());
3932 }
3933
3934 /**
3935 * Encode a {@link Uri} into an opaque filesystem path which can then be
3936 * resurrected by {@link #decodeFromFile(File)}.
3937 * <p>
3938 * This is used to offer a way to intercept filesystem calls in
3939 * {@link ContentProvider} unaware code and redirect them to a
3940 * {@link ContentProvider} when they attempt to use {@code _DATA} columns
3941 * that are otherwise deprecated.
3942 *
3943 * @hide
3944 */
3945 @SystemApi
3946 public static @NonNull File encodeToFile(@NonNull Uri uri) {
3947 return new File(translateDeprecatedDataPath(uri));
3948 }
3949
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003950 /** {@hide} */
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07003951 public static @NonNull Uri translateDeprecatedDataPath(@NonNull String path) {
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003952 final String ssp = "//" + path.substring(DEPRECATE_DATA_PREFIX.length());
3953 return Uri.parse(new Uri.Builder().scheme(SCHEME_CONTENT)
3954 .encodedOpaquePart(ssp).build().toString());
3955 }
3956
3957 /** {@hide} */
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07003958 public static @NonNull String translateDeprecatedDataPath(@NonNull Uri uri) {
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003959 return DEPRECATE_DATA_PREFIX + uri.getEncodedSchemeSpecificPart().substring(2);
3960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961}