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