2 * Copyright 2016 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;
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 class BookmarksDatabaseHandler extends SQLiteOpenHelper {
30 private static final int SCHEMA_VERSION = 1;
31 private static final String BOOKMARKS_DATABASE = "bookmarks.db";
32 private static final String BOOKMARKS_TABLE = "bookmarks";
34 static final String _ID = "_id";
35 static final String DISPLAY_ORDER = "displayorder";
36 static final String BOOKMARK_NAME = "bookmarkname";
37 static final String BOOKMARK_URL = "bookmarkurl";
38 static final String PARENT_FOLDER = "parentfolder";
39 static final String IS_FOLDER = "isfolder";
40 static final String FAVORITE_ICON = "favoriteicon";
42 // Initialize the database. The lint warnings for the unused parameters are suppressed.
43 BookmarksDatabaseHandler(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory factory, @SuppressWarnings("UnusedParameters") int version) {
44 super(context, BOOKMARKS_DATABASE, factory, SCHEMA_VERSION);
48 public void onCreate(SQLiteDatabase bookmarksDatabase) {
49 // Create the database if it doesn't exist.
50 String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" +
51 _ID + " integer primary key, " +
52 DISPLAY_ORDER + " integer, " +
53 BOOKMARK_NAME + " text, " +
54 BOOKMARK_URL + " text, " +
55 PARENT_FOLDER + " text, " +
56 IS_FOLDER + " boolean, " +
57 FAVORITE_ICON + " blob);";
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.
67 void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, String parentFolder, byte[] favoriteIcon) {
68 ContentValues bookmarkContentValues = new ContentValues();
70 // ID is created automatically.
71 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
72 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
73 bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
74 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
75 bookmarkContentValues.put(IS_FOLDER, false);
76 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
78 // Get a writable database handle.
79 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
81 // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case.
82 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
84 // Close the database handle.
85 bookmarksDatabase.close();
88 void createFolder(String folderName, int displayOrder, String parentFolder, byte[] favoriteIcon) {
89 ContentValues bookmarkContentValues = new ContentValues();
91 // ID is created automatically.
92 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
93 bookmarkContentValues.put(BOOKMARK_NAME, folderName);
94 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
95 bookmarkContentValues.put(IS_FOLDER, true);
96 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
98 // Get a writable database handle.
99 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
101 // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case.
102 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
104 // Close the database handle.
105 bookmarksDatabase.close();
108 Cursor getBookmarkCursor(int databaseId) {
109 // Get a readable database handle.
110 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
112 // Prepare the SQL statement to get the `Cursor` for `databaseId`
113 final String GET_ONE_BOOKMARK = "Select * FROM " + BOOKMARKS_TABLE +
114 " WHERE " + _ID + " = " + databaseId;
116 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
117 // We can't close the `Cursor` because we need to use it in the parent activity.
118 return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
121 String getFolderName (int databaseId) {
122 // Get a readable database handle.
123 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
125 // Prepare the SQL statement to get the `Cursor` for the folder.
126 final String GET_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
127 " WHERE " + _ID + " = " + databaseId;
129 // Get `folderCursor`. The second argument is `null` because there are no `selectionArgs`.
130 Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
133 folderCursor.moveToFirst();
134 String folderName = folderCursor.getString(folderCursor.getColumnIndex(BOOKMARK_NAME));
136 // Close the cursor and the database handle.
137 folderCursor.close();
138 bookmarksDatabase.close();
140 // Return the folder name.
144 Cursor getFolderCursor(String folderName) {
145 // Get a readable database handle.
146 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
148 // SQL escape `folderName`.
149 folderName = DatabaseUtils.sqlEscapeString(folderName);
151 // Prepare the SQL statement to get the `Cursor` for the folder.
152 final String GET_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
153 " WHERE " + BOOKMARK_NAME + " = " + folderName +
154 " AND " + IS_FOLDER + " = " + 1;
156 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
157 // We can't close the `Cursor` because we need to use it in the parent activity.
158 return bookmarksDatabase.rawQuery(GET_FOLDER, null);
161 Cursor getFoldersCursorExcept(String exceptFolders) {
162 // Get a readable database handle.
163 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
165 // Prepare the SQL statement to get the `Cursor` for the folders.
166 final String GET_FOLDERS_EXCEPT = "Select * FROM " + BOOKMARKS_TABLE +
167 " WHERE " + IS_FOLDER + " = " + 1 +
168 " AND " + BOOKMARK_NAME + " NOT IN (" + exceptFolders +
169 ") ORDER BY " + BOOKMARK_NAME + " ASC";
171 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
172 // We can't close the `Cursor` because we need to use it in the parent activity.
173 return bookmarksDatabase.rawQuery(GET_FOLDERS_EXCEPT, null);
176 Cursor getSubfoldersCursor(String currentFolder) {
177 // Get a readable database handle.
178 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
180 // SQL escape `currentFolder.
181 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
183 // Prepare the SQL statement to get the `Cursor` for the subfolders.
184 final String GET_SUBFOLDERS = "Select * FROM " + BOOKMARKS_TABLE +
185 " WHERE " + PARENT_FOLDER + " = " + currentFolder +
186 " AND " + IS_FOLDER + " = " + 1;
188 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
189 // We can't close the `Cursor` because we need to use it in the parent activity.
190 return bookmarksDatabase.rawQuery(GET_SUBFOLDERS, null);
193 String getParentFolder(String currentFolder) {
194 // Get a readable database handle.
195 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
197 // SQL escape `currentFolder`.
198 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
200 // Prepare the SQL statement to get the parent folder.
201 final String GET_PARENT_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
202 " WHERE " + IS_FOLDER + " = " + 1 +
203 " AND " + BOOKMARK_NAME + " = " + currentFolder;
205 // The second argument is `null` because there are no `selectionArgs`.
206 Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null);
207 bookmarkCursor.moveToFirst();
209 // Store the name of the parent folder.
210 String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
212 // Close the `Cursor`.
213 bookmarkCursor.close();
218 Cursor getAllBookmarksCursor() {
219 // Get a readable database handle.
220 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
222 // Get everything in the BOOKMARKS_TABLE.
223 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
225 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
226 // We can't close the Cursor because we need to use it in the parent activity.
227 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
230 Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
231 // Get a readable database handle.
232 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
234 // SQL escape `folderName`.
235 folderName = DatabaseUtils.sqlEscapeString(folderName);
237 // Get everything in the BOOKMARKS_TABLE.
238 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE +
239 " WHERE " + PARENT_FOLDER + " = " + folderName +
240 " ORDER BY " + DISPLAY_ORDER + " ASC";
242 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
243 // We can't close the Cursor because we need to use it in the parent activity.
244 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
247 Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
248 // Get a readable database handle.
249 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
251 // Prepare a string that contains the comma-separated list of IDs not to get.
252 String doNotGetIdsString = "";
253 // Extract the array to `doNotGetIdsString`.
254 for (long databaseIdLong : exceptIdLongArray) {
255 // If this is the first number, only add the number.
256 if (doNotGetIdsString.isEmpty()) {
257 doNotGetIdsString = String.valueOf(databaseIdLong);
258 } else { // If there already is a number in the string, place a `,` before the number.
259 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
263 // SQL escape `folderName`.
264 folderName = DatabaseUtils.sqlEscapeString(folderName);
266 // Prepare the SQL statement to select all items except those with the specified IDs.
267 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
268 " WHERE " + PARENT_FOLDER + " = " + folderName +
269 " AND " + _ID + " NOT IN (" + doNotGetIdsString +
270 ") ORDER BY " + DISPLAY_ORDER + " ASC";
272 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
273 // We can't close the `Cursor` because we need to use it in the parent activity.
274 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
277 boolean isFolder(int databaseId) {
278 // Get a readable database handle.
279 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
281 // Prepare the SQL statement to determine if `databaseId` is a folder.
282 final String CHECK_IF_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
283 " WHERE " + _ID + " = " + databaseId;
285 // Populate folderCursor. The second argument is `null` because there are no `selectionArgs`.
286 Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
288 // Ascertain if this database ID is a folder.
289 folderCursor.moveToFirst();
290 boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
292 // Close the `Cursor` and the database handle.
293 folderCursor.close();
294 bookmarksDatabase.close();
299 void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
300 // Store the updated values in `bookmarkContentValues`.
301 ContentValues bookmarkContentValues = new ContentValues();
303 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
304 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
306 // Get a writable database handle.
307 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
309 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
310 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
312 // Close the database handle.
313 bookmarksDatabase.close();
316 void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
317 // Store the updated values in `bookmarkContentValues`.
318 ContentValues bookmarkContentValues = new ContentValues();
320 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
321 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
322 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
324 // Get a writable database handle.
325 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
327 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
328 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
330 // Close the database handle.
331 bookmarksDatabase.close();
334 void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
335 // Get a writable database handle.
336 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
338 // Update the folder first. Store the updated values in `folderContentValues`.
339 ContentValues folderContentValues = new ContentValues();
341 folderContentValues.put(BOOKMARK_NAME, newFolderName);
343 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
344 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
347 // Update the bookmarks inside the folder with the new parent folder name.
348 ContentValues bookmarkContentValues = new ContentValues();
350 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
352 // SQL escape `oldFolderName`.
353 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
355 // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`.
356 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
358 // Close the database handle.
359 bookmarksDatabase.close();
362 void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
363 // Get a writable database handle.
364 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
366 // Update the folder first. Store the updated values in `folderContentValues`.
367 ContentValues folderContentValues = new ContentValues();
369 folderContentValues.put(BOOKMARK_NAME, newFolderName);
370 folderContentValues.put(FAVORITE_ICON, folderIcon);
372 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
373 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
376 // Update the bookmarks inside the folder with the new parent folder name.
377 ContentValues bookmarkContentValues = new ContentValues();
379 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
381 // SQL escape `oldFolderName`.
382 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
384 // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`.
385 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
387 // Close the database handle.
388 bookmarksDatabase.close();
391 void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
392 // Get a writable database handle.
393 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
395 // Store the new display order in `bookmarkContentValues`.
396 ContentValues bookmarkContentValues = new ContentValues();
397 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
399 // Update the database. The last argument is `null` because there are no `whereArgs`.
400 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
402 // Close the database handle.
403 bookmarksDatabase.close();
406 void moveToFolder(int databaseId, String newFolder) {
407 // Get a writable database handle.
408 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
410 // Get the highest `DISPLAY_ORDER` in the new folder
411 String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
412 final String NEW_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
413 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
414 " ORDER BY " + DISPLAY_ORDER + " ASC";
415 // The second argument is `null` because there are no `selectionArgs`.
416 Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
418 if (newFolderCursor.getCount() > 0) {
419 newFolderCursor.moveToLast();
420 displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
424 newFolderCursor.close();
426 // Store the new values in `bookmarkContentValues`.
427 ContentValues bookmarkContentValues = new ContentValues();
428 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
429 bookmarkContentValues.put(PARENT_FOLDER, newFolder);
431 // Update the database. The last argument is 'null' because there are no 'whereArgs'.
432 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
434 // Close the database handle.
435 bookmarksDatabase.close();
438 void deleteBookmark(int databaseId) {
439 // Get a writable database handle.
440 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
442 // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters.
443 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
445 // Close the database handle.
446 bookmarksDatabase.close();