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 DISPLAYORDER = "displayorder";
35 public static final String BOOKMARK_NAME = "bookmarkname";
36 public static final String BOOKMARK_URL = "bookmarkurl";
37 public static final String PARENTFOLDER = "parentfolder";
38 public static final String ISFOLDER = "isfolder";
39 public static final String FAVORITEICON = "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 DISPLAYORDER + " integer, " +
51 BOOKMARK_NAME + " text, " +
52 BOOKMARK_URL + " text, " +
53 PARENTFOLDER + " text, " +
54 ISFOLDER + " boolean, " +
55 FAVORITEICON + " 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(PARENTFOLDER, "");
72 bookmarkContentValues.put(ISFOLDER, false);
73 bookmarkContentValues.put(FAVORITEICON, 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 getBookmarksCursor() {
86 // Get a readable database handle.
87 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
89 // Get everything in the BOOKMARKS_TABLE.
90 final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
92 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
93 // We can't close the Cursor because we need to use it in the parent activity.
94 return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
97 public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) {
98 // Get a readable database handle.
99 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
101 // Prepare a string that contains the comma-separated list of IDs not to get.
102 String doNotGetIdsString = "";
103 // Extract the array to `doNotGetIdsString`.
104 for (long databaseIdLong : exceptIdLongArray) {
105 // If this is the first number, only add the number.
106 if (doNotGetIdsString.isEmpty()) {
107 doNotGetIdsString = String.valueOf(databaseIdLong);
108 } else { // If there already is a number in the string, place a `,` before the number.
109 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
113 // Prepare the SQL statement to select all items except those with the specified IDs.
114 final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
115 " WHERE " + _ID + " NOT IN (" + doNotGetIdsString + ")";
117 // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs.
118 // We can't close the Cursor because we need to use it in the parent activity.
119 return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
122 public String getBookmarkURL(int databaseId) {
123 // Get a readable database handle.
124 SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
126 // Prepare the SQL statement to get the row for the selected databaseId.
127 final String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE +
128 " WHERE " + _ID + " = " + databaseId;
130 // Save the results as Cursor and move it to the first (only) row. The second argument is "null" because there are no selectionArgs.
131 Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null);
132 bookmarksCursor.moveToFirst();
134 // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL.
135 int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL);
136 String bookmarkURLString = bookmarksCursor.getString(urlColumnInt);
138 // Close the Cursor and the database handle.
139 bookmarksCursor.close();
140 bookmarksDatabase.close();
142 // Return the bookmarkURLString.
143 return bookmarkURLString;
146 public void deleteBookmark(int databaseId) {
147 // Get a writable database handle.
148 SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
150 // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters.
151 bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
153 // Close the database handle.
154 bookmarksDatabase.close();