blob: 1e4b1e7b45c6bfcdf0ffb6d592531993d7a5daeb [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
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080019import android.accounts.Account;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070020import android.annotation.IntDef;
Jeff Sharkey673db442015-06-11 19:30:57 -070021import android.annotation.NonNull;
Scott Kennedy9f78f652015-03-01 15:29:25 -080022import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070023import android.annotation.RequiresPermission;
Ivan Chiangfd3415c2018-12-07 18:22:50 +080024import android.annotation.SystemApi;
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -070025import android.annotation.TestApi;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010026import android.annotation.UnsupportedAppUsage;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070027import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080028import android.app.ActivityManager;
Jeff Sharkey66a017b2013-01-17 18:18:22 -080029import android.app.ActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070030import android.app.AppGlobals;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070031import android.app.UriGrantsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.res.AssetFileDescriptor;
34import android.content.res.Resources;
35import android.database.ContentObserver;
Jeff Brown825c5132011-10-12 16:11:30 -070036import android.database.CrossProcessCursorWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.database.IContentObserver;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060039import android.graphics.Bitmap;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060040import android.graphics.ImageDecoder;
41import android.graphics.ImageDecoder.ImageInfo;
42import android.graphics.ImageDecoder.Source;
Jeff Sharkey5b836f22014-08-27 14:46:32 -070043import android.graphics.Point;
Daniel Nishic29d2b02016-06-30 12:20:41 -070044import android.graphics.drawable.Drawable;
Jeff Sharkey806bece2019-02-19 14:42:55 -070045import android.graphics.drawable.Icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
47import android.os.Bundle;
Jeff Browna7771df2012-05-07 20:06:46 -070048import android.os.CancellationSignal;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070049import android.os.DeadObjectException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070050import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070051import android.os.ICancellationSignal;
52import android.os.OperationCanceledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.ParcelFileDescriptor;
54import android.os.RemoteException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070055import android.os.ServiceManager;
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -080056import android.os.SystemClock;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070057import android.os.UserHandle;
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060058import android.os.storage.StorageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.text.TextUtils;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080060import android.util.EventLog;
Dianne Hackborn231cc602009-04-27 17:10:36 -070061import android.util.Log;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060062import android.util.Size;
Jeff Sharkey08da7a12013-08-11 20:53:18 -070063
Daniel Nishic29d2b02016-06-30 12:20:41 -070064import com.android.internal.util.MimeIconUtils;
Jeff Sharkey673db442015-06-11 19:30:57 -070065import com.android.internal.util.Preconditions;
66
Jeff Sharkey60cfad82016-01-05 17:30:57 -070067import dalvik.system.CloseGuard;
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.io.File;
70import java.io.FileInputStream;
71import java.io.FileNotFoundException;
72import java.io.IOException;
73import java.io.InputStream;
74import java.io.OutputStream;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070075import java.lang.annotation.Retention;
76import java.lang.annotation.RetentionPolicy;
Gilles Debunne03f02922010-06-09 14:11:45 -070077import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.util.List;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060079import java.util.Objects;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080080import java.util.Random;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070081import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083/**
84 * This class provides applications access to the content model.
Joe Fernandez558459f2011-10-13 16:47:36 -070085 *
86 * <div class="special reference">
87 * <h3>Developer Guides</h3>
88 * <p>For more information about using a ContentResolver with content providers, read the
89 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
90 * developer guide.</p>
Ng Zhi Anb837d0b2019-01-24 21:39:24 -080091 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -070093public abstract class ContentResolver implements ContentInterface {
Fred Quintanaac9385e2009-06-22 18:00:59 -070094 /**
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060095 * Enables logic that supports deprecation of {@code _data} columns,
96 * typically by replacing values with fake paths that the OS then offers to
97 * redirect to {@link #openFileDescriptor(Uri, String)}, which developers
98 * should be using directly.
99 *
100 * @hide
101 */
Jeff Sharkey10ec9d82018-11-28 14:52:45 -0700102 public static final boolean DEPRECATE_DATA_COLUMNS = StorageManager.hasIsolatedStorage();
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600103
104 /**
105 * Special filesystem path prefix which indicates that a path should be
106 * treated as a {@code content://} {@link Uri} when
107 * {@link #DEPRECATE_DATA_COLUMNS} is enabled.
108 * <p>
109 * The remainder of the path after this prefix is a
110 * {@link Uri#getSchemeSpecificPart()} value, which includes authority, path
111 * segments, and query parameters.
112 *
113 * @hide
114 */
115 public static final String DEPRECATE_DATA_PREFIX = "/mnt/content/";
116
117 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700118 * @deprecated instead use
119 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
120 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700121 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -0700122 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700123
124 /**
125 * If this extra is set to true, the sync request will be scheduled
126 * at the front of the sync request queue and without any delay
127 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700129
Fred Quintanaac9385e2009-06-22 18:00:59 -0700130 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000131 * If this extra is set to true, the sync request will be scheduled
132 * only when the device is plugged in. This is equivalent to calling
133 * setRequiresCharging(true) on {@link SyncRequest}.
134 */
135 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
136
137 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700138 * @deprecated instead use
139 * {@link #SYNC_EXTRAS_MANUAL}
140 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700141 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800143
144 /**
145 * If this extra is set to true then the sync settings (like getSyncAutomatically())
146 * are ignored by the sync scheduler.
147 */
148 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
149
150 /**
151 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
152 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
153 * retries will still honor the backoff.
154 */
155 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
156
157 /**
158 * If this extra is set to true then the request will not be retried if it fails.
159 */
160 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
161
162 /**
163 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
164 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
165 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700166 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800167
Georgi Nikolovb3395572013-06-18 18:27:31 -0700168 /**
169 * Indicates that this sync is intended to only upload local changes to the server.
170 * For example, this will be set to true if the sync is initiated by a call to
171 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
172 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700174
175 /**
176 * Indicates that the sync adapter should proceed with the delete operations,
177 * even if it determines that there are too many.
178 * See {@link SyncResult#tooManyDeletions}
179 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700181
182 /**
183 * Indicates that the sync adapter should not proceed with the delete operations,
184 * if it determines that there are too many.
185 * See {@link SyncResult#tooManyDeletions}
186 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
188
Matthew Williamsfa774182013-06-18 15:44:11 -0700189 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
190 /** {@hide} User-specified flag for expected upload size. */
191 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
192
193 /** {@hide} User-specified flag for expected download size. */
194 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
195
196 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
197 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
198
199 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700200 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700201
Fred Quintana4a6679b2009-08-17 13:05:39 -0700202 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700203 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800204 *
205 * Only the system and the shell user can set it.
206 *
207 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
208 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700209 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800210
211 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700212 * Set by the SyncManager to request that the SyncAdapter initialize itself for
213 * the given account/authority pair. One required initialization step is to
214 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
215 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
216 * do a full sync, though it is allowed to do so.
217 */
218 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
219
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800220 /** @hide */
221 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
222 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 public static final String SCHEME_CONTENT = "content";
225 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
226 public static final String SCHEME_FILE = "file";
227
228 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700229 * An extra {@link Point} describing the optimal size for a requested image
230 * resource, in pixels. If a provider has multiple sizes of the image, it
231 * should return the image closest to this size.
232 *
233 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
234 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
235 * CancellationSignal)
236 */
237 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
238
239 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800240 * An extra boolean describing whether a particular provider supports refresh
241 * or not. If a provider supports refresh, it should include this key in its
242 * returned Cursor as part of its query call.
243 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800244 */
245 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
246
247 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800248 * Key for an SQL style selection string that may be present in the query Bundle argument
249 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
250 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800251 *
252 * <p>Clients should never include user supplied values directly in the selection string,
253 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
254 * should use standard placeholder notation to represent values in a selection string,
255 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
256 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800257 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
258 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700259 *
260 * @see #QUERY_ARG_SORT_COLUMNS
261 * @see #QUERY_ARG_SORT_DIRECTION
262 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800263 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700264 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800265
266 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800267 * Key for SQL selection string arguments list.
268 *
269 * <p>Clients should never include user supplied values directly in the selection string,
270 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
271 * should use standard placeholder notation to represent values in a selection string,
272 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
273 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800274 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
275 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700276 *
277 * @see #QUERY_ARG_SORT_COLUMNS
278 * @see #QUERY_ARG_SORT_DIRECTION
279 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800280 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700281 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
282 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800283
284 /**
285 * Key for an SQL style sort string that may be present in the query Bundle argument
286 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
287 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800288 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800289 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
290 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700291 *
292 * @see #QUERY_ARG_SORT_COLUMNS
293 * @see #QUERY_ARG_SORT_DIRECTION
294 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800295 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700296 public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800297
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600298 /** {@hide} */
299 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
300 /** {@hide} */
301 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
302 /** {@hide} */
303 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
304
Steve McKay29c3f682016-12-16 14:52:59 -0800305 /**
Steve McKayd7ece9f2017-01-12 16:59:59 -0800306 * Specifies the list of columns against which to sort results. When first column values
307 * are identical, records are then sorted based on second column values, and so on.
Steve McKay29c3f682016-12-16 14:52:59 -0800308 *
309 * <p>Columns present in this list must also be included in the projection
310 * supplied to {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
311 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800312 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKay29c3f682016-12-16 14:52:59 -0800313 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700314 * <li>{@link ContentProvider} implementations: When preparing data in
315 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort columns
316 * is reflected in the returned Cursor, it is strongly recommended that
317 * {@link #QUERY_ARG_SORT_COLUMNS} then be included in the array of honored arguments
318 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800319 *
320 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
321 * arguments {@link Bundle}, the Content framework will attempt to synthesize
322 * an QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800323 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700324 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800325
326 /**
327 * Specifies desired sort order. When unspecified a provider may provide a default
328 * sort direction, or choose to return unsorted results.
329 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800330 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
331 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700332 * <li>{@link ContentProvider} implementations: When preparing data in
333 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
334 * is reflected in the returned Cursor, it is strongly recommended that
335 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
336 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800337 *
338 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
339 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700340 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800341 *
342 * @see #QUERY_SORT_DIRECTION_ASCENDING
343 * @see #QUERY_SORT_DIRECTION_DESCENDING
344 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700345 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800346
347 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700348 * Allows client to specify a hint to the provider declaring which collation
Steve McKay29c3f682016-12-16 14:52:59 -0800349 * to use when sorting text values.
350 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700351 * <p>Providers may support custom collators. When specifying a custom collator
352 * the value is determined by the Provider.
Steve McKay29c3f682016-12-16 14:52:59 -0800353 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700354 * <li>{@link ContentProvider} implementations: When preparing data in
355 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort collation
356 * is reflected in the returned Cursor, it is strongly recommended that
357 * {@link #QUERY_ARG_SORT_COLLATION} then be included in the array of honored arguments
358 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKay29c3f682016-12-16 14:52:59 -0800359 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700360 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
Steve McKayd7ece9f2017-01-12 16:59:59 -0800361 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700362 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800363 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700364 * @see java.text.Collator#PRIMARY
365 * @see java.text.Collator#SECONDARY
366 * @see java.text.Collator#TERTIARY
367 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800368 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700369 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800370
Steve McKay415f41b2017-02-01 13:38:25 -0800371 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700372 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800373 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700374 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800375 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
376 * when any QUERY_ARG_SORT* value was honored during the preparation of the
377 * results {@link Cursor}.
378 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700379 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
380 *
381 * @see #QUERY_ARG_SORT_COLUMNS
382 * @see #QUERY_ARG_SORT_DIRECTION
383 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800384 */
385 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
386
Steve McKay29c3f682016-12-16 14:52:59 -0800387 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700388 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800389 QUERY_SORT_DIRECTION_ASCENDING,
390 QUERY_SORT_DIRECTION_DESCENDING
391 })
392 @Retention(RetentionPolicy.SOURCE)
393 public @interface SortDirection {}
394 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
395 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
396
397 /**
398 * @see {@link java.text.Collector} for details on respective collation strength.
399 * @hide
400 */
401 @IntDef(flag = false, value = {
402 java.text.Collator.PRIMARY,
403 java.text.Collator.SECONDARY,
404 java.text.Collator.TERTIARY,
405 java.text.Collator.IDENTICAL
406 })
407 @Retention(RetentionPolicy.SOURCE)
408 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800409
410 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700411 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800412 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700413 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800414
415 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700416 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800417 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700418 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800419
420 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700421 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
422 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800423 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800424 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700425 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800426 *
427 * <p>Providers having returned -1 in a previous query are recommended to
428 * send content change notification once (if) full recordset size becomes
429 * known.
430 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700431 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800432
433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * This is the Android platform's base MIME type for a content: URI
435 * containing a Cursor of a single item. Applications should use this
436 * as the base type along with their own sub-type of their content: URIs
437 * that represent a particular item. For example, hypothetical IMAP email
438 * client may have a URI
439 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
440 * message in the inbox, whose MIME type would be reported as
441 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
444 */
445 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
448 * This is the Android platform's base MIME type for a content: URI
449 * containing a Cursor of zero or more items. Applications should use this
450 * as the base type along with their own sub-type of their content: URIs
451 * that represent a directory of items. For example, hypothetical IMAP email
452 * client may have a URI
453 * <code>content://com.company.provider.imap/inbox</code> for all of the
454 * messages in its inbox, whose MIME type would be reported as
455 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * <p>Note how the base MIME type varies between this and
458 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
459 * one single item or multiple items in the data set, while the sub-type
460 * remains the same because in either case the data structure contained
461 * in the cursor is the same.
462 */
463 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700464
Matt Caseybd7bcf02014-02-05 15:51:39 -0800465 /**
466 * This is the Android platform's generic MIME type to match any MIME
467 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
468 * {@code SUB_TYPE} is the sub-type of the application-dependent
469 * content, e.g., "audio", "video", "playlist".
470 */
471 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
472
Jeff Sharkey91e3cd42018-08-27 18:03:33 -0600473 /**
474 * Default MIME type for files whose type is otherwise unknown.
475 * @hide
476 */
477 public static final String MIME_TYPE_DEFAULT = "application/octet-stream";
478
Fred Quintanaac9385e2009-06-22 18:00:59 -0700479 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100480 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700481 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
482 /** @hide */
483 public static final int SYNC_ERROR_AUTHENTICATION = 2;
484 /** @hide */
485 public static final int SYNC_ERROR_IO = 3;
486 /** @hide */
487 public static final int SYNC_ERROR_PARSE = 4;
488 /** @hide */
489 public static final int SYNC_ERROR_CONFLICT = 5;
490 /** @hide */
491 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
492 /** @hide */
493 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
494 /** @hide */
495 public static final int SYNC_ERROR_INTERNAL = 8;
496
Alon Albert57286f92012-10-09 14:21:38 -0700497 private static final String[] SYNC_ERROR_NAMES = new String[] {
498 "already-in-progress",
499 "authentication-error",
500 "io-error",
501 "parse-error",
502 "conflict",
503 "too-many-deletions",
504 "too-many-retries",
505 "internal-error",
506 };
507
508 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800509 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700510 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
511 return String.valueOf(error);
512 }
513 return SYNC_ERROR_NAMES[error - 1];
514 }
515
Alon Albert5c113fa2013-02-07 08:07:32 -0800516 /** @hide */
517 public static int syncErrorStringToInt(String error) {
518 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
519 if (SYNC_ERROR_NAMES[i].equals(error)) {
520 return i + 1;
521 }
522 }
523 if (error != null) {
524 try {
525 return Integer.parseInt(error);
526 } catch (NumberFormatException e) {
527 Log.d(TAG, "error parsing sync error: " + error);
528 }
529 }
530 return 0;
531 }
532
Fred Quintanaac9385e2009-06-22 18:00:59 -0700533 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700534 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700535 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
536 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100537 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700538 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
539 /** @hide */
540 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
541
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700542 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700543 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
544 NOTIFY_SYNC_TO_NETWORK,
545 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS
546 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700547 @Retention(RetentionPolicy.SOURCE)
548 public @interface NotifyFlags {}
549
550 /**
551 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
552 * to the network.
553 */
554 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
555
556 /**
557 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
558 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
559 * using "notify for descendants." The purpose of this is to allow the provide to send
560 * a general notification of "something under X" changed that observers of that specific
561 * URI can receive, while also sending a specific URI under X. It would use this flag
562 * when sending the former, so that observers of "X and descendants" only see the latter.
563 */
564 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
565
Makoto Onuki75ad2492018-03-28 14:42:42 -0700566 /**
567 * No exception, throttled by app standby normally.
568 * @hide
569 */
570 public static final int SYNC_EXEMPTION_NONE = 0;
571
572 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700573 * Exemption given to a sync request made by a foreground app (including
574 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700575 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700576 * At the schedule time, we promote the sync adapter app for a higher bucket:
577 * - If the device is not dozing (so the sync will start right away)
578 * promote to ACTIVE for 1 hour.
579 * - If the device is dozing (so the sync *won't* start right away),
580 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
581 * device comes out of doze.
582 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
583 * so it can schedule and start more syncs without getting throttled, even when the first
584 * operation was canceled and now we're retrying.
585 *
586 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700587 * @hide
588 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700589 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700590
591 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700592 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700593 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
594 * @hide
595 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700596 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700597
598 /** @hide */
599 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
600 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700601 SYNC_EXEMPTION_PROMOTE_BUCKET,
602 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700603 })
604 @Retention(RetentionPolicy.SOURCE)
605 public @interface SyncExemption {}
606
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800607 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800608 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700609 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800610 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800611 private final Random mRandom = new Random(); // guarded by itself
612
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700613 public ContentResolver(@Nullable Context context) {
614 this(context, null);
615 }
616
617 /** {@hide} */
618 public ContentResolver(@Nullable Context context, @Nullable ContentInterface wrapped) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800619 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700620 mPackageName = mContext.getOpPackageName();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000621 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700622 mWrapped = wrapped;
623 }
624
625 /** {@hide} */
626 public static ContentResolver wrap(@NonNull ContentInterface wrapped) {
627 Preconditions.checkNotNull(wrapped);
628
629 return new ContentResolver(null, wrapped) {
630 @Override
631 public void unstableProviderDied(IContentProvider icp) {
632 throw new UnsupportedOperationException();
633 }
634 @Override
635 public boolean releaseUnstableProvider(IContentProvider icp) {
636 throw new UnsupportedOperationException();
637 }
638 @Override
639 public boolean releaseProvider(IContentProvider icp) {
640 throw new UnsupportedOperationException();
641 }
642 @Override
643 protected IContentProvider acquireUnstableProvider(Context c, String name) {
644 throw new UnsupportedOperationException();
645 }
646 @Override
647 protected IContentProvider acquireProvider(Context c, String name) {
648 throw new UnsupportedOperationException();
649 }
650 };
651 }
652
653 /**
654 * Create a {@link ContentResolver} instance that redirects all its methods
655 * to the given {@link ContentProvider}.
656 */
657 public static ContentResolver wrap(@NonNull ContentProvider wrapped) {
658 return wrap((ContentInterface) wrapped);
659 }
660
661 /**
662 * Create a {@link ContentResolver} instance that redirects all its methods
663 * to the given {@link ContentProviderClient}.
664 */
665 public static ContentResolver wrap(@NonNull ContentProviderClient wrapped) {
666 return wrap((ContentInterface) wrapped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668
669 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100670 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700672
673 /**
674 * Providing a default implementation of this, to avoid having to change a
675 * lot of other things, but implementations of ContentResolver should
676 * implement it.
677 *
678 * @hide
679 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100680 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700681 protected IContentProvider acquireExistingProvider(Context c, String name) {
682 return acquireProvider(c, name);
683 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100686 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700688 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100689 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700690 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
691 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100692 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700693 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700694 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100695 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700696 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700698 /** @hide */
699 public void appNotRespondingViaProvider(IContentProvider icp) {
700 throw new UnsupportedOperationException("appNotRespondingViaProvider");
701 }
702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 /**
704 * Return the MIME type of the given content URL.
705 *
706 * @param url A Uri identifying content (either a list or specific type),
707 * using the content:// scheme.
708 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
709 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700710 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700711 public final @Nullable String getType(@NonNull Uri url) {
712 Preconditions.checkNotNull(url, "url");
713
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700714 try {
715 if (mWrapped != null) return mWrapped.getType(url);
716 } catch (RemoteException e) {
717 return null;
718 }
719
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700720 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700721 IContentProvider provider = acquireExistingProvider(url);
722 if (provider != null) {
723 try {
724 return provider.getType(url);
725 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600726 // Arbitrary and not worth documenting, as Activity
727 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700728 return null;
729 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100730 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700731 return null;
732 } finally {
733 releaseProvider(provider);
734 }
735 }
736
737 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 return null;
739 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800742 String type = ActivityManager.getService().getProviderMimeType(
Nicolas Prevot1dddc7f2014-07-07 17:44:58 +0100743 ContentProvider.getUriWithoutUserId(url), resolveUserId(url));
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700744 return type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600746 throw e.rethrowFromSystemServer();
Ola Olsson145e6c42010-12-20 16:45:35 +0100747 } catch (java.lang.Exception e) {
748 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
749 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751 }
752
753 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700754 * Query for the possible MIME types for the representations the given
755 * content URL can be returned when opened as as stream with
756 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
757 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700758 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700759 * data that they contain.
760 *
761 * @param url A Uri identifying content (either a list or specific type),
762 * using the content:// scheme.
763 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500764 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700765 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700766 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700767 * data streams that match the given mimeTypeFilter. If there are none,
768 * null is returned.
769 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700770 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700771 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
772 Preconditions.checkNotNull(url, "url");
773 Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter");
774
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700775 try {
776 if (mWrapped != null) return mWrapped.getStreamTypes(url, mimeTypeFilter);
777 } catch (RemoteException e) {
778 return null;
779 }
780
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700781 IContentProvider provider = acquireProvider(url);
782 if (provider == null) {
783 return null;
784 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700785
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700786 try {
787 return provider.getStreamTypes(url, mimeTypeFilter);
788 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800789 // Arbitrary and not worth documenting, as Activity
790 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700791 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700792 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800793 releaseProvider(provider);
794 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700795 }
796
797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800799 * <p>
800 * For best performance, the caller should follow these guidelines:
801 * <ul>
802 * <li>Provide an explicit projection, to prevent
803 * reading data from storage that aren't going to be used.</li>
804 * <li>Use question mark parameter markers such as 'phone=?' instead of
805 * explicit values in the {@code selection} parameter, so that queries
806 * that differ only by those values will be recognized as the same
807 * for caching purposes.</li>
808 * </ul>
809 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 *
811 * @param uri The URI, using the content:// scheme, for the content to
812 * retrieve.
813 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800814 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 * @param selection A filter declaring which rows to return, formatted as an
816 * SQL WHERE clause (excluding the WHERE itself). Passing null will
817 * return all rows for the given URI.
818 * @param selectionArgs You may include ?s in selection, which will be
819 * replaced by the values from selectionArgs, in the order that they
820 * appear in the selection. The values will be bound as Strings.
821 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
822 * clause (excluding the ORDER BY itself). Passing null will use the
823 * default sort order, which may be unordered.
824 * @return A Cursor object, which is positioned before the first entry, or null
825 * @see Cursor
826 */
Tor Norbye788fc2b2015-07-05 16:10:42 -0700827 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
828 @Nullable String[] projection, @Nullable String selection,
829 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800830 return query(uri, projection, selection, selectionArgs, sortOrder, null);
831 }
832
833 /**
Jeff Brownc64ff372013-10-09 18:50:56 -0700834 * Query the given URI, returning a {@link Cursor} over the result set
835 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800836 * <p>
837 * For best performance, the caller should follow these guidelines:
838 * <ul>
839 * <li>Provide an explicit projection, to prevent
840 * reading data from storage that aren't going to be used.</li>
841 * <li>Use question mark parameter markers such as 'phone=?' instead of
842 * explicit values in the {@code selection} parameter, so that queries
843 * that differ only by those values will be recognized as the same
844 * for caching purposes.</li>
845 * </ul>
846 * </p>
847 *
848 * @param uri The URI, using the content:// scheme, for the content to
849 * retrieve.
850 * @param projection A list of which columns to return. Passing null will
851 * return all columns, which is inefficient.
852 * @param selection A filter declaring which rows to return, formatted as an
853 * SQL WHERE clause (excluding the WHERE itself). Passing null will
854 * return all rows for the given URI.
855 * @param selectionArgs You may include ?s in selection, which will be
856 * replaced by the values from selectionArgs, in the order that they
857 * appear in the selection. The values will be bound as Strings.
858 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
859 * clause (excluding the ORDER BY itself). Passing null will use the
860 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -0800861 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800862 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
863 * when the query is executed.
864 * @return A Cursor object, which is positioned before the first entry, or null
865 * @see Cursor
866 */
Steve McKayea93fe72016-12-02 11:35:35 -0800867 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -0700868 @Nullable String[] projection, @Nullable String selection,
869 @Nullable String[] selectionArgs, @Nullable String sortOrder,
870 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -0800871 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
872 return query(uri, projection, queryArgs, cancellationSignal);
873 }
874
875 /**
876 * Query the given URI, returning a {@link Cursor} over the result set
877 * with support for cancellation.
878 *
879 * <p>For best performance, the caller should follow these guidelines:
880 *
881 * <li>Provide an explicit projection, to prevent reading data from storage
882 * that aren't going to be used.
883 *
Steve McKay415f41b2017-02-01 13:38:25 -0800884 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
885 * the preparation of the result set by including the respective argument keys
886 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
887 * for details.
888 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -0700889 * @see #QUERY_ARG_SORT_COLUMNS
890 * @see #QUERY_ARG_SORT_DIRECTION
891 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800892 *
Steve McKayea93fe72016-12-02 11:35:35 -0800893 * @param uri The URI, using the content:// scheme, for the content to
894 * retrieve.
895 * @param projection A list of which columns to return. Passing null will
896 * return all columns, which is inefficient.
897 * @param queryArgs A Bundle containing any arguments to the query.
898 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
899 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
900 * when the query is executed.
901 * @return A Cursor object, which is positioned before the first entry, or null
902 * @see Cursor
903 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700904 @Override
Steve McKayea93fe72016-12-02 11:35:35 -0800905 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
906 @Nullable String[] projection, @Nullable Bundle queryArgs,
907 @Nullable CancellationSignal cancellationSignal) {
Jeff Sharkey673db442015-06-11 19:30:57 -0700908 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700909
910 try {
911 if (mWrapped != null) {
912 return mWrapped.query(uri, projection, queryArgs, cancellationSignal);
913 }
914 } catch (RemoteException e) {
915 return null;
916 }
917
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700918 IContentProvider unstableProvider = acquireUnstableProvider(uri);
919 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 return null;
921 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700922 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800923 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800925 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -0800926
Jeff Brown4c1241d2012-02-02 17:05:00 -0800927 ICancellationSignal remoteCancellationSignal = null;
928 if (cancellationSignal != null) {
929 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700930 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -0800931 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -0800932 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700933 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800934 qCursor = unstableProvider.query(mPackageName, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -0800935 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700936 } catch (DeadObjectException e) {
937 // The remote process has died... but we only hold an unstable
938 // reference though, so we might recover!!! Let's try!!!!
939 // This is exciting!!1!!1!!!!1
940 unstableProviderDied(unstableProvider);
941 stableProvider = acquireProvider(uri);
942 if (stableProvider == null) {
943 return null;
944 }
Steve McKayea93fe72016-12-02 11:35:35 -0800945 qCursor = stableProvider.query(
946 mPackageName, uri, projection, queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700947 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800948 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 return null;
950 }
Jeff Brownc21b5a02013-01-07 17:15:12 -0800951
952 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -0700953 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800954 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -0800955 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -0800956
957 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700958 final IContentProvider provider = (stableProvider != null) ? stableProvider
959 : acquireProvider(uri);
960 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700961 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800962 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700963 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800965 // Arbitrary and not worth documenting, as Activity
966 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700968 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -0800969 if (qCursor != null) {
970 qCursor.close();
971 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700972 if (cancellationSignal != null) {
973 cancellationSignal.setRemote(null);
974 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700975 if (unstableProvider != null) {
976 releaseUnstableProvider(unstableProvider);
977 }
978 if (stableProvider != null) {
979 releaseProvider(stableProvider);
980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
983
Fred Quintana89437372009-05-15 15:10:40 -0700984 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700985 * Transform the given <var>url</var> to a canonical representation of
986 * its referenced resource, which can be used across devices, persisted,
987 * backed up and restored, etc. The returned Uri is still a fully capable
988 * Uri for use with its content provider, allowing you to do all of the
989 * same content provider operations as with the original Uri --
990 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
991 * only difference in behavior between the original and new Uris is that
992 * the content provider may need to do some additional work at each call
993 * using it to resolve it to the correct resource, especially if the
994 * canonical Uri has been moved to a different environment.
995 *
996 * <p>If you are moving a canonical Uri between environments, you should
997 * perform another call to {@link #canonicalize} with that original Uri to
998 * re-canonicalize it for the current environment. Alternatively, you may
999 * want to use {@link #uncanonicalize} to transform it to a non-canonical
1000 * Uri that works only in the current environment but potentially more
1001 * efficiently than the canonical representation.</p>
1002 *
1003 * @param url The {@link Uri} that is to be transformed to a canonical
1004 * representation. Like all resolver calls, the input can be either
1005 * a non-canonical or canonical Uri.
1006 *
1007 * @return Returns the official canonical representation of <var>url</var>,
1008 * or null if the content provider does not support a canonical representation
1009 * of the given Uri. Many providers may not support canonicalization of some
1010 * or all of their Uris.
1011 *
1012 * @see #uncanonicalize
1013 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001014 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001015 public final @Nullable Uri canonicalize(@NonNull Uri url) {
1016 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001017
1018 try {
1019 if (mWrapped != null) return mWrapped.canonicalize(url);
1020 } catch (RemoteException e) {
1021 return null;
1022 }
1023
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001024 IContentProvider provider = acquireProvider(url);
1025 if (provider == null) {
1026 return null;
1027 }
1028
1029 try {
1030 return provider.canonicalize(mPackageName, url);
1031 } catch (RemoteException e) {
1032 // Arbitrary and not worth documenting, as Activity
1033 // Manager will kill this process shortly anyway.
1034 return null;
1035 } finally {
1036 releaseProvider(provider);
1037 }
1038 }
1039
1040 /**
1041 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
1042 * it to its local non-canonical form. This can be useful in some cases where
1043 * you know that you will only be using the Uri in the current environment and
1044 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001045 * provider or want to verify that the referenced data exists at all in the
1046 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001047 *
1048 * @param url The canonical {@link Uri} that is to be convered back to its
1049 * non-canonical form.
1050 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001051 * @return Returns the non-canonical representation of <var>url</var>. This will
1052 * return null if data identified by the canonical Uri can not be found in
1053 * the current environment; callers must always check for null and deal with
1054 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001055 *
1056 * @see #canonicalize
1057 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001058 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001059 public final @Nullable Uri uncanonicalize(@NonNull Uri url) {
1060 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001061
1062 try {
1063 if (mWrapped != null) return mWrapped.uncanonicalize(url);
1064 } catch (RemoteException e) {
1065 return null;
1066 }
1067
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001068 IContentProvider provider = acquireProvider(url);
1069 if (provider == null) {
1070 return null;
1071 }
1072
1073 try {
1074 return provider.uncanonicalize(mPackageName, url);
1075 } catch (RemoteException e) {
1076 // Arbitrary and not worth documenting, as Activity
1077 // Manager will kill this process shortly anyway.
1078 return null;
1079 } finally {
1080 releaseProvider(provider);
1081 }
1082 }
1083
1084 /**
Ben Lin1cf454f2016-11-10 13:50:54 -08001085 * This allows clients to request an explicit refresh of content identified by {@code uri}.
1086 * <p>
1087 * Client code should only invoke this method when there is a strong indication (such as a user
1088 * initiated pull to refresh gesture) that the content is stale.
1089 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -08001090 *
Ben Lin2b64a882016-11-11 15:24:58 -08001091 * @param url The Uri identifying the data to refresh.
Ben Lin1cf454f2016-11-10 13:50:54 -08001092 * @param args Additional options from the client. The definitions of these are specific to the
1093 * content provider being called.
1094 * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if
1095 * none. For example, if you called refresh on a particular uri, you should call
1096 * {@link CancellationSignal#throwIfCanceled()} to check whether the client has
1097 * canceled the refresh request.
1098 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -08001099 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001100 @Override
Ben Lin1cf454f2016-11-10 13:50:54 -08001101 public final boolean refresh(@NonNull Uri url, @Nullable Bundle args,
1102 @Nullable CancellationSignal cancellationSignal) {
1103 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001104
1105 try {
1106 if (mWrapped != null) return mWrapped.refresh(url, args, cancellationSignal);
1107 } catch (RemoteException e) {
1108 return false;
1109 }
1110
Ben Lin1cf454f2016-11-10 13:50:54 -08001111 IContentProvider provider = acquireProvider(url);
1112 if (provider == null) {
1113 return false;
1114 }
1115
1116 try {
1117 ICancellationSignal remoteCancellationSignal = null;
1118 if (cancellationSignal != null) {
1119 cancellationSignal.throwIfCanceled();
1120 remoteCancellationSignal = provider.createCancellationSignal();
1121 cancellationSignal.setRemote(remoteCancellationSignal);
1122 }
1123 return provider.refresh(mPackageName, url, args, remoteCancellationSignal);
1124 } catch (RemoteException e) {
1125 // Arbitrary and not worth documenting, as Activity
1126 // Manager will kill this process shortly anyway.
1127 return false;
1128 } finally {
1129 releaseProvider(provider);
1130 }
1131 }
1132
1133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 * Open a stream on to the content associated with a content URI. If there
1135 * is no data associated with the URI, FileNotFoundException is thrown.
1136 *
1137 * <h5>Accepts the following URI schemes:</h5>
1138 * <ul>
1139 * <li>content ({@link #SCHEME_CONTENT})</li>
1140 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1141 * <li>file ({@link #SCHEME_FILE})</li>
1142 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1145 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 * @param uri The desired URI.
1148 * @return InputStream
1149 * @throws FileNotFoundException if the provided URI could not be opened.
1150 * @see #openAssetFileDescriptor(Uri, String)
1151 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001152 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 throws FileNotFoundException {
Jeff Sharkey673db442015-06-11 19:30:57 -07001154 Preconditions.checkNotNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 String scheme = uri.getScheme();
1156 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1157 // Note: left here to avoid breaking compatibility. May be removed
1158 // with sufficient testing.
1159 OpenResourceIdResult r = getResourceId(uri);
1160 try {
1161 InputStream stream = r.r.openRawResource(r.id);
1162 return stream;
1163 } catch (Resources.NotFoundException ex) {
1164 throw new FileNotFoundException("Resource does not exist: " + uri);
1165 }
1166 } else if (SCHEME_FILE.equals(scheme)) {
1167 // Note: left here to avoid breaking compatibility. May be removed
1168 // with sufficient testing.
1169 return new FileInputStream(uri.getPath());
1170 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001171 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 try {
1173 return fd != null ? fd.createInputStream() : null;
1174 } catch (IOException e) {
1175 throw new FileNotFoundException("Unable to create stream");
1176 }
1177 }
1178 }
1179
1180 /**
1181 * Synonym for {@link #openOutputStream(Uri, String)
1182 * openOutputStream(uri, "w")}.
1183 * @throws FileNotFoundException if the provided URI could not be opened.
1184 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001185 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 throws FileNotFoundException {
1187 return openOutputStream(uri, "w");
1188 }
1189
1190 /**
1191 * Open a stream on to the content associated with a content URI. If there
1192 * is no data associated with the URI, FileNotFoundException is thrown.
1193 *
1194 * <h5>Accepts the following URI schemes:</h5>
1195 * <ul>
1196 * <li>content ({@link #SCHEME_CONTENT})</li>
1197 * <li>file ({@link #SCHEME_FILE})</li>
1198 * </ul>
1199 *
1200 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1201 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 * @param uri The desired URI.
1204 * @param mode May be "w", "wa", "rw", or "rwt".
1205 * @return OutputStream
1206 * @throws FileNotFoundException if the provided URI could not be opened.
1207 * @see #openAssetFileDescriptor(Uri, String)
1208 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001209 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001211 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 try {
1213 return fd != null ? fd.createOutputStream() : null;
1214 } catch (IOException e) {
1215 throw new FileNotFoundException("Unable to create stream");
1216 }
1217 }
1218
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001219 @Override
1220 public final @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
1221 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001222 try {
1223 if (mWrapped != null) return mWrapped.openFile(uri, mode, signal);
1224 } catch (RemoteException e) {
1225 return null;
1226 }
1227
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001228 return openFileDescriptor(uri, mode, signal);
1229 }
1230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001232 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1234 * underlying {@link ContentProvider#openFile}
1235 * ContentProvider.openFile()} method, so will <em>not</em> work with
1236 * providers that return sub-sections of files. If at all possible,
1237 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1238 * will receive a FileNotFoundException exception if the provider returns a
1239 * sub-section of a file.
1240 *
1241 * <h5>Accepts the following URI schemes:</h5>
1242 * <ul>
1243 * <li>content ({@link #SCHEME_CONTENT})</li>
1244 * <li>file ({@link #SCHEME_FILE})</li>
1245 * </ul>
1246 *
1247 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1248 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001249 * <p>
1250 * If opening with the exclusive "r" or "w" modes, the returned
1251 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1252 * of data. Opening with the "rw" mode implies a file on disk that supports
1253 * seeking. If possible, always use an exclusive mode to give the underlying
1254 * {@link ContentProvider} the most flexibility.
1255 * <p>
1256 * If you are writing a file, and need to communicate an error to the
1257 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 * @param uri The desired URI to open.
1260 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1261 * ContentProvider.openFile}.
1262 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1263 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001264 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 * file exists under the URI or the mode is invalid.
1266 * @see #openAssetFileDescriptor(Uri, String)
1267 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001268 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1269 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001270 return openFileDescriptor(uri, mode, null);
1271 }
1272
1273 /**
1274 * Open a raw file descriptor to access data under a URI. This
1275 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1276 * underlying {@link ContentProvider#openFile}
1277 * ContentProvider.openFile()} method, so will <em>not</em> work with
1278 * providers that return sub-sections of files. If at all possible,
1279 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1280 * will receive a FileNotFoundException exception if the provider returns a
1281 * sub-section of a file.
1282 *
1283 * <h5>Accepts the following URI schemes:</h5>
1284 * <ul>
1285 * <li>content ({@link #SCHEME_CONTENT})</li>
1286 * <li>file ({@link #SCHEME_FILE})</li>
1287 * </ul>
1288 *
1289 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1290 * on these schemes.
1291 * <p>
1292 * If opening with the exclusive "r" or "w" modes, the returned
1293 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1294 * of data. Opening with the "rw" mode implies a file on disk that supports
1295 * seeking. If possible, always use an exclusive mode to give the underlying
1296 * {@link ContentProvider} the most flexibility.
1297 * <p>
1298 * If you are writing a file, and need to communicate an error to the
1299 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1300 *
1301 * @param uri The desired URI to open.
1302 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1303 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001304 * @param cancellationSignal A signal to cancel the operation in progress,
1305 * or null if none. If the operation is canceled, then
1306 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001307 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1308 * own this descriptor and are responsible for closing it when done.
1309 * @throws FileNotFoundException Throws FileNotFoundException if no
1310 * file exists under the URI or the mode is invalid.
1311 * @see #openAssetFileDescriptor(Uri, String)
1312 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001313 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1314 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1315 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001316 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (afd == null) {
1318 return null;
1319 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 if (afd.getDeclaredLength() < 0) {
1322 // This is a full file!
1323 return afd.getParcelFileDescriptor();
1324 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 // Client can't handle a sub-section of a file, so close what
1327 // we got and bail with an exception.
1328 try {
1329 afd.close();
1330 } catch (IOException e) {
1331 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 throw new FileNotFoundException("Not a whole file");
1334 }
1335
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001336 @Override
1337 public final @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
1338 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001339 try {
1340 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, signal);
1341 } catch (RemoteException e) {
1342 return null;
1343 }
1344
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001345 return openAssetFileDescriptor(uri, mode, signal);
1346 }
1347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001349 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001351 * 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 -08001352 *
1353 * <h5>Accepts the following URI schemes:</h5>
1354 * <ul>
1355 * <li>content ({@link #SCHEME_CONTENT})</li>
1356 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1357 * <li>file ({@link #SCHEME_FILE})</li>
1358 * </ul>
1359 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1360 * <p>
1361 * A Uri object can be used to reference a resource in an APK file. The
1362 * Uri should be one of the following formats:
1363 * <ul>
1364 * <li><code>android.resource://package_name/id_number</code><br/>
1365 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1366 * For example <code>com.example.myapp</code><br/>
1367 * <code>id_number</code> is the int form of the ID.<br/>
1368 * The easiest way to construct this form is
1369 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1370 * </li>
1371 * <li><code>android.resource://package_name/type/name</code><br/>
1372 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1373 * For example <code>com.example.myapp</code><br/>
1374 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1375 * or <code>drawable</code>.
1376 * <code>name</code> is the string form of the resource name. That is, whatever the file
1377 * name was in your res directory, without the type extension.
1378 * The easiest way to construct this form is
1379 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1380 * </li>
1381 * </ul>
1382 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001383 * <p>Note that if this function is called for read-only input (mode is "r")
1384 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001385 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001386 * from any built-in data conversion that a provider implements.
1387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 * @param uri The desired URI to open.
1389 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1390 * ContentProvider.openAssetFile}.
1391 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1392 * own this descriptor and are responsible for closing it when done.
1393 * @throws FileNotFoundException Throws FileNotFoundException of no
1394 * file exists under the URI or the mode is invalid.
1395 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001396 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1397 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001398 return openAssetFileDescriptor(uri, mode, null);
1399 }
1400
1401 /**
1402 * Open a raw file descriptor to access data under a URI. This
1403 * interacts with the underlying {@link ContentProvider#openAssetFile}
1404 * method of the provider associated with the given URI, to retrieve any file stored there.
1405 *
1406 * <h5>Accepts the following URI schemes:</h5>
1407 * <ul>
1408 * <li>content ({@link #SCHEME_CONTENT})</li>
1409 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1410 * <li>file ({@link #SCHEME_FILE})</li>
1411 * </ul>
1412 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1413 * <p>
1414 * A Uri object can be used to reference a resource in an APK file. The
1415 * Uri should be one of the following formats:
1416 * <ul>
1417 * <li><code>android.resource://package_name/id_number</code><br/>
1418 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1419 * For example <code>com.example.myapp</code><br/>
1420 * <code>id_number</code> is the int form of the ID.<br/>
1421 * The easiest way to construct this form is
1422 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1423 * </li>
1424 * <li><code>android.resource://package_name/type/name</code><br/>
1425 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1426 * For example <code>com.example.myapp</code><br/>
1427 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1428 * or <code>drawable</code>.
1429 * <code>name</code> is the string form of the resource name. That is, whatever the file
1430 * name was in your res directory, without the type extension.
1431 * The easiest way to construct this form is
1432 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1433 * </li>
1434 * </ul>
1435 *
1436 * <p>Note that if this function is called for read-only input (mode is "r")
1437 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001438 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001439 * from any built-in data conversion that a provider implements.
1440 *
1441 * @param uri The desired URI to open.
1442 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1443 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001444 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001445 * none. If the operation is canceled, then
1446 * {@link OperationCanceledException} will be thrown.
1447 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1448 * own this descriptor and are responsible for closing it when done.
1449 * @throws FileNotFoundException Throws FileNotFoundException of no
1450 * file exists under the URI or the mode is invalid.
1451 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001452 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1453 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1454 throws FileNotFoundException {
1455 Preconditions.checkNotNull(uri, "uri");
1456 Preconditions.checkNotNull(mode, "mode");
1457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 String scheme = uri.getScheme();
1459 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1460 if (!"r".equals(mode)) {
1461 throw new FileNotFoundException("Can't write resources: " + uri);
1462 }
1463 OpenResourceIdResult r = getResourceId(uri);
1464 try {
1465 return r.r.openRawResourceFd(r.id);
1466 } catch (Resources.NotFoundException ex) {
1467 throw new FileNotFoundException("Resource does not exist: " + uri);
1468 }
1469 } else if (SCHEME_FILE.equals(scheme)) {
1470 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001471 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 return new AssetFileDescriptor(pfd, 0, -1);
1473 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001474 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001475 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001476 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001477 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1478 if (unstableProvider == null) {
1479 throw new FileNotFoundException("No content provider: " + uri);
1480 }
1481 IContentProvider stableProvider = null;
1482 AssetFileDescriptor fd = null;
1483
1484 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001485 ICancellationSignal remoteCancellationSignal = null;
1486 if (cancellationSignal != null) {
1487 cancellationSignal.throwIfCanceled();
1488 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1489 cancellationSignal.setRemote(remoteCancellationSignal);
1490 }
1491
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001492 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001493 fd = unstableProvider.openAssetFile(
1494 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001495 if (fd == null) {
1496 // The provider will be released by the finally{} clause
1497 return null;
1498 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001499 } catch (DeadObjectException e) {
1500 // The remote process has died... but we only hold an unstable
1501 // reference though, so we might recover!!! Let's try!!!!
1502 // This is exciting!!1!!1!!!!1
1503 unstableProviderDied(unstableProvider);
1504 stableProvider = acquireProvider(uri);
1505 if (stableProvider == null) {
1506 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001507 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001508 fd = stableProvider.openAssetFile(
1509 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001510 if (fd == null) {
1511 // The provider will be released by the finally{} clause
1512 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001513 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001514 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001515
1516 if (stableProvider == null) {
1517 stableProvider = acquireProvider(uri);
1518 }
1519 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001520 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001521 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1522 fd.getParcelFileDescriptor(), stableProvider);
1523
1524 // Success! Don't release the provider when exiting, let
1525 // ParcelFileDescriptorInner do that when it is closed.
1526 stableProvider = null;
1527
1528 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1529 fd.getDeclaredLength());
1530
1531 } catch (RemoteException e) {
1532 // Whatever, whatever, we'll go away.
1533 throw new FileNotFoundException(
1534 "Failed opening content provider: " + uri);
1535 } catch (FileNotFoundException e) {
1536 throw e;
1537 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001538 if (cancellationSignal != null) {
1539 cancellationSignal.setRemote(null);
1540 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001541 if (stableProvider != null) {
1542 releaseProvider(stableProvider);
1543 }
1544 if (unstableProvider != null) {
1545 releaseUnstableProvider(unstableProvider);
1546 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001547 }
1548 }
1549 }
1550 }
1551
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001552 @Override
1553 public final @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
1554 @NonNull String mimeTypeFilter, @Nullable Bundle opts,
1555 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001556 try {
1557 if (mWrapped != null) {
1558 return mWrapped.openTypedAssetFile(uri, mimeTypeFilter, opts, signal);
1559 }
1560 } catch (RemoteException e) {
1561 return null;
1562 }
1563
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001564 return openTypedAssetFileDescriptor(uri, mimeTypeFilter, opts, signal);
1565 }
1566
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001567 /**
1568 * Open a raw file descriptor to access (potentially type transformed)
1569 * data from a "content:" URI. This interacts with the underlying
1570 * {@link ContentProvider#openTypedAssetFile} method of the provider
1571 * associated with the given URI, to retrieve retrieve any appropriate
1572 * data stream for the data stored there.
1573 *
1574 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1575 * with "content:" URIs, because content providers are the only facility
1576 * with an associated MIME type to ensure that the returned data stream
1577 * is of the desired type.
1578 *
1579 * <p>All text/* streams are encoded in UTF-8.
1580 *
1581 * @param uri The desired URI to open.
1582 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001583 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001584 * select a type, though there is no way for you to determine what type
1585 * it is returning.
1586 * @param opts Additional provider-dependent options.
1587 * @return Returns a new ParcelFileDescriptor from which you can read the
1588 * data stream from the provider. Note that this may be a pipe, meaning
1589 * you can't seek in it. The only seek you should do is if the
1590 * AssetFileDescriptor contains an offset, to move to that offset before
1591 * reading. You own this descriptor and are responsible for closing it when done.
1592 * @throws FileNotFoundException Throws FileNotFoundException of no
1593 * data of the desired type exists under the URI.
1594 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001595 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1596 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001597 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1598 }
1599
1600 /**
1601 * Open a raw file descriptor to access (potentially type transformed)
1602 * data from a "content:" URI. This interacts with the underlying
1603 * {@link ContentProvider#openTypedAssetFile} method of the provider
1604 * associated with the given URI, to retrieve retrieve any appropriate
1605 * data stream for the data stored there.
1606 *
1607 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1608 * with "content:" URIs, because content providers are the only facility
1609 * with an associated MIME type to ensure that the returned data stream
1610 * is of the desired type.
1611 *
1612 * <p>All text/* streams are encoded in UTF-8.
1613 *
1614 * @param uri The desired URI to open.
1615 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001616 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001617 * select a type, though there is no way for you to determine what type
1618 * it is returning.
1619 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001620 * @param cancellationSignal A signal to cancel the operation in progress,
1621 * or null if none. If the operation is canceled, then
1622 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001623 * @return Returns a new ParcelFileDescriptor from which you can read the
1624 * data stream from the provider. Note that this may be a pipe, meaning
1625 * you can't seek in it. The only seek you should do is if the
1626 * AssetFileDescriptor contains an offset, to move to that offset before
1627 * reading. You own this descriptor and are responsible for closing it when done.
1628 * @throws FileNotFoundException Throws FileNotFoundException of no
1629 * data of the desired type exists under the URI.
1630 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001631 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1632 @NonNull String mimeType, @Nullable Bundle opts,
1633 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
1634 Preconditions.checkNotNull(uri, "uri");
1635 Preconditions.checkNotNull(mimeType, "mimeType");
1636
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001637 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1638 if (unstableProvider == null) {
1639 throw new FileNotFoundException("No content provider: " + uri);
1640 }
1641 IContentProvider stableProvider = null;
1642 AssetFileDescriptor fd = null;
1643
1644 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001645 ICancellationSignal remoteCancellationSignal = null;
1646 if (cancellationSignal != null) {
1647 cancellationSignal.throwIfCanceled();
1648 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1649 cancellationSignal.setRemote(remoteCancellationSignal);
1650 }
1651
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001652 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001653 fd = unstableProvider.openTypedAssetFile(
1654 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001655 if (fd == null) {
1656 // The provider will be released by the finally{} clause
1657 return null;
1658 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001659 } catch (DeadObjectException e) {
1660 // The remote process has died... but we only hold an unstable
1661 // reference though, so we might recover!!! Let's try!!!!
1662 // This is exciting!!1!!1!!!!1
1663 unstableProviderDied(unstableProvider);
1664 stableProvider = acquireProvider(uri);
1665 if (stableProvider == null) {
1666 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001667 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001668 fd = stableProvider.openTypedAssetFile(
1669 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001670 if (fd == null) {
1671 // The provider will be released by the finally{} clause
1672 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001675
1676 if (stableProvider == null) {
1677 stableProvider = acquireProvider(uri);
1678 }
1679 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001680 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001681 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1682 fd.getParcelFileDescriptor(), stableProvider);
1683
1684 // Success! Don't release the provider when exiting, let
1685 // ParcelFileDescriptorInner do that when it is closed.
1686 stableProvider = null;
1687
1688 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1689 fd.getDeclaredLength());
1690
1691 } catch (RemoteException e) {
1692 // Whatever, whatever, we'll go away.
1693 throw new FileNotFoundException(
1694 "Failed opening content provider: " + uri);
1695 } catch (FileNotFoundException e) {
1696 throw e;
1697 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001698 if (cancellationSignal != null) {
1699 cancellationSignal.setRemote(null);
1700 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001701 if (stableProvider != null) {
1702 releaseProvider(stableProvider);
1703 }
1704 if (unstableProvider != null) {
1705 releaseUnstableProvider(unstableProvider);
1706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
1708 }
1709
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001710 /**
1711 * A resource identified by the {@link Resources} that contains it, and a resource id.
1712 *
1713 * @hide
1714 */
1715 public class OpenResourceIdResult {
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001716 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001717 public Resources r;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001718 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001719 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001721
1722 /**
1723 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1724 *
1725 * @hide
1726 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001727 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001728 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 String authority = uri.getAuthority();
1730 Resources r;
1731 if (TextUtils.isEmpty(authority)) {
1732 throw new FileNotFoundException("No authority: " + uri);
1733 } else {
1734 try {
1735 r = mContext.getPackageManager().getResourcesForApplication(authority);
1736 } catch (NameNotFoundException ex) {
1737 throw new FileNotFoundException("No package found for authority: " + uri);
1738 }
1739 }
1740 List<String> path = uri.getPathSegments();
1741 if (path == null) {
1742 throw new FileNotFoundException("No path: " + uri);
1743 }
1744 int len = path.size();
1745 int id;
1746 if (len == 1) {
1747 try {
1748 id = Integer.parseInt(path.get(0));
1749 } catch (NumberFormatException e) {
1750 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
1751 }
1752 } else if (len == 2) {
1753 id = r.getIdentifier(path.get(1), path.get(0), authority);
1754 } else {
1755 throw new FileNotFoundException("More than two path segments: " + uri);
1756 }
1757 if (id == 0) {
1758 throw new FileNotFoundException("No resource found for: " + uri);
1759 }
1760 OpenResourceIdResult res = new OpenResourceIdResult();
1761 res.r = r;
1762 res.id = id;
1763 return res;
1764 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 /**
1767 * Inserts a row into a table at the given URL.
1768 *
1769 * If the content provider supports transactions the insertion will be atomic.
1770 *
1771 * @param url The URL of the table to insert into.
1772 * @param values The initial values for the newly inserted row. The key is the column name for
1773 * the field. Passing an empty ContentValues will create an empty row.
1774 * @return the URL of the newly created row.
1775 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001776 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001777 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1778 @Nullable ContentValues values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001779 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001780
1781 try {
1782 if (mWrapped != null) return mWrapped.insert(url, values);
1783 } catch (RemoteException e) {
1784 return null;
1785 }
1786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 IContentProvider provider = acquireProvider(url);
1788 if (provider == null) {
1789 throw new IllegalArgumentException("Unknown URL " + url);
1790 }
1791 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001792 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001793 Uri createdRow = provider.insert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001794 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001795 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
1796 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001798 // Arbitrary and not worth documenting, as Activity
1799 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 return null;
1801 } finally {
1802 releaseProvider(provider);
1803 }
1804 }
1805
Fred Quintana89437372009-05-15 15:10:40 -07001806 /**
Fred Quintana89437372009-05-15 15:10:40 -07001807 * Applies each of the {@link ContentProviderOperation} objects and returns an array
1808 * of their results. Passes through OperationApplicationException, which may be thrown
1809 * by the call to {@link ContentProviderOperation#apply}.
1810 * If all the applications succeed then a {@link ContentProviderResult} array with the
1811 * same number of elements as the operations will be returned. It is implementation-specific
1812 * how many, if any, operations will have been successfully applied if a call to
1813 * apply results in a {@link OperationApplicationException}.
1814 * @param authority the authority of the ContentProvider to which this batch should be applied
1815 * @param operations the operations to apply
1816 * @return the results of the applications
1817 * @throws OperationApplicationException thrown if an application fails.
1818 * See {@link ContentProviderOperation#apply} for more information.
1819 * @throws RemoteException thrown if a RemoteException is encountered while attempting
1820 * to communicate with a remote provider.
1821 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001822 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001823 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
1824 @NonNull ArrayList<ContentProviderOperation> operations)
1825 throws RemoteException, OperationApplicationException {
1826 Preconditions.checkNotNull(authority, "authority");
1827 Preconditions.checkNotNull(operations, "operations");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001828
1829 try {
1830 if (mWrapped != null) return mWrapped.applyBatch(authority, operations);
1831 } catch (RemoteException e) {
1832 return null;
1833 }
1834
Fred Quintana89437372009-05-15 15:10:40 -07001835 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001836 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07001837 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001838 }
1839 try {
Fred Quintana89437372009-05-15 15:10:40 -07001840 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001841 } finally {
1842 provider.release();
1843 }
1844 }
1845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 /**
1847 * Inserts multiple rows into a table at the given URL.
1848 *
1849 * This function make no guarantees about the atomicity of the insertions.
1850 *
1851 * @param url The URL of the table to insert into.
1852 * @param values The initial values for the newly inserted rows. The key is the column name for
1853 * the field. Passing null will create an empty row.
1854 * @return the number of newly created rows.
1855 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001856 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001857 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
1858 @NonNull ContentValues[] values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001859 Preconditions.checkNotNull(url, "url");
1860 Preconditions.checkNotNull(values, "values");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001861
1862 try {
1863 if (mWrapped != null) return mWrapped.bulkInsert(url, values);
1864 } catch (RemoteException e) {
1865 return 0;
1866 }
1867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 IContentProvider provider = acquireProvider(url);
1869 if (provider == null) {
1870 throw new IllegalArgumentException("Unknown URL " + url);
1871 }
1872 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001873 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001874 int rowsCreated = provider.bulkInsert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001875 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001876 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
1877 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001879 // Arbitrary and not worth documenting, as Activity
1880 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 return 0;
1882 } finally {
1883 releaseProvider(provider);
1884 }
1885 }
1886
1887 /**
1888 * Deletes row(s) specified by a content URI.
1889 *
1890 * If the content provider supports transactions, the deletion will be atomic.
1891 *
1892 * @param url The URL of the row to delete.
1893 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
1894 (excluding the WHERE itself).
1895 * @return The number of rows deleted.
1896 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001897 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001898 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07001899 @Nullable String[] selectionArgs) {
1900 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001901
1902 try {
1903 if (mWrapped != null) return mWrapped.delete(url, where, selectionArgs);
1904 } catch (RemoteException e) {
1905 return 0;
1906 }
1907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 IContentProvider provider = acquireProvider(url);
1909 if (provider == null) {
1910 throw new IllegalArgumentException("Unknown URL " + url);
1911 }
1912 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001913 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001914 int rowsDeleted = provider.delete(mPackageName, url, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001915 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001916 maybeLogUpdateToEventLog(durationMillis, url, "delete", where);
1917 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001919 // Arbitrary and not worth documenting, as Activity
1920 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 return -1;
1922 } finally {
1923 releaseProvider(provider);
1924 }
1925 }
1926
1927 /**
1928 * Update row(s) in a content URI.
1929 *
1930 * If the content provider supports transactions the update will be atomic.
1931 *
1932 * @param uri The URI to modify.
1933 * @param values The new field values. The key is the column name for the field.
1934 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08001935 * @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 -08001936 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001937 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 * @throws NullPointerException if uri or values are null
1939 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001940 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001941 public final int update(@RequiresPermission.Write @NonNull Uri uri,
1942 @Nullable ContentValues values, @Nullable String where,
1943 @Nullable String[] selectionArgs) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001944 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001945
1946 try {
1947 if (mWrapped != null) return mWrapped.update(uri, values, where, selectionArgs);
1948 } catch (RemoteException e) {
1949 return 0;
1950 }
1951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 IContentProvider provider = acquireProvider(uri);
1953 if (provider == null) {
1954 throw new IllegalArgumentException("Unknown URI " + uri);
1955 }
1956 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001957 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001958 int rowsUpdated = provider.update(mPackageName, uri, values, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001959 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001960 maybeLogUpdateToEventLog(durationMillis, uri, "update", where);
1961 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001963 // Arbitrary and not worth documenting, as Activity
1964 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 return -1;
1966 } finally {
1967 releaseProvider(provider);
1968 }
1969 }
1970
1971 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09001972 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001973 * read or write interfaces which are cheaper than using a Cursor and/or
1974 * do not fit into the traditional table model.
1975 *
1976 * @param method provider-defined method name to call. Opaque to
1977 * framework, but must be non-null.
1978 * @param arg provider-defined String argument. May be null.
1979 * @param extras provider-defined Bundle argument. May be null.
1980 * @return a result Bundle, possibly null. Will be null if the ContentProvider
1981 * does not implement call.
1982 * @throws NullPointerException if uri or method is null
1983 * @throws IllegalArgumentException if uri is not known
1984 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001985 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
1986 @Nullable String arg, @Nullable Bundle extras) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001987 return call(uri.getAuthority(), method, arg, extras);
1988 }
1989
1990 @Override
1991 public final @Nullable Bundle call(@NonNull String authority, @NonNull String method,
1992 @Nullable String arg, @Nullable Bundle extras) {
1993 Preconditions.checkNotNull(authority, "authority");
Jeff Sharkey673db442015-06-11 19:30:57 -07001994 Preconditions.checkNotNull(method, "method");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001995
1996 try {
1997 if (mWrapped != null) return mWrapped.call(authority, method, arg, extras);
1998 } catch (RemoteException e) {
1999 return null;
2000 }
2001
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002002 IContentProvider provider = acquireProvider(authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002003 if (provider == null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002004 throw new IllegalArgumentException("Unknown authority " + authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002005 }
2006 try {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002007 final Bundle res = provider.call(mPackageName, authority, method, arg, extras);
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002008 Bundle.setDefusable(res, true);
2009 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002010 } catch (RemoteException e) {
2011 // Arbitrary and not worth documenting, as Activity
2012 // Manager will kill this process shortly anyway.
2013 return null;
2014 } finally {
2015 releaseProvider(provider);
2016 }
2017 }
2018
2019 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002020 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 *
2022 * @param uri The URI to a content provider
2023 * @return The ContentProvider for the given URI, or null if no content provider is found.
2024 * @hide
2025 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002026 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002027 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04002028 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 return null;
2030 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002031 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002033 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
2035 return null;
2036 }
2037
2038 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002039 * Returns the content provider for the given content URI if the process
2040 * already has a reference on it.
2041 *
2042 * @param uri The URI to a content provider
2043 * @return The ContentProvider for the given URI, or null if no content provider is found.
2044 * @hide
2045 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002046 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002047 public final IContentProvider acquireExistingProvider(Uri uri) {
2048 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2049 return null;
2050 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002051 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002052 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002053 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002054 }
2055 return null;
2056 }
2057
2058 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 * @hide
2060 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002061 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002063 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 return null;
2065 }
2066 return acquireProvider(mContext, name);
2067 }
2068
2069 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002070 * Returns the content provider for the given content URI.
2071 *
2072 * @param uri The URI to a content provider
2073 * @return The ContentProvider for the given URI, or null if no content provider is found.
2074 * @hide
2075 */
2076 public final IContentProvider acquireUnstableProvider(Uri uri) {
2077 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2078 return null;
2079 }
2080 String auth = uri.getAuthority();
2081 if (auth != null) {
2082 return acquireUnstableProvider(mContext, uri.getAuthority());
2083 }
2084 return null;
2085 }
2086
2087 /**
2088 * @hide
2089 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002090 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002091 public final IContentProvider acquireUnstableProvider(String name) {
2092 if (name == null) {
2093 return null;
2094 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002095 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002096 }
2097
2098 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07002099 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2100 * that services the content at uri, starting the provider if necessary. Returns
2101 * null if there is no provider associated wih the uri. The caller must indicate that they are
2102 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002103 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002104 * keeping it active.
2105 * @param uri specifies which provider should be acquired
2106 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2107 * that services the content at uri or null if there isn't one.
2108 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002109 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
2110 Preconditions.checkNotNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07002111 IContentProvider provider = acquireProvider(uri);
2112 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002113 return new ContentProviderClient(this, provider, uri.getAuthority(), true);
Fred Quintana718d8a22009-04-29 17:53:20 -07002114 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002115 return null;
2116 }
2117
2118 /**
2119 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2120 * with the authority of name, starting the provider if necessary. Returns
2121 * null if there is no provider associated wih the uri. The caller must indicate that they are
2122 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002123 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002124 * keeping it active.
2125 * @param name specifies which provider should be acquired
2126 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2127 * with the authority of name or null if there isn't one.
2128 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002129 public final @Nullable ContentProviderClient acquireContentProviderClient(
2130 @NonNull String name) {
2131 Preconditions.checkNotNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07002132 IContentProvider provider = acquireProvider(name);
2133 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002134 return new ContentProviderClient(this, provider, name, true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002135 }
2136
2137 return null;
2138 }
2139
2140 /**
2141 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
2142 * not trust the stability of the target content provider. This turns off
2143 * the mechanism in the platform clean up processes that are dependent on
2144 * a content provider if that content provider's process goes away. Normally
2145 * you can safely assume that once you have acquired a provider, you can freely
2146 * use it as needed and it won't disappear, even if your process is in the
2147 * background. If using this method, you need to take care to deal with any
2148 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002149 * so that it can be re-opened later. In particular, catching a
2150 * {@link android.os.DeadObjectException} from the calls there will let you
2151 * know that the content provider has gone away; at that point the current
2152 * ContentProviderClient object is invalid, and you should release it. You
2153 * can acquire a new one if you would like to try to restart the provider
2154 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002155 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002156 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2157 @NonNull Uri uri) {
2158 Preconditions.checkNotNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002159 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002160 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002161 return new ContentProviderClient(this, provider, uri.getAuthority(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002162 }
2163
2164 return null;
2165 }
2166
2167 /**
2168 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
2169 * not trust the stability of the target content provider. This turns off
2170 * the mechanism in the platform clean up processes that are dependent on
2171 * a content provider if that content provider's process goes away. Normally
2172 * you can safely assume that once you have acquired a provider, you can freely
2173 * use it as needed and it won't disappear, even if your process is in the
2174 * background. If using this method, you need to take care to deal with any
2175 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002176 * so that it can be re-opened later. In particular, catching a
2177 * {@link android.os.DeadObjectException} from the calls there will let you
2178 * know that the content provider has gone away; at that point the current
2179 * ContentProviderClient object is invalid, and you should release it. You
2180 * can acquire a new one if you would like to try to restart the provider
2181 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002182 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002183 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2184 @NonNull String name) {
2185 Preconditions.checkNotNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002186 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002187 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002188 return new ContentProviderClient(this, provider, name, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07002189 }
2190
2191 return null;
2192 }
2193
2194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 * Register an observer class that gets callbacks when data identified by a
2196 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002197 * <p>
2198 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2199 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002201 * @param uri The URI to watch for changes. This can be a specific row URI,
2202 * or a base URI for a whole class of content.
2203 * @param notifyForDescendants When false, the observer will be notified
2204 * whenever a change occurs to the exact URI specified by
2205 * <code>uri</code> or to one of the URI's ancestors in the path
2206 * hierarchy. When true, the observer will also be notified
2207 * whenever a change occurs to the URI's descendants in the path
2208 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 * @param observer The object that receives callbacks when changes occur.
2210 * @see #unregisterContentObserver
2211 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002212 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07002213 @NonNull ContentObserver observer) {
2214 Preconditions.checkNotNull(uri, "uri");
2215 Preconditions.checkNotNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01002216 registerContentObserver(
2217 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002218 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01002219 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002220 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002221 }
2222
2223 /** @hide - designated user version */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002224 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07002225 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002226 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002228 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002229 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002231 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
2233 }
2234
2235 /**
2236 * Unregisters a change observer.
2237 *
2238 * @param observer The previously registered observer that is no longer needed.
2239 * @see #registerContentObserver
2240 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002241 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
2242 Preconditions.checkNotNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 try {
2244 IContentObserver contentObserver = observer.releaseContentObserver();
2245 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002246 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 contentObserver);
2248 }
2249 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002250 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 }
2252 }
2253
2254 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002255 * Notify registered observers that a row was updated and attempt to sync
2256 * changes to the network.
2257 * <p>
2258 * To observe events sent through this call, use
2259 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2260 * <p>
2261 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2262 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 *
Jeff Brown86de0592012-01-23 13:01:18 -08002264 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002265 * @param observer The observer that originated the change, may be
2266 * <code>null</null>. The observer that originated the change
2267 * will only receive the notification if it has requested to
2268 * receive self-change notifications by implementing
2269 * {@link ContentObserver#deliverSelfNotifications()} to return
2270 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002272 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 notifyChange(uri, observer, true /* sync to network */);
2274 }
2275
2276 /**
2277 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002278 * <p>
2279 * To observe events sent through this call, use
2280 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2281 * <p>
2282 * If syncToNetwork is true, this will attempt to schedule a local sync
2283 * using the sync adapter that's registered for the authority of the
2284 * provided uri. No account will be passed to the sync adapter, so all
2285 * matching accounts will be synchronized.
2286 * <p>
2287 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2288 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 *
Jeff Brown86de0592012-01-23 13:01:18 -08002290 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002291 * @param observer The observer that originated the change, may be
2292 * <code>null</null>. The observer that originated the change
2293 * will only receive the notification if it has requested to
2294 * receive self-change notifications by implementing
2295 * {@link ContentObserver#deliverSelfNotifications()} to return
2296 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002297 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002298 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002300 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2301 boolean syncToNetwork) {
2302 Preconditions.checkNotNull(uri, "uri");
Benjamin Franzadea1912015-06-19 16:03:38 +01002303 notifyChange(
2304 ContentProvider.getUriWithoutUserId(uri),
2305 observer,
2306 syncToNetwork,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002307 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002308 }
2309
2310 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002311 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002312 * <p>
2313 * To observe events sent through this call, use
2314 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2315 * <p>
2316 * If syncToNetwork is true, this will attempt to schedule a local sync
2317 * using the sync adapter that's registered for the authority of the
2318 * provided uri. No account will be passed to the sync adapter, so all
2319 * matching accounts will be synchronized.
2320 * <p>
2321 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2322 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002323 *
2324 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002325 * @param observer The observer that originated the change, may be
2326 * <code>null</null>. The observer that originated the change
2327 * will only receive the notification if it has requested to
2328 * receive self-change notifications by implementing
2329 * {@link ContentObserver#deliverSelfNotifications()} to return
2330 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002331 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2332 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2333 */
2334 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2335 @NotifyFlags int flags) {
2336 Preconditions.checkNotNull(uri, "uri");
2337 notifyChange(
2338 ContentProvider.getUriWithoutUserId(uri),
2339 observer,
2340 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002341 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002342 }
2343
2344 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002345 * Notify registered observers within the designated user(s) that a row was updated.
2346 *
2347 * @hide
2348 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002349 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002350 @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002352 getContentService().notifyChange(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 uri, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002354 observer != null && observer.deliverSelfNotifications(),
2355 syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0,
Makoto Onukie183a402018-08-29 11:46:41 -07002356 userHandle, mTargetSdkVersion, mContext.getPackageName());
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002357 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002358 throw e.rethrowFromSystemServer();
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002359 }
2360 }
2361
2362 /**
2363 * Notify registered observers within the designated user(s) that a row was updated.
2364 *
2365 * @hide
2366 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002367 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002368 @UserIdInt int userHandle) {
2369 try {
2370 getContentService().notifyChange(
2371 uri, observer == null ? null : observer.getContentObserver(),
2372 observer != null && observer.deliverSelfNotifications(), flags,
Makoto Onukie183a402018-08-29 11:46:41 -07002373 userHandle, mTargetSdkVersion, mContext.getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002375 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
2377 }
2378
2379 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002380 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002381 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002382 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002383 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2384 * the grant has already been persisted, taking it again will touch
2385 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002386 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002387 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002388 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002389 public void takePersistableUriPermission(@NonNull Uri uri,
2390 @Intent.AccessUriMode int modeFlags) {
2391 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002392 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002393 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002394 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2395 resolveUserId(uri));
2396 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002397 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002398 }
2399 }
2400
2401 /**
2402 * @hide
2403 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002404 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002405 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2406 @Intent.AccessUriMode int modeFlags) {
2407 Preconditions.checkNotNull(toPackage, "toPackage");
2408 Preconditions.checkNotNull(uri, "uri");
2409 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002410 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002411 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2412 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002413 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002414 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002415 }
2416 }
2417
2418 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002419 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002420 * previously made persistent with
2421 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2422 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002423 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002424 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002425 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002426 public void releasePersistableUriPermission(@NonNull Uri uri,
2427 @Intent.AccessUriMode int modeFlags) {
2428 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002429 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002430 UriGrantsManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002431 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2432 resolveUserId(uri));
2433 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002434 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002435 }
2436 }
2437
2438 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002439 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002440 * calling app. That is, the returned permissions have been granted
2441 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002442 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002443 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002444 *
2445 * @see #takePersistableUriPermission(Uri, int)
2446 * @see #releasePersistableUriPermission(Uri, int)
2447 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002448 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002449 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002450 return UriGrantsManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002451 .getPersistedUriPermissions(mPackageName, true).getList();
2452 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002453 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002454 }
2455 }
2456
2457 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002458 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002459 * calling app. That is, the returned permissions have been granted
2460 * <em>from</em> the calling app. Only grants taken with
2461 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002462 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002463 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002464 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002465 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002466 return UriGrantsManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002467 .getPersistedUriPermissions(mPackageName, false).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002468 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002469 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002470 }
2471 }
2472
2473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 * Start an asynchronous sync operation. If you want to monitor the progress
2475 * of the sync you may register a SyncObserver. Only values of the following
2476 * types may be used in the extras bundle:
2477 * <ul>
2478 * <li>Integer</li>
2479 * <li>Long</li>
2480 * <li>Boolean</li>
2481 * <li>Float</li>
2482 * <li>Double</li>
2483 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002484 * <li>Account</li>
2485 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 * </ul>
2487 *
2488 * @param uri the uri of the provider to sync or null to sync all providers.
2489 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002490 * @deprecated instead use
2491 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002493 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002495 Account account = null;
2496 if (extras != null) {
2497 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2498 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002499 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002500 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002501 }
2502 extras.remove(SYNC_EXTRAS_ACCOUNT);
2503 }
2504 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2505 }
2506
2507 /**
2508 * Start an asynchronous sync operation. If you want to monitor the progress
2509 * of the sync you may register a SyncObserver. Only values of the following
2510 * types may be used in the extras bundle:
2511 * <ul>
2512 * <li>Integer</li>
2513 * <li>Long</li>
2514 * <li>Boolean</li>
2515 * <li>Float</li>
2516 * <li>Double</li>
2517 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002518 * <li>Account</li>
2519 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002520 * </ul>
2521 *
2522 * @param account which account should be synced
2523 * @param authority which authority should be synced
2524 * @param extras any extras to pass to the SyncAdapter.
2525 */
2526 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002527 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002528 }
2529
2530 /**
2531 * @see #requestSync(Account, String, Bundle)
2532 * @hide
2533 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002534 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002535 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002536 if (extras == null) {
2537 throw new IllegalArgumentException("Must specify extras.");
2538 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002539 SyncRequest request =
2540 new SyncRequest.Builder()
2541 .setSyncAdapter(account, authority)
2542 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002543 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002544 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002545 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002546 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2547 // case, but it's only for debugging.
2548 getContentService().syncAsUser(request, userId, ActivityThread.currentPackageName());
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002549 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002550 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002551 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002552 }
2553
2554 /**
2555 * Register a sync with the SyncManager. These requests are built using the
2556 * {@link SyncRequest.Builder}.
2557 */
2558 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002560 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2561 // case, but it's only for debugging.
2562 getContentService().sync(request, ActivityThread.currentPackageName());
Matthew Williamsfa774182013-06-18 15:44:11 -07002563 } 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 * Check that only values of the following types are in the Bundle:
2570 * <ul>
2571 * <li>Integer</li>
2572 * <li>Long</li>
2573 * <li>Boolean</li>
2574 * <li>Float</li>
2575 * <li>Double</li>
2576 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002577 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 * <li>null</li>
2579 * </ul>
2580 * @param extras the Bundle to check
2581 */
2582 public static void validateSyncExtrasBundle(Bundle extras) {
2583 try {
2584 for (String key : extras.keySet()) {
2585 Object value = extras.get(key);
2586 if (value == null) continue;
2587 if (value instanceof Long) continue;
2588 if (value instanceof Integer) continue;
2589 if (value instanceof Boolean) continue;
2590 if (value instanceof Float) continue;
2591 if (value instanceof Double) continue;
2592 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002593 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 throw new IllegalArgumentException("unexpected value type: "
2595 + value.getClass().getName());
2596 }
2597 } catch (IllegalArgumentException e) {
2598 throw e;
2599 } catch (RuntimeException exc) {
2600 throw new IllegalArgumentException("error unparceling Bundle", exc);
2601 }
2602 }
2603
Fred Quintanaac9385e2009-06-22 18:00:59 -07002604 /**
2605 * Cancel any active or pending syncs that match the Uri. If the uri is null then
2606 * all syncs will be canceled.
2607 *
2608 * @param uri the uri of the provider to sync or null to sync all providers.
2609 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
2610 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002611 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002613 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
2614 }
2615
2616 /**
2617 * Cancel any active or pending syncs that match account and authority. The account and
2618 * authority can each independently be set to null, which means that syncs with any account
2619 * or authority, respectively, will match.
2620 *
2621 * @param account filters the syncs that match by this account
2622 * @param authority filters the syncs that match by this authority
2623 */
2624 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002626 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002628 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 }
2630 }
2631
Fred Quintanaac9385e2009-06-22 18:00:59 -07002632 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002633 * @see #cancelSync(Account, String)
2634 * @hide
2635 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002636 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002637 try {
2638 getContentService().cancelSyncAsUser(account, authority, null, userId);
2639 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002640 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002641 }
2642 }
2643
2644 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002645 * Get information about the SyncAdapters that are known to the system.
2646 * @return an array of SyncAdapters that have registered with the system
2647 */
2648 public static SyncAdapterType[] getSyncAdapterTypes() {
2649 try {
2650 return getContentService().getSyncAdapterTypes();
2651 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002652 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002653 }
2654 }
2655
2656 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002657 * @see #getSyncAdapterTypes()
2658 * @hide
2659 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002660 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002661 try {
2662 return getContentService().getSyncAdapterTypesAsUser(userId);
2663 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002664 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002665 }
2666 }
2667
2668 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07002669 * @hide
2670 * Returns the package names of syncadapters that match a given user and authority.
2671 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07002672 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07002673 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002674 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002675 try {
2676 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
2677 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002678 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07002679 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07002680 }
2681
2682 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002683 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002684 * <p>This method requires the caller to hold the permission
2685 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002686 *
2687 * @param account the account whose setting we are querying
2688 * @param authority the provider whose setting we are querying
2689 * @return true if the provider should be synced when a network tickle is received
2690 */
2691 public static boolean getSyncAutomatically(Account account, String authority) {
2692 try {
2693 return getContentService().getSyncAutomatically(account, authority);
2694 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002695 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002696 }
2697 }
2698
2699 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002700 * @see #getSyncAutomatically(Account, String)
2701 * @hide
2702 */
2703 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002704 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002705 try {
2706 return getContentService().getSyncAutomaticallyAsUser(account, authority, userId);
2707 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002708 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002709 }
2710 }
2711
2712 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002713 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002714 * <p>This method requires the caller to hold the permission
2715 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002716 *
2717 * @param account the account whose setting we are querying
2718 * @param authority the provider whose behavior is being controlled
2719 * @param sync true if the provider should be synced when tickles are received for it
2720 */
2721 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002722 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002723 }
2724
2725 /**
2726 * @see #setSyncAutomatically(Account, String, boolean)
2727 * @hide
2728 */
2729 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002730 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002731 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002732 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002733 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002734 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002735 }
2736 }
2737
2738 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002739 * Specifies that a sync should be requested with the specified the account, authority,
2740 * and extras at the given frequency. If there is already another periodic sync scheduled
2741 * with the account, authority and extras then a new periodic sync won't be added, instead
2742 * the frequency of the previous one will be updated.
2743 * <p>
2744 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
2745 * Although these sync are scheduled at the specified frequency, it may take longer for it to
2746 * actually be started if other syncs are ahead of it in the sync operation queue. This means
2747 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08002748 * <p>
2749 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
2750 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
2751 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
2752 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
2753 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002754 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002755 * <p>This method requires the caller to hold the permission
2756 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002757 * <p>The bundle for a periodic sync can be queried by applications with the correct
2758 * permissions using
2759 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
2760 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002761 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002762 * @param account the account to specify in the sync
2763 * @param authority the provider to specify in the sync request
2764 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07002765 * @param pollFrequency how frequently the sync should be performed, in seconds.
2766 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
2767 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08002768 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
2769 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002770 */
2771 public static void addPeriodicSync(Account account, String authority, Bundle extras,
2772 long pollFrequency) {
2773 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08002774 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002775 throw new IllegalArgumentException("illegal extras were set");
2776 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002777 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07002778 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002779 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002780 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002781 }
2782 }
2783
2784 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002785 * {@hide}
2786 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
2787 * extras were set for a periodic sync.
2788 *
2789 * @param extras bundle to validate.
2790 */
2791 public static boolean invalidPeriodicExtras(Bundle extras) {
2792 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
2793 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
2794 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
2795 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
2796 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
2797 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
2798 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
2799 return true;
2800 }
2801 return false;
2802 }
2803
2804 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002805 * Remove a periodic sync. Has no affect if account, authority and extras don't match
2806 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002807 * <p>This method requires the caller to hold the permission
2808 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002809 *
2810 * @param account the account of the periodic sync to remove
2811 * @param authority the provider of the periodic sync to remove
2812 * @param extras the extras of the periodic sync to remove
2813 */
2814 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
2815 validateSyncExtrasBundle(extras);
2816 try {
2817 getContentService().removePeriodicSync(account, authority, extras);
2818 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002819 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002820 }
2821 }
2822
2823 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002824 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
2825 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07002826 * <p>
2827 * If a periodic sync is specified, the caller must hold the permission
2828 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
2829 *</p>
2830 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002831 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07002832 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07002833 *
2834 * @param request SyncRequest object containing information about sync to cancel.
2835 */
2836 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002837 if (request == null) {
2838 throw new IllegalArgumentException("request cannot be null");
2839 }
2840 try {
2841 getContentService().cancelRequest(request);
2842 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002843 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002844 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002845 }
2846
2847 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002848 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002849 * <p>This method requires the caller to hold the permission
2850 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002851 *
2852 * @param account the account whose periodic syncs we are querying
2853 * @param authority the provider whose periodic syncs we are querying
2854 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
2855 */
2856 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
2857 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002858 return getContentService().getPeriodicSyncs(account, authority, null);
2859 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002860 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002861 }
2862 }
2863
2864 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002865 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002866 * <p>This method requires the caller to hold the permission
2867 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07002868 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
2869 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002870 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002871 try {
2872 return getContentService().getIsSyncable(account, authority);
2873 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002874 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002875 }
2876 }
2877
2878 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002879 * @see #getIsSyncable(Account, String)
2880 * @hide
2881 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002882 public static int getIsSyncableAsUser(Account account, String authority,
2883 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002884 try {
2885 return getContentService().getIsSyncableAsUser(account, authority, userId);
2886 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002887 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002888 }
2889 }
2890
2891 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002892 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002893 * <p>This method requires the caller to hold the permission
2894 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07002895 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07002896 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002897 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002898 try {
2899 getContentService().setIsSyncable(account, authority, syncable);
2900 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002901 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002902 }
2903 }
2904
2905 /**
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +00002906 * @see #setIsSyncable(Account, String, int)
2907 * @hide
2908 */
2909 public static void setIsSyncableAsUser(Account account, String authority, int syncable,
2910 int userId) {
2911 try {
2912 getContentService().setIsSyncableAsUser(account, authority, syncable, userId);
2913 } catch (RemoteException e) {
2914 throw e.rethrowFromSystemServer();
2915 }
2916 }
2917
2918 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002919 * Gets the master auto-sync setting that applies to all the providers and accounts.
2920 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002921 * <p>This method requires the caller to hold the permission
2922 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002923 *
2924 * @return the master auto-sync setting that applies to all the providers and accounts
2925 */
2926 public static boolean getMasterSyncAutomatically() {
2927 try {
2928 return getContentService().getMasterSyncAutomatically();
2929 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002930 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002931 }
2932 }
2933
2934 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002935 * @see #getMasterSyncAutomatically()
2936 * @hide
2937 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002938 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002939 try {
2940 return getContentService().getMasterSyncAutomaticallyAsUser(userId);
2941 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002942 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002943 }
2944 }
2945
2946 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002947 * Sets the master auto-sync setting that applies to all the providers and accounts.
2948 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002949 * <p>This method requires the caller to hold the permission
2950 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002951 *
2952 * @param sync the master auto-sync setting that applies to all the providers and accounts
2953 */
2954 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002955 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002956 }
2957
2958 /**
2959 * @see #setMasterSyncAutomatically(boolean)
2960 * @hide
2961 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002962 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002963 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002964 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002965 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002966 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002967 }
2968 }
2969
2970 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002971 * Returns true if there is currently a sync operation for the given account or authority
2972 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002973 * <p>This method requires the caller to hold the permission
2974 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002975 * @param account the account whose setting we are querying
2976 * @param authority the provider whose behavior is being queried
2977 * @return true if a sync is active for the given account or authority.
2978 */
2979 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002980 if (account == null) {
2981 throw new IllegalArgumentException("account must not be null");
2982 }
2983 if (authority == null) {
2984 throw new IllegalArgumentException("authority must not be null");
2985 }
2986
Fred Quintanaac9385e2009-06-22 18:00:59 -07002987 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002988 return getContentService().isSyncActive(account, authority, null);
2989 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002990 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002991 }
2992 }
2993
Fred Quintanaac9385e2009-06-22 18:00:59 -07002994 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07002995 * If a sync is active returns the information about it, otherwise returns null.
2996 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002997 * This method requires the caller to hold the permission
2998 * {@link android.Manifest.permission#READ_SYNC_STATS}.
2999 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003000 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07003001 * @deprecated
3002 * Since multiple concurrent syncs are now supported you should use
3003 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
3004 * This method returns the first item from the list of current syncs
3005 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003006 */
Fred Quintanac6a69552010-09-27 17:05:04 -07003007 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003008 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003009 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07003010 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
3011 if (syncs.isEmpty()) {
3012 return null;
3013 }
3014 return syncs.get(0);
3015 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003016 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07003017 }
3018 }
3019
3020 /**
3021 * Returns a list with information about all the active syncs. This list will be empty
3022 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003023 * <p>
3024 * This method requires the caller to hold the permission
3025 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3026 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07003027 * @return a List of SyncInfo objects for the currently active syncs.
3028 */
3029 public static List<SyncInfo> getCurrentSyncs() {
3030 try {
3031 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003032 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003033 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003034 }
3035 }
3036
3037 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003038 * @see #getCurrentSyncs()
3039 * @hide
3040 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003041 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003042 try {
3043 return getContentService().getCurrentSyncsAsUser(userId);
3044 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003045 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003046 }
3047 }
3048
3049 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07003050 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003051 * @param account the account whose setting we are querying
3052 * @param authority the provider whose behavior is being queried
3053 * @return the SyncStatusInfo for the authority, or null if none exists
3054 * @hide
3055 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003056 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07003057 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
3058 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003059 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003060 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003061 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003062 }
3063 }
3064
3065 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003066 * @see #getSyncStatus(Account, String)
3067 * @hide
3068 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003069 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003070 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003071 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003072 try {
3073 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
3074 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003075 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003076 }
3077 }
3078
3079 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003080 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003081 * <p>This method requires the caller to hold the permission
3082 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003083 * @param account the account whose setting we are querying
3084 * @param authority the provider whose behavior is being queried
3085 * @return true if there is a pending sync with the matching account and authority
3086 */
3087 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003088 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003089 }
3090
3091 /**
3092 * @see #requestSync(Account, String, Bundle)
3093 * @hide
3094 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003095 public static boolean isSyncPendingAsUser(Account account, String authority,
3096 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003097 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003098 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003099 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003100 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003101 }
3102 }
3103
Fred Quintana1b487ec2010-02-26 10:57:55 -08003104 /**
3105 * Request notifications when the different aspects of the SyncManager change. The
3106 * different items that can be requested are:
3107 * <ul>
3108 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
3109 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
3110 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
3111 * </ul>
3112 * The caller can set one or more of the status types in the mask for any
3113 * given listener registration.
3114 * @param mask the status change types that will cause the callback to be invoked
3115 * @param callback observer to be invoked when the status changes
3116 * @return a handle that can be used to remove the listener at a later time
3117 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003118 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003119 if (callback == null) {
3120 throw new IllegalArgumentException("you passed in a null callback");
3121 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003122 try {
3123 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08003124 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07003125 public void onStatusChanged(int which) throws RemoteException {
3126 callback.onStatusChanged(which);
3127 }
3128 };
3129 getContentService().addStatusChangeListener(mask, observer);
3130 return observer;
3131 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003132 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003133 }
3134 }
3135
Fred Quintana1b487ec2010-02-26 10:57:55 -08003136 /**
3137 * Remove a previously registered status change listener.
3138 * @param handle the handle that was returned by {@link #addStatusChangeListener}
3139 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003140 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003141 if (handle == null) {
3142 throw new IllegalArgumentException("you passed in a null handle");
3143 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003144 try {
3145 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
3146 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003147 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003148 }
3149 }
3150
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003151 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003152 * Store the given {@link Bundle} as a long-lived cached object within the
3153 * system. This can be useful to avoid expensive re-parsing when apps are
3154 * restarted multiple times on low-RAM devices.
3155 * <p>
3156 * The {@link Bundle} is automatically invalidated when a
3157 * {@link #notifyChange(Uri, ContentObserver)} event applies to the key.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003158 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003159 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003160 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003161 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003162 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003163 public void putCache(@NonNull Uri key, @Nullable Bundle value) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003164 try {
3165 getContentService().putCache(mContext.getPackageName(), key, value,
3166 mContext.getUserId());
3167 } catch (RemoteException e) {
3168 throw e.rethrowFromSystemServer();
3169 }
3170 }
3171
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003172 /**
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003173 * Retrieve the last {@link Bundle} stored as a long-lived cached object
3174 * within the system.
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003175 *
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003176 * @return {@code null} if no cached object has been stored, or if the
3177 * stored object has been invalidated due to a
3178 * {@link #notifyChange(Uri, ContentObserver)} event.
3179 * @hide
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003180 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003181 @SystemApi
Ivan Chianga46ade32019-02-25 11:30:34 +08003182 @RequiresPermission(android.Manifest.permission.CACHE_CONTENT)
Jeff Sharkeyb91eaa52019-02-19 11:09:13 -07003183 public @Nullable Bundle getCache(@NonNull Uri key) {
Jeff Sharkey87314082016-03-11 17:25:11 -07003184 try {
3185 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
3186 mContext.getUserId());
3187 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
3188 return bundle;
3189 } catch (RemoteException e) {
3190 throw e.rethrowFromSystemServer();
3191 }
3192 }
3193
Ben Lin8ea82002017-03-08 17:30:16 -08003194 /** {@hide} */
3195 public int getTargetSdkVersion() {
3196 return mTargetSdkVersion;
3197 }
3198
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003199 /**
3200 * Returns sampling percentage for a given duration.
3201 *
3202 * Always returns at least 1%.
3203 */
3204 private int samplePercentForDuration(long durationMillis) {
3205 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
3206 return 100;
3207 }
3208 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
3209 }
3210
Steve McKayea93fe72016-12-02 11:35:35 -08003211 private void maybeLogQueryToEventLog(
3212 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003213 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003214 int samplePercent = samplePercentForDuration(durationMillis);
3215 if (samplePercent < 100) {
3216 synchronized (mRandom) {
3217 if (mRandom.nextInt(100) >= samplePercent) {
3218 return;
3219 }
3220 }
3221 }
3222
Steve McKayea93fe72016-12-02 11:35:35 -08003223 // Ensure a non-null bundle.
3224 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
3225
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003226 StringBuilder projectionBuffer = new StringBuilder(100);
3227 if (projection != null) {
3228 for (int i = 0; i < projection.length; ++i) {
3229 // Note: not using a comma delimiter here, as the
3230 // multiple arguments to EventLog.writeEvent later
3231 // stringify with a comma delimiter, which would make
3232 // parsing uglier later.
3233 if (i != 0) projectionBuffer.append('/');
3234 projectionBuffer.append(projection[i]);
3235 }
3236 }
3237
3238 // ActivityThread.currentPackageName() only returns non-null if the
3239 // current thread is an application main thread. This parameter tells
3240 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003241 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003242
3243 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003244 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003245 uri.toString(),
3246 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08003247 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
3248 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003249 durationMillis,
3250 blockingPackage != null ? blockingPackage : "",
3251 samplePercent);
3252 }
3253
3254 private void maybeLogUpdateToEventLog(
3255 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003256 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003257 int samplePercent = samplePercentForDuration(durationMillis);
3258 if (samplePercent < 100) {
3259 synchronized (mRandom) {
3260 if (mRandom.nextInt(100) >= samplePercent) {
3261 return;
3262 }
3263 }
3264 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003265 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003266 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003267 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003268 uri.toString(),
3269 operation,
3270 selection != null ? selection : "",
3271 durationMillis,
3272 blockingPackage != null ? blockingPackage : "",
3273 samplePercent);
3274 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003275
Jeff Brown825c5132011-10-12 16:11:30 -07003276 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003277 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003278 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003279
3280 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003282 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003284 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003285 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 }
3287
3288 @Override
3289 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003290 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003292
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003293 if (mProviderReleased.compareAndSet(false, true)) {
3294 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 }
3297
3298 @Override
3299 protected void finalize() throws Throwable {
3300 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003301 if (mCloseGuard != null) {
3302 mCloseGuard.warnIfOpen();
3303 }
3304
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003305 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 } finally {
3307 super.finalize();
3308 }
3309 }
3310 }
3311
3312 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003313 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003314 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315
3316 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3317 super(pfd);
3318 mContentProvider = icp;
3319 }
3320
3321 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003322 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003323 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003324 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 }
3326 }
3327 }
3328
Dianne Hackborn231cc602009-04-27 17:10:36 -07003329 /** @hide */
3330 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003331
Dianne Hackborn231cc602009-04-27 17:10:36 -07003332 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003333 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003334 public static IContentService getContentService() {
3335 if (sContentService != null) {
3336 return sContentService;
3337 }
3338 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003339 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003340 return sContentService;
3341 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003342
Dianne Hackborn35654b62013-01-14 17:38:02 -08003343 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003344 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003345 public String getPackageName() {
3346 return mPackageName;
3347 }
3348
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003349 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003350 private static volatile IContentService sContentService;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003351 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003353
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003354 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003355 final String mPackageName;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003356 final int mTargetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -07003357 final ContentInterface mWrapped;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003360
3361 /** @hide */
3362 public int resolveUserId(Uri uri) {
3363 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3364 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003365
3366 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003367 public int getUserId() {
3368 return mContext.getUserId();
3369 }
3370
Jeff Sharkey806bece2019-02-19 14:42:55 -07003371 /** {@hide} */
3372 @Deprecated
Daniel Nishic29d2b02016-06-30 12:20:41 -07003373 public Drawable getTypeDrawable(String mimeType) {
Jeff Sharkey806bece2019-02-19 14:42:55 -07003374 return getTypeInfo(mimeType).getIcon().loadDrawable(mContext);
3375 }
3376
3377 /**
3378 * Return a detailed description of the given MIME type, including an icon
3379 * and label that describe the type.
3380 *
3381 * @param mimeType Valid, concrete MIME type.
3382 */
3383 public final @NonNull TypeInfo getTypeInfo(@NonNull String mimeType) {
3384 Objects.requireNonNull(mimeType);
3385 return MimeIconUtils.getTypeInfo(mimeType);
3386 }
3387
3388 /**
3389 * Detailed description of a specific MIME type, including an icon and label
3390 * that describe the type.
3391 */
3392 public static final class TypeInfo {
3393 private final Icon mIcon;
3394 private final CharSequence mLabel;
3395 private final CharSequence mContentDescription;
3396
3397 /** {@hide} */
3398 public TypeInfo(@NonNull Icon icon, @NonNull CharSequence label,
3399 @NonNull CharSequence contentDescription) {
3400 mIcon = Objects.requireNonNull(icon);
3401 mLabel = Objects.requireNonNull(label);
3402 mContentDescription = Objects.requireNonNull(contentDescription);
3403 }
3404
3405 /**
3406 * Return a visual representation of this MIME type. This can be styled
3407 * using {@link Icon#setTint(int)} to match surrounding UI.
3408 *
3409 * @see Icon#loadDrawable(Context)
3410 * @see android.widget.ImageView#setImageDrawable(Drawable)
3411 */
3412 public @NonNull Icon getIcon() {
3413 return mIcon;
3414 }
3415
3416 /**
3417 * Return a textual representation of this MIME type.
3418 *
3419 * @see android.widget.TextView#setText(CharSequence)
3420 */
3421 public @NonNull CharSequence getLabel() {
3422 return mLabel;
3423 }
3424
3425 /**
3426 * Return a content description for this MIME type.
3427 *
3428 * @see android.view.View#setContentDescription(CharSequence)
3429 */
3430 public @NonNull CharSequence getContentDescription() {
3431 return mContentDescription;
3432 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003433 }
Steve McKayea93fe72016-12-02 11:35:35 -08003434
3435 /**
3436 * @hide
3437 */
3438 public static @Nullable Bundle createSqlQueryBundle(
3439 @Nullable String selection,
3440 @Nullable String[] selectionArgs,
3441 @Nullable String sortOrder) {
3442
3443 if (selection == null && selectionArgs == null && sortOrder == null) {
3444 return null;
3445 }
3446
3447 Bundle queryArgs = new Bundle();
3448 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003449 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003450 }
3451 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003452 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003453 }
3454 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003455 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003456 }
3457 return queryArgs;
3458 }
Steve McKay29c3f682016-12-16 14:52:59 -08003459
3460 /**
3461 * Returns structured sort args formatted as an SQL sort clause.
3462 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003463 * NOTE: Collator clauses are suitable for use with non text fields. We might
3464 * choose to omit any collation clause since we don't know the underlying
3465 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3466 * appear to care much about the presence of collate clauses in queries
3467 * when ordering by numeric fields. For this reason we include collate
3468 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3469 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003470 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003471 * TODO: Would be nice to explicitly validate that colums referenced in
3472 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003473 *
3474 * @hide
3475 */
3476 public static String createSqlSortClause(Bundle queryArgs) {
3477 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3478 if (columns == null || columns.length == 0) {
3479 throw new IllegalArgumentException("Can't create sort clause without columns.");
3480 }
3481
3482 String query = TextUtils.join(", ", columns);
3483
Steve McKayd7ece9f2017-01-12 16:59:59 -08003484 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3485 // on their javadoc descriptions.
3486 int collation = queryArgs.getInt(
3487 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3488 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3489 query += " COLLATE NOCASE";
3490 }
3491
Steve McKay415f41b2017-02-01 13:38:25 -08003492 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3493 if (sortDir != Integer.MIN_VALUE) {
3494 switch (sortDir) {
3495 case QUERY_SORT_DIRECTION_ASCENDING:
3496 query += " ASC";
3497 break;
3498 case QUERY_SORT_DIRECTION_DESCENDING:
3499 query += " DESC";
3500 break;
3501 default:
3502 throw new IllegalArgumentException("Unsupported sort direction value."
3503 + " See ContentResolver documentation for details.");
3504 }
Steve McKay29c3f682016-12-16 14:52:59 -08003505 }
Steve McKay29c3f682016-12-16 14:52:59 -08003506 return query;
3507 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003508
3509 /**
3510 * Convenience method that efficiently loads a visual thumbnail for the
3511 * given {@link Uri}. Internally calls
3512 * {@link ContentProvider#openTypedAssetFile} on the remote provider, but
3513 * also defensively resizes any returned content to match the requested
3514 * target size.
3515 *
3516 * @param uri The item that should be visualized as a thumbnail.
3517 * @param size The target area on the screen where this thumbnail will be
3518 * shown. This is passed to the provider as {@link #EXTRA_SIZE}
3519 * to help it avoid downloading or generating heavy resources.
3520 * @param signal A signal to cancel the operation in progress.
3521 * @return Valid {@link Bitmap} which is a visual thumbnail.
3522 * @throws IOException If any trouble was encountered while generating or
3523 * loading the thumbnail, or if
3524 * {@link CancellationSignal#cancel()} was invoked.
3525 */
3526 public @NonNull Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size,
3527 @Nullable CancellationSignal signal) throws IOException {
3528 Objects.requireNonNull(uri);
3529 Objects.requireNonNull(size);
3530
3531 try (ContentProviderClient client = acquireContentProviderClient(uri)) {
Leon Scroggins III19ff0c52018-11-19 10:00:51 -05003532 return loadThumbnail(client, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003533 }
3534 }
3535
3536 /** {@hide} */
Jeff Sharkeye770d222018-12-07 15:15:59 -07003537 public static Bitmap loadThumbnail(@NonNull ContentInterface content, @NonNull Uri uri,
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003538 @NonNull Size size, @Nullable CancellationSignal signal, int allocator)
3539 throws IOException {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003540 Objects.requireNonNull(content);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003541 Objects.requireNonNull(uri);
3542 Objects.requireNonNull(size);
3543
3544 // Convert to Point, since that's what the API is defined as
3545 final Bundle opts = new Bundle();
3546 opts.putParcelable(EXTRA_SIZE, Point.convert(size));
3547
3548 return ImageDecoder.decodeBitmap(ImageDecoder.createSource(() -> {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003549 return content.openTypedAssetFile(uri, "image/*", opts, signal);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003550 }), (ImageDecoder decoder, ImageInfo info, Source source) -> {
3551 decoder.setAllocator(allocator);
3552
3553 // One last-ditch check to see if we've been canceled.
3554 if (signal != null) signal.throwIfCanceled();
3555
3556 // We requested a rough thumbnail size, but the remote size may have
3557 // returned something giant, so defensively scale down as needed.
3558 final int widthSample = info.getSize().getWidth() / size.getWidth();
3559 final int heightSample = info.getSize().getHeight() / size.getHeight();
3560 final int sample = Math.min(widthSample, heightSample);
3561 if (sample > 1) {
3562 decoder.setTargetSampleSize(sample);
3563 }
3564 });
3565 }
Makoto Onukiee93ad22018-10-18 16:24:13 -07003566
3567 /** {@hide} */
3568 public static void onDbCorruption(String tag, String message, Throwable stacktrace) {
3569 try {
3570 getContentService().onDbCorruption(tag, message, Log.getStackTraceString(stacktrace));
3571 } catch (RemoteException e) {
3572 e.rethrowFromSystemServer();
3573 }
3574 }
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003575
3576 /** {@hide} */
3577 public static Uri translateDeprecatedDataPath(String path) {
3578 final String ssp = "//" + path.substring(DEPRECATE_DATA_PREFIX.length());
3579 return Uri.parse(new Uri.Builder().scheme(SCHEME_CONTENT)
3580 .encodedOpaquePart(ssp).build().toString());
3581 }
3582
3583 /** {@hide} */
3584 public static String translateDeprecatedDataPath(Uri uri) {
3585 return DEPRECATE_DATA_PREFIX + uri.getEncodedSchemeSpecificPart().substring(2);
3586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587}