Allow customization of the search URLs.
authorSoren Stoutner <soren@stoutner.com>
Fri, 6 May 2016 23:25:28 +0000 (16:25 -0700)
committerSoren Stoutner <soren@stoutner.com>
Fri, 6 May 2016 23:25:28 +0000 (16:25 -0700)
app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java
app/src/main/res/values/strings.xml
app/src/main/res/xml/preferences.xml

index 99fef852d35edf0618b5a448095d3f9171d8ef33..78624a8ad3f30d19bb7f9a6f694bf985f64c4740 100644 (file)
@@ -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;
+                }
             }
         }
 
index 35021b3e8d09dcb333bf70d2954fc98123cf4a95..f52397b19abfb69df19d64d22f9b139fa9f3c135 100644 (file)
@@ -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();
index efc7e659459c1d4651568ef115539fdec4476d6f..69cc2ff7b15e2d0ac025e81599839c03535fcd41 100644 (file)
@@ -70,7 +70,7 @@
     <string name="create">Create</string>
 
     <!-- Preferences. -->
-    <string name="privacy_settings">Privacy Settings</string>
+    <string name="privacy">Privacy</string>
     <string name="javascript_preference">Enable JavaScript by default</string>
     <string name="javascript_preference_summary">JavaScript allows websites to run programs (scripts) on your device.</string>
     <string name="first_party_cookies_preference">Enable first-party cookies by default</string>
         Devices with versions of Android older than Lollipop (version 5.0) will also enable third-party cookies with this setting.</string>
     <string name="third_party_cookies_preference">Enable third-party cookies by default</string>
     <string name="third_party_cookies_summary">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.</string>
+        This setting requires Android Lollipop (version 5.0) or higher.  This setting has no effect if first-party cookies are disabled.</string>
     <string name="dom_storage_preference">Enable DOM storage by default</string>
     <string name="dom_storage_preference_summary">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.</string>
-    <string name="general_settings">General Settings</string>
+    <string name="search">Search</string>
+    <string name="javascript_disabled_search">JavaScript-disabled search</string>
+    <string-array name="javascript_disabled_search_entries">
+        <item>DuckDuckGo</item>
+        <item>Google</item>
+        <item>Bing</item>
+        <item>Yahoo</item>
+        <item>Custom</item>
+    </string-array>
+    <string-array name="javascript_disabled_search_entry_values">
+        <item>https://duckduckgo.com/html/?q=</item>
+        <item>https://www.google.com/search?q=</item>
+        <item>https://www.bing.com/search?q=</item>
+        <item>https://search.yahoo.com/mobile/s?nojs=1&amp;p=</item>
+        <item>Custom URL</item>
+    </string-array>
+    <string name="javascript_disabled_search_custom_url">JavaScript-disabled search custom URL</string>
+    <string name="javascript_enabled_search">JavaScript-enabled search</string>
+    <string-array name="javascript_enabled_search_entries">
+        <item>DuckDuckGo</item>
+        <item>Google</item>
+        <item>Bing</item>
+        <item>Yahoo</item>
+        <item>Custom</item>
+    </string-array>
+    <string-array name="javascript_enabled_search_entry_values">
+        <item>https://duckduckgo.com/?q=</item>
+        <item>https://www.google.com/search?q=</item>
+        <item>https://www.bing.com/search?q=</item>
+        <item>https://search.yahoo.com/mobile/s?p=</item>
+        <item>Custom URL</item>
+    </string-array>
+    <string name="javascript_enabled_search_custom_url">JavaScript-enabled search custom URL</string>
+    <string name="general">General</string>
     <string name="homepage_preference">Homepage</string>
     <string name="swipe_to_refresh_enabled">Swipe to refresh</string>
     <string name="swipe_to_refresh_enabled_summary">Some websites don\'t work well if swipe to refresh is enabled.</string>
index 747778e776380769b8a0e87b450283e20ccdebff..a44f999ad7f3e366c31a963a440cbb65f742963d 100644 (file)
@@ -20,8 +20,8 @@
 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
     <PreferenceCategory
-        android:key="privacy_settings"
-        android:title="@string/privacy_settings" >
+        android:key="privacy"
+        android:title="@string/privacy" >
 
         <SwitchPreference
             android:key="javascript_enabled"
     </PreferenceCategory>
 
     <PreferenceCategory
-        android:key="general_settings"
-        android:title="@string/general_settings" >
+        android:key="search"
+        android:title="@string/search">
+
+        <ListPreference
+            android:key="javascript_disabled_search"
+            android:title="@string/javascript_disabled_search"
+            android:entries="@array/javascript_disabled_search_entries"
+            android:entryValues="@array/javascript_disabled_search_entry_values"
+            android:defaultValue="https://duckduckgo.com/html/?q=" />
+
+        <EditTextPreference
+            android:key="javascript_disabled_search_custom_url"
+            android:title="@string/javascript_disabled_search_custom_url"
+            android:defaultValue=""
+            android:inputType="textUri"
+            android:singleLine="true" />
+
+        <ListPreference
+            android:key="javascript_enabled_search"
+            android:title="@string/javascript_enabled_search"
+            android:entries="@array/javascript_enabled_search_entries"
+            android:entryValues="@array/javascript_enabled_search_entry_values"
+            android:defaultValue="https://duckduckgo.com/?q=" />
+
+        <EditTextPreference
+            android:key="javascript_enabled_search_custom_url"
+            android:title="@string/javascript_enabled_search_custom_url"
+            android:defaultValue=""
+            android:inputType="textUri"
+            android:singleLine="true" />
+    </PreferenceCategory>
+
+    <PreferenceCategory
+        android:key="general"
+        android:title="@string/general" >
 
         <EditTextPreference
             android:key="homepage"