]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/ImportExportDatabaseHelper.kt
14d0cbc56b3edbda0e1694ba8ce071824e8cafd4
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / ImportExportDatabaseHelper.kt
1 /*
2  * Copyright 2018-2023 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.DatabaseUtils
25 import android.database.sqlite.SQLiteDatabase
26
27 import androidx.preference.PreferenceManager
28
29 import com.stoutner.privacybrowser.R
30 import com.stoutner.privacybrowser.activities.HOME_FOLDER_ID
31
32 import java.io.File
33 import java.io.FileInputStream
34 import java.io.FileOutputStream
35 import java.io.InputStream
36 import java.io.OutputStream
37
38 import java.util.Date
39
40 // Define the public constants.
41 const val IMPORT_EXPORT_SCHEMA_VERSION = 18
42 const val EXPORT_SUCCESSFUL = "A"
43 const val IMPORT_SUCCESSFUL = "B"
44
45 // Define the private class constants.
46 private const val ALLOW_SCREENSHOTS = "allow_screenshots"
47 private const val AMP_REDIRECTS = "amp_redirects"
48 private const val APP_THEME = "app_theme"
49 private const val BOTTOM_APP_BAR = "bottom_app_bar"
50 private const val CLEAR_CACHE = "clear_cache"
51 private const val CLEAR_COOKIES = "clear_cookies"
52 private const val CLEAR_DOM_STORAGE = "clear_dom_storage"
53 private const val CLEAR_EVERYTHING = "clear_everything"
54 private const val CLEAR_FORM_DATA = "clear_form_data"  // Clear form data can be removed once the minimum API >= 26.
55 private const val CLEAR_LOGCAT = "clear_logcat"
56 private const val CUSTOM_USER_AGENT = "custom_user_agent"
57 private const val DISPLAY_ADDITIONAL_APP_BAR_ICONS = "display_additional_app_bar_icons"
58 private const val DISPLAY_UNDER_CUTOUTS = "display_under_cutouts"
59 private const val DISPLAY_WEBPAGE_IMAGES = "display_webpage_images"
60 private const val DOM_STORAGE = "dom_storage"
61 private const val DOWNLOAD_WITH_EXTERNAL_APP = "download_with_external_app"
62 private const val EASYLIST = "easylist"
63 private const val EASYPRIVACY = "easyprivacy"
64 private const val FANBOYS_ANNOYANCE_LIST = "fanboys_annoyance_list"
65 private const val FANBOYS_SOCIAL_BLOCKING_LIST = "fanboys_social_blocking_list"
66 private const val FULL_SCREEN_BROWSING_MODE = "full_screen_browsing_mode"
67 private const val HIDE_APP_BAR = "hide_app_bar"
68 private const val HOMEPAGE = "homepage"
69 private const val INCOGNITO_MODE = "incognito_mode"
70 private const val JAVASCRIPT = "javascript"
71 private const val OPEN_INTENTS_IN_NEW_TAB = "open_intents_in_new_tab"
72 private const val PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS = "block_all_third_party_requests"
73 private const val PREFERENCES_FONT_SIZE = "font_size"
74 private const val PREFERENCES_TABLE = "preferences"
75 private const val PREFERENCES_USER_AGENT = "user_agent"
76 private const val PROXY = "proxy"
77 private const val PROXY_CUSTOM_URL = "proxy_custom_url"
78 private const val SAVE_FORM_DATA = "save_form_data"
79 private const val SEARCH = "search"
80 private const val SEARCH_CUSTOM_URL = "search_custom_url"
81 private const val SCROLL_APP_BAR = "scroll_app_bar"
82 private const val PREFERENCES_SWIPE_TO_REFRESH = "swipe_to_refresh"
83 private const val TRACKING_QUERIES = "tracking_queries"
84 private const val ULTRAPRIVACY = "ultraprivacy"
85
86 class ImportExportDatabaseHelper {
87     fun importUnencrypted(importFileInputStream: InputStream, context: Context): String {
88         return try {
89             // Create a temporary import file.
90             val temporaryImportFile = File.createTempFile("temporary_import_file", null, context.cacheDir)
91
92             // The file may be copied directly in Kotlin using `File.copyTo`.  <https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/copy-to.html>
93             // It can be copied in Android using `Files.copy` once the minimum API >= 26.
94             // <https://developer.android.com/reference/java/nio/file/Files#copy(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...)>
95             // However, the file cannot be acquired from the content URI until the minimum API >= 29.  <https://developer.android.com/reference/kotlin/android/content/ContentResolver#openfile>
96
97             // Create a temporary file output stream.
98             val temporaryImportFileOutputStream = FileOutputStream(temporaryImportFile)
99
100             // Create a transfer byte array.
101             val transferByteArray = ByteArray(1024)
102
103             // Create an integer to track the number of bytes read.
104             var bytesRead: Int
105
106             // Copy the import file to the temporary import file.
107             while (importFileInputStream.read(transferByteArray).also { bytesRead = it } > 0) {
108                 temporaryImportFileOutputStream.write(transferByteArray, 0, bytesRead)
109             }
110
111             // Flush the temporary import file output stream.
112             temporaryImportFileOutputStream.flush()
113
114             // Close the file streams.
115             importFileInputStream.close()
116             temporaryImportFileOutputStream.close()
117
118
119             // Get a handle for the shared preference.
120             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
121
122             // Open the import database.  Once the minimum API >= 27 the file can be opened directly without using the string.
123             val importDatabase = SQLiteDatabase.openDatabase(temporaryImportFile.toString(), null, SQLiteDatabase.OPEN_READWRITE)
124
125             // Get the database version.
126             val importDatabaseVersion = importDatabase.version
127
128             // Upgrade from schema version 1, first used in Privacy Browser 2.13, to schema version 2, first used in Privacy Browser 2.14.
129             // Previously this upgrade added `download_with_external_app` to the Preferences table.  But that is now removed in schema version 10.
130
131             // Upgrade from schema version 2, first used in Privacy Browser 2.14, to schema version 3, first used in Privacy Browser 2.15.
132             if (importDatabaseVersion < 3) {
133                 // `default_font_size` was renamed `font_size`.
134                 // Once the SQLite version is >= 3.25.0 (Android API >= 30) `ALTER TABLE RENAME COLUMN` can be used.  <https://www.sqlite.org/lang_altertable.html> <https://www.sqlite.org/changes.html>
135                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
136                 // In the meantime, a new column must be created with the new name.  There is no need to delete the old column on the temporary import database.
137
138                 // Create the new font size column.
139                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $PREFERENCES_FONT_SIZE TEXT")
140
141                 // Populate the preferences table with the current font size value.
142                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $PREFERENCES_FONT_SIZE = default_font_size")
143             }
144
145             // Upgrade from schema version 3, first used in Privacy Browser 2.15, to schema version 4, first used in Privacy Browser 2.16.
146             if (importDatabaseVersion < 4) {
147                 // Add the Pinned IP Addresses columns to the domains table.
148                 importDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $PINNED_IP_ADDRESSES  BOOLEAN")
149                 importDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $IP_ADDRESSES TEXT")
150             }
151
152             // Upgrade from schema version 4, first used in Privacy Browser 2.16, to schema version 5, first used in Privacy Browser 2.17.
153             if (importDatabaseVersion < 5) {
154                 // Add the hide and scroll app bar columns to the preferences table.
155                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $HIDE_APP_BAR BOOLEAN")
156                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $SCROLL_APP_BAR BOOLEAN")
157
158                 // Get the current hide and scroll app bar settings.
159                 val hideAppBar = sharedPreferences.getBoolean(HIDE_APP_BAR, true)
160                 val scrollAppBar = sharedPreferences.getBoolean(SCROLL_APP_BAR, true)
161
162                 // Populate the preferences table with the current app bar values.
163                 // This can switch to using the variables directly once the minimum API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
164                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
165                 if (hideAppBar)
166                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $HIDE_APP_BAR = 1")
167                 else
168                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $HIDE_APP_BAR = 0")
169
170                 if (scrollAppBar)
171                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $SCROLL_APP_BAR = 1")
172                 else
173                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $SCROLL_APP_BAR = 0")
174             }
175
176             // Upgrade from schema version 5, first used in Privacy Browser 2.17, to schema version 6, first used in Privacy Browser 3.0.
177             if (importDatabaseVersion < 6) {
178                 // Add the open intents in new tab column to the preferences table.
179                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $OPEN_INTENTS_IN_NEW_TAB BOOLEAN")
180
181                 // Get the current open intents in new tab preference.
182                 val openIntentsInNewTab = sharedPreferences.getBoolean(OPEN_INTENTS_IN_NEW_TAB, true)
183
184                 // Populate the preferences table with the current open intents value.
185                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
186                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
187                 if (openIntentsInNewTab)
188                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $OPEN_INTENTS_IN_NEW_TAB = 1")
189                 else
190                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $OPEN_INTENTS_IN_NEW_TAB = 0")
191             }
192
193             // Upgrade from schema version 6, first used in Privacy Browser 3.0, to schema version 7, first used in Privacy Browser 3.1.
194             if (importDatabaseVersion < 7) {
195                 // Previously this upgrade added `facebook_click_ids` to the Preferences table.  But that is now removed in schema version 15.
196
197                 // Add the wide viewport column to the domains table.
198                 importDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $WIDE_VIEWPORT INTEGER")
199
200                 // Add the Google Analytics, Twitter AMP redirects, and wide viewport columns to the preferences table.
201                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN google_analytics BOOLEAN")
202                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN twitter_amp_redirects BOOLEAN")
203                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $WIDE_VIEWPORT BOOLEAN")
204
205                 // Get the current preference values.
206                 val trackingQueries = sharedPreferences.getBoolean(TRACKING_QUERIES, true)
207                 val ampRedirects = sharedPreferences.getBoolean(AMP_REDIRECTS, true)
208                 val wideViewport = sharedPreferences.getBoolean(WIDE_VIEWPORT, true)
209
210                 // Populate the preferences with the current Tracking Queries value.  Google Analytics was renamed Tracking Queries in schema version 15.
211                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
212                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
213                 if (trackingQueries)
214                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET google_analytics = 1")
215                 else
216                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET google_analytics = 0")
217
218                 // Populate the preferences table with the current AMP Redirects value.  Twitter AMP Redirects was renamed AMP Redirects in schema version 15.
219                 if (ampRedirects)
220                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET twitter_amp_redirects = 1")
221                 else
222                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET twitter_amp_redirects = 0")
223
224                 // Populate the preferences table with the current wide viewport value.
225                 if (wideViewport)
226                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $WIDE_VIEWPORT = 1")
227                 else
228                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $WIDE_VIEWPORT = 0")
229             }
230
231             // Upgrade from schema version 7, first used in Privacy Browser 3.1, to schema version 8, first used in Privacy Browser 3.2.
232             if (importDatabaseVersion < 8) {
233                 // Add the UltraList column to the tables.
234                 importDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $ULTRALIST INTEGER")
235                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $ULTRALIST BOOLEAN")
236
237                 // Get the current preference values.
238                 val ultraList = sharedPreferences.getBoolean(ULTRALIST, true)
239
240                 // Populate the tables with the current UltraList value.
241                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
242                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
243                 if (ultraList) {
244                     importDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ULTRALIST = 1")
245                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $ULTRALIST = 1")
246                 } else {
247                     importDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $ULTRALIST = 0")
248                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $ULTRALIST = 0")
249                 }
250             }
251
252             // Upgrade from schema version 8, first used in Privacy Browser 3.2, to schema version 9, first used in Privacy Browser 3.3.
253             if (importDatabaseVersion < 9) {
254                 // Add the new proxy columns to the preferences table.
255                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $PROXY TEXT")
256                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $PROXY_CUSTOM_URL TEXT")
257
258                 // Get the current proxy values.
259                 val proxy = sharedPreferences.getString(PROXY, context.getString(R.string.proxy_default_value))
260                 var proxyCustomUrl = sharedPreferences.getString(PROXY_CUSTOM_URL, context.getString(R.string.proxy_custom_url_default_value))
261
262                 // SQL escape the proxy custom URL string.
263                 proxyCustomUrl = DatabaseUtils.sqlEscapeString(proxyCustomUrl)
264
265                 // Populate the preferences table with the current proxy values. The proxy custom URL does not need to be surrounded by `'` because it was SLQ escaped above.
266                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $PROXY = '$proxy'")
267                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $PROXY_CUSTOM_URL = $proxyCustomUrl")
268             }
269
270             // Upgrade from schema version 9, first used in Privacy Browser 3.3, to schema version 10, first used in Privacy Browser 3.4.
271             // Previously this upgrade added `download_location` and `download_custom_location` to the Preferences table.  But they were removed in schema version 13.
272
273             // Upgrade from schema version 10, first used in Privacy Browser 3.4, to schema version 11, first used in Privacy Browser 3.5.
274             if (importDatabaseVersion < 11) {
275                 // Add the app theme column to the preferences table.
276                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $APP_THEME TEXT")
277
278                 // Get a cursor for the dark theme preference.
279                 val darkThemePreferencesCursor = importDatabase.rawQuery("SELECT dark_theme FROM $PREFERENCES_TABLE", null)
280
281                 // Move to the first entry.
282                 darkThemePreferencesCursor.moveToFirst()
283
284                 // Get the old dark theme value, which is in column 0.
285                 val darkTheme = darkThemePreferencesCursor.getInt(0)
286
287                 // Close the dark theme preference cursor.
288                 darkThemePreferencesCursor.close()
289
290                 // Get the system default string.
291                 val systemDefault = context.getString(R.string.app_theme_default_value)
292
293                 // Get the theme entry values string array.
294                 val appThemeEntryValuesStringArray: Array<String> = context.resources.getStringArray(R.array.app_theme_entry_values)
295
296                 // Get the dark string.
297                 val dark = appThemeEntryValuesStringArray[2]
298
299                 // Populate the app theme according to the old dark theme preference.
300                 if (darkTheme == 0) {  // A light theme was selected.
301                     // Set the app theme to be the system default.
302                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $APP_THEME = '$systemDefault'")
303                 } else {  // A dark theme was selected.
304                     // Set the app theme to be dark.
305                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $APP_THEME = '$dark'")
306                 }
307
308                 // Add the WebView theme to the domains table.  This defaults to 0, which is `System default`, so a separate step isn't needed to populate the database.
309                 importDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $WEBVIEW_THEME INTEGER")
310
311                 // Add the WebView theme to the preferences table.
312                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $WEBVIEW_THEME TEXT")
313
314                 // Get the WebView theme default value string.
315                 val webViewThemeDefaultValue = context.getString(R.string.webview_theme_default_value)
316
317                 // Set the WebView theme in the preferences table to be the default.
318                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $WEBVIEW_THEME = '$webViewThemeDefaultValue'")
319             }
320
321             // Upgrade from schema version 11, first used in Privacy Browser 3.5, to schema version 12, first used in Privacy Browser 3.6.
322             if (importDatabaseVersion < 12) {
323                 // Add the clear logcat column to the preferences table.
324                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $CLEAR_LOGCAT BOOLEAN")
325
326                 // Get the current clear logcat value.
327                 val clearLogcat = sharedPreferences.getBoolean(CLEAR_LOGCAT, true)
328
329                 // Populate the preferences table with the current clear logcat value.
330                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
331                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
332                 if (clearLogcat)
333                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $CLEAR_LOGCAT = 1")
334                 else
335                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $CLEAR_LOGCAT = 0")
336             }
337
338             // Upgrade from schema version 12, first used in Privacy Browser 3.6, to schema version 13, first used in Privacy Browser 3.7.
339             // Do nothing.  `download_location` and `download_custom_location` were removed from the preferences table, but they can be left in the temporary import database without issue.
340
341             // Upgrade from schema version 13, first used in Privacy Browser 3.7, to schema version 14, first used in Privacy Browser 3.8.
342             if (importDatabaseVersion < 14) {
343                 // `enabledthirdpartycookies` was removed from the domains table.  `do_not_track` and `third_party_cookies` were removed from the preferences table.
344                 // There is no need to delete the columns as they will simply be ignored by the import.
345
346                 // `enablefirstpartycookies` was renamed `cookies`.
347                 // Once the SQLite version is >= 3.25.0 (Android API >= 30) `ALTER TABLE RENAME COLUMN` can be used.  <https://www.sqlite.org/lang_altertable.html> <https://www.sqlite.org/changes.html>
348                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
349                 // In the meantime, a new column must be created with the new name.  There is no need to delete the old column on the temporary import database.
350
351                 // Create the new cookies columns.
352                 importDatabase.execSQL("ALTER TABLE $DOMAINS_TABLE ADD COLUMN $COOKIES INTEGER")
353                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $COOKIES BOOLEAN")
354
355                 // Copy the data from the old cookies columns to the new ones.
356                 importDatabase.execSQL("UPDATE $DOMAINS_TABLE SET $COOKIES = enablefirstpartycookies")
357                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $COOKIES = first_party_cookies")
358
359                 // Create the new download with external app and bottom app bar columns.
360                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DOWNLOAD_WITH_EXTERNAL_APP BOOLEAN")
361                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $BOTTOM_APP_BAR BOOLEAN")
362
363                 // Get the current values for the new columns.
364                 val downloadWithExternalApp = sharedPreferences.getBoolean(DOWNLOAD_WITH_EXTERNAL_APP, false)
365                 val bottomAppBar = sharedPreferences.getBoolean(BOTTOM_APP_BAR, false)
366
367                 // Populate the preferences table with the current download with external app value.
368                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
369                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
370                 if (downloadWithExternalApp)
371                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_WITH_EXTERNAL_APP = 1")
372                 else
373                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_WITH_EXTERNAL_APP = 0")
374
375                 // Populate the preferences table with the current bottom app bar value.
376                 if (bottomAppBar)
377                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $BOTTOM_APP_BAR = 1")
378                 else
379                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $BOTTOM_APP_BAR = 0")
380             }
381
382             // Upgrade from schema version 14, first used in Privacy Browser 3.8, to schema version 15, first used in Privacy Browser 3.11.
383             if (importDatabaseVersion < 15) {
384                 // `facebook_click_ids` was removed from the preferences table.
385                 // There is no need to delete the columns as they will simply be ignored by the import.
386
387                 // `x_requested_with_header` was previously added to the preferences and domains tables in this version, but it was removed later in schema version 16.
388
389                 // Create the new URL modification columns.
390                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $TRACKING_QUERIES BOOLEAN")
391                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $AMP_REDIRECTS BOOLEAN")
392
393                 // Copy the data from the old columns to the new ones.
394                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $TRACKING_QUERIES = google_analytics")
395                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $AMP_REDIRECTS = twitter_amp_redirects")
396             }
397
398             // Upgrade from schema version 15, first used in Privacy Browser 3.11, to schema version 16, first used in Privacy Browser 3.12.
399             // This upgrade removed the `x_requested_with_header` from the domains and preferences tables.
400             // There is no need to delete the columns as they will simply be ignored by the import.
401
402             // Upgrade from schema version 16, first used in Privacy Browser 3.12, to schema version 17, first used in Privacy Browser 3.15.
403             if (importDatabaseVersion < 17) {
404                 // Add the folder ID column.
405                 importDatabase.execSQL("ALTER TABLE $BOOKMARKS_TABLE ADD COLUMN $FOLDER_ID INTEGER")
406
407                 // Get a cursor with all the folders.
408                 val foldersCursor = importDatabase.rawQuery("SELECT $ID FROM $BOOKMARKS_TABLE WHERE $IS_FOLDER = 1", null)
409
410                 // Get the folders cursor ID column index.
411                 val foldersCursorIdColumnIndex = foldersCursor.getColumnIndexOrThrow(ID)
412
413                 // Add a folder ID to each folder.
414                 while(foldersCursor.moveToNext()) {
415                     // Get the current folder database ID.
416                     val databaseId = foldersCursor.getInt(foldersCursorIdColumnIndex)
417
418                     // Generate a folder ID.
419                     val folderId = generateFolderId(importDatabase)
420
421                     // Create a folder content values.
422                     val folderContentValues = ContentValues()
423
424                     // Store the new folder ID in the content values.
425                     folderContentValues.put(FOLDER_ID, folderId)
426
427                     // Update the folder with the new folder ID.
428                     importDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
429                 }
430
431                 // Close the folders cursor.
432                 foldersCursor.close()
433
434
435                 // Add the parent folder ID column.
436                 importDatabase.execSQL("ALTER TABLE $BOOKMARKS_TABLE ADD COLUMN $PARENT_FOLDER_ID INTEGER")
437
438                 // Get a cursor with all the bookmarks.
439                 val bookmarksCursor = importDatabase.rawQuery("SELECT $ID, parentfolder FROM $BOOKMARKS_TABLE", null)
440
441                 // Get the bookmarks cursor ID column index.
442                 val bookmarksCursorIdColumnIndex = bookmarksCursor.getColumnIndexOrThrow(ID)
443                 val bookmarksCursorParentFolderColumnIndex = bookmarksCursor.getColumnIndexOrThrow("parentfolder")
444
445                 // Populate the parent folder ID for each bookmark.
446                 while(bookmarksCursor.moveToNext()) {
447                     // Get the information from the cursor.
448                     val databaseId = bookmarksCursor.getInt(bookmarksCursorIdColumnIndex)
449                     val oldParentFolderString = bookmarksCursor.getString(bookmarksCursorParentFolderColumnIndex)
450
451                     // Initialize the new parent folder ID.
452                     var newParentFolderId = HOME_FOLDER_ID
453
454                     // Get the parent folder ID if the bookmark is not in the home folder.
455                     if (oldParentFolderString.isNotEmpty()) {
456                         // SQL escape the old parent folder string.
457                         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(oldParentFolderString)
458
459                         // Get the parent folder cursor.
460                         val parentFolderCursor = importDatabase.rawQuery("SELECT $FOLDER_ID FROM $BOOKMARKS_TABLE WHERE $BOOKMARK_NAME = $sqlEscapedFolderName AND $IS_FOLDER = 1", null)
461
462                         // Get the new parent folder ID if it exists.
463                         if (parentFolderCursor.count > 0) {
464                             // Move to the first entry.
465                             parentFolderCursor.moveToFirst()
466
467                             // Get the new parent folder ID.
468                             newParentFolderId = parentFolderCursor.getLong(parentFolderCursor.getColumnIndexOrThrow(FOLDER_ID))
469                         }
470
471                         // Close the parent folder cursor.
472                         parentFolderCursor.close()
473                     }
474
475                     // Create a bookmark content values.
476                     val bookmarkContentValues = ContentValues()
477
478                     // Store the new parent folder ID in the content values.
479                     bookmarkContentValues.put(PARENT_FOLDER_ID, newParentFolderId)
480
481                     // Update the folder with the new folder ID.
482                     importDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
483                 }
484
485                 // Close the bookmarks cursor.
486                 bookmarksCursor.close()
487
488                 // This upgrade removed the old `parentfolder` string column.
489                 // SQLite amazingly only added a command to drop a column in version 3.35.0.  <https://www.sqlite.org/changes.html>
490                 // It will be a while before that is supported in Android.  <https://developer.android.com/reference/android/database/sqlite/package-summary>
491                 // Although a new table could be created and all the data copied to it, I think I will just leave the old parent folder column.  It will be wiped out the next time an import is run.
492             }
493
494             // Upgrade from schema version 17, first used in Privacy Browser 3.15, to schema version 18, first used in Privacy Browser 3.17.
495             if (importDatabaseVersion < 18) {
496                 // Create the new display under cutout column.
497                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DISPLAY_UNDER_CUTOUTS BOOLEAN")
498
499                 // Get the current display under cutout value.
500                 val displayUnderCutouts = sharedPreferences.getBoolean(DISPLAY_UNDER_CUTOUTS, false)
501
502                 // Populate the preferences table with the current display under cutouts value.
503                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
504                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
505                 if (displayUnderCutouts)
506                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DISPLAY_UNDER_CUTOUTS = 1")
507                 else
508                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DISPLAY_UNDER_CUTOUTS = 0")
509             }
510
511             /* End of database upgrade logic. */
512
513
514             // Get a cursor for the bookmarks table.
515             val importBookmarksCursor = importDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE", null)
516
517             // Delete the current bookmarks database.
518             context.deleteDatabase(BOOKMARKS_DATABASE)
519
520             // Create a new bookmarks database.
521             val bookmarksDatabaseHelper = BookmarksDatabaseHelper(context)
522
523             // Move to the first record.
524             importBookmarksCursor.moveToFirst()
525
526             // Get the bookmarks colum indexes.
527             val bookmarkNameColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(BOOKMARK_NAME)
528             val bookmarkUrlColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)
529             val bookmarkParentFolderIdColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(PARENT_FOLDER_ID)
530             val bookmarkDisplayOrderColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(DISPLAY_ORDER)
531             val bookmarkIsFolderColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(IS_FOLDER)
532             val bookmarkFolderIdColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(FOLDER_ID)
533             val bookmarkFavoriteIconColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(FAVORITE_ICON)
534
535             // Copy the data from the import bookmarks cursor into the bookmarks database.
536             for (i in 0 until importBookmarksCursor.count) {
537                 // Create a bookmark content values.
538                 val bookmarkContentValues = ContentValues()
539
540                 // Add the information for this bookmark to the content values.
541                 bookmarkContentValues.put(BOOKMARK_NAME, importBookmarksCursor.getString(bookmarkNameColumnIndex))
542                 bookmarkContentValues.put(BOOKMARK_URL, importBookmarksCursor.getString(bookmarkUrlColumnIndex))
543                 bookmarkContentValues.put(PARENT_FOLDER_ID, importBookmarksCursor.getLong(bookmarkParentFolderIdColumnIndex))
544                 bookmarkContentValues.put(DISPLAY_ORDER, importBookmarksCursor.getInt(bookmarkDisplayOrderColumnIndex))
545                 bookmarkContentValues.put(IS_FOLDER, importBookmarksCursor.getInt(bookmarkIsFolderColumnIndex))
546                 bookmarkContentValues.put(FOLDER_ID, importBookmarksCursor.getLong(bookmarkFolderIdColumnIndex))
547                 bookmarkContentValues.put(FAVORITE_ICON, importBookmarksCursor.getBlob(bookmarkFavoriteIconColumnIndex))
548
549                 // Insert the content values into the bookmarks database.
550                 bookmarksDatabaseHelper.createBookmark(bookmarkContentValues)
551
552                 // Advance to the next record.
553                 importBookmarksCursor.moveToNext()
554             }
555
556             // Upgrade from schema version 16, first used in Privacy Browser 3.12, to schema version 17, first used in Privacy Browser 3.15.
557             if (importDatabaseVersion < 16) {
558                 // Get the current switch default values.
559                 val javaScriptDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.javascript_key), false)
560                 val cookiesDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.cookies_key), false)
561                 val domStorageDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.dom_storage_key), false)
562                 val formDataDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.save_form_data_key), false)
563                 val easyListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.easylist_key), true)
564                 val easyPrivacyDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.easyprivacy_key), true)
565                 val fanboysAnnoyanceListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.fanboys_annoyance_list_key), true)
566                 val fanboysSocialBlockingListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.fanboys_social_blocking_list), true)
567                 val ultraListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.ultralist_key), true)
568                 val ultraPrivacyDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.ultraprivacy_key), true)
569                 val blockAllThirdPartyRequestsDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.block_all_third_party_requests_key), false)
570
571                 // Get a domains cursor.
572                 val importDomainsConversionCursor = importDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE", null)
573
574                 // Get the domains column indexes.
575                 val javaScriptColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_JAVASCRIPT)
576                 val cookiesColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(COOKIES)
577                 val domStorageColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_DOM_STORAGE)
578                 val formDataColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_FORM_DATA)
579                 val easyListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_EASYLIST)
580                 val easyPrivacyColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
581                 val fanboysAnnoyanceListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_ANNOYANCE_LIST)
582                 val fanboysSocialBlockingListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)
583                 val ultraListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ULTRALIST)
584                 val ultraPrivacyColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_ULTRAPRIVACY)
585                 val blockAllThirdPartyRequestsColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(BLOCK_ALL_THIRD_PARTY_REQUESTS)
586
587                 // Convert the domain from the switch booleans to the spinner integers.
588                 for (i in 0 until importDomainsConversionCursor.count) {
589                     // Move to the current record.
590                     importDomainsConversionCursor.moveToPosition(i)
591
592                     // Get the domain current values.
593                     val javaScriptDomainCurrentValue = importDomainsConversionCursor.getInt(javaScriptColumnIndex)
594                     val cookiesDomainCurrentValue = importDomainsConversionCursor.getInt(cookiesColumnIndex)
595                     val domStorageDomainCurrentValue = importDomainsConversionCursor.getInt(domStorageColumnIndex)
596                     val formDataDomainCurrentValue = importDomainsConversionCursor.getInt(formDataColumnIndex)
597                     val easyListDomainCurrentValue = importDomainsConversionCursor.getInt(easyListColumnIndex)
598                     val easyPrivacyDomainCurrentValue = importDomainsConversionCursor.getInt(easyPrivacyColumnIndex)
599                     val fanboysAnnoyanceListCurrentValue = importDomainsConversionCursor.getInt(fanboysAnnoyanceListColumnIndex)
600                     val fanboysSocialBlockingListCurrentValue = importDomainsConversionCursor.getInt(fanboysSocialBlockingListColumnIndex)
601                     val ultraListCurrentValue = importDomainsConversionCursor.getInt(ultraListColumnIndex)
602                     val ultraPrivacyCurrentValue = importDomainsConversionCursor.getInt(ultraPrivacyColumnIndex)
603                     val blockAllThirdPartyRequestsCurrentValue = importDomainsConversionCursor.getInt(blockAllThirdPartyRequestsColumnIndex)
604
605                     // Instantiate a domain content values.
606                     val domainContentValues = ContentValues()
607
608                     // Populate the domain content values.
609                     domainContentValues.put(ENABLE_JAVASCRIPT, convertFromSwitchToSpinner(javaScriptDefaultValue, javaScriptDomainCurrentValue))
610                     domainContentValues.put(COOKIES, convertFromSwitchToSpinner(cookiesDefaultValue, cookiesDomainCurrentValue))
611                     domainContentValues.put(ENABLE_DOM_STORAGE, convertFromSwitchToSpinner(domStorageDefaultValue, domStorageDomainCurrentValue))
612                     domainContentValues.put(ENABLE_FORM_DATA, convertFromSwitchToSpinner(formDataDefaultValue, formDataDomainCurrentValue))
613                     domainContentValues.put(ENABLE_EASYLIST, convertFromSwitchToSpinner(easyListDefaultValue, easyListDomainCurrentValue))
614                     domainContentValues.put(ENABLE_EASYPRIVACY, convertFromSwitchToSpinner(easyPrivacyDefaultValue, easyPrivacyDomainCurrentValue))
615                     domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, convertFromSwitchToSpinner(fanboysAnnoyanceListDefaultValue, fanboysAnnoyanceListCurrentValue))
616                     domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, convertFromSwitchToSpinner(fanboysSocialBlockingListDefaultValue, fanboysSocialBlockingListCurrentValue))
617                     domainContentValues.put(ULTRALIST, convertFromSwitchToSpinner(ultraListDefaultValue, ultraListCurrentValue))
618                     domainContentValues.put(ENABLE_ULTRAPRIVACY, convertFromSwitchToSpinner(ultraPrivacyDefaultValue, ultraPrivacyCurrentValue))
619                     domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, convertFromSwitchToSpinner(blockAllThirdPartyRequestsDefaultValue, blockAllThirdPartyRequestsCurrentValue))
620
621                     // Get the current database ID.
622                     val currentDatabaseId = importDomainsConversionCursor.getInt(importDomainsConversionCursor.getColumnIndexOrThrow(ID))
623
624                     // Update the row for the specified database ID.
625                     importDatabase.update(DOMAINS_TABLE, domainContentValues, "$ID = $currentDatabaseId", null)
626                 }
627
628                 // Close the cursor.
629                 importDomainsConversionCursor.close()
630             }
631
632             // Close the bookmarks cursor and database.
633             importBookmarksCursor.close()
634             bookmarksDatabaseHelper.close()
635
636
637             // Get a cursor for the domains table.
638             val importDomainsCursor = importDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE ORDER BY $DOMAIN_NAME ASC", null)
639
640             // Delete the current domains database.
641             context.deleteDatabase(DOMAINS_DATABASE)
642
643             // Create a new domains database.
644             val domainsDatabaseHelper = DomainsDatabaseHelper(context)
645
646             // Move to the first record.
647             importDomainsCursor.moveToFirst()
648
649             // Get the domain column indexes.
650             val domainNameColumnIndex = importDomainsCursor.getColumnIndexOrThrow(DOMAIN_NAME)
651             val domainJavaScriptColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_JAVASCRIPT)
652             val domainCookiesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(COOKIES)
653             val domainDomStorageColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_DOM_STORAGE)
654             val domainFormDataColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_FORM_DATA)  // Form data can be removed once the minimum API >= 26.
655             val domainEasyListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_EASYLIST)
656             val domainEasyPrivacyColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
657             val domainFanboysAnnoyanceListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_ANNOYANCE_LIST)
658             val domainFanboysSocialBlockingListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)
659             val domainUltraListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ULTRALIST)
660             val domainUltraPrivacyColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
661             val domainBlockAllThirdPartyRequestsColumnIndex = importDomainsCursor.getColumnIndexOrThrow(BLOCK_ALL_THIRD_PARTY_REQUESTS)
662             val domainUserAgentColumnIndex = importDomainsCursor.getColumnIndexOrThrow(USER_AGENT)
663             val domainFontSizeColumnIndex = importDomainsCursor.getColumnIndexOrThrow(FONT_SIZE)
664             val domainSwipeToRefreshColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SWIPE_TO_REFRESH)
665             val domainWebViewThemeColumnIndex = importDomainsCursor.getColumnIndexOrThrow(WEBVIEW_THEME)
666             val domainWideViewportColumnIndex = importDomainsCursor.getColumnIndexOrThrow(WIDE_VIEWPORT)
667             val domainDisplayImagesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(DISPLAY_IMAGES)
668             val domainPinnedSslCertificateColumnIndex = importDomainsCursor.getColumnIndexOrThrow(PINNED_SSL_CERTIFICATE)
669             val domainSslIssuedToCommonNameColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_COMMON_NAME)
670             val domainSslIssuedToOrganizationColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATION)
671             val domainSslIssuedToOrganizationalUnitColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)
672             val domainSslIssuedByCommonNameColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_COMMON_NAME)
673             val domainSslIssuedByOrganizationColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATION)
674             val domainSslIssuedByOrganizationalUnitColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)
675             val domainSslStartDateColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_START_DATE)
676             val domainSslEndDateColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_END_DATE)
677             val domainPinnedIpAddressesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(PINNED_IP_ADDRESSES)
678             val domainIpAddressesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(IP_ADDRESSES)
679
680             // Copy the data from the import domains cursor into the domains database.
681             for (i in 0 until importDomainsCursor.count) {
682                 // Create a domain content values.
683                 val domainContentValues = ContentValues()
684
685                 // Populate the domain content values.
686                 domainContentValues.put(DOMAIN_NAME, importDomainsCursor.getString(domainNameColumnIndex))
687                 domainContentValues.put(ENABLE_JAVASCRIPT, importDomainsCursor.getInt(domainJavaScriptColumnIndex))
688                 domainContentValues.put(COOKIES, importDomainsCursor.getInt(domainCookiesColumnIndex))
689                 domainContentValues.put(ENABLE_DOM_STORAGE, importDomainsCursor.getInt(domainDomStorageColumnIndex))
690                 domainContentValues.put(ENABLE_FORM_DATA, importDomainsCursor.getInt(domainFormDataColumnIndex))  // Form data can be removed once the minimum API >= 26.
691                 domainContentValues.put(ENABLE_EASYLIST, importDomainsCursor.getInt(domainEasyListColumnIndex))
692                 domainContentValues.put(ENABLE_EASYPRIVACY, importDomainsCursor.getInt(domainEasyPrivacyColumnIndex))
693                 domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, importDomainsCursor.getInt(domainFanboysAnnoyanceListColumnIndex))
694                 domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, importDomainsCursor.getInt(domainFanboysSocialBlockingListColumnIndex))
695                 domainContentValues.put(ULTRALIST, importDomainsCursor.getInt(domainUltraListColumnIndex))
696                 domainContentValues.put(ENABLE_ULTRAPRIVACY, importDomainsCursor.getInt(domainUltraPrivacyColumnIndex))
697                 domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, importDomainsCursor.getInt(domainBlockAllThirdPartyRequestsColumnIndex))
698                 domainContentValues.put(USER_AGENT, importDomainsCursor.getString(domainUserAgentColumnIndex))
699                 domainContentValues.put(FONT_SIZE, importDomainsCursor.getInt(domainFontSizeColumnIndex))
700                 domainContentValues.put(SWIPE_TO_REFRESH, importDomainsCursor.getInt(domainSwipeToRefreshColumnIndex))
701                 domainContentValues.put(WEBVIEW_THEME, importDomainsCursor.getInt(domainWebViewThemeColumnIndex))
702                 domainContentValues.put(WIDE_VIEWPORT, importDomainsCursor.getInt(domainWideViewportColumnIndex))
703                 domainContentValues.put(DISPLAY_IMAGES, importDomainsCursor.getInt(domainDisplayImagesColumnIndex))
704                 domainContentValues.put(PINNED_SSL_CERTIFICATE, importDomainsCursor.getInt(domainPinnedSslCertificateColumnIndex))
705                 domainContentValues.put(SSL_ISSUED_TO_COMMON_NAME, importDomainsCursor.getString(domainSslIssuedToCommonNameColumnIndex))
706                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATION, importDomainsCursor.getString(domainSslIssuedToOrganizationColumnIndex))
707                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, importDomainsCursor.getString(domainSslIssuedToOrganizationalUnitColumnIndex))
708                 domainContentValues.put(SSL_ISSUED_BY_COMMON_NAME, importDomainsCursor.getString(domainSslIssuedByCommonNameColumnIndex))
709                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATION, importDomainsCursor.getString(domainSslIssuedByOrganizationColumnIndex))
710                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, importDomainsCursor.getString(domainSslIssuedByOrganizationalUnitColumnIndex))
711                 domainContentValues.put(SSL_START_DATE, importDomainsCursor.getLong(domainSslStartDateColumnIndex))
712                 domainContentValues.put(SSL_END_DATE, importDomainsCursor.getLong(domainSslEndDateColumnIndex))
713                 domainContentValues.put(PINNED_IP_ADDRESSES, importDomainsCursor.getInt(domainPinnedIpAddressesColumnIndex))
714                 domainContentValues.put(IP_ADDRESSES, importDomainsCursor.getString(domainIpAddressesColumnIndex))
715
716                 // Insert the content values into the domains database.
717                 domainsDatabaseHelper.addDomain(domainContentValues)
718
719                 // Advance to the next record.
720                 importDomainsCursor.moveToNext()
721             }
722
723             // Close the domains cursor and database.
724             importDomainsCursor.close()
725             domainsDatabaseHelper.close()
726
727
728             // Get a cursor for the preferences table.
729             val importPreferencesCursor = importDatabase.rawQuery("SELECT * FROM $PREFERENCES_TABLE", null)
730
731             // Move to the first record.
732             importPreferencesCursor.moveToFirst()
733
734             // Import the preference data.
735             sharedPreferences.edit()
736                 .putBoolean(JAVASCRIPT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(JAVASCRIPT)) == 1)
737                 .putBoolean(COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(COOKIES)) == 1)
738                 .putBoolean(DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DOM_STORAGE)) == 1)
739                 .putBoolean(SAVE_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(SAVE_FORM_DATA)) == 1)  // Save form data can be removed once the minimum API >= 26.
740                 .putString(PREFERENCES_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_USER_AGENT)))
741                 .putString(CUSTOM_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(CUSTOM_USER_AGENT)))
742                 .putBoolean(INCOGNITO_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(INCOGNITO_MODE)) == 1)
743                 .putBoolean(ALLOW_SCREENSHOTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ALLOW_SCREENSHOTS)) == 1)
744                 .putBoolean(EASYLIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(EASYLIST)) == 1)
745                 .putBoolean(EASYPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(EASYPRIVACY)) == 1)
746                 .putBoolean(FANBOYS_ANNOYANCE_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FANBOYS_ANNOYANCE_LIST)) == 1)
747                 .putBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FANBOYS_SOCIAL_BLOCKING_LIST)) == 1)
748                 .putBoolean(ULTRALIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ULTRALIST)) == 1)
749                 .putBoolean(ULTRAPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ULTRAPRIVACY)) == 1)
750                 .putBoolean(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS)) == 1)
751                 .putBoolean(TRACKING_QUERIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(TRACKING_QUERIES)) == 1)
752                 .putBoolean(AMP_REDIRECTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(AMP_REDIRECTS)) == 1)
753                 .putString(SEARCH, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(SEARCH)))
754                 .putString(SEARCH_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(SEARCH_CUSTOM_URL)))
755                 .putString(PROXY, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PROXY)))
756                 .putString(PROXY_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PROXY_CUSTOM_URL)))
757                 .putBoolean(FULL_SCREEN_BROWSING_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FULL_SCREEN_BROWSING_MODE)) == 1)
758                 .putBoolean(HIDE_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(HIDE_APP_BAR)) == 1)
759                 .putBoolean(DISPLAY_UNDER_CUTOUTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_UNDER_CUTOUTS)) == 1)
760                 .putBoolean(CLEAR_EVERYTHING, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_EVERYTHING)) == 1)
761                 .putBoolean(CLEAR_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_COOKIES)) == 1)
762                 .putBoolean(CLEAR_DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_DOM_STORAGE)) == 1)
763                 .putBoolean(CLEAR_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_FORM_DATA)) == 1)  // Clear form data can be removed once the minimum API >= 26.
764                 .putBoolean(CLEAR_LOGCAT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_LOGCAT)) == 1)
765                 .putBoolean(CLEAR_CACHE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_CACHE)) == 1)
766                 .putString(HOMEPAGE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(HOMEPAGE)))
767                 .putString(PREFERENCES_FONT_SIZE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_FONT_SIZE)))
768                 .putBoolean(OPEN_INTENTS_IN_NEW_TAB, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(OPEN_INTENTS_IN_NEW_TAB)) == 1)
769                 .putBoolean(PREFERENCES_SWIPE_TO_REFRESH, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_SWIPE_TO_REFRESH)) == 1)
770                 .putBoolean(DOWNLOAD_WITH_EXTERNAL_APP, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DOWNLOAD_WITH_EXTERNAL_APP)) == 1)
771                 .putBoolean(SCROLL_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(SCROLL_APP_BAR)) == 1)
772                 .putBoolean(BOTTOM_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(BOTTOM_APP_BAR)) == 1)
773                 .putBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_ADDITIONAL_APP_BAR_ICONS)) == 1)
774                 .putString(APP_THEME, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(APP_THEME)))
775                 .putString(WEBVIEW_THEME, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(WEBVIEW_THEME)))
776                 .putBoolean(WIDE_VIEWPORT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(WIDE_VIEWPORT)) == 1)
777                 .putBoolean(DISPLAY_WEBPAGE_IMAGES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_WEBPAGE_IMAGES)) == 1)
778                 .apply()
779
780             // Close the preferences cursor and database.
781             importPreferencesCursor.close()
782             importDatabase.close()
783
784             // Delete the temporary import file database, journal, and other related auxiliary files.
785             SQLiteDatabase.deleteDatabase(temporaryImportFile)
786
787             // Return the import successful string.
788             IMPORT_SUCCESSFUL
789         } catch (exception: Exception) {
790             // Return the import error.
791             exception.toString()
792         }
793     }
794
795     fun exportUnencrypted(exportFileOutputStream: OutputStream, context: Context): String {
796         return try {
797             // Create a temporary export file.
798             val temporaryExportFile = File.createTempFile("temporary_export_file", null, context.cacheDir)
799
800             // Create the temporary export database.
801             val temporaryExportDatabase = SQLiteDatabase.openOrCreateDatabase(temporaryExportFile, null)
802
803             // Set the temporary export database version number.
804             temporaryExportDatabase.version = IMPORT_EXPORT_SCHEMA_VERSION
805
806
807             // Create the temporary export database bookmarks table.
808             temporaryExportDatabase.execSQL(CREATE_BOOKMARKS_TABLE)
809
810             // Open the bookmarks database.
811             val bookmarksDatabaseHelper = BookmarksDatabaseHelper(context)
812
813             // Get a full bookmarks cursor.
814             val bookmarksCursor = bookmarksDatabaseHelper.allBookmarks
815
816             // Move to the first record.
817             bookmarksCursor.moveToFirst()
818
819             // Get the bookmarks colum indexes.
820             val bookmarkNameColumnIndex = bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_NAME)
821             val bookmarkUrlColumnIndex = bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)
822             val bookmarkParentFolderIdColumnIndex = bookmarksCursor.getColumnIndexOrThrow(PARENT_FOLDER_ID)
823             val bookmarkDisplayOrderColumnIndex = bookmarksCursor.getColumnIndexOrThrow(DISPLAY_ORDER)
824             val bookmarkIsFolderColumnIndex = bookmarksCursor.getColumnIndexOrThrow(IS_FOLDER)
825             val bookmarkFolderIdColumnIndex = bookmarksCursor.getColumnIndexOrThrow(FOLDER_ID)
826             val bookmarkFavoriteIconColumnIndex = bookmarksCursor.getColumnIndexOrThrow(FAVORITE_ICON)
827
828             // Copy the data from the bookmarks cursor into the export database.
829             for (i in 0 until bookmarksCursor.count) {
830                 // Create a bookmark content values.
831                 val bookmarkContentValues = ContentValues()
832
833                 // Populate the bookmark content values.
834                 bookmarkContentValues.put(BOOKMARK_NAME, bookmarksCursor.getString(bookmarkNameColumnIndex))
835                 bookmarkContentValues.put(BOOKMARK_URL, bookmarksCursor.getString(bookmarkUrlColumnIndex))
836                 bookmarkContentValues.put(PARENT_FOLDER_ID, bookmarksCursor.getLong(bookmarkParentFolderIdColumnIndex))
837                 bookmarkContentValues.put(DISPLAY_ORDER, bookmarksCursor.getInt(bookmarkDisplayOrderColumnIndex))
838                 bookmarkContentValues.put(IS_FOLDER, bookmarksCursor.getInt(bookmarkIsFolderColumnIndex))
839                 bookmarkContentValues.put(FOLDER_ID, bookmarksCursor.getLong(bookmarkFolderIdColumnIndex))
840                 bookmarkContentValues.put(FAVORITE_ICON, bookmarksCursor.getBlob(bookmarkFavoriteIconColumnIndex))
841
842                 // Insert the content values into the temporary export database.
843                 temporaryExportDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues)
844
845                 // Advance to the next record.
846                 bookmarksCursor.moveToNext()
847             }
848
849             // Close the bookmarks cursor and database.
850             bookmarksCursor.close()
851             bookmarksDatabaseHelper.close()
852
853
854             // Create the temporary export database domains table.
855             temporaryExportDatabase.execSQL(CREATE_DOMAINS_TABLE)
856
857             // Open the domains database.
858             val domainsDatabaseHelper = DomainsDatabaseHelper(context)
859
860             // Get a full domains database cursor.
861             val domainsCursor = domainsDatabaseHelper.completeCursorOrderedByDomain
862
863             // Move to the first record.
864             domainsCursor.moveToFirst()
865
866             // Get the domain column indexes.
867             val domainNameColumnIndex = domainsCursor.getColumnIndexOrThrow(DOMAIN_NAME)
868             val domainJavaScriptColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_JAVASCRIPT)
869             val domainCookiesColumnIndex = domainsCursor.getColumnIndexOrThrow(COOKIES)
870             val domainDomStorageColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_DOM_STORAGE)
871             val domainFormDataColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_FORM_DATA)  // Form data can be removed once the minimum API >= 26.
872             val domainEasyListColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_EASYLIST)
873             val domainEasyPrivacyColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
874             val domainFanboysAnnoyanceListColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_ANNOYANCE_LIST)
875             val domainFanboysSocialBlockingListColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)
876             val domainUltraListColumnIndex = domainsCursor.getColumnIndexOrThrow(ULTRALIST)
877             val domainUltraPrivacyColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
878             val domainBlockAllThirdPartyRequestsColumnIndex = domainsCursor.getColumnIndexOrThrow(BLOCK_ALL_THIRD_PARTY_REQUESTS)
879             val domainUserAgentColumnIndex = domainsCursor.getColumnIndexOrThrow(USER_AGENT)
880             val domainFontSizeColumnIndex = domainsCursor.getColumnIndexOrThrow(FONT_SIZE)
881             val domainSwipeToRefreshColumnIndex = domainsCursor.getColumnIndexOrThrow(SWIPE_TO_REFRESH)
882             val domainWebViewThemeColumnIndex = domainsCursor.getColumnIndexOrThrow(WEBVIEW_THEME)
883             val domainWideViewportColumnIndex = domainsCursor.getColumnIndexOrThrow(WIDE_VIEWPORT)
884             val domainDisplayImagesColumnIndex = domainsCursor.getColumnIndexOrThrow(DISPLAY_IMAGES)
885             val domainPinnedSslCertificateColumnIndex = domainsCursor.getColumnIndexOrThrow(PINNED_SSL_CERTIFICATE)
886             val domainSslIssuedToCommonNameColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_COMMON_NAME)
887             val domainSslIssuedToOrganizationColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATION)
888             val domainSslIssuedToOrganizationalUnitColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)
889             val domainSslIssuedByCommonNameColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_COMMON_NAME)
890             val domainSslIssuedByOrganizationColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATION)
891             val domainSslIssuedByOrganizationalUnitColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)
892             val domainSslStartDateColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_START_DATE)
893             val domainSslEndDateColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_END_DATE)
894             val domainPinnedIpAddressesColumnIndex = domainsCursor.getColumnIndexOrThrow(PINNED_IP_ADDRESSES)
895             val domainIpAddressesColumnIndex = domainsCursor.getColumnIndexOrThrow(IP_ADDRESSES)
896
897             // Copy the data from the domains cursor into the export database.
898             for (i in 0 until domainsCursor.count) {
899                 // Create a domain content values.
900                 val domainContentValues = ContentValues()
901
902                 // Populate the domain content values.
903                 domainContentValues.put(DOMAIN_NAME, domainsCursor.getString(domainNameColumnIndex))
904                 domainContentValues.put(ENABLE_JAVASCRIPT, domainsCursor.getInt(domainJavaScriptColumnIndex))
905                 domainContentValues.put(COOKIES, domainsCursor.getInt(domainCookiesColumnIndex))
906                 domainContentValues.put(ENABLE_DOM_STORAGE, domainsCursor.getInt(domainDomStorageColumnIndex))
907                 domainContentValues.put(ENABLE_FORM_DATA, domainsCursor.getInt(domainFormDataColumnIndex))  // Form data can be removed once the minimum API >= 26.
908                 domainContentValues.put(ENABLE_EASYLIST, domainsCursor.getInt(domainEasyListColumnIndex))
909                 domainContentValues.put(ENABLE_EASYPRIVACY, domainsCursor.getInt(domainEasyPrivacyColumnIndex))
910                 domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, domainsCursor.getInt(domainFanboysAnnoyanceListColumnIndex))
911                 domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, domainsCursor.getInt(domainFanboysSocialBlockingListColumnIndex))
912                 domainContentValues.put(ULTRALIST, domainsCursor.getInt(domainUltraListColumnIndex))
913                 domainContentValues.put(ENABLE_ULTRAPRIVACY, domainsCursor.getInt(domainUltraPrivacyColumnIndex))
914                 domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, domainsCursor.getInt(domainBlockAllThirdPartyRequestsColumnIndex))
915                 domainContentValues.put(USER_AGENT, domainsCursor.getString(domainUserAgentColumnIndex))
916                 domainContentValues.put(FONT_SIZE, domainsCursor.getInt(domainFontSizeColumnIndex))
917                 domainContentValues.put(SWIPE_TO_REFRESH, domainsCursor.getInt(domainSwipeToRefreshColumnIndex))
918                 domainContentValues.put(WEBVIEW_THEME, domainsCursor.getInt(domainWebViewThemeColumnIndex))
919                 domainContentValues.put(WIDE_VIEWPORT, domainsCursor.getInt(domainWideViewportColumnIndex))
920                 domainContentValues.put(DISPLAY_IMAGES, domainsCursor.getInt(domainDisplayImagesColumnIndex))
921                 domainContentValues.put(PINNED_SSL_CERTIFICATE, domainsCursor.getInt(domainPinnedSslCertificateColumnIndex))
922                 domainContentValues.put(SSL_ISSUED_TO_COMMON_NAME, domainsCursor.getString(domainSslIssuedToCommonNameColumnIndex))
923                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATION, domainsCursor.getString(domainSslIssuedToOrganizationColumnIndex))
924                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainSslIssuedToOrganizationalUnitColumnIndex))
925                 domainContentValues.put(SSL_ISSUED_BY_COMMON_NAME, domainsCursor.getString(domainSslIssuedByCommonNameColumnIndex))
926                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATION, domainsCursor.getString(domainSslIssuedByOrganizationColumnIndex))
927                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainSslIssuedByOrganizationalUnitColumnIndex))
928                 domainContentValues.put(SSL_START_DATE, domainsCursor.getLong(domainSslStartDateColumnIndex))
929                 domainContentValues.put(SSL_END_DATE, domainsCursor.getLong(domainSslEndDateColumnIndex))
930                 domainContentValues.put(PINNED_IP_ADDRESSES, domainsCursor.getInt(domainPinnedIpAddressesColumnIndex))
931                 domainContentValues.put(IP_ADDRESSES, domainsCursor.getString(domainIpAddressesColumnIndex))
932
933                 // Insert the content values into the temporary export database.
934                 temporaryExportDatabase.insert(DOMAINS_TABLE, null, domainContentValues)
935
936                 // Advance to the next record.
937                 domainsCursor.moveToNext()
938             }
939
940             // Close the domains cursor and database.
941             domainsCursor.close()
942             domainsDatabaseHelper.close()
943
944
945             // Prepare the preferences table SQL creation string.
946             val createPreferencesTable = "CREATE TABLE $PREFERENCES_TABLE (" +
947                     "$ID INTEGER PRIMARY KEY, " +
948                     "$JAVASCRIPT BOOLEAN, " +
949                     "$COOKIES BOOLEAN, " +
950                     "$DOM_STORAGE BOOLEAN, " +
951                     "$SAVE_FORM_DATA BOOLEAN, " +
952                     "$PREFERENCES_USER_AGENT TEXT, " +
953                     "$CUSTOM_USER_AGENT TEXT, " +
954                     "$INCOGNITO_MODE BOOLEAN, " +
955                     "$ALLOW_SCREENSHOTS BOOLEAN, " +
956                     "$EASYLIST BOOLEAN, " +
957                     "$EASYPRIVACY BOOLEAN, " +
958                     "$FANBOYS_ANNOYANCE_LIST BOOLEAN, " +
959                     "$FANBOYS_SOCIAL_BLOCKING_LIST BOOLEAN, " +
960                     "$ULTRALIST BOOLEAN, " +
961                     "$ULTRAPRIVACY BOOLEAN, " +
962                     "$PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS BOOLEAN, " +
963                     "$TRACKING_QUERIES BOOLEAN, " +
964                     "$AMP_REDIRECTS BOOLEAN, " +
965                     "$SEARCH TEXT, " +
966                     "$SEARCH_CUSTOM_URL TEXT, " +
967                     "$PROXY TEXT, " +
968                     "$PROXY_CUSTOM_URL TEXT, " +
969                     "$FULL_SCREEN_BROWSING_MODE BOOLEAN, " +
970                     "$HIDE_APP_BAR BOOLEAN, " +
971                     "$DISPLAY_UNDER_CUTOUTS BOOLEAN, " +
972                     "$CLEAR_EVERYTHING BOOLEAN, " +
973                     "$CLEAR_COOKIES BOOLEAN, " +
974                     "$CLEAR_DOM_STORAGE BOOLEAN, " +
975                     "$CLEAR_FORM_DATA BOOLEAN, " +
976                     "$CLEAR_LOGCAT BOOLEAN, " +
977                     "$CLEAR_CACHE BOOLEAN, " +
978                     "$HOMEPAGE TEXT, " +
979                     "$PREFERENCES_FONT_SIZE TEXT, " +
980                     "$OPEN_INTENTS_IN_NEW_TAB BOOLEAN, " +
981                     "$PREFERENCES_SWIPE_TO_REFRESH BOOLEAN, " +
982                     "$DOWNLOAD_WITH_EXTERNAL_APP BOOLEAN, " +
983                     "$SCROLL_APP_BAR BOOLEAN, " +
984                     "$BOTTOM_APP_BAR BOOLEAN, " +
985                     "$DISPLAY_ADDITIONAL_APP_BAR_ICONS BOOLEAN, " +
986                     "$APP_THEME TEXT, " +
987                     "$WEBVIEW_THEME TEXT, " +
988                     "$WIDE_VIEWPORT BOOLEAN, " +
989                     "$DISPLAY_WEBPAGE_IMAGES BOOLEAN)"
990
991             // Create the temporary export database preferences table.
992             temporaryExportDatabase.execSQL(createPreferencesTable)
993
994             // Get a handle for the shared preference.
995             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
996
997             // Create a preferences content values.
998             val preferencesContentValues = ContentValues()
999
1000             // Populate the preferences content values.
1001             preferencesContentValues.put(JAVASCRIPT, sharedPreferences.getBoolean(JAVASCRIPT, false))
1002             preferencesContentValues.put(COOKIES, sharedPreferences.getBoolean(COOKIES, false))
1003             preferencesContentValues.put(DOM_STORAGE, sharedPreferences.getBoolean(DOM_STORAGE, false))
1004             preferencesContentValues.put(SAVE_FORM_DATA, sharedPreferences.getBoolean(SAVE_FORM_DATA, false))  // Save form data can be removed once the minimum API >= 26.
1005             preferencesContentValues.put(PREFERENCES_USER_AGENT, sharedPreferences.getString(PREFERENCES_USER_AGENT, context.getString(R.string.user_agent_default_value)))
1006             preferencesContentValues.put(CUSTOM_USER_AGENT, sharedPreferences.getString(CUSTOM_USER_AGENT, context.getString(R.string.custom_user_agent_default_value)))
1007             preferencesContentValues.put(INCOGNITO_MODE, sharedPreferences.getBoolean(INCOGNITO_MODE, false))
1008             preferencesContentValues.put(ALLOW_SCREENSHOTS, sharedPreferences.getBoolean(ALLOW_SCREENSHOTS, false))
1009             preferencesContentValues.put(EASYLIST, sharedPreferences.getBoolean(EASYLIST, true))
1010             preferencesContentValues.put(EASYPRIVACY, sharedPreferences.getBoolean(EASYPRIVACY, true))
1011             preferencesContentValues.put(FANBOYS_ANNOYANCE_LIST, sharedPreferences.getBoolean(FANBOYS_ANNOYANCE_LIST, true))
1012             preferencesContentValues.put(FANBOYS_SOCIAL_BLOCKING_LIST, sharedPreferences.getBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, true))
1013             preferencesContentValues.put(ULTRALIST, sharedPreferences.getBoolean(ULTRALIST, true))
1014             preferencesContentValues.put(ULTRAPRIVACY, sharedPreferences.getBoolean(ULTRAPRIVACY, true))
1015             preferencesContentValues.put(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS, sharedPreferences.getBoolean(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS, false))
1016             preferencesContentValues.put(TRACKING_QUERIES, sharedPreferences.getBoolean(TRACKING_QUERIES, true))
1017             preferencesContentValues.put(AMP_REDIRECTS, sharedPreferences.getBoolean(AMP_REDIRECTS, true))
1018             preferencesContentValues.put(SEARCH, sharedPreferences.getString(SEARCH, context.getString(R.string.search_default_value)))
1019             preferencesContentValues.put(SEARCH_CUSTOM_URL, sharedPreferences.getString(SEARCH_CUSTOM_URL, context.getString(R.string.search_custom_url_default_value)))
1020             preferencesContentValues.put(PROXY, sharedPreferences.getString(PROXY, context.getString(R.string.proxy_default_value)))
1021             preferencesContentValues.put(PROXY_CUSTOM_URL, sharedPreferences.getString(PROXY_CUSTOM_URL, context.getString(R.string.proxy_custom_url_default_value)))
1022             preferencesContentValues.put(FULL_SCREEN_BROWSING_MODE, sharedPreferences.getBoolean(FULL_SCREEN_BROWSING_MODE, false))
1023             preferencesContentValues.put(HIDE_APP_BAR, sharedPreferences.getBoolean(HIDE_APP_BAR, true))
1024             preferencesContentValues.put(DISPLAY_UNDER_CUTOUTS, sharedPreferences.getBoolean(DISPLAY_UNDER_CUTOUTS, false))
1025             preferencesContentValues.put(CLEAR_EVERYTHING, sharedPreferences.getBoolean(CLEAR_EVERYTHING, true))
1026             preferencesContentValues.put(CLEAR_COOKIES, sharedPreferences.getBoolean(CLEAR_COOKIES, true))
1027             preferencesContentValues.put(CLEAR_DOM_STORAGE, sharedPreferences.getBoolean(CLEAR_DOM_STORAGE, true))
1028             preferencesContentValues.put(CLEAR_FORM_DATA, sharedPreferences.getBoolean(CLEAR_FORM_DATA, true))  // Clear form data can be removed once the minimum API >= 26.
1029             preferencesContentValues.put(CLEAR_LOGCAT, sharedPreferences.getBoolean(CLEAR_LOGCAT, true))
1030             preferencesContentValues.put(CLEAR_CACHE, sharedPreferences.getBoolean(CLEAR_CACHE, true))
1031             preferencesContentValues.put(HOMEPAGE, sharedPreferences.getString(HOMEPAGE, context.getString(R.string.homepage_default_value)))
1032             preferencesContentValues.put(PREFERENCES_FONT_SIZE, sharedPreferences.getString(PREFERENCES_FONT_SIZE, context.getString(R.string.font_size_default_value)))
1033             preferencesContentValues.put(OPEN_INTENTS_IN_NEW_TAB, sharedPreferences.getBoolean(OPEN_INTENTS_IN_NEW_TAB, true))
1034             preferencesContentValues.put(PREFERENCES_SWIPE_TO_REFRESH, sharedPreferences.getBoolean(PREFERENCES_SWIPE_TO_REFRESH, true))
1035             preferencesContentValues.put(DOWNLOAD_WITH_EXTERNAL_APP, sharedPreferences.getBoolean(DOWNLOAD_WITH_EXTERNAL_APP, false))
1036             preferencesContentValues.put(SCROLL_APP_BAR, sharedPreferences.getBoolean(SCROLL_APP_BAR, true))
1037             preferencesContentValues.put(BOTTOM_APP_BAR, sharedPreferences.getBoolean(BOTTOM_APP_BAR, false))
1038             preferencesContentValues.put(DISPLAY_ADDITIONAL_APP_BAR_ICONS, sharedPreferences.getBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, false))
1039             preferencesContentValues.put(APP_THEME, sharedPreferences.getString(APP_THEME, context.getString(R.string.app_theme_default_value)))
1040             preferencesContentValues.put(WEBVIEW_THEME, sharedPreferences.getString(WEBVIEW_THEME, context.getString(R.string.webview_theme_default_value)))
1041             preferencesContentValues.put(WIDE_VIEWPORT, sharedPreferences.getBoolean(WIDE_VIEWPORT, true))
1042             preferencesContentValues.put(DISPLAY_WEBPAGE_IMAGES, sharedPreferences.getBoolean(DISPLAY_WEBPAGE_IMAGES, true))
1043
1044             // Insert the preferences content values into the temporary export database.
1045             temporaryExportDatabase.insert(PREFERENCES_TABLE, null, preferencesContentValues)
1046
1047             // Close the temporary export database.
1048             temporaryExportDatabase.close()
1049
1050
1051             // The file may be copied directly in Kotlin using `File.copyTo`.  <https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/copy-to.html>
1052             // It can be copied in Android using `Files.copy` once the minimum API >= 26.
1053             // <https://developer.android.com/reference/java/nio/file/Files#copy(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...)>
1054             // However, the file cannot be acquired from the content URI until the minimum API >= 29.  <https://developer.android.com/reference/kotlin/android/content/ContentResolver#openfile>
1055
1056             // Create the temporary export file input stream.
1057             val temporaryExportFileInputStream = FileInputStream(temporaryExportFile)
1058
1059             // Create a byte array.
1060             val transferByteArray = ByteArray(1024)
1061
1062             // Create an integer to track the number of bytes read.
1063             var bytesRead: Int
1064
1065             // Copy the temporary export file to the export file output stream.
1066             while (temporaryExportFileInputStream.read(transferByteArray).also { bytesRead = it } > 0) {
1067                 exportFileOutputStream.write(transferByteArray, 0, bytesRead)
1068             }
1069
1070             // Flush the export file output stream.
1071             exportFileOutputStream.flush()
1072
1073             // Close the file streams.
1074             temporaryExportFileInputStream.close()
1075             exportFileOutputStream.close()
1076
1077             // Delete the temporary export file database, journal, and other related auxiliary files.
1078             SQLiteDatabase.deleteDatabase(temporaryExportFile)
1079
1080             // Return the export successful string.
1081             EXPORT_SUCCESSFUL
1082         } catch (exception: Exception) {
1083             // Return the export error.
1084             exception.toString()
1085         }
1086     }
1087
1088     // This method is used to convert the old domain settings switches to spinners.
1089     private fun convertFromSwitchToSpinner(systemDefault: Boolean, currentDatabaseInteger: Int): Int {
1090         // Return the new spinner integer.
1091         return if ((!systemDefault && (currentDatabaseInteger == 0)) ||
1092             (systemDefault && (currentDatabaseInteger == 1)))  // The system default is currently selected.
1093             SYSTEM_DEFAULT
1094         else if (currentDatabaseInteger == 0)  // The switch is currently disabled and that is not the system default.
1095             DISABLED
1096         else  // The switch is currently enabled and that is not the system default.
1097             ENABLED
1098     }
1099
1100     private fun generateFolderId(database: SQLiteDatabase): Long {
1101         // Get the current time in epoch format.
1102         val possibleFolderId = Date().time
1103
1104         // Get a cursor with any folders that already have this folder ID.
1105         val existingFolderCursor = database.rawQuery("SELECT $ID FROM $BOOKMARKS_TABLE WHERE $FOLDER_ID = $possibleFolderId", null)
1106
1107         // Check if the folder ID is unique.
1108         val folderIdIsUnique = (existingFolderCursor.count == 0)
1109
1110         // Close the cursor.
1111         existingFolderCursor.close()
1112
1113         // Either return the folder ID or test a new one.
1114         return if (folderIdIsUnique)
1115             possibleFolderId
1116         else
1117             generateFolderId(database)
1118     }
1119 }