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 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 19 | import static android.Manifest.permission.INTERACT_ACROSS_USERS; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 20 | import static android.app.AppOpsManager.MODE_ALLOWED; |
| 21 | import static android.app.AppOpsManager.MODE_ERRORED; |
| 22 | import static android.app.AppOpsManager.MODE_IGNORED; |
| 23 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 24 | |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 25 | import android.annotation.NonNull; |
Scott Kennedy | 9f78f65 | 2015-03-01 15:29:25 -0800 | [diff] [blame] | 26 | import android.annotation.Nullable; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 27 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 28 | import android.app.AppOpsManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 29 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.pm.ProviderInfo; |
| 31 | import android.content.res.AssetFileDescriptor; |
| 32 | import android.content.res.Configuration; |
| 33 | import android.database.Cursor; |
Svet Ganov | 7271f3e | 2015-04-23 10:16:53 -0700 | [diff] [blame] | 34 | import android.database.MatrixCursor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.SQLException; |
| 36 | import android.net.Uri; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 37 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.os.Binder; |
Mathew Inwood | 45d2c25 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 39 | import android.os.Build; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 40 | import android.os.Bundle; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 41 | import android.os.CancellationSignal; |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 42 | import android.os.IBinder; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 43 | import android.os.ICancellationSignal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.os.ParcelFileDescriptor; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 45 | import android.os.Process; |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 46 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
Jeff Sharkey | b31afd2 | 2017-06-12 14:17:10 -0600 | [diff] [blame] | 48 | import android.os.storage.StorageManager; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 49 | import android.text.TextUtils; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 50 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | import java.io.File; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 53 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import java.io.FileNotFoundException; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 55 | import java.io.IOException; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 56 | import java.io.PrintWriter; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 57 | import java.util.ArrayList; |
Andreas Gampe | e6748ce | 2015-12-11 18:00:38 -0800 | [diff] [blame] | 58 | import java.util.Arrays; |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 59 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Content providers are one of the primary building blocks of Android applications, providing |
| 63 | * content to applications. They encapsulate data and provide it to applications through the single |
| 64 | * {@link ContentResolver} interface. A content provider is only required if you need to share |
| 65 | * data between multiple applications. For example, the contacts data is used by multiple |
| 66 | * applications and must be stored in a content provider. If you don't need to share data amongst |
| 67 | * multiple applications you can use a database directly via |
| 68 | * {@link android.database.sqlite.SQLiteDatabase}. |
| 69 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | * <p>When a request is made via |
| 71 | * a {@link ContentResolver} the system inspects the authority of the given URI and passes the |
| 72 | * request to the content provider registered with the authority. The content provider can interpret |
| 73 | * the rest of the URI however it wants. The {@link UriMatcher} class is helpful for parsing |
| 74 | * URIs.</p> |
| 75 | * |
| 76 | * <p>The primary methods that need to be implemented are: |
| 77 | * <ul> |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 78 | * <li>{@link #onCreate} which is called to initialize the provider</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * <li>{@link #query} which returns data to the caller</li> |
| 80 | * <li>{@link #insert} which inserts new data into the content provider</li> |
| 81 | * <li>{@link #update} which updates existing data in the content provider</li> |
| 82 | * <li>{@link #delete} which deletes data from the content provider</li> |
| 83 | * <li>{@link #getType} which returns the MIME type of data in the content provider</li> |
| 84 | * </ul></p> |
| 85 | * |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 86 | * <p class="caution">Data access methods (such as {@link #insert} and |
| 87 | * {@link #update}) may be called from many threads at once, and must be thread-safe. |
| 88 | * Other methods (such as {@link #onCreate}) are only called from the application |
| 89 | * main thread, and must avoid performing lengthy operations. See the method |
| 90 | * descriptions for their expected thread behavior.</p> |
| 91 | * |
| 92 | * <p>Requests to {@link ContentResolver} are automatically forwarded to the appropriate |
| 93 | * ContentProvider instance, so subclasses don't have to worry about the details of |
| 94 | * cross-process calls.</p> |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 95 | * |
| 96 | * <div class="special reference"> |
| 97 | * <h3>Developer Guides</h3> |
| 98 | * <p>For more information about using content providers, read the |
| 99 | * <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a> |
| 100 | * developer guide.</p> |
Nicole Borrelli | 8a5f04a | 2018-09-20 14:19:14 -0700 | [diff] [blame] | 101 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | */ |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 103 | public abstract class ContentProvider implements ComponentCallbacks2 { |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 104 | |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 105 | private static final String TAG = "ContentProvider"; |
| 106 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 107 | /* |
| 108 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 109 | * MockContentProvider. |
| 110 | */ |
| 111 | |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 112 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | private Context mContext = null; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 114 | private int mMyUid; |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 115 | |
| 116 | // Since most Providers have only one authority, we keep both a String and a String[] to improve |
| 117 | // performance. |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 118 | @UnsupportedAppUsage |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 119 | private String mAuthority; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 120 | @UnsupportedAppUsage |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 121 | private String[] mAuthorities; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 122 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | private String mReadPermission; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 124 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | private String mWritePermission; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 126 | @UnsupportedAppUsage |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 127 | private PathPermission[] mPathPermissions; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 128 | private boolean mExported; |
Dianne Hackborn | 7e6f976 | 2013-02-26 13:35:11 -0800 | [diff] [blame] | 129 | private boolean mNoPerms; |
Amith Yamasani | a6f4d58 | 2014-08-07 17:58:39 -0700 | [diff] [blame] | 130 | private boolean mSingleUser; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 132 | private final ThreadLocal<String> mCallingPackage = new ThreadLocal<>(); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 133 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | private Transport mTransport = new Transport(); |
| 135 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Construct a ContentProvider instance. Content providers must be |
| 138 | * <a href="{@docRoot}guide/topics/manifest/provider-element.html">declared |
| 139 | * in the manifest</a>, accessed with {@link ContentResolver}, and created |
| 140 | * automatically by the system, so applications usually do not create |
| 141 | * ContentProvider instances directly. |
| 142 | * |
| 143 | * <p>At construction time, the object is uninitialized, and most fields and |
| 144 | * methods are unavailable. Subclasses should initialize themselves in |
| 145 | * {@link #onCreate}, not the constructor. |
| 146 | * |
| 147 | * <p>Content providers are created on the application main thread at |
| 148 | * application launch time. The constructor must not perform lengthy |
| 149 | * operations, or application startup will be delayed. |
| 150 | */ |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 151 | public ContentProvider() { |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Constructor just for mocking. |
| 156 | * |
| 157 | * @param context A Context object which should be some mock instance (like the |
| 158 | * instance of {@link android.test.mock.MockContext}). |
| 159 | * @param readPermission The read permision you want this instance should have in the |
| 160 | * test, which is available via {@link #getReadPermission()}. |
| 161 | * @param writePermission The write permission you want this instance should have |
| 162 | * in the test, which is available via {@link #getWritePermission()}. |
| 163 | * @param pathPermissions The PathPermissions you want this instance should have |
| 164 | * in the test, which is available via {@link #getPathPermissions()}. |
| 165 | * @hide |
| 166 | */ |
Mathew Inwood | 45d2c25 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 167 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 168 | public ContentProvider( |
| 169 | Context context, |
| 170 | String readPermission, |
| 171 | String writePermission, |
| 172 | PathPermission[] pathPermissions) { |
| 173 | mContext = context; |
| 174 | mReadPermission = readPermission; |
| 175 | mWritePermission = writePermission; |
| 176 | mPathPermissions = pathPermissions; |
| 177 | } |
| 178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | /** |
| 180 | * Given an IContentProvider, try to coerce it back to the real |
| 181 | * ContentProvider object if it is running in the local process. This can |
| 182 | * be used if you know you are running in the same process as a provider, |
| 183 | * and want to get direct access to its implementation details. Most |
| 184 | * clients should not nor have a reason to use it. |
| 185 | * |
| 186 | * @param abstractInterface The ContentProvider interface that is to be |
| 187 | * coerced. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 188 | * @return If the IContentProvider is non-{@code null} and local, returns its actual |
| 189 | * ContentProvider instance. Otherwise returns {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | * @hide |
| 191 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 192 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | public static ContentProvider coerceToLocalContentProvider( |
| 194 | IContentProvider abstractInterface) { |
| 195 | if (abstractInterface instanceof Transport) { |
| 196 | return ((Transport)abstractInterface).getContentProvider(); |
| 197 | } |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Binder object that deals with remoting. |
| 203 | * |
| 204 | * @hide |
| 205 | */ |
| 206 | class Transport extends ContentProviderNative { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 207 | AppOpsManager mAppOpsManager = null; |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 208 | int mReadOp = AppOpsManager.OP_NONE; |
| 209 | int mWriteOp = AppOpsManager.OP_NONE; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | ContentProvider getContentProvider() { |
| 212 | return ContentProvider.this; |
| 213 | } |
| 214 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public String getProviderName() { |
| 217 | return getContentProvider().getClass().getName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 220 | @Override |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 221 | public Cursor query(String callingPkg, Uri uri, @Nullable String[] projection, |
| 222 | @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 223 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 224 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 225 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Svet Ganov | 7271f3e | 2015-04-23 10:16:53 -0700 | [diff] [blame] | 226 | // The caller has no access to the data, so return an empty cursor with |
| 227 | // the columns in the requested order. The caller may ask for an invalid |
| 228 | // column and we would not catch that but this is not a problem in practice. |
| 229 | // We do not call ContentProvider#query with a modified where clause since |
| 230 | // the implementation is not guaranteed to be backed by a SQL database, hence |
| 231 | // it may not handle properly the tautology where clause we would have created. |
Svet Ganov | a2147ec | 2015-04-27 17:00:44 -0700 | [diff] [blame] | 232 | if (projection != null) { |
| 233 | return new MatrixCursor(projection, 0); |
| 234 | } |
| 235 | |
| 236 | // Null projection means all columns but we have no idea which they are. |
| 237 | // However, the caller may be expecting to access them my index. Hence, |
| 238 | // we have to execute the query as if allowed to get a cursor with the |
| 239 | // columns. We then use the column names to return an empty cursor. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 240 | Cursor cursor = ContentProvider.this.query( |
| 241 | uri, projection, queryArgs, |
| 242 | CancellationSignal.fromTransport(cancellationSignal)); |
Makoto Onuki | 34bdcdb | 2015-06-12 17:14:57 -0700 | [diff] [blame] | 243 | if (cursor == null) { |
| 244 | return null; |
Svet Ganov | a2147ec | 2015-04-27 17:00:44 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Return an empty cursor for all columns. |
Makoto Onuki | 34bdcdb | 2015-06-12 17:14:57 -0700 | [diff] [blame] | 248 | return new MatrixCursor(cursor.getColumnNames(), 0); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 249 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 250 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 251 | try { |
| 252 | return ContentProvider.this.query( |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 253 | uri, projection, queryArgs, |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 254 | CancellationSignal.fromTransport(cancellationSignal)); |
| 255 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 256 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 260 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | public String getType(Uri uri) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 262 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 263 | uri = maybeGetUriWithoutUserId(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | return ContentProvider.this.getType(uri); |
| 265 | } |
| 266 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 267 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 268 | public Uri insert(String callingPkg, Uri uri, ContentValues initialValues) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 269 | uri = validateIncomingUri(uri); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 270 | int userId = getUserIdFromUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 271 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 272 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | d7960d1 | 2013-01-29 18:55:48 -0800 | [diff] [blame] | 273 | return rejectInsert(uri, initialValues); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 274 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 275 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 276 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 277 | return maybeAddUserId(ContentProvider.this.insert(uri, initialValues), userId); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 278 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 279 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 280 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 283 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 284 | public int bulkInsert(String callingPkg, Uri uri, ContentValues[] initialValues) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 285 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 286 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 287 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 288 | return 0; |
| 289 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 290 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 291 | try { |
| 292 | return ContentProvider.this.bulkInsert(uri, initialValues); |
| 293 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 294 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 295 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 298 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 299 | public ContentProviderResult[] applyBatch(String callingPkg, |
| 300 | ArrayList<ContentProviderOperation> operations) |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 301 | throws OperationApplicationException { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 302 | int numOperations = operations.size(); |
| 303 | final int[] userIds = new int[numOperations]; |
| 304 | for (int i = 0; i < numOperations; i++) { |
| 305 | ContentProviderOperation operation = operations.get(i); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 306 | Uri uri = operation.getUri(); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 307 | userIds[i] = getUserIdFromUri(uri); |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 308 | uri = validateIncomingUri(uri); |
| 309 | uri = maybeGetUriWithoutUserId(uri); |
| 310 | // Rebuild operation if we changed the Uri above |
| 311 | if (!Objects.equals(operation.getUri(), uri)) { |
| 312 | operation = new ContentProviderOperation(operation, uri); |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 313 | operations.set(i, operation); |
| 314 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 315 | if (operation.isReadOperation()) { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 316 | if (enforceReadPermission(callingPkg, uri, null) |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 317 | != AppOpsManager.MODE_ALLOWED) { |
| 318 | throw new OperationApplicationException("App op not allowed", 0); |
| 319 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 320 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 321 | if (operation.isWriteOperation()) { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 322 | if (enforceWritePermission(callingPkg, uri, null) |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 323 | != AppOpsManager.MODE_ALLOWED) { |
| 324 | throw new OperationApplicationException("App op not allowed", 0); |
| 325 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 328 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 329 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 330 | ContentProviderResult[] results = ContentProvider.this.applyBatch(operations); |
Jay Shrauner | ac2506c | 2014-12-15 12:28:25 -0800 | [diff] [blame] | 331 | if (results != null) { |
| 332 | for (int i = 0; i < results.length ; i++) { |
| 333 | if (userIds[i] != UserHandle.USER_CURRENT) { |
| 334 | // Adding the userId to the uri. |
| 335 | results[i] = new ContentProviderResult(results[i], userIds[i]); |
| 336 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | return results; |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 340 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 341 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 342 | } |
Fred Quintana | 6a8d533 | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 345 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 346 | public int delete(String callingPkg, Uri uri, String selection, String[] selectionArgs) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 347 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 348 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 349 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 350 | return 0; |
| 351 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 352 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 353 | try { |
| 354 | return ContentProvider.this.delete(uri, selection, selectionArgs); |
| 355 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 356 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 357 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 360 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 361 | public int update(String callingPkg, Uri uri, ContentValues values, String selection, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | String[] selectionArgs) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 363 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 364 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 365 | if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 366 | return 0; |
| 367 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 368 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 369 | try { |
| 370 | return ContentProvider.this.update(uri, values, selection, selectionArgs); |
| 371 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 372 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 373 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 376 | @Override |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 377 | public ParcelFileDescriptor openFile( |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 378 | String callingPkg, Uri uri, String mode, ICancellationSignal cancellationSignal, |
| 379 | IBinder callerToken) throws FileNotFoundException { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 380 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 381 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 382 | enforceFilePermission(callingPkg, uri, mode, callerToken); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 383 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 384 | try { |
| 385 | return ContentProvider.this.openFile( |
| 386 | uri, mode, CancellationSignal.fromTransport(cancellationSignal)); |
| 387 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 388 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 389 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 392 | @Override |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 393 | public AssetFileDescriptor openAssetFile( |
| 394 | String callingPkg, Uri uri, String mode, ICancellationSignal cancellationSignal) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | throws FileNotFoundException { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 396 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 397 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 398 | enforceFilePermission(callingPkg, uri, mode, null); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 399 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 400 | try { |
| 401 | return ContentProvider.this.openAssetFile( |
| 402 | uri, mode, CancellationSignal.fromTransport(cancellationSignal)); |
| 403 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 404 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 408 | @Override |
Scott Kennedy | 9f78f65 | 2015-03-01 15:29:25 -0800 | [diff] [blame] | 409 | public Bundle call( |
| 410 | String callingPkg, String method, @Nullable String arg, @Nullable Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 411 | Bundle.setDefusable(extras, true); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 412 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 413 | try { |
| 414 | return ContentProvider.this.call(method, arg, extras); |
| 415 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 416 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 417 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 420 | @Override |
| 421 | public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 422 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 423 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 424 | return ContentProvider.this.getStreamTypes(uri, mimeTypeFilter); |
| 425 | } |
| 426 | |
| 427 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 428 | public AssetFileDescriptor openTypedAssetFile(String callingPkg, Uri uri, String mimeType, |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 429 | Bundle opts, ICancellationSignal cancellationSignal) throws FileNotFoundException { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 430 | Bundle.setDefusable(opts, true); |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 431 | uri = validateIncomingUri(uri); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 432 | uri = maybeGetUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 433 | enforceFilePermission(callingPkg, uri, "r", null); |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 434 | final String original = setCallingPackage(callingPkg); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 435 | try { |
| 436 | return ContentProvider.this.openTypedAssetFile( |
| 437 | uri, mimeType, opts, CancellationSignal.fromTransport(cancellationSignal)); |
| 438 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 439 | setCallingPackage(original); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 440 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 443 | @Override |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 444 | public ICancellationSignal createCancellationSignal() { |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 445 | return CancellationSignal.createTransport(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 448 | @Override |
| 449 | public Uri canonicalize(String callingPkg, Uri uri) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 450 | uri = validateIncomingUri(uri); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 451 | int userId = getUserIdFromUri(uri); |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 452 | uri = getUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 453 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 454 | return null; |
| 455 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 456 | final String original = setCallingPackage(callingPkg); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 457 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 458 | return maybeAddUserId(ContentProvider.this.canonicalize(uri), userId); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 459 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 460 | setCallingPackage(original); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public Uri uncanonicalize(String callingPkg, Uri uri) { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 466 | uri = validateIncomingUri(uri); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 467 | int userId = getUserIdFromUri(uri); |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 468 | uri = getUriWithoutUserId(uri); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 469 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 470 | return null; |
| 471 | } |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 472 | final String original = setCallingPackage(callingPkg); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 473 | try { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 474 | return maybeAddUserId(ContentProvider.this.uncanonicalize(uri), userId); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 475 | } finally { |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 476 | setCallingPackage(original); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 480 | @Override |
| 481 | public boolean refresh(String callingPkg, Uri uri, Bundle args, |
| 482 | ICancellationSignal cancellationSignal) throws RemoteException { |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 483 | uri = validateIncomingUri(uri); |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 484 | uri = getUriWithoutUserId(uri); |
| 485 | if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) { |
| 486 | return false; |
| 487 | } |
| 488 | final String original = setCallingPackage(callingPkg); |
| 489 | try { |
| 490 | return ContentProvider.this.refresh(uri, args, |
| 491 | CancellationSignal.fromTransport(cancellationSignal)); |
| 492 | } finally { |
| 493 | setCallingPackage(original); |
| 494 | } |
| 495 | } |
| 496 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 497 | private void enforceFilePermission(String callingPkg, Uri uri, String mode, |
| 498 | IBinder callerToken) throws FileNotFoundException, SecurityException { |
Jeff Sharkey | ba76197 | 2013-02-28 15:57:36 -0800 | [diff] [blame] | 499 | if (mode != null && mode.indexOf('w') != -1) { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 500 | if (enforceWritePermission(callingPkg, uri, callerToken) |
| 501 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 502 | throw new FileNotFoundException("App op not allowed"); |
| 503 | } |
| 504 | } else { |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 505 | if (enforceReadPermission(callingPkg, uri, callerToken) |
| 506 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 507 | throw new FileNotFoundException("App op not allowed"); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 512 | private int enforceReadPermission(String callingPkg, Uri uri, IBinder callerToken) |
| 513 | throws SecurityException { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 514 | final int mode = enforceReadPermissionInner(uri, callingPkg, callerToken); |
| 515 | if (mode != MODE_ALLOWED) { |
| 516 | return mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 517 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 518 | |
| 519 | if (mReadOp != AppOpsManager.OP_NONE) { |
| 520 | return mAppOpsManager.noteProxyOp(mReadOp, callingPkg); |
| 521 | } |
| 522 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 523 | return AppOpsManager.MODE_ALLOWED; |
| 524 | } |
| 525 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 526 | private int enforceWritePermission(String callingPkg, Uri uri, IBinder callerToken) |
| 527 | throws SecurityException { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 528 | final int mode = enforceWritePermissionInner(uri, callingPkg, callerToken); |
| 529 | if (mode != MODE_ALLOWED) { |
| 530 | return mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 531 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 532 | |
| 533 | if (mWriteOp != AppOpsManager.OP_NONE) { |
| 534 | return mAppOpsManager.noteProxyOp(mWriteOp, callingPkg); |
| 535 | } |
| 536 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 537 | return AppOpsManager.MODE_ALLOWED; |
| 538 | } |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 539 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 540 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 541 | boolean checkUser(int pid, int uid, Context context) { |
| 542 | return UserHandle.getUserId(uid) == context.getUserId() |
Amith Yamasani | a6f4d58 | 2014-08-07 17:58:39 -0700 | [diff] [blame] | 543 | || mSingleUser |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 544 | || context.checkPermission(INTERACT_ACROSS_USERS, pid, uid) |
| 545 | == PERMISSION_GRANTED; |
| 546 | } |
| 547 | |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 548 | /** |
| 549 | * Verify that calling app holds both the given permission and any app-op |
| 550 | * associated with that permission. |
| 551 | */ |
| 552 | private int checkPermissionAndAppOp(String permission, String callingPkg, |
| 553 | IBinder callerToken) { |
| 554 | if (getContext().checkPermission(permission, Binder.getCallingPid(), Binder.getCallingUid(), |
| 555 | callerToken) != PERMISSION_GRANTED) { |
| 556 | return MODE_ERRORED; |
| 557 | } |
| 558 | |
| 559 | final int permOp = AppOpsManager.permissionToOpCode(permission); |
| 560 | if (permOp != AppOpsManager.OP_NONE) { |
| 561 | return mTransport.mAppOpsManager.noteProxyOp(permOp, callingPkg); |
| 562 | } |
| 563 | |
| 564 | return MODE_ALLOWED; |
| 565 | } |
| 566 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 567 | /** {@hide} */ |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 568 | protected int enforceReadPermissionInner(Uri uri, String callingPkg, IBinder callerToken) |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 569 | throws SecurityException { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 570 | final Context context = getContext(); |
| 571 | final int pid = Binder.getCallingPid(); |
| 572 | final int uid = Binder.getCallingUid(); |
| 573 | String missingPerm = null; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 574 | int strongestMode = MODE_ALLOWED; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 575 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 576 | if (UserHandle.isSameApp(uid, mMyUid)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 577 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 580 | if (mExported && checkUser(pid, uid, context)) { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 581 | final String componentPerm = getReadPermission(); |
| 582 | if (componentPerm != null) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 583 | final int mode = checkPermissionAndAppOp(componentPerm, callingPkg, callerToken); |
| 584 | if (mode == MODE_ALLOWED) { |
| 585 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 586 | } else { |
| 587 | missingPerm = componentPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 588 | strongestMode = Math.max(strongestMode, mode); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 589 | } |
Jeff Sharkey | e5d4933 | 2012-03-13 12:13:17 -0700 | [diff] [blame] | 590 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 591 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 592 | // track if unprotected read is allowed; any denied |
| 593 | // <path-permission> below removes this ability |
| 594 | boolean allowDefaultRead = (componentPerm == null); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 595 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 596 | final PathPermission[] pps = getPathPermissions(); |
| 597 | if (pps != null) { |
| 598 | final String path = uri.getPath(); |
| 599 | for (PathPermission pp : pps) { |
| 600 | final String pathPerm = pp.getReadPermission(); |
| 601 | if (pathPerm != null && pp.match(path)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 602 | final int mode = checkPermissionAndAppOp(pathPerm, callingPkg, callerToken); |
| 603 | if (mode == MODE_ALLOWED) { |
| 604 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 605 | } else { |
| 606 | // any denied <path-permission> means we lose |
| 607 | // default <provider> access. |
| 608 | allowDefaultRead = false; |
| 609 | missingPerm = pathPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 610 | strongestMode = Math.max(strongestMode, mode); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 615 | |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 616 | // if we passed <path-permission> checks above, and no default |
| 617 | // <provider> permission, then allow access. |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 618 | if (allowDefaultRead) return MODE_ALLOWED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 620 | |
| 621 | // last chance, check against any uri grants |
Amith Yamasani | 7d2d4fd | 2014-11-05 15:46:09 -0800 | [diff] [blame] | 622 | final int callingUserId = UserHandle.getUserId(uid); |
| 623 | final Uri userUri = (mSingleUser && !UserHandle.isSameUser(mMyUid, uid)) |
| 624 | ? maybeAddUserId(uri, callingUserId) : uri; |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 625 | if (context.checkUriPermission(userUri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 626 | callerToken) == PERMISSION_GRANTED) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 627 | return MODE_ALLOWED; |
| 628 | } |
| 629 | |
| 630 | // If the worst denial we found above was ignored, then pass that |
| 631 | // ignored through; otherwise we assume it should be a real error below. |
| 632 | if (strongestMode == MODE_IGNORED) { |
| 633 | return MODE_IGNORED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Sharkey | c0cc220 | 2017-03-21 19:25:34 -0600 | [diff] [blame] | 636 | final String suffix; |
| 637 | if (android.Manifest.permission.MANAGE_DOCUMENTS.equals(mReadPermission)) { |
| 638 | suffix = " requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs"; |
| 639 | } else if (mExported) { |
| 640 | suffix = " requires " + missingPerm + ", or grantUriPermission()"; |
| 641 | } else { |
| 642 | suffix = " requires the provider be exported, or grantUriPermission()"; |
| 643 | } |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 644 | throw new SecurityException("Permission Denial: reading " |
| 645 | + ContentProvider.this.getClass().getName() + " uri " + uri + " from pid=" + pid |
Jeff Sharkey | c0cc220 | 2017-03-21 19:25:34 -0600 | [diff] [blame] | 646 | + ", uid=" + uid + suffix); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /** {@hide} */ |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 650 | protected int enforceWritePermissionInner(Uri uri, String callingPkg, IBinder callerToken) |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 651 | throws SecurityException { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 652 | final Context context = getContext(); |
| 653 | final int pid = Binder.getCallingPid(); |
| 654 | final int uid = Binder.getCallingUid(); |
| 655 | String missingPerm = null; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 656 | int strongestMode = MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 657 | |
| 658 | if (UserHandle.isSameApp(uid, mMyUid)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 659 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 662 | if (mExported && checkUser(pid, uid, context)) { |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 663 | final String componentPerm = getWritePermission(); |
| 664 | if (componentPerm != null) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 665 | final int mode = checkPermissionAndAppOp(componentPerm, callingPkg, callerToken); |
| 666 | if (mode == MODE_ALLOWED) { |
| 667 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 668 | } else { |
| 669 | missingPerm = componentPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 670 | strongestMode = Math.max(strongestMode, mode); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | // track if unprotected write is allowed; any denied |
| 675 | // <path-permission> below removes this ability |
| 676 | boolean allowDefaultWrite = (componentPerm == null); |
| 677 | |
| 678 | final PathPermission[] pps = getPathPermissions(); |
| 679 | if (pps != null) { |
| 680 | final String path = uri.getPath(); |
| 681 | for (PathPermission pp : pps) { |
| 682 | final String pathPerm = pp.getWritePermission(); |
| 683 | if (pathPerm != null && pp.match(path)) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 684 | final int mode = checkPermissionAndAppOp(pathPerm, callingPkg, callerToken); |
| 685 | if (mode == MODE_ALLOWED) { |
| 686 | return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 687 | } else { |
| 688 | // any denied <path-permission> means we lose |
| 689 | // default <provider> access. |
| 690 | allowDefaultWrite = false; |
| 691 | missingPerm = pathPerm; |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 692 | strongestMode = Math.max(strongestMode, mode); |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | // if we passed <path-permission> checks above, and no default |
| 699 | // <provider> permission, then allow access. |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 700 | if (allowDefaultWrite) return MODE_ALLOWED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | // last chance, check against any uri grants |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 704 | if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_WRITE_URI_PERMISSION, |
| 705 | callerToken) == PERMISSION_GRANTED) { |
Jeff Sharkey | 0e621c3 | 2015-07-24 15:10:20 -0700 | [diff] [blame] | 706 | return MODE_ALLOWED; |
| 707 | } |
| 708 | |
| 709 | // If the worst denial we found above was ignored, then pass that |
| 710 | // ignored through; otherwise we assume it should be a real error below. |
| 711 | if (strongestMode == MODE_IGNORED) { |
| 712 | return MODE_IGNORED; |
Jeff Sharkey | 8a2998e | 2013-10-31 14:55:44 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | final String failReason = mExported |
| 716 | ? " requires " + missingPerm + ", or grantUriPermission()" |
| 717 | : " requires the provider be exported, or grantUriPermission()"; |
| 718 | throw new SecurityException("Permission Denial: writing " |
| 719 | + ContentProvider.this.getClass().getName() + " uri " + uri + " from pid=" + pid |
| 720 | + ", uid=" + uid + failReason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
| 722 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 724 | * Retrieves the Context this provider is running in. Only available once |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 725 | * {@link #onCreate} has been called -- this will return {@code null} in the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | * constructor. |
| 727 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 728 | public final @Nullable Context getContext() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | return mContext; |
| 730 | } |
| 731 | |
| 732 | /** |
Jeff Sharkey | 72e2e35 | 2013-09-09 18:52:48 -0700 | [diff] [blame] | 733 | * Set the calling package, returning the current value (or {@code null}) |
| 734 | * which can be used later to restore the previous state. |
| 735 | */ |
| 736 | private String setCallingPackage(String callingPackage) { |
| 737 | final String original = mCallingPackage.get(); |
| 738 | mCallingPackage.set(callingPackage); |
| 739 | return original; |
| 740 | } |
| 741 | |
| 742 | /** |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 743 | * Return the package name of the caller that initiated the request being |
| 744 | * processed on the current thread. The returned package will have been |
| 745 | * verified to belong to the calling UID. Returns {@code null} if not |
| 746 | * currently processing a request. |
| 747 | * <p> |
| 748 | * This will always return {@code null} when processing |
| 749 | * {@link #getType(Uri)} or {@link #getStreamTypes(Uri, String)} requests. |
| 750 | * |
| 751 | * @see Binder#getCallingUid() |
| 752 | * @see Context#grantUriPermission(String, Uri, int) |
| 753 | * @throws SecurityException if the calling package doesn't belong to the |
| 754 | * calling UID. |
| 755 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 756 | public final @Nullable String getCallingPackage() { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 757 | final String pkg = mCallingPackage.get(); |
| 758 | if (pkg != null) { |
| 759 | mTransport.mAppOpsManager.checkPackage(Binder.getCallingUid(), pkg); |
| 760 | } |
| 761 | return pkg; |
| 762 | } |
| 763 | |
| 764 | /** |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 765 | * Change the authorities of the ContentProvider. |
| 766 | * This is normally set for you from its manifest information when the provider is first |
| 767 | * created. |
| 768 | * @hide |
| 769 | * @param authorities the semi-colon separated authorities of the ContentProvider. |
| 770 | */ |
| 771 | protected final void setAuthorities(String authorities) { |
Nicolas Prevot | 6e412ad | 2014-09-08 18:26:55 +0100 | [diff] [blame] | 772 | if (authorities != null) { |
| 773 | if (authorities.indexOf(';') == -1) { |
| 774 | mAuthority = authorities; |
| 775 | mAuthorities = null; |
| 776 | } else { |
| 777 | mAuthority = null; |
| 778 | mAuthorities = authorities.split(";"); |
| 779 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
| 783 | /** @hide */ |
| 784 | protected final boolean matchesOurAuthorities(String authority) { |
| 785 | if (mAuthority != null) { |
| 786 | return mAuthority.equals(authority); |
| 787 | } |
Nicolas Prevot | 6e412ad | 2014-09-08 18:26:55 +0100 | [diff] [blame] | 788 | if (mAuthorities != null) { |
| 789 | int length = mAuthorities.length; |
| 790 | for (int i = 0; i < length; i++) { |
| 791 | if (mAuthorities[i].equals(authority)) return true; |
| 792 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 793 | } |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | * Change the permission required to read data from the content |
| 800 | * provider. This is normally set for you from its manifest information |
| 801 | * when the provider is first created. |
| 802 | * |
| 803 | * @param permission Name of the permission required for read-only access. |
| 804 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 805 | protected final void setReadPermission(@Nullable String permission) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | mReadPermission = permission; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Return the name of the permission required for read-only access to |
| 811 | * this content provider. This method can be called from multiple |
| 812 | * threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 813 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 814 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 816 | public final @Nullable String getReadPermission() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | return mReadPermission; |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Change the permission required to read and write data in the content |
| 822 | * provider. This is normally set for you from its manifest information |
| 823 | * when the provider is first created. |
| 824 | * |
| 825 | * @param permission Name of the permission required for read/write access. |
| 826 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 827 | protected final void setWritePermission(@Nullable String permission) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | mWritePermission = permission; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Return the name of the permission required for read/write access to |
| 833 | * this content provider. This method can be called from multiple |
| 834 | * threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 835 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 836 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 838 | public final @Nullable String getWritePermission() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | return mWritePermission; |
| 840 | } |
| 841 | |
| 842 | /** |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 843 | * Change the path-based permission required to read and/or write data in |
| 844 | * the content provider. This is normally set for you from its manifest |
| 845 | * information when the provider is first created. |
| 846 | * |
| 847 | * @param permissions Array of path permission descriptions. |
| 848 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 849 | protected final void setPathPermissions(@Nullable PathPermission[] permissions) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 850 | mPathPermissions = permissions; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Return the path-based permissions required for read and/or write access to |
| 855 | * this content provider. This method can be called from multiple |
| 856 | * threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 857 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 858 | * and Threads</a>. |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 859 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 860 | public final @Nullable PathPermission[] getPathPermissions() { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 861 | return mPathPermissions; |
| 862 | } |
| 863 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 864 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 865 | @UnsupportedAppUsage |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 866 | public final void setAppOps(int readOp, int writeOp) { |
Dianne Hackborn | 7e6f976 | 2013-02-26 13:35:11 -0800 | [diff] [blame] | 867 | if (!mNoPerms) { |
Dianne Hackborn | 7e6f976 | 2013-02-26 13:35:11 -0800 | [diff] [blame] | 868 | mTransport.mReadOp = readOp; |
| 869 | mTransport.mWriteOp = writeOp; |
| 870 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 871 | } |
| 872 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 873 | /** @hide */ |
| 874 | public AppOpsManager getAppOpsManager() { |
| 875 | return mTransport.mAppOpsManager; |
| 876 | } |
| 877 | |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 878 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 879 | * Implement this to initialize your content provider on startup. |
| 880 | * This method is called for all registered content providers on the |
| 881 | * application main thread at application launch time. It must not perform |
| 882 | * lengthy operations, or application startup will be delayed. |
| 883 | * |
| 884 | * <p>You should defer nontrivial initialization (such as opening, |
| 885 | * upgrading, and scanning databases) until the content provider is used |
| 886 | * (via {@link #query}, {@link #insert}, etc). Deferred initialization |
| 887 | * keeps application startup fast, avoids unnecessary work if the provider |
| 888 | * turns out not to be needed, and stops database errors (such as a full |
| 889 | * disk) from halting application launch. |
| 890 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 891 | * <p>If you use SQLite, {@link android.database.sqlite.SQLiteOpenHelper} |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 892 | * is a helpful utility class that makes it easy to manage databases, |
| 893 | * and will automatically defer opening until first use. If you do use |
| 894 | * SQLiteOpenHelper, make sure to avoid calling |
| 895 | * {@link android.database.sqlite.SQLiteOpenHelper#getReadableDatabase} or |
| 896 | * {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase} |
| 897 | * from this method. (Instead, override |
| 898 | * {@link android.database.sqlite.SQLiteOpenHelper#onOpen} to initialize the |
| 899 | * database when it is first opened.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | * |
| 901 | * @return true if the provider was successfully loaded, false otherwise |
| 902 | */ |
| 903 | public abstract boolean onCreate(); |
| 904 | |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 905 | /** |
| 906 | * {@inheritDoc} |
| 907 | * This method is always called on the application main thread, and must |
| 908 | * not perform lengthy operations. |
| 909 | * |
| 910 | * <p>The default content provider implementation does nothing. |
| 911 | * Override this method to take appropriate action. |
| 912 | * (Content providers do not usually care about things like screen |
| 913 | * orientation, but may want to know about locale changes.) |
| 914 | */ |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 915 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | public void onConfigurationChanged(Configuration newConfig) { |
| 917 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 918 | |
| 919 | /** |
| 920 | * {@inheritDoc} |
| 921 | * This method is always called on the application main thread, and must |
| 922 | * not perform lengthy operations. |
| 923 | * |
| 924 | * <p>The default content provider implementation does nothing. |
| 925 | * Subclasses may override this method to take appropriate action. |
| 926 | */ |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 927 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | public void onLowMemory() { |
| 929 | } |
| 930 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 931 | @Override |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 932 | public void onTrimMemory(int level) { |
| 933 | } |
| 934 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 936 | * Implement this to handle query requests from clients. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 937 | * |
| 938 | * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher should override |
| 939 | * {@link #query(Uri, String[], Bundle, CancellationSignal)} and provide a stub |
| 940 | * implementation of this method. |
| 941 | * |
| 942 | * <p>This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 943 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 944 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | * <p> |
| 946 | * Example client call:<p> |
| 947 | * <pre>// Request a specific record. |
| 948 | * Cursor managedCursor = managedQuery( |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 949 | ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | projection, // Which columns to return. |
| 951 | null, // WHERE clause. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 952 | null, // WHERE clause value substitution |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | People.NAME + " ASC"); // Sort order.</pre> |
| 954 | * Example implementation:<p> |
| 955 | * <pre>// SQLiteQueryBuilder is a helper class that creates the |
| 956 | // proper SQL syntax for us. |
| 957 | SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder(); |
| 958 | |
| 959 | // Set the table we're querying. |
| 960 | qBuilder.setTables(DATABASE_TABLE_NAME); |
| 961 | |
| 962 | // If the query ends in a specific record number, we're |
| 963 | // being asked for a specific record, so set the |
| 964 | // WHERE clause in our query. |
| 965 | if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){ |
| 966 | qBuilder.appendWhere("_id=" + uri.getPathLeafId()); |
| 967 | } |
| 968 | |
| 969 | // Make the query. |
| 970 | Cursor c = qBuilder.query(mDb, |
| 971 | projection, |
| 972 | selection, |
| 973 | selectionArgs, |
| 974 | groupBy, |
| 975 | having, |
| 976 | sortOrder); |
| 977 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 978 | return c;</pre> |
| 979 | * |
| 980 | * @param uri The URI to query. This will be the full URI sent by the client; |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 981 | * if the client is requesting a specific record, the URI will end in a record number |
| 982 | * that the implementation should parse and add to a WHERE or HAVING clause, specifying |
| 983 | * that _id value. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | * @param projection The list of columns to put into the cursor. If |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 985 | * {@code null} all columns are included. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | * @param selection A selection criteria to apply when filtering rows. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 987 | * If {@code null} then all rows are included. |
Alan Jones | 81a476f | 2009-05-21 12:32:17 +1000 | [diff] [blame] | 988 | * @param selectionArgs You may include ?s in selection, which will be replaced by |
| 989 | * the values from selectionArgs, in order that they appear in the selection. |
| 990 | * The values will be bound as Strings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | * @param sortOrder How the rows in the cursor should be sorted. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 992 | * If {@code null} then the provider is free to define the sort order. |
| 993 | * @return a Cursor or {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 995 | public abstract @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection, |
| 996 | @Nullable String selection, @Nullable String[] selectionArgs, |
| 997 | @Nullable String sortOrder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | |
Fred Quintana | 5bba632 | 2009-10-05 14:21:12 -0700 | [diff] [blame] | 999 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 1000 | * Implement this to handle query requests from clients with support for cancellation. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1001 | * |
| 1002 | * <p>Apps targeting {@link android.os.Build.VERSION_CODES#O} or higher should override |
| 1003 | * {@link #query(Uri, String[], Bundle, CancellationSignal)} instead of this method. |
| 1004 | * |
| 1005 | * <p>This method can be called from multiple threads, as described in |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1006 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1007 | * and Threads</a>. |
| 1008 | * <p> |
| 1009 | * Example client call:<p> |
| 1010 | * <pre>// Request a specific record. |
| 1011 | * Cursor managedCursor = managedQuery( |
| 1012 | ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2), |
| 1013 | projection, // Which columns to return. |
| 1014 | null, // WHERE clause. |
| 1015 | null, // WHERE clause value substitution |
| 1016 | People.NAME + " ASC"); // Sort order.</pre> |
| 1017 | * Example implementation:<p> |
| 1018 | * <pre>// SQLiteQueryBuilder is a helper class that creates the |
| 1019 | // proper SQL syntax for us. |
| 1020 | SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder(); |
| 1021 | |
| 1022 | // Set the table we're querying. |
| 1023 | qBuilder.setTables(DATABASE_TABLE_NAME); |
| 1024 | |
| 1025 | // If the query ends in a specific record number, we're |
| 1026 | // being asked for a specific record, so set the |
| 1027 | // WHERE clause in our query. |
| 1028 | if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){ |
| 1029 | qBuilder.appendWhere("_id=" + uri.getPathLeafId()); |
| 1030 | } |
| 1031 | |
| 1032 | // Make the query. |
| 1033 | Cursor c = qBuilder.query(mDb, |
| 1034 | projection, |
| 1035 | selection, |
| 1036 | selectionArgs, |
| 1037 | groupBy, |
| 1038 | having, |
| 1039 | sortOrder); |
| 1040 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 1041 | return c;</pre> |
| 1042 | * <p> |
| 1043 | * If you implement this method then you must also implement the version of |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 1044 | * {@link #query(Uri, String[], String, String[], String)} that does not take a cancellation |
| 1045 | * signal to ensure correct operation on older versions of the Android Framework in |
| 1046 | * which the cancellation signal overload was not available. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1047 | * |
| 1048 | * @param uri The URI to query. This will be the full URI sent by the client; |
| 1049 | * if the client is requesting a specific record, the URI will end in a record number |
| 1050 | * that the implementation should parse and add to a WHERE or HAVING clause, specifying |
| 1051 | * that _id value. |
| 1052 | * @param projection The list of columns to put into the cursor. If |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1053 | * {@code null} all columns are included. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1054 | * @param selection A selection criteria to apply when filtering rows. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1055 | * If {@code null} then all rows are included. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1056 | * @param selectionArgs You may include ?s in selection, which will be replaced by |
| 1057 | * the values from selectionArgs, in order that they appear in the selection. |
| 1058 | * The values will be bound as Strings. |
| 1059 | * @param sortOrder How the rows in the cursor should be sorted. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1060 | * If {@code null} then the provider is free to define the sort order. |
| 1061 | * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if none. |
Jeff Sharkey | 67f9d50 | 2017-08-05 13:49:13 -0600 | [diff] [blame] | 1062 | * If the operation is canceled, then {@link android.os.OperationCanceledException} will be thrown |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1063 | * when the query is executed. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1064 | * @return a Cursor or {@code null}. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1065 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1066 | public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection, |
| 1067 | @Nullable String selection, @Nullable String[] selectionArgs, |
| 1068 | @Nullable String sortOrder, @Nullable CancellationSignal cancellationSignal) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1069 | return query(uri, projection, selection, selectionArgs, sortOrder); |
| 1070 | } |
| 1071 | |
| 1072 | /** |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1073 | * Implement this to handle query requests where the arguments are packed into a {@link Bundle}. |
| 1074 | * Arguments may include traditional SQL style query arguments. When present these |
| 1075 | * should be handled according to the contract established in |
| 1076 | * {@link #query(Uri, String[], String, String[], String, CancellationSignal). |
| 1077 | * |
| 1078 | * <p>Traditional SQL arguments can be found in the bundle using the following keys: |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1079 | * <li>{@link ContentResolver#QUERY_ARG_SQL_SELECTION} |
| 1080 | * <li>{@link ContentResolver#QUERY_ARG_SQL_SELECTION_ARGS} |
| 1081 | * <li>{@link ContentResolver#QUERY_ARG_SQL_SORT_ORDER} |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1082 | * |
Steve McKay | 76b2770 | 2017-04-24 12:07:53 -0700 | [diff] [blame] | 1083 | * <p>This method can be called from multiple threads, as described in |
| 1084 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1085 | * and Threads</a>. |
| 1086 | * |
| 1087 | * <p> |
| 1088 | * Example client call:<p> |
| 1089 | * <pre>// Request 20 records starting at row index 30. |
| 1090 | Bundle queryArgs = new Bundle(); |
| 1091 | queryArgs.putInt(ContentResolver.QUERY_ARG_OFFSET, 30); |
| 1092 | queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20); |
| 1093 | |
| 1094 | Cursor cursor = getContentResolver().query( |
| 1095 | contentUri, // Content Uri is specific to individual content providers. |
| 1096 | projection, // String[] describing which columns to return. |
| 1097 | queryArgs, // Query arguments. |
| 1098 | null); // Cancellation signal.</pre> |
| 1099 | * |
| 1100 | * Example implementation:<p> |
| 1101 | * <pre> |
| 1102 | |
| 1103 | int recordsetSize = 0x1000; // Actual value is implementation specific. |
| 1104 | queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY; // ensure queryArgs is non-null |
| 1105 | |
| 1106 | int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0); |
| 1107 | int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MIN_VALUE); |
| 1108 | |
| 1109 | MatrixCursor c = new MatrixCursor(PROJECTION, limit); |
| 1110 | |
| 1111 | // Calculate the number of items to include in the cursor. |
| 1112 | int numItems = MathUtils.constrain(recordsetSize - offset, 0, limit); |
| 1113 | |
| 1114 | // Build the paged result set.... |
| 1115 | for (int i = offset; i < offset + numItems; i++) { |
| 1116 | // populate row from your data. |
| 1117 | } |
| 1118 | |
| 1119 | Bundle extras = new Bundle(); |
| 1120 | c.setExtras(extras); |
| 1121 | |
| 1122 | // Any QUERY_ARG_* key may be included if honored. |
| 1123 | // In an actual implementation, include only keys that are both present in queryArgs |
| 1124 | // and reflected in the Cursor output. For example, if QUERY_ARG_OFFSET were included |
| 1125 | // in queryArgs, but was ignored because it contained an invalid value (like –273), |
| 1126 | // then QUERY_ARG_OFFSET should be omitted. |
| 1127 | extras.putStringArray(ContentResolver.EXTRA_HONORED_ARGS, new String[] { |
| 1128 | ContentResolver.QUERY_ARG_OFFSET, |
| 1129 | ContentResolver.QUERY_ARG_LIMIT |
| 1130 | }); |
| 1131 | |
| 1132 | extras.putInt(ContentResolver.EXTRA_TOTAL_COUNT, recordsetSize); |
| 1133 | |
| 1134 | cursor.setNotificationUri(getContext().getContentResolver(), uri); |
| 1135 | |
| 1136 | return cursor;</pre> |
| 1137 | * <p> |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1138 | * @see #query(Uri, String[], String, String[], String, CancellationSignal) for |
| 1139 | * implementation details. |
| 1140 | * |
| 1141 | * @param uri The URI to query. This will be the full URI sent by the client. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1142 | * @param projection The list of columns to put into the cursor. |
| 1143 | * If {@code null} provide a default set of columns. |
| 1144 | * @param queryArgs A Bundle containing all additional information necessary for the query. |
| 1145 | * Values in the Bundle may include SQL style arguments. |
| 1146 | * @param cancellationSignal A signal to cancel the operation in progress, |
| 1147 | * or {@code null}. |
| 1148 | * @return a Cursor or {@code null}. |
| 1149 | */ |
| 1150 | public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection, |
| 1151 | @Nullable Bundle queryArgs, @Nullable CancellationSignal cancellationSignal) { |
| 1152 | queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY; |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1153 | |
Steve McKay | d7ece9f | 2017-01-12 16:59:59 -0800 | [diff] [blame] | 1154 | // if client doesn't supply an SQL sort order argument, attempt to build one from |
| 1155 | // QUERY_ARG_SORT* arguments. |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1156 | String sortClause = queryArgs.getString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER); |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1157 | if (sortClause == null && queryArgs.containsKey(ContentResolver.QUERY_ARG_SORT_COLUMNS)) { |
| 1158 | sortClause = ContentResolver.createSqlSortClause(queryArgs); |
| 1159 | } |
| 1160 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1161 | return query( |
| 1162 | uri, |
| 1163 | projection, |
Steve McKay | 29c3f68 | 2016-12-16 14:52:59 -0800 | [diff] [blame] | 1164 | queryArgs.getString(ContentResolver.QUERY_ARG_SQL_SELECTION), |
| 1165 | queryArgs.getStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS), |
| 1166 | sortClause, |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1167 | cancellationSignal); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1171 | * Implement this to handle requests for the MIME type of the data at the |
| 1172 | * given URI. The returned MIME type should start with |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | * <code>vnd.android.cursor.item</code> for a single record, |
| 1174 | * or <code>vnd.android.cursor.dir/</code> for multiple items. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1175 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1176 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1177 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | * |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1179 | * <p>Note that there are no permissions needed for an application to |
| 1180 | * access this information; if your content provider requires read and/or |
| 1181 | * write permissions, or is not exported, all applications can still call |
| 1182 | * this method regardless of their access permissions. This allows them |
| 1183 | * to retrieve the MIME type for a URI when dispatching intents. |
| 1184 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | * @param uri the URI to query. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1186 | * @return a MIME type string, or {@code null} if there is no type. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1188 | public abstract @Nullable String getType(@NonNull Uri uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | |
| 1190 | /** |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1191 | * Implement this to support canonicalization of URIs that refer to your |
| 1192 | * content provider. A canonical URI is one that can be transported across |
| 1193 | * devices, backup/restore, and other contexts, and still be able to refer |
| 1194 | * to the same data item. Typically this is implemented by adding query |
| 1195 | * params to the URI allowing the content provider to verify that an incoming |
| 1196 | * canonical URI references the same data as it was originally intended for and, |
| 1197 | * if it doesn't, to find that data (if it exists) in the current environment. |
| 1198 | * |
| 1199 | * <p>For example, if the content provider holds people and a normal URI in it |
| 1200 | * is created with a row index into that people database, the cananical representation |
| 1201 | * may have an additional query param at the end which specifies the name of the |
| 1202 | * person it is intended for. Later calls into the provider with that URI will look |
| 1203 | * up the row of that URI's base index and, if it doesn't match or its entry's |
| 1204 | * name doesn't match the name in the query param, perform a query on its database |
| 1205 | * to find the correct row to operate on.</p> |
| 1206 | * |
| 1207 | * <p>If you implement support for canonical URIs, <b>all</b> incoming calls with |
| 1208 | * URIs (including this one) must perform this verification and recovery of any |
| 1209 | * canonical URIs they receive. In addition, you must also implement |
| 1210 | * {@link #uncanonicalize} to strip the canonicalization of any of these URIs.</p> |
| 1211 | * |
| 1212 | * <p>The default implementation of this method returns null, indicating that |
| 1213 | * canonical URIs are not supported.</p> |
| 1214 | * |
| 1215 | * @param url The Uri to canonicalize. |
| 1216 | * |
| 1217 | * @return Return the canonical representation of <var>url</var>, or null if |
| 1218 | * canonicalization of that Uri is not supported. |
| 1219 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1220 | public @Nullable Uri canonicalize(@NonNull Uri url) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1221 | return null; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * Remove canonicalization from canonical URIs previously returned by |
| 1226 | * {@link #canonicalize}. For example, if your implementation is to add |
| 1227 | * a query param to canonicalize a URI, this method can simply trip any |
| 1228 | * query params on the URI. The default implementation always returns the |
| 1229 | * same <var>url</var> that was passed in. |
| 1230 | * |
| 1231 | * @param url The Uri to remove any canonicalization from. |
| 1232 | * |
Dianne Hackborn | b3ac67a | 2013-09-11 11:02:24 -0700 | [diff] [blame] | 1233 | * @return Return the non-canonical representation of <var>url</var>, return |
| 1234 | * the <var>url</var> as-is if there is nothing to do, or return null if |
| 1235 | * the data identified by the canonical representation can not be found in |
| 1236 | * the current environment. |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1237 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1238 | public @Nullable Uri uncanonicalize(@NonNull Uri url) { |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 1239 | return url; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 1243 | * Implement this to support refresh of content identified by {@code uri}. By default, this |
| 1244 | * method returns false; providers who wish to implement this should return true to signal the |
| 1245 | * client that the provider has tried refreshing with its own implementation. |
| 1246 | * <p> |
| 1247 | * This allows clients to request an explicit refresh of content identified by {@code uri}. |
| 1248 | * <p> |
| 1249 | * Client code should only invoke this method when there is a strong indication (such as a user |
| 1250 | * initiated pull to refresh gesture) that the content is stale. |
| 1251 | * <p> |
| 1252 | * Remember to send {@link ContentResolver#notifyChange(Uri, android.database.ContentObserver)} |
| 1253 | * notifications when content changes. |
| 1254 | * |
| 1255 | * @param uri The Uri identifying the data to refresh. |
| 1256 | * @param args Additional options from the client. The definitions of these are specific to the |
| 1257 | * content provider being called. |
| 1258 | * @param cancellationSignal A signal to cancel the operation in progress, or {@code null} if |
| 1259 | * none. For example, if you called refresh on a particular uri, you should call |
| 1260 | * {@link CancellationSignal#throwIfCanceled()} to check whether the client has |
| 1261 | * canceled the refresh request. |
| 1262 | * @return true if the provider actually tried refreshing. |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 1263 | */ |
| 1264 | public boolean refresh(Uri uri, @Nullable Bundle args, |
| 1265 | @Nullable CancellationSignal cancellationSignal) { |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
Dianne Hackborn | d7960d1 | 2013-01-29 18:55:48 -0800 | [diff] [blame] | 1270 | * @hide |
| 1271 | * Implementation when a caller has performed an insert on the content |
| 1272 | * provider, but that call has been rejected for the operation given |
| 1273 | * to {@link #setAppOps(int, int)}. The default implementation simply |
| 1274 | * returns a dummy URI that is the base URI with a 0 path element |
| 1275 | * appended. |
| 1276 | */ |
| 1277 | public Uri rejectInsert(Uri uri, ContentValues values) { |
| 1278 | // If not allowed, we need to return some reasonable URI. Maybe the |
| 1279 | // content provider should be responsible for this, but for now we |
| 1280 | // will just return the base URI with a dummy '0' tagged on to it. |
| 1281 | // You shouldn't be able to read if you can't write, anyway, so it |
| 1282 | // shouldn't matter much what is returned. |
| 1283 | return uri.buildUpon().appendPath("0").build(); |
| 1284 | } |
| 1285 | |
| 1286 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1287 | * Implement this to handle requests to insert a new row. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 1289 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1290 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1291 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1292 | * and Threads</a>. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1293 | * @param uri The content:// URI of the insertion request. This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | * @param values A set of column_name/value pairs to add to the database. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1295 | * This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | * @return The URI for the newly inserted item. |
| 1297 | */ |
Jeff Sharkey | 34796bd | 2015-06-11 21:55:32 -0700 | [diff] [blame] | 1298 | public abstract @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | |
| 1300 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1301 | * Override this to handle requests to insert a set of new rows, or the |
| 1302 | * default implementation will iterate over the values and call |
| 1303 | * {@link #insert} on each of them. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 1305 | * after inserting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1306 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1307 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1308 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | * |
| 1310 | * @param uri The content:// URI of the insertion request. |
| 1311 | * @param values An array of sets of column_name/value pairs to add to the database. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1312 | * This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | * @return The number of values that were inserted. |
| 1314 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1315 | public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | int numValues = values.length; |
| 1317 | for (int i = 0; i < numValues; i++) { |
| 1318 | insert(uri, values[i]); |
| 1319 | } |
| 1320 | return numValues; |
| 1321 | } |
| 1322 | |
| 1323 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1324 | * Implement this to handle requests to delete one or more rows. |
| 1325 | * The implementation should apply the selection clause when performing |
| 1326 | * deletion, allowing the operation to affect multiple rows in a directory. |
Taeho Kim | bd88de4 | 2013-10-28 15:08:53 +0900 | [diff] [blame] | 1327 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | * after deleting. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1329 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1330 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1331 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | * |
| 1333 | * <p>The implementation is responsible for parsing out a row ID at the end |
| 1334 | * of the URI, if a specific row is being deleted. That is, the client would |
| 1335 | * pass in <code>content://contacts/people/22</code> and the implementation is |
| 1336 | * responsible for parsing the record number (22) when creating a SQL statement. |
| 1337 | * |
| 1338 | * @param uri The full URI to query, including a row ID (if a specific record is requested). |
| 1339 | * @param selection An optional restriction to apply to rows when deleting. |
| 1340 | * @return The number of rows affected. |
| 1341 | * @throws SQLException |
| 1342 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1343 | public abstract int delete(@NonNull Uri uri, @Nullable String selection, |
| 1344 | @Nullable String[] selectionArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | |
| 1346 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1347 | * Implement this to handle requests to update one or more rows. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1348 | * The implementation should update all rows matching the selection |
| 1349 | * to set the columns according to the provided values map. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | * As a courtesy, call {@link ContentResolver#notifyChange(android.net.Uri ,android.database.ContentObserver) notifyChange()} |
| 1351 | * after updating. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1352 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1353 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1354 | * and Threads</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | * |
| 1356 | * @param uri The URI to query. This can potentially have a record ID if this |
| 1357 | * is an update request for a specific record. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1358 | * @param values A set of column_name/value pairs to update in the database. |
| 1359 | * This must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | * @param selection An optional filter to match rows to update. |
| 1361 | * @return the number of rows affected. |
| 1362 | */ |
Jeff Sharkey | 34796bd | 2015-06-11 21:55:32 -0700 | [diff] [blame] | 1363 | public abstract int update(@NonNull Uri uri, @Nullable ContentValues values, |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1364 | @Nullable String selection, @Nullable String[] selectionArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | |
| 1366 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1367 | * Override this to handle requests to open a file blob. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1368 | * The default implementation always throws {@link FileNotFoundException}. |
| 1369 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1370 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1371 | * and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1372 | * |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1373 | * <p>This method returns a ParcelFileDescriptor, which is returned directly |
| 1374 | * to the caller. This way large data (such as images and documents) can be |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1375 | * returned without copying the content. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | * |
| 1377 | * <p>The returned ParcelFileDescriptor is owned by the caller, so it is |
| 1378 | * their responsibility to close it when done. That is, the implementation |
| 1379 | * of this method should create a new ParcelFileDescriptor for each call. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1380 | * <p> |
| 1381 | * If opened with the exclusive "r" or "w" modes, the returned |
| 1382 | * ParcelFileDescriptor can be a pipe or socket pair to enable streaming |
| 1383 | * of data. Opening with the "rw" or "rwt" modes implies a file on disk that |
| 1384 | * supports seeking. |
| 1385 | * <p> |
| 1386 | * If you need to detect when the returned ParcelFileDescriptor has been |
| 1387 | * closed, or if the remote process has crashed or encountered some other |
| 1388 | * error, you can use {@link ParcelFileDescriptor#open(File, int, |
| 1389 | * android.os.Handler, android.os.ParcelFileDescriptor.OnCloseListener)}, |
| 1390 | * {@link ParcelFileDescriptor#createReliablePipe()}, or |
| 1391 | * {@link ParcelFileDescriptor#createReliableSocketPair()}. |
Jeff Sharkey | b31afd2 | 2017-06-12 14:17:10 -0600 | [diff] [blame] | 1392 | * <p> |
| 1393 | * If you need to return a large file that isn't backed by a real file on |
| 1394 | * disk, such as a file on a network share or cloud storage service, |
| 1395 | * consider using |
| 1396 | * {@link StorageManager#openProxyFileDescriptor(int, android.os.ProxyFileDescriptorCallback, android.os.Handler)} |
| 1397 | * which will let you to stream the content on-demand. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1399 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1400 | * to return the appropriate MIME type for the data returned here with |
| 1401 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1402 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1403 | * |
| 1404 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1405 | * that for any URIs that can be opened, you also support queries on them |
| 1406 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1407 | * You may also want to support other common columns if you have additional meta-data |
| 1408 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1409 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1410 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | * @param uri The URI whose file is to be opened. |
| 1412 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1413 | * "rw" for read and write access, or "rwt" for read and write access |
| 1414 | * that truncates any existing file. |
| 1415 | * |
| 1416 | * @return Returns a new ParcelFileDescriptor which you can use to access |
| 1417 | * the file. |
| 1418 | * |
| 1419 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1420 | * no file associated with the given URI or the mode is invalid. |
| 1421 | * @throws SecurityException Throws SecurityException if the caller does |
| 1422 | * not have permission to access the file. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1423 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | * @see #openAssetFile(Uri, String) |
| 1425 | * @see #openFileHelper(Uri, String) |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1426 | * @see #getType(android.net.Uri) |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1427 | * @see ParcelFileDescriptor#parseMode(String) |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1428 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1429 | public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | throws FileNotFoundException { |
| 1431 | throw new FileNotFoundException("No files supported by provider at " |
| 1432 | + uri); |
| 1433 | } |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | /** |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1436 | * Override this to handle requests to open a file blob. |
| 1437 | * The default implementation always throws {@link FileNotFoundException}. |
| 1438 | * This method can be called from multiple threads, as described in |
| 1439 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1440 | * and Threads</a>. |
| 1441 | * |
| 1442 | * <p>This method returns a ParcelFileDescriptor, which is returned directly |
| 1443 | * to the caller. This way large data (such as images and documents) can be |
| 1444 | * returned without copying the content. |
| 1445 | * |
| 1446 | * <p>The returned ParcelFileDescriptor is owned by the caller, so it is |
| 1447 | * their responsibility to close it when done. That is, the implementation |
| 1448 | * of this method should create a new ParcelFileDescriptor for each call. |
| 1449 | * <p> |
| 1450 | * If opened with the exclusive "r" or "w" modes, the returned |
| 1451 | * ParcelFileDescriptor can be a pipe or socket pair to enable streaming |
| 1452 | * of data. Opening with the "rw" or "rwt" modes implies a file on disk that |
| 1453 | * supports seeking. |
| 1454 | * <p> |
| 1455 | * If you need to detect when the returned ParcelFileDescriptor has been |
| 1456 | * closed, or if the remote process has crashed or encountered some other |
| 1457 | * error, you can use {@link ParcelFileDescriptor#open(File, int, |
| 1458 | * android.os.Handler, android.os.ParcelFileDescriptor.OnCloseListener)}, |
| 1459 | * {@link ParcelFileDescriptor#createReliablePipe()}, or |
| 1460 | * {@link ParcelFileDescriptor#createReliableSocketPair()}. |
| 1461 | * |
| 1462 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1463 | * to return the appropriate MIME type for the data returned here with |
| 1464 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1465 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1466 | * |
| 1467 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1468 | * that for any URIs that can be opened, you also support queries on them |
| 1469 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1470 | * You may also want to support other common columns if you have additional meta-data |
| 1471 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1472 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1473 | * |
| 1474 | * @param uri The URI whose file is to be opened. |
| 1475 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1476 | * "w" for write-only access, "rw" for read and write access, or |
| 1477 | * "rwt" for read and write access that truncates any existing |
| 1478 | * file. |
| 1479 | * @param signal A signal to cancel the operation in progress, or |
| 1480 | * {@code null} if none. For example, if you are downloading a |
| 1481 | * file from the network to service a "rw" mode request, you |
| 1482 | * should periodically call |
| 1483 | * {@link CancellationSignal#throwIfCanceled()} to check whether |
| 1484 | * the client has canceled the request and abort the download. |
| 1485 | * |
| 1486 | * @return Returns a new ParcelFileDescriptor which you can use to access |
| 1487 | * the file. |
| 1488 | * |
| 1489 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1490 | * no file associated with the given URI or the mode is invalid. |
| 1491 | * @throws SecurityException Throws SecurityException if the caller does |
| 1492 | * not have permission to access the file. |
| 1493 | * |
| 1494 | * @see #openAssetFile(Uri, String) |
| 1495 | * @see #openFileHelper(Uri, String) |
| 1496 | * @see #getType(android.net.Uri) |
Jeff Sharkey | e8c00d8 | 2013-10-15 15:46:10 -0700 | [diff] [blame] | 1497 | * @see ParcelFileDescriptor#parseMode(String) |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1498 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1499 | public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode, |
| 1500 | @Nullable CancellationSignal signal) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1501 | return openFile(uri, mode); |
| 1502 | } |
| 1503 | |
| 1504 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | * This is like {@link #openFile}, but can be implemented by providers |
| 1506 | * that need to be able to return sub-sections of files, often assets |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1507 | * inside of their .apk. |
| 1508 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1509 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1510 | * and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1511 | * |
| 1512 | * <p>If you implement this, your clients must be able to deal with such |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1513 | * file slices, either directly with |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1514 | * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 1516 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 1517 | * methods. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1518 | * <p> |
| 1519 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1520 | * streaming of data. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1521 | * |
| 1522 | * <p class="note">If you are implementing this to return a full file, you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | * should create the AssetFileDescriptor with |
| 1524 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1525 | * applications that cannot handle sub-sections of files.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1527 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1528 | * to return the appropriate MIME type for the data returned here with |
| 1529 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1530 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1531 | * |
| 1532 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1533 | * that for any URIs that can be opened, you also support queries on them |
| 1534 | * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p> |
| 1535 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | * @param uri The URI whose file is to be opened. |
| 1537 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1538 | * "w" for write-only access (erasing whatever data is currently in |
| 1539 | * the file), "wa" for write-only access to append to any existing data, |
| 1540 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 1541 | * and write access that truncates any existing file. |
| 1542 | * |
| 1543 | * @return Returns a new AssetFileDescriptor which you can use to access |
| 1544 | * the file. |
| 1545 | * |
| 1546 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1547 | * no file associated with the given URI or the mode is invalid. |
| 1548 | * @throws SecurityException Throws SecurityException if the caller does |
| 1549 | * not have permission to access the file. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1550 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | * @see #openFile(Uri, String) |
| 1552 | * @see #openFileHelper(Uri, String) |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1553 | * @see #getType(android.net.Uri) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1555 | public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1556 | throws FileNotFoundException { |
| 1557 | ParcelFileDescriptor fd = openFile(uri, mode); |
| 1558 | return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null; |
| 1559 | } |
| 1560 | |
| 1561 | /** |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1562 | * This is like {@link #openFile}, but can be implemented by providers |
| 1563 | * that need to be able to return sub-sections of files, often assets |
| 1564 | * inside of their .apk. |
| 1565 | * This method can be called from multiple threads, as described in |
| 1566 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1567 | * and Threads</a>. |
| 1568 | * |
| 1569 | * <p>If you implement this, your clients must be able to deal with such |
| 1570 | * file slices, either directly with |
| 1571 | * {@link ContentResolver#openAssetFileDescriptor}, or by using the higher-level |
| 1572 | * {@link ContentResolver#openInputStream ContentResolver.openInputStream} |
| 1573 | * or {@link ContentResolver#openOutputStream ContentResolver.openOutputStream} |
| 1574 | * methods. |
| 1575 | * <p> |
| 1576 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1577 | * streaming of data. |
| 1578 | * |
| 1579 | * <p class="note">If you are implementing this to return a full file, you |
| 1580 | * should create the AssetFileDescriptor with |
| 1581 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with |
| 1582 | * applications that cannot handle sub-sections of files.</p> |
| 1583 | * |
| 1584 | * <p class="note">For use in Intents, you will want to implement {@link #getType} |
| 1585 | * to return the appropriate MIME type for the data returned here with |
| 1586 | * the same URI. This will allow intent resolution to automatically determine the data MIME |
| 1587 | * type and select the appropriate matching targets as part of its operation.</p> |
| 1588 | * |
| 1589 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1590 | * that for any URIs that can be opened, you also support queries on them |
| 1591 | * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p> |
| 1592 | * |
| 1593 | * @param uri The URI whose file is to be opened. |
| 1594 | * @param mode Access mode for the file. May be "r" for read-only access, |
| 1595 | * "w" for write-only access (erasing whatever data is currently in |
| 1596 | * the file), "wa" for write-only access to append to any existing data, |
| 1597 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 1598 | * and write access that truncates any existing file. |
| 1599 | * @param signal A signal to cancel the operation in progress, or |
| 1600 | * {@code null} if none. For example, if you are downloading a |
| 1601 | * file from the network to service a "rw" mode request, you |
| 1602 | * should periodically call |
| 1603 | * {@link CancellationSignal#throwIfCanceled()} to check whether |
| 1604 | * the client has canceled the request and abort the download. |
| 1605 | * |
| 1606 | * @return Returns a new AssetFileDescriptor which you can use to access |
| 1607 | * the file. |
| 1608 | * |
| 1609 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1610 | * no file associated with the given URI or the mode is invalid. |
| 1611 | * @throws SecurityException Throws SecurityException if the caller does |
| 1612 | * not have permission to access the file. |
| 1613 | * |
| 1614 | * @see #openFile(Uri, String) |
| 1615 | * @see #openFileHelper(Uri, String) |
| 1616 | * @see #getType(android.net.Uri) |
| 1617 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1618 | public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode, |
| 1619 | @Nullable CancellationSignal signal) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1620 | return openAssetFile(uri, mode); |
| 1621 | } |
| 1622 | |
| 1623 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | * Convenience for subclasses that wish to implement {@link #openFile} |
| 1625 | * by looking up a column named "_data" at the given URI. |
| 1626 | * |
| 1627 | * @param uri The URI to be opened. |
| 1628 | * @param mode The file mode. May be "r" for read-only access, |
| 1629 | * "w" for write-only access (erasing whatever data is currently in |
| 1630 | * the file), "wa" for write-only access to append to any existing data, |
| 1631 | * "rw" for read and write access on any existing data, and "rwt" for read |
| 1632 | * and write access that truncates any existing file. |
| 1633 | * |
| 1634 | * @return Returns a new ParcelFileDescriptor that can be used by the |
| 1635 | * client to access the file. |
| 1636 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1637 | protected final @NonNull ParcelFileDescriptor openFileHelper(@NonNull Uri uri, |
| 1638 | @NonNull String mode) throws FileNotFoundException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | Cursor c = query(uri, new String[]{"_data"}, null, null, null); |
| 1640 | int count = (c != null) ? c.getCount() : 0; |
| 1641 | if (count != 1) { |
| 1642 | // If there is not exactly one result, throw an appropriate |
| 1643 | // exception. |
| 1644 | if (c != null) { |
| 1645 | c.close(); |
| 1646 | } |
| 1647 | if (count == 0) { |
| 1648 | throw new FileNotFoundException("No entry for " + uri); |
| 1649 | } |
| 1650 | throw new FileNotFoundException("Multiple items at " + uri); |
| 1651 | } |
| 1652 | |
| 1653 | c.moveToFirst(); |
| 1654 | int i = c.getColumnIndex("_data"); |
| 1655 | String path = (i >= 0 ? c.getString(i) : null); |
| 1656 | c.close(); |
| 1657 | if (path == null) { |
| 1658 | throw new FileNotFoundException("Column _data not found."); |
| 1659 | } |
| 1660 | |
Adam Lesinski | eb8c3f9 | 2013-09-20 14:08:25 -0700 | [diff] [blame] | 1661 | int modeBits = ParcelFileDescriptor.parseMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | return ParcelFileDescriptor.open(new File(path), modeBits); |
| 1663 | } |
| 1664 | |
| 1665 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1666 | * Called by a client to determine the types of data streams that this |
| 1667 | * content provider supports for the given URI. The default implementation |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1668 | * returns {@code null}, meaning no types. If your content provider stores data |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1669 | * of a particular type, return that MIME type if it matches the given |
| 1670 | * mimeTypeFilter. If it can perform type conversions, return an array |
| 1671 | * of all supported MIME types that match mimeTypeFilter. |
| 1672 | * |
| 1673 | * @param uri The data in the content provider being queried. |
| 1674 | * @param mimeTypeFilter The type of data the client desires. May be |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1675 | * a pattern, such as */* to retrieve all possible data types. |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1676 | * @return Returns {@code null} if there are no possible data streams for the |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1677 | * given mimeTypeFilter. Otherwise returns an array of all available |
| 1678 | * concrete MIME types. |
| 1679 | * |
| 1680 | * @see #getType(Uri) |
| 1681 | * @see #openTypedAssetFile(Uri, String, Bundle) |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1682 | * @see ClipDescription#compareMimeTypes(String, String) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1683 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1684 | public @Nullable String[] getStreamTypes(@NonNull Uri uri, @NonNull String mimeTypeFilter) { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1685 | return null; |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * Called by a client to open a read-only stream containing data of a |
| 1690 | * particular MIME type. This is like {@link #openAssetFile(Uri, String)}, |
| 1691 | * except the file can only be read-only and the content provider may |
| 1692 | * perform data conversions to generate data of the desired type. |
| 1693 | * |
| 1694 | * <p>The default implementation compares the given mimeType against the |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1695 | * result of {@link #getType(Uri)} and, if they match, simply calls |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1696 | * {@link #openAssetFile(Uri, String)}. |
| 1697 | * |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1698 | * <p>See {@link ClipData} for examples of the use and implementation |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1699 | * of this method. |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1700 | * <p> |
| 1701 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1702 | * streaming of data. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1703 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 1704 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1705 | * that for any URIs that can be opened, you also support queries on them |
| 1706 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1707 | * You may also want to support other common columns if you have additional meta-data |
| 1708 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1709 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1710 | * |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1711 | * @param uri The data in the content provider being queried. |
| 1712 | * @param mimeTypeFilter The type of data the client desires. May be |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1713 | * a pattern, such as */*, if the caller does not have specific type |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1714 | * requirements; in this case the content provider will pick its best |
| 1715 | * type matching the pattern. |
| 1716 | * @param opts Additional options from the client. The definitions of |
| 1717 | * these are specific to the content provider being called. |
| 1718 | * |
| 1719 | * @return Returns a new AssetFileDescriptor from which the client can |
| 1720 | * read data of the desired type. |
| 1721 | * |
| 1722 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1723 | * no file associated with the given URI or the mode is invalid. |
| 1724 | * @throws SecurityException Throws SecurityException if the caller does |
| 1725 | * not have permission to access the data. |
| 1726 | * @throws IllegalArgumentException Throws IllegalArgumentException if the |
| 1727 | * content provider does not support the requested MIME type. |
| 1728 | * |
| 1729 | * @see #getStreamTypes(Uri, String) |
| 1730 | * @see #openAssetFile(Uri, String) |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1731 | * @see ClipDescription#compareMimeTypes(String, String) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1732 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1733 | public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri, |
| 1734 | @NonNull String mimeTypeFilter, @Nullable Bundle opts) throws FileNotFoundException { |
Dianne Hackborn | 02dfd26 | 2010-08-13 12:34:58 -0700 | [diff] [blame] | 1735 | if ("*/*".equals(mimeTypeFilter)) { |
| 1736 | // If they can take anything, the untyped open call is good enough. |
| 1737 | return openAssetFile(uri, "r"); |
| 1738 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1739 | String baseType = getType(uri); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1740 | if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) { |
Dianne Hackborn | 02dfd26 | 2010-08-13 12:34:58 -0700 | [diff] [blame] | 1741 | // Use old untyped open call if this provider has a type for this |
| 1742 | // URI and it matches the request. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1743 | return openAssetFile(uri, "r"); |
| 1744 | } |
| 1745 | throw new FileNotFoundException("Can't open " + uri + " as type " + mimeTypeFilter); |
| 1746 | } |
| 1747 | |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1748 | |
| 1749 | /** |
| 1750 | * Called by a client to open a read-only stream containing data of a |
| 1751 | * particular MIME type. This is like {@link #openAssetFile(Uri, String)}, |
| 1752 | * except the file can only be read-only and the content provider may |
| 1753 | * perform data conversions to generate data of the desired type. |
| 1754 | * |
| 1755 | * <p>The default implementation compares the given mimeType against the |
| 1756 | * result of {@link #getType(Uri)} and, if they match, simply calls |
| 1757 | * {@link #openAssetFile(Uri, String)}. |
| 1758 | * |
| 1759 | * <p>See {@link ClipData} for examples of the use and implementation |
| 1760 | * of this method. |
| 1761 | * <p> |
| 1762 | * The returned AssetFileDescriptor can be a pipe or socket pair to enable |
| 1763 | * streaming of data. |
| 1764 | * |
| 1765 | * <p class="note">For better interoperability with other applications, it is recommended |
| 1766 | * that for any URIs that can be opened, you also support queries on them |
| 1767 | * containing at least the columns specified by {@link android.provider.OpenableColumns}. |
| 1768 | * You may also want to support other common columns if you have additional meta-data |
| 1769 | * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED} |
| 1770 | * in {@link android.provider.MediaStore.MediaColumns}.</p> |
| 1771 | * |
| 1772 | * @param uri The data in the content provider being queried. |
| 1773 | * @param mimeTypeFilter The type of data the client desires. May be |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 1774 | * a pattern, such as */*, if the caller does not have specific type |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1775 | * requirements; in this case the content provider will pick its best |
| 1776 | * type matching the pattern. |
| 1777 | * @param opts Additional options from the client. The definitions of |
| 1778 | * these are specific to the content provider being called. |
| 1779 | * @param signal A signal to cancel the operation in progress, or |
| 1780 | * {@code null} if none. For example, if you are downloading a |
| 1781 | * file from the network to service a "rw" mode request, you |
| 1782 | * should periodically call |
| 1783 | * {@link CancellationSignal#throwIfCanceled()} to check whether |
| 1784 | * the client has canceled the request and abort the download. |
| 1785 | * |
| 1786 | * @return Returns a new AssetFileDescriptor from which the client can |
| 1787 | * read data of the desired type. |
| 1788 | * |
| 1789 | * @throws FileNotFoundException Throws FileNotFoundException if there is |
| 1790 | * no file associated with the given URI or the mode is invalid. |
| 1791 | * @throws SecurityException Throws SecurityException if the caller does |
| 1792 | * not have permission to access the data. |
| 1793 | * @throws IllegalArgumentException Throws IllegalArgumentException if the |
| 1794 | * content provider does not support the requested MIME type. |
| 1795 | * |
| 1796 | * @see #getStreamTypes(Uri, String) |
| 1797 | * @see #openAssetFile(Uri, String) |
| 1798 | * @see ClipDescription#compareMimeTypes(String, String) |
| 1799 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1800 | public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri, |
| 1801 | @NonNull String mimeTypeFilter, @Nullable Bundle opts, |
| 1802 | @Nullable CancellationSignal signal) throws FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 1803 | return openTypedAssetFile(uri, mimeTypeFilter, opts); |
| 1804 | } |
| 1805 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1806 | /** |
| 1807 | * Interface to write a stream of data to a pipe. Use with |
| 1808 | * {@link ContentProvider#openPipeHelper}. |
| 1809 | */ |
| 1810 | public interface PipeDataWriter<T> { |
| 1811 | /** |
| 1812 | * Called from a background thread to stream data out to a pipe. |
| 1813 | * Note that the pipe is blocking, so this thread can block on |
| 1814 | * writes for an arbitrary amount of time if the client is slow |
| 1815 | * at reading. |
| 1816 | * |
| 1817 | * @param output The pipe where data should be written. This will be |
| 1818 | * closed for you upon returning from this function. |
| 1819 | * @param uri The URI whose data is to be written. |
| 1820 | * @param mimeType The desired type of data to be written. |
| 1821 | * @param opts Options supplied by caller. |
| 1822 | * @param args Your own custom arguments. |
| 1823 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1824 | public void writeDataToPipe(@NonNull ParcelFileDescriptor output, @NonNull Uri uri, |
| 1825 | @NonNull String mimeType, @Nullable Bundle opts, @Nullable T args); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /** |
| 1829 | * A helper function for implementing {@link #openTypedAssetFile}, for |
| 1830 | * creating a data pipe and background thread allowing you to stream |
| 1831 | * generated data back to the client. This function returns a new |
| 1832 | * ParcelFileDescriptor that should be returned to the caller (the caller |
| 1833 | * is responsible for closing it). |
| 1834 | * |
| 1835 | * @param uri The URI whose data is to be written. |
| 1836 | * @param mimeType The desired type of data to be written. |
| 1837 | * @param opts Options supplied by caller. |
| 1838 | * @param args Your own custom arguments. |
| 1839 | * @param func Interface implementing the function that will actually |
| 1840 | * stream the data. |
| 1841 | * @return Returns a new ParcelFileDescriptor holding the read side of |
| 1842 | * the pipe. This should be returned to the caller for reading; the caller |
| 1843 | * is responsible for closing it when done. |
| 1844 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1845 | public @NonNull <T> ParcelFileDescriptor openPipeHelper(final @NonNull Uri uri, |
| 1846 | final @NonNull String mimeType, final @Nullable Bundle opts, final @Nullable T args, |
| 1847 | final @NonNull PipeDataWriter<T> func) throws FileNotFoundException { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1848 | try { |
| 1849 | final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); |
| 1850 | |
| 1851 | AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() { |
| 1852 | @Override |
| 1853 | protected Object doInBackground(Object... params) { |
| 1854 | func.writeDataToPipe(fds[1], uri, mimeType, opts, args); |
| 1855 | try { |
| 1856 | fds[1].close(); |
| 1857 | } catch (IOException e) { |
| 1858 | Log.w(TAG, "Failure closing pipe", e); |
| 1859 | } |
| 1860 | return null; |
| 1861 | } |
| 1862 | }; |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1863 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 1864 | |
| 1865 | return fds[0]; |
| 1866 | } catch (IOException e) { |
| 1867 | throw new FileNotFoundException("failure making pipe"); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1872 | * Returns true if this instance is a temporary content provider. |
| 1873 | * @return true if this instance is a temporary content provider |
| 1874 | */ |
| 1875 | protected boolean isTemporary() { |
| 1876 | return false; |
| 1877 | } |
| 1878 | |
| 1879 | /** |
| 1880 | * Returns the Binder object for this provider. |
| 1881 | * |
| 1882 | * @return the Binder object for this provider |
| 1883 | * @hide |
| 1884 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1885 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | public IContentProvider getIContentProvider() { |
| 1887 | return mTransport; |
| 1888 | } |
| 1889 | |
| 1890 | /** |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 1891 | * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use |
| 1892 | * when directly instantiating the provider for testing. |
| 1893 | * @hide |
| 1894 | */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 1895 | @UnsupportedAppUsage |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 1896 | public void attachInfoForTesting(Context context, ProviderInfo info) { |
| 1897 | attachInfo(context, info, true); |
| 1898 | } |
| 1899 | |
| 1900 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | * After being instantiated, this is called to tell the content provider |
| 1902 | * about itself. |
| 1903 | * |
| 1904 | * @param context The context this provider is running in |
| 1905 | * @param info Registered information about this content provider |
| 1906 | */ |
| 1907 | public void attachInfo(Context context, ProviderInfo info) { |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 1908 | attachInfo(context, info, false); |
| 1909 | } |
| 1910 | |
| 1911 | private void attachInfo(Context context, ProviderInfo info, boolean testing) { |
Dianne Hackborn | 334d9ae | 2013-02-26 15:02:06 -0800 | [diff] [blame] | 1912 | mNoPerms = testing; |
| 1913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1914 | /* |
| 1915 | * Only allow it to be set once, so after the content service gives |
| 1916 | * this to us clients can't change it. |
| 1917 | */ |
| 1918 | if (mContext == null) { |
| 1919 | mContext = context; |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 1920 | if (context != null && mTransport != null) { |
Jeff Sharkey | 10cb312 | 2013-09-17 15:18:43 -0700 | [diff] [blame] | 1921 | mTransport.mAppOpsManager = (AppOpsManager) context.getSystemService( |
| 1922 | Context.APP_OPS_SERVICE); |
| 1923 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 1924 | mMyUid = Process.myUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1925 | if (info != null) { |
| 1926 | setReadPermission(info.readPermission); |
| 1927 | setWritePermission(info.writePermission); |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 1928 | setPathPermissions(info.pathPermissions); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1929 | mExported = info.exported; |
Amith Yamasani | a6f4d58 | 2014-08-07 17:58:39 -0700 | [diff] [blame] | 1930 | mSingleUser = (info.flags & ProviderInfo.FLAG_SINGLE_USER) != 0; |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 1931 | setAuthorities(info.authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | } |
| 1933 | ContentProvider.this.onCreate(); |
| 1934 | } |
| 1935 | } |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 1936 | |
| 1937 | /** |
Dan Egnor | 17876aa | 2010-07-28 12:28:04 -0700 | [diff] [blame] | 1938 | * Override this to handle requests to perform a batch of operations, or the |
| 1939 | * default implementation will iterate over the operations and call |
| 1940 | * {@link ContentProviderOperation#apply} on each of them. |
| 1941 | * If all calls to {@link ContentProviderOperation#apply} succeed |
| 1942 | * then a {@link ContentProviderResult} array with as many |
| 1943 | * elements as there were operations will be returned. If any of the calls |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1944 | * fail, it is up to the implementation how many of the others take effect. |
| 1945 | * This method can be called from multiple threads, as described in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1946 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes |
| 1947 | * and Threads</a>. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1948 | * |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 1949 | * @param operations the operations to apply |
| 1950 | * @return the results of the applications |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 1951 | * @throws OperationApplicationException thrown if any operation fails. |
| 1952 | * @see ContentProviderOperation#apply |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 1953 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1954 | public @NonNull ContentProviderResult[] applyBatch( |
| 1955 | @NonNull ArrayList<ContentProviderOperation> operations) |
| 1956 | throws OperationApplicationException { |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 1957 | final int numOperations = operations.size(); |
| 1958 | final ContentProviderResult[] results = new ContentProviderResult[numOperations]; |
| 1959 | for (int i = 0; i < numOperations; i++) { |
| 1960 | results[i] = operations.get(i).apply(this, results, i); |
Fred Quintana | ce31b23 | 2009-05-04 16:01:15 -0700 | [diff] [blame] | 1961 | } |
| 1962 | return results; |
| 1963 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 1964 | |
| 1965 | /** |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 1966 | * Call a provider-defined method. This can be used to implement |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1967 | * interfaces that are cheaper and/or unnatural for a table-like |
| 1968 | * model. |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 1969 | * |
Dianne Hackborn | 5d122d9 | 2013-03-12 18:37:07 -0700 | [diff] [blame] | 1970 | * <p class="note"><strong>WARNING:</strong> The framework does no permission checking |
| 1971 | * on this entry into the content provider besides the basic ability for the application |
| 1972 | * to get access to the provider at all. For example, it has no idea whether the call |
| 1973 | * being executed may read or write data in the provider, so can't enforce those |
| 1974 | * individual permissions. Any implementation of this method <strong>must</strong> |
| 1975 | * do its own permission checks on incoming calls to make sure they are allowed.</p> |
| 1976 | * |
Christopher Tate | 2bc6eb8 | 2013-01-03 12:04:08 -0800 | [diff] [blame] | 1977 | * @param method method name to call. Opaque to framework, but should not be {@code null}. |
| 1978 | * @param arg provider-defined String argument. May be {@code null}. |
| 1979 | * @param extras provider-defined Bundle argument. May be {@code null}. |
| 1980 | * @return provider-defined return value. May be {@code null}, which is also |
Brad Fitzpatrick | 534c84c | 2011-01-12 14:06:30 -0800 | [diff] [blame] | 1981 | * the default for providers which don't implement any call methods. |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 1982 | */ |
Jeff Sharkey | 673db44 | 2015-06-11 19:30:57 -0700 | [diff] [blame] | 1983 | public @Nullable Bundle call(@NonNull String method, @Nullable String arg, |
| 1984 | @Nullable Bundle extras) { |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 1985 | return null; |
| 1986 | } |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 1987 | |
| 1988 | /** |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 1989 | * Implement this to shut down the ContentProvider instance. You can then |
| 1990 | * invoke this method in unit tests. |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 1991 | * |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 1992 | * <p> |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 1993 | * Android normally handles ContentProvider startup and shutdown |
| 1994 | * automatically. You do not need to start up or shut down a |
| 1995 | * ContentProvider. When you invoke a test method on a ContentProvider, |
| 1996 | * however, a ContentProvider instance is started and keeps running after |
| 1997 | * the test finishes, even if a succeeding test instantiates another |
| 1998 | * ContentProvider. A conflict develops because the two instances are |
| 1999 | * usually running against the same underlying data source (for example, an |
| 2000 | * sqlite database). |
| 2001 | * </p> |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 2002 | * <p> |
Manuel Roman | 2c96a0c | 2010-08-05 16:39:49 -0700 | [diff] [blame] | 2003 | * Implementing shutDown() avoids this conflict by providing a way to |
| 2004 | * terminate the ContentProvider. This method can also prevent memory leaks |
| 2005 | * from multiple instantiations of the ContentProvider, and it can ensure |
| 2006 | * unit test isolation by allowing you to completely clean up the test |
| 2007 | * fixture before moving on to the next test. |
| 2008 | * </p> |
Vasu Nori | 0c9e14a | 2010-08-04 13:31:48 -0700 | [diff] [blame] | 2009 | */ |
| 2010 | public void shutdown() { |
| 2011 | Log.w(TAG, "implement ContentProvider shutdown() to make sure all database " + |
| 2012 | "connections are gracefully shutdown"); |
| 2013 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2014 | |
| 2015 | /** |
| 2016 | * Print the Provider's state into the given stream. This gets invoked if |
Jeff Sharkey | 5554b70 | 2012-04-11 18:30:51 -0700 | [diff] [blame] | 2017 | * you run "adb shell dumpsys activity provider <provider_component_name>". |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2018 | * |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2019 | * @param fd The raw file descriptor that the dump is being sent to. |
| 2020 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 2021 | * closed for you after you return. |
| 2022 | * @param args additional arguments to the dump request. |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 2023 | */ |
| 2024 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2025 | writer.println("nothing to dump"); |
| 2026 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2027 | |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2028 | /** @hide */ |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 2029 | public Uri validateIncomingUri(Uri uri) throws SecurityException { |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2030 | String auth = uri.getAuthority(); |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 2031 | if (!mSingleUser) { |
| 2032 | int userId = getUserIdFromAuthority(auth, UserHandle.USER_CURRENT); |
| 2033 | if (userId != UserHandle.USER_CURRENT && userId != mContext.getUserId()) { |
| 2034 | throw new SecurityException("trying to query a ContentProvider in user " |
| 2035 | + mContext.getUserId() + " with a uri belonging to user " + userId); |
| 2036 | } |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2037 | } |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2038 | if (!matchesOurAuthorities(getAuthorityWithoutUserId(auth))) { |
| 2039 | String message = "The authority of the uri " + uri + " does not match the one of the " |
| 2040 | + "contentProvider: "; |
| 2041 | if (mAuthority != null) { |
| 2042 | message += mAuthority; |
| 2043 | } else { |
Andreas Gampe | e6748ce | 2015-12-11 18:00:38 -0800 | [diff] [blame] | 2044 | message += Arrays.toString(mAuthorities); |
Nicolas Prevot | f300bab | 2014-08-07 19:23:17 +0100 | [diff] [blame] | 2045 | } |
| 2046 | throw new SecurityException(message); |
| 2047 | } |
Jeff Sharkey | 35f31cb | 2018-09-24 13:23:57 -0600 | [diff] [blame] | 2048 | |
| 2049 | // Normalize the path by removing any empty path segments, which can be |
| 2050 | // a source of security issues. |
| 2051 | final String encodedPath = uri.getEncodedPath(); |
| 2052 | if (encodedPath != null && encodedPath.indexOf("//") != -1) { |
| 2053 | final Uri normalized = uri.buildUpon() |
| 2054 | .encodedPath(encodedPath.replaceAll("//+", "/")).build(); |
| 2055 | Log.w(TAG, "Normalized " + uri + " to " + normalized |
| 2056 | + " to avoid possible security issues"); |
| 2057 | return normalized; |
| 2058 | } else { |
| 2059 | return uri; |
| 2060 | } |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2061 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2062 | |
| 2063 | /** @hide */ |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 2064 | private Uri maybeGetUriWithoutUserId(Uri uri) { |
| 2065 | if (mSingleUser) { |
| 2066 | return uri; |
| 2067 | } |
| 2068 | return getUriWithoutUserId(uri); |
| 2069 | } |
| 2070 | |
| 2071 | /** @hide */ |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2072 | public static int getUserIdFromAuthority(String auth, int defaultUserId) { |
| 2073 | if (auth == null) return defaultUserId; |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2074 | int end = auth.lastIndexOf('@'); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2075 | if (end == -1) return defaultUserId; |
| 2076 | String userIdString = auth.substring(0, end); |
| 2077 | try { |
| 2078 | return Integer.parseInt(userIdString); |
| 2079 | } catch (NumberFormatException e) { |
| 2080 | Log.w(TAG, "Error parsing userId.", e); |
| 2081 | return UserHandle.USER_NULL; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | /** @hide */ |
| 2086 | public static int getUserIdFromAuthority(String auth) { |
| 2087 | return getUserIdFromAuthority(auth, UserHandle.USER_CURRENT); |
| 2088 | } |
| 2089 | |
| 2090 | /** @hide */ |
| 2091 | public static int getUserIdFromUri(Uri uri, int defaultUserId) { |
| 2092 | if (uri == null) return defaultUserId; |
| 2093 | return getUserIdFromAuthority(uri.getAuthority(), defaultUserId); |
| 2094 | } |
| 2095 | |
| 2096 | /** @hide */ |
| 2097 | public static int getUserIdFromUri(Uri uri) { |
| 2098 | return getUserIdFromUri(uri, UserHandle.USER_CURRENT); |
| 2099 | } |
| 2100 | |
| 2101 | /** |
| 2102 | * Removes userId part from authority string. Expects format: |
| 2103 | * userId@some.authority |
| 2104 | * If there is no userId in the authority, it symply returns the argument |
| 2105 | * @hide |
| 2106 | */ |
| 2107 | public static String getAuthorityWithoutUserId(String auth) { |
| 2108 | if (auth == null) return null; |
Nicolas Prevot | 504d78e | 2014-06-26 10:07:33 +0100 | [diff] [blame] | 2109 | int end = auth.lastIndexOf('@'); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2110 | return auth.substring(end+1); |
| 2111 | } |
| 2112 | |
| 2113 | /** @hide */ |
| 2114 | public static Uri getUriWithoutUserId(Uri uri) { |
| 2115 | if (uri == null) return null; |
| 2116 | Uri.Builder builder = uri.buildUpon(); |
| 2117 | builder.authority(getAuthorityWithoutUserId(uri.getAuthority())); |
| 2118 | return builder.build(); |
| 2119 | } |
| 2120 | |
| 2121 | /** @hide */ |
| 2122 | public static boolean uriHasUserId(Uri uri) { |
| 2123 | if (uri == null) return false; |
| 2124 | return !TextUtils.isEmpty(uri.getUserInfo()); |
| 2125 | } |
| 2126 | |
| 2127 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 2128 | @UnsupportedAppUsage |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2129 | public static Uri maybeAddUserId(Uri uri, int userId) { |
| 2130 | if (uri == null) return null; |
| 2131 | if (userId != UserHandle.USER_CURRENT |
| 2132 | && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
| 2133 | if (!uriHasUserId(uri)) { |
| 2134 | //We don't add the user Id if there's already one |
| 2135 | Uri.Builder builder = uri.buildUpon(); |
| 2136 | builder.encodedAuthority("" + userId + "@" + uri.getEncodedAuthority()); |
| 2137 | return builder.build(); |
| 2138 | } |
| 2139 | } |
| 2140 | return uri; |
| 2141 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 2142 | } |