blob: b85ec2713beecf21c1d5a77cd8f22af10de1ddcb [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;
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -070024import android.annotation.TestApi;
Mathew Inwood1c77a112018-08-14 14:06:26 +010025import android.annotation.UnsupportedAppUsage;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070026import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080027import android.app.ActivityManager;
Jeff Sharkey66a017b2013-01-17 18:18:22 -080028import android.app.ActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070029import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.pm.PackageManager.NameNotFoundException;
31import android.content.res.AssetFileDescriptor;
32import android.content.res.Resources;
33import android.database.ContentObserver;
Jeff Brown825c5132011-10-12 16:11:30 -070034import android.database.CrossProcessCursorWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.database.IContentObserver;
Jeff Sharkey5b836f22014-08-27 14:46:32 -070037import android.graphics.Point;
Daniel Nishic29d2b02016-06-30 12:20:41 -070038import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.net.Uri;
40import android.os.Bundle;
Jeff Browna7771df2012-05-07 20:06:46 -070041import android.os.CancellationSignal;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070042import android.os.DeadObjectException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070043import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070044import android.os.ICancellationSignal;
45import android.os.OperationCanceledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.ParcelFileDescriptor;
47import android.os.RemoteException;
Dianne Hackborn231cc602009-04-27 17:10:36 -070048import android.os.ServiceManager;
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -080049import android.os.SystemClock;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070050import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.text.TextUtils;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080052import android.util.EventLog;
Dianne Hackborn231cc602009-04-27 17:10:36 -070053import android.util.Log;
Jeff Sharkey08da7a12013-08-11 20:53:18 -070054
Daniel Nishic29d2b02016-06-30 12:20:41 -070055import com.android.internal.util.MimeIconUtils;
Jeff Sharkey673db442015-06-11 19:30:57 -070056import com.android.internal.util.Preconditions;
57
Jeff Sharkey60cfad82016-01-05 17:30:57 -070058import dalvik.system.CloseGuard;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.io.File;
61import java.io.FileInputStream;
62import java.io.FileNotFoundException;
63import java.io.IOException;
64import java.io.InputStream;
65import java.io.OutputStream;
Dianne Hackborn141f11c2016-04-05 15:46:12 -070066import java.lang.annotation.Retention;
67import java.lang.annotation.RetentionPolicy;
Gilles Debunne03f02922010-06-09 14:11:45 -070068import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.util.List;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -080070import java.util.Random;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070071import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073/**
74 * This class provides applications access to the content model.
Joe Fernandez558459f2011-10-13 16:47:36 -070075 *
76 * <div class="special reference">
77 * <h3>Developer Guides</h3>
78 * <p>For more information about using a ContentResolver with content providers, read the
79 * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
80 * developer guide.</p>
Ng Zhi An3ca51302019-01-24 21:39:24 -080081 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 */
83public abstract class ContentResolver {
Fred Quintanaac9385e2009-06-22 18:00:59 -070084 /**
85 * @deprecated instead use
86 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
87 */
Fred Quintana4a6679b2009-08-17 13:05:39 -070088 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -070089 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -070090
91 /**
92 * If this extra is set to true, the sync request will be scheduled
93 * at the front of the sync request queue and without any delay
94 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -070096
Fred Quintanaac9385e2009-06-22 18:00:59 -070097 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +000098 * If this extra is set to true, the sync request will be scheduled
99 * only when the device is plugged in. This is equivalent to calling
100 * setRequiresCharging(true) on {@link SyncRequest}.
101 */
102 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
103
104 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700105 * @deprecated instead use
106 * {@link #SYNC_EXTRAS_MANUAL}
107 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700108 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800110
111 /**
112 * If this extra is set to true then the sync settings (like getSyncAutomatically())
113 * are ignored by the sync scheduler.
114 */
115 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
116
117 /**
118 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
119 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
120 * retries will still honor the backoff.
121 */
122 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
123
124 /**
125 * If this extra is set to true then the request will not be retried if it fails.
126 */
127 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
128
129 /**
130 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
131 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
132 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700133 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800134
Georgi Nikolovb3395572013-06-18 18:27:31 -0700135 /**
136 * Indicates that this sync is intended to only upload local changes to the server.
137 * For example, this will be set to true if the sync is initiated by a call to
138 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
139 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700141
142 /**
143 * Indicates that the sync adapter should proceed with the delete operations,
144 * even if it determines that there are too many.
145 * See {@link SyncResult#tooManyDeletions}
146 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700148
149 /**
150 * Indicates that the sync adapter should not proceed with the delete operations,
151 * if it determines that there are too many.
152 * See {@link SyncResult#tooManyDeletions}
153 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
155
Matthew Williamsfa774182013-06-18 15:44:11 -0700156 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
157 /** {@hide} User-specified flag for expected upload size. */
158 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
159
160 /** {@hide} User-specified flag for expected download size. */
161 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
162
163 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
164 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
165
166 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700167 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700168
Fred Quintana4a6679b2009-08-17 13:05:39 -0700169 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700170 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800171 *
172 * Only the system and the shell user can set it.
173 *
174 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
175 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700176 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800177
178 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700179 * Set by the SyncManager to request that the SyncAdapter initialize itself for
180 * the given account/authority pair. One required initialization step is to
181 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
182 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
183 * do a full sync, though it is allowed to do so.
184 */
185 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
186
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800187 /** @hide */
188 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
189 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final String SCHEME_CONTENT = "content";
192 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
193 public static final String SCHEME_FILE = "file";
194
195 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700196 * An extra {@link Point} describing the optimal size for a requested image
197 * resource, in pixels. If a provider has multiple sizes of the image, it
198 * should return the image closest to this size.
199 *
200 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
201 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
202 * CancellationSignal)
203 */
204 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
205
206 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800207 * An extra boolean describing whether a particular provider supports refresh
208 * or not. If a provider supports refresh, it should include this key in its
209 * returned Cursor as part of its query call.
210 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800211 */
212 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
213
214 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800215 * Key for an SQL style selection string that may be present in the query Bundle argument
216 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
217 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800218 *
219 * <p>Clients should never include user supplied values directly in the selection string,
220 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
221 * should use standard placeholder notation to represent values in a selection string,
222 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
223 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800224 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
225 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700226 *
227 * @see #QUERY_ARG_SORT_COLUMNS
228 * @see #QUERY_ARG_SORT_DIRECTION
229 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800230 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700231 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800232
233 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800234 * Key for SQL selection string arguments list.
235 *
236 * <p>Clients should never include user supplied values directly in the selection string,
237 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
238 * should use standard placeholder notation to represent values in a selection string,
239 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
240 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800241 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
242 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700243 *
244 * @see #QUERY_ARG_SORT_COLUMNS
245 * @see #QUERY_ARG_SORT_DIRECTION
246 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800247 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700248 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
249 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800250
251 /**
252 * Key for an SQL style sort string that may be present in the query Bundle argument
253 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
254 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800255 *
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_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800264
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600265 /** {@hide} */
266 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
267 /** {@hide} */
268 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
269 /** {@hide} */
270 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
271
Steve McKay29c3f682016-12-16 14:52:59 -0800272 /**
Steve McKayd7ece9f2017-01-12 16:59:59 -0800273 * Specifies the list of columns against which to sort results. When first column values
274 * are identical, records are then sorted based on second column values, and so on.
Steve McKay29c3f682016-12-16 14:52:59 -0800275 *
276 * <p>Columns present in this list must also be included in the projection
277 * supplied to {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
278 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800279 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKay29c3f682016-12-16 14:52:59 -0800280 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700281 * <li>{@link ContentProvider} implementations: When preparing data in
282 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort columns
283 * is reflected in the returned Cursor, it is strongly recommended that
284 * {@link #QUERY_ARG_SORT_COLUMNS} then be included in the array of honored arguments
285 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800286 *
287 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
288 * arguments {@link Bundle}, the Content framework will attempt to synthesize
289 * an QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800290 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700291 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800292
293 /**
294 * Specifies desired sort order. When unspecified a provider may provide a default
295 * sort direction, or choose to return unsorted results.
296 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800297 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
298 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700299 * <li>{@link ContentProvider} implementations: When preparing data in
300 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
301 * is reflected in the returned Cursor, it is strongly recommended that
302 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
303 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800304 *
305 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
306 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700307 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800308 *
309 * @see #QUERY_SORT_DIRECTION_ASCENDING
310 * @see #QUERY_SORT_DIRECTION_DESCENDING
311 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700312 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800313
314 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700315 * Allows client to specify a hint to the provider declaring which collation
Steve McKay29c3f682016-12-16 14:52:59 -0800316 * to use when sorting text values.
317 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700318 * <p>Providers may support custom collators. When specifying a custom collator
319 * the value is determined by the Provider.
Steve McKay29c3f682016-12-16 14:52:59 -0800320 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700321 * <li>{@link ContentProvider} implementations: When preparing data in
322 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort collation
323 * is reflected in the returned Cursor, it is strongly recommended that
324 * {@link #QUERY_ARG_SORT_COLLATION} then be included in the array of honored arguments
325 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKay29c3f682016-12-16 14:52:59 -0800326 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700327 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
Steve McKayd7ece9f2017-01-12 16:59:59 -0800328 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700329 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800330 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700331 * @see java.text.Collator#PRIMARY
332 * @see java.text.Collator#SECONDARY
333 * @see java.text.Collator#TERTIARY
334 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800335 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700336 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800337
Steve McKay415f41b2017-02-01 13:38:25 -0800338 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700339 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800340 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700341 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800342 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
343 * when any QUERY_ARG_SORT* value was honored during the preparation of the
344 * results {@link Cursor}.
345 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700346 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
347 *
348 * @see #QUERY_ARG_SORT_COLUMNS
349 * @see #QUERY_ARG_SORT_DIRECTION
350 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800351 */
352 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
353
Steve McKay29c3f682016-12-16 14:52:59 -0800354 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700355 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800356 QUERY_SORT_DIRECTION_ASCENDING,
357 QUERY_SORT_DIRECTION_DESCENDING
358 })
359 @Retention(RetentionPolicy.SOURCE)
360 public @interface SortDirection {}
361 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
362 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
363
364 /**
365 * @see {@link java.text.Collector} for details on respective collation strength.
366 * @hide
367 */
368 @IntDef(flag = false, value = {
369 java.text.Collator.PRIMARY,
370 java.text.Collator.SECONDARY,
371 java.text.Collator.TERTIARY,
372 java.text.Collator.IDENTICAL
373 })
374 @Retention(RetentionPolicy.SOURCE)
375 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800376
377 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700378 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800379 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700380 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800381
382 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700383 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800384 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700385 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800386
387 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700388 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
389 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800390 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800391 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700392 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800393 *
394 * <p>Providers having returned -1 in a previous query are recommended to
395 * send content change notification once (if) full recordset size becomes
396 * known.
397 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700398 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800399
400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * This is the Android platform's base MIME type for a content: URI
402 * containing a Cursor of a single item. Applications should use this
403 * as the base type along with their own sub-type of their content: URIs
404 * that represent a particular item. For example, hypothetical IMAP email
405 * client may have a URI
406 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
407 * message in the inbox, whose MIME type would be reported as
408 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
411 */
412 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * This is the Android platform's base MIME type for a content: URI
416 * containing a Cursor of zero or more items. Applications should use this
417 * as the base type along with their own sub-type of their content: URIs
418 * that represent a directory of items. For example, hypothetical IMAP email
419 * client may have a URI
420 * <code>content://com.company.provider.imap/inbox</code> for all of the
421 * messages in its inbox, whose MIME type would be reported as
422 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * <p>Note how the base MIME type varies between this and
425 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
426 * one single item or multiple items in the data set, while the sub-type
427 * remains the same because in either case the data structure contained
428 * in the cursor is the same.
429 */
430 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700431
Matt Caseybd7bcf02014-02-05 15:51:39 -0800432 /**
433 * This is the Android platform's generic MIME type to match any MIME
434 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
435 * {@code SUB_TYPE} is the sub-type of the application-dependent
436 * content, e.g., "audio", "video", "playlist".
437 */
438 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
439
Fred Quintanaac9385e2009-06-22 18:00:59 -0700440 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100441 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700442 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
443 /** @hide */
444 public static final int SYNC_ERROR_AUTHENTICATION = 2;
445 /** @hide */
446 public static final int SYNC_ERROR_IO = 3;
447 /** @hide */
448 public static final int SYNC_ERROR_PARSE = 4;
449 /** @hide */
450 public static final int SYNC_ERROR_CONFLICT = 5;
451 /** @hide */
452 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
453 /** @hide */
454 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
455 /** @hide */
456 public static final int SYNC_ERROR_INTERNAL = 8;
457
Alon Albert57286f92012-10-09 14:21:38 -0700458 private static final String[] SYNC_ERROR_NAMES = new String[] {
459 "already-in-progress",
460 "authentication-error",
461 "io-error",
462 "parse-error",
463 "conflict",
464 "too-many-deletions",
465 "too-many-retries",
466 "internal-error",
467 };
468
469 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800470 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700471 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
472 return String.valueOf(error);
473 }
474 return SYNC_ERROR_NAMES[error - 1];
475 }
476
Alon Albert5c113fa2013-02-07 08:07:32 -0800477 /** @hide */
478 public static int syncErrorStringToInt(String error) {
479 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
480 if (SYNC_ERROR_NAMES[i].equals(error)) {
481 return i + 1;
482 }
483 }
484 if (error != null) {
485 try {
486 return Integer.parseInt(error);
487 } catch (NumberFormatException e) {
488 Log.d(TAG, "error parsing sync error: " + error);
489 }
490 }
491 return 0;
492 }
493
Fred Quintanaac9385e2009-06-22 18:00:59 -0700494 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700495 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700496 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
497 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100498 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700499 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
500 /** @hide */
501 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
502
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700503 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700504 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
505 NOTIFY_SYNC_TO_NETWORK,
506 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS
507 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700508 @Retention(RetentionPolicy.SOURCE)
509 public @interface NotifyFlags {}
510
511 /**
512 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
513 * to the network.
514 */
515 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
516
517 /**
518 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
519 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
520 * using "notify for descendants." The purpose of this is to allow the provide to send
521 * a general notification of "something under X" changed that observers of that specific
522 * URI can receive, while also sending a specific URI under X. It would use this flag
523 * when sending the former, so that observers of "X and descendants" only see the latter.
524 */
525 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
526
Makoto Onuki75ad2492018-03-28 14:42:42 -0700527 /**
528 * No exception, throttled by app standby normally.
529 * @hide
530 */
531 public static final int SYNC_EXEMPTION_NONE = 0;
532
533 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700534 * Exemption given to a sync request made by a foreground app (including
535 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700536 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700537 * At the schedule time, we promote the sync adapter app for a higher bucket:
538 * - If the device is not dozing (so the sync will start right away)
539 * promote to ACTIVE for 1 hour.
540 * - If the device is dozing (so the sync *won't* start right away),
541 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
542 * device comes out of doze.
543 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
544 * so it can schedule and start more syncs without getting throttled, even when the first
545 * operation was canceled and now we're retrying.
546 *
547 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700548 * @hide
549 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700550 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700551
552 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700553 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700554 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
555 * @hide
556 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700557 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700558
559 /** @hide */
560 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
561 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700562 SYNC_EXEMPTION_PROMOTE_BUCKET,
563 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700564 })
565 @Retention(RetentionPolicy.SOURCE)
566 public @interface SyncExemption {}
567
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800568 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800569 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700570 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800571 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800572 private final Random mRandom = new Random(); // guarded by itself
573
Dianne Hackborn231cc602009-04-27 17:10:36 -0700574 public ContentResolver(Context context) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800575 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700576 mPackageName = mContext.getOpPackageName();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000577 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
580 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100581 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700583
584 /**
585 * Providing a default implementation of this, to avoid having to change a
586 * lot of other things, but implementations of ContentResolver should
587 * implement it.
588 *
589 * @hide
590 */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100591 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700592 protected IContentProvider acquireExistingProvider(Context c, String name) {
593 return acquireProvider(c, name);
594 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100597 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700599 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100600 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700601 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
602 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100603 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700604 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700605 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100606 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700607 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700609 /** @hide */
610 public void appNotRespondingViaProvider(IContentProvider icp) {
611 throw new UnsupportedOperationException("appNotRespondingViaProvider");
612 }
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 /**
615 * Return the MIME type of the given content URL.
616 *
617 * @param url A Uri identifying content (either a list or specific type),
618 * using the content:// scheme.
619 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
620 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700621 public final @Nullable String getType(@NonNull Uri url) {
622 Preconditions.checkNotNull(url, "url");
623
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700624 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700625 IContentProvider provider = acquireExistingProvider(url);
626 if (provider != null) {
627 try {
628 return provider.getType(url);
629 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600630 // Arbitrary and not worth documenting, as Activity
631 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700632 return null;
633 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100634 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700635 return null;
636 } finally {
637 releaseProvider(provider);
638 }
639 }
640
641 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 return null;
643 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800646 String type = ActivityManager.getService().getProviderMimeType(
Nicolas Prevot1dddc7f2014-07-07 17:44:58 +0100647 ContentProvider.getUriWithoutUserId(url), resolveUserId(url));
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700648 return type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600650 throw e.rethrowFromSystemServer();
Ola Olsson145e6c42010-12-20 16:45:35 +0100651 } catch (java.lang.Exception e) {
652 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
653 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655 }
656
657 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700658 * Query for the possible MIME types for the representations the given
659 * content URL can be returned when opened as as stream with
660 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
661 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700662 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700663 * data that they contain.
664 *
665 * @param url A Uri identifying content (either a list or specific type),
666 * using the content:// scheme.
667 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500668 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700669 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700670 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700671 * data streams that match the given mimeTypeFilter. If there are none,
672 * null is returned.
673 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700674 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
675 Preconditions.checkNotNull(url, "url");
676 Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter");
677
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700678 IContentProvider provider = acquireProvider(url);
679 if (provider == null) {
680 return null;
681 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700682
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700683 try {
684 return provider.getStreamTypes(url, mimeTypeFilter);
685 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800686 // Arbitrary and not worth documenting, as Activity
687 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700688 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700689 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800690 releaseProvider(provider);
691 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700692 }
693
694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800696 * <p>
697 * For best performance, the caller should follow these guidelines:
698 * <ul>
699 * <li>Provide an explicit projection, to prevent
700 * reading data from storage that aren't going to be used.</li>
701 * <li>Use question mark parameter markers such as 'phone=?' instead of
702 * explicit values in the {@code selection} parameter, so that queries
703 * that differ only by those values will be recognized as the same
704 * for caching purposes.</li>
705 * </ul>
706 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 *
708 * @param uri The URI, using the content:// scheme, for the content to
709 * retrieve.
710 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800711 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * @param selection A filter declaring which rows to return, formatted as an
713 * SQL WHERE clause (excluding the WHERE itself). Passing null will
714 * return all rows for the given URI.
715 * @param selectionArgs You may include ?s in selection, which will be
716 * replaced by the values from selectionArgs, in the order that they
717 * appear in the selection. The values will be bound as Strings.
718 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
719 * clause (excluding the ORDER BY itself). Passing null will use the
720 * default sort order, which may be unordered.
721 * @return A Cursor object, which is positioned before the first entry, or null
722 * @see Cursor
723 */
Tor Norbye788fc2b2015-07-05 16:10:42 -0700724 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
725 @Nullable String[] projection, @Nullable String selection,
726 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800727 return query(uri, projection, selection, selectionArgs, sortOrder, null);
728 }
729
730 /**
Jeff Brownc64ff372013-10-09 18:50:56 -0700731 * Query the given URI, returning a {@link Cursor} over the result set
732 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800733 * <p>
734 * For best performance, the caller should follow these guidelines:
735 * <ul>
736 * <li>Provide an explicit projection, to prevent
737 * reading data from storage that aren't going to be used.</li>
738 * <li>Use question mark parameter markers such as 'phone=?' instead of
739 * explicit values in the {@code selection} parameter, so that queries
740 * that differ only by those values will be recognized as the same
741 * for caching purposes.</li>
742 * </ul>
743 * </p>
744 *
745 * @param uri The URI, using the content:// scheme, for the content to
746 * retrieve.
747 * @param projection A list of which columns to return. Passing null will
748 * return all columns, which is inefficient.
749 * @param selection A filter declaring which rows to return, formatted as an
750 * SQL WHERE clause (excluding the WHERE itself). Passing null will
751 * return all rows for the given URI.
752 * @param selectionArgs You may include ?s in selection, which will be
753 * replaced by the values from selectionArgs, in the order that they
754 * appear in the selection. The values will be bound as Strings.
755 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
756 * clause (excluding the ORDER BY itself). Passing null will use the
757 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -0800758 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800759 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
760 * when the query is executed.
761 * @return A Cursor object, which is positioned before the first entry, or null
762 * @see Cursor
763 */
Steve McKayea93fe72016-12-02 11:35:35 -0800764 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -0700765 @Nullable String[] projection, @Nullable String selection,
766 @Nullable String[] selectionArgs, @Nullable String sortOrder,
767 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -0800768 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
769 return query(uri, projection, queryArgs, cancellationSignal);
770 }
771
772 /**
773 * Query the given URI, returning a {@link Cursor} over the result set
774 * with support for cancellation.
775 *
776 * <p>For best performance, the caller should follow these guidelines:
777 *
778 * <li>Provide an explicit projection, to prevent reading data from storage
779 * that aren't going to be used.
780 *
Steve McKay415f41b2017-02-01 13:38:25 -0800781 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
782 * the preparation of the result set by including the respective argument keys
783 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
784 * for details.
785 *
786 * @see #QUERY_ARG_SORT_COLUMNS, #QUERY_ARG_SORT_DIRECTION, #QUERY_ARG_SORT_COLLATION.
787 *
Steve McKayea93fe72016-12-02 11:35:35 -0800788 * @param uri The URI, using the content:// scheme, for the content to
789 * retrieve.
790 * @param projection A list of which columns to return. Passing null will
791 * return all columns, which is inefficient.
792 * @param queryArgs A Bundle containing any arguments to the query.
793 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
794 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
795 * when the query is executed.
796 * @return A Cursor object, which is positioned before the first entry, or null
797 * @see Cursor
798 */
799 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
800 @Nullable String[] projection, @Nullable Bundle queryArgs,
801 @Nullable CancellationSignal cancellationSignal) {
Jeff Sharkey673db442015-06-11 19:30:57 -0700802 Preconditions.checkNotNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700803 IContentProvider unstableProvider = acquireUnstableProvider(uri);
804 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 return null;
806 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700807 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800808 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800810 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -0800811
Jeff Brown4c1241d2012-02-02 17:05:00 -0800812 ICancellationSignal remoteCancellationSignal = null;
813 if (cancellationSignal != null) {
814 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700815 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -0800816 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -0800817 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700818 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800819 qCursor = unstableProvider.query(mPackageName, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -0800820 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700821 } catch (DeadObjectException e) {
822 // The remote process has died... but we only hold an unstable
823 // reference though, so we might recover!!! Let's try!!!!
824 // This is exciting!!1!!1!!!!1
825 unstableProviderDied(unstableProvider);
826 stableProvider = acquireProvider(uri);
827 if (stableProvider == null) {
828 return null;
829 }
Steve McKayea93fe72016-12-02 11:35:35 -0800830 qCursor = stableProvider.query(
831 mPackageName, uri, projection, queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700832 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800833 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 return null;
835 }
Jeff Brownc21b5a02013-01-07 17:15:12 -0800836
837 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -0700838 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800839 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -0800840 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -0800841
842 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700843 final IContentProvider provider = (stableProvider != null) ? stableProvider
844 : acquireProvider(uri);
845 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700846 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800847 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700848 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800850 // Arbitrary and not worth documenting, as Activity
851 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700853 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -0800854 if (qCursor != null) {
855 qCursor.close();
856 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700857 if (cancellationSignal != null) {
858 cancellationSignal.setRemote(null);
859 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700860 if (unstableProvider != null) {
861 releaseUnstableProvider(unstableProvider);
862 }
863 if (stableProvider != null) {
864 releaseProvider(stableProvider);
865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867 }
868
Fred Quintana89437372009-05-15 15:10:40 -0700869 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700870 * Transform the given <var>url</var> to a canonical representation of
871 * its referenced resource, which can be used across devices, persisted,
872 * backed up and restored, etc. The returned Uri is still a fully capable
873 * Uri for use with its content provider, allowing you to do all of the
874 * same content provider operations as with the original Uri --
875 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
876 * only difference in behavior between the original and new Uris is that
877 * the content provider may need to do some additional work at each call
878 * using it to resolve it to the correct resource, especially if the
879 * canonical Uri has been moved to a different environment.
880 *
881 * <p>If you are moving a canonical Uri between environments, you should
882 * perform another call to {@link #canonicalize} with that original Uri to
883 * re-canonicalize it for the current environment. Alternatively, you may
884 * want to use {@link #uncanonicalize} to transform it to a non-canonical
885 * Uri that works only in the current environment but potentially more
886 * efficiently than the canonical representation.</p>
887 *
888 * @param url The {@link Uri} that is to be transformed to a canonical
889 * representation. Like all resolver calls, the input can be either
890 * a non-canonical or canonical Uri.
891 *
892 * @return Returns the official canonical representation of <var>url</var>,
893 * or null if the content provider does not support a canonical representation
894 * of the given Uri. Many providers may not support canonicalization of some
895 * or all of their Uris.
896 *
897 * @see #uncanonicalize
898 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700899 public final @Nullable Uri canonicalize(@NonNull Uri url) {
900 Preconditions.checkNotNull(url, "url");
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700901 IContentProvider provider = acquireProvider(url);
902 if (provider == null) {
903 return null;
904 }
905
906 try {
907 return provider.canonicalize(mPackageName, url);
908 } catch (RemoteException e) {
909 // Arbitrary and not worth documenting, as Activity
910 // Manager will kill this process shortly anyway.
911 return null;
912 } finally {
913 releaseProvider(provider);
914 }
915 }
916
917 /**
918 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
919 * it to its local non-canonical form. This can be useful in some cases where
920 * you know that you will only be using the Uri in the current environment and
921 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -0700922 * provider or want to verify that the referenced data exists at all in the
923 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700924 *
925 * @param url The canonical {@link Uri} that is to be convered back to its
926 * non-canonical form.
927 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -0700928 * @return Returns the non-canonical representation of <var>url</var>. This will
929 * return null if data identified by the canonical Uri can not be found in
930 * the current environment; callers must always check for null and deal with
931 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700932 *
933 * @see #canonicalize
934 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700935 public final @Nullable Uri uncanonicalize(@NonNull Uri url) {
936 Preconditions.checkNotNull(url, "url");
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700937 IContentProvider provider = acquireProvider(url);
938 if (provider == null) {
939 return null;
940 }
941
942 try {
943 return provider.uncanonicalize(mPackageName, url);
944 } catch (RemoteException e) {
945 // Arbitrary and not worth documenting, as Activity
946 // Manager will kill this process shortly anyway.
947 return null;
948 } finally {
949 releaseProvider(provider);
950 }
951 }
952
953 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800954 * This allows clients to request an explicit refresh of content identified by {@code uri}.
955 * <p>
956 * Client code should only invoke this method when there is a strong indication (such as a user
957 * initiated pull to refresh gesture) that the content is stale.
958 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -0800959 *
Ben Lin2b64a882016-11-11 15:24:58 -0800960 * @param url The Uri identifying the data to refresh.
Ben Lin1cf454f2016-11-10 13:50:54 -0800961 * @param args Additional options from the client. The definitions of these are specific to the
962 * content provider being called.
963 * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if
964 * none. For example, if you called refresh on a particular uri, you should call
965 * {@link CancellationSignal#throwIfCanceled()} to check whether the client has
966 * canceled the refresh request.
967 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -0800968 */
969 public final boolean refresh(@NonNull Uri url, @Nullable Bundle args,
970 @Nullable CancellationSignal cancellationSignal) {
971 Preconditions.checkNotNull(url, "url");
972 IContentProvider provider = acquireProvider(url);
973 if (provider == null) {
974 return false;
975 }
976
977 try {
978 ICancellationSignal remoteCancellationSignal = null;
979 if (cancellationSignal != null) {
980 cancellationSignal.throwIfCanceled();
981 remoteCancellationSignal = provider.createCancellationSignal();
982 cancellationSignal.setRemote(remoteCancellationSignal);
983 }
984 return provider.refresh(mPackageName, url, args, remoteCancellationSignal);
985 } catch (RemoteException e) {
986 // Arbitrary and not worth documenting, as Activity
987 // Manager will kill this process shortly anyway.
988 return false;
989 } finally {
990 releaseProvider(provider);
991 }
992 }
993
994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * Open a stream on to the content associated with a content URI. If there
996 * is no data associated with the URI, FileNotFoundException is thrown.
997 *
998 * <h5>Accepts the following URI schemes:</h5>
999 * <ul>
1000 * <li>content ({@link #SCHEME_CONTENT})</li>
1001 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1002 * <li>file ({@link #SCHEME_FILE})</li>
1003 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1006 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 * @param uri The desired URI.
1009 * @return InputStream
1010 * @throws FileNotFoundException if the provided URI could not be opened.
1011 * @see #openAssetFileDescriptor(Uri, String)
1012 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001013 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 throws FileNotFoundException {
Jeff Sharkey673db442015-06-11 19:30:57 -07001015 Preconditions.checkNotNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 String scheme = uri.getScheme();
1017 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1018 // Note: left here to avoid breaking compatibility. May be removed
1019 // with sufficient testing.
1020 OpenResourceIdResult r = getResourceId(uri);
1021 try {
1022 InputStream stream = r.r.openRawResource(r.id);
1023 return stream;
1024 } catch (Resources.NotFoundException ex) {
1025 throw new FileNotFoundException("Resource does not exist: " + uri);
1026 }
1027 } else if (SCHEME_FILE.equals(scheme)) {
1028 // Note: left here to avoid breaking compatibility. May be removed
1029 // with sufficient testing.
1030 return new FileInputStream(uri.getPath());
1031 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001032 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 try {
1034 return fd != null ? fd.createInputStream() : null;
1035 } catch (IOException e) {
1036 throw new FileNotFoundException("Unable to create stream");
1037 }
1038 }
1039 }
1040
1041 /**
1042 * Synonym for {@link #openOutputStream(Uri, String)
1043 * openOutputStream(uri, "w")}.
1044 * @throws FileNotFoundException if the provided URI could not be opened.
1045 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001046 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 throws FileNotFoundException {
1048 return openOutputStream(uri, "w");
1049 }
1050
1051 /**
1052 * Open a stream on to the content associated with a content URI. If there
1053 * is no data associated with the URI, FileNotFoundException is thrown.
1054 *
1055 * <h5>Accepts the following URI schemes:</h5>
1056 * <ul>
1057 * <li>content ({@link #SCHEME_CONTENT})</li>
1058 * <li>file ({@link #SCHEME_FILE})</li>
1059 * </ul>
1060 *
1061 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1062 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001063 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 * @param uri The desired URI.
1065 * @param mode May be "w", "wa", "rw", or "rwt".
1066 * @return OutputStream
1067 * @throws FileNotFoundException if the provided URI could not be opened.
1068 * @see #openAssetFileDescriptor(Uri, String)
1069 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001070 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001072 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 try {
1074 return fd != null ? fd.createOutputStream() : null;
1075 } catch (IOException e) {
1076 throw new FileNotFoundException("Unable to create stream");
1077 }
1078 }
1079
1080 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001081 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1083 * underlying {@link ContentProvider#openFile}
1084 * ContentProvider.openFile()} method, so will <em>not</em> work with
1085 * providers that return sub-sections of files. If at all possible,
1086 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1087 * will receive a FileNotFoundException exception if the provider returns a
1088 * sub-section of a file.
1089 *
1090 * <h5>Accepts the following URI schemes:</h5>
1091 * <ul>
1092 * <li>content ({@link #SCHEME_CONTENT})</li>
1093 * <li>file ({@link #SCHEME_FILE})</li>
1094 * </ul>
1095 *
1096 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1097 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001098 * <p>
1099 * If opening with the exclusive "r" or "w" modes, the returned
1100 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1101 * of data. Opening with the "rw" mode implies a file on disk that supports
1102 * seeking. If possible, always use an exclusive mode to give the underlying
1103 * {@link ContentProvider} the most flexibility.
1104 * <p>
1105 * If you are writing a file, and need to communicate an error to the
1106 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 * @param uri The desired URI to open.
1109 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1110 * ContentProvider.openFile}.
1111 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1112 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001113 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * file exists under the URI or the mode is invalid.
1115 * @see #openAssetFileDescriptor(Uri, String)
1116 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001117 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1118 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001119 return openFileDescriptor(uri, mode, null);
1120 }
1121
1122 /**
1123 * Open a raw file descriptor to access data under a URI. This
1124 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1125 * underlying {@link ContentProvider#openFile}
1126 * ContentProvider.openFile()} method, so will <em>not</em> work with
1127 * providers that return sub-sections of files. If at all possible,
1128 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1129 * will receive a FileNotFoundException exception if the provider returns a
1130 * sub-section of a file.
1131 *
1132 * <h5>Accepts the following URI schemes:</h5>
1133 * <ul>
1134 * <li>content ({@link #SCHEME_CONTENT})</li>
1135 * <li>file ({@link #SCHEME_FILE})</li>
1136 * </ul>
1137 *
1138 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1139 * on these schemes.
1140 * <p>
1141 * If opening with the exclusive "r" or "w" modes, the returned
1142 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1143 * of data. Opening with the "rw" mode implies a file on disk that supports
1144 * seeking. If possible, always use an exclusive mode to give the underlying
1145 * {@link ContentProvider} the most flexibility.
1146 * <p>
1147 * If you are writing a file, and need to communicate an error to the
1148 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1149 *
1150 * @param uri The desired URI to open.
1151 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1152 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001153 * @param cancellationSignal A signal to cancel the operation in progress,
1154 * or null if none. If the operation is canceled, then
1155 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001156 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1157 * own this descriptor and are responsible for closing it when done.
1158 * @throws FileNotFoundException Throws FileNotFoundException if no
1159 * file exists under the URI or the mode is invalid.
1160 * @see #openAssetFileDescriptor(Uri, String)
1161 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001162 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1163 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1164 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001165 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (afd == null) {
1167 return null;
1168 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 if (afd.getDeclaredLength() < 0) {
1171 // This is a full file!
1172 return afd.getParcelFileDescriptor();
1173 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Client can't handle a sub-section of a file, so close what
1176 // we got and bail with an exception.
1177 try {
1178 afd.close();
1179 } catch (IOException e) {
1180 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 throw new FileNotFoundException("Not a whole file");
1183 }
1184
1185 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001186 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001188 * 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 -08001189 *
1190 * <h5>Accepts the following URI schemes:</h5>
1191 * <ul>
1192 * <li>content ({@link #SCHEME_CONTENT})</li>
1193 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1194 * <li>file ({@link #SCHEME_FILE})</li>
1195 * </ul>
1196 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1197 * <p>
1198 * A Uri object can be used to reference a resource in an APK file. The
1199 * Uri should be one of the following formats:
1200 * <ul>
1201 * <li><code>android.resource://package_name/id_number</code><br/>
1202 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1203 * For example <code>com.example.myapp</code><br/>
1204 * <code>id_number</code> is the int form of the ID.<br/>
1205 * The easiest way to construct this form is
1206 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1207 * </li>
1208 * <li><code>android.resource://package_name/type/name</code><br/>
1209 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1210 * For example <code>com.example.myapp</code><br/>
1211 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1212 * or <code>drawable</code>.
1213 * <code>name</code> is the string form of the resource name. That is, whatever the file
1214 * name was in your res directory, without the type extension.
1215 * The easiest way to construct this form is
1216 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1217 * </li>
1218 * </ul>
1219 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001220 * <p>Note that if this function is called for read-only input (mode is "r")
1221 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001222 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001223 * from any built-in data conversion that a provider implements.
1224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 * @param uri The desired URI to open.
1226 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1227 * ContentProvider.openAssetFile}.
1228 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1229 * own this descriptor and are responsible for closing it when done.
1230 * @throws FileNotFoundException Throws FileNotFoundException of no
1231 * file exists under the URI or the mode is invalid.
1232 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001233 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1234 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001235 return openAssetFileDescriptor(uri, mode, null);
1236 }
1237
1238 /**
1239 * Open a raw file descriptor to access data under a URI. This
1240 * interacts with the underlying {@link ContentProvider#openAssetFile}
1241 * method of the provider associated with the given URI, to retrieve any file stored there.
1242 *
1243 * <h5>Accepts the following URI schemes:</h5>
1244 * <ul>
1245 * <li>content ({@link #SCHEME_CONTENT})</li>
1246 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1247 * <li>file ({@link #SCHEME_FILE})</li>
1248 * </ul>
1249 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1250 * <p>
1251 * A Uri object can be used to reference a resource in an APK file. The
1252 * Uri should be one of the following formats:
1253 * <ul>
1254 * <li><code>android.resource://package_name/id_number</code><br/>
1255 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1256 * For example <code>com.example.myapp</code><br/>
1257 * <code>id_number</code> is the int form of the ID.<br/>
1258 * The easiest way to construct this form is
1259 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1260 * </li>
1261 * <li><code>android.resource://package_name/type/name</code><br/>
1262 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1263 * For example <code>com.example.myapp</code><br/>
1264 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1265 * or <code>drawable</code>.
1266 * <code>name</code> is the string form of the resource name. That is, whatever the file
1267 * name was in your res directory, without the type extension.
1268 * The easiest way to construct this form is
1269 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1270 * </li>
1271 * </ul>
1272 *
1273 * <p>Note that if this function is called for read-only input (mode is "r")
1274 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001275 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001276 * from any built-in data conversion that a provider implements.
1277 *
1278 * @param uri The desired URI to open.
1279 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1280 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001281 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001282 * none. If the operation is canceled, then
1283 * {@link OperationCanceledException} will be thrown.
1284 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1285 * own this descriptor and are responsible for closing it when done.
1286 * @throws FileNotFoundException Throws FileNotFoundException of no
1287 * file exists under the URI or the mode is invalid.
1288 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001289 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1290 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1291 throws FileNotFoundException {
1292 Preconditions.checkNotNull(uri, "uri");
1293 Preconditions.checkNotNull(mode, "mode");
1294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 String scheme = uri.getScheme();
1296 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1297 if (!"r".equals(mode)) {
1298 throw new FileNotFoundException("Can't write resources: " + uri);
1299 }
1300 OpenResourceIdResult r = getResourceId(uri);
1301 try {
1302 return r.r.openRawResourceFd(r.id);
1303 } catch (Resources.NotFoundException ex) {
1304 throw new FileNotFoundException("Resource does not exist: " + uri);
1305 }
1306 } else if (SCHEME_FILE.equals(scheme)) {
1307 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001308 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 return new AssetFileDescriptor(pfd, 0, -1);
1310 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001311 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001312 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001313 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001314 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1315 if (unstableProvider == null) {
1316 throw new FileNotFoundException("No content provider: " + uri);
1317 }
1318 IContentProvider stableProvider = null;
1319 AssetFileDescriptor fd = null;
1320
1321 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001322 ICancellationSignal remoteCancellationSignal = null;
1323 if (cancellationSignal != null) {
1324 cancellationSignal.throwIfCanceled();
1325 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1326 cancellationSignal.setRemote(remoteCancellationSignal);
1327 }
1328
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001329 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001330 fd = unstableProvider.openAssetFile(
1331 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001332 if (fd == null) {
1333 // The provider will be released by the finally{} clause
1334 return null;
1335 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001336 } catch (DeadObjectException e) {
1337 // The remote process has died... but we only hold an unstable
1338 // reference though, so we might recover!!! Let's try!!!!
1339 // This is exciting!!1!!1!!!!1
1340 unstableProviderDied(unstableProvider);
1341 stableProvider = acquireProvider(uri);
1342 if (stableProvider == null) {
1343 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001344 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001345 fd = stableProvider.openAssetFile(
1346 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001347 if (fd == null) {
1348 // The provider will be released by the finally{} clause
1349 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001350 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001351 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001352
1353 if (stableProvider == null) {
1354 stableProvider = acquireProvider(uri);
1355 }
1356 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001357 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001358 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1359 fd.getParcelFileDescriptor(), stableProvider);
1360
1361 // Success! Don't release the provider when exiting, let
1362 // ParcelFileDescriptorInner do that when it is closed.
1363 stableProvider = null;
1364
1365 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1366 fd.getDeclaredLength());
1367
1368 } catch (RemoteException e) {
1369 // Whatever, whatever, we'll go away.
1370 throw new FileNotFoundException(
1371 "Failed opening content provider: " + uri);
1372 } catch (FileNotFoundException e) {
1373 throw e;
1374 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001375 if (cancellationSignal != null) {
1376 cancellationSignal.setRemote(null);
1377 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001378 if (stableProvider != null) {
1379 releaseProvider(stableProvider);
1380 }
1381 if (unstableProvider != null) {
1382 releaseUnstableProvider(unstableProvider);
1383 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001384 }
1385 }
1386 }
1387 }
1388
1389 /**
1390 * Open a raw file descriptor to access (potentially type transformed)
1391 * data from a "content:" URI. This interacts with the underlying
1392 * {@link ContentProvider#openTypedAssetFile} method of the provider
1393 * associated with the given URI, to retrieve retrieve any appropriate
1394 * data stream for the data stored there.
1395 *
1396 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1397 * with "content:" URIs, because content providers are the only facility
1398 * with an associated MIME type to ensure that the returned data stream
1399 * is of the desired type.
1400 *
1401 * <p>All text/* streams are encoded in UTF-8.
1402 *
1403 * @param uri The desired URI to open.
1404 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001405 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001406 * select a type, though there is no way for you to determine what type
1407 * it is returning.
1408 * @param opts Additional provider-dependent options.
1409 * @return Returns a new ParcelFileDescriptor from which you can read the
1410 * data stream from the provider. Note that this may be a pipe, meaning
1411 * you can't seek in it. The only seek you should do is if the
1412 * AssetFileDescriptor contains an offset, to move to that offset before
1413 * reading. You own this descriptor and are responsible for closing it when done.
1414 * @throws FileNotFoundException Throws FileNotFoundException of no
1415 * data of the desired type exists under the URI.
1416 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001417 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1418 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001419 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1420 }
1421
1422 /**
1423 * Open a raw file descriptor to access (potentially type transformed)
1424 * data from a "content:" URI. This interacts with the underlying
1425 * {@link ContentProvider#openTypedAssetFile} method of the provider
1426 * associated with the given URI, to retrieve retrieve any appropriate
1427 * data stream for the data stored there.
1428 *
1429 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1430 * with "content:" URIs, because content providers are the only facility
1431 * with an associated MIME type to ensure that the returned data stream
1432 * is of the desired type.
1433 *
1434 * <p>All text/* streams are encoded in UTF-8.
1435 *
1436 * @param uri The desired URI to open.
1437 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001438 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001439 * select a type, though there is no way for you to determine what type
1440 * it is returning.
1441 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001442 * @param cancellationSignal A signal to cancel the operation in progress,
1443 * or null if none. If the operation is canceled, then
1444 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001445 * @return Returns a new ParcelFileDescriptor from which you can read the
1446 * data stream from the provider. Note that this may be a pipe, meaning
1447 * you can't seek in it. The only seek you should do is if the
1448 * AssetFileDescriptor contains an offset, to move to that offset before
1449 * reading. You own this descriptor and are responsible for closing it when done.
1450 * @throws FileNotFoundException Throws FileNotFoundException of no
1451 * data of the desired type exists under the URI.
1452 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001453 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1454 @NonNull String mimeType, @Nullable Bundle opts,
1455 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
1456 Preconditions.checkNotNull(uri, "uri");
1457 Preconditions.checkNotNull(mimeType, "mimeType");
1458
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001459 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1460 if (unstableProvider == null) {
1461 throw new FileNotFoundException("No content provider: " + uri);
1462 }
1463 IContentProvider stableProvider = null;
1464 AssetFileDescriptor fd = null;
1465
1466 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001467 ICancellationSignal remoteCancellationSignal = null;
1468 if (cancellationSignal != null) {
1469 cancellationSignal.throwIfCanceled();
1470 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1471 cancellationSignal.setRemote(remoteCancellationSignal);
1472 }
1473
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001474 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001475 fd = unstableProvider.openTypedAssetFile(
1476 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001477 if (fd == null) {
1478 // The provider will be released by the finally{} clause
1479 return null;
1480 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001481 } catch (DeadObjectException e) {
1482 // The remote process has died... but we only hold an unstable
1483 // reference though, so we might recover!!! Let's try!!!!
1484 // This is exciting!!1!!1!!!!1
1485 unstableProviderDied(unstableProvider);
1486 stableProvider = acquireProvider(uri);
1487 if (stableProvider == null) {
1488 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001489 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001490 fd = stableProvider.openTypedAssetFile(
1491 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001492 if (fd == null) {
1493 // The provider will be released by the finally{} clause
1494 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001497
1498 if (stableProvider == null) {
1499 stableProvider = acquireProvider(uri);
1500 }
1501 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001502 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001503 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1504 fd.getParcelFileDescriptor(), stableProvider);
1505
1506 // Success! Don't release the provider when exiting, let
1507 // ParcelFileDescriptorInner do that when it is closed.
1508 stableProvider = null;
1509
1510 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1511 fd.getDeclaredLength());
1512
1513 } catch (RemoteException e) {
1514 // Whatever, whatever, we'll go away.
1515 throw new FileNotFoundException(
1516 "Failed opening content provider: " + uri);
1517 } catch (FileNotFoundException e) {
1518 throw e;
1519 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001520 if (cancellationSignal != null) {
1521 cancellationSignal.setRemote(null);
1522 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001523 if (stableProvider != null) {
1524 releaseProvider(stableProvider);
1525 }
1526 if (unstableProvider != null) {
1527 releaseUnstableProvider(unstableProvider);
1528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530 }
1531
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001532 /**
1533 * A resource identified by the {@link Resources} that contains it, and a resource id.
1534 *
1535 * @hide
1536 */
1537 public class OpenResourceIdResult {
Mathew Inwood1c77a112018-08-14 14:06:26 +01001538 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001539 public Resources r;
Mathew Inwood1c77a112018-08-14 14:06:26 +01001540 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001541 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001543
1544 /**
1545 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1546 *
1547 * @hide
1548 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001549 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001550 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 String authority = uri.getAuthority();
1552 Resources r;
1553 if (TextUtils.isEmpty(authority)) {
1554 throw new FileNotFoundException("No authority: " + uri);
1555 } else {
1556 try {
1557 r = mContext.getPackageManager().getResourcesForApplication(authority);
1558 } catch (NameNotFoundException ex) {
1559 throw new FileNotFoundException("No package found for authority: " + uri);
1560 }
1561 }
1562 List<String> path = uri.getPathSegments();
1563 if (path == null) {
1564 throw new FileNotFoundException("No path: " + uri);
1565 }
1566 int len = path.size();
1567 int id;
1568 if (len == 1) {
1569 try {
1570 id = Integer.parseInt(path.get(0));
1571 } catch (NumberFormatException e) {
1572 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
1573 }
1574 } else if (len == 2) {
1575 id = r.getIdentifier(path.get(1), path.get(0), authority);
1576 } else {
1577 throw new FileNotFoundException("More than two path segments: " + uri);
1578 }
1579 if (id == 0) {
1580 throw new FileNotFoundException("No resource found for: " + uri);
1581 }
1582 OpenResourceIdResult res = new OpenResourceIdResult();
1583 res.r = r;
1584 res.id = id;
1585 return res;
1586 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 /**
1589 * Inserts a row into a table at the given URL.
1590 *
1591 * If the content provider supports transactions the insertion will be atomic.
1592 *
1593 * @param url The URL of the table to insert into.
1594 * @param values The initial values for the newly inserted row. The key is the column name for
1595 * the field. Passing an empty ContentValues will create an empty row.
1596 * @return the URL of the newly created row.
1597 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001598 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1599 @Nullable ContentValues values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001600 Preconditions.checkNotNull(url, "url");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 IContentProvider provider = acquireProvider(url);
1602 if (provider == null) {
1603 throw new IllegalArgumentException("Unknown URL " + url);
1604 }
1605 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001606 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001607 Uri createdRow = provider.insert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001608 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001609 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
1610 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001612 // Arbitrary and not worth documenting, as Activity
1613 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 return null;
1615 } finally {
1616 releaseProvider(provider);
1617 }
1618 }
1619
Fred Quintana89437372009-05-15 15:10:40 -07001620 /**
Fred Quintana89437372009-05-15 15:10:40 -07001621 * Applies each of the {@link ContentProviderOperation} objects and returns an array
1622 * of their results. Passes through OperationApplicationException, which may be thrown
1623 * by the call to {@link ContentProviderOperation#apply}.
1624 * If all the applications succeed then a {@link ContentProviderResult} array with the
1625 * same number of elements as the operations will be returned. It is implementation-specific
1626 * how many, if any, operations will have been successfully applied if a call to
1627 * apply results in a {@link OperationApplicationException}.
1628 * @param authority the authority of the ContentProvider to which this batch should be applied
1629 * @param operations the operations to apply
1630 * @return the results of the applications
1631 * @throws OperationApplicationException thrown if an application fails.
1632 * See {@link ContentProviderOperation#apply} for more information.
1633 * @throws RemoteException thrown if a RemoteException is encountered while attempting
1634 * to communicate with a remote provider.
1635 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001636 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
1637 @NonNull ArrayList<ContentProviderOperation> operations)
1638 throws RemoteException, OperationApplicationException {
1639 Preconditions.checkNotNull(authority, "authority");
1640 Preconditions.checkNotNull(operations, "operations");
Fred Quintana89437372009-05-15 15:10:40 -07001641 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001642 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07001643 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001644 }
1645 try {
Fred Quintana89437372009-05-15 15:10:40 -07001646 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001647 } finally {
1648 provider.release();
1649 }
1650 }
1651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 /**
1653 * Inserts multiple rows into a table at the given URL.
1654 *
1655 * This function make no guarantees about the atomicity of the insertions.
1656 *
1657 * @param url The URL of the table to insert into.
1658 * @param values The initial values for the newly inserted rows. The key is the column name for
1659 * the field. Passing null will create an empty row.
1660 * @return the number of newly created rows.
1661 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001662 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
1663 @NonNull ContentValues[] values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001664 Preconditions.checkNotNull(url, "url");
1665 Preconditions.checkNotNull(values, "values");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 IContentProvider provider = acquireProvider(url);
1667 if (provider == null) {
1668 throw new IllegalArgumentException("Unknown URL " + url);
1669 }
1670 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001671 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001672 int rowsCreated = provider.bulkInsert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001673 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001674 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
1675 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001677 // Arbitrary and not worth documenting, as Activity
1678 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 return 0;
1680 } finally {
1681 releaseProvider(provider);
1682 }
1683 }
1684
1685 /**
1686 * Deletes row(s) specified by a content URI.
1687 *
1688 * If the content provider supports transactions, the deletion will be atomic.
1689 *
1690 * @param url The URL of the row to delete.
1691 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
1692 (excluding the WHERE itself).
1693 * @return The number of rows deleted.
1694 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001695 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07001696 @Nullable String[] selectionArgs) {
1697 Preconditions.checkNotNull(url, "url");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 IContentProvider provider = acquireProvider(url);
1699 if (provider == null) {
1700 throw new IllegalArgumentException("Unknown URL " + url);
1701 }
1702 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001703 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001704 int rowsDeleted = provider.delete(mPackageName, url, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001705 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001706 maybeLogUpdateToEventLog(durationMillis, url, "delete", where);
1707 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001709 // Arbitrary and not worth documenting, as Activity
1710 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 return -1;
1712 } finally {
1713 releaseProvider(provider);
1714 }
1715 }
1716
1717 /**
1718 * Update row(s) in a content URI.
1719 *
1720 * If the content provider supports transactions the update will be atomic.
1721 *
1722 * @param uri The URI to modify.
1723 * @param values The new field values. The key is the column name for the field.
1724 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08001725 * @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 -08001726 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001727 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 * @throws NullPointerException if uri or values are null
1729 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001730 public final int update(@RequiresPermission.Write @NonNull Uri uri,
1731 @Nullable ContentValues values, @Nullable String where,
1732 @Nullable String[] selectionArgs) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001733 Preconditions.checkNotNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 IContentProvider provider = acquireProvider(uri);
1735 if (provider == null) {
1736 throw new IllegalArgumentException("Unknown URI " + uri);
1737 }
1738 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001739 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001740 int rowsUpdated = provider.update(mPackageName, uri, values, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001741 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001742 maybeLogUpdateToEventLog(durationMillis, uri, "update", where);
1743 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001745 // Arbitrary and not worth documenting, as Activity
1746 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 return -1;
1748 } finally {
1749 releaseProvider(provider);
1750 }
1751 }
1752
1753 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09001754 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001755 * read or write interfaces which are cheaper than using a Cursor and/or
1756 * do not fit into the traditional table model.
1757 *
1758 * @param method provider-defined method name to call. Opaque to
1759 * framework, but must be non-null.
1760 * @param arg provider-defined String argument. May be null.
1761 * @param extras provider-defined Bundle argument. May be null.
1762 * @return a result Bundle, possibly null. Will be null if the ContentProvider
1763 * does not implement call.
1764 * @throws NullPointerException if uri or method is null
1765 * @throws IllegalArgumentException if uri is not known
1766 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001767 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
1768 @Nullable String arg, @Nullable Bundle extras) {
1769 Preconditions.checkNotNull(uri, "uri");
1770 Preconditions.checkNotNull(method, "method");
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001771 IContentProvider provider = acquireProvider(uri);
1772 if (provider == null) {
1773 throw new IllegalArgumentException("Unknown URI " + uri);
1774 }
1775 try {
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001776 final Bundle res = provider.call(mPackageName, method, arg, extras);
1777 Bundle.setDefusable(res, true);
1778 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001779 } catch (RemoteException e) {
1780 // Arbitrary and not worth documenting, as Activity
1781 // Manager will kill this process shortly anyway.
1782 return null;
1783 } finally {
1784 releaseProvider(provider);
1785 }
1786 }
1787
1788 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001789 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 *
1791 * @param uri The URI to a content provider
1792 * @return The ContentProvider for the given URI, or null if no content provider is found.
1793 * @hide
1794 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001795 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001796 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04001797 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 return null;
1799 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001800 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001802 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804 return null;
1805 }
1806
1807 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001808 * Returns the content provider for the given content URI if the process
1809 * already has a reference on it.
1810 *
1811 * @param uri The URI to a content provider
1812 * @return The ContentProvider for the given URI, or null if no content provider is found.
1813 * @hide
1814 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001815 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001816 public final IContentProvider acquireExistingProvider(Uri uri) {
1817 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
1818 return null;
1819 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001820 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001821 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001822 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001823 }
1824 return null;
1825 }
1826
1827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 * @hide
1829 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001830 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08001832 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 return null;
1834 }
1835 return acquireProvider(mContext, name);
1836 }
1837
1838 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001839 * Returns the content provider for the given content URI.
1840 *
1841 * @param uri The URI to a content provider
1842 * @return The ContentProvider for the given URI, or null if no content provider is found.
1843 * @hide
1844 */
1845 public final IContentProvider acquireUnstableProvider(Uri uri) {
1846 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
1847 return null;
1848 }
1849 String auth = uri.getAuthority();
1850 if (auth != null) {
1851 return acquireUnstableProvider(mContext, uri.getAuthority());
1852 }
1853 return null;
1854 }
1855
1856 /**
1857 * @hide
1858 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001859 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001860 public final IContentProvider acquireUnstableProvider(String name) {
1861 if (name == null) {
1862 return null;
1863 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001864 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001865 }
1866
1867 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07001868 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1869 * that services the content at uri, starting the provider if necessary. Returns
1870 * null if there is no provider associated wih the uri. The caller must indicate that they are
1871 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07001872 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07001873 * keeping it active.
1874 * @param uri specifies which provider should be acquired
1875 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1876 * that services the content at uri or null if there isn't one.
1877 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001878 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
1879 Preconditions.checkNotNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07001880 IContentProvider provider = acquireProvider(uri);
1881 if (provider != null) {
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001882 return new ContentProviderClient(this, provider, true);
Fred Quintana718d8a22009-04-29 17:53:20 -07001883 }
Fred Quintana718d8a22009-04-29 17:53:20 -07001884 return null;
1885 }
1886
1887 /**
1888 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1889 * with the authority of name, starting the provider if necessary. Returns
1890 * null if there is no provider associated wih the uri. The caller must indicate that they are
1891 * done with the provider by calling {@link ContentProviderClient#release} which will allow
kopriva219f7dc2018-10-09 13:42:28 -07001892 * the system to release the provider if it determines that there is no other reason for
Fred Quintana718d8a22009-04-29 17:53:20 -07001893 * keeping it active.
1894 * @param name specifies which provider should be acquired
1895 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1896 * with the authority of name or null if there isn't one.
1897 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001898 public final @Nullable ContentProviderClient acquireContentProviderClient(
1899 @NonNull String name) {
1900 Preconditions.checkNotNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07001901 IContentProvider provider = acquireProvider(name);
1902 if (provider != null) {
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001903 return new ContentProviderClient(this, provider, true);
1904 }
1905
1906 return null;
1907 }
1908
1909 /**
1910 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
1911 * not trust the stability of the target content provider. This turns off
1912 * the mechanism in the platform clean up processes that are dependent on
1913 * a content provider if that content provider's process goes away. Normally
1914 * you can safely assume that once you have acquired a provider, you can freely
1915 * use it as needed and it won't disappear, even if your process is in the
1916 * background. If using this method, you need to take care to deal with any
1917 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001918 * so that it can be re-opened later. In particular, catching a
1919 * {@link android.os.DeadObjectException} from the calls there will let you
1920 * know that the content provider has gone away; at that point the current
1921 * ContentProviderClient object is invalid, and you should release it. You
1922 * can acquire a new one if you would like to try to restart the provider
1923 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001924 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001925 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
1926 @NonNull Uri uri) {
1927 Preconditions.checkNotNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001928 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001929 if (provider != null) {
1930 return new ContentProviderClient(this, provider, false);
1931 }
1932
1933 return null;
1934 }
1935
1936 /**
1937 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
1938 * not trust the stability of the target content provider. This turns off
1939 * the mechanism in the platform clean up processes that are dependent on
1940 * a content provider if that content provider's process goes away. Normally
1941 * you can safely assume that once you have acquired a provider, you can freely
1942 * use it as needed and it won't disappear, even if your process is in the
1943 * background. If using this method, you need to take care to deal with any
1944 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001945 * so that it can be re-opened later. In particular, catching a
1946 * {@link android.os.DeadObjectException} from the calls there will let you
1947 * know that the content provider has gone away; at that point the current
1948 * ContentProviderClient object is invalid, and you should release it. You
1949 * can acquire a new one if you would like to try to restart the provider
1950 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001951 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001952 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
1953 @NonNull String name) {
1954 Preconditions.checkNotNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001955 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001956 if (provider != null) {
1957 return new ContentProviderClient(this, provider, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07001958 }
1959
1960 return null;
1961 }
1962
1963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 * Register an observer class that gets callbacks when data identified by a
1965 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07001966 * <p>
1967 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
1968 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07001970 * @param uri The URI to watch for changes. This can be a specific row URI,
1971 * or a base URI for a whole class of content.
1972 * @param notifyForDescendants When false, the observer will be notified
1973 * whenever a change occurs to the exact URI specified by
1974 * <code>uri</code> or to one of the URI's ancestors in the path
1975 * hierarchy. When true, the observer will also be notified
1976 * whenever a change occurs to the URI's descendants in the path
1977 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 * @param observer The object that receives callbacks when changes occur.
1979 * @see #unregisterContentObserver
1980 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001981 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07001982 @NonNull ContentObserver observer) {
1983 Preconditions.checkNotNull(uri, "uri");
1984 Preconditions.checkNotNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01001985 registerContentObserver(
1986 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001987 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01001988 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08001989 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07001990 }
1991
1992 /** @hide - designated user version */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001993 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07001994 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001995 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001997 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07001998 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002000 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002 }
2003
2004 /**
2005 * Unregisters a change observer.
2006 *
2007 * @param observer The previously registered observer that is no longer needed.
2008 * @see #registerContentObserver
2009 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002010 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
2011 Preconditions.checkNotNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 try {
2013 IContentObserver contentObserver = observer.releaseContentObserver();
2014 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002015 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 contentObserver);
2017 }
2018 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002019 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021 }
2022
2023 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002024 * Notify registered observers that a row was updated and attempt to sync
2025 * changes to the network.
2026 * <p>
2027 * To observe events sent through this call, use
2028 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2029 * <p>
2030 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2031 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 *
Jeff Brown86de0592012-01-23 13:01:18 -08002033 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002034 * @param observer The observer that originated the change, may be
2035 * <code>null</null>. The observer that originated the change
2036 * will only receive the notification if it has requested to
2037 * receive self-change notifications by implementing
2038 * {@link ContentObserver#deliverSelfNotifications()} to return
2039 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002041 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 notifyChange(uri, observer, true /* sync to network */);
2043 }
2044
2045 /**
2046 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002047 * <p>
2048 * To observe events sent through this call, use
2049 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2050 * <p>
2051 * If syncToNetwork is true, this will attempt to schedule a local sync
2052 * using the sync adapter that's registered for the authority of the
2053 * provided uri. No account will be passed to the sync adapter, so all
2054 * matching accounts will be synchronized.
2055 * <p>
2056 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2057 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 *
Jeff Brown86de0592012-01-23 13:01:18 -08002059 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002060 * @param observer The observer that originated the change, may be
2061 * <code>null</null>. The observer that originated the change
2062 * will only receive the notification if it has requested to
2063 * receive self-change notifications by implementing
2064 * {@link ContentObserver#deliverSelfNotifications()} to return
2065 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002066 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002067 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002069 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2070 boolean syncToNetwork) {
2071 Preconditions.checkNotNull(uri, "uri");
Benjamin Franzadea1912015-06-19 16:03:38 +01002072 notifyChange(
2073 ContentProvider.getUriWithoutUserId(uri),
2074 observer,
2075 syncToNetwork,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002076 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002077 }
2078
2079 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002080 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002081 * <p>
2082 * To observe events sent through this call, use
2083 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2084 * <p>
2085 * If syncToNetwork is true, this will attempt to schedule a local sync
2086 * using the sync adapter that's registered for the authority of the
2087 * provided uri. No account will be passed to the sync adapter, so all
2088 * matching accounts will be synchronized.
2089 * <p>
2090 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2091 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002092 *
2093 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002094 * @param observer The observer that originated the change, may be
2095 * <code>null</null>. The observer that originated the change
2096 * will only receive the notification if it has requested to
2097 * receive self-change notifications by implementing
2098 * {@link ContentObserver#deliverSelfNotifications()} to return
2099 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002100 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2101 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2102 */
2103 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2104 @NotifyFlags int flags) {
2105 Preconditions.checkNotNull(uri, "uri");
2106 notifyChange(
2107 ContentProvider.getUriWithoutUserId(uri),
2108 observer,
2109 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002110 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002111 }
2112
2113 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002114 * Notify registered observers within the designated user(s) that a row was updated.
2115 *
2116 * @hide
2117 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002118 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002119 @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002121 getContentService().notifyChange(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 uri, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002123 observer != null && observer.deliverSelfNotifications(),
2124 syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002125 userHandle, mTargetSdkVersion);
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002126 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002127 throw e.rethrowFromSystemServer();
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002128 }
2129 }
2130
2131 /**
2132 * Notify registered observers within the designated user(s) that a row was updated.
2133 *
2134 * @hide
2135 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002136 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002137 @UserIdInt int userHandle) {
2138 try {
2139 getContentService().notifyChange(
2140 uri, observer == null ? null : observer.getContentObserver(),
2141 observer != null && observer.deliverSelfNotifications(), flags,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002142 userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002144 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
2146 }
2147
2148 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002149 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002150 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002151 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002152 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2153 * the grant has already been persisted, taking it again will touch
2154 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002155 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002156 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002157 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002158 public void takePersistableUriPermission(@NonNull Uri uri,
2159 @Intent.AccessUriMode int modeFlags) {
2160 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002161 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002162 ActivityManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002163 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2164 resolveUserId(uri));
2165 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002166 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002167 }
2168 }
2169
2170 /**
2171 * @hide
2172 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01002173 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002174 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2175 @Intent.AccessUriMode int modeFlags) {
2176 Preconditions.checkNotNull(toPackage, "toPackage");
2177 Preconditions.checkNotNull(uri, "uri");
2178 try {
2179 ActivityManager.getService().takePersistableUriPermission(
2180 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2181 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002182 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002183 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002184 }
2185 }
2186
2187 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002188 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002189 * previously made persistent with
2190 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2191 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002192 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002193 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002194 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002195 public void releasePersistableUriPermission(@NonNull Uri uri,
2196 @Intent.AccessUriMode int modeFlags) {
2197 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002198 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002199 ActivityManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002200 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2201 resolveUserId(uri));
2202 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002203 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002204 }
2205 }
2206
2207 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002208 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002209 * calling app. That is, the returned permissions have been granted
2210 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002211 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002212 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002213 *
2214 * @see #takePersistableUriPermission(Uri, int)
2215 * @see #releasePersistableUriPermission(Uri, int)
2216 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002217 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002218 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002219 return ActivityManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002220 .getPersistedUriPermissions(mPackageName, true).getList();
2221 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002222 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002223 }
2224 }
2225
2226 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002227 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002228 * calling app. That is, the returned permissions have been granted
2229 * <em>from</em> the calling app. Only grants taken with
2230 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002231 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002232 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002233 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002234 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002235 return ActivityManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002236 .getPersistedUriPermissions(mPackageName, false).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002237 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002238 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002239 }
2240 }
2241
2242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 * Start an asynchronous sync operation. If you want to monitor the progress
2244 * of the sync you may register a SyncObserver. Only values of the following
2245 * types may be used in the extras bundle:
2246 * <ul>
2247 * <li>Integer</li>
2248 * <li>Long</li>
2249 * <li>Boolean</li>
2250 * <li>Float</li>
2251 * <li>Double</li>
2252 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002253 * <li>Account</li>
2254 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 * </ul>
2256 *
2257 * @param uri the uri of the provider to sync or null to sync all providers.
2258 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002259 * @deprecated instead use
2260 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002262 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002264 Account account = null;
2265 if (extras != null) {
2266 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2267 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002268 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002269 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002270 }
2271 extras.remove(SYNC_EXTRAS_ACCOUNT);
2272 }
2273 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2274 }
2275
2276 /**
2277 * Start an asynchronous sync operation. If you want to monitor the progress
2278 * of the sync you may register a SyncObserver. Only values of the following
2279 * types may be used in the extras bundle:
2280 * <ul>
2281 * <li>Integer</li>
2282 * <li>Long</li>
2283 * <li>Boolean</li>
2284 * <li>Float</li>
2285 * <li>Double</li>
2286 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002287 * <li>Account</li>
2288 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002289 * </ul>
2290 *
2291 * @param account which account should be synced
2292 * @param authority which authority should be synced
2293 * @param extras any extras to pass to the SyncAdapter.
2294 */
2295 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002296 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002297 }
2298
2299 /**
2300 * @see #requestSync(Account, String, Bundle)
2301 * @hide
2302 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002303 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002304 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002305 if (extras == null) {
2306 throw new IllegalArgumentException("Must specify extras.");
2307 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002308 SyncRequest request =
2309 new SyncRequest.Builder()
2310 .setSyncAdapter(account, authority)
2311 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002312 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002313 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002314 try {
2315 getContentService().syncAsUser(request, userId);
2316 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002317 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002318 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002319 }
2320
2321 /**
2322 * Register a sync with the SyncManager. These requests are built using the
2323 * {@link SyncRequest.Builder}.
2324 */
2325 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07002327 getContentService().sync(request);
2328 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002329 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 }
2331 }
2332
2333 /**
2334 * Check that only values of the following types are in the Bundle:
2335 * <ul>
2336 * <li>Integer</li>
2337 * <li>Long</li>
2338 * <li>Boolean</li>
2339 * <li>Float</li>
2340 * <li>Double</li>
2341 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002342 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 * <li>null</li>
2344 * </ul>
2345 * @param extras the Bundle to check
2346 */
2347 public static void validateSyncExtrasBundle(Bundle extras) {
2348 try {
2349 for (String key : extras.keySet()) {
2350 Object value = extras.get(key);
2351 if (value == null) continue;
2352 if (value instanceof Long) continue;
2353 if (value instanceof Integer) continue;
2354 if (value instanceof Boolean) continue;
2355 if (value instanceof Float) continue;
2356 if (value instanceof Double) continue;
2357 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002358 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 throw new IllegalArgumentException("unexpected value type: "
2360 + value.getClass().getName());
2361 }
2362 } catch (IllegalArgumentException e) {
2363 throw e;
2364 } catch (RuntimeException exc) {
2365 throw new IllegalArgumentException("error unparceling Bundle", exc);
2366 }
2367 }
2368
Fred Quintanaac9385e2009-06-22 18:00:59 -07002369 /**
2370 * Cancel any active or pending syncs that match the Uri. If the uri is null then
2371 * all syncs will be canceled.
2372 *
2373 * @param uri the uri of the provider to sync or null to sync all providers.
2374 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
2375 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002376 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002378 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
2379 }
2380
2381 /**
2382 * Cancel any active or pending syncs that match account and authority. The account and
2383 * authority can each independently be set to null, which means that syncs with any account
2384 * or authority, respectively, will match.
2385 *
2386 * @param account filters the syncs that match by this account
2387 * @param authority filters the syncs that match by this authority
2388 */
2389 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002391 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002393 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 }
2395 }
2396
Fred Quintanaac9385e2009-06-22 18:00:59 -07002397 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002398 * @see #cancelSync(Account, String)
2399 * @hide
2400 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002401 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002402 try {
2403 getContentService().cancelSyncAsUser(account, authority, null, userId);
2404 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002405 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002406 }
2407 }
2408
2409 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002410 * Get information about the SyncAdapters that are known to the system.
2411 * @return an array of SyncAdapters that have registered with the system
2412 */
2413 public static SyncAdapterType[] getSyncAdapterTypes() {
2414 try {
2415 return getContentService().getSyncAdapterTypes();
2416 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002417 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002418 }
2419 }
2420
2421 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002422 * @see #getSyncAdapterTypes()
2423 * @hide
2424 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002425 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002426 try {
2427 return getContentService().getSyncAdapterTypesAsUser(userId);
2428 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002429 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002430 }
2431 }
2432
2433 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07002434 * @hide
2435 * Returns the package names of syncadapters that match a given user and authority.
2436 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07002437 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07002438 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002439 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002440 try {
2441 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
2442 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002443 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07002444 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07002445 }
2446
2447 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002448 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002449 * <p>This method requires the caller to hold the permission
2450 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002451 *
2452 * @param account the account whose setting we are querying
2453 * @param authority the provider whose setting we are querying
2454 * @return true if the provider should be synced when a network tickle is received
2455 */
2456 public static boolean getSyncAutomatically(Account account, String authority) {
2457 try {
2458 return getContentService().getSyncAutomatically(account, authority);
2459 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002460 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002461 }
2462 }
2463
2464 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002465 * @see #getSyncAutomatically(Account, String)
2466 * @hide
2467 */
2468 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002469 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002470 try {
2471 return getContentService().getSyncAutomaticallyAsUser(account, authority, userId);
2472 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002473 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002474 }
2475 }
2476
2477 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002478 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002479 * <p>This method requires the caller to hold the permission
2480 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002481 *
2482 * @param account the account whose setting we are querying
2483 * @param authority the provider whose behavior is being controlled
2484 * @param sync true if the provider should be synced when tickles are received for it
2485 */
2486 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002487 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002488 }
2489
2490 /**
2491 * @see #setSyncAutomatically(Account, String, boolean)
2492 * @hide
2493 */
2494 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002495 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002496 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002497 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002498 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002499 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002500 }
2501 }
2502
2503 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002504 * Specifies that a sync should be requested with the specified the account, authority,
2505 * and extras at the given frequency. If there is already another periodic sync scheduled
2506 * with the account, authority and extras then a new periodic sync won't be added, instead
2507 * the frequency of the previous one will be updated.
2508 * <p>
2509 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
2510 * Although these sync are scheduled at the specified frequency, it may take longer for it to
2511 * actually be started if other syncs are ahead of it in the sync operation queue. This means
2512 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08002513 * <p>
2514 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
2515 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
2516 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
2517 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
2518 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002519 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002520 * <p>This method requires the caller to hold the permission
2521 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002522 * <p>The bundle for a periodic sync can be queried by applications with the correct
2523 * permissions using
2524 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
2525 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002526 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002527 * @param account the account to specify in the sync
2528 * @param authority the provider to specify in the sync request
2529 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07002530 * @param pollFrequency how frequently the sync should be performed, in seconds.
2531 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
2532 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08002533 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
2534 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002535 */
2536 public static void addPeriodicSync(Account account, String authority, Bundle extras,
2537 long pollFrequency) {
2538 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08002539 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002540 throw new IllegalArgumentException("illegal extras were set");
2541 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002542 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07002543 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002544 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002545 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002546 }
2547 }
2548
2549 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002550 * {@hide}
2551 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
2552 * extras were set for a periodic sync.
2553 *
2554 * @param extras bundle to validate.
2555 */
2556 public static boolean invalidPeriodicExtras(Bundle extras) {
2557 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
2558 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
2559 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
2560 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
2561 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
2562 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
2563 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
2564 return true;
2565 }
2566 return false;
2567 }
2568
2569 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002570 * Remove a periodic sync. Has no affect if account, authority and extras don't match
2571 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002572 * <p>This method requires the caller to hold the permission
2573 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002574 *
2575 * @param account the account of the periodic sync to remove
2576 * @param authority the provider of the periodic sync to remove
2577 * @param extras the extras of the periodic sync to remove
2578 */
2579 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
2580 validateSyncExtrasBundle(extras);
2581 try {
2582 getContentService().removePeriodicSync(account, authority, extras);
2583 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002584 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002585 }
2586 }
2587
2588 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002589 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
2590 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07002591 * <p>
2592 * If a periodic sync is specified, the caller must hold the permission
2593 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
2594 *</p>
2595 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002596 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07002597 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07002598 *
2599 * @param request SyncRequest object containing information about sync to cancel.
2600 */
2601 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002602 if (request == null) {
2603 throw new IllegalArgumentException("request cannot be null");
2604 }
2605 try {
2606 getContentService().cancelRequest(request);
2607 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002608 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002609 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002610 }
2611
2612 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002613 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002614 * <p>This method requires the caller to hold the permission
2615 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002616 *
2617 * @param account the account whose periodic syncs we are querying
2618 * @param authority the provider whose periodic syncs we are querying
2619 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
2620 */
2621 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
2622 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002623 return getContentService().getPeriodicSyncs(account, authority, null);
2624 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002625 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002626 }
2627 }
2628
2629 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002630 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002631 * <p>This method requires the caller to hold the permission
2632 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07002633 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
2634 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002635 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002636 try {
2637 return getContentService().getIsSyncable(account, authority);
2638 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002639 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002640 }
2641 }
2642
2643 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002644 * @see #getIsSyncable(Account, String)
2645 * @hide
2646 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002647 public static int getIsSyncableAsUser(Account account, String authority,
2648 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002649 try {
2650 return getContentService().getIsSyncableAsUser(account, authority, userId);
2651 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002652 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002653 }
2654 }
2655
2656 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002657 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002658 * <p>This method requires the caller to hold the permission
2659 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07002660 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07002661 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002662 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002663 try {
2664 getContentService().setIsSyncable(account, authority, syncable);
2665 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002666 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002667 }
2668 }
2669
2670 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002671 * Gets the master auto-sync setting that applies to all the providers and accounts.
2672 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002673 * <p>This method requires the caller to hold the permission
2674 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002675 *
2676 * @return the master auto-sync setting that applies to all the providers and accounts
2677 */
2678 public static boolean getMasterSyncAutomatically() {
2679 try {
2680 return getContentService().getMasterSyncAutomatically();
2681 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002682 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002683 }
2684 }
2685
2686 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002687 * @see #getMasterSyncAutomatically()
2688 * @hide
2689 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002690 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002691 try {
2692 return getContentService().getMasterSyncAutomaticallyAsUser(userId);
2693 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002694 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002695 }
2696 }
2697
2698 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002699 * Sets the master auto-sync setting that applies to all the providers and accounts.
2700 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002701 * <p>This method requires the caller to hold the permission
2702 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002703 *
2704 * @param sync the master auto-sync setting that applies to all the providers and accounts
2705 */
2706 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002707 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002708 }
2709
2710 /**
2711 * @see #setMasterSyncAutomatically(boolean)
2712 * @hide
2713 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002714 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002715 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002716 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002717 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002718 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002719 }
2720 }
2721
2722 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002723 * Returns true if there is currently a sync operation for the given account or authority
2724 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002725 * <p>This method requires the caller to hold the permission
2726 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002727 * @param account the account whose setting we are querying
2728 * @param authority the provider whose behavior is being queried
2729 * @return true if a sync is active for the given account or authority.
2730 */
2731 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002732 if (account == null) {
2733 throw new IllegalArgumentException("account must not be null");
2734 }
2735 if (authority == null) {
2736 throw new IllegalArgumentException("authority must not be null");
2737 }
2738
Fred Quintanaac9385e2009-06-22 18:00:59 -07002739 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002740 return getContentService().isSyncActive(account, authority, null);
2741 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002742 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002743 }
2744 }
2745
Fred Quintanaac9385e2009-06-22 18:00:59 -07002746 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07002747 * If a sync is active returns the information about it, otherwise returns null.
2748 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002749 * This method requires the caller to hold the permission
2750 * {@link android.Manifest.permission#READ_SYNC_STATS}.
2751 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07002752 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07002753 * @deprecated
2754 * Since multiple concurrent syncs are now supported you should use
2755 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
2756 * This method returns the first item from the list of current syncs
2757 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002758 */
Fred Quintanac6a69552010-09-27 17:05:04 -07002759 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07002760 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002761 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07002762 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
2763 if (syncs.isEmpty()) {
2764 return null;
2765 }
2766 return syncs.get(0);
2767 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002768 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07002769 }
2770 }
2771
2772 /**
2773 * Returns a list with information about all the active syncs. This list will be empty
2774 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002775 * <p>
2776 * This method requires the caller to hold the permission
2777 * {@link android.Manifest.permission#READ_SYNC_STATS}.
2778 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07002779 * @return a List of SyncInfo objects for the currently active syncs.
2780 */
2781 public static List<SyncInfo> getCurrentSyncs() {
2782 try {
2783 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002784 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002785 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002786 }
2787 }
2788
2789 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002790 * @see #getCurrentSyncs()
2791 * @hide
2792 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002793 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002794 try {
2795 return getContentService().getCurrentSyncsAsUser(userId);
2796 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002797 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002798 }
2799 }
2800
2801 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07002802 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002803 * @param account the account whose setting we are querying
2804 * @param authority the provider whose behavior is being queried
2805 * @return the SyncStatusInfo for the authority, or null if none exists
2806 * @hide
2807 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01002808 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07002809 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
2810 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002811 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002812 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002813 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002814 }
2815 }
2816
2817 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002818 * @see #getSyncStatus(Account, String)
2819 * @hide
2820 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01002821 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002822 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002823 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002824 try {
2825 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
2826 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002827 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002828 }
2829 }
2830
2831 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002832 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002833 * <p>This method requires the caller to hold the permission
2834 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002835 * @param account the account whose setting we are querying
2836 * @param authority the provider whose behavior is being queried
2837 * @return true if there is a pending sync with the matching account and authority
2838 */
2839 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002840 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002841 }
2842
2843 /**
2844 * @see #requestSync(Account, String, Bundle)
2845 * @hide
2846 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002847 public static boolean isSyncPendingAsUser(Account account, String authority,
2848 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002849 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002850 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002851 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002852 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002853 }
2854 }
2855
Fred Quintana1b487ec2010-02-26 10:57:55 -08002856 /**
2857 * Request notifications when the different aspects of the SyncManager change. The
2858 * different items that can be requested are:
2859 * <ul>
2860 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
2861 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
2862 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
2863 * </ul>
2864 * The caller can set one or more of the status types in the mask for any
2865 * given listener registration.
2866 * @param mask the status change types that will cause the callback to be invoked
2867 * @param callback observer to be invoked when the status changes
2868 * @return a handle that can be used to remove the listener at a later time
2869 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07002870 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08002871 if (callback == null) {
2872 throw new IllegalArgumentException("you passed in a null callback");
2873 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07002874 try {
2875 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08002876 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07002877 public void onStatusChanged(int which) throws RemoteException {
2878 callback.onStatusChanged(which);
2879 }
2880 };
2881 getContentService().addStatusChangeListener(mask, observer);
2882 return observer;
2883 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002884 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002885 }
2886 }
2887
Fred Quintana1b487ec2010-02-26 10:57:55 -08002888 /**
2889 * Remove a previously registered status change listener.
2890 * @param handle the handle that was returned by {@link #addStatusChangeListener}
2891 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07002892 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08002893 if (handle == null) {
2894 throw new IllegalArgumentException("you passed in a null handle");
2895 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07002896 try {
2897 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
2898 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002899 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002900 }
2901 }
2902
Jeff Sharkey87314082016-03-11 17:25:11 -07002903 /** {@hide} */
2904 public void putCache(Uri key, Bundle value) {
2905 try {
2906 getContentService().putCache(mContext.getPackageName(), key, value,
2907 mContext.getUserId());
2908 } catch (RemoteException e) {
2909 throw e.rethrowFromSystemServer();
2910 }
2911 }
2912
2913 /** {@hide} */
2914 public Bundle getCache(Uri key) {
2915 try {
2916 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
2917 mContext.getUserId());
2918 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
2919 return bundle;
2920 } catch (RemoteException e) {
2921 throw e.rethrowFromSystemServer();
2922 }
2923 }
2924
Ben Lin8ea82002017-03-08 17:30:16 -08002925 /** {@hide} */
2926 public int getTargetSdkVersion() {
2927 return mTargetSdkVersion;
2928 }
2929
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002930 /**
2931 * Returns sampling percentage for a given duration.
2932 *
2933 * Always returns at least 1%.
2934 */
2935 private int samplePercentForDuration(long durationMillis) {
2936 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
2937 return 100;
2938 }
2939 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
2940 }
2941
Steve McKayea93fe72016-12-02 11:35:35 -08002942 private void maybeLogQueryToEventLog(
2943 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07002944 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002945 int samplePercent = samplePercentForDuration(durationMillis);
2946 if (samplePercent < 100) {
2947 synchronized (mRandom) {
2948 if (mRandom.nextInt(100) >= samplePercent) {
2949 return;
2950 }
2951 }
2952 }
2953
Steve McKayea93fe72016-12-02 11:35:35 -08002954 // Ensure a non-null bundle.
2955 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
2956
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002957 StringBuilder projectionBuffer = new StringBuilder(100);
2958 if (projection != null) {
2959 for (int i = 0; i < projection.length; ++i) {
2960 // Note: not using a comma delimiter here, as the
2961 // multiple arguments to EventLog.writeEvent later
2962 // stringify with a comma delimiter, which would make
2963 // parsing uglier later.
2964 if (i != 0) projectionBuffer.append('/');
2965 projectionBuffer.append(projection[i]);
2966 }
2967 }
2968
2969 // ActivityThread.currentPackageName() only returns non-null if the
2970 // current thread is an application main thread. This parameter tells
2971 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002972 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002973
2974 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07002975 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002976 uri.toString(),
2977 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08002978 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
2979 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002980 durationMillis,
2981 blockingPackage != null ? blockingPackage : "",
2982 samplePercent);
2983 }
2984
2985 private void maybeLogUpdateToEventLog(
2986 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07002987 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002988 int samplePercent = samplePercentForDuration(durationMillis);
2989 if (samplePercent < 100) {
2990 synchronized (mRandom) {
2991 if (mRandom.nextInt(100) >= samplePercent) {
2992 return;
2993 }
2994 }
2995 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002996 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002997 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07002998 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002999 uri.toString(),
3000 operation,
3001 selection != null ? selection : "",
3002 durationMillis,
3003 blockingPackage != null ? blockingPackage : "",
3004 samplePercent);
3005 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003006
Jeff Brown825c5132011-10-12 16:11:30 -07003007 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003008 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003009 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003010
3011 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003013 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003015 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003016 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 }
3018
3019 @Override
3020 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003021 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003023
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003024 if (mProviderReleased.compareAndSet(false, true)) {
3025 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 }
3028
3029 @Override
3030 protected void finalize() throws Throwable {
3031 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003032 if (mCloseGuard != null) {
3033 mCloseGuard.warnIfOpen();
3034 }
3035
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003036 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 } finally {
3038 super.finalize();
3039 }
3040 }
3041 }
3042
3043 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003044 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003045 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046
3047 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3048 super(pfd);
3049 mContentProvider = icp;
3050 }
3051
3052 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003053 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003054 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003055 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 }
3057 }
3058 }
3059
Dianne Hackborn231cc602009-04-27 17:10:36 -07003060 /** @hide */
3061 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003062
Dianne Hackborn231cc602009-04-27 17:10:36 -07003063 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +01003064 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003065 public static IContentService getContentService() {
3066 if (sContentService != null) {
3067 return sContentService;
3068 }
3069 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003070 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003071 return sContentService;
3072 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003073
Dianne Hackborn35654b62013-01-14 17:38:02 -08003074 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +01003075 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003076 public String getPackageName() {
3077 return mPackageName;
3078 }
3079
Mathew Inwood1c77a112018-08-14 14:06:26 +01003080 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003081 private static volatile IContentService sContentService;
Mathew Inwood1c77a112018-08-14 14:06:26 +01003082 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003084
Mathew Inwood1c77a112018-08-14 14:06:26 +01003085 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003086 final String mPackageName;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003087 final int mTargetSdkVersion;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003090
3091 /** @hide */
3092 public int resolveUserId(Uri uri) {
3093 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3094 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003095
3096 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003097 public int getUserId() {
3098 return mContext.getUserId();
3099 }
3100
3101 /** @hide */
Daniel Nishic29d2b02016-06-30 12:20:41 -07003102 public Drawable getTypeDrawable(String mimeType) {
3103 return MimeIconUtils.loadMimeIcon(mContext, mimeType);
3104 }
Steve McKayea93fe72016-12-02 11:35:35 -08003105
3106 /**
3107 * @hide
3108 */
3109 public static @Nullable Bundle createSqlQueryBundle(
3110 @Nullable String selection,
3111 @Nullable String[] selectionArgs,
3112 @Nullable String sortOrder) {
3113
3114 if (selection == null && selectionArgs == null && sortOrder == null) {
3115 return null;
3116 }
3117
3118 Bundle queryArgs = new Bundle();
3119 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003120 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003121 }
3122 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003123 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003124 }
3125 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003126 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003127 }
3128 return queryArgs;
3129 }
Steve McKay29c3f682016-12-16 14:52:59 -08003130
3131 /**
3132 * Returns structured sort args formatted as an SQL sort clause.
3133 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003134 * NOTE: Collator clauses are suitable for use with non text fields. We might
3135 * choose to omit any collation clause since we don't know the underlying
3136 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3137 * appear to care much about the presence of collate clauses in queries
3138 * when ordering by numeric fields. For this reason we include collate
3139 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3140 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003141 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003142 * TODO: Would be nice to explicitly validate that colums referenced in
3143 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003144 *
3145 * @hide
3146 */
3147 public static String createSqlSortClause(Bundle queryArgs) {
3148 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3149 if (columns == null || columns.length == 0) {
3150 throw new IllegalArgumentException("Can't create sort clause without columns.");
3151 }
3152
3153 String query = TextUtils.join(", ", columns);
3154
Steve McKayd7ece9f2017-01-12 16:59:59 -08003155 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3156 // on their javadoc descriptions.
3157 int collation = queryArgs.getInt(
3158 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3159 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3160 query += " COLLATE NOCASE";
3161 }
3162
Steve McKay415f41b2017-02-01 13:38:25 -08003163 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3164 if (sortDir != Integer.MIN_VALUE) {
3165 switch (sortDir) {
3166 case QUERY_SORT_DIRECTION_ASCENDING:
3167 query += " ASC";
3168 break;
3169 case QUERY_SORT_DIRECTION_DESCENDING:
3170 query += " DESC";
3171 break;
3172 default:
3173 throw new IllegalArgumentException("Unsupported sort direction value."
3174 + " See ContentResolver documentation for details.");
3175 }
Steve McKay29c3f682016-12-16 14:52:59 -08003176 }
Steve McKay29c3f682016-12-16 14:52:59 -08003177 return query;
3178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179}