]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/helpers/ImportExportDatabaseHelper.kt
Add import and export of the new download provider.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / ImportExportDatabaseHelper.kt
index 259a5b744793fec7083d1d9485897e15c5ade726..8f1b93c0ea87d8fae2357ea6b9d87157403e5e5f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2018-2023 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2018-2024 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -38,7 +38,7 @@ import java.io.OutputStream
 import java.util.Date
 
 // Define the public constants.
-const val IMPORT_EXPORT_SCHEMA_VERSION = 17
+const val IMPORT_EXPORT_SCHEMA_VERSION = 18
 const val EXPORT_SUCCESSFUL = "A"
 const val IMPORT_SUCCESSFUL = "B"
 
@@ -55,9 +55,10 @@ private const val CLEAR_FORM_DATA = "clear_form_data"  // Clear form data can be
 private const val CLEAR_LOGCAT = "clear_logcat"
 private const val CUSTOM_USER_AGENT = "custom_user_agent"
 private const val DISPLAY_ADDITIONAL_APP_BAR_ICONS = "display_additional_app_bar_icons"
+private const val DISPLAY_UNDER_CUTOUTS = "display_under_cutouts"
 private const val DISPLAY_WEBPAGE_IMAGES = "display_webpage_images"
 private const val DOM_STORAGE = "dom_storage"
-private const val DOWNLOAD_WITH_EXTERNAL_APP = "download_with_external_app"
+private const val DOWNLOAD_PROVIDER = "download_provider"
 private const val EASYLIST = "easylist"
 private const val EASYPRIVACY = "easyprivacy"
 private const val FANBOYS_ANNOYANCE_LIST = "fanboys_annoyance_list"
@@ -356,22 +357,27 @@ class ImportExportDatabaseHelper {
                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $COOKIES = first_party_cookies")
 
                 // Create the new download with external app and bottom app bar columns.
-                importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DOWNLOAD_WITH_EXTERNAL_APP BOOLEAN")
+                importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN download_with_external_app BOOLEAN")
                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $BOTTOM_APP_BAR BOOLEAN")
 
                 // Get the current values for the new columns.
-                val downloadWithExternalApp = sharedPreferences.getBoolean(DOWNLOAD_WITH_EXTERNAL_APP, false)
                 val bottomAppBar = sharedPreferences.getBoolean(BOTTOM_APP_BAR, false)
+                val downloadProviderString = sharedPreferences.getString(DOWNLOAD_PROVIDER, context.getString(R.string.download_provider_default_value))
 
-                // Populate the preferences table with the current download with external app value.
-                // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
-                // <https://developer.android.com/reference/android/database/sqlite/package-summary>
-                if (downloadWithExternalApp)
-                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_WITH_EXTERNAL_APP = 1")
-                else
-                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_WITH_EXTERNAL_APP = 0")
+                // Get the download provider entry values string array.
+                val tempDownloadProviderEntryValuesStringArray = context.resources.getStringArray(R.array.download_provider_entry_values)
+
+                // Populate the new download with external app preference.  It was added in this version of the schema, but removed in version 18.
+                // The new preference, `download_provider`, converts `download_with_external_app`, so it needs to exist.
+                // This code sets `download_with_external_app` to be as similar as possible to the current preference in the settings.
+                if (downloadProviderString == tempDownloadProviderEntryValuesStringArray[0])  // Download with Privacy Browser.
+                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET download_with_external_app = 0")
+                else  // Download with external app.
+                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET download_with_external_app = 1")
 
                 // Populate the preferences table with the current bottom app bar value.
+                // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
+                // <https://developer.android.com/reference/android/database/sqlite/package-summary>
                 if (bottomAppBar)
                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $BOTTOM_APP_BAR = 1")
                 else
@@ -490,6 +496,47 @@ class ImportExportDatabaseHelper {
                 // 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.
             }
 
+            // Upgrade from schema version 17, first used in Privacy Browser 3.15, to schema version 18, first used in Privacy Browser 3.17.
+            if (importDatabaseVersion < 18) {
+                // Create the new columns.
+                importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DISPLAY_UNDER_CUTOUTS BOOLEAN")
+                importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $DOWNLOAD_PROVIDER TEXT")
+
+                // Get the current display under cutout value.
+                val displayUnderCutouts = sharedPreferences.getBoolean(DISPLAY_UNDER_CUTOUTS, false)
+
+                // Populate the preferences table with the current display under cutouts value.
+                // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
+                // <https://developer.android.com/reference/android/database/sqlite/package-summary>
+                if (displayUnderCutouts)
+                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DISPLAY_UNDER_CUTOUTS = 1")
+                else
+                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DISPLAY_UNDER_CUTOUTS = 0")
+
+                // Get the download with external app cursor.
+                val downloadWithExternalAppCursor = importDatabase.rawQuery("SELECT download_with_external_app FROM $PREFERENCES_TABLE", null)
+
+                // Move to the first entry.
+                downloadWithExternalAppCursor.moveToFirst()
+
+                // Get the old download with external app setting.
+                val downloadWithExternalApp = (downloadWithExternalAppCursor.getInt(downloadWithExternalAppCursor.getColumnIndexOrThrow("download_with_external_app")) == 1)
+
+                // Close the cursor.
+                downloadWithExternalAppCursor.close()
+
+                // Get the download provider entry values string array.
+                val downloadProviderEntryValuesStringArray = context.resources.getStringArray(R.array.download_provider_entry_values)
+
+                // Populate the new download provider preference.
+                if (downloadWithExternalApp)  // Download with external app.
+                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_PROVIDER = '${downloadProviderEntryValuesStringArray[2]}'")
+                else  // Download with Privacy Browser.
+                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $DOWNLOAD_PROVIDER = '${downloadProviderEntryValuesStringArray[0]}'")
+            }
+
+            /* End of database upgrade logic. */
+
 
             // Get a cursor for the bookmarks table.
             val importBookmarksCursor = importDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE", null)
@@ -736,6 +783,7 @@ class ImportExportDatabaseHelper {
                 .putString(PROXY_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PROXY_CUSTOM_URL)))
                 .putBoolean(FULL_SCREEN_BROWSING_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(FULL_SCREEN_BROWSING_MODE)) == 1)
                 .putBoolean(HIDE_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(HIDE_APP_BAR)) == 1)
+                .putBoolean(DISPLAY_UNDER_CUTOUTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_UNDER_CUTOUTS)) == 1)
                 .putBoolean(CLEAR_EVERYTHING, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_EVERYTHING)) == 1)
                 .putBoolean(CLEAR_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_COOKIES)) == 1)
                 .putBoolean(CLEAR_DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(CLEAR_DOM_STORAGE)) == 1)
@@ -746,7 +794,7 @@ class ImportExportDatabaseHelper {
                 .putString(PREFERENCES_FONT_SIZE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_FONT_SIZE)))
                 .putBoolean(OPEN_INTENTS_IN_NEW_TAB, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(OPEN_INTENTS_IN_NEW_TAB)) == 1)
                 .putBoolean(PREFERENCES_SWIPE_TO_REFRESH, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(PREFERENCES_SWIPE_TO_REFRESH)) == 1)
-                .putBoolean(DOWNLOAD_WITH_EXTERNAL_APP, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DOWNLOAD_WITH_EXTERNAL_APP)) == 1)
+                .putString(DOWNLOAD_PROVIDER, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(DOWNLOAD_PROVIDER)))
                 .putBoolean(SCROLL_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(SCROLL_APP_BAR)) == 1)
                 .putBoolean(BOTTOM_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(BOTTOM_APP_BAR)) == 1)
                 .putBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(DISPLAY_ADDITIONAL_APP_BAR_ICONS)) == 1)
@@ -947,6 +995,7 @@ class ImportExportDatabaseHelper {
                     "$PROXY_CUSTOM_URL TEXT, " +
                     "$FULL_SCREEN_BROWSING_MODE BOOLEAN, " +
                     "$HIDE_APP_BAR BOOLEAN, " +
+                    "$DISPLAY_UNDER_CUTOUTS BOOLEAN, " +
                     "$CLEAR_EVERYTHING BOOLEAN, " +
                     "$CLEAR_COOKIES BOOLEAN, " +
                     "$CLEAR_DOM_STORAGE BOOLEAN, " +
@@ -957,7 +1006,7 @@ class ImportExportDatabaseHelper {
                     "$PREFERENCES_FONT_SIZE TEXT, " +
                     "$OPEN_INTENTS_IN_NEW_TAB BOOLEAN, " +
                     "$PREFERENCES_SWIPE_TO_REFRESH BOOLEAN, " +
-                    "$DOWNLOAD_WITH_EXTERNAL_APP BOOLEAN, " +
+                    "$DOWNLOAD_PROVIDER TEXT, " +
                     "$SCROLL_APP_BAR BOOLEAN, " +
                     "$BOTTOM_APP_BAR BOOLEAN, " +
                     "$DISPLAY_ADDITIONAL_APP_BAR_ICONS BOOLEAN, " +
@@ -999,6 +1048,7 @@ class ImportExportDatabaseHelper {
             preferencesContentValues.put(PROXY_CUSTOM_URL, sharedPreferences.getString(PROXY_CUSTOM_URL, context.getString(R.string.proxy_custom_url_default_value)))
             preferencesContentValues.put(FULL_SCREEN_BROWSING_MODE, sharedPreferences.getBoolean(FULL_SCREEN_BROWSING_MODE, false))
             preferencesContentValues.put(HIDE_APP_BAR, sharedPreferences.getBoolean(HIDE_APP_BAR, true))
+            preferencesContentValues.put(DISPLAY_UNDER_CUTOUTS, sharedPreferences.getBoolean(DISPLAY_UNDER_CUTOUTS, false))
             preferencesContentValues.put(CLEAR_EVERYTHING, sharedPreferences.getBoolean(CLEAR_EVERYTHING, true))
             preferencesContentValues.put(CLEAR_COOKIES, sharedPreferences.getBoolean(CLEAR_COOKIES, true))
             preferencesContentValues.put(CLEAR_DOM_STORAGE, sharedPreferences.getBoolean(CLEAR_DOM_STORAGE, true))
@@ -1009,7 +1059,7 @@ class ImportExportDatabaseHelper {
             preferencesContentValues.put(PREFERENCES_FONT_SIZE, sharedPreferences.getString(PREFERENCES_FONT_SIZE, context.getString(R.string.font_size_default_value)))
             preferencesContentValues.put(OPEN_INTENTS_IN_NEW_TAB, sharedPreferences.getBoolean(OPEN_INTENTS_IN_NEW_TAB, true))
             preferencesContentValues.put(PREFERENCES_SWIPE_TO_REFRESH, sharedPreferences.getBoolean(PREFERENCES_SWIPE_TO_REFRESH, true))
-            preferencesContentValues.put(DOWNLOAD_WITH_EXTERNAL_APP, sharedPreferences.getBoolean(DOWNLOAD_WITH_EXTERNAL_APP, false))
+            preferencesContentValues.put(DOWNLOAD_PROVIDER, sharedPreferences.getString(DOWNLOAD_PROVIDER, context.getString(R.string.download_provider_default_value)))
             preferencesContentValues.put(SCROLL_APP_BAR, sharedPreferences.getBoolean(SCROLL_APP_BAR, true))
             preferencesContentValues.put(BOTTOM_APP_BAR, sharedPreferences.getBoolean(BOTTOM_APP_BAR, false))
             preferencesContentValues.put(DISPLAY_ADDITIONAL_APP_BAR_ICONS, sharedPreferences.getBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, false))