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 | |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | /** |
| 22 | * A base class for Cursors that store their data in {@link CursorWindow}s. |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 23 | * <p> |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 24 | * The cursor owns the cursor window it uses. When the cursor is closed, |
| 25 | * its window is also closed. Likewise, when the window used by the cursor is |
| 26 | * changed, its old window is closed. This policy of strict ownership ensures |
| 27 | * that cursor windows are not leaked. |
| 28 | * </p><p> |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 29 | * Subclasses are responsible for filling the cursor window with data during |
| 30 | * {@link #onMove(int, int)}, allocating a new cursor window if necessary. |
| 31 | * During {@link #requery()}, the existing cursor window should be cleared and |
| 32 | * filled with new data. |
| 33 | * </p><p> |
| 34 | * If the contents of the cursor change or become invalid, the old window must be closed |
| 35 | * (because it is owned by the cursor) and set to null. |
| 36 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | */ |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 38 | public abstract class AbstractWindowedCursor extends AbstractCursor { |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 39 | /** |
| 40 | * The cursor window owned by this cursor. |
| 41 | */ |
| 42 | protected CursorWindow mWindow; |
| 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 45 | public byte[] getBlob(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | return mWindow.getBlob(mPos, columnIndex); |
| 48 | } |
| 49 | |
| 50 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 51 | public String getString(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | return mWindow.getString(mPos, columnIndex); |
| 54 | } |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 57 | public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | mWindow.copyStringToBuffer(mPos, columnIndex, buffer); |
| 60 | } |
| 61 | |
| 62 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 63 | public short getShort(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | return mWindow.getShort(mPos, columnIndex); |
| 66 | } |
| 67 | |
| 68 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 69 | public int getInt(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | return mWindow.getInt(mPos, columnIndex); |
| 72 | } |
| 73 | |
| 74 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 75 | public long getLong(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | return mWindow.getLong(mPos, columnIndex); |
| 78 | } |
| 79 | |
| 80 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 81 | public float getFloat(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | return mWindow.getFloat(mPos, columnIndex); |
| 84 | } |
| 85 | |
| 86 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 87 | public double getDouble(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | checkPosition(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | return mWindow.getDouble(mPos, columnIndex); |
| 90 | } |
| 91 | |
| 92 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 93 | public boolean isNull(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | checkPosition(); |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 95 | return mWindow.getType(mPos, columnIndex) == Cursor.FIELD_TYPE_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 98 | /** |
| 99 | * @deprecated Use {@link #getType} |
| 100 | */ |
| 101 | @Deprecated |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 102 | public boolean isBlob(int columnIndex) { |
| 103 | return getType(columnIndex) == Cursor.FIELD_TYPE_BLOB; |
| 104 | } |
| 105 | |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 106 | /** |
| 107 | * @deprecated Use {@link #getType} |
| 108 | */ |
| 109 | @Deprecated |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 110 | public boolean isString(int columnIndex) { |
| 111 | return getType(columnIndex) == Cursor.FIELD_TYPE_STRING; |
| 112 | } |
| 113 | |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 114 | /** |
| 115 | * @deprecated Use {@link #getType} |
| 116 | */ |
| 117 | @Deprecated |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 118 | public boolean isLong(int columnIndex) { |
| 119 | return getType(columnIndex) == Cursor.FIELD_TYPE_INTEGER; |
| 120 | } |
| 121 | |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 122 | /** |
| 123 | * @deprecated Use {@link #getType} |
| 124 | */ |
| 125 | @Deprecated |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 126 | public boolean isFloat(int columnIndex) { |
| 127 | return getType(columnIndex) == Cursor.FIELD_TYPE_FLOAT; |
| 128 | } |
| 129 | |
| 130 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 131 | public int getType(int columnIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | checkPosition(); |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 133 | return mWindow.getType(mPos, columnIndex); |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | @Override |
Jeff Hamilton | f1a4a0a | 2010-06-30 15:10:24 -0500 | [diff] [blame] | 137 | protected void checkPosition() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | super.checkPosition(); |
| 139 | |
| 140 | if (mWindow == null) { |
Vasu Nori | 8a358a7 | 2011-03-01 18:22:31 -0800 | [diff] [blame] | 141 | throw new StaleDataException("Attempting to access a closed CursorWindow." + |
| 142 | "Most probable cause: cursor is deactivated prior to calling this method."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public CursorWindow getWindow() { |
| 148 | return mWindow; |
| 149 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | /** |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 152 | * Sets a new cursor window for the cursor to use. |
| 153 | * <p> |
| 154 | * The cursor takes ownership of the provided cursor window; the cursor window |
| 155 | * will be closed when the cursor is closed or when the cursor adopts a new |
| 156 | * cursor window. |
| 157 | * </p><p> |
| 158 | * If the cursor previously had a cursor window, then it is closed when the |
| 159 | * new cursor window is assigned. |
| 160 | * </p> |
| 161 | * |
| 162 | * @param window The new cursor window, typically a remote cursor window. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | */ |
| 164 | public void setWindow(CursorWindow window) { |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 165 | if (window != mWindow) { |
| 166 | closeWindow(); |
| 167 | mWindow = window; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * Returns true if the cursor has an associated cursor window. |
| 173 | * |
| 174 | * @return True if the cursor has an associated cursor window. |
| 175 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | public boolean hasWindow() { |
| 177 | return mWindow != null; |
| 178 | } |
| 179 | |
| 180 | /** |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 181 | * Closes the cursor window and sets {@link #mWindow} to null. |
| 182 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | */ |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 184 | @UnsupportedAppUsage |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 185 | protected void closeWindow() { |
| 186 | if (mWindow != null) { |
| 187 | mWindow.close(); |
| 188 | mWindow = null; |
| 189 | } |
| 190 | } |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * If there is a window, clear it. |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 194 | * Otherwise, creates a new window. |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 195 | * |
| 196 | * @param name The window name. |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 197 | * @hide |
| 198 | */ |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 199 | @UnsupportedAppUsage |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 200 | protected void clearOrCreateWindow(String name) { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 201 | if (mWindow == null) { |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 202 | mWindow = new CursorWindow(name); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 203 | } else { |
| 204 | mWindow.clear(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** @hide */ |
| 209 | @Override |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 210 | @UnsupportedAppUsage |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 211 | protected void onDeactivateOrClose() { |
| 212 | super.onDeactivateOrClose(); |
| 213 | closeWindow(); |
| 214 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |