2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.helpers;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.database.Cursor;
25 import android.database.DatabaseUtils;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
29 public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
30 private static final int SCHEMA_VERSION = 1;
31 static final String BOOKMARKS_DATABASE = "bookmarks.db";
32 static final String BOOKMARKS_TABLE = "bookmarks";
34 public static final String _ID = "_id";
35 public static final String BOOKMARK_NAME = "bookmarkname";
36 public static final String BOOKMARK_URL = "bookmarkurl";
37 public static final String PARENT_FOLDER = "parentfolder";
38 public static final String DISPLAY_ORDER = "displayorder";
39 public static final String IS_FOLDER = "isfolder";
40 public static final String FAVORITE_ICON = "favoriteicon";
42 static final String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" +
43 _ID + " INTEGER PRIMARY KEY, " +
44 BOOKMARK_NAME + " TEXT, " +
45 BOOKMARK_URL + " TEXT, " +
46 PARENT_FOLDER + " TEXT, " +
47 DISPLAY_ORDER + " INTEGER, " +
48 IS_FOLDER + " BOOLEAN, " +
49 FAVORITE_ICON + " BLOB)";
51 // Initialize the database. The lint warnings for the unused parameters are suppressed.
52 public BookmarksDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
53 super(context, BOOKMARKS_DATABASE, cursorFactory, SCHEMA_VERSION);
57 public void onCreate(SQLiteDatabase bookmarksDatabase) {
58 // Create the bookmarks table.
59 bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
63 public void onUpgrade(SQLiteDatabase bookmarksDatabase, int oldVersion, int newVersion) {
64 // Code for upgrading the database will be added here when the schema version > 1.
68 public void createBookmark(String bookmarkName, String bookmarkURL, String parentFolder, int displayOrder, byte[] favoriteIcon) {
69 // Store the bookmark data in a `ContentValues`.
70 ContentValues bookmarkContentValues = new ContentValues();
72 // ID is created automatically.
73 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
74 bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
75 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
76 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
77 bookmarkContentValues.put(IS_FOLDER, false);
78 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
80 // Get a writable database handle.
81 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
83 // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created.
84 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
86 // Close the database handle.
87 bookmarksDatabase.close();
90 // Create a bookmark from content values.
91 void createBookmark(ContentValues contentValues) {
92 // Get a writable database.
93 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
95 // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created.
96 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, contentValues);
98 // Close the database handle.
99 bookmarksDatabase.close();
103 public void createFolder(String folderName, String parentFolder, byte[] favoriteIcon) {
104 ContentValues bookmarkContentValues = new ContentValues();
106 // ID is created automatically. Folders are always created at the top of the list.
107 bookmarkContentValues.put(BOOKMARK_NAME, folderName);
108 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
109 bookmarkContentValues.put(DISPLAY_ORDER, 0);
110 bookmarkContentValues.put(IS_FOLDER, true);
111 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
113 // Get a writable database handle.
114 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
116 // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case.
117 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
119 // Close the database handle.
120 bookmarksDatabase.close();
123 // Get a `Cursor` for the bookmark with the specified database ID.
124 public Cursor getBookmarkCursor(int databaseId) {
125 // Get a readable database handle.
126 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
128 // Prepare the SQL statement to get the cursor for the database ID.
129 String GET_ONE_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE +
130 " WHERE " + _ID + " = " + databaseId;
132 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. We can't close the `Cursor` because we need to use it in the parent activity.
133 return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
136 // Get the folder name for the specified database ID.
137 public String getFolderName (int databaseId) {
138 // Get a readable database handle.
139 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
141 // Prepare the SQL statement to get the cursor for the folder.
142 String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
143 " WHERE " + _ID + " = " + databaseId;
145 // Get a folder cursor.
146 Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
148 // Get the folder name.
149 folderCursor.moveToFirst();
150 String folderName = folderCursor.getString(folderCursor.getColumnIndex(BOOKMARK_NAME));
152 // Close the cursor and the database handle.
153 folderCursor.close();
154 bookmarksDatabase.close();
156 // Return the folder name.
160 // The the database ID for the specified folder name.
161 public int getFolderDatabaseId (String folderName) {
162 // Get a readable database handle.
163 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
165 // SQL escape `folderName`.
166 folderName = DatabaseUtils.sqlEscapeString(folderName);
168 // Prepare the SQL statement to get the `Cursor` for the folder.
169 String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
170 " WHERE " + BOOKMARK_NAME + " = " + folderName +
171 " AND " + IS_FOLDER + " = " + 1;
173 // Get `folderCursor`. The second argument is `null` because there are no `selectionArgs`.
174 Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
176 // Get the database ID.
177 folderCursor.moveToFirst();
178 int databaseId = folderCursor.getInt(folderCursor.getColumnIndex(_ID));
180 // Close the cursor and the database handle.
181 folderCursor.close();
182 bookmarksDatabase.close();
184 // Return the database ID.
188 // Get a `Cursor` for the specified folder name.
189 public Cursor getFolderCursor(String folderName) {
190 // Get a readable database handle.
191 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
193 // SQL escape `folderName`.
194 folderName = DatabaseUtils.sqlEscapeString(folderName);
196 // Prepare the SQL statement to get the `Cursor` for the folder.
197 String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
198 " WHERE " + BOOKMARK_NAME + " = " + folderName +
199 " AND " + IS_FOLDER + " = " + 1;
201 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
202 // We can't close the `Cursor` because we need to use it in the parent activity.
203 return bookmarksDatabase.rawQuery(GET_FOLDER, null);
206 // Get a `Cursor` of all the folders except those specified.
207 public Cursor getFoldersCursorExcept(String exceptFolders) {
208 // Get a readable database handle.
209 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
211 // Prepare the SQL statement to get the `Cursor` for the folders.
212 String GET_FOLDERS_EXCEPT = "SELECT * FROM " + BOOKMARKS_TABLE +
213 " WHERE " + IS_FOLDER + " = " + 1 +
214 " AND " + BOOKMARK_NAME + " NOT IN (" + exceptFolders +
215 ") ORDER BY " + BOOKMARK_NAME + " ASC";
217 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
218 // We can't close the `Cursor` because we need to use it in the parent activity.
219 return bookmarksDatabase.rawQuery(GET_FOLDERS_EXCEPT, null);
222 // Get a `Cursor` with all the subfolders of the specified folder.
223 public Cursor getSubfoldersCursor(String currentFolder) {
224 // Get a readable database handle.
225 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
227 // SQL escape `currentFolder.
228 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
230 // Prepare the SQL statement to get the `Cursor` for the subfolders.
231 String GET_SUBFOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE +
232 " WHERE " + PARENT_FOLDER + " = " + currentFolder +
233 " AND " + IS_FOLDER + " = " + 1;
235 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
236 // We can't close the `Cursor` because we need to use it in the parent activity.
237 return bookmarksDatabase.rawQuery(GET_SUBFOLDERS, null);
240 // Get a `String` with the name of the parent folder.
241 public String getParentFolder(String currentFolder) {
242 // Get a readable database handle.
243 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
245 // SQL escape `currentFolder`.
246 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
248 // Prepare the SQL statement to get the parent folder.
249 String GET_PARENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
250 " WHERE " + IS_FOLDER + " = " + 1 +
251 " AND " + BOOKMARK_NAME + " = " + currentFolder;
253 // Get the bookmark cursor and move to the first entry.
254 Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null);
255 bookmarkCursor.moveToFirst();
257 // Store the name of the parent folder.
258 String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
260 // Close the `Cursor`.
261 bookmarkCursor.close();
266 // Get a `Cursor` of all the folders.
267 public Cursor getAllFoldersCursor() {
268 // Get a readable database handle.
269 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
271 // Prepare the SQL statement to get the `Cursor` for all the folders.
272 String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE +
273 " WHERE " + IS_FOLDER + " = " + 1 +
274 " ORDER BY " + BOOKMARK_NAME + " ASC";
276 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
277 // We can't close the `Cursor` because we need to use it in the parent activity.
278 return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null);
281 // Get a cursor for all bookmarks and folders.
282 public Cursor getAllBookmarksCursor() {
283 // Get a readable database handle.
284 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
286 // Get everything in the bookmarks table.
287 String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE;
289 // Return the result as a Cursor. The Cursor cannot be closed because it is used in the parent activity.
290 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
293 // Get a `Cursor` for all bookmarks and folders in the specified folder.
294 public Cursor getAllBookmarksCursor(String folderName) {
295 // Get a readable database handle.
296 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
298 // SQL escape the folder name.
299 folderName = DatabaseUtils.sqlEscapeString(folderName);
301 // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
302 String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
303 " WHERE " + PARENT_FOLDER + " = " + folderName;
305 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
306 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
309 // Get a cursor for all bookmarks and folders ordered by display order.
310 public Cursor getAllBookmarksCursorByDisplayOrder() {
311 // Get a readable database handle.
312 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
314 // Get everything in the bookmarks table ordered by display order.
315 String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
316 " ORDER BY " + DISPLAY_ORDER + " ASC";
318 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
319 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
322 // Get a cursor for all bookmarks and folders in the specified folder ordered by display order.
323 public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
324 // Get a readable database handle.
325 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
327 // SQL escape `folderName`.
328 folderName = DatabaseUtils.sqlEscapeString(folderName);
330 // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
331 String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
332 " WHERE " + PARENT_FOLDER + " = " + folderName +
333 " ORDER BY " + DISPLAY_ORDER + " ASC";
335 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
336 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
339 // Get a `Cursor` for all bookmarks and folders in the specified folder except for a specific list of IDs.
340 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
341 // Get a readable database handle.
342 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
344 // Prepare a string builder that contains the comma-separated list of IDs not to get.
345 StringBuilder doNotGetIdsStringBuilder = new StringBuilder();
347 // Extract the array to `doNotGetIdsString`.
348 for (long databaseIdLong : exceptIdLongArray) {
349 // If this is the first number, only add the number.
350 if (doNotGetIdsStringBuilder.toString().isEmpty()) {
351 doNotGetIdsStringBuilder.append(databaseIdLong);
352 } else { // If there already is a number in the string, place a `,` before the new number.
353 doNotGetIdsStringBuilder.append(",");
354 doNotGetIdsStringBuilder.append(databaseIdLong);
358 // SQL escape `folderName`.
359 folderName = DatabaseUtils.sqlEscapeString(folderName);
361 // Prepare the SQL statement to select all items except those with the specified IDs.
362 String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
363 " WHERE " + PARENT_FOLDER + " = " + folderName +
364 " AND " + _ID + " NOT IN (" + doNotGetIdsStringBuilder.toString() +
365 ") ORDER BY " + DISPLAY_ORDER + " ASC";
367 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
368 // We can't close the `Cursor` because we need to use it in the parent activity.
369 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
372 // Check if a database ID is a folder.
373 public boolean isFolder(int databaseId) {
374 // Get a readable database handle.
375 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
377 // Prepare the SQL statement to determine if `databaseId` is a folder.
378 String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
379 " WHERE " + _ID + " = " + databaseId;
381 // Populate folderCursor. The second argument is `null` because there are no `selectionArgs`.
382 Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
384 // Ascertain if this database ID is a folder.
385 folderCursor.moveToFirst();
386 boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
388 // Close the `Cursor` and the database handle.
389 folderCursor.close();
390 bookmarksDatabase.close();
395 // Update the bookmark name and URL.
396 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
397 // Initialize a `ContentValues`.
398 ContentValues bookmarkContentValues = new ContentValues();
400 // Store the updated values.
401 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
402 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
404 // Get a writable database handle.
405 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
407 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
408 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
410 // Close the database handle.
411 bookmarksDatabase.close();
414 // Update the bookmark name, URL, parent folder, and display order.
415 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder) {
416 // Initialize a `ContentValues`.
417 ContentValues bookmarkContentValues = new ContentValues();
419 // Store the updated values.
420 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
421 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
422 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
423 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
425 // Get a writable database handle.
426 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
428 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
429 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
431 // Close the database handle.
432 bookmarksDatabase.close();
435 // Update the bookmark name, URL, and favorite icon.
436 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
437 // Initialize a `ContentValues`.
438 ContentValues bookmarkContentValues = new ContentValues();
440 // Store the updated values.
441 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
442 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
443 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
445 // Get a writable database handle.
446 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
448 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
449 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
451 // Close the database handle.
452 bookmarksDatabase.close();
455 // Update the bookmark name, URL, parent folder, display order, and favorite icon.
456 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder, byte[] favoriteIcon) {
457 // Initialize a `ContentValues`.
458 ContentValues bookmarkContentValues = new ContentValues();
460 // Store the updated values.
461 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
462 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
463 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
464 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
465 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
467 // Get a writable database handle.
468 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
470 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
471 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
473 // Close the database handle.
474 bookmarksDatabase.close();
477 // Update the folder name.
478 public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
479 // Get a writable database handle.
480 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
482 // Update the folder first. Store the new folder name in `folderContentValues`.
483 ContentValues folderContentValues = new ContentValues();
484 folderContentValues.put(BOOKMARK_NAME, newFolderName);
486 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
487 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
489 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
490 ContentValues bookmarkContentValues = new ContentValues();
491 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
493 // SQL escape `oldFolderName`.
494 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
496 // Run the update on all the bookmarks that currently list `oldFolderName` as their parent folder. The last argument is `null` because there are no `whereArgs`.
497 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
499 // Close the database handle.
500 bookmarksDatabase.close();
503 // Update the folder icon.
504 public void updateFolder(int databaseId, byte[] folderIcon) {
505 // Get a writable database handle.
506 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
508 // Store the updated icon in `folderContentValues`.
509 ContentValues folderContentValues = new ContentValues();
510 folderContentValues.put(FAVORITE_ICON, folderIcon);
512 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
513 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
515 // Close the database handle.
516 bookmarksDatabase.close();
519 // Update the folder name, parent folder, and display order.
520 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder) {
521 // Get a writable database handle.
522 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
524 // Update the folder first. Store the new folder name in `folderContentValues`.
525 ContentValues folderContentValues = new ContentValues();
526 folderContentValues.put(BOOKMARK_NAME, newFolderName);
527 folderContentValues.put(PARENT_FOLDER, parentFolder);
528 folderContentValues.put(DISPLAY_ORDER, displayOrder);
530 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
531 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
533 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
534 ContentValues bookmarkContentValues = new ContentValues();
535 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
537 // SQL escape `oldFolderName`.
538 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
540 // Run the update on all the bookmarks that currently list `oldFolderName` as their parent folder. The last argument is `null` because there are no `whereArgs`.
541 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
543 // Close the database handle.
544 bookmarksDatabase.close();
547 // Update the folder name and icon.
548 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
549 // Get a writable database handle.
550 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
552 // Update the folder first. Store the updated values in `folderContentValues`.
553 ContentValues folderContentValues = new ContentValues();
554 folderContentValues.put(BOOKMARK_NAME, newFolderName);
555 folderContentValues.put(FAVORITE_ICON, folderIcon);
557 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
558 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
560 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
561 ContentValues bookmarkContentValues = new ContentValues();
562 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
564 // SQL escape `oldFolderName`.
565 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
567 // Run the update on all the bookmarks that currently list `oldFolderName` as their parent folder. The last argument is `null` because there are no `whereArgs`.
568 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
570 // Close the database handle.
571 bookmarksDatabase.close();
574 // Update the folder name and icon.
575 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder, byte[] folderIcon) {
576 // Get a writable database handle.
577 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
579 // Update the folder first. Store the updated values in `folderContentValues`.
580 ContentValues folderContentValues = new ContentValues();
581 folderContentValues.put(BOOKMARK_NAME, newFolderName);
582 folderContentValues.put(PARENT_FOLDER, parentFolder);
583 folderContentValues.put(DISPLAY_ORDER, displayOrder);
584 folderContentValues.put(FAVORITE_ICON, folderIcon);
586 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
587 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
589 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
590 ContentValues bookmarkContentValues = new ContentValues();
591 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
593 // SQL escape `oldFolderName`.
594 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
596 // Run the update on all the bookmarks that currently list `oldFolderName` as their parent folder. The last argument is `null` because there are no `whereArgs`.
597 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
599 // Close the database handle.
600 bookmarksDatabase.close();
603 // Update the display order for one bookmark or folder.
604 public void updateDisplayOrder(int databaseId, int displayOrder) {
605 // Get a writable database handle.
606 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
608 // Store the new display order in `bookmarkContentValues`.
609 ContentValues bookmarkContentValues = new ContentValues();
610 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
612 // Update the database. The last argument is `null` because there are no `whereArgs`.
613 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
615 // Close the database handle.
616 bookmarksDatabase.close();
619 // Move one bookmark or folder to a new folder.
620 public void moveToFolder(int databaseId, String newFolder) {
621 // Get a writable database handle.
622 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
624 // SQL escape the new folder name.
625 String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
627 // Prepare a SQL query to select all the bookmarks in the new folder.
628 String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
629 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
630 " ORDER BY " + DISPLAY_ORDER + " ASC";
632 // Get a cursor for all the bookmarks in the new folder. The second argument is `null` because there are no `selectionArgs`.
633 Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
635 // Instantiate a variable to store the display order after the move.
638 // Set the new display order.
639 if (newFolderCursor.getCount() > 0) { // There are already bookmarks in the folder.
640 // Move to the last bookmark.
641 newFolderCursor.moveToLast();
643 // Set the display order to be one greater that the last bookmark.
644 displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
645 } else { // There are no bookmarks in the new folder.
646 // Set the display order to be `0`.
650 // Close the new folder `Cursor`.
651 newFolderCursor.close();
653 // Store the new values in `bookmarkContentValues`.
654 ContentValues bookmarkContentValues = new ContentValues();
655 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
656 bookmarkContentValues.put(PARENT_FOLDER, newFolder);
658 // Update the database. The last argument is `null` because there are no `whereArgs`.
659 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
661 // Close the database handle.
662 bookmarksDatabase.close();
665 // Delete one bookmark.
666 public void deleteBookmark(int databaseId) {
667 // Get a writable database handle.
668 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
670 // Deletes the row with the given `databaseId`. The last argument is `null` because we don't need additional parameters.
671 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
673 // Close the database handle.
674 bookmarksDatabase.close();