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