]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.kt
Add a bottom app bar to Settings. https://redmine.stoutner.com/issues/716
[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 // The private constants.
34 private const val SCHEMA_VERSION = 14
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         // The database constants.
40         const val DOMAINS_DATABASE = "domains.db"
41         const val DOMAINS_TABLE = "domains"
42
43         // The 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         // The 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 X_REQUESTED_WITH_HEADER = "x_requested_with_header"
66         const val FONT_SIZE = "fontsize"
67         const val SWIPE_TO_REFRESH = "swipetorefresh"
68         const val WEBVIEW_THEME = "webview_theme"
69         const val WIDE_VIEWPORT = "wide_viewport"
70         const val DISPLAY_IMAGES = "displayimages"
71         const val PINNED_SSL_CERTIFICATE = "pinnedsslcertificate"
72         const val SSL_ISSUED_TO_COMMON_NAME = "sslissuedtocommonname"
73         const val SSL_ISSUED_TO_ORGANIZATION = "sslissuedtoorganization"
74         const val SSL_ISSUED_TO_ORGANIZATIONAL_UNIT = "sslissuedtoorganizationalunit"
75         const val SSL_ISSUED_BY_COMMON_NAME = "sslissuedbycommonname"
76         const val SSL_ISSUED_BY_ORGANIZATION = "sslissuedbyorganization"
77         const val SSL_ISSUED_BY_ORGANIZATIONAL_UNIT = "sslissuedbyorganizationalunit"
78         const val SSL_START_DATE = "sslstartdate"
79         const val SSL_END_DATE = "sslenddate"
80         const val PINNED_IP_ADDRESSES = "pinned_ip_addresses"
81         const val IP_ADDRESSES = "ip_addresses"
82
83         // The table creation constant.
84         const val CREATE_DOMAINS_TABLE = "CREATE TABLE $DOMAINS_TABLE (" +
85                 "$ID INTEGER PRIMARY KEY, " +
86                 "$DOMAIN_NAME TEXT, " +
87                 "$ENABLE_JAVASCRIPT BOOLEAN, " +
88                 "$COOKIES BOOLEAN, " +
89                 "$ENABLE_DOM_STORAGE BOOLEAN, " +
90                 "$ENABLE_FORM_DATA BOOLEAN, " +
91                 "$ENABLE_EASYLIST BOOLEAN, " +
92                 "$ENABLE_EASYPRIVACY BOOLEAN, " +
93                 "$ENABLE_FANBOYS_ANNOYANCE_LIST BOOLEAN, " +
94                 "$ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST BOOLEAN, " +
95                 "$ULTRALIST BOOLEAN, " +
96                 "$ENABLE_ULTRAPRIVACY BOOLEAN, " +
97                 "$BLOCK_ALL_THIRD_PARTY_REQUESTS BOOLEAN, " +
98                 "$USER_AGENT TEXT, " +
99                 "$X_REQUESTED_WITH_HEADER INTEGER, " +
100                 "$FONT_SIZE INTEGER, " +
101                 "$SWIPE_TO_REFRESH INTEGER, " +
102                 "$WEBVIEW_THEME INTEGER, " +
103                 "$WIDE_VIEWPORT INTEGER, " +
104                 "$DISPLAY_IMAGES INTEGER, " +
105                 "$PINNED_SSL_CERTIFICATE BOOLEAN, " +
106                 "$SSL_ISSUED_TO_COMMON_NAME TEXT, " +
107                 "$SSL_ISSUED_TO_ORGANIZATION TEXT, " +
108                 "$SSL_ISSUED_TO_ORGANIZATIONAL_UNIT TEXT, " +
109                 "$SSL_ISSUED_BY_COMMON_NAME TEXT, " +
110                 "$SSL_ISSUED_BY_ORGANIZATION TEXT, " +
111                 "$SSL_ISSUED_BY_ORGANIZATIONAL_UNIT TEXT, " +
112                 "$SSL_START_DATE INTEGER, " +
113                 "$SSL_END_DATE INTEGER, " +
114                 "$PINNED_IP_ADDRESSES BOOLEAN, " +
115                 "$IP_ADDRESSES TEXT)"
116     }
117
118     override fun onCreate(domainsDatabase: SQLiteDatabase) {
119         // Create the domains table.
120         domainsDatabase.execSQL(CREATE_DOMAINS_TABLE)
121     }
122
123     override fun onUpgrade(domainsDatabase: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
124         // Upgrade from schema version 1, first used in Privacy Browser 2.0, to schema version 2, first used in Privacy Browser 2.3.
125         if (oldVersion < 2) {
126             // Add the display images column.
127             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $DISPLAY_IMAGES INTEGER")
128         }
129
130         // Upgrade from schema version 2, first used in Privacy Browser 2.3, to schema version 3, first used in Privacy Browser 2.5.
131         if (oldVersion < 3) {
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
144         // Upgrade from schema version 3, first used in Privacy Browser 2.5, to schema version 4, first used in Privacy Browser 2.6.
145         if (oldVersion < 4) {
146             // Add the night mode column.
147             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN nightmode INTEGER")
148         }
149
150         // Upgrade from schema version 4, first used in Privacy Browser 2.6, to schema version 5, first used in Privacy Browser 2.9.
151         if (oldVersion < 5) {
152             // Add the block lists columns.
153             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ENABLE_EASYLIST BOOLEAN")
154             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ENABLE_EASYPRIVACY BOOLEAN")
155             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ENABLE_FANBOYS_ANNOYANCE_LIST BOOLEAN")
156             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST BOOLEAN")
157
158             // Get a handle for the shared preference.
159             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext)
160
161             // Get the default block list settings.
162             val easyListEnabled = sharedPreferences.getBoolean("easylist", true)
163             val easyPrivacyEnabled = sharedPreferences.getBoolean("easyprivacy", true)
164             val fanboyAnnoyanceListEnabled = sharedPreferences.getBoolean("fanboys_annoyance_list", true)
165             val fanboySocialBlockingListEnabled = sharedPreferences.getBoolean("fanboys_social_blocking_list", true)
166
167             // Set EasyList for existing rows according to the current system-wide default.
168             // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
169             // <https://developer.android.com/reference/android/database/sqlite/package-summary>
170             if (easyListEnabled) {
171                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYLIST = 1")
172             } else {
173                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYLIST = 0")
174             }
175
176             // Set EasyPrivacy for existing rows according to the current system-wide default.
177             if (easyPrivacyEnabled) {
178                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYPRIVACY = 1")
179             } else {
180                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_EASYPRIVACY = 0")
181             }
182
183             // Set Fanboy's Annoyance List for existing rows according to the current system-wide default.
184             if (fanboyAnnoyanceListEnabled) {
185                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_ANNOYANCE_LIST = 1")
186             } else {
187                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_ANNOYANCE_LIST = 0")
188             }
189
190             // Set Fanboy's Social Blocking List for existing rows according to the current system-wide default.
191             if (fanboySocialBlockingListEnabled) {
192                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST = 1")
193             } else {
194                 domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST = 0")
195             }
196         }
197
198         // Upgrade from schema version 5, first used in Privacy Browser 2.9, to schema version 6, first used in Privacy Browser 2.11.
199         if (oldVersion < 6) {
200             // 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.
201             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $SWIPE_TO_REFRESH INTEGER")
202         }
203
204         // Upgrade from schema version 6, first used in Privacy Browser 2.11, to schema version 7, first used in Privacy Browser 2.12.
205         if (oldVersion < 7) {
206             // 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.
207             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $BLOCK_ALL_THIRD_PARTY_REQUESTS BOOLEAN")
208         }
209
210         // Upgrade from schema version 7, first used in Privacy Browser 2.12, to schema version 8, first used in Privacy Browser 2.12.
211         // For some reason (lack of planning or attention to detail), the 2.12 update included two schema version jumps.
212         if (oldVersion < 8) {
213             // Add the UltraPrivacy column.
214             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ENABLE_ULTRAPRIVACY BOOLEAN")
215
216             // Enable it for all existing rows.
217             domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ENABLE_ULTRAPRIVACY = 1")
218         }
219
220         // Upgrade from schema version 8, first used in Privacy Browser 2.12, to schema version 9, first used in Privacy Browser 2.16.
221         if (oldVersion < 9) {
222             // Add the pinned IP addresses columns.  These default to `0` and `""`, so a separate step isn't needed to populate the columns.
223             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $PINNED_IP_ADDRESSES BOOLEAN")
224             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $IP_ADDRESSES TEXT")
225         }
226
227         // Upgrade from schema version 9, first used in Privacy Browser 2.16, to schema version 10, first used in Privacy Browser 3.1.
228         if (oldVersion < 10) {
229             // Add the wide viewport column.  This defaults to `0`, which is `System default`, so a separate step isn't needed to populate the column.
230             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $WIDE_VIEWPORT INTEGER")
231         }
232
233         // Upgrade from schema version 10, first used in Privacy Browser 3.1, to schema version 11, first used in Privacy Browser 3.2.
234         if (oldVersion < 11) {
235             // Add the UltraList column.
236             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ULTRALIST BOOLEAN")
237
238             // Enable it for all existing rows.
239             domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ULTRALIST = 1")
240         }
241
242         // Upgrade from schema version 11, first used in Privacy Browser 3.2, to schema version 12, first used in Privacy Browser 3.5.
243         if (oldVersion < 12) {
244             // Add the WebView theme column.  This defaults to `0`, which is `System default`, so a separate step isn't needed to populate the column.
245             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $WEBVIEW_THEME INTEGER")
246
247             // SQLite amazingly only added a command to drop a column in version 3.35.0.  <https://www.sqlite.org/changes.html>
248             // It will be a while before that is supported in Android.  <https://developer.android.com/reference/android/database/sqlite/package-summary>
249             // 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.
250         }
251
252         // Upgrade from schema version 12, first used in Privacy Browser 3.5, to schema version 13, first used in Privacy Browser 3.8.
253         if (oldVersion < 13) {
254             // Add the cookies column.
255             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $COOKIES BOOLEAN")
256
257             // Copy the data from the old column to the new one.
258             domainsDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $COOKIES = enablefirstpartycookies")
259         }
260
261         // Upgrade from schema version 13, first used in Privacy Browser 3.8, to schema version 14, first used in Privacy Browser 3.11.
262         if (oldVersion < 14) {
263             // Add the X-Requested-With header column.  This defaults to `0`, which is `System default`, so a separate step isn't needed to populate the column.
264             domainsDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $X_REQUESTED_WITH_HEADER INTEGER")
265         }
266     }
267
268     val completeCursorOrderedByDomain: Cursor
269         get() {
270             // Get a readable database handle.
271             val domainsDatabase = this.readableDatabase
272
273             // 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.
274             return domainsDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE ORDER BY $DOMAIN_NAME ASC", null)
275         }
276
277     val domainNameCursorOrderedByDomain: Cursor
278         get() {
279             // Get a readable database handle.
280             val domainsDatabase = this.readableDatabase
281
282             // 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.
283             return domainsDatabase.rawQuery("SELECT $ID, $DOMAIN_NAME FROM $DOMAINS_TABLE ORDER BY $DOMAIN_NAME ASC", null)
284         }
285
286     fun getDomainNameCursorOrderedByDomainExcept(databaseId: Int): Cursor {
287         // Get a readable database handle.
288         val domainsDatabase = this.readableDatabase
289
290         // 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.
291         return domainsDatabase.rawQuery("SELECT $ID, $DOMAIN_NAME FROM $DOMAINS_TABLE WHERE $ID IS NOT $databaseId ORDER BY $DOMAIN_NAME ASC", null)
292     }
293
294     fun getCursorForId(databaseId: Int): Cursor {
295         // Get a readable database handle.
296         val domainsDatabase = this.readableDatabase
297
298         // Return a cursor for the specified database ID.  The cursor can't be closed because it is needed in the calling activity.
299         return domainsDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE WHERE $ID = $databaseId", null)
300     }
301
302     fun getCursorForDomainName(domainName: String): Cursor {
303         // Get a readable database handle.
304         val domainsDatabase = this.readableDatabase
305
306         // SQL escape the domain name.
307         val sqlEscapedDomainName = DatabaseUtils.sqlEscapeString(domainName)
308
309         // Return a cursor for the requested domain name.  The cursor can't be closed because it is needed in the calling activity.
310         return domainsDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE WHERE $DOMAIN_NAME = $sqlEscapedDomainName", null)
311     }
312
313     fun addDomain(domainName: String): Int {
314         // Instantiate a content values.
315         val domainContentValues = ContentValues()
316
317         // Get a handle for the shared preference.
318         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext)
319
320         // Get the default settings.
321         val javaScript = sharedPreferences.getBoolean(appContext.getString(R.string.javascript_key), false)
322         val cookies = sharedPreferences.getBoolean(appContext.getString(R.string.cookies_key), false)
323         val domStorage = sharedPreferences.getBoolean(appContext.getString(R.string.dom_storage_key), false)
324         val saveFormData = sharedPreferences.getBoolean(appContext.getString(R.string.save_form_data_key), false)  // Form data can be removed once the minimum API >= 26.
325         val easyList = sharedPreferences.getBoolean(appContext.getString(R.string.easylist_key), true)
326         val easyPrivacy = sharedPreferences.getBoolean(appContext.getString(R.string.easyprivacy_key), true)
327         val fanboyAnnoyanceList = sharedPreferences.getBoolean(appContext.getString(R.string.fanboys_annoyance_list_key), true)
328         val fanboySocialBlockingList = sharedPreferences.getBoolean(appContext.getString(R.string.fanboys_social_blocking_list_key), true)
329         val ultraList = sharedPreferences.getBoolean(appContext.getString(R.string.ultralist_key), true)
330         val ultraPrivacy = sharedPreferences.getBoolean(appContext.getString(R.string.ultraprivacy_key), true)
331         val blockAllThirdPartyRequests = sharedPreferences.getBoolean(appContext.getString(R.string.block_all_third_party_requests_key), false)
332
333         // Create entries for the database fields.  The ID is created automatically.  The pinned SSL certificate information is not created unless added by the user.
334         domainContentValues.put(DOMAIN_NAME, domainName)
335         domainContentValues.put(ENABLE_JAVASCRIPT, javaScript)
336         domainContentValues.put(COOKIES, cookies)
337         domainContentValues.put(ENABLE_DOM_STORAGE, domStorage)
338         domainContentValues.put(ENABLE_FORM_DATA, saveFormData) // Form data can be removed once the minimum API >= 26.
339         domainContentValues.put(ENABLE_EASYLIST, easyList)
340         domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacy)
341         domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboyAnnoyanceList)
342         domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboySocialBlockingList)
343         domainContentValues.put(ULTRALIST, ultraList)
344         domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacy)
345         domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests)
346         domainContentValues.put(USER_AGENT, appContext.getString(R.string.user_agent_default_value))
347         domainContentValues.put(X_REQUESTED_WITH_HEADER, 0)
348         domainContentValues.put(FONT_SIZE, 0)
349         domainContentValues.put(SWIPE_TO_REFRESH, 0)
350         domainContentValues.put(WEBVIEW_THEME, 0)
351         domainContentValues.put(WIDE_VIEWPORT, 0)
352         domainContentValues.put(DISPLAY_IMAGES, 0)
353
354         // Get a writable database handle.
355         val domainsDatabase = this.writableDatabase
356
357         // Insert a new row and store the resulting database ID.
358         val newDomainDatabaseId = domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues).toInt()
359
360         // Close the database handle.
361         domainsDatabase.close()
362
363         // Return the new domain database ID.
364         return newDomainDatabaseId
365     }
366
367     fun addDomain(contentValues: ContentValues) {
368         // Get a writable database handle.
369         val domainsDatabase = this.writableDatabase
370
371         // Add the new domain.
372         domainsDatabase.insert(DOMAINS_TABLE, null, contentValues)
373
374         // Close the database handle.
375         domainsDatabase.close()
376     }
377
378     fun updateDomain(databaseId: Int, domainName: String, javaScript: Boolean, cookies: Boolean, domStorage: Boolean, formData: Boolean, easyList: Boolean, easyPrivacy: Boolean, fanboysAnnoyance: Boolean,
379                      fanboysSocialBlocking: Boolean, ultraList: Boolean, ultraPrivacy: Boolean, blockAllThirdPartyRequests: Boolean, userAgent: String, xRequestedWithHeader: Int, fontSize: Int,
380                      swipeToRefresh: Int, webViewTheme: Int, wideViewport: Int, displayImages: Int, pinnedSslCertificate: Boolean, pinnedIpAddresses: Boolean) {
381
382         // Instantiate a content values.
383         val domainContentValues = ContentValues()
384
385         // Add entries for each field in the database.
386         domainContentValues.put(DOMAIN_NAME, domainName)
387         domainContentValues.put(ENABLE_JAVASCRIPT, javaScript)
388         domainContentValues.put(COOKIES, cookies)
389         domainContentValues.put(ENABLE_DOM_STORAGE, domStorage)
390         domainContentValues.put(ENABLE_FORM_DATA, formData) // Form data can be removed once the minimum API >= 26.
391         domainContentValues.put(ENABLE_EASYLIST, easyList)
392         domainContentValues.put(ENABLE_EASYPRIVACY, easyPrivacy)
393         domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, fanboysAnnoyance)
394         domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, fanboysSocialBlocking)
395         domainContentValues.put(ULTRALIST, ultraList)
396         domainContentValues.put(ENABLE_ULTRAPRIVACY, ultraPrivacy)
397         domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests)
398         domainContentValues.put(USER_AGENT, userAgent)
399         domainContentValues.put(X_REQUESTED_WITH_HEADER, xRequestedWithHeader)
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 }