]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/ImportExportDatabaseHelper.kt
8f1b93c0ea87d8fae2357ea6b9d87157403e5e5f
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / ImportExportDatabaseHelper.kt
1 /*
2  * Copyright 2018-2024 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_PROVIDER = "download_provider"
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 bottomAppBar = sharedPreferences.getBoolean(BOTTOM_APP_BAR, false)
365                 val downloadProviderString = sharedPreferences.getString(DOWNLOAD_PROVIDER, context.getString(R.string.download_provider_default_value))
366
367                 // Get the download provider entry values string array.
368                 val tempDownloadProviderEntryValuesStringArray = context.resources.getStringArray(R.array.download_provider_entry_values)
369
370                 // Populate the new download with external app preference.  It was added in this version of the schema, but removed in version 18.
371                 // The new preference, `download_provider`, converts `download_with_external_app`, so it needs to exist.
372                 // This code sets `download_with_external_app` to be as similar as possible to the current preference in the settings.
373                 if (downloadProviderString == tempDownloadProviderEntryValuesStringArray[0])  // Download with Privacy Browser.
374                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET download_with_external_app = 0")
375                 else  // Download with external app.
376                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET download_with_external_app = 1")
377
378                 // Populate the preferences table with the current bottom app bar value.
379                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
380                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
381                 if (bottomAppBar)
382                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $BOTTOM_APP_BAR = 1")
383                 else
384                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $BOTTOM_APP_BAR = 0")
385             }
386
387             // Upgrade from schema version 14, first used in Privacy Browser 3.8, to schema version 15, first used in Privacy Browser 3.11.
388             if (importDatabaseVersion < 15) {
389                 // `facebook_click_ids` was removed from the preferences table.
390                 // There is no need to delete the columns as they will simply be ignored by the import.
391
392                 // `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.
393
394                 // Create the new URL modification columns.
395                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $TRACKING_QUERIES BOOLEAN")
396                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $AMP_REDIRECTS BOOLEAN")
397
398                 // Copy the data from the old columns to the new ones.
399                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $TRACKING_QUERIES = google_analytics")
400                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $AMP_REDIRECTS = twitter_amp_redirects")
401             }
402
403             // Upgrade from schema version 15, first used in Privacy Browser 3.11, to schema version 16, first used in Privacy Browser 3.12.
404             // This upgrade removed the `x_requested_with_header` from the domains and preferences tables.
405             // There is no need to delete the columns as they will simply be ignored by the import.
406
407             // Upgrade from schema version 16, first used in Privacy Browser 3.12, to schema version 17, first used in Privacy Browser 3.15.
408             if (importDatabaseVersion < 17) {
409                 // Add the folder ID column.
410                 importDatabase.execSQL("ALTER TABLE $BOOKMARKS_TABLE ADD COLUMN $FOLDER_ID INTEGER")
411
412                 // Get a cursor with all the folders.
413                 val foldersCursor = importDatabase.rawQuery("SELECT $ID FROM $BOOKMARKS_TABLE WHERE $IS_FOLDER = 1", null)
414
415                 // Get the folders cursor ID column index.
416                 val foldersCursorIdColumnIndex = foldersCursor.getColumnIndexOrThrow(ID)
417
418                 // Add a folder ID to each folder.
419                 while(foldersCursor.moveToNext()) {
420                     // Get the current folder database ID.
421                     val databaseId = foldersCursor.getInt(foldersCursorIdColumnIndex)
422
423                     // Generate a folder ID.
424                     val folderId = generateFolderId(importDatabase)
425
426                     // Create a folder content values.
427                     val folderContentValues = ContentValues()
428
429                     // Store the new folder ID in the content values.
430                     folderContentValues.put(FOLDER_ID, folderId)
431
432                     // Update the folder with the new folder ID.
433                     importDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
434                 }
435
436                 // Close the folders cursor.
437                 foldersCursor.close()
438
439
440                 // Add the parent folder ID column.
441                 importDatabase.execSQL("ALTER TABLE $BOOKMARKS_TABLE ADD COLUMN $PARENT_FOLDER_ID INTEGER")
442
443                 // Get a cursor with all the bookmarks.
444                 val bookmarksCursor = importDatabase.rawQuery("SELECT $ID, parentfolder FROM $BOOKMARKS_TABLE", null)
445
446                 // Get the bookmarks cursor ID column index.
447                 val bookmarksCursorIdColumnIndex = bookmarksCursor.getColumnIndexOrThrow(ID)
448                 val bookmarksCursorParentFolderColumnIndex = bookmarksCursor.getColumnIndexOrThrow("parentfolder")
449
450                 // Populate the parent folder ID for each bookmark.
451                 while(bookmarksCursor.moveToNext()) {
452                     // Get the information from the cursor.
453                     val databaseId = bookmarksCursor.getInt(bookmarksCursorIdColumnIndex)
454                     val oldParentFolderString = bookmarksCursor.getString(bookmarksCursorParentFolderColumnIndex)
455
456                     // Initialize the new parent folder ID.
457                     var newParentFolderId = HOME_FOLDER_ID
458
459                     // Get the parent folder ID if the bookmark is not in the home folder.
460                     if (oldParentFolderString.isNotEmpty()) {
461                         // SQL escape the old parent folder string.
462                         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(oldParentFolderString)
463
464                         // Get the parent folder cursor.
465                         val parentFolderCursor = importDatabase.rawQuery("SELECT $FOLDER_ID FROM $BOOKMARKS_TABLE WHERE $BOOKMARK_NAME = $sqlEscapedFolderName AND $IS_FOLDER = 1", null)
466
467                         // Get the new parent folder ID if it exists.
468                         if (parentFolderCursor.count > 0) {
469                             // Move to the first entry.
470                             parentFolderCursor.moveToFirst()
471
472                             // Get the new parent folder ID.
473                             newParentFolderId = parentFolderCursor.getLong(parentFolderCursor.getColumnIndexOrThrow(FOLDER_ID))
474                         }
475
476                         // Close the parent folder cursor.
477                         parentFolderCursor.close()
478                     }
479
480                     // Create a bookmark content values.
481                     val bookmarkContentValues = ContentValues()
482
483                     // Store the new parent folder ID in the content values.
484                     bookmarkContentValues.put(PARENT_FOLDER_ID, newParentFolderId)
485
486                     // Update the folder with the new folder ID.
487                     importDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
488                 }
489
490                 // Close the bookmarks cursor.
491                 bookmarksCursor.close()
492
493                 // This upgrade removed the old `parentfolder` string column.
494                 // SQLite amazingly only added a command to drop a column in version 3.35.0.  <https://www.sqlite.org/changes.html>
495                 // It will be a while before that is supported in Android.  <https://developer.android.com/reference/android/database/sqlite/package-summary>
496                 // 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.
497             }
498
499             // Upgrade from schema version 17, first used in Privacy Browser 3.15, to schema version 18, first used in Privacy Browser 3.17.
500             if (importDatabaseVersion < 18) {
501                 // Create the new columns.
502                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DISPLAY_UNDER_CUTOUTS BOOLEAN")
503                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DOWNLOAD_PROVIDER TEXT")
504
505                 // Get the current display under cutout value.
506                 val displayUnderCutouts = sharedPreferences.getBoolean(DISPLAY_UNDER_CUTOUTS, false)
507
508                 // Populate the preferences table with the current display under cutouts value.
509                 // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
510                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
511                 if (displayUnderCutouts)
512                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DISPLAY_UNDER_CUTOUTS = 1")
513                 else
514                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DISPLAY_UNDER_CUTOUTS = 0")
515
516                 // Get the download with external app cursor.
517                 val downloadWithExternalAppCursor = importDatabase.rawQuery("SELECT download_with_external_app FROM $PREFERENCES_TABLE", null)
518
519                 // Move to the first entry.
520                 downloadWithExternalAppCursor.moveToFirst()
521
522                 // Get the old download with external app setting.
523                 val downloadWithExternalApp = (downloadWithExternalAppCursor.getInt(downloadWithExternalAppCursor.getColumnIndexOrThrow("download_with_external_app")) == 1)
524
525                 // Close the cursor.
526                 downloadWithExternalAppCursor.close()
527
528                 // Get the download provider entry values string array.
529                 val downloadProviderEntryValuesStringArray = context.resources.getStringArray(R.array.download_provider_entry_values)
530
531                 // Populate the new download provider preference.
532                 if (downloadWithExternalApp)  // Download with external app.
533                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_PROVIDER = '${downloadProviderEntryValuesStringArray[2]}'")
534                 else  // Download with Privacy Browser.
535                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_PROVIDER = '${downloadProviderEntryValuesStringArray[0]}'")
536             }
537
538             /* End of database upgrade logic. */
539
540
541             // Get a cursor for the bookmarks table.
542             val importBookmarksCursor = importDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE", null)
543
544             // Delete the current bookmarks database.
545             context.deleteDatabase(BOOKMARKS_DATABASE)
546
547             // Create a new bookmarks database.
548             val bookmarksDatabaseHelper = BookmarksDatabaseHelper(context)
549
550             // Move to the first record.
551             importBookmarksCursor.moveToFirst()
552
553             // Get the bookmarks colum indexes.
554             val bookmarkNameColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(BOOKMARK_NAME)
555             val bookmarkUrlColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)
556             val bookmarkParentFolderIdColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(PARENT_FOLDER_ID)
557             val bookmarkDisplayOrderColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(DISPLAY_ORDER)
558             val bookmarkIsFolderColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(IS_FOLDER)
559             val bookmarkFolderIdColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(FOLDER_ID)
560             val bookmarkFavoriteIconColumnIndex = importBookmarksCursor.getColumnIndexOrThrow(FAVORITE_ICON)
561
562             // Copy the data from the import bookmarks cursor into the bookmarks database.
563             for (i in 0 until importBookmarksCursor.count) {
564                 // Create a bookmark content values.
565                 val bookmarkContentValues = ContentValues()
566
567                 // Add the information for this bookmark to the content values.
568                 bookmarkContentValues.put(BOOKMARK_NAME, importBookmarksCursor.getString(bookmarkNameColumnIndex))
569                 bookmarkContentValues.put(BOOKMARK_URL, importBookmarksCursor.getString(bookmarkUrlColumnIndex))
570                 bookmarkContentValues.put(PARENT_FOLDER_ID, importBookmarksCursor.getLong(bookmarkParentFolderIdColumnIndex))
571                 bookmarkContentValues.put(DISPLAY_ORDER, importBookmarksCursor.getInt(bookmarkDisplayOrderColumnIndex))
572                 bookmarkContentValues.put(IS_FOLDER, importBookmarksCursor.getInt(bookmarkIsFolderColumnIndex))
573                 bookmarkContentValues.put(FOLDER_ID, importBookmarksCursor.getLong(bookmarkFolderIdColumnIndex))
574                 bookmarkContentValues.put(FAVORITE_ICON, importBookmarksCursor.getBlob(bookmarkFavoriteIconColumnIndex))
575
576                 // Insert the content values into the bookmarks database.
577                 bookmarksDatabaseHelper.createBookmark(bookmarkContentValues)
578
579                 // Advance to the next record.
580                 importBookmarksCursor.moveToNext()
581             }
582
583             // Upgrade from schema version 16, first used in Privacy Browser 3.12, to schema version 17, first used in Privacy Browser 3.15.
584             if (importDatabaseVersion < 16) {
585                 // Get the current switch default values.
586                 val javaScriptDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.javascript_key), false)
587                 val cookiesDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.cookies_key), false)
588                 val domStorageDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.dom_storage_key), false)
589                 val formDataDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.save_form_data_key), false)
590                 val easyListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.easylist_key), true)
591                 val easyPrivacyDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.easyprivacy_key), true)
592                 val fanboysAnnoyanceListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.fanboys_annoyance_list_key), true)
593                 val fanboysSocialBlockingListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.fanboys_social_blocking_list), true)
594                 val ultraListDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.ultralist_key), true)
595                 val ultraPrivacyDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.ultraprivacy_key), true)
596                 val blockAllThirdPartyRequestsDefaultValue = sharedPreferences.getBoolean(context.getString(R.string.block_all_third_party_requests_key), false)
597
598                 // Get a domains cursor.
599                 val importDomainsConversionCursor = importDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE", null)
600
601                 // Get the domains column indexes.
602                 val javaScriptColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_JAVASCRIPT)
603                 val cookiesColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(COOKIES)
604                 val domStorageColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_DOM_STORAGE)
605                 val formDataColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_FORM_DATA)
606                 val easyListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_EASYLIST)
607                 val easyPrivacyColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
608                 val fanboysAnnoyanceListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_ANNOYANCE_LIST)
609                 val fanboysSocialBlockingListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)
610                 val ultraListColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ULTRALIST)
611                 val ultraPrivacyColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(ENABLE_ULTRAPRIVACY)
612                 val blockAllThirdPartyRequestsColumnIndex = importDomainsConversionCursor.getColumnIndexOrThrow(BLOCK_ALL_THIRD_PARTY_REQUESTS)
613
614                 // Convert the domain from the switch booleans to the spinner integers.
615                 for (i in 0 until importDomainsConversionCursor.count) {
616                     // Move to the current record.
617                     importDomainsConversionCursor.moveToPosition(i)
618
619                     // Get the domain current values.
620                     val javaScriptDomainCurrentValue = importDomainsConversionCursor.getInt(javaScriptColumnIndex)
621                     val cookiesDomainCurrentValue = importDomainsConversionCursor.getInt(cookiesColumnIndex)
622                     val domStorageDomainCurrentValue = importDomainsConversionCursor.getInt(domStorageColumnIndex)
623                     val formDataDomainCurrentValue = importDomainsConversionCursor.getInt(formDataColumnIndex)
624                     val easyListDomainCurrentValue = importDomainsConversionCursor.getInt(easyListColumnIndex)
625                     val easyPrivacyDomainCurrentValue = importDomainsConversionCursor.getInt(easyPrivacyColumnIndex)
626                     val fanboysAnnoyanceListCurrentValue = importDomainsConversionCursor.getInt(fanboysAnnoyanceListColumnIndex)
627                     val fanboysSocialBlockingListCurrentValue = importDomainsConversionCursor.getInt(fanboysSocialBlockingListColumnIndex)
628                     val ultraListCurrentValue = importDomainsConversionCursor.getInt(ultraListColumnIndex)
629                     val ultraPrivacyCurrentValue = importDomainsConversionCursor.getInt(ultraPrivacyColumnIndex)
630                     val blockAllThirdPartyRequestsCurrentValue = importDomainsConversionCursor.getInt(blockAllThirdPartyRequestsColumnIndex)
631
632                     // Instantiate a domain content values.
633                     val domainContentValues = ContentValues()
634
635                     // Populate the domain content values.
636                     domainContentValues.put(ENABLE_JAVASCRIPT, convertFromSwitchToSpinner(javaScriptDefaultValue, javaScriptDomainCurrentValue))
637                     domainContentValues.put(COOKIES, convertFromSwitchToSpinner(cookiesDefaultValue, cookiesDomainCurrentValue))
638                     domainContentValues.put(ENABLE_DOM_STORAGE, convertFromSwitchToSpinner(domStorageDefaultValue, domStorageDomainCurrentValue))
639                     domainContentValues.put(ENABLE_FORM_DATA, convertFromSwitchToSpinner(formDataDefaultValue, formDataDomainCurrentValue))
640                     domainContentValues.put(ENABLE_EASYLIST, convertFromSwitchToSpinner(easyListDefaultValue, easyListDomainCurrentValue))
641                     domainContentValues.put(ENABLE_EASYPRIVACY, convertFromSwitchToSpinner(easyPrivacyDefaultValue, easyPrivacyDomainCurrentValue))
642                     domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, convertFromSwitchToSpinner(fanboysAnnoyanceListDefaultValue, fanboysAnnoyanceListCurrentValue))
643                     domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, convertFromSwitchToSpinner(fanboysSocialBlockingListDefaultValue, fanboysSocialBlockingListCurrentValue))
644                     domainContentValues.put(ULTRALIST, convertFromSwitchToSpinner(ultraListDefaultValue, ultraListCurrentValue))
645                     domainContentValues.put(ENABLE_ULTRAPRIVACY, convertFromSwitchToSpinner(ultraPrivacyDefaultValue, ultraPrivacyCurrentValue))
646                     domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, convertFromSwitchToSpinner(blockAllThirdPartyRequestsDefaultValue, blockAllThirdPartyRequestsCurrentValue))
647
648                     // Get the current database ID.
649                     val currentDatabaseId = importDomainsConversionCursor.getInt(importDomainsConversionCursor.getColumnIndexOrThrow(ID))
650
651                     // Update the row for the specified database ID.
652                     importDatabase.update(DOMAINS_TABLE, domainContentValues, "$ID = $currentDatabaseId", null)
653                 }
654
655                 // Close the cursor.
656                 importDomainsConversionCursor.close()
657             }
658
659             // Close the bookmarks cursor and database.
660             importBookmarksCursor.close()
661             bookmarksDatabaseHelper.close()
662
663
664             // Get a cursor for the domains table.
665             val importDomainsCursor = importDatabase.rawQuery("SELECT * FROM $DOMAINS_TABLE ORDER BY $DOMAIN_NAME ASC", null)
666
667             // Delete the current domains database.
668             context.deleteDatabase(DOMAINS_DATABASE)
669
670             // Create a new domains database.
671             val domainsDatabaseHelper = DomainsDatabaseHelper(context)
672
673             // Move to the first record.
674             importDomainsCursor.moveToFirst()
675
676             // Get the domain column indexes.
677             val domainNameColumnIndex = importDomainsCursor.getColumnIndexOrThrow(DOMAIN_NAME)
678             val domainJavaScriptColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_JAVASCRIPT)
679             val domainCookiesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(COOKIES)
680             val domainDomStorageColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_DOM_STORAGE)
681             val domainFormDataColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_FORM_DATA)  // Form data can be removed once the minimum API >= 26.
682             val domainEasyListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_EASYLIST)
683             val domainEasyPrivacyColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
684             val domainFanboysAnnoyanceListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_ANNOYANCE_LIST)
685             val domainFanboysSocialBlockingListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)
686             val domainUltraListColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ULTRALIST)
687             val domainUltraPrivacyColumnIndex = importDomainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
688             val domainBlockAllThirdPartyRequestsColumnIndex = importDomainsCursor.getColumnIndexOrThrow(BLOCK_ALL_THIRD_PARTY_REQUESTS)
689             val domainUserAgentColumnIndex = importDomainsCursor.getColumnIndexOrThrow(USER_AGENT)
690             val domainFontSizeColumnIndex = importDomainsCursor.getColumnIndexOrThrow(FONT_SIZE)
691             val domainSwipeToRefreshColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SWIPE_TO_REFRESH)
692             val domainWebViewThemeColumnIndex = importDomainsCursor.getColumnIndexOrThrow(WEBVIEW_THEME)
693             val domainWideViewportColumnIndex = importDomainsCursor.getColumnIndexOrThrow(WIDE_VIEWPORT)
694             val domainDisplayImagesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(DISPLAY_IMAGES)
695             val domainPinnedSslCertificateColumnIndex = importDomainsCursor.getColumnIndexOrThrow(PINNED_SSL_CERTIFICATE)
696             val domainSslIssuedToCommonNameColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_COMMON_NAME)
697             val domainSslIssuedToOrganizationColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATION)
698             val domainSslIssuedToOrganizationalUnitColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)
699             val domainSslIssuedByCommonNameColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_COMMON_NAME)
700             val domainSslIssuedByOrganizationColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATION)
701             val domainSslIssuedByOrganizationalUnitColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)
702             val domainSslStartDateColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_START_DATE)
703             val domainSslEndDateColumnIndex = importDomainsCursor.getColumnIndexOrThrow(SSL_END_DATE)
704             val domainPinnedIpAddressesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(PINNED_IP_ADDRESSES)
705             val domainIpAddressesColumnIndex = importDomainsCursor.getColumnIndexOrThrow(IP_ADDRESSES)
706
707             // Copy the data from the import domains cursor into the domains database.
708             for (i in 0 until importDomainsCursor.count) {
709                 // Create a domain content values.
710                 val domainContentValues = ContentValues()
711
712                 // Populate the domain content values.
713                 domainContentValues.put(DOMAIN_NAME, importDomainsCursor.getString(domainNameColumnIndex))
714                 domainContentValues.put(ENABLE_JAVASCRIPT, importDomainsCursor.getInt(domainJavaScriptColumnIndex))
715                 domainContentValues.put(COOKIES, importDomainsCursor.getInt(domainCookiesColumnIndex))
716                 domainContentValues.put(ENABLE_DOM_STORAGE, importDomainsCursor.getInt(domainDomStorageColumnIndex))
717                 domainContentValues.put(ENABLE_FORM_DATA, importDomainsCursor.getInt(domainFormDataColumnIndex))  // Form data can be removed once the minimum API >= 26.
718                 domainContentValues.put(ENABLE_EASYLIST, importDomainsCursor.getInt(domainEasyListColumnIndex))
719                 domainContentValues.put(ENABLE_EASYPRIVACY, importDomainsCursor.getInt(domainEasyPrivacyColumnIndex))
720                 domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, importDomainsCursor.getInt(domainFanboysAnnoyanceListColumnIndex))
721                 domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, importDomainsCursor.getInt(domainFanboysSocialBlockingListColumnIndex))
722                 domainContentValues.put(ULTRALIST, importDomainsCursor.getInt(domainUltraListColumnIndex))
723                 domainContentValues.put(ENABLE_ULTRAPRIVACY, importDomainsCursor.getInt(domainUltraPrivacyColumnIndex))
724                 domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, importDomainsCursor.getInt(domainBlockAllThirdPartyRequestsColumnIndex))
725                 domainContentValues.put(USER_AGENT, importDomainsCursor.getString(domainUserAgentColumnIndex))
726                 domainContentValues.put(FONT_SIZE, importDomainsCursor.getInt(domainFontSizeColumnIndex))
727                 domainContentValues.put(SWIPE_TO_REFRESH, importDomainsCursor.getInt(domainSwipeToRefreshColumnIndex))
728                 domainContentValues.put(WEBVIEW_THEME, importDomainsCursor.getInt(domainWebViewThemeColumnIndex))
729                 domainContentValues.put(WIDE_VIEWPORT, importDomainsCursor.getInt(domainWideViewportColumnIndex))
730                 domainContentValues.put(DISPLAY_IMAGES, importDomainsCursor.getInt(domainDisplayImagesColumnIndex))
731                 domainContentValues.put(PINNED_SSL_CERTIFICATE, importDomainsCursor.getInt(domainPinnedSslCertificateColumnIndex))
732                 domainContentValues.put(SSL_ISSUED_TO_COMMON_NAME, importDomainsCursor.getString(domainSslIssuedToCommonNameColumnIndex))
733                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATION, importDomainsCursor.getString(domainSslIssuedToOrganizationColumnIndex))
734                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, importDomainsCursor.getString(domainSslIssuedToOrganizationalUnitColumnIndex))
735                 domainContentValues.put(SSL_ISSUED_BY_COMMON_NAME, importDomainsCursor.getString(domainSslIssuedByCommonNameColumnIndex))
736                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATION, importDomainsCursor.getString(domainSslIssuedByOrganizationColumnIndex))
737                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, importDomainsCursor.getString(domainSslIssuedByOrganizationalUnitColumnIndex))
738                 domainContentValues.put(SSL_START_DATE, importDomainsCursor.getLong(domainSslStartDateColumnIndex))
739                 domainContentValues.put(SSL_END_DATE, importDomainsCursor.getLong(domainSslEndDateColumnIndex))
740                 domainContentValues.put(PINNED_IP_ADDRESSES, importDomainsCursor.getInt(domainPinnedIpAddressesColumnIndex))
741                 domainContentValues.put(IP_ADDRESSES, importDomainsCursor.getString(domainIpAddressesColumnIndex))
742
743                 // Insert the content values into the domains database.
744                 domainsDatabaseHelper.addDomain(domainContentValues)
745
746                 // Advance to the next record.
747                 importDomainsCursor.moveToNext()
748             }
749
750             // Close the domains cursor and database.
751             importDomainsCursor.close()
752             domainsDatabaseHelper.close()
753
754
755             // Get a cursor for the preferences table.
756             val importPreferencesCursor = importDatabase.rawQuery("SELECT * FROM $PREFERENCES_TABLE", null)
757
758             // Move to the first record.
759             importPreferencesCursor.moveToFirst()
760
761             // Import the preference data.
762             sharedPreferences.edit()
763                 .putBoolean(JAVASCRIPT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(JAVASCRIPT)) == 1)
764                 .putBoolean(COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(COOKIES)) == 1)
765                 .putBoolean(DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DOM_STORAGE)) == 1)
766                 .putBoolean(SAVE_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(SAVE_FORM_DATA)) == 1)  // Save form data can be removed once the minimum API >= 26.
767                 .putString(PREFERENCES_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_USER_AGENT)))
768                 .putString(CUSTOM_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(CUSTOM_USER_AGENT)))
769                 .putBoolean(INCOGNITO_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(INCOGNITO_MODE)) == 1)
770                 .putBoolean(ALLOW_SCREENSHOTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ALLOW_SCREENSHOTS)) == 1)
771                 .putBoolean(EASYLIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(EASYLIST)) == 1)
772                 .putBoolean(EASYPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(EASYPRIVACY)) == 1)
773                 .putBoolean(FANBOYS_ANNOYANCE_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FANBOYS_ANNOYANCE_LIST)) == 1)
774                 .putBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FANBOYS_SOCIAL_BLOCKING_LIST)) == 1)
775                 .putBoolean(ULTRALIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ULTRALIST)) == 1)
776                 .putBoolean(ULTRAPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ULTRAPRIVACY)) == 1)
777                 .putBoolean(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS)) == 1)
778                 .putBoolean(TRACKING_QUERIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(TRACKING_QUERIES)) == 1)
779                 .putBoolean(AMP_REDIRECTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(AMP_REDIRECTS)) == 1)
780                 .putString(SEARCH, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(SEARCH)))
781                 .putString(SEARCH_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(SEARCH_CUSTOM_URL)))
782                 .putString(PROXY, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PROXY)))
783                 .putString(PROXY_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PROXY_CUSTOM_URL)))
784                 .putBoolean(FULL_SCREEN_BROWSING_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FULL_SCREEN_BROWSING_MODE)) == 1)
785                 .putBoolean(HIDE_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(HIDE_APP_BAR)) == 1)
786                 .putBoolean(DISPLAY_UNDER_CUTOUTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_UNDER_CUTOUTS)) == 1)
787                 .putBoolean(CLEAR_EVERYTHING, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_EVERYTHING)) == 1)
788                 .putBoolean(CLEAR_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_COOKIES)) == 1)
789                 .putBoolean(CLEAR_DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_DOM_STORAGE)) == 1)
790                 .putBoolean(CLEAR_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_FORM_DATA)) == 1)  // Clear form data can be removed once the minimum API >= 26.
791                 .putBoolean(CLEAR_LOGCAT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_LOGCAT)) == 1)
792                 .putBoolean(CLEAR_CACHE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_CACHE)) == 1)
793                 .putString(HOMEPAGE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(HOMEPAGE)))
794                 .putString(PREFERENCES_FONT_SIZE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_FONT_SIZE)))
795                 .putBoolean(OPEN_INTENTS_IN_NEW_TAB, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(OPEN_INTENTS_IN_NEW_TAB)) == 1)
796                 .putBoolean(PREFERENCES_SWIPE_TO_REFRESH, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_SWIPE_TO_REFRESH)) == 1)
797                 .putString(DOWNLOAD_PROVIDER, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(DOWNLOAD_PROVIDER)))
798                 .putBoolean(SCROLL_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(SCROLL_APP_BAR)) == 1)
799                 .putBoolean(BOTTOM_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(BOTTOM_APP_BAR)) == 1)
800                 .putBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_ADDITIONAL_APP_BAR_ICONS)) == 1)
801                 .putString(APP_THEME, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(APP_THEME)))
802                 .putString(WEBVIEW_THEME, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(WEBVIEW_THEME)))
803                 .putBoolean(WIDE_VIEWPORT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(WIDE_VIEWPORT)) == 1)
804                 .putBoolean(DISPLAY_WEBPAGE_IMAGES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_WEBPAGE_IMAGES)) == 1)
805                 .apply()
806
807             // Close the preferences cursor and database.
808             importPreferencesCursor.close()
809             importDatabase.close()
810
811             // Delete the temporary import file database, journal, and other related auxiliary files.
812             SQLiteDatabase.deleteDatabase(temporaryImportFile)
813
814             // Return the import successful string.
815             IMPORT_SUCCESSFUL
816         } catch (exception: Exception) {
817             // Return the import error.
818             exception.toString()
819         }
820     }
821
822     fun exportUnencrypted(exportFileOutputStream: OutputStream, context: Context): String {
823         return try {
824             // Create a temporary export file.
825             val temporaryExportFile = File.createTempFile("temporary_export_file", null, context.cacheDir)
826
827             // Create the temporary export database.
828             val temporaryExportDatabase = SQLiteDatabase.openOrCreateDatabase(temporaryExportFile, null)
829
830             // Set the temporary export database version number.
831             temporaryExportDatabase.version = IMPORT_EXPORT_SCHEMA_VERSION
832
833
834             // Create the temporary export database bookmarks table.
835             temporaryExportDatabase.execSQL(CREATE_BOOKMARKS_TABLE)
836
837             // Open the bookmarks database.
838             val bookmarksDatabaseHelper = BookmarksDatabaseHelper(context)
839
840             // Get a full bookmarks cursor.
841             val bookmarksCursor = bookmarksDatabaseHelper.allBookmarks
842
843             // Move to the first record.
844             bookmarksCursor.moveToFirst()
845
846             // Get the bookmarks colum indexes.
847             val bookmarkNameColumnIndex = bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_NAME)
848             val bookmarkUrlColumnIndex = bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)
849             val bookmarkParentFolderIdColumnIndex = bookmarksCursor.getColumnIndexOrThrow(PARENT_FOLDER_ID)
850             val bookmarkDisplayOrderColumnIndex = bookmarksCursor.getColumnIndexOrThrow(DISPLAY_ORDER)
851             val bookmarkIsFolderColumnIndex = bookmarksCursor.getColumnIndexOrThrow(IS_FOLDER)
852             val bookmarkFolderIdColumnIndex = bookmarksCursor.getColumnIndexOrThrow(FOLDER_ID)
853             val bookmarkFavoriteIconColumnIndex = bookmarksCursor.getColumnIndexOrThrow(FAVORITE_ICON)
854
855             // Copy the data from the bookmarks cursor into the export database.
856             for (i in 0 until bookmarksCursor.count) {
857                 // Create a bookmark content values.
858                 val bookmarkContentValues = ContentValues()
859
860                 // Populate the bookmark content values.
861                 bookmarkContentValues.put(BOOKMARK_NAME, bookmarksCursor.getString(bookmarkNameColumnIndex))
862                 bookmarkContentValues.put(BOOKMARK_URL, bookmarksCursor.getString(bookmarkUrlColumnIndex))
863                 bookmarkContentValues.put(PARENT_FOLDER_ID, bookmarksCursor.getLong(bookmarkParentFolderIdColumnIndex))
864                 bookmarkContentValues.put(DISPLAY_ORDER, bookmarksCursor.getInt(bookmarkDisplayOrderColumnIndex))
865                 bookmarkContentValues.put(IS_FOLDER, bookmarksCursor.getInt(bookmarkIsFolderColumnIndex))
866                 bookmarkContentValues.put(FOLDER_ID, bookmarksCursor.getLong(bookmarkFolderIdColumnIndex))
867                 bookmarkContentValues.put(FAVORITE_ICON, bookmarksCursor.getBlob(bookmarkFavoriteIconColumnIndex))
868
869                 // Insert the content values into the temporary export database.
870                 temporaryExportDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues)
871
872                 // Advance to the next record.
873                 bookmarksCursor.moveToNext()
874             }
875
876             // Close the bookmarks cursor and database.
877             bookmarksCursor.close()
878             bookmarksDatabaseHelper.close()
879
880
881             // Create the temporary export database domains table.
882             temporaryExportDatabase.execSQL(CREATE_DOMAINS_TABLE)
883
884             // Open the domains database.
885             val domainsDatabaseHelper = DomainsDatabaseHelper(context)
886
887             // Get a full domains database cursor.
888             val domainsCursor = domainsDatabaseHelper.completeCursorOrderedByDomain
889
890             // Move to the first record.
891             domainsCursor.moveToFirst()
892
893             // Get the domain column indexes.
894             val domainNameColumnIndex = domainsCursor.getColumnIndexOrThrow(DOMAIN_NAME)
895             val domainJavaScriptColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_JAVASCRIPT)
896             val domainCookiesColumnIndex = domainsCursor.getColumnIndexOrThrow(COOKIES)
897             val domainDomStorageColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_DOM_STORAGE)
898             val domainFormDataColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_FORM_DATA)  // Form data can be removed once the minimum API >= 26.
899             val domainEasyListColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_EASYLIST)
900             val domainEasyPrivacyColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
901             val domainFanboysAnnoyanceListColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_ANNOYANCE_LIST)
902             val domainFanboysSocialBlockingListColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)
903             val domainUltraListColumnIndex = domainsCursor.getColumnIndexOrThrow(ULTRALIST)
904             val domainUltraPrivacyColumnIndex = domainsCursor.getColumnIndexOrThrow(ENABLE_EASYPRIVACY)
905             val domainBlockAllThirdPartyRequestsColumnIndex = domainsCursor.getColumnIndexOrThrow(BLOCK_ALL_THIRD_PARTY_REQUESTS)
906             val domainUserAgentColumnIndex = domainsCursor.getColumnIndexOrThrow(USER_AGENT)
907             val domainFontSizeColumnIndex = domainsCursor.getColumnIndexOrThrow(FONT_SIZE)
908             val domainSwipeToRefreshColumnIndex = domainsCursor.getColumnIndexOrThrow(SWIPE_TO_REFRESH)
909             val domainWebViewThemeColumnIndex = domainsCursor.getColumnIndexOrThrow(WEBVIEW_THEME)
910             val domainWideViewportColumnIndex = domainsCursor.getColumnIndexOrThrow(WIDE_VIEWPORT)
911             val domainDisplayImagesColumnIndex = domainsCursor.getColumnIndexOrThrow(DISPLAY_IMAGES)
912             val domainPinnedSslCertificateColumnIndex = domainsCursor.getColumnIndexOrThrow(PINNED_SSL_CERTIFICATE)
913             val domainSslIssuedToCommonNameColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_COMMON_NAME)
914             val domainSslIssuedToOrganizationColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATION)
915             val domainSslIssuedToOrganizationalUnitColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)
916             val domainSslIssuedByCommonNameColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_COMMON_NAME)
917             val domainSslIssuedByOrganizationColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATION)
918             val domainSslIssuedByOrganizationalUnitColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)
919             val domainSslStartDateColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_START_DATE)
920             val domainSslEndDateColumnIndex = domainsCursor.getColumnIndexOrThrow(SSL_END_DATE)
921             val domainPinnedIpAddressesColumnIndex = domainsCursor.getColumnIndexOrThrow(PINNED_IP_ADDRESSES)
922             val domainIpAddressesColumnIndex = domainsCursor.getColumnIndexOrThrow(IP_ADDRESSES)
923
924             // Copy the data from the domains cursor into the export database.
925             for (i in 0 until domainsCursor.count) {
926                 // Create a domain content values.
927                 val domainContentValues = ContentValues()
928
929                 // Populate the domain content values.
930                 domainContentValues.put(DOMAIN_NAME, domainsCursor.getString(domainNameColumnIndex))
931                 domainContentValues.put(ENABLE_JAVASCRIPT, domainsCursor.getInt(domainJavaScriptColumnIndex))
932                 domainContentValues.put(COOKIES, domainsCursor.getInt(domainCookiesColumnIndex))
933                 domainContentValues.put(ENABLE_DOM_STORAGE, domainsCursor.getInt(domainDomStorageColumnIndex))
934                 domainContentValues.put(ENABLE_FORM_DATA, domainsCursor.getInt(domainFormDataColumnIndex))  // Form data can be removed once the minimum API >= 26.
935                 domainContentValues.put(ENABLE_EASYLIST, domainsCursor.getInt(domainEasyListColumnIndex))
936                 domainContentValues.put(ENABLE_EASYPRIVACY, domainsCursor.getInt(domainEasyPrivacyColumnIndex))
937                 domainContentValues.put(ENABLE_FANBOYS_ANNOYANCE_LIST, domainsCursor.getInt(domainFanboysAnnoyanceListColumnIndex))
938                 domainContentValues.put(ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, domainsCursor.getInt(domainFanboysSocialBlockingListColumnIndex))
939                 domainContentValues.put(ULTRALIST, domainsCursor.getInt(domainUltraListColumnIndex))
940                 domainContentValues.put(ENABLE_ULTRAPRIVACY, domainsCursor.getInt(domainUltraPrivacyColumnIndex))
941                 domainContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, domainsCursor.getInt(domainBlockAllThirdPartyRequestsColumnIndex))
942                 domainContentValues.put(USER_AGENT, domainsCursor.getString(domainUserAgentColumnIndex))
943                 domainContentValues.put(FONT_SIZE, domainsCursor.getInt(domainFontSizeColumnIndex))
944                 domainContentValues.put(SWIPE_TO_REFRESH, domainsCursor.getInt(domainSwipeToRefreshColumnIndex))
945                 domainContentValues.put(WEBVIEW_THEME, domainsCursor.getInt(domainWebViewThemeColumnIndex))
946                 domainContentValues.put(WIDE_VIEWPORT, domainsCursor.getInt(domainWideViewportColumnIndex))
947                 domainContentValues.put(DISPLAY_IMAGES, domainsCursor.getInt(domainDisplayImagesColumnIndex))
948                 domainContentValues.put(PINNED_SSL_CERTIFICATE, domainsCursor.getInt(domainPinnedSslCertificateColumnIndex))
949                 domainContentValues.put(SSL_ISSUED_TO_COMMON_NAME, domainsCursor.getString(domainSslIssuedToCommonNameColumnIndex))
950                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATION, domainsCursor.getString(domainSslIssuedToOrganizationColumnIndex))
951                 domainContentValues.put(SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainSslIssuedToOrganizationalUnitColumnIndex))
952                 domainContentValues.put(SSL_ISSUED_BY_COMMON_NAME, domainsCursor.getString(domainSslIssuedByCommonNameColumnIndex))
953                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATION, domainsCursor.getString(domainSslIssuedByOrganizationColumnIndex))
954                 domainContentValues.put(SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainSslIssuedByOrganizationalUnitColumnIndex))
955                 domainContentValues.put(SSL_START_DATE, domainsCursor.getLong(domainSslStartDateColumnIndex))
956                 domainContentValues.put(SSL_END_DATE, domainsCursor.getLong(domainSslEndDateColumnIndex))
957                 domainContentValues.put(PINNED_IP_ADDRESSES, domainsCursor.getInt(domainPinnedIpAddressesColumnIndex))
958                 domainContentValues.put(IP_ADDRESSES, domainsCursor.getString(domainIpAddressesColumnIndex))
959
960                 // Insert the content values into the temporary export database.
961                 temporaryExportDatabase.insert(DOMAINS_TABLE, null, domainContentValues)
962
963                 // Advance to the next record.
964                 domainsCursor.moveToNext()
965             }
966
967             // Close the domains cursor and database.
968             domainsCursor.close()
969             domainsDatabaseHelper.close()
970
971
972             // Prepare the preferences table SQL creation string.
973             val createPreferencesTable = "CREATE TABLE $PREFERENCES_TABLE (" +
974                     "$ID INTEGER PRIMARY KEY, " +
975                     "$JAVASCRIPT BOOLEAN, " +
976                     "$COOKIES BOOLEAN, " +
977                     "$DOM_STORAGE BOOLEAN, " +
978                     "$SAVE_FORM_DATA BOOLEAN, " +
979                     "$PREFERENCES_USER_AGENT TEXT, " +
980                     "$CUSTOM_USER_AGENT TEXT, " +
981                     "$INCOGNITO_MODE BOOLEAN, " +
982                     "$ALLOW_SCREENSHOTS BOOLEAN, " +
983                     "$EASYLIST BOOLEAN, " +
984                     "$EASYPRIVACY BOOLEAN, " +
985                     "$FANBOYS_ANNOYANCE_LIST BOOLEAN, " +
986                     "$FANBOYS_SOCIAL_BLOCKING_LIST BOOLEAN, " +
987                     "$ULTRALIST BOOLEAN, " +
988                     "$ULTRAPRIVACY BOOLEAN, " +
989                     "$PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS BOOLEAN, " +
990                     "$TRACKING_QUERIES BOOLEAN, " +
991                     "$AMP_REDIRECTS BOOLEAN, " +
992                     "$SEARCH TEXT, " +
993                     "$SEARCH_CUSTOM_URL TEXT, " +
994                     "$PROXY TEXT, " +
995                     "$PROXY_CUSTOM_URL TEXT, " +
996                     "$FULL_SCREEN_BROWSING_MODE BOOLEAN, " +
997                     "$HIDE_APP_BAR BOOLEAN, " +
998                     "$DISPLAY_UNDER_CUTOUTS BOOLEAN, " +
999                     "$CLEAR_EVERYTHING BOOLEAN, " +
1000                     "$CLEAR_COOKIES BOOLEAN, " +
1001                     "$CLEAR_DOM_STORAGE BOOLEAN, " +
1002                     "$CLEAR_FORM_DATA BOOLEAN, " +
1003                     "$CLEAR_LOGCAT BOOLEAN, " +
1004                     "$CLEAR_CACHE BOOLEAN, " +
1005                     "$HOMEPAGE TEXT, " +
1006                     "$PREFERENCES_FONT_SIZE TEXT, " +
1007                     "$OPEN_INTENTS_IN_NEW_TAB BOOLEAN, " +
1008                     "$PREFERENCES_SWIPE_TO_REFRESH BOOLEAN, " +
1009                     "$DOWNLOAD_PROVIDER TEXT, " +
1010                     "$SCROLL_APP_BAR BOOLEAN, " +
1011                     "$BOTTOM_APP_BAR BOOLEAN, " +
1012                     "$DISPLAY_ADDITIONAL_APP_BAR_ICONS BOOLEAN, " +
1013                     "$APP_THEME TEXT, " +
1014                     "$WEBVIEW_THEME TEXT, " +
1015                     "$WIDE_VIEWPORT BOOLEAN, " +
1016                     "$DISPLAY_WEBPAGE_IMAGES BOOLEAN)"
1017
1018             // Create the temporary export database preferences table.
1019             temporaryExportDatabase.execSQL(createPreferencesTable)
1020
1021             // Get a handle for the shared preference.
1022             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
1023
1024             // Create a preferences content values.
1025             val preferencesContentValues = ContentValues()
1026
1027             // Populate the preferences content values.
1028             preferencesContentValues.put(JAVASCRIPT, sharedPreferences.getBoolean(JAVASCRIPT, false))
1029             preferencesContentValues.put(COOKIES, sharedPreferences.getBoolean(COOKIES, false))
1030             preferencesContentValues.put(DOM_STORAGE, sharedPreferences.getBoolean(DOM_STORAGE, false))
1031             preferencesContentValues.put(SAVE_FORM_DATA, sharedPreferences.getBoolean(SAVE_FORM_DATA, false))  // Save form data can be removed once the minimum API >= 26.
1032             preferencesContentValues.put(PREFERENCES_USER_AGENT, sharedPreferences.getString(PREFERENCES_USER_AGENT, context.getString(R.string.user_agent_default_value)))
1033             preferencesContentValues.put(CUSTOM_USER_AGENT, sharedPreferences.getString(CUSTOM_USER_AGENT, context.getString(R.string.custom_user_agent_default_value)))
1034             preferencesContentValues.put(INCOGNITO_MODE, sharedPreferences.getBoolean(INCOGNITO_MODE, false))
1035             preferencesContentValues.put(ALLOW_SCREENSHOTS, sharedPreferences.getBoolean(ALLOW_SCREENSHOTS, false))
1036             preferencesContentValues.put(EASYLIST, sharedPreferences.getBoolean(EASYLIST, true))
1037             preferencesContentValues.put(EASYPRIVACY, sharedPreferences.getBoolean(EASYPRIVACY, true))
1038             preferencesContentValues.put(FANBOYS_ANNOYANCE_LIST, sharedPreferences.getBoolean(FANBOYS_ANNOYANCE_LIST, true))
1039             preferencesContentValues.put(FANBOYS_SOCIAL_BLOCKING_LIST, sharedPreferences.getBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, true))
1040             preferencesContentValues.put(ULTRALIST, sharedPreferences.getBoolean(ULTRALIST, true))
1041             preferencesContentValues.put(ULTRAPRIVACY, sharedPreferences.getBoolean(ULTRAPRIVACY, true))
1042             preferencesContentValues.put(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS, sharedPreferences.getBoolean(PREFERENCES_BLOCK_ALL_THIRD_PARTY_REQUESTS, false))
1043             preferencesContentValues.put(TRACKING_QUERIES, sharedPreferences.getBoolean(TRACKING_QUERIES, true))
1044             preferencesContentValues.put(AMP_REDIRECTS, sharedPreferences.getBoolean(AMP_REDIRECTS, true))
1045             preferencesContentValues.put(SEARCH, sharedPreferences.getString(SEARCH, context.getString(R.string.search_default_value)))
1046             preferencesContentValues.put(SEARCH_CUSTOM_URL, sharedPreferences.getString(SEARCH_CUSTOM_URL, context.getString(R.string.search_custom_url_default_value)))
1047             preferencesContentValues.put(PROXY, sharedPreferences.getString(PROXY, context.getString(R.string.proxy_default_value)))
1048             preferencesContentValues.put(PROXY_CUSTOM_URL, sharedPreferences.getString(PROXY_CUSTOM_URL, context.getString(R.string.proxy_custom_url_default_value)))
1049             preferencesContentValues.put(FULL_SCREEN_BROWSING_MODE, sharedPreferences.getBoolean(FULL_SCREEN_BROWSING_MODE, false))
1050             preferencesContentValues.put(HIDE_APP_BAR, sharedPreferences.getBoolean(HIDE_APP_BAR, true))
1051             preferencesContentValues.put(DISPLAY_UNDER_CUTOUTS, sharedPreferences.getBoolean(DISPLAY_UNDER_CUTOUTS, false))
1052             preferencesContentValues.put(CLEAR_EVERYTHING, sharedPreferences.getBoolean(CLEAR_EVERYTHING, true))
1053             preferencesContentValues.put(CLEAR_COOKIES, sharedPreferences.getBoolean(CLEAR_COOKIES, true))
1054             preferencesContentValues.put(CLEAR_DOM_STORAGE, sharedPreferences.getBoolean(CLEAR_DOM_STORAGE, true))
1055             preferencesContentValues.put(CLEAR_FORM_DATA, sharedPreferences.getBoolean(CLEAR_FORM_DATA, true))  // Clear form data can be removed once the minimum API >= 26.
1056             preferencesContentValues.put(CLEAR_LOGCAT, sharedPreferences.getBoolean(CLEAR_LOGCAT, true))
1057             preferencesContentValues.put(CLEAR_CACHE, sharedPreferences.getBoolean(CLEAR_CACHE, true))
1058             preferencesContentValues.put(HOMEPAGE, sharedPreferences.getString(HOMEPAGE, context.getString(R.string.homepage_default_value)))
1059             preferencesContentValues.put(PREFERENCES_FONT_SIZE, sharedPreferences.getString(PREFERENCES_FONT_SIZE, context.getString(R.string.font_size_default_value)))
1060             preferencesContentValues.put(OPEN_INTENTS_IN_NEW_TAB, sharedPreferences.getBoolean(OPEN_INTENTS_IN_NEW_TAB, true))
1061             preferencesContentValues.put(PREFERENCES_SWIPE_TO_REFRESH, sharedPreferences.getBoolean(PREFERENCES_SWIPE_TO_REFRESH, true))
1062             preferencesContentValues.put(DOWNLOAD_PROVIDER, sharedPreferences.getString(DOWNLOAD_PROVIDER, context.getString(R.string.download_provider_default_value)))
1063             preferencesContentValues.put(SCROLL_APP_BAR, sharedPreferences.getBoolean(SCROLL_APP_BAR, true))
1064             preferencesContentValues.put(BOTTOM_APP_BAR, sharedPreferences.getBoolean(BOTTOM_APP_BAR, false))
1065             preferencesContentValues.put(DISPLAY_ADDITIONAL_APP_BAR_ICONS, sharedPreferences.getBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, false))
1066             preferencesContentValues.put(APP_THEME, sharedPreferences.getString(APP_THEME, context.getString(R.string.app_theme_default_value)))
1067             preferencesContentValues.put(WEBVIEW_THEME, sharedPreferences.getString(WEBVIEW_THEME, context.getString(R.string.webview_theme_default_value)))
1068             preferencesContentValues.put(WIDE_VIEWPORT, sharedPreferences.getBoolean(WIDE_VIEWPORT, true))
1069             preferencesContentValues.put(DISPLAY_WEBPAGE_IMAGES, sharedPreferences.getBoolean(DISPLAY_WEBPAGE_IMAGES, true))
1070
1071             // Insert the preferences content values into the temporary export database.
1072             temporaryExportDatabase.insert(PREFERENCES_TABLE, null, preferencesContentValues)
1073
1074             // Close the temporary export database.
1075             temporaryExportDatabase.close()
1076
1077
1078             // 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>
1079             // It can be copied in Android using `Files.copy` once the minimum API >= 26.
1080             // <https://developer.android.com/reference/java/nio/file/Files#copy(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...)>
1081             // 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>
1082
1083             // Create the temporary export file input stream.
1084             val temporaryExportFileInputStream = FileInputStream(temporaryExportFile)
1085
1086             // Create a byte array.
1087             val transferByteArray = ByteArray(1024)
1088
1089             // Create an integer to track the number of bytes read.
1090             var bytesRead: Int
1091
1092             // Copy the temporary export file to the export file output stream.
1093             while (temporaryExportFileInputStream.read(transferByteArray).also { bytesRead = it } > 0) {
1094                 exportFileOutputStream.write(transferByteArray, 0, bytesRead)
1095             }
1096
1097             // Flush the export file output stream.
1098             exportFileOutputStream.flush()
1099
1100             // Close the file streams.
1101             temporaryExportFileInputStream.close()
1102             exportFileOutputStream.close()
1103
1104             // Delete the temporary export file database, journal, and other related auxiliary files.
1105             SQLiteDatabase.deleteDatabase(temporaryExportFile)
1106
1107             // Return the export successful string.
1108             EXPORT_SUCCESSFUL
1109         } catch (exception: Exception) {
1110             // Return the export error.
1111             exception.toString()
1112         }
1113     }
1114
1115     // This method is used to convert the old domain settings switches to spinners.
1116     private fun convertFromSwitchToSpinner(systemDefault: Boolean, currentDatabaseInteger: Int): Int {
1117         // Return the new spinner integer.
1118         return if ((!systemDefault && (currentDatabaseInteger == 0)) ||
1119             (systemDefault && (currentDatabaseInteger == 1)))  // The system default is currently selected.
1120             SYSTEM_DEFAULT
1121         else if (currentDatabaseInteger == 0)  // The switch is currently disabled and that is not the system default.
1122             DISABLED
1123         else  // The switch is currently enabled and that is not the system default.
1124             ENABLED
1125     }
1126
1127     private fun generateFolderId(database: SQLiteDatabase): Long {
1128         // Get the current time in epoch format.
1129         val possibleFolderId = Date().time
1130
1131         // Get a cursor with any folders that already have this folder ID.
1132         val existingFolderCursor = database.rawQuery("SELECT $ID FROM $BOOKMARKS_TABLE WHERE $FOLDER_ID = $possibleFolderId", null)
1133
1134         // Check if the folder ID is unique.
1135         val folderIdIsUnique = (existingFolderCursor.count == 0)
1136
1137         // Close the cursor.
1138         existingFolderCursor.close()
1139
1140         // Either return the folder ID or test a new one.
1141         return if (folderIdIsUnique)
1142             possibleFolderId
1143         else
1144             generateFolderId(database)
1145     }
1146 }