]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java
Fix proxying through Orbot. https://redmine.stoutner.com/issues/473
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / BookmarksDatabaseHelper.java
1 /*
2  * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
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.getColumnIndex(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.getColumnIndex(_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.getColumnIndex(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.getColumnIndex(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             if (idsNotToGetStringBuilder.length() == 0) {  // This is the first number, so only add the number.
338                 idsNotToGetStringBuilder.append(databaseIdLong);
339             } else {  // This is not the first number, so place a `,` before the new number.
340                 idsNotToGetStringBuilder.append(",");
341                 idsNotToGetStringBuilder.append(databaseIdLong);
342             }
343         }
344
345         // Prepare the SQL statement to select all items except those with the specified IDs.
346         String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
347                 " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")";
348
349         // Return the results as a cursor.  The cursor cannot be closed because it will be used in the parent activity.
350         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null);
351     }
352
353     // Get a cursor for all bookmarks and folders by display order except for a specific of IDs.
354     public Cursor getAllBookmarksByDisplayOrderExcept(long[] exceptIdLongArray) {
355         // Get a readable database handle.
356         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
357
358         // Prepare a string builder to contain the comma-separated list of IDs not to get.
359         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
360
361         // Extract the array of IDs not to get to the string builder.
362         for (long databaseIdLong : exceptIdLongArray) {
363             if (idsNotToGetStringBuilder.length() == 0) {  // This is the first number, so only add the number.
364                 idsNotToGetStringBuilder.append(databaseIdLong);
365             } else {  // This is not the first number, so place a `,` before the new number.
366                 idsNotToGetStringBuilder.append(",");
367                 idsNotToGetStringBuilder.append(databaseIdLong);
368             }
369         }
370
371         // Prepare the SQL statement to select all items except those with the specified IDs.
372         String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
373                 " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() +
374                 ") ORDER BY " + DISPLAY_ORDER + " ASC";
375
376         // Return the results as a cursor.  The cursor cannot be closed because it will be used in the parent activity.
377         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null);
378     }
379
380     // Get a cursor for bookmarks and folders in the specified folder.
381     public Cursor getBookmarks(String folderName) {
382         // Get a readable database handle.
383         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
384
385         // SQL escape the folder name.
386         folderName = DatabaseUtils.sqlEscapeString(folderName);
387
388         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
389         String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
390                 " WHERE " + PARENT_FOLDER + " = " + folderName;
391
392         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
393         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
394     }
395
396     // Get a cursor for bookmarks and folders in the specified folder ordered by display order.
397     public Cursor getBookmarksByDisplayOrder(String folderName) {
398         // Get a readable database handle.
399         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
400
401         // SQL escape `folderName`.
402         folderName = DatabaseUtils.sqlEscapeString(folderName);
403
404         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
405         String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
406                 " WHERE " + PARENT_FOLDER + " = " + folderName +
407                 " ORDER BY " + DISPLAY_ORDER + " ASC";
408
409         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
410         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
411     }
412
413     // 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.
414     public Cursor getBookmarkIDs(String folderName) {
415         // Get a readable database handle.
416         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
417
418         // SQL escape the folder name.
419         folderName = DatabaseUtils.sqlEscapeString(folderName);
420
421         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
422         String GET_BOOKMARKS = "SELECT " + _ID + " FROM " + BOOKMARKS_TABLE +
423                 " WHERE " + PARENT_FOLDER + " = " + folderName;
424
425         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
426         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null);
427     }
428
429     // Get a cursor for bookmarks and folders in the specified folder except for ta specific list of IDs.
430     public Cursor getBookmarksExcept(long[] exceptIdLongArray, String folderName) {
431         // Get a readable database handle.
432         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
433
434         // Prepare a string builder to contain the comma-separated list of IDs not to get.
435         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
436
437         // Extract the array of IDs not to get to the string builder.
438         for (long databaseIdLong : exceptIdLongArray) {
439             if (idsNotToGetStringBuilder.length() == 0) {  // This is the first number, so only add the number.
440                 idsNotToGetStringBuilder.append(databaseIdLong);
441             } else {  // This is not the first number, so place a `,` before the new number.
442                 idsNotToGetStringBuilder.append(",");
443                 idsNotToGetStringBuilder.append(databaseIdLong);
444             }
445         }
446
447         // SQL escape the folder name.
448         folderName = DatabaseUtils.sqlEscapeString(folderName);
449
450         // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER` except those with the specified IDs.
451         String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
452                 " WHERE " + PARENT_FOLDER + " = " + folderName +
453                 " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")";
454
455         // Return the result as a cursor.  The cursor cannot be closed because it is used in the parent activity.
456         return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null);
457     }
458
459     // Get a cursor for bookmarks and folders in the specified folder by display order except for a specific list of IDs.
460     public Cursor getBookmarksByDisplayOrderExcept(long[] exceptIdLongArray, String folderName) {
461         // Get a readable database handle.
462         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
463
464         // Prepare a string builder to contain the comma-separated list of IDs not to get.
465         StringBuilder idsNotToGetStringBuilder = new StringBuilder();
466
467         // Extract the array of IDs not to get to the string builder.
468         for (long databaseIdLong : exceptIdLongArray) {
469             if (idsNotToGetStringBuilder.length() == 0) {  // This is the first number, so only add the number.
470                 idsNotToGetStringBuilder.append(databaseIdLong);
471             } else {  // This is not the first number, so place a `,` before the new number.
472                 idsNotToGetStringBuilder.append(",");
473                 idsNotToGetStringBuilder.append(databaseIdLong);
474             }
475         }
476
477         // SQL escape `folderName`.
478         folderName = DatabaseUtils.sqlEscapeString(folderName);
479
480         // Prepare the SQL statement to select all items except those with the specified IDs.
481         String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
482                 " WHERE " + PARENT_FOLDER + " = " + folderName +
483                 " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() +
484                 ") ORDER BY " + DISPLAY_ORDER + " ASC";
485
486         // Return the results as a cursor.  The cursor cannot be closed because it will be used in the parent activity.
487         return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null);
488     }
489
490     // Check if a database ID is a folder.
491     public boolean isFolder(int databaseId) {
492         // Get a readable database handle.
493         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
494
495         // Prepare the SQL statement to determine if `databaseId` is a folder.
496         String CHECK_IF_FOLDER = "SELECT " + IS_FOLDER + " FROM " + BOOKMARKS_TABLE +
497                 " WHERE " + _ID + " = " + databaseId;
498
499         // Populate the folder cursor.
500         Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
501
502         // Ascertain if this database ID is a folder.
503         folderCursor.moveToFirst();
504         boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
505
506         // Close the cursor and the database handle.
507         folderCursor.close();
508         bookmarksDatabase.close();
509
510         return isFolder;
511     }
512
513     // Update the bookmark name and URL.
514     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
515         // Initialize a ContentValues.
516         ContentValues bookmarkContentValues = new ContentValues();
517
518         // Store the updated values.
519         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
520         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
521
522         // Get a writable database handle.
523         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
524
525         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
526         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
527
528         // Close the database handle.
529         bookmarksDatabase.close();
530     }
531
532     // Update the bookmark name, URL, parent folder, and display order.
533     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder) {
534         // Initialize a `ContentValues`.
535         ContentValues bookmarkContentValues = new ContentValues();
536
537         // Store the updated values.
538         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
539         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
540         bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
541         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
542
543         // Get a writable database handle.
544         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
545
546         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
547         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
548
549         // Close the database handle.
550         bookmarksDatabase.close();
551     }
552
553     // Update the bookmark name, URL, and favorite icon.
554     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
555         // Initialize a `ContentValues`.
556         ContentValues bookmarkContentValues = new ContentValues();
557
558         // Store the updated values.
559         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
560         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
561         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
562
563         // Get a writable database handle.
564         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
565
566         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
567         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
568
569         // Close the database handle.
570         bookmarksDatabase.close();
571     }
572
573     // Update the bookmark name, URL, parent folder, display order, and favorite icon.
574     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, String parentFolder, int displayOrder, byte[] favoriteIcon) {
575         // Initialize a `ContentValues`.
576         ContentValues bookmarkContentValues = new ContentValues();
577
578         // Store the updated values.
579         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
580         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
581         bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
582         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
583         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
584
585         // Get a writable database handle.
586         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
587
588         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
589         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
590
591         // Close the database handle.
592         bookmarksDatabase.close();
593     }
594
595     // Update the folder name.
596     public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
597         // Get a writable database handle.
598         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
599
600         // Update the folder first.  Store the new folder name in `folderContentValues`.
601         ContentValues folderContentValues = new ContentValues();
602         folderContentValues.put(BOOKMARK_NAME, newFolderName);
603
604         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
605         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
606
607         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
608         ContentValues bookmarkContentValues = new ContentValues();
609         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
610
611         // SQL escape `oldFolderName`.
612         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
613
614         // 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`.
615         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
616
617         // Close the database handle.
618         bookmarksDatabase.close();
619     }
620
621     // Update the folder icon.
622     public void updateFolder(int databaseId, byte[] folderIcon) {
623         // Get a writable database handle.
624         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
625
626         // Store the updated icon in `folderContentValues`.
627         ContentValues folderContentValues = new ContentValues();
628         folderContentValues.put(FAVORITE_ICON, folderIcon);
629
630         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
631         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
632
633         // Close the database handle.
634         bookmarksDatabase.close();
635     }
636
637     // Update the folder name, parent folder, and display order.
638     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder) {
639         // Get a writable database handle.
640         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
641
642         // Update the folder first.  Store the new folder name in `folderContentValues`.
643         ContentValues folderContentValues = new ContentValues();
644         folderContentValues.put(BOOKMARK_NAME, newFolderName);
645         folderContentValues.put(PARENT_FOLDER, parentFolder);
646         folderContentValues.put(DISPLAY_ORDER, displayOrder);
647
648         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
649         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
650
651         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
652         ContentValues bookmarkContentValues = new ContentValues();
653         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
654
655         // SQL escape `oldFolderName`.
656         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
657
658         // 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`.
659         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
660
661         // Close the database handle.
662         bookmarksDatabase.close();
663     }
664
665     // Update the folder name and icon.
666     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
667         // Get a writable database handle.
668         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
669
670         // Update the folder first.  Store the updated values in `folderContentValues`.
671         ContentValues folderContentValues = new ContentValues();
672         folderContentValues.put(BOOKMARK_NAME, newFolderName);
673         folderContentValues.put(FAVORITE_ICON, folderIcon);
674
675         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
676         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
677
678         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
679         ContentValues bookmarkContentValues = new ContentValues();
680         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
681
682         // SQL escape `oldFolderName`.
683         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
684
685         // 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`.
686         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
687
688         // Close the database handle.
689         bookmarksDatabase.close();
690     }
691
692     // Update the folder name and icon.
693     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, String parentFolder, int displayOrder, byte[] folderIcon) {
694         // Get a writable database handle.
695         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
696
697         // Update the folder first.  Store the updated values in `folderContentValues`.
698         ContentValues folderContentValues = new ContentValues();
699         folderContentValues.put(BOOKMARK_NAME, newFolderName);
700         folderContentValues.put(PARENT_FOLDER, parentFolder);
701         folderContentValues.put(DISPLAY_ORDER, displayOrder);
702         folderContentValues.put(FAVORITE_ICON, folderIcon);
703
704         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
705         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
706
707         // Update the bookmarks inside the folder.  Store the new parent folder name in `bookmarkContentValues`.
708         ContentValues bookmarkContentValues = new ContentValues();
709         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
710
711         // SQL escape `oldFolderName`.
712         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
713
714         // 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`.
715         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
716
717         // Close the database handle.
718         bookmarksDatabase.close();
719     }
720
721     // Update the display order for one bookmark or folder.
722     public void updateDisplayOrder(int databaseId, int displayOrder) {
723         // Get a writable database handle.
724         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
725
726         // Store the new display order in `bookmarkContentValues`.
727         ContentValues bookmarkContentValues = new ContentValues();
728         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
729
730         // Update the database.  The last argument is `null` because there are no `whereArgs`.
731         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
732
733         // Close the database handle.
734         bookmarksDatabase.close();
735     }
736
737     // Move one bookmark or folder to a new folder.
738     public void moveToFolder(int databaseId, String newFolder) {
739         // Get a writable database handle.
740         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
741
742         // SQL escape the new folder name.
743         String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
744
745         // Prepare a SQL query to select all the bookmarks in the new folder.
746         String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
747                 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
748                 " ORDER BY " + DISPLAY_ORDER + " ASC";
749
750         // Get a cursor for all the bookmarks in the new folder.  The second argument is `null` because there are no `selectionArgs`.
751         Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
752
753         // Instantiate a variable to store the display order after the move.
754         int displayOrder;
755
756         // Set the new display order.
757         if (newFolderCursor.getCount() > 0) {  // There are already bookmarks in the folder.
758             // Move to the last bookmark.
759             newFolderCursor.moveToLast();
760
761             // Set the display order to be one greater that the last bookmark.
762             displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
763         } else {  // There are no bookmarks in the new folder.
764             // Set the display order to be `0`.
765             displayOrder = 0;
766         }
767
768         // Close the new folder `Cursor`.
769         newFolderCursor.close();
770
771         // Store the new values in `bookmarkContentValues`.
772         ContentValues bookmarkContentValues = new ContentValues();
773         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
774         bookmarkContentValues.put(PARENT_FOLDER, newFolder);
775
776         // Update the database.  The last argument is `null` because there are no `whereArgs`.
777         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
778
779         // Close the database handle.
780         bookmarksDatabase.close();
781     }
782
783     // Delete one bookmark.
784     public void deleteBookmark(int databaseId) {
785         // Get a writable database handle.
786         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
787
788         // Deletes the row with the given `databaseId`.  The last argument is `null` because we don't need additional parameters.
789         bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
790
791         // Close the database handle.
792         bookmarksDatabase.close();
793     }
794 }