|
18 | 18 |
|
19 | 19 | /** |
20 | 20 | * A base class for Cursors that store their data in {@link CursorWindow}s. |
| 21 | + * <p> |
| 22 | + * Subclasses are responsible for filling the cursor window with data during |
| 23 | + * {@link #onMove(int, int)}, allocating a new cursor window if necessary. |
| 24 | + * During {@link #requery()}, the existing cursor window should be cleared and |
| 25 | + * filled with new data. |
| 26 | + * </p><p> |
| 27 | + * If the contents of the cursor change or become invalid, the old window must be closed |
| 28 | + * (because it is owned by the cursor) and set to null. |
| 29 | + * </p> |
21 | 30 | */ |
22 | 31 | public abstract class AbstractWindowedCursor extends AbstractCursor { |
| 32 | + /** |
| 33 | + * The cursor window owned by this cursor. |
| 34 | + */ |
| 35 | + protected CursorWindow mWindow; |
| 36 | + |
23 | 37 | @Override |
24 | 38 | public byte[] getBlob(int columnIndex) { |
25 | 39 | checkPosition(); |
@@ -126,25 +140,44 @@ protected void checkPosition() { |
126 | 140 | public CursorWindow getWindow() { |
127 | 141 | return mWindow; |
128 | 142 | } |
129 | | - |
| 143 | + |
130 | 144 | /** |
131 | | - * Set a new cursor window to cursor, usually set a remote cursor window |
132 | | - * @param window cursor window |
| 145 | + * Sets a new cursor window for the cursor to use. |
| 146 | + * <p> |
| 147 | + * The cursor takes ownership of the provided cursor window; the cursor window |
| 148 | + * will be closed when the cursor is closed or when the cursor adopts a new |
| 149 | + * cursor window. |
| 150 | + * </p><p> |
| 151 | + * If the cursor previously had a cursor window, then it is closed when the |
| 152 | + * new cursor window is assigned. |
| 153 | + * </p> |
| 154 | + * |
| 155 | + * @param window The new cursor window, typically a remote cursor window. |
133 | 156 | */ |
134 | 157 | public void setWindow(CursorWindow window) { |
135 | | - if (mWindow != null) { |
136 | | - mWindow.close(); |
| 158 | + if (window != mWindow) { |
| 159 | + closeWindow(); |
| 160 | + mWindow = window; |
137 | 161 | } |
138 | | - mWindow = window; |
139 | 162 | } |
140 | | - |
| 163 | + |
| 164 | + /** |
| 165 | + * Returns true if the cursor has an associated cursor window. |
| 166 | + * |
| 167 | + * @return True if the cursor has an associated cursor window. |
| 168 | + */ |
141 | 169 | public boolean hasWindow() { |
142 | 170 | return mWindow != null; |
143 | 171 | } |
144 | 172 |
|
145 | 173 | /** |
146 | | - * This needs be updated in {@link #onMove} by subclasses, and |
147 | | - * needs to be set to NULL when the contents of the cursor change. |
| 174 | + * Closes the cursor window and sets {@link #mWindow} to null. |
| 175 | + * @hide |
148 | 176 | */ |
149 | | - protected CursorWindow mWindow; |
| 177 | + protected void closeWindow() { |
| 178 | + if (mWindow != null) { |
| 179 | + mWindow.close(); |
| 180 | + mWindow = null; |
| 181 | + } |
| 182 | + } |
150 | 183 | } |
0 commit comments