]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.kt
Add WebView DevTools to the navigation menu. https://redmine.stoutner.com/issues/893
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / DomainsDatabaseHelper.kt
1 /*
2  * Copyright © 2017-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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 Android 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 Android.  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.DatabaseUtils
26 import android.database.sqlite.SQLiteDatabase
27 import android.database.sqlite.SQLiteOpenHelper
28
29 import androidx.preference.PreferenceManager
30
31 import com.stoutner.privacybrowser.R
32
33 // Define the class constants.
34 private const val SCHEMA_VERSION = 15
35
36 class DomainsDatabaseHelper(private val appContext: Context) : SQLiteOpenHelper(appContext, DOMAINS_DATABASE, null, SCHEMA_VERSION) {
37     // Define the public companion object constants.  These can be moved to public class constants once the entire project has migrated to Kotlin.
38     companion object {
39         // Define the public database constants.
40         const val DOMAINS_DATABASE = "domains.db"
41         const val DOMAINS_TABLE = "domains"
42
43         // Define the public spinner constants.
44         const val SYSTEM_DEFAULT = 0
45         const val ENABLED = 1
46         const val DISABLED = 2
47         const val LIGHT_THEME = 1
48         const val DARK_THEME = 2
49
50         // Define the public schema constants.
51         const val ID = "_id"
52         const val DOMAIN_NAME = "domainname"
53         const val ENABLE_JAVASCRIPT = "enablejavascript"
54         const val COOKIES = "cookies"
55         const val ENABLE_DOM_STORAGE = "enabledomstorage"
56         const val ENABLE_FORM_DATA = "enableformdata" // Form data can be removed once the minimum API >= 26.
57         const val ENABLE_EASYLIST = "enableeasylist"
58         const val ENABLE_EASYPRIVACY = "enableeasyprivacy"
59         const val ENABLE_FANBOYS_ANNOYANCE_LIST = "enablefanboysannoyancelist"
60         const val ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST = "enablefanboyssocialblockinglist"
61         const val ULTRALIST = "ultralist"
62         const val ENABLE_ULTRAPRIVACY = "enableultraprivacy"
63         const val BLOCK_ALL_THIRD_PARTY_REQUESTS = "blockallthirdpartyrequests"
64         const val USER_AGENT = "useragent"
65         const val FONT_SIZE = "fontsize"
66         const val SWIPE_TO_REFRESH = "swipetorefresh"
67         const val WEBVIEW_THEME = "webview_theme"
68         const val WIDE_VIEWPORT = "wide_viewport"
69         const val DISPLAY_IMAGES = "displayimages"
70         const val PINNED_SSL_CERTIFICATE = "pinnedsslcertificate"
71         const val SSL_ISSUED_TO_COMMON_NAME = "sslissuedtocommonname"
72         const val SSL_ISSUED_TO_ORGANIZATION = "sslissuedtoorganization"
73         const val SSL_ISSUED_TO_ORGANIZATIONAL_UNIT = "sslissuedtoorganizationalunit"
74         const val SSL_ISSUED_BY_COMMON_NAME = "sslissuedbycommonname"
75         const val SSL_ISSUED_BY_ORGANIZATION = "sslissuedbyorganization"
76         const val SSL_ISSUED_BY_ORGANIZATIONAL_UNIT = "sslissuedbyorganizationalunit"
77         const val SSL_START_DATE = "sslstartdate"
78         const val SSL_END_DATE = "sslenddate"
79         const val PINNED_IP_ADDRESSES = "pinned_ip_addresses"
80         const val IP_ADDRESSES = "ip_addresses"
81
82         // Define the public table creation constant.
83         const val CREATE_DOMAINS_TABLE = "CREATE TABLE $DOMAINS_TABLE (" +
84                 "$ID INTEGER PRIMARY KEY, " +
85                 "$DOMAIN_NAME TEXT, " +
86                 "$ENABLE_JAVASCRIPT BOOLEAN, " +
87                 "$COOKIES BOOLEAN, " +
88                 "$ENABLE_DOM_STORAGE BOOLEAN, " +
89                 "$ENABLE_FORM_DATA BOOLEAN, " +
90                 "$ENABLE_EASYLIST BOOLEAN, " +
91                 "$ENABLE_EASYPRIVACY BOOLEAN, " +
92                 "$ENABLE_FANBOYS_ANNOYANCE_LIST BOOLEAN, " +
93                 "$ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST BOOLEAN, " +
94                 "$ULTRALIST BOOLEAN, " +
95                 "$ENABLE_ULTRAPRIVACY BOOLEAN, " +
96                 "$BLOCK_ALL_THIRD_PARTY_REQUESTS BOOLEAN, " +
97                 "$USER_AGENT TEXT, " +
98                 "$FONT_SIZE INTEGER, " +
99                 "$SWIPE_TO_REFRESH INTEGER, " +
100                 "$WEBVIEW_THEME INTEGER, " +
101                 "$WIDE_VIEWPORT INTEGER, " +
102                 "$DISPLAY_IMAGES INTEGER, " +
103                 "$PINNED_SSL_CERTIFICATE BOOLEAN, " +
104                 "$SSL_ISSUED_TO_COMMON_NAME TEXT, " +
105                 "$SSL_ISSUED_TO_ORGANIZATION TEXT, " +
106                 "$SSL_ISSUED_TO_ORGANIZATIONAL_UNIT TEXT, " +
107                 "$SSL_ISSUED_BY_COMMON_NAME TEXT, " +
108                 "$SSL_ISSUED_BY_ORGANIZATION TEXT, " +
109                 "$SSL_ISSUED_BY_ORGANIZATIONAL_UNIT TEXT, " +
110                 "$SSL_START_DATE INTEGER, " +
111                 "$SSL_END_DATE INTEGER, " +
112                 "$PINNED_IP_ADDRESSES BOOLEAN, " +
113                 "$IP_ADDRESSES TEXT)"
114     }
115
116     override fun onCreate(domainsDatabase: SQLiteDatabase) {
117         // Create the domains table.
118         domainsDatabase.execSQL(CREATE_DOMAINS_TABLE)
119     }
120
121     override fun onUpgrade(domainsDatabase: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
122         // Upgrade from schema version 1, first used in Privacy Browser 2.0, to schema version 2, first used in Privacy Browser 2.3.
123         if (oldVersion < 2) {
124             // Add the display images column.
125             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $DISPLAY_IMAGES INTEGER")
126         }
127
128         // Upgrade from schema version 2, first used in Privacy Browser 2.3, to schema version 3, first used in Privacy Browser 2.5.
129         if (oldVersion < 3) {
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")
140         }
141
142         // Upgrade from schema version 3, first used in Privacy Browser 2.5, to schema version 4, first used in Privacy Browser 2.6.
143         if (oldVersion < 4) {
144             // Add the night mode column.
145             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN nightmode INTEGER")
146         }
147
148         // Upgrade from schema version 4, first used in Privacy Browser 2.6, to schema version 5, first used in Privacy Browser 2.9.
149         if (oldVersion < 5) {
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             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext)
158
159             // Get the default block list settings.
160             val easyListEnabled = sharedPreferences.getBoolean("easylist", true)
161             val easyPrivacyEnabled = sharedPreferences.getBoolean("easyprivacy", true)
162             val fanboyAnnoyanceListEnabled = sharedPreferences.getBoolean("fanboys_annoyance_list", true)
163             val fanboySocialBlockingListEnabled = sharedPreferences.getBoolean("fanboys_social_blocking_list", true)
164
165             // Set EasyList for existing rows according to the current system-wide default.
166             // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
167             // <https://developer.android.com/reference/android/database/sqlite/package-summary>
168             if (easyListEnabled) {
169                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYLIST = 1")
170             } else {
171                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYLIST = 0")
172             }
173
174             // Set EasyPrivacy for existing rows according to the current system-wide default.
175             if (easyPrivacyEnabled) {
176                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYPRIVACY = 1")
177             } else {
178                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYPRIVACY = 0")
179             }
180
181             // Set Fanboy's Annoyance List for existing rows according to the current system-wide default.
182             if (fanboyAnnoyanceListEnabled) {
183                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_ANNOYANCE_LIST = 1")
184             } else {
185                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_ANNOYANCE_LIST = 0")
186             }
187
188             // Set Fanboy's Social Blocking List for existing rows according to the current system-wide default.
189             if (fanboySocialBlockingListEnabled) {
190                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST = 1")
191             } else {
192                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST = 0")
193             }
194         }
195
196         // Upgrade from schema version 5, first used in Privacy Browser 2.9, to schema version 6, first used in Privacy Browser 2.11.
197         if (oldVersion < 6) {
198             // Add the swipe to refresh column.  This defaults to `0`, which is `System default`, so a separate step isn't needed to populate the column.
199             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $SWIPE_TO_REFRESH INTEGER")
200         }
201
202         // Upgrade from schema version 6, first used in Privacy Browser 2.11, to schema version 7, first used in Privacy Browser 2.12.
203         if (oldVersion < 7) {
204             // Add the block all third-party requests column.  This defaults to `0`, which is off, so a separate step isn't needed to populate the column.
205             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $BLOCK_ALL_THIRD_PARTY_REQUESTS BOOLEAN")
206         }
207
208         // Upgrade from schema version 7, first used in Privacy Browser 2.12, to schema version 8, first used in Privacy Browser 2.12.
209         // For some reason (lack of planning or attention to detail), the 2.12 update included two schema version jumps.
210         if (oldVersion < 8) {
211             // Add the UltraPrivacy column.
212             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ENABLE_ULTRAPRIVACY BOOLEAN")
213
214             // Enable it for all existing rows.
215             domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_ULTRAPRIVACY = 1")
216         }
217
218         // Upgrade from schema version 8, first used in Privacy Browser 2.12, to schema version 9, first used in Privacy Browser 2.16.
219         if (oldVersion < 9) {
220             // Add the pinned IP addresses columns.  These default to `0` and `""`, so a separate step isn't needed to populate the columns.
221             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $PINNED_IP_ADDRESSES BOOLEAN")
222             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $IP_ADDRESSES TEXT")
223         }
224
225         // Upgrade from schema version 9, first used in Privacy Browser 2.16, to schema version 10, first used in Privacy Browser 3.1.
226         if (oldVersion < 10) {
227             // Add the wide viewport column.  This defaults to `0`, which is `System default`, so a separate step isn't needed to populate the column.
228             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $WIDE_VIEWPORT INTEGER")
229         }
230
231         // Upgrade from schema version 10, first used in Privacy Browser 3.1, to schema version 11, first used in Privacy Browser 3.2.
232         if (oldVersion < 11) {
233             // Add the UltraList column.
234             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ULTRALIST BOOLEAN")
235
236             // Enable it for all existing rows.
237             domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ULTRALIST = 1")
238         }
239
240         // Upgrade from schema version 11, first used in Privacy Browser 3.2, to schema version 12, first used in Privacy Browser 3.5.
241         if (oldVersion < 12) {
242             // Add the WebView theme column.  This defaults to `0`, which is `System default`, so a separate step isn't needed to populate the column.
243             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $WEBVIEW_THEME INTEGER")
244
245             // `night_mode` was removed.
246             // SQLite amazingly only added a command to drop a column in version 3.35.0.  <https://www.sqlite.org/changes.html>
247             // It will be a while before that is supported in Android.  <https://developer.android.com/reference/android/database/sqlite/package-summary>
248             // Although a new table could be created and all the data copied to it, I think I will just leave the old night mode column.  It will be wiped out the next time an import is run.
249         }
250
251         // Upgrade from schema version 12, first used in Privacy Browser 3.5, to schema version 13, first used in Privacy Browser 3.8.
252         if (oldVersion < 13) {
253             // Add the cookies column.
254             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $COOKIES BOOLEAN")
255
256             // Copy the data from the old column to the new one.
257             domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $COOKIES = enablefirstpartycookies")
258         }
259
260         // Upgrade from schema version 13, first used in Privacy Browser 3.8, to schema version 14, first used in Privacy Browser 3.11.
261         // This upgrade used to add the X-Requested-With header, but that was removed in schema version 15.
262
263         // Upgrade from schema version 14, first used in Privacy Browser 3.11, to schema version 15, first used in Privacy Browser 3.12.
264         // This upgrade removed `x_requested_with_header`.
265         // SQLite amazingly only added a command to drop a column in version 3.35.0.  <https://www.sqlite.org/changes.html>
266         // It will be a while before that is supported in Android.  <https://developer.android.com/reference/android/database/sqlite/package-summary>
267         // Although a new table could be created and all the data copied to it, I think I will just leave the X-Requested-With column.  It will be wiped out the next time an import is run.
268     }
269
270     val completeCursorOrderedByDomain: Cursor
271         get() {
272             // Get a readable database handle.
273             val domainsDatabase = this.readableDatabase
274
275             // Return everything in the domains table ordered by the domain name.  The cursor can't be closed because it is needed in the calling activity.
276             return domainsDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE ORDER BY $DOMAIN_NAME ASC", null)
277         }
278
279     val domainNameCursorOrderedByDomain: Cursor
280         get() {
281             // Get a readable database handle.
282             val domainsDatabase = this.readableDatabase
283
284             // Return the database id and the domain name in the domains table ordered by the domain name.  The cursor can't be closed because it is needed in the calling activity.
285             return domainsDatabase.rawQuery("SELECT $ID, $DOMAIN_NAME FROM $DOMAINS_TABLE ORDER BY $DOMAIN_NAME ASC", null)
286         }
287
288     fun getDomainNameCursorOrderedByDomainExcept(databaseId: Int): Cursor {
289         // Get a readable database handle.
290         val domainsDatabase = this.readableDatabase
291
292         // Return a cursor with the database IDs and domain names except for the specified ID ordered by domain name.  The cursor can't be closed because it is needed in the calling activity.
293         return domainsDatabase.rawQuery("SELECT $ID, $DOMAIN_NAME FROM $DOMAINS_TABLE WHERE $ID IS NOT $databaseId ORDER BY $DOMAIN_NAME ASC", null)
294     }
295
296     fun getCursorForId(databaseId: Int): Cursor {
297         // Get a readable database handle.
298         val domainsDatabase = this.readableDatabase
299
300         // Return a cursor for the specified database ID.  The cursor can't be closed because it is needed in the calling activity.
301         return domainsDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE WHERE $ID = $databaseId", null)
302     }
303
304     fun getCursorForDomainName(domainName: String): Cursor {
305         // Get a readable database handle.
306         val domainsDatabase = this.readableDatabase
307
308         // SQL escape the domain name.
309         val sqlEscapedDomainName = DatabaseUtils.sqlEscapeString(domainName)
310
311         // Return a cursor for the requested domain name.  The cursor can't be closed because it is needed in the calling activity.
312         return domainsDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE WHERE $DOMAIN_NAME = $sqlEscapedDomainName", null)
313     }
314
315     fun addDomain(domainName: String): Int {
316         // Instantiate a content values.
317         val domainContentValues = ContentValues()
318
319         // Get a handle for the shared preference.
320         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext)
321
322         // Get the default settings.
323         val javaScript = sharedPreferences.getBoolean(appContext.getString(R.string.javascript_key), false)
324         val cookies = sharedPreferences.getBoolean(appContext.getString(R.string.cookies_key), false)
325         val domStorage = sharedPreferences.getBoolean(appContext.getString(R.string.dom_storage_key), false)
326         val saveFormData = sharedPreferences.getBoolean(appContext.getString(R.string.save_form_data_key), false)  // Form data can be removed once the minimum API >= 26.
327         val easyList = sharedPreferences.getBoolean(appContext.getString(R.string.easylist_key), true)
328         val easyPrivacy = sharedPreferences.getBoolean(appContext.getString(R.string.easyprivacy_key), true)
329         val fanboyAnnoyanceList = sharedPreferences.getBoolean(appContext.getString(R.string.fanboys_annoyance_list_key), true)
330         val fanboySocialBlockingList = sharedPreferences.getBoolean(appContext.getString(R.string.fanboys_social_blocking_list_key), true)
331         val ultraList = sharedPreferences.getBoolean(appContext.getString(R.string.ultralist_key), true)
332         val ultraPrivacy = sharedPreferences.getBoolean(appContext.getString(R.string.ultraprivacy_key), true)
333         val blockAllThirdPartyRequests = sharedPreferences.getBoolean(appContext.getString(R.string.block_all_third_party_requests_key), false)
334
335         // Create entries for the database fields.  The ID is created automatically.  The pinned SSL certificate information is not created unless added by the user.
336         domainContentValues.put(DOMAIN_NAME, domainName)
337         domainContentValues.put(ENABLE_JAVASCRIPT, javaScript)
338         domainContentValues.put(COOKIES, cookies)
339         domainContentValues.put(ENABLE_DOM_STORAGE, domStorage)
340         domainContentValues.put(ENABLE_FORM_DATA, saveFormData) // Form data can be removed once the minimum API >= 26.
341         domainContentValues.put(ENABLE_EASYLIST, easyList)
342         domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacy)
343         domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboyAnnoyanceList)
344         domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboySocialBlockingList)
345         domainContentValues.put(ULTRALIST, ultraList)
346         domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacy)
347         domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests)
348         domainContentValues.put(USER_AGENT, appContext.getString(R.string.user_agent_default_value))
349         domainContentValues.put(FONT_SIZE, 0)
350         domainContentValues.put(SWIPE_TO_REFRESH, 0)
351         domainContentValues.put(WEBVIEW_THEME, 0)
352         domainContentValues.put(WIDE_VIEWPORT, 0)
353         domainContentValues.put(DISPLAY_IMAGES, 0)
354
355         // Get a writable database handle.
356         val domainsDatabase = this.writableDatabase
357
358         // Insert a new row and store the resulting database ID.
359         val newDomainDatabaseId = domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues).toInt()
360
361         // Close the database handle.
362         domainsDatabase.close()
363
364         // Return the new domain database ID.
365         return newDomainDatabaseId
366     }
367
368     fun addDomain(contentValues: ContentValues) {
369         // Get a writable database handle.
370         val domainsDatabase = this.writableDatabase
371
372         // Add the new domain.
373         domainsDatabase.insert(DOMAINS_TABLE, null, contentValues)
374
375         // Close the database handle.
376         domainsDatabase.close()
377     }
378
379     fun updateDomain(databaseId: Int, domainName: String, javaScript: Boolean, cookies: Boolean, domStorage: Boolean, formData: Boolean, easyList: Boolean, easyPrivacy: Boolean, fanboysAnnoyance: Boolean,
380                      fanboysSocialBlocking: Boolean, ultraList: Boolean, ultraPrivacy: Boolean, blockAllThirdPartyRequests: Boolean, userAgent: String, fontSize: Int,
381                      swipeToRefresh: Int, webViewTheme: Int, wideViewport: Int, displayImages: Int, pinnedSslCertificate: Boolean, pinnedIpAddresses: Boolean) {
382
383         // Instantiate a content values.
384         val domainContentValues = ContentValues()
385
386         // Add entries for each field in the database.
387         domainContentValues.put(DOMAIN_NAME, domainName)
388         domainContentValues.put(ENABLE_JAVASCRIPT, javaScript)
389         domainContentValues.put(COOKIES, cookies)
390         domainContentValues.put(ENABLE_DOM_STORAGE, domStorage)
391         domainContentValues.put(ENABLE_FORM_DATA, formData) // Form data can be removed once the minimum API >= 26.
392         domainContentValues.put(ENABLE_EASYLIST, easyList)
393         domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacy)
394         domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboysAnnoyance)
395         domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboysSocialBlocking)
396         domainContentValues.put(ULTRALIST, ultraList)
397         domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacy)
398         domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests)
399         domainContentValues.put(USER_AGENT, userAgent)
400         domainContentValues.put(FONT_SIZE, fontSize)
401         domainContentValues.put(SWIPE_TO_REFRESH, swipeToRefresh)
402         domainContentValues.put(WEBVIEW_THEME, webViewTheme)
403         domainContentValues.put(WIDE_VIEWPORT, wideViewport)
404         domainContentValues.put(DISPLAY_IMAGES, displayImages)
405         domainContentValues.put(PINNED_SSL_CERTIFICATE, pinnedSslCertificate)
406         domainContentValues.put(PINNED_IP_ADDRESSES, pinnedIpAddresses)
407
408         // Get a writable database handle.
409         val domainsDatabase = this.writableDatabase
410
411         // Update the row for the specified database ID.
412         domainsDatabase.update(DOMAINS_TABLE, domainContentValues, "$ID = $databaseId", null)
413
414         // Close the database handle.
415         domainsDatabase.close()
416     }
417
418     fun updatePinnedSslCertificate(databaseId: Int, sslIssuedToCommonName: String, sslIssuedToOrganization: String, sslIssuedToOrganizationalUnit: String, sslIssuedByCommonName: String,
419                                    sslIssuedByOrganization: String, sslIssuedByOrganizationalUnit: String, sslStartDate: Long, sslEndDate: Long) {
420         // Instantiate a content values.
421         val pinnedSslCertificateContentValues = ContentValues()
422
423         // Add entries for each field in the certificate.
424         pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_COMMON_NAME, sslIssuedToCommonName)
425         pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_ORGANIZATION, sslIssuedToOrganization)
426         pinnedSslCertificateContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, sslIssuedToOrganizationalUnit)
427         pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_COMMON_NAME, sslIssuedByCommonName)
428         pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_ORGANIZATION, sslIssuedByOrganization)
429         pinnedSslCertificateContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, sslIssuedByOrganizationalUnit)
430         pinnedSslCertificateContentValues.put(SSL_START_DATE, sslStartDate)
431         pinnedSslCertificateContentValues.put(SSL_END_DATE, sslEndDate)
432
433         // Get a writable database handle.
434         val domainsDatabase = this.writableDatabase
435
436         // Update the row for the specified database ID.
437         domainsDatabase.update(DOMAINS_TABLE, pinnedSslCertificateContentValues, "$ID = $databaseId", null)
438
439         // Close the database handle.
440         domainsDatabase.close()
441     }
442
443     fun updatePinnedIpAddresses(databaseId: Int, ipAddresses: String) {
444         // Instantiate a content values.
445         val pinnedIpAddressesContentValues = ContentValues()
446
447         // Add the IP addresses to the content values.
448         pinnedIpAddressesContentValues.put(IP_ADDRESSES, ipAddresses)
449
450         // Get a writable database handle.
451         val domainsDatabase = this.writableDatabase
452
453         // Update the row for the database ID.
454         domainsDatabase.update(DOMAINS_TABLE, pinnedIpAddressesContentValues, "$ID = $databaseId", null)
455
456         // Close the database handle.
457         domainsDatabase.close()
458     }
459
460     fun deleteDomain(databaseId: Int) {
461         // Get a writable database handle.
462         val domainsDatabase = this.writableDatabase
463
464         // Delete the row for the specified database ID.
465         domainsDatabase.delete(DOMAINS_TABLE, "$ID = $databaseId", null)
466
467         // Close the database handle.
468         domainsDatabase.close()
469     }
470 }