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