blob: bbfa5cceec1d4016d2810c33dd44bba86cb0616f [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.net.Uri;
46import android.os.Bundle;
Jeff Browna7771df2012-05-07 20:06:46 -070047import android.os.CancellationSignal;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070048import android.os.DeadObjectException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070049import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070050import android.os.ICancellationSignal;
51import android.os.OperationCanceledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.ParcelFileDescriptor;
53import android.os.RemoteException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070054import android.os.ServiceManager;
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -080055import android.os.SystemClock;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070056import android.os.UserHandle;
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060057import android.os.storage.StorageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.text.TextUtils;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080059import android.util.EventLog;
Dianne Hackborn231cc602009-04-27 17:10:36 -070060import android.util.Log;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060061import android.util.Size;
Jeff Sharkey08da7a12013-08-11 20:53:18 -070062
Daniel Nishic29d2b02016-06-30 12:20:41 -070063import com.android.internal.util.MimeIconUtils;
Jeff Sharkey673db442015-06-11 19:30:57 -070064import com.android.internal.util.Preconditions;
65
Jeff Sharkey60cfad82016-01-05 17:30:57 -070066import dalvik.system.CloseGuard;
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import java.io.File;
69import java.io.FileInputStream;
70import java.io.FileNotFoundException;
71import java.io.IOException;
72import java.io.InputStream;
73import java.io.OutputStream;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070074import java.lang.annotation.Retention;
75import java.lang.annotation.RetentionPolicy;
Gilles Debunne03f02922010-06-09 14:11:45 -070076import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.List;
Jeff Sharkey4e5efa32018-10-04 19:21:53 -060078import java.util.Objects;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080079import java.util.Random;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070080import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082/**
83 * This class provides applications access to the content model.
Joe Fernandez558459f2011-10-13 16:47:36 -070084 *
85 * <div class="special reference">
86 * <h3>Developer Guides</h3>
87 * <p>For more information about using a ContentResolver with content providers, read the
88 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
89 * developer guide.</p>
Ng Zhi Anb837d0b2019-01-24 21:39:24 -080090 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -070092public abstract class ContentResolver implements ContentInterface {
Fred Quintanaac9385e2009-06-22 18:00:59 -070093 /**
Jeff Sharkeybc2ae002018-07-31 10:45:37 -060094 * Enables logic that supports deprecation of {@code _data} columns,
95 * typically by replacing values with fake paths that the OS then offers to
96 * redirect to {@link #openFileDescriptor(Uri, String)}, which developers
97 * should be using directly.
98 *
99 * @hide
100 */
Jeff Sharkey10ec9d82018-11-28 14:52:45 -0700101 public static final boolean DEPRECATE_DATA_COLUMNS = StorageManager.hasIsolatedStorage();
Jeff Sharkeybc2ae002018-07-31 10:45:37 -0600102
103 /**
104 * Special filesystem path prefix which indicates that a path should be
105 * treated as a {@code content://} {@link Uri} when
106 * {@link #DEPRECATE_DATA_COLUMNS} is enabled.
107 * <p>
108 * The remainder of the path after this prefix is a
109 * {@link Uri#getSchemeSpecificPart()} value, which includes authority, path
110 * segments, and query parameters.
111 *
112 * @hide
113 */
114 public static final String DEPRECATE_DATA_PREFIX = "/mnt/content/";
115
116 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700117 * @deprecated instead use
118 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
119 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700120 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -0700121 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700122
123 /**
124 * If this extra is set to true, the sync request will be scheduled
125 * at the front of the sync request queue and without any delay
126 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700128
Fred Quintanaac9385e2009-06-22 18:00:59 -0700129 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000130 * If this extra is set to true, the sync request will be scheduled
131 * only when the device is plugged in. This is equivalent to calling
132 * setRequiresCharging(true) on {@link SyncRequest}.
133 */
134 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
135
136 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700137 * @deprecated instead use
138 * {@link #SYNC_EXTRAS_MANUAL}
139 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700140 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800142
143 /**
144 * If this extra is set to true then the sync settings (like getSyncAutomatically())
145 * are ignored by the sync scheduler.
146 */
147 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
148
149 /**
150 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
151 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
152 * retries will still honor the backoff.
153 */
154 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
155
156 /**
157 * If this extra is set to true then the request will not be retried if it fails.
158 */
159 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
160
161 /**
162 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
163 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
164 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700165 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800166
Georgi Nikolovb3395572013-06-18 18:27:31 -0700167 /**
168 * Indicates that this sync is intended to only upload local changes to the server.
169 * For example, this will be set to true if the sync is initiated by a call to
170 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
171 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700173
174 /**
175 * Indicates that the sync adapter should proceed with the delete operations,
176 * even if it determines that there are too many.
177 * See {@link SyncResult#tooManyDeletions}
178 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700180
181 /**
182 * Indicates that the sync adapter should not proceed with the delete operations,
183 * if it determines that there are too many.
184 * See {@link SyncResult#tooManyDeletions}
185 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
187
Matthew Williamsfa774182013-06-18 15:44:11 -0700188 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
189 /** {@hide} User-specified flag for expected upload size. */
190 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
191
192 /** {@hide} User-specified flag for expected download size. */
193 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
194
195 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
196 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
197
198 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700199 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700200
Fred Quintana4a6679b2009-08-17 13:05:39 -0700201 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700202 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800203 *
204 * Only the system and the shell user can set it.
205 *
206 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
207 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700208 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800209
210 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700211 * Set by the SyncManager to request that the SyncAdapter initialize itself for
212 * the given account/authority pair. One required initialization step is to
213 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
214 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
215 * do a full sync, though it is allowed to do so.
216 */
217 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
218
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800219 /** @hide */
220 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
221 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 public static final String SCHEME_CONTENT = "content";
224 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
225 public static final String SCHEME_FILE = "file";
226
227 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700228 * An extra {@link Point} describing the optimal size for a requested image
229 * resource, in pixels. If a provider has multiple sizes of the image, it
230 * should return the image closest to this size.
231 *
232 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
233 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
234 * CancellationSignal)
235 */
236 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
237
238 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800239 * An extra boolean describing whether a particular provider supports refresh
240 * or not. If a provider supports refresh, it should include this key in its
241 * returned Cursor as part of its query call.
242 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800243 */
244 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
245
246 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800247 * Key for an SQL style selection string that may be present in the query Bundle argument
248 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
249 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800250 *
251 * <p>Clients should never include user supplied values directly in the selection string,
252 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
253 * should use standard placeholder notation to represent values in a selection string,
254 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
255 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800256 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
257 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700258 *
259 * @see #QUERY_ARG_SORT_COLUMNS
260 * @see #QUERY_ARG_SORT_DIRECTION
261 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800262 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700263 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800264
265 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800266 * Key for SQL selection string arguments list.
267 *
268 * <p>Clients should never include user supplied values directly in the selection string,
269 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
270 * should use standard placeholder notation to represent values in a selection string,
271 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
272 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800273 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
274 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700275 *
276 * @see #QUERY_ARG_SORT_COLUMNS
277 * @see #QUERY_ARG_SORT_DIRECTION
278 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800279 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700280 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
281 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800282
283 /**
284 * Key for an SQL style sort string that may be present in the query Bundle argument
285 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
286 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800287 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800288 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
289 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700290 *
291 * @see #QUERY_ARG_SORT_COLUMNS
292 * @see #QUERY_ARG_SORT_DIRECTION
293 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800294 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700295 public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800296
Jeff Sharkey6adc98c2018-07-12 19:47:49 -0600297 /** {@hide} */
298 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
299 /** {@hide} */
300 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
301 /** {@hide} */
302 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
303
Steve McKay29c3f682016-12-16 14:52:59 -0800304 /**
Steve McKayd7ece9f2017-01-12 16:59:59 -0800305 * Specifies the list of columns against which to sort results. When first column values
306 * are identical, records are then sorted based on second column values, and so on.
Steve McKay29c3f682016-12-16 14:52:59 -0800307 *
308 * <p>Columns present in this list must also be included in the projection
309 * supplied to {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
310 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800311 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKay29c3f682016-12-16 14:52:59 -0800312 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700313 * <li>{@link ContentProvider} implementations: When preparing data in
314 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort columns
315 * is reflected in the returned Cursor, it is strongly recommended that
316 * {@link #QUERY_ARG_SORT_COLUMNS} then be included in the array of honored arguments
317 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800318 *
319 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
320 * arguments {@link Bundle}, the Content framework will attempt to synthesize
321 * an QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800322 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700323 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800324
325 /**
326 * Specifies desired sort order. When unspecified a provider may provide a default
327 * sort direction, or choose to return unsorted results.
328 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800329 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
330 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700331 * <li>{@link ContentProvider} implementations: When preparing data in
332 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
333 * is reflected in the returned Cursor, it is strongly recommended that
334 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
335 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800336 *
337 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
338 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700339 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800340 *
341 * @see #QUERY_SORT_DIRECTION_ASCENDING
342 * @see #QUERY_SORT_DIRECTION_DESCENDING
343 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700344 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800345
346 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700347 * Allows client to specify a hint to the provider declaring which collation
Steve McKay29c3f682016-12-16 14:52:59 -0800348 * to use when sorting text values.
349 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700350 * <p>Providers may support custom collators. When specifying a custom collator
351 * the value is determined by the Provider.
Steve McKay29c3f682016-12-16 14:52:59 -0800352 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700353 * <li>{@link ContentProvider} implementations: When preparing data in
354 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort collation
355 * is reflected in the returned Cursor, it is strongly recommended that
356 * {@link #QUERY_ARG_SORT_COLLATION} then be included in the array of honored arguments
357 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKay29c3f682016-12-16 14:52:59 -0800358 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700359 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
Steve McKayd7ece9f2017-01-12 16:59:59 -0800360 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700361 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800362 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700363 * @see java.text.Collator#PRIMARY
364 * @see java.text.Collator#SECONDARY
365 * @see java.text.Collator#TERTIARY
366 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800367 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700368 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800369
Steve McKay415f41b2017-02-01 13:38:25 -0800370 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700371 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800372 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700373 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800374 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
375 * when any QUERY_ARG_SORT* value was honored during the preparation of the
376 * results {@link Cursor}.
377 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700378 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
379 *
380 * @see #QUERY_ARG_SORT_COLUMNS
381 * @see #QUERY_ARG_SORT_DIRECTION
382 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800383 */
384 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
385
Steve McKay29c3f682016-12-16 14:52:59 -0800386 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700387 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800388 QUERY_SORT_DIRECTION_ASCENDING,
389 QUERY_SORT_DIRECTION_DESCENDING
390 })
391 @Retention(RetentionPolicy.SOURCE)
392 public @interface SortDirection {}
393 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
394 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
395
396 /**
397 * @see {@link java.text.Collector} for details on respective collation strength.
398 * @hide
399 */
400 @IntDef(flag = false, value = {
401 java.text.Collator.PRIMARY,
402 java.text.Collator.SECONDARY,
403 java.text.Collator.TERTIARY,
404 java.text.Collator.IDENTICAL
405 })
406 @Retention(RetentionPolicy.SOURCE)
407 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800408
409 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700410 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800411 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700412 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800413
414 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700415 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800416 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700417 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800418
419 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700420 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
421 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800422 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800423 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700424 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800425 *
426 * <p>Providers having returned -1 in a previous query are recommended to
427 * send content change notification once (if) full recordset size becomes
428 * known.
429 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700430 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800431
432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * This is the Android platform's base MIME type for a content: URI
434 * containing a Cursor of a single item. Applications should use this
435 * as the base type along with their own sub-type of their content: URIs
436 * that represent a particular item. For example, hypothetical IMAP email
437 * client may have a URI
438 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
439 * message in the inbox, whose MIME type would be reported as
440 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
443 */
444 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 /**
447 * This is the Android platform's base MIME type for a content: URI
448 * containing a Cursor of zero or more items. Applications should use this
449 * as the base type along with their own sub-type of their content: URIs
450 * that represent a directory of items. For example, hypothetical IMAP email
451 * client may have a URI
452 * <code>content://com.company.provider.imap/inbox</code> for all of the
453 * messages in its inbox, whose MIME type would be reported as
454 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * <p>Note how the base MIME type varies between this and
457 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
458 * one single item or multiple items in the data set, while the sub-type
459 * remains the same because in either case the data structure contained
460 * in the cursor is the same.
461 */
462 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700463
Matt Caseybd7bcf02014-02-05 15:51:39 -0800464 /**
465 * This is the Android platform's generic MIME type to match any MIME
466 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
467 * {@code SUB_TYPE} is the sub-type of the application-dependent
468 * content, e.g., "audio", "video", "playlist".
469 */
470 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
471
Jeff Sharkey91e3cd42018-08-27 18:03:33 -0600472 /**
473 * Default MIME type for files whose type is otherwise unknown.
474 * @hide
475 */
476 public static final String MIME_TYPE_DEFAULT = "application/octet-stream";
477
Fred Quintanaac9385e2009-06-22 18:00:59 -0700478 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100479 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700480 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
481 /** @hide */
482 public static final int SYNC_ERROR_AUTHENTICATION = 2;
483 /** @hide */
484 public static final int SYNC_ERROR_IO = 3;
485 /** @hide */
486 public static final int SYNC_ERROR_PARSE = 4;
487 /** @hide */
488 public static final int SYNC_ERROR_CONFLICT = 5;
489 /** @hide */
490 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
491 /** @hide */
492 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
493 /** @hide */
494 public static final int SYNC_ERROR_INTERNAL = 8;
495
Alon Albert57286f92012-10-09 14:21:38 -0700496 private static final String[] SYNC_ERROR_NAMES = new String[] {
497 "already-in-progress",
498 "authentication-error",
499 "io-error",
500 "parse-error",
501 "conflict",
502 "too-many-deletions",
503 "too-many-retries",
504 "internal-error",
505 };
506
507 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800508 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700509 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
510 return String.valueOf(error);
511 }
512 return SYNC_ERROR_NAMES[error - 1];
513 }
514
Alon Albert5c113fa2013-02-07 08:07:32 -0800515 /** @hide */
516 public static int syncErrorStringToInt(String error) {
517 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
518 if (SYNC_ERROR_NAMES[i].equals(error)) {
519 return i + 1;
520 }
521 }
522 if (error != null) {
523 try {
524 return Integer.parseInt(error);
525 } catch (NumberFormatException e) {
526 Log.d(TAG, "error parsing sync error: " + error);
527 }
528 }
529 return 0;
530 }
531
Fred Quintanaac9385e2009-06-22 18:00:59 -0700532 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700533 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700534 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
535 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100536 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700537 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
538 /** @hide */
539 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
540
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700541 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700542 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
543 NOTIFY_SYNC_TO_NETWORK,
544 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS
545 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700546 @Retention(RetentionPolicy.SOURCE)
547 public @interface NotifyFlags {}
548
549 /**
550 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
551 * to the network.
552 */
553 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
554
555 /**
556 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
557 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
558 * using "notify for descendants." The purpose of this is to allow the provide to send
559 * a general notification of "something under X" changed that observers of that specific
560 * URI can receive, while also sending a specific URI under X. It would use this flag
561 * when sending the former, so that observers of "X and descendants" only see the latter.
562 */
563 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
564
Makoto Onuki75ad2492018-03-28 14:42:42 -0700565 /**
566 * No exception, throttled by app standby normally.
567 * @hide
568 */
569 public static final int SYNC_EXEMPTION_NONE = 0;
570
571 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700572 * Exemption given to a sync request made by a foreground app (including
573 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700574 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700575 * At the schedule time, we promote the sync adapter app for a higher bucket:
576 * - If the device is not dozing (so the sync will start right away)
577 * promote to ACTIVE for 1 hour.
578 * - If the device is dozing (so the sync *won't* start right away),
579 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
580 * device comes out of doze.
581 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
582 * so it can schedule and start more syncs without getting throttled, even when the first
583 * operation was canceled and now we're retrying.
584 *
585 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700586 * @hide
587 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700588 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700589
590 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700591 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700592 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
593 * @hide
594 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700595 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700596
597 /** @hide */
598 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
599 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700600 SYNC_EXEMPTION_PROMOTE_BUCKET,
601 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700602 })
603 @Retention(RetentionPolicy.SOURCE)
604 public @interface SyncExemption {}
605
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800606 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800607 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700608 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800609 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800610 private final Random mRandom = new Random(); // guarded by itself
611
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700612 public ContentResolver(@Nullable Context context) {
613 this(context, null);
614 }
615
616 /** {@hide} */
617 public ContentResolver(@Nullable Context context, @Nullable ContentInterface wrapped) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800618 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700619 mPackageName = mContext.getOpPackageName();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000620 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700621 mWrapped = wrapped;
622 }
623
624 /** {@hide} */
625 public static ContentResolver wrap(@NonNull ContentInterface wrapped) {
626 Preconditions.checkNotNull(wrapped);
627
628 return new ContentResolver(null, wrapped) {
629 @Override
630 public void unstableProviderDied(IContentProvider icp) {
631 throw new UnsupportedOperationException();
632 }
633 @Override
634 public boolean releaseUnstableProvider(IContentProvider icp) {
635 throw new UnsupportedOperationException();
636 }
637 @Override
638 public boolean releaseProvider(IContentProvider icp) {
639 throw new UnsupportedOperationException();
640 }
641 @Override
642 protected IContentProvider acquireUnstableProvider(Context c, String name) {
643 throw new UnsupportedOperationException();
644 }
645 @Override
646 protected IContentProvider acquireProvider(Context c, String name) {
647 throw new UnsupportedOperationException();
648 }
649 };
650 }
651
652 /**
653 * Create a {@link ContentResolver} instance that redirects all its methods
654 * to the given {@link ContentProvider}.
655 */
656 public static ContentResolver wrap(@NonNull ContentProvider wrapped) {
657 return wrap((ContentInterface) wrapped);
658 }
659
660 /**
661 * Create a {@link ContentResolver} instance that redirects all its methods
662 * to the given {@link ContentProviderClient}.
663 */
664 public static ContentResolver wrap(@NonNull ContentProviderClient wrapped) {
665 return wrap((ContentInterface) wrapped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
668 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100669 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700671
672 /**
673 * Providing a default implementation of this, to avoid having to change a
674 * lot of other things, but implementations of ContentResolver should
675 * implement it.
676 *
677 * @hide
678 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100679 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700680 protected IContentProvider acquireExistingProvider(Context c, String name) {
681 return acquireProvider(c, name);
682 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100685 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700687 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100688 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700689 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
690 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100691 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700692 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700693 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100694 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700695 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700697 /** @hide */
698 public void appNotRespondingViaProvider(IContentProvider icp) {
699 throw new UnsupportedOperationException("appNotRespondingViaProvider");
700 }
701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 /**
703 * Return the MIME type of the given content URL.
704 *
705 * @param url A Uri identifying content (either a list or specific type),
706 * using the content:// scheme.
707 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
708 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700709 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700710 public final @Nullable String getType(@NonNull Uri url) {
711 Preconditions.checkNotNull(url, "url");
712
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700713 try {
714 if (mWrapped != null) return mWrapped.getType(url);
715 } catch (RemoteException e) {
716 return null;
717 }
718
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700719 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700720 IContentProvider provider = acquireExistingProvider(url);
721 if (provider != null) {
722 try {
723 return provider.getType(url);
724 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600725 // Arbitrary and not worth documenting, as Activity
726 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700727 return null;
728 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100729 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700730 return null;
731 } finally {
732 releaseProvider(provider);
733 }
734 }
735
736 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 return null;
738 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800741 String type = ActivityManager.getService().getProviderMimeType(
Nicolas Prevot1dddc7f2014-07-07 17:44:58 +0100742 ContentProvider.getUriWithoutUserId(url), resolveUserId(url));
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700743 return type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600745 throw e.rethrowFromSystemServer();
Ola Olsson145e6c42010-12-20 16:45:35 +0100746 } catch (java.lang.Exception e) {
747 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
748 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750 }
751
752 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700753 * Query for the possible MIME types for the representations the given
754 * content URL can be returned when opened as as stream with
755 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
756 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700757 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700758 * data that they contain.
759 *
760 * @param url A Uri identifying content (either a list or specific type),
761 * using the content:// scheme.
762 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500763 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700764 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700765 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700766 * data streams that match the given mimeTypeFilter. If there are none,
767 * null is returned.
768 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700769 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -0700770 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
771 Preconditions.checkNotNull(url, "url");
772 Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter");
773
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700774 try {
775 if (mWrapped != null) return mWrapped.getStreamTypes(url, mimeTypeFilter);
776 } catch (RemoteException e) {
777 return null;
778 }
779
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700780 IContentProvider provider = acquireProvider(url);
781 if (provider == null) {
782 return null;
783 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700784
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700785 try {
786 return provider.getStreamTypes(url, mimeTypeFilter);
787 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800788 // Arbitrary and not worth documenting, as Activity
789 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700790 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700791 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800792 releaseProvider(provider);
793 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700794 }
795
796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800798 * <p>
799 * For best performance, the caller should follow these guidelines:
800 * <ul>
801 * <li>Provide an explicit projection, to prevent
802 * reading data from storage that aren't going to be used.</li>
803 * <li>Use question mark parameter markers such as 'phone=?' instead of
804 * explicit values in the {@code selection} parameter, so that queries
805 * that differ only by those values will be recognized as the same
806 * for caching purposes.</li>
807 * </ul>
808 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 *
810 * @param uri The URI, using the content:// scheme, for the content to
811 * retrieve.
812 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800813 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 * @param selection A filter declaring which rows to return, formatted as an
815 * SQL WHERE clause (excluding the WHERE itself). Passing null will
816 * return all rows for the given URI.
817 * @param selectionArgs You may include ?s in selection, which will be
818 * replaced by the values from selectionArgs, in the order that they
819 * appear in the selection. The values will be bound as Strings.
820 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
821 * clause (excluding the ORDER BY itself). Passing null will use the
822 * default sort order, which may be unordered.
823 * @return A Cursor object, which is positioned before the first entry, or null
824 * @see Cursor
825 */
Tor Norbye788fc2b2015-07-05 16:10:42 -0700826 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
827 @Nullable String[] projection, @Nullable String selection,
828 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800829 return query(uri, projection, selection, selectionArgs, sortOrder, null);
830 }
831
832 /**
Jeff Brownc64ff372013-10-09 18:50:56 -0700833 * Query the given URI, returning a {@link Cursor} over the result set
834 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800835 * <p>
836 * For best performance, the caller should follow these guidelines:
837 * <ul>
838 * <li>Provide an explicit projection, to prevent
839 * reading data from storage that aren't going to be used.</li>
840 * <li>Use question mark parameter markers such as 'phone=?' instead of
841 * explicit values in the {@code selection} parameter, so that queries
842 * that differ only by those values will be recognized as the same
843 * for caching purposes.</li>
844 * </ul>
845 * </p>
846 *
847 * @param uri The URI, using the content:// scheme, for the content to
848 * retrieve.
849 * @param projection A list of which columns to return. Passing null will
850 * return all columns, which is inefficient.
851 * @param selection A filter declaring which rows to return, formatted as an
852 * SQL WHERE clause (excluding the WHERE itself). Passing null will
853 * return all rows for the given URI.
854 * @param selectionArgs You may include ?s in selection, which will be
855 * replaced by the values from selectionArgs, in the order that they
856 * appear in the selection. The values will be bound as Strings.
857 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
858 * clause (excluding the ORDER BY itself). Passing null will use the
859 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -0800860 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800861 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
862 * when the query is executed.
863 * @return A Cursor object, which is positioned before the first entry, or null
864 * @see Cursor
865 */
Steve McKayea93fe72016-12-02 11:35:35 -0800866 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -0700867 @Nullable String[] projection, @Nullable String selection,
868 @Nullable String[] selectionArgs, @Nullable String sortOrder,
869 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -0800870 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
871 return query(uri, projection, queryArgs, cancellationSignal);
872 }
873
874 /**
875 * Query the given URI, returning a {@link Cursor} over the result set
876 * with support for cancellation.
877 *
878 * <p>For best performance, the caller should follow these guidelines:
879 *
880 * <li>Provide an explicit projection, to prevent reading data from storage
881 * that aren't going to be used.
882 *
Steve McKay415f41b2017-02-01 13:38:25 -0800883 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
884 * the preparation of the result set by including the respective argument keys
885 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
886 * for details.
887 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -0700888 * @see #QUERY_ARG_SORT_COLUMNS
889 * @see #QUERY_ARG_SORT_DIRECTION
890 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800891 *
Steve McKayea93fe72016-12-02 11:35:35 -0800892 * @param uri The URI, using the content:// scheme, for the content to
893 * retrieve.
894 * @param projection A list of which columns to return. Passing null will
895 * return all columns, which is inefficient.
896 * @param queryArgs A Bundle containing any arguments to the query.
897 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
898 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
899 * when the query is executed.
900 * @return A Cursor object, which is positioned before the first entry, or null
901 * @see Cursor
902 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700903 @Override
Steve McKayea93fe72016-12-02 11:35:35 -0800904 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
905 @Nullable String[] projection, @Nullable Bundle queryArgs,
906 @Nullable CancellationSignal cancellationSignal) {
Jeff Sharkey673db442015-06-11 19:30:57 -0700907 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -0700908
909 try {
910 if (mWrapped != null) {
911 return mWrapped.query(uri, projection, queryArgs, cancellationSignal);
912 }
913 } catch (RemoteException e) {
914 return null;
915 }
916
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700917 IContentProvider unstableProvider = acquireUnstableProvider(uri);
918 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 return null;
920 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700921 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800922 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800924 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -0800925
Jeff Brown4c1241d2012-02-02 17:05:00 -0800926 ICancellationSignal remoteCancellationSignal = null;
927 if (cancellationSignal != null) {
928 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700929 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -0800930 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -0800931 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700932 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800933 qCursor = unstableProvider.query(mPackageName, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -0800934 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700935 } catch (DeadObjectException e) {
936 // The remote process has died... but we only hold an unstable
937 // reference though, so we might recover!!! Let's try!!!!
938 // This is exciting!!1!!1!!!!1
939 unstableProviderDied(unstableProvider);
940 stableProvider = acquireProvider(uri);
941 if (stableProvider == null) {
942 return null;
943 }
Steve McKayea93fe72016-12-02 11:35:35 -0800944 qCursor = stableProvider.query(
945 mPackageName, uri, projection, queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700946 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800947 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 return null;
949 }
Jeff Brownc21b5a02013-01-07 17:15:12 -0800950
951 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -0700952 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800953 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -0800954 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -0800955
956 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700957 final IContentProvider provider = (stableProvider != null) ? stableProvider
958 : acquireProvider(uri);
959 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700960 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800961 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700962 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800964 // Arbitrary and not worth documenting, as Activity
965 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700967 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -0800968 if (qCursor != null) {
969 qCursor.close();
970 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700971 if (cancellationSignal != null) {
972 cancellationSignal.setRemote(null);
973 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700974 if (unstableProvider != null) {
975 releaseUnstableProvider(unstableProvider);
976 }
977 if (stableProvider != null) {
978 releaseProvider(stableProvider);
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981 }
982
Fred Quintana89437372009-05-15 15:10:40 -0700983 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700984 * Transform the given <var>url</var> to a canonical representation of
985 * its referenced resource, which can be used across devices, persisted,
986 * backed up and restored, etc. The returned Uri is still a fully capable
987 * Uri for use with its content provider, allowing you to do all of the
988 * same content provider operations as with the original Uri --
989 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
990 * only difference in behavior between the original and new Uris is that
991 * the content provider may need to do some additional work at each call
992 * using it to resolve it to the correct resource, especially if the
993 * canonical Uri has been moved to a different environment.
994 *
995 * <p>If you are moving a canonical Uri between environments, you should
996 * perform another call to {@link #canonicalize} with that original Uri to
997 * re-canonicalize it for the current environment. Alternatively, you may
998 * want to use {@link #uncanonicalize} to transform it to a non-canonical
999 * Uri that works only in the current environment but potentially more
1000 * efficiently than the canonical representation.</p>
1001 *
1002 * @param url The {@link Uri} that is to be transformed to a canonical
1003 * representation. Like all resolver calls, the input can be either
1004 * a non-canonical or canonical Uri.
1005 *
1006 * @return Returns the official canonical representation of <var>url</var>,
1007 * or null if the content provider does not support a canonical representation
1008 * of the given Uri. Many providers may not support canonicalization of some
1009 * or all of their Uris.
1010 *
1011 * @see #uncanonicalize
1012 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001013 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001014 public final @Nullable Uri canonicalize(@NonNull Uri url) {
1015 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001016
1017 try {
1018 if (mWrapped != null) return mWrapped.canonicalize(url);
1019 } catch (RemoteException e) {
1020 return null;
1021 }
1022
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001023 IContentProvider provider = acquireProvider(url);
1024 if (provider == null) {
1025 return null;
1026 }
1027
1028 try {
1029 return provider.canonicalize(mPackageName, url);
1030 } catch (RemoteException e) {
1031 // Arbitrary and not worth documenting, as Activity
1032 // Manager will kill this process shortly anyway.
1033 return null;
1034 } finally {
1035 releaseProvider(provider);
1036 }
1037 }
1038
1039 /**
1040 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
1041 * it to its local non-canonical form. This can be useful in some cases where
1042 * you know that you will only be using the Uri in the current environment and
1043 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001044 * provider or want to verify that the referenced data exists at all in the
1045 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001046 *
1047 * @param url The canonical {@link Uri} that is to be convered back to its
1048 * non-canonical form.
1049 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -07001050 * @return Returns the non-canonical representation of <var>url</var>. This will
1051 * return null if data identified by the canonical Uri can not be found in
1052 * the current environment; callers must always check for null and deal with
1053 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001054 *
1055 * @see #canonicalize
1056 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001057 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001058 public final @Nullable Uri uncanonicalize(@NonNull Uri url) {
1059 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001060
1061 try {
1062 if (mWrapped != null) return mWrapped.uncanonicalize(url);
1063 } catch (RemoteException e) {
1064 return null;
1065 }
1066
Dianne Hackborn38ed2a42013-09-06 16:17:22 -07001067 IContentProvider provider = acquireProvider(url);
1068 if (provider == null) {
1069 return null;
1070 }
1071
1072 try {
1073 return provider.uncanonicalize(mPackageName, url);
1074 } catch (RemoteException e) {
1075 // Arbitrary and not worth documenting, as Activity
1076 // Manager will kill this process shortly anyway.
1077 return null;
1078 } finally {
1079 releaseProvider(provider);
1080 }
1081 }
1082
1083 /**
Ben Lin1cf454f2016-11-10 13:50:54 -08001084 * This allows clients to request an explicit refresh of content identified by {@code uri}.
1085 * <p>
1086 * Client code should only invoke this method when there is a strong indication (such as a user
1087 * initiated pull to refresh gesture) that the content is stale.
1088 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -08001089 *
Ben Lin2b64a882016-11-11 15:24:58 -08001090 * @param url The Uri identifying the data to refresh.
Ben Lin1cf454f2016-11-10 13:50:54 -08001091 * @param args Additional options from the client. The definitions of these are specific to the
1092 * content provider being called.
1093 * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if
1094 * none. For example, if you called refresh on a particular uri, you should call
1095 * {@link CancellationSignal#throwIfCanceled()} to check whether the client has
1096 * canceled the refresh request.
1097 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -08001098 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001099 @Override
Ben Lin1cf454f2016-11-10 13:50:54 -08001100 public final boolean refresh(@NonNull Uri url, @Nullable Bundle args,
1101 @Nullable CancellationSignal cancellationSignal) {
1102 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001103
1104 try {
1105 if (mWrapped != null) return mWrapped.refresh(url, args, cancellationSignal);
1106 } catch (RemoteException e) {
1107 return false;
1108 }
1109
Ben Lin1cf454f2016-11-10 13:50:54 -08001110 IContentProvider provider = acquireProvider(url);
1111 if (provider == null) {
1112 return false;
1113 }
1114
1115 try {
1116 ICancellationSignal remoteCancellationSignal = null;
1117 if (cancellationSignal != null) {
1118 cancellationSignal.throwIfCanceled();
1119 remoteCancellationSignal = provider.createCancellationSignal();
1120 cancellationSignal.setRemote(remoteCancellationSignal);
1121 }
1122 return provider.refresh(mPackageName, url, args, remoteCancellationSignal);
1123 } catch (RemoteException e) {
1124 // Arbitrary and not worth documenting, as Activity
1125 // Manager will kill this process shortly anyway.
1126 return false;
1127 } finally {
1128 releaseProvider(provider);
1129 }
1130 }
1131
1132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 * Open a stream on to the content associated with a content URI. If there
1134 * is no data associated with the URI, FileNotFoundException is thrown.
1135 *
1136 * <h5>Accepts the following URI schemes:</h5>
1137 * <ul>
1138 * <li>content ({@link #SCHEME_CONTENT})</li>
1139 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1140 * <li>file ({@link #SCHEME_FILE})</li>
1141 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1144 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 * @param uri The desired URI.
1147 * @return InputStream
1148 * @throws FileNotFoundException if the provided URI could not be opened.
1149 * @see #openAssetFileDescriptor(Uri, String)
1150 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001151 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 throws FileNotFoundException {
Jeff Sharkey673db442015-06-11 19:30:57 -07001153 Preconditions.checkNotNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 String scheme = uri.getScheme();
1155 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1156 // Note: left here to avoid breaking compatibility. May be removed
1157 // with sufficient testing.
1158 OpenResourceIdResult r = getResourceId(uri);
1159 try {
1160 InputStream stream = r.r.openRawResource(r.id);
1161 return stream;
1162 } catch (Resources.NotFoundException ex) {
1163 throw new FileNotFoundException("Resource does not exist: " + uri);
1164 }
1165 } else if (SCHEME_FILE.equals(scheme)) {
1166 // Note: left here to avoid breaking compatibility. May be removed
1167 // with sufficient testing.
1168 return new FileInputStream(uri.getPath());
1169 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001170 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 try {
1172 return fd != null ? fd.createInputStream() : null;
1173 } catch (IOException e) {
1174 throw new FileNotFoundException("Unable to create stream");
1175 }
1176 }
1177 }
1178
1179 /**
1180 * Synonym for {@link #openOutputStream(Uri, String)
1181 * openOutputStream(uri, "w")}.
1182 * @throws FileNotFoundException if the provided URI could not be opened.
1183 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001184 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 throws FileNotFoundException {
1186 return openOutputStream(uri, "w");
1187 }
1188
1189 /**
1190 * Open a stream on to the content associated with a content URI. If there
1191 * is no data associated with the URI, FileNotFoundException is thrown.
1192 *
1193 * <h5>Accepts the following URI schemes:</h5>
1194 * <ul>
1195 * <li>content ({@link #SCHEME_CONTENT})</li>
1196 * <li>file ({@link #SCHEME_FILE})</li>
1197 * </ul>
1198 *
1199 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1200 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 * @param uri The desired URI.
1203 * @param mode May be "w", "wa", "rw", or "rwt".
1204 * @return OutputStream
1205 * @throws FileNotFoundException if the provided URI could not be opened.
1206 * @see #openAssetFileDescriptor(Uri, String)
1207 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001208 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001210 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 try {
1212 return fd != null ? fd.createOutputStream() : null;
1213 } catch (IOException e) {
1214 throw new FileNotFoundException("Unable to create stream");
1215 }
1216 }
1217
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001218 @Override
1219 public final @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
1220 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001221 try {
1222 if (mWrapped != null) return mWrapped.openFile(uri, mode, signal);
1223 } catch (RemoteException e) {
1224 return null;
1225 }
1226
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001227 return openFileDescriptor(uri, mode, signal);
1228 }
1229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001231 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1233 * underlying {@link ContentProvider#openFile}
1234 * ContentProvider.openFile()} method, so will <em>not</em> work with
1235 * providers that return sub-sections of files. If at all possible,
1236 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1237 * will receive a FileNotFoundException exception if the provider returns a
1238 * sub-section of a file.
1239 *
1240 * <h5>Accepts the following URI schemes:</h5>
1241 * <ul>
1242 * <li>content ({@link #SCHEME_CONTENT})</li>
1243 * <li>file ({@link #SCHEME_FILE})</li>
1244 * </ul>
1245 *
1246 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1247 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001248 * <p>
1249 * If opening with the exclusive "r" or "w" modes, the returned
1250 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1251 * of data. Opening with the "rw" mode implies a file on disk that supports
1252 * seeking. If possible, always use an exclusive mode to give the underlying
1253 * {@link ContentProvider} the most flexibility.
1254 * <p>
1255 * If you are writing a file, and need to communicate an error to the
1256 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 * @param uri The desired URI to open.
1259 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1260 * ContentProvider.openFile}.
1261 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1262 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001263 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 * file exists under the URI or the mode is invalid.
1265 * @see #openAssetFileDescriptor(Uri, String)
1266 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001267 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1268 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001269 return openFileDescriptor(uri, mode, null);
1270 }
1271
1272 /**
1273 * Open a raw file descriptor to access data under a URI. This
1274 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1275 * underlying {@link ContentProvider#openFile}
1276 * ContentProvider.openFile()} method, so will <em>not</em> work with
1277 * providers that return sub-sections of files. If at all possible,
1278 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1279 * will receive a FileNotFoundException exception if the provider returns a
1280 * sub-section of a file.
1281 *
1282 * <h5>Accepts the following URI schemes:</h5>
1283 * <ul>
1284 * <li>content ({@link #SCHEME_CONTENT})</li>
1285 * <li>file ({@link #SCHEME_FILE})</li>
1286 * </ul>
1287 *
1288 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1289 * on these schemes.
1290 * <p>
1291 * If opening with the exclusive "r" or "w" modes, the returned
1292 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1293 * of data. Opening with the "rw" mode implies a file on disk that supports
1294 * seeking. If possible, always use an exclusive mode to give the underlying
1295 * {@link ContentProvider} the most flexibility.
1296 * <p>
1297 * If you are writing a file, and need to communicate an error to the
1298 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1299 *
1300 * @param uri The desired URI to open.
1301 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1302 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001303 * @param cancellationSignal A signal to cancel the operation in progress,
1304 * or null if none. If the operation is canceled, then
1305 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001306 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1307 * own this descriptor and are responsible for closing it when done.
1308 * @throws FileNotFoundException Throws FileNotFoundException if no
1309 * file exists under the URI or the mode is invalid.
1310 * @see #openAssetFileDescriptor(Uri, String)
1311 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001312 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1313 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1314 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001315 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 if (afd == null) {
1317 return null;
1318 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (afd.getDeclaredLength() < 0) {
1321 // This is a full file!
1322 return afd.getParcelFileDescriptor();
1323 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 // Client can't handle a sub-section of a file, so close what
1326 // we got and bail with an exception.
1327 try {
1328 afd.close();
1329 } catch (IOException e) {
1330 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 throw new FileNotFoundException("Not a whole file");
1333 }
1334
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001335 @Override
1336 public final @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
1337 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001338 try {
1339 if (mWrapped != null) return mWrapped.openAssetFile(uri, mode, signal);
1340 } catch (RemoteException e) {
1341 return null;
1342 }
1343
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001344 return openAssetFileDescriptor(uri, mode, signal);
1345 }
1346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001348 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001350 * 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 -08001351 *
1352 * <h5>Accepts the following URI schemes:</h5>
1353 * <ul>
1354 * <li>content ({@link #SCHEME_CONTENT})</li>
1355 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1356 * <li>file ({@link #SCHEME_FILE})</li>
1357 * </ul>
1358 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1359 * <p>
1360 * A Uri object can be used to reference a resource in an APK file. The
1361 * Uri should be one of the following formats:
1362 * <ul>
1363 * <li><code>android.resource://package_name/id_number</code><br/>
1364 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1365 * For example <code>com.example.myapp</code><br/>
1366 * <code>id_number</code> is the int form of the ID.<br/>
1367 * The easiest way to construct this form is
1368 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1369 * </li>
1370 * <li><code>android.resource://package_name/type/name</code><br/>
1371 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1372 * For example <code>com.example.myapp</code><br/>
1373 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1374 * or <code>drawable</code>.
1375 * <code>name</code> is the string form of the resource name. That is, whatever the file
1376 * name was in your res directory, without the type extension.
1377 * The easiest way to construct this form is
1378 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1379 * </li>
1380 * </ul>
1381 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001382 * <p>Note that if this function is called for read-only input (mode is "r")
1383 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001384 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001385 * from any built-in data conversion that a provider implements.
1386 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 * @param uri The desired URI to open.
1388 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1389 * ContentProvider.openAssetFile}.
1390 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1391 * own this descriptor and are responsible for closing it when done.
1392 * @throws FileNotFoundException Throws FileNotFoundException of no
1393 * file exists under the URI or the mode is invalid.
1394 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001395 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1396 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001397 return openAssetFileDescriptor(uri, mode, null);
1398 }
1399
1400 /**
1401 * Open a raw file descriptor to access data under a URI. This
1402 * interacts with the underlying {@link ContentProvider#openAssetFile}
1403 * method of the provider associated with the given URI, to retrieve any file stored there.
1404 *
1405 * <h5>Accepts the following URI schemes:</h5>
1406 * <ul>
1407 * <li>content ({@link #SCHEME_CONTENT})</li>
1408 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1409 * <li>file ({@link #SCHEME_FILE})</li>
1410 * </ul>
1411 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1412 * <p>
1413 * A Uri object can be used to reference a resource in an APK file. The
1414 * Uri should be one of the following formats:
1415 * <ul>
1416 * <li><code>android.resource://package_name/id_number</code><br/>
1417 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1418 * For example <code>com.example.myapp</code><br/>
1419 * <code>id_number</code> is the int form of the ID.<br/>
1420 * The easiest way to construct this form is
1421 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1422 * </li>
1423 * <li><code>android.resource://package_name/type/name</code><br/>
1424 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1425 * For example <code>com.example.myapp</code><br/>
1426 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1427 * or <code>drawable</code>.
1428 * <code>name</code> is the string form of the resource name. That is, whatever the file
1429 * name was in your res directory, without the type extension.
1430 * The easiest way to construct this form is
1431 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1432 * </li>
1433 * </ul>
1434 *
1435 * <p>Note that if this function is called for read-only input (mode is "r")
1436 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001437 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001438 * from any built-in data conversion that a provider implements.
1439 *
1440 * @param uri The desired URI to open.
1441 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1442 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001443 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001444 * none. If the operation is canceled, then
1445 * {@link OperationCanceledException} will be thrown.
1446 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1447 * own this descriptor and are responsible for closing it when done.
1448 * @throws FileNotFoundException Throws FileNotFoundException of no
1449 * file exists under the URI or the mode is invalid.
1450 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001451 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1452 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1453 throws FileNotFoundException {
1454 Preconditions.checkNotNull(uri, "uri");
1455 Preconditions.checkNotNull(mode, "mode");
1456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 String scheme = uri.getScheme();
1458 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1459 if (!"r".equals(mode)) {
1460 throw new FileNotFoundException("Can't write resources: " + uri);
1461 }
1462 OpenResourceIdResult r = getResourceId(uri);
1463 try {
1464 return r.r.openRawResourceFd(r.id);
1465 } catch (Resources.NotFoundException ex) {
1466 throw new FileNotFoundException("Resource does not exist: " + uri);
1467 }
1468 } else if (SCHEME_FILE.equals(scheme)) {
1469 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001470 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 return new AssetFileDescriptor(pfd, 0, -1);
1472 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001473 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001474 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001475 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001476 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1477 if (unstableProvider == null) {
1478 throw new FileNotFoundException("No content provider: " + uri);
1479 }
1480 IContentProvider stableProvider = null;
1481 AssetFileDescriptor fd = null;
1482
1483 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001484 ICancellationSignal remoteCancellationSignal = null;
1485 if (cancellationSignal != null) {
1486 cancellationSignal.throwIfCanceled();
1487 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1488 cancellationSignal.setRemote(remoteCancellationSignal);
1489 }
1490
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001491 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001492 fd = unstableProvider.openAssetFile(
1493 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001494 if (fd == null) {
1495 // The provider will be released by the finally{} clause
1496 return null;
1497 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001498 } catch (DeadObjectException e) {
1499 // The remote process has died... but we only hold an unstable
1500 // reference though, so we might recover!!! Let's try!!!!
1501 // This is exciting!!1!!1!!!!1
1502 unstableProviderDied(unstableProvider);
1503 stableProvider = acquireProvider(uri);
1504 if (stableProvider == null) {
1505 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001506 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001507 fd = stableProvider.openAssetFile(
1508 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001509 if (fd == null) {
1510 // The provider will be released by the finally{} clause
1511 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001512 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001513 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001514
1515 if (stableProvider == null) {
1516 stableProvider = acquireProvider(uri);
1517 }
1518 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001519 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001520 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1521 fd.getParcelFileDescriptor(), stableProvider);
1522
1523 // Success! Don't release the provider when exiting, let
1524 // ParcelFileDescriptorInner do that when it is closed.
1525 stableProvider = null;
1526
1527 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1528 fd.getDeclaredLength());
1529
1530 } catch (RemoteException e) {
1531 // Whatever, whatever, we'll go away.
1532 throw new FileNotFoundException(
1533 "Failed opening content provider: " + uri);
1534 } catch (FileNotFoundException e) {
1535 throw e;
1536 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001537 if (cancellationSignal != null) {
1538 cancellationSignal.setRemote(null);
1539 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001540 if (stableProvider != null) {
1541 releaseProvider(stableProvider);
1542 }
1543 if (unstableProvider != null) {
1544 releaseUnstableProvider(unstableProvider);
1545 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001546 }
1547 }
1548 }
1549 }
1550
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001551 @Override
1552 public final @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
1553 @NonNull String mimeTypeFilter, @Nullable Bundle opts,
1554 @Nullable CancellationSignal signal) throws FileNotFoundException {
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001555 try {
1556 if (mWrapped != null) {
1557 return mWrapped.openTypedAssetFile(uri, mimeTypeFilter, opts, signal);
1558 }
1559 } catch (RemoteException e) {
1560 return null;
1561 }
1562
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001563 return openTypedAssetFileDescriptor(uri, mimeTypeFilter, opts, signal);
1564 }
1565
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001566 /**
1567 * Open a raw file descriptor to access (potentially type transformed)
1568 * data from a "content:" URI. This interacts with the underlying
1569 * {@link ContentProvider#openTypedAssetFile} method of the provider
1570 * associated with the given URI, to retrieve retrieve any appropriate
1571 * data stream for the data stored there.
1572 *
1573 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1574 * with "content:" URIs, because content providers are the only facility
1575 * with an associated MIME type to ensure that the returned data stream
1576 * is of the desired type.
1577 *
1578 * <p>All text/* streams are encoded in UTF-8.
1579 *
1580 * @param uri The desired URI to open.
1581 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001582 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001583 * select a type, though there is no way for you to determine what type
1584 * it is returning.
1585 * @param opts Additional provider-dependent options.
1586 * @return Returns a new ParcelFileDescriptor from which you can read the
1587 * data stream from the provider. Note that this may be a pipe, meaning
1588 * you can't seek in it. The only seek you should do is if the
1589 * AssetFileDescriptor contains an offset, to move to that offset before
1590 * reading. You own this descriptor and are responsible for closing it when done.
1591 * @throws FileNotFoundException Throws FileNotFoundException of no
1592 * data of the desired type exists under the URI.
1593 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001594 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1595 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001596 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1597 }
1598
1599 /**
1600 * Open a raw file descriptor to access (potentially type transformed)
1601 * data from a "content:" URI. This interacts with the underlying
1602 * {@link ContentProvider#openTypedAssetFile} method of the provider
1603 * associated with the given URI, to retrieve retrieve any appropriate
1604 * data stream for the data stored there.
1605 *
1606 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1607 * with "content:" URIs, because content providers are the only facility
1608 * with an associated MIME type to ensure that the returned data stream
1609 * is of the desired type.
1610 *
1611 * <p>All text/* streams are encoded in UTF-8.
1612 *
1613 * @param uri The desired URI to open.
1614 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001615 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001616 * select a type, though there is no way for you to determine what type
1617 * it is returning.
1618 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001619 * @param cancellationSignal A signal to cancel the operation in progress,
1620 * or null if none. If the operation is canceled, then
1621 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001622 * @return Returns a new ParcelFileDescriptor from which you can read the
1623 * data stream from the provider. Note that this may be a pipe, meaning
1624 * you can't seek in it. The only seek you should do is if the
1625 * AssetFileDescriptor contains an offset, to move to that offset before
1626 * reading. You own this descriptor and are responsible for closing it when done.
1627 * @throws FileNotFoundException Throws FileNotFoundException of no
1628 * data of the desired type exists under the URI.
1629 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001630 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1631 @NonNull String mimeType, @Nullable Bundle opts,
1632 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
1633 Preconditions.checkNotNull(uri, "uri");
1634 Preconditions.checkNotNull(mimeType, "mimeType");
1635
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001636 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1637 if (unstableProvider == null) {
1638 throw new FileNotFoundException("No content provider: " + uri);
1639 }
1640 IContentProvider stableProvider = null;
1641 AssetFileDescriptor fd = null;
1642
1643 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001644 ICancellationSignal remoteCancellationSignal = null;
1645 if (cancellationSignal != null) {
1646 cancellationSignal.throwIfCanceled();
1647 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1648 cancellationSignal.setRemote(remoteCancellationSignal);
1649 }
1650
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001651 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001652 fd = unstableProvider.openTypedAssetFile(
1653 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001654 if (fd == null) {
1655 // The provider will be released by the finally{} clause
1656 return null;
1657 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001658 } catch (DeadObjectException e) {
1659 // The remote process has died... but we only hold an unstable
1660 // reference though, so we might recover!!! Let's try!!!!
1661 // This is exciting!!1!!1!!!!1
1662 unstableProviderDied(unstableProvider);
1663 stableProvider = acquireProvider(uri);
1664 if (stableProvider == null) {
1665 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001666 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001667 fd = stableProvider.openTypedAssetFile(
1668 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001669 if (fd == null) {
1670 // The provider will be released by the finally{} clause
1671 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001674
1675 if (stableProvider == null) {
1676 stableProvider = acquireProvider(uri);
1677 }
1678 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001679 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001680 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1681 fd.getParcelFileDescriptor(), stableProvider);
1682
1683 // Success! Don't release the provider when exiting, let
1684 // ParcelFileDescriptorInner do that when it is closed.
1685 stableProvider = null;
1686
1687 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1688 fd.getDeclaredLength());
1689
1690 } catch (RemoteException e) {
1691 // Whatever, whatever, we'll go away.
1692 throw new FileNotFoundException(
1693 "Failed opening content provider: " + uri);
1694 } catch (FileNotFoundException e) {
1695 throw e;
1696 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001697 if (cancellationSignal != null) {
1698 cancellationSignal.setRemote(null);
1699 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001700 if (stableProvider != null) {
1701 releaseProvider(stableProvider);
1702 }
1703 if (unstableProvider != null) {
1704 releaseUnstableProvider(unstableProvider);
1705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
1707 }
1708
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001709 /**
1710 * A resource identified by the {@link Resources} that contains it, and a resource id.
1711 *
1712 * @hide
1713 */
1714 public class OpenResourceIdResult {
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001715 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001716 public Resources r;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001717 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001718 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001720
1721 /**
1722 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1723 *
1724 * @hide
1725 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01001726 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001727 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 String authority = uri.getAuthority();
1729 Resources r;
1730 if (TextUtils.isEmpty(authority)) {
1731 throw new FileNotFoundException("No authority: " + uri);
1732 } else {
1733 try {
1734 r = mContext.getPackageManager().getResourcesForApplication(authority);
1735 } catch (NameNotFoundException ex) {
1736 throw new FileNotFoundException("No package found for authority: " + uri);
1737 }
1738 }
1739 List<String> path = uri.getPathSegments();
1740 if (path == null) {
1741 throw new FileNotFoundException("No path: " + uri);
1742 }
1743 int len = path.size();
1744 int id;
1745 if (len == 1) {
1746 try {
1747 id = Integer.parseInt(path.get(0));
1748 } catch (NumberFormatException e) {
1749 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
1750 }
1751 } else if (len == 2) {
1752 id = r.getIdentifier(path.get(1), path.get(0), authority);
1753 } else {
1754 throw new FileNotFoundException("More than two path segments: " + uri);
1755 }
1756 if (id == 0) {
1757 throw new FileNotFoundException("No resource found for: " + uri);
1758 }
1759 OpenResourceIdResult res = new OpenResourceIdResult();
1760 res.r = r;
1761 res.id = id;
1762 return res;
1763 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 /**
1766 * Inserts a row into a table at the given URL.
1767 *
1768 * If the content provider supports transactions the insertion will be atomic.
1769 *
1770 * @param url The URL of the table to insert into.
1771 * @param values The initial values for the newly inserted row. The key is the column name for
1772 * the field. Passing an empty ContentValues will create an empty row.
1773 * @return the URL of the newly created row.
1774 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001775 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001776 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1777 @Nullable ContentValues values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001778 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001779
1780 try {
1781 if (mWrapped != null) return mWrapped.insert(url, values);
1782 } catch (RemoteException e) {
1783 return null;
1784 }
1785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 IContentProvider provider = acquireProvider(url);
1787 if (provider == null) {
1788 throw new IllegalArgumentException("Unknown URL " + url);
1789 }
1790 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001791 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001792 Uri createdRow = provider.insert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001793 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001794 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
1795 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001797 // Arbitrary and not worth documenting, as Activity
1798 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 return null;
1800 } finally {
1801 releaseProvider(provider);
1802 }
1803 }
1804
Fred Quintana89437372009-05-15 15:10:40 -07001805 /**
Fred Quintana89437372009-05-15 15:10:40 -07001806 * Applies each of the {@link ContentProviderOperation} objects and returns an array
1807 * of their results. Passes through OperationApplicationException, which may be thrown
1808 * by the call to {@link ContentProviderOperation#apply}.
1809 * If all the applications succeed then a {@link ContentProviderResult} array with the
1810 * same number of elements as the operations will be returned. It is implementation-specific
1811 * how many, if any, operations will have been successfully applied if a call to
1812 * apply results in a {@link OperationApplicationException}.
1813 * @param authority the authority of the ContentProvider to which this batch should be applied
1814 * @param operations the operations to apply
1815 * @return the results of the applications
1816 * @throws OperationApplicationException thrown if an application fails.
1817 * See {@link ContentProviderOperation#apply} for more information.
1818 * @throws RemoteException thrown if a RemoteException is encountered while attempting
1819 * to communicate with a remote provider.
1820 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001821 @Override
Jeff Sharkey673db442015-06-11 19:30:57 -07001822 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
1823 @NonNull ArrayList<ContentProviderOperation> operations)
1824 throws RemoteException, OperationApplicationException {
1825 Preconditions.checkNotNull(authority, "authority");
1826 Preconditions.checkNotNull(operations, "operations");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001827
1828 try {
1829 if (mWrapped != null) return mWrapped.applyBatch(authority, operations);
1830 } catch (RemoteException e) {
1831 return null;
1832 }
1833
Fred Quintana89437372009-05-15 15:10:40 -07001834 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001835 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07001836 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001837 }
1838 try {
Fred Quintana89437372009-05-15 15:10:40 -07001839 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001840 } finally {
1841 provider.release();
1842 }
1843 }
1844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 /**
1846 * Inserts multiple rows into a table at the given URL.
1847 *
1848 * This function make no guarantees about the atomicity of the insertions.
1849 *
1850 * @param url The URL of the table to insert into.
1851 * @param values The initial values for the newly inserted rows. The key is the column name for
1852 * the field. Passing null will create an empty row.
1853 * @return the number of newly created rows.
1854 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001855 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001856 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
1857 @NonNull ContentValues[] values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001858 Preconditions.checkNotNull(url, "url");
1859 Preconditions.checkNotNull(values, "values");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001860
1861 try {
1862 if (mWrapped != null) return mWrapped.bulkInsert(url, values);
1863 } catch (RemoteException e) {
1864 return 0;
1865 }
1866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 IContentProvider provider = acquireProvider(url);
1868 if (provider == null) {
1869 throw new IllegalArgumentException("Unknown URL " + url);
1870 }
1871 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001872 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001873 int rowsCreated = provider.bulkInsert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001874 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001875 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
1876 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001878 // Arbitrary and not worth documenting, as Activity
1879 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 return 0;
1881 } finally {
1882 releaseProvider(provider);
1883 }
1884 }
1885
1886 /**
1887 * Deletes row(s) specified by a content URI.
1888 *
1889 * If the content provider supports transactions, the deletion will be atomic.
1890 *
1891 * @param url The URL of the row to delete.
1892 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
1893 (excluding the WHERE itself).
1894 * @return The number of rows deleted.
1895 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001896 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001897 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07001898 @Nullable String[] selectionArgs) {
1899 Preconditions.checkNotNull(url, "url");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001900
1901 try {
1902 if (mWrapped != null) return mWrapped.delete(url, where, selectionArgs);
1903 } catch (RemoteException e) {
1904 return 0;
1905 }
1906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 IContentProvider provider = acquireProvider(url);
1908 if (provider == null) {
1909 throw new IllegalArgumentException("Unknown URL " + url);
1910 }
1911 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001912 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001913 int rowsDeleted = provider.delete(mPackageName, url, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001914 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001915 maybeLogUpdateToEventLog(durationMillis, url, "delete", where);
1916 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001918 // Arbitrary and not worth documenting, as Activity
1919 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 return -1;
1921 } finally {
1922 releaseProvider(provider);
1923 }
1924 }
1925
1926 /**
1927 * Update row(s) in a content URI.
1928 *
1929 * If the content provider supports transactions the update will be atomic.
1930 *
1931 * @param uri The URI to modify.
1932 * @param values The new field values. The key is the column name for the field.
1933 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08001934 * @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 -08001935 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001936 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 * @throws NullPointerException if uri or values are null
1938 */
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001939 @Override
Tor Norbye788fc2b2015-07-05 16:10:42 -07001940 public final int update(@RequiresPermission.Write @NonNull Uri uri,
1941 @Nullable ContentValues values, @Nullable String where,
1942 @Nullable String[] selectionArgs) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001943 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001944
1945 try {
1946 if (mWrapped != null) return mWrapped.update(uri, values, where, selectionArgs);
1947 } catch (RemoteException e) {
1948 return 0;
1949 }
1950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 IContentProvider provider = acquireProvider(uri);
1952 if (provider == null) {
1953 throw new IllegalArgumentException("Unknown URI " + uri);
1954 }
1955 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001956 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001957 int rowsUpdated = provider.update(mPackageName, uri, values, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001958 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001959 maybeLogUpdateToEventLog(durationMillis, uri, "update", where);
1960 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001962 // Arbitrary and not worth documenting, as Activity
1963 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 return -1;
1965 } finally {
1966 releaseProvider(provider);
1967 }
1968 }
1969
1970 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09001971 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001972 * read or write interfaces which are cheaper than using a Cursor and/or
1973 * do not fit into the traditional table model.
1974 *
1975 * @param method provider-defined method name to call. Opaque to
1976 * framework, but must be non-null.
1977 * @param arg provider-defined String argument. May be null.
1978 * @param extras provider-defined Bundle argument. May be null.
1979 * @return a result Bundle, possibly null. Will be null if the ContentProvider
1980 * does not implement call.
1981 * @throws NullPointerException if uri or method is null
1982 * @throws IllegalArgumentException if uri is not known
1983 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001984 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
1985 @Nullable String arg, @Nullable Bundle extras) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07001986 return call(uri.getAuthority(), method, arg, extras);
1987 }
1988
1989 @Override
1990 public final @Nullable Bundle call(@NonNull String authority, @NonNull String method,
1991 @Nullable String arg, @Nullable Bundle extras) {
1992 Preconditions.checkNotNull(authority, "authority");
Jeff Sharkey673db442015-06-11 19:30:57 -07001993 Preconditions.checkNotNull(method, "method");
Jeff Sharkeya13887f2019-02-15 15:53:35 -07001994
1995 try {
1996 if (mWrapped != null) return mWrapped.call(authority, method, arg, extras);
1997 } catch (RemoteException e) {
1998 return null;
1999 }
2000
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002001 IContentProvider provider = acquireProvider(authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002002 if (provider == null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002003 throw new IllegalArgumentException("Unknown authority " + authority);
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002004 }
2005 try {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002006 final Bundle res = provider.call(mPackageName, authority, method, arg, extras);
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002007 Bundle.setDefusable(res, true);
2008 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08002009 } catch (RemoteException e) {
2010 // Arbitrary and not worth documenting, as Activity
2011 // Manager will kill this process shortly anyway.
2012 return null;
2013 } finally {
2014 releaseProvider(provider);
2015 }
2016 }
2017
2018 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002019 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 *
2021 * @param uri The URI to a content provider
2022 * @return The ContentProvider for the given URI, or null if no content provider is found.
2023 * @hide
2024 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002025 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002026 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04002027 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 return null;
2029 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002030 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002032 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
2034 return null;
2035 }
2036
2037 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002038 * Returns the content provider for the given content URI if the process
2039 * already has a reference on it.
2040 *
2041 * @param uri The URI to a content provider
2042 * @return The ContentProvider for the given URI, or null if no content provider is found.
2043 * @hide
2044 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002045 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002046 public final IContentProvider acquireExistingProvider(Uri uri) {
2047 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2048 return null;
2049 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002050 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002051 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02002052 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002053 }
2054 return null;
2055 }
2056
2057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 * @hide
2059 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002060 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08002062 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 return null;
2064 }
2065 return acquireProvider(mContext, name);
2066 }
2067
2068 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002069 * Returns the content provider for the given content URI.
2070 *
2071 * @param uri The URI to a content provider
2072 * @return The ContentProvider for the given URI, or null if no content provider is found.
2073 * @hide
2074 */
2075 public final IContentProvider acquireUnstableProvider(Uri uri) {
2076 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
2077 return null;
2078 }
2079 String auth = uri.getAuthority();
2080 if (auth != null) {
2081 return acquireUnstableProvider(mContext, uri.getAuthority());
2082 }
2083 return null;
2084 }
2085
2086 /**
2087 * @hide
2088 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002089 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002090 public final IContentProvider acquireUnstableProvider(String name) {
2091 if (name == null) {
2092 return null;
2093 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002094 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002095 }
2096
2097 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07002098 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2099 * that services the content at uri, starting the provider if necessary. Returns
2100 * null if there is no provider associated wih the uri. The caller must indicate that they are
2101 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002102 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002103 * keeping it active.
2104 * @param uri specifies which provider should be acquired
2105 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2106 * that services the content at uri or null if there isn't one.
2107 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002108 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
2109 Preconditions.checkNotNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07002110 IContentProvider provider = acquireProvider(uri);
2111 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002112 return new ContentProviderClient(this, provider, uri.getAuthority(), true);
Fred Quintana718d8a22009-04-29 17:53:20 -07002113 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002114 return null;
2115 }
2116
2117 /**
2118 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2119 * with the authority of name, starting the provider if necessary. Returns
2120 * null if there is no provider associated wih the uri. The caller must indicate that they are
2121 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07002122 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07002123 * keeping it active.
2124 * @param name specifies which provider should be acquired
2125 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
2126 * with the authority of name or null if there isn't one.
2127 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002128 public final @Nullable ContentProviderClient acquireContentProviderClient(
2129 @NonNull String name) {
2130 Preconditions.checkNotNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07002131 IContentProvider provider = acquireProvider(name);
2132 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002133 return new ContentProviderClient(this, provider, name, true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002134 }
2135
2136 return null;
2137 }
2138
2139 /**
2140 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
2141 * not trust the stability of the target content provider. This turns off
2142 * the mechanism in the platform clean up processes that are dependent on
2143 * a content provider if that content provider's process goes away. Normally
2144 * you can safely assume that once you have acquired a provider, you can freely
2145 * use it as needed and it won't disappear, even if your process is in the
2146 * background. If using this method, you need to take care to deal with any
2147 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002148 * so that it can be re-opened later. In particular, catching a
2149 * {@link android.os.DeadObjectException} from the calls there will let you
2150 * know that the content provider has gone away; at that point the current
2151 * ContentProviderClient object is invalid, and you should release it. You
2152 * can acquire a new one if you would like to try to restart the provider
2153 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002154 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002155 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2156 @NonNull Uri uri) {
2157 Preconditions.checkNotNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002158 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002159 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002160 return new ContentProviderClient(this, provider, uri.getAuthority(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002161 }
2162
2163 return null;
2164 }
2165
2166 /**
2167 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
2168 * not trust the stability of the target content provider. This turns off
2169 * the mechanism in the platform clean up processes that are dependent on
2170 * a content provider if that content provider's process goes away. Normally
2171 * you can safely assume that once you have acquired a provider, you can freely
2172 * use it as needed and it won't disappear, even if your process is in the
2173 * background. If using this method, you need to take care to deal with any
2174 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002175 * so that it can be re-opened later. In particular, catching a
2176 * {@link android.os.DeadObjectException} from the calls there will let you
2177 * know that the content provider has gone away; at that point the current
2178 * ContentProviderClient object is invalid, and you should release it. You
2179 * can acquire a new one if you would like to try to restart the provider
2180 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002181 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002182 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
2183 @NonNull String name) {
2184 Preconditions.checkNotNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002185 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002186 if (provider != null) {
Jeff Sharkey633a13e2018-12-07 12:00:45 -07002187 return new ContentProviderClient(this, provider, name, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07002188 }
2189
2190 return null;
2191 }
2192
2193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 * Register an observer class that gets callbacks when data identified by a
2195 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002196 * <p>
2197 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2198 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002200 * @param uri The URI to watch for changes. This can be a specific row URI,
2201 * or a base URI for a whole class of content.
2202 * @param notifyForDescendants When false, the observer will be notified
2203 * whenever a change occurs to the exact URI specified by
2204 * <code>uri</code> or to one of the URI's ancestors in the path
2205 * hierarchy. When true, the observer will also be notified
2206 * whenever a change occurs to the URI's descendants in the path
2207 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 * @param observer The object that receives callbacks when changes occur.
2209 * @see #unregisterContentObserver
2210 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002211 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07002212 @NonNull ContentObserver observer) {
2213 Preconditions.checkNotNull(uri, "uri");
2214 Preconditions.checkNotNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01002215 registerContentObserver(
2216 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08002217 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01002218 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002219 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002220 }
2221
2222 /** @hide - designated user version */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002223 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07002224 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002225 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002227 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002228 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002230 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 }
2232 }
2233
2234 /**
2235 * Unregisters a change observer.
2236 *
2237 * @param observer The previously registered observer that is no longer needed.
2238 * @see #registerContentObserver
2239 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002240 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
2241 Preconditions.checkNotNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 try {
2243 IContentObserver contentObserver = observer.releaseContentObserver();
2244 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002245 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 contentObserver);
2247 }
2248 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002249 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 }
2251 }
2252
2253 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002254 * Notify registered observers that a row was updated and attempt to sync
2255 * changes to the network.
2256 * <p>
2257 * To observe events sent through this call, use
2258 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2259 * <p>
2260 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2261 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 *
Jeff Brown86de0592012-01-23 13:01:18 -08002263 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002264 * @param observer The observer that originated the change, may be
2265 * <code>null</null>. The observer that originated the change
2266 * will only receive the notification if it has requested to
2267 * receive self-change notifications by implementing
2268 * {@link ContentObserver#deliverSelfNotifications()} to return
2269 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002271 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 notifyChange(uri, observer, true /* sync to network */);
2273 }
2274
2275 /**
2276 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002277 * <p>
2278 * To observe events sent through this call, use
2279 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2280 * <p>
2281 * If syncToNetwork is true, this will attempt to schedule a local sync
2282 * using the sync adapter that's registered for the authority of the
2283 * provided uri. No account will be passed to the sync adapter, so all
2284 * matching accounts will be synchronized.
2285 * <p>
2286 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2287 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 *
Jeff Brown86de0592012-01-23 13:01:18 -08002289 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002290 * @param observer The observer that originated the change, may be
2291 * <code>null</null>. The observer that originated the change
2292 * will only receive the notification if it has requested to
2293 * receive self-change notifications by implementing
2294 * {@link ContentObserver#deliverSelfNotifications()} to return
2295 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002296 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002297 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002299 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2300 boolean syncToNetwork) {
2301 Preconditions.checkNotNull(uri, "uri");
Benjamin Franzadea1912015-06-19 16:03:38 +01002302 notifyChange(
2303 ContentProvider.getUriWithoutUserId(uri),
2304 observer,
2305 syncToNetwork,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002306 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002307 }
2308
2309 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002310 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002311 * <p>
2312 * To observe events sent through this call, use
2313 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2314 * <p>
2315 * If syncToNetwork is true, this will attempt to schedule a local sync
2316 * using the sync adapter that's registered for the authority of the
2317 * provided uri. No account will be passed to the sync adapter, so all
2318 * matching accounts will be synchronized.
2319 * <p>
2320 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2321 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002322 *
2323 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002324 * @param observer The observer that originated the change, may be
2325 * <code>null</null>. The observer that originated the change
2326 * will only receive the notification if it has requested to
2327 * receive self-change notifications by implementing
2328 * {@link ContentObserver#deliverSelfNotifications()} to return
2329 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002330 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2331 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2332 */
2333 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2334 @NotifyFlags int flags) {
2335 Preconditions.checkNotNull(uri, "uri");
2336 notifyChange(
2337 ContentProvider.getUriWithoutUserId(uri),
2338 observer,
2339 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002340 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002341 }
2342
2343 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002344 * Notify registered observers within the designated user(s) that a row was updated.
2345 *
2346 * @hide
2347 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002348 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002349 @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002351 getContentService().notifyChange(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 uri, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002353 observer != null && observer.deliverSelfNotifications(),
2354 syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0,
Makoto Onukie183a402018-08-29 11:46:41 -07002355 userHandle, mTargetSdkVersion, mContext.getPackageName());
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002356 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002357 throw e.rethrowFromSystemServer();
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002358 }
2359 }
2360
2361 /**
2362 * Notify registered observers within the designated user(s) that a row was updated.
2363 *
2364 * @hide
2365 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002366 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002367 @UserIdInt int userHandle) {
2368 try {
2369 getContentService().notifyChange(
2370 uri, observer == null ? null : observer.getContentObserver(),
2371 observer != null && observer.deliverSelfNotifications(), flags,
Makoto Onukie183a402018-08-29 11:46:41 -07002372 userHandle, mTargetSdkVersion, mContext.getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002374 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
2376 }
2377
2378 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002379 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002380 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002381 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002382 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2383 * the grant has already been persisted, taking it again will touch
2384 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002385 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002386 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002387 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002388 public void takePersistableUriPermission(@NonNull Uri uri,
2389 @Intent.AccessUriMode int modeFlags) {
2390 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002391 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002392 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002393 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2394 resolveUserId(uri));
2395 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002396 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002397 }
2398 }
2399
2400 /**
2401 * @hide
2402 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01002403 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002404 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2405 @Intent.AccessUriMode int modeFlags) {
2406 Preconditions.checkNotNull(toPackage, "toPackage");
2407 Preconditions.checkNotNull(uri, "uri");
2408 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002409 UriGrantsManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002410 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2411 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002412 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002413 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002414 }
2415 }
2416
2417 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002418 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002419 * previously made persistent with
2420 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2421 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002422 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002423 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002424 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002425 public void releasePersistableUriPermission(@NonNull Uri uri,
2426 @Intent.AccessUriMode int modeFlags) {
2427 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002428 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002429 UriGrantsManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002430 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2431 resolveUserId(uri));
2432 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002433 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002434 }
2435 }
2436
2437 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002438 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002439 * calling app. That is, the returned permissions have been granted
2440 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002441 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002442 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002443 *
2444 * @see #takePersistableUriPermission(Uri, int)
2445 * @see #releasePersistableUriPermission(Uri, int)
2446 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002447 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002448 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002449 return UriGrantsManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002450 .getPersistedUriPermissions(mPackageName, true).getList();
2451 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002452 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002453 }
2454 }
2455
2456 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002457 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002458 * calling app. That is, the returned permissions have been granted
2459 * <em>from</em> the calling app. Only grants taken with
2460 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002461 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002462 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002463 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002464 try {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002465 return UriGrantsManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002466 .getPersistedUriPermissions(mPackageName, false).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002467 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002468 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002469 }
2470 }
2471
2472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 * Start an asynchronous sync operation. If you want to monitor the progress
2474 * of the sync you may register a SyncObserver. Only values of the following
2475 * types may be used in the extras bundle:
2476 * <ul>
2477 * <li>Integer</li>
2478 * <li>Long</li>
2479 * <li>Boolean</li>
2480 * <li>Float</li>
2481 * <li>Double</li>
2482 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002483 * <li>Account</li>
2484 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 * </ul>
2486 *
2487 * @param uri the uri of the provider to sync or null to sync all providers.
2488 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002489 * @deprecated instead use
2490 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002492 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002494 Account account = null;
2495 if (extras != null) {
2496 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2497 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002498 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002499 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002500 }
2501 extras.remove(SYNC_EXTRAS_ACCOUNT);
2502 }
2503 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2504 }
2505
2506 /**
2507 * Start an asynchronous sync operation. If you want to monitor the progress
2508 * of the sync you may register a SyncObserver. Only values of the following
2509 * types may be used in the extras bundle:
2510 * <ul>
2511 * <li>Integer</li>
2512 * <li>Long</li>
2513 * <li>Boolean</li>
2514 * <li>Float</li>
2515 * <li>Double</li>
2516 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002517 * <li>Account</li>
2518 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002519 * </ul>
2520 *
2521 * @param account which account should be synced
2522 * @param authority which authority should be synced
2523 * @param extras any extras to pass to the SyncAdapter.
2524 */
2525 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002526 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002527 }
2528
2529 /**
2530 * @see #requestSync(Account, String, Bundle)
2531 * @hide
2532 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002533 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002534 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002535 if (extras == null) {
2536 throw new IllegalArgumentException("Must specify extras.");
2537 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002538 SyncRequest request =
2539 new SyncRequest.Builder()
2540 .setSyncAdapter(account, authority)
2541 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002542 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002543 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002544 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002545 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2546 // case, but it's only for debugging.
2547 getContentService().syncAsUser(request, userId, ActivityThread.currentPackageName());
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002548 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002549 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002550 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002551 }
2552
2553 /**
2554 * Register a sync with the SyncManager. These requests are built using the
2555 * {@link SyncRequest.Builder}.
2556 */
2557 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 try {
Makoto Onukie183a402018-08-29 11:46:41 -07002559 // Note ActivityThread.currentPackageName() may not be accurate in a shared process
2560 // case, but it's only for debugging.
2561 getContentService().sync(request, ActivityThread.currentPackageName());
Matthew Williamsfa774182013-06-18 15:44:11 -07002562 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002563 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 }
2565 }
2566
2567 /**
2568 * Check that only values of the following types are in the Bundle:
2569 * <ul>
2570 * <li>Integer</li>
2571 * <li>Long</li>
2572 * <li>Boolean</li>
2573 * <li>Float</li>
2574 * <li>Double</li>
2575 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002576 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 * <li>null</li>
2578 * </ul>
2579 * @param extras the Bundle to check
2580 */
2581 public static void validateSyncExtrasBundle(Bundle extras) {
2582 try {
2583 for (String key : extras.keySet()) {
2584 Object value = extras.get(key);
2585 if (value == null) continue;
2586 if (value instanceof Long) continue;
2587 if (value instanceof Integer) continue;
2588 if (value instanceof Boolean) continue;
2589 if (value instanceof Float) continue;
2590 if (value instanceof Double) continue;
2591 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002592 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 throw new IllegalArgumentException("unexpected value type: "
2594 + value.getClass().getName());
2595 }
2596 } catch (IllegalArgumentException e) {
2597 throw e;
2598 } catch (RuntimeException exc) {
2599 throw new IllegalArgumentException("error unparceling Bundle", exc);
2600 }
2601 }
2602
Fred Quintanaac9385e2009-06-22 18:00:59 -07002603 /**
2604 * Cancel any active or pending syncs that match the Uri. If the uri is null then
2605 * all syncs will be canceled.
2606 *
2607 * @param uri the uri of the provider to sync or null to sync all providers.
2608 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
2609 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002610 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002612 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
2613 }
2614
2615 /**
2616 * Cancel any active or pending syncs that match account and authority. The account and
2617 * authority can each independently be set to null, which means that syncs with any account
2618 * or authority, respectively, will match.
2619 *
2620 * @param account filters the syncs that match by this account
2621 * @param authority filters the syncs that match by this authority
2622 */
2623 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002625 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002627 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 }
2629 }
2630
Fred Quintanaac9385e2009-06-22 18:00:59 -07002631 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002632 * @see #cancelSync(Account, String)
2633 * @hide
2634 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002635 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002636 try {
2637 getContentService().cancelSyncAsUser(account, authority, null, userId);
2638 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002639 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002640 }
2641 }
2642
2643 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002644 * Get information about the SyncAdapters that are known to the system.
2645 * @return an array of SyncAdapters that have registered with the system
2646 */
2647 public static SyncAdapterType[] getSyncAdapterTypes() {
2648 try {
2649 return getContentService().getSyncAdapterTypes();
2650 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002651 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002652 }
2653 }
2654
2655 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002656 * @see #getSyncAdapterTypes()
2657 * @hide
2658 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002659 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002660 try {
2661 return getContentService().getSyncAdapterTypesAsUser(userId);
2662 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002663 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002664 }
2665 }
2666
2667 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07002668 * @hide
2669 * Returns the package names of syncadapters that match a given user and authority.
2670 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07002671 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07002672 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002673 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002674 try {
2675 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
2676 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002677 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07002678 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07002679 }
2680
2681 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002682 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002683 * <p>This method requires the caller to hold the permission
2684 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002685 *
2686 * @param account the account whose setting we are querying
2687 * @param authority the provider whose setting we are querying
2688 * @return true if the provider should be synced when a network tickle is received
2689 */
2690 public static boolean getSyncAutomatically(Account account, String authority) {
2691 try {
2692 return getContentService().getSyncAutomatically(account, authority);
2693 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002694 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002695 }
2696 }
2697
2698 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002699 * @see #getSyncAutomatically(Account, String)
2700 * @hide
2701 */
2702 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002703 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002704 try {
2705 return getContentService().getSyncAutomaticallyAsUser(account, authority, userId);
2706 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002707 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002708 }
2709 }
2710
2711 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002712 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002713 * <p>This method requires the caller to hold the permission
2714 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002715 *
2716 * @param account the account whose setting we are querying
2717 * @param authority the provider whose behavior is being controlled
2718 * @param sync true if the provider should be synced when tickles are received for it
2719 */
2720 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002721 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002722 }
2723
2724 /**
2725 * @see #setSyncAutomatically(Account, String, boolean)
2726 * @hide
2727 */
2728 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002729 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002730 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002731 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002732 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002733 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002734 }
2735 }
2736
2737 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002738 * Specifies that a sync should be requested with the specified the account, authority,
2739 * and extras at the given frequency. If there is already another periodic sync scheduled
2740 * with the account, authority and extras then a new periodic sync won't be added, instead
2741 * the frequency of the previous one will be updated.
2742 * <p>
2743 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
2744 * Although these sync are scheduled at the specified frequency, it may take longer for it to
2745 * actually be started if other syncs are ahead of it in the sync operation queue. This means
2746 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08002747 * <p>
2748 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
2749 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
2750 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
2751 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
2752 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002753 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002754 * <p>This method requires the caller to hold the permission
2755 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002756 * <p>The bundle for a periodic sync can be queried by applications with the correct
2757 * permissions using
2758 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
2759 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002760 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002761 * @param account the account to specify in the sync
2762 * @param authority the provider to specify in the sync request
2763 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07002764 * @param pollFrequency how frequently the sync should be performed, in seconds.
2765 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
2766 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08002767 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
2768 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002769 */
2770 public static void addPeriodicSync(Account account, String authority, Bundle extras,
2771 long pollFrequency) {
2772 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08002773 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002774 throw new IllegalArgumentException("illegal extras were set");
2775 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002776 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07002777 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002778 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002779 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002780 }
2781 }
2782
2783 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002784 * {@hide}
2785 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
2786 * extras were set for a periodic sync.
2787 *
2788 * @param extras bundle to validate.
2789 */
2790 public static boolean invalidPeriodicExtras(Bundle extras) {
2791 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
2792 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
2793 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
2794 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
2795 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
2796 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
2797 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
2798 return true;
2799 }
2800 return false;
2801 }
2802
2803 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002804 * Remove a periodic sync. Has no affect if account, authority and extras don't match
2805 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002806 * <p>This method requires the caller to hold the permission
2807 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002808 *
2809 * @param account the account of the periodic sync to remove
2810 * @param authority the provider of the periodic sync to remove
2811 * @param extras the extras of the periodic sync to remove
2812 */
2813 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
2814 validateSyncExtrasBundle(extras);
2815 try {
2816 getContentService().removePeriodicSync(account, authority, extras);
2817 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002818 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002819 }
2820 }
2821
2822 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002823 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
2824 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07002825 * <p>
2826 * If a periodic sync is specified, the caller must hold the permission
2827 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
2828 *</p>
2829 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002830 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07002831 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07002832 *
2833 * @param request SyncRequest object containing information about sync to cancel.
2834 */
2835 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002836 if (request == null) {
2837 throw new IllegalArgumentException("request cannot be null");
2838 }
2839 try {
2840 getContentService().cancelRequest(request);
2841 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002842 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002843 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002844 }
2845
2846 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002847 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002848 * <p>This method requires the caller to hold the permission
2849 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002850 *
2851 * @param account the account whose periodic syncs we are querying
2852 * @param authority the provider whose periodic syncs we are querying
2853 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
2854 */
2855 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
2856 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002857 return getContentService().getPeriodicSyncs(account, authority, null);
2858 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002859 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002860 }
2861 }
2862
2863 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002864 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002865 * <p>This method requires the caller to hold the permission
2866 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07002867 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
2868 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002869 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002870 try {
2871 return getContentService().getIsSyncable(account, authority);
2872 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002873 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002874 }
2875 }
2876
2877 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002878 * @see #getIsSyncable(Account, String)
2879 * @hide
2880 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002881 public static int getIsSyncableAsUser(Account account, String authority,
2882 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002883 try {
2884 return getContentService().getIsSyncableAsUser(account, authority, userId);
2885 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002886 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002887 }
2888 }
2889
2890 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002891 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002892 * <p>This method requires the caller to hold the permission
2893 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07002894 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07002895 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002896 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002897 try {
2898 getContentService().setIsSyncable(account, authority, syncable);
2899 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002900 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002901 }
2902 }
2903
2904 /**
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +00002905 * @see #setIsSyncable(Account, String, int)
2906 * @hide
2907 */
2908 public static void setIsSyncableAsUser(Account account, String authority, int syncable,
2909 int userId) {
2910 try {
2911 getContentService().setIsSyncableAsUser(account, authority, syncable, userId);
2912 } catch (RemoteException e) {
2913 throw e.rethrowFromSystemServer();
2914 }
2915 }
2916
2917 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002918 * Gets the master auto-sync setting that applies to all the providers and accounts.
2919 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002920 * <p>This method requires the caller to hold the permission
2921 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002922 *
2923 * @return the master auto-sync setting that applies to all the providers and accounts
2924 */
2925 public static boolean getMasterSyncAutomatically() {
2926 try {
2927 return getContentService().getMasterSyncAutomatically();
2928 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002929 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002930 }
2931 }
2932
2933 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002934 * @see #getMasterSyncAutomatically()
2935 * @hide
2936 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002937 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002938 try {
2939 return getContentService().getMasterSyncAutomaticallyAsUser(userId);
2940 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002941 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002942 }
2943 }
2944
2945 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002946 * Sets the master auto-sync setting that applies to all the providers and accounts.
2947 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002948 * <p>This method requires the caller to hold the permission
2949 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002950 *
2951 * @param sync the master auto-sync setting that applies to all the providers and accounts
2952 */
2953 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002954 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002955 }
2956
2957 /**
2958 * @see #setMasterSyncAutomatically(boolean)
2959 * @hide
2960 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002961 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002962 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002963 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002964 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002965 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002966 }
2967 }
2968
2969 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002970 * Returns true if there is currently a sync operation for the given account or authority
2971 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002972 * <p>This method requires the caller to hold the permission
2973 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002974 * @param account the account whose setting we are querying
2975 * @param authority the provider whose behavior is being queried
2976 * @return true if a sync is active for the given account or authority.
2977 */
2978 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002979 if (account == null) {
2980 throw new IllegalArgumentException("account must not be null");
2981 }
2982 if (authority == null) {
2983 throw new IllegalArgumentException("authority must not be null");
2984 }
2985
Fred Quintanaac9385e2009-06-22 18:00:59 -07002986 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002987 return getContentService().isSyncActive(account, authority, null);
2988 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002989 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002990 }
2991 }
2992
Fred Quintanaac9385e2009-06-22 18:00:59 -07002993 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07002994 * If a sync is active returns the information about it, otherwise returns null.
2995 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002996 * This method requires the caller to hold the permission
2997 * {@link android.Manifest.permission#READ_SYNC_STATS}.
2998 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07002999 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07003000 * @deprecated
3001 * Since multiple concurrent syncs are now supported you should use
3002 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
3003 * This method returns the first item from the list of current syncs
3004 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003005 */
Fred Quintanac6a69552010-09-27 17:05:04 -07003006 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07003007 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003008 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07003009 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
3010 if (syncs.isEmpty()) {
3011 return null;
3012 }
3013 return syncs.get(0);
3014 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003015 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07003016 }
3017 }
3018
3019 /**
3020 * Returns a list with information about all the active syncs. This list will be empty
3021 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003022 * <p>
3023 * This method requires the caller to hold the permission
3024 * {@link android.Manifest.permission#READ_SYNC_STATS}.
3025 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07003026 * @return a List of SyncInfo objects for the currently active syncs.
3027 */
3028 public static List<SyncInfo> getCurrentSyncs() {
3029 try {
3030 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003031 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003032 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003033 }
3034 }
3035
3036 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003037 * @see #getCurrentSyncs()
3038 * @hide
3039 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003040 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003041 try {
3042 return getContentService().getCurrentSyncsAsUser(userId);
3043 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003044 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003045 }
3046 }
3047
3048 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07003049 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003050 * @param account the account whose setting we are querying
3051 * @param authority the provider whose behavior is being queried
3052 * @return the SyncStatusInfo for the authority, or null if none exists
3053 * @hide
3054 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003055 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07003056 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
3057 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003058 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07003059 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003060 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003061 }
3062 }
3063
3064 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003065 * @see #getSyncStatus(Account, String)
3066 * @hide
3067 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003068 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003069 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003070 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003071 try {
3072 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
3073 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003074 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01003075 }
3076 }
3077
3078 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07003079 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07003080 * <p>This method requires the caller to hold the permission
3081 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07003082 * @param account the account whose setting we are querying
3083 * @param authority the provider whose behavior is being queried
3084 * @return true if there is a pending sync with the matching account and authority
3085 */
3086 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01003087 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003088 }
3089
3090 /**
3091 * @see #requestSync(Account, String, Bundle)
3092 * @hide
3093 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003094 public static boolean isSyncPendingAsUser(Account account, String authority,
3095 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07003096 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01003097 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003098 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003099 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003100 }
3101 }
3102
Fred Quintana1b487ec2010-02-26 10:57:55 -08003103 /**
3104 * Request notifications when the different aspects of the SyncManager change. The
3105 * different items that can be requested are:
3106 * <ul>
3107 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
3108 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
3109 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
3110 * </ul>
3111 * The caller can set one or more of the status types in the mask for any
3112 * given listener registration.
3113 * @param mask the status change types that will cause the callback to be invoked
3114 * @param callback observer to be invoked when the status changes
3115 * @return a handle that can be used to remove the listener at a later time
3116 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003117 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003118 if (callback == null) {
3119 throw new IllegalArgumentException("you passed in a null callback");
3120 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003121 try {
3122 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08003123 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07003124 public void onStatusChanged(int which) throws RemoteException {
3125 callback.onStatusChanged(which);
3126 }
3127 };
3128 getContentService().addStatusChangeListener(mask, observer);
3129 return observer;
3130 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003131 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003132 }
3133 }
3134
Fred Quintana1b487ec2010-02-26 10:57:55 -08003135 /**
3136 * Remove a previously registered status change listener.
3137 * @param handle the handle that was returned by {@link #addStatusChangeListener}
3138 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07003139 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08003140 if (handle == null) {
3141 throw new IllegalArgumentException("you passed in a null handle");
3142 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003143 try {
3144 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
3145 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003146 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07003147 }
3148 }
3149
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003150 /**
3151 * Put the cache with the key.
3152 *
3153 * @param key the key to add
3154 * @param value the value to add
Ivan Chiang5922ce22019-02-13 11:42:42 +08003155 * {@hide}
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003156 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003157 @SystemApi
Jeff Sharkey87314082016-03-11 17:25:11 -07003158 public void putCache(Uri key, Bundle value) {
3159 try {
3160 getContentService().putCache(mContext.getPackageName(), key, value,
3161 mContext.getUserId());
3162 } catch (RemoteException e) {
3163 throw e.rethrowFromSystemServer();
3164 }
3165 }
3166
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003167 /**
3168 * Get the cache with the key.
3169 *
3170 * @param key the key to get the value
3171 * @return the matched value. If the key doesn't exist, will return null.
3172 * @see #putCache(Uri, Bundle)
Ivan Chiang5922ce22019-02-13 11:42:42 +08003173 * {@hide}
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003174 */
Ivan Chiang5922ce22019-02-13 11:42:42 +08003175 @SystemApi
Jeff Sharkey87314082016-03-11 17:25:11 -07003176 public Bundle getCache(Uri key) {
3177 try {
3178 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
3179 mContext.getUserId());
3180 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
3181 return bundle;
3182 } catch (RemoteException e) {
3183 throw e.rethrowFromSystemServer();
3184 }
3185 }
3186
Ben Lin8ea82002017-03-08 17:30:16 -08003187 /** {@hide} */
3188 public int getTargetSdkVersion() {
3189 return mTargetSdkVersion;
3190 }
3191
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003192 /**
3193 * Returns sampling percentage for a given duration.
3194 *
3195 * Always returns at least 1%.
3196 */
3197 private int samplePercentForDuration(long durationMillis) {
3198 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
3199 return 100;
3200 }
3201 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
3202 }
3203
Steve McKayea93fe72016-12-02 11:35:35 -08003204 private void maybeLogQueryToEventLog(
3205 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003206 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003207 int samplePercent = samplePercentForDuration(durationMillis);
3208 if (samplePercent < 100) {
3209 synchronized (mRandom) {
3210 if (mRandom.nextInt(100) >= samplePercent) {
3211 return;
3212 }
3213 }
3214 }
3215
Steve McKayea93fe72016-12-02 11:35:35 -08003216 // Ensure a non-null bundle.
3217 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
3218
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003219 StringBuilder projectionBuffer = new StringBuilder(100);
3220 if (projection != null) {
3221 for (int i = 0; i < projection.length; ++i) {
3222 // Note: not using a comma delimiter here, as the
3223 // multiple arguments to EventLog.writeEvent later
3224 // stringify with a comma delimiter, which would make
3225 // parsing uglier later.
3226 if (i != 0) projectionBuffer.append('/');
3227 projectionBuffer.append(projection[i]);
3228 }
3229 }
3230
3231 // ActivityThread.currentPackageName() only returns non-null if the
3232 // current thread is an application main thread. This parameter tells
3233 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003234 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003235
3236 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003237 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003238 uri.toString(),
3239 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08003240 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
3241 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003242 durationMillis,
3243 blockingPackage != null ? blockingPackage : "",
3244 samplePercent);
3245 }
3246
3247 private void maybeLogUpdateToEventLog(
3248 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07003249 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003250 int samplePercent = samplePercentForDuration(durationMillis);
3251 if (samplePercent < 100) {
3252 synchronized (mRandom) {
3253 if (mRandom.nextInt(100) >= samplePercent) {
3254 return;
3255 }
3256 }
3257 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003258 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003259 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07003260 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08003261 uri.toString(),
3262 operation,
3263 selection != null ? selection : "",
3264 durationMillis,
3265 blockingPackage != null ? blockingPackage : "",
3266 samplePercent);
3267 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003268
Jeff Brown825c5132011-10-12 16:11:30 -07003269 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003270 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003271 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003272
3273 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003275 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003277 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003278 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 }
3280
3281 @Override
3282 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003283 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003285
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003286 if (mProviderReleased.compareAndSet(false, true)) {
3287 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 }
3290
3291 @Override
3292 protected void finalize() throws Throwable {
3293 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003294 if (mCloseGuard != null) {
3295 mCloseGuard.warnIfOpen();
3296 }
3297
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003298 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 } finally {
3300 super.finalize();
3301 }
3302 }
3303 }
3304
3305 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003306 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003307 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308
3309 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3310 super(pfd);
3311 mContentProvider = icp;
3312 }
3313
3314 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003315 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003316 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003317 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 }
3319 }
3320 }
3321
Dianne Hackborn231cc602009-04-27 17:10:36 -07003322 /** @hide */
3323 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003324
Dianne Hackborn231cc602009-04-27 17:10:36 -07003325 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003326 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003327 public static IContentService getContentService() {
3328 if (sContentService != null) {
3329 return sContentService;
3330 }
3331 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003332 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003333 return sContentService;
3334 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003335
Dianne Hackborn35654b62013-01-14 17:38:02 -08003336 /** @hide */
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003337 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003338 public String getPackageName() {
3339 return mPackageName;
3340 }
3341
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003342 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003343 private static volatile IContentService sContentService;
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003344 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003346
Mathew Inwood5c0d3542018-08-14 13:54:31 +01003347 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003348 final String mPackageName;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003349 final int mTargetSdkVersion;
Jeff Sharkeya13887f2019-02-15 15:53:35 -07003350 final ContentInterface mWrapped;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003353
3354 /** @hide */
3355 public int resolveUserId(Uri uri) {
3356 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3357 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003358
3359 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003360 public int getUserId() {
3361 return mContext.getUserId();
3362 }
3363
Ivan Chiangfd3415c2018-12-07 18:22:50 +08003364 /**
3365 * Get the system drawable of the mime type.
3366 *
3367 * @param mimeType the requested mime type
3368 * @return the matched drawable
3369 * @hide
3370 */
3371 @SystemApi
Daniel Nishic29d2b02016-06-30 12:20:41 -07003372 public Drawable getTypeDrawable(String mimeType) {
3373 return MimeIconUtils.loadMimeIcon(mContext, mimeType);
3374 }
Steve McKayea93fe72016-12-02 11:35:35 -08003375
3376 /**
3377 * @hide
3378 */
3379 public static @Nullable Bundle createSqlQueryBundle(
3380 @Nullable String selection,
3381 @Nullable String[] selectionArgs,
3382 @Nullable String sortOrder) {
3383
3384 if (selection == null && selectionArgs == null && sortOrder == null) {
3385 return null;
3386 }
3387
3388 Bundle queryArgs = new Bundle();
3389 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003390 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003391 }
3392 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003393 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003394 }
3395 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003396 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003397 }
3398 return queryArgs;
3399 }
Steve McKay29c3f682016-12-16 14:52:59 -08003400
3401 /**
3402 * Returns structured sort args formatted as an SQL sort clause.
3403 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003404 * NOTE: Collator clauses are suitable for use with non text fields. We might
3405 * choose to omit any collation clause since we don't know the underlying
3406 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3407 * appear to care much about the presence of collate clauses in queries
3408 * when ordering by numeric fields. For this reason we include collate
3409 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3410 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003411 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003412 * TODO: Would be nice to explicitly validate that colums referenced in
3413 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003414 *
3415 * @hide
3416 */
3417 public static String createSqlSortClause(Bundle queryArgs) {
3418 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3419 if (columns == null || columns.length == 0) {
3420 throw new IllegalArgumentException("Can't create sort clause without columns.");
3421 }
3422
3423 String query = TextUtils.join(", ", columns);
3424
Steve McKayd7ece9f2017-01-12 16:59:59 -08003425 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3426 // on their javadoc descriptions.
3427 int collation = queryArgs.getInt(
3428 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3429 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3430 query += " COLLATE NOCASE";
3431 }
3432
Steve McKay415f41b2017-02-01 13:38:25 -08003433 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3434 if (sortDir != Integer.MIN_VALUE) {
3435 switch (sortDir) {
3436 case QUERY_SORT_DIRECTION_ASCENDING:
3437 query += " ASC";
3438 break;
3439 case QUERY_SORT_DIRECTION_DESCENDING:
3440 query += " DESC";
3441 break;
3442 default:
3443 throw new IllegalArgumentException("Unsupported sort direction value."
3444 + " See ContentResolver documentation for details.");
3445 }
Steve McKay29c3f682016-12-16 14:52:59 -08003446 }
Steve McKay29c3f682016-12-16 14:52:59 -08003447 return query;
3448 }
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003449
3450 /**
3451 * Convenience method that efficiently loads a visual thumbnail for the
3452 * given {@link Uri}. Internally calls
3453 * {@link ContentProvider#openTypedAssetFile} on the remote provider, but
3454 * also defensively resizes any returned content to match the requested
3455 * target size.
3456 *
3457 * @param uri The item that should be visualized as a thumbnail.
3458 * @param size The target area on the screen where this thumbnail will be
3459 * shown. This is passed to the provider as {@link #EXTRA_SIZE}
3460 * to help it avoid downloading or generating heavy resources.
3461 * @param signal A signal to cancel the operation in progress.
3462 * @return Valid {@link Bitmap} which is a visual thumbnail.
3463 * @throws IOException If any trouble was encountered while generating or
3464 * loading the thumbnail, or if
3465 * {@link CancellationSignal#cancel()} was invoked.
3466 */
3467 public @NonNull Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size,
3468 @Nullable CancellationSignal signal) throws IOException {
3469 Objects.requireNonNull(uri);
3470 Objects.requireNonNull(size);
3471
3472 try (ContentProviderClient client = acquireContentProviderClient(uri)) {
Leon Scroggins III19ff0c52018-11-19 10:00:51 -05003473 return loadThumbnail(client, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003474 }
3475 }
3476
3477 /** {@hide} */
Jeff Sharkeye770d222018-12-07 15:15:59 -07003478 public static Bitmap loadThumbnail(@NonNull ContentInterface content, @NonNull Uri uri,
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003479 @NonNull Size size, @Nullable CancellationSignal signal, int allocator)
3480 throws IOException {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003481 Objects.requireNonNull(content);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003482 Objects.requireNonNull(uri);
3483 Objects.requireNonNull(size);
3484
3485 // Convert to Point, since that's what the API is defined as
3486 final Bundle opts = new Bundle();
3487 opts.putParcelable(EXTRA_SIZE, Point.convert(size));
3488
3489 return ImageDecoder.decodeBitmap(ImageDecoder.createSource(() -> {
Jeff Sharkeye770d222018-12-07 15:15:59 -07003490 return content.openTypedAssetFile(uri, "image/*", opts, signal);
Jeff Sharkey4e5efa32018-10-04 19:21:53 -06003491 }), (ImageDecoder decoder, ImageInfo info, Source source) -> {
3492 decoder.setAllocator(allocator);
3493
3494 // One last-ditch check to see if we've been canceled.
3495 if (signal != null) signal.throwIfCanceled();
3496
3497 // We requested a rough thumbnail size, but the remote size may have
3498 // returned something giant, so defensively scale down as needed.
3499 final int widthSample = info.getSize().getWidth() / size.getWidth();
3500 final int heightSample = info.getSize().getHeight() / size.getHeight();
3501 final int sample = Math.min(widthSample, heightSample);
3502 if (sample > 1) {
3503 decoder.setTargetSampleSize(sample);
3504 }
3505 });
3506 }
Makoto Onukiee93ad22018-10-18 16:24:13 -07003507
3508 /** {@hide} */
3509 public static void onDbCorruption(String tag, String message, Throwable stacktrace) {
3510 try {
3511 getContentService().onDbCorruption(tag, message, Log.getStackTraceString(stacktrace));
3512 } catch (RemoteException e) {
3513 e.rethrowFromSystemServer();
3514 }
3515 }
Jeff Sharkeybc2ae002018-07-31 10:45:37 -06003516
3517 /** {@hide} */
3518 public static Uri translateDeprecatedDataPath(String path) {
3519 final String ssp = "//" + path.substring(DEPRECATE_DATA_PREFIX.length());
3520 return Uri.parse(new Uri.Builder().scheme(SCHEME_CONTENT)
3521 .encodedOpaquePart(ssp).build().toString());
3522 }
3523
3524 /** {@hide} */
3525 public static String translateDeprecatedDataPath(Uri uri) {
3526 return DEPRECATE_DATA_PREFIX + uri.getEncodedSchemeSpecificPart().substring(2);
3527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528}