blob: a988f0684fca4d0916beb22773d11f6f88d2ec15 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.database;
18
Mathew Inwood41b31942018-08-10 16:00:53 +010019import android.annotation.UnsupportedAppUsage;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021/**
22 * A base class for Cursors that store their data in {@link CursorWindow}s.
Jeff Brown7ce74522011-10-07 13:29:37 -070023 * <p>
Jeff Brownd2183652011-10-09 12:39:53 -070024 * 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 Brown7ce74522011-10-07 13:29:37 -070029 * 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 Project9066cfe2009-03-03 19:31:44 -080037 */
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050038public abstract class AbstractWindowedCursor extends AbstractCursor {
Jeff Brown7ce74522011-10-07 13:29:37 -070039 /**
40 * The cursor window owned by this cursor.
41 */
42 protected CursorWindow mWindow;
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050045 public byte[] getBlob(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 return mWindow.getBlob(mPos, columnIndex);
48 }
49
50 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050051 public String getString(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 return mWindow.getString(mPos, columnIndex);
54 }
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050057 public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 mWindow.copyStringToBuffer(mPos, columnIndex, buffer);
60 }
61
62 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050063 public short getShort(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 return mWindow.getShort(mPos, columnIndex);
66 }
67
68 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050069 public int getInt(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 return mWindow.getInt(mPos, columnIndex);
72 }
73
74 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050075 public long getLong(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 return mWindow.getLong(mPos, columnIndex);
78 }
79
80 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050081 public float getFloat(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 return mWindow.getFloat(mPos, columnIndex);
84 }
85
86 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050087 public double getDouble(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 checkPosition();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 return mWindow.getDouble(mPos, columnIndex);
90 }
91
92 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050093 public boolean isNull(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 checkPosition();
Vasu Nori8b0dd7d2010-05-18 11:54:31 -070095 return mWindow.getType(mPos, columnIndex) == Cursor.FIELD_TYPE_NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 }
97
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -050098 /**
99 * @deprecated Use {@link #getType}
100 */
101 @Deprecated
Vasu Nori8b0dd7d2010-05-18 11:54:31 -0700102 public boolean isBlob(int columnIndex) {
103 return getType(columnIndex) == Cursor.FIELD_TYPE_BLOB;
104 }
105
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -0500106 /**
107 * @deprecated Use {@link #getType}
108 */
109 @Deprecated
Vasu Nori8b0dd7d2010-05-18 11:54:31 -0700110 public boolean isString(int columnIndex) {
111 return getType(columnIndex) == Cursor.FIELD_TYPE_STRING;
112 }
113
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -0500114 /**
115 * @deprecated Use {@link #getType}
116 */
117 @Deprecated
Vasu Nori8b0dd7d2010-05-18 11:54:31 -0700118 public boolean isLong(int columnIndex) {
119 return getType(columnIndex) == Cursor.FIELD_TYPE_INTEGER;
120 }
121
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -0500122 /**
123 * @deprecated Use {@link #getType}
124 */
125 @Deprecated
Vasu Nori8b0dd7d2010-05-18 11:54:31 -0700126 public boolean isFloat(int columnIndex) {
127 return getType(columnIndex) == Cursor.FIELD_TYPE_FLOAT;
128 }
129
130 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -0500131 public int getType(int columnIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 checkPosition();
Vasu Nori8b0dd7d2010-05-18 11:54:31 -0700133 return mWindow.getType(mPos, columnIndex);
Fred Quintana03d94902009-05-22 14:23:31 -0700134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 @Override
Jeff Hamiltonf1a4a0a2010-06-30 15:10:24 -0500137 protected void checkPosition() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 super.checkPosition();
139
140 if (mWindow == null) {
Vasu Nori8a358a72011-03-01 18:22:31 -0800141 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 Project9066cfe2009-03-03 19:31:44 -0800143 }
144 }
145
146 @Override
147 public CursorWindow getWindow() {
148 return mWindow;
149 }
Jeff Brown7ce74522011-10-07 13:29:37 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
Jeff Brown7ce74522011-10-07 13:29:37 -0700152 * 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 Project9066cfe2009-03-03 19:31:44 -0800163 */
164 public void setWindow(CursorWindow window) {
Jeff Brown7ce74522011-10-07 13:29:37 -0700165 if (window != mWindow) {
166 closeWindow();
167 mWindow = window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
Jeff Brown7ce74522011-10-07 13:29:37 -0700170
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 Project9066cfe2009-03-03 19:31:44 -0800176 public boolean hasWindow() {
177 return mWindow != null;
178 }
179
180 /**
Jeff Brown7ce74522011-10-07 13:29:37 -0700181 * Closes the cursor window and sets {@link #mWindow} to null.
182 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 */
Mathew Inwood41b31942018-08-10 16:00:53 +0100184 @UnsupportedAppUsage
Jeff Brown7ce74522011-10-07 13:29:37 -0700185 protected void closeWindow() {
186 if (mWindow != null) {
187 mWindow.close();
188 mWindow = null;
189 }
190 }
Jeff Brownd2183652011-10-09 12:39:53 -0700191
192 /**
193 * If there is a window, clear it.
Jeff Brown5e5d6d82011-10-12 15:41:34 -0700194 * Otherwise, creates a new window.
Jeff Brown0cde89f2011-10-10 14:50:10 -0700195 *
196 * @param name The window name.
Jeff Brownd2183652011-10-09 12:39:53 -0700197 * @hide
198 */
Mathew Inwood41b31942018-08-10 16:00:53 +0100199 @UnsupportedAppUsage
Jeff Brown5e5d6d82011-10-12 15:41:34 -0700200 protected void clearOrCreateWindow(String name) {
Jeff Brownd2183652011-10-09 12:39:53 -0700201 if (mWindow == null) {
Jeff Brown5e5d6d82011-10-12 15:41:34 -0700202 mWindow = new CursorWindow(name);
Jeff Brownd2183652011-10-09 12:39:53 -0700203 } else {
204 mWindow.clear();
205 }
206 }
207
208 /** @hide */
209 @Override
Mathew Inwood41b31942018-08-10 16:00:53 +0100210 @UnsupportedAppUsage
Jeff Brownd2183652011-10-09 12:39:53 -0700211 protected void onDeactivateOrClose() {
212 super.onDeactivateOrClose();
213 closeWindow();
214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215}