The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content; |
| 18 | |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 19 | import android.accounts.Account; |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Scott Kennedy | 9f78f65 | 2015-03-01 15:29:25 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 23 | import android.annotation.RequiresPermission; |
Jeff Sharkey | a73b8fd | 2016-01-06 17:02:08 -0700 | [diff] [blame] | 24 | import android.annotation.TestApi; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 25 | import android.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 26 | import android.annotation.UserIdInt; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 27 | import android.app.ActivityManager; |
Jeff Sharkey | 66a017b | 2013-01-17 18:18:22 -0800 | [diff] [blame] | 28 | import android.app.ActivityThread; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 29 | import android.app.AppGlobals; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
| 31 | import android.content.res.AssetFileDescriptor; |
| 32 | import android.content.res.Resources; |
| 33 | import android.database.ContentObserver; |
Jeff Brown | 825c513 | 2011-10-12 16:11:30 -0700 | [diff] [blame] | 34 | import android.database.CrossProcessCursorWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.database.IContentObserver; |
Jeff Sharkey | 5b836f2 | 2014-08-27 14:46:32 -0700 | [diff] [blame] | 37 | import android.graphics.Point; |
Daniel Nishi | c29d2b0 | 2016-06-30 12:20:41 -0700 | [diff] [blame] | 38 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.net.Uri; |
| 40 | import android.os.Bundle; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 41 | import android.os.CancellationSignal; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 42 | import android.os.DeadObjectException; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 43 | import android.os.IBinder; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 44 | import android.os.ICancellationSignal; |
| 45 | import android.os.OperationCanceledException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.ParcelFileDescriptor; |
| 47 | import android.os.RemoteException; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 48 | import android.os.ServiceManager; |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 49 | import android.os.SystemClock; |
Dianne Hackborn | 5e03e2ca | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.text.TextUtils; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 52 | import android.util.EventLog; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 53 | import android.util.Log; |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 54 | |
Daniel Nishi | c29d2b0 | 2016-06-30 12:20:41 -0700 | [diff] [blame] | 55 | import com.android.internal.util.MimeIconUtils; |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 56 | import com.android.internal.util.Preconditions; |
| 57 | |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 58 | import dalvik.system.CloseGuard; |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import java.io.File; |
| 61 | import java.io.FileInputStream; |
| 62 | import java.io.FileNotFoundException; |
| 63 | import java.io.IOException; |
| 64 | import java.io.InputStream; |
| 65 | import java.io.OutputStream; |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 66 | import java.lang.annotation.Retention; |
| 67 | import java.lang.annotation.RetentionPolicy; |
Gilles Debunne | 03f0292 | 2010-06-09 14:11:45 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import java.util.List; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 70 | import java.util.Random; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 71 | import java.util.concurrent.atomic.AtomicBoolean; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | /** |
| 74 | * This class provides applications access to the content model. |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 75 | * |
| 76 | * <div class="special reference"> |
| 77 | * <h3>Developer Guides</h3> |
| 78 | * <p>For more information about using a ContentResolver with content providers, read the |
| 79 | * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a> |
| 80 | * developer guide.</p> |
Ng Zhi An | 3ca5130 | 2019-01-24 21:39:24 -0800 | [diff] [blame] | 81 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | */ |
| 83 | public abstract class ContentResolver { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 84 | /** |
| 85 | * @deprecated instead use |
| 86 | * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)} |
| 87 | */ |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 88 | @Deprecated |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 89 | public static final String SYNC_EXTRAS_ACCOUNT = "account"; |
Georgi Nikolov | b339557 | 2013-06-18 18:27:31 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * If this extra is set to true, the sync request will be scheduled |
| 93 | * at the front of the sync request queue and without any delay |
| 94 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | public static final String SYNC_EXTRAS_EXPEDITED = "expedited"; |
Georgi Nikolov | b339557 | 2013-06-18 18:27:31 -0700 | [diff] [blame] | 96 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 97 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 98 | * If this extra is set to true, the sync request will be scheduled |
| 99 | * only when the device is plugged in. This is equivalent to calling |
| 100 | * setRequiresCharging(true) on {@link SyncRequest}. |
| 101 | */ |
| 102 | public static final String SYNC_EXTRAS_REQUIRE_CHARGING = "require_charging"; |
| 103 | |
| 104 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 105 | * @deprecated instead use |
| 106 | * {@link #SYNC_EXTRAS_MANUAL} |
| 107 | */ |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 108 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | public static final String SYNC_EXTRAS_FORCE = "force"; |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * If this extra is set to true then the sync settings (like getSyncAutomatically()) |
| 113 | * are ignored by the sync scheduler. |
| 114 | */ |
| 115 | public static final String SYNC_EXTRAS_IGNORE_SETTINGS = "ignore_settings"; |
| 116 | |
| 117 | /** |
| 118 | * If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries) |
| 119 | * are ignored by the sync scheduler. If this request fails and gets rescheduled then the |
| 120 | * retries will still honor the backoff. |
| 121 | */ |
| 122 | public static final String SYNC_EXTRAS_IGNORE_BACKOFF = "ignore_backoff"; |
| 123 | |
| 124 | /** |
| 125 | * If this extra is set to true then the request will not be retried if it fails. |
| 126 | */ |
| 127 | public static final String SYNC_EXTRAS_DO_NOT_RETRY = "do_not_retry"; |
| 128 | |
| 129 | /** |
| 130 | * Setting this extra is the equivalent of setting both {@link #SYNC_EXTRAS_IGNORE_SETTINGS} |
| 131 | * and {@link #SYNC_EXTRAS_IGNORE_BACKOFF} |
| 132 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 133 | public static final String SYNC_EXTRAS_MANUAL = "force"; |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 134 | |
Georgi Nikolov | b339557 | 2013-06-18 18:27:31 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Indicates that this sync is intended to only upload local changes to the server. |
| 137 | * For example, this will be set to true if the sync is initiated by a call to |
| 138 | * {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)} |
| 139 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | public static final String SYNC_EXTRAS_UPLOAD = "upload"; |
Georgi Nikolov | b339557 | 2013-06-18 18:27:31 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Indicates that the sync adapter should proceed with the delete operations, |
| 144 | * even if it determines that there are too many. |
| 145 | * See {@link SyncResult#tooManyDeletions} |
| 146 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | public static final String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS = "deletions_override"; |
Georgi Nikolov | b339557 | 2013-06-18 18:27:31 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Indicates that the sync adapter should not proceed with the delete operations, |
| 151 | * if it determines that there are too many. |
| 152 | * See {@link SyncResult#tooManyDeletions} |
| 153 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | public static final String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS = "discard_deletions"; |
| 155 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 156 | /* Extensions to API. TODO: Not clear if we will keep these as public flags. */ |
| 157 | /** {@hide} User-specified flag for expected upload size. */ |
| 158 | public static final String SYNC_EXTRAS_EXPECTED_UPLOAD = "expected_upload"; |
| 159 | |
| 160 | /** {@hide} User-specified flag for expected download size. */ |
| 161 | public static final String SYNC_EXTRAS_EXPECTED_DOWNLOAD = "expected_download"; |
| 162 | |
| 163 | /** {@hide} Priority of this sync with respect to other syncs scheduled for this application. */ |
| 164 | public static final String SYNC_EXTRAS_PRIORITY = "sync_priority"; |
| 165 | |
| 166 | /** {@hide} Flag to allow sync to occur on metered network. */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 167 | public static final String SYNC_EXTRAS_DISALLOW_METERED = "allow_metered"; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 168 | |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 169 | /** |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 170 | * {@hide} Integer extra containing a SyncExemption flag. |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 171 | * |
| 172 | * Only the system and the shell user can set it. |
| 173 | * |
| 174 | * This extra is "virtual". Once passed to the system server, it'll be removed from the bundle. |
| 175 | */ |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 176 | public static final String SYNC_VIRTUAL_EXTRAS_EXEMPTION_FLAG = "v_exemption"; |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 177 | |
| 178 | /** |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 179 | * Set by the SyncManager to request that the SyncAdapter initialize itself for |
| 180 | * the given account/authority pair. One required initialization step is to |
| 181 | * ensure that {@link #setIsSyncable(android.accounts.Account, String, int)} has been |
| 182 | * called with a >= 0 value. When this flag is set the SyncAdapter does not need to |
| 183 | * do a full sync, though it is allowed to do so. |
| 184 | */ |
| 185 | public static final String SYNC_EXTRAS_INITIALIZE = "initialize"; |
| 186 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 187 | /** @hide */ |
| 188 | public static final Intent ACTION_SYNC_CONN_STATUS_CHANGED = |
| 189 | new Intent("com.android.sync.SYNC_CONN_STATUS_CHANGED"); |
| 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | public static final String SCHEME_CONTENT = "content"; |
| 192 | public static final String SCHEME_ANDROID_RESOURCE = "android.resource"; |
| 193 | public static final String SCHEME_FILE = "file"; |
| 194 | |
| 195 | /** |
Jeff Sharkey | 5b836f2 | 2014-08-27 14:46:32 -0700 | [diff] [blame] | 196 | * An extra {@link Point} describing the optimal size for a requested image |
| 197 | * resource, in pixels. If a provider has multiple sizes of the image, it |
| 198 | * should return the image closest to this size. |
| 199 | * |
| 200 | * @see #openTypedAssetFileDescriptor(Uri, String, Bundle) |
| 201 | * @see #openTypedAssetFileDescriptor(Uri, String, Bundle, |
| 202 | * CancellationSignal) |
| 203 | */ |
| 204 | public static final String EXTRA_SIZE = "android.content.extra.SIZE"; |
| 205 | |
| 206 | /** |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 207 | * An extra boolean describing whether a particular provider supports refresh |
| 208 | * or not. If a provider supports refresh, it should include this key in its |
| 209 | * returned Cursor as part of its query call. |
| 210 | * |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 211 | */ |
| 212 | public static final String EXTRA_REFRESH_SUPPORTED = "android.content.extra.REFRESH_SUPPORTED"; |
| 213 | |
| 214 | /** |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 215 | * Key for an SQL style selection string that may be present in the query Bundle argument |
| 216 | * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)} |
| 217 | * when called by a legacy client. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 218 | * |
| 219 | * <p>Clients should never include user supplied values directly in the selection string, |
| 220 | * as this presents an avenue for SQL injection attacks. In lieu of this, a client |
| 221 | * should use standard placeholder notation to represent values in a selection string, |
| 222 | * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}. |
| 223 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 224 | * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly |
| 225 | * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b> |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 226 | * |
| 227 | * @see #QUERY_ARG_SORT_COLUMNS |
| 228 | * @see #QUERY_ARG_SORT_DIRECTION |
| 229 | * @see #QUERY_ARG_SORT_COLLATION |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 230 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 231 | public static final String QUERY_ARG_SQL_SELECTION = "android:query-arg-sql-selection"; |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 232 | |
| 233 | /** |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 234 | * Key for SQL selection string arguments list. |
| 235 | * |
| 236 | * <p>Clients should never include user supplied values directly in the selection string, |
| 237 | * as this presents an avenue for SQL injection attacks. In lieu of this, a client |
| 238 | * should use standard placeholder notation to represent values in a selection string, |
| 239 | * then supply a corresponding value in {@value #QUERY_ARG_SQL_SELECTION_ARGS}. |
| 240 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 241 | * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly |
| 242 | * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b> |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 243 | * |
| 244 | * @see #QUERY_ARG_SORT_COLUMNS |
| 245 | * @see #QUERY_ARG_SORT_DIRECTION |
| 246 | * @see #QUERY_ARG_SORT_COLLATION |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 247 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 248 | public static final String QUERY_ARG_SQL_SELECTION_ARGS = |
| 249 | "android:query-arg-sql-selection-args"; |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * Key for an SQL style sort string that may be present in the query Bundle argument |
| 253 | * passed to {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)} |
| 254 | * when called by a legacy client. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 255 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 256 | * <p><b>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher are strongly |
| 257 | * encourage to use structured query arguments in lieu of opaque SQL query clauses.</b> |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 258 | * |
| 259 | * @see #QUERY_ARG_SORT_COLUMNS |
| 260 | * @see #QUERY_ARG_SORT_DIRECTION |
| 261 | * @see #QUERY_ARG_SORT_COLLATION |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 262 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 263 | public static final String QUERY_ARG_SQL_SORT_ORDER = "android:query-arg-sql-sort-order"; |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 264 | |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 265 | /** {@hide} */ |
| 266 | public static final String QUERY_ARG_SQL_GROUP_BY = "android:query-arg-sql-group-by"; |
| 267 | /** {@hide} */ |
| 268 | public static final String QUERY_ARG_SQL_HAVING = "android:query-arg-sql-having"; |
| 269 | /** {@hide} */ |
| 270 | public static final String QUERY_ARG_SQL_LIMIT = "android:query-arg-sql-limit"; |
| 271 | |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 272 | /** |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 273 | * Specifies the list of columns against which to sort results. When first column values |
| 274 | * are identical, records are then sorted based on second column values, and so on. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 275 | * |
| 276 | * <p>Columns present in this list must also be included in the projection |
| 277 | * supplied to {@link ContentResolver#query(Uri, String[], Bundle, CancellationSignal)}. |
| 278 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 279 | * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher: |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 280 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 281 | * <li>{@link ContentProvider} implementations: When preparing data in |
| 282 | * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort columns |
| 283 | * is reflected in the returned Cursor, it is strongly recommended that |
| 284 | * {@link #QUERY_ARG_SORT_COLUMNS} then be included in the array of honored arguments |
| 285 | * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 286 | * |
| 287 | * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the |
| 288 | * arguments {@link Bundle}, the Content framework will attempt to synthesize |
| 289 | * an QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 290 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 291 | public static final String QUERY_ARG_SORT_COLUMNS = "android:query-arg-sort-columns"; |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * Specifies desired sort order. When unspecified a provider may provide a default |
| 295 | * sort direction, or choose to return unsorted results. |
| 296 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 297 | * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher: |
| 298 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 299 | * <li>{@link ContentProvider} implementations: When preparing data in |
| 300 | * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort direction |
| 301 | * is reflected in the returned Cursor, it is strongly recommended that |
| 302 | * {@link #QUERY_ARG_SORT_DIRECTION} then be included in the array of honored arguments |
| 303 | * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 304 | * |
| 305 | * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the |
| 306 | * arguments {@link Bundle}, the Content framework will attempt to synthesize |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 307 | * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 308 | * |
| 309 | * @see #QUERY_SORT_DIRECTION_ASCENDING |
| 310 | * @see #QUERY_SORT_DIRECTION_DESCENDING |
| 311 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 312 | public static final String QUERY_ARG_SORT_DIRECTION = "android:query-arg-sort-direction"; |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 313 | |
| 314 | /** |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 315 | * Allows client to specify a hint to the provider declaring which collation |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 316 | * to use when sorting text values. |
| 317 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 318 | * <p>Providers may support custom collators. When specifying a custom collator |
| 319 | * the value is determined by the Provider. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 320 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 321 | * <li>{@link ContentProvider} implementations: When preparing data in |
| 322 | * {@link ContentProvider#query(Uri, String[], Bundle, CancellationSignal)}, if sort collation |
| 323 | * is reflected in the returned Cursor, it is strongly recommended that |
| 324 | * {@link #QUERY_ARG_SORT_COLLATION} then be included in the array of honored arguments |
| 325 | * reflected in {@link Cursor} extras {@link Bundle} under {@link #EXTRA_HONORED_ARGS}. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 326 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 327 | * <li>When querying a provider, where no QUERY_ARG_SQL* otherwise exists in the |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 328 | * arguments {@link Bundle}, the Content framework will attempt to synthesize |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 329 | * a QUERY_ARG_SQL* argument using the corresponding QUERY_ARG_SORT* values. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 330 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 331 | * @see java.text.Collator#PRIMARY |
| 332 | * @see java.text.Collator#SECONDARY |
| 333 | * @see java.text.Collator#TERTIARY |
| 334 | * @see java.text.Collator#IDENTICAL |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 335 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 336 | public static final String QUERY_ARG_SORT_COLLATION = "android:query-arg-sort-collation"; |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 337 | |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 338 | /** |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 339 | * Allows provider to report back to client which query keys are honored in a Cursor. |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 340 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 341 | * <p>Key identifying a {@code String[]} containing all QUERY_ARG_SORT* arguments |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 342 | * honored by the provider. Include this in {@link Cursor} extras {@link Bundle} |
| 343 | * when any QUERY_ARG_SORT* value was honored during the preparation of the |
| 344 | * results {@link Cursor}. |
| 345 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 346 | * <p>If present, ALL honored arguments are enumerated in this extra’s payload. |
| 347 | * |
| 348 | * @see #QUERY_ARG_SORT_COLUMNS |
| 349 | * @see #QUERY_ARG_SORT_DIRECTION |
| 350 | * @see #QUERY_ARG_SORT_COLLATION |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 351 | */ |
| 352 | public static final String EXTRA_HONORED_ARGS = "android.content.extra.HONORED_ARGS"; |
| 353 | |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 354 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 355 | @IntDef(flag = false, prefix = { "QUERY_SORT_DIRECTION_" }, value = { |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 356 | QUERY_SORT_DIRECTION_ASCENDING, |
| 357 | QUERY_SORT_DIRECTION_DESCENDING |
| 358 | }) |
| 359 | @Retention(RetentionPolicy.SOURCE) |
| 360 | public @interface SortDirection {} |
| 361 | public static final int QUERY_SORT_DIRECTION_ASCENDING = 0; |
| 362 | public static final int QUERY_SORT_DIRECTION_DESCENDING = 1; |
| 363 | |
| 364 | /** |
| 365 | * @see {@link java.text.Collector} for details on respective collation strength. |
| 366 | * @hide |
| 367 | */ |
| 368 | @IntDef(flag = false, value = { |
| 369 | java.text.Collator.PRIMARY, |
| 370 | java.text.Collator.SECONDARY, |
| 371 | java.text.Collator.TERTIARY, |
| 372 | java.text.Collator.IDENTICAL |
| 373 | }) |
| 374 | @Retention(RetentionPolicy.SOURCE) |
| 375 | public @interface QueryCollator {} |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 376 | |
| 377 | /** |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 378 | * Specifies the offset row index within a Cursor. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 379 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 380 | public static final String QUERY_ARG_OFFSET = "android:query-arg-offset"; |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 381 | |
| 382 | /** |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 383 | * Specifies the max number of rows to include in a Cursor. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 384 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 385 | public static final String QUERY_ARG_LIMIT = "android:query-arg-limit"; |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 386 | |
| 387 | /** |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 388 | * Added to {@link Cursor} extras {@link Bundle} to indicate total row count of |
| 389 | * recordset when paging is supported. Providers must include this when |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 390 | * implementing paging support. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 391 | * |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 392 | * <p>A provider may return -1 that row count of the recordset is unknown. |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 393 | * |
| 394 | * <p>Providers having returned -1 in a previous query are recommended to |
| 395 | * send content change notification once (if) full recordset size becomes |
| 396 | * known. |
| 397 | */ |
Steve McKay | d74a3bd | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 398 | public static final String EXTRA_TOTAL_COUNT = "android.content.extra.TOTAL_COUNT"; |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 399 | |
| 400 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | * This is the Android platform's base MIME type for a content: URI |
| 402 | * containing a Cursor of a single item. Applications should use this |
| 403 | * as the base type along with their own sub-type of their content: URIs |
| 404 | * that represent a particular item. For example, hypothetical IMAP email |
| 405 | * client may have a URI |
| 406 | * <code>content://com.company.provider.imap/inbox/1</code> for a particular |
| 407 | * message in the inbox, whose MIME type would be reported as |
| 408 | * <code>CURSOR_ITEM_BASE_TYPE + "/vnd.company.imap-msg"</code> |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 409 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | * <p>Compare with {@link #CURSOR_DIR_BASE_TYPE}. |
| 411 | */ |
| 412 | public static final String CURSOR_ITEM_BASE_TYPE = "vnd.android.cursor.item"; |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | /** |
| 415 | * This is the Android platform's base MIME type for a content: URI |
| 416 | * containing a Cursor of zero or more items. Applications should use this |
| 417 | * as the base type along with their own sub-type of their content: URIs |
| 418 | * that represent a directory of items. For example, hypothetical IMAP email |
| 419 | * client may have a URI |
| 420 | * <code>content://com.company.provider.imap/inbox</code> for all of the |
| 421 | * messages in its inbox, whose MIME type would be reported as |
| 422 | * <code>CURSOR_DIR_BASE_TYPE + "/vnd.company.imap-msg"</code> |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 423 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | * <p>Note how the base MIME type varies between this and |
| 425 | * {@link #CURSOR_ITEM_BASE_TYPE} depending on whether there is |
| 426 | * one single item or multiple items in the data set, while the sub-type |
| 427 | * remains the same because in either case the data structure contained |
| 428 | * in the cursor is the same. |
| 429 | */ |
| 430 | public static final String CURSOR_DIR_BASE_TYPE = "vnd.android.cursor.dir"; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 431 | |
Matt Casey | bd7bcf0 | 2014-02-05 15:51:39 -0800 | [diff] [blame] | 432 | /** |
| 433 | * This is the Android platform's generic MIME type to match any MIME |
| 434 | * type of the form "{@link #CURSOR_ITEM_BASE_TYPE}/{@code SUB_TYPE}". |
| 435 | * {@code SUB_TYPE} is the sub-type of the application-dependent |
| 436 | * content, e.g., "audio", "video", "playlist". |
| 437 | */ |
| 438 | public static final String ANY_CURSOR_ITEM_TYPE = "vnd.android.cursor.item/*"; |
| 439 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 440 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 441 | @UnsupportedAppUsage |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 442 | public static final int SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS = 1; |
| 443 | /** @hide */ |
| 444 | public static final int SYNC_ERROR_AUTHENTICATION = 2; |
| 445 | /** @hide */ |
| 446 | public static final int SYNC_ERROR_IO = 3; |
| 447 | /** @hide */ |
| 448 | public static final int SYNC_ERROR_PARSE = 4; |
| 449 | /** @hide */ |
| 450 | public static final int SYNC_ERROR_CONFLICT = 5; |
| 451 | /** @hide */ |
| 452 | public static final int SYNC_ERROR_TOO_MANY_DELETIONS = 6; |
| 453 | /** @hide */ |
| 454 | public static final int SYNC_ERROR_TOO_MANY_RETRIES = 7; |
| 455 | /** @hide */ |
| 456 | public static final int SYNC_ERROR_INTERNAL = 8; |
| 457 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 458 | private static final String[] SYNC_ERROR_NAMES = new String[] { |
| 459 | "already-in-progress", |
| 460 | "authentication-error", |
| 461 | "io-error", |
| 462 | "parse-error", |
| 463 | "conflict", |
| 464 | "too-many-deletions", |
| 465 | "too-many-retries", |
| 466 | "internal-error", |
| 467 | }; |
| 468 | |
| 469 | /** @hide */ |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 470 | public static String syncErrorToString(int error) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 471 | if (error < 1 || error > SYNC_ERROR_NAMES.length) { |
| 472 | return String.valueOf(error); |
| 473 | } |
| 474 | return SYNC_ERROR_NAMES[error - 1]; |
| 475 | } |
| 476 | |
Alon Albert | 5c113fa | 2013-02-07 08:07:32 -0800 | [diff] [blame] | 477 | /** @hide */ |
| 478 | public static int syncErrorStringToInt(String error) { |
| 479 | for (int i = 0, n = SYNC_ERROR_NAMES.length; i < n; i++) { |
| 480 | if (SYNC_ERROR_NAMES[i].equals(error)) { |
| 481 | return i + 1; |
| 482 | } |
| 483 | } |
| 484 | if (error != null) { |
| 485 | try { |
| 486 | return Integer.parseInt(error); |
| 487 | } catch (NumberFormatException e) { |
| 488 | Log.d(TAG, "error parsing sync error: " + error); |
| 489 | } |
| 490 | } |
| 491 | return 0; |
| 492 | } |
| 493 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 494 | public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 495 | public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 496 | public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2; |
| 497 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 498 | @UnsupportedAppUsage |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 499 | public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3; |
| 500 | /** @hide */ |
| 501 | public static final int SYNC_OBSERVER_TYPE_ALL = 0x7fffffff; |
| 502 | |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 503 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 504 | @IntDef(flag = true, prefix = { "NOTIFY_" }, value = { |
| 505 | NOTIFY_SYNC_TO_NETWORK, |
| 506 | NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS |
| 507 | }) |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 508 | @Retention(RetentionPolicy.SOURCE) |
| 509 | public @interface NotifyFlags {} |
| 510 | |
| 511 | /** |
| 512 | * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: attempt to sync the change |
| 513 | * to the network. |
| 514 | */ |
| 515 | public static final int NOTIFY_SYNC_TO_NETWORK = 1<<0; |
| 516 | |
| 517 | /** |
| 518 | * Flag for {@link #notifyChange(Uri, ContentObserver, int)}: if set, this notification |
| 519 | * will be skipped if it is being delivered to the root URI of a ContentObserver that is |
| 520 | * using "notify for descendants." The purpose of this is to allow the provide to send |
| 521 | * a general notification of "something under X" changed that observers of that specific |
| 522 | * URI can receive, while also sending a specific URI under X. It would use this flag |
| 523 | * when sending the former, so that observers of "X and descendants" only see the latter. |
| 524 | */ |
| 525 | public static final int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS = 1<<1; |
| 526 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 527 | /** |
| 528 | * No exception, throttled by app standby normally. |
| 529 | * @hide |
| 530 | */ |
| 531 | public static final int SYNC_EXEMPTION_NONE = 0; |
| 532 | |
| 533 | /** |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 534 | * Exemption given to a sync request made by a foreground app (including |
| 535 | * PROCESS_STATE_IMPORTANT_FOREGROUND). |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 536 | * |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 537 | * At the schedule time, we promote the sync adapter app for a higher bucket: |
| 538 | * - If the device is not dozing (so the sync will start right away) |
| 539 | * promote to ACTIVE for 1 hour. |
| 540 | * - If the device is dozing (so the sync *won't* start right away), |
| 541 | * promote to WORKING_SET for 4 hours, so it'll get a higher chance to be started once the |
| 542 | * device comes out of doze. |
| 543 | * - When the sync actually starts, we promote the sync adapter app to ACTIVE for 10 minutes, |
| 544 | * so it can schedule and start more syncs without getting throttled, even when the first |
| 545 | * operation was canceled and now we're retrying. |
| 546 | * |
| 547 | * |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 548 | * @hide |
| 549 | */ |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 550 | public static final int SYNC_EXEMPTION_PROMOTE_BUCKET = 1; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 551 | |
| 552 | /** |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 553 | * In addition to {@link #SYNC_EXEMPTION_PROMOTE_BUCKET}, we put the sync adapter app in the |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 554 | * temp whitelist for 10 minutes, so that even RARE apps can run syncs right away. |
| 555 | * @hide |
| 556 | */ |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 557 | public static final int SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP = 2; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 558 | |
| 559 | /** @hide */ |
| 560 | @IntDef(flag = false, prefix = { "SYNC_EXEMPTION_" }, value = { |
| 561 | SYNC_EXEMPTION_NONE, |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 562 | SYNC_EXEMPTION_PROMOTE_BUCKET, |
| 563 | SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 564 | }) |
| 565 | @Retention(RetentionPolicy.SOURCE) |
| 566 | public @interface SyncExemption {} |
| 567 | |
Brad Fitzpatrick | 2588096 | 2010-02-22 15:17:49 -0800 | [diff] [blame] | 568 | // Always log queries which take 500ms+; shorter queries are |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 569 | // sampled accordingly. |
Jeff Sharkey | 2b4d22c | 2013-04-26 10:52:00 -0700 | [diff] [blame] | 570 | private static final boolean ENABLE_CONTENT_SAMPLE = false; |
Brad Fitzpatrick | 2588096 | 2010-02-22 15:17:49 -0800 | [diff] [blame] | 571 | private static final int SLOW_THRESHOLD_MILLIS = 500; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 572 | private final Random mRandom = new Random(); // guarded by itself |
| 573 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 574 | public ContentResolver(Context context) { |
Jeff Sharkey | 66a017b | 2013-01-17 18:18:22 -0800 | [diff] [blame] | 575 | mContext = context != null ? context : ActivityThread.currentApplication(); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 576 | mPackageName = mContext.getOpPackageName(); |
Jeff Sharkey | 4b2e87f | 2017-04-26 00:36:02 +0000 | [diff] [blame] | 577 | mTargetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 581 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | protected abstract IContentProvider acquireProvider(Context c, String name); |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * Providing a default implementation of this, to avoid having to change a |
| 586 | * lot of other things, but implementations of ContentResolver should |
| 587 | * implement it. |
| 588 | * |
| 589 | * @hide |
| 590 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 591 | @UnsupportedAppUsage |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 592 | protected IContentProvider acquireExistingProvider(Context c, String name) { |
| 593 | return acquireProvider(c, name); |
| 594 | } |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 597 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | public abstract boolean releaseProvider(IContentProvider icp); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 599 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 600 | @UnsupportedAppUsage |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 601 | protected abstract IContentProvider acquireUnstableProvider(Context c, String name); |
| 602 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 603 | @UnsupportedAppUsage |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 604 | public abstract boolean releaseUnstableProvider(IContentProvider icp); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 605 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 606 | @UnsupportedAppUsage |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 607 | public abstract void unstableProviderDied(IContentProvider icp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 609 | /** @hide */ |
| 610 | public void appNotRespondingViaProvider(IContentProvider icp) { |
| 611 | throw new UnsupportedOperationException("appNotRespondingViaProvider"); |
| 612 | } |
| 613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | /** |
| 615 | * Return the MIME type of the given content URL. |
| 616 | * |
| 617 | * @param url A Uri identifying content (either a list or specific type), |
| 618 | * using the content:// scheme. |
| 619 | * @return A MIME type for the content, or null if the URL is invalid or the type is unknown |
| 620 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 621 | public final @Nullable String getType(@NonNull Uri url) { |
| 622 | Preconditions.checkNotNull(url, "url"); |
| 623 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 624 | // XXX would like to have an acquireExistingUnstableProvider for this. |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 625 | IContentProvider provider = acquireExistingProvider(url); |
| 626 | if (provider != null) { |
| 627 | try { |
| 628 | return provider.getType(url); |
| 629 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 630 | // Arbitrary and not worth documenting, as Activity |
| 631 | // Manager will kill this process shortly anyway. |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 632 | return null; |
| 633 | } catch (java.lang.Exception e) { |
Ola Olsson | 145e6c4 | 2010-12-20 16:45:35 +0100 | [diff] [blame] | 634 | Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")"); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 635 | return null; |
| 636 | } finally { |
| 637 | releaseProvider(provider); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (!SCHEME_CONTENT.equals(url.getScheme())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | return null; |
| 643 | } |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 644 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 646 | String type = ActivityManager.getService().getProviderMimeType( |
Nicolas Prevot | 1dddc7f | 2014-07-07 17:44:58 +0100 | [diff] [blame] | 647 | ContentProvider.getUriWithoutUserId(url), resolveUserId(url)); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 648 | return type; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 650 | throw e.rethrowFromSystemServer(); |
Ola Olsson | 145e6c4 | 2010-12-20 16:45:35 +0100 | [diff] [blame] | 651 | } catch (java.lang.Exception e) { |
| 652 | Log.w(TAG, "Failed to get type for: " + url + " (" + e.getMessage() + ")"); |
| 653 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 658 | * Query for the possible MIME types for the representations the given |
| 659 | * content URL can be returned when opened as as stream with |
| 660 | * {@link #openTypedAssetFileDescriptor}. Note that the types here are |
| 661 | * not necessarily a superset of the type returned by {@link #getType} -- |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 662 | * many content providers cannot return a raw stream for the structured |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 663 | * data that they contain. |
| 664 | * |
| 665 | * @param url A Uri identifying content (either a list or specific type), |
| 666 | * using the content:// scheme. |
| 667 | * @param mimeTypeFilter The desired MIME type. This may be a pattern, |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 668 | * such as */*, to query for all available MIME types that match the |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 669 | * pattern. |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 670 | * @return Returns an array of MIME type strings for all available |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 671 | * data streams that match the given mimeTypeFilter. If there are none, |
| 672 | * null is returned. |
| 673 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 674 | public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) { |
| 675 | Preconditions.checkNotNull(url, "url"); |
| 676 | Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter"); |
| 677 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 678 | IContentProvider provider = acquireProvider(url); |
| 679 | if (provider == null) { |
| 680 | return null; |
| 681 | } |
Dianne Hackborn | 64bbbb4 | 2010-09-27 20:25:20 -0700 | [diff] [blame] | 682 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 683 | try { |
| 684 | return provider.getStreamTypes(url, mimeTypeFilter); |
| 685 | } catch (RemoteException e) { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 686 | // Arbitrary and not worth documenting, as Activity |
| 687 | // Manager will kill this process shortly anyway. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 688 | return null; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 689 | } finally { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 690 | releaseProvider(provider); |
| 691 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | * Query the given URI, returning a {@link Cursor} over the result set. |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 696 | * <p> |
| 697 | * For best performance, the caller should follow these guidelines: |
| 698 | * <ul> |
| 699 | * <li>Provide an explicit projection, to prevent |
| 700 | * reading data from storage that aren't going to be used.</li> |
| 701 | * <li>Use question mark parameter markers such as 'phone=?' instead of |
| 702 | * explicit values in the {@code selection} parameter, so that queries |
| 703 | * that differ only by those values will be recognized as the same |
| 704 | * for caching purposes.</li> |
| 705 | * </ul> |
| 706 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | * |
| 708 | * @param uri The URI, using the content:// scheme, for the content to |
| 709 | * retrieve. |
| 710 | * @param projection A list of which columns to return. Passing null will |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 711 | * return all columns, which is inefficient. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | * @param selection A filter declaring which rows to return, formatted as an |
| 713 | * SQL WHERE clause (excluding the WHERE itself). Passing null will |
| 714 | * return all rows for the given URI. |
| 715 | * @param selectionArgs You may include ?s in selection, which will be |
| 716 | * replaced by the values from selectionArgs, in the order that they |
| 717 | * appear in the selection. The values will be bound as Strings. |
| 718 | * @param sortOrder How to order the rows, formatted as an SQL ORDER BY |
| 719 | * clause (excluding the ORDER BY itself). Passing null will use the |
| 720 | * default sort order, which may be unordered. |
| 721 | * @return A Cursor object, which is positioned before the first entry, or null |
| 722 | * @see Cursor |
| 723 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 724 | public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, |
| 725 | @Nullable String[] projection, @Nullable String selection, |
| 726 | @Nullable String[] selectionArgs, @Nullable String sortOrder) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 727 | return query(uri, projection, selection, selectionArgs, sortOrder, null); |
| 728 | } |
| 729 | |
| 730 | /** |
Jeff Brown | c64ff37 | 2013-10-09 18:50:56 -0700 | [diff] [blame] | 731 | * Query the given URI, returning a {@link Cursor} over the result set |
| 732 | * with optional support for cancellation. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 733 | * <p> |
| 734 | * For best performance, the caller should follow these guidelines: |
| 735 | * <ul> |
| 736 | * <li>Provide an explicit projection, to prevent |
| 737 | * reading data from storage that aren't going to be used.</li> |
| 738 | * <li>Use question mark parameter markers such as 'phone=?' instead of |
| 739 | * explicit values in the {@code selection} parameter, so that queries |
| 740 | * that differ only by those values will be recognized as the same |
| 741 | * for caching purposes.</li> |
| 742 | * </ul> |
| 743 | * </p> |
| 744 | * |
| 745 | * @param uri The URI, using the content:// scheme, for the content to |
| 746 | * retrieve. |
| 747 | * @param projection A list of which columns to return. Passing null will |
| 748 | * return all columns, which is inefficient. |
| 749 | * @param selection A filter declaring which rows to return, formatted as an |
| 750 | * SQL WHERE clause (excluding the WHERE itself). Passing null will |
| 751 | * return all rows for the given URI. |
| 752 | * @param selectionArgs You may include ?s in selection, which will be |
| 753 | * replaced by the values from selectionArgs, in the order that they |
| 754 | * appear in the selection. The values will be bound as Strings. |
| 755 | * @param sortOrder How to order the rows, formatted as an SQL ORDER BY |
| 756 | * clause (excluding the ORDER BY itself). Passing null will use the |
| 757 | * default sort order, which may be unordered. |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 758 | * @param cancellationSignal A signal to cancel the operation in progress, or null if none. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 759 | * If the operation is canceled, then {@link OperationCanceledException} will be thrown |
| 760 | * when the query is executed. |
| 761 | * @return A Cursor object, which is positioned before the first entry, or null |
| 762 | * @see Cursor |
| 763 | */ |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 764 | public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 765 | @Nullable String[] projection, @Nullable String selection, |
| 766 | @Nullable String[] selectionArgs, @Nullable String sortOrder, |
| 767 | @Nullable CancellationSignal cancellationSignal) { |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 768 | Bundle queryArgs = createSqlQueryBundle(selection, selectionArgs, sortOrder); |
| 769 | return query(uri, projection, queryArgs, cancellationSignal); |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Query the given URI, returning a {@link Cursor} over the result set |
| 774 | * with support for cancellation. |
| 775 | * |
| 776 | * <p>For best performance, the caller should follow these guidelines: |
| 777 | * |
| 778 | * <li>Provide an explicit projection, to prevent reading data from storage |
| 779 | * that aren't going to be used. |
| 780 | * |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 781 | * Provider must identify which QUERY_ARG_SORT* arguments were honored during |
| 782 | * the preparation of the result set by including the respective argument keys |
| 783 | * in the {@link Cursor} extras {@link Bundle}. See {@link #EXTRA_HONORED_ARGS} |
| 784 | * for details. |
| 785 | * |
| 786 | * @see #QUERY_ARG_SORT_COLUMNS, #QUERY_ARG_SORT_DIRECTION, #QUERY_ARG_SORT_COLLATION. |
| 787 | * |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 788 | * @param uri The URI, using the content:// scheme, for the content to |
| 789 | * retrieve. |
| 790 | * @param projection A list of which columns to return. Passing null will |
| 791 | * return all columns, which is inefficient. |
| 792 | * @param queryArgs A Bundle containing any arguments to the query. |
| 793 | * @param cancellationSignal A signal to cancel the operation in progress, or null if none. |
| 794 | * If the operation is canceled, then {@link OperationCanceledException} will be thrown |
| 795 | * when the query is executed. |
| 796 | * @return A Cursor object, which is positioned before the first entry, or null |
| 797 | * @see Cursor |
| 798 | */ |
| 799 | public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri, |
| 800 | @Nullable String[] projection, @Nullable Bundle queryArgs, |
| 801 | @Nullable CancellationSignal cancellationSignal) { |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 802 | Preconditions.checkNotNull(uri, "uri"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 803 | IContentProvider unstableProvider = acquireUnstableProvider(uri); |
| 804 | if (unstableProvider == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | return null; |
| 806 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 807 | IContentProvider stableProvider = null; |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 808 | Cursor qCursor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | try { |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 810 | long startTime = SystemClock.uptimeMillis(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 811 | |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 812 | ICancellationSignal remoteCancellationSignal = null; |
| 813 | if (cancellationSignal != null) { |
| 814 | cancellationSignal.throwIfCanceled(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 815 | remoteCancellationSignal = unstableProvider.createCancellationSignal(); |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 816 | cancellationSignal.setRemote(remoteCancellationSignal); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 817 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 818 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 819 | qCursor = unstableProvider.query(mPackageName, uri, projection, |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 820 | queryArgs, remoteCancellationSignal); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 821 | } catch (DeadObjectException e) { |
| 822 | // The remote process has died... but we only hold an unstable |
| 823 | // reference though, so we might recover!!! Let's try!!!! |
| 824 | // This is exciting!!1!!1!!!!1 |
| 825 | unstableProviderDied(unstableProvider); |
| 826 | stableProvider = acquireProvider(uri); |
| 827 | if (stableProvider == null) { |
| 828 | return null; |
| 829 | } |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 830 | qCursor = stableProvider.query( |
| 831 | mPackageName, uri, projection, queryArgs, remoteCancellationSignal); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 832 | } |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 833 | if (qCursor == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | return null; |
| 835 | } |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 836 | |
| 837 | // Force query execution. Might fail and throw a runtime exception here. |
Vasu Nori | 020e534 | 2010-04-28 14:22:38 -0700 | [diff] [blame] | 838 | qCursor.getCount(); |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 839 | long durationMillis = SystemClock.uptimeMillis() - startTime; |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 840 | maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs); |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 841 | |
| 842 | // Wrap the cursor object into CursorWrapperInner object. |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 843 | final IContentProvider provider = (stableProvider != null) ? stableProvider |
| 844 | : acquireProvider(uri); |
| 845 | final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 846 | stableProvider = null; |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 847 | qCursor = null; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 848 | return wrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } catch (RemoteException e) { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 850 | // Arbitrary and not worth documenting, as Activity |
| 851 | // Manager will kill this process shortly anyway. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | return null; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 853 | } finally { |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 854 | if (qCursor != null) { |
| 855 | qCursor.close(); |
| 856 | } |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 857 | if (cancellationSignal != null) { |
| 858 | cancellationSignal.setRemote(null); |
| 859 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 860 | if (unstableProvider != null) { |
| 861 | releaseUnstableProvider(unstableProvider); |
| 862 | } |
| 863 | if (stableProvider != null) { |
| 864 | releaseProvider(stableProvider); |
| 865 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 869 | /** |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 870 | * Transform the given <var>url</var> to a canonical representation of |
| 871 | * its referenced resource, which can be used across devices, persisted, |
| 872 | * backed up and restored, etc. The returned Uri is still a fully capable |
| 873 | * Uri for use with its content provider, allowing you to do all of the |
| 874 | * same content provider operations as with the original Uri -- |
| 875 | * {@link #query}, {@link #openInputStream(android.net.Uri)}, etc. The |
| 876 | * only difference in behavior between the original and new Uris is that |
| 877 | * the content provider may need to do some additional work at each call |
| 878 | * using it to resolve it to the correct resource, especially if the |
| 879 | * canonical Uri has been moved to a different environment. |
| 880 | * |
| 881 | * <p>If you are moving a canonical Uri between environments, you should |
| 882 | * perform another call to {@link #canonicalize} with that original Uri to |
| 883 | * re-canonicalize it for the current environment. Alternatively, you may |
| 884 | * want to use {@link #uncanonicalize} to transform it to a non-canonical |
| 885 | * Uri that works only in the current environment but potentially more |
| 886 | * efficiently than the canonical representation.</p> |
| 887 | * |
| 888 | * @param url The {@link Uri} that is to be transformed to a canonical |
| 889 | * representation. Like all resolver calls, the input can be either |
| 890 | * a non-canonical or canonical Uri. |
| 891 | * |
| 892 | * @return Returns the official canonical representation of <var>url</var>, |
| 893 | * or null if the content provider does not support a canonical representation |
| 894 | * of the given Uri. Many providers may not support canonicalization of some |
| 895 | * or all of their Uris. |
| 896 | * |
| 897 | * @see #uncanonicalize |
| 898 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 899 | public final @Nullable Uri canonicalize(@NonNull Uri url) { |
| 900 | Preconditions.checkNotNull(url, "url"); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 901 | IContentProvider provider = acquireProvider(url); |
| 902 | if (provider == null) { |
| 903 | return null; |
| 904 | } |
| 905 | |
| 906 | try { |
| 907 | return provider.canonicalize(mPackageName, url); |
| 908 | } catch (RemoteException e) { |
| 909 | // Arbitrary and not worth documenting, as Activity |
| 910 | // Manager will kill this process shortly anyway. |
| 911 | return null; |
| 912 | } finally { |
| 913 | releaseProvider(provider); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Given a canonical Uri previously generated by {@link #canonicalize}, convert |
| 919 | * it to its local non-canonical form. This can be useful in some cases where |
| 920 | * you know that you will only be using the Uri in the current environment and |
| 921 | * want to avoid any possible overhead when using it with the content |
Dianne Hackborn | b3ac67a | 2013-09-11 11:02:24 -0700 | [diff] [blame] | 922 | * provider or want to verify that the referenced data exists at all in the |
| 923 | * new environment. |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 924 | * |
| 925 | * @param url The canonical {@link Uri} that is to be convered back to its |
| 926 | * non-canonical form. |
| 927 | * |
Dianne Hackborn | b3ac67a | 2013-09-11 11:02:24 -0700 | [diff] [blame] | 928 | * @return Returns the non-canonical representation of <var>url</var>. This will |
| 929 | * return null if data identified by the canonical Uri can not be found in |
| 930 | * the current environment; callers must always check for null and deal with |
| 931 | * that by appropriately falling back to an alternative. |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 932 | * |
| 933 | * @see #canonicalize |
| 934 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 935 | public final @Nullable Uri uncanonicalize(@NonNull Uri url) { |
| 936 | Preconditions.checkNotNull(url, "url"); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 937 | IContentProvider provider = acquireProvider(url); |
| 938 | if (provider == null) { |
| 939 | return null; |
| 940 | } |
| 941 | |
| 942 | try { |
| 943 | return provider.uncanonicalize(mPackageName, url); |
| 944 | } catch (RemoteException e) { |
| 945 | // Arbitrary and not worth documenting, as Activity |
| 946 | // Manager will kill this process shortly anyway. |
| 947 | return null; |
| 948 | } finally { |
| 949 | releaseProvider(provider); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /** |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 954 | * This allows clients to request an explicit refresh of content identified by {@code uri}. |
| 955 | * <p> |
| 956 | * Client code should only invoke this method when there is a strong indication (such as a user |
| 957 | * initiated pull to refresh gesture) that the content is stale. |
| 958 | * <p> |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 959 | * |
Ben Lin | 2b64a88 | 2016-11-11 15:24:58 -0800 | [diff] [blame] | 960 | * @param url The Uri identifying the data to refresh. |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 961 | * @param args Additional options from the client. The definitions of these are specific to the |
| 962 | * content provider being called. |
| 963 | * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if |
| 964 | * none. For example, if you called refresh on a particular uri, you should call |
| 965 | * {@link CancellationSignal#throwIfCanceled()} to check whether the client has |
| 966 | * canceled the refresh request. |
| 967 | * @return true if the provider actually tried refreshing. |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 968 | */ |
| 969 | public final boolean refresh(@NonNull Uri url, @Nullable Bundle args, |
| 970 | @Nullable CancellationSignal cancellationSignal) { |
| 971 | Preconditions.checkNotNull(url, "url"); |
| 972 | IContentProvider provider = acquireProvider(url); |
| 973 | if (provider == null) { |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | try { |
| 978 | ICancellationSignal remoteCancellationSignal = null; |
| 979 | if (cancellationSignal != null) { |
| 980 | cancellationSignal.throwIfCanceled(); |
| 981 | remoteCancellationSignal = provider.createCancellationSignal(); |
| 982 | cancellationSignal.setRemote(remoteCancellationSignal); |
| 983 | } |
| 984 | return provider.refresh(mPackageName, url, args, remoteCancellationSignal); |
| 985 | } catch (RemoteException e) { |
| 986 | // Arbitrary and not worth documenting, as Activity |
| 987 | // Manager will kill this process shortly anyway. |
| 988 | return false; |
| 989 | } finally { |
| 990 | releaseProvider(provider); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | * Open a stream on to the content associated with a content URI. If there |
| 996 | * is no data associated with the URI, FileNotFoundException is thrown. |
| 997 | * |
| 998 | * <h5>Accepts the following URI schemes:</h5> |
| 999 | * <ul> |
| 1000 | * <li>content ({@link #SCHEME_CONTENT})</li> |
| 1001 | * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li> |
| 1002 | * <li>file ({@link #SCHEME_FILE})</li> |
| 1003 | * </ul> |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1004 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information |
| 1006 | * on these schemes. |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1007 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | * @param uri The desired URI. |
| 1009 | * @return InputStream |
| 1010 | * @throws FileNotFoundException if the provided URI could not be opened. |
| 1011 | * @see #openAssetFileDescriptor(Uri, String) |
| 1012 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1013 | public final @Nullable InputStream openInputStream(@NonNull Uri uri) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | throws FileNotFoundException { |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1015 | Preconditions.checkNotNull(uri, "uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | String scheme = uri.getScheme(); |
| 1017 | if (SCHEME_ANDROID_RESOURCE.equals(scheme)) { |
| 1018 | // Note: left here to avoid breaking compatibility. May be removed |
| 1019 | // with sufficient testing. |
| 1020 | OpenResourceIdResult r = getResourceId(uri); |
| 1021 | try { |
| 1022 | InputStream stream = r.r.openRawResource(r.id); |
| 1023 | return stream; |
| 1024 | } catch (Resources.NotFoundException ex) { |
| 1025 | throw new FileNotFoundException("Resource does not exist: " + uri); |
| 1026 | } |
| 1027 | } else if (SCHEME_FILE.equals(scheme)) { |
| 1028 | // Note: left here to avoid breaking compatibility. May be removed |
| 1029 | // with sufficient testing. |
| 1030 | return new FileInputStream(uri.getPath()); |
| 1031 | } else { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1032 | AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | try { |
| 1034 | return fd != null ? fd.createInputStream() : null; |
| 1035 | } catch (IOException e) { |
| 1036 | throw new FileNotFoundException("Unable to create stream"); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * Synonym for {@link #openOutputStream(Uri, String) |
| 1043 | * openOutputStream(uri, "w")}. |
| 1044 | * @throws FileNotFoundException if the provided URI could not be opened. |
| 1045 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1046 | public final @Nullable OutputStream openOutputStream(@NonNull Uri uri) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | throws FileNotFoundException { |
| 1048 | return openOutputStream(uri, "w"); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Open a stream on to the content associated with a content URI. If there |
| 1053 | * is no data associated with the URI, FileNotFoundException is thrown. |
| 1054 | * |
| 1055 | * <h5>Accepts the following URI schemes:</h5> |
| 1056 | * <ul> |
| 1057 | * <li>content ({@link #SCHEME_CONTENT})</li> |
| 1058 | * <li>file ({@link #SCHEME_FILE})</li> |
| 1059 | * </ul> |
| 1060 | * |
| 1061 | * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information |
| 1062 | * on these schemes. |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1063 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | * @param uri The desired URI. |
| 1065 | * @param mode May be "w", "wa", "rw", or "rwt". |
| 1066 | * @return OutputStream |
| 1067 | * @throws FileNotFoundException if the provided URI could not be opened. |
| 1068 | * @see #openAssetFileDescriptor(Uri, String) |
| 1069 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1070 | public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1072 | AssetFileDescriptor fd = openAssetFileDescriptor(uri, mode, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | try { |
| 1074 | return fd != null ? fd.createOutputStream() : null; |
| 1075 | } catch (IOException e) { |
| 1076 | throw new FileNotFoundException("Unable to create stream"); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1081 | * Open a raw file descriptor to access data under a URI. This |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the |
| 1083 | * underlying {@link ContentProvider#openFile} |
| 1084 | * ContentProvider.openFile()} method, so will <em>not</em> work with |
| 1085 | * providers that return sub-sections of files. If at all possible, |
| 1086 | * you should use {@link #openAssetFileDescriptor(Uri, String)}. You |
| 1087 | * will receive a FileNotFoundException exception if the provider returns a |
| 1088 | * sub-section of a file. |
| 1089 | * |
| 1090 | * <h5>Accepts the following URI schemes:</h5> |
| 1091 | * <ul> |
| 1092 | * <li>content ({@link #SCHEME_CONTENT})</li> |
| 1093 | * <li>file ({@link #SCHEME_FILE})</li> |
| 1094 | * </ul> |
| 1095 | * |
| 1096 | * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information |
| 1097 | * on these schemes. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1098 | * <p> |
| 1099 | * If opening with the exclusive "r" or "w" modes, the returned |
| 1100 | * ParcelFileDescriptor could be a pipe or socket pair to enable streaming |
| 1101 | * of data. Opening with the "rw" mode implies a file on disk that supports |
| 1102 | * seeking. If possible, always use an exclusive mode to give the underlying |
| 1103 | * {@link ContentProvider} the most flexibility. |
| 1104 | * <p> |
| 1105 | * If you are writing a file, and need to communicate an error to the |
| 1106 | * provider, use {@link ParcelFileDescriptor#closeWithError(String)}. |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1107 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | * @param uri The desired URI to open. |
| 1109 | * @param mode The file mode to use, as per {@link ContentProvider#openFile |
| 1110 | * ContentProvider.openFile}. |
| 1111 | * @return Returns a new ParcelFileDescriptor pointing to the file. You |
| 1112 | * own this descriptor and are responsible for closing it when done. |
Yury Zhauniarovich | f9c5176 | 2012-05-12 18:39:25 +0200 | [diff] [blame] | 1113 | * @throws FileNotFoundException Throws FileNotFoundException if no |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | * file exists under the URI or the mode is invalid. |
| 1115 | * @see #openAssetFileDescriptor(Uri, String) |
| 1116 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1117 | public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri, |
| 1118 | @NonNull String mode) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1119 | return openFileDescriptor(uri, mode, null); |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * Open a raw file descriptor to access data under a URI. This |
| 1124 | * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the |
| 1125 | * underlying {@link ContentProvider#openFile} |
| 1126 | * ContentProvider.openFile()} method, so will <em>not</em> work with |
| 1127 | * providers that return sub-sections of files. If at all possible, |
| 1128 | * you should use {@link #openAssetFileDescriptor(Uri, String)}. You |
| 1129 | * will receive a FileNotFoundException exception if the provider returns a |
| 1130 | * sub-section of a file. |
| 1131 | * |
| 1132 | * <h5>Accepts the following URI schemes:</h5> |
| 1133 | * <ul> |
| 1134 | * <li>content ({@link #SCHEME_CONTENT})</li> |
| 1135 | * <li>file ({@link #SCHEME_FILE})</li> |
| 1136 | * </ul> |
| 1137 | * |
| 1138 | * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information |
| 1139 | * on these schemes. |
| 1140 | * <p> |
| 1141 | * If opening with the exclusive "r" or "w" modes, the returned |
| 1142 | * ParcelFileDescriptor could be a pipe or socket pair to enable streaming |
| 1143 | * of data. Opening with the "rw" mode implies a file on disk that supports |
| 1144 | * seeking. If possible, always use an exclusive mode to give the underlying |
| 1145 | * {@link ContentProvider} the most flexibility. |
| 1146 | * <p> |
| 1147 | * If you are writing a file, and need to communicate an error to the |
| 1148 | * provider, use {@link ParcelFileDescriptor#closeWithError(String)}. |
| 1149 | * |
| 1150 | * @param uri The desired URI to open. |
| 1151 | * @param mode The file mode to use, as per {@link ContentProvider#openFile |
| 1152 | * ContentProvider.openFile}. |
Ying Wang | 9436631 | 2013-08-23 22:20:03 -0700 | [diff] [blame] | 1153 | * @param cancellationSignal A signal to cancel the operation in progress, |
| 1154 | * or null if none. If the operation is canceled, then |
| 1155 | * {@link OperationCanceledException} will be thrown. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1156 | * @return Returns a new ParcelFileDescriptor pointing to the file. You |
| 1157 | * own this descriptor and are responsible for closing it when done. |
| 1158 | * @throws FileNotFoundException Throws FileNotFoundException if no |
| 1159 | * file exists under the URI or the mode is invalid. |
| 1160 | * @see #openAssetFileDescriptor(Uri, String) |
| 1161 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1162 | public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri, |
| 1163 | @NonNull String mode, @Nullable CancellationSignal cancellationSignal) |
| 1164 | throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1165 | AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | if (afd == null) { |
| 1167 | return null; |
| 1168 | } |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | if (afd.getDeclaredLength() < 0) { |
| 1171 | // This is a full file! |
| 1172 | return afd.getParcelFileDescriptor(); |
| 1173 | } |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | // Client can't handle a sub-section of a file, so close what |
| 1176 | // we got and bail with an exception. |
| 1177 | try { |
| 1178 | afd.close(); |
| 1179 | } catch (IOException e) { |
| 1180 | } |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | throw new FileNotFoundException("Not a whole file"); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1186 | * Open a raw file descriptor to access data under a URI. This |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | * interacts with the underlying {@link ContentProvider#openAssetFile} |
Gilles Debunne | 03f0292 | 2010-06-09 14:11:45 -0700 | [diff] [blame] | 1188 | * method of the provider associated with the given URI, to retrieve any file stored there. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | * |
| 1190 | * <h5>Accepts the following URI schemes:</h5> |
| 1191 | * <ul> |
| 1192 | * <li>content ({@link #SCHEME_CONTENT})</li> |
| 1193 | * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li> |
| 1194 | * <li>file ({@link #SCHEME_FILE})</li> |
| 1195 | * </ul> |
| 1196 | * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5> |
| 1197 | * <p> |
| 1198 | * A Uri object can be used to reference a resource in an APK file. The |
| 1199 | * Uri should be one of the following formats: |
| 1200 | * <ul> |
| 1201 | * <li><code>android.resource://package_name/id_number</code><br/> |
| 1202 | * <code>package_name</code> is your package name as listed in your AndroidManifest.xml. |
| 1203 | * For example <code>com.example.myapp</code><br/> |
| 1204 | * <code>id_number</code> is the int form of the ID.<br/> |
| 1205 | * The easiest way to construct this form is |
| 1206 | * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre> |
| 1207 | * </li> |
| 1208 | * <li><code>android.resource://package_name/type/name</code><br/> |
| 1209 | * <code>package_name</code> is your package name as listed in your AndroidManifest.xml. |
| 1210 | * For example <code>com.example.myapp</code><br/> |
| 1211 | * <code>type</code> is the string form of the resource type. For example, <code>raw</code> |
| 1212 | * or <code>drawable</code>. |
| 1213 | * <code>name</code> is the string form of the resource name. That is, whatever the file |
| 1214 | * name was in your res directory, without the type extension. |
| 1215 | * The easiest way to construct this form is |
| 1216 | * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre> |
| 1217 | * </li> |
| 1218 | * </ul> |
| 1219 | * |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1220 | * <p>Note that if this function is called for read-only input (mode is "r") |
| 1221 | * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor} |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1222 | * for you with a MIME type of "*/*". This allows such callers to benefit |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1223 | * from any built-in data conversion that a provider implements. |
| 1224 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | * @param uri The desired URI to open. |
| 1226 | * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile |
| 1227 | * ContentProvider.openAssetFile}. |
| 1228 | * @return Returns a new ParcelFileDescriptor pointing to the file. You |
| 1229 | * own this descriptor and are responsible for closing it when done. |
| 1230 | * @throws FileNotFoundException Throws FileNotFoundException of no |
| 1231 | * file exists under the URI or the mode is invalid. |
| 1232 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1233 | public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri, |
| 1234 | @NonNull String mode) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1235 | return openAssetFileDescriptor(uri, mode, null); |
| 1236 | } |
| 1237 | |
| 1238 | /** |
| 1239 | * Open a raw file descriptor to access data under a URI. This |
| 1240 | * interacts with the underlying {@link ContentProvider#openAssetFile} |
| 1241 | * method of the provider associated with the given URI, to retrieve any file stored there. |
| 1242 | * |
| 1243 | * <h5>Accepts the following URI schemes:</h5> |
| 1244 | * <ul> |
| 1245 | * <li>content ({@link #SCHEME_CONTENT})</li> |
| 1246 | * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li> |
| 1247 | * <li>file ({@link #SCHEME_FILE})</li> |
| 1248 | * </ul> |
| 1249 | * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5> |
| 1250 | * <p> |
| 1251 | * A Uri object can be used to reference a resource in an APK file. The |
| 1252 | * Uri should be one of the following formats: |
| 1253 | * <ul> |
| 1254 | * <li><code>android.resource://package_name/id_number</code><br/> |
| 1255 | * <code>package_name</code> is your package name as listed in your AndroidManifest.xml. |
| 1256 | * For example <code>com.example.myapp</code><br/> |
| 1257 | * <code>id_number</code> is the int form of the ID.<br/> |
| 1258 | * The easiest way to construct this form is |
| 1259 | * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre> |
| 1260 | * </li> |
| 1261 | * <li><code>android.resource://package_name/type/name</code><br/> |
| 1262 | * <code>package_name</code> is your package name as listed in your AndroidManifest.xml. |
| 1263 | * For example <code>com.example.myapp</code><br/> |
| 1264 | * <code>type</code> is the string form of the resource type. For example, <code>raw</code> |
| 1265 | * or <code>drawable</code>. |
| 1266 | * <code>name</code> is the string form of the resource name. That is, whatever the file |
| 1267 | * name was in your res directory, without the type extension. |
| 1268 | * The easiest way to construct this form is |
| 1269 | * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre> |
| 1270 | * </li> |
| 1271 | * </ul> |
| 1272 | * |
| 1273 | * <p>Note that if this function is called for read-only input (mode is "r") |
| 1274 | * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor} |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1275 | * for you with a MIME type of "*/*". This allows such callers to benefit |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1276 | * from any built-in data conversion that a provider implements. |
| 1277 | * |
| 1278 | * @param uri The desired URI to open. |
| 1279 | * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile |
| 1280 | * ContentProvider.openAssetFile}. |
Ying Wang | 9436631 | 2013-08-23 22:20:03 -0700 | [diff] [blame] | 1281 | * @param cancellationSignal A signal to cancel the operation in progress, or null if |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1282 | * none. If the operation is canceled, then |
| 1283 | * {@link OperationCanceledException} will be thrown. |
| 1284 | * @return Returns a new ParcelFileDescriptor pointing to the file. You |
| 1285 | * own this descriptor and are responsible for closing it when done. |
| 1286 | * @throws FileNotFoundException Throws FileNotFoundException of no |
| 1287 | * file exists under the URI or the mode is invalid. |
| 1288 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1289 | public final @Nullable AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri, |
| 1290 | @NonNull String mode, @Nullable CancellationSignal cancellationSignal) |
| 1291 | throws FileNotFoundException { |
| 1292 | Preconditions.checkNotNull(uri, "uri"); |
| 1293 | Preconditions.checkNotNull(mode, "mode"); |
| 1294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | String scheme = uri.getScheme(); |
| 1296 | if (SCHEME_ANDROID_RESOURCE.equals(scheme)) { |
| 1297 | if (!"r".equals(mode)) { |
| 1298 | throw new FileNotFoundException("Can't write resources: " + uri); |
| 1299 | } |
| 1300 | OpenResourceIdResult r = getResourceId(uri); |
| 1301 | try { |
| 1302 | return r.r.openRawResourceFd(r.id); |
| 1303 | } catch (Resources.NotFoundException ex) { |
| 1304 | throw new FileNotFoundException("Resource does not exist: " + uri); |
| 1305 | } |
| 1306 | } else if (SCHEME_FILE.equals(scheme)) { |
| 1307 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open( |
Adam Lesinski | eb8c3f9 | 2013-09-20 14:08:25 -0700 | [diff] [blame] | 1308 | new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | return new AssetFileDescriptor(pfd, 0, -1); |
| 1310 | } else { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1311 | if ("r".equals(mode)) { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1312 | return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1313 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1314 | IContentProvider unstableProvider = acquireUnstableProvider(uri); |
| 1315 | if (unstableProvider == null) { |
| 1316 | throw new FileNotFoundException("No content provider: " + uri); |
| 1317 | } |
| 1318 | IContentProvider stableProvider = null; |
| 1319 | AssetFileDescriptor fd = null; |
| 1320 | |
| 1321 | try { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1322 | ICancellationSignal remoteCancellationSignal = null; |
| 1323 | if (cancellationSignal != null) { |
| 1324 | cancellationSignal.throwIfCanceled(); |
| 1325 | remoteCancellationSignal = unstableProvider.createCancellationSignal(); |
| 1326 | cancellationSignal.setRemote(remoteCancellationSignal); |
| 1327 | } |
| 1328 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1329 | try { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1330 | fd = unstableProvider.openAssetFile( |
| 1331 | mPackageName, uri, mode, remoteCancellationSignal); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1332 | if (fd == null) { |
| 1333 | // The provider will be released by the finally{} clause |
| 1334 | return null; |
| 1335 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1336 | } catch (DeadObjectException e) { |
| 1337 | // The remote process has died... but we only hold an unstable |
| 1338 | // reference though, so we might recover!!! Let's try!!!! |
| 1339 | // This is exciting!!1!!1!!!!1 |
| 1340 | unstableProviderDied(unstableProvider); |
| 1341 | stableProvider = acquireProvider(uri); |
| 1342 | if (stableProvider == null) { |
| 1343 | throw new FileNotFoundException("No content provider: " + uri); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1344 | } |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1345 | fd = stableProvider.openAssetFile( |
| 1346 | mPackageName, uri, mode, remoteCancellationSignal); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1347 | if (fd == null) { |
| 1348 | // The provider will be released by the finally{} clause |
| 1349 | return null; |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1350 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1351 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1352 | |
| 1353 | if (stableProvider == null) { |
| 1354 | stableProvider = acquireProvider(uri); |
| 1355 | } |
| 1356 | releaseUnstableProvider(unstableProvider); |
Koji Fukui | f783c02c | 2014-02-07 15:01:36 +0900 | [diff] [blame] | 1357 | unstableProvider = null; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1358 | ParcelFileDescriptor pfd = new ParcelFileDescriptorInner( |
| 1359 | fd.getParcelFileDescriptor(), stableProvider); |
| 1360 | |
| 1361 | // Success! Don't release the provider when exiting, let |
| 1362 | // ParcelFileDescriptorInner do that when it is closed. |
| 1363 | stableProvider = null; |
| 1364 | |
| 1365 | return new AssetFileDescriptor(pfd, fd.getStartOffset(), |
| 1366 | fd.getDeclaredLength()); |
| 1367 | |
| 1368 | } catch (RemoteException e) { |
| 1369 | // Whatever, whatever, we'll go away. |
| 1370 | throw new FileNotFoundException( |
| 1371 | "Failed opening content provider: " + uri); |
| 1372 | } catch (FileNotFoundException e) { |
| 1373 | throw e; |
| 1374 | } finally { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1375 | if (cancellationSignal != null) { |
| 1376 | cancellationSignal.setRemote(null); |
| 1377 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1378 | if (stableProvider != null) { |
| 1379 | releaseProvider(stableProvider); |
| 1380 | } |
| 1381 | if (unstableProvider != null) { |
| 1382 | releaseUnstableProvider(unstableProvider); |
| 1383 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | /** |
| 1390 | * Open a raw file descriptor to access (potentially type transformed) |
| 1391 | * data from a "content:" URI. This interacts with the underlying |
| 1392 | * {@link ContentProvider#openTypedAssetFile} method of the provider |
| 1393 | * associated with the given URI, to retrieve retrieve any appropriate |
| 1394 | * data stream for the data stored there. |
| 1395 | * |
| 1396 | * <p>Unlike {@link #openAssetFileDescriptor}, this function only works |
| 1397 | * with "content:" URIs, because content providers are the only facility |
| 1398 | * with an associated MIME type to ensure that the returned data stream |
| 1399 | * is of the desired type. |
| 1400 | * |
| 1401 | * <p>All text/* streams are encoded in UTF-8. |
| 1402 | * |
| 1403 | * @param uri The desired URI to open. |
| 1404 | * @param mimeType The desired MIME type of the returned data. This can |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1405 | * be a pattern such as */*, which will allow the content provider to |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1406 | * select a type, though there is no way for you to determine what type |
| 1407 | * it is returning. |
| 1408 | * @param opts Additional provider-dependent options. |
| 1409 | * @return Returns a new ParcelFileDescriptor from which you can read the |
| 1410 | * data stream from the provider. Note that this may be a pipe, meaning |
| 1411 | * you can't seek in it. The only seek you should do is if the |
| 1412 | * AssetFileDescriptor contains an offset, to move to that offset before |
| 1413 | * reading. You own this descriptor and are responsible for closing it when done. |
| 1414 | * @throws FileNotFoundException Throws FileNotFoundException of no |
| 1415 | * data of the desired type exists under the URI. |
| 1416 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1417 | public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri, |
| 1418 | @NonNull String mimeType, @Nullable Bundle opts) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1419 | return openTypedAssetFileDescriptor(uri, mimeType, opts, null); |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Open a raw file descriptor to access (potentially type transformed) |
| 1424 | * data from a "content:" URI. This interacts with the underlying |
| 1425 | * {@link ContentProvider#openTypedAssetFile} method of the provider |
| 1426 | * associated with the given URI, to retrieve retrieve any appropriate |
| 1427 | * data stream for the data stored there. |
| 1428 | * |
| 1429 | * <p>Unlike {@link #openAssetFileDescriptor}, this function only works |
| 1430 | * with "content:" URIs, because content providers are the only facility |
| 1431 | * with an associated MIME type to ensure that the returned data stream |
| 1432 | * is of the desired type. |
| 1433 | * |
| 1434 | * <p>All text/* streams are encoded in UTF-8. |
| 1435 | * |
| 1436 | * @param uri The desired URI to open. |
| 1437 | * @param mimeType The desired MIME type of the returned data. This can |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1438 | * be a pattern such as */*, which will allow the content provider to |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1439 | * select a type, though there is no way for you to determine what type |
| 1440 | * it is returning. |
| 1441 | * @param opts Additional provider-dependent options. |
Ying Wang | 9436631 | 2013-08-23 22:20:03 -0700 | [diff] [blame] | 1442 | * @param cancellationSignal A signal to cancel the operation in progress, |
| 1443 | * or null if none. If the operation is canceled, then |
| 1444 | * {@link OperationCanceledException} will be thrown. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1445 | * @return Returns a new ParcelFileDescriptor from which you can read the |
| 1446 | * data stream from the provider. Note that this may be a pipe, meaning |
| 1447 | * you can't seek in it. The only seek you should do is if the |
| 1448 | * AssetFileDescriptor contains an offset, to move to that offset before |
| 1449 | * reading. You own this descriptor and are responsible for closing it when done. |
| 1450 | * @throws FileNotFoundException Throws FileNotFoundException of no |
| 1451 | * data of the desired type exists under the URI. |
| 1452 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1453 | public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri, |
| 1454 | @NonNull String mimeType, @Nullable Bundle opts, |
| 1455 | @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException { |
| 1456 | Preconditions.checkNotNull(uri, "uri"); |
| 1457 | Preconditions.checkNotNull(mimeType, "mimeType"); |
| 1458 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1459 | IContentProvider unstableProvider = acquireUnstableProvider(uri); |
| 1460 | if (unstableProvider == null) { |
| 1461 | throw new FileNotFoundException("No content provider: " + uri); |
| 1462 | } |
| 1463 | IContentProvider stableProvider = null; |
| 1464 | AssetFileDescriptor fd = null; |
| 1465 | |
| 1466 | try { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1467 | ICancellationSignal remoteCancellationSignal = null; |
| 1468 | if (cancellationSignal != null) { |
| 1469 | cancellationSignal.throwIfCanceled(); |
| 1470 | remoteCancellationSignal = unstableProvider.createCancellationSignal(); |
| 1471 | cancellationSignal.setRemote(remoteCancellationSignal); |
| 1472 | } |
| 1473 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1474 | try { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1475 | fd = unstableProvider.openTypedAssetFile( |
| 1476 | mPackageName, uri, mimeType, opts, remoteCancellationSignal); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1477 | if (fd == null) { |
| 1478 | // The provider will be released by the finally{} clause |
| 1479 | return null; |
| 1480 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1481 | } catch (DeadObjectException e) { |
| 1482 | // The remote process has died... but we only hold an unstable |
| 1483 | // reference though, so we might recover!!! Let's try!!!! |
| 1484 | // This is exciting!!1!!1!!!!1 |
| 1485 | unstableProviderDied(unstableProvider); |
| 1486 | stableProvider = acquireProvider(uri); |
| 1487 | if (stableProvider == null) { |
| 1488 | throw new FileNotFoundException("No content provider: " + uri); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1489 | } |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1490 | fd = stableProvider.openTypedAssetFile( |
| 1491 | mPackageName, uri, mimeType, opts, remoteCancellationSignal); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1492 | if (fd == null) { |
| 1493 | // The provider will be released by the finally{} clause |
| 1494 | return null; |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1495 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1497 | |
| 1498 | if (stableProvider == null) { |
| 1499 | stableProvider = acquireProvider(uri); |
| 1500 | } |
| 1501 | releaseUnstableProvider(unstableProvider); |
Koji Fukui | f783c02c | 2014-02-07 15:01:36 +0900 | [diff] [blame] | 1502 | unstableProvider = null; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1503 | ParcelFileDescriptor pfd = new ParcelFileDescriptorInner( |
| 1504 | fd.getParcelFileDescriptor(), stableProvider); |
| 1505 | |
| 1506 | // Success! Don't release the provider when exiting, let |
| 1507 | // ParcelFileDescriptorInner do that when it is closed. |
| 1508 | stableProvider = null; |
| 1509 | |
| 1510 | return new AssetFileDescriptor(pfd, fd.getStartOffset(), |
| 1511 | fd.getDeclaredLength()); |
| 1512 | |
| 1513 | } catch (RemoteException e) { |
| 1514 | // Whatever, whatever, we'll go away. |
| 1515 | throw new FileNotFoundException( |
| 1516 | "Failed opening content provider: " + uri); |
| 1517 | } catch (FileNotFoundException e) { |
| 1518 | throw e; |
| 1519 | } finally { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1520 | if (cancellationSignal != null) { |
| 1521 | cancellationSignal.setRemote(null); |
| 1522 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1523 | if (stableProvider != null) { |
| 1524 | releaseProvider(stableProvider); |
| 1525 | } |
| 1526 | if (unstableProvider != null) { |
| 1527 | releaseUnstableProvider(unstableProvider); |
| 1528 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
Bjorn Bringert | 4c87a3f | 2009-09-16 15:59:37 +0100 | [diff] [blame] | 1532 | /** |
| 1533 | * A resource identified by the {@link Resources} that contains it, and a resource id. |
| 1534 | * |
| 1535 | * @hide |
| 1536 | */ |
| 1537 | public class OpenResourceIdResult { |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1538 | @UnsupportedAppUsage |
Bjorn Bringert | 4c87a3f | 2009-09-16 15:59:37 +0100 | [diff] [blame] | 1539 | public Resources r; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1540 | @UnsupportedAppUsage |
Bjorn Bringert | 4c87a3f | 2009-09-16 15:59:37 +0100 | [diff] [blame] | 1541 | public int id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | } |
Bjorn Bringert | 4c87a3f | 2009-09-16 15:59:37 +0100 | [diff] [blame] | 1543 | |
| 1544 | /** |
| 1545 | * Resolves an android.resource URI to a {@link Resources} and a resource id. |
| 1546 | * |
| 1547 | * @hide |
| 1548 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1549 | @UnsupportedAppUsage |
Bjorn Bringert | 4c87a3f | 2009-09-16 15:59:37 +0100 | [diff] [blame] | 1550 | public OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | String authority = uri.getAuthority(); |
| 1552 | Resources r; |
| 1553 | if (TextUtils.isEmpty(authority)) { |
| 1554 | throw new FileNotFoundException("No authority: " + uri); |
| 1555 | } else { |
| 1556 | try { |
| 1557 | r = mContext.getPackageManager().getResourcesForApplication(authority); |
| 1558 | } catch (NameNotFoundException ex) { |
| 1559 | throw new FileNotFoundException("No package found for authority: " + uri); |
| 1560 | } |
| 1561 | } |
| 1562 | List<String> path = uri.getPathSegments(); |
| 1563 | if (path == null) { |
| 1564 | throw new FileNotFoundException("No path: " + uri); |
| 1565 | } |
| 1566 | int len = path.size(); |
| 1567 | int id; |
| 1568 | if (len == 1) { |
| 1569 | try { |
| 1570 | id = Integer.parseInt(path.get(0)); |
| 1571 | } catch (NumberFormatException e) { |
| 1572 | throw new FileNotFoundException("Single path segment is not a resource ID: " + uri); |
| 1573 | } |
| 1574 | } else if (len == 2) { |
| 1575 | id = r.getIdentifier(path.get(1), path.get(0), authority); |
| 1576 | } else { |
| 1577 | throw new FileNotFoundException("More than two path segments: " + uri); |
| 1578 | } |
| 1579 | if (id == 0) { |
| 1580 | throw new FileNotFoundException("No resource found for: " + uri); |
| 1581 | } |
| 1582 | OpenResourceIdResult res = new OpenResourceIdResult(); |
| 1583 | res.r = r; |
| 1584 | res.id = id; |
| 1585 | return res; |
| 1586 | } |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 1587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | /** |
| 1589 | * Inserts a row into a table at the given URL. |
| 1590 | * |
| 1591 | * If the content provider supports transactions the insertion will be atomic. |
| 1592 | * |
| 1593 | * @param url The URL of the table to insert into. |
| 1594 | * @param values The initial values for the newly inserted row. The key is the column name for |
| 1595 | * the field. Passing an empty ContentValues will create an empty row. |
| 1596 | * @return the URL of the newly created row. |
| 1597 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 1598 | public final @Nullable Uri insert(@RequiresPermission.Write @NonNull Uri url, |
| 1599 | @Nullable ContentValues values) { |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1600 | Preconditions.checkNotNull(url, "url"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | IContentProvider provider = acquireProvider(url); |
| 1602 | if (provider == null) { |
| 1603 | throw new IllegalArgumentException("Unknown URL " + url); |
| 1604 | } |
| 1605 | try { |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1606 | long startTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1607 | Uri createdRow = provider.insert(mPackageName, url, values); |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1608 | long durationMillis = SystemClock.uptimeMillis() - startTime; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 1609 | maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */); |
| 1610 | return createdRow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | } catch (RemoteException e) { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1612 | // Arbitrary and not worth documenting, as Activity |
| 1613 | // Manager will kill this process shortly anyway. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | return null; |
| 1615 | } finally { |
| 1616 | releaseProvider(provider); |
| 1617 | } |
| 1618 | } |
| 1619 | |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 1620 | /** |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 1621 | * Applies each of the {@link ContentProviderOperation} objects and returns an array |
| 1622 | * of their results. Passes through OperationApplicationException, which may be thrown |
| 1623 | * by the call to {@link ContentProviderOperation#apply}. |
| 1624 | * If all the applications succeed then a {@link ContentProviderResult} array with the |
| 1625 | * same number of elements as the operations will be returned. It is implementation-specific |
| 1626 | * how many, if any, operations will have been successfully applied if a call to |
| 1627 | * apply results in a {@link OperationApplicationException}. |
| 1628 | * @param authority the authority of the ContentProvider to which this batch should be applied |
| 1629 | * @param operations the operations to apply |
| 1630 | * @return the results of the applications |
| 1631 | * @throws OperationApplicationException thrown if an application fails. |
| 1632 | * See {@link ContentProviderOperation#apply} for more information. |
| 1633 | * @throws RemoteException thrown if a RemoteException is encountered while attempting |
| 1634 | * to communicate with a remote provider. |
| 1635 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1636 | public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority, |
| 1637 | @NonNull ArrayList<ContentProviderOperation> operations) |
| 1638 | throws RemoteException, OperationApplicationException { |
| 1639 | Preconditions.checkNotNull(authority, "authority"); |
| 1640 | Preconditions.checkNotNull(operations, "operations"); |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 1641 | ContentProviderClient provider = acquireContentProviderClient(authority); |
Fred Quintana | 6a8d5332f | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 1642 | if (provider == null) { |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 1643 | throw new IllegalArgumentException("Unknown authority " + authority); |
Fred Quintana | 6a8d5332f | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 1644 | } |
| 1645 | try { |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 1646 | return provider.applyBatch(operations); |
Fred Quintana | 6a8d5332f | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 1647 | } finally { |
| 1648 | provider.release(); |
| 1649 | } |
| 1650 | } |
| 1651 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | /** |
| 1653 | * Inserts multiple rows into a table at the given URL. |
| 1654 | * |
| 1655 | * This function make no guarantees about the atomicity of the insertions. |
| 1656 | * |
| 1657 | * @param url The URL of the table to insert into. |
| 1658 | * @param values The initial values for the newly inserted rows. The key is the column name for |
| 1659 | * the field. Passing null will create an empty row. |
| 1660 | * @return the number of newly created rows. |
| 1661 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 1662 | public final int bulkInsert(@RequiresPermission.Write @NonNull Uri url, |
| 1663 | @NonNull ContentValues[] values) { |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1664 | Preconditions.checkNotNull(url, "url"); |
| 1665 | Preconditions.checkNotNull(values, "values"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | IContentProvider provider = acquireProvider(url); |
| 1667 | if (provider == null) { |
| 1668 | throw new IllegalArgumentException("Unknown URL " + url); |
| 1669 | } |
| 1670 | try { |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1671 | long startTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1672 | int rowsCreated = provider.bulkInsert(mPackageName, url, values); |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1673 | long durationMillis = SystemClock.uptimeMillis() - startTime; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 1674 | maybeLogUpdateToEventLog(durationMillis, url, "bulkinsert", null /* where */); |
| 1675 | return rowsCreated; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1676 | } catch (RemoteException e) { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1677 | // Arbitrary and not worth documenting, as Activity |
| 1678 | // Manager will kill this process shortly anyway. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1679 | return 0; |
| 1680 | } finally { |
| 1681 | releaseProvider(provider); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * Deletes row(s) specified by a content URI. |
| 1687 | * |
| 1688 | * If the content provider supports transactions, the deletion will be atomic. |
| 1689 | * |
| 1690 | * @param url The URL of the row to delete. |
| 1691 | * @param where A filter to apply to rows before deleting, formatted as an SQL WHERE clause |
| 1692 | (excluding the WHERE itself). |
| 1693 | * @return The number of rows deleted. |
| 1694 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 1695 | public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where, |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1696 | @Nullable String[] selectionArgs) { |
| 1697 | Preconditions.checkNotNull(url, "url"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | IContentProvider provider = acquireProvider(url); |
| 1699 | if (provider == null) { |
| 1700 | throw new IllegalArgumentException("Unknown URL " + url); |
| 1701 | } |
| 1702 | try { |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1703 | long startTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1704 | int rowsDeleted = provider.delete(mPackageName, url, where, selectionArgs); |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1705 | long durationMillis = SystemClock.uptimeMillis() - startTime; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 1706 | maybeLogUpdateToEventLog(durationMillis, url, "delete", where); |
| 1707 | return rowsDeleted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | } catch (RemoteException e) { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1709 | // Arbitrary and not worth documenting, as Activity |
| 1710 | // Manager will kill this process shortly anyway. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | return -1; |
| 1712 | } finally { |
| 1713 | releaseProvider(provider); |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * Update row(s) in a content URI. |
| 1719 | * |
| 1720 | * If the content provider supports transactions the update will be atomic. |
| 1721 | * |
| 1722 | * @param uri The URI to modify. |
| 1723 | * @param values The new field values. The key is the column name for the field. |
| 1724 | A null value will remove an existing field value. |
Omari Stephens | d2a2daa | 2010-03-10 18:53:54 -0800 | [diff] [blame] | 1725 | * @param where A filter to apply to rows before updating, formatted as an SQL WHERE clause |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | (excluding the WHERE itself). |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1727 | * @return the number of rows updated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | * @throws NullPointerException if uri or values are null |
| 1729 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 1730 | public final int update(@RequiresPermission.Write @NonNull Uri uri, |
| 1731 | @Nullable ContentValues values, @Nullable String where, |
| 1732 | @Nullable String[] selectionArgs) { |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1733 | Preconditions.checkNotNull(uri, "uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | IContentProvider provider = acquireProvider(uri); |
| 1735 | if (provider == null) { |
| 1736 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 1737 | } |
| 1738 | try { |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1739 | long startTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1740 | int rowsUpdated = provider.update(mPackageName, uri, values, where, selectionArgs); |
Brad Fitzpatrick | d72f718 | 2010-02-11 17:07:51 -0800 | [diff] [blame] | 1741 | long durationMillis = SystemClock.uptimeMillis() - startTime; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 1742 | maybeLogUpdateToEventLog(durationMillis, uri, "update", where); |
| 1743 | return rowsUpdated; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | } catch (RemoteException e) { |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1745 | // Arbitrary and not worth documenting, as Activity |
| 1746 | // Manager will kill this process shortly anyway. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1747 | return -1; |
| 1748 | } finally { |
| 1749 | releaseProvider(provider); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 1754 | * Call a provider-defined method. This can be used to implement |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1755 | * read or write interfaces which are cheaper than using a Cursor and/or |
| 1756 | * do not fit into the traditional table model. |
| 1757 | * |
| 1758 | * @param method provider-defined method name to call. Opaque to |
| 1759 | * framework, but must be non-null. |
| 1760 | * @param arg provider-defined String argument. May be null. |
| 1761 | * @param extras provider-defined Bundle argument. May be null. |
| 1762 | * @return a result Bundle, possibly null. Will be null if the ContentProvider |
| 1763 | * does not implement call. |
| 1764 | * @throws NullPointerException if uri or method is null |
| 1765 | * @throws IllegalArgumentException if uri is not known |
| 1766 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1767 | public final @Nullable Bundle call(@NonNull Uri uri, @NonNull String method, |
| 1768 | @Nullable String arg, @Nullable Bundle extras) { |
| 1769 | Preconditions.checkNotNull(uri, "uri"); |
| 1770 | Preconditions.checkNotNull(method, "method"); |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1771 | IContentProvider provider = acquireProvider(uri); |
| 1772 | if (provider == null) { |
| 1773 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 1774 | } |
| 1775 | try { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1776 | final Bundle res = provider.call(mPackageName, method, arg, extras); |
| 1777 | Bundle.setDefusable(res, true); |
| 1778 | return res; |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1779 | } catch (RemoteException e) { |
| 1780 | // Arbitrary and not worth documenting, as Activity |
| 1781 | // Manager will kill this process shortly anyway. |
| 1782 | return null; |
| 1783 | } finally { |
| 1784 | releaseProvider(provider); |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | /** |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1789 | * Returns the content provider for the given content URI. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | * |
| 1791 | * @param uri The URI to a content provider |
| 1792 | * @return The ContentProvider for the given URI, or null if no content provider is found. |
| 1793 | * @hide |
| 1794 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1795 | @UnsupportedAppUsage |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1796 | public final IContentProvider acquireProvider(Uri uri) { |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 1797 | if (!SCHEME_CONTENT.equals(uri.getScheme())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | return null; |
| 1799 | } |
Yury Zhauniarovich | f9c5176 | 2012-05-12 18:39:25 +0200 | [diff] [blame] | 1800 | final String auth = uri.getAuthority(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | if (auth != null) { |
Yury Zhauniarovich | f9c5176 | 2012-05-12 18:39:25 +0200 | [diff] [blame] | 1802 | return acquireProvider(mContext, auth); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1803 | } |
| 1804 | return null; |
| 1805 | } |
| 1806 | |
| 1807 | /** |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1808 | * Returns the content provider for the given content URI if the process |
| 1809 | * already has a reference on it. |
| 1810 | * |
| 1811 | * @param uri The URI to a content provider |
| 1812 | * @return The ContentProvider for the given URI, or null if no content provider is found. |
| 1813 | * @hide |
| 1814 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1815 | @UnsupportedAppUsage |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1816 | public final IContentProvider acquireExistingProvider(Uri uri) { |
| 1817 | if (!SCHEME_CONTENT.equals(uri.getScheme())) { |
| 1818 | return null; |
| 1819 | } |
Yury Zhauniarovich | f9c5176 | 2012-05-12 18:39:25 +0200 | [diff] [blame] | 1820 | final String auth = uri.getAuthority(); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1821 | if (auth != null) { |
Yury Zhauniarovich | f9c5176 | 2012-05-12 18:39:25 +0200 | [diff] [blame] | 1822 | return acquireExistingProvider(mContext, auth); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1823 | } |
| 1824 | return null; |
| 1825 | } |
| 1826 | |
| 1827 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1828 | * @hide |
| 1829 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1830 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1831 | public final IContentProvider acquireProvider(String name) { |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 1832 | if (name == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | return null; |
| 1834 | } |
| 1835 | return acquireProvider(mContext, name); |
| 1836 | } |
| 1837 | |
| 1838 | /** |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1839 | * Returns the content provider for the given content URI. |
| 1840 | * |
| 1841 | * @param uri The URI to a content provider |
| 1842 | * @return The ContentProvider for the given URI, or null if no content provider is found. |
| 1843 | * @hide |
| 1844 | */ |
| 1845 | public final IContentProvider acquireUnstableProvider(Uri uri) { |
| 1846 | if (!SCHEME_CONTENT.equals(uri.getScheme())) { |
| 1847 | return null; |
| 1848 | } |
| 1849 | String auth = uri.getAuthority(); |
| 1850 | if (auth != null) { |
| 1851 | return acquireUnstableProvider(mContext, uri.getAuthority()); |
| 1852 | } |
| 1853 | return null; |
| 1854 | } |
| 1855 | |
| 1856 | /** |
| 1857 | * @hide |
| 1858 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1859 | @UnsupportedAppUsage |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1860 | public final IContentProvider acquireUnstableProvider(String name) { |
| 1861 | if (name == null) { |
| 1862 | return null; |
| 1863 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1864 | return acquireUnstableProvider(mContext, name); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /** |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1868 | * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider} |
| 1869 | * that services the content at uri, starting the provider if necessary. Returns |
| 1870 | * null if there is no provider associated wih the uri. The caller must indicate that they are |
| 1871 | * done with the provider by calling {@link ContentProviderClient#release} which will allow |
kopriva | 219f7dc | 2018-10-09 13:42:28 -0700 | [diff] [blame] | 1872 | * the system to release the provider if it determines that there is no other reason for |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1873 | * keeping it active. |
| 1874 | * @param uri specifies which provider should be acquired |
| 1875 | * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider} |
| 1876 | * that services the content at uri or null if there isn't one. |
| 1877 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1878 | public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull Uri uri) { |
| 1879 | Preconditions.checkNotNull(uri, "uri"); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1880 | IContentProvider provider = acquireProvider(uri); |
| 1881 | if (provider != null) { |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1882 | return new ContentProviderClient(this, provider, true); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1883 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1884 | return null; |
| 1885 | } |
| 1886 | |
| 1887 | /** |
| 1888 | * Returns a {@link ContentProviderClient} that is associated with the {@link ContentProvider} |
| 1889 | * with the authority of name, starting the provider if necessary. Returns |
| 1890 | * null if there is no provider associated wih the uri. The caller must indicate that they are |
| 1891 | * done with the provider by calling {@link ContentProviderClient#release} which will allow |
kopriva | 219f7dc | 2018-10-09 13:42:28 -0700 | [diff] [blame] | 1892 | * the system to release the provider if it determines that there is no other reason for |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1893 | * keeping it active. |
| 1894 | * @param name specifies which provider should be acquired |
| 1895 | * @return a {@link ContentProviderClient} that is associated with the {@link ContentProvider} |
| 1896 | * with the authority of name or null if there isn't one. |
| 1897 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1898 | public final @Nullable ContentProviderClient acquireContentProviderClient( |
| 1899 | @NonNull String name) { |
| 1900 | Preconditions.checkNotNull(name, "name"); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1901 | IContentProvider provider = acquireProvider(name); |
| 1902 | if (provider != null) { |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1903 | return new ContentProviderClient(this, provider, true); |
| 1904 | } |
| 1905 | |
| 1906 | return null; |
| 1907 | } |
| 1908 | |
| 1909 | /** |
| 1910 | * Like {@link #acquireContentProviderClient(Uri)}, but for use when you do |
| 1911 | * not trust the stability of the target content provider. This turns off |
| 1912 | * the mechanism in the platform clean up processes that are dependent on |
| 1913 | * a content provider if that content provider's process goes away. Normally |
| 1914 | * you can safely assume that once you have acquired a provider, you can freely |
| 1915 | * use it as needed and it won't disappear, even if your process is in the |
| 1916 | * background. If using this method, you need to take care to deal with any |
| 1917 | * failures when communicating with the provider, and be sure to close it |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1918 | * so that it can be re-opened later. In particular, catching a |
| 1919 | * {@link android.os.DeadObjectException} from the calls there will let you |
| 1920 | * know that the content provider has gone away; at that point the current |
| 1921 | * ContentProviderClient object is invalid, and you should release it. You |
| 1922 | * can acquire a new one if you would like to try to restart the provider |
| 1923 | * and perform new operations on it. |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1924 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1925 | public final @Nullable ContentProviderClient acquireUnstableContentProviderClient( |
| 1926 | @NonNull Uri uri) { |
| 1927 | Preconditions.checkNotNull(uri, "uri"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1928 | IContentProvider provider = acquireUnstableProvider(uri); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1929 | if (provider != null) { |
| 1930 | return new ContentProviderClient(this, provider, false); |
| 1931 | } |
| 1932 | |
| 1933 | return null; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * Like {@link #acquireContentProviderClient(String)}, but for use when you do |
| 1938 | * not trust the stability of the target content provider. This turns off |
| 1939 | * the mechanism in the platform clean up processes that are dependent on |
| 1940 | * a content provider if that content provider's process goes away. Normally |
| 1941 | * you can safely assume that once you have acquired a provider, you can freely |
| 1942 | * use it as needed and it won't disappear, even if your process is in the |
| 1943 | * background. If using this method, you need to take care to deal with any |
| 1944 | * failures when communicating with the provider, and be sure to close it |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1945 | * so that it can be re-opened later. In particular, catching a |
| 1946 | * {@link android.os.DeadObjectException} from the calls there will let you |
| 1947 | * know that the content provider has gone away; at that point the current |
| 1948 | * ContentProviderClient object is invalid, and you should release it. You |
| 1949 | * can acquire a new one if you would like to try to restart the provider |
| 1950 | * and perform new operations on it. |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1951 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1952 | public final @Nullable ContentProviderClient acquireUnstableContentProviderClient( |
| 1953 | @NonNull String name) { |
| 1954 | Preconditions.checkNotNull(name, "name"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1955 | IContentProvider provider = acquireUnstableProvider(name); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1956 | if (provider != null) { |
| 1957 | return new ContentProviderClient(this, provider, false); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | return null; |
| 1961 | } |
| 1962 | |
| 1963 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | * Register an observer class that gets callbacks when data identified by a |
| 1965 | * given content URI changes. |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 1966 | * <p> |
| 1967 | * Starting in {@link android.os.Build.VERSION_CODES#O}, all content |
| 1968 | * notifications must be backed by a valid {@link ContentProvider}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | * |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 1970 | * @param uri The URI to watch for changes. This can be a specific row URI, |
| 1971 | * or a base URI for a whole class of content. |
| 1972 | * @param notifyForDescendants When false, the observer will be notified |
| 1973 | * whenever a change occurs to the exact URI specified by |
| 1974 | * <code>uri</code> or to one of the URI's ancestors in the path |
| 1975 | * hierarchy. When true, the observer will also be notified |
| 1976 | * whenever a change occurs to the URI's descendants in the path |
| 1977 | * hierarchy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | * @param observer The object that receives callbacks when changes occur. |
| 1979 | * @see #unregisterContentObserver |
| 1980 | */ |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1981 | public final void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants, |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1982 | @NonNull ContentObserver observer) { |
| 1983 | Preconditions.checkNotNull(uri, "uri"); |
| 1984 | Preconditions.checkNotNull(observer, "observer"); |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 1985 | registerContentObserver( |
| 1986 | ContentProvider.getUriWithoutUserId(uri), |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1987 | notifyForDescendants, |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 1988 | observer, |
Sudheer Shanka | b4e2ddd | 2017-02-03 15:15:57 -0800 | [diff] [blame] | 1989 | ContentProvider.getUserIdFromUri(uri, mContext.getUserId())); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /** @hide - designated user version */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1993 | @UnsupportedAppUsage |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1994 | public final void registerContentObserver(Uri uri, boolean notifyForDescendents, |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 1995 | ContentObserver observer, @UserIdInt int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | try { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1997 | getContentService().registerContentObserver(uri, notifyForDescendents, |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 1998 | observer.getContentObserver(), userHandle, mTargetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2000 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | /** |
| 2005 | * Unregisters a change observer. |
| 2006 | * |
| 2007 | * @param observer The previously registered observer that is no longer needed. |
| 2008 | * @see #registerContentObserver |
| 2009 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2010 | public final void unregisterContentObserver(@NonNull ContentObserver observer) { |
| 2011 | Preconditions.checkNotNull(observer, "observer"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2012 | try { |
| 2013 | IContentObserver contentObserver = observer.releaseContentObserver(); |
| 2014 | if (contentObserver != null) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2015 | getContentService().unregisterContentObserver( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | contentObserver); |
| 2017 | } |
| 2018 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2019 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | /** |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2024 | * Notify registered observers that a row was updated and attempt to sync |
| 2025 | * changes to the network. |
| 2026 | * <p> |
| 2027 | * To observe events sent through this call, use |
| 2028 | * {@link #registerContentObserver(Uri, boolean, ContentObserver)}. |
| 2029 | * <p> |
| 2030 | * Starting in {@link android.os.Build.VERSION_CODES#O}, all content |
| 2031 | * notifications must be backed by a valid {@link ContentProvider}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | * |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 2033 | * @param uri The uri of the content that was changed. |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2034 | * @param observer The observer that originated the change, may be |
| 2035 | * <code>null</null>. The observer that originated the change |
| 2036 | * will only receive the notification if it has requested to |
| 2037 | * receive self-change notifications by implementing |
| 2038 | * {@link ContentObserver#deliverSelfNotifications()} to return |
| 2039 | * true. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2041 | public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2042 | notifyChange(uri, observer, true /* sync to network */); |
| 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * Notify registered observers that a row was updated. |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2047 | * <p> |
| 2048 | * To observe events sent through this call, use |
| 2049 | * {@link #registerContentObserver(Uri, boolean, ContentObserver)}. |
| 2050 | * <p> |
| 2051 | * If syncToNetwork is true, this will attempt to schedule a local sync |
| 2052 | * using the sync adapter that's registered for the authority of the |
| 2053 | * provided uri. No account will be passed to the sync adapter, so all |
| 2054 | * matching accounts will be synchronized. |
| 2055 | * <p> |
| 2056 | * Starting in {@link android.os.Build.VERSION_CODES#O}, all content |
| 2057 | * notifications must be backed by a valid {@link ContentProvider}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2058 | * |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 2059 | * @param uri The uri of the content that was changed. |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2060 | * @param observer The observer that originated the change, may be |
| 2061 | * <code>null</null>. The observer that originated the change |
| 2062 | * will only receive the notification if it has requested to |
| 2063 | * receive self-change notifications by implementing |
| 2064 | * {@link ContentObserver#deliverSelfNotifications()} to return |
| 2065 | * true. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2066 | * @param syncToNetwork If true, same as {@link #NOTIFY_SYNC_TO_NETWORK}. |
Steve Pomeroy | d7a1aad | 2012-01-18 16:15:59 -0500 | [diff] [blame] | 2067 | * @see #requestSync(android.accounts.Account, String, android.os.Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2068 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2069 | public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, |
| 2070 | boolean syncToNetwork) { |
| 2071 | Preconditions.checkNotNull(uri, "uri"); |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 2072 | notifyChange( |
| 2073 | ContentProvider.getUriWithoutUserId(uri), |
| 2074 | observer, |
| 2075 | syncToNetwork, |
Sudheer Shanka | b4e2ddd | 2017-02-03 15:15:57 -0800 | [diff] [blame] | 2076 | ContentProvider.getUserIdFromUri(uri, mContext.getUserId())); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /** |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2080 | * Notify registered observers that a row was updated. |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2081 | * <p> |
| 2082 | * To observe events sent through this call, use |
| 2083 | * {@link #registerContentObserver(Uri, boolean, ContentObserver)}. |
| 2084 | * <p> |
| 2085 | * If syncToNetwork is true, this will attempt to schedule a local sync |
| 2086 | * using the sync adapter that's registered for the authority of the |
| 2087 | * provided uri. No account will be passed to the sync adapter, so all |
| 2088 | * matching accounts will be synchronized. |
| 2089 | * <p> |
| 2090 | * Starting in {@link android.os.Build.VERSION_CODES#O}, all content |
| 2091 | * notifications must be backed by a valid {@link ContentProvider}. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2092 | * |
| 2093 | * @param uri The uri of the content that was changed. |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2094 | * @param observer The observer that originated the change, may be |
| 2095 | * <code>null</null>. The observer that originated the change |
| 2096 | * will only receive the notification if it has requested to |
| 2097 | * receive self-change notifications by implementing |
| 2098 | * {@link ContentObserver#deliverSelfNotifications()} to return |
| 2099 | * true. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2100 | * @param flags Additional flags: {@link #NOTIFY_SYNC_TO_NETWORK}. |
| 2101 | * @see #requestSync(android.accounts.Account, String, android.os.Bundle) |
| 2102 | */ |
| 2103 | public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, |
| 2104 | @NotifyFlags int flags) { |
| 2105 | Preconditions.checkNotNull(uri, "uri"); |
| 2106 | notifyChange( |
| 2107 | ContentProvider.getUriWithoutUserId(uri), |
| 2108 | observer, |
| 2109 | flags, |
Sudheer Shanka | b4e2ddd | 2017-02-03 15:15:57 -0800 | [diff] [blame] | 2110 | ContentProvider.getUserIdFromUri(uri, mContext.getUserId())); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | /** |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 2114 | * Notify registered observers within the designated user(s) that a row was updated. |
| 2115 | * |
| 2116 | * @hide |
| 2117 | */ |
Dianne Hackborn | e761777 | 2016-04-27 17:03:52 -0700 | [diff] [blame] | 2118 | public void notifyChange(@NonNull Uri uri, ContentObserver observer, boolean syncToNetwork, |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2119 | @UserIdInt int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2120 | try { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2121 | getContentService().notifyChange( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2122 | uri, observer == null ? null : observer.getContentObserver(), |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2123 | observer != null && observer.deliverSelfNotifications(), |
| 2124 | syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0, |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2125 | userHandle, mTargetSdkVersion); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2126 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2127 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | /** |
| 2132 | * Notify registered observers within the designated user(s) that a row was updated. |
| 2133 | * |
| 2134 | * @hide |
| 2135 | */ |
Dianne Hackborn | e761777 | 2016-04-27 17:03:52 -0700 | [diff] [blame] | 2136 | public void notifyChange(@NonNull Uri uri, ContentObserver observer, @NotifyFlags int flags, |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 2137 | @UserIdInt int userHandle) { |
| 2138 | try { |
| 2139 | getContentService().notifyChange( |
| 2140 | uri, observer == null ? null : observer.getContentObserver(), |
| 2141 | observer != null && observer.deliverSelfNotifications(), flags, |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 2142 | userHandle, mTargetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2143 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2144 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /** |
Jeff Sharkey | adef88a | 2013-10-15 13:54:44 -0700 | [diff] [blame] | 2149 | * Take a persistable URI permission grant that has been offered. Once |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2150 | * taken, the permission grant will be remembered across device reboots. |
Jeff Sharkey | adef88a | 2013-10-15 13:54:44 -0700 | [diff] [blame] | 2151 | * Only URI permissions granted with |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2152 | * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} can be persisted. If |
| 2153 | * the grant has already been persisted, taking it again will touch |
| 2154 | * {@link UriPermission#getPersistedTime()}. |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2155 | * |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2156 | * @see #getPersistedUriPermissions() |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2157 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2158 | public void takePersistableUriPermission(@NonNull Uri uri, |
| 2159 | @Intent.AccessUriMode int modeFlags) { |
| 2160 | Preconditions.checkNotNull(uri, "uri"); |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2161 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2162 | ActivityManager.getService().takePersistableUriPermission( |
Felipe Leme | 988234a | 2018-02-14 12:00:29 -0800 | [diff] [blame] | 2163 | ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null, |
| 2164 | resolveUserId(uri)); |
| 2165 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2166 | throw e.rethrowFromSystemServer(); |
Felipe Leme | 988234a | 2018-02-14 12:00:29 -0800 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | /** |
| 2171 | * @hide |
| 2172 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 2173 | @UnsupportedAppUsage |
Felipe Leme | 988234a | 2018-02-14 12:00:29 -0800 | [diff] [blame] | 2174 | public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri, |
| 2175 | @Intent.AccessUriMode int modeFlags) { |
| 2176 | Preconditions.checkNotNull(toPackage, "toPackage"); |
| 2177 | Preconditions.checkNotNull(uri, "uri"); |
| 2178 | try { |
| 2179 | ActivityManager.getService().takePersistableUriPermission( |
| 2180 | ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage, |
| 2181 | resolveUserId(uri)); |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2182 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2183 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | /** |
Jeff Sharkey | adef88a | 2013-10-15 13:54:44 -0700 | [diff] [blame] | 2188 | * Relinquish a persisted URI permission grant. The URI must have been |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2189 | * previously made persistent with |
| 2190 | * {@link #takePersistableUriPermission(Uri, int)}. Any non-persistent |
| 2191 | * grants to the calling package will remain intact. |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2192 | * |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2193 | * @see #getPersistedUriPermissions() |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2194 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2195 | public void releasePersistableUriPermission(@NonNull Uri uri, |
| 2196 | @Intent.AccessUriMode int modeFlags) { |
| 2197 | Preconditions.checkNotNull(uri, "uri"); |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2198 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2199 | ActivityManager.getService().releasePersistableUriPermission( |
Felipe Leme | 988234a | 2018-02-14 12:00:29 -0800 | [diff] [blame] | 2200 | ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null, |
| 2201 | resolveUserId(uri)); |
| 2202 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2203 | throw e.rethrowFromSystemServer(); |
Felipe Leme | 988234a | 2018-02-14 12:00:29 -0800 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /** |
Jeff Sharkey | adef88a | 2013-10-15 13:54:44 -0700 | [diff] [blame] | 2208 | * Return list of all URI permission grants that have been persisted by the |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2209 | * calling app. That is, the returned permissions have been granted |
| 2210 | * <em>to</em> the calling app. Only persistable grants taken with |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2211 | * {@link #takePersistableUriPermission(Uri, int)} are returned. |
Fyodor Kupolov | 9bbaacf | 2016-06-20 14:03:44 -0700 | [diff] [blame] | 2212 | * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked. |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2213 | * |
| 2214 | * @see #takePersistableUriPermission(Uri, int) |
| 2215 | * @see #releasePersistableUriPermission(Uri, int) |
| 2216 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2217 | public @NonNull List<UriPermission> getPersistedUriPermissions() { |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2218 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2219 | return ActivityManager.getService() |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2220 | .getPersistedUriPermissions(mPackageName, true).getList(); |
| 2221 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2222 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | /** |
Jeff Sharkey | adef88a | 2013-10-15 13:54:44 -0700 | [diff] [blame] | 2227 | * Return list of all persisted URI permission grants that are hosted by the |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2228 | * calling app. That is, the returned permissions have been granted |
| 2229 | * <em>from</em> the calling app. Only grants taken with |
| 2230 | * {@link #takePersistableUriPermission(Uri, int)} are returned. |
Fyodor Kupolov | 9bbaacf | 2016-06-20 14:03:44 -0700 | [diff] [blame] | 2231 | * <p>Note: Some of the returned URIs may not be usable until after the user is unlocked. |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2232 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 2233 | public @NonNull List<UriPermission> getOutgoingPersistedUriPermissions() { |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2234 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2235 | return ActivityManager.getService() |
Jeff Sharkey | bcaac0a | 2013-10-09 14:21:08 -0700 | [diff] [blame] | 2236 | .getPersistedUriPermissions(mPackageName, false).getList(); |
Jeff Sharkey | e66c177 | 2013-09-20 14:30:59 -0700 | [diff] [blame] | 2237 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2238 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 08da7a1 | 2013-08-11 20:53:18 -0700 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | * Start an asynchronous sync operation. If you want to monitor the progress |
| 2244 | * of the sync you may register a SyncObserver. Only values of the following |
| 2245 | * types may be used in the extras bundle: |
| 2246 | * <ul> |
| 2247 | * <li>Integer</li> |
| 2248 | * <li>Long</li> |
| 2249 | * <li>Boolean</li> |
| 2250 | * <li>Float</li> |
| 2251 | * <li>Double</li> |
| 2252 | * <li>String</li> |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2253 | * <li>Account</li> |
| 2254 | * <li>null</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2255 | * </ul> |
| 2256 | * |
| 2257 | * @param uri the uri of the provider to sync or null to sync all providers. |
| 2258 | * @param extras any extras to pass to the SyncAdapter. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2259 | * @deprecated instead use |
| 2260 | * {@link #requestSync(android.accounts.Account, String, android.os.Bundle)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2261 | */ |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2262 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2263 | public void startSync(Uri uri, Bundle extras) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2264 | Account account = null; |
| 2265 | if (extras != null) { |
| 2266 | String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT); |
| 2267 | if (!TextUtils.isEmpty(accountName)) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2268 | // TODO: No references to Google in AOSP |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2269 | account = new Account(accountName, "com.google"); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2270 | } |
| 2271 | extras.remove(SYNC_EXTRAS_ACCOUNT); |
| 2272 | } |
| 2273 | requestSync(account, uri != null ? uri.getAuthority() : null, extras); |
| 2274 | } |
| 2275 | |
| 2276 | /** |
| 2277 | * Start an asynchronous sync operation. If you want to monitor the progress |
| 2278 | * of the sync you may register a SyncObserver. Only values of the following |
| 2279 | * types may be used in the extras bundle: |
| 2280 | * <ul> |
| 2281 | * <li>Integer</li> |
| 2282 | * <li>Long</li> |
| 2283 | * <li>Boolean</li> |
| 2284 | * <li>Float</li> |
| 2285 | * <li>Double</li> |
| 2286 | * <li>String</li> |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2287 | * <li>Account</li> |
| 2288 | * <li>null</li> |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2289 | * </ul> |
| 2290 | * |
| 2291 | * @param account which account should be synced |
| 2292 | * @param authority which authority should be synced |
| 2293 | * @param extras any extras to pass to the SyncAdapter. |
| 2294 | */ |
| 2295 | public static void requestSync(Account account, String authority, Bundle extras) { |
Alexandra Gherghina | 03e1e83 | 2014-09-10 16:06:10 +0100 | [diff] [blame] | 2296 | requestSyncAsUser(account, authority, UserHandle.myUserId(), extras); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /** |
| 2300 | * @see #requestSync(Account, String, Bundle) |
| 2301 | * @hide |
| 2302 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2303 | public static void requestSyncAsUser(Account account, String authority, @UserIdInt int userId, |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2304 | Bundle extras) { |
Matthew Williams | d8abd6a | 2013-09-09 14:35:27 -0700 | [diff] [blame] | 2305 | if (extras == null) { |
| 2306 | throw new IllegalArgumentException("Must specify extras."); |
| 2307 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2308 | SyncRequest request = |
| 2309 | new SyncRequest.Builder() |
| 2310 | .setSyncAdapter(account, authority) |
| 2311 | .setExtras(extras) |
Nick Kralevich | 69002ae | 2013-10-19 08:43:08 -0700 | [diff] [blame] | 2312 | .syncOnce() // Immediate sync. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2313 | .build(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2314 | try { |
| 2315 | getContentService().syncAsUser(request, userId); |
| 2316 | } catch(RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2317 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2318 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /** |
| 2322 | * Register a sync with the SyncManager. These requests are built using the |
| 2323 | * {@link SyncRequest.Builder}. |
| 2324 | */ |
| 2325 | public static void requestSync(SyncRequest request) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | try { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2327 | getContentService().sync(request); |
| 2328 | } catch(RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2329 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | /** |
| 2334 | * Check that only values of the following types are in the Bundle: |
| 2335 | * <ul> |
| 2336 | * <li>Integer</li> |
| 2337 | * <li>Long</li> |
| 2338 | * <li>Boolean</li> |
| 2339 | * <li>Float</li> |
| 2340 | * <li>Double</li> |
| 2341 | * <li>String</li> |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2342 | * <li>Account</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2343 | * <li>null</li> |
| 2344 | * </ul> |
| 2345 | * @param extras the Bundle to check |
| 2346 | */ |
| 2347 | public static void validateSyncExtrasBundle(Bundle extras) { |
| 2348 | try { |
| 2349 | for (String key : extras.keySet()) { |
| 2350 | Object value = extras.get(key); |
| 2351 | if (value == null) continue; |
| 2352 | if (value instanceof Long) continue; |
| 2353 | if (value instanceof Integer) continue; |
| 2354 | if (value instanceof Boolean) continue; |
| 2355 | if (value instanceof Float) continue; |
| 2356 | if (value instanceof Double) continue; |
| 2357 | if (value instanceof String) continue; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2358 | if (value instanceof Account) continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2359 | throw new IllegalArgumentException("unexpected value type: " |
| 2360 | + value.getClass().getName()); |
| 2361 | } |
| 2362 | } catch (IllegalArgumentException e) { |
| 2363 | throw e; |
| 2364 | } catch (RuntimeException exc) { |
| 2365 | throw new IllegalArgumentException("error unparceling Bundle", exc); |
| 2366 | } |
| 2367 | } |
| 2368 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2369 | /** |
| 2370 | * Cancel any active or pending syncs that match the Uri. If the uri is null then |
| 2371 | * all syncs will be canceled. |
| 2372 | * |
| 2373 | * @param uri the uri of the provider to sync or null to sync all providers. |
| 2374 | * @deprecated instead use {@link #cancelSync(android.accounts.Account, String)} |
| 2375 | */ |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2376 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2377 | public void cancelSync(Uri uri) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2378 | cancelSync(null /* all accounts */, uri != null ? uri.getAuthority() : null); |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * Cancel any active or pending syncs that match account and authority. The account and |
| 2383 | * authority can each independently be set to null, which means that syncs with any account |
| 2384 | * or authority, respectively, will match. |
| 2385 | * |
| 2386 | * @param account filters the syncs that match by this account |
| 2387 | * @param authority filters the syncs that match by this authority |
| 2388 | */ |
| 2389 | public static void cancelSync(Account account, String authority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2390 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2391 | getContentService().cancelSync(account, authority, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2392 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2393 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2397 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2398 | * @see #cancelSync(Account, String) |
| 2399 | * @hide |
| 2400 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2401 | public static void cancelSyncAsUser(Account account, String authority, @UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2402 | try { |
| 2403 | getContentService().cancelSyncAsUser(account, authority, null, userId); |
| 2404 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2405 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2410 | * Get information about the SyncAdapters that are known to the system. |
| 2411 | * @return an array of SyncAdapters that have registered with the system |
| 2412 | */ |
| 2413 | public static SyncAdapterType[] getSyncAdapterTypes() { |
| 2414 | try { |
| 2415 | return getContentService().getSyncAdapterTypes(); |
| 2416 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2417 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2422 | * @see #getSyncAdapterTypes() |
| 2423 | * @hide |
| 2424 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2425 | public static SyncAdapterType[] getSyncAdapterTypesAsUser(@UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2426 | try { |
| 2427 | return getContentService().getSyncAdapterTypesAsUser(userId); |
| 2428 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2429 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | /** |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 2434 | * @hide |
| 2435 | * Returns the package names of syncadapters that match a given user and authority. |
| 2436 | */ |
Jeff Sharkey | a73b8fd | 2016-01-06 17:02:08 -0700 | [diff] [blame] | 2437 | @TestApi |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 2438 | public static String[] getSyncAdapterPackagesForAuthorityAsUser(String authority, |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2439 | @UserIdInt int userId) { |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 2440 | try { |
| 2441 | return getContentService().getSyncAdapterPackagesForAuthorityAsUser(authority, userId); |
| 2442 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2443 | throw e.rethrowFromSystemServer(); |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 2444 | } |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2448 | * Check if the provider should be synced when a network tickle is received |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2449 | * <p>This method requires the caller to hold the permission |
| 2450 | * {@link android.Manifest.permission#READ_SYNC_SETTINGS}. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2451 | * |
| 2452 | * @param account the account whose setting we are querying |
| 2453 | * @param authority the provider whose setting we are querying |
| 2454 | * @return true if the provider should be synced when a network tickle is received |
| 2455 | */ |
| 2456 | public static boolean getSyncAutomatically(Account account, String authority) { |
| 2457 | try { |
| 2458 | return getContentService().getSyncAutomatically(account, authority); |
| 2459 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2460 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2465 | * @see #getSyncAutomatically(Account, String) |
| 2466 | * @hide |
| 2467 | */ |
| 2468 | public static boolean getSyncAutomaticallyAsUser(Account account, String authority, |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2469 | @UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2470 | try { |
| 2471 | return getContentService().getSyncAutomaticallyAsUser(account, authority, userId); |
| 2472 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2473 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2478 | * Set whether or not the provider is synced when it receives a network tickle. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2479 | * <p>This method requires the caller to hold the permission |
| 2480 | * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2481 | * |
| 2482 | * @param account the account whose setting we are querying |
| 2483 | * @param authority the provider whose behavior is being controlled |
| 2484 | * @param sync true if the provider should be synced when tickles are received for it |
| 2485 | */ |
| 2486 | public static void setSyncAutomatically(Account account, String authority, boolean sync) { |
Alexandra Gherghina | 03e1e83 | 2014-09-10 16:06:10 +0100 | [diff] [blame] | 2487 | setSyncAutomaticallyAsUser(account, authority, sync, UserHandle.myUserId()); |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | /** |
| 2491 | * @see #setSyncAutomatically(Account, String, boolean) |
| 2492 | * @hide |
| 2493 | */ |
| 2494 | public static void setSyncAutomaticallyAsUser(Account account, String authority, boolean sync, |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2495 | @UserIdInt int userId) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2496 | try { |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 2497 | getContentService().setSyncAutomaticallyAsUser(account, authority, sync, userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2498 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2499 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | /** |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2504 | * Specifies that a sync should be requested with the specified the account, authority, |
| 2505 | * and extras at the given frequency. If there is already another periodic sync scheduled |
| 2506 | * with the account, authority and extras then a new periodic sync won't be added, instead |
| 2507 | * the frequency of the previous one will be updated. |
| 2508 | * <p> |
| 2509 | * These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings. |
| 2510 | * Although these sync are scheduled at the specified frequency, it may take longer for it to |
| 2511 | * actually be started if other syncs are ahead of it in the sync operation queue. This means |
| 2512 | * that the actual start time may drift. |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2513 | * <p> |
| 2514 | * Periodic syncs are not allowed to have any of {@link #SYNC_EXTRAS_DO_NOT_RETRY}, |
| 2515 | * {@link #SYNC_EXTRAS_IGNORE_BACKOFF}, {@link #SYNC_EXTRAS_IGNORE_SETTINGS}, |
| 2516 | * {@link #SYNC_EXTRAS_INITIALIZE}, {@link #SYNC_EXTRAS_FORCE}, |
| 2517 | * {@link #SYNC_EXTRAS_EXPEDITED}, {@link #SYNC_EXTRAS_MANUAL} set to true. |
| 2518 | * If any are supplied then an {@link IllegalArgumentException} will be thrown. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2519 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2520 | * <p>This method requires the caller to hold the permission |
| 2521 | * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2522 | * <p>The bundle for a periodic sync can be queried by applications with the correct |
| 2523 | * permissions using |
| 2524 | * {@link ContentResolver#getPeriodicSyncs(Account account, String provider)}, so no |
| 2525 | * sensitive data should be transferred here. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2526 | * |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2527 | * @param account the account to specify in the sync |
| 2528 | * @param authority the provider to specify in the sync request |
| 2529 | * @param extras extra parameters to go along with the sync request |
Liefu Liu | 5c0d7b3 | 2018-03-21 10:25:49 -0700 | [diff] [blame] | 2530 | * @param pollFrequency how frequently the sync should be performed, in seconds. |
| 2531 | * On Android API level 24 and above, a minmam interval of 15 minutes is enforced. |
| 2532 | * On previous versions, the minimum interval is 1 hour. |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2533 | * @throws IllegalArgumentException if an illegal extra was set or if any of the parameters |
| 2534 | * are null. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2535 | */ |
| 2536 | public static void addPeriodicSync(Account account, String authority, Bundle extras, |
| 2537 | long pollFrequency) { |
| 2538 | validateSyncExtrasBundle(extras); |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 2539 | if (invalidPeriodicExtras(extras)) { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2540 | throw new IllegalArgumentException("illegal extras were set"); |
| 2541 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2542 | try { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2543 | getContentService().addPeriodicSync(account, authority, extras, pollFrequency); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2544 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2545 | throw e.rethrowFromSystemServer(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2550 | * {@hide} |
| 2551 | * Helper function to throw an <code>IllegalArgumentException</code> if any illegal |
| 2552 | * extras were set for a periodic sync. |
| 2553 | * |
| 2554 | * @param extras bundle to validate. |
| 2555 | */ |
| 2556 | public static boolean invalidPeriodicExtras(Bundle extras) { |
| 2557 | if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false) |
| 2558 | || extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false) |
| 2559 | || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false) |
| 2560 | || extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false) |
| 2561 | || extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false) |
| 2562 | || extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false) |
| 2563 | || extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) { |
| 2564 | return true; |
| 2565 | } |
| 2566 | return false; |
| 2567 | } |
| 2568 | |
| 2569 | /** |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2570 | * Remove a periodic sync. Has no affect if account, authority and extras don't match |
| 2571 | * an existing periodic sync. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2572 | * <p>This method requires the caller to hold the permission |
| 2573 | * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2574 | * |
| 2575 | * @param account the account of the periodic sync to remove |
| 2576 | * @param authority the provider of the periodic sync to remove |
| 2577 | * @param extras the extras of the periodic sync to remove |
| 2578 | */ |
| 2579 | public static void removePeriodicSync(Account account, String authority, Bundle extras) { |
| 2580 | validateSyncExtrasBundle(extras); |
| 2581 | try { |
| 2582 | getContentService().removePeriodicSync(account, authority, extras); |
| 2583 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2584 | throw e.rethrowFromSystemServer(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2589 | * Remove the specified sync. This will cancel any pending or active syncs. If the request is |
| 2590 | * for a periodic sync, this call will remove any future occurrences. |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 2591 | * <p> |
| 2592 | * If a periodic sync is specified, the caller must hold the permission |
| 2593 | * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. |
| 2594 | *</p> |
| 2595 | * It is possible to cancel a sync using a SyncRequest object that is not the same object |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2596 | * with which you requested the sync. Do so by building a SyncRequest with the same |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 2597 | * adapter, frequency, <b>and</b> extras bundle. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2598 | * |
| 2599 | * @param request SyncRequest object containing information about sync to cancel. |
| 2600 | */ |
| 2601 | public static void cancelSync(SyncRequest request) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2602 | if (request == null) { |
| 2603 | throw new IllegalArgumentException("request cannot be null"); |
| 2604 | } |
| 2605 | try { |
| 2606 | getContentService().cancelRequest(request); |
| 2607 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2608 | throw e.rethrowFromSystemServer(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2609 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | /** |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2613 | * Get the list of information about the periodic syncs for the given account and authority. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2614 | * <p>This method requires the caller to hold the permission |
| 2615 | * {@link android.Manifest.permission#READ_SYNC_SETTINGS}. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2616 | * |
| 2617 | * @param account the account whose periodic syncs we are querying |
| 2618 | * @param authority the provider whose periodic syncs we are querying |
| 2619 | * @return a list of PeriodicSync objects. This list may be empty but will never be null. |
| 2620 | */ |
| 2621 | public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) { |
| 2622 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2623 | return getContentService().getPeriodicSyncs(account, authority, null); |
| 2624 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2625 | throw e.rethrowFromSystemServer(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | /** |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2630 | * Check if this account/provider is syncable. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2631 | * <p>This method requires the caller to hold the permission |
| 2632 | * {@link android.Manifest.permission#READ_SYNC_SETTINGS}. |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2633 | * @return >0 if it is syncable, 0 if not, and <0 if the state isn't known yet. |
| 2634 | */ |
Jim Miller | 20ea6ce | 2009-08-17 15:47:14 -0700 | [diff] [blame] | 2635 | public static int getIsSyncable(Account account, String authority) { |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2636 | try { |
| 2637 | return getContentService().getIsSyncable(account, authority); |
| 2638 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2639 | throw e.rethrowFromSystemServer(); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2644 | * @see #getIsSyncable(Account, String) |
| 2645 | * @hide |
| 2646 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2647 | public static int getIsSyncableAsUser(Account account, String authority, |
| 2648 | @UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2649 | try { |
| 2650 | return getContentService().getIsSyncableAsUser(account, authority, userId); |
| 2651 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2652 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | /** |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2657 | * Set whether this account/provider is syncable. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2658 | * <p>This method requires the caller to hold the permission |
| 2659 | * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. |
Fred Quintana | 718671b | 2009-08-17 14:08:37 -0700 | [diff] [blame] | 2660 | * @param syncable >0 denotes syncable, 0 means not syncable, <0 means unknown |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2661 | */ |
Jim Miller | 20ea6ce | 2009-08-17 15:47:14 -0700 | [diff] [blame] | 2662 | public static void setIsSyncable(Account account, String authority, int syncable) { |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2663 | try { |
| 2664 | getContentService().setIsSyncable(account, authority, syncable); |
| 2665 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2666 | throw e.rethrowFromSystemServer(); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2671 | * Gets the master auto-sync setting that applies to all the providers and accounts. |
| 2672 | * If this is false then the per-provider auto-sync setting is ignored. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2673 | * <p>This method requires the caller to hold the permission |
| 2674 | * {@link android.Manifest.permission#READ_SYNC_SETTINGS}. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2675 | * |
| 2676 | * @return the master auto-sync setting that applies to all the providers and accounts |
| 2677 | */ |
| 2678 | public static boolean getMasterSyncAutomatically() { |
| 2679 | try { |
| 2680 | return getContentService().getMasterSyncAutomatically(); |
| 2681 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2682 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2687 | * @see #getMasterSyncAutomatically() |
| 2688 | * @hide |
| 2689 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2690 | public static boolean getMasterSyncAutomaticallyAsUser(@UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2691 | try { |
| 2692 | return getContentService().getMasterSyncAutomaticallyAsUser(userId); |
| 2693 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2694 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2699 | * Sets the master auto-sync setting that applies to all the providers and accounts. |
| 2700 | * If this is false then the per-provider auto-sync setting is ignored. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2701 | * <p>This method requires the caller to hold the permission |
| 2702 | * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2703 | * |
| 2704 | * @param sync the master auto-sync setting that applies to all the providers and accounts |
| 2705 | */ |
| 2706 | public static void setMasterSyncAutomatically(boolean sync) { |
Alexandra Gherghina | 03e1e83 | 2014-09-10 16:06:10 +0100 | [diff] [blame] | 2707 | setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId()); |
Alexandra Gherghina | 0e9ac20 | 2014-07-15 23:11:48 +0100 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | /** |
| 2711 | * @see #setMasterSyncAutomatically(boolean) |
| 2712 | * @hide |
| 2713 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2714 | public static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2715 | try { |
Alexandra Gherghina | 0e9ac20 | 2014-07-15 23:11:48 +0100 | [diff] [blame] | 2716 | getContentService().setMasterSyncAutomaticallyAsUser(sync, userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2717 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2718 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2723 | * Returns true if there is currently a sync operation for the given account or authority |
| 2724 | * actively being processed. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2725 | * <p>This method requires the caller to hold the permission |
| 2726 | * {@link android.Manifest.permission#READ_SYNC_STATS}. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2727 | * @param account the account whose setting we are querying |
| 2728 | * @param authority the provider whose behavior is being queried |
| 2729 | * @return true if a sync is active for the given account or authority. |
| 2730 | */ |
| 2731 | public static boolean isSyncActive(Account account, String authority) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2732 | if (account == null) { |
| 2733 | throw new IllegalArgumentException("account must not be null"); |
| 2734 | } |
| 2735 | if (authority == null) { |
| 2736 | throw new IllegalArgumentException("authority must not be null"); |
| 2737 | } |
| 2738 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2739 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2740 | return getContentService().isSyncActive(account, authority, null); |
| 2741 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2742 | throw e.rethrowFromSystemServer(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2743 | } |
| 2744 | } |
| 2745 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2746 | /** |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 2747 | * If a sync is active returns the information about it, otherwise returns null. |
| 2748 | * <p> |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2749 | * This method requires the caller to hold the permission |
| 2750 | * {@link android.Manifest.permission#READ_SYNC_STATS}. |
| 2751 | * <p> |
Fred Quintana | d5e4fdc | 2010-03-30 15:16:21 -0700 | [diff] [blame] | 2752 | * @return the SyncInfo for the currently active sync or null if one is not active. |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 2753 | * @deprecated |
| 2754 | * Since multiple concurrent syncs are now supported you should use |
| 2755 | * {@link #getCurrentSyncs()} to get the accurate list of current syncs. |
| 2756 | * This method returns the first item from the list of current syncs |
| 2757 | * or null if there are none. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2758 | */ |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 2759 | @Deprecated |
Fred Quintana | d5e4fdc | 2010-03-30 15:16:21 -0700 | [diff] [blame] | 2760 | public static SyncInfo getCurrentSync() { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2761 | try { |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 2762 | final List<SyncInfo> syncs = getContentService().getCurrentSyncs(); |
| 2763 | if (syncs.isEmpty()) { |
| 2764 | return null; |
| 2765 | } |
| 2766 | return syncs.get(0); |
| 2767 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2768 | throw e.rethrowFromSystemServer(); |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | /** |
| 2773 | * Returns a list with information about all the active syncs. This list will be empty |
| 2774 | * if there are no active syncs. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2775 | * <p> |
| 2776 | * This method requires the caller to hold the permission |
| 2777 | * {@link android.Manifest.permission#READ_SYNC_STATS}. |
| 2778 | * <p> |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 2779 | * @return a List of SyncInfo objects for the currently active syncs. |
| 2780 | */ |
| 2781 | public static List<SyncInfo> getCurrentSyncs() { |
| 2782 | try { |
| 2783 | return getContentService().getCurrentSyncs(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2784 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2785 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2790 | * @see #getCurrentSyncs() |
| 2791 | * @hide |
| 2792 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2793 | public static List<SyncInfo> getCurrentSyncsAsUser(@UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2794 | try { |
| 2795 | return getContentService().getCurrentSyncsAsUser(userId); |
| 2796 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2797 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | /** |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 2802 | * Returns the status that matches the authority. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2803 | * @param account the account whose setting we are querying |
| 2804 | * @param authority the provider whose behavior is being queried |
| 2805 | * @return the SyncStatusInfo for the authority, or null if none exists |
| 2806 | * @hide |
| 2807 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 2808 | @UnsupportedAppUsage |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2809 | public static SyncStatusInfo getSyncStatus(Account account, String authority) { |
| 2810 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2811 | return getContentService().getSyncStatus(account, authority, null); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2812 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2813 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2818 | * @see #getSyncStatus(Account, String) |
| 2819 | * @hide |
| 2820 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 2821 | @UnsupportedAppUsage |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2822 | public static SyncStatusInfo getSyncStatusAsUser(Account account, String authority, |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2823 | @UserIdInt int userId) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2824 | try { |
| 2825 | return getContentService().getSyncStatusAsUser(account, authority, null, userId); |
| 2826 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2827 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2832 | * Return true if the pending status is true of any matching authorities. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 2833 | * <p>This method requires the caller to hold the permission |
| 2834 | * {@link android.Manifest.permission#READ_SYNC_STATS}. |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2835 | * @param account the account whose setting we are querying |
| 2836 | * @param authority the provider whose behavior is being queried |
| 2837 | * @return true if there is a pending sync with the matching account and authority |
| 2838 | */ |
| 2839 | public static boolean isSyncPending(Account account, String authority) { |
Alexandra Gherghina | 03e1e83 | 2014-09-10 16:06:10 +0100 | [diff] [blame] | 2840 | return isSyncPendingAsUser(account, authority, UserHandle.myUserId()); |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * @see #requestSync(Account, String, Bundle) |
| 2845 | * @hide |
| 2846 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 2847 | public static boolean isSyncPendingAsUser(Account account, String authority, |
| 2848 | @UserIdInt int userId) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2849 | try { |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 2850 | return getContentService().isSyncPendingAsUser(account, authority, null, userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2851 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2852 | throw e.rethrowFromSystemServer(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2853 | } |
| 2854 | } |
| 2855 | |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 2856 | /** |
| 2857 | * Request notifications when the different aspects of the SyncManager change. The |
| 2858 | * different items that can be requested are: |
| 2859 | * <ul> |
| 2860 | * <li> {@link #SYNC_OBSERVER_TYPE_PENDING} |
| 2861 | * <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE} |
| 2862 | * <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS} |
| 2863 | * </ul> |
| 2864 | * The caller can set one or more of the status types in the mask for any |
| 2865 | * given listener registration. |
| 2866 | * @param mask the status change types that will cause the callback to be invoked |
| 2867 | * @param callback observer to be invoked when the status changes |
| 2868 | * @return a handle that can be used to remove the listener at a later time |
| 2869 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2870 | public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) { |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 2871 | if (callback == null) { |
| 2872 | throw new IllegalArgumentException("you passed in a null callback"); |
| 2873 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2874 | try { |
| 2875 | ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() { |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 2876 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2877 | public void onStatusChanged(int which) throws RemoteException { |
| 2878 | callback.onStatusChanged(which); |
| 2879 | } |
| 2880 | }; |
| 2881 | getContentService().addStatusChangeListener(mask, observer); |
| 2882 | return observer; |
| 2883 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2884 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2885 | } |
| 2886 | } |
| 2887 | |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 2888 | /** |
| 2889 | * Remove a previously registered status change listener. |
| 2890 | * @param handle the handle that was returned by {@link #addStatusChangeListener} |
| 2891 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2892 | public static void removeStatusChangeListener(Object handle) { |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 2893 | if (handle == null) { |
| 2894 | throw new IllegalArgumentException("you passed in a null handle"); |
| 2895 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2896 | try { |
| 2897 | getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle); |
| 2898 | } catch (RemoteException e) { |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 2899 | throw e.rethrowFromSystemServer(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 2903 | /** {@hide} */ |
| 2904 | public void putCache(Uri key, Bundle value) { |
| 2905 | try { |
| 2906 | getContentService().putCache(mContext.getPackageName(), key, value, |
| 2907 | mContext.getUserId()); |
| 2908 | } catch (RemoteException e) { |
| 2909 | throw e.rethrowFromSystemServer(); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | /** {@hide} */ |
| 2914 | public Bundle getCache(Uri key) { |
| 2915 | try { |
| 2916 | final Bundle bundle = getContentService().getCache(mContext.getPackageName(), key, |
| 2917 | mContext.getUserId()); |
| 2918 | if (bundle != null) bundle.setClassLoader(mContext.getClassLoader()); |
| 2919 | return bundle; |
| 2920 | } catch (RemoteException e) { |
| 2921 | throw e.rethrowFromSystemServer(); |
| 2922 | } |
| 2923 | } |
| 2924 | |
Ben Lin | 8ea8200 | 2017-03-08 17:30:16 -0800 | [diff] [blame] | 2925 | /** {@hide} */ |
| 2926 | public int getTargetSdkVersion() { |
| 2927 | return mTargetSdkVersion; |
| 2928 | } |
| 2929 | |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2930 | /** |
| 2931 | * Returns sampling percentage for a given duration. |
| 2932 | * |
| 2933 | * Always returns at least 1%. |
| 2934 | */ |
| 2935 | private int samplePercentForDuration(long durationMillis) { |
| 2936 | if (durationMillis >= SLOW_THRESHOLD_MILLIS) { |
| 2937 | return 100; |
| 2938 | } |
| 2939 | return (int) (100 * durationMillis / SLOW_THRESHOLD_MILLIS) + 1; |
| 2940 | } |
| 2941 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 2942 | private void maybeLogQueryToEventLog( |
| 2943 | long durationMillis, Uri uri, String[] projection, @Nullable Bundle queryArgs) { |
Jeff Sharkey | 2b4d22c | 2013-04-26 10:52:00 -0700 | [diff] [blame] | 2944 | if (!ENABLE_CONTENT_SAMPLE) return; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2945 | int samplePercent = samplePercentForDuration(durationMillis); |
| 2946 | if (samplePercent < 100) { |
| 2947 | synchronized (mRandom) { |
| 2948 | if (mRandom.nextInt(100) >= samplePercent) { |
| 2949 | return; |
| 2950 | } |
| 2951 | } |
| 2952 | } |
| 2953 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 2954 | // Ensure a non-null bundle. |
| 2955 | queryArgs = (queryArgs != null) ? queryArgs : Bundle.EMPTY; |
| 2956 | |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2957 | StringBuilder projectionBuffer = new StringBuilder(100); |
| 2958 | if (projection != null) { |
| 2959 | for (int i = 0; i < projection.length; ++i) { |
| 2960 | // Note: not using a comma delimiter here, as the |
| 2961 | // multiple arguments to EventLog.writeEvent later |
| 2962 | // stringify with a comma delimiter, which would make |
| 2963 | // parsing uglier later. |
| 2964 | if (i != 0) projectionBuffer.append('/'); |
| 2965 | projectionBuffer.append(projection[i]); |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | // ActivityThread.currentPackageName() only returns non-null if the |
| 2970 | // current thread is an application main thread. This parameter tells |
| 2971 | // us whether an event loop is blocked, and if so, which app it is. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2972 | String blockingPackage = AppGlobals.getInitialPackage(); |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2973 | |
| 2974 | EventLog.writeEvent( |
Brad Fitzpatrick | a8fbedb | 2010-04-08 14:08:54 -0700 | [diff] [blame] | 2975 | EventLogTags.CONTENT_QUERY_SAMPLE, |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2976 | uri.toString(), |
| 2977 | projectionBuffer.toString(), |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 2978 | queryArgs.getString(QUERY_ARG_SQL_SELECTION, ""), |
| 2979 | queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER, ""), |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2980 | durationMillis, |
| 2981 | blockingPackage != null ? blockingPackage : "", |
| 2982 | samplePercent); |
| 2983 | } |
| 2984 | |
| 2985 | private void maybeLogUpdateToEventLog( |
| 2986 | long durationMillis, Uri uri, String operation, String selection) { |
Jeff Sharkey | 2b4d22c | 2013-04-26 10:52:00 -0700 | [diff] [blame] | 2987 | if (!ENABLE_CONTENT_SAMPLE) return; |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2988 | int samplePercent = samplePercentForDuration(durationMillis); |
| 2989 | if (samplePercent < 100) { |
| 2990 | synchronized (mRandom) { |
| 2991 | if (mRandom.nextInt(100) >= samplePercent) { |
| 2992 | return; |
| 2993 | } |
| 2994 | } |
| 2995 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2996 | String blockingPackage = AppGlobals.getInitialPackage(); |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2997 | EventLog.writeEvent( |
Brad Fitzpatrick | a8fbedb | 2010-04-08 14:08:54 -0700 | [diff] [blame] | 2998 | EventLogTags.CONTENT_UPDATE_SAMPLE, |
Brad Fitzpatrick | a63730d | 2010-02-07 22:25:34 -0800 | [diff] [blame] | 2999 | uri.toString(), |
| 3000 | operation, |
| 3001 | selection != null ? selection : "", |
| 3002 | durationMillis, |
| 3003 | blockingPackage != null ? blockingPackage : "", |
| 3004 | samplePercent); |
| 3005 | } |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3006 | |
Jeff Brown | 825c513 | 2011-10-12 16:11:30 -0700 | [diff] [blame] | 3007 | private final class CursorWrapperInner extends CrossProcessCursorWrapper { |
Gilles Debunne | 03f0292 | 2010-06-09 14:11:45 -0700 | [diff] [blame] | 3008 | private final IContentProvider mContentProvider; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3009 | private final AtomicBoolean mProviderReleased = new AtomicBoolean(); |
Jeff Brown | baaf8c3 | 2011-10-09 14:07:00 -0700 | [diff] [blame] | 3010 | |
| 3011 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3012 | |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3013 | CursorWrapperInner(Cursor cursor, IContentProvider contentProvider) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3014 | super(cursor); |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3015 | mContentProvider = contentProvider; |
Jeff Brown | baaf8c3 | 2011-10-09 14:07:00 -0700 | [diff] [blame] | 3016 | mCloseGuard.open("close"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | @Override |
| 3020 | public void close() { |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3021 | mCloseGuard.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3022 | super.close(); |
Jeff Brown | baaf8c3 | 2011-10-09 14:07:00 -0700 | [diff] [blame] | 3023 | |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3024 | if (mProviderReleased.compareAndSet(false, true)) { |
| 3025 | ContentResolver.this.releaseProvider(mContentProvider); |
Jeff Brown | baaf8c3 | 2011-10-09 14:07:00 -0700 | [diff] [blame] | 3026 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | @Override |
| 3030 | protected void finalize() throws Throwable { |
| 3031 | try { |
Narayan Kamath | 492e9e8 | 2017-03-22 14:28:08 +0000 | [diff] [blame] | 3032 | if (mCloseGuard != null) { |
| 3033 | mCloseGuard.warnIfOpen(); |
| 3034 | } |
| 3035 | |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3036 | close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3037 | } finally { |
| 3038 | super.finalize(); |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | private final class ParcelFileDescriptorInner extends ParcelFileDescriptor { |
Gilles Debunne | 03f0292 | 2010-06-09 14:11:45 -0700 | [diff] [blame] | 3044 | private final IContentProvider mContentProvider; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3045 | private final AtomicBoolean mProviderReleased = new AtomicBoolean(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | |
| 3047 | ParcelFileDescriptorInner(ParcelFileDescriptor pfd, IContentProvider icp) { |
| 3048 | super(pfd); |
| 3049 | mContentProvider = icp; |
| 3050 | } |
| 3051 | |
| 3052 | @Override |
Amith Yamasani | 487c11a | 2013-09-18 09:16:15 -0700 | [diff] [blame] | 3053 | public void releaseResources() { |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3054 | if (mProviderReleased.compareAndSet(false, true)) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 3055 | ContentResolver.this.releaseProvider(mContentProvider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | } |
| 3059 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3060 | /** @hide */ |
| 3061 | public static final String CONTENT_SERVICE_NAME = "content"; |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 3062 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3063 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 3064 | @UnsupportedAppUsage |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3065 | public static IContentService getContentService() { |
| 3066 | if (sContentService != null) { |
| 3067 | return sContentService; |
| 3068 | } |
| 3069 | IBinder b = ServiceManager.getService(CONTENT_SERVICE_NAME); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3070 | sContentService = IContentService.Stub.asInterface(b); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3071 | return sContentService; |
| 3072 | } |
Tom O'Neill | 0ba1cb0 | 2010-01-08 12:53:50 -0800 | [diff] [blame] | 3073 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 3074 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 3075 | @UnsupportedAppUsage |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 3076 | public String getPackageName() { |
| 3077 | return mPackageName; |
| 3078 | } |
| 3079 | |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 3080 | @UnsupportedAppUsage |
Jeff Sharkey | c907d21 | 2018-04-09 09:56:42 -0600 | [diff] [blame] | 3081 | private static volatile IContentService sContentService; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 3082 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3083 | private final Context mContext; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3084 | |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 3085 | @UnsupportedAppUsage |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 3086 | final String mPackageName; |
Jeff Sharkey | 912e80d | 2017-02-24 11:00:55 -0700 | [diff] [blame] | 3087 | final int mTargetSdkVersion; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 3088 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3089 | private static final String TAG = "ContentResolver"; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 3090 | |
| 3091 | /** @hide */ |
| 3092 | public int resolveUserId(Uri uri) { |
| 3093 | return ContentProvider.getUserIdFromUri(uri, mContext.getUserId()); |
| 3094 | } |
Daniel Nishi | c29d2b0 | 2016-06-30 12:20:41 -0700 | [diff] [blame] | 3095 | |
| 3096 | /** @hide */ |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 3097 | public int getUserId() { |
| 3098 | return mContext.getUserId(); |
| 3099 | } |
| 3100 | |
| 3101 | /** @hide */ |
Daniel Nishi | c29d2b0 | 2016-06-30 12:20:41 -0700 | [diff] [blame] | 3102 | public Drawable getTypeDrawable(String mimeType) { |
| 3103 | return MimeIconUtils.loadMimeIcon(mContext, mimeType); |
| 3104 | } |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 3105 | |
| 3106 | /** |
| 3107 | * @hide |
| 3108 | */ |
| 3109 | public static @Nullable Bundle createSqlQueryBundle( |
| 3110 | @Nullable String selection, |
| 3111 | @Nullable String[] selectionArgs, |
| 3112 | @Nullable String sortOrder) { |
| 3113 | |
| 3114 | if (selection == null && selectionArgs == null && sortOrder == null) { |
| 3115 | return null; |
| 3116 | } |
| 3117 | |
| 3118 | Bundle queryArgs = new Bundle(); |
| 3119 | if (selection != null) { |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3120 | queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection); |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 3121 | } |
| 3122 | if (selectionArgs != null) { |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3123 | queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs); |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 3124 | } |
| 3125 | if (sortOrder != null) { |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3126 | queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder); |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 3127 | } |
| 3128 | return queryArgs; |
| 3129 | } |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3130 | |
| 3131 | /** |
| 3132 | * Returns structured sort args formatted as an SQL sort clause. |
| 3133 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 3134 | * NOTE: Collator clauses are suitable for use with non text fields. We might |
| 3135 | * choose to omit any collation clause since we don't know the underlying |
| 3136 | * type of data to be collated. Imperical testing shows that sqlite3 doesn't |
| 3137 | * appear to care much about the presence of collate clauses in queries |
| 3138 | * when ordering by numeric fields. For this reason we include collate |
| 3139 | * clause unilaterally when {@link #QUERY_ARG_SORT_COLLATION} is present |
| 3140 | * in query args bundle. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3141 | * |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 3142 | * TODO: Would be nice to explicitly validate that colums referenced in |
| 3143 | * {@link #QUERY_ARG_SORT_COLUMNS} are present in the associated projection. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3144 | * |
| 3145 | * @hide |
| 3146 | */ |
| 3147 | public static String createSqlSortClause(Bundle queryArgs) { |
| 3148 | String[] columns = queryArgs.getStringArray(QUERY_ARG_SORT_COLUMNS); |
| 3149 | if (columns == null || columns.length == 0) { |
| 3150 | throw new IllegalArgumentException("Can't create sort clause without columns."); |
| 3151 | } |
| 3152 | |
| 3153 | String query = TextUtils.join(", ", columns); |
| 3154 | |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 3155 | // Interpret PRIMARY and SECONDARY collation strength as no-case collation based |
| 3156 | // on their javadoc descriptions. |
| 3157 | int collation = queryArgs.getInt( |
| 3158 | ContentResolver.QUERY_ARG_SORT_COLLATION, java.text.Collator.IDENTICAL); |
| 3159 | if (collation == java.text.Collator.PRIMARY || collation == java.text.Collator.SECONDARY) { |
| 3160 | query += " COLLATE NOCASE"; |
| 3161 | } |
| 3162 | |
Steve McKay | 415f41b | 2017-02-01 13:38:25 -0800 | [diff] [blame] | 3163 | int sortDir = queryArgs.getInt(QUERY_ARG_SORT_DIRECTION, Integer.MIN_VALUE); |
| 3164 | if (sortDir != Integer.MIN_VALUE) { |
| 3165 | switch (sortDir) { |
| 3166 | case QUERY_SORT_DIRECTION_ASCENDING: |
| 3167 | query += " ASC"; |
| 3168 | break; |
| 3169 | case QUERY_SORT_DIRECTION_DESCENDING: |
| 3170 | query += " DESC"; |
| 3171 | break; |
| 3172 | default: |
| 3173 | throw new IllegalArgumentException("Unsupported sort direction value." |
| 3174 | + " See ContentResolver documentation for details."); |
| 3175 | } |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3176 | } |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 3177 | return query; |
| 3178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | } |