2 * Copyright 2017 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.helpers;
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;
28 public class DomainsDatabaseHelper extends SQLiteOpenHelper {
29 private static final int SCHEMA_VERSION = 1;
30 private static final String DOMAINS_DATABASE = "domains.db";
31 private static final String DOMAINS_TABLE = "domains";
32 private static final String _ID = "_id";
34 public static final String DOMAIN_NAME = "domainname";
35 public static final String ENABLE_JAVASCRIPT = "enablejavascript";
36 public static final String ENABLE_FIRST_PARTY_COOKIES = "enablefirstpartycookies";
37 public static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
38 public static final String ENABLE_DOM_STORAGE = "enabledomstorage";
39 public static final String ENABLE_FORM_DATA = "enableformdata";
40 public static final String USER_AGENT = "useragent";
41 public static final String FONT_SIZE = "fontsize";
43 // Initialize the database. The lint warnings for the unused parameters are suppressed.
44 public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
45 super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
49 public void onCreate(SQLiteDatabase domainsDatabase) {
50 // Setup the SQL string to create the `domains` table.
51 final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
52 _ID + " integer primary key, " +
53 DOMAIN_NAME + " text, " +
54 ENABLE_JAVASCRIPT + " boolean, " +
55 ENABLE_FIRST_PARTY_COOKIES + " boolean, " +
56 ENABLE_THIRD_PARTY_COOKIES + " boolean, " +
57 ENABLE_DOM_STORAGE + " boolean, " +
58 ENABLE_FORM_DATA + " boolean, " +
59 USER_AGENT + " text, " +
60 FONT_SIZE + " integer);";
62 // Create the `domains` table if it doesn't exist.
63 domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
67 public void onUpgrade(SQLiteDatabase domainsDatabase, int oldVersion, int newVersion) {
68 // Code for upgrading the database will be added here when the schema version > 1.
71 public Cursor getCursorOrderedByDomain() {
72 // Get a readable database handle.
73 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
75 // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN_NAME`.
76 final String GET_CURSOR_SORTED_BY_DOMAIN = "Select * FROM " + DOMAINS_TABLE +
77 " ORDER BY " + DOMAIN_NAME + " ASC";
79 // 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.
80 return domainsDatabase.rawQuery(GET_CURSOR_SORTED_BY_DOMAIN, null);
83 public Cursor getCursorForId(int databaseId) {
84 // Get a readable database handle.
85 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
87 // Prepare the SQL statement to ge the `Cursor` for `databaseId`.
88 final String GET_CURSOR_FOR_ID = "Select * FROM " + DOMAINS_TABLE +
89 " WHERE " + _ID + " = " + databaseId;
91 // 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.
92 return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
95 public void addDomain(String domainName) {
96 // Store the domain data in a `ContentValues`.
97 ContentValues domainContentValues = new ContentValues();
99 // Create entries for each field in the database. The ID is created automatically.
100 domainContentValues.put(DOMAIN_NAME, domainName);
101 domainContentValues.put(ENABLE_JAVASCRIPT, false);
102 domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, false);
103 domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, false);
104 domainContentValues.put(ENABLE_DOM_STORAGE, false);
105 domainContentValues.put(ENABLE_FORM_DATA, false);
106 domainContentValues.put(USER_AGENT, "PrivacyBrowser/1.0");
107 domainContentValues.put(FONT_SIZE, "100");
109 // Get a writable database handle.
110 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
112 // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created.
113 domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
115 // Close the database handle.
116 domainsDatabase.close();
119 public void saveDomain(int databaseId, String domainName, boolean javaScriptEnabled, boolean firstPartyCookiesEnabled, boolean thirdPartyCookiesEnabled, boolean domStorageEnabled, boolean formDataEnabled, String userAgent, int fontSize) {
120 // Store the domain data in a `ContentValues`.
121 ContentValues domainContentValues = new ContentValues();
123 // Add entries for each field in the database.
124 domainContentValues.put(DOMAIN_NAME, domainName);
125 domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
126 domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
127 domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
128 domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
129 domainContentValues.put(ENABLE_FORM_DATA, formDataEnabled);
130 domainContentValues.put(USER_AGENT, userAgent);
131 domainContentValues.put(FONT_SIZE, fontSize);
133 // Get a writable database handle.
134 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
136 // Update the row for `databaseId`. The last argument is `null` because there are no `whereArgs`.
137 domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
139 // Close the database handle.
140 domainsDatabase.close();
143 public void deleteDomain(int databaseId) {
144 // Get a writable database handle.
145 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
147 // Delete the row for `databaseId`. The last argument is `null` because we don't need additional parameters.
148 domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
150 // Close the database handle.
151 domainsDatabase.close();