From c0f2dcca77fffd804c90cee5103795fc3849112b Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 8 May 2019 17:15:32 -0700 Subject: [PATCH] Add option to remove Twitter AMP redirects. https://redmine.stoutner.com/issues/417 --- .../activities/MainWebViewActivity.java | 147 +++++++++--------- .../fragments/SettingsFragment.java | 33 ++++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/preferences.xml | 6 + 4 files changed, 117 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index aa091453..d397a619 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -282,6 +282,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // The URL sanitizers are set in `applyAppSettings()` and used in `sanitizeUrl()`. private boolean sanitizeGoogleAnalytics; private boolean sanitizeFacebookClickIds; + private boolean sanitizeTwitterAmpRedirects; // The download strings are used in `onCreate()`, `onRequestPermissionResult()` and `initializeWebView()`. private String downloadUrl; @@ -3062,6 +3063,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook boolean doNotTrackEnabled = sharedPreferences.getBoolean("do_not_track", false); sanitizeGoogleAnalytics = sharedPreferences.getBoolean("google_analytics", true); sanitizeFacebookClickIds = sharedPreferences.getBoolean("facebook_click_ids", true); + sanitizeTwitterAmpRedirects = sharedPreferences.getBoolean("twitter_amp_redirects", true); proxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false); fullScreenBrowsingModeEnabled = sharedPreferences.getBoolean("full_screen_browsing_mode", false); hideAppBar = sharedPreferences.getBoolean("hide_app_bar", true); @@ -3452,53 +3454,49 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook nestedScrollWebView.getSettings().setTextZoom(fontSize); } - // Only set the user agent if the webpage is not currently loading. Otherwise, changing the user agent on redirects can cause the original website to reload. - // - if (nestedScrollWebView.getProgress() == 100) { // A URL is not loading. - // Set the user agent. - if (userAgentName.equals(getString(R.string.system_default_user_agent))) { // Use the system default user agent. - // Get the array position of the default user agent name. - int defaultUserAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName); - - // Set the user agent according to the system default. - switch (defaultUserAgentArrayPosition) { - case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list. - // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names. - nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName); - break; - - case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT: - // Set the user agent to `""`, which uses the default value. - nestedScrollWebView.getSettings().setUserAgentString(""); - break; - - case SETTINGS_CUSTOM_USER_AGENT: - // Set the default custom user agent. - nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value))); - break; - - default: - // Get the user agent string from the user agent data array - nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[defaultUserAgentArrayPosition]); - } - } else { // Set the user agent according to the stored name. - // Get the array position of the user agent name. - int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName); - - switch (userAgentArrayPosition) { - case UNRECOGNIZED_USER_AGENT: // The user agent name contains a custom user agent. - nestedScrollWebView.getSettings().setUserAgentString(userAgentName); - break; - - case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT: - // Set the user agent to `""`, which uses the default value. - nestedScrollWebView.getSettings().setUserAgentString(""); - break; - - default: - // Get the user agent string from the user agent data array. - nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]); - } + // Set the user agent. + if (userAgentName.equals(getString(R.string.system_default_user_agent))) { // Use the system default user agent. + // Get the array position of the default user agent name. + int defaultUserAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName); + + // Set the user agent according to the system default. + switch (defaultUserAgentArrayPosition) { + case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list. + // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names. + nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName); + break; + + case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT: + // Set the user agent to `""`, which uses the default value. + nestedScrollWebView.getSettings().setUserAgentString(""); + break; + + case SETTINGS_CUSTOM_USER_AGENT: + // Set the default custom user agent. + nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value))); + break; + + default: + // Get the user agent string from the user agent data array + nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[defaultUserAgentArrayPosition]); + } + } else { // Set the user agent according to the stored name. + // Get the array position of the user agent name. + int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName); + + switch (userAgentArrayPosition) { + case UNRECOGNIZED_USER_AGENT: // The user agent name contains a custom user agent. + nestedScrollWebView.getSettings().setUserAgentString(userAgentName); + break; + + case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT: + // Set the user agent to `""`, which uses the default value. + nestedScrollWebView.getSettings().setUserAgentString(""); + break; + + default: + // Get the user agent string from the user agent data array. + nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]); } } @@ -3596,33 +3594,29 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook cookieManager.setAcceptThirdPartyCookies(nestedScrollWebView, defaultThirdPartyCookiesEnabled); } - // Only set the user agent if the webpage is not currently loading. Otherwise, changing the user agent on redirects can cause the original website to reload. - // - if (nestedScrollWebView.getProgress() == 100) { // A URL is not loading. - // Get the array position of the user agent name. - int userAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName); + // Get the array position of the user agent name. + int userAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName); - // Set the user agent. - switch (userAgentArrayPosition) { - case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list. - // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names. - nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName); - break; + // Set the user agent. + switch (userAgentArrayPosition) { + case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list. + // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names. + nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName); + break; - case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT: - // Set the user agent to `""`, which uses the default value. - nestedScrollWebView.getSettings().setUserAgentString(""); - break; + case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT: + // Set the user agent to `""`, which uses the default value. + nestedScrollWebView.getSettings().setUserAgentString(""); + break; - case SETTINGS_CUSTOM_USER_AGENT: - // Set the default custom user agent. - nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value))); - break; + case SETTINGS_CUSTOM_USER_AGENT: + // Set the default custom user agent. + nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value))); + break; - default: - // Get the user agent string from the user agent data array - nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]); - } + default: + // Get the user agent string from the user agent data array + nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]); } // Set the loading of webpage images. @@ -3989,12 +3983,20 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook url = url.substring(0, url.indexOf("?fbclid=")); } - // Remove &fbclid=`. + // Remove `&fbclid=`. if (url.contains("&fbclid=")) { url = url.substring(0, url.indexOf("&fbclid=")); } } + // Sanitize Twitter AMP redirects. + if (sanitizeTwitterAmpRedirects) { + // Remove `?amp=1`. + if (url.contains("?amp=1")) { + url = url.substring(0, url.indexOf("?amp=1")); + } + } + // Return the sanitized URL. return url; } @@ -4889,6 +4891,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Check requests against the block lists. The deprecated `shouldInterceptRequest()` must be used until minimum API >= 21. @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { + // Sanitize the URL. + url = sanitizeUrl(url); + // Get a handle for the navigation view. NavigationView navigationView = findViewById(R.id.navigationview); diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java index 51962b6b..73ad08ba 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java @@ -74,6 +74,7 @@ public class SettingsFragment extends PreferenceFragment { Preference blockAllThirdPartyRequestsPreference = findPreference("block_all_third_party_requests"); Preference googleAnalyticsPreference = findPreference("google_analytics"); Preference facebookClickIdsPreference = findPreference("facebook_click_ids"); + Preference twitterAmpRedirectsPreference = findPreference("twitter_amp_redirects"); Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot"); Preference torHomepagePreference = findPreference("tor_homepage"); Preference torSearchPreference = findPreference("tor_search"); @@ -489,6 +490,21 @@ public class SettingsFragment extends PreferenceFragment { } } + // Set the Twitter AMP redirects icon according to the theme. + if (savedPreferences.getBoolean("twitter_amp_redirects", true)) { + if (darkTheme) { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_enabled_dark); + } else { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_enabled_light); + } + } else { + if (darkTheme) { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_disabled_dark); + } else { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_disabled_light); + } + } + // Set the Tor icons according to the theme. if (proxyThroughOrbot) { // Proxying is enabled. if (darkTheme) { @@ -1176,6 +1192,23 @@ public class SettingsFragment extends PreferenceFragment { } break; + case "twitter_amp_redirects": + // Update the icon. + if (sharedPreferences.getBoolean("twitter_amp_redirects", true)) { + if (darkTheme) { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_enabled_dark); + } else { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_enabled_light); + } + } else { + if (darkTheme) { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_disabled_dark); + } else { + twitterAmpRedirectsPreference.setIcon(R.drawable.modify_url_disabled_light); + } + } + break; + case "proxy_through_orbot": // Get current settings. boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6f041478..83c8e81b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -519,6 +519,8 @@ Remove “?utm_” or “&utm_” and anything after it from URLs. Facebook Click IDs Remove “?fbclid=” or “&fbclid=” and anything after it from URLs. + Twitter AMP redirects + Remove “?amp=1” and anything after it from URLs. Tor Proxy through Orbot Proxy all web traffic through Orbot on localhost:8118. diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index fe3a1440..b211879f 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -144,6 +144,12 @@ android:title="@string/facebook_click_ids" android:summary="@string/facebook_click_ids_summary" android:defaultValue="true" /> + +