From 5be7baa728da6b72d7cd104e4c30d8fe1a41c757 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 6 May 2016 16:25:28 -0700 Subject: [PATCH] Allow customization of the search URLs. --- .../privacybrowser/MainWebViewActivity.java | 38 +++++++--- .../privacybrowser/SettingsFragment.java | 69 +++++++++++++++++-- app/src/main/res/values/strings.xml | 39 ++++++++++- app/src/main/res/xml/preferences.xml | 41 +++++++++-- 4 files changed, 168 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index 99fef852..78624a8a 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -82,6 +82,14 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation public static boolean thirdPartyCookiesEnabled; // domStorageEnabled is public static so it can be accessed from SettingsFragment. It is also used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected(). public static boolean domStorageEnabled; + // javaScriptDisabledSearchURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox(). + public static String javaScriptDisabledSearchURL; + // javaScriptDisabledSearchCustomURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox(). + public static String javaScriptDisabledSearchCustomURL; + // javaScriptEnabledSearchURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox(). + public static String javaScriptEnabledSearchURL; + // javaScriptEnabledSearchCustomURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox(). + public static String javaScriptEnabledSearchCustomURL; // homepage is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and onOptionsItemSelected(). public static String homepage; // swipeToRefresh is public static so it can be accessed from SettingsFragment. It is also used in onCreate(). @@ -314,28 +322,34 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Get the shared preference values. SharedPreferences savedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - // Set JavaScript initial status. + // Set JavaScript initial status. The default value is false. javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false); mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled); // Initialize cookieManager. cookieManager = CookieManager.getInstance(); - // Set cookies initial status. + // Set cookies initial status. The default value is false. firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false); cookieManager.setAcceptCookie(firstPartyCookiesEnabled); - // Set third-party cookies initial status if API >= 21. + // Set third-party cookies initial status if API >= 21. The default value is false. if (Build.VERSION.SDK_INT >= 21) { thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false); cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled); } - // Set DOM storage initial status. + // Set DOM storage initial status. The default value is false. domStorageEnabled = savedPreferences.getBoolean("dom_storage_enabled", false); mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled); - // Set homepage initial status. + // 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", ""); + javaScriptEnabledSearchURL = savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="); + javaScriptEnabledSearchCustomURL = savedPreferences.getString("javascript_enabled_search_custom_url", ""); + + // Set homepage initial status. The default value is "https://www.duckduckgo.com". homepage = savedPreferences.getString("homepage", "https://www.duckduckgo.com"); // Set swipe to refresh initial status. The default is true. @@ -781,9 +795,17 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Use the correct search URL based on javaScriptEnabled. if (javaScriptEnabled) { - formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString; - } else { - formattedUrlString = "https://duckduckgo.com/html/?q=" + encodedUrlString; + if (javaScriptEnabledSearchURL.equals("Custom URL")) { + formattedUrlString = javaScriptEnabledSearchCustomURL + encodedUrlString; + } else { + formattedUrlString = javaScriptEnabledSearchURL + encodedUrlString; + } + } else { // JavaScript is disabled. + if (javaScriptDisabledSearchURL.equals("Custom URL")) { + formattedUrlString = javaScriptDisabledSearchCustomURL + encodedUrlString; + } else { + formattedUrlString = javaScriptDisabledSearchURL + encodedUrlString; + } } } diff --git a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java index 35021b3e..f52397b1 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java @@ -19,6 +19,7 @@ package com.stoutner.privacybrowser; +import android.annotation.SuppressLint; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; @@ -35,17 +36,40 @@ public class SettingsFragment extends PreferenceFragment { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); - final Preference homepagePreference = findPreference("homepage"); - // Initialize savedPreferences. savedPreferences = getPreferenceScreen().getSharedPreferences(); + + // 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"); + javaScriptDisabledSearchPreference.setSummary(savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=")); + + // Set the summary text for "javascript_disabled_search_custom_url" (the default is "") and enable it if "javascript_disabled_search" is set to "Custom URL". + final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url"); + javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", "")); + javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL")); + + // Set the JavaScript-enabed searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded. + // The default is "https://duckduckgo.com/?q=". + final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search"); + javaScriptEnabledSearchPreference.setSummary(savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=")); + + // Set the summary text for "javascript_enabled_search_custom_url" (the default is "") and enable it if "javascript_enabled_search" is set to "Custom URL". + final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url"); + 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")); + // Listen for preference changes. preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override + // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. + @SuppressLint("SetJavaScriptEnabled") public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // Several keys need to update the toggleJavaScript icon. @@ -135,6 +159,42 @@ public class SettingsFragment extends PreferenceFragment { } return; + 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=". + javaScriptDisabledSearchPreference.setSummary(sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=")); + + // Enable "javascript_disabled_search_custom_url" if "javascript_disabled_search" is set to "Custom URL". + javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL")); + + // 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; + + 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 "". + javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", "")); + + // Update javaScriptDisabledSearchCustomURL. The default is "". + MainWebViewActivity.javaScriptDisabledSearchCustomURL = sharedPreferences.getString("javascript_disabled_search_custom_url", ""); + + 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=". + javaScriptEnabledSearchPreference.setSummary(sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=")); + + // Enable "javascript_enabled_search_custom_url" if "javascript_enabled_search" is set to "Custom URL". + javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL")); + + // Update the javaScriptEnabledSearchURL variable. The default is "https://duckduckgo.com/?q=". + MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="); + return; + + 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 "". + javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", "")); + + // Update javaScriptEnabledSearchCustomURL. The default is "". + MainWebViewActivity.javaScriptEnabledSearchCustomURL = sharedPreferences.getString("javascript_enabled_search_custom_url", ""); + case "homepage": // Set the new homepage URL as the summary text for the Homepage preference. The default is "https://www.duckduckgo.com". homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com")); @@ -149,6 +209,7 @@ public class SettingsFragment extends PreferenceFragment { // Update swipeRefreshLayout to match the new state. MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled); + return; // If no match, do nothing. default: @@ -160,8 +221,8 @@ public class SettingsFragment extends PreferenceFragment { savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener); } - // It is necessary to reregister the listener on every resume or it will randomly stop working for the user because apps can be paused and resumed at any time, - // even when they are in the foreground. + // It is necessary to re-register the listener on every resume or it will randomly stop working because apps can be paused and resumed at any time + // even while running in the foreground. @Override public void onResume() { super.onResume(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index efc7e659..69cc2ff7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -70,7 +70,7 @@ Create - Privacy Settings + Privacy Enable JavaScript by default JavaScript allows websites to run programs (scripts) on your device. Enable first-party cookies by default @@ -79,11 +79,44 @@ Devices with versions of Android older than Lollipop (version 5.0) will also enable third-party cookies with this setting. Enable third-party cookies by default Third-party cookies allow parts of websites that aren\'t the main website, like advertisements, to store information on your device. - This setting requires Android Lollipop (version 5.0) or higher. It has no effect if first-party cookies are disabled. + This setting requires Android Lollipop (version 5.0) or higher. This setting has no effect if first-party cookies are disabled. 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. - General Settings + Search + JavaScript-disabled search + + DuckDuckGo + Google + Bing + Yahoo + Custom + + + https://duckduckgo.com/html/?q= + https://www.google.com/search?q= + https://www.bing.com/search?q= + https://search.yahoo.com/mobile/s?nojs=1&p= + Custom URL + + JavaScript-disabled search custom URL + JavaScript-enabled search + + DuckDuckGo + Google + Bing + Yahoo + Custom + + + https://duckduckgo.com/?q= + https://www.google.com/search?q= + https://www.bing.com/search?q= + https://search.yahoo.com/mobile/s?p= + Custom URL + + JavaScript-enabled search custom URL + General Homepage Swipe to refresh Some websites don\'t work well if swipe to refresh is enabled. diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 747778e7..a44f999a 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -20,8 +20,8 @@ + android:key="privacy" + android:title="@string/privacy" > + android:key="search" + android:title="@string/search"> + + + + + + + + + + +