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