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 | |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 19 | import android.annotation.BytesLong; |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Vasu Nori | 34ad57f0 | 2010-12-21 09:32:36 -0800 | [diff] [blame] | 21 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.database.sqlite.SQLiteClosable; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 23 | import android.database.sqlite.SQLiteException; |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 24 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 27 | import android.os.Process; |
| 28 | import android.util.Log; |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 29 | import android.util.LongSparseArray; |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 30 | import android.util.SparseIntArray; |
| 31 | |
Chris Craik | 0834e35 | 2017-06-16 10:34:11 -0700 | [diff] [blame] | 32 | import dalvik.annotation.optimization.FastNative; |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 33 | import dalvik.system.CloseGuard; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * A buffer containing multiple cursor rows. |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 37 | * <p> |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 38 | * A {@link CursorWindow} is read-write when initially created and used locally. |
| 39 | * When sent to a remote process (by writing it to a {@link Parcel}), the remote process |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 40 | * receives a read-only view of the cursor window. Typically the cursor window |
| 41 | * will be allocated by the producer, filled with data, and then sent to the |
| 42 | * consumer for reading. |
| 43 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
| 45 | public class CursorWindow extends SQLiteClosable implements Parcelable { |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 46 | private static final String STATS_TAG = "CursorWindowStats"; |
| 47 | |
yingying | 1ec4f36 | 2014-05-07 17:12:25 +0800 | [diff] [blame] | 48 | // This static member will be evaluated when first used. |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 49 | @UnsupportedAppUsage |
yingying | 1ec4f36 | 2014-05-07 17:12:25 +0800 | [diff] [blame] | 50 | private static int sCursorWindowSize = -1; |
Vasu Nori | 34ad57f0 | 2010-12-21 09:32:36 -0800 | [diff] [blame] | 51 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 52 | /** |
| 53 | * The native CursorWindow object pointer. (FOR INTERNAL USE ONLY) |
| 54 | * @hide |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 55 | */ |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 56 | @UnsupportedAppUsage |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 57 | public long mWindowPtr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 59 | private int mStartPos; |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 60 | private final String mName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 62 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 63 | |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 64 | private static native long nativeCreate(String name, int cursorWindowSize); |
| 65 | private static native long nativeCreateFromParcel(Parcel parcel); |
| 66 | private static native void nativeDispose(long windowPtr); |
| 67 | private static native void nativeWriteToParcel(long windowPtr, Parcel parcel); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 68 | |
Chris Craik | 0834e35 | 2017-06-16 10:34:11 -0700 | [diff] [blame] | 69 | private static native String nativeGetName(long windowPtr); |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 70 | private static native byte[] nativeGetBlob(long windowPtr, int row, int column); |
| 71 | private static native String nativeGetString(long windowPtr, int row, int column); |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 72 | private static native void nativeCopyStringToBuffer(long windowPtr, int row, int column, |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 73 | CharArrayBuffer buffer); |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 74 | private static native boolean nativePutBlob(long windowPtr, byte[] value, int row, int column); |
Chris Craik | 0834e35 | 2017-06-16 10:34:11 -0700 | [diff] [blame] | 75 | private static native boolean nativePutString(long windowPtr, String value, |
| 76 | int row, int column); |
| 77 | |
| 78 | // Below native methods don't do unconstrained work, so are FastNative for performance |
| 79 | |
| 80 | @FastNative |
| 81 | private static native void nativeClear(long windowPtr); |
| 82 | |
| 83 | @FastNative |
| 84 | private static native int nativeGetNumRows(long windowPtr); |
| 85 | @FastNative |
| 86 | private static native boolean nativeSetNumColumns(long windowPtr, int columnNum); |
| 87 | @FastNative |
| 88 | private static native boolean nativeAllocRow(long windowPtr); |
| 89 | @FastNative |
| 90 | private static native void nativeFreeLastRow(long windowPtr); |
| 91 | |
| 92 | @FastNative |
| 93 | private static native int nativeGetType(long windowPtr, int row, int column); |
| 94 | @FastNative |
| 95 | private static native long nativeGetLong(long windowPtr, int row, int column); |
| 96 | @FastNative |
| 97 | private static native double nativeGetDouble(long windowPtr, int row, int column); |
| 98 | |
| 99 | @FastNative |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 100 | private static native boolean nativePutLong(long windowPtr, long value, int row, int column); |
Chris Craik | 0834e35 | 2017-06-16 10:34:11 -0700 | [diff] [blame] | 101 | @FastNative |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 102 | private static native boolean nativePutDouble(long windowPtr, double value, int row, int column); |
Chris Craik | 0834e35 | 2017-06-16 10:34:11 -0700 | [diff] [blame] | 103 | @FastNative |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 104 | private static native boolean nativePutNull(long windowPtr, int row, int column); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 105 | |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | /** |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 108 | * Creates a new empty cursor window and gives it a name. |
| 109 | * <p> |
| 110 | * The cursor initially has no rows or columns. Call {@link #setNumColumns(int)} to |
| 111 | * set the number of columns before adding any rows to the cursor. |
| 112 | * </p> |
| 113 | * |
| 114 | * @param name The name of the cursor window, or null if none. |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 115 | */ |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 116 | public CursorWindow(String name) { |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 117 | this(name, getCursorWindowSize()); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates a new empty cursor window and gives it a name. |
| 122 | * <p> |
| 123 | * The cursor initially has no rows or columns. Call {@link #setNumColumns(int)} to |
| 124 | * set the number of columns before adding any rows to the cursor. |
| 125 | * </p> |
| 126 | * |
| 127 | * @param name The name of the cursor window, or null if none. |
| 128 | * @param windowSizeBytes Size of cursor window in bytes. |
| 129 | * <p><strong>Note:</strong> Memory is dynamically allocated as data rows are added to the |
| 130 | * window. Depending on the amount of data stored, the actual amount of memory allocated can be |
| 131 | * lower than specified size, but cannot exceed it. |
| 132 | */ |
| 133 | public CursorWindow(String name, @BytesLong long windowSizeBytes) { |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 134 | mStartPos = 0; |
Jeff Brown | 5a05c23 | 2012-01-12 12:04:22 -0800 | [diff] [blame] | 135 | mName = name != null && name.length() != 0 ? name : "<unnamed>"; |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 136 | mWindowPtr = nativeCreate(mName, (int) windowSizeBytes); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 137 | if (mWindowPtr == 0) { |
| 138 | throw new CursorWindowAllocationException("Cursor window allocation of " + |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 139 | windowSizeBytes + " bytes failed. " + printStats()); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 140 | } |
| 141 | mCloseGuard.open("close"); |
| 142 | recordNewWindow(Binder.getCallingPid(), mWindowPtr); |
| 143 | } |
| 144 | |
| 145 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 146 | * Creates a new empty cursor window. |
| 147 | * <p> |
| 148 | * The cursor initially has no rows or columns. Call {@link #setNumColumns(int)} to |
| 149 | * set the number of columns before adding any rows to the cursor. |
| 150 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | * |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 152 | * @param localWindow True if this window will be used in this process only, |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 153 | * false if it might be sent to another processes. This argument is ignored. |
| 154 | * |
| 155 | * @deprecated There is no longer a distinction between local and remote |
| 156 | * cursor windows. Use the {@link #CursorWindow(String)} constructor instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | */ |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 158 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | public CursorWindow(boolean localWindow) { |
Jeff Brown | 5e5d6d8 | 2011-10-12 15:41:34 -0700 | [diff] [blame] | 160 | this((String)null); |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 163 | private CursorWindow(Parcel source) { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 164 | mStartPos = source.readInt(); |
Jeff Brown | 0cde89f | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 165 | mWindowPtr = nativeCreateFromParcel(source); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 166 | if (mWindowPtr == 0) { |
| 167 | throw new CursorWindowAllocationException("Cursor window could not be " |
| 168 | + "created from binder."); |
| 169 | } |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 170 | mName = nativeGetName(mWindowPtr); |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 171 | mCloseGuard.open("close"); |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 175 | protected void finalize() throws Throwable { |
| 176 | try { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 177 | if (mCloseGuard != null) { |
| 178 | mCloseGuard.warnIfOpen(); |
| 179 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 180 | dispose(); |
| 181 | } finally { |
| 182 | super.finalize(); |
| 183 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | private void dispose() { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 187 | if (mCloseGuard != null) { |
| 188 | mCloseGuard.close(); |
| 189 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 190 | if (mWindowPtr != 0) { |
| 191 | recordClosingOfWindow(mWindowPtr); |
| 192 | nativeDispose(mWindowPtr); |
| 193 | mWindowPtr = 0; |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 194 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Jeff Brown | 5a05c23 | 2012-01-12 12:04:22 -0800 | [diff] [blame] | 198 | * Gets the name of this cursor window, never null. |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 199 | * @hide |
| 200 | */ |
| 201 | public String getName() { |
| 202 | return mName; |
| 203 | } |
| 204 | |
| 205 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 206 | * Clears out the existing contents of the window, making it safe to reuse |
| 207 | * for new data. |
| 208 | * <p> |
| 209 | * The start position ({@link #getStartPosition()}), number of rows ({@link #getNumRows()}), |
| 210 | * and number of columns in the cursor are all reset to zero. |
| 211 | * </p> |
| 212 | */ |
| 213 | public void clear() { |
| 214 | acquireReference(); |
| 215 | try { |
| 216 | mStartPos = 0; |
| 217 | nativeClear(mWindowPtr); |
| 218 | } finally { |
| 219 | releaseReference(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Gets the start position of this cursor window. |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 225 | * <p> |
| 226 | * The start position is the zero-based index of the first row that this window contains |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 227 | * relative to the entire result set of the {@link Cursor}. |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 228 | * </p> |
Brad Fitzpatrick | 9ffdfa0 | 2010-03-09 13:18:02 -0800 | [diff] [blame] | 229 | * |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 230 | * @return The zero-based start position. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | */ |
| 232 | public int getStartPosition() { |
| 233 | return mStartPos; |
| 234 | } |
| 235 | |
| 236 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 237 | * Sets the start position of this cursor window. |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 238 | * <p> |
| 239 | * The start position is the zero-based index of the first row that this window contains |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 240 | * relative to the entire result set of the {@link Cursor}. |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 241 | * </p> |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 242 | * |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 243 | * @param pos The new zero-based start position. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | */ |
| 245 | public void setStartPosition(int pos) { |
| 246 | mStartPos = pos; |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 247 | } |
| 248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 250 | * Gets the number of rows in this window. |
| 251 | * |
| 252 | * @return The number of rows in this cursor window. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | */ |
| 254 | public int getNumRows() { |
| 255 | acquireReference(); |
| 256 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 257 | return nativeGetNumRows(mWindowPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } finally { |
| 259 | releaseReference(); |
| 260 | } |
| 261 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 264 | * Sets the number of columns in this window. |
| 265 | * <p> |
| 266 | * This method must be called before any rows are added to the window, otherwise |
| 267 | * it will fail to set the number of columns if it differs from the current number |
| 268 | * of columns. |
| 269 | * </p> |
| 270 | * |
| 271 | * @param columnNum The new number of columns. |
| 272 | * @return True if successful. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | */ |
| 274 | public boolean setNumColumns(int columnNum) { |
| 275 | acquireReference(); |
| 276 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 277 | return nativeSetNumColumns(mWindowPtr, columnNum); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } finally { |
| 279 | releaseReference(); |
| 280 | } |
| 281 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 284 | * Allocates a new row at the end of this cursor window. |
| 285 | * |
| 286 | * @return True if successful, false if the cursor window is out of memory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | */ |
| 288 | public boolean allocRow(){ |
| 289 | acquireReference(); |
| 290 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 291 | return nativeAllocRow(mWindowPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } finally { |
| 293 | releaseReference(); |
| 294 | } |
| 295 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 298 | * Frees the last row in this cursor window. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | */ |
| 300 | public void freeLastRow(){ |
| 301 | acquireReference(); |
| 302 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 303 | nativeFreeLastRow(mWindowPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | } finally { |
| 305 | releaseReference(); |
| 306 | } |
| 307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | |
| 309 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 310 | * Returns true if the field at the specified row and column index |
| 311 | * has type {@link Cursor#FIELD_TYPE_NULL}. |
| 312 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 313 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 314 | * @param column The zero-based column index. |
| 315 | * @return True if the field has type {@link Cursor#FIELD_TYPE_NULL}. |
| 316 | * @deprecated Use {@link #getType(int, int)} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | */ |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 318 | @Deprecated |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 319 | public boolean isNull(int row, int column) { |
| 320 | return getType(row, column) == Cursor.FIELD_TYPE_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 323 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 324 | * Returns true if the field at the specified row and column index |
| 325 | * has type {@link Cursor#FIELD_TYPE_BLOB} or {@link Cursor#FIELD_TYPE_NULL}. |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 326 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 327 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 328 | * @param column The zero-based column index. |
| 329 | * @return True if the field has type {@link Cursor#FIELD_TYPE_BLOB} or |
| 330 | * {@link Cursor#FIELD_TYPE_NULL}. |
| 331 | * @deprecated Use {@link #getType(int, int)} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | */ |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 333 | @Deprecated |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 334 | public boolean isBlob(int row, int column) { |
| 335 | int type = getType(row, column); |
Vasu Nori | 0a2c6cc | 2010-06-14 15:57:30 -0700 | [diff] [blame] | 336 | return type == Cursor.FIELD_TYPE_BLOB || type == Cursor.FIELD_TYPE_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 339 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 340 | * Returns true if the field at the specified row and column index |
| 341 | * has type {@link Cursor#FIELD_TYPE_INTEGER}. |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 342 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 343 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 344 | * @param column The zero-based column index. |
| 345 | * @return True if the field has type {@link Cursor#FIELD_TYPE_INTEGER}. |
| 346 | * @deprecated Use {@link #getType(int, int)} instead. |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 347 | */ |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 348 | @Deprecated |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 349 | public boolean isLong(int row, int column) { |
| 350 | return getType(row, column) == Cursor.FIELD_TYPE_INTEGER; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 354 | * Returns true if the field at the specified row and column index |
| 355 | * has type {@link Cursor#FIELD_TYPE_FLOAT}. |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 356 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 357 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 358 | * @param column The zero-based column index. |
| 359 | * @return True if the field has type {@link Cursor#FIELD_TYPE_FLOAT}. |
| 360 | * @deprecated Use {@link #getType(int, int)} instead. |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 361 | */ |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 362 | @Deprecated |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 363 | public boolean isFloat(int row, int column) { |
| 364 | return getType(row, column) == Cursor.FIELD_TYPE_FLOAT; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 368 | * Returns true if the field at the specified row and column index |
| 369 | * has type {@link Cursor#FIELD_TYPE_STRING} or {@link Cursor#FIELD_TYPE_NULL}. |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 370 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 371 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 372 | * @param column The zero-based column index. |
| 373 | * @return True if the field has type {@link Cursor#FIELD_TYPE_STRING} |
| 374 | * or {@link Cursor#FIELD_TYPE_NULL}. |
| 375 | * @deprecated Use {@link #getType(int, int)} instead. |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 376 | */ |
Vasu Nori | 8b0dd7d | 2010-05-18 11:54:31 -0700 | [diff] [blame] | 377 | @Deprecated |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 378 | public boolean isString(int row, int column) { |
| 379 | int type = getType(row, column); |
Vasu Nori | 0a2c6cc | 2010-06-14 15:57:30 -0700 | [diff] [blame] | 380 | return type == Cursor.FIELD_TYPE_STRING || type == Cursor.FIELD_TYPE_NULL; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 381 | } |
| 382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 384 | * Returns the type of the field at the specified row and column index. |
| 385 | * <p> |
| 386 | * The returned field types are: |
| 387 | * <ul> |
| 388 | * <li>{@link Cursor#FIELD_TYPE_NULL}</li> |
| 389 | * <li>{@link Cursor#FIELD_TYPE_INTEGER}</li> |
| 390 | * <li>{@link Cursor#FIELD_TYPE_FLOAT}</li> |
| 391 | * <li>{@link Cursor#FIELD_TYPE_STRING}</li> |
| 392 | * <li>{@link Cursor#FIELD_TYPE_BLOB}</li> |
| 393 | * </ul> |
| 394 | * </p> |
| 395 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 396 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 397 | * @param column The zero-based column index. |
| 398 | * @return The field type. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 400 | public int getType(int row, int column) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | acquireReference(); |
| 402 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 403 | return nativeGetType(mWindowPtr, row - mStartPos, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } finally { |
| 405 | releaseReference(); |
| 406 | } |
| 407 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | |
| 409 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 410 | * Gets the value of the field at the specified row and column index as a byte array. |
| 411 | * <p> |
| 412 | * The result is determined as follows: |
| 413 | * <ul> |
| 414 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_NULL}, then the result |
| 415 | * is <code>null</code>.</li> |
| 416 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_BLOB}, then the result |
| 417 | * is the blob value.</li> |
| 418 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_STRING}, then the result |
| 419 | * is the array of bytes that make up the internal representation of the |
| 420 | * string value.</li> |
| 421 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_INTEGER} or |
| 422 | * {@link Cursor#FIELD_TYPE_FLOAT}, then a {@link SQLiteException} is thrown.</li> |
| 423 | * </ul> |
| 424 | * </p> |
| 425 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 426 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 427 | * @param column The zero-based column index. |
| 428 | * @return The value of the field as a byte array. |
| 429 | */ |
| 430 | public byte[] getBlob(int row, int column) { |
| 431 | acquireReference(); |
| 432 | try { |
| 433 | return nativeGetBlob(mWindowPtr, row - mStartPos, column); |
| 434 | } finally { |
| 435 | releaseReference(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Gets the value of the field at the specified row and column index as a string. |
| 441 | * <p> |
| 442 | * The result is determined as follows: |
| 443 | * <ul> |
| 444 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_NULL}, then the result |
| 445 | * is <code>null</code>.</li> |
| 446 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_STRING}, then the result |
| 447 | * is the string value.</li> |
| 448 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_INTEGER}, then the result |
| 449 | * is a string representation of the integer in decimal, obtained by formatting the |
| 450 | * value with the <code>printf</code> family of functions using |
| 451 | * format specifier <code>%lld</code>.</li> |
| 452 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_FLOAT}, then the result |
| 453 | * is a string representation of the floating-point value in decimal, obtained by |
| 454 | * formatting the value with the <code>printf</code> family of functions using |
| 455 | * format specifier <code>%g</code>.</li> |
| 456 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_BLOB}, then a |
| 457 | * {@link SQLiteException} is thrown.</li> |
| 458 | * </ul> |
| 459 | * </p> |
| 460 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 461 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 462 | * @param column The zero-based column index. |
| 463 | * @return The value of the field as a string. |
| 464 | */ |
| 465 | public String getString(int row, int column) { |
| 466 | acquireReference(); |
| 467 | try { |
| 468 | return nativeGetString(mWindowPtr, row - mStartPos, column); |
| 469 | } finally { |
| 470 | releaseReference(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Copies the text of the field at the specified row and column index into |
| 476 | * a {@link CharArrayBuffer}. |
| 477 | * <p> |
| 478 | * The buffer is populated as follows: |
| 479 | * <ul> |
| 480 | * <li>If the buffer is too small for the value to be copied, then it is |
| 481 | * automatically resized.</li> |
| 482 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_NULL}, then the buffer |
| 483 | * is set to an empty string.</li> |
| 484 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_STRING}, then the buffer |
| 485 | * is set to the contents of the string.</li> |
| 486 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_INTEGER}, then the buffer |
| 487 | * is set to a string representation of the integer in decimal, obtained by formatting the |
| 488 | * value with the <code>printf</code> family of functions using |
| 489 | * format specifier <code>%lld</code>.</li> |
| 490 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_FLOAT}, then the buffer is |
| 491 | * set to a string representation of the floating-point value in decimal, obtained by |
| 492 | * formatting the value with the <code>printf</code> family of functions using |
| 493 | * format specifier <code>%g</code>.</li> |
| 494 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_BLOB}, then a |
| 495 | * {@link SQLiteException} is thrown.</li> |
| 496 | * </ul> |
| 497 | * </p> |
| 498 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 499 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 500 | * @param column The zero-based column index. |
| 501 | * @param buffer The {@link CharArrayBuffer} to hold the string. It is automatically |
| 502 | * resized if the requested string is larger than the buffer's current capacity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 504 | public void copyStringToBuffer(int row, int column, CharArrayBuffer buffer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | if (buffer == null) { |
| 506 | throw new IllegalArgumentException("CharArrayBuffer should not be null"); |
| 507 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | acquireReference(); |
| 509 | try { |
Jeff Brown | 0772007 | 2011-10-27 19:12:31 -0700 | [diff] [blame] | 510 | nativeCopyStringToBuffer(mWindowPtr, row - mStartPos, column, buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | } finally { |
| 512 | releaseReference(); |
| 513 | } |
| 514 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 515 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 517 | * Gets the value of the field at the specified row and column index as a <code>long</code>. |
| 518 | * <p> |
| 519 | * The result is determined as follows: |
| 520 | * <ul> |
| 521 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_NULL}, then the result |
| 522 | * is <code>0L</code>.</li> |
| 523 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_STRING}, then the result |
| 524 | * is the value obtained by parsing the string value with <code>strtoll</code>. |
| 525 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_INTEGER}, then the result |
| 526 | * is the <code>long</code> value.</li> |
| 527 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_FLOAT}, then the result |
| 528 | * is the floating-point value converted to a <code>long</code>.</li> |
| 529 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_BLOB}, then a |
| 530 | * {@link SQLiteException} is thrown.</li> |
| 531 | * </ul> |
| 532 | * </p> |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 533 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 534 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 535 | * @param column The zero-based column index. |
| 536 | * @return The value of the field as a <code>long</code>. |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 537 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 538 | public long getLong(int row, int column) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | acquireReference(); |
| 540 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 541 | return nativeGetLong(mWindowPtr, row - mStartPos, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } finally { |
| 543 | releaseReference(); |
| 544 | } |
| 545 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 546 | |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 547 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 548 | * Gets the value of the field at the specified row and column index as a |
| 549 | * <code>double</code>. |
| 550 | * <p> |
| 551 | * The result is determined as follows: |
| 552 | * <ul> |
| 553 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_NULL}, then the result |
| 554 | * is <code>0.0</code>.</li> |
| 555 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_STRING}, then the result |
| 556 | * is the value obtained by parsing the string value with <code>strtod</code>. |
| 557 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_INTEGER}, then the result |
| 558 | * is the integer value converted to a <code>double</code>.</li> |
| 559 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_FLOAT}, then the result |
| 560 | * is the <code>double</code> value.</li> |
| 561 | * <li>If the field is of type {@link Cursor#FIELD_TYPE_BLOB}, then a |
| 562 | * {@link SQLiteException} is thrown.</li> |
| 563 | * </ul> |
| 564 | * </p> |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 565 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 566 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 567 | * @param column The zero-based column index. |
| 568 | * @return The value of the field as a <code>double</code>. |
Daniel Trebbien | adf4194 | 2010-10-31 12:21:05 -0700 | [diff] [blame] | 569 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 570 | public double getDouble(int row, int column) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | acquireReference(); |
| 572 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 573 | return nativeGetDouble(mWindowPtr, row - mStartPos, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } finally { |
| 575 | releaseReference(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 580 | * Gets the value of the field at the specified row and column index as a |
| 581 | * <code>short</code>. |
| 582 | * <p> |
| 583 | * The result is determined by invoking {@link #getLong} and converting the |
| 584 | * result to <code>short</code>. |
| 585 | * </p> |
| 586 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 587 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 588 | * @param column The zero-based column index. |
| 589 | * @return The value of the field as a <code>short</code>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 591 | public short getShort(int row, int column) { |
| 592 | return (short) getLong(row, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 596 | * Gets the value of the field at the specified row and column index as an |
| 597 | * <code>int</code>. |
| 598 | * <p> |
| 599 | * The result is determined by invoking {@link #getLong} and converting the |
| 600 | * result to <code>int</code>. |
| 601 | * </p> |
| 602 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 603 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 604 | * @param column The zero-based column index. |
| 605 | * @return The value of the field as an <code>int</code>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 607 | public int getInt(int row, int column) { |
| 608 | return (int) getLong(row, column); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Gets the value of the field at the specified row and column index as a |
| 613 | * <code>float</code>. |
| 614 | * <p> |
| 615 | * The result is determined by invoking {@link #getDouble} and converting the |
| 616 | * result to <code>float</code>. |
| 617 | * </p> |
| 618 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 619 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 620 | * @param column The zero-based column index. |
| 621 | * @return The value of the field as an <code>float</code>. |
| 622 | */ |
| 623 | public float getFloat(int row, int column) { |
| 624 | return (float) getDouble(row, column); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Copies a byte array into the field at the specified row and column index. |
| 629 | * |
| 630 | * @param value The value to store. |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 631 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 632 | * @param column The zero-based column index. |
| 633 | * @return True if successful. |
| 634 | */ |
| 635 | public boolean putBlob(byte[] value, int row, int column) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | acquireReference(); |
| 637 | try { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 638 | return nativePutBlob(mWindowPtr, value, row - mStartPos, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | } finally { |
| 640 | releaseReference(); |
| 641 | } |
| 642 | } |
| 643 | |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Copies a string into the field at the specified row and column index. |
| 646 | * |
| 647 | * @param value The value to store. |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 648 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 649 | * @param column The zero-based column index. |
| 650 | * @return True if successful. |
| 651 | */ |
| 652 | public boolean putString(String value, int row, int column) { |
| 653 | acquireReference(); |
| 654 | try { |
| 655 | return nativePutString(mWindowPtr, value, row - mStartPos, column); |
| 656 | } finally { |
| 657 | releaseReference(); |
| 658 | } |
| 659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | |
| 661 | /** |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 662 | * Puts a long integer into the field at the specified row and column index. |
| 663 | * |
| 664 | * @param value The value to store. |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 665 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 666 | * @param column The zero-based column index. |
| 667 | * @return True if successful. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | */ |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 669 | public boolean putLong(long value, int row, int column) { |
| 670 | acquireReference(); |
| 671 | try { |
| 672 | return nativePutLong(mWindowPtr, value, row - mStartPos, column); |
| 673 | } finally { |
| 674 | releaseReference(); |
Vasu Nori | 0e453d1 | 2011-01-06 09:52:51 -0800 | [diff] [blame] | 675 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 677 | |
| 678 | /** |
| 679 | * Puts a double-precision floating point value into the field at the |
| 680 | * specified row and column index. |
| 681 | * |
| 682 | * @param value The value to store. |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 683 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 684 | * @param column The zero-based column index. |
| 685 | * @return True if successful. |
| 686 | */ |
| 687 | public boolean putDouble(double value, int row, int column) { |
| 688 | acquireReference(); |
| 689 | try { |
| 690 | return nativePutDouble(mWindowPtr, value, row - mStartPos, column); |
| 691 | } finally { |
| 692 | releaseReference(); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Puts a null value into the field at the specified row and column index. |
| 698 | * |
Jeff Brown | 80e7b80 | 2011-10-12 17:42:41 -0700 | [diff] [blame] | 699 | * @param row The zero-based row index. |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 700 | * @param column The zero-based column index. |
| 701 | * @return True if successful. |
| 702 | */ |
| 703 | public boolean putNull(int row, int column) { |
| 704 | acquireReference(); |
| 705 | try { |
| 706 | return nativePutNull(mWindowPtr, row - mStartPos, column); |
| 707 | } finally { |
| 708 | releaseReference(); |
| 709 | } |
| 710 | } |
| 711 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 712 | public static final @android.annotation.NonNull Parcelable.Creator<CursorWindow> CREATOR |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | = new Parcelable.Creator<CursorWindow>() { |
| 714 | public CursorWindow createFromParcel(Parcel source) { |
| 715 | return new CursorWindow(source); |
| 716 | } |
| 717 | |
| 718 | public CursorWindow[] newArray(int size) { |
| 719 | return new CursorWindow[size]; |
| 720 | } |
| 721 | }; |
| 722 | |
| 723 | public static CursorWindow newFromParcel(Parcel p) { |
| 724 | return CREATOR.createFromParcel(p); |
| 725 | } |
| 726 | |
| 727 | public int describeContents() { |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Brown | 03bd302 | 2012-03-06 13:48:56 -0800 | [diff] [blame] | 732 | acquireReference(); |
| 733 | try { |
| 734 | dest.writeInt(mStartPos); |
| 735 | nativeWriteToParcel(mWindowPtr, dest); |
| 736 | } finally { |
| 737 | releaseReference(); |
| 738 | } |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 739 | |
| 740 | if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) { |
| 741 | releaseReference(); |
| 742 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | } |
| 744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | @Override |
| 746 | protected void onAllReferencesReleased() { |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 747 | dispose(); |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 750 | @UnsupportedAppUsage |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 751 | private static final LongSparseArray<Integer> sWindowToPidMap = new LongSparseArray<Integer>(); |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 752 | |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 753 | private void recordNewWindow(int pid, long window) { |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 754 | synchronized (sWindowToPidMap) { |
| 755 | sWindowToPidMap.put(window, pid); |
| 756 | if (Log.isLoggable(STATS_TAG, Log.VERBOSE)) { |
| 757 | Log.i(STATS_TAG, "Created a new Cursor. " + printStats()); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 762 | private void recordClosingOfWindow(long window) { |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 763 | synchronized (sWindowToPidMap) { |
| 764 | if (sWindowToPidMap.size() == 0) { |
| 765 | // this means we are not in the ContentProvider. |
| 766 | return; |
| 767 | } |
| 768 | sWindowToPidMap.delete(window); |
| 769 | } |
| 770 | } |
Jeff Brown | 3bc6bbc | 2011-10-06 13:11:04 -0700 | [diff] [blame] | 771 | |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 772 | @UnsupportedAppUsage |
Vasu Nori | 6141e13 | 2010-12-23 20:46:46 -0800 | [diff] [blame] | 773 | private String printStats() { |
| 774 | StringBuilder buff = new StringBuilder(); |
| 775 | int myPid = Process.myPid(); |
| 776 | int total = 0; |
| 777 | SparseIntArray pidCounts = new SparseIntArray(); |
| 778 | synchronized (sWindowToPidMap) { |
| 779 | int size = sWindowToPidMap.size(); |
| 780 | if (size == 0) { |
| 781 | // this means we are not in the ContentProvider. |
| 782 | return ""; |
| 783 | } |
| 784 | for (int indx = 0; indx < size; indx++) { |
| 785 | int pid = sWindowToPidMap.valueAt(indx); |
| 786 | int value = pidCounts.get(pid); |
| 787 | pidCounts.put(pid, ++value); |
| 788 | } |
| 789 | } |
| 790 | int numPids = pidCounts.size(); |
| 791 | for (int i = 0; i < numPids;i++) { |
| 792 | buff.append(" (# cursors opened by "); |
| 793 | int pid = pidCounts.keyAt(i); |
| 794 | if (pid == myPid) { |
| 795 | buff.append("this proc="); |
| 796 | } else { |
| 797 | buff.append("pid " + pid + "="); |
| 798 | } |
| 799 | int num = pidCounts.get(pid); |
| 800 | buff.append(num + ")"); |
| 801 | total += num; |
| 802 | } |
| 803 | // limit the returned string size to 1000 |
| 804 | String s = (buff.length() > 980) ? buff.substring(0, 980) : buff.toString(); |
| 805 | return "# Open Cursors=" + total + s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | } |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 807 | |
Fyodor Kupolov | 6bb32a9 | 2017-10-10 10:36:43 -0700 | [diff] [blame] | 808 | private static int getCursorWindowSize() { |
| 809 | if (sCursorWindowSize < 0) { |
| 810 | // The cursor window size. resource xml file specifies the value in kB. |
| 811 | // convert it to bytes here by multiplying with 1024. |
| 812 | sCursorWindowSize = Resources.getSystem().getInteger( |
| 813 | com.android.internal.R.integer.config_cursorWindowSize) * 1024; |
| 814 | } |
| 815 | return sCursorWindowSize; |
| 816 | } |
| 817 | |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 818 | @Override |
| 819 | public String toString() { |
Ashok Bhat | 738702d | 2014-01-02 13:42:56 +0000 | [diff] [blame] | 820 | return getName() + " {" + Long.toHexString(mWindowPtr) + "}"; |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 821 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } |