]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.java
Create UltraList. https://redmine.stoutner.com/issues/450
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / DomainsDatabaseHelper.java
1 /*
2  * Copyright © 2017-2019 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.content.SharedPreferences;
25 import android.database.Cursor;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
28 import android.preference.PreferenceManager;
29
30 public class DomainsDatabaseHelper extends SQLiteOpenHelper {
31     private static final int SCHEMA_VERSION = 11;
32     static final String DOMAINS_DATABASE = "domains.db";
33     static final String DOMAINS_TABLE = "domains";
34
35     public static final String _ID = "_id";
36     public static final String DOMAIN_NAME = "domainname";
37     public static final String ENABLE_JAVASCRIPT = "enablejavascript";
38     public static final String ENABLE_FIRST_PARTY_COOKIES = "enablefirstpartycookies";
39     public static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
40     public static final String ENABLE_DOM_STORAGE = "enabledomstorage";
41     public static final String ENABLE_FORM_DATA = "enableformdata";  // Form data can be removed once the minimum API >= 26.
42     public static final String ENABLE_EASYLIST = "enableeasylist";
43     public static final String ENABLE_EASYPRIVACY = "enableeasyprivacy";
44     public static final String ENABLE_FANBOYS_ANNOYANCE_LIST = "enablefanboysannoyancelist";
45     public static final String ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST = "enablefanboyssocialblockinglist";
46     public static final String ULTRALIST = "ultralist";
47     public static final String ENABLE_ULTRAPRIVACY = "enableultraprivacy";
48     public static final String BLOCK_ALL_THIRD_PARTY_REQUESTS = "blockallthirdpartyrequests";
49     public static final String USER_AGENT = "useragent";
50     public static final String FONT_SIZE = "fontsize";
51     public static final String SWIPE_TO_REFRESH = "swipetorefresh";
52     public static final String NIGHT_MODE = "nightmode";
53     public static final String WIDE_VIEWPORT = "wide_viewport";
54     public static final String DISPLAY_IMAGES = "displayimages";
55     public static final String PINNED_SSL_CERTIFICATE = "pinnedsslcertificate";
56     public static final String SSL_ISSUED_TO_COMMON_NAME = "sslissuedtocommonname";
57     public static final String SSL_ISSUED_TO_ORGANIZATION = "sslissuedtoorganization";
58     public static final String SSL_ISSUED_TO_ORGANIZATIONAL_UNIT = "sslissuedtoorganizationalunit";
59     public static final String SSL_ISSUED_BY_COMMON_NAME = "sslissuedbycommonname";
60     public static final String SSL_ISSUED_BY_ORGANIZATION = "sslissuedbyorganization";
61     public static final String SSL_ISSUED_BY_ORGANIZATIONAL_UNIT = "sslissuedbyorganizationalunit";
62     public static final String SSL_START_DATE = "sslstartdate";
63     public static final String SSL_END_DATE = "sslenddate";
64     public static final String PINNED_IP_ADDRESSES = "pinned_ip_addresses";
65     public static final String IP_ADDRESSES = "ip_addresses";
66
67     // Spinner constants.
68     public static final int SYSTEM_DEFAULT = 0;
69     public static final int ENABLED = 1;
70     public static final int DISABLED = 2;
71
72     static final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
73             _ID + " INTEGER PRIMARY KEY, " +
74             DOMAIN_NAME + " TEXT, " +
75             ENABLE_JAVASCRIPT + " BOOLEAN, " +
76             ENABLE_FIRST_PARTY_COOKIES + " BOOLEAN, " +
77             ENABLE_THIRD_PARTY_COOKIES + " BOOLEAN, " +
78             ENABLE_DOM_STORAGE + " BOOLEAN, " +
79             ENABLE_FORM_DATA + " BOOLEAN, " +
80             ENABLE_EASYLIST + " BOOLEAN, " +
81             ENABLE_EASYPRIVACY + " BOOLEAN, " +
82             ENABLE_FANBOYS_ANNOYANCE_LIST + " BOOLEAN, " +
83             ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " BOOLEAN, " +
84             ULTRALIST + " BOOLEAN, " +
85             ENABLE_ULTRAPRIVACY + " BOOLEAN, " +
86             BLOCK_ALL_THIRD_PARTY_REQUESTS + " BOOLEAN, " +
87             USER_AGENT + " TEXT, " +
88             FONT_SIZE + " INTEGER, " +
89             SWIPE_TO_REFRESH + " INTEGER, " +
90             NIGHT_MODE + " INTEGER, " +
91             WIDE_VIEWPORT + " INTEGER, " +
92             DISPLAY_IMAGES + " INTEGER, " +
93             PINNED_SSL_CERTIFICATE + " BOOLEAN, " +
94             SSL_ISSUED_TO_COMMON_NAME + " TEXT, " +
95             SSL_ISSUED_TO_ORGANIZATION + " TEXT, " +
96             SSL_ISSUED_TO_ORGANIZATIONAL_UNIT + " TEXT, " +
97             SSL_ISSUED_BY_COMMON_NAME + " TEXT, " +
98             SSL_ISSUED_BY_ORGANIZATION + " TEXT, " +
99             SSL_ISSUED_BY_ORGANIZATIONAL_UNIT + " TEXT, " +
100             SSL_START_DATE + " INTEGER, " +
101             SSL_END_DATE + " INTEGER, " +
102             PINNED_IP_ADDRESSES + " BOOLEAN, " +
103             IP_ADDRESSES + " TEXT)";
104
105     private final Context appContext;
106
107     // Initialize the database.  The lint warnings for the unused parameters are suppressed.
108     public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
109         super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
110
111         // Store a handle for the context.
112         appContext = context;
113     }
114
115     @Override
116     public void onCreate(SQLiteDatabase domainsDatabase) {
117         // Create the domains table.
118         domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
119     }
120
121     @Override
122     public void onUpgrade(SQLiteDatabase domainsDatabase, int oldVersion, int newVersion) {
123         // Upgrade the database table.
124         switch (oldVersion) {
125             // Upgrade from schema version 1.
126             case 1:
127                 // Add the display images column.
128                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DISPLAY_IMAGES + " INTEGER");
129
130             // Upgrade from schema version 2.
131             case 2:
132                 //  Add the SSL certificate columns.
133                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + PINNED_SSL_CERTIFICATE + " BOOLEAN");
134                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_TO_COMMON_NAME + " TEXT");
135                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_TO_ORGANIZATION + " TEXT");
136                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_TO_ORGANIZATIONAL_UNIT + " TEXT");
137                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_BY_COMMON_NAME + " TEXT");
138                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_BY_ORGANIZATION + " TEXT");
139                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_BY_ORGANIZATIONAL_UNIT + " TEXT");
140                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_START_DATE + " INTEGER");
141                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_END_DATE + " INTEGER");
142
143             // Upgrade from schema version 3.
144             case 3:
145                 // Add the Night Mode column.
146                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + NIGHT_MODE + " INTEGER");
147
148             // Upgrade from schema version 4.
149             case 4:
150                 // Add the block lists columns.
151                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_EASYLIST + " BOOLEAN");
152                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_EASYPRIVACY + " BOOLEAN");
153                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_FANBOYS_ANNOYANCE_LIST + " BOOLEAN");
154                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " BOOLEAN");
155
156                 // Get a handle for the shared preference.
157                 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
158
159                 // Get the default block list settings.
160                 boolean easyListEnabled = sharedPreferences.getBoolean("easylist", true);
161                 boolean easyPrivacyEnabled = sharedPreferences.getBoolean("easyprivacy", true);
162                 boolean fanboyAnnoyanceListEnabled = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
163                 boolean fanboySocialBlockingListEnabled = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
164
165                 // Set EasyList for existing rows according to the current system-wide default.
166                 if (easyListEnabled) {
167                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYLIST + " = " + 1);
168                 } else {
169                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYLIST + " = " + 0);
170                 }
171
172                 // Set EasyPrivacy for existing rows according to the current system-wide default.
173                 if (easyPrivacyEnabled) {
174                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYPRIVACY + " = " + 1);
175                 } else {
176                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYPRIVACY + " = " + 0);
177                 }
178
179                 // Set Fanboy's Annoyance List for existing rows according to the current system-wide default.
180                 if (fanboyAnnoyanceListEnabled) {
181                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_ANNOYANCE_LIST + " = " + 1);
182                 } else {
183                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_ANNOYANCE_LIST + " = " + 0);
184                 }
185
186                 // Set Fanboy's Social Blocking List for existing rows according to the current system-wide default.
187                 if (fanboySocialBlockingListEnabled) {
188                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " = " + 1);
189                 } else {
190                     domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " = " + 0);
191                 }
192
193             // Upgrade from schema version 5.
194             case 5:
195                 // Add the swipe to refresh column.
196                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SWIPE_TO_REFRESH + " INTEGER");
197
198             // Upgrade from schema version 6.
199             case 6:
200                 // Add the block all third-party requests column.
201                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + BLOCK_ALL_THIRD_PARTY_REQUESTS + " BOOLEAN");
202
203             // Upgrade from schema version 7.
204             case 7:
205                 // Add the UltraPrivacy column.
206                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_ULTRAPRIVACY + " BOOLEAN");
207
208                 // Enable it for all existing rows.
209                 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_ULTRAPRIVACY + " = " + 1);
210
211             // Upgrade from schema version 8.
212             case 8:
213                 // Add the Pinned IP Addresses columns.
214                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + PINNED_IP_ADDRESSES + " BOOLEAN");
215                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + IP_ADDRESSES + " TEXT");
216
217             // Upgrade from schema version 9.
218             case 9:
219                 // Add the Wide Viewport column.
220                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + WIDE_VIEWPORT + " INTEGER");
221
222             // Upgrade from schema version 10.
223             case 10:
224                 // Add the UltraList column.
225                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ULTRALIST + " BOOLEAN");
226
227                 // Enable it for all existing rows.
228                 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ULTRALIST + " = " + 1);
229         }
230     }
231
232     Cursor getCompleteCursorOrderedByDomain() {
233         // Get a readable database handle.
234         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
235
236         // Return everything in the domains table ordered by the domain name.  The second argument is `null` because there are no `selectionArgs`.
237         return domainsDatabase.rawQuery("SELECT * FROM " + DOMAINS_TABLE + " ORDER BY " + DOMAIN_NAME + " ASC", null);
238     }
239
240     public Cursor getDomainNameCursorOrderedByDomain() {
241         // Get a readable database handle.
242         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
243
244         // Get everything in the domains table ordered by the domain name.
245         String GET_CURSOR_ORDERED_BY_DOMAIN = "SELECT " + _ID + ", " + DOMAIN_NAME +
246                 " FROM " + DOMAINS_TABLE +
247                 " ORDER BY " + DOMAIN_NAME + " ASC";
248
249         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  The cursor can't be closed because it is needed in the calling activity.
250         return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN, null);
251     }
252
253     public Cursor getDomainNameCursorOrderedByDomainExcept(int databaseId) {
254         // Get a readable database handle.
255         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
256
257         // Prepare the SQL statement to select all rows except that with `databaseId`.
258         String GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT = "SELECT " + _ID + ", " + DOMAIN_NAME +
259                 " FROM " + DOMAINS_TABLE +
260                 " WHERE " + _ID + " IS NOT " + databaseId +
261                 " ORDER BY " + DOMAIN_NAME + " ASC";
262
263         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  The cursor can't be closed because it is needed in the calling activity.
264         return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT, null);
265     }
266
267     public Cursor getCursorForId(int databaseId) {
268         // Get a readable database handle.
269         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
270
271         // Prepare the SQL statement to get the `Cursor` for `databaseId`.
272         String GET_CURSOR_FOR_ID = "SELECT * FROM " + DOMAINS_TABLE +
273                 " WHERE " + _ID + " = " + databaseId;
274
275         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  The cursor can't be closed because it is needed in the calling activity.
276         return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
277     }
278
279     public Cursor getCursorForDomainName(String domainName) {
280         // Get a readable database handle.
281         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
282
283         // Return a cursor for the requested domain name.
284         return domainsDatabase.query(DOMAINS_TABLE, null, DOMAIN_NAME + " = " + "\"" + domainName + "\"", null, null, null, null);
285
286     }
287
288     public int addDomain(String domainName) {
289         // Store the domain data in a `ContentValues`.
290         ContentValues domainContentValues = new ContentValues();
291
292         // Get a handle for the shared preference.
293         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
294
295         // Get the default settings.
296         boolean javaScript = sharedPreferences.getBoolean("javascript", false);
297         boolean firstPartyCookies = sharedPreferences.getBoolean("first_party_cookies", false);
298         boolean thirdPartyCookies = sharedPreferences.getBoolean("third_party_cookies", false);
299         boolean domStorage = sharedPreferences.getBoolean("dom_storage", false);
300         boolean saveFormData = sharedPreferences.getBoolean("save_form_data", false);  // Form data can be removed once the minimum API >= 26.
301         boolean easyList = sharedPreferences.getBoolean("easylist", true);
302         boolean easyPrivacy = sharedPreferences.getBoolean("easyprivacy", true);
303         boolean fanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
304         boolean fanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
305         boolean ultraList = sharedPreferences.getBoolean("ultralist", true);
306         boolean ultraPrivacy = sharedPreferences.getBoolean("ultraprivacy", true);
307         boolean blockAllThirdPartyRequests = sharedPreferences.getBoolean("block_all_third_party_requests", false);
308
309         // Create entries for the database fields.  The ID is created automatically.  The pinned SSL certificate information is not created unless added by the user.
310         domainContentValues.put(DOMAIN_NAME, domainName);
311         domainContentValues.put(ENABLE_JAVASCRIPT, javaScript);
312         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookies);
313         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookies);
314         domainContentValues.put(ENABLE_DOM_STORAGE, domStorage);
315         domainContentValues.put(ENABLE_FORM_DATA, saveFormData);  // Form data can be removed once the minimum API >= 26.
316         domainContentValues.put(ENABLE_EASYLIST, easyList);
317         domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacy);
318         domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboyAnnoyanceList);
319         domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboySocialBlockingList);
320         domainContentValues.put(ULTRALIST, ultraList);
321         domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacy);
322         domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests);
323         domainContentValues.put(USER_AGENT, "System default user agent");
324         domainContentValues.put(FONT_SIZE, 0);
325         domainContentValues.put(SWIPE_TO_REFRESH, 0);
326         domainContentValues.put(NIGHT_MODE, 0);
327         domainContentValues.put(WIDE_VIEWPORT, 0);
328         domainContentValues.put(DISPLAY_IMAGES, 0);
329
330         // Get a writable database handle.
331         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
332
333         // Insert a new row and store the resulting database ID.  The second argument is `null`, which makes it so that a completely null row cannot be created.
334         int newDomainDatabaseId  = (int) domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
335
336         // Close the database handle.
337         domainsDatabase.close();
338
339         // Return the new domain database ID.
340         return newDomainDatabaseId;
341     }
342
343     void addDomain(ContentValues contentValues) {
344         // Get a writable database handle.
345         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
346
347         // Add the new domain.
348         domainsDatabase.insert(DOMAINS_TABLE, null, contentValues);
349
350         // Close the database handle.
351         domainsDatabase.close();
352     }
353
354     public void updateDomain(int databaseId, String domainName, boolean javaScript, boolean firstPartyCookies, boolean thirdPartyCookies, boolean domStorage, boolean formData, boolean easyList,
355                              boolean easyPrivacy, boolean fanboysAnnoyance, boolean fanboysSocialBlocking, boolean ultraList, boolean ultraPrivacy, boolean blockAllThirdPartyRequests, String userAgent,
356                              int fontSize, int swipeToRefresh, int nightMode, int wideViewport, int displayImages, boolean pinnedSslCertificate, boolean pinnedIpAddresses) {
357
358         // Store the domain data in a `ContentValues`.
359         ContentValues domainContentValues = new ContentValues();
360
361         // Add entries for each field in the database.
362         domainContentValues.put(DOMAIN_NAME, domainName);
363         domainContentValues.put(ENABLE_JAVASCRIPT, javaScript);
364         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookies);
365         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookies);
366         domainContentValues.put(ENABLE_DOM_STORAGE, domStorage);
367         domainContentValues.put(ENABLE_FORM_DATA, formData);  // Form data can be removed once the minimum API >= 26.
368         domainContentValues.put(ENABLE_EASYLIST, easyList);
369         domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacy);
370         domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboysAnnoyance);
371         domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboysSocialBlocking);
372         domainContentValues.put(ULTRALIST, ultraList);
373         domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacy);
374         domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests);
375         domainContentValues.put(USER_AGENT, userAgent);
376         domainContentValues.put(FONT_SIZE, fontSize);
377         domainContentValues.put(SWIPE_TO_REFRESH, swipeToRefresh);
378         domainContentValues.put(NIGHT_MODE, nightMode);
379         domainContentValues.put(WIDE_VIEWPORT, wideViewport);
380         domainContentValues.put(DISPLAY_IMAGES, displayImages);
381         domainContentValues.put(PINNED_SSL_CERTIFICATE, pinnedSslCertificate);
382         domainContentValues.put(PINNED_IP_ADDRESSES, pinnedIpAddresses);
383
384         // Get a writable database handle.
385         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
386
387         // Update the row for `databaseId`.  The last argument is `null` because there are no `whereArgs`.
388         domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
389
390         // Close the database handle.
391         domainsDatabase.close();
392     }
393
394     public void updatePinnedSslCertificate(int databaseId, String sslIssuedToCommonName, String sslIssuedToOrganization, String sslIssuedToOrganizationalUnit, String sslIssuedByCommonName,
395                                      String sslIssuedByOrganization, String sslIssuedByOrganizationalUnit, long sslStartDate, long sslEndDate) {
396
397         // Store the pinned SSL certificate in a content values.
398         ContentValues pinnedSslCertificateContentValues = new ContentValues();
399
400         // Add entries for each field in the certificate.
401         pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_COMMON_NAME, sslIssuedToCommonName);
402         pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_ORGANIZATION, sslIssuedToOrganization);
403         pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, sslIssuedToOrganizationalUnit);
404         pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_COMMON_NAME, sslIssuedByCommonName);
405         pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_ORGANIZATION, sslIssuedByOrganization);
406         pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, sslIssuedByOrganizationalUnit);
407         pinnedSslCertificateContentValues.put(SSL_START_DATE, sslStartDate);
408         pinnedSslCertificateContentValues.put(SSL_END_DATE, sslEndDate);
409
410         // Get a writable database handle.
411         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
412
413         // Update the row for database ID.
414         domainsDatabase.update(DOMAINS_TABLE, pinnedSslCertificateContentValues, _ID + " = " + databaseId, null);
415
416         // Close the database handle.
417         domainsDatabase.close();
418     }
419
420     public void updatePinnedIpAddresses(int databaseId, String ipAddresses) {
421         // Store the pinned IP addresses in a content values.
422         ContentValues pinnedIpAddressesContentValues = new ContentValues();
423
424         // Add the IP addresses to the content values.
425         pinnedIpAddressesContentValues.put(IP_ADDRESSES, ipAddresses);
426
427         // Get a writable database handle.
428         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
429
430         // Update the row for the database ID.
431         domainsDatabase.update(DOMAINS_TABLE, pinnedIpAddressesContentValues, _ID + " = " + databaseId, null);
432
433         // Close the database handle.
434         domainsDatabase.close();
435     }
436
437     public void deleteDomain(int databaseId) {
438         // Get a writable database handle.
439         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
440
441         // Delete the row for `databaseId`.  The last argument is `null` because we don't need additional parameters.
442         domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
443
444         // Close the database handle.
445         domainsDatabase.close();
446     }
447 }