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.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteOpenHelper;
28 public class BookmarksDatabaseHandler extends SQLiteOpenHelper {
29 private static final int SCHEMA_VERSION = 1;
30 private static final String BOOKMARKS_DATABASE = "bookmarks.db";
31 private static final String BOOKMARKS_TABLE = "bookmarks";
33 public static final String _ID = "_id";
34 public static final String DISPLAY_ORDER = "displayorder";
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 IS_FOLDER = "isfolder";
39 public static final String FAVORITE_ICON = "favoriteicon";
41 public BookmarksDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
42 super(context, BOOKMARKS_DATABASE, factory, SCHEMA_VERSION);
46 public void onCreate(SQLiteDatabase bookmarksDatabase) {
47 // Create the database if it doesn't exist.
48 String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" +
49 _ID + " integer primary key, " +
50 DISPLAY_ORDER + " integer, " +
51 BOOKMARK_NAME + " text, " +
52 BOOKMARK_URL + " text, " +
53 PARENT_FOLDER + " text, " +
54 IS_FOLDER + " boolean, " +
55 FAVORITE_ICON + " blob);";
57 bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
61 public void onUpgrade(SQLiteDatabase bookmarksDatabase, int oldVersion, int newVersion) {
62 // Code for upgrading the database will be added here when the schema version > 1.
65 public void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, byte[] favoriteIcon) {
66 ContentValues bookmarkContentValues = new ContentValues();
68 // ID is created automatically.
69 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
70 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
71 bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
72 bookmarkContentValues.put(PARENT_FOLDER, "");
73 bookmarkContentValues.put(IS_FOLDER, false);
74 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
76 // Get a writable database handle.
77 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
79 // The second argument is "null", which makes it so that completely null rows cannot be created. Not a problem in our case.
80 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
82 // Close the database handle.
83 bookmarksDatabase.close();
86 public Cursor getBookmarkCursor(int databaseId) {
87 // Get a readable database handle.
88 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
90 // Prepare the SQL statement to get the cursor for `databaseId`
91 final String GET_ONE_BOOKMARK = "Select * FROM " + BOOKMARKS_TABLE +
92 " WHERE " + _ID + " = " + databaseId;
94 // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs.
95 // We can't close the `Cursor` because we need to use it in the parent activity.
96 return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
99 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) {
100 // Get a readable database handle.
101 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
103 // Prepare a string that contains the comma-separated list of IDs not to get.
104 String doNotGetIdsString = "";
105 // Extract the array to `doNotGetIdsString`.
106 for (long databaseIdLong : exceptIdLongArray) {
107 // If this is the first number, only add the number.
108 if (doNotGetIdsString.isEmpty()) {
109 doNotGetIdsString = String.valueOf(databaseIdLong);
110 } else { // If there already is a number in the string, place a `,` before the number.
111 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
115 // Prepare the SQL statement to select all items except those with the specified IDs.
116 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
117 " WHERE " + _ID + " NOT IN (" + doNotGetIdsString + ") ORDER BY " + DISPLAY_ORDER + " ASC";
119 // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs.
120 // We can't close the `Cursor` because we need to use it in the parent activity.
121 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
124 public Cursor getAllBookmarksCursor() {
125 // Get a readable database handle.
126 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
128 // Get everything in the BOOKMARKS_TABLE.
129 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE + " ORDER BY " + DISPLAY_ORDER + " ASC";
131 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
132 // We can't close the Cursor because we need to use it in the parent activity.
133 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
136 public String getBookmarkURL(int databaseId) {
137 // Get a readable database handle.
138 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
140 // Prepare the SQL statement to get the row for the selected databaseId.
141 final String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE +
142 " WHERE " + _ID + " = " + databaseId;
144 // Save the results as Cursor and move it to the first (only) row. The second argument is "null" because there are no selectionArgs.
145 Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null);
146 bookmarksCursor.moveToFirst();
148 // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL.
149 int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL);
150 String bookmarkURLString = bookmarksCursor.getString(urlColumnInt);
152 // Close the Cursor and the database handle.
153 bookmarksCursor.close();
154 bookmarksDatabase.close();
156 // Return the bookmarkURLString.
157 return bookmarkURLString;
160 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
161 // Store the updated values in `bookmarkContentValues`.
162 ContentValues bookmarkContentValues = new ContentValues();
164 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
165 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
167 // Get a writable database handle.
168 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
170 // Update the database. The last argument is `null` because there are no `whereArgs`.
171 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
173 // Close the database handle.
174 bookmarksDatabase.close();
177 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
178 // Store the updated values in `bookmarkContentValues`.
179 ContentValues bookmarkContentValues = new ContentValues();
181 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
182 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
183 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
185 // Get a writable database handle.
186 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
188 // Update the database. The last argument is `null` because there are no `whereArgs`.
189 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
191 // Close the database handle.
192 bookmarksDatabase.close();
195 public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
196 // Store the updated values in `bookmarkContentValues`.
197 ContentValues bookmarkContentValues = new ContentValues();
199 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
201 // Get a writable database handle.
202 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
204 // Update the database. The last argument is `null` because there are no `whereArgs`.
205 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
207 // Close the database handle.
208 bookmarksDatabase.close();
211 public void deleteBookmark(int databaseId) {
212 // Get a writable database handle.
213 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
215 // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters.
216 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
218 // Close the database handle.
219 bookmarksDatabase.close();