blob: 6cd1cd3354c247771960aa9c5587ddb0034c1751 [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;
58import android.os.RemoteException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070059import android.os.ServiceManager;
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -080060import android.os.SystemClock;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070061import android.os.UserHandle;
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060062import android.os.storage.StorageManager;
Ivan Chiang6da7b0a2019-04-11 20:01:17 +080063import android.system.Int32Ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.text.TextUtils;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080065import android.util.EventLog;
Dianne Hackborn231cc602009-04-27 17:10:36 -070066import android.util.Log;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060067import android.util.Size;
Jeff Sharkey1307f422019-11-13 13:03:10 -070068import android.util.SparseArray;
Jeff Sharkey08da7a12013-08-11 20:53:18 -070069
Daniel Nishic29d2b02016-06-30 12:20:41 -070070import com.android.internal.util.MimeIconUtils;
Jeff Sharkey673db442015-06-11 19:30:57 -070071
Jeff Sharkey60cfad82016-01-05 17:30:57 -070072import dalvik.system.CloseGuard;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.File;
75import java.io.FileInputStream;
76import java.io.FileNotFoundException;
77import java.io.IOException;
78import java.io.InputStream;
79import java.io.OutputStream;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070080import java.lang.annotation.Retention;
81import java.lang.annotation.RetentionPolicy;
Gilles Debunne03f02922010-06-09 14:11:45 -070082import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.List;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060084import java.util.Objects;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080085import java.util.Random;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070086import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088/**
89 * This class provides applications access to the content model.
Joe Fernandez558459f2011-10-13 16:47:36 -070090 *
91 * <div class="special reference">
92 * <h3>Developer Guides</h3>
93 * <p>For more information about using a ContentResolver with content providers, read the
94 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
95 * developer guide.</p>
Ng Zhi Anb837d0b2019-01-24 21:39:24 -080096 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -070098public abstract class ContentResolver implements ContentInterface {
Fred Quintanaac9385e2009-06-22 18:00:59 -070099 /**
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600100 * Enables logic that supports deprecation of {@code _data} columns,
101 * typically by replacing values with fake paths that the OS then offers to
102 * redirect to {@link #openFileDescriptor(Uri, String)}, which developers
103 * should be using directly.
104 *
105 * @hide
106 */
Jeff Sharkey10ec9d82018-11-28 14:52:45 -0700107 public static final boolean DEPRECATE_DATA_COLUMNS = StorageManager.hasIsolatedStorage();
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600108
109 /**
110 * Special filesystem path prefix which indicates that a path should be
111 * treated as a {@code content://} {@link Uri} when
112 * {@link #DEPRECATE_DATA_COLUMNS} is enabled.
113 * <p>
114 * The remainder of the path after this prefix is a
115 * {@link Uri#getSchemeSpecificPart()} value, which includes authority, path
116 * segments, and query parameters.
117 *
118 * @hide
119 */
120 public static final String DEPRECATE_DATA_PREFIX = "/mnt/content/";
121
122 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700123 * @deprecated instead use
124 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
125 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700126 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -0700127 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700128
129 /**
130 * If this extra is set to true, the sync request will be scheduled
131 * at the front of the sync request queue and without any delay
132 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700134
Fred Quintanaac9385e2009-06-22 18:00:59 -0700135 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000136 * If this extra is set to true, the sync request will be scheduled
137 * only when the device is plugged in. This is equivalent to calling
138 * setRequiresCharging(true) on {@link SyncRequest}.
139 */
140 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
141
142 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700143 * @deprecated instead use
144 * {@link #SYNC_EXTRAS_MANUAL}
145 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700146 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800148
149 /**
150 * If this extra is set to true then the sync settings (like getSyncAutomatically())
151 * are ignored by the sync scheduler.
152 */
153 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
154
155 /**
156 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
157 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
158 * retries will still honor the backoff.
159 */
160 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
161
162 /**
163 * If this extra is set to true then the request will not be retried if it fails.
164 */
165 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
166
167 /**
168 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
169 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
170 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700171 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800172
Georgi Nikolovb3395572013-06-18 18:27:31 -0700173 /**
174 * Indicates that this sync is intended to only upload local changes to the server.
175 * For example, this will be set to true if the sync is initiated by a call to
176 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
177 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700179
180 /**
181 * Indicates that the sync adapter should proceed with the delete operations,
182 * even if it determines that there are too many.
183 * See {@link SyncResult#tooManyDeletions}
184 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700186
187 /**
188 * Indicates that the sync adapter should not proceed with the delete operations,
189 * if it determines that there are too many.
190 * See {@link SyncResult#tooManyDeletions}
191 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
193
Matthew Williamsfa774182013-06-18 15:44:11 -0700194 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
195 /** {@hide} User-specified flag for expected upload size. */
196 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
197
198 /** {@hide} User-specified flag for expected download size. */
199 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
200
201 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
202 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
203
204 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700205 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700206
Fred Quintana4a6679b2009-08-17 13:05:39 -0700207 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700208 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800209 *
210 * Only the system and the shell user can set it.
211 *
212 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
213 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700214 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800215
216 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700217 * Set by the SyncManager to request that the SyncAdapter initialize itself for
218 * the given account/authority pair. One required initialization step is to
219 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
220 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
221 * do a full sync, though it is allowed to do so.
222 */
223 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
224
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800225 /** @hide */
226 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
227 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 public static final String SCHEME_CONTENT = "content";
230 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
231 public static final String SCHEME_FILE = "file";
232
233 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700234 * An extra {@link Point} describing the optimal size for a requested image
235 * resource, in pixels. If a provider has multiple sizes of the image, it
236 * should return the image closest to this size.
237 *
238 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
239 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
240 * CancellationSignal)
241 */
242 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
243
244 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800245 * An extra boolean describing whether a particular provider supports refresh
246 * or not. If a provider supports refresh, it should include this key in its
247 * returned Cursor as part of its query call.
248 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800249 */
250 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
251
252 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800253 * Key for an SQL style selection string that may be present in the query Bundle argument
254 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
255 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800256 *
257 * <p>Clients should never include user supplied values directly in the selection string,
258 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
259 * should use standard placeholder notation to represent values in a selection string,
260 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
261 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800262 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
263 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700264 *
265 * @see #QUERY_ARG_SORT_COLUMNS
266 * @see #QUERY_ARG_SORT_DIRECTION
267 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600268 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800269 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700270 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800271
272 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800273 * Key for SQL selection string arguments list.
274 *
275 * <p>Clients should never include user supplied values directly in the selection string,
276 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
277 * should use standard placeholder notation to represent values in a selection string,
278 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
279 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800280 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
281 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700282 *
283 * @see #QUERY_ARG_SORT_COLUMNS
284 * @see #QUERY_ARG_SORT_DIRECTION
285 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600286 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800287 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700288 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
289 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800290
291 /**
292 * Key for an SQL style sort string that may be present in the query Bundle argument
293 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
294 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800295 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800296 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
297 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700298 *
299 * @see #QUERY_ARG_SORT_COLUMNS
300 * @see #QUERY_ARG_SORT_DIRECTION
301 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600302 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800303 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700304 public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800305
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700306 /**
307 * Key for an SQL style {@code GROUP BY} string that may be present in the
308 * query Bundle argument passed to
309 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
310 *
311 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
312 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
313 *
314 * @see #QUERY_ARG_GROUP_COLUMNS
315 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600316 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700317
318 /**
319 * Key for an SQL style {@code HAVING} string that may be present in the
320 * query Bundle argument passed to
321 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
322 *
323 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
324 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
325 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600326 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700327
328 /**
329 * Key for an SQL style {@code LIMIT} string that may be present in the
330 * query Bundle argument passed to
331 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
332 *
333 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
334 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
335 *
336 * @see #QUERY_ARG_LIMIT
337 * @see #QUERY_ARG_OFFSET
338 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600339 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
340
Steve McKay29c3f682016-12-16 14:52:59 -0800341 /**
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700342 * Specifies the list of columns (stored as a {@code String[]}) against
343 * which to sort results. When first column values are identical, records
344 * are then sorted based on second column values, and so on.
345 * <p>
346 * Columns present in this list must also be included in the projection
347 * supplied to
348 * {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
349 * <p>
350 * Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKayd74a3bd2017-04-24 12:07:53 -0700351 * <li>{@link ContentProvider} implementations: When preparing data in
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700352 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
353 * if sort columns is reflected in the returned Cursor, it is strongly
354 * recommended that {@link #QUERY_ARG_SORT_COLUMNS} then be included in the
355 * array of honored arguments reflected in {@link Cursor} extras
356 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
357 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in
358 * the arguments {@link Bundle}, the Content framework will attempt to
359 * synthesize an QUERY_ARG_SQL* argument using the corresponding
360 * QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800361 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700362 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800363
364 /**
365 * Specifies desired sort order. When unspecified a provider may provide a default
366 * sort direction, or choose to return unsorted results.
367 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800368 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
369 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700370 * <li>{@link ContentProvider} implementations: When preparing data in
371 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
372 * is reflected in the returned Cursor, it is strongly recommended that
373 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
374 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800375 *
376 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
377 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700378 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800379 *
380 * @see #QUERY_SORT_DIRECTION_ASCENDING
381 * @see #QUERY_SORT_DIRECTION_DESCENDING
382 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700383 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800384
385 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700386 * Allows client to specify a hint to the provider declaring which collation
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600387 * to use when sorting values.
388 * <p>
389 * Providers may support custom collators. When specifying a custom collator
Steve McKayd74a3bd2017-04-24 12:07:53 -0700390 * the value is determined by the Provider.
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600391 * <p>
392 * {@link ContentProvider} implementations: When preparing data in
393 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
394 * if sort collation is reflected in the returned Cursor, it is strongly
395 * recommended that {@link #QUERY_ARG_SORT_COLLATION} then be included in
396 * the array of honored arguments reflected in {@link Cursor} extras
397 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
398 * <p>
399 * When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
400 * arguments {@link Bundle}, the Content framework will attempt to
401 * synthesize a QUERY_ARG_SQL* argument using the corresponding
402 * QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800403 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700404 * @see java.text.Collator#PRIMARY
405 * @see java.text.Collator#SECONDARY
406 * @see java.text.Collator#TERTIARY
407 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800408 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700409 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800410
Steve McKay415f41b2017-02-01 13:38:25 -0800411 /**
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600412 * Allows client to specify a hint to the provider declaring which locale to
413 * use when sorting values.
414 * <p>
415 * The value is defined as a RFC 3066 locale ID followed by an optional
416 * keyword list, which is the locale format used to configure ICU through
417 * classes like {@link android.icu.util.ULocale}. This supports requesting
418 * advanced sorting options, such as {@code de@collation=phonebook},
419 * {@code zh@collation=pinyin}, etc.
420 * <p>
421 * {@link ContentProvider} implementations: When preparing data in
422 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
423 * if sort locale is reflected in the returned Cursor, it is strongly
424 * recommended that {@link #QUERY_ARG_SORT_LOCALE} then be included in the
425 * array of honored arguments reflected in {@link Cursor} extras
426 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
427 *
428 * @see java.util.Locale#Locale(String)
429 * @see android.icu.util.ULocale#ULocale(String)
430 */
431 public static final String QUERY_ARG_SORT_LOCALE = "android:query-arg-sort-locale";
432
433 /**
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700434 * Specifies the list of columns (stored as a {@code String[]}) against
435 * which to group results. When column values are identical, multiple
436 * records are collapsed together into a single record.
437 * <p>
438 * Columns present in this list must also be included in the projection
439 * supplied to
440 * {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
441 * <p>
442 * Apps targeting {@link android.os.Build.VERSION_CODES#R} or higher:
443 * <li>{@link ContentProvider} implementations: When preparing data in
444 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
445 * if group columns is reflected in the returned Cursor, it is strongly
446 * recommended that {@link #QUERY_ARG_SORT_COLUMNS} then be included in the
447 * array of honored arguments reflected in {@link Cursor} extras
448 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
449 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in
450 * the arguments {@link Bundle}, the Content framework will attempt to
451 * synthesize an QUERY_ARG_SQL* argument using the corresponding
452 * QUERY_ARG_SORT* values.
453 */
454 public static final String QUERY_ARG_GROUP_COLUMNS = "android:query-arg-group-columns";
455
456 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700457 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800458 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700459 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800460 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
461 * when any QUERY_ARG_SORT* value was honored during the preparation of the
462 * results {@link Cursor}.
463 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700464 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
465 *
466 * @see #QUERY_ARG_SORT_COLUMNS
467 * @see #QUERY_ARG_SORT_DIRECTION
468 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600469 * @see #QUERY_ARG_SORT_LOCALE
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700470 * @see #QUERY_ARG_GROUP_COLUMNS
Steve McKay415f41b2017-02-01 13:38:25 -0800471 */
472 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
473
Steve McKay29c3f682016-12-16 14:52:59 -0800474 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700475 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800476 QUERY_SORT_DIRECTION_ASCENDING,
477 QUERY_SORT_DIRECTION_DESCENDING
478 })
479 @Retention(RetentionPolicy.SOURCE)
480 public @interface SortDirection {}
481 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
482 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
483
484 /**
485 * @see {@link java.text.Collector} for details on respective collation strength.
486 * @hide
487 */
488 @IntDef(flag = false, value = {
489 java.text.Collator.PRIMARY,
490 java.text.Collator.SECONDARY,
491 java.text.Collator.TERTIARY,
492 java.text.Collator.IDENTICAL
493 })
494 @Retention(RetentionPolicy.SOURCE)
495 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800496
497 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700498 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800499 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700500 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800501
502 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700503 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800504 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700505 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800506
507 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700508 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
509 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800510 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800511 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700512 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800513 *
514 * <p>Providers having returned -1 in a previous query are recommended to
515 * send content change notification once (if) full recordset size becomes
516 * known.
517 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700518 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800519
520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 * This is the Android platform's base MIME type for a content: URI
522 * containing a Cursor of a single item. Applications should use this
523 * as the base type along with their own sub-type of their content: URIs
524 * that represent a particular item. For example, hypothetical IMAP email
525 * client may have a URI
526 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
527 * message in the inbox, whose MIME type would be reported as
528 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800529 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
531 */
532 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 /**
535 * This is the Android platform's base MIME type for a content: URI
536 * containing a Cursor of zero or more items. Applications should use this
537 * as the base type along with their own sub-type of their content: URIs
538 * that represent a directory of items. For example, hypothetical IMAP email
539 * client may have a URI
540 * <code>content://com.company.provider.imap/inbox</code> for all of the
541 * messages in its inbox, whose MIME type would be reported as
542 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800543 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 * <p>Note how the base MIME type varies between this and
545 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
546 * one single item or multiple items in the data set, while the sub-type
547 * remains the same because in either case the data structure contained
548 * in the cursor is the same.
549 */
550 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700551
Matt Caseybd7bcf02014-02-05 15:51:39 -0800552 /**
553 * This is the Android platform's generic MIME type to match any MIME
554 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
555 * {@code SUB_TYPE} is the sub-type of the application-dependent
556 * content, e.g., "audio", "video", "playlist".
557 */
558 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
559
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600560 /** {@hide} */
561 @Deprecated
562 public static final String MIME_TYPE_DEFAULT = ClipDescription.MIMETYPE_UNKNOWN;
Jeff Sharkey91e3cd42018-08-27 18:03:33 -0600563
Fred Quintanaac9385e2009-06-22 18:00:59 -0700564 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100565 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700566 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
567 /** @hide */
568 public static final int SYNC_ERROR_AUTHENTICATION = 2;
569 /** @hide */
570 public static final int SYNC_ERROR_IO = 3;
571 /** @hide */
572 public static final int SYNC_ERROR_PARSE = 4;
573 /** @hide */
574 public static final int SYNC_ERROR_CONFLICT = 5;
575 /** @hide */
576 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
577 /** @hide */
578 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
579 /** @hide */
580 public static final int SYNC_ERROR_INTERNAL = 8;
581
Alon Albert57286f92012-10-09 14:21:38 -0700582 private static final String[] SYNC_ERROR_NAMES = new String[] {
583 "already-in-progress",
584 "authentication-error",
585 "io-error",
586 "parse-error",
587 "conflict",
588 "too-many-deletions",
589 "too-many-retries",
590 "internal-error",
591 };
592
593 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800594 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700595 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
596 return String.valueOf(error);
597 }
598 return SYNC_ERROR_NAMES[error - 1];
599 }
600
Alon Albert5c113fa2013-02-07 08:07:32 -0800601 /** @hide */
602 public static int syncErrorStringToInt(String error) {
603 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
604 if (SYNC_ERROR_NAMES[i].equals(error)) {
605 return i + 1;
606 }
607 }
608 if (error != null) {
609 try {
610 return Integer.parseInt(error);
611 } catch (NumberFormatException e) {
612 Log.d(TAG, "error parsing sync error: " + error);
613 }
614 }
615 return 0;
616 }
617
Fred Quintanaac9385e2009-06-22 18:00:59 -0700618 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700619 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700620 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
621 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100622 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700623 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
624 /** @hide */
625 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
626
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700627 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700628 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
629 NOTIFY_SYNC_TO_NETWORK,
630 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS
631 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700632 @Retention(RetentionPolicy.SOURCE)
633 public @interface NotifyFlags {}
634
635 /**
636 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
637 * to the network.
638 */
639 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
640
641 /**
642 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
643 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
644 * using "notify for descendants." The purpose of this is to allow the provide to send
645 * a general notification of "something under X" changed that observers of that specific
646 * URI can receive, while also sending a specific URI under X. It would use this flag
647 * when sending the former, so that observers of "X and descendants" only see the latter.
648 */
649 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
650
Makoto Onuki75ad2492018-03-28 14:42:42 -0700651 /**
652 * No exception, throttled by app standby normally.
653 * @hide
654 */
655 public static final int SYNC_EXEMPTION_NONE = 0;
656
657 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700658 * Exemption given to a sync request made by a foreground app (including
659 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700660 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700661 * At the schedule time, we promote the sync adapter app for a higher bucket:
662 * - If the device is not dozing (so the sync will start right away)
663 * promote to ACTIVE for 1 hour.
664 * - If the device is dozing (so the sync *won't* start right away),
665 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
666 * device comes out of doze.
667 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
668 * so it can schedule and start more syncs without getting throttled, even when the first
669 * operation was canceled and now we're retrying.
670 *
671 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700672 * @hide
673 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700674 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700675
676 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700677 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700678 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
679 * @hide
680 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700681 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700682
683 /** @hide */
684 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
685 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700686 SYNC_EXEMPTION_PROMOTE_BUCKET,
687 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700688 })
689 @Retention(RetentionPolicy.SOURCE)
690 public @interface SyncExemption {}
691
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800692 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800693 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700694 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800695 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800696 private final Random mRandom = new Random(); // guarded by itself
697
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700698 public ContentResolver(@Nullable Context context) {
699 this(context, null);
700 }
701
702 /** {@hide} */
703 public ContentResolver(@Nullable Context context, @Nullable ContentInterface wrapped) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800704 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700705 mPackageName = mContext.getOpPackageName();
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700706 mFeatureId = mContext.getFeatureId();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000707 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700708 mWrapped = wrapped;
709 }
710
711 /** {@hide} */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700712 public static @NonNull ContentResolver wrap(@NonNull ContentInterface wrapped) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000713 Objects.requireNonNull(wrapped);
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700714
715 return new ContentResolver(null, wrapped) {
716 @Override
717 public void unstableProviderDied(IContentProvider icp) {
718 throw new UnsupportedOperationException();
719 }
720 @Override
721 public boolean releaseUnstableProvider(IContentProvider icp) {
722 throw new UnsupportedOperationException();
723 }
724 @Override
725 public boolean releaseProvider(IContentProvider icp) {
726 throw new UnsupportedOperationException();
727 }
728 @Override
729 protected IContentProvider acquireUnstableProvider(Context c, String name) {
730 throw new UnsupportedOperationException();
731 }
732 @Override
733 protected IContentProvider acquireProvider(Context c, String name) {
734 throw new UnsupportedOperationException();
735 }
736 };
737 }
738
739 /**
740 * Create a {@link ContentResolver} instance that redirects all its methods
741 * to the given {@link ContentProvider}.
742 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700743 public static @NonNull ContentResolver wrap(@NonNull ContentProvider wrapped) {
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700744 return wrap((ContentInterface) wrapped);
745 }
746
747 /**
748 * Create a {@link ContentResolver} instance that redirects all its methods
749 * to the given {@link ContentProviderClient}.
750 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700751 public static @NonNull ContentResolver wrap(@NonNull ContentProviderClient wrapped) {
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700752 return wrap((ContentInterface) wrapped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
754
755 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100756 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700758
759 /**
760 * Providing a default implementation of this, to avoid having to change a
761 * lot of other things, but implementations of ContentResolver should
762 * implement it.
763 *
764 * @hide
765 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100766 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700767 protected IContentProvider acquireExistingProvider(Context c, String name) {
768 return acquireProvider(c, name);
769 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100772 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700774 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100775 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700776 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
777 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100778 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700779 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700780 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100781 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700782 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700784 /** @hide */
785 public void appNotRespondingViaProvider(IContentProvider icp) {
786 throw new UnsupportedOperationException("appNotRespondingViaProvider");
787 }
788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 /**
790 * Return the MIME type of the given content URL.
791 *
792 * @param url A Uri identifying content (either a list or specific type),
793 * using the content:// scheme.
794 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
795 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700796 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700797 public final @Nullable String getType(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000798 Objects.requireNonNull(url, "url");
Jeff Sharkey673db442015-06-11 19:30:57 -0700799
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700800 try {
801 if (mWrapped != null) return mWrapped.getType(url);
802 } catch (RemoteException e) {
803 return null;
804 }
805
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700806 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700807 IContentProvider provider = acquireExistingProvider(url);
808 if (provider != null) {
809 try {
810 return provider.getType(url);
811 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600812 // Arbitrary and not worth documenting, as Activity
813 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700814 return null;
815 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100816 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700817 return null;
818 } finally {
819 releaseProvider(provider);
820 }
821 }
822
823 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 return null;
825 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800828 String type = ActivityManager.getService().getProviderMimeType(
Nicolas Prevot1dddc7f2014-07-07 17:44:58 +0100829 ContentProvider.getUriWithoutUserId(url), resolveUserId(url));
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700830 return type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600832 throw e.rethrowFromSystemServer();
Ola Olsson145e6c42010-12-20 16:45:35 +0100833 } catch (java.lang.Exception e) {
834 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
835 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837 }
838
839 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700840 * Query for the possible MIME types for the representations the given
841 * content URL can be returned when opened as as stream with
842 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
843 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700844 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700845 * data that they contain.
846 *
847 * @param url A Uri identifying content (either a list or specific type),
848 * using the content:// scheme.
849 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500850 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700851 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700852 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700853 * data streams that match the given mimeTypeFilter. If there are none,
854 * null is returned.
855 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700856 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700857 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000858 Objects.requireNonNull(url, "url");
859 Objects.requireNonNull(mimeTypeFilter, "mimeTypeFilter");
Jeff Sharkey673db442015-06-11 19:30:57 -0700860
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700861 try {
862 if (mWrapped != null) return mWrapped.getStreamTypes(url, mimeTypeFilter);
863 } catch (RemoteException e) {
864 return null;
865 }
866
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700867 IContentProvider provider = acquireProvider(url);
868 if (provider == null) {
869 return null;
870 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700871
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700872 try {
873 return provider.getStreamTypes(url, mimeTypeFilter);
874 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800875 // Arbitrary and not worth documenting, as Activity
876 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700877 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700878 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800879 releaseProvider(provider);
880 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700881 }
882
883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800885 * <p>
886 * For best performance, the caller should follow these guidelines:
887 * <ul>
888 * <li>Provide an explicit projection, to prevent
889 * reading data from storage that aren't going to be used.</li>
890 * <li>Use question mark parameter markers such as 'phone=?' instead of
891 * explicit values in the {@code selection} parameter, so that queries
892 * that differ only by those values will be recognized as the same
893 * for caching purposes.</li>
894 * </ul>
895 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 *
897 * @param uri The URI, using the content:// scheme, for the content to
898 * retrieve.
899 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800900 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * @param selection A filter declaring which rows to return, formatted as an
902 * SQL WHERE clause (excluding the WHERE itself). Passing null will
903 * return all rows for the given URI.
904 * @param selectionArgs You may include ?s in selection, which will be
905 * replaced by the values from selectionArgs, in the order that they
906 * appear in the selection. The values will be bound as Strings.
907 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
908 * clause (excluding the ORDER BY itself). Passing null will use the
909 * default sort order, which may be unordered.
Varun Shahc4fdfa32019-05-10 14:10:08 -0700910 * @return A Cursor object, which is positioned before the first entry. May return
911 * <code>null</code> if the underlying content provider returns <code>null</code>,
912 * or if it crashes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 * @see Cursor
914 */
Tor Norbye788fc2b2015-07-05 16:10:42 -0700915 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
916 @Nullable String[] projection, @Nullable String selection,
917 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800918 return query(uri, projection, selection, selectionArgs, sortOrder, null);
919 }
920
921 /**
Jeff Brownc64ff372013-10-09 18:50:56 -0700922 * Query the given URI, returning a {@link Cursor} over the result set
923 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800924 * <p>
925 * For best performance, the caller should follow these guidelines:
926 * <ul>
927 * <li>Provide an explicit projection, to prevent
928 * reading data from storage that aren't going to be used.</li>
929 * <li>Use question mark parameter markers such as 'phone=?' instead of
930 * explicit values in the {@code selection} parameter, so that queries
931 * that differ only by those values will be recognized as the same
932 * for caching purposes.</li>
933 * </ul>
934 * </p>
935 *
936 * @param uri The URI, using the content:// scheme, for the content to
937 * retrieve.
938 * @param projection A list of which columns to return. Passing null will
939 * return all columns, which is inefficient.
940 * @param selection A filter declaring which rows to return, formatted as an
941 * SQL WHERE clause (excluding the WHERE itself). Passing null will
942 * return all rows for the given URI.
943 * @param selectionArgs You may include ?s in selection, which will be
944 * replaced by the values from selectionArgs, in the order that they
945 * appear in the selection. The values will be bound as Strings.
946 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
947 * clause (excluding the ORDER BY itself). Passing null will use the
948 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -0800949 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800950 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
951 * when the query is executed.
Varun Shahc4fdfa32019-05-10 14:10:08 -0700952 * @return A Cursor object, which is positioned before the first entry. May return
953 * <code>null</code> if the underlying content provider returns <code>null</code>,
954 * or if it crashes.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800955 * @see Cursor
956 */
Steve McKayea93fe72016-12-02 11:35:35 -0800957 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -0700958 @Nullable String[] projection, @Nullable String selection,
959 @Nullable String[] selectionArgs, @Nullable String sortOrder,
960 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -0800961 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
962 return query(uri, projection, queryArgs, cancellationSignal);
963 }
964
965 /**
966 * Query the given URI, returning a {@link Cursor} over the result set
967 * with support for cancellation.
968 *
969 * <p>For best performance, the caller should follow these guidelines:
970 *
971 * <li>Provide an explicit projection, to prevent reading data from storage
972 * that aren't going to be used.
973 *
Steve McKay415f41b2017-02-01 13:38:25 -0800974 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
975 * the preparation of the result set by including the respective argument keys
976 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
977 * for details.
978 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -0700979 * @see #QUERY_ARG_SORT_COLUMNS
980 * @see #QUERY_ARG_SORT_DIRECTION
981 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800982 *
Steve McKayea93fe72016-12-02 11:35:35 -0800983 * @param uri The URI, using the content:// scheme, for the content to
984 * retrieve.
985 * @param projection A list of which columns to return. Passing null will
986 * return all columns, which is inefficient.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -0700987 * @param queryArgs A Bundle containing additional information necessary for
988 * the operation. Arguments may include SQL style arguments, such
989 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
990 * the documentation for each individual provider will indicate
991 * which arguments they support.
Steve McKayea93fe72016-12-02 11:35:35 -0800992 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
993 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
994 * when the query is executed.
Varun Shahc4fdfa32019-05-10 14:10:08 -0700995 * @return A Cursor object, which is positioned before the first entry. May return
996 * <code>null</code> if the underlying content provider returns <code>null</code>,
997 * or if it crashes.
Steve McKayea93fe72016-12-02 11:35:35 -0800998 * @see Cursor
999 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001000 @Override
Steve McKayea93fe72016-12-02 11:35:35 -08001001 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
1002 @Nullable String[] projection, @Nullable Bundle queryArgs,
1003 @Nullable CancellationSignal cancellationSignal) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001004 Objects.requireNonNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001005
1006 try {
1007 if (mWrapped != null) {
1008 return mWrapped.query(uri, projection, queryArgs, cancellationSignal);
1009 }
1010 } catch (RemoteException e) {
1011 return null;
1012 }
1013
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001014 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1015 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 return null;
1017 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001018 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -08001019 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001021 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -08001022
Jeff Brown4c1241d2012-02-02 17:05:00 -08001023 ICancellationSignal remoteCancellationSignal = null;
1024 if (cancellationSignal != null) {
1025 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001026 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -08001027 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -08001028 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001029 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001030 qCursor = unstableProvider.query(mPackageName, mFeatureId, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -08001031 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001032 } catch (DeadObjectException e) {
1033 // The remote process has died... but we only hold an unstable
1034 // reference though, so we might recover!!! Let's try!!!!
1035 // This is exciting!!1!!1!!!!1
1036 unstableProviderDied(unstableProvider);
1037 stableProvider = acquireProvider(uri);
1038 if (stableProvider == null) {
1039 return null;
1040 }
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001041 qCursor = stableProvider.query(mPackageName, mFeatureId, uri, projection,
1042 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001043 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001044 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 return null;
1046 }
Jeff Brownc21b5a02013-01-07 17:15:12 -08001047
1048 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -07001049 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001050 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -08001051 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -08001052
1053 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001054 final IContentProvider provider = (stableProvider != null) ? stableProvider
1055 : acquireProvider(uri);
1056 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001057 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -08001058 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001059 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001061 // Arbitrary and not worth documenting, as Activity
1062 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001064 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -08001065 if (qCursor != null) {
1066 qCursor.close();
1067 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001068 if (cancellationSignal != null) {
1069 cancellationSignal.setRemote(null);
1070 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001071 if (unstableProvider != null) {
1072 releaseUnstableProvider(unstableProvider);
1073 }
1074 if (stableProvider != null) {
1075 releaseProvider(stableProvider);
1076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
1078 }
1079
Jeff Sharkey63784022019-04-17 10:39:42 -06001080 /** {@hide} */
1081 public final @NonNull Uri canonicalizeOrElse(@NonNull Uri uri) {
1082 final Uri res = canonicalize(uri);
1083 return (res != null) ? res : uri;
1084 }
1085
Fred Quintana89437372009-05-15 15:10:40 -07001086 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001087 * Transform the given <var>url</var> to a canonical representation of
1088 * its referenced resource, which can be used across devices, persisted,
1089 * backed up and restored, etc. The returned Uri is still a fully capable
1090 * Uri for use with its content provider, allowing you to do all of the
1091 * same content provider operations as with the original Uri --
1092 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
1093 * only difference in behavior between the original and new Uris is that
1094 * the content provider may need to do some additional work at each call
1095 * using it to resolve it to the correct resource, especially if the
1096 * canonical Uri has been moved to a different environment.
1097 *
1098 * <p>If you are moving a canonical Uri between environments, you should
1099 * perform another call to {@link #canonicalize} with that original Uri to
1100 * re-canonicalize it for the current environment. Alternatively, you may
1101 * want to use {@link #uncanonicalize} to transform it to a non-canonical
1102 * Uri that works only in the current environment but potentially more
1103 * efficiently than the canonical representation.</p>
1104 *
1105 * @param url The {@link Uri} that is to be transformed to a canonical
1106 * representation. Like all resolver calls, the input can be either
1107 * a non-canonical or canonical Uri.
1108 *
1109 * @return Returns the official canonical representation of <var>url</var>,
1110 * or null if the content provider does not support a canonical representation
1111 * of the given Uri. Many providers may not support canonicalization of some
1112 * or all of their Uris.
1113 *
1114 * @see #uncanonicalize
1115 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001116 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001117 public final @Nullable Uri canonicalize(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001118 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001119
1120 try {
1121 if (mWrapped != null) return mWrapped.canonicalize(url);
1122 } catch (RemoteException e) {
1123 return null;
1124 }
1125
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001126 IContentProvider provider = acquireProvider(url);
1127 if (provider == null) {
1128 return null;
1129 }
1130
1131 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001132 return provider.canonicalize(mPackageName, mFeatureId, url);
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001133 } catch (RemoteException e) {
1134 // Arbitrary and not worth documenting, as Activity
1135 // Manager will kill this process shortly anyway.
1136 return null;
1137 } finally {
1138 releaseProvider(provider);
1139 }
1140 }
1141
1142 /**
1143 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
1144 * it to its local non-canonical form. This can be useful in some cases where
1145 * you know that you will only be using the Uri in the current environment and
1146 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001147 * provider or want to verify that the referenced data exists at all in the
1148 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001149 *
1150 * @param url The canonical {@link Uri} that is to be convered back to its
1151 * non-canonical form.
1152 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001153 * @return Returns the non-canonical representation of <var>url</var>. This will
1154 * return null if data identified by the canonical Uri can not be found in
1155 * the current environment; callers must always check for null and deal with
1156 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001157 *
1158 * @see #canonicalize
1159 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001160 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001161 public final @Nullable Uri uncanonicalize(@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.uncanonicalize(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.uncanonicalize(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 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001187 * This allows clients to request an explicit refresh of content identified
1188 * by {@code uri}.
Ben Lin1cf454f2016-11-10 13:50:54 -08001189 * <p>
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001190 * Client code should only invoke this method when there is a strong
1191 * indication (such as a user initiated pull to refresh gesture) that the
1192 * content is stale.
Ben Lin1cf454f2016-11-10 13:50:54 -08001193 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -08001194 *
Ben Lin2b64a882016-11-11 15:24:58 -08001195 * @param url The Uri identifying the data to refresh.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001196 * @param extras Additional options from the client. The definitions of
1197 * these are specific to the content provider being called.
1198 * @param cancellationSignal A signal to cancel the operation in progress,
1199 * or {@code null} if none. For example, if you called refresh on
1200 * a particular uri, you should call
1201 * {@link CancellationSignal#throwIfCanceled()} to check whether
1202 * the client has canceled the refresh request.
Ben Lin1cf454f2016-11-10 13:50:54 -08001203 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -08001204 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001205 @Override
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001206 public final boolean refresh(@NonNull Uri url, @Nullable Bundle extras,
Ben Lin1cf454f2016-11-10 13:50:54 -08001207 @Nullable CancellationSignal cancellationSignal) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001208 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001209
1210 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001211 if (mWrapped != null) return mWrapped.refresh(url, extras, cancellationSignal);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001212 } catch (RemoteException e) {
1213 return false;
1214 }
1215
Ben Lin1cf454f2016-11-10 13:50:54 -08001216 IContentProvider provider = acquireProvider(url);
1217 if (provider == null) {
1218 return false;
1219 }
1220
1221 try {
1222 ICancellationSignal remoteCancellationSignal = null;
1223 if (cancellationSignal != null) {
1224 cancellationSignal.throwIfCanceled();
1225 remoteCancellationSignal = provider.createCancellationSignal();
1226 cancellationSignal.setRemote(remoteCancellationSignal);
1227 }
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001228 return provider.refresh(mPackageName, mFeatureId, url, extras,
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001229 remoteCancellationSignal);
Ben Lin1cf454f2016-11-10 13:50:54 -08001230 } catch (RemoteException e) {
1231 // Arbitrary and not worth documenting, as Activity
1232 // Manager will kill this process shortly anyway.
1233 return false;
1234 } finally {
1235 releaseProvider(provider);
1236 }
1237 }
1238
Jeff Sharkey9edef252019-05-20 14:00:17 -06001239 /** {@hide} */
1240 @Override
1241 public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001242 Objects.requireNonNull(uri, "uri");
Jeff Sharkey9edef252019-05-20 14:00:17 -06001243
1244 try {
1245 if (mWrapped != null) return mWrapped.checkUriPermission(uri, uid, modeFlags);
1246 } catch (RemoteException e) {
1247 return PackageManager.PERMISSION_DENIED;
1248 }
1249
1250 try (ContentProviderClient client = acquireUnstableContentProviderClient(uri)) {
1251 return client.checkUriPermission(uri, uid, modeFlags);
1252 } catch (RemoteException e) {
1253 return PackageManager.PERMISSION_DENIED;
1254 }
1255 }
1256
Ben Lin1cf454f2016-11-10 13:50:54 -08001257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 * Open a stream on to the content associated with a content URI. If there
1259 * is no data associated with the URI, FileNotFoundException is thrown.
1260 *
1261 * <h5>Accepts the following URI schemes:</h5>
1262 * <ul>
1263 * <li>content ({@link #SCHEME_CONTENT})</li>
1264 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1265 * <li>file ({@link #SCHEME_FILE})</li>
1266 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1269 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 * @param uri The desired URI.
1272 * @return InputStream
1273 * @throws FileNotFoundException if the provided URI could not be opened.
1274 * @see #openAssetFileDescriptor(Uri, String)
1275 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001276 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001278 Objects.requireNonNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 String scheme = uri.getScheme();
1280 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1281 // Note: left here to avoid breaking compatibility. May be removed
1282 // with sufficient testing.
1283 OpenResourceIdResult r = getResourceId(uri);
1284 try {
1285 InputStream stream = r.r.openRawResource(r.id);
1286 return stream;
1287 } catch (Resources.NotFoundException ex) {
1288 throw new FileNotFoundException("Resource does not exist: " + uri);
1289 }
1290 } else if (SCHEME_FILE.equals(scheme)) {
1291 // Note: left here to avoid breaking compatibility. May be removed
1292 // with sufficient testing.
1293 return new FileInputStream(uri.getPath());
1294 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001295 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 try {
1297 return fd != null ? fd.createInputStream() : null;
1298 } catch (IOException e) {
1299 throw new FileNotFoundException("Unable to create stream");
1300 }
1301 }
1302 }
1303
1304 /**
1305 * Synonym for {@link #openOutputStream(Uri, String)
1306 * openOutputStream(uri, "w")}.
1307 * @throws FileNotFoundException if the provided URI could not be opened.
1308 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001309 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 throws FileNotFoundException {
1311 return openOutputStream(uri, "w");
1312 }
1313
1314 /**
1315 * Open a stream on to the content associated with a content URI. If there
1316 * is no data associated with the URI, FileNotFoundException is thrown.
1317 *
1318 * <h5>Accepts the following URI schemes:</h5>
1319 * <ul>
1320 * <li>content ({@link #SCHEME_CONTENT})</li>
1321 * <li>file ({@link #SCHEME_FILE})</li>
1322 * </ul>
1323 *
1324 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1325 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 * @param uri The desired URI.
1328 * @param mode May be "w", "wa", "rw", or "rwt".
1329 * @return OutputStream
1330 * @throws FileNotFoundException if the provided URI could not be opened.
1331 * @see #openAssetFileDescriptor(Uri, String)
1332 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001333 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001335 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 try {
1337 return fd != null ? fd.createOutputStream() : null;
1338 } catch (IOException e) {
1339 throw new FileNotFoundException("Unable to create stream");
1340 }
1341 }
1342
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001343 @Override
1344 public final @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
1345 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001346 try {
1347 if (mWrapped != null) return mWrapped.openFile(uri, mode, signal);
1348 } catch (RemoteException e) {
1349 return null;
1350 }
1351
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001352 return openFileDescriptor(uri, mode, signal);
1353 }
1354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001356 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1358 * underlying {@link ContentProvider#openFile}
1359 * ContentProvider.openFile()} method, so will <em>not</em> work with
1360 * providers that return sub-sections of files. If at all possible,
1361 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1362 * will receive a FileNotFoundException exception if the provider returns a
1363 * sub-section of a file.
1364 *
1365 * <h5>Accepts the following URI schemes:</h5>
1366 * <ul>
1367 * <li>content ({@link #SCHEME_CONTENT})</li>
1368 * <li>file ({@link #SCHEME_FILE})</li>
1369 * </ul>
1370 *
1371 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1372 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001373 * <p>
1374 * If opening with the exclusive "r" or "w" modes, the returned
1375 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1376 * of data. Opening with the "rw" mode implies a file on disk that supports
1377 * seeking. If possible, always use an exclusive mode to give the underlying
1378 * {@link ContentProvider} the most flexibility.
1379 * <p>
1380 * If you are writing a file, and need to communicate an error to the
1381 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 * @param uri The desired URI to open.
1384 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1385 * ContentProvider.openFile}.
1386 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1387 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001388 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 * file exists under the URI or the mode is invalid.
1390 * @see #openAssetFileDescriptor(Uri, String)
1391 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001392 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1393 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001394 return openFileDescriptor(uri, mode, null);
1395 }
1396
1397 /**
1398 * Open a raw file descriptor to access data under a URI. This
1399 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1400 * underlying {@link ContentProvider#openFile}
1401 * ContentProvider.openFile()} method, so will <em>not</em> work with
1402 * providers that return sub-sections of files. If at all possible,
1403 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1404 * will receive a FileNotFoundException exception if the provider returns a
1405 * sub-section of a file.
1406 *
1407 * <h5>Accepts the following URI schemes:</h5>
1408 * <ul>
1409 * <li>content ({@link #SCHEME_CONTENT})</li>
1410 * <li>file ({@link #SCHEME_FILE})</li>
1411 * </ul>
1412 *
1413 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1414 * on these schemes.
1415 * <p>
1416 * If opening with the exclusive "r" or "w" modes, the returned
1417 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1418 * of data. Opening with the "rw" mode implies a file on disk that supports
1419 * seeking. If possible, always use an exclusive mode to give the underlying
1420 * {@link ContentProvider} the most flexibility.
1421 * <p>
1422 * If you are writing a file, and need to communicate an error to the
1423 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1424 *
1425 * @param uri The desired URI to open.
1426 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1427 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001428 * @param cancellationSignal A signal to cancel the operation in progress,
1429 * or null if none. If the operation is canceled, then
1430 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001431 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1432 * own this descriptor and are responsible for closing it when done.
1433 * @throws FileNotFoundException Throws FileNotFoundException if no
1434 * file exists under the URI or the mode is invalid.
1435 * @see #openAssetFileDescriptor(Uri, String)
1436 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001437 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1438 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1439 throws FileNotFoundException {
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001440 try {
1441 if (mWrapped != null) return mWrapped.openFile(uri, mode, cancellationSignal);
1442 } catch (RemoteException e) {
1443 return null;
1444 }
1445
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001446 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 if (afd == null) {
1448 return null;
1449 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (afd.getDeclaredLength() < 0) {
1452 // This is a full file!
1453 return afd.getParcelFileDescriptor();
1454 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 // Client can't handle a sub-section of a file, so close what
1457 // we got and bail with an exception.
1458 try {
1459 afd.close();
1460 } catch (IOException e) {
1461 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 throw new FileNotFoundException("Not a whole file");
1464 }
1465
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001466 @Override
1467 public final @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
1468 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001469 try {
1470 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, signal);
1471 } catch (RemoteException e) {
1472 return null;
1473 }
1474
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001475 return openAssetFileDescriptor(uri, mode, signal);
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001479 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001481 * 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 -08001482 *
1483 * <h5>Accepts the following URI schemes:</h5>
1484 * <ul>
1485 * <li>content ({@link #SCHEME_CONTENT})</li>
1486 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1487 * <li>file ({@link #SCHEME_FILE})</li>
1488 * </ul>
1489 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1490 * <p>
1491 * A Uri object can be used to reference a resource in an APK file. The
1492 * Uri should be one of the following formats:
1493 * <ul>
1494 * <li><code>android.resource://package_name/id_number</code><br/>
1495 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1496 * For example <code>com.example.myapp</code><br/>
1497 * <code>id_number</code> is the int form of the ID.<br/>
1498 * The easiest way to construct this form is
1499 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1500 * </li>
1501 * <li><code>android.resource://package_name/type/name</code><br/>
1502 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1503 * For example <code>com.example.myapp</code><br/>
1504 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1505 * or <code>drawable</code>.
1506 * <code>name</code> is the string form of the resource name. That is, whatever the file
1507 * name was in your res directory, without the type extension.
1508 * The easiest way to construct this form is
1509 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1510 * </li>
1511 * </ul>
1512 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001513 * <p>Note that if this function is called for read-only input (mode is "r")
1514 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001515 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001516 * from any built-in data conversion that a provider implements.
1517 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 * @param uri The desired URI to open.
1519 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1520 * ContentProvider.openAssetFile}.
1521 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1522 * own this descriptor and are responsible for closing it when done.
1523 * @throws FileNotFoundException Throws FileNotFoundException of no
1524 * file exists under the URI or the mode is invalid.
1525 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001526 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1527 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001528 return openAssetFileDescriptor(uri, mode, null);
1529 }
1530
1531 /**
1532 * Open a raw file descriptor to access data under a URI. This
1533 * interacts with the underlying {@link ContentProvider#openAssetFile}
1534 * method of the provider associated with the given URI, to retrieve any file stored there.
1535 *
1536 * <h5>Accepts the following URI schemes:</h5>
1537 * <ul>
1538 * <li>content ({@link #SCHEME_CONTENT})</li>
1539 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1540 * <li>file ({@link #SCHEME_FILE})</li>
1541 * </ul>
1542 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1543 * <p>
1544 * A Uri object can be used to reference a resource in an APK file. The
1545 * Uri should be one of the following formats:
1546 * <ul>
1547 * <li><code>android.resource://package_name/id_number</code><br/>
1548 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1549 * For example <code>com.example.myapp</code><br/>
1550 * <code>id_number</code> is the int form of the ID.<br/>
1551 * The easiest way to construct this form is
1552 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1553 * </li>
1554 * <li><code>android.resource://package_name/type/name</code><br/>
1555 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1556 * For example <code>com.example.myapp</code><br/>
1557 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1558 * or <code>drawable</code>.
1559 * <code>name</code> is the string form of the resource name. That is, whatever the file
1560 * name was in your res directory, without the type extension.
1561 * The easiest way to construct this form is
1562 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1563 * </li>
1564 * </ul>
1565 *
1566 * <p>Note that if this function is called for read-only input (mode is "r")
1567 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001568 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001569 * from any built-in data conversion that a provider implements.
1570 *
1571 * @param uri The desired URI to open.
1572 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1573 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001574 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001575 * none. If the operation is canceled, then
1576 * {@link OperationCanceledException} will be thrown.
1577 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1578 * own this descriptor and are responsible for closing it when done.
1579 * @throws FileNotFoundException Throws FileNotFoundException of no
1580 * file exists under the URI or the mode is invalid.
1581 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001582 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1583 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1584 throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001585 Objects.requireNonNull(uri, "uri");
1586 Objects.requireNonNull(mode, "mode");
Jeff Sharkey673db442015-06-11 19:30:57 -07001587
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001588 try {
1589 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, cancellationSignal);
1590 } catch (RemoteException e) {
1591 return null;
1592 }
1593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 String scheme = uri.getScheme();
1595 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1596 if (!"r".equals(mode)) {
1597 throw new FileNotFoundException("Can't write resources: " + uri);
1598 }
1599 OpenResourceIdResult r = getResourceId(uri);
1600 try {
1601 return r.r.openRawResourceFd(r.id);
1602 } catch (Resources.NotFoundException ex) {
1603 throw new FileNotFoundException("Resource does not exist: " + uri);
1604 }
1605 } else if (SCHEME_FILE.equals(scheme)) {
1606 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001607 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 return new AssetFileDescriptor(pfd, 0, -1);
1609 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001610 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001611 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001612 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001613 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1614 if (unstableProvider == null) {
1615 throw new FileNotFoundException("No content provider: " + uri);
1616 }
1617 IContentProvider stableProvider = null;
1618 AssetFileDescriptor fd = null;
1619
1620 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001621 ICancellationSignal remoteCancellationSignal = null;
1622 if (cancellationSignal != null) {
1623 cancellationSignal.throwIfCanceled();
1624 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1625 cancellationSignal.setRemote(remoteCancellationSignal);
1626 }
1627
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001628 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001629 fd = unstableProvider.openAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001630 mPackageName, mFeatureId, uri, mode, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001631 if (fd == null) {
1632 // The provider will be released by the finally{} clause
1633 return null;
1634 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001635 } catch (DeadObjectException e) {
1636 // The remote process has died... but we only hold an unstable
1637 // reference though, so we might recover!!! Let's try!!!!
1638 // This is exciting!!1!!1!!!!1
1639 unstableProviderDied(unstableProvider);
1640 stableProvider = acquireProvider(uri);
1641 if (stableProvider == null) {
1642 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001643 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001644 fd = stableProvider.openAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001645 mPackageName, mFeatureId, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001646 if (fd == null) {
1647 // The provider will be released by the finally{} clause
1648 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001649 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001650 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001651
1652 if (stableProvider == null) {
1653 stableProvider = acquireProvider(uri);
1654 }
1655 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001656 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001657 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1658 fd.getParcelFileDescriptor(), stableProvider);
1659
1660 // Success! Don't release the provider when exiting, let
1661 // ParcelFileDescriptorInner do that when it is closed.
1662 stableProvider = null;
1663
1664 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1665 fd.getDeclaredLength());
1666
1667 } catch (RemoteException e) {
1668 // Whatever, whatever, we'll go away.
1669 throw new FileNotFoundException(
1670 "Failed opening content provider: " + uri);
1671 } catch (FileNotFoundException e) {
1672 throw e;
1673 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001674 if (cancellationSignal != null) {
1675 cancellationSignal.setRemote(null);
1676 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001677 if (stableProvider != null) {
1678 releaseProvider(stableProvider);
1679 }
1680 if (unstableProvider != null) {
1681 releaseUnstableProvider(unstableProvider);
1682 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001683 }
1684 }
1685 }
1686 }
1687
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001688 @Override
1689 public final @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
1690 @NonNull String mimeTypeFilter, @Nullable Bundle opts,
1691 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001692 try {
1693 if (mWrapped != null) {
1694 return mWrapped.openTypedAssetFile(uri, mimeTypeFilter, opts, signal);
1695 }
1696 } catch (RemoteException e) {
1697 return null;
1698 }
1699
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001700 return openTypedAssetFileDescriptor(uri, mimeTypeFilter, opts, signal);
1701 }
1702
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001703 /**
1704 * Open a raw file descriptor to access (potentially type transformed)
1705 * data from a "content:" URI. This interacts with the underlying
1706 * {@link ContentProvider#openTypedAssetFile} method of the provider
1707 * associated with the given URI, to retrieve retrieve any appropriate
1708 * data stream for the data stored there.
1709 *
1710 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1711 * with "content:" URIs, because content providers are the only facility
1712 * with an associated MIME type to ensure that the returned data stream
1713 * is of the desired type.
1714 *
1715 * <p>All text/* streams are encoded in UTF-8.
1716 *
1717 * @param uri The desired URI to open.
1718 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001719 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001720 * select a type, though there is no way for you to determine what type
1721 * it is returning.
1722 * @param opts Additional provider-dependent options.
1723 * @return Returns a new ParcelFileDescriptor from which you can read the
1724 * data stream from the provider. Note that this may be a pipe, meaning
1725 * you can't seek in it. The only seek you should do is if the
1726 * AssetFileDescriptor contains an offset, to move to that offset before
1727 * reading. You own this descriptor and are responsible for closing it when done.
1728 * @throws FileNotFoundException Throws FileNotFoundException of no
1729 * data of the desired type exists under the URI.
1730 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001731 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1732 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001733 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1734 }
1735
1736 /**
1737 * Open a raw file descriptor to access (potentially type transformed)
1738 * data from a "content:" URI. This interacts with the underlying
1739 * {@link ContentProvider#openTypedAssetFile} method of the provider
1740 * associated with the given URI, to retrieve retrieve any appropriate
1741 * data stream for the data stored there.
1742 *
1743 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1744 * with "content:" URIs, because content providers are the only facility
1745 * with an associated MIME type to ensure that the returned data stream
1746 * is of the desired type.
1747 *
1748 * <p>All text/* streams are encoded in UTF-8.
1749 *
1750 * @param uri The desired URI to open.
1751 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001752 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001753 * select a type, though there is no way for you to determine what type
1754 * it is returning.
1755 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001756 * @param cancellationSignal A signal to cancel the operation in progress,
1757 * or null if none. If the operation is canceled, then
1758 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001759 * @return Returns a new ParcelFileDescriptor from which you can read the
1760 * data stream from the provider. Note that this may be a pipe, meaning
1761 * you can't seek in it. The only seek you should do is if the
1762 * AssetFileDescriptor contains an offset, to move to that offset before
1763 * reading. You own this descriptor and are responsible for closing it when done.
1764 * @throws FileNotFoundException Throws FileNotFoundException of no
1765 * data of the desired type exists under the URI.
1766 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001767 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1768 @NonNull String mimeType, @Nullable Bundle opts,
1769 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001770 Objects.requireNonNull(uri, "uri");
1771 Objects.requireNonNull(mimeType, "mimeType");
Jeff Sharkey673db442015-06-11 19:30:57 -07001772
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001773 try {
1774 if (mWrapped != null) return mWrapped.openTypedAssetFile(uri, mimeType, opts, cancellationSignal);
1775 } catch (RemoteException e) {
1776 return null;
1777 }
1778
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001779 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1780 if (unstableProvider == null) {
1781 throw new FileNotFoundException("No content provider: " + uri);
1782 }
1783 IContentProvider stableProvider = null;
1784 AssetFileDescriptor fd = null;
1785
1786 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001787 ICancellationSignal remoteCancellationSignal = null;
1788 if (cancellationSignal != null) {
1789 cancellationSignal.throwIfCanceled();
1790 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1791 cancellationSignal.setRemote(remoteCancellationSignal);
1792 }
1793
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001794 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001795 fd = unstableProvider.openTypedAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001796 mPackageName, mFeatureId, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001797 if (fd == null) {
1798 // The provider will be released by the finally{} clause
1799 return null;
1800 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001801 } catch (DeadObjectException e) {
1802 // The remote process has died... but we only hold an unstable
1803 // reference though, so we might recover!!! Let's try!!!!
1804 // This is exciting!!1!!1!!!!1
1805 unstableProviderDied(unstableProvider);
1806 stableProvider = acquireProvider(uri);
1807 if (stableProvider == null) {
1808 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001809 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001810 fd = stableProvider.openTypedAssetFile(
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001811 mPackageName, mFeatureId, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001812 if (fd == null) {
1813 // The provider will be released by the finally{} clause
1814 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001817
1818 if (stableProvider == null) {
1819 stableProvider = acquireProvider(uri);
1820 }
1821 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001822 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001823 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1824 fd.getParcelFileDescriptor(), stableProvider);
1825
1826 // Success! Don't release the provider when exiting, let
1827 // ParcelFileDescriptorInner do that when it is closed.
1828 stableProvider = null;
1829
1830 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
Varun Shah840a9c42019-10-17 16:07:53 -07001831 fd.getDeclaredLength(), fd.getExtras());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001832
1833 } catch (RemoteException e) {
1834 // Whatever, whatever, we'll go away.
1835 throw new FileNotFoundException(
1836 "Failed opening content provider: " + uri);
1837 } catch (FileNotFoundException e) {
1838 throw e;
1839 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001840 if (cancellationSignal != null) {
1841 cancellationSignal.setRemote(null);
1842 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001843 if (stableProvider != null) {
1844 releaseProvider(stableProvider);
1845 }
1846 if (unstableProvider != null) {
1847 releaseUnstableProvider(unstableProvider);
1848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
1850 }
1851
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001852 /**
1853 * A resource identified by the {@link Resources} that contains it, and a resource id.
1854 *
1855 * @hide
1856 */
1857 public class OpenResourceIdResult {
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001858 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001859 public Resources r;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001860 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001861 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001863
1864 /**
1865 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1866 *
1867 * @hide
1868 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001869 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001870 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 String authority = uri.getAuthority();
1872 Resources r;
1873 if (TextUtils.isEmpty(authority)) {
1874 throw new FileNotFoundException("No authority: " + uri);
1875 } else {
1876 try {
1877 r = mContext.getPackageManager().getResourcesForApplication(authority);
1878 } catch (NameNotFoundException ex) {
1879 throw new FileNotFoundException("No package found for authority: " + uri);
1880 }
1881 }
1882 List<String> path = uri.getPathSegments();
1883 if (path == null) {
1884 throw new FileNotFoundException("No path: " + uri);
1885 }
1886 int len = path.size();
1887 int id;
1888 if (len == 1) {
1889 try {
1890 id = Integer.parseInt(path.get(0));
1891 } catch (NumberFormatException e) {
1892 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
1893 }
1894 } else if (len == 2) {
1895 id = r.getIdentifier(path.get(1), path.get(0), authority);
1896 } else {
1897 throw new FileNotFoundException("More than two path segments: " + uri);
1898 }
1899 if (id == 0) {
1900 throw new FileNotFoundException("No resource found for: " + uri);
1901 }
1902 OpenResourceIdResult res = new OpenResourceIdResult();
1903 res.r = r;
1904 res.id = id;
1905 return res;
1906 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 /**
1909 * Inserts a row into a table at the given URL.
1910 *
1911 * If the content provider supports transactions the insertion will be atomic.
1912 *
1913 * @param url The URL of the table to insert into.
1914 * @param values The initial values for the newly inserted row. The key is the column name for
1915 * the field. Passing an empty ContentValues will create an empty row.
Varun Shahc4fdfa32019-05-10 14:10:08 -07001916 * @return the URL of the newly created row. May return <code>null</code> if the underlying
1917 * content provider returns <code>null</code>, or if it crashes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001919 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1920 @Nullable ContentValues values) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001921 return insert(url, values, null);
1922 }
1923
1924 /**
1925 * Inserts a row into a table at the given URL.
1926 *
1927 * If the content provider supports transactions the insertion will be atomic.
1928 *
1929 * @param url The URL of the table to insert into.
1930 * @param values The initial values for the newly inserted row. The key is the column name for
1931 * the field. Passing an empty ContentValues will create an empty row.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001932 * @param extras A Bundle containing additional information necessary for
1933 * the operation. Arguments may include SQL style arguments, such
1934 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1935 * the documentation for each individual provider will indicate
1936 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001937 * @return the URL of the newly created row. May return <code>null</code> if the underlying
1938 * content provider returns <code>null</code>, or if it crashes.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001939 * @throws IllegalArgumentException if the provider doesn't support one of
1940 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001941 */
1942 @Override
1943 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1944 @Nullable ContentValues values, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001945 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001946
1947 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001948 if (mWrapped != null) return mWrapped.insert(url, values, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001949 } catch (RemoteException e) {
1950 return null;
1951 }
1952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 IContentProvider provider = acquireProvider(url);
1954 if (provider == null) {
1955 throw new IllegalArgumentException("Unknown URL " + url);
1956 }
1957 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001958 long startTime = SystemClock.uptimeMillis();
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001959 Uri createdRow = provider.insert(mPackageName, mFeatureId, url, values, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001960 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001961 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
1962 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001964 // Arbitrary and not worth documenting, as Activity
1965 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 return null;
1967 } finally {
1968 releaseProvider(provider);
1969 }
1970 }
1971
Fred Quintana89437372009-05-15 15:10:40 -07001972 /**
Fred Quintana89437372009-05-15 15:10:40 -07001973 * Applies each of the {@link ContentProviderOperation} objects and returns an array
1974 * of their results. Passes through OperationApplicationException, which may be thrown
1975 * by the call to {@link ContentProviderOperation#apply}.
1976 * If all the applications succeed then a {@link ContentProviderResult} array with the
1977 * same number of elements as the operations will be returned. It is implementation-specific
1978 * how many, if any, operations will have been successfully applied if a call to
1979 * apply results in a {@link OperationApplicationException}.
1980 * @param authority the authority of the ContentProvider to which this batch should be applied
1981 * @param operations the operations to apply
1982 * @return the results of the applications
1983 * @throws OperationApplicationException thrown if an application fails.
1984 * See {@link ContentProviderOperation#apply} for more information.
1985 * @throws RemoteException thrown if a RemoteException is encountered while attempting
1986 * to communicate with a remote provider.
1987 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001988 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001989 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
1990 @NonNull ArrayList<ContentProviderOperation> operations)
1991 throws RemoteException, OperationApplicationException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001992 Objects.requireNonNull(authority, "authority");
1993 Objects.requireNonNull(operations, "operations");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001994
1995 try {
1996 if (mWrapped != null) return mWrapped.applyBatch(authority, operations);
1997 } catch (RemoteException e) {
1998 return null;
1999 }
2000
Fred Quintana89437372009-05-15 15:10:40 -07002001 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002002 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07002003 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002004 }
2005 try {
Fred Quintana89437372009-05-15 15:10:40 -07002006 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002007 } finally {
2008 provider.release();
2009 }
2010 }
2011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 /**
2013 * Inserts multiple rows into a table at the given URL.
2014 *
2015 * This function make no guarantees about the atomicity of the insertions.
2016 *
2017 * @param url The URL of the table to insert into.
2018 * @param values The initial values for the newly inserted rows. The key is the column name for
2019 * the field. Passing null will create an empty row.
2020 * @return the number of newly created rows.
2021 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002022 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07002023 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
2024 @NonNull ContentValues[] values) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002025 Objects.requireNonNull(url, "url");
2026 Objects.requireNonNull(values, "values");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002027
2028 try {
2029 if (mWrapped != null) return mWrapped.bulkInsert(url, values);
2030 } catch (RemoteException e) {
2031 return 0;
2032 }
2033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 IContentProvider provider = acquireProvider(url);
2035 if (provider == null) {
2036 throw new IllegalArgumentException("Unknown URL " + url);
2037 }
2038 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002039 long startTime = SystemClock.uptimeMillis();
Philip P. Moltmann128b7032019-09-27 08:44:12 -07002040 int rowsCreated = provider.bulkInsert(mPackageName, mFeatureId, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002041 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002042 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
2043 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002045 // Arbitrary and not worth documenting, as Activity
2046 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 return 0;
2048 } finally {
2049 releaseProvider(provider);
2050 }
2051 }
2052
2053 /**
2054 * Deletes row(s) specified by a content URI.
2055 *
2056 * If the content provider supports transactions, the deletion will be atomic.
2057 *
2058 * @param url The URL of the row to delete.
2059 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
2060 (excluding the WHERE itself).
2061 * @return The number of rows deleted.
2062 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002063 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07002064 @Nullable String[] selectionArgs) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002065 return delete(url, createSqlQueryBundle(where, selectionArgs));
2066 }
2067
2068 /**
2069 * Deletes row(s) specified by a content URI.
2070 *
2071 * If the content provider supports transactions, the deletion will be atomic.
2072 *
2073 * @param url The URL of the row to delete.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002074 * @param extras A Bundle containing additional information necessary for
2075 * the operation. Arguments may include SQL style arguments, such
2076 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2077 * the documentation for each individual provider will indicate
2078 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002079 * @return The number of rows deleted.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002080 * @throws IllegalArgumentException if the provider doesn't support one of
2081 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002082 */
2083 @Override
2084 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002085 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002086
2087 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002088 if (mWrapped != null) return mWrapped.delete(url, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002089 } catch (RemoteException e) {
2090 return 0;
2091 }
2092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 IContentProvider provider = acquireProvider(url);
2094 if (provider == null) {
2095 throw new IllegalArgumentException("Unknown URL " + url);
2096 }
2097 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002098 long startTime = SystemClock.uptimeMillis();
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002099 int rowsDeleted = provider.delete(mPackageName, mFeatureId, url, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002100 long durationMillis = SystemClock.uptimeMillis() - startTime;
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002101 maybeLogUpdateToEventLog(durationMillis, url, "delete", null);
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002102 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002104 // Arbitrary and not worth documenting, as Activity
2105 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 return -1;
2107 } finally {
2108 releaseProvider(provider);
2109 }
2110 }
2111
2112 /**
2113 * Update row(s) in a content URI.
2114 *
2115 * If the content provider supports transactions the update will be atomic.
2116 *
2117 * @param uri The URI to modify.
2118 * @param values The new field values. The key is the column name for the field.
2119 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08002120 * @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 -08002121 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002122 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 * @throws NullPointerException if uri or values are null
2124 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002125 public final int update(@RequiresPermission.Write @NonNull Uri uri,
2126 @Nullable ContentValues values, @Nullable String where,
2127 @Nullable String[] selectionArgs) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002128 return update(uri, values, createSqlQueryBundle(where, selectionArgs));
2129 }
2130
2131 /**
2132 * Update row(s) in a content URI.
2133 *
2134 * If the content provider supports transactions the update will be atomic.
2135 *
2136 * @param uri The URI to modify.
2137 * @param values The new field values. The key is the column name for the field.
2138 A null value will remove an existing field value.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002139 * @param extras A Bundle containing additional information necessary for
2140 * the operation. Arguments may include SQL style arguments, such
2141 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2142 * the documentation for each individual provider will indicate
2143 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002144 * @return the number of rows updated.
2145 * @throws NullPointerException if uri or values are null
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002146 * @throws IllegalArgumentException if the provider doesn't support one of
2147 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002148 */
2149 @Override
2150 public final int update(@RequiresPermission.Write @NonNull Uri uri,
2151 @Nullable ContentValues values, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002152 Objects.requireNonNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002153
2154 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002155 if (mWrapped != null) return mWrapped.update(uri, values, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002156 } catch (RemoteException e) {
2157 return 0;
2158 }
2159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 IContentProvider provider = acquireProvider(uri);
2161 if (provider == null) {
2162 throw new IllegalArgumentException("Unknown URI " + uri);
2163 }
2164 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002165 long startTime = SystemClock.uptimeMillis();
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002166 int rowsUpdated = provider.update(mPackageName, mFeatureId, uri, values, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002167 long durationMillis = SystemClock.uptimeMillis() - startTime;
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002168 maybeLogUpdateToEventLog(durationMillis, uri, "update", null);
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002169 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002171 // Arbitrary and not worth documenting, as Activity
2172 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 return -1;
2174 } finally {
2175 releaseProvider(provider);
2176 }
2177 }
2178
2179 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09002180 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002181 * read or write interfaces which are cheaper than using a Cursor and/or
2182 * do not fit into the traditional table model.
2183 *
2184 * @param method provider-defined method name to call. Opaque to
2185 * framework, but must be non-null.
2186 * @param arg provider-defined String argument. May be null.
2187 * @param extras provider-defined Bundle argument. May be null.
2188 * @return a result Bundle, possibly null. Will be null if the ContentProvider
2189 * does not implement call.
2190 * @throws NullPointerException if uri or method is null
2191 * @throws IllegalArgumentException if uri is not known
2192 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002193 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
2194 @Nullable String arg, @Nullable Bundle extras) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002195 return call(uri.getAuthority(), method, arg, extras);
2196 }
2197
2198 @Override
2199 public final @Nullable Bundle call(@NonNull String authority, @NonNull String method,
2200 @Nullable String arg, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002201 Objects.requireNonNull(authority, "authority");
2202 Objects.requireNonNull(method, "method");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002203
2204 try {
2205 if (mWrapped != null) return mWrapped.call(authority, method, arg, extras);
2206 } catch (RemoteException e) {
2207 return null;
2208 }
2209
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002210 IContentProvider provider = acquireProvider(authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002211 if (provider == null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002212 throw new IllegalArgumentException("Unknown authority " + authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002213 }
2214 try {
Philip P. Moltmann128b7032019-09-27 08:44:12 -07002215 final Bundle res = provider.call(mPackageName, mFeatureId, authority, method, arg,
2216 extras);
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002217 Bundle.setDefusable(res, true);
2218 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002219 } catch (RemoteException e) {
2220 // Arbitrary and not worth documenting, as Activity
2221 // Manager will kill this process shortly anyway.
2222 return null;
2223 } finally {
2224 releaseProvider(provider);
2225 }
2226 }
2227
2228 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002229 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 *
2231 * @param uri The URI to a content provider
2232 * @return The ContentProvider for the given URI, or null if no content provider is found.
2233 * @hide
2234 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002235 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002236 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04002237 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 return null;
2239 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002240 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002242 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 }
2244 return null;
2245 }
2246
2247 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002248 * Returns the content provider for the given content URI if the process
2249 * already has a reference on it.
2250 *
2251 * @param uri The URI to a content provider
2252 * @return The ContentProvider for the given URI, or null if no content provider is found.
2253 * @hide
2254 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002255 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002256 public final IContentProvider acquireExistingProvider(Uri uri) {
2257 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2258 return null;
2259 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002260 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002261 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002262 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002263 }
2264 return null;
2265 }
2266
2267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 * @hide
2269 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002270 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002272 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 return null;
2274 }
2275 return acquireProvider(mContext, name);
2276 }
2277
2278 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002279 * Returns the content provider for the given content URI.
2280 *
2281 * @param uri The URI to a content provider
2282 * @return The ContentProvider for the given URI, or null if no content provider is found.
2283 * @hide
2284 */
2285 public final IContentProvider acquireUnstableProvider(Uri uri) {
2286 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2287 return null;
2288 }
2289 String auth = uri.getAuthority();
2290 if (auth != null) {
2291 return acquireUnstableProvider(mContext, uri.getAuthority());
2292 }
2293 return null;
2294 }
2295
2296 /**
2297 * @hide
2298 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002299 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002300 public final IContentProvider acquireUnstableProvider(String name) {
2301 if (name == null) {
2302 return null;
2303 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002304 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002305 }
2306
2307 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07002308 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2309 * that services the content at uri, starting the provider if necessary. Returns
2310 * null if there is no provider associated wih the uri. The caller must indicate that they are
2311 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002312 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002313 * keeping it active.
2314 * @param uri specifies which provider should be acquired
2315 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2316 * that services the content at uri or null if there isn't one.
2317 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002318 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002319 Objects.requireNonNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07002320 IContentProvider provider = acquireProvider(uri);
2321 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002322 return new ContentProviderClient(this, provider, uri.getAuthority(), true);
Fred Quintana718d8a22009-04-29 17:53:20 -07002323 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002324 return null;
2325 }
2326
2327 /**
2328 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2329 * with the authority of name, starting the provider if necessary. Returns
2330 * null if there is no provider associated wih the uri. The caller must indicate that they are
2331 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002332 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002333 * keeping it active.
2334 * @param name specifies which provider should be acquired
2335 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2336 * with the authority of name or null if there isn't one.
2337 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002338 public final @Nullable ContentProviderClient acquireContentProviderClient(
2339 @NonNull String name) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002340 Objects.requireNonNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07002341 IContentProvider provider = acquireProvider(name);
2342 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002343 return new ContentProviderClient(this, provider, name, true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002344 }
2345
2346 return null;
2347 }
2348
2349 /**
2350 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
2351 * not trust the stability of the target content provider. This turns off
2352 * the mechanism in the platform clean up processes that are dependent on
2353 * a content provider if that content provider's process goes away. Normally
2354 * you can safely assume that once you have acquired a provider, you can freely
2355 * use it as needed and it won't disappear, even if your process is in the
2356 * background. If using this method, you need to take care to deal with any
2357 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002358 * so that it can be re-opened later. In particular, catching a
2359 * {@link android.os.DeadObjectException} from the calls there will let you
2360 * know that the content provider has gone away; at that point the current
2361 * ContentProviderClient object is invalid, and you should release it. You
2362 * can acquire a new one if you would like to try to restart the provider
2363 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002364 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002365 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2366 @NonNull Uri uri) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002367 Objects.requireNonNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002368 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002369 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002370 return new ContentProviderClient(this, provider, uri.getAuthority(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002371 }
2372
2373 return null;
2374 }
2375
2376 /**
2377 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
2378 * not trust the stability of the target content provider. This turns off
2379 * the mechanism in the platform clean up processes that are dependent on
2380 * a content provider if that content provider's process goes away. Normally
2381 * you can safely assume that once you have acquired a provider, you can freely
2382 * use it as needed and it won't disappear, even if your process is in the
2383 * background. If using this method, you need to take care to deal with any
2384 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002385 * so that it can be re-opened later. In particular, catching a
2386 * {@link android.os.DeadObjectException} from the calls there will let you
2387 * know that the content provider has gone away; at that point the current
2388 * ContentProviderClient object is invalid, and you should release it. You
2389 * can acquire a new one if you would like to try to restart the provider
2390 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002391 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002392 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2393 @NonNull String name) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002394 Objects.requireNonNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002395 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002396 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002397 return new ContentProviderClient(this, provider, name, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07002398 }
2399
2400 return null;
2401 }
2402
2403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 * Register an observer class that gets callbacks when data identified by a
2405 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002406 * <p>
2407 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2408 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002410 * @param uri The URI to watch for changes. This can be a specific row URI,
2411 * or a base URI for a whole class of content.
2412 * @param notifyForDescendants When false, the observer will be notified
2413 * whenever a change occurs to the exact URI specified by
2414 * <code>uri</code> or to one of the URI's ancestors in the path
2415 * hierarchy. When true, the observer will also be notified
2416 * whenever a change occurs to the URI's descendants in the path
2417 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 * @param observer The object that receives callbacks when changes occur.
2419 * @see #unregisterContentObserver
2420 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002421 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07002422 @NonNull ContentObserver observer) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002423 Objects.requireNonNull(uri, "uri");
2424 Objects.requireNonNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01002425 registerContentObserver(
2426 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002427 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01002428 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002429 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002430 }
2431
2432 /** @hide - designated user version */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002433 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07002434 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002435 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002437 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002438 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002440 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
2442 }
2443
2444 /**
2445 * Unregisters a change observer.
2446 *
2447 * @param observer The previously registered observer that is no longer needed.
2448 * @see #registerContentObserver
2449 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002450 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002451 Objects.requireNonNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 try {
2453 IContentObserver contentObserver = observer.releaseContentObserver();
2454 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002455 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 contentObserver);
2457 }
2458 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002459 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 }
2461 }
2462
2463 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002464 * Notify registered observers that a row was updated and attempt to sync
2465 * changes to the network.
2466 * <p>
2467 * To observe events sent through this call, use
2468 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2469 * <p>
2470 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2471 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 *
Jeff Brown86de0592012-01-23 13:01:18 -08002473 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002474 * @param observer The observer that originated the change, may be
2475 * <code>null</null>. The observer that originated the change
2476 * will only receive the notification if it has requested to
2477 * receive self-change notifications by implementing
2478 * {@link ContentObserver#deliverSelfNotifications()} to return
2479 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002481 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 notifyChange(uri, observer, true /* sync to network */);
2483 }
2484
2485 /**
2486 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002487 * <p>
2488 * To observe events sent through this call, use
2489 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2490 * <p>
2491 * If syncToNetwork is true, this will attempt to schedule a local sync
2492 * using the sync adapter that's registered for the authority of the
2493 * provided uri. No account will be passed to the sync adapter, so all
2494 * matching accounts will be synchronized.
2495 * <p>
2496 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2497 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 *
Jeff Brown86de0592012-01-23 13:01:18 -08002499 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002500 * @param observer The observer that originated the change, may be
2501 * <code>null</null>. The observer that originated the change
2502 * will only receive the notification if it has requested to
2503 * receive self-change notifications by implementing
2504 * {@link ContentObserver#deliverSelfNotifications()} to return
2505 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002506 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002507 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
Jeff Sharkey1307f422019-11-13 13:03:10 -07002508 * @deprecated callers should consider migrating to
2509 * {@link #notifyChange(Uri, ContentObserver, int)}, as it
2510 * offers support for many more options than just
2511 * {@link #NOTIFY_SYNC_TO_NETWORK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002513 @Deprecated
Jeff Sharkey673db442015-06-11 19:30:57 -07002514 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2515 boolean syncToNetwork) {
Jeff Sharkey1307f422019-11-13 13:03:10 -07002516 notifyChange(uri, observer, syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0);
Christopher Tate16aa9732012-09-17 16:23:44 -07002517 }
2518
2519 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002520 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002521 * <p>
2522 * To observe events sent through this call, use
2523 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2524 * <p>
Jeff Sharkey1307f422019-11-13 13:03:10 -07002525 * If {@link #NOTIFY_SYNC_TO_NETWORK} is set, this will attempt to schedule
2526 * a local sync using the sync adapter that's registered for the authority
2527 * of the provided uri. No account will be passed to the sync adapter, so
2528 * all matching accounts will be synchronized.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002529 * <p>
2530 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2531 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002532 *
2533 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002534 * @param observer The observer that originated the change, may be
2535 * <code>null</null>. The observer that originated the change
2536 * will only receive the notification if it has requested to
2537 * receive self-change notifications by implementing
2538 * {@link ContentObserver#deliverSelfNotifications()} to return
2539 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002540 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2541 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2542 */
2543 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2544 @NotifyFlags int flags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002545 Objects.requireNonNull(uri, "uri");
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002546 notifyChange(
2547 ContentProvider.getUriWithoutUserId(uri),
2548 observer,
2549 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002550 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002551 }
2552
2553 /**
Jeff Sharkey1307f422019-11-13 13:03:10 -07002554 * Notify registered observers that several rows have been updated.
2555 * <p>
2556 * To observe events sent through this call, use
2557 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2558 * <p>
2559 * If {@link #NOTIFY_SYNC_TO_NETWORK} is set, this will attempt to schedule
2560 * a local sync using the sync adapter that's registered for the authority
2561 * of the provided uri. No account will be passed to the sync adapter, so
2562 * all matching accounts will be synchronized.
2563 * <p>
2564 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2565 * notifications must be backed by a valid {@link ContentProvider}.
2566 *
2567 * @param uris The uris of the content that was changed.
2568 * @param observer The observer that originated the change, may be
2569 * <code>null</null>. The observer that originated the change
2570 * will only receive the notification if it has requested to
2571 * receive self-change notifications by implementing
2572 * {@link ContentObserver#deliverSelfNotifications()} to return
2573 * true.
2574 * @param flags Flags such as {@link #NOTIFY_SYNC_TO_NETWORK} or
2575 * {@link #NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS}.
2576 */
2577 public void notifyChange(@NonNull Iterable<Uri> uris, @Nullable ContentObserver observer,
2578 @NotifyFlags int flags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002579 Objects.requireNonNull(uris, "uris");
Jeff Sharkey1307f422019-11-13 13:03:10 -07002580
2581 // Cluster based on user ID
2582 final SparseArray<ArrayList<Uri>> clusteredByUser = new SparseArray<>();
2583 for (Uri uri : uris) {
2584 final int userId = ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
2585 ArrayList<Uri> list = clusteredByUser.get(userId);
2586 if (list == null) {
2587 list = new ArrayList<>();
2588 clusteredByUser.put(userId, list);
2589 }
2590 list.add(ContentProvider.getUriWithoutUserId(uri));
2591 }
2592
2593 for (int i = 0; i < clusteredByUser.size(); i++) {
2594 final int userId = clusteredByUser.keyAt(i);
2595 final ArrayList<Uri> list = clusteredByUser.valueAt(i);
2596 notifyChange(list.toArray(new Uri[list.size()]), observer, flags, userId);
2597 }
2598 }
2599
2600 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002601 * Notify registered observers within the designated user(s) that a row was updated.
2602 *
Jeff Sharkey1307f422019-11-13 13:03:10 -07002603 * @deprecated callers should consider migrating to
2604 * {@link #notifyChange(Uri, ContentObserver, int)}, as it
2605 * offers support for many more options than just
2606 * {@link #NOTIFY_SYNC_TO_NETWORK}.
Christopher Tate16aa9732012-09-17 16:23:44 -07002607 * @hide
2608 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002609 @Deprecated
Dianne Hackborne7617772016-04-27 17:03:52 -07002610 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002611 @UserIdInt int userHandle) {
Jeff Sharkey1307f422019-11-13 13:03:10 -07002612 notifyChange(uri, observer, syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0, userHandle);
2613 }
2614
2615 /** {@hide} */
2616 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
2617 @UserIdInt int userHandle) {
2618 notifyChange(new Uri[] { uri }, observer, flags, userHandle);
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002619 }
2620
2621 /**
2622 * Notify registered observers within the designated user(s) that a row was updated.
2623 *
2624 * @hide
2625 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002626 public void notifyChange(@NonNull Uri[] uris, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002627 @UserIdInt int userHandle) {
2628 try {
2629 getContentService().notifyChange(
Jeff Sharkey1307f422019-11-13 13:03:10 -07002630 uris, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002631 observer != null && observer.deliverSelfNotifications(), flags,
Makoto Onukie183a402018-08-29 11:46:41 -07002632 userHandle, mTargetSdkVersion, mContext.getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002634 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 }
2636 }
2637
2638 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002639 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002640 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002641 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002642 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2643 * the grant has already been persisted, taking it again will touch
2644 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002645 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002646 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002647 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002648 public void takePersistableUriPermission(@NonNull Uri uri,
2649 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002650 Objects.requireNonNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002651 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002652 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002653 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2654 resolveUserId(uri));
2655 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002656 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002657 }
2658 }
2659
2660 /**
2661 * @hide
2662 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002663 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002664 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2665 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002666 Objects.requireNonNull(toPackage, "toPackage");
2667 Objects.requireNonNull(uri, "uri");
Felipe Leme988234a2018-02-14 12:00:29 -08002668 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002669 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002670 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2671 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002672 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002673 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002674 }
2675 }
2676
2677 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002678 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002679 * previously made persistent with
2680 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2681 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002682 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002683 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002684 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002685 public void releasePersistableUriPermission(@NonNull Uri uri,
2686 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002687 Objects.requireNonNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002688 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002689 UriGrantsManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002690 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2691 resolveUserId(uri));
2692 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002693 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002694 }
2695 }
2696
2697 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002698 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002699 * calling app. That is, the returned permissions have been granted
2700 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002701 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002702 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002703 *
2704 * @see #takePersistableUriPermission(Uri, int)
2705 * @see #releasePersistableUriPermission(Uri, int)
2706 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002707 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002708 try {
Sudheer Shanka1b817f62019-05-20 16:54:59 -07002709 return UriGrantsManager.getService().getUriPermissions(
2710 mPackageName, true /* incoming */, true /* persistedOnly */).getList();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002711 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002712 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002713 }
2714 }
2715
2716 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002717 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002718 * calling app. That is, the returned permissions have been granted
2719 * <em>from</em> the calling app. Only grants taken with
2720 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002721 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002722 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002723 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002724 try {
Sudheer Shanka1b817f62019-05-20 16:54:59 -07002725 return UriGrantsManager.getService().getUriPermissions(
2726 mPackageName, false /* incoming */, true /* persistedOnly */).getList();
2727 } catch (RemoteException e) {
2728 throw e.rethrowFromSystemServer();
2729 }
2730 }
2731
2732 /** @hide */
2733 public @NonNull List<UriPermission> getOutgoingUriPermissions() {
2734 try {
2735 return UriGrantsManager.getService().getUriPermissions(
2736 mPackageName, false /* incoming */, false /* persistedOnly */).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002737 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002738 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002739 }
2740 }
2741
2742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 * Start an asynchronous sync operation. If you want to monitor the progress
2744 * of the sync you may register a SyncObserver. Only values of the following
2745 * types may be used in the extras bundle:
2746 * <ul>
2747 * <li>Integer</li>
2748 * <li>Long</li>
2749 * <li>Boolean</li>
2750 * <li>Float</li>
2751 * <li>Double</li>
2752 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002753 * <li>Account</li>
2754 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 * </ul>
2756 *
2757 * @param uri the uri of the provider to sync or null to sync all providers.
2758 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002759 * @deprecated instead use
2760 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002762 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002764 Account account = null;
2765 if (extras != null) {
2766 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2767 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002768 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002769 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002770 }
2771 extras.remove(SYNC_EXTRAS_ACCOUNT);
2772 }
2773 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2774 }
2775
2776 /**
2777 * Start an asynchronous sync operation. If you want to monitor the progress
2778 * of the sync you may register a SyncObserver. Only values of the following
2779 * types may be used in the extras bundle:
2780 * <ul>
2781 * <li>Integer</li>
2782 * <li>Long</li>
2783 * <li>Boolean</li>
2784 * <li>Float</li>
2785 * <li>Double</li>
2786 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002787 * <li>Account</li>
2788 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002789 * </ul>
2790 *
2791 * @param account which account should be synced
2792 * @param authority which authority should be synced
2793 * @param extras any extras to pass to the SyncAdapter.
2794 */
2795 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002796 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002797 }
2798
2799 /**
2800 * @see #requestSync(Account, String, Bundle)
2801 * @hide
2802 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002803 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002804 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002805 if (extras == null) {
2806 throw new IllegalArgumentException("Must specify extras.");
2807 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002808 SyncRequest request =
2809 new SyncRequest.Builder()
2810 .setSyncAdapter(account, authority)
2811 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002812 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002813 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002814 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002815 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2816 // case, but it's only for debugging.
2817 getContentService().syncAsUser(request, userId, ActivityThread.currentPackageName());
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002818 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002819 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002820 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002821 }
2822
2823 /**
2824 * Register a sync with the SyncManager. These requests are built using the
2825 * {@link SyncRequest.Builder}.
2826 */
2827 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002829 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2830 // case, but it's only for debugging.
2831 getContentService().sync(request, ActivityThread.currentPackageName());
Matthew Williamsfa774182013-06-18 15:44:11 -07002832 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002833 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 }
2835 }
2836
2837 /**
2838 * Check that only values of the following types are in the Bundle:
2839 * <ul>
2840 * <li>Integer</li>
2841 * <li>Long</li>
2842 * <li>Boolean</li>
2843 * <li>Float</li>
2844 * <li>Double</li>
2845 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002846 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 * <li>null</li>
2848 * </ul>
2849 * @param extras the Bundle to check
2850 */
2851 public static void validateSyncExtrasBundle(Bundle extras) {
2852 try {
2853 for (String key : extras.keySet()) {
2854 Object value = extras.get(key);
2855 if (value == null) continue;
2856 if (value instanceof Long) continue;
2857 if (value instanceof Integer) continue;
2858 if (value instanceof Boolean) continue;
2859 if (value instanceof Float) continue;
2860 if (value instanceof Double) continue;
2861 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002862 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 throw new IllegalArgumentException("unexpected value type: "
2864 + value.getClass().getName());
2865 }
2866 } catch (IllegalArgumentException e) {
2867 throw e;
2868 } catch (RuntimeException exc) {
2869 throw new IllegalArgumentException("error unparceling Bundle", exc);
2870 }
2871 }
2872
Fred Quintanaac9385e2009-06-22 18:00:59 -07002873 /**
2874 * Cancel any active or pending syncs that match the Uri. If the uri is null then
2875 * all syncs will be canceled.
2876 *
2877 * @param uri the uri of the provider to sync or null to sync all providers.
2878 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
2879 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002880 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002882 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
2883 }
2884
2885 /**
2886 * Cancel any active or pending syncs that match account and authority. The account and
2887 * authority can each independently be set to null, which means that syncs with any account
2888 * or authority, respectively, will match.
2889 *
2890 * @param account filters the syncs that match by this account
2891 * @param authority filters the syncs that match by this authority
2892 */
2893 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002895 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002897 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 }
2899 }
2900
Fred Quintanaac9385e2009-06-22 18:00:59 -07002901 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002902 * @see #cancelSync(Account, String)
2903 * @hide
2904 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002905 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002906 try {
2907 getContentService().cancelSyncAsUser(account, authority, null, userId);
2908 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002909 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002910 }
2911 }
2912
2913 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002914 * Get information about the SyncAdapters that are known to the system.
2915 * @return an array of SyncAdapters that have registered with the system
2916 */
2917 public static SyncAdapterType[] getSyncAdapterTypes() {
2918 try {
2919 return getContentService().getSyncAdapterTypes();
2920 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002921 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002922 }
2923 }
2924
2925 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002926 * @see #getSyncAdapterTypes()
2927 * @hide
2928 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002929 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002930 try {
2931 return getContentService().getSyncAdapterTypesAsUser(userId);
2932 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002933 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002934 }
2935 }
2936
2937 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07002938 * @hide
2939 * Returns the package names of syncadapters that match a given user and authority.
2940 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07002941 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07002942 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002943 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002944 try {
2945 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
2946 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002947 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07002948 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07002949 }
2950
2951 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002952 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002953 * <p>This method requires the caller to hold the permission
2954 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002955 *
2956 * @param account the account whose setting we are querying
2957 * @param authority the provider whose setting we are querying
2958 * @return true if the provider should be synced when a network tickle is received
2959 */
2960 public static boolean getSyncAutomatically(Account account, String authority) {
2961 try {
2962 return getContentService().getSyncAutomatically(account, authority);
2963 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002964 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002965 }
2966 }
2967
2968 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002969 * @see #getSyncAutomatically(Account, String)
2970 * @hide
2971 */
2972 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002973 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002974 try {
2975 return getContentService().getSyncAutomaticallyAsUser(account, authority, 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 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002982 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002983 * <p>This method requires the caller to hold the permission
2984 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002985 *
2986 * @param account the account whose setting we are querying
2987 * @param authority the provider whose behavior is being controlled
2988 * @param sync true if the provider should be synced when tickles are received for it
2989 */
2990 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002991 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002992 }
2993
2994 /**
2995 * @see #setSyncAutomatically(Account, String, boolean)
2996 * @hide
2997 */
2998 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002999 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003000 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003001 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003002 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003003 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003004 }
3005 }
3006
3007 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003008 * Specifies that a sync should be requested with the specified the account, authority,
3009 * and extras at the given frequency. If there is already another periodic sync scheduled
3010 * with the account, authority and extras then a new periodic sync won't be added, instead
3011 * the frequency of the previous one will be updated.
3012 * <p>
3013 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
3014 * Although these sync are scheduled at the specified frequency, it may take longer for it to
3015 * actually be started if other syncs are ahead of it in the sync operation queue. This means
3016 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08003017 * <p>
3018 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
3019 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
3020 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
3021 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
3022 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003023 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003024 * <p>This method requires the caller to hold the permission
3025 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003026 * <p>The bundle for a periodic sync can be queried by applications with the correct
3027 * permissions using
3028 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
3029 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003030 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003031 * @param account the account to specify in the sync
3032 * @param authority the provider to specify in the sync request
3033 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07003034 * @param pollFrequency how frequently the sync should be performed, in seconds.
3035 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
3036 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08003037 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
3038 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003039 */
3040 public static void addPeriodicSync(Account account, String authority, Bundle extras,
3041 long pollFrequency) {
3042 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08003043 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08003044 throw new IllegalArgumentException("illegal extras were set");
3045 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003046 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07003047 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003048 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003049 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003050 }
3051 }
3052
3053 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003054 * {@hide}
3055 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
3056 * extras were set for a periodic sync.
3057 *
3058 * @param extras bundle to validate.
3059 */
3060 public static boolean invalidPeriodicExtras(Bundle extras) {
3061 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
3062 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
3063 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
3064 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
3065 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
3066 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
3067 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
3068 return true;
3069 }
3070 return false;
3071 }
3072
3073 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003074 * Remove a periodic sync. Has no affect if account, authority and extras don't match
3075 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003076 * <p>This method requires the caller to hold the permission
3077 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003078 *
3079 * @param account the account of the periodic sync to remove
3080 * @param authority the provider of the periodic sync to remove
3081 * @param extras the extras of the periodic sync to remove
3082 */
3083 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
3084 validateSyncExtrasBundle(extras);
3085 try {
3086 getContentService().removePeriodicSync(account, authority, extras);
3087 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003088 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003089 }
3090 }
3091
3092 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003093 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
3094 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07003095 * <p>
3096 * If a periodic sync is specified, the caller must hold the permission
3097 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
3098 *</p>
3099 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003100 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07003101 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07003102 *
3103 * @param request SyncRequest object containing information about sync to cancel.
3104 */
3105 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003106 if (request == null) {
3107 throw new IllegalArgumentException("request cannot be null");
3108 }
3109 try {
3110 getContentService().cancelRequest(request);
3111 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003112 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003113 }
Matthew Williamsfa774182013-06-18 15:44:11 -07003114 }
3115
3116 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003117 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003118 * <p>This method requires the caller to hold the permission
3119 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003120 *
3121 * @param account the account whose periodic syncs we are querying
3122 * @param authority the provider whose periodic syncs we are querying
3123 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
3124 */
3125 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
3126 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003127 return getContentService().getPeriodicSyncs(account, authority, null);
3128 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003129 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003130 }
3131 }
3132
3133 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07003134 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003135 * <p>This method requires the caller to hold the permission
3136 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07003137 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
3138 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07003139 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07003140 try {
3141 return getContentService().getIsSyncable(account, authority);
3142 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003143 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07003144 }
3145 }
3146
3147 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003148 * @see #getIsSyncable(Account, String)
3149 * @hide
3150 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003151 public static int getIsSyncableAsUser(Account account, String authority,
3152 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003153 try {
3154 return getContentService().getIsSyncableAsUser(account, authority, userId);
3155 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003156 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003157 }
3158 }
3159
3160 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07003161 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003162 * <p>This method requires the caller to hold the permission
3163 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07003164 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07003165 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07003166 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07003167 try {
3168 getContentService().setIsSyncable(account, authority, syncable);
3169 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003170 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07003171 }
3172 }
3173
3174 /**
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +00003175 * @see #setIsSyncable(Account, String, int)
3176 * @hide
3177 */
3178 public static void setIsSyncableAsUser(Account account, String authority, int syncable,
3179 int userId) {
3180 try {
3181 getContentService().setIsSyncableAsUser(account, authority, syncable, userId);
3182 } catch (RemoteException e) {
3183 throw e.rethrowFromSystemServer();
3184 }
3185 }
3186
3187 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003188 * Gets the master auto-sync setting that applies to all the providers and accounts.
3189 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003190 * <p>This method requires the caller to hold the permission
3191 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003192 *
3193 * @return the master auto-sync setting that applies to all the providers and accounts
3194 */
3195 public static boolean getMasterSyncAutomatically() {
3196 try {
3197 return getContentService().getMasterSyncAutomatically();
3198 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003199 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003200 }
3201 }
3202
3203 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003204 * @see #getMasterSyncAutomatically()
3205 * @hide
3206 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003207 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003208 try {
3209 return getContentService().getMasterSyncAutomaticallyAsUser(userId);
3210 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003211 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003212 }
3213 }
3214
3215 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003216 * Sets the master auto-sync setting that applies to all the providers and accounts.
3217 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003218 * <p>This method requires the caller to hold the permission
3219 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003220 *
3221 * @param sync the master auto-sync setting that applies to all the providers and accounts
3222 */
3223 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003224 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01003225 }
3226
3227 /**
3228 * @see #setMasterSyncAutomatically(boolean)
3229 * @hide
3230 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003231 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003232 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01003233 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003234 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003235 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003236 }
3237 }
3238
3239 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003240 * Returns true if there is currently a sync operation for the given account or authority
3241 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003242 * <p>This method requires the caller to hold the permission
3243 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003244 * @param account the account whose setting we are querying
3245 * @param authority the provider whose behavior is being queried
3246 * @return true if a sync is active for the given account or authority.
3247 */
3248 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003249 if (account == null) {
3250 throw new IllegalArgumentException("account must not be null");
3251 }
3252 if (authority == null) {
3253 throw new IllegalArgumentException("authority must not be null");
3254 }
3255
Fred Quintanaac9385e2009-06-22 18:00:59 -07003256 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003257 return getContentService().isSyncActive(account, authority, null);
3258 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003259 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003260 }
3261 }
3262
Fred Quintanaac9385e2009-06-22 18:00:59 -07003263 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07003264 * If a sync is active returns the information about it, otherwise returns null.
3265 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003266 * This method requires the caller to hold the permission
3267 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3268 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003269 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07003270 * @deprecated
3271 * Since multiple concurrent syncs are now supported you should use
3272 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
3273 * This method returns the first item from the list of current syncs
3274 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003275 */
Fred Quintanac6a69552010-09-27 17:05:04 -07003276 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003277 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003278 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07003279 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
3280 if (syncs.isEmpty()) {
3281 return null;
3282 }
3283 return syncs.get(0);
3284 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003285 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07003286 }
3287 }
3288
3289 /**
3290 * Returns a list with information about all the active syncs. This list will be empty
3291 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003292 * <p>
3293 * This method requires the caller to hold the permission
3294 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3295 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07003296 * @return a List of SyncInfo objects for the currently active syncs.
3297 */
3298 public static List<SyncInfo> getCurrentSyncs() {
3299 try {
3300 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003301 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003302 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003303 }
3304 }
3305
3306 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003307 * @see #getCurrentSyncs()
3308 * @hide
3309 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003310 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003311 try {
3312 return getContentService().getCurrentSyncsAsUser(userId);
3313 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003314 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003315 }
3316 }
3317
3318 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07003319 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003320 * @param account the account whose setting we are querying
3321 * @param authority the provider whose behavior is being queried
3322 * @return the SyncStatusInfo for the authority, or null if none exists
3323 * @hide
3324 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003325 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07003326 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
3327 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003328 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003329 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003330 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003331 }
3332 }
3333
3334 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003335 * @see #getSyncStatus(Account, String)
3336 * @hide
3337 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003338 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003339 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003340 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003341 try {
3342 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
3343 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003344 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003345 }
3346 }
3347
3348 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003349 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003350 * <p>This method requires the caller to hold the permission
3351 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003352 * @param account the account whose setting we are querying
3353 * @param authority the provider whose behavior is being queried
3354 * @return true if there is a pending sync with the matching account and authority
3355 */
3356 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003357 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003358 }
3359
3360 /**
3361 * @see #requestSync(Account, String, Bundle)
3362 * @hide
3363 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003364 public static boolean isSyncPendingAsUser(Account account, String authority,
3365 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003366 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003367 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003368 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003369 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003370 }
3371 }
3372
Fred Quintana1b487ec2010-02-26 10:57:55 -08003373 /**
3374 * Request notifications when the different aspects of the SyncManager change. The
3375 * different items that can be requested are:
3376 * <ul>
3377 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
3378 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
3379 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
3380 * </ul>
3381 * The caller can set one or more of the status types in the mask for any
3382 * given listener registration.
3383 * @param mask the status change types that will cause the callback to be invoked
3384 * @param callback observer to be invoked when the status changes
3385 * @return a handle that can be used to remove the listener at a later time
3386 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003387 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003388 if (callback == null) {
3389 throw new IllegalArgumentException("you passed in a null callback");
3390 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003391 try {
3392 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08003393 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07003394 public void onStatusChanged(int which) throws RemoteException {
3395 callback.onStatusChanged(which);
3396 }
3397 };
3398 getContentService().addStatusChangeListener(mask, observer);
3399 return observer;
3400 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003401 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003402 }
3403 }
3404
Fred Quintana1b487ec2010-02-26 10:57:55 -08003405 /**
3406 * Remove a previously registered status change listener.
3407 * @param handle the handle that was returned by {@link #addStatusChangeListener}
3408 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003409 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003410 if (handle == null) {
3411 throw new IllegalArgumentException("you passed in a null handle");
3412 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003413 try {
3414 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
3415 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003416 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003417 }
3418 }
3419
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003420 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003421 * Store the given {@link Bundle} as a long-lived cached object within the
3422 * system. This can be useful to avoid expensive re-parsing when apps are
3423 * restarted multiple times on low-RAM devices.
3424 * <p>
3425 * The {@link Bundle} is automatically invalidated when a
3426 * {@link #notifyChange(Uri, ContentObserver)} event applies to the key.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003427 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003428 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003429 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003430 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003431 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003432 public void putCache(@NonNull Uri key, @Nullable Bundle value) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003433 try {
3434 getContentService().putCache(mContext.getPackageName(), key, value,
3435 mContext.getUserId());
3436 } catch (RemoteException e) {
3437 throw e.rethrowFromSystemServer();
3438 }
3439 }
3440
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003441 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003442 * Retrieve the last {@link Bundle} stored as a long-lived cached object
3443 * within the system.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003444 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003445 * @return {@code null} if no cached object has been stored, or if the
3446 * stored object has been invalidated due to a
3447 * {@link #notifyChange(Uri, ContentObserver)} event.
3448 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003449 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003450 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003451 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003452 public @Nullable Bundle getCache(@NonNull Uri key) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003453 try {
3454 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
3455 mContext.getUserId());
3456 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
3457 return bundle;
3458 } catch (RemoteException e) {
3459 throw e.rethrowFromSystemServer();
3460 }
3461 }
3462
Ben Lin8ea82002017-03-08 17:30:16 -08003463 /** {@hide} */
3464 public int getTargetSdkVersion() {
3465 return mTargetSdkVersion;
3466 }
3467
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003468 /**
3469 * Returns sampling percentage for a given duration.
3470 *
3471 * Always returns at least 1%.
3472 */
3473 private int samplePercentForDuration(long durationMillis) {
3474 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
3475 return 100;
3476 }
3477 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
3478 }
3479
Steve McKayea93fe72016-12-02 11:35:35 -08003480 private void maybeLogQueryToEventLog(
3481 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003482 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003483 int samplePercent = samplePercentForDuration(durationMillis);
3484 if (samplePercent < 100) {
3485 synchronized (mRandom) {
3486 if (mRandom.nextInt(100) >= samplePercent) {
3487 return;
3488 }
3489 }
3490 }
3491
Steve McKayea93fe72016-12-02 11:35:35 -08003492 // Ensure a non-null bundle.
3493 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
3494
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003495 StringBuilder projectionBuffer = new StringBuilder(100);
3496 if (projection != null) {
3497 for (int i = 0; i < projection.length; ++i) {
3498 // Note: not using a comma delimiter here, as the
3499 // multiple arguments to EventLog.writeEvent later
3500 // stringify with a comma delimiter, which would make
3501 // parsing uglier later.
3502 if (i != 0) projectionBuffer.append('/');
3503 projectionBuffer.append(projection[i]);
3504 }
3505 }
3506
3507 // ActivityThread.currentPackageName() only returns non-null if the
3508 // current thread is an application main thread. This parameter tells
3509 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003510 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003511
3512 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003513 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003514 uri.toString(),
3515 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08003516 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
3517 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003518 durationMillis,
3519 blockingPackage != null ? blockingPackage : "",
3520 samplePercent);
3521 }
3522
3523 private void maybeLogUpdateToEventLog(
3524 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003525 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003526 int samplePercent = samplePercentForDuration(durationMillis);
3527 if (samplePercent < 100) {
3528 synchronized (mRandom) {
3529 if (mRandom.nextInt(100) >= samplePercent) {
3530 return;
3531 }
3532 }
3533 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003534 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003535 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003536 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003537 uri.toString(),
3538 operation,
3539 selection != null ? selection : "",
3540 durationMillis,
3541 blockingPackage != null ? blockingPackage : "",
3542 samplePercent);
3543 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003544
Jeff Brown825c5132011-10-12 16:11:30 -07003545 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003546 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003547 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003548
3549 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003551 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003553 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003554 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 }
3556
3557 @Override
3558 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003559 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003561
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003562 if (mProviderReleased.compareAndSet(false, true)) {
3563 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 }
3566
3567 @Override
3568 protected void finalize() throws Throwable {
3569 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003570 if (mCloseGuard != null) {
3571 mCloseGuard.warnIfOpen();
3572 }
3573
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003574 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 } finally {
3576 super.finalize();
3577 }
3578 }
3579 }
3580
3581 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003582 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003583 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584
3585 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3586 super(pfd);
3587 mContentProvider = icp;
3588 }
3589
3590 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003591 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003592 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003593 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
3595 }
3596 }
3597
Dianne Hackborn231cc602009-04-27 17:10:36 -07003598 /** @hide */
3599 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003600
Dianne Hackborn231cc602009-04-27 17:10:36 -07003601 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003602 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003603 public static IContentService getContentService() {
3604 if (sContentService != null) {
3605 return sContentService;
3606 }
3607 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003608 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003609 return sContentService;
3610 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003611
Dianne Hackborn35654b62013-01-14 17:38:02 -08003612 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003613 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003614 public String getPackageName() {
3615 return mPackageName;
3616 }
3617
Philip P. Moltmann128b7032019-09-27 08:44:12 -07003618 /** @hide */
3619 public @Nullable String getFeatureId() {
3620 return mFeatureId;
3621 }
3622
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003623 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003624 private static volatile IContentService sContentService;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003625 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003627
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003628 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003629 final String mPackageName;
Philip P. Moltmann128b7032019-09-27 08:44:12 -07003630 final @Nullable String mFeatureId;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003631 final int mTargetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -07003632 final ContentInterface mWrapped;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003635
3636 /** @hide */
3637 public int resolveUserId(Uri uri) {
3638 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3639 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003640
3641 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003642 public int getUserId() {
3643 return mContext.getUserId();
3644 }
3645
Jeff Sharkey806bece2019-02-19 14:42:55 -07003646 /** {@hide} */
3647 @Deprecated
Daniel Nishic29d2b02016-06-30 12:20:41 -07003648 public Drawable getTypeDrawable(String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003649 return getTypeInfo(mimeType).getIcon().loadDrawable(mContext);
3650 }
3651
3652 /**
3653 * Return a detailed description of the given MIME type, including an icon
3654 * and label that describe the type.
3655 *
3656 * @param mimeType Valid, concrete MIME type.
3657 */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003658 public final @NonNull MimeTypeInfo getTypeInfo(@NonNull String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003659 Objects.requireNonNull(mimeType);
3660 return MimeIconUtils.getTypeInfo(mimeType);
3661 }
3662
3663 /**
3664 * Detailed description of a specific MIME type, including an icon and label
3665 * that describe the type.
3666 */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003667 public static final class MimeTypeInfo {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003668 private final Icon mIcon;
3669 private final CharSequence mLabel;
3670 private final CharSequence mContentDescription;
3671
3672 /** {@hide} */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003673 public MimeTypeInfo(@NonNull Icon icon, @NonNull CharSequence label,
Jeff Sharkey806bece2019-02-19 14:42:55 -07003674 @NonNull CharSequence contentDescription) {
3675 mIcon = Objects.requireNonNull(icon);
3676 mLabel = Objects.requireNonNull(label);
3677 mContentDescription = Objects.requireNonNull(contentDescription);
3678 }
3679
3680 /**
3681 * Return a visual representation of this MIME type. This can be styled
3682 * using {@link Icon#setTint(int)} to match surrounding UI.
3683 *
3684 * @see Icon#loadDrawable(Context)
3685 * @see android.widget.ImageView#setImageDrawable(Drawable)
3686 */
3687 public @NonNull Icon getIcon() {
3688 return mIcon;
3689 }
3690
3691 /**
3692 * Return a textual representation of this MIME type.
3693 *
3694 * @see android.widget.TextView#setText(CharSequence)
3695 */
3696 public @NonNull CharSequence getLabel() {
3697 return mLabel;
3698 }
3699
3700 /**
3701 * Return a content description for this MIME type.
3702 *
3703 * @see android.view.View#setContentDescription(CharSequence)
3704 */
3705 public @NonNull CharSequence getContentDescription() {
3706 return mContentDescription;
3707 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003708 }
Steve McKayea93fe72016-12-02 11:35:35 -08003709
3710 /**
3711 * @hide
3712 */
3713 public static @Nullable Bundle createSqlQueryBundle(
3714 @Nullable String selection,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07003715 @Nullable String[] selectionArgs) {
3716 return createSqlQueryBundle(selection, selectionArgs, null);
3717 }
3718
3719 /**
3720 * @hide
3721 */
3722 public static @Nullable Bundle createSqlQueryBundle(
3723 @Nullable String selection,
Steve McKayea93fe72016-12-02 11:35:35 -08003724 @Nullable String[] selectionArgs,
3725 @Nullable String sortOrder) {
3726
3727 if (selection == null && selectionArgs == null && sortOrder == null) {
3728 return null;
3729 }
3730
3731 Bundle queryArgs = new Bundle();
3732 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003733 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003734 }
3735 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003736 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003737 }
3738 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003739 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003740 }
3741 return queryArgs;
3742 }
Steve McKay29c3f682016-12-16 14:52:59 -08003743
3744 /**
3745 * Returns structured sort args formatted as an SQL sort clause.
3746 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003747 * NOTE: Collator clauses are suitable for use with non text fields. We might
3748 * choose to omit any collation clause since we don't know the underlying
3749 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3750 * appear to care much about the presence of collate clauses in queries
3751 * when ordering by numeric fields. For this reason we include collate
3752 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3753 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003754 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003755 * TODO: Would be nice to explicitly validate that colums referenced in
3756 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003757 *
3758 * @hide
3759 */
3760 public static String createSqlSortClause(Bundle queryArgs) {
3761 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3762 if (columns == null || columns.length == 0) {
3763 throw new IllegalArgumentException("Can't create sort clause without columns.");
3764 }
3765
3766 String query = TextUtils.join(", ", columns);
3767
Steve McKayd7ece9f2017-01-12 16:59:59 -08003768 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3769 // on their javadoc descriptions.
3770 int collation = queryArgs.getInt(
3771 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3772 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3773 query += " COLLATE NOCASE";
3774 }
3775
Steve McKay415f41b2017-02-01 13:38:25 -08003776 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3777 if (sortDir != Integer.MIN_VALUE) {
3778 switch (sortDir) {
3779 case QUERY_SORT_DIRECTION_ASCENDING:
3780 query += " ASC";
3781 break;
3782 case QUERY_SORT_DIRECTION_DESCENDING:
3783 query += " DESC";
3784 break;
3785 default:
3786 throw new IllegalArgumentException("Unsupported sort direction value."
3787 + " See ContentResolver documentation for details.");
3788 }
Steve McKay29c3f682016-12-16 14:52:59 -08003789 }
Steve McKay29c3f682016-12-16 14:52:59 -08003790 return query;
3791 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003792
3793 /**
3794 * Convenience method that efficiently loads a visual thumbnail for the
3795 * given {@link Uri}. Internally calls
3796 * {@link ContentProvider#openTypedAssetFile} on the remote provider, but
3797 * also defensively resizes any returned content to match the requested
3798 * target size.
3799 *
3800 * @param uri The item that should be visualized as a thumbnail.
3801 * @param size The target area on the screen where this thumbnail will be
3802 * shown. This is passed to the provider as {@link #EXTRA_SIZE}
3803 * to help it avoid downloading or generating heavy resources.
3804 * @param signal A signal to cancel the operation in progress.
3805 * @return Valid {@link Bitmap} which is a visual thumbnail.
3806 * @throws IOException If any trouble was encountered while generating or
3807 * loading the thumbnail, or if
3808 * {@link CancellationSignal#cancel()} was invoked.
3809 */
3810 public @NonNull Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size,
3811 @Nullable CancellationSignal signal) throws IOException {
Jeff Sharkey448c1ea2019-03-29 18:10:57 -06003812 return loadThumbnail(this, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003813 }
3814
3815 /** {@hide} */
Jeff Sharkeye770d222018-12-07 15:15:59 -07003816 public static Bitmap loadThumbnail(@NonNull ContentInterface content, @NonNull Uri uri,
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003817 @NonNull Size size, @Nullable CancellationSignal signal, int allocator)
3818 throws IOException {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003819 Objects.requireNonNull(content);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003820 Objects.requireNonNull(uri);
3821 Objects.requireNonNull(size);
3822
3823 // Convert to Point, since that's what the API is defined as
3824 final Bundle opts = new Bundle();
3825 opts.putParcelable(EXTRA_SIZE, Point.convert(size));
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003826 final Int32Ref orientation = new Int32Ref(0);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003827
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003828 Bitmap bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(() -> {
3829 final AssetFileDescriptor afd = content.openTypedAssetFile(uri, "image/*", opts,
3830 signal);
3831 final Bundle extras = afd.getExtras();
3832 orientation.value = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
3833 return afd;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003834 }), (ImageDecoder decoder, ImageInfo info, Source source) -> {
Tony Huangde421372019-10-25 18:15:24 +08003835 decoder.setAllocator(allocator);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003836
Tony Huangde421372019-10-25 18:15:24 +08003837 // One last-ditch check to see if we've been canceled.
3838 if (signal != null) signal.throwIfCanceled();
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003839
Tony Huangde421372019-10-25 18:15:24 +08003840 // We requested a rough thumbnail size, but the remote size may have
3841 // returned something giant, so defensively scale down as needed.
3842 final int widthSample = info.getSize().getWidth() / size.getWidth();
3843 final int heightSample = info.getSize().getHeight() / size.getHeight();
3844 final int sample = Math.max(widthSample, heightSample);
3845 if (sample > 1) {
3846 decoder.setTargetSampleSize(sample);
3847 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003848 });
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003849
3850 // Transform the bitmap if requested. We use a side-channel to
3851 // communicate the orientation, since EXIF thumbnails don't contain
3852 // the rotation flags of the original image.
3853 if (orientation.value != 0) {
3854 final int width = bitmap.getWidth();
3855 final int height = bitmap.getHeight();
3856
3857 final Matrix m = new Matrix();
3858 m.setRotate(orientation.value, width / 2, height / 2);
3859 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
3860 }
3861
3862 return bitmap;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003863 }
Makoto Onukiee93ad22018-10-18 16:24:13 -07003864
3865 /** {@hide} */
3866 public static void onDbCorruption(String tag, String message, Throwable stacktrace) {
3867 try {
3868 getContentService().onDbCorruption(tag, message, Log.getStackTraceString(stacktrace));
3869 } catch (RemoteException e) {
3870 e.rethrowFromSystemServer();
3871 }
3872 }
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003873
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07003874 /**
3875 * Decode a path generated by {@link #encodeToFile(Uri)} back into
3876 * the original {@link Uri}.
3877 * <p>
3878 * This is used to offer a way to intercept filesystem calls in
3879 * {@link ContentProvider} unaware code and redirect them to a
3880 * {@link ContentProvider} when they attempt to use {@code _DATA} columns
3881 * that are otherwise deprecated.
3882 *
3883 * @hide
3884 */
3885 @SystemApi
3886 public static @NonNull Uri decodeFromFile(@NonNull File file) {
3887 return translateDeprecatedDataPath(file.getAbsolutePath());
3888 }
3889
3890 /**
3891 * Encode a {@link Uri} into an opaque filesystem path which can then be
3892 * resurrected by {@link #decodeFromFile(File)}.
3893 * <p>
3894 * This is used to offer a way to intercept filesystem calls in
3895 * {@link ContentProvider} unaware code and redirect them to a
3896 * {@link ContentProvider} when they attempt to use {@code _DATA} columns
3897 * that are otherwise deprecated.
3898 *
3899 * @hide
3900 */
3901 @SystemApi
3902 public static @NonNull File encodeToFile(@NonNull Uri uri) {
3903 return new File(translateDeprecatedDataPath(uri));
3904 }
3905
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003906 /** {@hide} */
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07003907 public static @NonNull Uri translateDeprecatedDataPath(@NonNull String path) {
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003908 final String ssp = "//" + path.substring(DEPRECATE_DATA_PREFIX.length());
3909 return Uri.parse(new Uri.Builder().scheme(SCHEME_CONTENT)
3910 .encodedOpaquePart(ssp).build().toString());
3911 }
3912
3913 /** {@hide} */
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07003914 public static @NonNull String translateDeprecatedDataPath(@NonNull Uri uri) {
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003915 return DEPRECATE_DATA_PREFIX + uri.getEncodedSchemeSpecificPart().substring(2);
3916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917}