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;
28 import android.provider.ContactsContract;
29 import android.support.design.widget.Snackbar;
30 import android.support.v4.content.ContextCompat;
32 public class BookmarksDatabaseHandler extends SQLiteOpenHelper {
33 private static final int SCHEMA_VERSION = 1;
34 private static final String BOOKMARKS_DATABASE = "bookmarks.db";
35 private static final String BOOKMARKS_TABLE = "bookmarks";
37 public static final String _ID = "_id";
38 public static final String DISPLAY_ORDER = "displayorder";
39 public static final String BOOKMARK_NAME = "bookmarkname";
40 public static final String BOOKMARK_URL = "bookmarkurl";
41 public static final String PARENT_FOLDER = "parentfolder";
42 public static final String IS_FOLDER = "isfolder";
43 public static final String FAVORITE_ICON = "favoriteicon";
45 public BookmarksDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
46 super(context, BOOKMARKS_DATABASE, factory, SCHEMA_VERSION);
50 public void onCreate(SQLiteDatabase bookmarksDatabase) {
51 // Create the database if it doesn't exist.
52 String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" +
53 _ID + " integer primary key, " +
54 DISPLAY_ORDER + " integer, " +
55 BOOKMARK_NAME + " text, " +
56 BOOKMARK_URL + " text, " +
57 PARENT_FOLDER + " text, " +
58 IS_FOLDER + " boolean, " +
59 FAVORITE_ICON + " blob);";
61 bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
65 public void onUpgrade(SQLiteDatabase bookmarksDatabase, int oldVersion, int newVersion) {
66 // Code for upgrading the database will be added here when the schema version > 1.
69 public void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, String parentFolder, byte[] favoriteIcon) {
70 ContentValues bookmarkContentValues = new ContentValues();
72 // ID is created automatically.
73 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
74 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
75 bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
76 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
77 bookmarkContentValues.put(IS_FOLDER, false);
78 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
80 // Get a writable database handle.
81 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
83 // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case.
84 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
86 // Close the database handle.
87 bookmarksDatabase.close();
90 public void createFolder(String folderName, int displayOrder, String parentFolder, byte[] favoriteIcon) {
91 ContentValues bookmarkContentValues = new ContentValues();
93 // ID is created automatically.
94 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
95 bookmarkContentValues.put(BOOKMARK_NAME, folderName);
96 bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
97 bookmarkContentValues.put(IS_FOLDER, true);
98 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
100 // Get a writable database handle.
101 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
103 // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case.
104 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
106 // Close the database handle.
107 bookmarksDatabase.close();
110 public Cursor getBookmarkCursor(int databaseId) {
111 // Get a readable database handle.
112 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
114 // Prepare the SQL statement to get the `Cursor` for `databaseId`
115 final String GET_ONE_BOOKMARK = "Select * FROM " + BOOKMARKS_TABLE +
116 " WHERE " + _ID + " = " + databaseId;
118 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
119 // We can't close the `Cursor` because we need to use it in the parent activity.
120 return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
123 public String getFolderName (int databaseId) {
124 // Get a readable database handle.
125 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
127 // Prepare the SQL statement to get the `Cursor` for the folder.
128 final String GET_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
129 " WHERE " + _ID + " = " + databaseId;
131 // Get `folderCursor`. The second argument is `null` because there are no `selectionArgs`.
132 Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
135 folderCursor.moveToFirst();
136 String folderName = folderCursor.getString(folderCursor.getColumnIndex(BOOKMARK_NAME));
138 // Close the cursor and the database handle.
139 folderCursor.close();
140 bookmarksDatabase.close();
142 // Return the folder name.
146 public Cursor getFolderCursor(String folderName) {
147 // Get a readable database handle.
148 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
150 // SQL escape `folderName`.
151 folderName = DatabaseUtils.sqlEscapeString(folderName);
153 // Prepare the SQL statement to get the `Cursor` for the folder.
154 final String GET_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
155 " WHERE " + BOOKMARK_NAME + " = " + folderName +
156 " AND " + IS_FOLDER + " = " + 1;
158 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
159 // We can't close the `Cursor` because we need to use it in the parent activity.
160 return bookmarksDatabase.rawQuery(GET_FOLDER, null);
163 public Cursor getFoldersCursorExcept(String exceptFolders) {
164 // Get a readable database handle.
165 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
167 // Prepare the SQL statement to get the `Cursor` for the folders.
168 final String GET_FOLDERS_EXCEPT = "Select * FROM " + BOOKMARKS_TABLE +
169 " WHERE " + IS_FOLDER + " = " + 1 +
170 " AND " + BOOKMARK_NAME + " NOT IN (" + exceptFolders +
171 ") ORDER BY " + BOOKMARK_NAME + " ASC";
173 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
174 // We can't close the `Cursor` because we need to use it in the parent activity.
175 return bookmarksDatabase.rawQuery(GET_FOLDERS_EXCEPT, null);
178 public Cursor getSubfoldersCursor(String currentFolder) {
179 // Get a readable database handle.
180 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
182 // SQL escape `currentFolder.
183 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
185 // Prepare the SQL statement to get the `Cursor` for the subfolders.
186 final String GET_SUBFOLDERS = "Select * FROM " + BOOKMARKS_TABLE +
187 " WHERE " + PARENT_FOLDER + " = " + currentFolder +
188 " AND " + IS_FOLDER + " = " + 1;
190 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
191 // We can't close the `Cursor` because we need to use it in the parent activity.
192 return bookmarksDatabase.rawQuery(GET_SUBFOLDERS, null);
195 public String getParentFolder(String currentFolder) {
196 // Get a readable database handle.
197 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
199 // SQL escape `currentFolder`.
200 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
202 // Prepare the SQL statement to get the parent folder.
203 final String GET_PARENT_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
204 " WHERE " + IS_FOLDER + " = " + 1 +
205 " AND " + BOOKMARK_NAME + " = " + currentFolder;
207 // The second argument is `null` because there are no `selectionArgs`.
208 Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null);
209 bookmarkCursor.moveToFirst();
211 // Store the name of the parent folder.
212 String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
214 // Close the `Cursor`.
215 bookmarkCursor.close();
220 public Cursor getAllBookmarksCursor() {
221 // Get a readable database handle.
222 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
224 // Get everything in the BOOKMARKS_TABLE.
225 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
227 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
228 // We can't close the Cursor because we need to use it in the parent activity.
229 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
232 public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
233 // Get a readable database handle.
234 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
236 // SQL escape `folderName`.
237 folderName = DatabaseUtils.sqlEscapeString(folderName);
239 // Get everything in the BOOKMARKS_TABLE.
240 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE +
241 " WHERE " + PARENT_FOLDER + " = " + folderName +
242 " ORDER BY " + DISPLAY_ORDER + " ASC";
244 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
245 // We can't close the Cursor because we need to use it in the parent activity.
246 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
249 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
250 // Get a readable database handle.
251 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
253 // Prepare a string that contains the comma-separated list of IDs not to get.
254 String doNotGetIdsString = "";
255 // Extract the array to `doNotGetIdsString`.
256 for (long databaseIdLong : exceptIdLongArray) {
257 // If this is the first number, only add the number.
258 if (doNotGetIdsString.isEmpty()) {
259 doNotGetIdsString = String.valueOf(databaseIdLong);
260 } else { // If there already is a number in the string, place a `,` before the number.
261 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
265 // SQL escape `folderName`.
266 folderName = DatabaseUtils.sqlEscapeString(folderName);
268 // Prepare the SQL statement to select all items except those with the specified IDs.
269 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
270 " WHERE " + PARENT_FOLDER + " = " + folderName +
271 " AND " + _ID + " NOT IN (" + doNotGetIdsString +
272 ") ORDER BY " + DISPLAY_ORDER + " ASC";
274 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
275 // We can't close the `Cursor` because we need to use it in the parent activity.
276 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
279 public boolean isFolder(int databaseId) {
280 // Get a readable database handle.
281 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
283 // Prepare the SQL statement to determine if `databaseId` is a folder.
284 final String CHECK_IF_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
285 " WHERE " + _ID + " = " + databaseId;
287 // Populate folderCursor. The second argument is `null` because there are no `selectionArgs`.
288 Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
290 // Ascertain if this database ID is a folder.
291 folderCursor.moveToFirst();
292 boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
294 // Close the `Cursor` and the database handle.
295 folderCursor.close();
296 bookmarksDatabase.close();
301 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
302 // Store the updated values in `bookmarkContentValues`.
303 ContentValues bookmarkContentValues = new ContentValues();
305 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
306 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
308 // Get a writable database handle.
309 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
311 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
312 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
314 // Close the database handle.
315 bookmarksDatabase.close();
318 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
319 // Store the updated values in `bookmarkContentValues`.
320 ContentValues bookmarkContentValues = new ContentValues();
322 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
323 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
324 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
326 // Get a writable database handle.
327 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
329 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
330 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
332 // Close the database handle.
333 bookmarksDatabase.close();
336 public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
337 // Get a writable database handle.
338 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
340 // Update the folder first. Store the updated values in `folderContentValues`.
341 ContentValues folderContentValues = new ContentValues();
343 folderContentValues.put(BOOKMARK_NAME, newFolderName);
345 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
346 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
349 // Update the bookmarks inside the folder with the new parent folder name.
350 ContentValues bookmarkContentValues = new ContentValues();
352 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
354 // SQL escape `oldFolderName`.
355 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
357 // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`.
358 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
360 // Close the database handle.
361 bookmarksDatabase.close();
364 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
365 // Get a writable database handle.
366 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
368 // Update the folder first. Store the updated values in `folderContentValues`.
369 ContentValues folderContentValues = new ContentValues();
371 folderContentValues.put(BOOKMARK_NAME, newFolderName);
372 folderContentValues.put(FAVORITE_ICON, folderIcon);
374 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
375 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
378 // Update the bookmarks inside the folder with the new parent folder name.
379 ContentValues bookmarkContentValues = new ContentValues();
381 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
383 // SQL escape `oldFolderName`.
384 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
386 // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`.
387 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
389 // Close the database handle.
390 bookmarksDatabase.close();
393 public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
394 // Get a writable database handle.
395 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
397 // Store the new display order in `bookmarkContentValues`.
398 ContentValues bookmarkContentValues = new ContentValues();
399 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
401 // Update the database. The last argument is `null` because there are no `whereArgs`.
402 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
404 // Close the database handle.
405 bookmarksDatabase.close();
408 public void moveToFolder(int databaseId, String newFolder) {
409 // Get a writable database handle.
410 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
412 // Get the highest `DISPLAY_ORDER` in the new folder
413 String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
414 final String NEW_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
415 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
416 " ORDER BY " + DISPLAY_ORDER + " ASC";
417 // The second argument is `null` because there are no `selectionArgs`.
418 Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
420 if (newFolderCursor.getCount() > 0) {
421 newFolderCursor.moveToLast();
422 displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
426 newFolderCursor.close();
428 // Store the new values in `bookmarkContentValues`.
429 ContentValues bookmarkContentValues = new ContentValues();
430 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
431 bookmarkContentValues.put(PARENT_FOLDER, newFolder);
433 // Update the database. The last argument is 'null' because there are no 'whereArgs'.
434 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
436 // Close the database handle.
437 bookmarksDatabase.close();
440 public void deleteBookmark(int databaseId) {
441 // Get a writable database handle.
442 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
444 // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters.
445 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
447 // Close the database handle.
448 bookmarksDatabase.close();