+
+ public void closeFindOnPage(View view) {
+ // Delete the contents of `find_on_page_edittext`.
+ findOnPageEditText.setText(null);
+
+ // Hide the Find on Page `RelativeLayout`.
+ LinearLayout findOnPageLinearLayout = (LinearLayout) findViewById(R.id.find_on_page_linearlayout);
+ findOnPageLinearLayout.setVisibility(View.GONE);
+
+ // Show the URL app bar.
+ Toolbar appBarToolbar = (Toolbar) findViewById(R.id.appBar);
+ appBarToolbar.setVisibility(View.VISIBLE);
+
+ // Hides the keyboard so we can see the webpage. `0` indicates no additional flags.
+ inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+ }
+
+ private void applySettings() {
+ // Get the shared preference values. `this` references the current context.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+
+ // Store the values from `sharedPreferences` in variables.
+ String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
+ String customUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0");
+ String javaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
+ String javaScriptDisabledCustomSearchString = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
+ String javaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
+ String javaScriptEnabledCustomSearchString = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
+ String homepageString = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
+ String defaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
+ swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", false);
+ boolean doNotTrackEnabled = sharedPreferences.getBoolean("do_not_track", true);
+ boolean proxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
+
+ // Because they can be modified on-the-fly by the user, these default settings are only applied when the program first runs.
+ if (javaScriptEnabled == null) { // If `javaScriptEnabled` is null the program is just starting.
+ // Get the values from `sharedPreferences`.
+ javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
+ firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
+ thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
+ domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
+ saveFormDataEnabled = sharedPreferences.getBoolean("save_form_data_enabled", false);
+
+ // Apply the default settings.
+ mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
+ cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
+ mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
+ mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
+ mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
+
+ // Set third-party cookies status if API >= 21.
+ if (Build.VERSION.SDK_INT >= 21) {
+ cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled);
+ }
+ }
+
+ // Apply the other settings from `sharedPreferences`.
+ homepage = homepageString;
+ swipeRefreshLayout.setEnabled(swipeToRefreshEnabled);
+
+ // Set the user agent initial status.
+ switch (userAgentString) {
+ case "Default user agent":
+ // Set the user agent to `""`, which uses the default value.
+ mainWebView.getSettings().setUserAgentString("");
+ break;
+
+ case "Custom user agent":
+ // Set the custom user agent.
+ mainWebView.getSettings().setUserAgentString(customUserAgentString);
+ break;
+
+ default:
+ // Use the selected user agent.
+ mainWebView.getSettings().setUserAgentString(userAgentString);
+ break;
+ }
+
+ // Set JavaScript disabled search.
+ if (javaScriptDisabledSearchString.equals("Custom URL")) { // Get the custom URL string.
+ javaScriptDisabledSearchURL = javaScriptDisabledCustomSearchString;
+ } else { // Use the string from the pre-built list.
+ javaScriptDisabledSearchURL = javaScriptDisabledSearchString;
+ }
+
+ // Set JavaScript enabled search.
+ if (javaScriptEnabledSearchString.equals("Custom URL")) { // Get the custom URL string.
+ javaScriptEnabledSearchURL = javaScriptEnabledCustomSearchString;
+ } else { // Use the string from the pre-built list.
+ javaScriptEnabledSearchURL = javaScriptEnabledSearchString;
+ }
+
+ // Set Do Not Track status.
+ if (doNotTrackEnabled) {
+ customHeaders.put("DNT", "1");
+ } else {
+ customHeaders.remove("DNT");
+ }
+
+ // Set Orbot proxy status.
+ if (proxyThroughOrbot) {
+ // Set the proxy. `this` refers to the current activity where an `AlertDialog` might be displayed.
+ OrbotProxyHelper.setProxy(getApplicationContext(), this, "localhost", "8118");
+ } else { // Reset the proxy to default. The host is `""` and the port is `"0"`.
+ OrbotProxyHelper.setProxy(getApplicationContext(), this, "", "0");
+ }
+ }
+
+ private void updatePrivacyIcons(boolean runInvalidateOptionsMenu) {
+ // Get handles for the icons.
+ MenuItem privacyIcon = mainMenu.findItem(R.id.toggleJavaScript);
+ MenuItem firstPartyCookiesIcon = mainMenu.findItem(R.id.toggleFirstPartyCookies);
+ MenuItem domStorageIcon = mainMenu.findItem(R.id.toggleDomStorage);
+ MenuItem formDataIcon = mainMenu.findItem(R.id.toggleSaveFormData);
+
+ // Update `privacyIcon`.
+ if (javaScriptEnabled) { // JavaScript is enabled.
+ privacyIcon.setIcon(R.drawable.javascript_enabled);
+ } else if (firstPartyCookiesEnabled) { // JavaScript is disabled but cookies are enabled.
+ privacyIcon.setIcon(R.drawable.warning);
+ } else { // All the dangerous features are disabled.
+ privacyIcon.setIcon(R.drawable.privacy_mode);
+ }
+
+ // Update `firstPartyCookiesIcon`.
+ if (firstPartyCookiesEnabled) { // First-party cookies are enabled.
+ firstPartyCookiesIcon.setIcon(R.drawable.cookies_enabled);
+ } else { // First-party cookies are disabled.
+ firstPartyCookiesIcon.setIcon(R.drawable.cookies_disabled);
+ }
+
+ // Update `domStorageIcon`.
+ if (javaScriptEnabled && domStorageEnabled) { // Both JavaScript and DOM storage are enabled.
+ domStorageIcon.setIcon(R.drawable.dom_storage_enabled);
+ } else if (javaScriptEnabled) { // JavaScript is enabled but DOM storage is disabled.
+ domStorageIcon.setIcon(R.drawable.dom_storage_disabled);
+ } else { // JavaScript is disabled, so DOM storage is ghosted.
+ domStorageIcon.setIcon(R.drawable.dom_storage_ghosted);
+ }
+
+ // Update `formDataIcon`.
+ if (saveFormDataEnabled) { // Form data is enabled.
+ formDataIcon.setIcon(R.drawable.form_data_enabled);
+ } else { // Form data is disabled.
+ formDataIcon.setIcon(R.drawable.form_data_disabled);
+ }
+
+ // `invalidateOptionsMenu` calls `onPrepareOptionsMenu()` and redraws the icons in the `AppBar`. `this` references the current activity.
+ if (runInvalidateOptionsMenu) {
+ ActivityCompat.invalidateOptionsMenu(this);
+ }
+ }