2 * Copyright © 2017-2019 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.content.SharedPreferences;
25 import android.database.Cursor;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
28 import android.preference.PreferenceManager;
30 public class DomainsDatabaseHelper extends SQLiteOpenHelper {
31 private static final int SCHEMA_VERSION = 10;
32 static final String DOMAINS_DATABASE = "domains.db";
33 static final String DOMAINS_TABLE = "domains";
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 ENABLE_ULTRAPRIVACY = "enableultraprivacy";
47 public static final String BLOCK_ALL_THIRD_PARTY_REQUESTS = "blockallthirdpartyrequests";
48 public static final String USER_AGENT = "useragent";
49 public static final String FONT_SIZE = "fontsize";
50 public static final String SWIPE_TO_REFRESH = "swipetorefresh";
51 public static final String NIGHT_MODE = "nightmode";
52 public static final String WIDE_VIEWPORT = "wide_viewport";
53 public static final String DISPLAY_IMAGES = "displayimages";
54 public static final String PINNED_SSL_CERTIFICATE = "pinnedsslcertificate";
55 public static final String SSL_ISSUED_TO_COMMON_NAME = "sslissuedtocommonname";
56 public static final String SSL_ISSUED_TO_ORGANIZATION = "sslissuedtoorganization";
57 public static final String SSL_ISSUED_TO_ORGANIZATIONAL_UNIT = "sslissuedtoorganizationalunit";
58 public static final String SSL_ISSUED_BY_COMMON_NAME = "sslissuedbycommonname";
59 public static final String SSL_ISSUED_BY_ORGANIZATION = "sslissuedbyorganization";
60 public static final String SSL_ISSUED_BY_ORGANIZATIONAL_UNIT = "sslissuedbyorganizationalunit";
61 public static final String SSL_START_DATE = "sslstartdate";
62 public static final String SSL_END_DATE = "sslenddate";
63 public static final String PINNED_IP_ADDRESSES = "pinned_ip_addresses";
64 public static final String IP_ADDRESSES = "ip_addresses";
67 public static final int SYSTEM_DEFAULT = 0;
68 public static final int ENABLED = 1;
69 public static final int DISABLED = 2;
71 static final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
72 _ID + " INTEGER PRIMARY KEY, " +
73 DOMAIN_NAME + " TEXT, " +
74 ENABLE_JAVASCRIPT + " BOOLEAN, " +
75 ENABLE_FIRST_PARTY_COOKIES + " BOOLEAN, " +
76 ENABLE_THIRD_PARTY_COOKIES + " BOOLEAN, " +
77 ENABLE_DOM_STORAGE + " BOOLEAN, " +
78 ENABLE_FORM_DATA + " BOOLEAN, " +
79 ENABLE_EASYLIST + " BOOLEAN, " +
80 ENABLE_EASYPRIVACY + " BOOLEAN, " +
81 ENABLE_FANBOYS_ANNOYANCE_LIST + " BOOLEAN, " +
82 ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " BOOLEAN, " +
83 ENABLE_ULTRAPRIVACY + " BOOLEAN, " +
84 BLOCK_ALL_THIRD_PARTY_REQUESTS + " BOOLEAN, " +
85 USER_AGENT + " TEXT, " +
86 FONT_SIZE + " INTEGER, " +
87 SWIPE_TO_REFRESH + " INTEGER, " +
88 NIGHT_MODE + " INTEGER, " +
89 WIDE_VIEWPORT + " INTEGER, " +
90 DISPLAY_IMAGES + " INTEGER, " +
91 PINNED_SSL_CERTIFICATE + " BOOLEAN, " +
92 SSL_ISSUED_TO_COMMON_NAME + " TEXT, " +
93 SSL_ISSUED_TO_ORGANIZATION + " TEXT, " +
94 SSL_ISSUED_TO_ORGANIZATIONAL_UNIT + " TEXT, " +
95 SSL_ISSUED_BY_COMMON_NAME + " TEXT, " +
96 SSL_ISSUED_BY_ORGANIZATION + " TEXT, " +
97 SSL_ISSUED_BY_ORGANIZATIONAL_UNIT + " TEXT, " +
98 SSL_START_DATE + " INTEGER, " +
99 SSL_END_DATE + " INTEGER, " +
100 PINNED_IP_ADDRESSES + " BOOLEAN, " +
101 IP_ADDRESSES + " TEXT)";
103 private final Context appContext;
105 // Initialize the database. The lint warnings for the unused parameters are suppressed.
106 public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
107 super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
109 // Store a handle for the context.
110 appContext = context;
114 public void onCreate(SQLiteDatabase domainsDatabase) {
115 // Create the domains table.
116 domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
120 public void onUpgrade(SQLiteDatabase domainsDatabase, int oldVersion, int newVersion) {
121 // Upgrade the database table.
122 switch (oldVersion) {
123 // Upgrade from schema version 1.
125 // Add the display images column.
126 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DISPLAY_IMAGES + " INTEGER");
128 // Upgrade from schema version 2.
130 // Add the SSL certificate columns.
131 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + PINNED_SSL_CERTIFICATE + " BOOLEAN");
132 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_TO_COMMON_NAME + " TEXT");
133 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_TO_ORGANIZATION + " TEXT");
134 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_TO_ORGANIZATIONAL_UNIT + " TEXT");
135 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_BY_COMMON_NAME + " TEXT");
136 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_BY_ORGANIZATION + " TEXT");
137 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_ISSUED_BY_ORGANIZATIONAL_UNIT + " TEXT");
138 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_START_DATE + " INTEGER");
139 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SSL_END_DATE + " INTEGER");
141 // Upgrade from schema version 3.
143 // Add the Night Mode column.
144 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + NIGHT_MODE + " INTEGER");
146 // Upgrade from schema version 4.
148 // Add the block lists columns.
149 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_EASYLIST + " BOOLEAN");
150 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_EASYPRIVACY + " BOOLEAN");
151 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_FANBOYS_ANNOYANCE_LIST + " BOOLEAN");
152 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " BOOLEAN");
154 // Get a handle for the shared preference.
155 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
157 // Get the default block list settings.
158 boolean easyListEnabled = sharedPreferences.getBoolean("easylist", true);
159 boolean easyPrivacyEnabled = sharedPreferences.getBoolean("easyprivacy", true);
160 boolean fanboyAnnoyanceListEnabled = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
161 boolean fanboySocialBlockingListEnabled = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
163 // Set EasyList for existing rows according to the current system-wide default.
164 if (easyListEnabled) {
165 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYLIST + " = " + 1);
167 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYLIST + " = " + 0);
170 // Set EasyPrivacy for existing rows according to the current system-wide default.
171 if (easyPrivacyEnabled) {
172 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYPRIVACY + " = " + 1);
174 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_EASYPRIVACY + " = " + 0);
177 // Set Fanboy's Annoyance List for existing rows according to the current system-wide default.
178 if (fanboyAnnoyanceListEnabled) {
179 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_ANNOYANCE_LIST + " = " + 1);
181 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_ANNOYANCE_LIST + " = " + 0);
184 // Set Fanboy's Social Blocking List for existing rows according to the current system-wide default.
185 if (fanboySocialBlockingListEnabled) {
186 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " = " + 1);
188 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST + " = " + 0);
191 // Upgrade from schema version 5.
193 // Add the swipe to refresh column.
194 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + SWIPE_TO_REFRESH + " INTEGER");
196 // Upgrade from schema version 6.
198 // Add the block all third-party requests column.
199 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + BLOCK_ALL_THIRD_PARTY_REQUESTS + " BOOLEAN");
201 // Upgrade from schema version 7.
203 // Add the UltraPrivacy column.
204 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ENABLE_ULTRAPRIVACY + " BOOLEAN");
206 // Enable it for all existing rows.
207 domainsDatabase.execSQL("UPDATE " + DOMAINS_TABLE + " SET " + ENABLE_ULTRAPRIVACY + " = " + 1);
209 // Upgrade from schema version 8.
211 // Add the Pinned IP Addresses columns.
212 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + PINNED_IP_ADDRESSES + " BOOLEAN");
213 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + IP_ADDRESSES + " TEXT");
215 // Upgrade from schema version 9.
217 // Add the Wide Viewport column.
218 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + WIDE_VIEWPORT + " INTEGER");
222 Cursor getCompleteCursorOrderedByDomain() {
223 // Get a readable database handle.
224 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
226 // Return everything in the domains table ordered by the domain name. The second argument is `null` because there are no `selectionArgs`.
227 return domainsDatabase.rawQuery("SELECT * FROM " + DOMAINS_TABLE + " ORDER BY " + DOMAIN_NAME + " ASC", null);
230 public Cursor getDomainNameCursorOrderedByDomain() {
231 // Get a readable database handle.
232 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
234 // Get everything in the domains table ordered by the domain name.
235 String GET_CURSOR_ORDERED_BY_DOMAIN = "SELECT " + _ID + ", " + DOMAIN_NAME +
236 " FROM " + DOMAINS_TABLE +
237 " ORDER BY " + DOMAIN_NAME + " ASC";
239 // 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.
240 return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN, null);
243 public Cursor getDomainNameCursorOrderedByDomainExcept(int databaseId) {
244 // Get a readable database handle.
245 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
247 // Prepare the SQL statement to select all rows except that with `databaseId`.
248 String GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT = "SELECT " + _ID + ", " + DOMAIN_NAME +
249 " FROM " + DOMAINS_TABLE +
250 " WHERE " + _ID + " IS NOT " + databaseId +
251 " ORDER BY " + DOMAIN_NAME + " ASC";
253 // 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.
254 return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT, null);
257 public Cursor getCursorForId(int databaseId) {
258 // Get a readable database handle.
259 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
261 // Prepare the SQL statement to get the `Cursor` for `databaseId`.
262 String GET_CURSOR_FOR_ID = "SELECT * FROM " + DOMAINS_TABLE +
263 " WHERE " + _ID + " = " + databaseId;
265 // 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.
266 return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
269 public Cursor getCursorForDomainName(String domainName) {
270 // Get a readable database handle.
271 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
273 // Return a cursor for the requested domain name.
274 return domainsDatabase.query(DOMAINS_TABLE, null, DOMAIN_NAME + " = " + "\"" + domainName + "\"", null, null, null, null);
278 public int addDomain(String domainName) {
279 // Store the domain data in a `ContentValues`.
280 ContentValues domainContentValues = new ContentValues();
282 // Get a handle for the shared preference.
283 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
285 // Get the default settings.
286 boolean javaScriptEnabled = sharedPreferences.getBoolean("javascript", false);
287 boolean firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies", false);
288 boolean thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies", false);
289 boolean domStorageEnabled = sharedPreferences.getBoolean("dom_storage", false);
290 boolean saveFormDataEnabled = sharedPreferences.getBoolean("save_form_data", false); // Form data can be removed once the minimum API >= 26.
291 boolean easyListEnabled = sharedPreferences.getBoolean("easylist", true);
292 boolean easyPrivacyEnabled = sharedPreferences.getBoolean("easyprivacy", true);
293 boolean fanboyAnnoyanceListEnabled = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
294 boolean fanboySocialBlockingListEnabled = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
295 boolean ultraPrivacyEnabled = sharedPreferences.getBoolean("ultraprivacy", true);
296 boolean blockAllThirdPartyRequests = sharedPreferences.getBoolean("block_all_third_party_requests", false);
298 // Create entries for the database fields. The ID is created automatically. The pinned SSL certificate information is not created unless added by the user.
299 domainContentValues.put(DOMAIN_NAME, domainName);
300 domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
301 domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
302 domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
303 domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
304 domainContentValues.put(ENABLE_FORM_DATA, saveFormDataEnabled); // Form data can be removed once the minimum API >= 26.
305 domainContentValues.put(ENABLE_EASYLIST, easyListEnabled);
306 domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacyEnabled);
307 domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboyAnnoyanceListEnabled);
308 domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboySocialBlockingListEnabled);
309 domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacyEnabled);
310 domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests);
311 domainContentValues.put(USER_AGENT, "System default user agent");
312 domainContentValues.put(FONT_SIZE, 0);
313 domainContentValues.put(SWIPE_TO_REFRESH, 0);
314 domainContentValues.put(NIGHT_MODE, 0);
315 domainContentValues.put(WIDE_VIEWPORT, 0);
316 domainContentValues.put(DISPLAY_IMAGES, 0);
318 // Get a writable database handle.
319 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
321 // 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.
322 int newDomainDatabaseId = (int) domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
324 // Close the database handle.
325 domainsDatabase.close();
327 // Return the new domain database ID.
328 return newDomainDatabaseId;
331 void addDomain(ContentValues contentValues) {
332 // Get a writable database handle.
333 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
335 // Add the new domain.
336 domainsDatabase.insert(DOMAINS_TABLE, null, contentValues);
338 // Close the database handle.
339 domainsDatabase.close();
342 public void updateDomain(int databaseId, String domainName, boolean javaScriptEnabled, boolean firstPartyCookiesEnabled, boolean thirdPartyCookiesEnabled, boolean domStorageEnabled, boolean formDataEnabled,
343 boolean easyListEnabled, boolean easyPrivacyEnabled, boolean fanboysAnnoyanceEnabled, boolean fanboysSocialBlockingEnabled, boolean ultraPrivacyEnabled,
344 boolean blockAllThirdPartyRequests, String userAgent, int fontSize, int swipeToRefresh, int nightMode, int wideViewport, int displayImages, boolean pinnedSslCertificate,
345 boolean pinnedIpAddresses) {
347 // Store the domain data in a `ContentValues`.
348 ContentValues domainContentValues = new ContentValues();
350 // Add entries for each field in the database.
351 domainContentValues.put(DOMAIN_NAME, domainName);
352 domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
353 domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
354 domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
355 domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
356 domainContentValues.put(ENABLE_FORM_DATA, formDataEnabled); // Form data can be removed once the minimum API >= 26.
357 domainContentValues.put(ENABLE_EASYLIST, easyListEnabled);
358 domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacyEnabled);
359 domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboysAnnoyanceEnabled);
360 domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboysSocialBlockingEnabled);
361 domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacyEnabled);
362 domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests);
363 domainContentValues.put(USER_AGENT, userAgent);
364 domainContentValues.put(FONT_SIZE, fontSize);
365 domainContentValues.put(SWIPE_TO_REFRESH, swipeToRefresh);
366 domainContentValues.put(NIGHT_MODE, nightMode);
367 domainContentValues.put(WIDE_VIEWPORT, wideViewport);
368 domainContentValues.put(DISPLAY_IMAGES, displayImages);
369 domainContentValues.put(PINNED_SSL_CERTIFICATE, pinnedSslCertificate);
370 domainContentValues.put(PINNED_IP_ADDRESSES, pinnedIpAddresses);
372 // Get a writable database handle.
373 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
375 // Update the row for `databaseId`. The last argument is `null` because there are no `whereArgs`.
376 domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
378 // Close the database handle.
379 domainsDatabase.close();
382 public void updatePinnedSslCertificate(int databaseId, String sslIssuedToCommonName, String sslIssuedToOrganization, String sslIssuedToOrganizationalUnit, String sslIssuedByCommonName,
383 String sslIssuedByOrganization, String sslIssuedByOrganizationalUnit, long sslStartDate, long sslEndDate) {
385 // Store the pinned SSL certificate in a content values.
386 ContentValues pinnedSslCertificateContentValues = new ContentValues();
388 // Add entries for each field in the certificate.
389 pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_COMMON_NAME, sslIssuedToCommonName);
390 pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_ORGANIZATION, sslIssuedToOrganization);
391 pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, sslIssuedToOrganizationalUnit);
392 pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_COMMON_NAME, sslIssuedByCommonName);
393 pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_ORGANIZATION, sslIssuedByOrganization);
394 pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, sslIssuedByOrganizationalUnit);
395 pinnedSslCertificateContentValues.put(SSL_START_DATE, sslStartDate);
396 pinnedSslCertificateContentValues.put(SSL_END_DATE, sslEndDate);
398 // Get a writable database handle.
399 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
401 // Update the row for database ID.
402 domainsDatabase.update(DOMAINS_TABLE, pinnedSslCertificateContentValues, _ID + " = " + databaseId, null);
404 // Close the database handle.
405 domainsDatabase.close();
408 public void updatePinnedIpAddresses(int databaseId, String ipAddresses) {
409 // Store the pinned IP addresses in a content values.
410 ContentValues pinnedIpAddressesContentValues = new ContentValues();
412 // Add the IP addresses to the content values.
413 pinnedIpAddressesContentValues.put(IP_ADDRESSES, ipAddresses);
415 // Get a writable database handle.
416 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
418 // Update the row for the database ID.
419 domainsDatabase.update(DOMAINS_TABLE, pinnedIpAddressesContentValues, _ID + " = " + databaseId, null);
421 // Close the database handle.
422 domainsDatabase.close();
425 public void deleteDomain(int databaseId) {
426 // Get a writable database handle.
427 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
429 // Delete the row for `databaseId`. The last argument is `null` because we don't need additional parameters.
430 domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
432 // Close the database handle.
433 domainsDatabase.close();