]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java
45344d3bd93c70edb7027dd90b97af1481f5919b
[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         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 String getBookmarkURL(int databaseID) {
98         // Get a readable database handle.
99         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
100
101         // Get the row for the selected databaseID.
102         String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE +
103                 " WHERE " + ID + " = " + databaseID;
104
105         // Save the results as Cursor and move it to the first (only) row.  The second argument is "null" because there are no selectionArgs.
106         Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null);
107         bookmarksCursor.moveToFirst();
108
109         // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL.
110         int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL);
111         String bookmarkURLString = bookmarksCursor.getString(urlColumnInt);
112
113         // Close the Cursor and the database handle.
114         bookmarksCursor.close();
115         bookmarksDatabase.close();
116
117         // Return the bookmarkURLString.
118         return bookmarkURLString;
119     }
120 }