X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=d397a619e3c0cd9a35a32aa8ef9c7a37f2fcaef6;hp=df7cef48fd39a8052e9685b050fe1ac12f267d91;hb=c0f2dcca77fffd804c90cee5103795fc3849112b;hpb=7dcfe46842e801486a3696bea379602350e6891a 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 df7cef48..d397a619 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -279,6 +279,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // The swipe refresh layout top padding is used when exiting full screen browsing mode. It is used in an inner class in `initializeWebView()`. private int swipeRefreshLayoutPaddingTop; + // 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; private String downloadContentDisposition; @@ -2999,6 +3004,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } private void loadUrl(String url) { + // Sanitize the URL. + url = sanitizeUrl(url); + // Apply the domain settings. applyDomainSettings(currentWebView, url, true, false); @@ -3053,6 +3061,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Store the values from the shared preferences in variables. incognitoModeEnabled = sharedPreferences.getBoolean("incognito_mode", false); 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); @@ -3443,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]); } } @@ -3587,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. @@ -3959,12 +3962,54 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook startActivity(openWithBrowserIntent); } + private String sanitizeUrl(String url) { + // Sanitize Google Analytics. + if (sanitizeGoogleAnalytics) { + // Remove `?utm_`. + if (url.contains("?utm_")) { + url = url.substring(0, url.indexOf("?utm_")); + } + + // Remove `&utm_`. + if (url.contains("&utm_")) { + url = url.substring(0, url.indexOf("&utm_")); + } + } + + // Sanitize Facebook Click IDs. + if (sanitizeFacebookClickIds) { + // Remove `?fbclid=`. + if (url.contains("?fbclid=")) { + url = url.substring(0, url.indexOf("?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; + } + public void addTab(View view) { // Add a new tab with a blank URL. addNewTab(""); } private void addNewTab(String url) { + // Sanitize the URL. + url = sanitizeUrl(url); + // Get a handle for the tab layout and the view pager. TabLayout tabLayout = findViewById(R.id.tablayout); ViewPager webViewPager = findViewById(R.id.webviewpager); @@ -4770,6 +4815,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // The deprecated `shouldOverrideUrlLoading` must be used until API >= 24. @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { + // Sanitize the url. + url = sanitizeUrl(url); + if (url.startsWith("http")) { // Load the URL in Privacy Browser. // Apply the domain settings for the new URL. This doesn't do anything if the domain has not changed. boolean userAgentChanged = applyDomainSettings(nestedScrollWebView, url, true, false); @@ -4843,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);