From d73de413ff6568f09acafa835be1af3e3cd2cefe Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 10 Sep 2018 21:24:00 -0700 Subject: [PATCH] Add user agent controls to the options menu. https://redmine.stoutner.com/issues/305 --- .idea/dictionaries/soren.xml | 1 + app/build.gradle | 2 +- .../activities/MainWebViewActivity.java | 257 ++++++++++++++---- .../main/res/menu/webview_options_menu.xml | 113 +++++++- app/src/main/res/values-de/strings.xml | 12 + app/src/main/res/values-es/strings.xml | 12 + app/src/main/res/values-it/strings.xml | 12 + app/src/main/res/values-ru/strings.xml | 12 + app/src/main/res/values/strings.xml | 12 + 9 files changed, 364 insertions(+), 69 deletions(-) diff --git a/.idea/dictionaries/soren.xml b/.idea/dictionaries/soren.xml index 9a0b75ff..5bed1892 100644 --- a/.idea/dictionaries/soren.xml +++ b/.idea/dictionaries/soren.xml @@ -85,6 +85,7 @@ listview logins lossless + macos mailto materialdesignicons mimetype diff --git a/app/build.gradle b/app/build.gradle index 8710e38d..25eacaff 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -71,7 +71,7 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:design:28.0.0-rc01' + implementation 'com.android.support:design:28.0.0-rc02' // Only compile Firebase ads for the free flavor. freeImplementation 'com.google.firebase:firebase-ads:15.0.1' 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 076f4145..d9eed0c0 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -362,6 +362,12 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook private boolean fanboysSocialBlockingListEnabled; private boolean ultraPrivacyEnabled; + // `webViewDefaultUserAgent` is used in `onCreate()` and `onPrepareOptionsMenu()`. + private String webViewDefaultUserAgent; + + // `defaultCustomUserAgentString` is used in `onPrepareOptionsMenu()` and `applyDomainSettings()`. + private String defaultCustomUserAgentString; + // `privacyBrowserRuntime` is used in `onCreate()`, `onOptionsItemSelected()`, and `applyAppSettings()`. private Runtime privacyBrowserRuntime; @@ -1216,6 +1222,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook saveFormDataEnabled = false; // Form data can be removed once the minimum API >= 26. nightMode = false; + // Store the default user agent. + webViewDefaultUserAgent = mainWebView.getSettings().getUserAgentString(); + // Initialize the WebView title. webViewTitle = getString(R.string.no_title); @@ -2103,6 +2112,38 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook ultraPrivacyMenuItem.setTitle(ultraPrivacyBlockedRequests + " - " + getString(R.string.ultraprivacy)); blockAllThirdParyRequestsMenuItem.setTitle(thirdPartyBlockedRequests + " - " + getString(R.string.block_all_third_party_requests)); + // Get the current user agent. + String currentUserAgent = mainWebView.getSettings().getUserAgentString(); + + // Select the current user agent menu item. A switch statement cannot be used because the user agents are not compile time constants. + if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[0])) { // Privacy Browser. + menu.findItem(R.id.user_agent_privacy_browser).setChecked(true); + } else if (currentUserAgent.equals(webViewDefaultUserAgent)) { // WebView Default. + menu.findItem(R.id.user_agent_webview_default).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[2])) { // Firefox on Android. + menu.findItem(R.id.user_agent_firefox_on_android).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[3])) { // Chrome on Android. + menu.findItem(R.id.user_agent_chrome_on_android).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[4])) { // Safari on iOS. + menu.findItem(R.id.user_agent_safari_on_ios).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[5])) { // Firefox on Linux. + menu.findItem(R.id.user_agent_firefox_on_linux).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[6])) { // Chromium on Linux. + menu.findItem(R.id.user_agent_chromium_on_linux).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[7])) { // Firefox on Windows. + menu.findItem(R.id.user_agent_firefox_on_windows).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[8])) { // Chrome on Windows. + menu.findItem(R.id.user_agent_chrome_on_windows).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[9])) { // Edge on Windows. + menu.findItem(R.id.user_agent_edge_on_windows).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[10])) { // Internet Explorer on Windows. + menu.findItem(R.id.user_agent_internet_explorer_on_windows).setChecked(true); + } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[11])) { // Safari on macOS. + menu.findItem(R.id.user_agent_safari_on_macos).setChecked(true); + } else { // Custom user agent. + menu.findItem(R.id.user_agent_custom).setChecked(true); + } + // Initialize font size variables. int fontSize = mainWebView.getSettings().getTextZoom(); String fontSizeTitle; @@ -2435,61 +2476,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook .show(); return true; - case R.id.font_size_twenty_five_percent: - mainWebView.getSettings().setTextZoom(25); - return true; - - case R.id.font_size_fifty_percent: - mainWebView.getSettings().setTextZoom(50); - return true; - - case R.id.font_size_seventy_five_percent: - mainWebView.getSettings().setTextZoom(75); - return true; - - case R.id.font_size_one_hundred_percent: - mainWebView.getSettings().setTextZoom(100); - return true; - - case R.id.font_size_one_hundred_twenty_five_percent: - mainWebView.getSettings().setTextZoom(125); - return true; - - case R.id.font_size_one_hundred_fifty_percent: - mainWebView.getSettings().setTextZoom(150); - return true; - - case R.id.font_size_one_hundred_seventy_five_percent: - mainWebView.getSettings().setTextZoom(175); - return true; - - case R.id.font_size_two_hundred_percent: - mainWebView.getSettings().setTextZoom(200); - return true; - - case R.id.swipe_to_refresh: - // Toggle swipe to refresh. - swipeRefreshLayout.setEnabled(!swipeRefreshLayout.isEnabled()); - return true; - - case R.id.display_images: - if (mainWebView.getSettings().getLoadsImagesAutomatically()) { // Images are currently loaded automatically. - mainWebView.getSettings().setLoadsImagesAutomatically(false); - mainWebView.reload(); - } else { // Images are not currently loaded automatically. - mainWebView.getSettings().setLoadsImagesAutomatically(true); - } - - // Set `onTheFlyDisplayImagesSet`. - onTheFlyDisplayImagesSet = true; - return true; - - case R.id.view_source: - // Launch the View Source activity. - Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class); - startActivity(viewSourceIntent); - return true; - case R.id.easylist: // Toggle the EasyList status. easyListEnabled = !easyListEnabled; @@ -2560,6 +2546,165 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook mainWebView.reload(); return true; + case R.id.user_agent_privacy_browser: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[0]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_webview_default: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(""); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_firefox_on_android: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[2]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_chrome_on_android: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[3]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_safari_on_ios: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[4]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_firefox_on_linux: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[5]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_chromium_on_linux: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[6]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_firefox_on_windows: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[7]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_chrome_on_windows: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[8]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_edge_on_windows: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[9]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_internet_explorer_on_windows: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[10]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_safari_on_macos: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[11]); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.user_agent_custom: + // Update the user agent. + mainWebView.getSettings().setUserAgentString(defaultCustomUserAgentString); + + // Reload the WebView. + mainWebView.reload(); + return true; + + case R.id.font_size_twenty_five_percent: + mainWebView.getSettings().setTextZoom(25); + return true; + + case R.id.font_size_fifty_percent: + mainWebView.getSettings().setTextZoom(50); + return true; + + case R.id.font_size_seventy_five_percent: + mainWebView.getSettings().setTextZoom(75); + return true; + + case R.id.font_size_one_hundred_percent: + mainWebView.getSettings().setTextZoom(100); + return true; + + case R.id.font_size_one_hundred_twenty_five_percent: + mainWebView.getSettings().setTextZoom(125); + return true; + + case R.id.font_size_one_hundred_fifty_percent: + mainWebView.getSettings().setTextZoom(150); + return true; + + case R.id.font_size_one_hundred_seventy_five_percent: + mainWebView.getSettings().setTextZoom(175); + return true; + + case R.id.font_size_two_hundred_percent: + mainWebView.getSettings().setTextZoom(200); + return true; + + case R.id.swipe_to_refresh: + // Toggle swipe to refresh. + swipeRefreshLayout.setEnabled(!swipeRefreshLayout.isEnabled()); + return true; + + case R.id.display_images: + if (mainWebView.getSettings().getLoadsImagesAutomatically()) { // Images are currently loaded automatically. + mainWebView.getSettings().setLoadsImagesAutomatically(false); + mainWebView.reload(); + } else { // Images are not currently loaded automatically. + mainWebView.getSettings().setLoadsImagesAutomatically(true); + } + + // Set `onTheFlyDisplayImagesSet`. + onTheFlyDisplayImagesSet = true; + return true; + + case R.id.view_source: + // Launch the View Source activity. + Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class); + startActivity(viewSourceIntent); + return true; + case R.id.share: // Setup the share string. String shareString = webViewTitle + " – " + urlTextBox.getText().toString(); @@ -3921,7 +4066,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Store the general preference information. String defaultFontSizeString = sharedPreferences.getString("default_font_size", "100"); String defaultUserAgentName = sharedPreferences.getString("user_agent", "Privacy Browser"); - String defaultCustomUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"); + defaultCustomUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"); boolean defaultSwipeToRefresh = sharedPreferences.getBoolean("swipe_to_refresh", true); nightMode = sharedPreferences.getBoolean("night_mode", false); diff --git a/app/src/main/res/menu/webview_options_menu.xml b/app/src/main/res/menu/webview_options_menu.xml index 8bc8a9a6..5e1d19c9 100644 --- a/app/src/main/res/menu/webview_options_menu.xml +++ b/app/src/main/res/menu/webview_options_menu.xml @@ -149,10 +149,99 @@ app:showAsAction="never" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -160,49 +249,49 @@ @@ -211,21 +300,21 @@ diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 8a4ec357..bfc071c0 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -131,6 +131,18 @@ Cookies löschen DOM-Speicher löschen Formulardaten löschen + WebView-Standard + Firefox auf Android + Chrome auf Android + Safari auf iOS + Firefox auf Linux + Chromium auf Linux + Firefox auf Windows + Chrome auf Windows + Edge auf Windows + Internet Explorer auf Windows + Safari auf macOS + Eigener Schriftgröße 25% 50% diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 3a46aebf..8afa404e 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -134,6 +134,18 @@ Lista de bloqueo social de Fanboy Bloquear todas las solicitudes de terceros Diseño + WebView por defecto + Firefox en Android + Chrome en Android + Safari en iOS + Firefox en Linux + Chromium en Linux + Firefox en Windows + Chrome en Windows + Edge en Windows + Internet Explorer en Windows + Safari en macOS + Personalizado Tamaño de fuente 25% 50% diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 37b1f3ad..0d092c31 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -134,6 +134,18 @@ Fanboy’s social blocking list Blocca tutte le richieste di Terze Parti Layout + WebView Default + Firefox su Android + Chrome su Android + Safari su iOS + Firefox su Linux + Chromium su Linux + Firefox su Windows + Chrome su Windows + Edge su Windows + Internet Explorer su Windows + Safari su macOS + Personalizzato Dimensione font 25% 50% diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 9f0b3990..d753623c 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -135,6 +135,18 @@ Fanboy’s social blocking list Блокировать все сторонние запросы Внешний вид + WebView по умолчанию + Firefox на Android + Chrome на Android + Safari на iOS + Firefox на Linux + Chromium на Linux + Firefox на Windows + Chrome на Windows + Edge на Windows + Internet Explorer на Windows + Safari на macOS + Настраиваемый Размер шрифта 25% 50% diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce962578..f370ba3b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -135,6 +135,18 @@ Fanboy’s Social Blocking List Block All Third-Party Requests Layout + WebView Default + Firefox on Android + Chrome on Android + Safari on iOS + Firefox on Linux + Chromium on Linux + Firefox on Windows + Chrome on Windows + Edge on Windows + Internet Explorer on Windows + Safari on macOS + Custom Font Size 25% 50% -- 2.45.2