]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java
5a5a025a16aae46bb03fd25cc23bf67c81df9d77
[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 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";
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                 DISPLAY_ORDER + " integer, " +
51                 BOOKMARK_NAME + " text, " +
52                 BOOKMARK_URL + " text, " +
53                 PARENT_FOLDER + " text, " +
54                 IS_FOLDER + " boolean, " +
55                 FAVORITE_ICON + " 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, int displayOrder, byte[] favoriteIcon) {
66         ContentValues bookmarkContentValues = new ContentValues();
67
68         // ID is created automatically.
69         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
70         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
71         bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL);
72         bookmarkContentValues.put(PARENT_FOLDER, "");
73         bookmarkContentValues.put(IS_FOLDER, false);
74         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
75
76         // Get a writable database handle.
77         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
78
79         // The second argument is "null", which makes it so that completely null rows cannot be created.  Not a problem in our case.
80         bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues);
81
82         // Close the database handle.
83         bookmarksDatabase.close();
84     }
85
86     public Cursor getBookmarkCursor(int databaseId) {
87         // Get a readable database handle.
88         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
89
90         // Prepare the SQL statement to get the cursor for `databaseId`
91         final String GET_ONE_BOOKMARK = "Select * FROM " + BOOKMARKS_TABLE +
92                 " WHERE " + _ID + " = " + databaseId;
93
94         // Return the results as a `Cursor`.  The second argument is `null` because there are no selectionArgs.
95         // We can't close the `Cursor` because we need to use it in the parent activity.
96         return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null);
97     }
98
99     public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) {
100         // Get a readable database handle.
101         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
102
103         // Prepare a string that contains the comma-separated list of IDs not to get.
104         String doNotGetIdsString = "";
105         // Extract the array to `doNotGetIdsString`.
106         for (long databaseIdLong : exceptIdLongArray) {
107             // If this is the first number, only add the number.
108             if (doNotGetIdsString.isEmpty()) {
109                 doNotGetIdsString = String.valueOf(databaseIdLong);
110             } else {  // If there already is a number in the string, place a `,` before the number.
111                 doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong;
112             }
113         }
114
115         // Prepare the SQL statement to select all items except those with the specified IDs.
116         final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE +
117                 " WHERE " + _ID + " NOT IN (" + doNotGetIdsString + ") ORDER BY " + DISPLAY_ORDER + " ASC";
118
119         // Return the results as a `Cursor`.  The second argument is `null` because there are no selectionArgs.
120         // We can't close the `Cursor` because we need to use it in the parent activity.
121         return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null);
122     }
123
124     public Cursor getAllBookmarksCursor() {
125         // Get a readable database handle.
126         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
127
128         // Get everything in the BOOKMARKS_TABLE.
129         final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE + " ORDER BY " + DISPLAY_ORDER + " ASC";
130
131         // Return the results as a Cursor.  The second argument is `null` because there are no selectionArgs.
132         // We can't close the Cursor because we need to use it in the parent activity.
133         return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null);
134     }
135
136     public String getBookmarkURL(int databaseId) {
137         // Get a readable database handle.
138         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
139
140         // Prepare the SQL statement to get the row for the selected databaseId.
141         final String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE +
142                 " WHERE " + _ID + " = " + databaseId;
143
144         // Save the results as Cursor and move it to the first (only) row.  The second argument is "null" because there are no selectionArgs.
145         Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null);
146         bookmarksCursor.moveToFirst();
147
148         // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL.
149         int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL);
150         String bookmarkURLString = bookmarksCursor.getString(urlColumnInt);
151
152         // Close the Cursor and the database handle.
153         bookmarksCursor.close();
154         bookmarksDatabase.close();
155
156         // Return the bookmarkURLString.
157         return bookmarkURLString;
158     }
159
160     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) {
161         // Store the updated values in `bookmarkContentValues`.
162         ContentValues bookmarkContentValues = new ContentValues();
163
164         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
165         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
166
167         // Get a writable database handle.
168         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
169
170         // Update the database.  The last argument is `null` because there are no `whereArgs`.
171         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
172
173         // Close the database handle.
174         bookmarksDatabase.close();
175     }
176
177     public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl, byte[] favoriteIcon) {
178         // Store the updated values in `bookmarkContentValues`.
179         ContentValues bookmarkContentValues = new ContentValues();
180
181         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName);
182         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl);
183         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon);
184
185         // Get a writable database handle.
186         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
187
188         // Update the database.  The last argument is `null` because there are no `whereArgs`.
189         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
190
191         // Close the database handle.
192         bookmarksDatabase.close();
193     }
194
195     public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) {
196         // Store the updated values in `bookmarkContentValues`.
197         ContentValues bookmarkContentValues = new ContentValues();
198
199         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
200
201         // Get a writable database handle.
202         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
203
204         // Update the database.  The last argument is `null` because there are no `whereArgs`.
205         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
206
207         // Close the database handle.
208         bookmarksDatabase.close();
209     }
210
211     public void deleteBookmark(int databaseId) {
212         // Get a writable database handle.
213         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
214
215         // Deletes the row with the given databaseId.  The last argument is null because we don't need additional parameters.
216         bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
217
218         // Close the database handle.
219         bookmarksDatabase.close();
220     }
221 }