package com.stoutner.privacybrowser.fragments;
import android.annotation.SuppressLint;
+import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebView;
import com.stoutner.privacybrowser.R;
+import com.stoutner.privacybrowser.activities.MainWebViewActivity;
public class SettingsFragment extends PreferenceFragment {
private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
+ final Preference clearEverythingPreference = findPreference("clear_everything");
+ final Preference clearCookiesPreference = findPreference("clear_cookies");
+ final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
+ final Preference clearFormDataPreference = findPreference("clear_form_data");
+ final Preference clearCachePreference = findPreference("clear_cache");
final Preference homepagePreference = findPreference("homepage");
final Preference defaultFontSizePreference = findPreference("default_font_size");
final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
+ final Preference darkThemePreference = findPreference("dark_theme");
+ final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
// Set dependencies.
domStoragePreference.setDependency("javascript_enabled");
boolean proxyThroughOrbotBoolean = savedPreferences.getBoolean("proxy_through_orbot", false);
boolean fullScreenBrowsingModeBoolean = savedPreferences.getBoolean("full_screen_browsing_mode", false);
boolean hideSystemBarsBoolean = savedPreferences.getBoolean("hide_system_bars", false);
+ boolean clearEverythingBoolean = savedPreferences.getBoolean("clear_everything", true);
// Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
// Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
+ // Set the status of the `Clear and Exit` preferences.
+ clearCookiesPreference.setEnabled(!clearEverythingBoolean);
+ clearDomStoragePreference.setEnabled(!clearEverythingBoolean);
+ clearFormDataPreference.setEnabled(!clearEverythingBoolean);
+ clearCachePreference.setEnabled(!clearEverythingBoolean);
// Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
}
}
- } else { // `fullScreenBrwosingModeBoolean` is false.
+ } else { // `fullScreenBrowsingModeBoolean` is false.
fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
}
+ // Set the `clearEverythingPreference` icon.
+ if (clearEverythingBoolean) {
+ clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled);
+ } else {
+ clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
+ }
+
+ // Set the `clearCookiesPreference` icon.
+ if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cookies", true)) {
+ clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
+ } else {
+ clearCookiesPreference.setIcon(R.drawable.cookies_warning);
+ }
+
+ // Set the `clearDomStoragePreference` icon.
+ if (clearEverythingBoolean || savedPreferences.getBoolean("clear_dom_storage", true)) {
+ clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
+ } else {
+ clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
+ }
+
+ // Set the `clearFormDataPreference` icon.
+ if (clearEverythingBoolean || savedPreferences.getBoolean("clear_form_data", true)) {
+ clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
+ } else {
+ clearFormDataPreference.setIcon(R.drawable.form_data_warning);
+ }
+
+ // Set the `clearCachePreference` icon.
+ if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cache", true)) {
+ clearCachePreference.setIcon(R.drawable.cache_cleared);
+ } else {
+ clearCachePreference.setIcon(R.drawable.cache_warning);
+ }
+
// Set the `swipeToRefreshPreference` icon.
if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
}
+ // Set the `darkThemePreference` icon.
+ if (savedPreferences.getBoolean("dark_theme", false)) {
+ darkThemePreference.setIcon(R.drawable.theme_dark);
+ } else {
+ darkThemePreference.setIcon(R.drawable.theme_light);
+ }
+
+ // Set the `displayWebpageImagesPreference` icon.
+ if (savedPreferences.getBoolean("display_webpage_images", true)) {
+ displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
+ } else {
+ displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
+ }
+
+
// Listen for preference changes.
preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
}
break;
+ case "clear_everything":
+ // Store the new `clear_everything` status
+ boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
+
+ // Update the status of the `Clear and Exit` preferences.
+ clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
+ clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
+ clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
+ clearCachePreference.setEnabled(!newClearEverythingBoolean);
+
+ // Update the `clearEverythingPreference` icon.
+ if (newClearEverythingBoolean) {
+ clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled);
+ } else {
+ clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
+ }
+
+ // Update the `clearCookiesPreference` icon.
+ if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
+ clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
+ } else {
+ clearCookiesPreference.setIcon(R.drawable.cookies_warning);
+ }
+
+ // Update the `clearDomStoragePreference` icon.
+ if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
+ clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
+ } else {
+ clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
+ }
+
+ // Update the `clearFormDataPreference` icon.
+ if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
+ clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
+ } else {
+ clearFormDataPreference.setIcon(R.drawable.form_data_warning);
+ }
+
+ // Update the `clearCachePreference` icon.
+ if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
+ clearCachePreference.setIcon(R.drawable.cache_cleared);
+ } else {
+ clearCachePreference.setIcon(R.drawable.cache_warning);
+ }
+ break;
+
+ case "clear_cookies":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("clear_cookies", true)) {
+ clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
+ } else {
+ clearCookiesPreference.setIcon(R.drawable.cookies_warning);
+ }
+ break;
+
+ case "clear_dom_storage":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
+ clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
+ } else {
+ clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
+ }
+ break;
+
+ case "clear_form_data":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("clear_form_data", true)) {
+ clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
+ } else {
+ clearFormDataPreference.setIcon(R.drawable.form_data_warning);
+ }
+ break;
+
+ case "clear_cache":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("clear_cache", true)) {
+ clearCachePreference.setIcon(R.drawable.cache_cleared);
+ } else {
+ clearCachePreference.setIcon(R.drawable.cache_warning);
+ }
+ break;
+
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"));
}
break;
- default:
- // If no match, do nothing.
+ case "dark_theme":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("dark_theme", false)) {
+ darkThemePreference.setIcon(R.drawable.theme_dark);
+ } else {
+ darkThemePreference.setIcon(R.drawable.theme_light);
+ }
+
+ // Create an `Intent` to restart Privacy Browser.
+ Intent intent = getActivity().getParentActivityIntent();
+
+ // Assert that `intent` is not `null` to remove the lint error below.
+ assert intent != null;
+
+ // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+
+ // Make it so.
+ startActivity(intent);
+ break;
+
+ case "display_webpage_images":
+ if (sharedPreferences.getBoolean("display_webpage_images", true)) {
+ // Update the icon.
+ displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
+
+ // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
+ MainWebViewActivity.reloadOnRestartBoolean = false;
+ } else {
+ // Update the icon.
+ displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
+
+ // Set `mainWebView` to reload on restart to remove the current images.
+ MainWebViewActivity.reloadOnRestartBoolean = true;
+ }
break;
}
}