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 = "domain";
35 public static final String FONT_SIZE = "fontsize";
37 private static final String ENABLE_JAVASCRIPT = "enablejavascript";
38 private static final String ENABLE_FIRST_PARTY_COOKIES = "enablefirstpartycookies";
39 private static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
40 private static final String ENABLE_DOM_STORAGE = "enabledomstorage";
41 private static final String ENABLE_FORM_DATA = "enableformdata";
42 private static final String USER_AGENT_NAME = "useragentname";
43 private static final String USER_AGENT_STRING = "useragentstring";
44 private static final String CUSTOM_USER_AGENT_STRING = "customuseragent";
46 // Initialize the database. The lint warnings for the unused parameters are suppressed.
47 public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
48 super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
52 public void onCreate(SQLiteDatabase domainsDatabase) {
53 // Setup the SQL string to create the `domains` table.
54 final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
55 _ID + " integer primary key, " +
57 ENABLE_JAVASCRIPT + " boolean, " +
58 ENABLE_FIRST_PARTY_COOKIES + " boolean, " +
59 ENABLE_THIRD_PARTY_COOKIES + " boolean, " +
60 ENABLE_DOM_STORAGE + " boolean, " +
61 ENABLE_FORM_DATA + " boolean, " +
62 USER_AGENT_NAME + " text, " +
63 USER_AGENT_STRING + " text, " +
64 CUSTOM_USER_AGENT_STRING + " text, " +
65 FONT_SIZE + " integer);";
67 // Create the `domains` table if it doesn't exist.
68 domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
72 public void onUpgrade(SQLiteDatabase domainsDatabase, int oldVersion, int newVersion) {
73 // Code for upgrading the database will be added here when the schema version > 1.
76 public Cursor getCursorOrderedByDomain() {
77 // Get a readable database handle.
78 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
80 // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN`.
81 final String GET_CURSOR_SORTED_BY_DOMAIN = "Select * FROM " + DOMAINS_TABLE +
82 " ORDER BY " + DOMAIN + " ASC";
84 // 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.
85 return domainsDatabase.rawQuery(GET_CURSOR_SORTED_BY_DOMAIN, null);
88 public Cursor getCursorForId(int databaseId) {
89 // Get a readable database handle.
90 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
92 // Prepare the SQL statement to ge the `Cursor` for `databaseId`.
93 final String GET_CURSOR_FOR_ID = "Select * FROM " + DOMAINS_TABLE +
94 " WHERE " + _ID + " = " + databaseId;
96 // 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.
97 return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
100 public void addDomain(String domainName) {
101 // We need to store the domain data in a `ContentValues`.
102 ContentValues domainContentValues = new ContentValues();
104 // Create entries for each field in the database. The ID is created automatically.
105 domainContentValues.put(DOMAIN, domainName);
106 domainContentValues.put(ENABLE_JAVASCRIPT, false);
107 domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, false);
108 domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, false);
109 domainContentValues.put(ENABLE_DOM_STORAGE, false);
110 domainContentValues.put(ENABLE_FORM_DATA, false);
111 domainContentValues.put(USER_AGENT_NAME, "Privacy Browser 1.0");
112 domainContentValues.put(USER_AGENT_STRING, "PrivacyBrowser/1.0");
113 domainContentValues.put(CUSTOM_USER_AGENT_STRING, "PrivacyBrowser/1.0");
114 domainContentValues.put(FONT_SIZE, "100");
116 // Get a writable database handle.
117 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
119 // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created.
120 domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
122 // Close the database handle.
123 domainsDatabase.close();