2 * Copyright © 2016-2018 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 `databaseId`
129 final 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 final String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
143 " WHERE " + _ID + " = " + databaseId;
145 // Get `folderCursor`. The second argument is `null` because there are no `selectionArgs`.
146 Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
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 final 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 final 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 final 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 final 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 final String GET_PARENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
250 " WHERE " + IS_FOLDER + " = " + 1 +
251 " AND " + BOOKMARK_NAME + " = " + currentFolder;
253 // The second argument is `null` because there are no `selectionArgs`.
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 final 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 in the bookmarks table.
287 final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE;
289 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs. 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 `folderName`.
299 folderName = DatabaseUtils.sqlEscapeString(folderName);
301 // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
302 final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
303 " WHERE " + PARENT_FOLDER + " = " + folderName;
305 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. 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 in the specified folder ordered by display order.
310 public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
311 // Get a readable database handle.
312 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
314 // SQL escape `folderName`.
315 folderName = DatabaseUtils.sqlEscapeString(folderName);
317 // Get everything in the `BOOKMARKS_TABLE` with `folderName` as the `PARENT_FOLDER`.
318 final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
319 " WHERE " + PARENT_FOLDER + " = " + folderName +
320 " ORDER BY " + DISPLAY_ORDER + " ASC";
322 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. The Cursor cannot be closed because it is used in the parent activity.
323 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
326 // Get a `Cursor` for all bookmarks and folders in the specified folder except for a specific list of IDs.
327 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
328 // Get a readable database handle.
329 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
331 // Prepare a string builder that contains the comma-separated list of IDs not to get.
332 StringBuilder doNotGetIdsStringBuilder = new StringBuilder();
334 // Extract the array to `doNotGetIdsString`.
335 for (long databaseIdLong : exceptIdLongArray) {
336 // If this is the first number, only add the number.
337 if (doNotGetIdsStringBuilder.toString().isEmpty()) {
338 doNotGetIdsStringBuilder.append(databaseIdLong);
339 } else { // If there already is a number in the string, place a `,` before the new number.
340 doNotGetIdsStringBuilder.append(",");
341 doNotGetIdsStringBuilder.append(databaseIdLong);
345 // SQL escape `folderName`.
346 folderName = DatabaseUtils.sqlEscapeString(folderName);
348 // Prepare the SQL statement to select all items except those with the specified IDs.
349 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
350 " WHERE " + PARENT_FOLDER + " = " + folderName +
351 " AND " + _ID + " NOT IN (" + doNotGetIdsStringBuilder.toString() +
352 ") ORDER BY " + DISPLAY_ORDER + " ASC";
354 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
355 // We can't close the `Cursor` because we need to use it in the parent activity.
356 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
359 // Check if a database ID is a folder.
360 public boolean isFolder(int databaseId) {
361 // Get a readable database handle.
362 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
364 // Prepare the SQL statement to determine if `databaseId` is a folder.
365 final String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
366 " WHERE " + _ID + " = " + databaseId;
368 // Populate folderCursor. The second argument is `null` because there are no `selectionArgs`.
369 Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
371 // Ascertain if this database ID is a folder.
372 folderCursor.moveToFirst();
373 boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
375 // Close the `Cursor` and the database handle.
376 folderCursor.close();
377 bookmarksDatabase.close();
382 // Update the bookmark name and URL.
383 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
384 // Initialize a `ContentValues`.
385 ContentValues bookmarkContentValues = new ContentValues();
387 // Store the updated values.
388 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
389 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
391 // Get a writable database handle.
392 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
394 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
395 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
397 // Close the database handle.
398 bookmarksDatabase.close();
401 // Update the bookmark name, URL, parent folder, and display order.
402 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder) {
403 // Initialize a `ContentValues`.
404 ContentValues bookmarkContentValues = new ContentValues();
406 // Store the updated values.
407 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
408 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
409 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
410 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
412 // Get a writable database handle.
413 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
415 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
416 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
418 // Close the database handle.
419 bookmarksDatabase.close();
422 // Update the bookmark name, URL, and favorite icon.
423 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
424 // Initialize a `ContentValues`.
425 ContentValues bookmarkContentValues = new ContentValues();
427 // Store the updated values.
428 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
429 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
430 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
432 // Get a writable database handle.
433 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
435 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
436 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
438 // Close the database handle.
439 bookmarksDatabase.close();
442 // Update the bookmark name, URL, parent folder, display order, and favorite icon.
443 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder, byte[] favoriteIcon) {
444 // Initialize a `ContentValues`.
445 ContentValues bookmarkContentValues = new ContentValues();
447 // Store the updated values.
448 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
449 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
450 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
451 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
452 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
454 // Get a writable database handle.
455 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
457 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
458 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
460 // Close the database handle.
461 bookmarksDatabase.close();
464 // Update the folder name.
465 public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
466 // Get a writable database handle.
467 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
469 // Update the folder first. Store the new folder name in `folderContentValues`.
470 ContentValues folderContentValues = new ContentValues();
471 folderContentValues.put(BOOKMARK_NAME, newFolderName);
473 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
474 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
476 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
477 ContentValues bookmarkContentValues = new ContentValues();
478 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
480 // SQL escape `oldFolderName`.
481 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
483 // 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`.
484 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
486 // Close the database handle.
487 bookmarksDatabase.close();
490 // Update the folder icon.
491 public void updateFolder(int databaseId, byte[] folderIcon) {
492 // Get a writable database handle.
493 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
495 // Store the updated icon in `folderContentValues`.
496 ContentValues folderContentValues = new ContentValues();
497 folderContentValues.put(FAVORITE_ICON, folderIcon);
499 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
500 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
502 // Close the database handle.
503 bookmarksDatabase.close();
506 // Update the folder name, parent folder, and display order.
507 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder) {
508 // Get a writable database handle.
509 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
511 // Update the folder first. Store the new folder name in `folderContentValues`.
512 ContentValues folderContentValues = new ContentValues();
513 folderContentValues.put(BOOKMARK_NAME, newFolderName);
514 folderContentValues.put(PARENT_FOLDER, parentFolder);
515 folderContentValues.put(DISPLAY_ORDER, displayOrder);
517 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
518 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
520 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
521 ContentValues bookmarkContentValues = new ContentValues();
522 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
524 // SQL escape `oldFolderName`.
525 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
527 // 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`.
528 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
530 // Close the database handle.
531 bookmarksDatabase.close();
534 // Update the folder name and icon.
535 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
536 // Get a writable database handle.
537 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
539 // Update the folder first. Store the updated values in `folderContentValues`.
540 ContentValues folderContentValues = new ContentValues();
541 folderContentValues.put(BOOKMARK_NAME, newFolderName);
542 folderContentValues.put(FAVORITE_ICON, folderIcon);
544 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
545 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
547 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
548 ContentValues bookmarkContentValues = new ContentValues();
549 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
551 // SQL escape `oldFolderName`.
552 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
554 // 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`.
555 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
557 // Close the database handle.
558 bookmarksDatabase.close();
561 // Update the folder name and icon.
562 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder, byte[] folderIcon) {
563 // Get a writable database handle.
564 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
566 // Update the folder first. Store the updated values in `folderContentValues`.
567 ContentValues folderContentValues = new ContentValues();
568 folderContentValues.put(BOOKMARK_NAME, newFolderName);
569 folderContentValues.put(PARENT_FOLDER, parentFolder);
570 folderContentValues.put(DISPLAY_ORDER, displayOrder);
571 folderContentValues.put(FAVORITE_ICON, folderIcon);
573 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
574 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
576 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
577 ContentValues bookmarkContentValues = new ContentValues();
578 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
580 // SQL escape `oldFolderName`.
581 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
583 // 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`.
584 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
586 // Close the database handle.
587 bookmarksDatabase.close();
590 // Update the display order for one bookmark or folder.
591 public void updateDisplayOrder(int databaseId, int displayOrder) {
592 // Get a writable database handle.
593 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
595 // Store the new display order in `bookmarkContentValues`.
596 ContentValues bookmarkContentValues = new ContentValues();
597 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
599 // Update the database. The last argument is `null` because there are no `whereArgs`.
600 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
602 // Close the database handle.
603 bookmarksDatabase.close();
606 // Move one bookmark or folder to a new folder.
607 public void moveToFolder(int databaseId, String newFolder) {
608 // Get a writable database handle.
609 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
611 // SQL escape the new folder name.
612 String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
614 // Prepare a SQL query to select all the bookmarks in the new folder.
615 final String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
616 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
617 " ORDER BY " + DISPLAY_ORDER + " ASC";
619 // Get a cursor for all the bookmarks in the new folder. The second argument is `null` because there are no `selectionArgs`.
620 Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
622 // Instantiate a variable to store the display order after the move.
625 // Set the new display order.
626 if (newFolderCursor.getCount() > 0) { // There are already bookmarks in the folder.
627 // Move to the last bookmark.
628 newFolderCursor.moveToLast();
630 // Set the display order to be one greater that the last bookmark.
631 displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
632 } else { // There are no bookmarks in the new folder.
633 // Set the display order to be `0`.
637 // Close the new folder `Cursor`.
638 newFolderCursor.close();
640 // Store the new values in `bookmarkContentValues`.
641 ContentValues bookmarkContentValues = new ContentValues();
642 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
643 bookmarkContentValues.put(PARENT_FOLDER, newFolder);
645 // Update the database. The last argument is `null` because there are no `whereArgs`.
646 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
648 // Close the database handle.
649 bookmarksDatabase.close();
652 // Delete one bookmark.
653 public void deleteBookmark(int databaseId) {
654 // Get a writable database handle.
655 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
657 // Deletes the row with the given `databaseId`. The last argument is `null` because we don't need additional parameters.
658 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
660 // Close the database handle.
661 bookmarksDatabase.close();