]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java
bc17bc7dba7c4d4c068a36d08dca8f8f41efbb4d
[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.DatabaseUtils;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
28 import android.provider.ContactsContract;
29
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";
34
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";
42
43     public BookmarksDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
44         super(context, BOOKMARKS_DATABASE, factory, SCHEMA_VERSION);
45     }
46
47     @Override
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);";
58
59         bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
60     }
61
62     @Override
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.
65     }
66
67     public void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, String parentFolder, byte[] favoriteIcon) {
68         ContentValues bookmarkContentValues = new ContentValues();
69
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);
77
78         // Get a writable database handle.
79         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
80
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);
83
84         // Close the database handle.
85         bookmarksDatabase.close();
86     }
87
88     public void createFolder(String folderName, int displayOrder, String parentFolder, byte[] favoriteIcon) {
89         ContentValues bookmarkContentValues = new ContentValues();
90
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);
97
98         // Get a writable database handle.
99         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
100
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);
103
104         // Close the database handle.
105         bookmarksDatabase.close();
106     }
107
108     public Cursor getBookmarkCursor(int databaseId) {
109         // Get a readable database handle.
110         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
111
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;
115
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);
119     }
120
121     public Cursor getFolderCursor(String folderName) {
122         // Get a readable database handle.
123         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
124
125         // SQL escape `folderName`.
126         folderName = DatabaseUtils.sqlEscapeString(folderName);
127
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;
132
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);
136     }
137
138     public String getParentFolder(String currentFolder) {
139         // Get a readable database handle.
140         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
141
142         // SQL escape `currentFolder`.
143         currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
144
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;
148
149         // The second argument is `null` because there are no `selectionArgs`.
150         Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null);
151         bookmarkCursor.moveToFirst();
152
153         // Store the name of the parent folder.
154         String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
155
156         // Close the `Cursor`.
157         bookmarkCursor.close();
158
159         return parentFolder;
160     }
161
162     public Cursor getAllBookmarksCursor() {
163         // Get a readable database handle.
164         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
165
166         // Get everything in the BOOKMARKS_TABLE.
167         final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE;
168
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);
172     }
173
174     public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
175         // Get a readable database handle.
176         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
177
178         // SQL escape `folderName`.
179         folderName = DatabaseUtils.sqlEscapeString(folderName);
180
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";
184
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);
188     }
189
190     public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
191         // Get a readable database handle.
192         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
193
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;
203             }
204         }
205
206         // SQL escape `folderName`.
207         folderName = DatabaseUtils.sqlEscapeString(folderName);
208
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";
212
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);
216     }
217
218     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
219         // Store the updated values in `bookmarkContentValues`.
220         ContentValues bookmarkContentValues = new ContentValues();
221
222         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
223         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
224
225         // Get a writable database handle.
226         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
227
228         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
229         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
230
231         // Close the database handle.
232         bookmarksDatabase.close();
233     }
234
235     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
236         // Store the updated values in `bookmarkContentValues`.
237         ContentValues bookmarkContentValues = new ContentValues();
238
239         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
240         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
241         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
242
243         // Get a writable database handle.
244         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
245
246         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
247         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
248
249         // Close the database handle.
250         bookmarksDatabase.close();
251     }
252
253     public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
254         // Get a writable database handle.
255         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
256
257         // Update the folder first.  Store the updated values in `folderContentValues`.
258         ContentValues folderContentValues = new ContentValues();
259
260         folderContentValues.put(BOOKMARK_NAME, newFolderName);
261
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);
264
265
266         // Update the bookmarks inside the folder with the new parent folder name.
267         ContentValues bookmarkContentValues = new ContentValues();
268
269         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
270
271         // SQL escape `oldFolderName`.
272         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
273
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);
276
277         // Close the database handle.
278         bookmarksDatabase.close();
279     }
280
281     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
282         // Get a writable database handle.
283         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
284
285         // Update the folder first.  Store the updated values in `folderContentValues`.
286         ContentValues folderContentValues = new ContentValues();
287
288         folderContentValues.put(BOOKMARK_NAME, newFolderName);
289         folderContentValues.put(FAVORITE_ICON, folderIcon);
290
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);
293
294
295         // Update the bookmarks inside the folder with the new parent folder name.
296         ContentValues bookmarkContentValues = new ContentValues();
297
298         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
299
300         // SQL escape `oldFolderName`.
301         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
302
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);
305
306         // Close the database handle.
307         bookmarksDatabase.close();
308     }
309
310     public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
311         // Store the updated values in `bookmarkContentValues`.
312         ContentValues bookmarkContentValues = new ContentValues();
313
314         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
315
316         // Get a writable database handle.
317         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
318
319         // Update the database.  The last argument is `null` because there are no `whereArgs`.
320         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
321
322         // Close the database handle.
323         bookmarksDatabase.close();
324     }
325
326     public void deleteBookmark(int databaseId) {
327         // Get a writable database handle.
328         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
329
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);
332
333         // Close the database handle.
334         bookmarksDatabase.close();
335     }
336 }