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