]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.java
Add domains `Snackbars` and ghosting of switches.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / DomainsDatabaseHelper.java
1 /*
2  * Copyright 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.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteOpenHelper;
27
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
33     public 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";
42
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);
46     }
47
48     @Override
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);";
61
62         // Create the `domains` table if it doesn't exist.
63         domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
64     }
65
66     @Override
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.
69     }
70
71     public Cursor getDomainNameCursorOrderedByDomain() {
72         // Get a readable database handle.
73         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
74
75         // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN_NAME`.
76         final String GET_CURSOR_ORDERED_BY_DOMAIN = "SELECT " + _ID + ", " + DOMAIN_NAME +
77                 " FROM " + DOMAINS_TABLE +
78                 " ORDER BY " + DOMAIN_NAME + " ASC";
79
80         // 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.
81         return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN, null);
82     }
83
84     public Cursor getDomainNameCursorOrderedByDomainExcept(int databaseId) {
85         // Get a readable database handle.
86         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
87
88         // Prepare the SQL statement to select all rows except that with `databaseId`.
89         final String GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT = "SELECT " + _ID + ", " + DOMAIN_NAME +
90                 " FROM " + DOMAINS_TABLE +
91                 " WHERE " + _ID + " IS NOT " + databaseId +
92                 " ORDER BY " + DOMAIN_NAME + " ASC";
93
94         // 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.
95         return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT, null);
96     }
97
98     public Cursor getCursorForId(int databaseId) {
99         // Get a readable database handle.
100         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
101
102         // Prepare the SQL statement to ge the `Cursor` for `databaseId`.
103         final String GET_CURSOR_FOR_ID = "Select * FROM " + DOMAINS_TABLE +
104                 " WHERE " + _ID + " = " + databaseId;
105
106         // 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.
107         return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
108     }
109
110     public void addDomain(String domainName) {
111         // Store the domain data in a `ContentValues`.
112         ContentValues domainContentValues = new ContentValues();
113
114         // Create entries for each field in the database.  The ID is created automatically.
115         domainContentValues.put(DOMAIN_NAME, domainName);
116         domainContentValues.put(ENABLE_JAVASCRIPT, false);
117         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, false);
118         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, false);
119         domainContentValues.put(ENABLE_DOM_STORAGE, false);
120         domainContentValues.put(ENABLE_FORM_DATA, false);
121         domainContentValues.put(USER_AGENT, "PrivacyBrowser/1.0");
122         domainContentValues.put(FONT_SIZE, "100");
123
124         // Get a writable database handle.
125         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
126
127         // Insert a new row.  The second argument is `null`, which makes it so that a completely null row cannot be created.
128         domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
129
130         // Close the database handle.
131         domainsDatabase.close();
132     }
133
134     public void saveDomain(int databaseId, String domainName, boolean javaScriptEnabled, boolean firstPartyCookiesEnabled, boolean thirdPartyCookiesEnabled, boolean domStorageEnabled, boolean formDataEnabled, String userAgent, int fontSize) {
135         // Store the domain data in a `ContentValues`.
136         ContentValues domainContentValues = new ContentValues();
137
138         // Add entries for each field in the database.
139         domainContentValues.put(DOMAIN_NAME, domainName);
140         domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
141         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
142         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
143         domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
144         domainContentValues.put(ENABLE_FORM_DATA, formDataEnabled);
145         domainContentValues.put(USER_AGENT, userAgent);
146         domainContentValues.put(FONT_SIZE, fontSize);
147
148         // Get a writable database handle.
149         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
150
151         // Update the row for `databaseId`.  The last argument is `null` because there are no `whereArgs`.
152         domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
153
154         // Close the database handle.
155         domainsDatabase.close();
156     }
157
158     public void deleteDomain(int databaseId) {
159         // Get a writable database handle.
160         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
161
162         // Delete the row for `databaseId`.  The last argument is `null` because we don't need additional parameters.
163         domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
164
165         // Close the database handle.
166         domainsDatabase.close();
167     }
168 }