Skip to content

Commit f6aca13

Browse files
committed
Uses default Android CursorWindow size unless overridden by application
1 parent dfecce3 commit f6aca13

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteCursor.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@
4242
public class SQLiteCursor extends AbstractWindowedCursor {
4343
static final String TAG = "SQLiteCursor";
4444
static final int NO_COUNT = -1;
45-
private static final int CURSOR_WINDOW_EXTRA = 512;
4645
private static boolean CURSOR_WINDOW_NEEDS_RECREATED = false;
47-
private static final int DEFAULT_CURSOR_WINDOW_SIZE = (int)(8 * Math.pow(1024, 2));
46+
private static final int DEFAULT_CURSOR_WINDOW_SIZE = -1;
4847
public static int PREFERRED_CURSOR_WINDOW_SIZE = DEFAULT_CURSOR_WINDOW_SIZE;
4948

5049
/** The name of the table to edit */
@@ -154,29 +153,38 @@ public static void resetCursorWindowSize() {
154153
** versions are required.
155154
*/
156155
private void awc_clearOrCreateWindow(String name) {
157-
int cursorWindowAllocationSize = PREFERRED_CURSOR_WINDOW_SIZE + CURSOR_WINDOW_EXTRA;
156+
int cursorWindowAllocationSize = PREFERRED_CURSOR_WINDOW_SIZE;
158157
if (CURSOR_WINDOW_NEEDS_RECREATED) {
159158
awc_closeWindow();
160159
CURSOR_WINDOW_NEEDS_RECREATED = false;
161160
}
162161
CursorWindow win = getWindow();
163162
if ( win==null ) {
164-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
165-
win = new CursorWindow(name, cursorWindowAllocationSize);
166-
} else {
167-
try {
168-
@SuppressLint("DiscouragedPrivateApi")
169-
Field field = CursorWindow.class.getDeclaredField("sCursorWindowSize");
170-
if (field != null) {
171-
field.setAccessible(true);
172-
field.set(null, cursorWindowAllocationSize);
173-
Log.i(TAG, String.format("Set CursorWindow allocation size to %s", cursorWindowAllocationSize));
163+
/* if the application has set a specific window size via setCursorWindowSize
164+
* then use it. Otherwise, use the system default size defined internally
165+
* by CursorWindow / com.android.internal.R.integer.config_cursorWindowSize
166+
*/
167+
if(cursorWindowAllocationSize > 0) {
168+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
169+
win = new CursorWindow(name, cursorWindowAllocationSize);
170+
} else {
171+
try {
172+
@SuppressLint("DiscouragedPrivateApi")
173+
Field field = CursorWindow.class.getDeclaredField("sCursorWindowSize");
174+
if (field != null) {
175+
field.setAccessible(true);
176+
field.set(null, cursorWindowAllocationSize);
177+
Log.i(TAG, String.format("Set CursorWindow allocation size to %s", cursorWindowAllocationSize));
178+
}
179+
} catch (Exception ex) {
180+
Log.e(TAG, "Failed to override CursorWindow allocation size", ex);
174181
}
175-
} catch (Exception ex) {
176-
Log.e(TAG, "Failed to override CursorWindow allocation size", ex);
182+
win = new CursorWindow(name);
177183
}
184+
} else {
178185
win = new CursorWindow(name);
179186
}
187+
180188
setWindow(win);
181189
}else{
182190
win.clear();

0 commit comments

Comments
 (0)