blob: ac98e12bda3e683f9aeec2135d1e951dd9efebd9 [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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 */
82public abstract class ContentResolver {
Fred Quintanaac9385e2009-06-22 18:00:59 -070083 /**
84 * @deprecated instead use
85 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
86 */
Fred Quintana4a6679b2009-08-17 13:05:39 -070087 @Deprecated
Fred Quintanaac9385e2009-06-22 18:00:59 -070088 public static final String SYNC_EXTRAS_ACCOUNT = "account";
Georgi Nikolovb3395572013-06-18 18:27:31 -070089
90 /**
91 * If this extra is set to true, the sync request will be scheduled
92 * at the front of the sync request queue and without any delay
93 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public static final String SYNC_EXTRAS_EXPEDITED = "expedited";
Georgi Nikolovb3395572013-06-18 18:27:31 -070095
Fred Quintanaac9385e2009-06-22 18:00:59 -070096 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +000097 * If this extra is set to true, the sync request will be scheduled
98 * only when the device is plugged in. This is equivalent to calling
99 * setRequiresCharging(true) on {@link SyncRequest}.
100 */
101 public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging";
102
103 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -0700104 * @deprecated instead use
105 * {@link #SYNC_EXTRAS_MANUAL}
106 */
Fred Quintana4a6679b2009-08-17 13:05:39 -0700107 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final String SYNC_EXTRAS_FORCE = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800109
110 /**
111 * If this extra is set to true then the sync settings (like getSyncAutomatically())
112 * are ignored by the sync scheduler.
113 */
114 public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings";
115
116 /**
117 * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries)
118 * are ignored by the sync scheduler. If this request fails and gets rescheduled then the
119 * retries will still honor the backoff.
120 */
121 public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff";
122
123 /**
124 * If this extra is set to true then the request will not be retried if it fails.
125 */
126 public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry";
127
128 /**
129 * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS}
130 * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF}
131 */
Fred Quintanaac9385e2009-06-22 18:00:59 -0700132 public static final String SYNC_EXTRAS_MANUAL = "force";
Fred Quintana53bd2522010-02-05 15:28:12 -0800133
Georgi Nikolovb3395572013-06-18 18:27:31 -0700134 /**
135 * Indicates that this sync is intended to only upload local changes to the server.
136 * For example, this will be set to true if the sync is initiated by a call to
137 * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}
138 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static final String SYNC_EXTRAS_UPLOAD = "upload";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700140
141 /**
142 * Indicates that the sync adapter should proceed with the delete operations,
143 * even if it determines that there are too many.
144 * See {@link SyncResult#tooManyDeletions}
145 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override";
Georgi Nikolovb3395572013-06-18 18:27:31 -0700147
148 /**
149 * Indicates that the sync adapter should not proceed with the delete operations,
150 * if it determines that there are too many.
151 * See {@link SyncResult#tooManyDeletions}
152 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions";
154
Matthew Williamsfa774182013-06-18 15:44:11 -0700155 /* Extensions to API. TODO: Not clear if we will keep these as public flags. */
156 /** {@hide} User-specified flag for expected upload size. */
157 public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload";
158
159 /** {@hide} User-specified flag for expected download size. */
160 public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download";
161
162 /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */
163 public static final String SYNC_EXTRAS_PRIORITY = "sync_priority";
164
165 /** {@hide} Flag to allow sync to occur on metered network. */
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700166 public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered";
Matthew Williamsfa774182013-06-18 15:44:11 -0700167
Fred Quintana4a6679b2009-08-17 13:05:39 -0700168 /**
Makoto Onuki75ad2492018-03-28 14:42:42 -0700169 * {@hide} Integer extra containing a SyncExemption flag.
Makoto Onuki61283ec2018-01-31 17:22:36 -0800170 *
171 * Only the system and the shell user can set it.
172 *
173 * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle.
174 */
Makoto Onuki75ad2492018-03-28 14:42:42 -0700175 public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption";
Makoto Onuki61283ec2018-01-31 17:22:36 -0800176
177 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -0700178 * Set by the SyncManager to request that the SyncAdapter initialize itself for
179 * the given account/authority pair. One required initialization step is to
180 * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been
181 * called with a >= 0 value. When this flag is set the SyncAdapter does not need to
182 * do a full sync, though it is allowed to do so.
183 */
184 public static final String SYNC_EXTRAS_INITIALIZE = "initialize";
185
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800186 /** @hide */
187 public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED =
188 new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED");
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final String SCHEME_CONTENT = "content";
191 public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
192 public static final String SCHEME_FILE = "file";
193
194 /**
Jeff Sharkey5b836f22014-08-27 14:46:32 -0700195 * An extra {@link Point} describing the optimal size for a requested image
196 * resource, in pixels. If a provider has multiple sizes of the image, it
197 * should return the image closest to this size.
198 *
199 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle)
200 * @see #openTypedAssetFileDescriptor(Uri, String, Bundle,
201 * CancellationSignal)
202 */
203 public static final String EXTRA_SIZE = "android.content.extra.SIZE";
204
205 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800206 * An extra boolean describing whether a particular provider supports refresh
207 * or not. If a provider supports refresh, it should include this key in its
208 * returned Cursor as part of its query call.
209 *
Ben Lin1cf454f2016-11-10 13:50:54 -0800210 */
211 public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED";
212
213 /**
Steve McKayea93fe72016-12-02 11:35:35 -0800214 * Key for an SQL style selection string that may be present in the query Bundle argument
215 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
216 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800217 *
218 * <p>Clients should never include user supplied values directly in the selection string,
219 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
220 * should use standard placeholder notation to represent values in a selection string,
221 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
222 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800223 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
224 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700225 *
226 * @see #QUERY_ARG_SORT_COLUMNS
227 * @see #QUERY_ARG_SORT_DIRECTION
228 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800229 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700230 public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection";
Steve McKayea93fe72016-12-02 11:35:35 -0800231
232 /**
Steve McKay29c3f682016-12-16 14:52:59 -0800233 * Key for SQL selection string arguments list.
234 *
235 * <p>Clients should never include user supplied values directly in the selection string,
236 * as this presents an avenue for SQL injection attacks. In lieu of this, a client
237 * should use standard placeholder notation to represent values in a selection string,
238 * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}.
239 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800240 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
241 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700242 *
243 * @see #QUERY_ARG_SORT_COLUMNS
244 * @see #QUERY_ARG_SORT_DIRECTION
245 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800246 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700247 public static final String QUERY_ARG_SQL_SELECTION_ARGS =
248 "android:query-arg-sql-selection-args";
Steve McKayea93fe72016-12-02 11:35:35 -0800249
250 /**
251 * Key for an SQL style sort string that may be present in the query Bundle argument
252 * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}
253 * when called by a legacy client.
Steve McKay29c3f682016-12-16 14:52:59 -0800254 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800255 * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly
256 * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b>
Steve McKayd74a3bd2017-04-24 12:07:53 -0700257 *
258 * @see #QUERY_ARG_SORT_COLUMNS
259 * @see #QUERY_ARG_SORT_DIRECTION
260 * @see #QUERY_ARG_SORT_COLLATION
Steve McKayea93fe72016-12-02 11:35:35 -0800261 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700262 public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order";
Steve McKay29c3f682016-12-16 14:52:59 -0800263
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600264 /** {@hide} */
265 public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by";
266 /** {@hide} */
267 public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having";
268 /** {@hide} */
269 public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit";
270
Steve McKay29c3f682016-12-16 14:52:59 -0800271 /**
Steve McKayd7ece9f2017-01-12 16:59:59 -0800272 * Specifies the list of columns against which to sort results. When first column values
273 * are identical, records are then sorted based on second column values, and so on.
Steve McKay29c3f682016-12-16 14:52:59 -0800274 *
275 * <p>Columns present in this list must also be included in the projection
276 * supplied to {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}.
277 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800278 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
Steve McKay29c3f682016-12-16 14:52:59 -0800279 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700280 * <li>{@link ContentProvider} implementations: When preparing data in
281 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort columns
282 * is reflected in the returned Cursor, it is strongly recommended that
283 * {@link #QUERY_ARG_SORT_COLUMNS} then be included in the array of honored arguments
284 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800285 *
286 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
287 * arguments {@link Bundle}, the Content framework will attempt to synthesize
288 * an QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800289 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700290 public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns";
Steve McKay29c3f682016-12-16 14:52:59 -0800291
292 /**
293 * Specifies desired sort order. When unspecified a provider may provide a default
294 * sort direction, or choose to return unsorted results.
295 *
Steve McKayd7ece9f2017-01-12 16:59:59 -0800296 * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher:
297 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700298 * <li>{@link ContentProvider} implementations: When preparing data in
299 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction
300 * is reflected in the returned Cursor, it is strongly recommended that
301 * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments
302 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800303 *
304 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
305 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700306 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKay29c3f682016-12-16 14:52:59 -0800307 *
308 * @see #QUERY_SORT_DIRECTION_ASCENDING
309 * @see #QUERY_SORT_DIRECTION_DESCENDING
310 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700311 public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction";
Steve McKay29c3f682016-12-16 14:52:59 -0800312
313 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700314 * Allows client to specify a hint to the provider declaring which collation
Steve McKay29c3f682016-12-16 14:52:59 -0800315 * to use when sorting text values.
316 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700317 * <p>Providers may support custom collators. When specifying a custom collator
318 * the value is determined by the Provider.
Steve McKay29c3f682016-12-16 14:52:59 -0800319 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700320 * <li>{@link ContentProvider} implementations: When preparing data in
321 * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort collation
322 * is reflected in the returned Cursor, it is strongly recommended that
323 * {@link #QUERY_ARG_SORT_COLLATION} then be included in the array of honored arguments
324 * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}.
Steve McKay29c3f682016-12-16 14:52:59 -0800325 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700326 * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the
Steve McKayd7ece9f2017-01-12 16:59:59 -0800327 * arguments {@link Bundle}, the Content framework will attempt to synthesize
Steve McKayd74a3bd2017-04-24 12:07:53 -0700328 * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800329 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700330 * @see java.text.Collator#PRIMARY
331 * @see java.text.Collator#SECONDARY
332 * @see java.text.Collator#TERTIARY
333 * @see java.text.Collator#IDENTICAL
Steve McKay29c3f682016-12-16 14:52:59 -0800334 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700335 public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation";
Steve McKay29c3f682016-12-16 14:52:59 -0800336
Steve McKay415f41b2017-02-01 13:38:25 -0800337 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700338 * Allows provider to report back to client which query keys are honored in a Cursor.
Steve McKay415f41b2017-02-01 13:38:25 -0800339 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700340 * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments
Steve McKay415f41b2017-02-01 13:38:25 -0800341 * honored by the provider. Include this in {@link Cursor} extras {@link Bundle}
342 * when any QUERY_ARG_SORT* value was honored during the preparation of the
343 * results {@link Cursor}.
344 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700345 * <p>If present, ALL honored arguments are enumerated in this extra’s payload.
346 *
347 * @see #QUERY_ARG_SORT_COLUMNS
348 * @see #QUERY_ARG_SORT_DIRECTION
349 * @see #QUERY_ARG_SORT_COLLATION
Steve McKay415f41b2017-02-01 13:38:25 -0800350 */
351 public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS";
352
Steve McKay29c3f682016-12-16 14:52:59 -0800353 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700354 @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = {
Steve McKay29c3f682016-12-16 14:52:59 -0800355 QUERY_SORT_DIRECTION_ASCENDING,
356 QUERY_SORT_DIRECTION_DESCENDING
357 })
358 @Retention(RetentionPolicy.SOURCE)
359 public @interface SortDirection {}
360 public static final int QUERY_SORT_DIRECTION_ASCENDING = 0;
361 public static final int QUERY_SORT_DIRECTION_DESCENDING = 1;
362
363 /**
364 * @see {@link java.text.Collector} for details on respective collation strength.
365 * @hide
366 */
367 @IntDef(flag = false, value = {
368 java.text.Collator.PRIMARY,
369 java.text.Collator.SECONDARY,
370 java.text.Collator.TERTIARY,
371 java.text.Collator.IDENTICAL
372 })
373 @Retention(RetentionPolicy.SOURCE)
374 public @interface QueryCollator {}
Steve McKayea93fe72016-12-02 11:35:35 -0800375
376 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700377 * Specifies the offset row index within a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800378 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700379 public static final String QUERY_ARG_OFFSET = "android:query-arg-offset";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800380
381 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700382 * Specifies the max number of rows to include in a Cursor.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800383 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700384 public static final String QUERY_ARG_LIMIT = "android:query-arg-limit";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800385
386 /**
Steve McKayd74a3bd2017-04-24 12:07:53 -0700387 * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of
388 * recordset when paging is supported. Providers must include this when
Steve McKay415f41b2017-02-01 13:38:25 -0800389 * implementing paging support.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800390 *
Steve McKayd74a3bd2017-04-24 12:07:53 -0700391 * <p>A provider may return -1 that row count of the recordset is unknown.
Steve McKayd7ece9f2017-01-12 16:59:59 -0800392 *
393 * <p>Providers having returned -1 in a previous query are recommended to
394 * send content change notification once (if) full recordset size becomes
395 * known.
396 */
Steve McKayd74a3bd2017-04-24 12:07:53 -0700397 public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT";
Steve McKayd7ece9f2017-01-12 16:59:59 -0800398
399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * This is the Android platform's base MIME type for a content: URI
401 * containing a Cursor of a single item. Applications should use this
402 * as the base type along with their own sub-type of their content: URIs
403 * that represent a particular item. For example, hypothetical IMAP email
404 * client may have a URI
405 * <code>content://com.company.provider.imap/inbox/1</code> for a particular
406 * message in the inbox, whose MIME type would be reported as
407 * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}.
410 */
411 public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * This is the Android platform's base MIME type for a content: URI
415 * containing a Cursor of zero or more items. Applications should use this
416 * as the base type along with their own sub-type of their content: URIs
417 * that represent a directory of items. For example, hypothetical IMAP email
418 * client may have a URI
419 * <code>content://com.company.provider.imap/inbox</code> for all of the
420 * messages in its inbox, whose MIME type would be reported as
421 * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800422 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * <p>Note how the base MIME type varies between this and
424 * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is
425 * one single item or multiple items in the data set, while the sub-type
426 * remains the same because in either case the data structure contained
427 * in the cursor is the same.
428 */
429 public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir";
Fred Quintanaac9385e2009-06-22 18:00:59 -0700430
Matt Caseybd7bcf02014-02-05 15:51:39 -0800431 /**
432 * This is the Android platform's generic MIME type to match any MIME
433 * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}".
434 * {@code SUB_TYPE} is the sub-type of the application-dependent
435 * content, e.g., "audio", "video", "playlist".
436 */
437 public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*";
438
Fred Quintanaac9385e2009-06-22 18:00:59 -0700439 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100440 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700441 public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1;
442 /** @hide */
443 public static final int SYNC_ERROR_AUTHENTICATION = 2;
444 /** @hide */
445 public static final int SYNC_ERROR_IO = 3;
446 /** @hide */
447 public static final int SYNC_ERROR_PARSE = 4;
448 /** @hide */
449 public static final int SYNC_ERROR_CONFLICT = 5;
450 /** @hide */
451 public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6;
452 /** @hide */
453 public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7;
454 /** @hide */
455 public static final int SYNC_ERROR_INTERNAL = 8;
456
Alon Albert57286f92012-10-09 14:21:38 -0700457 private static final String[] SYNC_ERROR_NAMES = new String[] {
458 "already-in-progress",
459 "authentication-error",
460 "io-error",
461 "parse-error",
462 "conflict",
463 "too-many-deletions",
464 "too-many-retries",
465 "internal-error",
466 };
467
468 /** @hide */
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800469 public static String syncErrorToString(int error) {
Alon Albert57286f92012-10-09 14:21:38 -0700470 if (error < 1 || error > SYNC_ERROR_NAMES.length) {
471 return String.valueOf(error);
472 }
473 return SYNC_ERROR_NAMES[error - 1];
474 }
475
Alon Albert5c113fa2013-02-07 08:07:32 -0800476 /** @hide */
477 public static int syncErrorStringToInt(String error) {
478 for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) {
479 if (SYNC_ERROR_NAMES[i].equals(error)) {
480 return i + 1;
481 }
482 }
483 if (error != null) {
484 try {
485 return Integer.parseInt(error);
486 } catch (NumberFormatException e) {
487 Log.d(TAG, "error parsing sync error: " + error);
488 }
489 }
490 return 0;
491 }
492
Fred Quintanaac9385e2009-06-22 18:00:59 -0700493 public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700494 public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
Fred Quintanaac9385e2009-06-22 18:00:59 -0700495 public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
496 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100497 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -0700498 public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
499 /** @hide */
500 public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff;
501
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700502 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700503 @IntDef(flag = true, prefix = { "NOTIFY_" }, value = {
504 NOTIFY_SYNC_TO_NETWORK,
505 NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS
506 })
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700507 @Retention(RetentionPolicy.SOURCE)
508 public @interface NotifyFlags {}
509
510 /**
511 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change
512 * to the network.
513 */
514 public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0;
515
516 /**
517 * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification
518 * will be skipped if it is being delivered to the root URI of a ContentObserver that is
519 * using "notify for descendants." The purpose of this is to allow the provide to send
520 * a general notification of "something under X" changed that observers of that specific
521 * URI can receive, while also sending a specific URI under X. It would use this flag
522 * when sending the former, so that observers of "X and descendants" only see the latter.
523 */
524 public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1;
525
Makoto Onuki75ad2492018-03-28 14:42:42 -0700526 /**
527 * No exception, throttled by app standby normally.
528 * @hide
529 */
530 public static final int SYNC_EXEMPTION_NONE = 0;
531
532 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700533 * Exemption given to a sync request made by a foreground app (including
534 * PROCESS_STATE_IMPORTANT_FOREGROUND).
Makoto Onuki75ad2492018-03-28 14:42:42 -0700535 *
Makoto Onukid5f25d22018-05-22 16:02:17 -0700536 * At the schedule time, we promote the sync adapter app for a higher bucket:
537 * - If the device is not dozing (so the sync will start right away)
538 * promote to ACTIVE for 1 hour.
539 * - If the device is dozing (so the sync *won't* start right away),
540 * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the
541 * device comes out of doze.
542 * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes,
543 * so it can schedule and start more syncs without getting throttled, even when the first
544 * operation was canceled and now we're retrying.
545 *
546 *
Makoto Onuki75ad2492018-03-28 14:42:42 -0700547 * @hide
548 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700549 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700550
551 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700552 * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the
Makoto Onuki75ad2492018-03-28 14:42:42 -0700553 * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away.
554 * @hide
555 */
Makoto Onukid5f25d22018-05-22 16:02:17 -0700556 public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2;
Makoto Onuki75ad2492018-03-28 14:42:42 -0700557
558 /** @hide */
559 @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = {
560 SYNC_EXEMPTION_NONE,
Makoto Onukid5f25d22018-05-22 16:02:17 -0700561 SYNC_EXEMPTION_PROMOTE_BUCKET,
562 SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP,
Makoto Onuki75ad2492018-03-28 14:42:42 -0700563 })
564 @Retention(RetentionPolicy.SOURCE)
565 public @interface SyncExemption {}
566
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800567 // Always log queries which take 500ms+; shorter queries are
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800568 // sampled accordingly.
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -0700569 private static final boolean ENABLE_CONTENT_SAMPLE = false;
Brad Fitzpatrick25880962010-02-22 15:17:49 -0800570 private static final int SLOW_THRESHOLD_MILLIS = 500;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800571 private final Random mRandom = new Random(); // guarded by itself
572
Dianne Hackborn231cc602009-04-27 17:10:36 -0700573 public ContentResolver(Context context) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800574 mContext = context != null ? context : ActivityThread.currentApplication();
Dianne Hackborn95d78532013-09-11 09:51:14 -0700575 mPackageName = mContext.getOpPackageName();
Jeff Sharkey4b2e87f2017-04-26 00:36:02 +0000576 mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578
579 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100580 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 protected abstract IContentProvider acquireProvider(Context c, String name);
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700582
583 /**
584 * Providing a default implementation of this, to avoid having to change a
585 * lot of other things, but implementations of ContentResolver should
586 * implement it.
587 *
588 * @hide
589 */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100590 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700591 protected IContentProvider acquireExistingProvider(Context c, String name) {
592 return acquireProvider(c, name);
593 }
Jeff Sharkeye66c1772013-09-20 14:30:59 -0700594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100596 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 public abstract boolean releaseProvider(IContentProvider icp);
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700598 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100599 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700600 protected abstract IContentProvider acquireUnstableProvider(Context c, String name);
601 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100602 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -0700603 public abstract boolean releaseUnstableProvider(IContentProvider icp);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700604 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100605 @UnsupportedAppUsage
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700606 public abstract void unstableProviderDied(IContentProvider icp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700608 /** @hide */
609 public void appNotRespondingViaProvider(IContentProvider icp) {
610 throw new UnsupportedOperationException("appNotRespondingViaProvider");
611 }
612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 /**
614 * Return the MIME type of the given content URL.
615 *
616 * @param url A Uri identifying content (either a list or specific type),
617 * using the content:// scheme.
618 * @return A MIME type for the content, or null if the URL is invalid or the type is unknown
619 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700620 public final @Nullable String getType(@NonNull Uri url) {
621 Preconditions.checkNotNull(url, "url");
622
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700623 // XXX would like to have an acquireExistingUnstableProvider for this.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700624 IContentProvider provider = acquireExistingProvider(url);
625 if (provider != null) {
626 try {
627 return provider.getType(url);
628 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600629 // Arbitrary and not worth documenting, as Activity
630 // Manager will kill this process shortly anyway.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700631 return null;
632 } catch (java.lang.Exception e) {
Ola Olsson145e6c42010-12-20 16:45:35 +0100633 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700634 return null;
635 } finally {
636 releaseProvider(provider);
637 }
638 }
639
640 if (!SCHEME_CONTENT.equals(url.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 return null;
642 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800645 String type = ActivityManager.getService().getProviderMimeType(
Nicolas Prevot1dddc7f2014-07-07 17:44:58 +0100646 ContentProvider.getUriWithoutUserId(url), resolveUserId(url));
Dianne Hackborncca1f0e2010-09-26 18:34:53 -0700647 return type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -0600649 throw e.rethrowFromSystemServer();
Ola Olsson145e6c42010-12-20 16:45:35 +0100650 } catch (java.lang.Exception e) {
651 Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")");
652 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
654 }
655
656 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700657 * Query for the possible MIME types for the representations the given
658 * content URL can be returned when opened as as stream with
659 * {@link #openTypedAssetFileDescriptor}. Note that the types here are
660 * not necessarily a superset of the type returned by {@link #getType} --
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700661 * many content providers cannot return a raw stream for the structured
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700662 * data that they contain.
663 *
664 * @param url A Uri identifying content (either a list or specific type),
665 * using the content:// scheme.
666 * @param mimeTypeFilter The desired MIME type. This may be a pattern,
John Spurlock33900182014-01-02 11:04:18 -0500667 * such as *&#47;*, to query for all available MIME types that match the
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700668 * pattern.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700669 * @return Returns an array of MIME type strings for all available
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700670 * data streams that match the given mimeTypeFilter. If there are none,
671 * null is returned.
672 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700673 public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) {
674 Preconditions.checkNotNull(url, "url");
675 Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter");
676
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700677 IContentProvider provider = acquireProvider(url);
678 if (provider == null) {
679 return null;
680 }
Dianne Hackborn64bbbb42010-09-27 20:25:20 -0700681
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700682 try {
683 return provider.getStreamTypes(url, mimeTypeFilter);
684 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800685 // Arbitrary and not worth documenting, as Activity
686 // Manager will kill this process shortly anyway.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700687 return null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700688 } finally {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800689 releaseProvider(provider);
690 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700691 }
692
693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 * Query the given URI, returning a {@link Cursor} over the result set.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800695 * <p>
696 * For best performance, the caller should follow these guidelines:
697 * <ul>
698 * <li>Provide an explicit projection, to prevent
699 * reading data from storage that aren't going to be used.</li>
700 * <li>Use question mark parameter markers such as 'phone=?' instead of
701 * explicit values in the {@code selection} parameter, so that queries
702 * that differ only by those values will be recognized as the same
703 * for caching purposes.</li>
704 * </ul>
705 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 *
707 * @param uri The URI, using the content:// scheme, for the content to
708 * retrieve.
709 * @param projection A list of which columns to return. Passing null will
Tom O'Neill0ba1cb02010-01-08 12:53:50 -0800710 * return all columns, which is inefficient.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 * @param selection A filter declaring which rows to return, formatted as an
712 * SQL WHERE clause (excluding the WHERE itself). Passing null will
713 * return all rows for the given URI.
714 * @param selectionArgs You may include ?s in selection, which will be
715 * replaced by the values from selectionArgs, in the order that they
716 * appear in the selection. The values will be bound as Strings.
717 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
718 * clause (excluding the ORDER BY itself). Passing null will use the
719 * default sort order, which may be unordered.
720 * @return A Cursor object, which is positioned before the first entry, or null
721 * @see Cursor
722 */
Tor Norbye788fc2b2015-07-05 16:10:42 -0700723 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
724 @Nullable String[] projection, @Nullable String selection,
725 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800726 return query(uri, projection, selection, selectionArgs, sortOrder, null);
727 }
728
729 /**
Jeff Brownc64ff372013-10-09 18:50:56 -0700730 * Query the given URI, returning a {@link Cursor} over the result set
731 * with optional support for cancellation.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800732 * <p>
733 * For best performance, the caller should follow these guidelines:
734 * <ul>
735 * <li>Provide an explicit projection, to prevent
736 * reading data from storage that aren't going to be used.</li>
737 * <li>Use question mark parameter markers such as 'phone=?' instead of
738 * explicit values in the {@code selection} parameter, so that queries
739 * that differ only by those values will be recognized as the same
740 * for caching purposes.</li>
741 * </ul>
742 * </p>
743 *
744 * @param uri The URI, using the content:// scheme, for the content to
745 * retrieve.
746 * @param projection A list of which columns to return. Passing null will
747 * return all columns, which is inefficient.
748 * @param selection A filter declaring which rows to return, formatted as an
749 * SQL WHERE clause (excluding the WHERE itself). Passing null will
750 * return all rows for the given URI.
751 * @param selectionArgs You may include ?s in selection, which will be
752 * replaced by the values from selectionArgs, in the order that they
753 * appear in the selection. The values will be bound as Strings.
754 * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
755 * clause (excluding the ORDER BY itself). Passing null will use the
756 * default sort order, which may be unordered.
Jeff Brown4c1241d2012-02-02 17:05:00 -0800757 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
Jeff Brown75ea64f2012-01-25 19:37:13 -0800758 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
759 * when the query is executed.
760 * @return A Cursor object, which is positioned before the first entry, or null
761 * @see Cursor
762 */
Steve McKayea93fe72016-12-02 11:35:35 -0800763 public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
Tor Norbye788fc2b2015-07-05 16:10:42 -0700764 @Nullable String[] projection, @Nullable String selection,
765 @Nullable String[] selectionArgs, @Nullable String sortOrder,
766 @Nullable CancellationSignal cancellationSignal) {
Steve McKayea93fe72016-12-02 11:35:35 -0800767 Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder);
768 return query(uri, projection, queryArgs, cancellationSignal);
769 }
770
771 /**
772 * Query the given URI, returning a {@link Cursor} over the result set
773 * with support for cancellation.
774 *
775 * <p>For best performance, the caller should follow these guidelines:
776 *
777 * <li>Provide an explicit projection, to prevent reading data from storage
778 * that aren't going to be used.
779 *
Steve McKay415f41b2017-02-01 13:38:25 -0800780 * Provider must identify which QUERY_ARG_SORT* arguments were honored during
781 * the preparation of the result set by including the respective argument keys
782 * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS}
783 * for details.
784 *
785 * @see #QUERY_ARG_SORT_COLUMNS, #QUERY_ARG_SORT_DIRECTION, #QUERY_ARG_SORT_COLLATION.
786 *
Steve McKayea93fe72016-12-02 11:35:35 -0800787 * @param uri The URI, using the content:// scheme, for the content to
788 * retrieve.
789 * @param projection A list of which columns to return. Passing null will
790 * return all columns, which is inefficient.
791 * @param queryArgs A Bundle containing any arguments to the query.
792 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
793 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
794 * when the query is executed.
795 * @return A Cursor object, which is positioned before the first entry, or null
796 * @see Cursor
797 */
798 public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
799 @Nullable String[] projection, @Nullable Bundle queryArgs,
800 @Nullable CancellationSignal cancellationSignal) {
Jeff Sharkey673db442015-06-11 19:30:57 -0700801 Preconditions.checkNotNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700802 IContentProvider unstableProvider = acquireUnstableProvider(uri);
803 if (unstableProvider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 return null;
805 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700806 IContentProvider stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800807 Cursor qCursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800809 long startTime = SystemClock.uptimeMillis();
Jeff Brown75ea64f2012-01-25 19:37:13 -0800810
Jeff Brown4c1241d2012-02-02 17:05:00 -0800811 ICancellationSignal remoteCancellationSignal = null;
812 if (cancellationSignal != null) {
813 cancellationSignal.throwIfCanceled();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700814 remoteCancellationSignal = unstableProvider.createCancellationSignal();
Jeff Brown4c1241d2012-02-02 17:05:00 -0800815 cancellationSignal.setRemote(remoteCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -0800816 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700817 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800818 qCursor = unstableProvider.query(mPackageName, uri, projection,
Steve McKayea93fe72016-12-02 11:35:35 -0800819 queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700820 } catch (DeadObjectException e) {
821 // The remote process has died... but we only hold an unstable
822 // reference though, so we might recover!!! Let's try!!!!
823 // This is exciting!!1!!1!!!!1
824 unstableProviderDied(unstableProvider);
825 stableProvider = acquireProvider(uri);
826 if (stableProvider == null) {
827 return null;
828 }
Steve McKayea93fe72016-12-02 11:35:35 -0800829 qCursor = stableProvider.query(
830 mPackageName, uri, projection, queryArgs, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700831 }
Brad Fitzpatricka63730d2010-02-07 22:25:34 -0800832 if (qCursor == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 return null;
834 }
Jeff Brownc21b5a02013-01-07 17:15:12 -0800835
836 // Force query execution. Might fail and throw a runtime exception here.
Vasu Nori020e5342010-04-28 14:22:38 -0700837 qCursor.getCount();
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -0800838 long durationMillis = SystemClock.uptimeMillis() - startTime;
Steve McKayea93fe72016-12-02 11:35:35 -0800839 maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
Jeff Brownc21b5a02013-01-07 17:15:12 -0800840
841 // Wrap the cursor object into CursorWrapperInner object.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700842 final IContentProvider provider = (stableProvider != null) ? stableProvider
843 : acquireProvider(uri);
844 final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700845 stableProvider = null;
Jeff Brownc21b5a02013-01-07 17:15:12 -0800846 qCursor = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700847 return wrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -0800849 // Arbitrary and not worth documenting, as Activity
850 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 return null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700852 } finally {
Jeff Brownc21b5a02013-01-07 17:15:12 -0800853 if (qCursor != null) {
854 qCursor.close();
855 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700856 if (cancellationSignal != null) {
857 cancellationSignal.setRemote(null);
858 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700859 if (unstableProvider != null) {
860 releaseUnstableProvider(unstableProvider);
861 }
862 if (stableProvider != null) {
863 releaseProvider(stableProvider);
864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
866 }
867
Fred Quintana89437372009-05-15 15:10:40 -0700868 /**
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700869 * Transform the given <var>url</var> to a canonical representation of
870 * its referenced resource, which can be used across devices, persisted,
871 * backed up and restored, etc. The returned Uri is still a fully capable
872 * Uri for use with its content provider, allowing you to do all of the
873 * same content provider operations as with the original Uri --
874 * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The
875 * only difference in behavior between the original and new Uris is that
876 * the content provider may need to do some additional work at each call
877 * using it to resolve it to the correct resource, especially if the
878 * canonical Uri has been moved to a different environment.
879 *
880 * <p>If you are moving a canonical Uri between environments, you should
881 * perform another call to {@link #canonicalize} with that original Uri to
882 * re-canonicalize it for the current environment. Alternatively, you may
883 * want to use {@link #uncanonicalize} to transform it to a non-canonical
884 * Uri that works only in the current environment but potentially more
885 * efficiently than the canonical representation.</p>
886 *
887 * @param url The {@link Uri} that is to be transformed to a canonical
888 * representation. Like all resolver calls, the input can be either
889 * a non-canonical or canonical Uri.
890 *
891 * @return Returns the official canonical representation of <var>url</var>,
892 * or null if the content provider does not support a canonical representation
893 * of the given Uri. Many providers may not support canonicalization of some
894 * or all of their Uris.
895 *
896 * @see #uncanonicalize
897 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700898 public final @Nullable Uri canonicalize(@NonNull Uri url) {
899 Preconditions.checkNotNull(url, "url");
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700900 IContentProvider provider = acquireProvider(url);
901 if (provider == null) {
902 return null;
903 }
904
905 try {
906 return provider.canonicalize(mPackageName, url);
907 } catch (RemoteException e) {
908 // Arbitrary and not worth documenting, as Activity
909 // Manager will kill this process shortly anyway.
910 return null;
911 } finally {
912 releaseProvider(provider);
913 }
914 }
915
916 /**
917 * Given a canonical Uri previously generated by {@link #canonicalize}, convert
918 * it to its local non-canonical form. This can be useful in some cases where
919 * you know that you will only be using the Uri in the current environment and
920 * want to avoid any possible overhead when using it with the content
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -0700921 * provider or want to verify that the referenced data exists at all in the
922 * new environment.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700923 *
924 * @param url The canonical {@link Uri} that is to be convered back to its
925 * non-canonical form.
926 *
Dianne Hackbornb3ac67a2013-09-11 11:02:24 -0700927 * @return Returns the non-canonical representation of <var>url</var>. This will
928 * return null if data identified by the canonical Uri can not be found in
929 * the current environment; callers must always check for null and deal with
930 * that by appropriately falling back to an alternative.
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700931 *
932 * @see #canonicalize
933 */
Jeff Sharkey673db442015-06-11 19:30:57 -0700934 public final @Nullable Uri uncanonicalize(@NonNull Uri url) {
935 Preconditions.checkNotNull(url, "url");
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700936 IContentProvider provider = acquireProvider(url);
937 if (provider == null) {
938 return null;
939 }
940
941 try {
942 return provider.uncanonicalize(mPackageName, url);
943 } catch (RemoteException e) {
944 // Arbitrary and not worth documenting, as Activity
945 // Manager will kill this process shortly anyway.
946 return null;
947 } finally {
948 releaseProvider(provider);
949 }
950 }
951
952 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800953 * This allows clients to request an explicit refresh of content identified by {@code uri}.
954 * <p>
955 * Client code should only invoke this method when there is a strong indication (such as a user
956 * initiated pull to refresh gesture) that the content is stale.
957 * <p>
Ben Lin1cf454f2016-11-10 13:50:54 -0800958 *
Ben Lin2b64a882016-11-11 15:24:58 -0800959 * @param url The Uri identifying the data to refresh.
Ben Lin1cf454f2016-11-10 13:50:54 -0800960 * @param args Additional options from the client. The definitions of these are specific to the
961 * content provider being called.
962 * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if
963 * none. For example, if you called refresh on a particular uri, you should call
964 * {@link CancellationSignal#throwIfCanceled()} to check whether the client has
965 * canceled the refresh request.
966 * @return true if the provider actually tried refreshing.
Ben Lin1cf454f2016-11-10 13:50:54 -0800967 */
968 public final boolean refresh(@NonNull Uri url, @Nullable Bundle args,
969 @Nullable CancellationSignal cancellationSignal) {
970 Preconditions.checkNotNull(url, "url");
971 IContentProvider provider = acquireProvider(url);
972 if (provider == null) {
973 return false;
974 }
975
976 try {
977 ICancellationSignal remoteCancellationSignal = null;
978 if (cancellationSignal != null) {
979 cancellationSignal.throwIfCanceled();
980 remoteCancellationSignal = provider.createCancellationSignal();
981 cancellationSignal.setRemote(remoteCancellationSignal);
982 }
983 return provider.refresh(mPackageName, url, args, remoteCancellationSignal);
984 } catch (RemoteException e) {
985 // Arbitrary and not worth documenting, as Activity
986 // Manager will kill this process shortly anyway.
987 return false;
988 } finally {
989 releaseProvider(provider);
990 }
991 }
992
993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 * Open a stream on to the content associated with a content URI. If there
995 * is no data associated with the URI, FileNotFoundException is thrown.
996 *
997 * <h5>Accepts the following URI schemes:</h5>
998 * <ul>
999 * <li>content ({@link #SCHEME_CONTENT})</li>
1000 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1001 * <li>file ({@link #SCHEME_FILE})</li>
1002 * </ul>
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1005 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 * @param uri The desired URI.
1008 * @return InputStream
1009 * @throws FileNotFoundException if the provided URI could not be opened.
1010 * @see #openAssetFileDescriptor(Uri, String)
1011 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001012 public final @Nullable InputStream openInputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 throws FileNotFoundException {
Jeff Sharkey673db442015-06-11 19:30:57 -07001014 Preconditions.checkNotNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 String scheme = uri.getScheme();
1016 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1017 // Note: left here to avoid breaking compatibility. May be removed
1018 // with sufficient testing.
1019 OpenResourceIdResult r = getResourceId(uri);
1020 try {
1021 InputStream stream = r.r.openRawResource(r.id);
1022 return stream;
1023 } catch (Resources.NotFoundException ex) {
1024 throw new FileNotFoundException("Resource does not exist: " + uri);
1025 }
1026 } else if (SCHEME_FILE.equals(scheme)) {
1027 // Note: left here to avoid breaking compatibility. May be removed
1028 // with sufficient testing.
1029 return new FileInputStream(uri.getPath());
1030 } else {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001031 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 try {
1033 return fd != null ? fd.createInputStream() : null;
1034 } catch (IOException e) {
1035 throw new FileNotFoundException("Unable to create stream");
1036 }
1037 }
1038 }
1039
1040 /**
1041 * Synonym for {@link #openOutputStream(Uri, String)
1042 * openOutputStream(uri, "w")}.
1043 * @throws FileNotFoundException if the provided URI could not be opened.
1044 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001045 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 throws FileNotFoundException {
1047 return openOutputStream(uri, "w");
1048 }
1049
1050 /**
1051 * Open a stream on to the content associated with a content URI. If there
1052 * is no data associated with the URI, FileNotFoundException is thrown.
1053 *
1054 * <h5>Accepts the following URI schemes:</h5>
1055 * <ul>
1056 * <li>content ({@link #SCHEME_CONTENT})</li>
1057 * <li>file ({@link #SCHEME_FILE})</li>
1058 * </ul>
1059 *
1060 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1061 * on these schemes.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001062 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 * @param uri The desired URI.
1064 * @param mode May be "w", "wa", "rw", or "rwt".
1065 * @return OutputStream
1066 * @throws FileNotFoundException if the provided URI could not be opened.
1067 * @see #openAssetFileDescriptor(Uri, String)
1068 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001069 public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001071 AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 try {
1073 return fd != null ? fd.createOutputStream() : null;
1074 } catch (IOException e) {
1075 throw new FileNotFoundException("Unable to create stream");
1076 }
1077 }
1078
1079 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001080 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1082 * underlying {@link ContentProvider#openFile}
1083 * ContentProvider.openFile()} method, so will <em>not</em> work with
1084 * providers that return sub-sections of files. If at all possible,
1085 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1086 * will receive a FileNotFoundException exception if the provider returns a
1087 * sub-section of a file.
1088 *
1089 * <h5>Accepts the following URI schemes:</h5>
1090 * <ul>
1091 * <li>content ({@link #SCHEME_CONTENT})</li>
1092 * <li>file ({@link #SCHEME_FILE})</li>
1093 * </ul>
1094 *
1095 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1096 * on these schemes.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001097 * <p>
1098 * If opening with the exclusive "r" or "w" modes, the returned
1099 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1100 * of data. Opening with the "rw" mode implies a file on disk that supports
1101 * seeking. If possible, always use an exclusive mode to give the underlying
1102 * {@link ContentProvider} the most flexibility.
1103 * <p>
1104 * If you are writing a file, and need to communicate an error to the
1105 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 * @param uri The desired URI to open.
1108 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1109 * ContentProvider.openFile}.
1110 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1111 * own this descriptor and are responsible for closing it when done.
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001112 * @throws FileNotFoundException Throws FileNotFoundException if no
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 * file exists under the URI or the mode is invalid.
1114 * @see #openAssetFileDescriptor(Uri, String)
1115 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001116 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1117 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001118 return openFileDescriptor(uri, mode, null);
1119 }
1120
1121 /**
1122 * Open a raw file descriptor to access data under a URI. This
1123 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
1124 * underlying {@link ContentProvider#openFile}
1125 * ContentProvider.openFile()} method, so will <em>not</em> work with
1126 * providers that return sub-sections of files. If at all possible,
1127 * you should use {@link #openAssetFileDescriptor(Uri, String)}. You
1128 * will receive a FileNotFoundException exception if the provider returns a
1129 * sub-section of a file.
1130 *
1131 * <h5>Accepts the following URI schemes:</h5>
1132 * <ul>
1133 * <li>content ({@link #SCHEME_CONTENT})</li>
1134 * <li>file ({@link #SCHEME_FILE})</li>
1135 * </ul>
1136 *
1137 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
1138 * on these schemes.
1139 * <p>
1140 * If opening with the exclusive "r" or "w" modes, the returned
1141 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
1142 * of data. Opening with the "rw" mode implies a file on disk that supports
1143 * seeking. If possible, always use an exclusive mode to give the underlying
1144 * {@link ContentProvider} the most flexibility.
1145 * <p>
1146 * If you are writing a file, and need to communicate an error to the
1147 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
1148 *
1149 * @param uri The desired URI to open.
1150 * @param mode The file mode to use, as per {@link ContentProvider#openFile
1151 * ContentProvider.openFile}.
Ying Wang94366312013-08-23 22:20:03 -07001152 * @param cancellationSignal A signal to cancel the operation in progress,
1153 * or null if none. If the operation is canceled, then
1154 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001155 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1156 * own this descriptor and are responsible for closing it when done.
1157 * @throws FileNotFoundException Throws FileNotFoundException if no
1158 * file exists under the URI or the mode is invalid.
1159 * @see #openAssetFileDescriptor(Uri, String)
1160 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001161 public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
1162 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1163 throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001164 AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (afd == null) {
1166 return null;
1167 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 if (afd.getDeclaredLength() < 0) {
1170 // This is a full file!
1171 return afd.getParcelFileDescriptor();
1172 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 // Client can't handle a sub-section of a file, so close what
1175 // we got and bail with an exception.
1176 try {
1177 afd.close();
1178 } catch (IOException e) {
1179 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 throw new FileNotFoundException("Not a whole file");
1182 }
1183
1184 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001185 * Open a raw file descriptor to access data under a URI. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 * interacts with the underlying {@link ContentProvider#openAssetFile}
Gilles Debunne03f02922010-06-09 14:11:45 -07001187 * 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 -08001188 *
1189 * <h5>Accepts the following URI schemes:</h5>
1190 * <ul>
1191 * <li>content ({@link #SCHEME_CONTENT})</li>
1192 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1193 * <li>file ({@link #SCHEME_FILE})</li>
1194 * </ul>
1195 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1196 * <p>
1197 * A Uri object can be used to reference a resource in an APK file. The
1198 * Uri should be one of the following formats:
1199 * <ul>
1200 * <li><code>android.resource://package_name/id_number</code><br/>
1201 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1202 * For example <code>com.example.myapp</code><br/>
1203 * <code>id_number</code> is the int form of the ID.<br/>
1204 * The easiest way to construct this form is
1205 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1206 * </li>
1207 * <li><code>android.resource://package_name/type/name</code><br/>
1208 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1209 * For example <code>com.example.myapp</code><br/>
1210 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1211 * or <code>drawable</code>.
1212 * <code>name</code> is the string form of the resource name. That is, whatever the file
1213 * name was in your res directory, without the type extension.
1214 * The easiest way to construct this form is
1215 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1216 * </li>
1217 * </ul>
1218 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001219 * <p>Note that if this function is called for read-only input (mode is "r")
1220 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001221 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001222 * from any built-in data conversion that a provider implements.
1223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 * @param uri The desired URI to open.
1225 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1226 * ContentProvider.openAssetFile}.
1227 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1228 * own this descriptor and are responsible for closing it when done.
1229 * @throws FileNotFoundException Throws FileNotFoundException of no
1230 * file exists under the URI or the mode is invalid.
1231 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001232 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1233 @NonNull String mode) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001234 return openAssetFileDescriptor(uri, mode, null);
1235 }
1236
1237 /**
1238 * Open a raw file descriptor to access data under a URI. This
1239 * interacts with the underlying {@link ContentProvider#openAssetFile}
1240 * method of the provider associated with the given URI, to retrieve any file stored there.
1241 *
1242 * <h5>Accepts the following URI schemes:</h5>
1243 * <ul>
1244 * <li>content ({@link #SCHEME_CONTENT})</li>
1245 * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
1246 * <li>file ({@link #SCHEME_FILE})</li>
1247 * </ul>
1248 * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
1249 * <p>
1250 * A Uri object can be used to reference a resource in an APK file. The
1251 * Uri should be one of the following formats:
1252 * <ul>
1253 * <li><code>android.resource://package_name/id_number</code><br/>
1254 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1255 * For example <code>com.example.myapp</code><br/>
1256 * <code>id_number</code> is the int form of the ID.<br/>
1257 * The easiest way to construct this form is
1258 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
1259 * </li>
1260 * <li><code>android.resource://package_name/type/name</code><br/>
1261 * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
1262 * For example <code>com.example.myapp</code><br/>
1263 * <code>type</code> is the string form of the resource type. For example, <code>raw</code>
1264 * or <code>drawable</code>.
1265 * <code>name</code> is the string form of the resource name. That is, whatever the file
1266 * name was in your res directory, without the type extension.
1267 * The easiest way to construct this form is
1268 * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
1269 * </li>
1270 * </ul>
1271 *
1272 * <p>Note that if this function is called for read-only input (mode is "r")
1273 * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
John Spurlock33900182014-01-02 11:04:18 -05001274 * for you with a MIME type of "*&#47;*". This allows such callers to benefit
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001275 * from any built-in data conversion that a provider implements.
1276 *
1277 * @param uri The desired URI to open.
1278 * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
1279 * ContentProvider.openAssetFile}.
Ying Wang94366312013-08-23 22:20:03 -07001280 * @param cancellationSignal A signal to cancel the operation in progress, or null if
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001281 * none. If the operation is canceled, then
1282 * {@link OperationCanceledException} will be thrown.
1283 * @return Returns a new ParcelFileDescriptor pointing to the file. You
1284 * own this descriptor and are responsible for closing it when done.
1285 * @throws FileNotFoundException Throws FileNotFoundException of no
1286 * file exists under the URI or the mode is invalid.
1287 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001288 public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri,
1289 @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
1290 throws FileNotFoundException {
1291 Preconditions.checkNotNull(uri, "uri");
1292 Preconditions.checkNotNull(mode, "mode");
1293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 String scheme = uri.getScheme();
1295 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1296 if (!"r".equals(mode)) {
1297 throw new FileNotFoundException("Can't write resources: " + uri);
1298 }
1299 OpenResourceIdResult r = getResourceId(uri);
1300 try {
1301 return r.r.openRawResourceFd(r.id);
1302 } catch (Resources.NotFoundException ex) {
1303 throw new FileNotFoundException("Resource does not exist: " + uri);
1304 }
1305 } else if (SCHEME_FILE.equals(scheme)) {
1306 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
Adam Lesinskieb8c3f92013-09-20 14:08:25 -07001307 new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 return new AssetFileDescriptor(pfd, 0, -1);
1309 } else {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001310 if ("r".equals(mode)) {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001311 return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001312 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001313 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1314 if (unstableProvider == null) {
1315 throw new FileNotFoundException("No content provider: " + uri);
1316 }
1317 IContentProvider stableProvider = null;
1318 AssetFileDescriptor fd = null;
1319
1320 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001321 ICancellationSignal remoteCancellationSignal = null;
1322 if (cancellationSignal != null) {
1323 cancellationSignal.throwIfCanceled();
1324 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1325 cancellationSignal.setRemote(remoteCancellationSignal);
1326 }
1327
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001328 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001329 fd = unstableProvider.openAssetFile(
1330 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001331 if (fd == null) {
1332 // The provider will be released by the finally{} clause
1333 return null;
1334 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001335 } catch (DeadObjectException e) {
1336 // The remote process has died... but we only hold an unstable
1337 // reference though, so we might recover!!! Let's try!!!!
1338 // This is exciting!!1!!1!!!!1
1339 unstableProviderDied(unstableProvider);
1340 stableProvider = acquireProvider(uri);
1341 if (stableProvider == null) {
1342 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001343 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001344 fd = stableProvider.openAssetFile(
1345 mPackageName, uri, mode, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001346 if (fd == null) {
1347 // The provider will be released by the finally{} clause
1348 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001349 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001350 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001351
1352 if (stableProvider == null) {
1353 stableProvider = acquireProvider(uri);
1354 }
1355 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001356 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001357 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1358 fd.getParcelFileDescriptor(), stableProvider);
1359
1360 // Success! Don't release the provider when exiting, let
1361 // ParcelFileDescriptorInner do that when it is closed.
1362 stableProvider = null;
1363
1364 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1365 fd.getDeclaredLength());
1366
1367 } catch (RemoteException e) {
1368 // Whatever, whatever, we'll go away.
1369 throw new FileNotFoundException(
1370 "Failed opening content provider: " + uri);
1371 } catch (FileNotFoundException e) {
1372 throw e;
1373 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001374 if (cancellationSignal != null) {
1375 cancellationSignal.setRemote(null);
1376 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001377 if (stableProvider != null) {
1378 releaseProvider(stableProvider);
1379 }
1380 if (unstableProvider != null) {
1381 releaseUnstableProvider(unstableProvider);
1382 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001383 }
1384 }
1385 }
1386 }
1387
1388 /**
1389 * Open a raw file descriptor to access (potentially type transformed)
1390 * data from a "content:" URI. This interacts with the underlying
1391 * {@link ContentProvider#openTypedAssetFile} method of the provider
1392 * associated with the given URI, to retrieve retrieve any appropriate
1393 * data stream for the data stored there.
1394 *
1395 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1396 * with "content:" URIs, because content providers are the only facility
1397 * with an associated MIME type to ensure that the returned data stream
1398 * is of the desired type.
1399 *
1400 * <p>All text/* streams are encoded in UTF-8.
1401 *
1402 * @param uri The desired URI to open.
1403 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001404 * be a pattern such as *&#47;*, which will allow the content provider to
Dianne Hackborn23fdaf62010-08-06 12:16:55 -07001405 * select a type, though there is no way for you to determine what type
1406 * it is returning.
1407 * @param opts Additional provider-dependent options.
1408 * @return Returns a new ParcelFileDescriptor from which you can read the
1409 * data stream from the provider. Note that this may be a pipe, meaning
1410 * you can't seek in it. The only seek you should do is if the
1411 * AssetFileDescriptor contains an offset, to move to that offset before
1412 * reading. You own this descriptor and are responsible for closing it when done.
1413 * @throws FileNotFoundException Throws FileNotFoundException of no
1414 * data of the desired type exists under the URI.
1415 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001416 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1417 @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001418 return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
1419 }
1420
1421 /**
1422 * Open a raw file descriptor to access (potentially type transformed)
1423 * data from a "content:" URI. This interacts with the underlying
1424 * {@link ContentProvider#openTypedAssetFile} method of the provider
1425 * associated with the given URI, to retrieve retrieve any appropriate
1426 * data stream for the data stored there.
1427 *
1428 * <p>Unlike {@link #openAssetFileDescriptor}, this function only works
1429 * with "content:" URIs, because content providers are the only facility
1430 * with an associated MIME type to ensure that the returned data stream
1431 * is of the desired type.
1432 *
1433 * <p>All text/* streams are encoded in UTF-8.
1434 *
1435 * @param uri The desired URI to open.
1436 * @param mimeType The desired MIME type of the returned data. This can
John Spurlock33900182014-01-02 11:04:18 -05001437 * be a pattern such as *&#47;*, which will allow the content provider to
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001438 * select a type, though there is no way for you to determine what type
1439 * it is returning.
1440 * @param opts Additional provider-dependent options.
Ying Wang94366312013-08-23 22:20:03 -07001441 * @param cancellationSignal A signal to cancel the operation in progress,
1442 * or null if none. If the operation is canceled, then
1443 * {@link OperationCanceledException} will be thrown.
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001444 * @return Returns a new ParcelFileDescriptor from which you can read the
1445 * data stream from the provider. Note that this may be a pipe, meaning
1446 * you can't seek in it. The only seek you should do is if the
1447 * AssetFileDescriptor contains an offset, to move to that offset before
1448 * reading. You own this descriptor and are responsible for closing it when done.
1449 * @throws FileNotFoundException Throws FileNotFoundException of no
1450 * data of the desired type exists under the URI.
1451 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001452 public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
1453 @NonNull String mimeType, @Nullable Bundle opts,
1454 @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
1455 Preconditions.checkNotNull(uri, "uri");
1456 Preconditions.checkNotNull(mimeType, "mimeType");
1457
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001458 IContentProvider unstableProvider = acquireUnstableProvider(uri);
1459 if (unstableProvider == null) {
1460 throw new FileNotFoundException("No content provider: " + uri);
1461 }
1462 IContentProvider stableProvider = null;
1463 AssetFileDescriptor fd = null;
1464
1465 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001466 ICancellationSignal remoteCancellationSignal = null;
1467 if (cancellationSignal != null) {
1468 cancellationSignal.throwIfCanceled();
1469 remoteCancellationSignal = unstableProvider.createCancellationSignal();
1470 cancellationSignal.setRemote(remoteCancellationSignal);
1471 }
1472
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001473 try {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001474 fd = unstableProvider.openTypedAssetFile(
1475 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001476 if (fd == null) {
1477 // The provider will be released by the finally{} clause
1478 return null;
1479 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001480 } catch (DeadObjectException e) {
1481 // The remote process has died... but we only hold an unstable
1482 // reference though, so we might recover!!! Let's try!!!!
1483 // This is exciting!!1!!1!!!!1
1484 unstableProviderDied(unstableProvider);
1485 stableProvider = acquireProvider(uri);
1486 if (stableProvider == null) {
1487 throw new FileNotFoundException("No content provider: " + uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001488 }
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001489 fd = stableProvider.openTypedAssetFile(
1490 mPackageName, uri, mimeType, opts, remoteCancellationSignal);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001491 if (fd == null) {
1492 // The provider will be released by the finally{} clause
1493 return null;
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001496
1497 if (stableProvider == null) {
1498 stableProvider = acquireProvider(uri);
1499 }
1500 releaseUnstableProvider(unstableProvider);
Koji Fukuif783c022014-02-07 15:01:36 +09001501 unstableProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001502 ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(
1503 fd.getParcelFileDescriptor(), stableProvider);
1504
1505 // Success! Don't release the provider when exiting, let
1506 // ParcelFileDescriptorInner do that when it is closed.
1507 stableProvider = null;
1508
1509 return new AssetFileDescriptor(pfd, fd.getStartOffset(),
1510 fd.getDeclaredLength());
1511
1512 } catch (RemoteException e) {
1513 // Whatever, whatever, we'll go away.
1514 throw new FileNotFoundException(
1515 "Failed opening content provider: " + uri);
1516 } catch (FileNotFoundException e) {
1517 throw e;
1518 } finally {
Jeff Sharkeybd3b9022013-08-20 15:20:04 -07001519 if (cancellationSignal != null) {
1520 cancellationSignal.setRemote(null);
1521 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001522 if (stableProvider != null) {
1523 releaseProvider(stableProvider);
1524 }
1525 if (unstableProvider != null) {
1526 releaseUnstableProvider(unstableProvider);
1527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529 }
1530
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001531 /**
1532 * A resource identified by the {@link Resources} that contains it, and a resource id.
1533 *
1534 * @hide
1535 */
1536 public class OpenResourceIdResult {
Mathew Inwood1c77a112018-08-14 14:06:26 +01001537 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001538 public Resources r;
Mathew Inwood1c77a112018-08-14 14:06:26 +01001539 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001540 public int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001542
1543 /**
1544 * Resolves an android.resource URI to a {@link Resources} and a resource id.
1545 *
1546 * @hide
1547 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001548 @UnsupportedAppUsage
Bjorn Bringert4c87a3f2009-09-16 15:59:37 +01001549 public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 String authority = uri.getAuthority();
1551 Resources r;
1552 if (TextUtils.isEmpty(authority)) {
1553 throw new FileNotFoundException("No authority: " + uri);
1554 } else {
1555 try {
1556 r = mContext.getPackageManager().getResourcesForApplication(authority);
1557 } catch (NameNotFoundException ex) {
1558 throw new FileNotFoundException("No package found for authority: " + uri);
1559 }
1560 }
1561 List<String> path = uri.getPathSegments();
1562 if (path == null) {
1563 throw new FileNotFoundException("No path: " + uri);
1564 }
1565 int len = path.size();
1566 int id;
1567 if (len == 1) {
1568 try {
1569 id = Integer.parseInt(path.get(0));
1570 } catch (NumberFormatException e) {
1571 throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
1572 }
1573 } else if (len == 2) {
1574 id = r.getIdentifier(path.get(1), path.get(0), authority);
1575 } else {
1576 throw new FileNotFoundException("More than two path segments: " + uri);
1577 }
1578 if (id == 0) {
1579 throw new FileNotFoundException("No resource found for: " + uri);
1580 }
1581 OpenResourceIdResult res = new OpenResourceIdResult();
1582 res.r = r;
1583 res.id = id;
1584 return res;
1585 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08001586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 /**
1588 * Inserts a row into a table at the given URL.
1589 *
1590 * If the content provider supports transactions the insertion will be atomic.
1591 *
1592 * @param url The URL of the table to insert into.
1593 * @param values The initial values for the newly inserted row. The key is the column name for
1594 * the field. Passing an empty ContentValues will create an empty row.
1595 * @return the URL of the newly created row.
1596 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001597 public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url,
1598 @Nullable ContentValues values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001599 Preconditions.checkNotNull(url, "url");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 IContentProvider provider = acquireProvider(url);
1601 if (provider == null) {
1602 throw new IllegalArgumentException("Unknown URL " + url);
1603 }
1604 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001605 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001606 Uri createdRow = provider.insert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001607 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001608 maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
1609 return createdRow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001611 // Arbitrary and not worth documenting, as Activity
1612 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 return null;
1614 } finally {
1615 releaseProvider(provider);
1616 }
1617 }
1618
Fred Quintana89437372009-05-15 15:10:40 -07001619 /**
Fred Quintana89437372009-05-15 15:10:40 -07001620 * Applies each of the {@link ContentProviderOperation} objects and returns an array
1621 * of their results. Passes through OperationApplicationException, which may be thrown
1622 * by the call to {@link ContentProviderOperation#apply}.
1623 * If all the applications succeed then a {@link ContentProviderResult} array with the
1624 * same number of elements as the operations will be returned. It is implementation-specific
1625 * how many, if any, operations will have been successfully applied if a call to
1626 * apply results in a {@link OperationApplicationException}.
1627 * @param authority the authority of the ContentProvider to which this batch should be applied
1628 * @param operations the operations to apply
1629 * @return the results of the applications
1630 * @throws OperationApplicationException thrown if an application fails.
1631 * See {@link ContentProviderOperation#apply} for more information.
1632 * @throws RemoteException thrown if a RemoteException is encountered while attempting
1633 * to communicate with a remote provider.
1634 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001635 public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
1636 @NonNull ArrayList<ContentProviderOperation> operations)
1637 throws RemoteException, OperationApplicationException {
1638 Preconditions.checkNotNull(authority, "authority");
1639 Preconditions.checkNotNull(operations, "operations");
Fred Quintana89437372009-05-15 15:10:40 -07001640 ContentProviderClient provider = acquireContentProviderClient(authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001641 if (provider == null) {
Fred Quintana89437372009-05-15 15:10:40 -07001642 throw new IllegalArgumentException("Unknown authority " + authority);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001643 }
1644 try {
Fred Quintana89437372009-05-15 15:10:40 -07001645 return provider.applyBatch(operations);
Fred Quintana6a8d5332009-05-07 17:35:38 -07001646 } finally {
1647 provider.release();
1648 }
1649 }
1650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 /**
1652 * Inserts multiple rows into a table at the given URL.
1653 *
1654 * This function make no guarantees about the atomicity of the insertions.
1655 *
1656 * @param url The URL of the table to insert into.
1657 * @param values The initial values for the newly inserted rows. The key is the column name for
1658 * the field. Passing null will create an empty row.
1659 * @return the number of newly created rows.
1660 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001661 public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url,
1662 @NonNull ContentValues[] values) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001663 Preconditions.checkNotNull(url, "url");
1664 Preconditions.checkNotNull(values, "values");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 IContentProvider provider = acquireProvider(url);
1666 if (provider == null) {
1667 throw new IllegalArgumentException("Unknown URL " + url);
1668 }
1669 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001670 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001671 int rowsCreated = provider.bulkInsert(mPackageName, url, values);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001672 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001673 maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */);
1674 return rowsCreated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001676 // Arbitrary and not worth documenting, as Activity
1677 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 return 0;
1679 } finally {
1680 releaseProvider(provider);
1681 }
1682 }
1683
1684 /**
1685 * Deletes row(s) specified by a content URI.
1686 *
1687 * If the content provider supports transactions, the deletion will be atomic.
1688 *
1689 * @param url The URL of the row to delete.
1690 * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause
1691 (excluding the WHERE itself).
1692 * @return The number of rows deleted.
1693 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001694 public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
Jeff Sharkey673db442015-06-11 19:30:57 -07001695 @Nullable String[] selectionArgs) {
1696 Preconditions.checkNotNull(url, "url");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 IContentProvider provider = acquireProvider(url);
1698 if (provider == null) {
1699 throw new IllegalArgumentException("Unknown URL " + url);
1700 }
1701 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001702 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001703 int rowsDeleted = provider.delete(mPackageName, url, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001704 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001705 maybeLogUpdateToEventLog(durationMillis, url, "delete", where);
1706 return rowsDeleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001708 // Arbitrary and not worth documenting, as Activity
1709 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 return -1;
1711 } finally {
1712 releaseProvider(provider);
1713 }
1714 }
1715
1716 /**
1717 * Update row(s) in a content URI.
1718 *
1719 * If the content provider supports transactions the update will be atomic.
1720 *
1721 * @param uri The URI to modify.
1722 * @param values The new field values. The key is the column name for the field.
1723 A null value will remove an existing field value.
Omari Stephensd2a2daa2010-03-10 18:53:54 -08001724 * @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 -08001725 (excluding the WHERE itself).
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001726 * @return the number of rows updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 * @throws NullPointerException if uri or values are null
1728 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07001729 public final int update(@RequiresPermission.Write @NonNull Uri uri,
1730 @Nullable ContentValues values, @Nullable String where,
1731 @Nullable String[] selectionArgs) {
Jeff Sharkey673db442015-06-11 19:30:57 -07001732 Preconditions.checkNotNull(uri, "uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 IContentProvider provider = acquireProvider(uri);
1734 if (provider == null) {
1735 throw new IllegalArgumentException("Unknown URI " + uri);
1736 }
1737 try {
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001738 long startTime = SystemClock.uptimeMillis();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001739 int rowsUpdated = provider.update(mPackageName, uri, values, where, selectionArgs);
Brad Fitzpatrickd72f7182010-02-11 17:07:51 -08001740 long durationMillis = SystemClock.uptimeMillis() - startTime;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08001741 maybeLogUpdateToEventLog(durationMillis, uri, "update", where);
1742 return rowsUpdated;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 } catch (RemoteException e) {
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001744 // Arbitrary and not worth documenting, as Activity
1745 // Manager will kill this process shortly anyway.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 return -1;
1747 } finally {
1748 releaseProvider(provider);
1749 }
1750 }
1751
1752 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +09001753 * Call a provider-defined method. This can be used to implement
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001754 * read or write interfaces which are cheaper than using a Cursor and/or
1755 * do not fit into the traditional table model.
1756 *
1757 * @param method provider-defined method name to call. Opaque to
1758 * framework, but must be non-null.
1759 * @param arg provider-defined String argument. May be null.
1760 * @param extras provider-defined Bundle argument. May be null.
1761 * @return a result Bundle, possibly null. Will be null if the ContentProvider
1762 * does not implement call.
1763 * @throws NullPointerException if uri or method is null
1764 * @throws IllegalArgumentException if uri is not known
1765 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001766 public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method,
1767 @Nullable String arg, @Nullable Bundle extras) {
1768 Preconditions.checkNotNull(uri, "uri");
1769 Preconditions.checkNotNull(method, "method");
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001770 IContentProvider provider = acquireProvider(uri);
1771 if (provider == null) {
1772 throw new IllegalArgumentException("Unknown URI " + uri);
1773 }
1774 try {
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001775 final Bundle res = provider.call(mPackageName, method, arg, extras);
1776 Bundle.setDefusable(res, true);
1777 return res;
Brad Fitzpatrick534c84c2011-01-12 14:06:30 -08001778 } catch (RemoteException e) {
1779 // Arbitrary and not worth documenting, as Activity
1780 // Manager will kill this process shortly anyway.
1781 return null;
1782 } finally {
1783 releaseProvider(provider);
1784 }
1785 }
1786
1787 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001788 * Returns the content provider for the given content URI.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 *
1790 * @param uri The URI to a content provider
1791 * @return The ContentProvider for the given URI, or null if no content provider is found.
1792 * @hide
1793 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001794 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001795 public final IContentProvider acquireProvider(Uri uri) {
Jason Monkd18651f2017-10-05 14:18:49 -04001796 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 return null;
1798 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001799 final String auth = uri.getAuthority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001801 return acquireProvider(mContext, auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803 return null;
1804 }
1805
1806 /**
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001807 * Returns the content provider for the given content URI if the process
1808 * already has a reference on it.
1809 *
1810 * @param uri The URI to a content provider
1811 * @return The ContentProvider for the given URI, or null if no content provider is found.
1812 * @hide
1813 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001814 @UnsupportedAppUsage
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001815 public final IContentProvider acquireExistingProvider(Uri uri) {
1816 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
1817 return null;
1818 }
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001819 final String auth = uri.getAuthority();
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001820 if (auth != null) {
Yury Zhauniarovichf9c51762012-05-12 18:39:25 +02001821 return acquireExistingProvider(mContext, auth);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001822 }
1823 return null;
1824 }
1825
1826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * @hide
1828 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001829 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 public final IContentProvider acquireProvider(String name) {
Brad Fitzpatrick1877d012010-03-04 17:48:13 -08001831 if (name == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 return null;
1833 }
1834 return acquireProvider(mContext, name);
1835 }
1836
1837 /**
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001838 * Returns the content provider for the given content URI.
1839 *
1840 * @param uri The URI to a content provider
1841 * @return The ContentProvider for the given URI, or null if no content provider is found.
1842 * @hide
1843 */
1844 public final IContentProvider acquireUnstableProvider(Uri uri) {
1845 if (!SCHEME_CONTENT.equals(uri.getScheme())) {
1846 return null;
1847 }
1848 String auth = uri.getAuthority();
1849 if (auth != null) {
1850 return acquireUnstableProvider(mContext, uri.getAuthority());
1851 }
1852 return null;
1853 }
1854
1855 /**
1856 * @hide
1857 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001858 @UnsupportedAppUsage
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001859 public final IContentProvider acquireUnstableProvider(String name) {
1860 if (name == null) {
1861 return null;
1862 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001863 return acquireUnstableProvider(mContext, name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001864 }
1865
1866 /**
Fred Quintana718d8a22009-04-29 17:53:20 -07001867 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1868 * that services the content at uri, starting the provider if necessary. Returns
1869 * null if there is no provider associated wih the uri. The caller must indicate that they are
1870 * done with the provider by calling {@link ContentProviderClient#release} which will allow
1871 * the system to release the provider it it determines that there is no other reason for
1872 * keeping it active.
1873 * @param uri specifies which provider should be acquired
1874 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1875 * that services the content at uri or null if there isn't one.
1876 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001877 public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) {
1878 Preconditions.checkNotNull(uri, "uri");
Fred Quintana718d8a22009-04-29 17:53:20 -07001879 IContentProvider provider = acquireProvider(uri);
1880 if (provider != null) {
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001881 return new ContentProviderClient(this, provider, true);
Fred Quintana718d8a22009-04-29 17:53:20 -07001882 }
Fred Quintana718d8a22009-04-29 17:53:20 -07001883 return null;
1884 }
1885
1886 /**
1887 * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1888 * with the authority of name, starting the provider if necessary. Returns
1889 * null if there is no provider associated wih the uri. The caller must indicate that they are
1890 * done with the provider by calling {@link ContentProviderClient#release} which will allow
1891 * the system to release the provider it it determines that there is no other reason for
1892 * keeping it active.
1893 * @param name specifies which provider should be acquired
1894 * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider}
1895 * with the authority of name or null if there isn't one.
1896 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001897 public final @Nullable ContentProviderClient acquireContentProviderClient(
1898 @NonNull String name) {
1899 Preconditions.checkNotNull(name, "name");
Fred Quintana718d8a22009-04-29 17:53:20 -07001900 IContentProvider provider = acquireProvider(name);
1901 if (provider != null) {
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001902 return new ContentProviderClient(this, provider, true);
1903 }
1904
1905 return null;
1906 }
1907
1908 /**
1909 * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do
1910 * not trust the stability of the target content provider. This turns off
1911 * the mechanism in the platform clean up processes that are dependent on
1912 * a content provider if that content provider's process goes away. Normally
1913 * you can safely assume that once you have acquired a provider, you can freely
1914 * use it as needed and it won't disappear, even if your process is in the
1915 * background. If using this method, you need to take care to deal with any
1916 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001917 * so that it can be re-opened later. In particular, catching a
1918 * {@link android.os.DeadObjectException} from the calls there will let you
1919 * know that the content provider has gone away; at that point the current
1920 * ContentProviderClient object is invalid, and you should release it. You
1921 * can acquire a new one if you would like to try to restart the provider
1922 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001923 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001924 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
1925 @NonNull Uri uri) {
1926 Preconditions.checkNotNull(uri, "uri");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001927 IContentProvider provider = acquireUnstableProvider(uri);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001928 if (provider != null) {
1929 return new ContentProviderClient(this, provider, false);
1930 }
1931
1932 return null;
1933 }
1934
1935 /**
1936 * Like {@link #acquireContentProviderClient(String)}, but for use when you do
1937 * not trust the stability of the target content provider. This turns off
1938 * the mechanism in the platform clean up processes that are dependent on
1939 * a content provider if that content provider's process goes away. Normally
1940 * you can safely assume that once you have acquired a provider, you can freely
1941 * use it as needed and it won't disappear, even if your process is in the
1942 * background. If using this method, you need to take care to deal with any
1943 * failures when communicating with the provider, and be sure to close it
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001944 * so that it can be re-opened later. In particular, catching a
1945 * {@link android.os.DeadObjectException} from the calls there will let you
1946 * know that the content provider has gone away; at that point the current
1947 * ContentProviderClient object is invalid, and you should release it. You
1948 * can acquire a new one if you would like to try to restart the provider
1949 * and perform new operations on it.
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001950 */
Jeff Sharkey673db442015-06-11 19:30:57 -07001951 public final @Nullable ContentProviderClient acquireUnstableContentProviderClient(
1952 @NonNull String name) {
1953 Preconditions.checkNotNull(name, "name");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001954 IContentProvider provider = acquireUnstableProvider(name);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001955 if (provider != null) {
1956 return new ContentProviderClient(this, provider, false);
Fred Quintana718d8a22009-04-29 17:53:20 -07001957 }
1958
1959 return null;
1960 }
1961
1962 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 * Register an observer class that gets callbacks when data identified by a
1964 * given content URI changes.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07001965 * <p>
1966 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
1967 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 *
Jeff Sharkey912e80d2017-02-24 11:00:55 -07001969 * @param uri The URI to watch for changes. This can be a specific row URI,
1970 * or a base URI for a whole class of content.
1971 * @param notifyForDescendants When false, the observer will be notified
1972 * whenever a change occurs to the exact URI specified by
1973 * <code>uri</code> or to one of the URI's ancestors in the path
1974 * hierarchy. When true, the observer will also be notified
1975 * whenever a change occurs to the URI's descendants in the path
1976 * hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 * @param observer The object that receives callbacks when changes occur.
1978 * @see #unregisterContentObserver
1979 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001980 public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
Jeff Sharkey673db442015-06-11 19:30:57 -07001981 @NonNull ContentObserver observer) {
1982 Preconditions.checkNotNull(uri, "uri");
1983 Preconditions.checkNotNull(observer, "observer");
Benjamin Franzadea1912015-06-19 16:03:38 +01001984 registerContentObserver(
1985 ContentProvider.getUriWithoutUserId(uri),
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001986 notifyForDescendants,
Benjamin Franzadea1912015-06-19 16:03:38 +01001987 observer,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08001988 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07001989 }
1990
1991 /** @hide - designated user version */
Mathew Inwood1c77a112018-08-14 14:06:26 +01001992 @UnsupportedAppUsage
Christopher Tate16aa9732012-09-17 16:23:44 -07001993 public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001994 ContentObserver observer, @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001996 getContentService().registerContentObserver(uri, notifyForDescendents,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07001997 observer.getContentObserver(), userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06001999 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001 }
2002
2003 /**
2004 * Unregisters a change observer.
2005 *
2006 * @param observer The previously registered observer that is no longer needed.
2007 * @see #registerContentObserver
2008 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002009 public final void unregisterContentObserver(@NonNull ContentObserver observer) {
2010 Preconditions.checkNotNull(observer, "observer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 try {
2012 IContentObserver contentObserver = observer.releaseContentObserver();
2013 if (contentObserver != null) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002014 getContentService().unregisterContentObserver(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 contentObserver);
2016 }
2017 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002018 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020 }
2021
2022 /**
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002023 * Notify registered observers that a row was updated and attempt to sync
2024 * changes to the network.
2025 * <p>
2026 * To observe events sent through this call, use
2027 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2028 * <p>
2029 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2030 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 *
Jeff Brown86de0592012-01-23 13:01:18 -08002032 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002033 * @param observer The observer that originated the change, may be
2034 * <code>null</null>. The observer that originated the change
2035 * will only receive the notification if it has requested to
2036 * receive self-change notifications by implementing
2037 * {@link ContentObserver#deliverSelfNotifications()} to return
2038 * true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002040 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 notifyChange(uri, observer, true /* sync to network */);
2042 }
2043
2044 /**
2045 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002046 * <p>
2047 * To observe events sent through this call, use
2048 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2049 * <p>
2050 * If syncToNetwork is true, this will attempt to schedule a local sync
2051 * using the sync adapter that's registered for the authority of the
2052 * provided uri. No account will be passed to the sync adapter, so all
2053 * matching accounts will be synchronized.
2054 * <p>
2055 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2056 * notifications must be backed by a valid {@link ContentProvider}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 *
Jeff Brown86de0592012-01-23 13:01:18 -08002058 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002059 * @param observer The observer that originated the change, may be
2060 * <code>null</null>. The observer that originated the change
2061 * will only receive the notification if it has requested to
2062 * receive self-change notifications by implementing
2063 * {@link ContentObserver#deliverSelfNotifications()} to return
2064 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002065 * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}.
Steve Pomeroyd7a1aad2012-01-18 16:15:59 -05002066 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002068 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2069 boolean syncToNetwork) {
2070 Preconditions.checkNotNull(uri, "uri");
Benjamin Franzadea1912015-06-19 16:03:38 +01002071 notifyChange(
2072 ContentProvider.getUriWithoutUserId(uri),
2073 observer,
2074 syncToNetwork,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002075 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Christopher Tate16aa9732012-09-17 16:23:44 -07002076 }
2077
2078 /**
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002079 * Notify registered observers that a row was updated.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002080 * <p>
2081 * To observe events sent through this call, use
2082 * {@link #registerContentObserver(Uri, boolean, ContentObserver)}.
2083 * <p>
2084 * If syncToNetwork is true, this will attempt to schedule a local sync
2085 * using the sync adapter that's registered for the authority of the
2086 * provided uri. No account will be passed to the sync adapter, so all
2087 * matching accounts will be synchronized.
2088 * <p>
2089 * Starting in {@link android.os.Build.VERSION_CODES#O}, all content
2090 * notifications must be backed by a valid {@link ContentProvider}.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002091 *
2092 * @param uri The uri of the content that was changed.
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002093 * @param observer The observer that originated the change, may be
2094 * <code>null</null>. The observer that originated the change
2095 * will only receive the notification if it has requested to
2096 * receive self-change notifications by implementing
2097 * {@link ContentObserver#deliverSelfNotifications()} to return
2098 * true.
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002099 * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}.
2100 * @see #requestSync(android.accounts.Account, String, android.os.Bundle)
2101 */
2102 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer,
2103 @NotifyFlags int flags) {
2104 Preconditions.checkNotNull(uri, "uri");
2105 notifyChange(
2106 ContentProvider.getUriWithoutUserId(uri),
2107 observer,
2108 flags,
Sudheer Shankab4e2ddd2017-02-03 15:15:57 -08002109 ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002110 }
2111
2112 /**
Christopher Tate16aa9732012-09-17 16:23:44 -07002113 * Notify registered observers within the designated user(s) that a row was updated.
2114 *
2115 * @hide
2116 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002117 public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002118 @UserIdInt int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 try {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002120 getContentService().notifyChange(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 uri, observer == null ? null : observer.getContentObserver(),
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002122 observer != null && observer.deliverSelfNotifications(),
2123 syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002124 userHandle, mTargetSdkVersion);
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002125 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002126 throw e.rethrowFromSystemServer();
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002127 }
2128 }
2129
2130 /**
2131 * Notify registered observers within the designated user(s) that a row was updated.
2132 *
2133 * @hide
2134 */
Dianne Hackborne7617772016-04-27 17:03:52 -07002135 public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags,
Dianne Hackborn141f11c2016-04-05 15:46:12 -07002136 @UserIdInt int userHandle) {
2137 try {
2138 getContentService().notifyChange(
2139 uri, observer == null ? null : observer.getContentObserver(),
2140 observer != null && observer.deliverSelfNotifications(), flags,
Jeff Sharkey912e80d2017-02-24 11:00:55 -07002141 userHandle, mTargetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002143 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145 }
2146
2147 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002148 * Take a persistable URI permission grant that has been offered. Once
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002149 * taken, the permission grant will be remembered across device reboots.
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002150 * Only URI permissions granted with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002151 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If
2152 * the grant has already been persisted, taking it again will touch
2153 * {@link UriPermission#getPersistedTime()}.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002154 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002155 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002156 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002157 public void takePersistableUriPermission(@NonNull Uri uri,
2158 @Intent.AccessUriMode int modeFlags) {
2159 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002160 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002161 ActivityManager.getService().takePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002162 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2163 resolveUserId(uri));
2164 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002165 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002166 }
2167 }
2168
2169 /**
2170 * @hide
2171 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01002172 @UnsupportedAppUsage
Felipe Leme988234a2018-02-14 12:00:29 -08002173 public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
2174 @Intent.AccessUriMode int modeFlags) {
2175 Preconditions.checkNotNull(toPackage, "toPackage");
2176 Preconditions.checkNotNull(uri, "uri");
2177 try {
2178 ActivityManager.getService().takePersistableUriPermission(
2179 ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
2180 resolveUserId(uri));
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002181 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002182 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002183 }
2184 }
2185
2186 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002187 * Relinquish a persisted URI permission grant. The URI must have been
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002188 * previously made persistent with
2189 * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent
2190 * grants to the calling package will remain intact.
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002191 *
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002192 * @see #getPersistedUriPermissions()
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002193 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002194 public void releasePersistableUriPermission(@NonNull Uri uri,
2195 @Intent.AccessUriMode int modeFlags) {
2196 Preconditions.checkNotNull(uri, "uri");
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002197 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002198 ActivityManager.getService().releasePersistableUriPermission(
Felipe Leme988234a2018-02-14 12:00:29 -08002199 ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
2200 resolveUserId(uri));
2201 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002202 throw e.rethrowFromSystemServer();
Felipe Leme988234a2018-02-14 12:00:29 -08002203 }
2204 }
2205
2206 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002207 * Return list of all URI permission grants that have been persisted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002208 * calling app. That is, the returned permissions have been granted
2209 * <em>to</em> the calling app. Only persistable grants taken with
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002210 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002211 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002212 *
2213 * @see #takePersistableUriPermission(Uri, int)
2214 * @see #releasePersistableUriPermission(Uri, int)
2215 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002216 public @NonNull List<UriPermission> getPersistedUriPermissions() {
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002217 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002218 return ActivityManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002219 .getPersistedUriPermissions(mPackageName, true).getList();
2220 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002221 throw e.rethrowFromSystemServer();
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002222 }
2223 }
2224
2225 /**
Jeff Sharkeyadef88a2013-10-15 13:54:44 -07002226 * Return list of all persisted URI permission grants that are hosted by the
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002227 * calling app. That is, the returned permissions have been granted
2228 * <em>from</em> the calling app. Only grants taken with
2229 * {@link #takePersistableUriPermission(Uri, int)} are returned.
Fyodor Kupolov9bbaacf2016-06-20 14:03:44 -07002230 * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked.
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002231 */
Jeff Sharkey673db442015-06-11 19:30:57 -07002232 public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() {
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002233 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002234 return ActivityManager.getService()
Jeff Sharkeybcaac0a2013-10-09 14:21:08 -07002235 .getPersistedUriPermissions(mPackageName, false).getList();
Jeff Sharkeye66c1772013-09-20 14:30:59 -07002236 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002237 throw e.rethrowFromSystemServer();
Jeff Sharkey08da7a12013-08-11 20:53:18 -07002238 }
2239 }
2240
2241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 * Start an asynchronous sync operation. If you want to monitor the progress
2243 * of the sync you may register a SyncObserver. Only values of the following
2244 * types may be used in the extras bundle:
2245 * <ul>
2246 * <li>Integer</li>
2247 * <li>Long</li>
2248 * <li>Boolean</li>
2249 * <li>Float</li>
2250 * <li>Double</li>
2251 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002252 * <li>Account</li>
2253 * <li>null</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 * </ul>
2255 *
2256 * @param uri the uri of the provider to sync or null to sync all providers.
2257 * @param extras any extras to pass to the SyncAdapter.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002258 * @deprecated instead use
2259 * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002261 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 public void startSync(Uri uri, Bundle extras) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002263 Account account = null;
2264 if (extras != null) {
2265 String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
2266 if (!TextUtils.isEmpty(accountName)) {
Svet Ganovf6d424f12016-09-20 20:18:53 -07002267 // TODO: No references to Google in AOSP
Costin Manolache3348f142009-09-29 18:58:36 -07002268 account = new Account(accountName, "com.google");
Fred Quintanaac9385e2009-06-22 18:00:59 -07002269 }
2270 extras.remove(SYNC_EXTRAS_ACCOUNT);
2271 }
2272 requestSync(account, uri != null ? uri.getAuthority() : null, extras);
2273 }
2274
2275 /**
2276 * Start an asynchronous sync operation. If you want to monitor the progress
2277 * of the sync you may register a SyncObserver. Only values of the following
2278 * types may be used in the extras bundle:
2279 * <ul>
2280 * <li>Integer</li>
2281 * <li>Long</li>
2282 * <li>Boolean</li>
2283 * <li>Float</li>
2284 * <li>Double</li>
2285 * <li>String</li>
Matthew Williamsfa774182013-06-18 15:44:11 -07002286 * <li>Account</li>
2287 * <li>null</li>
Fred Quintanaac9385e2009-06-22 18:00:59 -07002288 * </ul>
2289 *
2290 * @param account which account should be synced
2291 * @param authority which authority should be synced
2292 * @param extras any extras to pass to the SyncAdapter.
2293 */
2294 public static void requestSync(Account account, String authority, Bundle extras) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002295 requestSyncAsUser(account, authority, UserHandle.myUserId(), extras);
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002296 }
2297
2298 /**
2299 * @see #requestSync(Account, String, Bundle)
2300 * @hide
2301 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002302 public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId,
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002303 Bundle extras) {
Matthew Williamsd8abd6a2013-09-09 14:35:27 -07002304 if (extras == null) {
2305 throw new IllegalArgumentException("Must specify extras.");
2306 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002307 SyncRequest request =
2308 new SyncRequest.Builder()
2309 .setSyncAdapter(account, authority)
2310 .setExtras(extras)
Nick Kralevich69002ae2013-10-19 08:43:08 -07002311 .syncOnce() // Immediate sync.
Matthew Williamsfa774182013-06-18 15:44:11 -07002312 .build();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002313 try {
2314 getContentService().syncAsUser(request, userId);
2315 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002316 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002317 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002318 }
2319
2320 /**
2321 * Register a sync with the SyncManager. These requests are built using the
2322 * {@link SyncRequest.Builder}.
2323 */
2324 public static void requestSync(SyncRequest request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07002326 getContentService().sync(request);
2327 } catch(RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002328 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 }
2330 }
2331
2332 /**
2333 * Check that only values of the following types are in the Bundle:
2334 * <ul>
2335 * <li>Integer</li>
2336 * <li>Long</li>
2337 * <li>Boolean</li>
2338 * <li>Float</li>
2339 * <li>Double</li>
2340 * <li>String</li>
Fred Quintanad9d2f112009-04-23 13:36:27 -07002341 * <li>Account</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 * <li>null</li>
2343 * </ul>
2344 * @param extras the Bundle to check
2345 */
2346 public static void validateSyncExtrasBundle(Bundle extras) {
2347 try {
2348 for (String key : extras.keySet()) {
2349 Object value = extras.get(key);
2350 if (value == null) continue;
2351 if (value instanceof Long) continue;
2352 if (value instanceof Integer) continue;
2353 if (value instanceof Boolean) continue;
2354 if (value instanceof Float) continue;
2355 if (value instanceof Double) continue;
2356 if (value instanceof String) continue;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002357 if (value instanceof Account) continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 throw new IllegalArgumentException("unexpected value type: "
2359 + value.getClass().getName());
2360 }
2361 } catch (IllegalArgumentException e) {
2362 throw e;
2363 } catch (RuntimeException exc) {
2364 throw new IllegalArgumentException("error unparceling Bundle", exc);
2365 }
2366 }
2367
Fred Quintanaac9385e2009-06-22 18:00:59 -07002368 /**
2369 * Cancel any active or pending syncs that match the Uri. If the uri is null then
2370 * all syncs will be canceled.
2371 *
2372 * @param uri the uri of the provider to sync or null to sync all providers.
2373 * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)}
2374 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -07002375 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 public void cancelSync(Uri uri) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002377 cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null);
2378 }
2379
2380 /**
2381 * Cancel any active or pending syncs that match account and authority. The account and
2382 * authority can each independently be set to null, which means that syncs with any account
2383 * or authority, respectively, will match.
2384 *
2385 * @param account filters the syncs that match by this account
2386 * @param authority filters the syncs that match by this authority
2387 */
2388 public static void cancelSync(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002390 getContentService().cancelSync(account, authority, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002392 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394 }
2395
Fred Quintanaac9385e2009-06-22 18:00:59 -07002396 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002397 * @see #cancelSync(Account, String)
2398 * @hide
2399 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002400 public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002401 try {
2402 getContentService().cancelSyncAsUser(account, authority, null, userId);
2403 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002404 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002405 }
2406 }
2407
2408 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002409 * Get information about the SyncAdapters that are known to the system.
2410 * @return an array of SyncAdapters that have registered with the system
2411 */
2412 public static SyncAdapterType[] getSyncAdapterTypes() {
2413 try {
2414 return getContentService().getSyncAdapterTypes();
2415 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002416 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002417 }
2418 }
2419
2420 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002421 * @see #getSyncAdapterTypes()
2422 * @hide
2423 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002424 public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002425 try {
2426 return getContentService().getSyncAdapterTypesAsUser(userId);
2427 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002428 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002429 }
2430 }
2431
2432 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -07002433 * @hide
2434 * Returns the package names of syncadapters that match a given user and authority.
2435 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -07002436 @TestApi
Amith Yamasani37a40c22015-06-17 13:25:42 -07002437 public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002438 @UserIdInt int userId) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002439 try {
2440 return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId);
2441 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002442 throw e.rethrowFromSystemServer();
Amith Yamasani37a40c22015-06-17 13:25:42 -07002443 }
Amith Yamasani37a40c22015-06-17 13:25:42 -07002444 }
2445
2446 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002447 * Check if the provider should be synced when a network tickle is received
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002448 * <p>This method requires the caller to hold the permission
2449 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002450 *
2451 * @param account the account whose setting we are querying
2452 * @param authority the provider whose setting we are querying
2453 * @return true if the provider should be synced when a network tickle is received
2454 */
2455 public static boolean getSyncAutomatically(Account account, String authority) {
2456 try {
2457 return getContentService().getSyncAutomatically(account, authority);
2458 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002459 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002460 }
2461 }
2462
2463 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002464 * @see #getSyncAutomatically(Account, String)
2465 * @hide
2466 */
2467 public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002468 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002469 try {
2470 return getContentService().getSyncAutomaticallyAsUser(account, authority, userId);
2471 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002472 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002473 }
2474 }
2475
2476 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002477 * Set whether or not the provider is synced when it receives a network tickle.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002478 * <p>This method requires the caller to hold the permission
2479 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002480 *
2481 * @param account the account whose setting we are querying
2482 * @param authority the provider whose behavior is being controlled
2483 * @param sync true if the provider should be synced when tickles are received for it
2484 */
2485 public static void setSyncAutomatically(Account account, String authority, boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002486 setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002487 }
2488
2489 /**
2490 * @see #setSyncAutomatically(Account, String, boolean)
2491 * @hide
2492 */
2493 public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002494 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002495 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002496 getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002497 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002498 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002499 }
2500 }
2501
2502 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002503 * Specifies that a sync should be requested with the specified the account, authority,
2504 * and extras at the given frequency. If there is already another periodic sync scheduled
2505 * with the account, authority and extras then a new periodic sync won't be added, instead
2506 * the frequency of the previous one will be updated.
2507 * <p>
2508 * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings.
2509 * Although these sync are scheduled at the specified frequency, it may take longer for it to
2510 * actually be started if other syncs are ahead of it in the sync operation queue. This means
2511 * that the actual start time may drift.
Fred Quintana53bd2522010-02-05 15:28:12 -08002512 * <p>
2513 * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY},
2514 * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS},
2515 * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE},
2516 * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true.
2517 * If any are supplied then an {@link IllegalArgumentException} will be thrown.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002518 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002519 * <p>This method requires the caller to hold the permission
2520 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002521 * <p>The bundle for a periodic sync can be queried by applications with the correct
2522 * permissions using
2523 * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no
2524 * sensitive data should be transferred here.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002525 *
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002526 * @param account the account to specify in the sync
2527 * @param authority the provider to specify in the sync request
2528 * @param extras extra parameters to go along with the sync request
Liefu Liu5c0d7b32018-03-21 10:25:49 -07002529 * @param pollFrequency how frequently the sync should be performed, in seconds.
2530 * On Android API level 24 and above, a minmam interval of 15 minutes is enforced.
2531 * On previous versions, the minimum interval is 1 hour.
Fred Quintana53bd2522010-02-05 15:28:12 -08002532 * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters
2533 * are null.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002534 */
2535 public static void addPeriodicSync(Account account, String authority, Bundle extras,
2536 long pollFrequency) {
2537 validateSyncExtrasBundle(extras);
Makoto Onuki61283ec2018-01-31 17:22:36 -08002538 if (invalidPeriodicExtras(extras)) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002539 throw new IllegalArgumentException("illegal extras were set");
2540 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002541 try {
Matthew Williamsfa774182013-06-18 15:44:11 -07002542 getContentService().addPeriodicSync(account, authority, extras, pollFrequency);
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002543 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002544 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002545 }
2546 }
2547
2548 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002549 * {@hide}
2550 * Helper function to throw an <code>IllegalArgumentException</code> if any illegal
2551 * extras were set for a periodic sync.
2552 *
2553 * @param extras bundle to validate.
2554 */
2555 public static boolean invalidPeriodicExtras(Bundle extras) {
2556 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)
2557 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
2558 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)
2559 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)
2560 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)
2561 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false)
2562 || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
2563 return true;
2564 }
2565 return false;
2566 }
2567
2568 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002569 * Remove a periodic sync. Has no affect if account, authority and extras don't match
2570 * an existing periodic sync.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002571 * <p>This method requires the caller to hold the permission
2572 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002573 *
2574 * @param account the account of the periodic sync to remove
2575 * @param authority the provider of the periodic sync to remove
2576 * @param extras the extras of the periodic sync to remove
2577 */
2578 public static void removePeriodicSync(Account account, String authority, Bundle extras) {
2579 validateSyncExtrasBundle(extras);
2580 try {
2581 getContentService().removePeriodicSync(account, authority, extras);
2582 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002583 throw e.rethrowFromSystemServer();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002584 }
2585 }
2586
2587 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002588 * Remove the specified sync. This will cancel any pending or active syncs. If the request is
2589 * for a periodic sync, this call will remove any future occurrences.
Matthew Williams5a9decd2014-06-04 09:25:11 -07002590 * <p>
2591 * If a periodic sync is specified, the caller must hold the permission
2592 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
2593 *</p>
2594 * It is possible to cancel a sync using a SyncRequest object that is not the same object
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002595 * with which you requested the sync. Do so by building a SyncRequest with the same
Matthew Williams5a9decd2014-06-04 09:25:11 -07002596 * adapter, frequency, <b>and</b> extras bundle.
Matthew Williamsfa774182013-06-18 15:44:11 -07002597 *
2598 * @param request SyncRequest object containing information about sync to cancel.
2599 */
2600 public static void cancelSync(SyncRequest request) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002601 if (request == null) {
2602 throw new IllegalArgumentException("request cannot be null");
2603 }
2604 try {
2605 getContentService().cancelRequest(request);
2606 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002607 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002608 }
Matthew Williamsfa774182013-06-18 15:44:11 -07002609 }
2610
2611 /**
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002612 * Get the list of information about the periodic syncs for the given account and authority.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002613 * <p>This method requires the caller to hold the permission
2614 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002615 *
2616 * @param account the account whose periodic syncs we are querying
2617 * @param authority the provider whose periodic syncs we are querying
2618 * @return a list of PeriodicSync objects. This list may be empty but will never be null.
2619 */
2620 public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
2621 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002622 return getContentService().getPeriodicSyncs(account, authority, null);
2623 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002624 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002625 }
2626 }
2627
2628 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002629 * Check if this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002630 * <p>This method requires the caller to hold the permission
2631 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintana5e787c42009-08-16 23:13:53 -07002632 * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet.
2633 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002634 public static int getIsSyncable(Account account, String authority) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002635 try {
2636 return getContentService().getIsSyncable(account, authority);
2637 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002638 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002639 }
2640 }
2641
2642 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002643 * @see #getIsSyncable(Account, String)
2644 * @hide
2645 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002646 public static int getIsSyncableAsUser(Account account, String authority,
2647 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002648 try {
2649 return getContentService().getIsSyncableAsUser(account, authority, userId);
2650 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002651 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002652 }
2653 }
2654
2655 /**
Fred Quintana5e787c42009-08-16 23:13:53 -07002656 * Set whether this account/provider is syncable.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002657 * <p>This method requires the caller to hold the permission
2658 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintana718671b2009-08-17 14:08:37 -07002659 * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown
Fred Quintana5e787c42009-08-16 23:13:53 -07002660 */
Jim Miller20ea6ce2009-08-17 15:47:14 -07002661 public static void setIsSyncable(Account account, String authority, int syncable) {
Fred Quintana5e787c42009-08-16 23:13:53 -07002662 try {
2663 getContentService().setIsSyncable(account, authority, syncable);
2664 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002665 throw e.rethrowFromSystemServer();
Fred Quintana5e787c42009-08-16 23:13:53 -07002666 }
2667 }
2668
2669 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002670 * Gets the master auto-sync setting that applies to all the providers and accounts.
2671 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002672 * <p>This method requires the caller to hold the permission
2673 * {@link android.Manifest.permission#READ_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002674 *
2675 * @return the master auto-sync setting that applies to all the providers and accounts
2676 */
2677 public static boolean getMasterSyncAutomatically() {
2678 try {
2679 return getContentService().getMasterSyncAutomatically();
2680 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002681 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002682 }
2683 }
2684
2685 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002686 * @see #getMasterSyncAutomatically()
2687 * @hide
2688 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002689 public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002690 try {
2691 return getContentService().getMasterSyncAutomaticallyAsUser(userId);
2692 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002693 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002694 }
2695 }
2696
2697 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002698 * Sets the master auto-sync setting that applies to all the providers and accounts.
2699 * If this is false then the per-provider auto-sync setting is ignored.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002700 * <p>This method requires the caller to hold the permission
2701 * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002702 *
2703 * @param sync the master auto-sync setting that applies to all the providers and accounts
2704 */
2705 public static void setMasterSyncAutomatically(boolean sync) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002706 setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002707 }
2708
2709 /**
2710 * @see #setMasterSyncAutomatically(boolean)
2711 * @hide
2712 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002713 public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002714 try {
Alexandra Gherghina0e9ac202014-07-15 23:11:48 +01002715 getContentService().setMasterSyncAutomaticallyAsUser(sync, userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002716 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002717 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002718 }
2719 }
2720
2721 /**
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002722 * Returns true if there is currently a sync operation for the given account or authority
2723 * actively being processed.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002724 * <p>This method requires the caller to hold the permission
2725 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002726 * @param account the account whose setting we are querying
2727 * @param authority the provider whose behavior is being queried
2728 * @return true if a sync is active for the given account or authority.
2729 */
2730 public static boolean isSyncActive(Account account, String authority) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002731 if (account == null) {
2732 throw new IllegalArgumentException("account must not be null");
2733 }
2734 if (authority == null) {
2735 throw new IllegalArgumentException("authority must not be null");
2736 }
2737
Fred Quintanaac9385e2009-06-22 18:00:59 -07002738 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002739 return getContentService().isSyncActive(account, authority, null);
2740 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002741 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002742 }
2743 }
2744
Fred Quintanaac9385e2009-06-22 18:00:59 -07002745 /**
Fred Quintanac6a69552010-09-27 17:05:04 -07002746 * If a sync is active returns the information about it, otherwise returns null.
2747 * <p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002748 * This method requires the caller to hold the permission
2749 * {@link android.Manifest.permission#READ_SYNC_STATS}.
2750 * <p>
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07002751 * @return the SyncInfo for the currently active sync or null if one is not active.
Fred Quintanac6a69552010-09-27 17:05:04 -07002752 * @deprecated
2753 * Since multiple concurrent syncs are now supported you should use
2754 * {@link #getCurrentSyncs()} to get the accurate list of current syncs.
2755 * This method returns the first item from the list of current syncs
2756 * or null if there are none.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002757 */
Fred Quintanac6a69552010-09-27 17:05:04 -07002758 @Deprecated
Fred Quintanad5e4fdc2010-03-30 15:16:21 -07002759 public static SyncInfo getCurrentSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002760 try {
Fred Quintanac6a69552010-09-27 17:05:04 -07002761 final List<SyncInfo> syncs = getContentService().getCurrentSyncs();
2762 if (syncs.isEmpty()) {
2763 return null;
2764 }
2765 return syncs.get(0);
2766 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002767 throw e.rethrowFromSystemServer();
Fred Quintanac6a69552010-09-27 17:05:04 -07002768 }
2769 }
2770
2771 /**
2772 * Returns a list with information about all the active syncs. This list will be empty
2773 * if there are no active syncs.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002774 * <p>
2775 * This method requires the caller to hold the permission
2776 * {@link android.Manifest.permission#READ_SYNC_STATS}.
2777 * <p>
Fred Quintanac6a69552010-09-27 17:05:04 -07002778 * @return a List of SyncInfo objects for the currently active syncs.
2779 */
2780 public static List<SyncInfo> getCurrentSyncs() {
2781 try {
2782 return getContentService().getCurrentSyncs();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002783 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002784 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002785 }
2786 }
2787
2788 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002789 * @see #getCurrentSyncs()
2790 * @hide
2791 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002792 public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002793 try {
2794 return getContentService().getCurrentSyncsAsUser(userId);
2795 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002796 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002797 }
2798 }
2799
2800 /**
Fred Quintana4a6679b2009-08-17 13:05:39 -07002801 * Returns the status that matches the authority.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002802 * @param account the account whose setting we are querying
2803 * @param authority the provider whose behavior is being queried
2804 * @return the SyncStatusInfo for the authority, or null if none exists
2805 * @hide
2806 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01002807 @UnsupportedAppUsage
Fred Quintanaac9385e2009-06-22 18:00:59 -07002808 public static SyncStatusInfo getSyncStatus(Account account, String authority) {
2809 try {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002810 return getContentService().getSyncStatus(account, authority, null);
Fred Quintanaac9385e2009-06-22 18:00:59 -07002811 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002812 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002813 }
2814 }
2815
2816 /**
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002817 * @see #getSyncStatus(Account, String)
2818 * @hide
2819 */
Mathew Inwood1c77a112018-08-14 14:06:26 +01002820 @UnsupportedAppUsage
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002821 public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority,
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002822 @UserIdInt int userId) {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002823 try {
2824 return getContentService().getSyncStatusAsUser(account, authority, null, userId);
2825 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002826 throw e.rethrowFromSystemServer();
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01002827 }
2828 }
2829
2830 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07002831 * Return true if the pending status is true of any matching authorities.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07002832 * <p>This method requires the caller to hold the permission
2833 * {@link android.Manifest.permission#READ_SYNC_STATS}.
Fred Quintanaac9385e2009-06-22 18:00:59 -07002834 * @param account the account whose setting we are querying
2835 * @param authority the provider whose behavior is being queried
2836 * @return true if there is a pending sync with the matching account and authority
2837 */
2838 public static boolean isSyncPending(Account account, String authority) {
Alexandra Gherghina03e1e832014-09-10 16:06:10 +01002839 return isSyncPendingAsUser(account, authority, UserHandle.myUserId());
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002840 }
2841
2842 /**
2843 * @see #requestSync(Account, String, Bundle)
2844 * @hide
2845 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002846 public static boolean isSyncPendingAsUser(Account account, String authority,
2847 @UserIdInt int userId) {
Fred Quintanaac9385e2009-06-22 18:00:59 -07002848 try {
Alexandra Gherghinacb228072014-07-01 15:14:11 +01002849 return getContentService().isSyncPendingAsUser(account, authority, null, userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002850 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002851 throw e.rethrowFromSystemServer();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002852 }
2853 }
2854
Fred Quintana1b487ec2010-02-26 10:57:55 -08002855 /**
2856 * Request notifications when the different aspects of the SyncManager change. The
2857 * different items that can be requested are:
2858 * <ul>
2859 * <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
2860 * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
2861 * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
2862 * </ul>
2863 * The caller can set one or more of the status types in the mask for any
2864 * given listener registration.
2865 * @param mask the status change types that will cause the callback to be invoked
2866 * @param callback observer to be invoked when the status changes
2867 * @return a handle that can be used to remove the listener at a later time
2868 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07002869 public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08002870 if (callback == null) {
2871 throw new IllegalArgumentException("you passed in a null callback");
2872 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07002873 try {
2874 ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
Steve McKayea93fe72016-12-02 11:35:35 -08002875 @Override
Fred Quintanaac9385e2009-06-22 18:00:59 -07002876 public void onStatusChanged(int which) throws RemoteException {
2877 callback.onStatusChanged(which);
2878 }
2879 };
2880 getContentService().addStatusChangeListener(mask, observer);
2881 return observer;
2882 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002883 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002884 }
2885 }
2886
Fred Quintana1b487ec2010-02-26 10:57:55 -08002887 /**
2888 * Remove a previously registered status change listener.
2889 * @param handle the handle that was returned by {@link #addStatusChangeListener}
2890 */
Fred Quintanaac9385e2009-06-22 18:00:59 -07002891 public static void removeStatusChangeListener(Object handle) {
Fred Quintana1b487ec2010-02-26 10:57:55 -08002892 if (handle == null) {
2893 throw new IllegalArgumentException("you passed in a null handle");
2894 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07002895 try {
2896 getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
2897 } catch (RemoteException e) {
Jeff Sharkeyc907d212018-04-09 09:56:42 -06002898 throw e.rethrowFromSystemServer();
Fred Quintanaac9385e2009-06-22 18:00:59 -07002899 }
2900 }
2901
Jeff Sharkey87314082016-03-11 17:25:11 -07002902 /** {@hide} */
2903 public void putCache(Uri key, Bundle value) {
2904 try {
2905 getContentService().putCache(mContext.getPackageName(), key, value,
2906 mContext.getUserId());
2907 } catch (RemoteException e) {
2908 throw e.rethrowFromSystemServer();
2909 }
2910 }
2911
2912 /** {@hide} */
2913 public Bundle getCache(Uri key) {
2914 try {
2915 final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key,
2916 mContext.getUserId());
2917 if (bundle != null) bundle.setClassLoader(mContext.getClassLoader());
2918 return bundle;
2919 } catch (RemoteException e) {
2920 throw e.rethrowFromSystemServer();
2921 }
2922 }
2923
Ben Lin8ea82002017-03-08 17:30:16 -08002924 /** {@hide} */
2925 public int getTargetSdkVersion() {
2926 return mTargetSdkVersion;
2927 }
2928
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002929 /**
2930 * Returns sampling percentage for a given duration.
2931 *
2932 * Always returns at least 1%.
2933 */
2934 private int samplePercentForDuration(long durationMillis) {
2935 if (durationMillis >= SLOW_THRESHOLD_MILLIS) {
2936 return 100;
2937 }
2938 return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1;
2939 }
2940
Steve McKayea93fe72016-12-02 11:35:35 -08002941 private void maybeLogQueryToEventLog(
2942 long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07002943 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002944 int samplePercent = samplePercentForDuration(durationMillis);
2945 if (samplePercent < 100) {
2946 synchronized (mRandom) {
2947 if (mRandom.nextInt(100) >= samplePercent) {
2948 return;
2949 }
2950 }
2951 }
2952
Steve McKayea93fe72016-12-02 11:35:35 -08002953 // Ensure a non-null bundle.
2954 queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY;
2955
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002956 StringBuilder projectionBuffer = new StringBuilder(100);
2957 if (projection != null) {
2958 for (int i = 0; i < projection.length; ++i) {
2959 // Note: not using a comma delimiter here, as the
2960 // multiple arguments to EventLog.writeEvent later
2961 // stringify with a comma delimiter, which would make
2962 // parsing uglier later.
2963 if (i != 0) projectionBuffer.append('/');
2964 projectionBuffer.append(projection[i]);
2965 }
2966 }
2967
2968 // ActivityThread.currentPackageName() only returns non-null if the
2969 // current thread is an application main thread. This parameter tells
2970 // us whether an event loop is blocked, and if so, which app it is.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002971 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002972
2973 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07002974 EventLogTags.CONTENT_QUERY_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002975 uri.toString(),
2976 projectionBuffer.toString(),
Steve McKay29c3f682016-12-16 14:52:59 -08002977 queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""),
2978 queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""),
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002979 durationMillis,
2980 blockingPackage != null ? blockingPackage : "",
2981 samplePercent);
2982 }
2983
2984 private void maybeLogUpdateToEventLog(
2985 long durationMillis, Uri uri, String operation, String selection) {
Jeff Sharkey2b4d22c2013-04-26 10:52:00 -07002986 if (!ENABLE_CONTENT_SAMPLE) return;
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002987 int samplePercent = samplePercentForDuration(durationMillis);
2988 if (samplePercent < 100) {
2989 synchronized (mRandom) {
2990 if (mRandom.nextInt(100) >= samplePercent) {
2991 return;
2992 }
2993 }
2994 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002995 String blockingPackage = AppGlobals.getInitialPackage();
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002996 EventLog.writeEvent(
Brad Fitzpatricka8fbedb2010-04-08 14:08:54 -07002997 EventLogTags.CONTENT_UPDATE_SAMPLE,
Brad Fitzpatricka63730d2010-02-07 22:25:34 -08002998 uri.toString(),
2999 operation,
3000 selection != null ? selection : "",
3001 durationMillis,
3002 blockingPackage != null ? blockingPackage : "",
3003 samplePercent);
3004 }
Fred Quintanaac9385e2009-06-22 18:00:59 -07003005
Jeff Brown825c5132011-10-12 16:11:30 -07003006 private final class CursorWrapperInner extends CrossProcessCursorWrapper {
Gilles Debunne03f02922010-06-09 14:11:45 -07003007 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003008 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003009
3010 private final CloseGuard mCloseGuard = CloseGuard.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003012 CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 super(cursor);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003014 mContentProvider = contentProvider;
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003015 mCloseGuard.open("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 }
3017
3018 @Override
3019 public void close() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003020 mCloseGuard.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 super.close();
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003022
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003023 if (mProviderReleased.compareAndSet(false, true)) {
3024 ContentResolver.this.releaseProvider(mContentProvider);
Jeff Brownbaaf8c32011-10-09 14:07:00 -07003025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 }
3027
3028 @Override
3029 protected void finalize() throws Throwable {
3030 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +00003031 if (mCloseGuard != null) {
3032 mCloseGuard.warnIfOpen();
3033 }
3034
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003035 close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 } finally {
3037 super.finalize();
3038 }
3039 }
3040 }
3041
3042 private final class ParcelFileDescriptorInner extends ParcelFileDescriptor {
Gilles Debunne03f02922010-06-09 14:11:45 -07003043 private final IContentProvider mContentProvider;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003044 private final AtomicBoolean mProviderReleased = new AtomicBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045
3046 ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) {
3047 super(pfd);
3048 mContentProvider = icp;
3049 }
3050
3051 @Override
Amith Yamasani487c11a2013-09-18 09:16:15 -07003052 public void releaseResources() {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003053 if (mProviderReleased.compareAndSet(false, true)) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003054 ContentResolver.this.releaseProvider(mContentProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 }
3056 }
3057 }
3058
Dianne Hackborn231cc602009-04-27 17:10:36 -07003059 /** @hide */
3060 public static final String CONTENT_SERVICE_NAME = "content";
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003061
Dianne Hackborn231cc602009-04-27 17:10:36 -07003062 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +01003063 @UnsupportedAppUsage
Dianne Hackborn231cc602009-04-27 17:10:36 -07003064 public static IContentService getContentService() {
3065 if (sContentService != null) {
3066 return sContentService;
3067 }
3068 IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003069 sContentService = IContentService.Stub.asInterface(b);
Dianne Hackborn231cc602009-04-27 17:10:36 -07003070 return sContentService;
3071 }
Tom O'Neill0ba1cb02010-01-08 12:53:50 -08003072
Dianne Hackborn35654b62013-01-14 17:38:02 -08003073 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +01003074 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003075 public String getPackageName() {
3076 return mPackageName;
3077 }
3078
Mathew Inwood1c77a112018-08-14 14:06:26 +01003079 @UnsupportedAppUsage
Jeff Sharkeyc907d212018-04-09 09:56:42 -06003080 private static volatile IContentService sContentService;
Mathew Inwood1c77a112018-08-14 14:06:26 +01003081 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 private final Context mContext;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003083
Mathew Inwood1c77a112018-08-14 14:06:26 +01003084 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08003085 final String mPackageName;
Jeff Sharkey912e80d2017-02-24 11:00:55 -07003086 final int mTargetSdkVersion;
Jeff Sharkey60cfad82016-01-05 17:30:57 -07003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 private static final String TAG = "ContentResolver";
Nicolas Prevotd85fc722014-04-16 19:52:08 +01003089
3090 /** @hide */
3091 public int resolveUserId(Uri uri) {
3092 return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
3093 }
Daniel Nishic29d2b02016-06-30 12:20:41 -07003094
3095 /** @hide */
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003096 public int getUserId() {
3097 return mContext.getUserId();
3098 }
3099
3100 /** @hide */
Daniel Nishic29d2b02016-06-30 12:20:41 -07003101 public Drawable getTypeDrawable(String mimeType) {
3102 return MimeIconUtils.loadMimeIcon(mContext, mimeType);
3103 }
Steve McKayea93fe72016-12-02 11:35:35 -08003104
3105 /**
3106 * @hide
3107 */
3108 public static @Nullable Bundle createSqlQueryBundle(
3109 @Nullable String selection,
3110 @Nullable String[] selectionArgs,
3111 @Nullable String sortOrder) {
3112
3113 if (selection == null && selectionArgs == null && sortOrder == null) {
3114 return null;
3115 }
3116
3117 Bundle queryArgs = new Bundle();
3118 if (selection != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003119 queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
Steve McKayea93fe72016-12-02 11:35:35 -08003120 }
3121 if (selectionArgs != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003122 queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
Steve McKayea93fe72016-12-02 11:35:35 -08003123 }
3124 if (sortOrder != null) {
Steve McKay29c3f682016-12-16 14:52:59 -08003125 queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);
Steve McKayea93fe72016-12-02 11:35:35 -08003126 }
3127 return queryArgs;
3128 }
Steve McKay29c3f682016-12-16 14:52:59 -08003129
3130 /**
3131 * Returns structured sort args formatted as an SQL sort clause.
3132 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003133 * NOTE: Collator clauses are suitable for use with non text fields. We might
3134 * choose to omit any collation clause since we don't know the underlying
3135 * type of data to be collated. Imperical testing shows that sqlite3 doesn't
3136 * appear to care much about the presence of collate clauses in queries
3137 * when ordering by numeric fields. For this reason we include collate
3138 * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present
3139 * in query args bundle.
Steve McKay29c3f682016-12-16 14:52:59 -08003140 *
Steve McKayd7ece9f2017-01-12 16:59:59 -08003141 * TODO: Would be nice to explicitly validate that colums referenced in
3142 * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection.
Steve McKay29c3f682016-12-16 14:52:59 -08003143 *
3144 * @hide
3145 */
3146 public static String createSqlSortClause(Bundle queryArgs) {
3147 String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS);
3148 if (columns == null || columns.length == 0) {
3149 throw new IllegalArgumentException("Can't create sort clause without columns.");
3150 }
3151
3152 String query = TextUtils.join(", ", columns);
3153
Steve McKayd7ece9f2017-01-12 16:59:59 -08003154 // Interpret PRIMARY and SECONDARY collation strength as no-case collation based
3155 // on their javadoc descriptions.
3156 int collation = queryArgs.getInt(
3157 ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL);
3158 if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) {
3159 query += " COLLATE NOCASE";
3160 }
3161
Steve McKay415f41b2017-02-01 13:38:25 -08003162 int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE);
3163 if (sortDir != Integer.MIN_VALUE) {
3164 switch (sortDir) {
3165 case QUERY_SORT_DIRECTION_ASCENDING:
3166 query += " ASC";
3167 break;
3168 case QUERY_SORT_DIRECTION_DESCENDING:
3169 query += " DESC";
3170 break;
3171 default:
3172 throw new IllegalArgumentException("Unsupported sort direction value."
3173 + " See ContentResolver documentation for details.");
3174 }
Steve McKay29c3f682016-12-16 14:52:59 -08003175 }
Steve McKay29c3f682016-12-16 14:52:59 -08003176 return query;
3177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178}