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, byte[] favoriteIcon) {
66 ContentValues bookmarkContentValues = new ContentValues();
68 // ID is created automatically.
69 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
70 bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
71 bookmarkContentValues.put(PARENT_FOLDER, "");
72 bookmarkContentValues.put(IS_FOLDER, false);
73 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
75 // Get a writable database handle.
76 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
78 // The second argument is "null", which makes it so that completely null rows cannot be created. Not a problem in our case.
79 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
81 // Close the database handle.
82 bookmarksDatabase.close();
85 public Cursor getBookmarkCursor(int databaseId) {
86 // Get a readable database handle.
87 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
89 // Prepare the SQL statement to get the cursor for `databaseId`.
90 final String GET_ONE_BOOKMARK = "Select * FROM " + BOOKMARKS_TABLE +
91 " WHERE " + _ID + " = " + databaseId;
93 // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs.
94 // We can't close the `Cursor` because we need to use it in the parent activity.
95 return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
98 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) {
99 // Get a readable database handle.
100 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
102 // Prepare a string that contains the comma-separated list of IDs not to get.
103 String doNotGetIdsString = "";
104 // Extract the array to `doNotGetIdsString`.
105 for (long databaseIdLong : exceptIdLongArray) {
106 // If this is the first number, only add the number.
107 if (doNotGetIdsString.isEmpty()) {
108 doNotGetIdsString = String.valueOf(databaseIdLong);
109 } else { // If there already is a number in the string, place a `,` before the number.
110 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
114 // Prepare the SQL statement to select all items except those with the specified IDs.
115 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
116 " WHERE " + _ID + " NOT IN (" + doNotGetIdsString + ")";
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_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
123 public Cursor getAllBookmarksCursor() {
124 // Get a readable database handle.
125 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
127 // Get everything in the BOOKMARKS_TABLE.
128 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
130 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
131 // We can't close the Cursor because we need to use it in the parent activity.
132 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
135 public String getBookmarkURL(int databaseId) {
136 // Get a readable database handle.
137 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
139 // Prepare the SQL statement to get the row for the selected databaseId.
140 final String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE +
141 " WHERE " + _ID + " = " + databaseId;
143 // Save the results as Cursor and move it to the first (only) row. The second argument is "null" because there are no selectionArgs.
144 Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null);
145 bookmarksCursor.moveToFirst();
147 // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL.
148 int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL);
149 String bookmarkURLString = bookmarksCursor.getString(urlColumnInt);
151 // Close the Cursor and the database handle.
152 bookmarksCursor.close();
153 bookmarksDatabase.close();
155 // Return the bookmarkURLString.
156 return bookmarkURLString;
159 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
160 // Store the updated values in `bookmarkContentValues`.
161 ContentValues bookmarkContentValues = new ContentValues();
163 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
164 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
166 // Get a writable database handle.
167 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
169 // Update the database. The last argument is `null` because there are no `whereArgs`.
170 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
172 // Close the database handle.
173 bookmarksDatabase.close();
176 public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
177 // Store the updated values in `bookmarkContentValues`.
178 ContentValues bookmarkContentValues = new ContentValues();
180 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
181 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
182 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
184 // Get a writable database handle.
185 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
187 // Update the database. The last argument is `null` because there are no `whereArgs`.
188 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
190 // Close the database handle.
191 bookmarksDatabase.close();
194 public void deleteBookmark(int databaseId) {
195 // Get a writable database handle.
196 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
198 // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters.
199 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
201 // Close the database handle.
202 bookmarksDatabase.close();