From 5b29a622c06536ff790aac64c0182feb9a621326 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 27 Apr 2016 23:56:09 -0700 Subject: [PATCH] Add a SharedPreferencesChangeListener. --- .../privacybrowser/MainWebViewActivity.java | 37 +++-- .../privacybrowser/SettingsActivity.java | 2 +- .../privacybrowser/SettingsFragment.java | 136 ++++++++++++++++++ app/src/main/res/values/strings.xml | 7 +- app/src/main/res/xml/preferences.xml | 4 +- 5 files changed, 158 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index 2720a8ac..bcbdd9f3 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -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; diff --git a/app/src/main/java/com/stoutner/privacybrowser/SettingsActivity.java b/app/src/main/java/com/stoutner/privacybrowser/SettingsActivity.java index 4c0a603b..5fd8e1af 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/SettingsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/SettingsActivity.java @@ -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(); } } diff --git a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java index 1968c65b..9f11cb5f 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java @@ -19,13 +19,149 @@ 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); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 73c4a465..2805c5db 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,19 +72,18 @@ Privacy Settings Enable JavaScript by default JavaScript allows websites to run programs (scripts) on your device. - Enable First-Party Cookies by default + Enable first-party cookies by default 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. - Enable Third-Party Cookies by default + 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. - Enable DOM Storage by default + 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 Homepage - Set the homepage. About Privacy Browser diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 06dbbd62..bc300f00 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -55,8 +55,8 @@ + android:inputType="textUri" + android:singleLine="true" /> \ No newline at end of file -- 2.45.2