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