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