blob: b134c3796b406726953b4a7015d01ca8938765d4 [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;
Jeff Sharkey9ed18342020-03-08 12:42:27 -060026import android.annotation.SuppressLint;
Ivan Chiangfd3415c2018-12-07 18:22:50 +080027import android.annotation.SystemApi;
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -070028import android.annotation.TestApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070029import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080030import android.app.ActivityManager;
Jeff Sharkey66a017b2013-01-17 18:18:22 -080031import android.app.ActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070032import android.app.AppGlobals;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070033import android.app.UriGrantsManager;
Artur Satayeve23a0eb2019-12-10 17:47:52 +000034import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey9edef252019-05-20 14:00:17 -060035import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.PackageManager.NameNotFoundException;
37import android.content.res.AssetFileDescriptor;
38import android.content.res.Resources;
39import android.database.ContentObserver;
Jeff Brown825c5132011-10-12 16:11:30 -070040import android.database.CrossProcessCursorWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.database.IContentObserver;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060043import android.graphics.Bitmap;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060044import android.graphics.ImageDecoder;
45import android.graphics.ImageDecoder.ImageInfo;
46import android.graphics.ImageDecoder.Source;
Ivan Chiang6da7b0a2019-04-11 20:01:17 +080047import android.graphics.Matrix;
Jeff Sharkey5b836f22014-08-27 14:46:32 -070048import android.graphics.Point;
Daniel Nishic29d2b02016-06-30 12:20:41 -070049import android.graphics.drawable.Drawable;
Jeff Sharkey806bece2019-02-19 14:42:55 -070050import android.graphics.drawable.Icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.net.Uri;
52import android.os.Bundle;
Jeff Browna7771df2012-05-07 20:06:46 -070053import android.os.CancellationSignal;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070054import android.os.DeadObjectException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070055import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070056import android.os.ICancellationSignal;
57import android.os.OperationCanceledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.ParcelFileDescriptor;
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -080059import android.os.RemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.RemoteException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070061import android.os.ServiceManager;
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -080062import android.os.SystemClock;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070063import android.os.UserHandle;
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060064import android.os.storage.StorageManager;
Ivan Chiang6da7b0a2019-04-11 20:01:17 +080065import android.system.Int32Ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.text.TextUtils;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080067import android.util.EventLog;
Dianne Hackborn231cc602009-04-27 17:10:36 -070068import android.util.Log;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060069import android.util.Size;
Jeff Sharkey1307f422019-11-13 13:03:10 -070070import android.util.SparseArray;
Jeff Sharkey08da7a12013-08-11 20:53:18 -070071
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -080072import com.android.internal.annotations.GuardedBy;
Daniel Nishic29d2b02016-06-30 12:20:41 -070073import com.android.internal.util.MimeIconUtils;
Jeff Sharkey673db442015-06-11 19:30:57 -070074
Jeff Sharkey60cfad82016-01-05 17:30:57 -070075import dalvik.system.CloseGuard;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.File;
78import java.io.FileInputStream;
79import java.io.FileNotFoundException;
80import java.io.IOException;
81import java.io.InputStream;
82import java.io.OutputStream;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070083import java.lang.annotation.Retention;
84import java.lang.annotation.RetentionPolicy;
Gilles Debunne03f02922010-06-09 14:11:45 -070085import java.util.ArrayList;
Jeff Sharkey8b0cff72020-03-09 15:49:01 -060086import java.util.Collection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060088import java.util.Objects;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080089import java.util.Random;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070090import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092/**
93 * This class provides applications access to the content model.
Joe Fernandez558459f2011-10-13 16:47:36 -070094 *
95 * <div class="special reference">
96 * <h3>Developer Guides</h3>
97 * <p>For more information about using a ContentResolver with content providers, read the
98 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
99 * developer guide.</p>
Ng Zhi Anb837d0b2019-01-24 21:39:24 -0800100 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700102public abstract class ContentResolver implements ContentInterface {
Fred Quintanaac9385e2009-06-22 18:00:59 -0700103 /**
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600104 * Enables logic that supports deprecation of {@code _data} columns,
105 * typically by replacing values with fake paths that the OS then offers to
106 * redirect to {@link #openFileDescriptor(Uri, String)}, which developers
107 * should be using directly.
108 *
109 * @hide
110 */
Jeff Sharkey10ec9d82018-11-28 14:52:45 -0700111 public static final boolean DEPRECATE_DATA_COLUMNS = StorageManager.hasIsolatedStorage();
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600112
113 /**
114 * Special filesystem path prefix which indicates that a path should be
115 * treated as a {@code content://} {@link Uri} when
116 * {@link #DEPRECATE_DATA_COLUMNS} is enabled.
117 * <p>
118 * The remainder of the path after this prefix is a
119 * {@link Uri#getSchemeSpecificPart()} value, which includes authority, path
120 * segments, and query parameters.
121 *
122 * @hide
123 */
124 public static final String DEPRECATE_DATA_PREFIX = "/mnt/content/";
125
126 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700127 * @deprecated instead use
128 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
129 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700130 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -0700131 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700132
133 /**
134 * If this extra is set to true, the sync request will be scheduled
135 * at the front of the sync request queue and without any delay
136 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700138
Fred Quintanaac9385e2009-06-22 18:00:59 -0700139 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000140 * If this extra is set to true, the sync request will be scheduled
141 * only when the device is plugged in. This is equivalent to calling
142 * setRequiresCharging(true) on {@link SyncRequest}.
143 */
144 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
145
146 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700147 * @deprecated instead use
148 * {@link #SYNC_EXTRAS_MANUAL}
149 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700150 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800152
153 /**
154 * If this extra is set to true then the sync settings (like getSyncAutomatically())
155 * are ignored by the sync scheduler.
156 */
157 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
158
159 /**
160 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
161 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
162 * retries will still honor the backoff.
163 */
164 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
165
166 /**
167 * If this extra is set to true then the request will not be retried if it fails.
168 */
169 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
170
171 /**
172 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
173 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
174 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700175 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800176
Georgi Nikolovb3395572013-06-18 18:27:31 -0700177 /**
178 * Indicates that this sync is intended to only upload local changes to the server.
179 * For example, this will be set to true if the sync is initiated by a call to
180 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
181 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700183
184 /**
185 * Indicates that the sync adapter should proceed with the delete operations,
186 * even if it determines that there are too many.
187 * See {@link SyncResult#tooManyDeletions}
188 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700190
191 /**
192 * Indicates that the sync adapter should not proceed with the delete operations,
193 * if it determines that there are too many.
194 * See {@link SyncResult#tooManyDeletions}
195 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
197
Matthew Williamsfa774182013-06-18 15:44:11 -0700198 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
199 /** {@hide} User-specified flag for expected upload size. */
200 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
201
202 /** {@hide} User-specified flag for expected download size. */
203 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
204
205 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
206 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
207
208 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700209 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700210
Fred Quintana4a6679b2009-08-17 13:05:39 -0700211 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700212 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800213 *
214 * Only the system and the shell user can set it.
215 *
216 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
217 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700218 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800219
220 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700221 * Set by the SyncManager to request that the SyncAdapter initialize itself for
222 * the given account/authority pair. One required initialization step is to
223 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
224 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
225 * do a full sync, though it is allowed to do so.
226 */
227 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
228
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800229 /** @hide */
230 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
231 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public static final String SCHEME_CONTENT = "content";
234 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
235 public static final String SCHEME_FILE = "file";
236
237 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700238 * An extra {@link Point} describing the optimal size for a requested image
239 * resource, in pixels. If a provider has multiple sizes of the image, it
240 * should return the image closest to this size.
241 *
242 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
243 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
244 * CancellationSignal)
245 */
246 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
247
248 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800249 * An extra boolean describing whether a particular provider supports refresh
250 * or not. If a provider supports refresh, it should include this key in its
251 * returned Cursor as part of its query call.
252 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800253 */
254 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
255
256 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800257 * Key for an SQL style selection string that may be present in the query Bundle argument
258 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
259 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800260 *
261 * <p>Clients should never include user supplied values directly in the selection string,
262 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
263 * should use standard placeholder notation to represent values in a selection string,
264 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
265 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800266 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
267 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700268 *
269 * @see #QUERY_ARG_SORT_COLUMNS
270 * @see #QUERY_ARG_SORT_DIRECTION
271 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600272 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800273 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700274 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800275
276 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800277 * Key for SQL selection string arguments list.
278 *
279 * <p>Clients should never include user supplied values directly in the selection string,
280 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
281 * should use standard placeholder notation to represent values in a selection string,
282 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
283 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800284 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
285 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700286 *
287 * @see #QUERY_ARG_SORT_COLUMNS
288 * @see #QUERY_ARG_SORT_DIRECTION
289 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600290 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800291 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700292 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
293 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800294
295 /**
296 * Key for an SQL style sort string that may be present in the query Bundle argument
297 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
298 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800299 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800300 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
301 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700302 *
303 * @see #QUERY_ARG_SORT_COLUMNS
304 * @see #QUERY_ARG_SORT_DIRECTION
305 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600306 * @see #QUERY_ARG_SORT_LOCALE
Steve McKayea93fe72016-12-02 11:35:35 -0800307 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700308 public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800309
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700310 /**
311 * Key for an SQL style {@code GROUP BY} string that may be present in the
312 * query Bundle argument passed to
313 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
314 *
315 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
316 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
317 *
318 * @see #QUERY_ARG_GROUP_COLUMNS
319 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600320 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700321
322 /**
323 * Key for an SQL style {@code HAVING} string that may be present in the
324 * query Bundle argument passed to
325 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
326 *
327 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
328 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
329 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600330 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700331
332 /**
333 * Key for an SQL style {@code LIMIT} string that may be present in the
334 * query Bundle argument passed to
335 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}.
336 *
337 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
338 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
339 *
340 * @see #QUERY_ARG_LIMIT
341 * @see #QUERY_ARG_OFFSET
342 */
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600343 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
344
Steve McKay29c3f682016-12-16 14:52:59 -0800345 /**
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700346 * Specifies the list of columns (stored as a {@code String[]}) against
347 * which to sort results. When first column values are identical, records
348 * are then sorted based on second column values, and so on.
349 * <p>
350 * Columns present in this list must also be included in the projection
351 * supplied to
352 * {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
353 * <p>
354 * Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKayd74a3bd2017-04-24 12:07:53 -0700355 * <li>{@link ContentProvider} implementations: When preparing data in
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700356 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
357 * if sort columns is reflected in the returned Cursor, it is strongly
358 * recommended that {@link #QUERY_ARG_SORT_COLUMNS} then be included in the
359 * array of honored arguments reflected in {@link Cursor} extras
360 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
361 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in
362 * the arguments {@link Bundle}, the Content framework will attempt to
363 * synthesize an QUERY_ARG_SQL* argument using the corresponding
364 * QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800365 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700366 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800367
368 /**
369 * Specifies desired sort order. When unspecified a provider may provide a default
370 * sort direction, or choose to return unsorted results.
371 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800372 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
373 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700374 * <li>{@link ContentProvider} implementations: When preparing data in
375 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
376 * is reflected in the returned Cursor, it is strongly recommended that
377 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
378 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800379 *
380 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
381 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700382 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800383 *
384 * @see #QUERY_SORT_DIRECTION_ASCENDING
385 * @see #QUERY_SORT_DIRECTION_DESCENDING
386 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700387 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800388
389 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700390 * Allows client to specify a hint to the provider declaring which collation
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600391 * to use when sorting values.
392 * <p>
393 * Providers may support custom collators. When specifying a custom collator
Steve McKayd74a3bd2017-04-24 12:07:53 -0700394 * the value is determined by the Provider.
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600395 * <p>
396 * {@link ContentProvider} implementations: When preparing data in
397 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
398 * if sort collation is reflected in the returned Cursor, it is strongly
399 * recommended that {@link #QUERY_ARG_SORT_COLLATION} then be included in
400 * the array of honored arguments reflected in {@link Cursor} extras
401 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
402 * <p>
403 * When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
404 * arguments {@link Bundle}, the Content framework will attempt to
405 * synthesize a QUERY_ARG_SQL* argument using the corresponding
406 * QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800407 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700408 * @see java.text.Collator#PRIMARY
409 * @see java.text.Collator#SECONDARY
410 * @see java.text.Collator#TERTIARY
411 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800412 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700413 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800414
Steve McKay415f41b2017-02-01 13:38:25 -0800415 /**
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600416 * Allows client to specify a hint to the provider declaring which locale to
417 * use when sorting values.
418 * <p>
419 * The value is defined as a RFC 3066 locale ID followed by an optional
420 * keyword list, which is the locale format used to configure ICU through
421 * classes like {@link android.icu.util.ULocale}. This supports requesting
422 * advanced sorting options, such as {@code de@collation=phonebook},
423 * {@code zh@collation=pinyin}, etc.
424 * <p>
425 * {@link ContentProvider} implementations: When preparing data in
426 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
427 * if sort locale is reflected in the returned Cursor, it is strongly
428 * recommended that {@link #QUERY_ARG_SORT_LOCALE} then be included in the
429 * array of honored arguments reflected in {@link Cursor} extras
430 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
431 *
432 * @see java.util.Locale#Locale(String)
433 * @see android.icu.util.ULocale#ULocale(String)
434 */
435 public static final String QUERY_ARG_SORT_LOCALE = "android:query-arg-sort-locale";
436
437 /**
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700438 * Specifies the list of columns (stored as a {@code String[]}) against
439 * which to group results. When column values are identical, multiple
440 * records are collapsed together into a single record.
441 * <p>
442 * Columns present in this list must also be included in the projection
443 * supplied to
444 * {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
445 * <p>
446 * Apps targeting {@link android.os.Build.VERSION_CODES#R} or higher:
447 * <li>{@link ContentProvider} implementations: When preparing data in
448 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)},
449 * if group columns is reflected in the returned Cursor, it is strongly
450 * recommended that {@link #QUERY_ARG_SORT_COLUMNS} then be included in the
451 * array of honored arguments reflected in {@link Cursor} extras
452 * {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
453 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in
454 * the arguments {@link Bundle}, the Content framework will attempt to
455 * synthesize an QUERY_ARG_SQL* argument using the corresponding
456 * QUERY_ARG_SORT* values.
457 */
458 public static final String QUERY_ARG_GROUP_COLUMNS = "android:query-arg-group-columns";
459
460 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700461 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800462 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700463 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800464 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
465 * when any QUERY_ARG_SORT* value was honored during the preparation of the
466 * results {@link Cursor}.
467 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700468 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
469 *
470 * @see #QUERY_ARG_SORT_COLUMNS
471 * @see #QUERY_ARG_SORT_DIRECTION
472 * @see #QUERY_ARG_SORT_COLLATION
Jeff Sharkey85e90d02019-10-03 15:23:02 -0600473 * @see #QUERY_ARG_SORT_LOCALE
Jeff Sharkeyb500cb82019-11-14 13:59:24 -0700474 * @see #QUERY_ARG_GROUP_COLUMNS
Steve McKay415f41b2017-02-01 13:38:25 -0800475 */
476 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
477
Steve McKay29c3f682016-12-16 14:52:59 -0800478 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700479 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800480 QUERY_SORT_DIRECTION_ASCENDING,
481 QUERY_SORT_DIRECTION_DESCENDING
482 })
483 @Retention(RetentionPolicy.SOURCE)
484 public @interface SortDirection {}
485 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
486 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
487
488 /**
489 * @see {@link java.text.Collector} for details on respective collation strength.
490 * @hide
491 */
492 @IntDef(flag = false, value = {
493 java.text.Collator.PRIMARY,
494 java.text.Collator.SECONDARY,
495 java.text.Collator.TERTIARY,
496 java.text.Collator.IDENTICAL
497 })
498 @Retention(RetentionPolicy.SOURCE)
499 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800500
501 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700502 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800503 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700504 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800505
506 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700507 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800508 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700509 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800510
511 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700512 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
513 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800514 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800515 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700516 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800517 *
518 * <p>Providers having returned -1 in a previous query are recommended to
519 * send content change notification once (if) full recordset size becomes
520 * known.
521 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700522 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800523
524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * This is the Android platform's base MIME type for a content: URI
526 * containing a Cursor of a single item. Applications should use this
527 * as the base type along with their own sub-type of their content: URIs
528 * that represent a particular item. For example, hypothetical IMAP email
529 * client may have a URI
530 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
531 * message in the inbox, whose MIME type would be reported as
532 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
535 */
536 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 /**
539 * This is the Android platform's base MIME type for a content: URI
540 * containing a Cursor of zero or more items. Applications should use this
541 * as the base type along with their own sub-type of their content: URIs
542 * that represent a directory of items. For example, hypothetical IMAP email
543 * client may have a URI
544 * <code>content://com.company.provider.imap/inbox</code> for all of the
545 * messages in its inbox, whose MIME type would be reported as
546 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 * <p>Note how the base MIME type varies between this and
549 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
550 * one single item or multiple items in the data set, while the sub-type
551 * remains the same because in either case the data structure contained
552 * in the cursor is the same.
553 */
554 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700555
Matt Caseybd7bcf02014-02-05 15:51:39 -0800556 /**
557 * This is the Android platform's generic MIME type to match any MIME
558 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
559 * {@code SUB_TYPE} is the sub-type of the application-dependent
560 * content, e.g., "audio", "video", "playlist".
561 */
562 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
563
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600564 /** {@hide} */
565 @Deprecated
566 public static final String MIME_TYPE_DEFAULT = ClipDescription.MIMETYPE_UNKNOWN;
Jeff Sharkey91e3cd42018-08-27 18:03:33 -0600567
Fred Quintanaac9385e2009-06-22 18:00:59 -0700568 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100569 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700570 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
571 /** @hide */
572 public static final int SYNC_ERROR_AUTHENTICATION = 2;
573 /** @hide */
574 public static final int SYNC_ERROR_IO = 3;
575 /** @hide */
576 public static final int SYNC_ERROR_PARSE = 4;
577 /** @hide */
578 public static final int SYNC_ERROR_CONFLICT = 5;
579 /** @hide */
580 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
581 /** @hide */
582 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
583 /** @hide */
584 public static final int SYNC_ERROR_INTERNAL = 8;
585
Alon Albert57286f92012-10-09 14:21:38 -0700586 private static final String[] SYNC_ERROR_NAMES = new String[] {
587 "already-in-progress",
588 "authentication-error",
589 "io-error",
590 "parse-error",
591 "conflict",
592 "too-many-deletions",
593 "too-many-retries",
594 "internal-error",
595 };
596
597 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800598 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700599 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
600 return String.valueOf(error);
601 }
602 return SYNC_ERROR_NAMES[error - 1];
603 }
604
Alon Albert5c113fa2013-02-07 08:07:32 -0800605 /** @hide */
606 public static int syncErrorStringToInt(String error) {
607 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
608 if (SYNC_ERROR_NAMES[i].equals(error)) {
609 return i + 1;
610 }
611 }
612 if (error != null) {
613 try {
614 return Integer.parseInt(error);
615 } catch (NumberFormatException e) {
616 Log.d(TAG, "error parsing sync error: " + error);
617 }
618 }
619 return 0;
620 }
621
Fred Quintanaac9385e2009-06-22 18:00:59 -0700622 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700623 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700624 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
625 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100626 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700627 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
628 /** @hide */
629 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
630
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700631 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700632 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
633 NOTIFY_SYNC_TO_NETWORK,
Jeff Sharkeyd7032532020-03-05 09:56:41 -0700634 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS,
635 NOTIFY_INSERT,
636 NOTIFY_UPDATE,
637 NOTIFY_DELETE
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700638 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700639 @Retention(RetentionPolicy.SOURCE)
640 public @interface NotifyFlags {}
641
642 /**
643 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
644 * to the network.
645 */
646 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
647
648 /**
649 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
650 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
651 * using "notify for descendants." The purpose of this is to allow the provide to send
652 * a general notification of "something under X" changed that observers of that specific
653 * URI can receive, while also sending a specific URI under X. It would use this flag
654 * when sending the former, so that observers of "X and descendants" only see the latter.
655 */
656 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
657
Makoto Onuki75ad2492018-03-28 14:42:42 -0700658 /**
Jeff Sharkeyd7032532020-03-05 09:56:41 -0700659 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: typically set
660 * by a {@link ContentProvider} to indicate that this notification is the
661 * result of an {@link ContentProvider#insert} call.
662 * <p>
663 * Sending these detailed flags are optional, but providers are strongly
664 * recommended to send them.
665 */
666 public static final int NOTIFY_INSERT = 1 << 2;
667
668 /**
669 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: typically set
670 * by a {@link ContentProvider} to indicate that this notification is the
671 * result of an {@link ContentProvider#update} call.
672 * <p>
673 * Sending these detailed flags are optional, but providers are strongly
674 * recommended to send them.
675 */
676 public static final int NOTIFY_UPDATE = 1 << 3;
677
678 /**
679 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: typically set
680 * by a {@link ContentProvider} to indicate that this notification is the
681 * result of a {@link ContentProvider#delete} call.
682 * <p>
683 * Sending these detailed flags are optional, but providers are strongly
684 * recommended to send them.
685 */
686 public static final int NOTIFY_DELETE = 1 << 4;
687
688 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700689 * No exception, throttled by app standby normally.
690 * @hide
691 */
692 public static final int SYNC_EXEMPTION_NONE = 0;
693
694 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700695 * Exemption given to a sync request made by a foreground app (including
696 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700697 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700698 * At the schedule time, we promote the sync adapter app for a higher bucket:
699 * - If the device is not dozing (so the sync will start right away)
700 * promote to ACTIVE for 1 hour.
701 * - If the device is dozing (so the sync *won't* start right away),
702 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
703 * device comes out of doze.
704 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
705 * so it can schedule and start more syncs without getting throttled, even when the first
706 * operation was canceled and now we're retrying.
707 *
708 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700709 * @hide
710 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700711 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700712
713 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700714 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700715 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
716 * @hide
717 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700718 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700719
720 /** @hide */
721 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
722 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700723 SYNC_EXEMPTION_PROMOTE_BUCKET,
724 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700725 })
726 @Retention(RetentionPolicy.SOURCE)
727 public @interface SyncExemption {}
728
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800729 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800730 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700731 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800732 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800733 private final Random mRandom = new Random(); // guarded by itself
734
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800735 /** @hide */
736 public static final String REMOTE_CALLBACK_RESULT = "result";
737
Dmitri Plotnikovab87b272020-02-13 21:09:18 +0000738 /**
739 * How long we wait for an attached process to publish its content providers
740 * before we decide it must be hung.
741 * @hide
742 */
743 public static final int CONTENT_PROVIDER_PUBLISH_TIMEOUT_MILLIS = 10 * 1000;
744
745 /**
746 * How long we wait for an provider to be published. Should be longer than
747 * {@link #CONTENT_PROVIDER_PUBLISH_TIMEOUT_MILLIS}.
748 * @hide
749 */
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800750 public static final int CONTENT_PROVIDER_READY_TIMEOUT_MILLIS =
Dmitri Plotnikovab87b272020-02-13 21:09:18 +0000751 CONTENT_PROVIDER_PUBLISH_TIMEOUT_MILLIS + 10 * 1000;
752
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800753 // Timeout given a ContentProvider that has already been started and connected to.
754 private static final int CONTENT_PROVIDER_TIMEOUT_MILLIS = 3 * 1000;
755
Dmitri Plotnikovab87b272020-02-13 21:09:18 +0000756 // Should be >= {@link #CONTENT_PROVIDER_WAIT_TIMEOUT_MILLIS}, because that's how
757 // long ActivityManagerService is giving a content provider to get published if a new process
758 // needs to be started for that.
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800759 private static final int REMOTE_CONTENT_PROVIDER_TIMEOUT_MILLIS =
760 CONTENT_PROVIDER_READY_TIMEOUT_MILLIS + CONTENT_PROVIDER_TIMEOUT_MILLIS;
Dmitri Plotnikovab87b272020-02-13 21:09:18 +0000761
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700762 public ContentResolver(@Nullable Context context) {
763 this(context, null);
764 }
765
766 /** {@hide} */
767 public ContentResolver(@Nullable Context context, @Nullable ContentInterface wrapped) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800768 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700769 mPackageName = mContext.getOpPackageName();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800770 mAttributionTag = mContext.getAttributionTag();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000771 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700772 mWrapped = wrapped;
773 }
774
775 /** {@hide} */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700776 public static @NonNull ContentResolver wrap(@NonNull ContentInterface wrapped) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000777 Objects.requireNonNull(wrapped);
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700778
779 return new ContentResolver(null, wrapped) {
780 @Override
781 public void unstableProviderDied(IContentProvider icp) {
782 throw new UnsupportedOperationException();
783 }
784 @Override
785 public boolean releaseUnstableProvider(IContentProvider icp) {
786 throw new UnsupportedOperationException();
787 }
788 @Override
789 public boolean releaseProvider(IContentProvider icp) {
790 throw new UnsupportedOperationException();
791 }
792 @Override
793 protected IContentProvider acquireUnstableProvider(Context c, String name) {
794 throw new UnsupportedOperationException();
795 }
796 @Override
797 protected IContentProvider acquireProvider(Context c, String name) {
798 throw new UnsupportedOperationException();
799 }
800 };
801 }
802
803 /**
804 * Create a {@link ContentResolver} instance that redirects all its methods
805 * to the given {@link ContentProvider}.
806 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700807 public static @NonNull ContentResolver wrap(@NonNull ContentProvider wrapped) {
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700808 return wrap((ContentInterface) wrapped);
809 }
810
811 /**
812 * Create a {@link ContentResolver} instance that redirects all its methods
813 * to the given {@link ContentProviderClient}.
814 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700815 public static @NonNull ContentResolver wrap(@NonNull ContentProviderClient wrapped) {
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700816 return wrap((ContentInterface) wrapped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818
819 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100820 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700822
823 /**
824 * Providing a default implementation of this, to avoid having to change a
825 * lot of other things, but implementations of ContentResolver should
826 * implement it.
827 *
828 * @hide
829 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100830 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700831 protected IContentProvider acquireExistingProvider(Context c, String name) {
832 return acquireProvider(c, name);
833 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100836 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700838 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100839 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700840 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
841 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100842 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700843 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700844 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100845 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700846 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700848 /** @hide */
849 public void appNotRespondingViaProvider(IContentProvider icp) {
850 throw new UnsupportedOperationException("appNotRespondingViaProvider");
851 }
852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 /**
854 * Return the MIME type of the given content URL.
855 *
856 * @param url A Uri identifying content (either a list or specific type),
857 * using the content:// scheme.
858 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
859 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700860 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700861 public final @Nullable String getType(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000862 Objects.requireNonNull(url, "url");
Jeff Sharkey673db442015-06-11 19:30:57 -0700863
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700864 try {
865 if (mWrapped != null) return mWrapped.getType(url);
866 } catch (RemoteException e) {
867 return null;
868 }
869
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700870 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700871 IContentProvider provider = acquireExistingProvider(url);
872 if (provider != null) {
873 try {
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800874 final StringResultListener resultListener = new StringResultListener();
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800875 provider.getTypeAsync(url, new RemoteCallback(resultListener));
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800876 resultListener.waitForResult(CONTENT_PROVIDER_TIMEOUT_MILLIS);
877 return resultListener.result;
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700878 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600879 // Arbitrary and not worth documenting, as Activity
880 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700881 return null;
882 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100883 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700884 return null;
885 } finally {
886 releaseProvider(provider);
887 }
888 }
889
890 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 return null;
892 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 try {
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800895 final StringResultListener resultListener = new StringResultListener();
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800896 ActivityManager.getService().getProviderMimeTypeAsync(
897 ContentProvider.getUriWithoutUserId(url),
898 resolveUserId(url),
899 new RemoteCallback(resultListener));
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800900 resultListener.waitForResult(REMOTE_CONTENT_PROVIDER_TIMEOUT_MILLIS);
901 return resultListener.result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 } catch (RemoteException e) {
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800903 // We just failed to send a oneway request to the System Server. Nothing to do.
904 return null;
Ola Olsson145e6c42010-12-20 16:45:35 +0100905 } catch (java.lang.Exception e) {
906 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
907 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
909 }
910
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800911 private abstract static class ResultListener<T> implements RemoteCallback.OnResultListener {
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800912 @GuardedBy("this")
913 public boolean done;
914
915 @GuardedBy("this")
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800916 public T result;
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800917
918 @Override
919 public void onResult(Bundle result) {
920 synchronized (this) {
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800921 this.result = getResultFromBundle(result);
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800922 done = true;
923 notifyAll();
924 }
925 }
926
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800927 protected abstract T getResultFromBundle(Bundle result);
928
929 public void waitForResult(long timeout) {
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800930 synchronized (this) {
931 if (!done) {
932 try {
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800933 wait(timeout);
Dmitri Plotnikovd55a3872020-01-16 17:00:02 -0800934 } catch (InterruptedException e) {
935 // Ignore
936 }
937 }
938 }
939 }
940 }
941
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -0800942 private static class StringResultListener extends ResultListener<String> {
943 @Override
944 protected String getResultFromBundle(Bundle result) {
945 return result.getString(REMOTE_CALLBACK_RESULT);
946 }
947 }
948
949 private static class UriResultListener extends ResultListener<Uri> {
950 @Override
951 protected Uri getResultFromBundle(Bundle result) {
952 return result.getParcelable(REMOTE_CALLBACK_RESULT);
953 }
954 }
955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700957 * Query for the possible MIME types for the representations the given
958 * content URL can be returned when opened as as stream with
959 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
960 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700961 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700962 * data that they contain.
963 *
964 * @param url A Uri identifying content (either a list or specific type),
965 * using the content:// scheme.
966 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500967 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700968 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700969 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700970 * data streams that match the given mimeTypeFilter. If there are none,
971 * null is returned.
972 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700973 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700974 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +0000975 Objects.requireNonNull(url, "url");
976 Objects.requireNonNull(mimeTypeFilter, "mimeTypeFilter");
Jeff Sharkey673db442015-06-11 19:30:57 -0700977
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700978 try {
979 if (mWrapped != null) return mWrapped.getStreamTypes(url, mimeTypeFilter);
980 } catch (RemoteException e) {
981 return null;
982 }
983
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700984 IContentProvider provider = acquireProvider(url);
985 if (provider == null) {
986 return null;
987 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700988
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700989 try {
990 return provider.getStreamTypes(url, mimeTypeFilter);
991 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800992 // Arbitrary and not worth documenting, as Activity
993 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700994 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700995 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800996 releaseProvider(provider);
997 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700998 }
999
1000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001002 * <p>
1003 * For best performance, the caller should follow these guidelines:
1004 * <ul>
1005 * <li>Provide an explicit projection, to prevent
1006 * reading data from storage that aren't going to be used.</li>
1007 * <li>Use question mark parameter markers such as 'phone=?' instead of
1008 * explicit values in the {@code selection} parameter, so that queries
1009 * that differ only by those values will be recognized as the same
1010 * for caching purposes.</li>
1011 * </ul>
1012 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 *
1014 * @param uri The URI, using the content:// scheme, for the content to
1015 * retrieve.
1016 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001017 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * @param selection A filter declaring which rows to return, formatted as an
1019 * SQL WHERE clause (excluding the WHERE itself). Passing null will
1020 * return all rows for the given URI.
1021 * @param selectionArgs You may include ?s in selection, which will be
1022 * replaced by the values from selectionArgs, in the order that they
1023 * appear in the selection. The values will be bound as Strings.
1024 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
1025 * clause (excluding the ORDER BY itself). Passing null will use the
1026 * default sort order, which may be unordered.
Varun Shahc4fdfa32019-05-10 14:10:08 -07001027 * @return A Cursor object, which is positioned before the first entry. May return
1028 * <code>null</code> if the underlying content provider returns <code>null</code>,
1029 * or if it crashes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 * @see Cursor
1031 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001032 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
1033 @Nullable String[] projection, @Nullable String selection,
1034 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -08001035 return query(uri, projection, selection, selectionArgs, sortOrder, null);
1036 }
1037
1038 /**
Jeff Brownc64ff372013-10-09 18:50:56 -07001039 * Query the given URI, returning a {@link Cursor} over the result set
1040 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001041 * <p>
1042 * For best performance, the caller should follow these guidelines:
1043 * <ul>
1044 * <li>Provide an explicit projection, to prevent
1045 * reading data from storage that aren't going to be used.</li>
1046 * <li>Use question mark parameter markers such as 'phone=?' instead of
1047 * explicit values in the {@code selection} parameter, so that queries
1048 * that differ only by those values will be recognized as the same
1049 * for caching purposes.</li>
1050 * </ul>
1051 * </p>
1052 *
1053 * @param uri The URI, using the content:// scheme, for the content to
1054 * retrieve.
1055 * @param projection A list of which columns to return. Passing null will
1056 * return all columns, which is inefficient.
1057 * @param selection A filter declaring which rows to return, formatted as an
1058 * SQL WHERE clause (excluding the WHERE itself). Passing null will
1059 * return all rows for the given URI.
1060 * @param selectionArgs You may include ?s in selection, which will be
1061 * replaced by the values from selectionArgs, in the order that they
1062 * appear in the selection. The values will be bound as Strings.
1063 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
1064 * clause (excluding the ORDER BY itself). Passing null will use the
1065 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -08001066 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001067 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
1068 * when the query is executed.
Varun Shahc4fdfa32019-05-10 14:10:08 -07001069 * @return A Cursor object, which is positioned before the first entry. May return
1070 * <code>null</code> if the underlying content provider returns <code>null</code>,
1071 * or if it crashes.
Jeff Brown75ea64f2012-01-25 19:37:13 -08001072 * @see Cursor
1073 */
Steve McKayea93fe72016-12-02 11:35:35 -08001074 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -07001075 @Nullable String[] projection, @Nullable String selection,
1076 @Nullable String[] selectionArgs, @Nullable String sortOrder,
1077 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -08001078 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
1079 return query(uri, projection, queryArgs, cancellationSignal);
1080 }
1081
1082 /**
1083 * Query the given URI, returning a {@link Cursor} over the result set
1084 * with support for cancellation.
1085 *
1086 * <p>For best performance, the caller should follow these guidelines:
1087 *
1088 * <li>Provide an explicit projection, to prevent reading data from storage
1089 * that aren't going to be used.
1090 *
Steve McKay415f41b2017-02-01 13:38:25 -08001091 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
1092 * the preparation of the result set by including the respective argument keys
1093 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
1094 * for details.
1095 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07001096 * @see #QUERY_ARG_SORT_COLUMNS
1097 * @see #QUERY_ARG_SORT_DIRECTION
1098 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -08001099 *
Steve McKayea93fe72016-12-02 11:35:35 -08001100 * @param uri The URI, using the content:// scheme, for the content to
1101 * retrieve.
1102 * @param projection A list of which columns to return. Passing null will
1103 * return all columns, which is inefficient.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07001104 * @param queryArgs A Bundle containing additional information necessary for
1105 * the operation. Arguments may include SQL style arguments, such
1106 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
1107 * the documentation for each individual provider will indicate
1108 * which arguments they support.
Steve McKayea93fe72016-12-02 11:35:35 -08001109 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
1110 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
1111 * when the query is executed.
Varun Shahc4fdfa32019-05-10 14:10:08 -07001112 * @return A Cursor object, which is positioned before the first entry. May return
1113 * <code>null</code> if the underlying content provider returns <code>null</code>,
1114 * or if it crashes.
Steve McKayea93fe72016-12-02 11:35:35 -08001115 * @see Cursor
1116 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001117 @Override
Steve McKayea93fe72016-12-02 11:35:35 -08001118 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
1119 @Nullable String[] projection, @Nullable Bundle queryArgs,
1120 @Nullable CancellationSignal cancellationSignal) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001121 Objects.requireNonNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001122
1123 try {
1124 if (mWrapped != null) {
1125 return mWrapped.query(uri, projection, queryArgs, cancellationSignal);
1126 }
1127 } catch (RemoteException e) {
1128 return null;
1129 }
1130
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001131 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1132 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 return null;
1134 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001135 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -08001136 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001138 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -08001139
Jeff Brown4c1241d2012-02-02 17:05:00 -08001140 ICancellationSignal remoteCancellationSignal = null;
1141 if (cancellationSignal != null) {
1142 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001143 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -08001144 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -08001145 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001146 try {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001147 qCursor = unstableProvider.query(mPackageName, mAttributionTag, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -08001148 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001149 } catch (DeadObjectException e) {
1150 // The remote process has died... but we only hold an unstable
1151 // reference though, so we might recover!!! Let's try!!!!
1152 // This is exciting!!1!!1!!!!1
1153 unstableProviderDied(unstableProvider);
1154 stableProvider = acquireProvider(uri);
1155 if (stableProvider == null) {
1156 return null;
1157 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001158 qCursor = stableProvider.query(mPackageName, mAttributionTag, uri, projection,
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001159 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001160 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001161 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 return null;
1163 }
Jeff Brownc21b5a02013-01-07 17:15:12 -08001164
1165 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -07001166 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001167 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -08001168 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -08001169
1170 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001171 final IContentProvider provider = (stableProvider != null) ? stableProvider
1172 : acquireProvider(uri);
1173 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001174 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -08001175 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001176 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001178 // Arbitrary and not worth documenting, as Activity
1179 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001181 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -08001182 if (qCursor != null) {
1183 qCursor.close();
1184 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001185 if (cancellationSignal != null) {
1186 cancellationSignal.setRemote(null);
1187 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001188 if (unstableProvider != null) {
1189 releaseUnstableProvider(unstableProvider);
1190 }
1191 if (stableProvider != null) {
1192 releaseProvider(stableProvider);
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 }
1196
Jeff Sharkey63784022019-04-17 10:39:42 -06001197 /** {@hide} */
1198 public final @NonNull Uri canonicalizeOrElse(@NonNull Uri uri) {
1199 final Uri res = canonicalize(uri);
1200 return (res != null) ? res : uri;
1201 }
1202
Fred Quintana89437372009-05-15 15:10:40 -07001203 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001204 * Transform the given <var>url</var> to a canonical representation of
1205 * its referenced resource, which can be used across devices, persisted,
1206 * backed up and restored, etc. The returned Uri is still a fully capable
1207 * Uri for use with its content provider, allowing you to do all of the
1208 * same content provider operations as with the original Uri --
1209 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
1210 * only difference in behavior between the original and new Uris is that
1211 * the content provider may need to do some additional work at each call
1212 * using it to resolve it to the correct resource, especially if the
1213 * canonical Uri has been moved to a different environment.
1214 *
1215 * <p>If you are moving a canonical Uri between environments, you should
1216 * perform another call to {@link #canonicalize} with that original Uri to
1217 * re-canonicalize it for the current environment. Alternatively, you may
1218 * want to use {@link #uncanonicalize} to transform it to a non-canonical
1219 * Uri that works only in the current environment but potentially more
1220 * efficiently than the canonical representation.</p>
1221 *
1222 * @param url The {@link Uri} that is to be transformed to a canonical
1223 * representation. Like all resolver calls, the input can be either
1224 * a non-canonical or canonical Uri.
1225 *
1226 * @return Returns the official canonical representation of <var>url</var>,
1227 * or null if the content provider does not support a canonical representation
1228 * of the given Uri. Many providers may not support canonicalization of some
1229 * or all of their Uris.
1230 *
1231 * @see #uncanonicalize
1232 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001233 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001234 public final @Nullable Uri canonicalize(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001235 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001236
1237 try {
1238 if (mWrapped != null) return mWrapped.canonicalize(url);
1239 } catch (RemoteException e) {
1240 return null;
1241 }
1242
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001243 IContentProvider provider = acquireProvider(url);
1244 if (provider == null) {
1245 return null;
1246 }
1247
1248 try {
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -08001249 final UriResultListener resultListener = new UriResultListener();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001250 provider.canonicalizeAsync(mPackageName, mAttributionTag, url,
Dmitri Plotnikov7a223fb2020-02-14 17:04:13 -08001251 new RemoteCallback(resultListener));
1252 resultListener.waitForResult(CONTENT_PROVIDER_TIMEOUT_MILLIS);
1253 return resultListener.result;
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001254 } catch (RemoteException e) {
1255 // Arbitrary and not worth documenting, as Activity
1256 // Manager will kill this process shortly anyway.
1257 return null;
1258 } finally {
1259 releaseProvider(provider);
1260 }
1261 }
1262
1263 /**
1264 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
1265 * it to its local non-canonical form. This can be useful in some cases where
1266 * you know that you will only be using the Uri in the current environment and
1267 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001268 * provider or want to verify that the referenced data exists at all in the
1269 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001270 *
1271 * @param url The canonical {@link Uri} that is to be convered back to its
1272 * non-canonical form.
1273 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001274 * @return Returns the non-canonical representation of <var>url</var>. This will
1275 * return null if data identified by the canonical Uri can not be found in
1276 * the current environment; callers must always check for null and deal with
1277 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001278 *
1279 * @see #canonicalize
1280 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001281 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001282 public final @Nullable Uri uncanonicalize(@NonNull Uri url) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001283 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001284
1285 try {
1286 if (mWrapped != null) return mWrapped.uncanonicalize(url);
1287 } catch (RemoteException e) {
1288 return null;
1289 }
1290
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001291 IContentProvider provider = acquireProvider(url);
1292 if (provider == null) {
1293 return null;
1294 }
1295
1296 try {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001297 return provider.uncanonicalize(mPackageName, mAttributionTag, url);
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001298 } catch (RemoteException e) {
1299 // Arbitrary and not worth documenting, as Activity
1300 // Manager will kill this process shortly anyway.
1301 return null;
1302 } finally {
1303 releaseProvider(provider);
1304 }
1305 }
1306
1307 /**
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001308 * This allows clients to request an explicit refresh of content identified
1309 * by {@code uri}.
Ben Lin1cf454f2016-11-10 13:50:54 -08001310 * <p>
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001311 * Client code should only invoke this method when there is a strong
1312 * indication (such as a user initiated pull to refresh gesture) that the
1313 * content is stale.
Ben Lin1cf454f2016-11-10 13:50:54 -08001314 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -08001315 *
Ben Lin2b64a882016-11-11 15:24:58 -08001316 * @param url The Uri identifying the data to refresh.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001317 * @param extras Additional options from the client. The definitions of
1318 * these are specific to the content provider being called.
1319 * @param cancellationSignal A signal to cancel the operation in progress,
1320 * or {@code null} if none. For example, if you called refresh on
1321 * a particular uri, you should call
1322 * {@link CancellationSignal#throwIfCanceled()} to check whether
1323 * the client has canceled the refresh request.
Ben Lin1cf454f2016-11-10 13:50:54 -08001324 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -08001325 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001326 @Override
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001327 public final boolean refresh(@NonNull Uri url, @Nullable Bundle extras,
Ben Lin1cf454f2016-11-10 13:50:54 -08001328 @Nullable CancellationSignal cancellationSignal) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001329 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001330
1331 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07001332 if (mWrapped != null) return mWrapped.refresh(url, extras, cancellationSignal);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001333 } catch (RemoteException e) {
1334 return false;
1335 }
1336
Ben Lin1cf454f2016-11-10 13:50:54 -08001337 IContentProvider provider = acquireProvider(url);
1338 if (provider == null) {
1339 return false;
1340 }
1341
1342 try {
1343 ICancellationSignal remoteCancellationSignal = null;
1344 if (cancellationSignal != null) {
1345 cancellationSignal.throwIfCanceled();
1346 remoteCancellationSignal = provider.createCancellationSignal();
1347 cancellationSignal.setRemote(remoteCancellationSignal);
1348 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001349 return provider.refresh(mPackageName, mAttributionTag, url, extras,
Philip P. Moltmann128b7032019-09-27 08:44:12 -07001350 remoteCancellationSignal);
Ben Lin1cf454f2016-11-10 13:50:54 -08001351 } catch (RemoteException e) {
1352 // Arbitrary and not worth documenting, as Activity
1353 // Manager will kill this process shortly anyway.
1354 return false;
1355 } finally {
1356 releaseProvider(provider);
1357 }
1358 }
1359
Jeff Sharkey9edef252019-05-20 14:00:17 -06001360 /** {@hide} */
1361 @Override
1362 public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001363 Objects.requireNonNull(uri, "uri");
Jeff Sharkey9edef252019-05-20 14:00:17 -06001364
1365 try {
1366 if (mWrapped != null) return mWrapped.checkUriPermission(uri, uid, modeFlags);
1367 } catch (RemoteException e) {
1368 return PackageManager.PERMISSION_DENIED;
1369 }
1370
1371 try (ContentProviderClient client = acquireUnstableContentProviderClient(uri)) {
1372 return client.checkUriPermission(uri, uid, modeFlags);
1373 } catch (RemoteException e) {
1374 return PackageManager.PERMISSION_DENIED;
1375 }
1376 }
1377
Ben Lin1cf454f2016-11-10 13:50:54 -08001378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 * Open a stream on to the content associated with a content URI. If there
1380 * is no data associated with the URI, FileNotFoundException is thrown.
1381 *
1382 * <h5>Accepts the following URI schemes:</h5>
1383 * <ul>
1384 * <li>content ({@link #SCHEME_CONTENT})</li>
1385 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1386 * <li>file ({@link #SCHEME_FILE})</li>
1387 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001388 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1390 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001391 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 * @param uri The desired URI.
1393 * @return InputStream
1394 * @throws FileNotFoundException if the provided URI could not be opened.
1395 * @see #openAssetFileDescriptor(Uri, String)
1396 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001397 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001399 Objects.requireNonNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 String scheme = uri.getScheme();
1401 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1402 // Note: left here to avoid breaking compatibility. May be removed
1403 // with sufficient testing.
1404 OpenResourceIdResult r = getResourceId(uri);
1405 try {
1406 InputStream stream = r.r.openRawResource(r.id);
1407 return stream;
1408 } catch (Resources.NotFoundException ex) {
1409 throw new FileNotFoundException("Resource does not exist: " + uri);
1410 }
1411 } else if (SCHEME_FILE.equals(scheme)) {
1412 // Note: left here to avoid breaking compatibility. May be removed
1413 // with sufficient testing.
1414 return new FileInputStream(uri.getPath());
1415 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001416 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 try {
1418 return fd != null ? fd.createInputStream() : null;
1419 } catch (IOException e) {
1420 throw new FileNotFoundException("Unable to create stream");
1421 }
1422 }
1423 }
1424
1425 /**
1426 * Synonym for {@link #openOutputStream(Uri, String)
1427 * openOutputStream(uri, "w")}.
1428 * @throws FileNotFoundException if the provided URI could not be opened.
1429 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001430 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 throws FileNotFoundException {
1432 return openOutputStream(uri, "w");
1433 }
1434
1435 /**
1436 * Open a stream on to the content associated with a content URI. If there
1437 * is no data associated with the URI, FileNotFoundException is thrown.
1438 *
1439 * <h5>Accepts the following URI schemes:</h5>
1440 * <ul>
1441 * <li>content ({@link #SCHEME_CONTENT})</li>
1442 * <li>file ({@link #SCHEME_FILE})</li>
1443 * </ul>
1444 *
1445 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1446 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 * @param uri The desired URI.
1449 * @param mode May be "w", "wa", "rw", or "rwt".
1450 * @return OutputStream
1451 * @throws FileNotFoundException if the provided URI could not be opened.
1452 * @see #openAssetFileDescriptor(Uri, String)
1453 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001454 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001456 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 try {
1458 return fd != null ? fd.createOutputStream() : null;
1459 } catch (IOException e) {
1460 throw new FileNotFoundException("Unable to create stream");
1461 }
1462 }
1463
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001464 @Override
1465 public final @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
1466 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001467 try {
1468 if (mWrapped != null) return mWrapped.openFile(uri, mode, signal);
1469 } catch (RemoteException e) {
1470 return null;
1471 }
1472
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001473 return openFileDescriptor(uri, mode, signal);
1474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001477 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1479 * underlying {@link ContentProvider#openFile}
1480 * ContentProvider.openFile()} method, so will <em>not</em> work with
1481 * providers that return sub-sections of files. If at all possible,
1482 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1483 * will receive a FileNotFoundException exception if the provider returns a
1484 * sub-section of a file.
1485 *
1486 * <h5>Accepts the following URI schemes:</h5>
1487 * <ul>
1488 * <li>content ({@link #SCHEME_CONTENT})</li>
1489 * <li>file ({@link #SCHEME_FILE})</li>
1490 * </ul>
1491 *
1492 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1493 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001494 * <p>
1495 * If opening with the exclusive "r" or "w" modes, the returned
1496 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1497 * of data. Opening with the "rw" mode implies a file on disk that supports
1498 * seeking. If possible, always use an exclusive mode to give the underlying
1499 * {@link ContentProvider} the most flexibility.
1500 * <p>
1501 * If you are writing a file, and need to communicate an error to the
1502 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 * @param uri The desired URI to open.
1505 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1506 * ContentProvider.openFile}.
1507 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1508 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001509 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * file exists under the URI or the mode is invalid.
1511 * @see #openAssetFileDescriptor(Uri, String)
1512 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001513 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1514 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001515 return openFileDescriptor(uri, mode, null);
1516 }
1517
1518 /**
1519 * Open a raw file descriptor to access data under a URI. This
1520 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1521 * underlying {@link ContentProvider#openFile}
1522 * ContentProvider.openFile()} method, so will <em>not</em> work with
1523 * providers that return sub-sections of files. If at all possible,
1524 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1525 * will receive a FileNotFoundException exception if the provider returns a
1526 * sub-section of a file.
1527 *
1528 * <h5>Accepts the following URI schemes:</h5>
1529 * <ul>
1530 * <li>content ({@link #SCHEME_CONTENT})</li>
1531 * <li>file ({@link #SCHEME_FILE})</li>
1532 * </ul>
1533 *
1534 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1535 * on these schemes.
1536 * <p>
1537 * If opening with the exclusive "r" or "w" modes, the returned
1538 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1539 * of data. Opening with the "rw" mode implies a file on disk that supports
1540 * seeking. If possible, always use an exclusive mode to give the underlying
1541 * {@link ContentProvider} the most flexibility.
1542 * <p>
1543 * If you are writing a file, and need to communicate an error to the
1544 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1545 *
1546 * @param uri The desired URI to open.
1547 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1548 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001549 * @param cancellationSignal A signal to cancel the operation in progress,
1550 * or null if none. If the operation is canceled, then
1551 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001552 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1553 * own this descriptor and are responsible for closing it when done.
1554 * @throws FileNotFoundException Throws FileNotFoundException if no
1555 * file exists under the URI or the mode is invalid.
1556 * @see #openAssetFileDescriptor(Uri, String)
1557 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001558 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1559 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1560 throws FileNotFoundException {
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001561 try {
1562 if (mWrapped != null) return mWrapped.openFile(uri, mode, cancellationSignal);
1563 } catch (RemoteException e) {
1564 return null;
1565 }
1566
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001567 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 if (afd == null) {
1569 return null;
1570 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 if (afd.getDeclaredLength() < 0) {
1573 // This is a full file!
1574 return afd.getParcelFileDescriptor();
1575 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 // Client can't handle a sub-section of a file, so close what
1578 // we got and bail with an exception.
1579 try {
1580 afd.close();
1581 } catch (IOException e) {
1582 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 throw new FileNotFoundException("Not a whole file");
1585 }
1586
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001587 @Override
1588 public final @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
1589 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001590 try {
1591 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, signal);
1592 } catch (RemoteException e) {
1593 return null;
1594 }
1595
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001596 return openAssetFileDescriptor(uri, mode, signal);
1597 }
1598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001600 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001602 * 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 -08001603 *
1604 * <h5>Accepts the following URI schemes:</h5>
1605 * <ul>
1606 * <li>content ({@link #SCHEME_CONTENT})</li>
1607 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1608 * <li>file ({@link #SCHEME_FILE})</li>
1609 * </ul>
1610 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1611 * <p>
1612 * A Uri object can be used to reference a resource in an APK file. The
1613 * Uri should be one of the following formats:
1614 * <ul>
1615 * <li><code>android.resource://package_name/id_number</code><br/>
1616 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1617 * For example <code>com.example.myapp</code><br/>
1618 * <code>id_number</code> is the int form of the ID.<br/>
1619 * The easiest way to construct this form is
1620 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1621 * </li>
1622 * <li><code>android.resource://package_name/type/name</code><br/>
1623 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1624 * For example <code>com.example.myapp</code><br/>
1625 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1626 * or <code>drawable</code>.
1627 * <code>name</code> is the string form of the resource name. That is, whatever the file
1628 * name was in your res directory, without the type extension.
1629 * The easiest way to construct this form is
1630 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1631 * </li>
1632 * </ul>
1633 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001634 * <p>Note that if this function is called for read-only input (mode is "r")
1635 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001636 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001637 * from any built-in data conversion that a provider implements.
1638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 * @param uri The desired URI to open.
1640 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1641 * ContentProvider.openAssetFile}.
1642 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1643 * own this descriptor and are responsible for closing it when done.
1644 * @throws FileNotFoundException Throws FileNotFoundException of no
1645 * file exists under the URI or the mode is invalid.
1646 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001647 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1648 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001649 return openAssetFileDescriptor(uri, mode, null);
1650 }
1651
1652 /**
1653 * Open a raw file descriptor to access data under a URI. This
1654 * interacts with the underlying {@link ContentProvider#openAssetFile}
1655 * method of the provider associated with the given URI, to retrieve any file stored there.
1656 *
1657 * <h5>Accepts the following URI schemes:</h5>
1658 * <ul>
1659 * <li>content ({@link #SCHEME_CONTENT})</li>
1660 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1661 * <li>file ({@link #SCHEME_FILE})</li>
1662 * </ul>
1663 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1664 * <p>
1665 * A Uri object can be used to reference a resource in an APK file. The
1666 * Uri should be one of the following formats:
1667 * <ul>
1668 * <li><code>android.resource://package_name/id_number</code><br/>
1669 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1670 * For example <code>com.example.myapp</code><br/>
1671 * <code>id_number</code> is the int form of the ID.<br/>
1672 * The easiest way to construct this form is
1673 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1674 * </li>
1675 * <li><code>android.resource://package_name/type/name</code><br/>
1676 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1677 * For example <code>com.example.myapp</code><br/>
1678 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1679 * or <code>drawable</code>.
1680 * <code>name</code> is the string form of the resource name. That is, whatever the file
1681 * name was in your res directory, without the type extension.
1682 * The easiest way to construct this form is
1683 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1684 * </li>
1685 * </ul>
1686 *
1687 * <p>Note that if this function is called for read-only input (mode is "r")
1688 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001689 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001690 * from any built-in data conversion that a provider implements.
1691 *
1692 * @param uri The desired URI to open.
1693 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1694 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001695 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001696 * none. If the operation is canceled, then
1697 * {@link OperationCanceledException} will be thrown.
1698 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1699 * own this descriptor and are responsible for closing it when done.
1700 * @throws FileNotFoundException Throws FileNotFoundException of no
1701 * file exists under the URI or the mode is invalid.
1702 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001703 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1704 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1705 throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001706 Objects.requireNonNull(uri, "uri");
1707 Objects.requireNonNull(mode, "mode");
Jeff Sharkey673db442015-06-11 19:30:57 -07001708
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001709 try {
1710 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, cancellationSignal);
1711 } catch (RemoteException e) {
1712 return null;
1713 }
1714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 String scheme = uri.getScheme();
1716 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1717 if (!"r".equals(mode)) {
1718 throw new FileNotFoundException("Can't write resources: " + uri);
1719 }
1720 OpenResourceIdResult r = getResourceId(uri);
1721 try {
1722 return r.r.openRawResourceFd(r.id);
1723 } catch (Resources.NotFoundException ex) {
1724 throw new FileNotFoundException("Resource does not exist: " + uri);
1725 }
1726 } else if (SCHEME_FILE.equals(scheme)) {
1727 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001728 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 return new AssetFileDescriptor(pfd, 0, -1);
1730 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001731 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001732 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001733 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001734 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1735 if (unstableProvider == null) {
1736 throw new FileNotFoundException("No content provider: " + uri);
1737 }
1738 IContentProvider stableProvider = null;
1739 AssetFileDescriptor fd = null;
1740
1741 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001742 ICancellationSignal remoteCancellationSignal = null;
1743 if (cancellationSignal != null) {
1744 cancellationSignal.throwIfCanceled();
1745 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1746 cancellationSignal.setRemote(remoteCancellationSignal);
1747 }
1748
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001749 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001750 fd = unstableProvider.openAssetFile(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001751 mPackageName, mAttributionTag, uri, mode,
1752 remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001753 if (fd == null) {
1754 // The provider will be released by the finally{} clause
1755 return null;
1756 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001757 } catch (DeadObjectException e) {
1758 // The remote process has died... but we only hold an unstable
1759 // reference though, so we might recover!!! Let's try!!!!
1760 // This is exciting!!1!!1!!!!1
1761 unstableProviderDied(unstableProvider);
1762 stableProvider = acquireProvider(uri);
1763 if (stableProvider == null) {
1764 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001765 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001766 fd = stableProvider.openAssetFile(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001767 mPackageName, mAttributionTag, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001768 if (fd == null) {
1769 // The provider will be released by the finally{} clause
1770 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001771 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001772 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001773
1774 if (stableProvider == null) {
1775 stableProvider = acquireProvider(uri);
1776 }
1777 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001778 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001779 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1780 fd.getParcelFileDescriptor(), stableProvider);
1781
1782 // Success! Don't release the provider when exiting, let
1783 // ParcelFileDescriptorInner do that when it is closed.
1784 stableProvider = null;
1785
1786 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1787 fd.getDeclaredLength());
1788
1789 } catch (RemoteException e) {
1790 // Whatever, whatever, we'll go away.
1791 throw new FileNotFoundException(
1792 "Failed opening content provider: " + uri);
1793 } catch (FileNotFoundException e) {
1794 throw e;
1795 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001796 if (cancellationSignal != null) {
1797 cancellationSignal.setRemote(null);
1798 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001799 if (stableProvider != null) {
1800 releaseProvider(stableProvider);
1801 }
1802 if (unstableProvider != null) {
1803 releaseUnstableProvider(unstableProvider);
1804 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001805 }
1806 }
1807 }
1808 }
1809
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001810 @Override
1811 public final @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
1812 @NonNull String mimeTypeFilter, @Nullable Bundle opts,
1813 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001814 try {
1815 if (mWrapped != null) {
1816 return mWrapped.openTypedAssetFile(uri, mimeTypeFilter, opts, signal);
1817 }
1818 } catch (RemoteException e) {
1819 return null;
1820 }
1821
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001822 return openTypedAssetFileDescriptor(uri, mimeTypeFilter, opts, signal);
1823 }
1824
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001825 /**
1826 * Open a raw file descriptor to access (potentially type transformed)
1827 * data from a "content:" URI. This interacts with the underlying
1828 * {@link ContentProvider#openTypedAssetFile} method of the provider
1829 * associated with the given URI, to retrieve retrieve any appropriate
1830 * data stream for the data stored there.
1831 *
1832 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1833 * with "content:" URIs, because content providers are the only facility
1834 * with an associated MIME type to ensure that the returned data stream
1835 * is of the desired type.
1836 *
1837 * <p>All text/* streams are encoded in UTF-8.
1838 *
1839 * @param uri The desired URI to open.
1840 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001841 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001842 * select a type, though there is no way for you to determine what type
1843 * it is returning.
1844 * @param opts Additional provider-dependent options.
1845 * @return Returns a new ParcelFileDescriptor from which you can read the
1846 * data stream from the provider. Note that this may be a pipe, meaning
1847 * you can't seek in it. The only seek you should do is if the
1848 * AssetFileDescriptor contains an offset, to move to that offset before
1849 * reading. You own this descriptor and are responsible for closing it when done.
1850 * @throws FileNotFoundException Throws FileNotFoundException of no
1851 * data of the desired type exists under the URI.
1852 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001853 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1854 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001855 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1856 }
1857
1858 /**
1859 * Open a raw file descriptor to access (potentially type transformed)
1860 * data from a "content:" URI. This interacts with the underlying
1861 * {@link ContentProvider#openTypedAssetFile} method of the provider
1862 * associated with the given URI, to retrieve retrieve any appropriate
1863 * data stream for the data stored there.
1864 *
1865 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1866 * with "content:" URIs, because content providers are the only facility
1867 * with an associated MIME type to ensure that the returned data stream
1868 * is of the desired type.
1869 *
1870 * <p>All text/* streams are encoded in UTF-8.
1871 *
1872 * @param uri The desired URI to open.
1873 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001874 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001875 * select a type, though there is no way for you to determine what type
1876 * it is returning.
1877 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001878 * @param cancellationSignal A signal to cancel the operation in progress,
1879 * or null if none. If the operation is canceled, then
1880 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001881 * @return Returns a new ParcelFileDescriptor from which you can read the
1882 * data stream from the provider. Note that this may be a pipe, meaning
1883 * you can't seek in it. The only seek you should do is if the
1884 * AssetFileDescriptor contains an offset, to move to that offset before
1885 * reading. You own this descriptor and are responsible for closing it when done.
1886 * @throws FileNotFoundException Throws FileNotFoundException of no
1887 * data of the desired type exists under the URI.
1888 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001889 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1890 @NonNull String mimeType, @Nullable Bundle opts,
1891 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00001892 Objects.requireNonNull(uri, "uri");
1893 Objects.requireNonNull(mimeType, "mimeType");
Jeff Sharkey673db442015-06-11 19:30:57 -07001894
Jeff Sharkey5693cfd2019-03-29 20:09:37 -06001895 try {
1896 if (mWrapped != null) return mWrapped.openTypedAssetFile(uri, mimeType, opts, cancellationSignal);
1897 } catch (RemoteException e) {
1898 return null;
1899 }
1900
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001901 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1902 if (unstableProvider == null) {
1903 throw new FileNotFoundException("No content provider: " + uri);
1904 }
1905 IContentProvider stableProvider = null;
1906 AssetFileDescriptor fd = null;
1907
1908 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001909 ICancellationSignal remoteCancellationSignal = null;
1910 if (cancellationSignal != null) {
1911 cancellationSignal.throwIfCanceled();
1912 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1913 cancellationSignal.setRemote(remoteCancellationSignal);
1914 }
1915
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001916 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001917 fd = unstableProvider.openTypedAssetFile(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001918 mPackageName, mAttributionTag, uri, mimeType, opts,
1919 remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001920 if (fd == null) {
1921 // The provider will be released by the finally{} clause
1922 return null;
1923 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001924 } catch (DeadObjectException e) {
1925 // The remote process has died... but we only hold an unstable
1926 // reference though, so we might recover!!! Let's try!!!!
1927 // This is exciting!!1!!1!!!!1
1928 unstableProviderDied(unstableProvider);
1929 stableProvider = acquireProvider(uri);
1930 if (stableProvider == null) {
1931 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001932 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001933 fd = stableProvider.openTypedAssetFile(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08001934 mPackageName, mAttributionTag, uri, mimeType, opts,
1935 remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001936 if (fd == null) {
1937 // The provider will be released by the finally{} clause
1938 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001941
1942 if (stableProvider == null) {
1943 stableProvider = acquireProvider(uri);
1944 }
1945 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001946 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001947 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1948 fd.getParcelFileDescriptor(), stableProvider);
1949
1950 // Success! Don't release the provider when exiting, let
1951 // ParcelFileDescriptorInner do that when it is closed.
1952 stableProvider = null;
1953
1954 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
Varun Shah840a9c42019-10-17 16:07:53 -07001955 fd.getDeclaredLength(), fd.getExtras());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001956
1957 } catch (RemoteException e) {
1958 // Whatever, whatever, we'll go away.
1959 throw new FileNotFoundException(
1960 "Failed opening content provider: " + uri);
1961 } catch (FileNotFoundException e) {
1962 throw e;
1963 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001964 if (cancellationSignal != null) {
1965 cancellationSignal.setRemote(null);
1966 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001967 if (stableProvider != null) {
1968 releaseProvider(stableProvider);
1969 }
1970 if (unstableProvider != null) {
1971 releaseUnstableProvider(unstableProvider);
1972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 }
1974 }
1975
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001976 /**
1977 * A resource identified by the {@link Resources} that contains it, and a resource id.
1978 *
1979 * @hide
1980 */
1981 public class OpenResourceIdResult {
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001982 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001983 public Resources r;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001984 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001985 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001987
1988 /**
1989 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1990 *
1991 * @hide
1992 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001993 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001994 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 String authority = uri.getAuthority();
1996 Resources r;
1997 if (TextUtils.isEmpty(authority)) {
1998 throw new FileNotFoundException("No authority: " + uri);
1999 } else {
2000 try {
2001 r = mContext.getPackageManager().getResourcesForApplication(authority);
2002 } catch (NameNotFoundException ex) {
2003 throw new FileNotFoundException("No package found for authority: " + uri);
2004 }
2005 }
2006 List<String> path = uri.getPathSegments();
2007 if (path == null) {
2008 throw new FileNotFoundException("No path: " + uri);
2009 }
2010 int len = path.size();
2011 int id;
2012 if (len == 1) {
2013 try {
2014 id = Integer.parseInt(path.get(0));
2015 } catch (NumberFormatException e) {
2016 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
2017 }
2018 } else if (len == 2) {
2019 id = r.getIdentifier(path.get(1), path.get(0), authority);
2020 } else {
2021 throw new FileNotFoundException("More than two path segments: " + uri);
2022 }
2023 if (id == 0) {
2024 throw new FileNotFoundException("No resource found for: " + uri);
2025 }
2026 OpenResourceIdResult res = new OpenResourceIdResult();
2027 res.r = r;
2028 res.id = id;
2029 return res;
2030 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08002031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 /**
2033 * Inserts a row into a table at the given URL.
2034 *
2035 * If the content provider supports transactions the insertion will be atomic.
2036 *
2037 * @param url The URL of the table to insert into.
2038 * @param values The initial values for the newly inserted row. The key is the column name for
2039 * the field. Passing an empty ContentValues will create an empty row.
Varun Shahc4fdfa32019-05-10 14:10:08 -07002040 * @return the URL of the newly created row. May return <code>null</code> if the underlying
2041 * content provider returns <code>null</code>, or if it crashes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002043 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
2044 @Nullable ContentValues values) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002045 return insert(url, values, null);
2046 }
2047
2048 /**
2049 * Inserts a row into a table at the given URL.
2050 *
2051 * If the content provider supports transactions the insertion will be atomic.
2052 *
2053 * @param url The URL of the table to insert into.
2054 * @param values The initial values for the newly inserted row. The key is the column name for
2055 * the field. Passing an empty ContentValues will create an empty row.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002056 * @param extras A Bundle containing additional information necessary for
2057 * the operation. Arguments may include SQL style arguments, such
2058 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2059 * the documentation for each individual provider will indicate
2060 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002061 * @return the URL of the newly created row. May return <code>null</code> if the underlying
2062 * content provider returns <code>null</code>, or if it crashes.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002063 * @throws IllegalArgumentException if the provider doesn't support one of
2064 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002065 */
2066 @Override
2067 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
2068 @Nullable ContentValues values, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002069 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002070
2071 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002072 if (mWrapped != null) return mWrapped.insert(url, values, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002073 } catch (RemoteException e) {
2074 return null;
2075 }
2076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 IContentProvider provider = acquireProvider(url);
2078 if (provider == null) {
2079 throw new IllegalArgumentException("Unknown URL " + url);
2080 }
2081 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002082 long startTime = SystemClock.uptimeMillis();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002083 Uri createdRow = provider.insert(mPackageName, mAttributionTag, url, values, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002084 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002085 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
2086 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002088 // Arbitrary and not worth documenting, as Activity
2089 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 return null;
2091 } finally {
2092 releaseProvider(provider);
2093 }
2094 }
2095
Fred Quintana89437372009-05-15 15:10:40 -07002096 /**
Fred Quintana89437372009-05-15 15:10:40 -07002097 * Applies each of the {@link ContentProviderOperation} objects and returns an array
2098 * of their results. Passes through OperationApplicationException, which may be thrown
2099 * by the call to {@link ContentProviderOperation#apply}.
2100 * If all the applications succeed then a {@link ContentProviderResult} array with the
2101 * same number of elements as the operations will be returned. It is implementation-specific
2102 * how many, if any, operations will have been successfully applied if a call to
2103 * apply results in a {@link OperationApplicationException}.
2104 * @param authority the authority of the ContentProvider to which this batch should be applied
2105 * @param operations the operations to apply
2106 * @return the results of the applications
2107 * @throws OperationApplicationException thrown if an application fails.
2108 * See {@link ContentProviderOperation#apply} for more information.
2109 * @throws RemoteException thrown if a RemoteException is encountered while attempting
2110 * to communicate with a remote provider.
2111 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002112 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07002113 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
2114 @NonNull ArrayList<ContentProviderOperation> operations)
2115 throws RemoteException, OperationApplicationException {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002116 Objects.requireNonNull(authority, "authority");
2117 Objects.requireNonNull(operations, "operations");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002118
2119 try {
2120 if (mWrapped != null) return mWrapped.applyBatch(authority, operations);
2121 } catch (RemoteException e) {
2122 return null;
2123 }
2124
Fred Quintana89437372009-05-15 15:10:40 -07002125 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002126 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07002127 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002128 }
2129 try {
Fred Quintana89437372009-05-15 15:10:40 -07002130 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07002131 } finally {
2132 provider.release();
2133 }
2134 }
2135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 /**
2137 * Inserts multiple rows into a table at the given URL.
2138 *
2139 * This function make no guarantees about the atomicity of the insertions.
2140 *
2141 * @param url The URL of the table to insert into.
2142 * @param values The initial values for the newly inserted rows. The key is the column name for
2143 * the field. Passing null will create an empty row.
2144 * @return the number of newly created rows.
2145 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002146 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07002147 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
2148 @NonNull ContentValues[] values) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002149 Objects.requireNonNull(url, "url");
2150 Objects.requireNonNull(values, "values");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002151
2152 try {
2153 if (mWrapped != null) return mWrapped.bulkInsert(url, values);
2154 } catch (RemoteException e) {
2155 return 0;
2156 }
2157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 IContentProvider provider = acquireProvider(url);
2159 if (provider == null) {
2160 throw new IllegalArgumentException("Unknown URL " + url);
2161 }
2162 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002163 long startTime = SystemClock.uptimeMillis();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002164 int rowsCreated = provider.bulkInsert(mPackageName, mAttributionTag, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002165 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002166 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
2167 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002169 // Arbitrary and not worth documenting, as Activity
2170 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 return 0;
2172 } finally {
2173 releaseProvider(provider);
2174 }
2175 }
2176
2177 /**
2178 * Deletes row(s) specified by a content URI.
2179 *
2180 * If the content provider supports transactions, the deletion will be atomic.
2181 *
2182 * @param url The URL of the row to delete.
2183 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
2184 (excluding the WHERE itself).
2185 * @return The number of rows deleted.
2186 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002187 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07002188 @Nullable String[] selectionArgs) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002189 return delete(url, createSqlQueryBundle(where, selectionArgs));
2190 }
2191
2192 /**
2193 * Deletes row(s) specified by a content URI.
2194 *
2195 * If the content provider supports transactions, the deletion will be atomic.
2196 *
2197 * @param url The URL of the row to delete.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002198 * @param extras A Bundle containing additional information necessary for
2199 * the operation. Arguments may include SQL style arguments, such
2200 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2201 * the documentation for each individual provider will indicate
2202 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002203 * @return The number of rows deleted.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002204 * @throws IllegalArgumentException if the provider doesn't support one of
2205 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002206 */
2207 @Override
2208 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002209 Objects.requireNonNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002210
2211 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002212 if (mWrapped != null) return mWrapped.delete(url, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002213 } catch (RemoteException e) {
2214 return 0;
2215 }
2216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 IContentProvider provider = acquireProvider(url);
2218 if (provider == null) {
2219 throw new IllegalArgumentException("Unknown URL " + url);
2220 }
2221 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002222 long startTime = SystemClock.uptimeMillis();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002223 int rowsDeleted = provider.delete(mPackageName, mAttributionTag, url, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002224 long durationMillis = SystemClock.uptimeMillis() - startTime;
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002225 maybeLogUpdateToEventLog(durationMillis, url, "delete", null);
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002226 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002228 // Arbitrary and not worth documenting, as Activity
2229 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 return -1;
2231 } finally {
2232 releaseProvider(provider);
2233 }
2234 }
2235
2236 /**
2237 * Update row(s) in a content URI.
2238 *
2239 * If the content provider supports transactions the update will be atomic.
2240 *
2241 * @param uri The URI to modify.
2242 * @param values The new field values. The key is the column name for the field.
2243 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08002244 * @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 -08002245 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002246 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 * @throws NullPointerException if uri or values are null
2248 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07002249 public final int update(@RequiresPermission.Write @NonNull Uri uri,
2250 @Nullable ContentValues values, @Nullable String where,
2251 @Nullable String[] selectionArgs) {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002252 return update(uri, values, createSqlQueryBundle(where, selectionArgs));
2253 }
2254
2255 /**
2256 * Update row(s) in a content URI.
2257 *
2258 * If the content provider supports transactions the update will be atomic.
2259 *
2260 * @param uri The URI to modify.
2261 * @param values The new field values. The key is the column name for the field.
2262 A null value will remove an existing field value.
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002263 * @param extras A Bundle containing additional information necessary for
2264 * the operation. Arguments may include SQL style arguments, such
2265 * as {@link ContentResolver#QUERY_ARG_SQL_LIMIT}, but note that
2266 * the documentation for each individual provider will indicate
2267 * which arguments they support.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002268 * @return the number of rows updated.
2269 * @throws NullPointerException if uri or values are null
Jeff Sharkeyc192ca5a2020-01-08 11:00:23 -07002270 * @throws IllegalArgumentException if the provider doesn't support one of
2271 * the requested Bundle arguments.
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002272 */
2273 @Override
2274 public final int update(@RequiresPermission.Write @NonNull Uri uri,
2275 @Nullable ContentValues values, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002276 Objects.requireNonNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002277
2278 try {
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002279 if (mWrapped != null) return mWrapped.update(uri, values, extras);
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002280 } catch (RemoteException e) {
2281 return 0;
2282 }
2283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 IContentProvider provider = acquireProvider(uri);
2285 if (provider == null) {
2286 throw new IllegalArgumentException("Unknown URI " + uri);
2287 }
2288 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002289 long startTime = SystemClock.uptimeMillis();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002290 int rowsUpdated = provider.update(mPackageName, mAttributionTag, uri, values, extras);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08002291 long durationMillis = SystemClock.uptimeMillis() - startTime;
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07002292 maybeLogUpdateToEventLog(durationMillis, uri, "update", null);
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002293 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002295 // Arbitrary and not worth documenting, as Activity
2296 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 return -1;
2298 } finally {
2299 releaseProvider(provider);
2300 }
2301 }
2302
2303 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09002304 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002305 * read or write interfaces which are cheaper than using a Cursor and/or
2306 * do not fit into the traditional table model.
2307 *
2308 * @param method provider-defined method name to call. Opaque to
2309 * framework, but must be non-null.
2310 * @param arg provider-defined String argument. May be null.
2311 * @param extras provider-defined Bundle argument. May be null.
2312 * @return a result Bundle, possibly null. Will be null if the ContentProvider
2313 * does not implement call.
2314 * @throws NullPointerException if uri or method is null
2315 * @throws IllegalArgumentException if uri is not known
2316 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002317 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
2318 @Nullable String arg, @Nullable Bundle extras) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002319 return call(uri.getAuthority(), method, arg, extras);
2320 }
2321
2322 @Override
2323 public final @Nullable Bundle call(@NonNull String authority, @NonNull String method,
2324 @Nullable String arg, @Nullable Bundle extras) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002325 Objects.requireNonNull(authority, "authority");
2326 Objects.requireNonNull(method, "method");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07002327
2328 try {
2329 if (mWrapped != null) return mWrapped.call(authority, method, arg, extras);
2330 } catch (RemoteException e) {
2331 return null;
2332 }
2333
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002334 IContentProvider provider = acquireProvider(authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002335 if (provider == null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002336 throw new IllegalArgumentException("Unknown authority " + authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002337 }
2338 try {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002339 final Bundle res = provider.call(mPackageName, mAttributionTag, authority, method, arg,
Philip P. Moltmann128b7032019-09-27 08:44:12 -07002340 extras);
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002341 Bundle.setDefusable(res, true);
2342 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002343 } catch (RemoteException e) {
2344 // Arbitrary and not worth documenting, as Activity
2345 // Manager will kill this process shortly anyway.
2346 return null;
2347 } finally {
2348 releaseProvider(provider);
2349 }
2350 }
2351
2352 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002353 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 *
2355 * @param uri The URI to a content provider
2356 * @return The ContentProvider for the given URI, or null if no content provider is found.
2357 * @hide
2358 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002359 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002360 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04002361 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 return null;
2363 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002364 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002366 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 return null;
2369 }
2370
2371 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002372 * Returns the content provider for the given content URI if the process
2373 * already has a reference on it.
2374 *
2375 * @param uri The URI to a content provider
2376 * @return The ContentProvider for the given URI, or null if no content provider is found.
2377 * @hide
2378 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002379 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002380 public final IContentProvider acquireExistingProvider(Uri uri) {
2381 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2382 return null;
2383 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002384 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002385 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002386 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002387 }
2388 return null;
2389 }
2390
2391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 * @hide
2393 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002394 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002396 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 return null;
2398 }
2399 return acquireProvider(mContext, name);
2400 }
2401
2402 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002403 * Returns the content provider for the given content URI.
2404 *
2405 * @param uri The URI to a content provider
2406 * @return The ContentProvider for the given URI, or null if no content provider is found.
2407 * @hide
2408 */
2409 public final IContentProvider acquireUnstableProvider(Uri uri) {
2410 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2411 return null;
2412 }
2413 String auth = uri.getAuthority();
2414 if (auth != null) {
2415 return acquireUnstableProvider(mContext, uri.getAuthority());
2416 }
2417 return null;
2418 }
2419
2420 /**
2421 * @hide
2422 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002423 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002424 public final IContentProvider acquireUnstableProvider(String name) {
2425 if (name == null) {
2426 return null;
2427 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002428 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002429 }
2430
2431 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07002432 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2433 * that services the content at uri, starting the provider if necessary. Returns
2434 * null if there is no provider associated wih the uri. The caller must indicate that they are
2435 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002436 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002437 * keeping it active.
2438 * @param uri specifies which provider should be acquired
2439 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2440 * that services the content at uri or null if there isn't one.
2441 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002442 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002443 Objects.requireNonNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07002444 IContentProvider provider = acquireProvider(uri);
2445 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002446 return new ContentProviderClient(this, provider, uri.getAuthority(), true);
Fred Quintana718d8a22009-04-29 17:53:20 -07002447 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002448 return null;
2449 }
2450
2451 /**
2452 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2453 * with the authority of name, starting the provider if necessary. Returns
2454 * null if there is no provider associated wih the uri. The caller must indicate that they are
2455 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002456 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002457 * keeping it active.
2458 * @param name specifies which provider should be acquired
2459 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2460 * with the authority of name or null if there isn't one.
2461 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002462 public final @Nullable ContentProviderClient acquireContentProviderClient(
2463 @NonNull String name) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002464 Objects.requireNonNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07002465 IContentProvider provider = acquireProvider(name);
2466 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002467 return new ContentProviderClient(this, provider, name, true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002468 }
2469
2470 return null;
2471 }
2472
2473 /**
2474 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
2475 * not trust the stability of the target content provider. This turns off
2476 * the mechanism in the platform clean up processes that are dependent on
2477 * a content provider if that content provider's process goes away. Normally
2478 * you can safely assume that once you have acquired a provider, you can freely
2479 * use it as needed and it won't disappear, even if your process is in the
2480 * background. If using this method, you need to take care to deal with any
2481 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002482 * so that it can be re-opened later. In particular, catching a
2483 * {@link android.os.DeadObjectException} from the calls there will let you
2484 * know that the content provider has gone away; at that point the current
2485 * ContentProviderClient object is invalid, and you should release it. You
2486 * can acquire a new one if you would like to try to restart the provider
2487 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002488 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002489 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2490 @NonNull Uri uri) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002491 Objects.requireNonNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002492 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002493 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002494 return new ContentProviderClient(this, provider, uri.getAuthority(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002495 }
2496
2497 return null;
2498 }
2499
2500 /**
2501 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
2502 * not trust the stability of the target content provider. This turns off
2503 * the mechanism in the platform clean up processes that are dependent on
2504 * a content provider if that content provider's process goes away. Normally
2505 * you can safely assume that once you have acquired a provider, you can freely
2506 * use it as needed and it won't disappear, even if your process is in the
2507 * background. If using this method, you need to take care to deal with any
2508 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002509 * so that it can be re-opened later. In particular, catching a
2510 * {@link android.os.DeadObjectException} from the calls there will let you
2511 * know that the content provider has gone away; at that point the current
2512 * ContentProviderClient object is invalid, and you should release it. You
2513 * can acquire a new one if you would like to try to restart the provider
2514 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002515 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002516 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2517 @NonNull String name) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002518 Objects.requireNonNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002519 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002520 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002521 return new ContentProviderClient(this, provider, name, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07002522 }
2523
2524 return null;
2525 }
2526
2527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * Register an observer class that gets callbacks when data identified by a
2529 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002530 * <p>
2531 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2532 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002534 * @param uri The URI to watch for changes. This can be a specific row URI,
2535 * or a base URI for a whole class of content.
2536 * @param notifyForDescendants When false, the observer will be notified
2537 * whenever a change occurs to the exact URI specified by
2538 * <code>uri</code> or to one of the URI's ancestors in the path
2539 * hierarchy. When true, the observer will also be notified
2540 * whenever a change occurs to the URI's descendants in the path
2541 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 * @param observer The object that receives callbacks when changes occur.
2543 * @see #unregisterContentObserver
2544 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002545 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07002546 @NonNull ContentObserver observer) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002547 Objects.requireNonNull(uri, "uri");
2548 Objects.requireNonNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01002549 registerContentObserver(
2550 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002551 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01002552 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002553 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002554 }
2555
2556 /** @hide - designated user version */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002557 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07002558 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002559 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002561 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002562 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002564 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
2566 }
2567
2568 /**
2569 * Unregisters a change observer.
2570 *
2571 * @param observer The previously registered observer that is no longer needed.
2572 * @see #registerContentObserver
2573 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002574 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002575 Objects.requireNonNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 try {
2577 IContentObserver contentObserver = observer.releaseContentObserver();
2578 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002579 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 contentObserver);
2581 }
2582 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002583 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585 }
2586
2587 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002588 * Notify registered observers that a row was updated and attempt to sync
2589 * changes to the network.
2590 * <p>
2591 * To observe events sent through this call, use
2592 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2593 * <p>
2594 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2595 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 *
Jeff Brown86de0592012-01-23 13:01:18 -08002597 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002598 * @param observer The observer that originated the change, may be
2599 * <code>null</null>. The observer that originated the change
2600 * will only receive the notification if it has requested to
2601 * receive self-change notifications by implementing
2602 * {@link ContentObserver#deliverSelfNotifications()} to return
2603 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002605 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 notifyChange(uri, observer, true /* sync to network */);
2607 }
2608
2609 /**
2610 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002611 * <p>
2612 * To observe events sent through this call, use
2613 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2614 * <p>
2615 * If syncToNetwork is true, this will attempt to schedule a local sync
2616 * using the sync adapter that's registered for the authority of the
2617 * provided uri. No account will be passed to the sync adapter, so all
2618 * matching accounts will be synchronized.
2619 * <p>
2620 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2621 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 *
Jeff Brown86de0592012-01-23 13:01:18 -08002623 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002624 * @param observer The observer that originated the change, may be
2625 * <code>null</null>. The observer that originated the change
2626 * will only receive the notification if it has requested to
2627 * receive self-change notifications by implementing
2628 * {@link ContentObserver#deliverSelfNotifications()} to return
2629 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002630 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002631 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
Jeff Sharkey1307f422019-11-13 13:03:10 -07002632 * @deprecated callers should consider migrating to
2633 * {@link #notifyChange(Uri, ContentObserver, int)}, as it
2634 * offers support for many more options than just
2635 * {@link #NOTIFY_SYNC_TO_NETWORK}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002637 @Deprecated
Jeff Sharkey673db442015-06-11 19:30:57 -07002638 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2639 boolean syncToNetwork) {
Jeff Sharkey1307f422019-11-13 13:03:10 -07002640 notifyChange(uri, observer, syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0);
Christopher Tate16aa9732012-09-17 16:23:44 -07002641 }
2642
2643 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002644 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002645 * <p>
2646 * To observe events sent through this call, use
2647 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2648 * <p>
Jeff Sharkey1307f422019-11-13 13:03:10 -07002649 * If {@link #NOTIFY_SYNC_TO_NETWORK} is set, this will attempt to schedule
2650 * a local sync using the sync adapter that's registered for the authority
2651 * of the provided uri. No account will be passed to the sync adapter, so
2652 * all matching accounts will be synchronized.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002653 * <p>
2654 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2655 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002656 *
2657 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002658 * @param observer The observer that originated the change, may be
2659 * <code>null</null>. The observer that originated the change
2660 * will only receive the notification if it has requested to
2661 * receive self-change notifications by implementing
2662 * {@link ContentObserver#deliverSelfNotifications()} to return
2663 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002664 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2665 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2666 */
2667 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2668 @NotifyFlags int flags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002669 Objects.requireNonNull(uri, "uri");
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002670 notifyChange(
2671 ContentProvider.getUriWithoutUserId(uri),
2672 observer,
2673 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002674 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002675 }
2676
Jeff Sharkey8b0cff72020-03-09 15:49:01 -06002677 /** @removed */
2678 @Deprecated
2679 public void notifyChange(@NonNull Iterable<Uri> uris, @Nullable ContentObserver observer,
2680 @NotifyFlags int flags) {
2681 final Collection<Uri> asCollection = new ArrayList<>();
2682 uris.forEach(asCollection::add);
2683 notifyChange(asCollection, observer, flags);
2684 }
2685
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002686 /**
Jeff Sharkey1307f422019-11-13 13:03:10 -07002687 * Notify registered observers that several rows have been updated.
2688 * <p>
2689 * To observe events sent through this call, use
2690 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2691 * <p>
2692 * If {@link #NOTIFY_SYNC_TO_NETWORK} is set, this will attempt to schedule
2693 * a local sync using the sync adapter that's registered for the authority
2694 * of the provided uri. No account will be passed to the sync adapter, so
2695 * all matching accounts will be synchronized.
2696 * <p>
2697 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2698 * notifications must be backed by a valid {@link ContentProvider}.
2699 *
2700 * @param uris The uris of the content that was changed.
2701 * @param observer The observer that originated the change, may be
2702 * <code>null</null>. The observer that originated the change
2703 * will only receive the notification if it has requested to
2704 * receive self-change notifications by implementing
2705 * {@link ContentObserver#deliverSelfNotifications()} to return
2706 * true.
2707 * @param flags Flags such as {@link #NOTIFY_SYNC_TO_NETWORK} or
2708 * {@link #NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS}.
2709 */
Jeff Sharkey8b0cff72020-03-09 15:49:01 -06002710 public void notifyChange(@NonNull Collection<Uri> uris, @Nullable ContentObserver observer,
Jeff Sharkey1307f422019-11-13 13:03:10 -07002711 @NotifyFlags int flags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002712 Objects.requireNonNull(uris, "uris");
Jeff Sharkey1307f422019-11-13 13:03:10 -07002713
2714 // Cluster based on user ID
2715 final SparseArray<ArrayList<Uri>> clusteredByUser = new SparseArray<>();
2716 for (Uri uri : uris) {
2717 final int userId = ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
2718 ArrayList<Uri> list = clusteredByUser.get(userId);
2719 if (list == null) {
2720 list = new ArrayList<>();
2721 clusteredByUser.put(userId, list);
2722 }
2723 list.add(ContentProvider.getUriWithoutUserId(uri));
2724 }
2725
2726 for (int i = 0; i < clusteredByUser.size(); i++) {
2727 final int userId = clusteredByUser.keyAt(i);
2728 final ArrayList<Uri> list = clusteredByUser.valueAt(i);
2729 notifyChange(list.toArray(new Uri[list.size()]), observer, flags, userId);
2730 }
2731 }
2732
2733 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002734 * Notify registered observers within the designated user(s) that a row was updated.
2735 *
Jeff Sharkey1307f422019-11-13 13:03:10 -07002736 * @deprecated callers should consider migrating to
2737 * {@link #notifyChange(Uri, ContentObserver, int)}, as it
2738 * offers support for many more options than just
2739 * {@link #NOTIFY_SYNC_TO_NETWORK}.
Christopher Tate16aa9732012-09-17 16:23:44 -07002740 * @hide
2741 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002742 @Deprecated
Dianne Hackborne7617772016-04-27 17:03:52 -07002743 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002744 @UserIdInt int userHandle) {
Jeff Sharkey1307f422019-11-13 13:03:10 -07002745 notifyChange(uri, observer, syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0, userHandle);
2746 }
2747
2748 /** {@hide} */
2749 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
2750 @UserIdInt int userHandle) {
2751 notifyChange(new Uri[] { uri }, observer, flags, userHandle);
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002752 }
2753
2754 /**
2755 * Notify registered observers within the designated user(s) that a row was updated.
2756 *
2757 * @hide
2758 */
Jeff Sharkey1307f422019-11-13 13:03:10 -07002759 public void notifyChange(@NonNull Uri[] uris, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002760 @UserIdInt int userHandle) {
2761 try {
2762 getContentService().notifyChange(
Jeff Sharkey1307f422019-11-13 13:03:10 -07002763 uris, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002764 observer != null && observer.deliverSelfNotifications(), flags,
Makoto Onukie183a402018-08-29 11:46:41 -07002765 userHandle, mTargetSdkVersion, mContext.getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002767 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
2769 }
2770
2771 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002772 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002773 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002774 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002775 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2776 * the grant has already been persisted, taking it again will touch
2777 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002778 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002779 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002780 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002781 public void takePersistableUriPermission(@NonNull Uri uri,
2782 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002783 Objects.requireNonNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002784 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002785 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002786 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2787 resolveUserId(uri));
2788 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002789 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002790 }
2791 }
2792
2793 /**
2794 * @hide
2795 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002796 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002797 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2798 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002799 Objects.requireNonNull(toPackage, "toPackage");
2800 Objects.requireNonNull(uri, "uri");
Felipe Leme988234a2018-02-14 12:00:29 -08002801 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002802 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002803 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2804 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002805 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002806 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002807 }
2808 }
2809
2810 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002811 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002812 * previously made persistent with
2813 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2814 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002815 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002816 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002817 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002818 public void releasePersistableUriPermission(@NonNull Uri uri,
2819 @Intent.AccessUriMode int modeFlags) {
Daulet Zhanguzina2044e12019-12-30 16:34:59 +00002820 Objects.requireNonNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002821 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002822 UriGrantsManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002823 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2824 resolveUserId(uri));
2825 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002826 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002827 }
2828 }
2829
2830 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002831 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002832 * calling app. That is, the returned permissions have been granted
2833 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002834 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002835 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002836 *
2837 * @see #takePersistableUriPermission(Uri, int)
2838 * @see #releasePersistableUriPermission(Uri, int)
2839 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002840 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002841 try {
Sudheer Shanka1b817f62019-05-20 16:54:59 -07002842 return UriGrantsManager.getService().getUriPermissions(
2843 mPackageName, true /* incoming */, true /* persistedOnly */).getList();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002844 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002845 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002846 }
2847 }
2848
2849 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002850 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002851 * calling app. That is, the returned permissions have been granted
2852 * <em>from</em> the calling app. Only grants taken with
2853 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002854 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002855 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002856 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002857 try {
Sudheer Shanka1b817f62019-05-20 16:54:59 -07002858 return UriGrantsManager.getService().getUriPermissions(
2859 mPackageName, false /* incoming */, true /* persistedOnly */).getList();
2860 } catch (RemoteException e) {
2861 throw e.rethrowFromSystemServer();
2862 }
2863 }
2864
2865 /** @hide */
2866 public @NonNull List<UriPermission> getOutgoingUriPermissions() {
2867 try {
2868 return UriGrantsManager.getService().getUriPermissions(
2869 mPackageName, false /* incoming */, false /* persistedOnly */).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002870 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002871 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002872 }
2873 }
2874
2875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 * Start an asynchronous sync operation. If you want to monitor the progress
2877 * of the sync you may register a SyncObserver. Only values of the following
2878 * types may be used in the extras bundle:
2879 * <ul>
2880 * <li>Integer</li>
2881 * <li>Long</li>
2882 * <li>Boolean</li>
2883 * <li>Float</li>
2884 * <li>Double</li>
2885 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002886 * <li>Account</li>
2887 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 * </ul>
2889 *
2890 * @param uri the uri of the provider to sync or null to sync all providers.
2891 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002892 * @deprecated instead use
2893 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002895 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002897 Account account = null;
2898 if (extras != null) {
2899 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2900 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002901 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002902 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002903 }
2904 extras.remove(SYNC_EXTRAS_ACCOUNT);
2905 }
2906 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2907 }
2908
2909 /**
2910 * Start an asynchronous sync operation. If you want to monitor the progress
2911 * of the sync you may register a SyncObserver. Only values of the following
2912 * types may be used in the extras bundle:
2913 * <ul>
2914 * <li>Integer</li>
2915 * <li>Long</li>
2916 * <li>Boolean</li>
2917 * <li>Float</li>
2918 * <li>Double</li>
2919 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002920 * <li>Account</li>
2921 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002922 * </ul>
2923 *
2924 * @param account which account should be synced
2925 * @param authority which authority should be synced
2926 * @param extras any extras to pass to the SyncAdapter.
2927 */
2928 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002929 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002930 }
2931
2932 /**
2933 * @see #requestSync(Account, String, Bundle)
2934 * @hide
2935 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002936 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002937 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002938 if (extras == null) {
2939 throw new IllegalArgumentException("Must specify extras.");
2940 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002941 SyncRequest request =
2942 new SyncRequest.Builder()
2943 .setSyncAdapter(account, authority)
2944 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002945 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002946 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002947 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002948 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2949 // case, but it's only for debugging.
2950 getContentService().syncAsUser(request, userId, ActivityThread.currentPackageName());
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002951 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002952 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002953 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002954 }
2955
2956 /**
2957 * Register a sync with the SyncManager. These requests are built using the
2958 * {@link SyncRequest.Builder}.
2959 */
2960 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002962 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2963 // case, but it's only for debugging.
2964 getContentService().sync(request, ActivityThread.currentPackageName());
Matthew Williamsfa774182013-06-18 15:44:11 -07002965 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002966 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
2968 }
2969
2970 /**
2971 * Check that only values of the following types are in the Bundle:
2972 * <ul>
2973 * <li>Integer</li>
2974 * <li>Long</li>
2975 * <li>Boolean</li>
2976 * <li>Float</li>
2977 * <li>Double</li>
2978 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002979 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 * <li>null</li>
2981 * </ul>
2982 * @param extras the Bundle to check
2983 */
2984 public static void validateSyncExtrasBundle(Bundle extras) {
2985 try {
2986 for (String key : extras.keySet()) {
2987 Object value = extras.get(key);
2988 if (value == null) continue;
2989 if (value instanceof Long) continue;
2990 if (value instanceof Integer) continue;
2991 if (value instanceof Boolean) continue;
2992 if (value instanceof Float) continue;
2993 if (value instanceof Double) continue;
2994 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002995 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 throw new IllegalArgumentException("unexpected value type: "
2997 + value.getClass().getName());
2998 }
2999 } catch (IllegalArgumentException e) {
3000 throw e;
3001 } catch (RuntimeException exc) {
3002 throw new IllegalArgumentException("error unparceling Bundle", exc);
3003 }
3004 }
3005
Fred Quintanaac9385e2009-06-22 18:00:59 -07003006 /**
3007 * Cancel any active or pending syncs that match the Uri. If the uri is null then
3008 * all syncs will be canceled.
3009 *
3010 * @param uri the uri of the provider to sync or null to sync all providers.
3011 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
3012 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07003013 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003015 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
3016 }
3017
3018 /**
3019 * Cancel any active or pending syncs that match account and authority. The account and
3020 * authority can each independently be set to null, which means that syncs with any account
3021 * or authority, respectively, will match.
3022 *
3023 * @param account filters the syncs that match by this account
3024 * @param authority filters the syncs that match by this authority
3025 */
3026 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003028 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003030 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 }
3032 }
3033
Fred Quintanaac9385e2009-06-22 18:00:59 -07003034 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003035 * @see #cancelSync(Account, String)
3036 * @hide
3037 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003038 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003039 try {
3040 getContentService().cancelSyncAsUser(account, authority, null, userId);
3041 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003042 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003043 }
3044 }
3045
3046 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003047 * Get information about the SyncAdapters that are known to the system.
3048 * @return an array of SyncAdapters that have registered with the system
3049 */
3050 public static SyncAdapterType[] getSyncAdapterTypes() {
3051 try {
3052 return getContentService().getSyncAdapterTypes();
3053 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003054 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003055 }
3056 }
3057
3058 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003059 * @see #getSyncAdapterTypes()
3060 * @hide
3061 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003062 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003063 try {
3064 return getContentService().getSyncAdapterTypesAsUser(userId);
3065 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003066 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003067 }
3068 }
3069
3070 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07003071 * @hide
3072 * Returns the package names of syncadapters that match a given user and authority.
3073 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07003074 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07003075 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003076 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07003077 try {
3078 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
3079 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003080 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07003081 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07003082 }
3083
3084 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003085 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003086 * <p>This method requires the caller to hold the permission
3087 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003088 *
3089 * @param account the account whose setting we are querying
3090 * @param authority the provider whose setting we are querying
3091 * @return true if the provider should be synced when a network tickle is received
3092 */
3093 public static boolean getSyncAutomatically(Account account, String authority) {
3094 try {
3095 return getContentService().getSyncAutomatically(account, authority);
3096 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003097 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003098 }
3099 }
3100
3101 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003102 * @see #getSyncAutomatically(Account, String)
3103 * @hide
3104 */
3105 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003106 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003107 try {
3108 return getContentService().getSyncAutomaticallyAsUser(account, authority, userId);
3109 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003110 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003111 }
3112 }
3113
3114 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003115 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003116 * <p>This method requires the caller to hold the permission
3117 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003118 *
3119 * @param account the account whose setting we are querying
3120 * @param authority the provider whose behavior is being controlled
3121 * @param sync true if the provider should be synced when tickles are received for it
3122 */
3123 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003124 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003125 }
3126
3127 /**
3128 * @see #setSyncAutomatically(Account, String, boolean)
3129 * @hide
3130 */
3131 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003132 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003133 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003134 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003135 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003136 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003137 }
3138 }
3139
3140 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003141 * Specifies that a sync should be requested with the specified the account, authority,
3142 * and extras at the given frequency. If there is already another periodic sync scheduled
3143 * with the account, authority and extras then a new periodic sync won't be added, instead
3144 * the frequency of the previous one will be updated.
3145 * <p>
3146 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
3147 * Although these sync are scheduled at the specified frequency, it may take longer for it to
3148 * actually be started if other syncs are ahead of it in the sync operation queue. This means
3149 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08003150 * <p>
3151 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
3152 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
3153 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
3154 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
3155 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003156 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003157 * <p>This method requires the caller to hold the permission
3158 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003159 * <p>The bundle for a periodic sync can be queried by applications with the correct
3160 * permissions using
3161 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
3162 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003163 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003164 * @param account the account to specify in the sync
3165 * @param authority the provider to specify in the sync request
3166 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07003167 * @param pollFrequency how frequently the sync should be performed, in seconds.
3168 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
3169 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08003170 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
3171 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003172 */
3173 public static void addPeriodicSync(Account account, String authority, Bundle extras,
3174 long pollFrequency) {
3175 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08003176 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08003177 throw new IllegalArgumentException("illegal extras were set");
3178 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003179 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07003180 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003181 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003182 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003183 }
3184 }
3185
3186 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003187 * {@hide}
3188 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
3189 * extras were set for a periodic sync.
3190 *
3191 * @param extras bundle to validate.
3192 */
3193 public static boolean invalidPeriodicExtras(Bundle extras) {
3194 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
3195 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
3196 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
3197 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
3198 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
3199 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
3200 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
3201 return true;
3202 }
3203 return false;
3204 }
3205
3206 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003207 * Remove a periodic sync. Has no affect if account, authority and extras don't match
3208 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003209 * <p>This method requires the caller to hold the permission
3210 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003211 *
3212 * @param account the account of the periodic sync to remove
3213 * @param authority the provider of the periodic sync to remove
3214 * @param extras the extras of the periodic sync to remove
3215 */
3216 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
3217 validateSyncExtrasBundle(extras);
3218 try {
3219 getContentService().removePeriodicSync(account, authority, extras);
3220 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003221 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003222 }
3223 }
3224
3225 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003226 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
3227 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07003228 * <p>
3229 * If a periodic sync is specified, the caller must hold the permission
3230 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
3231 *</p>
3232 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003233 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07003234 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07003235 *
3236 * @param request SyncRequest object containing information about sync to cancel.
3237 */
3238 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003239 if (request == null) {
3240 throw new IllegalArgumentException("request cannot be null");
3241 }
3242 try {
3243 getContentService().cancelRequest(request);
3244 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003245 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003246 }
Matthew Williamsfa774182013-06-18 15:44:11 -07003247 }
3248
3249 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003250 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003251 * <p>This method requires the caller to hold the permission
3252 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003253 *
3254 * @param account the account whose periodic syncs we are querying
3255 * @param authority the provider whose periodic syncs we are querying
3256 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
3257 */
3258 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
3259 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003260 return getContentService().getPeriodicSyncs(account, authority, null);
3261 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003262 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003263 }
3264 }
3265
3266 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07003267 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003268 * <p>This method requires the caller to hold the permission
3269 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07003270 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
3271 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07003272 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07003273 try {
3274 return getContentService().getIsSyncable(account, authority);
3275 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003276 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07003277 }
3278 }
3279
3280 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003281 * @see #getIsSyncable(Account, String)
3282 * @hide
3283 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003284 public static int getIsSyncableAsUser(Account account, String authority,
3285 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003286 try {
3287 return getContentService().getIsSyncableAsUser(account, authority, userId);
3288 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003289 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003290 }
3291 }
3292
3293 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07003294 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003295 * <p>This method requires the caller to hold the permission
3296 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07003297 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07003298 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07003299 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07003300 try {
3301 getContentService().setIsSyncable(account, authority, syncable);
3302 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003303 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07003304 }
3305 }
3306
3307 /**
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +00003308 * @see #setIsSyncable(Account, String, int)
3309 * @hide
3310 */
3311 public static void setIsSyncableAsUser(Account account, String authority, int syncable,
3312 int userId) {
3313 try {
3314 getContentService().setIsSyncableAsUser(account, authority, syncable, userId);
3315 } catch (RemoteException e) {
3316 throw e.rethrowFromSystemServer();
3317 }
3318 }
3319
3320 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003321 * Gets the master auto-sync setting that applies to all the providers and accounts.
3322 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003323 * <p>This method requires the caller to hold the permission
3324 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003325 *
3326 * @return the master auto-sync setting that applies to all the providers and accounts
3327 */
3328 public static boolean getMasterSyncAutomatically() {
3329 try {
3330 return getContentService().getMasterSyncAutomatically();
3331 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003332 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003333 }
3334 }
3335
3336 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003337 * @see #getMasterSyncAutomatically()
3338 * @hide
3339 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003340 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003341 try {
3342 return getContentService().getMasterSyncAutomaticallyAsUser(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 * Sets the master auto-sync setting that applies to all the providers and accounts.
3350 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003351 * <p>This method requires the caller to hold the permission
3352 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003353 *
3354 * @param sync the master auto-sync setting that applies to all the providers and accounts
3355 */
3356 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003357 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01003358 }
3359
3360 /**
3361 * @see #setMasterSyncAutomatically(boolean)
3362 * @hide
3363 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003364 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003365 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01003366 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003367 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003368 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003369 }
3370 }
3371
3372 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003373 * Returns true if there is currently a sync operation for the given account or authority
3374 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003375 * <p>This method requires the caller to hold the permission
3376 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003377 * @param account the account whose setting we are querying
3378 * @param authority the provider whose behavior is being queried
3379 * @return true if a sync is active for the given account or authority.
3380 */
3381 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003382 if (account == null) {
3383 throw new IllegalArgumentException("account must not be null");
3384 }
3385 if (authority == null) {
3386 throw new IllegalArgumentException("authority must not be null");
3387 }
3388
Fred Quintanaac9385e2009-06-22 18:00:59 -07003389 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003390 return getContentService().isSyncActive(account, authority, null);
3391 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003392 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003393 }
3394 }
3395
Fred Quintanaac9385e2009-06-22 18:00:59 -07003396 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07003397 * If a sync is active returns the information about it, otherwise returns null.
3398 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003399 * This method requires the caller to hold the permission
3400 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3401 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003402 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07003403 * @deprecated
3404 * Since multiple concurrent syncs are now supported you should use
3405 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
3406 * This method returns the first item from the list of current syncs
3407 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003408 */
Fred Quintanac6a69552010-09-27 17:05:04 -07003409 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003410 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003411 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07003412 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
3413 if (syncs.isEmpty()) {
3414 return null;
3415 }
3416 return syncs.get(0);
3417 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003418 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07003419 }
3420 }
3421
3422 /**
3423 * Returns a list with information about all the active syncs. This list will be empty
3424 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003425 * <p>
3426 * This method requires the caller to hold the permission
3427 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3428 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07003429 * @return a List of SyncInfo objects for the currently active syncs.
3430 */
3431 public static List<SyncInfo> getCurrentSyncs() {
3432 try {
3433 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003434 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003435 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003436 }
3437 }
3438
3439 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003440 * @see #getCurrentSyncs()
3441 * @hide
3442 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003443 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003444 try {
3445 return getContentService().getCurrentSyncsAsUser(userId);
3446 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003447 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003448 }
3449 }
3450
3451 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07003452 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003453 * @param account the account whose setting we are querying
3454 * @param authority the provider whose behavior is being queried
3455 * @return the SyncStatusInfo for the authority, or null if none exists
3456 * @hide
3457 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003458 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07003459 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
3460 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003461 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003462 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003463 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003464 }
3465 }
3466
3467 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003468 * @see #getSyncStatus(Account, String)
3469 * @hide
3470 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003471 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003472 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003473 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003474 try {
3475 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
3476 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003477 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003478 }
3479 }
3480
3481 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003482 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003483 * <p>This method requires the caller to hold the permission
3484 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003485 * @param account the account whose setting we are querying
3486 * @param authority the provider whose behavior is being queried
3487 * @return true if there is a pending sync with the matching account and authority
3488 */
3489 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003490 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003491 }
3492
3493 /**
3494 * @see #requestSync(Account, String, Bundle)
3495 * @hide
3496 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003497 public static boolean isSyncPendingAsUser(Account account, String authority,
3498 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003499 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003500 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003501 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003502 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003503 }
3504 }
3505
Fred Quintana1b487ec2010-02-26 10:57:55 -08003506 /**
3507 * Request notifications when the different aspects of the SyncManager change. The
3508 * different items that can be requested are:
3509 * <ul>
3510 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
3511 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
3512 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
3513 * </ul>
3514 * The caller can set one or more of the status types in the mask for any
3515 * given listener registration.
3516 * @param mask the status change types that will cause the callback to be invoked
3517 * @param callback observer to be invoked when the status changes
3518 * @return a handle that can be used to remove the listener at a later time
3519 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003520 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003521 if (callback == null) {
3522 throw new IllegalArgumentException("you passed in a null callback");
3523 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003524 try {
3525 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08003526 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07003527 public void onStatusChanged(int which) throws RemoteException {
3528 callback.onStatusChanged(which);
3529 }
3530 };
3531 getContentService().addStatusChangeListener(mask, observer);
3532 return observer;
3533 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003534 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003535 }
3536 }
3537
Fred Quintana1b487ec2010-02-26 10:57:55 -08003538 /**
3539 * Remove a previously registered status change listener.
3540 * @param handle the handle that was returned by {@link #addStatusChangeListener}
3541 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003542 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003543 if (handle == null) {
3544 throw new IllegalArgumentException("you passed in a null handle");
3545 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003546 try {
3547 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
3548 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003549 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003550 }
3551 }
3552
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003553 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003554 * Store the given {@link Bundle} as a long-lived cached object within the
3555 * system. This can be useful to avoid expensive re-parsing when apps are
3556 * restarted multiple times on low-RAM devices.
3557 * <p>
3558 * The {@link Bundle} is automatically invalidated when a
3559 * {@link #notifyChange(Uri, ContentObserver)} event applies to the key.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003560 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003561 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003562 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003563 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003564 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003565 public void putCache(@NonNull Uri key, @Nullable Bundle value) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003566 try {
3567 getContentService().putCache(mContext.getPackageName(), key, value,
3568 mContext.getUserId());
3569 } catch (RemoteException e) {
3570 throw e.rethrowFromSystemServer();
3571 }
3572 }
3573
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003574 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003575 * Retrieve the last {@link Bundle} stored as a long-lived cached object
3576 * within the system.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003577 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003578 * @return {@code null} if no cached object has been stored, or if the
3579 * stored object has been invalidated due to a
3580 * {@link #notifyChange(Uri, ContentObserver)} event.
3581 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003582 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003583 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003584 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003585 public @Nullable Bundle getCache(@NonNull Uri key) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003586 try {
3587 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
3588 mContext.getUserId());
3589 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
3590 return bundle;
3591 } catch (RemoteException e) {
3592 throw e.rethrowFromSystemServer();
3593 }
3594 }
3595
Ben Lin8ea82002017-03-08 17:30:16 -08003596 /** {@hide} */
3597 public int getTargetSdkVersion() {
3598 return mTargetSdkVersion;
3599 }
3600
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003601 /**
3602 * Returns sampling percentage for a given duration.
3603 *
3604 * Always returns at least 1%.
3605 */
3606 private int samplePercentForDuration(long durationMillis) {
3607 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
3608 return 100;
3609 }
3610 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
3611 }
3612
Steve McKayea93fe72016-12-02 11:35:35 -08003613 private void maybeLogQueryToEventLog(
3614 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003615 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003616 int samplePercent = samplePercentForDuration(durationMillis);
3617 if (samplePercent < 100) {
3618 synchronized (mRandom) {
3619 if (mRandom.nextInt(100) >= samplePercent) {
3620 return;
3621 }
3622 }
3623 }
3624
Steve McKayea93fe72016-12-02 11:35:35 -08003625 // Ensure a non-null bundle.
3626 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
3627
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003628 StringBuilder projectionBuffer = new StringBuilder(100);
3629 if (projection != null) {
3630 for (int i = 0; i < projection.length; ++i) {
3631 // Note: not using a comma delimiter here, as the
3632 // multiple arguments to EventLog.writeEvent later
3633 // stringify with a comma delimiter, which would make
3634 // parsing uglier later.
3635 if (i != 0) projectionBuffer.append('/');
3636 projectionBuffer.append(projection[i]);
3637 }
3638 }
3639
3640 // ActivityThread.currentPackageName() only returns non-null if the
3641 // current thread is an application main thread. This parameter tells
3642 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003643 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003644
3645 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003646 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003647 uri.toString(),
3648 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08003649 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
3650 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003651 durationMillis,
3652 blockingPackage != null ? blockingPackage : "",
3653 samplePercent);
3654 }
3655
3656 private void maybeLogUpdateToEventLog(
3657 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003658 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003659 int samplePercent = samplePercentForDuration(durationMillis);
3660 if (samplePercent < 100) {
3661 synchronized (mRandom) {
3662 if (mRandom.nextInt(100) >= samplePercent) {
3663 return;
3664 }
3665 }
3666 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003667 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003668 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003669 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003670 uri.toString(),
3671 operation,
3672 selection != null ? selection : "",
3673 durationMillis,
3674 blockingPackage != null ? blockingPackage : "",
3675 samplePercent);
3676 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003677
Jeff Brown825c5132011-10-12 16:11:30 -07003678 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003679 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003680 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003681
3682 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003684 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003686 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003687 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 }
3689
3690 @Override
3691 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003692 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003694
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003695 if (mProviderReleased.compareAndSet(false, true)) {
3696 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 }
3699
3700 @Override
3701 protected void finalize() throws Throwable {
3702 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003703 if (mCloseGuard != null) {
3704 mCloseGuard.warnIfOpen();
3705 }
3706
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003707 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 } finally {
3709 super.finalize();
3710 }
3711 }
3712 }
3713
3714 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003715 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003716 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717
3718 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3719 super(pfd);
3720 mContentProvider = icp;
3721 }
3722
3723 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003724 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003725 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003726 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
3728 }
3729 }
3730
Dianne Hackborn231cc602009-04-27 17:10:36 -07003731 /** @hide */
3732 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003733
Dianne Hackborn231cc602009-04-27 17:10:36 -07003734 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003735 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003736 public static IContentService getContentService() {
3737 if (sContentService != null) {
3738 return sContentService;
3739 }
3740 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003741 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003742 return sContentService;
3743 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003744
Dianne Hackborn35654b62013-01-14 17:38:02 -08003745 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003746 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003747 public String getPackageName() {
3748 return mPackageName;
3749 }
3750
Philip P. Moltmann128b7032019-09-27 08:44:12 -07003751 /** @hide */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003752 public @Nullable String getAttributionTag() {
3753 return mAttributionTag;
Philip P. Moltmann128b7032019-09-27 08:44:12 -07003754 }
3755
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003756 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003757 private static volatile IContentService sContentService;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003758 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003760
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003761 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003762 final String mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003763 final @Nullable String mAttributionTag;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003764 final int mTargetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -07003765 final ContentInterface mWrapped;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003768
3769 /** @hide */
3770 public int resolveUserId(Uri uri) {
3771 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3772 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003773
3774 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003775 public int getUserId() {
3776 return mContext.getUserId();
3777 }
3778
Jeff Sharkey806bece2019-02-19 14:42:55 -07003779 /** {@hide} */
3780 @Deprecated
Daniel Nishic29d2b02016-06-30 12:20:41 -07003781 public Drawable getTypeDrawable(String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003782 return getTypeInfo(mimeType).getIcon().loadDrawable(mContext);
3783 }
3784
3785 /**
3786 * Return a detailed description of the given MIME type, including an icon
3787 * and label that describe the type.
3788 *
3789 * @param mimeType Valid, concrete MIME type.
3790 */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003791 public final @NonNull MimeTypeInfo getTypeInfo(@NonNull String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003792 Objects.requireNonNull(mimeType);
3793 return MimeIconUtils.getTypeInfo(mimeType);
3794 }
3795
3796 /**
3797 * Detailed description of a specific MIME type, including an icon and label
3798 * that describe the type.
3799 */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003800 public static final class MimeTypeInfo {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003801 private final Icon mIcon;
3802 private final CharSequence mLabel;
3803 private final CharSequence mContentDescription;
3804
3805 /** {@hide} */
Jeff Sharkey0185ea02019-04-09 10:15:09 -06003806 public MimeTypeInfo(@NonNull Icon icon, @NonNull CharSequence label,
Jeff Sharkey806bece2019-02-19 14:42:55 -07003807 @NonNull CharSequence contentDescription) {
3808 mIcon = Objects.requireNonNull(icon);
3809 mLabel = Objects.requireNonNull(label);
3810 mContentDescription = Objects.requireNonNull(contentDescription);
3811 }
3812
3813 /**
3814 * Return a visual representation of this MIME type. This can be styled
3815 * using {@link Icon#setTint(int)} to match surrounding UI.
3816 *
3817 * @see Icon#loadDrawable(Context)
3818 * @see android.widget.ImageView#setImageDrawable(Drawable)
3819 */
3820 public @NonNull Icon getIcon() {
3821 return mIcon;
3822 }
3823
3824 /**
3825 * Return a textual representation of this MIME type.
3826 *
3827 * @see android.widget.TextView#setText(CharSequence)
3828 */
3829 public @NonNull CharSequence getLabel() {
3830 return mLabel;
3831 }
3832
3833 /**
3834 * Return a content description for this MIME type.
3835 *
3836 * @see android.view.View#setContentDescription(CharSequence)
3837 */
3838 public @NonNull CharSequence getContentDescription() {
3839 return mContentDescription;
3840 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003841 }
Steve McKayea93fe72016-12-02 11:35:35 -08003842
3843 /**
3844 * @hide
3845 */
3846 public static @Nullable Bundle createSqlQueryBundle(
3847 @Nullable String selection,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -07003848 @Nullable String[] selectionArgs) {
3849 return createSqlQueryBundle(selection, selectionArgs, null);
3850 }
3851
3852 /**
3853 * @hide
3854 */
3855 public static @Nullable Bundle createSqlQueryBundle(
3856 @Nullable String selection,
Steve McKayea93fe72016-12-02 11:35:35 -08003857 @Nullable String[] selectionArgs,
3858 @Nullable String sortOrder) {
3859
3860 if (selection == null && selectionArgs == null && sortOrder == null) {
3861 return null;
3862 }
3863
3864 Bundle queryArgs = new Bundle();
3865 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003866 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003867 }
3868 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003869 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003870 }
3871 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003872 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003873 }
3874 return queryArgs;
3875 }
Steve McKay29c3f682016-12-16 14:52:59 -08003876
Sudheer Shanka7d28b5b2020-02-11 14:32:59 -08003877 /** @hide */
3878 public static @NonNull Bundle includeSqlSelectionArgs(@NonNull Bundle queryArgs,
3879 @Nullable String selection, @Nullable String[] selectionArgs) {
3880 if (selection != null) {
3881 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
3882 }
3883 if (selectionArgs != null) {
3884 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
3885 }
3886 return queryArgs;
3887 }
3888
Steve McKay29c3f682016-12-16 14:52:59 -08003889 /**
3890 * Returns structured sort args formatted as an SQL sort clause.
3891 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003892 * NOTE: Collator clauses are suitable for use with non text fields. We might
3893 * choose to omit any collation clause since we don't know the underlying
3894 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3895 * appear to care much about the presence of collate clauses in queries
3896 * when ordering by numeric fields. For this reason we include collate
3897 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3898 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003899 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003900 * TODO: Would be nice to explicitly validate that colums referenced in
3901 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003902 *
3903 * @hide
3904 */
3905 public static String createSqlSortClause(Bundle queryArgs) {
3906 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3907 if (columns == null || columns.length == 0) {
3908 throw new IllegalArgumentException("Can't create sort clause without columns.");
3909 }
3910
3911 String query = TextUtils.join(", ", columns);
3912
Steve McKayd7ece9f2017-01-12 16:59:59 -08003913 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3914 // on their javadoc descriptions.
3915 int collation = queryArgs.getInt(
3916 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3917 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3918 query += " COLLATE NOCASE";
3919 }
3920
Steve McKay415f41b2017-02-01 13:38:25 -08003921 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3922 if (sortDir != Integer.MIN_VALUE) {
3923 switch (sortDir) {
3924 case QUERY_SORT_DIRECTION_ASCENDING:
3925 query += " ASC";
3926 break;
3927 case QUERY_SORT_DIRECTION_DESCENDING:
3928 query += " DESC";
3929 break;
3930 default:
3931 throw new IllegalArgumentException("Unsupported sort direction value."
3932 + " See ContentResolver documentation for details.");
3933 }
Steve McKay29c3f682016-12-16 14:52:59 -08003934 }
Steve McKay29c3f682016-12-16 14:52:59 -08003935 return query;
3936 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003937
3938 /**
3939 * Convenience method that efficiently loads a visual thumbnail for the
3940 * given {@link Uri}. Internally calls
3941 * {@link ContentProvider#openTypedAssetFile} on the remote provider, but
3942 * also defensively resizes any returned content to match the requested
3943 * target size.
3944 *
3945 * @param uri The item that should be visualized as a thumbnail.
3946 * @param size The target area on the screen where this thumbnail will be
3947 * shown. This is passed to the provider as {@link #EXTRA_SIZE}
3948 * to help it avoid downloading or generating heavy resources.
3949 * @param signal A signal to cancel the operation in progress.
3950 * @return Valid {@link Bitmap} which is a visual thumbnail.
3951 * @throws IOException If any trouble was encountered while generating or
3952 * loading the thumbnail, or if
3953 * {@link CancellationSignal#cancel()} was invoked.
3954 */
3955 public @NonNull Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size,
3956 @Nullable CancellationSignal signal) throws IOException {
Jeff Sharkey448c1ea2019-03-29 18:10:57 -06003957 return loadThumbnail(this, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003958 }
3959
3960 /** {@hide} */
Jeff Sharkeye770d222018-12-07 15:15:59 -07003961 public static Bitmap loadThumbnail(@NonNull ContentInterface content, @NonNull Uri uri,
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003962 @NonNull Size size, @Nullable CancellationSignal signal, int allocator)
3963 throws IOException {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003964 Objects.requireNonNull(content);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003965 Objects.requireNonNull(uri);
3966 Objects.requireNonNull(size);
3967
3968 // Convert to Point, since that's what the API is defined as
3969 final Bundle opts = new Bundle();
3970 opts.putParcelable(EXTRA_SIZE, Point.convert(size));
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003971 final Int32Ref orientation = new Int32Ref(0);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003972
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003973 Bitmap bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(() -> {
3974 final AssetFileDescriptor afd = content.openTypedAssetFile(uri, "image/*", opts,
3975 signal);
3976 final Bundle extras = afd.getExtras();
3977 orientation.value = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
3978 return afd;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003979 }), (ImageDecoder decoder, ImageInfo info, Source source) -> {
Tony Huangde421372019-10-25 18:15:24 +08003980 decoder.setAllocator(allocator);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003981
Tony Huangde421372019-10-25 18:15:24 +08003982 // One last-ditch check to see if we've been canceled.
3983 if (signal != null) signal.throwIfCanceled();
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003984
Tony Huangde421372019-10-25 18:15:24 +08003985 // We requested a rough thumbnail size, but the remote size may have
3986 // returned something giant, so defensively scale down as needed.
3987 final int widthSample = info.getSize().getWidth() / size.getWidth();
3988 final int heightSample = info.getSize().getHeight() / size.getHeight();
3989 final int sample = Math.max(widthSample, heightSample);
3990 if (sample > 1) {
3991 decoder.setTargetSampleSize(sample);
3992 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003993 });
Ivan Chiang6da7b0a2019-04-11 20:01:17 +08003994
3995 // Transform the bitmap if requested. We use a side-channel to
3996 // communicate the orientation, since EXIF thumbnails don't contain
3997 // the rotation flags of the original image.
3998 if (orientation.value != 0) {
3999 final int width = bitmap.getWidth();
4000 final int height = bitmap.getHeight();
4001
4002 final Matrix m = new Matrix();
4003 m.setRotate(orientation.value, width / 2, height / 2);
4004 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
4005 }
4006
4007 return bitmap;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06004008 }
Makoto Onukiee93ad22018-10-18 16:24:13 -07004009
4010 /** {@hide} */
4011 public static void onDbCorruption(String tag, String message, Throwable stacktrace) {
4012 try {
4013 getContentService().onDbCorruption(tag, message, Log.getStackTraceString(stacktrace));
4014 } catch (RemoteException e) {
4015 e.rethrowFromSystemServer();
4016 }
4017 }
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06004018
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07004019 /**
4020 * Decode a path generated by {@link #encodeToFile(Uri)} back into
4021 * the original {@link Uri}.
4022 * <p>
4023 * This is used to offer a way to intercept filesystem calls in
4024 * {@link ContentProvider} unaware code and redirect them to a
4025 * {@link ContentProvider} when they attempt to use {@code _DATA} columns
4026 * that are otherwise deprecated.
4027 *
4028 * @hide
4029 */
4030 @SystemApi
Jeff Sharkey9ed18342020-03-08 12:42:27 -06004031 @TestApi
4032 // We can't accept an already-opened FD here, since these methods are
4033 // rewriting actual filesystem paths
4034 @SuppressLint("StreamFiles")
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07004035 public static @NonNull Uri decodeFromFile(@NonNull File file) {
4036 return translateDeprecatedDataPath(file.getAbsolutePath());
4037 }
4038
4039 /**
4040 * Encode a {@link Uri} into an opaque filesystem path which can then be
4041 * resurrected by {@link #decodeFromFile(File)}.
4042 * <p>
4043 * This is used to offer a way to intercept filesystem calls in
4044 * {@link ContentProvider} unaware code and redirect them to a
4045 * {@link ContentProvider} when they attempt to use {@code _DATA} columns
4046 * that are otherwise deprecated.
4047 *
4048 * @hide
4049 */
4050 @SystemApi
Jeff Sharkey9ed18342020-03-08 12:42:27 -06004051 @TestApi
4052 // We can't accept an already-opened FD here, since these methods are
4053 // rewriting actual filesystem paths
4054 @SuppressLint("StreamFiles")
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07004055 public static @NonNull File encodeToFile(@NonNull Uri uri) {
4056 return new File(translateDeprecatedDataPath(uri));
4057 }
4058
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06004059 /** {@hide} */
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07004060 public static @NonNull Uri translateDeprecatedDataPath(@NonNull String path) {
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06004061 final String ssp = "//" + path.substring(DEPRECATE_DATA_PREFIX.length());
4062 return Uri.parse(new Uri.Builder().scheme(SCHEME_CONTENT)
4063 .encodedOpaquePart(ssp).build().toString());
4064 }
4065
4066 /** {@hide} */
Jeff Sharkey197fe1f2020-01-07 22:06:37 -07004067 public static @NonNull String translateDeprecatedDataPath(@NonNull Uri uri) {
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06004068 return DEPRECATE_DATA_PREFIX + uri.getEncodedSchemeSpecificPart().substring(2);
4069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070}