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 getBookmark(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 // Get 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 getFolder(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 getFoldersExcept(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 getSubfolders(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 the name of the parent folder.
241 public String getParentFolderName(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 current folder.
249 String GET_CURRENT_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_CURRENT_FOLDER, null);
255 bookmarkCursor.moveToFirst();
257 // Store the name of the parent folder.
258 String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
261 bookmarkCursor.close();
266 // Get the name of the parent folder.
267 public String getParentFolderName(int databaseId) {
268 // Get a readable database handle.
269 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
271 // Prepare the SQL statement to get the current bookmark.
272 String GET_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE +
273 " WHERE " + _ID + " = " + databaseId;
275 // Get the bookmark cursor and move to the first entry.
276 Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK, null);
277 bookmarkCursor.moveToFirst();
279 // Store the name of the parent folder.
280 String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
283 bookmarkCursor.close();
288 // Get a cursor of all the folders.
289 public Cursor getAllFolders() {
290 // Get a readable database handle.
291 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
293 // Prepare the SQL statement to get the `Cursor` for all the folders.
294 String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE +
295 " WHERE " + IS_FOLDER + " = " + 1 +
296 " ORDER BY " + BOOKMARK_NAME + " ASC";
298 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
299 // We can't close the `Cursor` because we need to use it in the parent activity.
300 return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null);
303 // Get a cursor for all bookmarks and folders.
304 public Cursor getAllBookmarks() {
305 // Get a readable database handle.
306 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
308 // Get everything in the bookmarks table.
309 String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE;
311 // Return the result as a Cursor. The Cursor cannot be closed because it is used in the parent activity.
312 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
315 // Get a cursor for all bookmarks and folders ordered by display order.
316 public Cursor getAllBookmarksByDisplayOrder() {
317 // Get a readable database handle.
318 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
320 // Get everything in the bookmarks table ordered by display order.
321 String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
322 " ORDER BY " + DISPLAY_ORDER + " ASC";
324 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
325 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
328 // Get a cursor for all bookmarks and folders except those with the specified IDs.
329 public Cursor getAllBookmarksExcept(long[] exceptIdLongArray) {
330 // Get a readable database handle.
331 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
333 // Prepare a string builder to contain the comma-separated list of IDs not to get.
334 StringBuilder idsNotToGetStringBuilder = new StringBuilder();
336 // Extract the array of IDs not to get to the string builder.
337 for (long databaseIdLong : exceptIdLongArray) {
338 if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number.
339 idsNotToGetStringBuilder.append(databaseIdLong);
340 } else { // This is not the first number, so place a `,` before the new number.
341 idsNotToGetStringBuilder.append(",");
342 idsNotToGetStringBuilder.append(databaseIdLong);
346 // Prepare the SQL statement to select all items except those with the specified IDs.
347 String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
348 " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")";
350 // Return the results as a cursor. The cursor cannot be closed because it will be used in the parent activity.
351 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null);
354 // Get a cursor for all bookmarks and folders by display order except for a specific of IDs.
355 public Cursor getAllBookmarksByDisplayOrderExcept(long[] exceptIdLongArray) {
356 // Get a readable database handle.
357 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
359 // Prepare a string builder to contain the comma-separated list of IDs not to get.
360 StringBuilder idsNotToGetStringBuilder = new StringBuilder();
362 // Extract the array of IDs not to get to the string builder.
363 for (long databaseIdLong : exceptIdLongArray) {
364 if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number.
365 idsNotToGetStringBuilder.append(databaseIdLong);
366 } else { // This is not the first number, so place a `,` before the new number.
367 idsNotToGetStringBuilder.append(",");
368 idsNotToGetStringBuilder.append(databaseIdLong);
372 // Prepare the SQL statement to select all items except those with the specified IDs.
373 String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
374 " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() +
375 ") ORDER BY " + DISPLAY_ORDER + " ASC";
377 // Return the results as a cursor. The cursor cannot be closed because it will be used in the parent activity.
378 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null);
381 // Get a cursor for bookmarks and folders in the specified folder.
382 public Cursor getBookmarks(String folderName) {
383 // Get a readable database handle.
384 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
386 // SQL escape the folder name.
387 folderName = DatabaseUtils.sqlEscapeString(folderName);
389 // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
390 String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
391 " WHERE " + PARENT_FOLDER + " = " + folderName;
393 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
394 return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
397 // Get a cursor for bookmarks and folders in the specified folder ordered by display order.
398 public Cursor getBookmarksByDisplayOrder(String folderName) {
399 // Get a readable database handle.
400 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
402 // SQL escape `folderName`.
403 folderName = DatabaseUtils.sqlEscapeString(folderName);
405 // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
406 String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
407 " WHERE " + PARENT_FOLDER + " = " + folderName +
408 " ORDER BY " + DISPLAY_ORDER + " ASC";
410 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
411 return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
414 // Get a cursor for bookmarks and folders in the specified folder except for ta specific list of IDs.
415 public Cursor getBookmarksExcept(long[] exceptIdLongArray, String folderName) {
416 // Get a readable database handle.
417 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
419 // Prepare a string builder to contain the comma-separated list of IDs not to get.
420 StringBuilder idsNotToGetStringBuilder = new StringBuilder();
422 // Extract the array of IDs not to get to the string builder.
423 for (long databaseIdLong : exceptIdLongArray) {
424 if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number.
425 idsNotToGetStringBuilder.append(databaseIdLong);
426 } else { // This is not the first number, so place a `,` before the new number.
427 idsNotToGetStringBuilder.append(",");
428 idsNotToGetStringBuilder.append(databaseIdLong);
432 // SQL escape the folder name.
433 folderName = DatabaseUtils.sqlEscapeString(folderName);
435 // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER` except those with the specified IDs.
436 String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
437 " WHERE " + PARENT_FOLDER + " = " + folderName +
438 " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")";
440 // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity.
441 return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null);
444 // Get a cursor for bookmarks and folders in the specified folder by display order except for a specific list of IDs.
445 public Cursor getBookmarksByDisplayOrderExcept(long[] exceptIdLongArray, String folderName) {
446 // Get a readable database handle.
447 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
449 // Prepare a string builder to contain the comma-separated list of IDs not to get.
450 StringBuilder idsNotToGetStringBuilder = new StringBuilder();
452 // Extract the array of IDs not to get to the string builder.
453 for (long databaseIdLong : exceptIdLongArray) {
454 if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number.
455 idsNotToGetStringBuilder.append(databaseIdLong);
456 } else { // This is not the first number, so place a `,` before the new number.
457 idsNotToGetStringBuilder.append(",");
458 idsNotToGetStringBuilder.append(databaseIdLong);
462 // SQL escape `folderName`.
463 folderName = DatabaseUtils.sqlEscapeString(folderName);
465 // Prepare the SQL statement to select all items except those with the specified IDs.
466 String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
467 " WHERE " + PARENT_FOLDER + " = " + folderName +
468 " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() +
469 ") ORDER BY " + DISPLAY_ORDER + " ASC";
471 // Return the results as a cursor. The cursor cannot be closed because it will be used in the parent activity.
472 return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null);
475 // Check if a database ID is a folder.
476 public boolean isFolder(int databaseId) {
477 // Get a readable database handle.
478 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
480 // Prepare the SQL statement to determine if `databaseId` is a folder.
481 String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
482 " WHERE " + _ID + " = " + databaseId;
484 // Populate the folder cursor.
485 Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
487 // Ascertain if this database ID is a folder.
488 folderCursor.moveToFirst();
489 boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
491 // Close the cursor and the database handle.
492 folderCursor.close();
493 bookmarksDatabase.close();
498 // Update the bookmark name and URL.
499 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
500 // Initialize a ContentValues.
501 ContentValues bookmarkContentValues = new ContentValues();
503 // Store the updated values.
504 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
505 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
507 // Get a writable database handle.
508 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
510 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
511 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
513 // Close the database handle.
514 bookmarksDatabase.close();
517 // Update the bookmark name, URL, parent folder, and display order.
518 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder) {
519 // Initialize a `ContentValues`.
520 ContentValues bookmarkContentValues = new ContentValues();
522 // Store the updated values.
523 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
524 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
525 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
526 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
528 // Get a writable database handle.
529 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
531 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
532 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
534 // Close the database handle.
535 bookmarksDatabase.close();
538 // Update the bookmark name, URL, and favorite icon.
539 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
540 // Initialize a `ContentValues`.
541 ContentValues bookmarkContentValues = new ContentValues();
543 // Store the updated values.
544 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
545 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
546 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
548 // Get a writable database handle.
549 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
551 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
552 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
554 // Close the database handle.
555 bookmarksDatabase.close();
558 // Update the bookmark name, URL, parent folder, display order, and favorite icon.
559 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder, byte[] favoriteIcon) {
560 // Initialize a `ContentValues`.
561 ContentValues bookmarkContentValues = new ContentValues();
563 // Store the updated values.
564 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
565 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
566 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
567 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
568 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
570 // Get a writable database handle.
571 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
573 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
574 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
576 // Close the database handle.
577 bookmarksDatabase.close();
580 // Update the folder name.
581 public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
582 // Get a writable database handle.
583 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
585 // Update the folder first. Store the new folder name in `folderContentValues`.
586 ContentValues folderContentValues = new ContentValues();
587 folderContentValues.put(BOOKMARK_NAME, newFolderName);
589 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
590 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
592 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
593 ContentValues bookmarkContentValues = new ContentValues();
594 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
596 // SQL escape `oldFolderName`.
597 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
599 // 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`.
600 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
602 // Close the database handle.
603 bookmarksDatabase.close();
606 // Update the folder icon.
607 public void updateFolder(int databaseId, byte[] folderIcon) {
608 // Get a writable database handle.
609 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
611 // Store the updated icon in `folderContentValues`.
612 ContentValues folderContentValues = new ContentValues();
613 folderContentValues.put(FAVORITE_ICON, folderIcon);
615 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
616 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
618 // Close the database handle.
619 bookmarksDatabase.close();
622 // Update the folder name, parent folder, and display order.
623 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder) {
624 // Get a writable database handle.
625 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
627 // Update the folder first. Store the new folder name in `folderContentValues`.
628 ContentValues folderContentValues = new ContentValues();
629 folderContentValues.put(BOOKMARK_NAME, newFolderName);
630 folderContentValues.put(PARENT_FOLDER, parentFolder);
631 folderContentValues.put(DISPLAY_ORDER, displayOrder);
633 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
634 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
636 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
637 ContentValues bookmarkContentValues = new ContentValues();
638 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
640 // SQL escape `oldFolderName`.
641 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
643 // 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`.
644 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
646 // Close the database handle.
647 bookmarksDatabase.close();
650 // Update the folder name and icon.
651 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
652 // Get a writable database handle.
653 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
655 // Update the folder first. Store the updated values in `folderContentValues`.
656 ContentValues folderContentValues = new ContentValues();
657 folderContentValues.put(BOOKMARK_NAME, newFolderName);
658 folderContentValues.put(FAVORITE_ICON, folderIcon);
660 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
661 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
663 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
664 ContentValues bookmarkContentValues = new ContentValues();
665 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
667 // SQL escape `oldFolderName`.
668 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
670 // 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`.
671 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
673 // Close the database handle.
674 bookmarksDatabase.close();
677 // Update the folder name and icon.
678 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder, byte[] folderIcon) {
679 // Get a writable database handle.
680 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
682 // Update the folder first. Store the updated values in `folderContentValues`.
683 ContentValues folderContentValues = new ContentValues();
684 folderContentValues.put(BOOKMARK_NAME, newFolderName);
685 folderContentValues.put(PARENT_FOLDER, parentFolder);
686 folderContentValues.put(DISPLAY_ORDER, displayOrder);
687 folderContentValues.put(FAVORITE_ICON, folderIcon);
689 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
690 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
692 // Update the bookmarks inside the folder. Store the new parent folder name in `bookmarkContentValues`.
693 ContentValues bookmarkContentValues = new ContentValues();
694 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
696 // SQL escape `oldFolderName`.
697 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
699 // 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`.
700 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
702 // Close the database handle.
703 bookmarksDatabase.close();
706 // Update the display order for one bookmark or folder.
707 public void updateDisplayOrder(int databaseId, int displayOrder) {
708 // Get a writable database handle.
709 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
711 // Store the new display order in `bookmarkContentValues`.
712 ContentValues bookmarkContentValues = new ContentValues();
713 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
715 // Update the database. The last argument is `null` because there are no `whereArgs`.
716 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
718 // Close the database handle.
719 bookmarksDatabase.close();
722 // Move one bookmark or folder to a new folder.
723 public void moveToFolder(int databaseId, String newFolder) {
724 // Get a writable database handle.
725 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
727 // SQL escape the new folder name.
728 String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
730 // Prepare a SQL query to select all the bookmarks in the new folder.
731 String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
732 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
733 " ORDER BY " + DISPLAY_ORDER + " ASC";
735 // Get a cursor for all the bookmarks in the new folder. The second argument is `null` because there are no `selectionArgs`.
736 Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
738 // Instantiate a variable to store the display order after the move.
741 // Set the new display order.
742 if (newFolderCursor.getCount() > 0) { // There are already bookmarks in the folder.
743 // Move to the last bookmark.
744 newFolderCursor.moveToLast();
746 // Set the display order to be one greater that the last bookmark.
747 displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
748 } else { // There are no bookmarks in the new folder.
749 // Set the display order to be `0`.
753 // Close the new folder `Cursor`.
754 newFolderCursor.close();
756 // Store the new values in `bookmarkContentValues`.
757 ContentValues bookmarkContentValues = new ContentValues();
758 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
759 bookmarkContentValues.put(PARENT_FOLDER, newFolder);
761 // Update the database. The last argument is `null` because there are no `whereArgs`.
762 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
764 // Close the database handle.
765 bookmarksDatabase.close();
768 // Delete one bookmark.
769 public void deleteBookmark(int databaseId) {
770 // Get a writable database handle.
771 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
773 // Deletes the row with the given `databaseId`. The last argument is `null` because we don't need additional parameters.
774 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
776 // Close the database handle.
777 bookmarksDatabase.close();