]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java
Hide the move to folder menu item if no folders exist. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / BookmarksDatabaseHelper.java
1 /*
2  * Copyright © 2016-2017 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.helpers;
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
29 public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
30     private static final int SCHEMA_VERSION = 1;
31     private static final String BOOKMARKS_DATABASE = "bookmarks.db";
32     private static final String BOOKMARKS_TABLE = "bookmarks";
33
34     public static final String _ID = "_id";
35     public static final String DISPLAY_ORDER = "displayorder";
36     public static final String BOOKMARK_NAME = "bookmarkname";
37     public static final String BOOKMARK_URL = "bookmarkurl";
38     public static final String PARENT_FOLDER = "parentfolder";
39     public static final String IS_FOLDER = "isfolder";
40     public static final String FAVORITE_ICON = "favoriteicon";
41
42     // Initialize the database.  The lint warnings for the unused parameters are suppressed.
43     public BookmarksDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
44         super(context, BOOKMARKS_DATABASE, cursorFactory, SCHEMA_VERSION);
45     }
46
47     @Override
48     public void onCreate(SQLiteDatabase bookmarksDatabase) {
49         // Setup the SQL string to create the `bookmarks` table.
50         final 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         // Create the `bookmarks` table if it doesn't exist.
60         bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE);
61     }
62
63     @Override
64     public void onUpgrade(SQLiteDatabase bookmarksDatabase, int oldVersion, int newVersion) {
65         // Code for upgrading the database will be added here when the schema version > 1.
66     }
67
68     public void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, String parentFolder, byte[] favoriteIcon) {
69         // We need to store the bookmark data in a `ContentValues`.
70         ContentValues bookmarkContentValues = new ContentValues();
71
72         // ID is created automatically.
73         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
74         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
75         bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
76         bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
77         bookmarkContentValues.put(IS_FOLDER, false);
78         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
79
80         // Get a writable database handle.
81         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
82
83         // Insert a new row.  The second argument is `null`, which makes it so that a completely null row cannot be created.
84         bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
85
86         // Close the database handle.
87         bookmarksDatabase.close();
88     }
89
90     public void createFolder(String folderName, int displayOrder, String parentFolder, byte[] favoriteIcon) {
91         ContentValues bookmarkContentValues = new ContentValues();
92
93         // ID is created automatically.
94         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
95         bookmarkContentValues.put(BOOKMARK_NAME, folderName);
96         bookmarkContentValues.put(PARENT_FOLDER, parentFolder);
97         bookmarkContentValues.put(IS_FOLDER, true);
98         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
99
100         // Get a writable database handle.
101         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
102
103         // The second argument is `null`, which makes it so that completely null rows cannot be created.  Not a problem in our case.
104         bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
105
106         // Close the database handle.
107         bookmarksDatabase.close();
108     }
109
110     public Cursor getBookmarkCursor(int databaseId) {
111         // Get a readable database handle.
112         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
113
114         // Prepare the SQL statement to get the `Cursor` for `databaseId`
115         final String GET_ONE_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE +
116                 " WHERE " + _ID + " = " + databaseId;
117
118         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  We can't close the `Cursor` because we need to use it in the parent activity.
119         return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
120     }
121
122     public String getFolderName (int databaseId) {
123         // Get a readable database handle.
124         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
125
126         // Prepare the SQL statement to get the `Cursor` for the folder.
127         final String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
128                 " WHERE " + _ID + " = " + databaseId;
129
130         // Get `folderCursor`.  The second argument is `null` because there are no `selectionArgs`.
131         Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null);
132
133         // Get `folderName`.
134         folderCursor.moveToFirst();
135         String folderName = folderCursor.getString(folderCursor.getColumnIndex(BOOKMARK_NAME));
136
137         // Close the cursor and the database handle.
138         folderCursor.close();
139         bookmarksDatabase.close();
140
141         // Return the folder name.
142         return folderName;
143     }
144
145     public Cursor getFolderCursor(String folderName) {
146         // Get a readable database handle.
147         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
148
149         // SQL escape `folderName`.
150         folderName = DatabaseUtils.sqlEscapeString(folderName);
151
152         // Prepare the SQL statement to get the `Cursor` for the folder.
153         final String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
154                 " WHERE " + BOOKMARK_NAME + " = " + folderName +
155                 " AND " + IS_FOLDER + " = " + 1;
156
157         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.
158         // We can't close the `Cursor` because we need to use it in the parent activity.
159         return bookmarksDatabase.rawQuery(GET_FOLDER, null);
160     }
161
162     public Cursor getFoldersCursorExcept(String exceptFolders) {
163         // Get a readable database handle.
164         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
165
166         // Prepare the SQL statement to get the `Cursor` for the folders.
167         final String GET_FOLDERS_EXCEPT = "SELECT * FROM " + BOOKMARKS_TABLE +
168                 " WHERE " + IS_FOLDER + " = " + 1 +
169                 " AND " + BOOKMARK_NAME + " NOT IN (" + exceptFolders +
170                 ") ORDER BY " + BOOKMARK_NAME + " ASC";
171
172         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.
173         // We can't close the `Cursor` because we need to use it in the parent activity.
174         return bookmarksDatabase.rawQuery(GET_FOLDERS_EXCEPT, null);
175     }
176
177     public Cursor getSubfoldersCursor(String currentFolder) {
178         // Get a readable database handle.
179         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
180
181         // SQL escape `currentFolder.
182         currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
183
184         // Prepare the SQL statement to get the `Cursor` for the subfolders.
185         final String GET_SUBFOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE +
186                 " WHERE " + PARENT_FOLDER + " = " + currentFolder +
187                 " AND " + IS_FOLDER + " = " + 1;
188
189         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.
190         // We can't close the `Cursor` because we need to use it in the parent activity.
191         return bookmarksDatabase.rawQuery(GET_SUBFOLDERS, null);
192     }
193
194     public String getParentFolder(String currentFolder) {
195         // Get a readable database handle.
196         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
197
198         // SQL escape `currentFolder`.
199         currentFolder = DatabaseUtils.sqlEscapeString(currentFolder);
200
201         // Prepare the SQL statement to get the parent folder.
202         final String GET_PARENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
203                 " WHERE " + IS_FOLDER + " = " + 1 +
204                 " AND " + BOOKMARK_NAME + " = " + currentFolder;
205
206         // The second argument is `null` because there are no `selectionArgs`.
207         Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null);
208         bookmarkCursor.moveToFirst();
209
210         // Store the name of the parent folder.
211         String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
212
213         // Close the `Cursor`.
214         bookmarkCursor.close();
215
216         return parentFolder;
217     }
218
219     public Cursor getAllFoldersCursor() {
220         // Get a readable database handle.
221         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
222
223         // Prepare the SQL statement to get the `Cursor` for all the folders.
224         final String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE +
225                 " WHERE " + IS_FOLDER + " = " + 1;
226
227         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.
228         // We can't close the `Cursor` because we need to use it in the parent activity.
229         return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null);
230     }
231
232     public Cursor getAllBookmarksCursor() {
233         // Get a readable database handle.
234         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
235
236         // Get everything in `BOOKMARKS_TABLE`.
237         final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE;
238
239         // Return the results as a Cursor.  The second argument is `null` because there are no selectionArgs.
240         // We can't close the Cursor because we need to use it in the parent activity.
241         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
242     }
243
244     public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) {
245         // Get a readable database handle.
246         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
247
248         // SQL escape `folderName`.
249         folderName = DatabaseUtils.sqlEscapeString(folderName);
250
251         // Get everything in the `BOOKMARKS_TABLE` with `folderName` as the `PARENT_FOLDER`.
252         final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE +
253                 " WHERE " + PARENT_FOLDER + " = " + folderName +
254                 " ORDER BY " + DISPLAY_ORDER + " ASC";
255
256         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  We can't close the `Cursor` because we need to use it in the parent activity.
257         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
258     }
259
260     public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) {
261         // Get a readable database handle.
262         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
263
264         // Prepare a string that contains the comma-separated list of IDs not to get.
265         String doNotGetIdsString = "";
266         // Extract the array to `doNotGetIdsString`.
267         for (long databaseIdLong : exceptIdLongArray) {
268             // If this is the first number, only add the number.
269             if (doNotGetIdsString.isEmpty()) {
270                 doNotGetIdsString = String.valueOf(databaseIdLong);
271             } else {  // If there already is a number in the string, place a `,` before the number.
272                 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
273             }
274         }
275
276         // SQL escape `folderName`.
277         folderName = DatabaseUtils.sqlEscapeString(folderName);
278
279         // Prepare the SQL statement to select all items except those with the specified IDs.
280         final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE +
281                 " WHERE " + PARENT_FOLDER + " = " + folderName +
282                 " AND " + _ID + " NOT IN (" + doNotGetIdsString +
283                 ") ORDER BY " + DISPLAY_ORDER + " ASC";
284
285         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.
286         // We can't close the `Cursor` because we need to use it in the parent activity.
287         return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
288     }
289
290     public boolean isFolder(int databaseId) {
291         // Get a readable database handle.
292         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
293
294         // Prepare the SQL statement to determine if `databaseId` is a folder.
295         final String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
296                 " WHERE " + _ID + " = " + databaseId;
297
298         // Populate folderCursor.  The second argument is `null` because there are no `selectionArgs`.
299         Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null);
300
301         // Ascertain if this database ID is a folder.
302         folderCursor.moveToFirst();
303         boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
304
305         // Close the `Cursor` and the database handle.
306         folderCursor.close();
307         bookmarksDatabase.close();
308
309         return isFolder;
310     }
311
312     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
313         // Store the updated values in `bookmarkContentValues`.
314         ContentValues bookmarkContentValues = new ContentValues();
315
316         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
317         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
318
319         // Get a writable database handle.
320         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
321
322         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
323         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
324
325         // Close the database handle.
326         bookmarksDatabase.close();
327     }
328
329     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
330         // Store the updated values in `bookmarkContentValues`.
331         ContentValues bookmarkContentValues = new ContentValues();
332
333         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
334         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
335         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
336
337         // Get a writable database handle.
338         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
339
340         // Update the bookmark.  The last argument is `null` because there are no `whereArgs`.
341         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
342
343         // Close the database handle.
344         bookmarksDatabase.close();
345     }
346
347     public void updateFolder(int databaseId, String oldFolderName, String newFolderName) {
348         // Get a writable database handle.
349         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
350
351         // Update the folder first.  Store the updated values in `folderContentValues`.
352         ContentValues folderContentValues = new ContentValues();
353
354         folderContentValues.put(BOOKMARK_NAME, newFolderName);
355
356         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
357         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
358
359
360         // Update the bookmarks inside the folder with the new parent folder name.
361         ContentValues bookmarkContentValues = new ContentValues();
362
363         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
364
365         // SQL escape `oldFolderName`.
366         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
367
368         // Run the update on the bookmarks.  The last argument is `null` because there are no `whereArgs`.
369         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
370
371         // Close the database handle.
372         bookmarksDatabase.close();
373     }
374
375     public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) {
376         // Get a writable database handle.
377         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
378
379         // Update the folder first.  Store the updated values in `folderContentValues`.
380         ContentValues folderContentValues = new ContentValues();
381
382         folderContentValues.put(BOOKMARK_NAME, newFolderName);
383         folderContentValues.put(FAVORITE_ICON, folderIcon);
384
385         // Run the update on the folder.  The last argument is `null` because there are no `whereArgs`.
386         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null);
387
388
389         // Update the bookmarks inside the folder with the new parent folder name.
390         ContentValues bookmarkContentValues = new ContentValues();
391
392         bookmarkContentValues.put(PARENT_FOLDER, newFolderName);
393
394         // SQL escape `oldFolderName`.
395         oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName);
396
397         // Run the update on the bookmarks.  The last argument is `null` because there are no `whereArgs`.
398         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null);
399
400         // Close the database handle.
401         bookmarksDatabase.close();
402     }
403
404     public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
405         // Get a writable database handle.
406         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
407
408         // Store the new display order in `bookmarkContentValues`.
409         ContentValues bookmarkContentValues = new ContentValues();
410         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
411
412         // Update the database.  The last argument is `null` because there are no `whereArgs`.
413         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
414
415         // Close the database handle.
416         bookmarksDatabase.close();
417     }
418
419     public void moveToFolder(int databaseId, String newFolder) {
420         // Get a writable database handle.
421         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
422
423         // Get the highest `DISPLAY_ORDER` in the new folder
424         String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder);
425         final String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
426                 " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped +
427                 " ORDER BY " + DISPLAY_ORDER + " ASC";
428         // The second argument is `null` because there are no `selectionArgs`.
429         Cursor newFolderCursor = bookmarksDatabase.rawQuery(NEW_FOLDER, null);
430         int displayOrder;
431         if (newFolderCursor.getCount() > 0) {
432             newFolderCursor.moveToLast();
433             displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
434         } else {
435             displayOrder = 0;
436         }
437         newFolderCursor.close();
438
439         // Store the new values in `bookmarkContentValues`.
440         ContentValues bookmarkContentValues = new ContentValues();
441         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
442         bookmarkContentValues.put(PARENT_FOLDER, newFolder);
443
444         // Update the database.  The last argument is `null` because there are no `whereArgs`.
445         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
446
447         // Close the database handle.
448         bookmarksDatabase.close();
449     }
450
451     public void deleteBookmark(int databaseId) {
452         // Get a writable database handle.
453         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
454
455         // Deletes the row with the given `databaseId`.  The last argument is `null` because we don't need additional parameters.
456         bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
457
458         // Close the database handle.
459         bookmarksDatabase.close();
460     }
461 }