+ // Only display the form data menu items if the API < 26.
+ optionsSaveFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
+ optionsClearFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
+
+ // Disable the clear form data menu item if the API >= 26 so that the status of the main Clear Data is calculated correctly.
+ optionsClearFormDataMenuItem.setEnabled(Build.VERSION.SDK_INT < 26);
+
+ // Get the shared preferences.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+
+ // Get the dark theme and app bar preferences.
+ boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean(getString(R.string.display_additional_app_bar_icons_key), false);
+
+ // Set the status of the additional app bar icons. Setting the refresh menu item to `SHOW_AS_ACTION_ALWAYS` makes it appear even on small devices like phones.
+ if (displayAdditionalAppBarIcons) {
+ optionsRefreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
+ bookmarksMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ optionsCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
+ } else { //Do not display the additional icons.
+ optionsRefreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ bookmarksMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ optionsCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ }
+
+ // Replace Refresh with Stop if a URL is already loading.
+ if (currentWebView != null && currentWebView.getProgress() != 100) {
+ // Set the title.
+ optionsRefreshMenuItem.setTitle(R.string.stop);
+
+ // Set the icon if it is displayed in the app bar. Once the minimum API is >= 26, the blue and black icons can be combined with a tint list.
+ if (displayAdditionalAppBarIcons) {
+ optionsRefreshMenuItem.setIcon(R.drawable.close_blue);
+ }
+ }
+
+ // Done.
+ return true;
+ }
+
+ @Override
+ public boolean onPrepareOptionsMenu(Menu menu) {
+ // Get a handle for the cookie manager.
+ CookieManager cookieManager = CookieManager.getInstance();
+
+ // Initialize the current user agent string and the font size.
+ String currentUserAgent = getString(R.string.user_agent_privacy_browser);
+ int fontSize = 100;
+
+ // Set items that require the current web view to be populated. It will be null when the program is first opened, as `onPrepareOptionsMenu()` is called before the first WebView is initialized.
+ if (currentWebView != null) {
+ // Set the add or edit domain text.
+ if (currentWebView.getDomainSettingsApplied()) {
+ optionsAddOrEditDomainMenuItem.setTitle(R.string.edit_domain_settings);
+ } else {
+ optionsAddOrEditDomainMenuItem.setTitle(R.string.add_domain_settings);
+ }
+
+ // Get the current user agent from the WebView.
+ currentUserAgent = currentWebView.getSettings().getUserAgentString();
+
+ // Get the current font size from the
+ fontSize = currentWebView.getSettings().getTextZoom();
+
+ // Set the status of the menu item checkboxes.
+ optionsDomStorageMenuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
+ optionsSaveFormDataMenuItem.setChecked(currentWebView.getSettings().getSaveFormData()); // Form data can be removed once the minimum API >= 26.
+ optionsEasyListMenuItem.setChecked(currentWebView.getEasyListEnabled());
+ optionsEasyPrivacyMenuItem.setChecked(currentWebView.getEasyPrivacyEnabled());
+ optionsFanboysAnnoyanceListMenuItem.setChecked(currentWebView.getFanboysAnnoyanceListEnabled());
+ optionsFanboysSocialBlockingListMenuItem.setChecked(currentWebView.getFanboysSocialBlockingListEnabled());
+ optionsUltraListMenuItem.setChecked(currentWebView.getUltraListEnabled());
+ optionsUltraPrivacyMenuItem.setChecked(currentWebView.getUltraPrivacyEnabled());
+ optionsBlockAllThirdPartyRequestsMenuItem.setChecked(currentWebView.getBlockAllThirdPartyRequests());
+ optionsSwipeToRefreshMenuItem.setChecked(currentWebView.getSwipeToRefresh());
+ optionsWideViewportMenuItem.setChecked(currentWebView.getSettings().getUseWideViewPort());
+ optionsDisplayImagesMenuItem.setChecked(currentWebView.getSettings().getLoadsImagesAutomatically());
+
+ // Initialize the display names for the blocklists with the number of blocked requests.
+ optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
+ optionsEasyListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASYLIST) + " - " + getString(R.string.easylist));
+ optionsEasyPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASYPRIVACY) + " - " + getString(R.string.easyprivacy));
+ optionsFanboysAnnoyanceListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST) + " - " + getString(R.string.fanboys_annoyance_list));
+ optionsFanboysSocialBlockingListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST) + " - " + getString(R.string.fanboys_social_blocking_list));
+ optionsUltraListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRALIST) + " - " + getString(R.string.ultralist));
+ optionsUltraPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRAPRIVACY) + " - " + getString(R.string.ultraprivacy));
+ optionsBlockAllThirdPartyRequestsMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS) + " - " + getString(R.string.block_all_third_party_requests));
+
+ // Enable DOM Storage if JavaScript is enabled.
+ optionsDomStorageMenuItem.setEnabled(currentWebView.getSettings().getJavaScriptEnabled());
+
+ // Set the checkbox status for dark WebView if the WebView supports it.
+ if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
+ optionsDarkWebViewMenuItem.setChecked(WebSettingsCompat.getForceDark(currentWebView.getSettings()) == WebSettingsCompat.FORCE_DARK_ON);
+ }
+ }
+
+ // Set the cookies menu item checked status.
+ optionsCookiesMenuItem.setChecked(cookieManager.acceptCookie());
+
+ // Enable Clear Cookies if there are any.
+ optionsClearCookiesMenuItem.setEnabled(cookieManager.hasCookies());
+
+ // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`, which links to `/data/data/com.stoutner.privacybrowser.standard`.
+ String privateDataDirectoryString = getApplicationInfo().dataDir;
+
+ // Get a count of the number of files in the Local Storage directory.
+ File localStorageDirectory = new File (privateDataDirectoryString + "/app_webview/Local Storage/");
+ int localStorageDirectoryNumberOfFiles = 0;
+ if (localStorageDirectory.exists()) {
+ // `Objects.requireNonNull` removes a lint warning that `localStorageDirectory.list` might produce a null pointed exception if it is dereferenced.
+ localStorageDirectoryNumberOfFiles = Objects.requireNonNull(localStorageDirectory.list()).length;
+ }
+
+ // Get a count of the number of files in the IndexedDB directory.
+ File indexedDBDirectory = new File (privateDataDirectoryString + "/app_webview/IndexedDB");
+ int indexedDBDirectoryNumberOfFiles = 0;
+ if (indexedDBDirectory.exists()) {
+ // `Objects.requireNonNull` removes a lint warning that `indexedDBDirectory.list` might produce a null pointed exception if it is dereferenced.
+ indexedDBDirectoryNumberOfFiles = Objects.requireNonNull(indexedDBDirectory.list()).length;
+ }
+
+ // Enable Clear DOM Storage if there is any.
+ optionsClearDomStorageMenuItem.setEnabled(localStorageDirectoryNumberOfFiles > 0 || indexedDBDirectoryNumberOfFiles > 0);
+
+ // Enable Clear Form Data is there is any. This can be removed once the minimum API >= 26.
+ if (Build.VERSION.SDK_INT < 26) {
+ // Get the WebView database.
+ WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
+
+ // Enable the clear form data menu item if there is anything to clear.
+ optionsClearFormDataMenuItem.setEnabled(webViewDatabase.hasFormData());
+ }
+
+ // Enable Clear Data if any of the submenu items are enabled.
+ optionsClearDataMenuItem.setEnabled(optionsClearCookiesMenuItem.isEnabled() || optionsClearDomStorageMenuItem.isEnabled() || optionsClearFormDataMenuItem.isEnabled());
+
+ // Disable Fanboy's Social Blocking List menu item if Fanboy's Annoyance List is checked.
+ optionsFanboysSocialBlockingListMenuItem.setEnabled(!optionsFanboysAnnoyanceListMenuItem.isChecked());
+
+ // Set the proxy title and check the applied proxy.
+ switch (proxyMode) {
+ case ProxyHelper.NONE:
+ // Set the proxy title.
+ optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_none));
+
+ // Check the proxy None radio button.
+ optionsProxyNoneMenuItem.setChecked(true);
+ break;
+
+ case ProxyHelper.TOR:
+ // Set the proxy title.
+ optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_tor));
+
+ // Check the proxy Tor radio button.
+ optionsProxyTorMenuItem.setChecked(true);
+ break;
+
+ case ProxyHelper.I2P:
+ // Set the proxy title.
+ optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_i2p));
+
+ // Check the proxy I2P radio button.
+ optionsProxyI2pMenuItem.setChecked(true);
+ break;
+
+ case ProxyHelper.CUSTOM:
+ // Set the proxy title.
+ optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_custom));
+
+ // Check the proxy Custom radio button.
+ optionsProxyCustomMenuItem.setChecked(true);
+ break;
+ }
+
+ // Select the current user agent menu item. A switch statement cannot be used because the user agents are not compile time constants.
+ if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[0])) { // Privacy Browser.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_privacy_browser));
+
+ // Select the Privacy Browser radio box.
+ optionsUserAgentPrivacyBrowserMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(webViewDefaultUserAgent)) { // WebView Default.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_webview_default));
+
+ // Select the WebView Default radio box.
+ optionsUserAgentWebViewDefaultMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[2])) { // Firefox on Android.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_android));
+
+ // Select the Firefox on Android radio box.
+ optionsUserAgentFirefoxOnAndroidMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[3])) { // Chrome on Android.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_android));
+
+ // Select the Chrome on Android radio box.
+ optionsUserAgentChromeOnAndroidMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[4])) { // Safari on iOS.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_ios));
+
+ // Select the Safari on iOS radio box.
+ optionsUserAgentSafariOnIosMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[5])) { // Firefox on Linux.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_linux));
+
+ // Select the Firefox on Linux radio box.
+ optionsUserAgentFirefoxOnLinuxMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[6])) { // Chromium on Linux.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chromium_on_linux));
+
+ // Select the Chromium on Linux radio box.
+ optionsUserAgentChromiumOnLinuxMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[7])) { // Firefox on Windows.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_windows));
+
+ // Select the Firefox on Windows radio box.
+ optionsUserAgentFirefoxOnWindowsMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[8])) { // Chrome on Windows.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_windows));
+
+ // Select the Chrome on Windows radio box.
+ optionsUserAgentChromeOnWindowsMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[9])) { // Edge on Windows.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_edge_on_windows));
+
+ // Select the Edge on Windows radio box.
+ optionsUserAgentEdgeOnWindowsMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[10])) { // Internet Explorer on Windows.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_internet_explorer_on_windows));
+
+ // Select the Internet on Windows radio box.
+ optionsUserAgentInternetExplorerOnWindowsMenuItem.setChecked(true);
+ } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[11])) { // Safari on macOS.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_macos));
+
+ // Select the Safari on macOS radio box.
+ optionsUserAgentSafariOnMacosMenuItem.setChecked(true);
+ } else { // Custom user agent.
+ // Update the user agent menu item title.
+ optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_custom));
+
+ // Select the Custom radio box.
+ optionsUserAgentCustomMenuItem.setChecked(true);
+ }
+
+ // Set the font size title.
+ optionsFontSizeMenuItem.setTitle(getString(R.string.font_size) + " - " + fontSize + "%");
+
+ // Run all the other default commands.
+ super.onPrepareOptionsMenu(menu);
+
+ // Display the menu.
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem menuItem) {
+ // Get a handle for the shared preferences.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);