From: Soren Stoutner Date: Wed, 11 May 2016 22:00:54 +0000 (-0700) Subject: Add preference for customizing the user agent. X-Git-Tag: v1.6~1 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=218d83a82183986e87152c73a5bec86bb6bb1770;p=PrivacyBrowserAndroid.git Add preference for customizing the user agent. --- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7271863d..d137b34b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,7 +38,7 @@ android:allowBackup="false" android:fullBackupContent="false" > - + @@ -66,10 +66,12 @@ + diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index 78624a8a..23339002 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -343,6 +343,24 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation domStorageEnabled = savedPreferences.getBoolean("dom_storage_enabled", false); mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled); + // Set the user agent initial status. + String userAgentString = savedPreferences.getString("user_agent", "Default user agent"); + switch (userAgentString) { + case "Default user agent": + // Do nothing. + break; + + case "Custom user agent": + // Set the custom user agent on mainWebView, The default is "PrivacyBrowser/1.0". + mainWebView.getSettings().setUserAgentString(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0")); + break; + + default: + // Set the selected user agent on mainWebView. The default is "PrivacyBrowser/1.0". + mainWebView.getSettings().setUserAgentString(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")); + break; + } + // Set the initial status for the search URLs. javaScriptDisabledSearchURL = savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="); javaScriptDisabledSearchCustomURL = savedPreferences.getString("javascript_disabled_search_custom_url", ""); @@ -618,8 +636,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation case R.id.refresh: mainWebView.reload(); + return true; default: + // Don't consume the event. return super.onOptionsItemSelected(menuItem); } } diff --git a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java index f52397b1..95b838aa 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java @@ -40,6 +40,22 @@ public class SettingsFragment extends PreferenceFragment { savedPreferences = getPreferenceScreen().getSharedPreferences(); + // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded. + final Preference userAgentPreference = findPreference("user_agent"); + // Get the user agent text from the webview (which changes based on the version of Android and WebView installed) if we are using the default. + if (savedPreferences.getString("user_agent", "Default user agent").equals("Default user agent")) { + // Once API >= 17 we can use getDefaultUserAgent() instead of getUserAgentString(). + userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString()); + } else { // Display the user-agent from the preference as the summary text. + userAgentPreference.setSummary(savedPreferences.getString("user_agent", "Default user agent")); + } + + // Set the summary text for "custom_user_agent" (the default is "PrivacyBrowser/1.0") and enable it if "user_agent" it set to "Custom user agent". + final Preference customUserAgent = findPreference("custom_user_agent"); + customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0")); + customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent")); + + // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded. // The default is "https://duckduckgo.com/html/?q=". final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search"); @@ -60,6 +76,7 @@ public class SettingsFragment extends PreferenceFragment { javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", "")); javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL")); + // Set the homepage URL as the summary text for the Homepage preference when the preference screen is loaded. The default is "https://www.duckduckgo.com". final Preference homepagePreference = findPreference("homepage"); homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com")); @@ -94,7 +111,7 @@ public class SettingsFragment extends PreferenceFragment { toggleJavaScript.setIcon(R.drawable.privacy_mode); } } - return; + break; case "first_party_cookies_enabled": // Set firstPartyCookiesEnabled to the new state. The default is false. @@ -118,7 +135,7 @@ public class SettingsFragment extends PreferenceFragment { toggleJavaScript.setIcon(R.drawable.privacy_mode); } } - return; + break; case "third_party_cookies_enabled": // Set thirdPartyCookiesEnabled to the new state. The default is false. @@ -133,7 +150,7 @@ public class SettingsFragment extends PreferenceFragment { MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled); MainWebViewActivity.mainWebView.reload(); } - return; + break; case "dom_storage_enabled": // Set domStorageEnabled to the new state. The default is false. @@ -157,7 +174,43 @@ public class SettingsFragment extends PreferenceFragment { toggleJavaScript.setIcon(R.drawable.privacy_mode); } } - return; + break; + + case "user_agent": + String userAgentString = sharedPreferences.getString("user_agent", "Default user agent"); + + switch (userAgentString) { + case "Default user agent": + // Set the default user agent on mainWebView, display the user agent as the summary text for userAgentPreference, and disable customUserAgent. + // Once API >= 17 we can use getDefaultUserAgent(). For now, setUserAgentString("") sets the WebView's default user agent. + MainWebViewActivity.mainWebView.getSettings().setUserAgentString(""); + userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString()); + customUserAgent.setEnabled(false); + break; + + case "Custom user agent": + // Set the custom user agent on mainWebView, display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent. + MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0")); + userAgentPreference.setSummary("Custom user agent"); + customUserAgent.setEnabled(true); + break; + + default: + // Set the user agent on mainWebView, display the user agent as the summary text for userAgentPreference, and disable customUserAgent. + MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0")); + userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString()); + customUserAgent.setEnabled(false); + break; + } + break; + + case "custom_user_agent": + // Set the new custom user agent as the summary text for "custom_user_agent". The default is "PrivacyBrowser/1.0". + customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0")); + + // Update mainWebView's user agent. The default is "PrivacyBrowser/1.0". + MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0")); + break; case "javascript_disabled_search": // Set the new search URL as the summary text for the JavaScript-disabled search preference. The default is "https://duckduckgo.com/html/?q=". @@ -168,7 +221,7 @@ public class SettingsFragment extends PreferenceFragment { // Update the javaScriptDisabledSearchURL variable. The default is "https://duckduckgo.com/html/?q=". MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="); - return; + break; case "javascript_disabled_search_custom_url": // Set the new custom search URL as the summary text for "javascript_disabled_search_custom_url". The default is "". @@ -176,6 +229,7 @@ public class SettingsFragment extends PreferenceFragment { // Update javaScriptDisabledSearchCustomURL. The default is "". MainWebViewActivity.javaScriptDisabledSearchCustomURL = sharedPreferences.getString("javascript_disabled_search_custom_url", ""); + break; case "javascript_enabled_search": // Set the new search URL as the summary text for the JavaScript-enabled search preference. The default is "https://duckduckgo.com/?q=". @@ -186,7 +240,7 @@ public class SettingsFragment extends PreferenceFragment { // Update the javaScriptEnabledSearchURL variable. The default is "https://duckduckgo.com/?q=". MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="); - return; + break; case "javascript_enabled_search_custom_url": // Set the new custom search URL as the summary text for "javascript_enabled_search_custom_url". The default is "". @@ -194,6 +248,7 @@ public class SettingsFragment extends PreferenceFragment { // Update javaScriptEnabledSearchCustomURL. The default is "". MainWebViewActivity.javaScriptEnabledSearchCustomURL = sharedPreferences.getString("javascript_enabled_search_custom_url", ""); + break; case "homepage": // Set the new homepage URL as the summary text for the Homepage preference. The default is "https://www.duckduckgo.com". @@ -201,7 +256,7 @@ public class SettingsFragment extends PreferenceFragment { // Update the homepage variable. The default is "https://www.duckduckgo.com". MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com"); - return; + break; case "swipe_to_refresh_enabled": // Set swipeToRefreshEnabled to the new state. The default is true. @@ -209,10 +264,11 @@ public class SettingsFragment extends PreferenceFragment { // Update swipeRefreshLayout to match the new state. MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled); - return; + break; - // If no match, do nothing. default: + // If no match, do nothing. + break; } } }; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 69cc2ff7..931175fc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -83,6 +83,36 @@ Enable DOM storage by default Document Object Management storage, also called web storage, is an enhanced form of cookies that allows websites to store larger and more complex types of information, like pictures, on your device. + User agent + + WebView Default + Privacy Browser 1.0 + Firefox 46 on Android 6.0.1 + Chrome 50 on Android 6.0.1 + Firefox 46 on Linux + Chromium 50 on Linux + Konqueror 4.14 on Linux + Firefox 46 on Windows 10 + Chrome 50 on Windows 10 + Internet Explorer 11 on Windows 10 + Edge 13 on Windows 10 + Custom + + + Default user agent + PrivacyBrowser/1.0 + Mozilla/5.0 (Android 6.0.1; Mobile; rv:46.0) Gecko/46.0 Firefox/46.0 + Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6P Build/MHC19Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.89 Mobile Safari/537.36 + Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 + Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36 + Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.21 (KHTML, like Gecko) konqueror/4.14.14 Safari/537.21 + Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0 + Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 + Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko + Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 + Custom user agent + + Custom user agent Search JavaScript-disabled search diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index a44f999a..813c1ca2 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -46,6 +46,21 @@ android:title="@string/dom_storage_preference" android:summary="@string/dom_storage_preference_summary" android:defaultValue="false" /> + + + + // Android doesn't let EditTextPreferences have more than one line, but I include singleLine="false" in case they ever wisen up. +