Fred Quintana | 6a8d533 | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 17 | package android.content; |
| 18 | |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 21 | import android.content.res.AssetFileDescriptor; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 22 | import android.database.CrossProcessCursorWrapper; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.net.Uri; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 26 | import android.os.CancellationSignal; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 27 | import android.os.DeadObjectException; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 29 | import android.os.ICancellationSignal; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 30 | import android.os.Looper; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 31 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
| 33 | import android.util.Log; |
| 34 | |
| 35 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 37 | import com.android.internal.util.Preconditions; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 38 | |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 39 | import dalvik.system.CloseGuard; |
| 40 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 41 | import java.io.FileNotFoundException; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 43 | import java.util.concurrent.atomic.AtomicBoolean; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 46 | * The public interface object used to interact with a specific |
| 47 | * {@link ContentProvider}. |
| 48 | * <p> |
| 49 | * Instances can be obtained by calling |
| 50 | * {@link ContentResolver#acquireContentProviderClient} or |
| 51 | * {@link ContentResolver#acquireUnstableContentProviderClient}. Instances must |
| 52 | * be released using {@link #close()} in order to indicate to the system that |
| 53 | * the underlying {@link ContentProvider} is no longer needed and can be killed |
| 54 | * to free up resources. |
| 55 | * <p> |
| 56 | * Note that you should generally create a new ContentProviderClient instance |
| 57 | * for each thread that will be performing operations. Unlike |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 58 | * {@link ContentResolver}, the methods here such as {@link #query} and |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 59 | * {@link #openFile} are not thread safe -- you must not call {@link #close()} |
| 60 | * on the ContentProviderClient those calls are made from until you are finished |
| 61 | * with the data they have returned. |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 62 | */ |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 63 | public class ContentProviderClient implements AutoCloseable { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 64 | private static final String TAG = "ContentProviderClient"; |
| 65 | |
| 66 | @GuardedBy("ContentProviderClient.class") |
| 67 | private static Handler sAnrHandler; |
| 68 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 69 | private final ContentResolver mContentResolver; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 70 | private final IContentProvider mContentProvider; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 71 | private final String mPackageName; |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 72 | private final boolean mStable; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 73 | |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 74 | private final AtomicBoolean mClosed = new AtomicBoolean(); |
| 75 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 76 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 77 | private long mAnrTimeout; |
| 78 | private NotRespondingRunnable mAnrRunnable; |
| 79 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 80 | /** {@hide} */ |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 81 | @VisibleForTesting |
| 82 | public ContentProviderClient( |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 83 | ContentResolver contentResolver, IContentProvider contentProvider, boolean stable) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 84 | mContentResolver = contentResolver; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 85 | mContentProvider = contentProvider; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 86 | mPackageName = contentResolver.mPackageName; |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 87 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 88 | mStable = stable; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 89 | |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 90 | mCloseGuard.open("close"); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 93 | /** {@hide} */ |
| 94 | public void setDetectNotResponding(long timeoutMillis) { |
| 95 | synchronized (ContentProviderClient.class) { |
| 96 | mAnrTimeout = timeoutMillis; |
| 97 | |
| 98 | if (timeoutMillis > 0) { |
| 99 | if (mAnrRunnable == null) { |
| 100 | mAnrRunnable = new NotRespondingRunnable(); |
| 101 | } |
| 102 | if (sAnrHandler == null) { |
| 103 | sAnrHandler = new Handler(Looper.getMainLooper(), null, true /* async */); |
| 104 | } |
| 105 | } else { |
| 106 | mAnrRunnable = null; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void beforeRemote() { |
| 112 | if (mAnrRunnable != null) { |
| 113 | sAnrHandler.postDelayed(mAnrRunnable, mAnrTimeout); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private void afterRemote() { |
| 118 | if (mAnrRunnable != null) { |
| 119 | sAnrHandler.removeCallbacks(mAnrRunnable); |
| 120 | } |
| 121 | } |
| 122 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 123 | /** See {@link ContentProvider#query ContentProvider.query} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 124 | public @Nullable Cursor query(@NonNull Uri url, @Nullable String[] projection, |
| 125 | @Nullable String selection, @Nullable String[] selectionArgs, |
| 126 | @Nullable String sortOrder) throws RemoteException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 127 | return query(url, projection, selection, selectionArgs, sortOrder, null); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** See {@link ContentProvider#query ContentProvider.query} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 131 | public @Nullable Cursor query(@NonNull Uri url, @Nullable String[] projection, |
| 132 | @Nullable String selection, @Nullable String[] selectionArgs, |
| 133 | @Nullable String sortOrder, @Nullable CancellationSignal cancellationSignal) |
| 134 | throws RemoteException { |
| 135 | Preconditions.checkNotNull(url, "url"); |
| 136 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 137 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 138 | try { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 139 | ICancellationSignal remoteCancellationSignal = null; |
| 140 | if (cancellationSignal != null) { |
| 141 | cancellationSignal.throwIfCanceled(); |
| 142 | remoteCancellationSignal = mContentProvider.createCancellationSignal(); |
| 143 | cancellationSignal.setRemote(remoteCancellationSignal); |
| 144 | } |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 145 | final Cursor cursor = mContentProvider.query(mPackageName, url, projection, selection, |
| 146 | selectionArgs, sortOrder, remoteCancellationSignal); |
Jeff Sharkey | 703fe24 | 2016-01-07 18:52:26 -0700 | [diff] [blame] | 147 | if (cursor == null) { |
| 148 | return null; |
| 149 | } |
| 150 | |
Makoto Onuki | e798ba1 | 2016-02-05 11:43:47 -0800 | [diff] [blame] | 151 | if ("com.google.android.gms".equals(mPackageName)) { |
Jeff Sharkey | 8b0db04 | 2016-01-07 09:47:20 -0700 | [diff] [blame] | 152 | // They're casting to a concrete subclass, sigh |
| 153 | return cursor; |
| 154 | } else { |
| 155 | return new CursorWrapperInner(cursor); |
| 156 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 157 | } catch (DeadObjectException e) { |
| 158 | if (!mStable) { |
| 159 | mContentResolver.unstableProviderDied(mContentProvider); |
| 160 | } |
| 161 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 162 | } finally { |
| 163 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 164 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 167 | /** See {@link ContentProvider#getType ContentProvider.getType} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 168 | public @Nullable String getType(@NonNull Uri url) throws RemoteException { |
| 169 | Preconditions.checkNotNull(url, "url"); |
| 170 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 171 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 172 | try { |
| 173 | return mContentProvider.getType(url); |
| 174 | } catch (DeadObjectException e) { |
| 175 | if (!mStable) { |
| 176 | mContentResolver.unstableProviderDied(mContentProvider); |
| 177 | } |
| 178 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 179 | } finally { |
| 180 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 181 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 184 | /** See {@link ContentProvider#getStreamTypes ContentProvider.getStreamTypes} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 185 | public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) |
| 186 | throws RemoteException { |
| 187 | Preconditions.checkNotNull(url, "url"); |
| 188 | Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter"); |
| 189 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 190 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 191 | try { |
| 192 | return mContentProvider.getStreamTypes(url, mimeTypeFilter); |
| 193 | } catch (DeadObjectException e) { |
| 194 | if (!mStable) { |
| 195 | mContentResolver.unstableProviderDied(mContentProvider); |
| 196 | } |
| 197 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 198 | } finally { |
| 199 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 200 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 203 | /** See {@link ContentProvider#canonicalize} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 204 | public final @Nullable Uri canonicalize(@NonNull Uri url) throws RemoteException { |
| 205 | Preconditions.checkNotNull(url, "url"); |
| 206 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 207 | beforeRemote(); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 208 | try { |
| 209 | return mContentProvider.canonicalize(mPackageName, url); |
| 210 | } catch (DeadObjectException e) { |
| 211 | if (!mStable) { |
| 212 | mContentResolver.unstableProviderDied(mContentProvider); |
| 213 | } |
| 214 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 215 | } finally { |
| 216 | afterRemote(); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | /** See {@link ContentProvider#uncanonicalize} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 221 | public final @Nullable Uri uncanonicalize(@NonNull Uri url) throws RemoteException { |
| 222 | Preconditions.checkNotNull(url, "url"); |
| 223 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 224 | beforeRemote(); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 225 | try { |
| 226 | return mContentProvider.uncanonicalize(mPackageName, url); |
| 227 | } catch (DeadObjectException e) { |
| 228 | if (!mStable) { |
| 229 | mContentResolver.unstableProviderDied(mContentProvider); |
| 230 | } |
| 231 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 232 | } finally { |
| 233 | afterRemote(); |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 237 | /** See {@link ContentProvider#insert ContentProvider.insert} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 238 | public @Nullable Uri insert(@NonNull Uri url, @Nullable ContentValues initialValues) |
| 239 | throws RemoteException { |
| 240 | Preconditions.checkNotNull(url, "url"); |
| 241 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 242 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 243 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 244 | return mContentProvider.insert(mPackageName, url, initialValues); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 245 | } catch (DeadObjectException e) { |
| 246 | if (!mStable) { |
| 247 | mContentResolver.unstableProviderDied(mContentProvider); |
| 248 | } |
| 249 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 250 | } finally { |
| 251 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 252 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 255 | /** See {@link ContentProvider#bulkInsert ContentProvider.bulkInsert} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 256 | public int bulkInsert(@NonNull Uri url, @NonNull ContentValues[] initialValues) |
| 257 | throws RemoteException { |
| 258 | Preconditions.checkNotNull(url, "url"); |
| 259 | Preconditions.checkNotNull(initialValues, "initialValues"); |
| 260 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 261 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 262 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 263 | return mContentProvider.bulkInsert(mPackageName, url, initialValues); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 264 | } catch (DeadObjectException e) { |
| 265 | if (!mStable) { |
| 266 | mContentResolver.unstableProviderDied(mContentProvider); |
| 267 | } |
| 268 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 269 | } finally { |
| 270 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 271 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 274 | /** See {@link ContentProvider#delete ContentProvider.delete} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 275 | public int delete(@NonNull Uri url, @Nullable String selection, |
| 276 | @Nullable String[] selectionArgs) throws RemoteException { |
| 277 | Preconditions.checkNotNull(url, "url"); |
| 278 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 279 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 280 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 281 | return mContentProvider.delete(mPackageName, url, selection, selectionArgs); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 282 | } catch (DeadObjectException e) { |
| 283 | if (!mStable) { |
| 284 | mContentResolver.unstableProviderDied(mContentProvider); |
| 285 | } |
| 286 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 287 | } finally { |
| 288 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 289 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 292 | /** See {@link ContentProvider#update ContentProvider.update} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 293 | public int update(@NonNull Uri url, @Nullable ContentValues values, @Nullable String selection, |
| 294 | @Nullable String[] selectionArgs) throws RemoteException { |
| 295 | Preconditions.checkNotNull(url, "url"); |
| 296 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 297 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 298 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 299 | return mContentProvider.update(mPackageName, url, values, selection, selectionArgs); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 300 | } catch (DeadObjectException e) { |
| 301 | if (!mStable) { |
| 302 | mContentResolver.unstableProviderDied(mContentProvider); |
| 303 | } |
| 304 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 305 | } finally { |
| 306 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 307 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 310 | /** |
| 311 | * See {@link ContentProvider#openFile ContentProvider.openFile}. Note that |
| 312 | * this <em>does not</em> |
| 313 | * take care of non-content: URIs such as file:. It is strongly recommended |
| 314 | * you use the {@link ContentResolver#openFileDescriptor |
| 315 | * ContentResolver.openFileDescriptor} API instead. |
| 316 | */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 317 | public @Nullable ParcelFileDescriptor openFile(@NonNull Uri url, @NonNull String mode) |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 318 | throws RemoteException, FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 319 | return openFile(url, mode, null); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * See {@link ContentProvider#openFile ContentProvider.openFile}. Note that |
| 324 | * this <em>does not</em> |
| 325 | * take care of non-content: URIs such as file:. It is strongly recommended |
| 326 | * you use the {@link ContentResolver#openFileDescriptor |
| 327 | * ContentResolver.openFileDescriptor} API instead. |
| 328 | */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 329 | public @Nullable ParcelFileDescriptor openFile(@NonNull Uri url, @NonNull String mode, |
| 330 | @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException { |
| 331 | Preconditions.checkNotNull(url, "url"); |
| 332 | Preconditions.checkNotNull(mode, "mode"); |
| 333 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 334 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 335 | try { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 336 | ICancellationSignal remoteSignal = null; |
| 337 | if (signal != null) { |
| 338 | signal.throwIfCanceled(); |
| 339 | remoteSignal = mContentProvider.createCancellationSignal(); |
| 340 | signal.setRemote(remoteSignal); |
| 341 | } |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 342 | return mContentProvider.openFile(mPackageName, url, mode, remoteSignal, null); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 343 | } catch (DeadObjectException e) { |
| 344 | if (!mStable) { |
| 345 | mContentResolver.unstableProviderDied(mContentProvider); |
| 346 | } |
| 347 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 348 | } finally { |
| 349 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 350 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 353 | /** |
| 354 | * See {@link ContentProvider#openAssetFile ContentProvider.openAssetFile}. |
| 355 | * Note that this <em>does not</em> |
| 356 | * take care of non-content: URIs such as file:. It is strongly recommended |
| 357 | * you use the {@link ContentResolver#openAssetFileDescriptor |
| 358 | * ContentResolver.openAssetFileDescriptor} API instead. |
| 359 | */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 360 | public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri url, @NonNull String mode) |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 361 | throws RemoteException, FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 362 | return openAssetFile(url, mode, null); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * See {@link ContentProvider#openAssetFile ContentProvider.openAssetFile}. |
| 367 | * Note that this <em>does not</em> |
| 368 | * take care of non-content: URIs such as file:. It is strongly recommended |
| 369 | * you use the {@link ContentResolver#openAssetFileDescriptor |
| 370 | * ContentResolver.openAssetFileDescriptor} API instead. |
| 371 | */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 372 | public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri url, @NonNull String mode, |
| 373 | @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException { |
| 374 | Preconditions.checkNotNull(url, "url"); |
| 375 | Preconditions.checkNotNull(mode, "mode"); |
| 376 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 377 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 378 | try { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 379 | ICancellationSignal remoteSignal = null; |
| 380 | if (signal != null) { |
| 381 | signal.throwIfCanceled(); |
| 382 | remoteSignal = mContentProvider.createCancellationSignal(); |
| 383 | signal.setRemote(remoteSignal); |
| 384 | } |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 385 | return mContentProvider.openAssetFile(mPackageName, url, mode, remoteSignal); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 386 | } catch (DeadObjectException e) { |
| 387 | if (!mStable) { |
| 388 | mContentResolver.unstableProviderDied(mContentProvider); |
| 389 | } |
| 390 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 391 | } finally { |
| 392 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 393 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 396 | /** See {@link ContentProvider#openTypedAssetFile ContentProvider.openTypedAssetFile} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 397 | public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri, |
| 398 | @NonNull String mimeType, @Nullable Bundle opts) |
| 399 | throws RemoteException, FileNotFoundException { |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 400 | return openTypedAssetFileDescriptor(uri, mimeType, opts, null); |
| 401 | } |
| 402 | |
| 403 | /** See {@link ContentProvider#openTypedAssetFile ContentProvider.openTypedAssetFile} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 404 | public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri, |
| 405 | @NonNull String mimeType, @Nullable Bundle opts, @Nullable CancellationSignal signal) |
| 406 | throws RemoteException, FileNotFoundException { |
| 407 | Preconditions.checkNotNull(uri, "uri"); |
| 408 | Preconditions.checkNotNull(mimeType, "mimeType"); |
| 409 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 410 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 411 | try { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 412 | ICancellationSignal remoteSignal = null; |
| 413 | if (signal != null) { |
| 414 | signal.throwIfCanceled(); |
| 415 | remoteSignal = mContentProvider.createCancellationSignal(); |
| 416 | signal.setRemote(remoteSignal); |
| 417 | } |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 418 | return mContentProvider.openTypedAssetFile( |
| 419 | mPackageName, uri, mimeType, opts, remoteSignal); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 420 | } catch (DeadObjectException e) { |
| 421 | if (!mStable) { |
| 422 | mContentResolver.unstableProviderDied(mContentProvider); |
| 423 | } |
| 424 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 425 | } finally { |
| 426 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 427 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** See {@link ContentProvider#applyBatch ContentProvider.applyBatch} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 431 | public @NonNull ContentProviderResult[] applyBatch( |
| 432 | @NonNull ArrayList<ContentProviderOperation> operations) |
| 433 | throws RemoteException, OperationApplicationException { |
| 434 | Preconditions.checkNotNull(operations, "operations"); |
| 435 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 436 | beforeRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 437 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 438 | return mContentProvider.applyBatch(mPackageName, operations); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 439 | } catch (DeadObjectException e) { |
| 440 | if (!mStable) { |
| 441 | mContentResolver.unstableProviderDied(mContentProvider); |
| 442 | } |
| 443 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 444 | } finally { |
| 445 | afterRemote(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 446 | } |
Fred Quintana | 6a8d533 | 2009-05-07 17:35:38 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 449 | /** See {@link ContentProvider#call(String, String, Bundle)} */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 450 | public @Nullable Bundle call(@NonNull String method, @Nullable String arg, |
| 451 | @Nullable Bundle extras) throws RemoteException { |
| 452 | Preconditions.checkNotNull(method, "method"); |
| 453 | |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 454 | beforeRemote(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 455 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 456 | return mContentProvider.call(mPackageName, method, arg, extras); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 457 | } catch (DeadObjectException e) { |
| 458 | if (!mStable) { |
| 459 | mContentResolver.unstableProviderDied(mContentProvider); |
| 460 | } |
| 461 | throw e; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 462 | } finally { |
| 463 | afterRemote(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 467 | /** |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 468 | * Closes this client connection, indicating to the system that the |
| 469 | * underlying {@link ContentProvider} is no longer needed. |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 470 | */ |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 471 | @Override |
| 472 | public void close() { |
| 473 | closeInternal(); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * @deprecated replaced by {@link #close()}. |
| 478 | */ |
| 479 | @Deprecated |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 480 | public boolean release() { |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 481 | return closeInternal(); |
| 482 | } |
| 483 | |
| 484 | private boolean closeInternal() { |
| 485 | mCloseGuard.close(); |
| 486 | if (mClosed.compareAndSet(false, true)) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 487 | if (mStable) { |
| 488 | return mContentResolver.releaseProvider(mContentProvider); |
| 489 | } else { |
| 490 | return mContentResolver.releaseUnstableProvider(mContentProvider); |
| 491 | } |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 492 | } else { |
| 493 | return false; |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 494 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 497 | @Override |
| 498 | protected void finalize() throws Throwable { |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 499 | try { |
| 500 | mCloseGuard.warnIfOpen(); |
| 501 | close(); |
| 502 | } finally { |
| 503 | super.finalize(); |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 507 | /** |
| 508 | * Get a reference to the {@link ContentProvider} that is associated with this |
| 509 | * client. If the {@link ContentProvider} is running in a different process then |
| 510 | * null will be returned. This can be used if you know you are running in the same |
| 511 | * process as a provider, and want to get direct access to its implementation details. |
| 512 | * |
| 513 | * @return If the associated {@link ContentProvider} is local, returns it. |
| 514 | * Otherwise returns null. |
| 515 | */ |
Jeff Sharkey | 73ed509 | 2015-06-12 09:49:31 -0700 | [diff] [blame] | 516 | public @Nullable ContentProvider getLocalContentProvider() { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 517 | return ContentProvider.coerceToLocalContentProvider(mContentProvider); |
| 518 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 519 | |
| 520 | /** {@hide} */ |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 521 | public static void releaseQuietly(ContentProviderClient client) { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 522 | if (client != null) { |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 523 | try { |
| 524 | client.release(); |
| 525 | } catch (Exception ignored) { |
| 526 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 529 | |
| 530 | private class NotRespondingRunnable implements Runnable { |
| 531 | @Override |
| 532 | public void run() { |
| 533 | Log.w(TAG, "Detected provider not responding: " + mContentProvider); |
| 534 | mContentResolver.appNotRespondingViaProvider(mContentProvider); |
| 535 | } |
| 536 | } |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 537 | |
| 538 | private final class CursorWrapperInner extends CrossProcessCursorWrapper { |
| 539 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 540 | |
| 541 | CursorWrapperInner(Cursor cursor) { |
| 542 | super(cursor); |
| 543 | mCloseGuard.open("close"); |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public void close() { |
| 548 | mCloseGuard.close(); |
| 549 | super.close(); |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | protected void finalize() throws Throwable { |
| 554 | try { |
| 555 | mCloseGuard.warnIfOpen(); |
| 556 | close(); |
| 557 | } finally { |
| 558 | super.finalize(); |
| 559 | } |
| 560 | } |
| 561 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 562 | } |