]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java
Add the ability to delete bookmarks.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / BookmarksDatabaseHandler.java
1 /**
2  * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser;
21
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;
27
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";
32
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";
40
41     public BookmarksDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
42         super(context, BOOKMARKS_DATABASE, factory, SCHEMA_VERSION);
43     }
44
45     @Override
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);";
56
57         bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
58     }
59
60     @Override
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.
63     }
64
65     public void createBookmark(String bookmarkName, String bookmarkURL, byte[] favoriteIcon) {
66         ContentValues bookmarkContentValues = new ContentValues();
67
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);
74
75         // Get a writable database handle.
76         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
77
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);
80
81         // Close the database handle.
82         bookmarksDatabase.close();
83     }
84
85     public Cursor getBookmarksCursor() {
86         // Get a readable database handle.
87         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
88
89         // Get everything in the BOOKMARKS_TABLE.
90         final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
91
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);
95     }
96
97     public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) {
98         // Get a readable database handle.
99         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
100
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;
110             }
111         }
112
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 + ")";
116
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);
120     }
121
122     public String getBookmarkURL(int databaseId) {
123         // Get a readable database handle.
124         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
125
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;
129
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();
133
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);
137
138         // Close the Cursor and the database handle.
139         bookmarksCursor.close();
140         bookmarksDatabase.close();
141
142         // Return the bookmarkURLString.
143         return bookmarkURLString;
144     }
145
146     public void deleteBookmark(int databaseId) {
147         // Get a writable database handle.
148         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
149
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);
152
153         // Close the database handle.
154         bookmarksDatabase.close();
155     }
156 }