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.database; |
| 18 | |
Jeff Brown | 655e66b | 2012-01-23 15:51:41 -0800 | [diff] [blame] | 19 | import android.net.Uri; |
Svetoslav | 86b1df2 | 2014-08-21 14:22:53 -0700 | [diff] [blame] | 20 | import android.os.*; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Wraps a BulkCursor around an existing Cursor making it remotable. |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 25 | * <p> |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 26 | * If the wrapped cursor returns non-null from {@link CrossProcessCursor#getWindow} |
| 27 | * then it is assumed to own the window. Otherwise, the adaptor provides a |
| 28 | * window to be filled and ensures it gets closed as needed during deactivation |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 29 | * and requeries. |
| 30 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | * |
| 32 | * {@hide} |
| 33 | */ |
Svetoslav | 86b1df2 | 2014-08-21 14:22:53 -0700 | [diff] [blame] | 34 | public final class CursorToBulkCursorAdaptor extends BulkCursorNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | implements IBinder.DeathRecipient { |
| 36 | private static final String TAG = "Cursor"; |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 37 | |
| 38 | private final Object mLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | private final String mProviderName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | private ContentObserverProxy mObserver; |
| 41 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 42 | /** |
| 43 | * The cursor that is being adapted. |
| 44 | * This field is set to null when the cursor is closed. |
| 45 | */ |
| 46 | private CrossProcessCursor mCursor; |
| 47 | |
| 48 | /** |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 49 | * The cursor window that was filled by the cross process cursor in the |
| 50 | * case where the cursor does not support getWindow. |
| 51 | * This field is only ever non-null when the window has actually be filled. |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 52 | */ |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 53 | private CursorWindow mFilledWindow; |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 54 | |
| 55 | private static final class ContentObserverProxy extends ContentObserver { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | protected IContentObserver mRemote; |
| 57 | |
| 58 | public ContentObserverProxy(IContentObserver remoteObserver, DeathRecipient recipient) { |
| 59 | super(null); |
| 60 | mRemote = remoteObserver; |
| 61 | try { |
| 62 | remoteObserver.asBinder().linkToDeath(recipient, 0); |
| 63 | } catch (RemoteException e) { |
| 64 | // Do nothing, the far side is dead |
| 65 | } |
| 66 | } |
Svetoslav | 86b1df2 | 2014-08-21 14:22:53 -0700 | [diff] [blame] | 67 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | public boolean unlinkToDeath(DeathRecipient recipient) { |
| 69 | return mRemote.asBinder().unlinkToDeath(recipient, 0); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean deliverSelfNotifications() { |
| 74 | // The far side handles the self notifications. |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | @Override |
Jeff Brown | 655e66b | 2012-01-23 15:51:41 -0800 | [diff] [blame] | 79 | public void onChange(boolean selfChange, Uri uri) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | try { |
Svetoslav | 86b1df2 | 2014-08-21 14:22:53 -0700 | [diff] [blame] | 81 | mRemote.onChange(selfChange, uri, android.os.Process.myUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } catch (RemoteException ex) { |
| 83 | // Do nothing, the far side is dead |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 88 | public CursorToBulkCursorAdaptor(Cursor cursor, IContentObserver observer, |
| 89 | String providerName) { |
Jeff Brown | 825c513 | 2011-10-12 16:11:30 -0700 | [diff] [blame] | 90 | if (cursor instanceof CrossProcessCursor) { |
| 91 | mCursor = (CrossProcessCursor)cursor; |
| 92 | } else { |
| 93 | mCursor = new CrossProcessCursorWrapper(cursor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | mProviderName = providerName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 97 | synchronized (mLock) { |
| 98 | createAndRegisterObserverProxyLocked(observer); |
| 99 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 101 | |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 102 | private void closeFilledWindowLocked() { |
| 103 | if (mFilledWindow != null) { |
| 104 | mFilledWindow.close(); |
| 105 | mFilledWindow = null; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | private void disposeLocked() { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 110 | if (mCursor != null) { |
| 111 | unregisterObserverProxyLocked(); |
| 112 | mCursor.close(); |
| 113 | mCursor = null; |
| 114 | } |
| 115 | |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 116 | closeFilledWindowLocked(); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private void throwIfCursorIsClosed() { |
| 120 | if (mCursor == null) { |
| 121 | throw new StaleDataException("Attempted to access a cursor after it has been closed."); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | public void binderDied() { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 127 | synchronized (mLock) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 128 | disposeLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 131 | |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Returns an object that contains sufficient metadata to reconstruct |
| 134 | * the cursor remotely. May throw if an error occurs when executing the query |
| 135 | * and obtaining the row count. |
| 136 | */ |
Jeff Brown | fb5a496 | 2012-03-14 17:38:59 -0700 | [diff] [blame] | 137 | public BulkCursorDescriptor getBulkCursorDescriptor() { |
| 138 | synchronized (mLock) { |
| 139 | throwIfCursorIsClosed(); |
| 140 | |
| 141 | BulkCursorDescriptor d = new BulkCursorDescriptor(); |
| 142 | d.cursor = this; |
| 143 | d.columnNames = mCursor.getColumnNames(); |
| 144 | d.wantsAllOnMoveCalls = mCursor.getWantsAllOnMoveCalls(); |
| 145 | d.count = mCursor.getCount(); |
| 146 | d.window = mCursor.getWindow(); |
| 147 | if (d.window != null) { |
| 148 | // Acquire a reference to the window because its reference count will be |
| 149 | // decremented when it is returned as part of the binder call reply parcel. |
| 150 | d.window.acquireReference(); |
| 151 | } |
| 152 | return d; |
| 153 | } |
| 154 | } |
| 155 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 156 | @Override |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 157 | public CursorWindow getWindow(int position) { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 158 | synchronized (mLock) { |
| 159 | throwIfCursorIsClosed(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 161 | if (!mCursor.moveToPosition(position)) { |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 162 | closeFilledWindowLocked(); |
| 163 | return null; |
| 164 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 165 | |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 166 | CursorWindow window = mCursor.getWindow(); |
| 167 | if (window != null) { |
| 168 | closeFilledWindowLocked(); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 169 | } else { |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 170 | window = mFilledWindow; |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 171 | if (window == null) { |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 172 | mFilledWindow = new CursorWindow(mProviderName); |
| 173 | window = mFilledWindow; |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 174 | } else if (position < window.getStartPosition() |
| 175 | || position >= window.getStartPosition() + window.getNumRows()) { |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 176 | window.clear(); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 177 | } |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 178 | mCursor.fillWindow(position, window); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 180 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 181 | if (window != null) { |
Jeff Brown | fb5a496 | 2012-03-14 17:38:59 -0700 | [diff] [blame] | 182 | // Acquire a reference to the window because its reference count will be |
| 183 | // decremented when it is returned as part of the binder call reply parcel. |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 184 | window.acquireReference(); |
| 185 | } |
| 186 | return window; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public void onMove(int position) { |
| 192 | synchronized (mLock) { |
| 193 | throwIfCursorIsClosed(); |
| 194 | |
| 195 | mCursor.onMove(mCursor.getPosition(), position); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 200 | public void deactivate() { |
| 201 | synchronized (mLock) { |
| 202 | if (mCursor != null) { |
| 203 | unregisterObserverProxyLocked(); |
| 204 | mCursor.deactivate(); |
| 205 | } |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 206 | |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 207 | closeFilledWindowLocked(); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void close() { |
| 213 | synchronized (mLock) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 214 | disposeLocked(); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | @Override |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 219 | public int requery(IContentObserver observer) { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 220 | synchronized (mLock) { |
| 221 | throwIfCursorIsClosed(); |
| 222 | |
Jeff Brown | b4009c7 | 2011-10-12 16:05:41 -0700 | [diff] [blame] | 223 | closeFilledWindowLocked(); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 224 | |
| 225 | try { |
| 226 | if (!mCursor.requery()) { |
| 227 | return -1; |
| 228 | } |
| 229 | } catch (IllegalStateException e) { |
| 230 | IllegalStateException leakProgram = new IllegalStateException( |
| 231 | mProviderName + " Requery misuse db, mCursor isClosed:" + |
| 232 | mCursor.isClosed(), e); |
| 233 | throw leakProgram; |
| 234 | } |
| 235 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 236 | unregisterObserverProxyLocked(); |
| 237 | createAndRegisterObserverProxyLocked(observer); |
| 238 | return mCursor.getCount(); |
| 239 | } |
| 240 | } |
| 241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Create a ContentObserver from the observer and register it as an observer on the |
| 244 | * underlying cursor. |
| 245 | * @param observer the IContentObserver that wants to monitor the cursor |
| 246 | * @throws IllegalStateException if an observer is already registered |
| 247 | */ |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 248 | private void createAndRegisterObserverProxyLocked(IContentObserver observer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | if (mObserver != null) { |
| 250 | throw new IllegalStateException("an observer is already registered"); |
| 251 | } |
| 252 | mObserver = new ContentObserverProxy(observer, this); |
| 253 | mCursor.registerContentObserver(mObserver); |
| 254 | } |
| 255 | |
| 256 | /** Unregister the observer if it is already registered. */ |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 257 | private void unregisterObserverProxyLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | if (mObserver != null) { |
| 259 | mCursor.unregisterContentObserver(mObserver); |
| 260 | mObserver.unlinkToDeath(this); |
| 261 | mObserver = null; |
| 262 | } |
| 263 | } |
| 264 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 265 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | public Bundle getExtras() { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 267 | synchronized (mLock) { |
| 268 | throwIfCursorIsClosed(); |
| 269 | |
| 270 | return mCursor.getExtras(); |
| 271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 274 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | public Bundle respond(Bundle extras) { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 276 | synchronized (mLock) { |
| 277 | throwIfCursorIsClosed(); |
| 278 | |
| 279 | return mCursor.respond(extras); |
| 280 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | } |