]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/commitdiff
Add a SharedPreferencesChangeListener.
authorSoren Stoutner <soren@stoutner.com>
Thu, 28 Apr 2016 06:56:09 +0000 (23:56 -0700)
committerSoren Stoutner <soren@stoutner.com>
Thu, 28 Apr 2016 06:56:09 +0000 (23:56 -0700)
app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
app/src/main/java/com/stoutner/privacybrowser/SettingsActivity.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 2720a8ac95c48474ffa33db27b54a1793dc05f25..bcbdd9f30480ab7ff28f94b8e0bcdd23bab90d9e 100644 (file)
@@ -67,35 +67,30 @@ import java.net.URLEncoder;
 public class MainWebViewActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener {
     // favoriteIcon is public static so it can be accessed from CreateHomeScreenShortcut.
     public static Bitmap favoriteIcon;
-    // mainWebView is public static so it can be accessed from AboutDialog.  It is also used in onCreate(), onOptionsItemSelected(), onNavigationItemSelected(), and loadUrlFromTextBox().
+    // mainWebView is public static so it can be accessed from AboutDialog and SettingsFragment.  It is also used in onCreate(), onOptionsItemSelected(), onNavigationItemSelected(), and loadUrlFromTextBox().
     public static WebView mainWebView;
 
+    // mainMenu is public static so it can be accessed from SettingsFragment.  It is also used in onCreateOptionsMenu() and onOptionsItemSelected().
+    public static Menu mainMenu;
+    // cookieManager is public static so it can be accessed from SettingsFragment.  It is also used in onCreate(), onOptionsItemSelected(), and onNavigationItemSelected().
+    public static CookieManager cookieManager;
+    // javaScriptEnabled is public static so it can be accessed from SettingsFragment.  It is also used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox().
+    public static boolean javaScriptEnabled;
+    // firstPartyCookiesEnabled is public static so it can be accessed from SettingsFragment.  It is also used in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
+    public static boolean firstPartyCookiesEnabled;
+    // thirdPartyCookiesEnabled is uesd in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
+    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;
+    // homepage is public static so it can be accessed from  SettingsFragment.  It is also used in onCreate() and onOptionsItemSelected().
+    public static String homepage;
+
     // drawerToggle is used in onCreate(), onPostCreate(), onConfigurationChanged(), onNewIntent(), and onNavigationItemSelected().
     private ActionBarDrawerToggle drawerToggle;
     // drawerLayout is used in onCreate(), onNewIntent(), and onBackPressed().
     private DrawerLayout drawerLayout;
-    // mainMenu is used in onCreateOptionsMenu() and onOptionsItemSelected().
-    private Menu mainMenu;
     // formattedUrlString is used in onCreate(), onOptionsItemSelected(), onCreateHomeScreenShortcutCreate(), and loadUrlFromTextBox().
     private String formattedUrlString;
-    // homepage is used in onCreate() and onOptionsItemSelected().
-    private String homepage;
-    // javaScriptEnabled is used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox().
-    private boolean javaScriptEnabled;
-    // domStorageEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
-    private boolean domStorageEnabled;
-
-    /* saveFormDataEnabled does nothing until database storage is implemented.
-    // saveFormDataEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
-    private boolean saveFormDataEnabled;
-    */
-
-    // cookieManager is used in onCreate(), onOptionsItemSelected(), and onNavigationItemSelected().
-    private CookieManager cookieManager;
-    // firstPartyCookiesEnabled is used in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
-    private boolean firstPartyCookiesEnabled;
-    // thirdPartyCookiesEnabled is uesd in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
-    private boolean thirdPartyCookiesEnabled;
 
     // urlTextBox is used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox().
     private EditText urlTextBox;
index 4c0a603b39a85ce561c0e11ec1aa9d75564a584b..5fd8e1aff1b3882d276145e85d36655f9585de03 100644 (file)
@@ -27,7 +27,7 @@ public class SettingsActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        // Display SettingsFragment
+        // Display SettingsFragment.
         getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
     }
 }
index 1968c65b2810fec71ef3bf84cdacafadf479fded..9f11cb5f48c7f457f1417e2571435a98ddf52879 100644 (file)
 
 package com.stoutner.privacybrowser;
 
+import android.content.SharedPreferences;
+import android.os.Build;
 import android.os.Bundle;
+import android.preference.Preference;
 import android.preference.PreferenceFragment;
+import android.view.MenuItem;
 
 public class SettingsFragment extends PreferenceFragment {
+    private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
+    private SharedPreferences savedPreferences;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.preferences);
+
+        final Preference homepagePreference = findPreference("homepage");
+
+        // Initialize savedPreferences.
+        savedPreferences = getPreferenceScreen().getSharedPreferences();
+
+        // 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".
+        homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
+
+        // Listen for preference changes.
+        preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
+            @Override
+            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+
+                // Several keys need to update the toggleJavaScript icon.
+                MenuItem toggleJavaScript = MainWebViewActivity.mainMenu.findItem(R.id.toggleJavaScript);
+
+                switch (key) {
+                    case "javascript_enabled":
+                        // Set javaScriptEnabled to the new state.  The default is false.
+                        MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
+
+                        // Update mainWebView and reload the website.
+                        MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
+                        MainWebViewActivity.mainWebView.reload();
+
+                        // Update the toggleJavaScript icon.
+                        if (MainWebViewActivity.javaScriptEnabled) {
+                            toggleJavaScript.setIcon(R.drawable.javascript_enabled);
+                        } else {
+                            if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
+                                toggleJavaScript.setIcon(R.drawable.warning);
+                            } else {
+                                toggleJavaScript.setIcon(R.drawable.privacy_mode);
+                            }
+                        }
+                        return;
+
+                    case "first_party_cookies_enabled":
+                        // Set firstPartyCookiesEnabled to the new state.  The default is false.
+                        MainWebViewActivity.firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
+
+                        // Update the checkbox in the options menu.
+                        MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
+                        firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
+
+                        // Update mainWebView and reload the website.
+                        MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
+                        MainWebViewActivity.mainWebView.reload();
+
+                        // Update the toggleJavaScript icon.
+                        if (MainWebViewActivity.javaScriptEnabled) {
+                            toggleJavaScript.setIcon(R.drawable.javascript_enabled);
+                        } else {
+                            if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
+                                toggleJavaScript.setIcon(R.drawable.warning);
+                            } else {
+                                toggleJavaScript.setIcon(R.drawable.privacy_mode);
+                            }
+                        }
+                        return;
+
+                    case "third_party_cookies_enabled":
+                        // Set thirdPartyCookiesEnabled to the new state.  The default is false.
+                        MainWebViewActivity.thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
+
+                        // Update the checkbox in the options menu.
+                        MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
+                        thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
+
+                        // Update mainWebView and reload the website if API >= 21.
+                        if (Build.VERSION.SDK_INT >= 21) {
+                            MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
+                            MainWebViewActivity.mainWebView.reload();
+                        }
+                        return;
+
+                    case "dom_storage_enabled":
+                        // Set domStorageEnabled to the new state.  The default is false.
+                        MainWebViewActivity.domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
+
+                        // Update the checkbox in the options menu.
+                        MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
+                        domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
+
+                        // Update mainWebView and reload the website.
+                        MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
+                        MainWebViewActivity.mainWebView.reload();
+
+                        // Update the toggleJavaScript icon.
+                        if (MainWebViewActivity.javaScriptEnabled) {
+                            toggleJavaScript.setIcon(R.drawable.javascript_enabled);
+                        } else {
+                            if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
+                                toggleJavaScript.setIcon(R.drawable.warning);
+                            } else {
+                                toggleJavaScript.setIcon(R.drawable.privacy_mode);
+                            }
+                        }
+                        return;
+
+                    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"));
+
+                        // Update the homepage variable.  The default is "https://www.duckduckgo.com".
+                        MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
+                        return;
+
+                    // If no match, do nothing.
+                    default:
+                }
+            }
+        };
+
+        // Register the listener.
+        savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
     }
 }
index 73c4a46508d318a8a0dd0127b8a9693a9dcf080e..2805c5dbf898762cfd545fd2e0d72ae20d757e5a 100644 (file)
     <string name="privacy_settings">Privacy Settings</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>
+    <string name="first_party_cookies_preference">Enable first-party cookies by default</string>
     <string name="first_party_cookies_preference_summary">Cookies allow websites to store information on your device.
         First-party cookies come from the server listed in the address bar.
         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_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>
-    <string name="dom_storage_preference">Enable DOM Storage by default</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="homepage_preference">Homepage</string>
-    <string name="homepage_preference_summary">Set the homepage.</string>
 
     <!-- About Dialog. -->
     <string name="about_privacy_browser">About Privacy Browser</string>
index 06dbbd62a2a86edf58f11e7440ac163dbc828819..bc300f002268bdc8ca9ad77aa6cec9a6d546e0b1 100644 (file)
@@ -55,8 +55,8 @@
         <EditTextPreference
             android:key="homepage"
             android:title="@string/homepage_preference"
-            android:summary="@string/homepage_preference_summary"
             android:defaultValue="https://www.duckduckgo.com"
-            android:singleLine="true"/>
+            android:inputType="textUri"
+            android:singleLine="true" />
     </PreferenceCategory>
 </PreferenceScreen>
\ No newline at end of file