- case R.id.font_size_seventy_five_percent:
- currentWebView.getSettings().setTextZoom(75);
- return true;
-
- case R.id.font_size_one_hundred_percent:
- currentWebView.getSettings().setTextZoom(100);
- return true;
-
- case R.id.font_size_one_hundred_twenty_five_percent:
- currentWebView.getSettings().setTextZoom(125);
- return true;
-
- case R.id.font_size_one_hundred_fifty_percent:
- currentWebView.getSettings().setTextZoom(150);
- return true;
-
- case R.id.font_size_one_hundred_seventy_five_percent:
- currentWebView.getSettings().setTextZoom(175);
- return true;
-
- case R.id.font_size_two_hundred_percent:
- currentWebView.getSettings().setTextZoom(200);
- return true;
-
- case R.id.swipe_to_refresh:
- // Get a handle for the swipe refresh layout.
- SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
-
- // Toggle swipe to refresh.
- swipeRefreshLayout.setEnabled(!swipeRefreshLayout.isEnabled());
- return true;
-
- case R.id.display_images:
- if (currentWebView.getSettings().getLoadsImagesAutomatically()) { // Images are currently loaded automatically.
- // Disable loading of images.
- currentWebView.getSettings().setLoadsImagesAutomatically(false);
-
- // Reload the website to remove existing images.
- currentWebView.reload();
- } else { // Images are not currently loaded automatically.
- // Enable loading of images. Missing images will be loaded without the need for a reload.
- currentWebView.getSettings().setLoadsImagesAutomatically(true);
- }
- return true;
-
- case R.id.night_mode:
- // Toggle night mode.
- nightMode = !nightMode;
-
- // Enable or disable JavaScript according to night mode, the global preference, and any domain settings.
- if (nightMode) { // Night mode is enabled, which requires JavaScript.
- // Enable JavaScript.
- currentWebView.getSettings().setJavaScriptEnabled(true);
- } else if (currentWebView.getDomainSettingsApplied()) { // Night mode is disabled and domain settings are applied. Set JavaScript according to the domain settings.
- // Apply the JavaScript preference that was stored the last time domain settings were loaded.
- currentWebView.getSettings().setJavaScriptEnabled(domainSettingsJavaScriptEnabled);
- } else { // Night mode is disabled and domain settings are not applied. Set JavaScript according to the global preference.
- // Apply the JavaScript preference.
- currentWebView.getSettings().setJavaScriptEnabled(sharedPreferences.getBoolean("javascript", false));
- }
-
- // Update the privacy icons.
- updatePrivacyIcons(false);
-
- // Reload the website.
- currentWebView.reload();
- return true;
-
- case R.id.find_on_page:
- // Get a handle for the views.
- Toolbar toolbar = findViewById(R.id.toolbar);
- LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
-
- // Hide the toolbar.
- toolbar.setVisibility(View.GONE);
-
- // Show the find on page linear layout.
- findOnPageLinearLayout.setVisibility(View.VISIBLE);
-
- // Display the keyboard. The app must wait 200 ms before running the command to work around a bug in Android.
- // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working
- findOnPageEditText.postDelayed(() -> {
- // Set the focus on `findOnPageEditText`.
- findOnPageEditText.requestFocus();
-
- // Display the keyboard. `0` sets no input flags.
- inputMethodManager.showSoftInput(findOnPageEditText, 0);
- }, 200);
- return true;
-
- case R.id.view_source:
- // Create an intent to launch the view source activity.
- Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class);
-
- // Add the user agent as an extra to the intent.
- viewSourceIntent.putExtra("user_agent", currentWebView.getSettings().getUserAgentString());
-
- // Make it so.
- startActivity(viewSourceIntent);
- return true;
-
- case R.id.share_url:
- // Setup the share string.
- String shareString = currentWebView.getTitle() + " – " + formattedUrlString;
-
- // Create the share intent.
- Intent shareIntent = new Intent(Intent.ACTION_SEND);
- shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
- shareIntent.setType("text/plain");
-
- // Make it so.
- startActivity(Intent.createChooser(shareIntent, getString(R.string.share_url)));
- return true;
-
- case R.id.print:
- // Get a `PrintManager` instance.
- PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
-
- // Create a print document adapter form the current WebView.
- PrintDocumentAdapter printDocumentAdapter = currentWebView.createPrintDocumentAdapter();
-
- // Remove the lint error below that `printManager` might be `null`.
- assert printManager != null;
-
- // Print the document. The print attributes are `null`.
- printManager.print(getString(R.string.privacy_browser_web_page), printDocumentAdapter, null);
- return true;
-
- case R.id.open_with_app:
- openWithApp(formattedUrlString);
- return true;
-
- case R.id.open_with_browser:
- openWithBrowser(formattedUrlString);
- return true;
-
- case R.id.add_to_homescreen:
- // Instantiate the create home screen shortcut dialog.
- DialogFragment createHomeScreenShortcutDialogFragment = CreateHomeScreenShortcutDialog.createDialog(currentWebView.getTitle(), formattedUrlString, favoriteIconBitmap);
-
- // Show the create home screen shortcut dialog.
- createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut));
- return true;
-
- case R.id.proxy_through_orbot:
- // Toggle the proxy through Orbot variable.
- proxyThroughOrbot = !proxyThroughOrbot;
-
- // Apply the proxy through Orbot settings.
- applyProxyThroughOrbot(true);
- return true;
-
- case R.id.refresh:
- if (menuItem.getTitle().equals(getString(R.string.refresh))) { // The refresh button was pushed.
- // Reload the current WebView.
- currentWebView.reload();
- } else { // The stop button was pushed.
- // Stop the loading of the WebView.
- currentWebView.stopLoading();
- }
- return true;
-
- case R.id.ad_consent:
- // Display the ad consent dialog.
- DialogFragment adConsentDialogFragment = new AdConsentDialog();
- adConsentDialogFragment.show(getSupportFragmentManager(), getString(R.string.ad_consent));
- return true;
-
- default:
- // Don't consume the event.
- return super.onOptionsItemSelected(menuItem);
- }
- }
-
- // removeAllCookies is deprecated, but it is required for API < 21.
- @SuppressWarnings("deprecation")
- @Override
- public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
- // Get the menu item ID.
- int menuItemId = menuItem.getItemId();
-
- // Run the commands that correspond to the selected menu item.
- switch (menuItemId) {
- case R.id.close_tab:
- // Get a handle for the tab layout.
- TabLayout tabLayout = findViewById(R.id.tablayout);
-
- // Get the current tab number.
- int currentTabNumber = tabLayout.getSelectedTabPosition();
-
- // Delete the current tab.
- tabLayout.removeTabAt(currentTabNumber);
-
- // Delete the current page.
- webViewPagerAdapter.deletePage(currentTabNumber);
- break;
-
- case R.id.clear_and_exit:
- // Close the bookmarks cursor and database.
- bookmarksCursor.close();
- bookmarksDatabaseHelper.close();
-
- // Get a handle for the shared preferences.
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
-
- // Get the status of the clear everything preference.
- boolean clearEverything = sharedPreferences.getBoolean("clear_everything", true);
-
- // Clear cookies.
- if (clearEverything || sharedPreferences.getBoolean("clear_cookies", true)) {
- // The command to remove cookies changed slightly in API 21.
- if (Build.VERSION.SDK_INT >= 21) {
- cookieManager.removeAllCookies(null);
- } else {
- cookieManager.removeAllCookie();
- }
-
- // Manually delete the cookies database, as `CookieManager` sometimes will not flush its changes to disk before `System.exit(0)` is run.
- try {
- // Two commands must be used because `Runtime.exec()` does not like `*`.
- Process deleteCookiesProcess = privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies");
- Process deleteCookiesJournalProcess = privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies-journal");
-
- // Wait until the processes have finished.
- deleteCookiesProcess.waitFor();
- deleteCookiesJournalProcess.waitFor();
- } catch (Exception exception) {
- // Do nothing if an error is thrown.
- }
- }
-
- // Clear DOM storage.
- if (clearEverything || sharedPreferences.getBoolean("clear_dom_storage", true)) {
- // Ask `WebStorage` to clear the DOM storage.
- WebStorage webStorage = WebStorage.getInstance();
- webStorage.deleteAllData();
-
- // Manually delete the DOM storage files and directories, as `WebStorage` sometimes will not flush its changes to disk before `System.exit(0)` is run.
- try {
- // A `String[]` must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
- Process deleteLocalStorageProcess = privacyBrowserRuntime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
-
- // Multiple commands must be used because `Runtime.exec()` does not like `*`.
- Process deleteIndexProcess = privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
- Process deleteQuotaManagerProcess = privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
- Process deleteQuotaManagerJournalProcess = privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
- Process deleteDatabaseProcess = privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
-
- // Wait until the processes have finished.
- deleteLocalStorageProcess.waitFor();
- deleteIndexProcess.waitFor();
- deleteQuotaManagerProcess.waitFor();
- deleteQuotaManagerJournalProcess.waitFor();
- deleteDatabaseProcess.waitFor();
- } catch (Exception exception) {
- // Do nothing if an error is thrown.
- }
- }
-
- // Clear form data if the API < 26.
- if ((Build.VERSION.SDK_INT < 26) && (clearEverything || sharedPreferences.getBoolean("clear_form_data", true))) {
- WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
- webViewDatabase.clearFormData();
-
- // Manually delete the form data database, as `WebViewDatabase` sometimes will not flush its changes to disk before `System.exit(0)` is run.
- try {
- // A string array must be used because the database contains a space and `Runtime.exec` will not otherwise escape the string correctly.
- Process deleteWebDataProcess = privacyBrowserRuntime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data"});
- Process deleteWebDataJournalProcess = privacyBrowserRuntime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data-journal"});
-
- // Wait until the processes have finished.
- deleteWebDataProcess.waitFor();
- deleteWebDataJournalProcess.waitFor();
- } catch (Exception exception) {
- // Do nothing if an error is thrown.
- }
- }
-
- // Clear the cache.
- if (clearEverything || sharedPreferences.getBoolean("clear_cache", true)) {
- // Clear the cache from each WebView.
- for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
- // Get the WebView tab fragment.
- WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
-
- // Get the fragment view.
- View fragmentView = webViewTabFragment.getView();
-
- // Only clear the cache if the WebView exists.
- if (fragmentView != null) {
- // Get the nested scroll WebView from the tab fragment.
- NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
-
- // Clear the cache for this WebView.
- nestedScrollWebView.clearCache(true);
- }
- }
-
- // Manually delete the cache directories.
- try {
- // Delete the main cache directory.
- Process deleteCacheProcess = privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/cache");
-
- // Delete the secondary `Service Worker` cache directory.
- // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
- Process deleteServiceWorkerProcess = privacyBrowserRuntime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Service Worker/"});
-
- // Wait until the processes have finished.
- deleteCacheProcess.waitFor();
- deleteServiceWorkerProcess.waitFor();
- } catch (Exception exception) {
- // Do nothing if an error is thrown.
- }
- }
-
- // Wipe out each WebView.
- for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
- // Get the WebView tab fragment.
- WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
-
- // Get the fragment view.
- View fragmentView = webViewTabFragment.getView();
-
- // Only wipe out the WebView if it exists.
- if (fragmentView != null) {
- // Get the nested scroll WebView from the tab fragment.
- NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
-
- // Clear SSL certificate preferences for this WebView.
- nestedScrollWebView.clearSslPreferences();
-
- // Clear the back/forward history for this WebView.
- nestedScrollWebView.clearHistory();
-
- // Destroy the internal state of `mainWebView`.
- nestedScrollWebView.destroy();
- }
- }
-
- // Clear the formatted URL string.
- formattedUrlString = null;
-
- // Clear the custom headers.
- customHeaders.clear();
-
- // Manually delete the `app_webview` folder, which contains the cookies, DOM storage, form data, and `Service Worker` cache.
- // See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`.
- if (clearEverything) {
- try {
- // Delete the folder.
- Process deleteAppWebviewProcess = privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview");
-
- // Wait until the process has finished.
- deleteAppWebviewProcess.waitFor();
- } catch (Exception exception) {
- // Do nothing if an error is thrown.
- }
- }
-
- // Close Privacy Browser. `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
- if (Build.VERSION.SDK_INT >= 21) {
- finishAndRemoveTask();
- } else {
- finish();
- }
-
- // Remove the terminated program from RAM. The status code is `0`.
- System.exit(0);
- break;
-
- case R.id.home:
- loadUrl(homepage);
- break;
-
- case R.id.back:
- if (currentWebView.canGoBack()) {
- // Reset the formatted URL string so the page will load correctly if blocking of third-party requests is enabled.
- formattedUrlString = "";
-
- // Set `navigatingHistory` so that the domain settings are applied when the new URL is loaded.
- navigatingHistory = true;