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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- // Display SettingsFragment
+ // Display SettingsFragment.
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
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);
}
}
<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>
<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