]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/helpers/ImportExportDatabaseHelper.kt
Standardize suggested file names. https://redmine.stoutner.com/issues/951
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / ImportExportDatabaseHelper.kt
index 5dfd845e09c556bf4f10a44ef57d04c3c7005240..d1648f011a79924e613558eb92d8a105568a4607 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2018-2023 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -35,53 +35,49 @@ import java.io.InputStream
 import java.io.OutputStream
 
 // Define the private class constants.
-private const val SCHEMA_VERSION = 15
-private const val PREFERENCES_TABLE = "preferences"
-
-// Define the private preferences constants.
-private const val ID = "_id"
-private const val JAVASCRIPT = "javascript"
+private const val ALLOW_SCREENSHOTS = "allow_screenshots"
+private const val AMP_REDIRECTS = "amp_redirects"
+private const val APP_THEME = "app_theme"
+private const val BLOCK_ALL_THIRD_PARTY_REQUESTS = "block_all_third_party_requests"
+private const val BOTTOM_APP_BAR = "bottom_app_bar"
+private const val CLEAR_CACHE = "clear_cache"
+private const val CLEAR_COOKIES = "clear_cookies"
+private const val CLEAR_DOM_STORAGE = "clear_dom_storage"
+private const val CLEAR_EVERYTHING = "clear_everything"
+private const val CLEAR_FORM_DATA = "clear_form_data"  // Clear form data can be removed once the minimum API >= 26.
+private const val CLEAR_LOGCAT = "clear_logcat"
 private const val COOKIES = "cookies"
-private const val DOM_STORAGE = "dom_storage"
-private const val SAVE_FORM_DATA = "save_form_data"
-private const val USER_AGENT = "user_agent"
 private const val CUSTOM_USER_AGENT = "custom_user_agent"
-private const val X_REQUESTED_WITH_HEADER = "x_requested_with_header"
-private const val INCOGNITO_MODE = "incognito_mode"
-private const val ALLOW_SCREENSHOTS = "allow_screenshots"
+private const val DISPLAY_ADDITIONAL_APP_BAR_ICONS = "display_additional_app_bar_icons"
+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 EASYLIST = "easylist"
 private const val EASYPRIVACY = "easyprivacy"
 private const val FANBOYS_ANNOYANCE_LIST = "fanboys_annoyance_list"
 private const val FANBOYS_SOCIAL_BLOCKING_LIST = "fanboys_social_blocking_list"
-private const val ULTRALIST = "ultralist"
-private const val ULTRAPRIVACY = "ultraprivacy"
-private const val BLOCK_ALL_THIRD_PARTY_REQUESTS = "block_all_third_party_requests"
-private const val TRACKING_QUERIES = "tracking_queries"
-private const val AMP_REDIRECTS = "amp_redirects"
-private const val SEARCH = "search"
-private const val SEARCH_CUSTOM_URL = "search_custom_url"
-private const val PROXY = "proxy"
-private const val PROXY_CUSTOM_URL = "proxy_custom_url"
+private const val FONT_SIZE = "font_size"
 private const val FULL_SCREEN_BROWSING_MODE = "full_screen_browsing_mode"
 private const val HIDE_APP_BAR = "hide_app_bar"
-private const val CLEAR_EVERYTHING = "clear_everything"
-private const val CLEAR_COOKIES = "clear_cookies"
-private const val CLEAR_DOM_STORAGE = "clear_dom_storage"
-private const val CLEAR_FORM_DATA = "clear_form_data"
-private const val CLEAR_LOGCAT = "clear_logcat"
-private const val CLEAR_CACHE = "clear_cache"
 private const val HOMEPAGE = "homepage"
-private const val FONT_SIZE = "font_size"
+private const val ID = "_id"
+private const val INCOGNITO_MODE = "incognito_mode"
+private const val JAVASCRIPT = "javascript"
 private const val OPEN_INTENTS_IN_NEW_TAB = "open_intents_in_new_tab"
-private const val SWIPE_TO_REFRESH = "swipe_to_refresh"
-private const val DOWNLOAD_WITH_EXTERNAL_APP = "download_with_external_app"
+private const val PREFERENCES_TABLE = "preferences"
+private const val PROXY = "proxy"
+private const val PROXY_CUSTOM_URL = "proxy_custom_url"
+private const val SAVE_FORM_DATA = "save_form_data"
+private const val SEARCH = "search"
+private const val SEARCH_CUSTOM_URL = "search_custom_url"
 private const val SCROLL_APP_BAR = "scroll_app_bar"
-private const val BOTTOM_APP_BAR = "bottom_app_bar"
-private const val DISPLAY_ADDITIONAL_APP_BAR_ICONS = "display_additional_app_bar_icons"
-private const val APP_THEME = "app_theme"
+private const val SWIPE_TO_REFRESH = "swipe_to_refresh"
+private const val TRACKING_QUERIES = "tracking_queries"
+private const val ULTRALIST = "ultralist"
+private const val ULTRAPRIVACY = "ultraprivacy"
+private const val USER_AGENT = "user_agent"
 private const val WEBVIEW_THEME = "webview_theme"
 private const val WIDE_VIEWPORT = "wide_viewport"
-private const val DISPLAY_WEBPAGE_IMAGES = "display_webpage_images"
 
 class ImportExportDatabaseHelper {
     // Define the public companion object constants.  These can be moved to public class constants once the entire project has migrated to Kotlin.
@@ -89,6 +85,7 @@ class ImportExportDatabaseHelper {
         // Define the public class constants.
         const val EXPORT_SUCCESSFUL = "Export Successful"
         const val IMPORT_SUCCESSFUL = "Import Successful"
+        const val SCHEMA_VERSION = 16
     }
 
     fun importUnencrypted(importFileInputStream: InputStream, context: Context): String {
@@ -137,6 +134,7 @@ class ImportExportDatabaseHelper {
 
             // Upgrade from schema version 2, first used in Privacy Browser 2.14, to schema version 3, first used in Privacy Browser 2.15.
             if (importDatabaseVersion < 3) {
+                // `default_font_size` was renamed `font_size`.
                 // 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>
                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
                 // 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.
@@ -166,7 +164,7 @@ class ImportExportDatabaseHelper {
                 val scrollAppBar = sharedPreferences.getBoolean(SCROLL_APP_BAR, true)
 
                 // Populate the preferences table with the current app bar values.
-                // This can switch to using the variables directly once the API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
+                // This can switch to using the variables directly once the minimum API >= 30.  <https://www.sqlite.org/datatype3.html#boolean_datatype>
                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
                 if (hideAppBar)
                     importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $HIDE_APP_BAR = 1")
@@ -347,7 +345,9 @@ class ImportExportDatabaseHelper {
             // Upgrade from schema version 13, first used in Privacy Browser 3.7, to schema version 14, first used in Privacy Browser 3.8.
             if (importDatabaseVersion < 14) {
                 // `enabledthirdpartycookies` was removed from the domains table.  `do_not_track` and `third_party_cookies` were removed from the preferences table.
+                // There is no need to delete the columns as they will simply be ignored by the import.
 
+                // `enablefirstpartycookies` was renamed `cookies`.
                 // 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>
                 // <https://developer.android.com/reference/android/database/sqlite/package-summary>
                 // 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.
@@ -386,10 +386,9 @@ class ImportExportDatabaseHelper {
             // Upgrade from schema version 14, first used in Privacy Browser 3.8, to schema version 15, first used in Privacy Browser 3.11.
             if (importDatabaseVersion < 15) {
                 // `facebook_click_ids` was removed from the preferences table.
+                // There is no need to delete the columns as they will simply be ignored by the import.
 
-                // 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>
-                // <https://developer.android.com/reference/android/database/sqlite/package-summary>
-                // 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.
+                // `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.
 
                 // Create the new URL modification columns.
                 importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $TRACKING_QUERIES BOOLEAN")
@@ -398,23 +397,12 @@ class ImportExportDatabaseHelper {
                 // Copy the data from the old columns to the new ones.
                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $TRACKING_QUERIES = google_analytics")
                 importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $AMP_REDIRECTS = twitter_amp_redirects")
-
-                // Create the new X-Requested-with header columns.
-                importDatabase.execSQL("ALTER TABLE $PREFERENCES_TABLE ADD COLUMN $X_REQUESTED_WITH_HEADER BOOLEAN")
-                importDatabase.execSQL("ALTER TABLE ${DomainsDatabaseHelper.DOMAINS_TABLE} ADD COLUMN ${DomainsDatabaseHelper.X_REQUESTED_WITH_HEADER} INTEGER")
-
-                // Get the current X-Requested-With header preferences value.
-                val xRequestedWithHeader = sharedPreferences.getBoolean(X_REQUESTED_WITH_HEADER, true)
-
-                // Populate the Preferences X-Requested-With header with the current value.  The domains X-Requested-With header will default to 0, which is `System default`.
-                // 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 (xRequestedWithHeader)
-                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $X_REQUESTED_WITH_HEADER = 1")
-                else
-                    importDatabase.execSQL("UPDATE $PREFERENCES_TABLE SET $X_REQUESTED_WITH_HEADER = 0")
             }
 
+            // Upgrade from schema version 15, first used in Privacy Browser 3.11, to schema version 16, first used in Privacy Browser 3.12.
+            // This upgrade removed the `x_requested_with_header` from the domains and preferences tables.
+            // There is no need to delete the columns as they will simply be ignored by the import.
+
             // Get a cursor for the bookmarks table.
             val importBookmarksCursor = importDatabase.rawQuery("SELECT * FROM ${BookmarksDatabaseHelper.BOOKMARKS_TABLE}", null)
 
@@ -486,7 +474,6 @@ class ImportExportDatabaseHelper {
                 domainContentValues.put(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS,
                     importDomainsCursor.getInt(importDomainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)))
                 domainContentValues.put(DomainsDatabaseHelper.USER_AGENT, importDomainsCursor.getString(importDomainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.USER_AGENT)))
-                domainContentValues.put(DomainsDatabaseHelper.X_REQUESTED_WITH_HEADER, importDomainsCursor.getInt(importDomainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.X_REQUESTED_WITH_HEADER)))
                 domainContentValues.put(DomainsDatabaseHelper.FONT_SIZE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.FONT_SIZE)))
                 domainContentValues.put(DomainsDatabaseHelper.SWIPE_TO_REFRESH, importDomainsCursor.getInt(importDomainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.SWIPE_TO_REFRESH)))
                 domainContentValues.put(DomainsDatabaseHelper.WEBVIEW_THEME, importDomainsCursor.getInt(importDomainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.WEBVIEW_THEME)))
@@ -536,7 +523,6 @@ class ImportExportDatabaseHelper {
                 .putBoolean(SAVE_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(SAVE_FORM_DATA)) == 1)  // Save form data can be removed once the minimum API >= 26.
                 .putString(USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(USER_AGENT)))
                 .putString(CUSTOM_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndexOrThrow(CUSTOM_USER_AGENT)))
-                .putBoolean(X_REQUESTED_WITH_HEADER, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(X_REQUESTED_WITH_HEADER)) == 1)
                 .putBoolean(INCOGNITO_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(INCOGNITO_MODE)) == 1)
                 .putBoolean(ALLOW_SCREENSHOTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(ALLOW_SCREENSHOTS)) == 1)
                 .putBoolean(EASYLIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndexOrThrow(EASYLIST)) == 1)
@@ -670,7 +656,6 @@ class ImportExportDatabaseHelper {
                 domainContentValues.put(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY, domainsCursor.getInt(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)))
                 domainContentValues.put(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS, domainsCursor.getInt(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)))
                 domainContentValues.put(DomainsDatabaseHelper.USER_AGENT, domainsCursor.getString(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.USER_AGENT)))
-                domainContentValues.put(DomainsDatabaseHelper.X_REQUESTED_WITH_HEADER, domainsCursor.getInt(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.X_REQUESTED_WITH_HEADER)))
                 domainContentValues.put(DomainsDatabaseHelper.FONT_SIZE, domainsCursor.getInt(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.FONT_SIZE)))
                 domainContentValues.put(DomainsDatabaseHelper.SWIPE_TO_REFRESH, domainsCursor.getInt(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.SWIPE_TO_REFRESH)))
                 domainContentValues.put(DomainsDatabaseHelper.WEBVIEW_THEME, domainsCursor.getInt(domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.WEBVIEW_THEME)))
@@ -711,7 +696,6 @@ class ImportExportDatabaseHelper {
                     "$SAVE_FORM_DATA BOOLEAN, " +
                     "$USER_AGENT TEXT, " +
                     "$CUSTOM_USER_AGENT TEXT, " +
-                    "$X_REQUESTED_WITH_HEADER BOOLEAN, " +
                     "$INCOGNITO_MODE BOOLEAN, " +
                     "$ALLOW_SCREENSHOTS BOOLEAN, " +
                     "$EASYLIST BOOLEAN, " +
@@ -764,7 +748,6 @@ class ImportExportDatabaseHelper {
             preferencesContentValues.put(SAVE_FORM_DATA, sharedPreferences.getBoolean(SAVE_FORM_DATA, false))  // Save form data can be removed once the minimum API >= 26.
             preferencesContentValues.put(USER_AGENT, sharedPreferences.getString(USER_AGENT, context.getString(R.string.user_agent_default_value)))
             preferencesContentValues.put(CUSTOM_USER_AGENT, sharedPreferences.getString(CUSTOM_USER_AGENT, context.getString(R.string.custom_user_agent_default_value)))
-            preferencesContentValues.put(X_REQUESTED_WITH_HEADER, sharedPreferences.getBoolean(X_REQUESTED_WITH_HEADER, true))
             preferencesContentValues.put(INCOGNITO_MODE, sharedPreferences.getBoolean(INCOGNITO_MODE, false))
             preferencesContentValues.put(ALLOW_SCREENSHOTS, sharedPreferences.getBoolean(ALLOW_SCREENSHOTS, false))
             preferencesContentValues.put(EASYLIST, sharedPreferences.getBoolean(EASYLIST, true))
@@ -844,4 +827,4 @@ class ImportExportDatabaseHelper {
             exception.toString()
         }
     }
-}
\ No newline at end of file
+}