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;
30 public class BookmarksDatabaseHandler extends SQLiteOpenHelper {
31 private static final int SCHEMA_VERSION = 1;
32 private static final String BOOKMARKS_DATABASE = "bookmarks.db";
33 private static final String BOOKMARKS_TABLE = "bookmarks";
35 public static final String _ID = "_id";
36 public static final String DISPLAY_ORDER = "displayorder";
37 public static final String BOOKMARK_NAME = "bookmarkname";
38 public static final String BOOKMARK_URL = "bookmarkurl";
39 public static final String PARENT_FOLDER = "parentfolder";
40 public static final String IS_FOLDER = "isfolder";
41 public static final String FAVORITE_ICON = "favoriteicon";
43 public BookmarksDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, 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 public 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 public 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 public 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 public Cursor getFolderCursor(String folderName) {
122 // Get a readable database handle.
123 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
125 // SQL escape `folderName`.
126 folderName = DatabaseUtils.sqlEscapeString(folderName);
128 // Prepare the SQL statement to get the `Cursor` for the folder.
129 final String GET_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
130 " WHERE " + BOOKMARK_NAME + " = " + folderName +
131 " AND " + IS_FOLDER + " = " + 1;
133 // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
134 // We can't close the `Cursor` because we need to use it in the parent activity.
135 return bookmarksDatabase.rawQuery(GET_FOLDER, null);
138 public String getParentFolder(String currentFolder) {
139 // Get a readable database handle.
140 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
142 // SQL escape `currentFolder`.
143 currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
145 // Prepare the SQL statement to get the parent folder.
146 final String GET_PARENT_FOLDER = "Select * FROM " + BOOKMARKS_TABLE +
147 " WHERE " + IS_FOLDER + " = " + 1 + " AND " + BOOKMARK_NAME + " = " + currentFolder;
149 // The second argument is `null` because there are no `selectionArgs`.
150 Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null);
151 bookmarkCursor.moveToFirst();
153 // Store the name of the parent folder.
154 String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
156 // Close the `Cursor`.
157 bookmarkCursor.close();
162 public Cursor getAllBookmarksCursor() {
163 // Get a readable database handle.
164 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
166 // Get everything in the BOOKMARKS_TABLE.
167 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
169 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
170 // We can't close the Cursor because we need to use it in the parent activity.
171 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
174 public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
175 // Get a readable database handle.
176 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
178 // SQL escape `folderName`.
179 folderName = DatabaseUtils.sqlEscapeString(folderName);
181 // Get everything in the BOOKMARKS_TABLE.
182 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE +
183 " WHERE " + PARENT_FOLDER + " = " + folderName + " ORDER BY " + DISPLAY_ORDER + " ASC";
185 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
186 // We can't close the Cursor because we need to use it in the parent activity.
187 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
190 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
191 // Get a readable database handle.
192 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
194 // Prepare a string that contains the comma-separated list of IDs not to get.
195 String doNotGetIdsString = "";
196 // Extract the array to `doNotGetIdsString`.
197 for (long databaseIdLong : exceptIdLongArray) {
198 // If this is the first number, only add the number.
199 if (doNotGetIdsString.isEmpty()) {
200 doNotGetIdsString = String.valueOf(databaseIdLong);
201 } else { // If there already is a number in the string, place a `,` before the number.
202 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
206 // SQL escape `folderName`.
207 folderName = DatabaseUtils.sqlEscapeString(folderName);
209 // Prepare the SQL statement to select all items except those with the specified IDs.
210 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
211 " WHERE " + PARENT_FOLDER + " = " + folderName + " AND " + _ID + " NOT IN (" + doNotGetIdsString + ") ORDER BY " + DISPLAY_ORDER + " ASC";
213 // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs.
214 // We can't close the `Cursor` because we need to use it in the parent activity.
215 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
218 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
219 // Store the updated values in `bookmarkContentValues`.
220 ContentValues bookmarkContentValues = new ContentValues();
222 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
223 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
225 // Get a writable database handle.
226 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
228 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
229 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
231 // Close the database handle.
232 bookmarksDatabase.close();
235 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
236 // Store the updated values in `bookmarkContentValues`.
237 ContentValues bookmarkContentValues = new ContentValues();
239 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
240 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
241 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
243 // Get a writable database handle.
244 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
246 // Update the bookmark. The last argument is `null` because there are no `whereArgs`.
247 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
249 // Close the database handle.
250 bookmarksDatabase.close();
253 public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
254 // Get a writable database handle.
255 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
257 // Update the folder first. Store the updated values in `folderContentValues`.
258 ContentValues folderContentValues = new ContentValues();
260 folderContentValues.put(BOOKMARK_NAME, newFolderName);
262 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
263 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
266 // Update the bookmarks inside the folder with the new parent folder name.
267 ContentValues bookmarkContentValues = new ContentValues();
269 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
271 // SQL escape `oldFolderName`.
272 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
274 // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`.
275 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
277 // Close the database handle.
278 bookmarksDatabase.close();
281 public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
282 // Get a writable database handle.
283 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
285 // Update the folder first. Store the updated values in `folderContentValues`.
286 ContentValues folderContentValues = new ContentValues();
288 folderContentValues.put(BOOKMARK_NAME, newFolderName);
289 folderContentValues.put(FAVORITE_ICON, folderIcon);
291 // Run the update on the folder. The last argument is `null` because there are no `whereArgs`.
292 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
295 // Update the bookmarks inside the folder with the new parent folder name.
296 ContentValues bookmarkContentValues = new ContentValues();
298 bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
300 // SQL escape `oldFolderName`.
301 oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
303 // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`.
304 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
306 // Close the database handle.
307 bookmarksDatabase.close();
310 public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
311 // Store the updated values in `bookmarkContentValues`.
312 ContentValues bookmarkContentValues = new ContentValues();
314 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
316 // Get a writable database handle.
317 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
319 // Update the database. The last argument is `null` because there are no `whereArgs`.
320 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
322 // Close the database handle.
323 bookmarksDatabase.close();
326 public void deleteBookmark(int databaseId) {
327 // Get a writable database handle.
328 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
330 // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters.
331 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
333 // Close the database handle.
334 bookmarksDatabase.close();