]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.java
0e1448b4bd53a234c2762580b663c6ca9a714c71
[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 calling 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 get 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 calling activity.
107         return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
108     }
109
110     public Cursor getCursorForDomainName(String domainName) {
111         // Get a readable database handle.
112         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
113
114         // Prepare the SQL statement to get the `Cursor` for `domainName`.
115         final String GET_CURSOR_FOR_DOMAIN_NAME = "SELECT * FROM " + DOMAINS_TABLE +
116                 " WHERE " + DOMAIN_NAME + " = " + "\"" + domainName + "\"";
117
118         // 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 us it in the calling activity.
119         return domainsDatabase.rawQuery(GET_CURSOR_FOR_DOMAIN_NAME, null);
120     }
121
122     public void addDomain(String domainName) {
123         // Store the domain data in a `ContentValues`.
124         ContentValues domainContentValues = new ContentValues();
125
126         // Create entries for each field in the database.  The ID is created automatically.
127         domainContentValues.put(DOMAIN_NAME, domainName);
128         domainContentValues.put(ENABLE_JAVASCRIPT, false);
129         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, false);
130         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, false);
131         domainContentValues.put(ENABLE_DOM_STORAGE, false);
132         domainContentValues.put(ENABLE_FORM_DATA, false);
133         domainContentValues.put(USER_AGENT, "PrivacyBrowser/1.0");
134         domainContentValues.put(FONT_SIZE, "100");
135
136         // Get a writable database handle.
137         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
138
139         // Insert a new row.  The second argument is `null`, which makes it so that a completely null row cannot be created.
140         domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
141
142         // Close the database handle.
143         domainsDatabase.close();
144     }
145
146     public void saveDomain(int databaseId, String domainName, boolean javaScriptEnabled, boolean firstPartyCookiesEnabled, boolean thirdPartyCookiesEnabled, boolean domStorageEnabled, boolean formDataEnabled, String userAgent, int fontSize) {
147         // Store the domain data in a `ContentValues`.
148         ContentValues domainContentValues = new ContentValues();
149
150         // Add entries for each field in the database.
151         domainContentValues.put(DOMAIN_NAME, domainName);
152         domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
153         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
154         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
155         domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
156         domainContentValues.put(ENABLE_FORM_DATA, formDataEnabled);
157         domainContentValues.put(USER_AGENT, userAgent);
158         domainContentValues.put(FONT_SIZE, fontSize);
159
160         // Get a writable database handle.
161         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
162
163         // Update the row for `databaseId`.  The last argument is `null` because there are no `whereArgs`.
164         domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
165
166         // Close the database handle.
167         domainsDatabase.close();
168     }
169
170     public void deleteDomain(int databaseId) {
171         // Get a writable database handle.
172         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
173
174         // Delete the row for `databaseId`.  The last argument is `null` because we don't need additional parameters.
175         domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
176
177         // Close the database handle.
178         domainsDatabase.close();
179     }
180 }