]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java
Update the URL in the copyright header. https://redmine.stoutner.com/issues/796
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / BookmarksDatabaseHelper.java
1 /*
2  * Copyright © 2016-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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.
10  *
11  * Privacy Browser Android 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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers;
21
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;
28
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";
33
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";
41
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)";
50
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);
54     }
55
56     @Override
57     public void onCreate(SQLiteDatabase bookmarksDatabase) {
58         // Create the bookmarks table.
59         bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
60     }
61
62     @Override
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.
65     }
66
67     // Create a bookmark.
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();
71
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);
79
80         // Get a writable database handle.
81         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
82
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);
85
86         // Close the database handle.
87         bookmarksDatabase.close();
88     }
89
90     // Create a bookmark from content values.
91     void createBookmark(ContentValues contentValues) {
92         // Get a writable database.
93         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
94
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);
97
98         // Close the database handle.
99         bookmarksDatabase.close();
100     }
101
102     // Create a folder.
103     public void createFolder(String folderName, String parentFolder, byte[] favoriteIcon) {
104         ContentValues bookmarkContentValues = new ContentValues();
105
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);
112
113         // Get a writable database handle.
114         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
115
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);
118
119         // Close the database handle.
120         bookmarksDatabase.close();
121     }
122
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();
127
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;
131
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);
134     }
135
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();
140
141         // Prepare the SQL statement to get the cursor for the folder.
142         String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
143                 " WHERE " + _ID + " = " + databaseId;
144
145         // Get a folder cursor.
146         Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
147
148         // Get the folder name.
149         folderCursor.moveToFirst();
150         String folderName = folderCursor.getString(folderCursor.getColumnIndexOrThrow(BOOKMARK_NAME));
151
152         // Close the cursor and the database handle.
153         folderCursor.close();
154         bookmarksDatabase.close();
155
156         // Return the folder name.
157         return folderName;
158     }
159
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();
164
165         // SQL escape `folderName`.
166         folderName = DatabaseUtils.sqlEscapeString(folderName);
167
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;
172
173         // Get `folderCursor`.  The second argument is `null` because there are no `selectionArgs`.
174         Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
175
176         // Get the database ID.
177         folderCursor.moveToFirst();
178         int databaseId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(_ID));
179
180         // Close the cursor and the database handle.
181         folderCursor.close();
182         bookmarksDatabase.close();
183
184         // Return the database ID.
185         return databaseId;
186     }
187
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();
192
193         // SQL escape `folderName`.
194         folderName = DatabaseUtils.sqlEscapeString(folderName);
195
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;
200
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);
204     }
205
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();
210
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";
216
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);
220     }
221
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();
226
227         // SQL escape `currentFolder.
228         currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
229
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;
234
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);
238     }
239
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();
244
245         // SQL escape `currentFolder`.
246         currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
247
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;
252
253         // Get the bookmark cursor and move to the first entry.
254         Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_CURRENT_FOLDER, null);
255         bookmarkCursor.moveToFirst();
256
257         // Store the name of the parent folder.
258         String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER));
259
260         // Close the cursor.
261         bookmarkCursor.close();
262
263         return parentFolder;
264     }
265
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();
270
271         // Prepare the SQL statement to get the current bookmark.
272         String GET_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE +
273                 " WHERE " + _ID + " = " + databaseId;
274
275         // Get the bookmark cursor and move to the first entry.
276         Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK, null);
277         bookmarkCursor.moveToFirst();
278
279         // Store the name of the parent folder.
280         String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER));
281
282         // Close the cursor.
283         bookmarkCursor.close();
284
285         return parentFolder;
286     }
287
288     // Get a cursor of all the folders.
289     public Cursor getAllFolders() {
290         // Get a readable database handle.
291         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
292
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";
297
298         // Return the results as a cursor.  The cursor cannot be closed because it is used in the parent activity.
299         return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null);
300     }
301
302     // Get a cursor for all bookmarks and folders.
303     public Cursor getAllBookmarks() {
304         // Get a readable database handle.
305         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
306
307         // Get everything in the bookmarks table.
308         String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE;
309
310         // Return the result as a Cursor.  The Cursor cannot be closed because it is used in the parent activity.
311         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
312     }
313
314     // Get a cursor for all bookmarks and folders ordered by display order.
315     public Cursor getAllBookmarksByDisplayOrder() {
316         // Get a readable database handle.
317         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
318
319         // Get everything in the bookmarks table ordered by display order.
320         String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
321                 " ORDER BY " + DISPLAY_ORDER + " ASC";
322
323         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
324         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
325     }
326
327     // Get a cursor for all bookmarks and folders except those with the specified IDs.
328     public Cursor getAllBookmarksExcept(long[] exceptIdLongArray) {
329         // Get a readable database handle.
330         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
331
332         // Prepare a string builder to contain the comma-separated list of IDs not to get.
333         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
334
335         // Extract the array of IDs not to get to the string builder.
336         for (long databaseIdLong : exceptIdLongArray) {
337             // Check to see if there is already a number in the builder.
338             if (idsNotToGetStringBuilder.length() > 0) {
339                 // This is not the first number, so place a `,` before the new number.
340                 idsNotToGetStringBuilder.append(",");
341             }
342
343             // Add the new number to the builder.
344             idsNotToGetStringBuilder.append(databaseIdLong);
345         }
346
347         // Prepare the SQL statement to select all items except those with the specified IDs.
348         String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
349                 " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")";
350
351         // Return the results as a cursor.  The cursor cannot be closed because it will be used in the parent activity.
352         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null);
353     }
354
355     // Get a cursor for all bookmarks and folders by display order except for a specific of IDs.
356     public Cursor getAllBookmarksByDisplayOrderExcept(long[] exceptIdLongArray) {
357         // Get a readable database handle.
358         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
359
360         // Prepare a string builder to contain the comma-separated list of IDs not to get.
361         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
362
363         // Extract the array of IDs not to get to the string builder.
364         for (long databaseIdLong : exceptIdLongArray) {
365             // Check to see if there is already a number in the builder.
366             if (idsNotToGetStringBuilder.length() > 0) {
367                 // This is not the first number, so place a `,` before the new number.
368                 idsNotToGetStringBuilder.append(",");
369             }
370
371             // Add the new number to the builder.
372             idsNotToGetStringBuilder.append(databaseIdLong);
373         }
374
375         // Prepare the SQL statement to select all items except those with the specified IDs.
376         String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
377                 " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() +
378                 ") ORDER BY " + DISPLAY_ORDER + " ASC";
379
380         // Return the results as a cursor.  The cursor cannot be closed because it will be used in the parent activity.
381         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null);
382     }
383
384     // Get a cursor for bookmarks and folders in the specified folder.
385     public Cursor getBookmarks(String folderName) {
386         // Get a readable database handle.
387         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
388
389         // SQL escape the folder name.
390         folderName = DatabaseUtils.sqlEscapeString(folderName);
391
392         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
393         String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
394                 " WHERE " + PARENT_FOLDER + " = " + folderName;
395
396         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
397         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
398     }
399
400     // Get a cursor for bookmarks and folders in the specified folder ordered by display order.
401     public Cursor getBookmarksByDisplayOrder(String folderName) {
402         // Get a readable database handle.
403         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
404
405         // SQL escape `folderName`.
406         folderName = DatabaseUtils.sqlEscapeString(folderName);
407
408         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
409         String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
410                 " WHERE " + PARENT_FOLDER + " = " + folderName +
411                 " ORDER BY " + DISPLAY_ORDER + " ASC";
412
413         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
414         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
415     }
416
417     // Get a cursor with just database ID of bookmarks and folders in the specified folder.  This is useful for deleting folders with bookmarks that have favorite icons too large to fit in a cursor.
418     public Cursor getBookmarkIds(String folderName) {
419         // Get a readable database handle.
420         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
421
422         // SQL escape the folder name.
423         folderName = DatabaseUtils.sqlEscapeString(folderName);
424
425         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
426         String GET_BOOKMARKS = "SELECT " + _ID + " FROM " + BOOKMARKS_TABLE +
427                 " WHERE " + PARENT_FOLDER + " = " + folderName;
428
429         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
430         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
431     }
432
433     // Get a cursor for bookmarks and folders in the specified folder except for ta specific list of IDs.
434     public Cursor getBookmarksExcept(long[] exceptIdLongArray, String folderName) {
435         // Get a readable database handle.
436         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
437
438         // Prepare a string builder to contain the comma-separated list of IDs not to get.
439         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
440
441         // Extract the array of IDs not to get to the string builder.
442         for (long databaseIdLong : exceptIdLongArray) {
443             // Check to see if there is already a number in the builder.
444             if (idsNotToGetStringBuilder.length() > 0) {
445                 // This is not the first number, so place a `,` before the new number.
446                 idsNotToGetStringBuilder.append(",");
447             }
448
449             // Add the new number to the builder.
450             idsNotToGetStringBuilder.append(databaseIdLong);
451         }
452
453         // SQL escape the folder name.
454         folderName = DatabaseUtils.sqlEscapeString(folderName);
455
456         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER` except those with the specified IDs.
457         String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
458                 " WHERE " + PARENT_FOLDER + " = " + folderName +
459                 " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")";
460
461         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
462         return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null);
463     }
464
465     // Get a cursor for bookmarks and folders in the specified folder by display order except for a specific list of IDs.
466     public Cursor getBookmarksByDisplayOrderExcept(long[] exceptIdLongArray, String folderName) {
467         // Get a readable database handle.
468         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
469
470         // Prepare a string builder to contain the comma-separated list of IDs not to get.
471         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
472
473         // Extract the array of IDs not to get to the string builder.
474         for (long databaseIdLong : exceptIdLongArray) {
475             // Check to see if there is already a number in the builder.
476             if (idsNotToGetStringBuilder.length() > 0) {
477                 // This is not the first number, so place a `,` before the new number.
478                 idsNotToGetStringBuilder.append(",");
479             }
480
481             // Add the new number to the builder.
482             idsNotToGetStringBuilder.append(databaseIdLong);
483         }
484
485         // SQL escape `folderName`.
486         folderName = DatabaseUtils.sqlEscapeString(folderName);
487
488         // Prepare the SQL statement to select all items except those with the specified IDs.
489         String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
490                 " WHERE " + PARENT_FOLDER + " = " + folderName +
491                 " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() +
492                 ") ORDER BY " + DISPLAY_ORDER + " ASC";
493
494         // Return the results as a cursor.  The cursor cannot be closed because it will be used in the parent activity.
495         return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null);
496     }
497
498     // Check if a database ID is a folder.
499     public boolean isFolder(int databaseId) {
500         // Get a readable database handle.
501         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
502
503         // Prepare the SQL statement to determine if `databaseId` is a folder.
504         String CHECK_IF_FOLDER = "SELECT " + IS_FOLDER + " FROM " + BOOKMARKS_TABLE +
505                 " WHERE " + _ID + " = " + databaseId;
506
507         // Populate the folder cursor.
508         Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
509
510         // Ascertain if this database ID is a folder.
511         folderCursor.moveToFirst();
512         boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndexOrThrow(IS_FOLDER)) == 1);
513
514         // Close the cursor and the database handle.
515         folderCursor.close();
516         bookmarksDatabase.close();
517
518         return isFolder;
519     }
520
521     // Update the bookmark name and URL.
522     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
523         // Initialize a ContentValues.
524         ContentValues bookmarkContentValues = new ContentValues();
525
526         // Store the updated values.
527         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
528         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
529
530         // Get a writable database handle.
531         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
532
533         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
534         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
535
536         // Close the database handle.
537         bookmarksDatabase.close();
538     }
539
540     // Update the bookmark name, URL, parent folder, and display order.
541     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder) {
542         // Initialize a `ContentValues`.
543         ContentValues bookmarkContentValues = new ContentValues();
544
545         // Store the updated values.
546         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
547         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
548         bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
549         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
550
551         // Get a writable database handle.
552         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
553
554         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
555         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
556
557         // Close the database handle.
558         bookmarksDatabase.close();
559     }
560
561     // Update the bookmark name, URL, and favorite icon.
562     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
563         // Initialize a `ContentValues`.
564         ContentValues bookmarkContentValues = new ContentValues();
565
566         // Store the updated values.
567         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
568         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
569         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
570
571         // Get a writable database handle.
572         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
573
574         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
575         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
576
577         // Close the database handle.
578         bookmarksDatabase.close();
579     }
580
581     // Update the bookmark name, URL, parent folder, display order, and favorite icon.
582     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder, byte[] favoriteIcon) {
583         // Initialize a `ContentValues`.
584         ContentValues bookmarkContentValues = new ContentValues();
585
586         // Store the updated values.
587         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
588         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
589         bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
590         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
591         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
592
593         // Get a writable database handle.
594         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
595
596         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
597         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
598
599         // Close the database handle.
600         bookmarksDatabase.close();
601     }
602
603     // Update the folder name.
604     public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
605         // Get a writable database handle.
606         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
607
608         // Update the folder first.  Store the new folder name in `folderContentValues`.
609         ContentValues folderContentValues = new ContentValues();
610         folderContentValues.put(BOOKMARK_NAME, newFolderName);
611
612         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
613         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
614
615         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
616         ContentValues bookmarkContentValues = new ContentValues();
617         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
618
619         // SQL escape `oldFolderName`.
620         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
621
622         // 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`.
623         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
624
625         // Close the database handle.
626         bookmarksDatabase.close();
627     }
628
629     // Update the folder icon.
630     public void updateFolder(int databaseId, byte[] folderIcon) {
631         // Get a writable database handle.
632         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
633
634         // Store the updated icon in `folderContentValues`.
635         ContentValues folderContentValues = new ContentValues();
636         folderContentValues.put(FAVORITE_ICON, folderIcon);
637
638         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
639         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
640
641         // Close the database handle.
642         bookmarksDatabase.close();
643     }
644
645     // Update the folder name, parent folder, and display order.
646     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder) {
647         // Get a writable database handle.
648         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
649
650         // Update the folder first.  Store the new folder name in `folderContentValues`.
651         ContentValues folderContentValues = new ContentValues();
652         folderContentValues.put(BOOKMARK_NAME, newFolderName);
653         folderContentValues.put(PARENT_FOLDER, parentFolder);
654         folderContentValues.put(DISPLAY_ORDER, displayOrder);
655
656         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
657         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
658
659         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
660         ContentValues bookmarkContentValues = new ContentValues();
661         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
662
663         // SQL escape `oldFolderName`.
664         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
665
666         // 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`.
667         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
668
669         // Close the database handle.
670         bookmarksDatabase.close();
671     }
672
673     // Update the folder name and icon.
674     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
675         // Get a writable database handle.
676         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
677
678         // Update the folder first.  Store the updated values in `folderContentValues`.
679         ContentValues folderContentValues = new ContentValues();
680         folderContentValues.put(BOOKMARK_NAME, newFolderName);
681         folderContentValues.put(FAVORITE_ICON, folderIcon);
682
683         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
684         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
685
686         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
687         ContentValues bookmarkContentValues = new ContentValues();
688         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
689
690         // SQL escape `oldFolderName`.
691         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
692
693         // 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`.
694         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
695
696         // Close the database handle.
697         bookmarksDatabase.close();
698     }
699
700     // Update the folder name and icon.
701     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder, byte[] folderIcon) {
702         // Get a writable database handle.
703         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
704
705         // Update the folder first.  Store the updated values in `folderContentValues`.
706         ContentValues folderContentValues = new ContentValues();
707         folderContentValues.put(BOOKMARK_NAME, newFolderName);
708         folderContentValues.put(PARENT_FOLDER, parentFolder);
709         folderContentValues.put(DISPLAY_ORDER, displayOrder);
710         folderContentValues.put(FAVORITE_ICON, folderIcon);
711
712         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
713         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
714
715         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
716         ContentValues bookmarkContentValues = new ContentValues();
717         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
718
719         // SQL escape `oldFolderName`.
720         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
721
722         // 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`.
723         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
724
725         // Close the database handle.
726         bookmarksDatabase.close();
727     }
728
729     // Update the display order for one bookmark or folder.
730     public void updateDisplayOrder(int databaseId, int displayOrder) {
731         // Get a writable database handle.
732         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
733
734         // Store the new display order in `bookmarkContentValues`.
735         ContentValues bookmarkContentValues = new ContentValues();
736         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
737
738         // Update the database.  The last argument is `null` because there are no `whereArgs`.
739         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
740
741         // Close the database handle.
742         bookmarksDatabase.close();
743     }
744
745     // Move one bookmark or folder to a new folder.
746     public void moveToFolder(int databaseId, String newFolder) {
747         // Get a writable database handle.
748         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
749
750         // SQL escape the new folder name.
751         String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
752
753         // Prepare a SQL query to select all the bookmarks in the new folder.
754         String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
755                 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
756                 " ORDER BY " + DISPLAY_ORDER + " ASC";
757
758         // Get a cursor for all the bookmarks in the new folder.  The second argument is `null` because there are no `selectionArgs`.
759         Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
760
761         // Instantiate a variable to store the display order after the move.
762         int displayOrder;
763
764         // Set the new display order.
765         if (newFolderCursor.getCount() > 0) {  // There are already bookmarks in the folder.
766             // Move to the last bookmark.
767             newFolderCursor.moveToLast();
768
769             // Set the display order to be one greater that the last bookmark.
770             displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndexOrThrow(DISPLAY_ORDER)) + 1;
771         } else {  // There are no bookmarks in the new folder.
772             // Set the display order to be `0`.
773             displayOrder = 0;
774         }
775
776         // Close the new folder `Cursor`.
777         newFolderCursor.close();
778
779         // Store the new values in `bookmarkContentValues`.
780         ContentValues bookmarkContentValues = new ContentValues();
781         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
782         bookmarkContentValues.put(PARENT_FOLDER, newFolder);
783
784         // Update the database.  The last argument is `null` because there are no `whereArgs`.
785         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
786
787         // Close the database handle.
788         bookmarksDatabase.close();
789     }
790
791     // Delete one bookmark.
792     public void deleteBookmark(int databaseId) {
793         // Get a writable database handle.
794         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
795
796         // Deletes the row with the given `databaseId`.  The last argument is `null` because we don't need additional parameters.
797         bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
798
799         // Close the database handle.
800         bookmarksDatabase.close();
801     }
802 }