Fix unwanted restarts when a keyboard is connected/disconnected. https://redmine...
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
1 /*
2  * Copyright © 2015-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * Download cookie code contributed 2017 Hendrik Knackstedt.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
5  *
6  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
7  *
8  * Privacy Browser is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Privacy Browser is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 package com.stoutner.privacybrowser.activities;
23
24 import android.Manifest;
25 import android.annotation.SuppressLint;
26 import android.app.Activity;
27 import android.app.DownloadManager;
28 import android.app.SearchManager;
29 import android.content.ActivityNotFoundException;
30 import android.content.BroadcastReceiver;
31 import android.content.ClipData;
32 import android.content.ClipboardManager;
33 import android.content.Context;
34 import android.content.Intent;
35 import android.content.IntentFilter;
36 import android.content.SharedPreferences;
37 import android.content.pm.PackageManager;
38 import android.content.res.Configuration;
39 import android.database.Cursor;
40 import android.graphics.Bitmap;
41 import android.graphics.BitmapFactory;
42 import android.graphics.Typeface;
43 import android.graphics.drawable.BitmapDrawable;
44 import android.graphics.drawable.Drawable;
45 import android.net.Uri;
46 import android.net.http.SslCertificate;
47 import android.net.http.SslError;
48 import android.os.Build;
49 import android.os.Bundle;
50 import android.os.Environment;
51 import android.os.Handler;
52 import android.preference.PreferenceManager;
53 import android.print.PrintDocumentAdapter;
54 import android.print.PrintManager;
55 import android.text.Editable;
56 import android.text.Spanned;
57 import android.text.TextWatcher;
58 import android.text.style.ForegroundColorSpan;
59 import android.util.Patterns;
60 import android.view.ContextMenu;
61 import android.view.GestureDetector;
62 import android.view.KeyEvent;
63 import android.view.Menu;
64 import android.view.MenuItem;
65 import android.view.MotionEvent;
66 import android.view.View;
67 import android.view.ViewGroup;
68 import android.view.WindowManager;
69 import android.view.inputmethod.InputMethodManager;
70 import android.webkit.CookieManager;
71 import android.webkit.HttpAuthHandler;
72 import android.webkit.SslErrorHandler;
73 import android.webkit.ValueCallback;
74 import android.webkit.WebBackForwardList;
75 import android.webkit.WebChromeClient;
76 import android.webkit.WebResourceResponse;
77 import android.webkit.WebSettings;
78 import android.webkit.WebStorage;
79 import android.webkit.WebView;
80 import android.webkit.WebViewClient;
81 import android.webkit.WebViewDatabase;
82 import android.widget.ArrayAdapter;
83 import android.widget.CursorAdapter;
84 import android.widget.EditText;
85 import android.widget.FrameLayout;
86 import android.widget.ImageView;
87 import android.widget.LinearLayout;
88 import android.widget.ListView;
89 import android.widget.ProgressBar;
90 import android.widget.RadioButton;
91 import android.widget.RelativeLayout;
92 import android.widget.TextView;
93
94 import androidx.annotation.NonNull;
95 import androidx.appcompat.app.ActionBar;
96 import androidx.appcompat.app.ActionBarDrawerToggle;
97 import androidx.appcompat.app.AppCompatActivity;
98 import androidx.appcompat.widget.Toolbar;
99 import androidx.core.app.ActivityCompat;
100 import androidx.core.content.ContextCompat;
101 import androidx.core.view.GravityCompat;
102 import androidx.drawerlayout.widget.DrawerLayout;
103 import androidx.fragment.app.DialogFragment;
104 import androidx.fragment.app.FragmentManager;
105 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
106 import androidx.viewpager.widget.ViewPager;
107
108 import com.google.android.material.floatingactionbutton.FloatingActionButton;
109 import com.google.android.material.navigation.NavigationView;
110 import com.google.android.material.snackbar.Snackbar;
111 import com.google.android.material.tabs.TabLayout;
112
113 import com.stoutner.privacybrowser.BuildConfig;
114 import com.stoutner.privacybrowser.R;
115 import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter;
116 import com.stoutner.privacybrowser.asynctasks.GetHostIpAddresses;
117 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
118 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
119 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
120 import com.stoutner.privacybrowser.dialogs.CreateHomeScreenShortcutDialog;
121 import com.stoutner.privacybrowser.dialogs.DownloadFileDialog;
122 import com.stoutner.privacybrowser.dialogs.DownloadImageDialog;
123 import com.stoutner.privacybrowser.dialogs.DownloadLocationPermissionDialog;
124 import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog;
125 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
126 import com.stoutner.privacybrowser.dialogs.HttpAuthenticationDialog;
127 import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog;
128 import com.stoutner.privacybrowser.dialogs.SslCertificateErrorDialog;
129 import com.stoutner.privacybrowser.dialogs.UrlHistoryDialog;
130 import com.stoutner.privacybrowser.dialogs.ViewSslCertificateDialog;
131 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
132 import com.stoutner.privacybrowser.helpers.AdHelper;
133 import com.stoutner.privacybrowser.helpers.BlockListHelper;
134 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
135 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper;
136 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
137 import com.stoutner.privacybrowser.helpers.OrbotProxyHelper;
138 import com.stoutner.privacybrowser.views.NestedScrollWebView;
139
140 import java.io.ByteArrayInputStream;
141 import java.io.ByteArrayOutputStream;
142 import java.io.File;
143 import java.io.IOException;
144 import java.io.UnsupportedEncodingException;
145 import java.net.MalformedURLException;
146 import java.net.URL;
147 import java.net.URLDecoder;
148 import java.net.URLEncoder;
149 import java.util.ArrayList;
150 import java.util.Date;
151 import java.util.HashMap;
152 import java.util.HashSet;
153 import java.util.List;
154 import java.util.Map;
155 import java.util.Set;
156
157 // TODO.  Store up reloads for tabs that are not visible.
158 // TODO.  New tabs are white in dark mode.
159 // TODO.  Hide the tabs in full screen mode.
160 // TODO.  Find on page.
161 // TODO.  Use TabLayout.setScrollPosition to scroll to new tabs.
162
163 // AppCompatActivity from android.support.v7.app.AppCompatActivity must be used to have access to the SupportActionBar until the minimum API is >= 21.
164 public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener,
165         DownloadFileDialog.DownloadFileListener, DownloadImageDialog.DownloadImageListener, DownloadLocationPermissionDialog.DownloadLocationPermissionDialogListener, EditBookmarkDialog.EditBookmarkListener,
166         EditBookmarkFolderDialog.EditBookmarkFolderListener, NavigationView.OnNavigationItemSelectedListener, WebViewTabFragment.NewTabListener, PinnedMismatchDialog.PinnedMismatchListener,
167         UrlHistoryDialog.UrlHistoryListener {
168
169     // `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`.  It is also used in `onCreate()`, `onResume()`, and `applyProxyThroughOrbot()`.
170     public static String orbotStatus;
171
172     // The WebView pager adapter is accessed from `HttpAuthenticationDialog`, `PinnedMismatchDialog`, and `SslCertificateErrorDialog`.  It is also used in `onCreate()`, `onResume()`, and `addTab()`.
173     public static WebViewPagerAdapter webViewPagerAdapter;
174
175     // `reloadOnRestart` is public static so it can be accessed from `SettingsFragment`.  It is used in `onRestart()`
176     public static boolean reloadOnRestart;
177
178     // The load URL on restart variables are public static so they can be accessed from `BookmarksActivity`.  They are used in `onRestart()`.
179     public static boolean loadUrlOnRestart;
180     public static String urlToLoadOnRestart;
181
182     // `restartFromBookmarksActivity` is public static so it can be accessed from `BookmarksActivity`.  It is also used in `onRestart()`.
183     public static boolean restartFromBookmarksActivity;
184
185     // `currentBookmarksFolder` is public static so it can be accessed from `BookmarksActivity`.  It is also used in `onCreate()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`,
186     // `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
187     public static String currentBookmarksFolder;
188
189     // The user agent constants are public static so they can be accessed from `SettingsFragment`, `DomainsActivity`, and `DomainSettingsFragment`.
190     public final static int UNRECOGNIZED_USER_AGENT = -1;
191     public final static int SETTINGS_WEBVIEW_DEFAULT_USER_AGENT = 1;
192     public final static int SETTINGS_CUSTOM_USER_AGENT = 12;
193     public final static int DOMAINS_SYSTEM_DEFAULT_USER_AGENT = 0;
194     public final static int DOMAINS_WEBVIEW_DEFAULT_USER_AGENT = 2;
195     public final static int DOMAINS_CUSTOM_USER_AGENT = 13;
196
197
198
199     // The current WebView is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`,
200     // `findNextOnPage()`, `closeFindOnPage()`, `loadUrlFromTextBox()`, `onSslMismatchBack()`, `applyProxyThroughOrbot()`, and `applyDomainSettings()`.
201     private NestedScrollWebView currentWebView;
202
203     // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateContextMenu()`, and `loadUrl()`.
204     private final Map<String, String> customHeaders = new HashMap<>();
205
206     // The search URL is set in `applyProxyThroughOrbot()` and used in `onCreate()`, `onNewIntent()`, `loadURLFromTextBox()`, and `initializeWebView()`.
207     private String searchURL;
208
209     // The options menu is set in `onCreateOptionsMenu()` and used in `onOptionsItemSelected()`, `updatePrivacyIcons()`, and `initializeWebView()`.
210     private Menu optionsMenu;
211
212     // The blocklists are populated in `onCreate()` and accessed from `initializeWebView()`.
213     private ArrayList<List<String[]>> easyList;
214     private ArrayList<List<String[]>> easyPrivacy;
215     private ArrayList<List<String[]>> fanboysAnnoyanceList;
216     private ArrayList<List<String[]>> fanboysSocialList;
217     private ArrayList<List<String[]>> ultraPrivacy;
218
219     // `webViewDefaultUserAgent` is used in `onCreate()` and `onPrepareOptionsMenu()`.
220     private String webViewDefaultUserAgent;
221
222     // `proxyThroughOrbot` is used in `onRestart()`, `onOptionsItemSelected()`, `applyAppSettings()`, and `applyProxyThroughOrbot()`.
223     private boolean proxyThroughOrbot;
224
225     // The incognito mode is set in `applyAppSettings()` and used in `initializeWebView()`.
226     private boolean incognitoModeEnabled;
227
228     // The full screen browsing mode tracker is set it `applyAppSettings()` and used in `initializeWebView()`.
229     private boolean fullScreenBrowsingModeEnabled;
230
231     // `inFullScreenBrowsingMode` is used in `onCreate()`, `onConfigurationChanged()`, and `applyAppSettings()`.
232     private boolean inFullScreenBrowsingMode;
233
234     // Hide app bar is used in `applyAppSettings()` and `initializeWebView()`.
235     private boolean hideAppBar;
236
237     // `reapplyDomainSettingsOnRestart` is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, and `onAddDomain()`, .
238     private boolean reapplyDomainSettingsOnRestart;
239
240     // `reapplyAppSettingsOnRestart` is used in `onNavigationItemSelected()` and `onRestart()`.
241     private boolean reapplyAppSettingsOnRestart;
242
243     // `displayingFullScreenVideo` is used in `onCreate()` and `onResume()`.
244     private boolean displayingFullScreenVideo;
245
246     // `orbotStatusBroadcastReceiver` is used in `onCreate()` and `onDestroy()`.
247     private BroadcastReceiver orbotStatusBroadcastReceiver;
248
249     // `waitingForOrbot` is used in `onCreate()`, `onResume()`, and `applyProxyThroughOrbot()`.
250     private boolean waitingForOrbot;
251
252     // The action bar drawer toggle is initialized in `onCreate()` and used in `onResume()`.
253     private ActionBarDrawerToggle actionBarDrawerToggle;
254
255     // The color spans are used in `onCreate()` and `highlightUrlText()`.
256     private ForegroundColorSpan redColorSpan;
257     private ForegroundColorSpan initialGrayColorSpan;
258     private ForegroundColorSpan finalGrayColorSpan;
259
260     // The drawer header padding variables are used in `onCreate()` and `onConfigurationChanged()`.
261     private int drawerHeaderPaddingLeftAndRight;
262     private int drawerHeaderPaddingTop;
263     private int drawerHeaderPaddingBottom;
264
265     // `bookmarksDatabaseHelper` is used in `onCreate()`, `onDestroy`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`,
266     // and `loadBookmarksFolder()`.
267     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
268
269     // `bookmarksCursor` is used in `onDestroy()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
270     private Cursor bookmarksCursor;
271
272     // `bookmarksCursorAdapter` is used in `onCreateBookmark()`, `onCreateBookmarkFolder()` `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
273     private CursorAdapter bookmarksCursorAdapter;
274
275     // `oldFolderNameString` is used in `onCreate()` and `onSaveEditBookmarkFolder()`.
276     private String oldFolderNameString;
277
278     // `fileChooserCallback` is used in `onCreate()` and `onActivityResult()`.
279     private ValueCallback<Uri[]> fileChooserCallback;
280
281     // The download strings are used in `onCreate()`, `onRequestPermissionResult()` and `initializeWebView()`.
282     private String downloadUrl;
283     private String downloadContentDisposition;
284     private long downloadContentLength;
285
286     // `downloadImageUrl` is used in `onCreateContextMenu()` and `onRequestPermissionResult()`.
287     private String downloadImageUrl;
288
289     // The request codes are used in `onCreate()`, `onCreateContextMenu()`, `onCloseDownloadLocationPermissionDialog()`, `onRequestPermissionResult()`, and `initializeWebView()`.
290     private final int DOWNLOAD_FILE_REQUEST_CODE = 1;
291     private final int DOWNLOAD_IMAGE_REQUEST_CODE = 2;
292
293     @Override
294     // Remove the warning about needing to override `performClick()` when using an `OnTouchListener` with `WebView`.
295     @SuppressLint("ClickableViewAccessibility")
296     protected void onCreate(Bundle savedInstanceState) {
297         // Get a handle for the shared preferences.
298         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
299
300         // Get the theme and screenshot preferences.
301         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
302         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
303
304         // Disable screenshots if not allowed.
305         if (!allowScreenshots) {
306             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
307         }
308
309         // Set the activity theme.
310         if (darkTheme) {
311             setTheme(R.style.PrivacyBrowserDark);
312         } else {
313             setTheme(R.style.PrivacyBrowserLight);
314         }
315
316         // Run the default commands.
317         super.onCreate(savedInstanceState);
318
319         // Set the content view.
320         setContentView(R.layout.main_framelayout);
321
322         // Get a handle for the input method.
323         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
324
325         // Remove the lint warning below that the input method manager might be null.
326         assert inputMethodManager != null;
327
328         // Get a handle for the toolbar.
329         Toolbar toolbar = findViewById(R.id.toolbar);
330
331         // Set the action bar.  `SupportActionBar` must be used until the minimum API is >= 21.
332         setSupportActionBar(toolbar);
333
334         // Get a handle for the action bar.
335         ActionBar actionBar = getSupportActionBar();
336
337         // This is needed to get rid of the Android Studio warning that the action bar might be null.
338         assert actionBar != null;
339
340         // Add the custom layout, which shows the URL text bar.
341         actionBar.setCustomView(R.layout.url_app_bar);
342         actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
343
344         // Initialize the foreground color spans for highlighting the URLs.  We have to use the deprecated `getColor()` until API >= 23.
345         redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
346         initialGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
347         finalGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
348
349         // Get handles for the URL views.
350         EditText urlEditText = findViewById(R.id.url_edittext);
351
352         // Remove the formatting from `urlTextBar` when the user is editing the text.
353         urlEditText.setOnFocusChangeListener((View v, boolean hasFocus) -> {
354             if (hasFocus) {  // The user is editing the URL text box.
355                 // Remove the highlighting.
356                 urlEditText.getText().removeSpan(redColorSpan);
357                 urlEditText.getText().removeSpan(initialGrayColorSpan);
358                 urlEditText.getText().removeSpan(finalGrayColorSpan);
359             } else {  // The user has stopped editing the URL text box.
360                 // Move to the beginning of the string.
361                 urlEditText.setSelection(0);
362
363                 // Reapply the highlighting.
364                 highlightUrlText();
365             }
366         });
367
368         // Set the go button on the keyboard to load the URL in `urlTextBox`.
369         urlEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
370             // If the event is a key-down event on the `enter` button, load the URL.
371             if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
372                 // Load the URL into the mainWebView and consume the event.
373                 loadUrlFromTextBox();
374
375                 // If the enter key was pressed, consume the event.
376                 return true;
377             } else {
378                 // If any other key was pressed, do not consume the event.
379                 return false;
380             }
381         });
382
383         // Initialize the Orbot status and the waiting for Orbot trackers.
384         orbotStatus = "unknown";
385         waitingForOrbot = false;
386
387         // Create an Orbot status `BroadcastReceiver`.
388         orbotStatusBroadcastReceiver = new BroadcastReceiver() {
389             @Override
390             public void onReceive(Context context, Intent intent) {
391                 // Store the content of the status message in `orbotStatus`.
392                 orbotStatus = intent.getStringExtra("org.torproject.android.intent.extra.STATUS");
393
394                 // If Privacy Browser is waiting on Orbot, load the website now that Orbot is connected.
395                 if (orbotStatus.equals("ON") && waitingForOrbot) {
396                     // Reset the waiting for Orbot status.
397                     waitingForOrbot = false;
398
399                     // Get the intent that started the app.
400                     Intent launchingIntent = getIntent();
401
402                     // Get the information from the intent.
403                     String launchingIntentAction = launchingIntent.getAction();
404                     Uri launchingIntentUriData = launchingIntent.getData();
405
406                     // If the intent action is a web search, perform the search.
407                     if ((launchingIntentAction != null) && launchingIntentAction.equals(Intent.ACTION_WEB_SEARCH)) {
408                         // Create an encoded URL string.
409                         String encodedUrlString;
410
411                         // Sanitize the search input and convert it to a search.
412                         try {
413                             encodedUrlString = URLEncoder.encode(launchingIntent.getStringExtra(SearchManager.QUERY), "UTF-8");
414                         } catch (UnsupportedEncodingException exception) {
415                             encodedUrlString = "";
416                         }
417
418                         // Load the completed search URL.
419                         loadUrl(searchURL + encodedUrlString);
420                     } else if (launchingIntentUriData != null){  // Check to see if the intent contains a new URL.
421                         // Load the URL from the intent.
422                         loadUrl(launchingIntentUriData.toString());
423                     } else {  // The is no URL in the intent.
424                         // Select the homepage based on the proxy through Orbot status.
425                         if (proxyThroughOrbot) {
426                             // Load the Tor homepage.
427                             loadUrl(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
428                         } else {
429                             // Load the normal homepage.
430                             loadUrl(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
431                         }
432                     }
433                 }
434             }
435         };
436
437         // Register `orbotStatusBroadcastReceiver` on `this` context.
438         this.registerReceiver(orbotStatusBroadcastReceiver, new IntentFilter("org.torproject.android.intent.action.STATUS"));
439
440         // Instantiate the blocklist helper.
441         BlockListHelper blockListHelper = new BlockListHelper();
442
443         // Parse the block lists.
444         easyList = blockListHelper.parseBlockList(getAssets(), "blocklists/easylist.txt");
445         easyPrivacy = blockListHelper.parseBlockList(getAssets(), "blocklists/easyprivacy.txt");
446         fanboysAnnoyanceList = blockListHelper.parseBlockList(getAssets(), "blocklists/fanboy-annoyance.txt");
447         fanboysSocialList = blockListHelper.parseBlockList(getAssets(), "blocklists/fanboy-social.txt");
448         ultraPrivacy = blockListHelper.parseBlockList(getAssets(), "blocklists/ultraprivacy.txt");
449
450         // Get handles for views that need to be modified.
451         DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
452         NavigationView navigationView = findViewById(R.id.navigationview);
453         TabLayout tabLayout = findViewById(R.id.tablayout);
454         SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
455         ViewPager webViewPager = findViewById(R.id.webviewpager);
456         ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
457         FloatingActionButton launchBookmarksActivityFab = findViewById(R.id.launch_bookmarks_activity_fab);
458         FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
459         FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab);
460         EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
461
462         // Listen for touches on the navigation menu.
463         navigationView.setNavigationItemSelectedListener(this);
464
465         // Get handles for the navigation menu and the back and forward menu items.  The menu is zero-based.
466         Menu navigationMenu = navigationView.getMenu();
467         MenuItem navigationCloseTabMenuItem = navigationMenu.getItem(0);
468         MenuItem navigationBackMenuItem = navigationMenu.getItem(3);
469         MenuItem navigationForwardMenuItem = navigationMenu.getItem(4);
470         MenuItem navigationHistoryMenuItem = navigationMenu.getItem(5);
471         MenuItem navigationRequestsMenuItem = navigationMenu.getItem(6);
472
473         // Initialize the web view pager adapter.
474         webViewPagerAdapter = new WebViewPagerAdapter(getSupportFragmentManager());
475
476         // Set the pager adapter on the web view pager.
477         webViewPager.setAdapter(webViewPagerAdapter);
478
479         // Store up to 100 tabs in memory.
480         webViewPager.setOffscreenPageLimit(100);
481
482         // Update the web view pager every time a tab is modified.
483         webViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
484             @Override
485             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
486                 // Do nothing.
487             }
488
489             @Override
490             public void onPageSelected(int position) {
491                 // Set the current WebView.
492                 setCurrentWebView(position);
493
494                 // Select the corresponding tab if it does not match the currently selected page.  This will happen if the page was scrolled via swiping in the view pager.
495                 if (tabLayout.getSelectedTabPosition() != position) {
496                     // Get a handle for the corresponding tab.
497                     TabLayout.Tab correspondingTab = tabLayout.getTabAt(position);
498
499                     // Assert that the corresponding tab is not null.
500                     assert correspondingTab != null;
501
502                     // Select the corresponding tab.
503                     correspondingTab.select();
504                 }
505             }
506
507             @Override
508             public void onPageScrollStateChanged(int state) {
509                 // Do nothing.
510             }
511         });
512
513         // Display the View SSL Certificate dialog when the currently selected tab is reselected.
514         tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
515             @Override
516             public void onTabSelected(TabLayout.Tab tab) {
517                 // Select the same page in the view pager.
518                 webViewPager.setCurrentItem(tab.getPosition());
519             }
520
521             @Override
522             public void onTabUnselected(TabLayout.Tab tab) {
523                 // Do nothing.
524             }
525
526             @Override
527             public void onTabReselected(TabLayout.Tab tab) {
528                 // Instantiate the View SSL Certificate dialog.
529                 DialogFragment viewSslCertificateDialogFragment = ViewSslCertificateDialog.displayDialog(currentWebView.getWebViewFragmentId());
530
531                 // Display the View SSL Certificate dialog.
532                 viewSslCertificateDialogFragment.show(getSupportFragmentManager(), getString(R.string.view_ssl_certificate));
533             }
534         });
535
536         // Add the first tab.
537         addTab(null);
538
539         // Set the bookmarks drawer resources according to the theme.  This can't be done in the layout due to compatibility issues with the `DrawerLayout` support widget.
540         // The deprecated `getResources().getDrawable()` must be used until the minimum API >= 21 and and `getResources().getColor()` must be used until the minimum API >= 23.
541         if (darkTheme) {
542             launchBookmarksActivityFab.setImageDrawable(getResources().getDrawable(R.drawable.bookmarks_dark));
543             createBookmarkFolderFab.setImageDrawable(getResources().getDrawable(R.drawable.create_folder_dark));
544             createBookmarkFab.setImageDrawable(getResources().getDrawable(R.drawable.create_bookmark_dark));
545             bookmarksListView.setBackgroundColor(getResources().getColor(R.color.gray_850));
546         } else {
547             launchBookmarksActivityFab.setImageDrawable(getResources().getDrawable(R.drawable.bookmarks_light));
548             createBookmarkFolderFab.setImageDrawable(getResources().getDrawable(R.drawable.create_folder_light));
549             createBookmarkFab.setImageDrawable(getResources().getDrawable(R.drawable.create_bookmark_light));
550             bookmarksListView.setBackgroundColor(getResources().getColor(R.color.white));
551         }
552
553         // Set the launch bookmarks activity FAB to launch the bookmarks activity.
554         launchBookmarksActivityFab.setOnClickListener(v -> {
555             // Get a copy of the favorite icon bitmap.
556             Bitmap favoriteIconBitmap = currentWebView.getFavoriteOrDefaultIcon();
557
558             // Create a favorite icon byte array output stream.
559             ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
560
561             // Convert the favorite icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
562             favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
563
564             // Convert the favorite icon byte array stream to a byte array.
565             byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
566
567             // Create an intent to launch the bookmarks activity.
568             Intent bookmarksIntent = new Intent(getApplicationContext(), BookmarksActivity.class);
569
570             // Add the extra information to the intent.
571             bookmarksIntent.putExtra("current_url", currentWebView.getUrl());
572             bookmarksIntent.putExtra("current_title", currentWebView.getTitle());
573             bookmarksIntent.putExtra("current_folder", currentBookmarksFolder);
574             bookmarksIntent.putExtra("favorite_icon_byte_array", favoriteIconByteArray);
575
576             // Make it so.
577             startActivity(bookmarksIntent);
578         });
579
580         // Set the create new bookmark folder FAB to display an alert dialog.
581         createBookmarkFolderFab.setOnClickListener(v -> {
582             // Create a create bookmark folder dialog.
583             DialogFragment createBookmarkFolderDialog = CreateBookmarkFolderDialog.createBookmarkFolder(currentWebView.getFavoriteOrDefaultIcon());
584
585             // Show the create bookmark folder dialog.
586             createBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.create_folder));
587         });
588
589         // Set the create new bookmark FAB to display an alert dialog.
590         createBookmarkFab.setOnClickListener(view -> {
591             // Instantiate the create bookmark dialog.
592             DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebView.getUrl(), currentWebView.getTitle(), currentWebView.getFavoriteOrDefaultIcon());
593
594             // Display the create bookmark dialog.
595             createBookmarkDialog.show(getSupportFragmentManager(), getString(R.string.create_bookmark));
596         });
597
598         // Search for the string on the page whenever a character changes in the `findOnPageEditText`.
599         findOnPageEditText.addTextChangedListener(new TextWatcher() {
600             @Override
601             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
602                 // Do nothing.
603             }
604
605             @Override
606             public void onTextChanged(CharSequence s, int start, int before, int count) {
607                 // Do nothing.
608             }
609
610             @Override
611             public void afterTextChanged(Editable s) {
612                 // Search for the text in `mainWebView`.
613                 currentWebView.findAllAsync(findOnPageEditText.getText().toString());
614             }
615         });
616
617         // Set the `check mark` button for the `findOnPageEditText` keyboard to close the soft keyboard.
618         findOnPageEditText.setOnKeyListener((v, keyCode, event) -> {
619             if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {  // The `enter` key was pressed.
620                 // Hide the soft keyboard.
621                 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
622
623                 // Consume the event.
624                 return true;
625             } else {  // A different key was pressed.
626                 // Do not consume the event.
627                 return false;
628             }
629         });
630
631         // Implement swipe to refresh.
632         swipeRefreshLayout.setOnRefreshListener(() -> currentWebView.reload());
633
634         // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
635         swipeRefreshLayout.setProgressViewOffset(false, swipeRefreshLayout.getProgressViewStartOffset() - 10, swipeRefreshLayout.getProgressViewEndOffset());
636
637         // Set the swipe to refresh color according to the theme.
638         if (darkTheme) {
639             swipeRefreshLayout.setColorSchemeResources(R.color.blue_800);
640             swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.gray_850);
641         } else {
642             swipeRefreshLayout.setColorSchemeResources(R.color.blue_500);
643         }
644
645         // `DrawerTitle` identifies the `DrawerLayouts` in accessibility mode.
646         drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));
647         drawerLayout.setDrawerTitle(GravityCompat.END, getString(R.string.bookmarks));
648
649         // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
650         bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
651
652         // Initialize `currentBookmarksFolder`.  `""` is the home folder in the database.
653         currentBookmarksFolder = "";
654
655         // Load the home folder, which is `""` in the database.
656         loadBookmarksFolder();
657
658         bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
659             // Convert the id from long to int to match the format of the bookmarks database.
660             int databaseID = (int) id;
661
662             // Get the bookmark cursor for this ID and move it to the first row.
663             Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseID);
664             bookmarkCursor.moveToFirst();
665
666             // Act upon the bookmark according to the type.
667             if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {  // The selected bookmark is a folder.
668                 // Store the new folder name in `currentBookmarksFolder`.
669                 currentBookmarksFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
670
671                 // Load the new folder.
672                 loadBookmarksFolder();
673             } else {  // The selected bookmark is not a folder.
674                 // Load the bookmark URL.
675                 loadUrl(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
676
677                 // Close the bookmarks drawer.
678                 drawerLayout.closeDrawer(GravityCompat.END);
679             }
680
681             // Close the `Cursor`.
682             bookmarkCursor.close();
683         });
684
685         bookmarksListView.setOnItemLongClickListener((parent, view, position, id) -> {
686             // Convert the database ID from `long` to `int`.
687             int databaseId = (int) id;
688
689             // Find out if the selected bookmark is a folder.
690             boolean isFolder = bookmarksDatabaseHelper.isFolder(databaseId);
691
692             if (isFolder) {
693                 // Save the current folder name, which is used in `onSaveEditBookmarkFolder()`.
694                 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
695
696                 // Show the edit bookmark folder `AlertDialog` and name the instance `@string/edit_folder`.
697                 DialogFragment editBookmarkFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId, currentWebView.getFavoriteOrDefaultIcon());
698                 editBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.edit_folder));
699             } else {
700                 // Show the edit bookmark `AlertDialog` and name the instance `@string/edit_bookmark`.
701                 DialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId, currentWebView.getFavoriteOrDefaultIcon());
702                 editBookmarkDialog.show(getSupportFragmentManager(), getString(R.string.edit_bookmark));
703             }
704
705             // Consume the event.
706             return true;
707         });
708
709         // Get the status bar pixel size.
710         int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
711         int statusBarPixelSize = getResources().getDimensionPixelSize(statusBarResourceId);
712
713         // Get the resource density.
714         float screenDensity = getResources().getDisplayMetrics().density;
715
716         // Calculate the drawer header padding.  This is used to move the text in the drawer headers below any cutouts.
717         drawerHeaderPaddingLeftAndRight = (int) (15 * screenDensity);
718         drawerHeaderPaddingTop = statusBarPixelSize + (int) (4 * screenDensity);
719         drawerHeaderPaddingBottom = (int) (8 * screenDensity);
720
721         // The drawer listener is used to update the navigation menu.`
722         drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
723             @Override
724             public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
725             }
726
727             @Override
728             public void onDrawerOpened(@NonNull View drawerView) {
729             }
730
731             @Override
732             public void onDrawerClosed(@NonNull View drawerView) {
733             }
734
735             @Override
736             public void onDrawerStateChanged(int newState) {
737                 if ((newState == DrawerLayout.STATE_SETTLING) || (newState == DrawerLayout.STATE_DRAGGING)) {  // A drawer is opening or closing.
738                     // Get handles for the drawer headers.
739                     TextView navigationHeaderTextView = findViewById(R.id.navigationText);
740                     TextView bookmarksHeaderTextView = findViewById(R.id.bookmarks_title_textview);
741
742                     // Apply the navigation header paddings if the view is not null (sometimes it is null if another activity has already started).  This moves the text in the header below any cutouts.
743                     if (navigationHeaderTextView != null) {
744                         navigationHeaderTextView.setPadding(drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingTop, drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingBottom);
745                     }
746
747                     // Apply the bookmarks header paddings if the view is not null (sometimes it is null if another activity has already started).  This moves the text in the header below any cutouts.
748                     if (bookmarksHeaderTextView != null) {
749                         bookmarksHeaderTextView.setPadding(drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingTop, drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingBottom);
750                     }
751
752                     // Update the navigation menu items.
753                     navigationCloseTabMenuItem.setEnabled(tabLayout.getTabCount() > 1);
754                     navigationBackMenuItem.setEnabled(currentWebView.canGoBack());
755                     navigationForwardMenuItem.setEnabled(currentWebView.canGoForward());
756                     navigationHistoryMenuItem.setEnabled((currentWebView.canGoBack() || currentWebView.canGoForward()));
757                     navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
758
759                     // Hide the keyboard (if displayed).
760                     inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
761
762                     // Clear the focus from from the URL text box and the WebView.  This removes any text selection markers and context menus, which otherwise draw above the open drawers.
763                     urlEditText.clearFocus();
764                     currentWebView.clearFocus();
765                 }
766             }
767         });
768
769         // Create the hamburger icon at the start of the AppBar.
770         actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
771
772         // Replace the header that `WebView` creates for `X-Requested-With` with a null value.  The default value is the application ID (com.stoutner.privacybrowser.standard).
773         customHeaders.put("X-Requested-With", "");
774
775         // Initialize the default preference values the first time the program is run.  `false` keeps this command from resetting any current preferences back to default.
776         PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
777
778         // Inflate a bare WebView to get the default user agent.  It is not used to render content on the screen.
779         @SuppressLint("InflateParams") View webViewLayout = getLayoutInflater().inflate(R.layout.bare_webview, null, false);
780
781         // Get a handle for the WebView.
782         WebView bareWebView = webViewLayout.findViewById(R.id.bare_webview);
783
784         // Store the default user agent.
785         webViewDefaultUserAgent = bareWebView.getSettings().getUserAgentString();
786
787         // Destroy the bare WebView.
788         bareWebView.destroy();
789     }
790
791     @Override
792     protected void onNewIntent(Intent intent) {
793         // Get the information from the intent.
794         String intentAction = intent.getAction();
795         Uri intentUriData = intent.getData();
796
797         // Only process the URI if it contains data.  If the user pressed the desktop icon after the app was already running the URI will be null.
798         if (intentUriData != null) {
799             // Sets the new intent as the activity intent, which replaces the one that originally started the app.
800             setIntent(intent);
801
802             // Add a new tab.
803             addTab(null);
804
805             // Create a URL string.
806             String url;
807
808             // If the intent action is a web search, perform the search.
809             if ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH)) {
810                 // Create an encoded URL string.
811                 String encodedUrlString;
812
813                 // Sanitize the search input and convert it to a search.
814                 try {
815                     encodedUrlString = URLEncoder.encode(intent.getStringExtra(SearchManager.QUERY), "UTF-8");
816                 } catch (UnsupportedEncodingException exception) {
817                     encodedUrlString = "";
818                 }
819
820                 // Add the base search URL.
821                 url = searchURL + encodedUrlString;
822             } else {  // The intent should contain a URL.
823                 // Set the intent data as the URL.
824                 url = intentUriData.toString();
825             }
826
827             // Load the URL.
828             loadUrl(url);
829
830             // Get a handle for the drawer layout.
831             DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
832
833             // Close the navigation drawer if it is open.
834             if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
835                 drawerLayout.closeDrawer(GravityCompat.START);
836             }
837
838             // Close the bookmarks drawer if it is open.
839             if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
840                 drawerLayout.closeDrawer(GravityCompat.END);
841             }
842
843             // Clear the keyboard if displayed and remove the focus on the urlTextBar if it has it.
844             currentWebView.requestFocus();
845         }
846     }
847
848     @Override
849     public void onRestart() {
850         // Run the default commands.
851         super.onRestart();
852
853         // Make sure Orbot is running if Privacy Browser is proxying through Orbot.
854         if (proxyThroughOrbot) {
855             // Request Orbot to start.  If Orbot is already running no hard will be caused by this request.
856             Intent orbotIntent = new Intent("org.torproject.android.intent.action.START");
857
858             // Send the intent to the Orbot package.
859             orbotIntent.setPackage("org.torproject.android");
860
861             // Make it so.
862             sendBroadcast(orbotIntent);
863         }
864
865         // Apply the app settings if returning from the Settings activity.
866         if (reapplyAppSettingsOnRestart) {
867             // Apply the app settings.
868             applyAppSettings();
869
870             // Reload the webpage to handle changes to night mode and displaying of images.
871             if (reloadOnRestart) {
872                 // Reload the WebViews.
873                 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
874                     // Get the WebView tab fragment.
875                     WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
876
877                     // Get the fragment view.
878                     View fragmentView = webViewTabFragment.getView();
879
880                     // Only reload the WebViews if they exist.
881                     if (fragmentView != null) {
882                         // Get the nested scroll WebView from the tab fragment.
883                         NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
884
885                         // Reload the WebView.  This doesn't seem to work if for WebViews that aren't visible.
886                         nestedScrollWebView.reload();
887                     }
888                 }
889
890                 // Reset `reloadOnRestartBoolean`.
891                 reloadOnRestart = false;
892             }
893
894             // Reset the return from settings flag.
895             reapplyAppSettingsOnRestart = false;
896         }
897
898         // TODO apply to all the tabs.
899         // Apply the domain settings if returning from the Domains activity.
900         if (reapplyDomainSettingsOnRestart) {
901             // Reset the current domain name so the domain settings will be reapplied.
902             currentWebView.resetCurrentDomainName();
903
904             // Reapply the domain settings.
905             applyDomainSettings(currentWebView, currentWebView.getUrl(), false, true);  // TODO.
906
907             // Reset the reapply domain settings on restart tracker.
908             reapplyDomainSettingsOnRestart = false;
909         }
910
911         // Load the URL on restart (used when loading a bookmark.
912         if (loadUrlOnRestart) {
913             // Load the specified URL.
914             loadUrl(urlToLoadOnRestart);
915
916             // Reset the load on restart tracker.
917             loadUrlOnRestart = false;
918         }
919
920         // Update the bookmarks drawer if returning from the Bookmarks activity.
921         if (restartFromBookmarksActivity) {
922             // Get a handle for the drawer layout.
923             DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
924
925             // Close the bookmarks drawer.
926             drawerLayout.closeDrawer(GravityCompat.END);
927
928             // Reload the bookmarks drawer.
929             loadBookmarksFolder();
930
931             // Reset `restartFromBookmarksActivity`.
932             restartFromBookmarksActivity = false;
933         }
934
935         // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.  This can be important if the screen was rotated.
936         updatePrivacyIcons(true);
937     }
938
939     // `onResume()` runs after `onStart()`, which runs after `onCreate()` and `onRestart()`.
940     @Override
941     public void onResume() {
942         // Run the default commands.
943         super.onResume();
944
945         for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
946             // Get the WebView tab fragment.
947             WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
948
949             // Get the fragment view.
950             View fragmentView = webViewTabFragment.getView();
951
952             // Only resume the WebViews if they exist (they won't when the app is first created).
953             if (fragmentView != null) {
954                 // Get the nested scroll WebView from the tab fragment.
955                 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
956
957                 // Resume the nested scroll WebView JavaScript timers.
958                 nestedScrollWebView.resumeTimers();
959
960                 // Resume the nested scroll WebView.
961                 nestedScrollWebView.onResume();
962             }
963         }
964
965         // Display a message to the user if waiting for Orbot.
966         if (waitingForOrbot && !orbotStatus.equals("ON")) {
967             // Disable the wide view port so that the waiting for Orbot text is displayed correctly.
968             currentWebView.getSettings().setUseWideViewPort(false);
969
970             // Load a waiting page.  `null` specifies no encoding, which defaults to ASCII.
971             currentWebView.loadData("<html><body><br/><center><h1>" + getString(R.string.waiting_for_orbot) + "</h1></center></body></html>", "text/html", null);
972         }
973
974         if (displayingFullScreenVideo || inFullScreenBrowsingMode) {
975             // Get a handle for the root frame layouts.
976             FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout);
977
978             // Remove the translucent status flag.  This is necessary so the root frame layout can fill the entire screen.
979             getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
980
981             /* Hide the system bars.
982              * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
983              * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
984              * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
985              * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
986              */
987             rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
988                     View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
989         } else if (BuildConfig.FLAVOR.contentEquals("free")) {  // Resume the adView for the free flavor.
990             // Resume the ad.
991             AdHelper.resumeAd(findViewById(R.id.adview));
992         }
993     }
994
995     @Override
996     public void onPause() {
997         // Run the default commands.
998         super.onPause();
999
1000         for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
1001             // Get the WebView tab fragment.
1002             WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
1003
1004             // Get the fragment view.
1005             View fragmentView = webViewTabFragment.getView();
1006
1007             // Only pause the WebViews if they exist (they won't when the app is first created).
1008             if (fragmentView != null) {
1009                 // Get the nested scroll WebView from the tab fragment.
1010                 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
1011
1012                 // Pause the nested scroll WebView.
1013                 nestedScrollWebView.onPause();
1014
1015                 // Pause the nested scroll WebView JavaScript timers.
1016                 nestedScrollWebView.pauseTimers();
1017             }
1018         }
1019
1020         // Pause the ad or it will continue to consume resources in the background on the free flavor.
1021         if (BuildConfig.FLAVOR.contentEquals("free")) {
1022             // Pause the ad.
1023             AdHelper.pauseAd(findViewById(R.id.adview));
1024         }
1025     }
1026
1027     @Override
1028     public void onDestroy() {
1029         // Unregister the Orbot status broadcast receiver.
1030         this.unregisterReceiver(orbotStatusBroadcastReceiver);
1031
1032         // Close the bookmarks cursor and database.
1033         bookmarksCursor.close();
1034         bookmarksDatabaseHelper.close();
1035
1036         // Run the default commands.
1037         super.onDestroy();
1038     }
1039
1040     @Override
1041     public boolean onCreateOptionsMenu(Menu menu) {
1042         // Inflate the menu.  This adds items to the action bar if it is present.
1043         getMenuInflater().inflate(R.menu.webview_options_menu, menu);
1044
1045         // Store a handle for the options menu so it can be used by `onOptionsItemSelected()` and `updatePrivacyIcons()`.
1046         optionsMenu = menu;
1047
1048         // Set the initial status of the privacy icons.  `false` does not call `invalidateOptionsMenu` as the last step.
1049         updatePrivacyIcons(false);
1050
1051         // Get handles for the menu items.
1052         MenuItem toggleFirstPartyCookiesMenuItem = menu.findItem(R.id.toggle_first_party_cookies);
1053         MenuItem toggleThirdPartyCookiesMenuItem = menu.findItem(R.id.toggle_third_party_cookies);
1054         MenuItem toggleDomStorageMenuItem = menu.findItem(R.id.toggle_dom_storage);
1055         MenuItem toggleSaveFormDataMenuItem = menu.findItem(R.id.toggle_save_form_data);  // Form data can be removed once the minimum API >= 26.
1056         MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data);  // Form data can be removed once the minimum API >= 26.
1057         MenuItem refreshMenuItem = menu.findItem(R.id.refresh);
1058         MenuItem adConsentMenuItem = menu.findItem(R.id.ad_consent);
1059
1060         // Only display third-party cookies if API >= 21
1061         toggleThirdPartyCookiesMenuItem.setVisible(Build.VERSION.SDK_INT >= 21);
1062
1063         // Only display the form data menu items if the API < 26.
1064         toggleSaveFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
1065         clearFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
1066
1067         // Disable the clear form data menu item if the API >= 26 so that the status of the main Clear Data is calculated correctly.
1068         clearFormDataMenuItem.setEnabled(Build.VERSION.SDK_INT < 26);
1069
1070         // Only show Ad Consent if this is the free flavor.
1071         adConsentMenuItem.setVisible(BuildConfig.FLAVOR.contentEquals("free"));
1072
1073         // Get the shared preference values.
1074         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
1075
1076         // Get the dark theme and app bar preferences..
1077         boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean("display_additional_app_bar_icons", false);
1078         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
1079
1080         // 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.
1081         if (displayAdditionalAppBarIcons) {
1082             toggleFirstPartyCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
1083             toggleDomStorageMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
1084             refreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
1085         } else { //Do not display the additional icons.
1086             toggleFirstPartyCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
1087             toggleDomStorageMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
1088             refreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
1089         }
1090
1091         // Replace Refresh with Stop if a URL is already loading.
1092         if (currentWebView != null && currentWebView.getProgress() != 100) {
1093             // Set the title.
1094             refreshMenuItem.setTitle(R.string.stop);
1095
1096             // If the icon is displayed in the AppBar, set it according to the theme.
1097             if (displayAdditionalAppBarIcons) {
1098                 if (darkTheme) {
1099                     refreshMenuItem.setIcon(R.drawable.close_dark);
1100                 } else {
1101                     refreshMenuItem.setIcon(R.drawable.close_light);
1102                 }
1103             }
1104         }
1105
1106         return true;
1107     }
1108
1109     @Override
1110     public boolean onPrepareOptionsMenu(Menu menu) {
1111         // Get handles for the menu items.
1112         MenuItem addOrEditDomain = menu.findItem(R.id.add_or_edit_domain);
1113         MenuItem firstPartyCookiesMenuItem = menu.findItem(R.id.toggle_first_party_cookies);
1114         MenuItem thirdPartyCookiesMenuItem = menu.findItem(R.id.toggle_third_party_cookies);
1115         MenuItem domStorageMenuItem = menu.findItem(R.id.toggle_dom_storage);
1116         MenuItem saveFormDataMenuItem = menu.findItem(R.id.toggle_save_form_data);  // Form data can be removed once the minimum API >= 26.
1117         MenuItem clearDataMenuItem = menu.findItem(R.id.clear_data);
1118         MenuItem clearCookiesMenuItem = menu.findItem(R.id.clear_cookies);
1119         MenuItem clearDOMStorageMenuItem = menu.findItem(R.id.clear_dom_storage);
1120         MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data);  // Form data can be removed once the minimum API >= 26.
1121         MenuItem blocklistsMenuItem = menu.findItem(R.id.blocklists);
1122         MenuItem easyListMenuItem = menu.findItem(R.id.easylist);
1123         MenuItem easyPrivacyMenuItem = menu.findItem(R.id.easyprivacy);
1124         MenuItem fanboysAnnoyanceListMenuItem = menu.findItem(R.id.fanboys_annoyance_list);
1125         MenuItem fanboysSocialBlockingListMenuItem = menu.findItem(R.id.fanboys_social_blocking_list);
1126         MenuItem ultraPrivacyMenuItem = menu.findItem(R.id.ultraprivacy);
1127         MenuItem blockAllThirdPartyRequestsMenuItem = menu.findItem(R.id.block_all_third_party_requests);
1128         MenuItem fontSizeMenuItem = menu.findItem(R.id.font_size);
1129         MenuItem swipeToRefreshMenuItem = menu.findItem(R.id.swipe_to_refresh);
1130         MenuItem displayImagesMenuItem = menu.findItem(R.id.display_images);
1131         MenuItem nightModeMenuItem = menu.findItem(R.id.night_mode);
1132         MenuItem proxyThroughOrbotMenuItem = menu.findItem(R.id.proxy_through_orbot);
1133
1134         // Get a handle for the cookie manager.
1135         CookieManager cookieManager = CookieManager.getInstance();
1136
1137         // Initialize the current user agent string and the font size.
1138         String currentUserAgent = getString(R.string.user_agent_privacy_browser);
1139         int fontSize = 100;
1140
1141         // 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.
1142         if (currentWebView != null) {
1143             // Set the add or edit domain text.
1144             if (currentWebView.getDomainSettingsApplied()) {
1145                 addOrEditDomain.setTitle(R.string.edit_domain_settings);
1146             } else {
1147                 addOrEditDomain.setTitle(R.string.add_domain_settings);
1148             }
1149
1150             // Get the current user agent from the WebView.
1151             currentUserAgent = currentWebView.getSettings().getUserAgentString();
1152
1153             // Get the current font size from the
1154             fontSize = currentWebView.getSettings().getTextZoom();
1155
1156             // Set the status of the menu item checkboxes.
1157             domStorageMenuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
1158             saveFormDataMenuItem.setChecked(currentWebView.getSettings().getSaveFormData());  // Form data can be removed once the minimum API >= 26.
1159             easyListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASY_LIST));
1160             easyPrivacyMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASY_PRIVACY));
1161             fanboysAnnoyanceListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1162             fanboysSocialBlockingListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST));
1163             ultraPrivacyMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRA_PRIVACY));
1164             blockAllThirdPartyRequestsMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
1165             swipeToRefreshMenuItem.setChecked(currentWebView.getSwipeToRefresh());
1166             displayImagesMenuItem.setChecked(currentWebView.getSettings().getLoadsImagesAutomatically());
1167             nightModeMenuItem.setChecked(currentWebView.getNightMode());
1168
1169             // Initialize the display names for the blocklists with the number of blocked requests.
1170             blocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
1171             easyListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASY_LIST) + " - " + getString(R.string.easylist));
1172             easyPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASY_PRIVACY) + " - " + getString(R.string.easyprivacy));
1173             fanboysAnnoyanceListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST) + " - " + getString(R.string.fanboys_annoyance_list));
1174             fanboysSocialBlockingListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST) + " - " + getString(R.string.fanboys_social_blocking_list));
1175             ultraPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRA_PRIVACY) + " - " + getString(R.string.ultraprivacy));
1176             blockAllThirdPartyRequestsMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS) + " - " + getString(R.string.block_all_third_party_requests));
1177
1178             // Only modify third-party cookies if the API >= 21.
1179             if (Build.VERSION.SDK_INT >= 21) {
1180                 // Set the status of the third-party cookies checkbox.
1181                 thirdPartyCookiesMenuItem.setChecked(cookieManager.acceptThirdPartyCookies(currentWebView));
1182
1183                 // Enable third-party cookies if first-party cookies are enabled.
1184                 thirdPartyCookiesMenuItem.setEnabled(cookieManager.acceptCookie());
1185             }
1186
1187             // Enable DOM Storage if JavaScript is enabled.
1188             domStorageMenuItem.setEnabled(currentWebView.getSettings().getJavaScriptEnabled());
1189         }
1190
1191         // Set the status of the menu item checkboxes.
1192         firstPartyCookiesMenuItem.setChecked(cookieManager.acceptCookie());
1193         proxyThroughOrbotMenuItem.setChecked(proxyThroughOrbot);
1194
1195         // Enable Clear Cookies if there are any.
1196         clearCookiesMenuItem.setEnabled(cookieManager.hasCookies());
1197
1198         // 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`.
1199         String privateDataDirectoryString = getApplicationInfo().dataDir;
1200
1201         // Get a count of the number of files in the Local Storage directory.
1202         File localStorageDirectory = new File (privateDataDirectoryString + "/app_webview/Local Storage/");
1203         int localStorageDirectoryNumberOfFiles = 0;
1204         if (localStorageDirectory.exists()) {
1205             localStorageDirectoryNumberOfFiles = localStorageDirectory.list().length;
1206         }
1207
1208         // Get a count of the number of files in the IndexedDB directory.
1209         File indexedDBDirectory = new File (privateDataDirectoryString + "/app_webview/IndexedDB");
1210         int indexedDBDirectoryNumberOfFiles = 0;
1211         if (indexedDBDirectory.exists()) {
1212             indexedDBDirectoryNumberOfFiles = indexedDBDirectory.list().length;
1213         }
1214
1215         // Enable Clear DOM Storage if there is any.
1216         clearDOMStorageMenuItem.setEnabled(localStorageDirectoryNumberOfFiles > 0 || indexedDBDirectoryNumberOfFiles > 0);
1217
1218         // Enable Clear Form Data is there is any.  This can be removed once the minimum API >= 26.
1219         if (Build.VERSION.SDK_INT < 26) {
1220             // Get the WebView database.
1221             WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
1222
1223             // Enable the clear form data menu item if there is anything to clear.
1224             clearFormDataMenuItem.setEnabled(webViewDatabase.hasFormData());
1225         }
1226
1227         // Enable Clear Data if any of the submenu items are enabled.
1228         clearDataMenuItem.setEnabled(clearCookiesMenuItem.isEnabled() || clearDOMStorageMenuItem.isEnabled() || clearFormDataMenuItem.isEnabled());
1229
1230         // Disable Fanboy's Social Blocking List menu item if Fanboy's Annoyance List is checked.
1231         fanboysSocialBlockingListMenuItem.setEnabled(!fanboysAnnoyanceListMenuItem.isChecked());
1232
1233         // Select the current user agent menu item.  A switch statement cannot be used because the user agents are not compile time constants.
1234         if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[0])) {  // Privacy Browser.
1235             menu.findItem(R.id.user_agent_privacy_browser).setChecked(true);
1236         } else if (currentUserAgent.equals(webViewDefaultUserAgent)) {  // WebView Default.
1237             menu.findItem(R.id.user_agent_webview_default).setChecked(true);
1238         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[2])) {  // Firefox on Android.
1239             menu.findItem(R.id.user_agent_firefox_on_android).setChecked(true);
1240         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[3])) {  // Chrome on Android.
1241             menu.findItem(R.id.user_agent_chrome_on_android).setChecked(true);
1242         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[4])) {  // Safari on iOS.
1243             menu.findItem(R.id.user_agent_safari_on_ios).setChecked(true);
1244         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[5])) {  // Firefox on Linux.
1245             menu.findItem(R.id.user_agent_firefox_on_linux).setChecked(true);
1246         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[6])) {  // Chromium on Linux.
1247             menu.findItem(R.id.user_agent_chromium_on_linux).setChecked(true);
1248         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[7])) {  // Firefox on Windows.
1249             menu.findItem(R.id.user_agent_firefox_on_windows).setChecked(true);
1250         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[8])) {  // Chrome on Windows.
1251             menu.findItem(R.id.user_agent_chrome_on_windows).setChecked(true);
1252         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[9])) {  // Edge on Windows.
1253             menu.findItem(R.id.user_agent_edge_on_windows).setChecked(true);
1254         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[10])) {  // Internet Explorer on Windows.
1255             menu.findItem(R.id.user_agent_internet_explorer_on_windows).setChecked(true);
1256         } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[11])) {  // Safari on macOS.
1257             menu.findItem(R.id.user_agent_safari_on_macos).setChecked(true);
1258         } else {  // Custom user agent.
1259             menu.findItem(R.id.user_agent_custom).setChecked(true);
1260         }
1261
1262         // Instantiate the font size title and the selected font size menu item.
1263         String fontSizeTitle;
1264         MenuItem selectedFontSizeMenuItem;
1265
1266         // Prepare the font size title and current size menu item.
1267         switch (fontSize) {
1268             case 25:
1269                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.twenty_five_percent);
1270                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_twenty_five_percent);
1271                 break;
1272
1273             case 50:
1274                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.fifty_percent);
1275                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_fifty_percent);
1276                 break;
1277
1278             case 75:
1279                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.seventy_five_percent);
1280                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_seventy_five_percent);
1281                 break;
1282
1283             case 100:
1284                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.one_hundred_percent);
1285                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_one_hundred_percent);
1286                 break;
1287
1288             case 125:
1289                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.one_hundred_twenty_five_percent);
1290                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_one_hundred_twenty_five_percent);
1291                 break;
1292
1293             case 150:
1294                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.one_hundred_fifty_percent);
1295                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_one_hundred_fifty_percent);
1296                 break;
1297
1298             case 175:
1299                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.one_hundred_seventy_five_percent);
1300                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_one_hundred_seventy_five_percent);
1301                 break;
1302
1303             case 200:
1304                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.two_hundred_percent);
1305                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_two_hundred_percent);
1306                 break;
1307
1308             default:
1309                 fontSizeTitle = getString(R.string.font_size) + " - " + getString(R.string.one_hundred_percent);
1310                 selectedFontSizeMenuItem = menu.findItem(R.id.font_size_one_hundred_percent);
1311                 break;
1312         }
1313
1314         // Set the font size title and select the current size menu item.
1315         fontSizeMenuItem.setTitle(fontSizeTitle);
1316         selectedFontSizeMenuItem.setChecked(true);
1317
1318         // Run all the other default commands.
1319         super.onPrepareOptionsMenu(menu);
1320
1321         // Display the menu.
1322         return true;
1323     }
1324
1325     @Override
1326     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
1327     @SuppressLint("SetJavaScriptEnabled")
1328     public boolean onOptionsItemSelected(MenuItem menuItem) {
1329         // Reenter full screen browsing mode if it was interrupted by the options menu.  <https://redmine.stoutner.com/issues/389>
1330         if (inFullScreenBrowsingMode) {
1331             // Remove the translucent status flag.  This is necessary so the root frame layout can fill the entire screen.
1332             getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
1333
1334             FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout);
1335
1336             /* Hide the system bars.
1337              * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
1338              * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
1339              * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
1340              * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
1341              */
1342             rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
1343                     View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
1344         }
1345
1346         // Get the selected menu item ID.
1347         int menuItemId = menuItem.getItemId();
1348
1349         // Get a handle for the shared preferences.
1350         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
1351
1352         // Get a handle for the cookie manager.
1353         CookieManager cookieManager = CookieManager.getInstance();
1354
1355         // Run the commands that correlate to the selected menu item.
1356         switch (menuItemId) {
1357             case R.id.toggle_javascript:
1358                 // Toggle the JavaScript status.
1359                 currentWebView.getSettings().setJavaScriptEnabled(!currentWebView.getSettings().getJavaScriptEnabled());
1360
1361                 // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
1362                 updatePrivacyIcons(true);
1363
1364                 // Display a `Snackbar`.
1365                 if (currentWebView.getSettings().getJavaScriptEnabled()) {  // JavaScrip is enabled.
1366                     Snackbar.make(findViewById(R.id.webviewpager), R.string.javascript_enabled, Snackbar.LENGTH_SHORT).show();
1367                 } else if (cookieManager.acceptCookie()) {  // JavaScript is disabled, but first-party cookies are enabled.
1368                     Snackbar.make(findViewById(R.id.webviewpager), R.string.javascript_disabled, Snackbar.LENGTH_SHORT).show();
1369                 } else {  // Privacy mode.
1370                     Snackbar.make(findViewById(R.id.webviewpager), R.string.privacy_mode, Snackbar.LENGTH_SHORT).show();
1371                 }
1372
1373                 // Reload the current WebView.
1374                 currentWebView.reload();
1375                 return true;
1376
1377             case R.id.add_or_edit_domain:
1378                 if (currentWebView.getDomainSettingsApplied()) {  // Edit the current domain settings.
1379                     // Reapply the domain settings on returning to `MainWebViewActivity`.
1380                     reapplyDomainSettingsOnRestart = true;
1381
1382                     // TODO.  Move these to `putExtra`.  The certificate can be stored as strings.
1383                     // Store the current SSL certificate and IP addresses in the domains activity.
1384                     DomainsActivity.currentSslCertificate = currentWebView.getCertificate();
1385                     DomainsActivity.currentIpAddresses = currentWebView.getCurrentIpAddresses();
1386
1387                     // Create an intent to launch the domains activity.
1388                     Intent domainsIntent = new Intent(this, DomainsActivity.class);
1389
1390                     // Add the extra information to the intent.
1391                     domainsIntent.putExtra("load_domain", currentWebView.getDomainSettingsDatabaseId());
1392                     domainsIntent.putExtra("close_on_back", true);
1393                     domainsIntent.putExtra("current_url", currentWebView.getUrl());
1394
1395                     // Make it so.
1396                     startActivity(domainsIntent);
1397                 } else {  // Add a new domain.
1398                     // Apply the new domain settings on returning to `MainWebViewActivity`.
1399                     reapplyDomainSettingsOnRestart = true;
1400
1401                     // Get the current domain
1402                     Uri currentUri = Uri.parse(currentWebView.getUrl());
1403                     String currentDomain = currentUri.getHost();
1404
1405                     // Initialize the database handler.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
1406                     DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
1407
1408                     // Create the domain and store the database ID.
1409                     int newDomainDatabaseId = domainsDatabaseHelper.addDomain(currentDomain);
1410
1411                     // TODO.  Move these to `putExtra`.  The certificate can be stored as strings.
1412                     // Store the current SSL certificate and IP addresses in the domains activity.
1413                     DomainsActivity.currentSslCertificate = currentWebView.getCertificate();
1414                     DomainsActivity.currentIpAddresses = currentWebView.getCurrentIpAddresses();
1415
1416                     // Create an intent to launch the domains activity.
1417                     Intent domainsIntent = new Intent(this, DomainsActivity.class);
1418
1419                     // Add the extra information to the intent.
1420                     domainsIntent.putExtra("load_domain", newDomainDatabaseId);
1421                     domainsIntent.putExtra("close_on_back", true);
1422                     domainsIntent.putExtra("current_url", currentWebView.getUrl());
1423
1424                     // Make it so.
1425                     startActivity(domainsIntent);
1426                 }
1427                 return true;
1428
1429             case R.id.toggle_first_party_cookies:
1430                 // Switch the first-party cookie status.
1431                 cookieManager.setAcceptCookie(!cookieManager.acceptCookie());
1432
1433                 // Store the first-party cookie status.
1434                 currentWebView.setAcceptFirstPartyCookies(cookieManager.acceptCookie());
1435
1436                 // Update the menu checkbox.
1437                 menuItem.setChecked(cookieManager.acceptCookie());
1438
1439                 // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
1440                 updatePrivacyIcons(true);
1441
1442                 // Display a snackbar.
1443                 if (cookieManager.acceptCookie()) {  // First-party cookies are enabled.
1444                     Snackbar.make(findViewById(R.id.webviewpager), R.string.first_party_cookies_enabled, Snackbar.LENGTH_SHORT).show();
1445                 } else if (currentWebView.getSettings().getJavaScriptEnabled()) {  // JavaScript is still enabled.
1446                     Snackbar.make(findViewById(R.id.webviewpager), R.string.first_party_cookies_disabled, Snackbar.LENGTH_SHORT).show();
1447                 } else {  // Privacy mode.
1448                     Snackbar.make(findViewById(R.id.webviewpager), R.string.privacy_mode, Snackbar.LENGTH_SHORT).show();
1449                 }
1450
1451                 // Reload the current WebView.
1452                 currentWebView.reload();
1453                 return true;
1454
1455             case R.id.toggle_third_party_cookies:
1456                 if (Build.VERSION.SDK_INT >= 21) {
1457                     // Switch the status of thirdPartyCookiesEnabled.
1458                     cookieManager.setAcceptThirdPartyCookies(currentWebView, !cookieManager.acceptThirdPartyCookies(currentWebView));
1459
1460                     // Update the menu checkbox.
1461                     menuItem.setChecked(cookieManager.acceptThirdPartyCookies(currentWebView));
1462
1463                     // Display a snackbar.
1464                     if (cookieManager.acceptThirdPartyCookies(currentWebView)) {
1465                         Snackbar.make(findViewById(R.id.webviewpager), R.string.third_party_cookies_enabled, Snackbar.LENGTH_SHORT).show();
1466                     } else {
1467                         Snackbar.make(findViewById(R.id.webviewpager), R.string.third_party_cookies_disabled, Snackbar.LENGTH_SHORT).show();
1468                     }
1469
1470                     // Reload the current WebView.
1471                     currentWebView.reload();
1472                 } // Else do nothing because SDK < 21.
1473                 return true;
1474
1475             case R.id.toggle_dom_storage:
1476                 // Toggle the status of domStorageEnabled.
1477                 currentWebView.getSettings().setDomStorageEnabled(!currentWebView.getSettings().getDomStorageEnabled());
1478
1479                 // Update the menu checkbox.
1480                 menuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
1481
1482                 // Update the privacy icon.  `true` refreshes the app bar icons.
1483                 updatePrivacyIcons(true);
1484
1485                 // Display a snackbar.
1486                 if (currentWebView.getSettings().getDomStorageEnabled()) {
1487                     Snackbar.make(findViewById(R.id.webviewpager), R.string.dom_storage_enabled, Snackbar.LENGTH_SHORT).show();
1488                 } else {
1489                     Snackbar.make(findViewById(R.id.webviewpager), R.string.dom_storage_disabled, Snackbar.LENGTH_SHORT).show();
1490                 }
1491
1492                 // Reload the current WebView.
1493                 currentWebView.reload();
1494                 return true;
1495
1496             // Form data can be removed once the minimum API >= 26.
1497             case R.id.toggle_save_form_data:
1498                 // Switch the status of saveFormDataEnabled.
1499                 currentWebView.getSettings().setSaveFormData(!currentWebView.getSettings().getSaveFormData());
1500
1501                 // Update the menu checkbox.
1502                 menuItem.setChecked(currentWebView.getSettings().getSaveFormData());
1503
1504                 // Display a snackbar.
1505                 if (currentWebView.getSettings().getSaveFormData()) {
1506                     Snackbar.make(findViewById(R.id.webviewpager), R.string.form_data_enabled, Snackbar.LENGTH_SHORT).show();
1507                 } else {
1508                     Snackbar.make(findViewById(R.id.webviewpager), R.string.form_data_disabled, Snackbar.LENGTH_SHORT).show();
1509                 }
1510
1511                 // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
1512                 updatePrivacyIcons(true);
1513
1514                 // Reload the current WebView.
1515                 currentWebView.reload();
1516                 return true;
1517
1518             case R.id.clear_cookies:
1519                 Snackbar.make(findViewById(R.id.webviewpager), R.string.cookies_deleted, Snackbar.LENGTH_LONG)
1520                         .setAction(R.string.undo, v -> {
1521                             // Do nothing because everything will be handled by `onDismissed()` below.
1522                         })
1523                         .addCallback(new Snackbar.Callback() {
1524                             @SuppressLint("SwitchIntDef")  // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
1525                             @Override
1526                             public void onDismissed(Snackbar snackbar, int event) {
1527                                 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {  // The snackbar was dismissed without the undo button being pushed.
1528                                     // Delete the cookies, which command varies by SDK.
1529                                     if (Build.VERSION.SDK_INT < 21) {
1530                                         cookieManager.removeAllCookie();
1531                                     } else {
1532                                         cookieManager.removeAllCookies(null);
1533                                     }
1534                                 }
1535                             }
1536                         })
1537                         .show();
1538                 return true;
1539
1540             case R.id.clear_dom_storage:
1541                 Snackbar.make(findViewById(R.id.webviewpager), R.string.dom_storage_deleted, Snackbar.LENGTH_LONG)
1542                         .setAction(R.string.undo, v -> {
1543                             // Do nothing because everything will be handled by `onDismissed()` below.
1544                         })
1545                         .addCallback(new Snackbar.Callback() {
1546                             @SuppressLint("SwitchIntDef")  // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
1547                             @Override
1548                             public void onDismissed(Snackbar snackbar, int event) {
1549                                 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {  // The snackbar was dismissed without the undo button being pushed.
1550                                     // Delete the DOM Storage.
1551                                     WebStorage webStorage = WebStorage.getInstance();
1552                                     webStorage.deleteAllData();
1553
1554                                     // Initialize a handler to manually delete the DOM storage files and directories.
1555                                     Handler deleteDomStorageHandler = new Handler();
1556
1557                                     // Setup a runnable to manually delete the DOM storage files and directories.
1558                                     Runnable deleteDomStorageRunnable = () -> {
1559                                         try {
1560                                             // Get a handle for the runtime.
1561                                             Runtime runtime = Runtime.getRuntime();
1562
1563                                             // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
1564                                             // which links to `/data/data/com.stoutner.privacybrowser.standard`.
1565                                             String privateDataDirectoryString = getApplicationInfo().dataDir;
1566
1567                                             // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
1568                                             Process deleteLocalStorageProcess = runtime.exec(new String[]{"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
1569
1570                                             // Multiple commands must be used because `Runtime.exec()` does not like `*`.
1571                                             Process deleteIndexProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
1572                                             Process deleteQuotaManagerProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
1573                                             Process deleteQuotaManagerJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
1574                                             Process deleteDatabasesProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
1575
1576                                             // Wait for the processes to finish.
1577                                             deleteLocalStorageProcess.waitFor();
1578                                             deleteIndexProcess.waitFor();
1579                                             deleteQuotaManagerProcess.waitFor();
1580                                             deleteQuotaManagerJournalProcess.waitFor();
1581                                             deleteDatabasesProcess.waitFor();
1582                                         } catch (Exception exception) {
1583                                             // Do nothing if an error is thrown.
1584                                         }
1585                                     };
1586
1587                                     // Manually delete the DOM storage files after 200 milliseconds.
1588                                     deleteDomStorageHandler.postDelayed(deleteDomStorageRunnable, 200);
1589                                 }
1590                             }
1591                         })
1592                         .show();
1593                 return true;
1594
1595             // Form data can be remove once the minimum API >= 26.
1596             case R.id.clear_form_data:
1597                 Snackbar.make(findViewById(R.id.webviewpager), R.string.form_data_deleted, Snackbar.LENGTH_LONG)
1598                         .setAction(R.string.undo, v -> {
1599                             // Do nothing because everything will be handled by `onDismissed()` below.
1600                         })
1601                         .addCallback(new Snackbar.Callback() {
1602                             @SuppressLint("SwitchIntDef")  // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
1603                             @Override
1604                             public void onDismissed(Snackbar snackbar, int event) {
1605                                 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {  // The snackbar was dismissed without the undo button being pushed.
1606                                     // Delete the form data.
1607                                     WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(getApplicationContext());
1608                                     mainWebViewDatabase.clearFormData();
1609                                 }
1610                             }
1611                         })
1612                         .show();
1613                 return true;
1614
1615             case R.id.easylist:
1616                 // Toggle the EasyList status.
1617                 currentWebView.enableBlocklist(NestedScrollWebView.EASY_LIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.EASY_LIST));
1618
1619                 // Update the menu checkbox.
1620                 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASY_LIST));
1621
1622                 // Reload the current WebView.
1623                 currentWebView.reload();
1624                 return true;
1625
1626             case R.id.easyprivacy:
1627                 // Toggle the EasyPrivacy status.
1628                 currentWebView.enableBlocklist(NestedScrollWebView.EASY_PRIVACY, !currentWebView.isBlocklistEnabled(NestedScrollWebView.EASY_PRIVACY));
1629
1630                 // Update the menu checkbox.
1631                 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASY_PRIVACY));
1632
1633                 // Reload the current WebView.
1634                 currentWebView.reload();
1635                 return true;
1636
1637             case R.id.fanboys_annoyance_list:
1638                 // Toggle Fanboy's Annoyance List status.
1639                 currentWebView.enableBlocklist(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1640
1641                 // Update the menu checkbox.
1642                 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1643
1644                 // Update the staus of Fanboy's Social Blocking List.
1645                 MenuItem fanboysSocialBlockingListMenuItem = optionsMenu.findItem(R.id.fanboys_social_blocking_list);
1646                 fanboysSocialBlockingListMenuItem.setEnabled(!currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1647
1648                 // Reload the current WebView.
1649                 currentWebView.reload();
1650                 return true;
1651
1652             case R.id.fanboys_social_blocking_list:
1653                 // Toggle Fanboy's Social Blocking List status.
1654                 currentWebView.enableBlocklist(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST));
1655
1656                 // Update the menu checkbox.
1657                 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST));
1658
1659                 // Reload the current WebView.
1660                 currentWebView.reload();
1661                 return true;
1662
1663             case R.id.ultraprivacy:
1664                 // Toggle the UltraPrivacy status.
1665                 currentWebView.enableBlocklist(NestedScrollWebView.ULTRA_PRIVACY, !currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRA_PRIVACY));
1666
1667                 // Update the menu checkbox.
1668                 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRA_PRIVACY));
1669
1670                 // Reload the current WebView.
1671                 currentWebView.reload();
1672                 return true;
1673
1674             case R.id.block_all_third_party_requests:
1675                 //Toggle the third-party requests blocker status.
1676                 currentWebView.enableBlocklist(NestedScrollWebView.THIRD_PARTY_REQUESTS, !currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
1677
1678                 // Update the menu checkbox.
1679                 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
1680
1681                 // Reload the current WebView.
1682                 currentWebView.reload();
1683                 return true;
1684
1685             case R.id.user_agent_privacy_browser:
1686                 // Update the user agent.
1687                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[0]);
1688
1689                 // Reload the current WebView.
1690                 currentWebView.reload();
1691                 return true;
1692
1693             case R.id.user_agent_webview_default:
1694                 // Update the user agent.
1695                 currentWebView.getSettings().setUserAgentString("");
1696
1697                 // Reload the current WebView.
1698                 currentWebView.reload();
1699                 return true;
1700
1701             case R.id.user_agent_firefox_on_android:
1702                 // Update the user agent.
1703                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[2]);
1704
1705                 // Reload the current WebView.
1706                 currentWebView.reload();
1707                 return true;
1708
1709             case R.id.user_agent_chrome_on_android:
1710                 // Update the user agent.
1711                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[3]);
1712
1713                 // Reload the current WebView.
1714                 currentWebView.reload();
1715                 return true;
1716
1717             case R.id.user_agent_safari_on_ios:
1718                 // Update the user agent.
1719                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[4]);
1720
1721                 // Reload the current WebView.
1722                 currentWebView.reload();
1723                 return true;
1724
1725             case R.id.user_agent_firefox_on_linux:
1726                 // Update the user agent.
1727                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[5]);
1728
1729                 // Reload the current WebView.
1730                 currentWebView.reload();
1731                 return true;
1732
1733             case R.id.user_agent_chromium_on_linux:
1734                 // Update the user agent.
1735                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[6]);
1736
1737                 // Reload the current WebView.
1738                 currentWebView.reload();
1739                 return true;
1740
1741             case R.id.user_agent_firefox_on_windows:
1742                 // Update the user agent.
1743                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[7]);
1744
1745                 // Reload the current WebView.
1746                 currentWebView.reload();
1747                 return true;
1748
1749             case R.id.user_agent_chrome_on_windows:
1750                 // Update the user agent.
1751                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[8]);
1752
1753                 // Reload the current WebView.
1754                 currentWebView.reload();
1755                 return true;
1756
1757             case R.id.user_agent_edge_on_windows:
1758                 // Update the user agent.
1759                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[9]);
1760
1761                 // Reload the current WebView.
1762                 currentWebView.reload();
1763                 return true;
1764
1765             case R.id.user_agent_internet_explorer_on_windows:
1766                 // Update the user agent.
1767                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[10]);
1768
1769                 // Reload the current WebView.
1770                 currentWebView.reload();
1771                 return true;
1772
1773             case R.id.user_agent_safari_on_macos:
1774                 // Update the user agent.
1775                 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[11]);
1776
1777                 // Reload the current WebView.
1778                 currentWebView.reload();
1779                 return true;
1780
1781             case R.id.user_agent_custom:
1782                 // Update the user agent.
1783                 currentWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
1784
1785                 // Reload the current WebView.
1786                 currentWebView.reload();
1787                 return true;
1788
1789             case R.id.font_size_twenty_five_percent:
1790                 currentWebView.getSettings().setTextZoom(25);
1791                 return true;
1792
1793             case R.id.font_size_fifty_percent:
1794                 currentWebView.getSettings().setTextZoom(50);
1795                 return true;
1796
1797             case R.id.font_size_seventy_five_percent:
1798                 currentWebView.getSettings().setTextZoom(75);
1799                 return true;
1800
1801             case R.id.font_size_one_hundred_percent:
1802                 currentWebView.getSettings().setTextZoom(100);
1803                 return true;
1804
1805             case R.id.font_size_one_hundred_twenty_five_percent:
1806                 currentWebView.getSettings().setTextZoom(125);
1807                 return true;
1808
1809             case R.id.font_size_one_hundred_fifty_percent:
1810                 currentWebView.getSettings().setTextZoom(150);
1811                 return true;
1812
1813             case R.id.font_size_one_hundred_seventy_five_percent:
1814                 currentWebView.getSettings().setTextZoom(175);
1815                 return true;
1816
1817             case R.id.font_size_two_hundred_percent:
1818                 currentWebView.getSettings().setTextZoom(200);
1819                 return true;
1820
1821             case R.id.swipe_to_refresh:
1822                 // Toggle the stored status of swipe to refresh.
1823                 currentWebView.setSwipeToRefresh(!currentWebView.getSwipeToRefresh());
1824
1825                 // Get a handle for the swipe refresh layout.
1826                 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
1827
1828                 // Update the swipe refresh layout.
1829                 if (currentWebView.getSwipeToRefresh()) {  // Swipe to refresh is enabled.
1830                     if (Build.VERSION.SDK_INT >= 23) {  // For API >= 23, the status of the scroll refresh listener is continuously updated by the on scroll change listener.
1831                         // Only enable the swipe refresh layout if the WebView is scrolled to the top.
1832                         swipeRefreshLayout.setEnabled(currentWebView.getY() == 0);
1833                     } else {  // For API < 23, the swipe refresh layout is always enabled.
1834                         // Enable the swipe refresh layout.
1835                         swipeRefreshLayout.setEnabled(true);
1836                     }
1837                 } else {  // Swipe to refresh is disabled.
1838                     // Disable the swipe refresh layout.
1839                     swipeRefreshLayout.setEnabled(false);
1840                 }
1841                 return true;
1842
1843             case R.id.display_images:
1844                 if (currentWebView.getSettings().getLoadsImagesAutomatically()) {  // Images are currently loaded automatically.
1845                     // Disable loading of images.
1846                     currentWebView.getSettings().setLoadsImagesAutomatically(false);
1847
1848                     // Reload the website to remove existing images.
1849                     currentWebView.reload();
1850                 } else {  // Images are not currently loaded automatically.
1851                     // Enable loading of images.  Missing images will be loaded without the need for a reload.
1852                     currentWebView.getSettings().setLoadsImagesAutomatically(true);
1853                 }
1854                 return true;
1855
1856             case R.id.night_mode:
1857                 // Toggle night mode.
1858                 currentWebView.setNightMode(!currentWebView.getNightMode());
1859
1860                 // Enable or disable JavaScript according to night mode, the global preference, and any domain settings.
1861                 if (currentWebView.getNightMode()) {  // Night mode is enabled, which requires JavaScript.
1862                     // Enable JavaScript.
1863                     currentWebView.getSettings().setJavaScriptEnabled(true);
1864                 } else if (currentWebView.getDomainSettingsApplied()) {  // Night mode is disabled and domain settings are applied.  Set JavaScript according to the domain settings.
1865                     // Apply the JavaScript preference that was stored the last time domain settings were loaded.
1866                     currentWebView.getSettings().setJavaScriptEnabled(currentWebView.getDomainSettingsJavaScriptEnabled());
1867                 } else {  // Night mode is disabled and domain settings are not applied.  Set JavaScript according to the global preference.
1868                     // Apply the JavaScript preference.
1869                     currentWebView.getSettings().setJavaScriptEnabled(sharedPreferences.getBoolean("javascript", false));
1870                 }
1871
1872                 // Update the privacy icons.
1873                 updatePrivacyIcons(false);
1874
1875                 // Reload the website.
1876                 currentWebView.reload();
1877                 return true;
1878
1879             case R.id.find_on_page:
1880                 // Get a handle for the views.
1881                 Toolbar toolbar = findViewById(R.id.toolbar);
1882                 LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
1883                 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
1884
1885                 // Hide the toolbar.
1886                 toolbar.setVisibility(View.GONE);
1887
1888                 // Show the find on page linear layout.
1889                 findOnPageLinearLayout.setVisibility(View.VISIBLE);
1890
1891                 // Display the keyboard.  The app must wait 200 ms before running the command to work around a bug in Android.
1892                 // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working
1893                 findOnPageEditText.postDelayed(() -> {
1894                     // Set the focus on `findOnPageEditText`.
1895                     findOnPageEditText.requestFocus();
1896
1897                     // Get a handle for the input method manager.
1898                     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
1899
1900                     // Remove the lint warning below that the input method manager might be null.
1901                     assert inputMethodManager != null;
1902
1903                     // Display the keyboard.  `0` sets no input flags.
1904                     inputMethodManager.showSoftInput(findOnPageEditText, 0);
1905                 }, 200);
1906                 return true;
1907
1908             case R.id.view_source:
1909                 // Create an intent to launch the view source activity.
1910                 Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class);
1911
1912                 // Add the variables to the intent.
1913                 viewSourceIntent.putExtra("user_agent", currentWebView.getSettings().getUserAgentString());
1914                 viewSourceIntent.putExtra("current_url", currentWebView.getUrl());
1915
1916                 // Make it so.
1917                 startActivity(viewSourceIntent);
1918                 return true;
1919
1920             case R.id.share_url:
1921                 // Setup the share string.
1922                 String shareString = currentWebView.getTitle() + " – " + currentWebView.getUrl();
1923
1924                 // Create the share intent.
1925                 Intent shareIntent = new Intent(Intent.ACTION_SEND);
1926                 shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
1927                 shareIntent.setType("text/plain");
1928
1929                 // Make it so.
1930                 startActivity(Intent.createChooser(shareIntent, getString(R.string.share_url)));
1931                 return true;
1932
1933             case R.id.print:
1934                 // Get a print manager instance.
1935                 PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
1936
1937                 // Remove the lint error below that print manager might be null.
1938                 assert printManager != null;
1939
1940                 // Create a print document adapter from the current WebView.
1941                 PrintDocumentAdapter printDocumentAdapter = currentWebView.createPrintDocumentAdapter();
1942
1943                 // Print the document.
1944                 printManager.print(getString(R.string.privacy_browser_web_page), printDocumentAdapter, null);
1945                 return true;
1946
1947             case R.id.open_with_app:
1948                 openWithApp(currentWebView.getUrl());
1949                 return true;
1950
1951             case R.id.open_with_browser:
1952                 openWithBrowser(currentWebView.getUrl());
1953                 return true;
1954
1955             case R.id.add_to_homescreen:
1956                 // Instantiate the create home screen shortcut dialog.
1957                 DialogFragment createHomeScreenShortcutDialogFragment = CreateHomeScreenShortcutDialog.createDialog(currentWebView.getTitle(), currentWebView.getUrl(),
1958                         currentWebView.getFavoriteOrDefaultIcon());
1959
1960                 // Show the create home screen shortcut dialog.
1961                 createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut));
1962                 return true;
1963
1964             case R.id.proxy_through_orbot:
1965                 // Toggle the proxy through Orbot variable.
1966                 proxyThroughOrbot = !proxyThroughOrbot;
1967
1968                 // Apply the proxy through Orbot settings.
1969                 applyProxyThroughOrbot(true);
1970                 return true;
1971
1972             case R.id.refresh:
1973                 if (menuItem.getTitle().equals(getString(R.string.refresh))) {  // The refresh button was pushed.
1974                     // Reload the current WebView.
1975                     currentWebView.reload();
1976                 } else {  // The stop button was pushed.
1977                     // Stop the loading of the WebView.
1978                     currentWebView.stopLoading();
1979                 }
1980                 return true;
1981
1982             case R.id.ad_consent:
1983                 // Display the ad consent dialog.
1984                 DialogFragment adConsentDialogFragment = new AdConsentDialog();
1985                 adConsentDialogFragment.show(getSupportFragmentManager(), getString(R.string.ad_consent));
1986                 return true;
1987
1988             default:
1989                 // Don't consume the event.
1990                 return super.onOptionsItemSelected(menuItem);
1991         }
1992     }
1993
1994     // removeAllCookies is deprecated, but it is required for API < 21.
1995     @Override
1996     public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
1997         // Get the menu item ID.
1998         int menuItemId = menuItem.getItemId();
1999
2000         // Get a handle for the shared preferences.
2001         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
2002
2003         // Run the commands that correspond to the selected menu item.
2004         switch (menuItemId) {
2005             case R.id.close_tab:
2006                 // Get a handle for the tab layout and the view pager.
2007                 TabLayout tabLayout = findViewById(R.id.tablayout);
2008                 ViewPager webViewPager = findViewById(R.id.webviewpager);
2009
2010                 // Get the current tab number.
2011                 int currentTabNumber = tabLayout.getSelectedTabPosition();
2012
2013                 // Delete the current tab.
2014                 tabLayout.removeTabAt(currentTabNumber);
2015
2016                 // Delete the current page.  If the selected page number did not change during the delete, it will return true, meaning that the current WebView must be reset.
2017                 if (webViewPagerAdapter.deletePage(currentTabNumber, webViewPager)) {
2018                     setCurrentWebView(currentTabNumber);
2019                 }
2020                 break;
2021
2022             case R.id.clear_and_exit:
2023                 // Close the bookmarks cursor and database.
2024                 bookmarksCursor.close();
2025                 bookmarksDatabaseHelper.close();
2026
2027                 // Get the status of the clear everything preference.
2028                 boolean clearEverything = sharedPreferences.getBoolean("clear_everything", true);
2029
2030                 // Get a handle for the runtime.
2031                 Runtime runtime = Runtime.getRuntime();
2032
2033                 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
2034                 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
2035                 String privateDataDirectoryString = getApplicationInfo().dataDir;
2036
2037                 // Clear cookies.
2038                 if (clearEverything || sharedPreferences.getBoolean("clear_cookies", true)) {
2039                     // The command to remove cookies changed slightly in API 21.
2040                     if (Build.VERSION.SDK_INT >= 21) {
2041                         CookieManager.getInstance().removeAllCookies(null);
2042                     } else {
2043                         CookieManager.getInstance().removeAllCookie();
2044                     }
2045
2046                     // Manually delete the cookies database, as `CookieManager` sometimes will not flush its changes to disk before `System.exit(0)` is run.
2047                     try {
2048                         // Two commands must be used because `Runtime.exec()` does not like `*`.
2049                         Process deleteCookiesProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies");
2050                         Process deleteCookiesJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies-journal");
2051
2052                         // Wait until the processes have finished.
2053                         deleteCookiesProcess.waitFor();
2054                         deleteCookiesJournalProcess.waitFor();
2055                     } catch (Exception exception) {
2056                         // Do nothing if an error is thrown.
2057                     }
2058                 }
2059
2060                 // Clear DOM storage.
2061                 if (clearEverything || sharedPreferences.getBoolean("clear_dom_storage", true)) {
2062                     // Ask `WebStorage` to clear the DOM storage.
2063                     WebStorage webStorage = WebStorage.getInstance();
2064                     webStorage.deleteAllData();
2065
2066                     // Manually delete the DOM storage files and directories, as `WebStorage` sometimes will not flush its changes to disk before `System.exit(0)` is run.
2067                     try {
2068                         // A `String[]` must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
2069                         Process deleteLocalStorageProcess = runtime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
2070
2071                         // Multiple commands must be used because `Runtime.exec()` does not like `*`.
2072                         Process deleteIndexProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
2073                         Process deleteQuotaManagerProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
2074                         Process deleteQuotaManagerJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
2075                         Process deleteDatabaseProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
2076
2077                         // Wait until the processes have finished.
2078                         deleteLocalStorageProcess.waitFor();
2079                         deleteIndexProcess.waitFor();
2080                         deleteQuotaManagerProcess.waitFor();
2081                         deleteQuotaManagerJournalProcess.waitFor();
2082                         deleteDatabaseProcess.waitFor();
2083                     } catch (Exception exception) {
2084                         // Do nothing if an error is thrown.
2085                     }
2086                 }
2087
2088                 // Clear form data if the API < 26.
2089                 if ((Build.VERSION.SDK_INT < 26) && (clearEverything || sharedPreferences.getBoolean("clear_form_data", true))) {
2090                     WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
2091                     webViewDatabase.clearFormData();
2092
2093                     // Manually delete the form data database, as `WebViewDatabase` sometimes will not flush its changes to disk before `System.exit(0)` is run.
2094                     try {
2095                         // A string array must be used because the database contains a space and `Runtime.exec` will not otherwise escape the string correctly.
2096                         Process deleteWebDataProcess = runtime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data"});
2097                         Process deleteWebDataJournalProcess = runtime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data-journal"});
2098
2099                         // Wait until the processes have finished.
2100                         deleteWebDataProcess.waitFor();
2101                         deleteWebDataJournalProcess.waitFor();
2102                     } catch (Exception exception) {
2103                         // Do nothing if an error is thrown.
2104                     }
2105                 }
2106
2107                 // Clear the cache.
2108                 if (clearEverything || sharedPreferences.getBoolean("clear_cache", true)) {
2109                     // Clear the cache from each WebView.
2110                     for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
2111                         // Get the WebView tab fragment.
2112                         WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
2113
2114                         // Get the fragment view.
2115                         View fragmentView = webViewTabFragment.getView();
2116
2117                         // Only clear the cache if the WebView exists.
2118                         if (fragmentView != null) {
2119                             // Get the nested scroll WebView from the tab fragment.
2120                             NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
2121
2122                             // Clear the cache for this WebView.
2123                             nestedScrollWebView.clearCache(true);
2124                         }
2125                     }
2126
2127                     // Manually delete the cache directories.
2128                     try {
2129                         // Delete the main cache directory.
2130                         Process deleteCacheProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/cache");
2131
2132                         // Delete the secondary `Service Worker` cache directory.
2133                         // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
2134                         Process deleteServiceWorkerProcess = runtime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Service Worker/"});
2135
2136                         // Wait until the processes have finished.
2137                         deleteCacheProcess.waitFor();
2138                         deleteServiceWorkerProcess.waitFor();
2139                     } catch (Exception exception) {
2140                         // Do nothing if an error is thrown.
2141                     }
2142                 }
2143
2144                 // Wipe out each WebView.
2145                 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
2146                     // Get the WebView tab fragment.
2147                     WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
2148
2149                     // Get the fragment view.
2150                     View fragmentView = webViewTabFragment.getView();
2151
2152                     // Only wipe out the WebView if it exists.
2153                     if (fragmentView != null) {
2154                         // Get the nested scroll WebView from the tab fragment.
2155                         NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
2156
2157                         // Clear SSL certificate preferences for this WebView.
2158                         nestedScrollWebView.clearSslPreferences();
2159
2160                         // Clear the back/forward history for this WebView.
2161                         nestedScrollWebView.clearHistory();
2162
2163                         // Destroy the internal state of `mainWebView`.
2164                         nestedScrollWebView.destroy();
2165                     }
2166                 }
2167
2168                 // Clear the custom headers.
2169                 customHeaders.clear();
2170
2171                 // Manually delete the `app_webview` folder, which contains the cookies, DOM storage, form data, and `Service Worker` cache.
2172                 // See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`.
2173                 if (clearEverything) {
2174                     try {
2175                         // Delete the folder.
2176                         Process deleteAppWebviewProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview");
2177
2178                         // Wait until the process has finished.
2179                         deleteAppWebviewProcess.waitFor();
2180                     } catch (Exception exception) {
2181                         // Do nothing if an error is thrown.
2182                     }
2183                 }
2184
2185                 // Close Privacy Browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
2186                 if (Build.VERSION.SDK_INT >= 21) {
2187                     finishAndRemoveTask();
2188                 } else {
2189                     finish();
2190                 }
2191
2192                 // Remove the terminated program from RAM.  The status code is `0`.
2193                 System.exit(0);
2194                 break;
2195
2196             case R.id.home:
2197                 // Select the homepage based on the proxy through Orbot status.
2198                 if (proxyThroughOrbot) {
2199                     // Load the Tor homepage.
2200                     loadUrl(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
2201                 } else {
2202                     // Load the normal homepage.
2203                     loadUrl(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
2204                 }
2205                 break;
2206
2207             case R.id.back:
2208                 if (currentWebView.canGoBack()) {
2209                     // Reset the current domain name so that navigation works if third-party requests are blocked.
2210                     currentWebView.resetCurrentDomainName();
2211
2212                     // Set navigating history so that the domain settings are applied when the new URL is loaded.
2213                     currentWebView.setNavigatingHistory(true);
2214
2215                     // Load the previous website in the history.
2216                     currentWebView.goBack();
2217                 }
2218                 break;
2219
2220             case R.id.forward:
2221                 if (currentWebView.canGoForward()) {
2222                     // Reset the current domain name so that navigation works if third-party requests are blocked.
2223                     currentWebView.resetCurrentDomainName();
2224
2225                     // Set navigating history so that the domain settings are applied when the new URL is loaded.
2226                     currentWebView.setNavigatingHistory(true);
2227
2228                     // Load the next website in the history.
2229                     currentWebView.goForward();
2230                 }
2231                 break;
2232
2233             case R.id.history:
2234                 // Get the `WebBackForwardList`.
2235                 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
2236
2237                 // Show the URL history dialog and name this instance `R.string.history`.
2238                 DialogFragment urlHistoryDialogFragment = UrlHistoryDialog.loadBackForwardList(this, webBackForwardList);
2239                 urlHistoryDialogFragment.show(getSupportFragmentManager(), getString(R.string.history));
2240                 break;
2241
2242             case R.id.requests:
2243                 // Populate the resource requests.
2244                 RequestsActivity.resourceRequests = currentWebView.getResourceRequests();
2245
2246                 // Create an intent to launch the Requests activity.
2247                 Intent requestsIntent = new Intent(this, RequestsActivity.class);
2248
2249                 // Add the block third-party requests status to the intent.
2250                 requestsIntent.putExtra("block_all_third_party_requests", currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
2251
2252                 // Make it so.
2253                 startActivity(requestsIntent);
2254                 break;
2255
2256             case R.id.downloads:
2257                 // Launch the system Download Manager.
2258                 Intent downloadManagerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2259
2260                 // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
2261                 downloadManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2262
2263                 startActivity(downloadManagerIntent);
2264                 break;
2265
2266             case R.id.domains:
2267                 // Set the flag to reapply the domain settings on restart when returning from Domain Settings.
2268                 reapplyDomainSettingsOnRestart = true;
2269
2270                 // TODO.  Move these to `putExtra`.  The certificate can be stored as strings.
2271                 // Store the current SSL certificate and IP addresses in the domains activity.
2272                 DomainsActivity.currentSslCertificate = currentWebView.getCertificate();
2273                 DomainsActivity.currentIpAddresses = currentWebView.getCurrentIpAddresses();
2274
2275                 // Launch the domains activity.
2276                 Intent domainsIntent = new Intent(this, DomainsActivity.class);
2277
2278                 // Add the extra information to the intent.
2279                 domainsIntent.putExtra("current_url", currentWebView.getUrl());
2280
2281                 // Make it so.
2282                 startActivity(domainsIntent);
2283                 break;
2284
2285             case R.id.settings:
2286                 // Set the flag to reapply app settings on restart when returning from Settings.
2287                 reapplyAppSettingsOnRestart = true;
2288
2289                 // Set the flag to reapply the domain settings on restart when returning from Settings.
2290                 reapplyDomainSettingsOnRestart = true;
2291
2292                 // Launch the settings activity.
2293                 Intent settingsIntent = new Intent(this, SettingsActivity.class);
2294                 startActivity(settingsIntent);
2295                 break;
2296
2297             case R.id.import_export:
2298                 // Launch the import/export activity.
2299                 Intent importExportIntent = new Intent (this, ImportExportActivity.class);
2300                 startActivity(importExportIntent);
2301                 break;
2302
2303             case R.id.logcat:
2304                 // Launch the logcat activity.
2305                 Intent logcatIntent = new Intent(this, LogcatActivity.class);
2306                 startActivity(logcatIntent);
2307                 break;
2308
2309             case R.id.guide:
2310                 // Launch `GuideActivity`.
2311                 Intent guideIntent = new Intent(this, GuideActivity.class);
2312                 startActivity(guideIntent);
2313                 break;
2314
2315             case R.id.about:
2316                 // Create an intent to launch the about activity.
2317                 Intent aboutIntent = new Intent(this, AboutActivity.class);
2318
2319                 // Create a string array for the blocklist versions.
2320                 String[] blocklistVersions = new String[] {easyList.get(0).get(0)[0], easyPrivacy.get(0).get(0)[0], fanboysAnnoyanceList.get(0).get(0)[0], fanboysSocialList.get(0).get(0)[0],
2321                         ultraPrivacy.get(0).get(0)[0]};
2322
2323                 // Add the blocklist versions to the intent.
2324                 aboutIntent.putExtra("blocklist_versions", blocklistVersions);
2325
2326                 // Make it so.
2327                 startActivity(aboutIntent);
2328                 break;
2329         }
2330
2331         // Get a handle for the drawer layout.
2332         DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
2333
2334         // Close the navigation drawer.
2335         drawerLayout.closeDrawer(GravityCompat.START);
2336         return true;
2337     }
2338
2339     @Override
2340     public void onPostCreate(Bundle savedInstanceState) {
2341         // Run the default commands.
2342         super.onPostCreate(savedInstanceState);
2343
2344         // Sync the state of the DrawerToggle after the default `onRestoreInstanceState()` has finished.  This creates the navigation drawer icon.
2345         actionBarDrawerToggle.syncState();
2346     }
2347
2348     @Override
2349     public void onConfigurationChanged(Configuration newConfig) {
2350         // Run the default commands.
2351         super.onConfigurationChanged(newConfig);
2352
2353         // Get the status bar pixel size.
2354         int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
2355         int statusBarPixelSize = getResources().getDimensionPixelSize(statusBarResourceId);
2356
2357         // Get the resource density.
2358         float screenDensity = getResources().getDisplayMetrics().density;
2359
2360         // Recalculate the drawer header padding.
2361         drawerHeaderPaddingLeftAndRight = (int) (15 * screenDensity);
2362         drawerHeaderPaddingTop = statusBarPixelSize + (int) (4 * screenDensity);
2363         drawerHeaderPaddingBottom = (int) (8 * screenDensity);
2364
2365         // Reload the ad for the free flavor if not in full screen mode.
2366         if (BuildConfig.FLAVOR.contentEquals("free") && !inFullScreenBrowsingMode) {
2367             // Reload the ad.  The AdView is destroyed and recreated, which changes the ID, every time it is reloaded to handle possible rotations.
2368             AdHelper.loadAd(findViewById(R.id.adview), getApplicationContext(), getString(R.string.ad_unit_id));
2369         }
2370
2371         // `invalidateOptionsMenu` should recalculate the number of action buttons from the menu to display on the app bar, but it doesn't because of the this bug:
2372         // https://code.google.com/p/android/issues/detail?id=20493#c8
2373         // ActivityCompat.invalidateOptionsMenu(this);
2374     }
2375
2376     @Override
2377     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
2378         // Store the hit test result.
2379         final WebView.HitTestResult hitTestResult = currentWebView.getHitTestResult();
2380
2381         // Create the URL strings.
2382         final String imageUrl;
2383         final String linkUrl;
2384
2385         // Get handles for the system managers.
2386         final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
2387         FragmentManager fragmentManager = getSupportFragmentManager();
2388         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
2389
2390         // Remove the lint errors below that the clipboard manager might be null.
2391         assert clipboardManager != null;
2392
2393         // Process the link according to the type.
2394         switch (hitTestResult.getType()) {
2395             // `SRC_ANCHOR_TYPE` is a link.
2396             case WebView.HitTestResult.SRC_ANCHOR_TYPE:
2397                 // Get the target URL.
2398                 linkUrl = hitTestResult.getExtra();
2399
2400                 // Set the target URL as the title of the `ContextMenu`.
2401                 menu.setHeaderTitle(linkUrl);
2402
2403                 // Add a Load URL entry.
2404                 menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2405                     // Add a new tab.
2406                     addTab(null);
2407
2408                     // Load the URL.
2409                     loadUrl(linkUrl);
2410                     return false;
2411                 });
2412
2413                 // Add an Open with App entry.
2414                 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2415                     openWithApp(linkUrl);
2416                     return false;
2417                 });
2418
2419                 // Add an Open with Browser entry.
2420                 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2421                     openWithBrowser(linkUrl);
2422                     return false;
2423                 });
2424
2425                 // Add a Copy URL entry.
2426                 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2427                     // Save the link URL in a `ClipData`.
2428                     ClipData srcAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), linkUrl);
2429
2430                     // Set the `ClipData` as the clipboard's primary clip.
2431                     clipboardManager.setPrimaryClip(srcAnchorTypeClipData);
2432                     return false;
2433                 });
2434
2435                 // Add a Download URL entry.
2436                 menu.add(R.string.download_url).setOnMenuItemClickListener((MenuItem item) -> {
2437                     // Check if the download should be processed by an external app.
2438                     if (sharedPreferences.getBoolean("download_with_external_app", false)) {  // Download with an external app.
2439                         openUrlWithExternalApp(linkUrl);
2440                     } else {  // Download with Android's download manager.
2441                         // Check to see if the storage permission has already been granted.
2442                         if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {  // The storage permission needs to be requested.
2443                             // Store the variables for future use by `onRequestPermissionsResult()`.
2444                             downloadUrl = linkUrl;
2445                             downloadContentDisposition = "none";
2446                             downloadContentLength = -1;
2447
2448                             // Show a dialog if the user has previously denied the permission.
2449                             if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {  // Show a dialog explaining the request first.
2450                                 // Instantiate the download location permission alert dialog and set the download type to DOWNLOAD_FILE.
2451                                 DialogFragment downloadLocationPermissionDialogFragment = DownloadLocationPermissionDialog.downloadType(DownloadLocationPermissionDialog.DOWNLOAD_FILE);
2452
2453                                 // Show the download location permission alert dialog.  The permission will be requested when the the dialog is closed.
2454                                 downloadLocationPermissionDialogFragment.show(fragmentManager, getString(R.string.download_location));
2455                             } else {  // Show the permission request directly.
2456                                 // Request the permission.  The download dialog will be launched by `onRequestPermissionResult()`.
2457                                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_FILE_REQUEST_CODE);
2458                             }
2459                         } else {  // The storage permission has already been granted.
2460                             // Get a handle for the download file alert dialog.
2461                             DialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(linkUrl, "none", -1);
2462
2463                             // Show the download file alert dialog.
2464                             downloadFileDialogFragment.show(fragmentManager, getString(R.string.download));
2465                         }
2466                     }
2467                     return false;
2468                 });
2469
2470                 // Add a Cancel entry, which by default closes the context menu.
2471                 menu.add(R.string.cancel);
2472                 break;
2473
2474             case WebView.HitTestResult.EMAIL_TYPE:
2475                 // Get the target URL.
2476                 linkUrl = hitTestResult.getExtra();
2477
2478                 // Set the target URL as the title of the `ContextMenu`.
2479                 menu.setHeaderTitle(linkUrl);
2480
2481                 // Add a Write Email entry.
2482                 menu.add(R.string.write_email).setOnMenuItemClickListener(item -> {
2483                     // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
2484                     Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
2485
2486                     // Parse the url and set it as the data for the `Intent`.
2487                     emailIntent.setData(Uri.parse("mailto:" + linkUrl));
2488
2489                     // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser.
2490                     emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2491
2492                     // Make it so.
2493                     startActivity(emailIntent);
2494                     return false;
2495                 });
2496
2497                 // Add a Copy Email Address entry.
2498                 menu.add(R.string.copy_email_address).setOnMenuItemClickListener(item -> {
2499                     // Save the email address in a `ClipData`.
2500                     ClipData srcEmailTypeClipData = ClipData.newPlainText(getString(R.string.email_address), linkUrl);
2501
2502                     // Set the `ClipData` as the clipboard's primary clip.
2503                     clipboardManager.setPrimaryClip(srcEmailTypeClipData);
2504                     return false;
2505                 });
2506
2507                 // Add a `Cancel` entry, which by default closes the `ContextMenu`.
2508                 menu.add(R.string.cancel);
2509                 break;
2510
2511             // `IMAGE_TYPE` is an image.
2512             case WebView.HitTestResult.IMAGE_TYPE:
2513                 // Get the image URL.
2514                 imageUrl = hitTestResult.getExtra();
2515
2516                 // Set the image URL as the title of the `ContextMenu`.
2517                 menu.setHeaderTitle(imageUrl);
2518
2519                 // Add a View Image entry.
2520                 menu.add(R.string.view_image).setOnMenuItemClickListener(item -> {
2521                     loadUrl(imageUrl);
2522                     return false;
2523                 });
2524
2525                 // Add a Download Image entry.
2526                 menu.add(R.string.download_image).setOnMenuItemClickListener((MenuItem item) -> {
2527                     // Check if the download should be processed by an external app.
2528                     if (sharedPreferences.getBoolean("download_with_external_app", false)) {  // Download with an external app.
2529                         openUrlWithExternalApp(imageUrl);
2530                     } else {  // Download with Android's download manager.
2531                         // Check to see if the storage permission has already been granted.
2532                         if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {  // The storage permission needs to be requested.
2533                             // Store the image URL for use by `onRequestPermissionResult()`.
2534                             downloadImageUrl = imageUrl;
2535
2536                             // Show a dialog if the user has previously denied the permission.
2537                             if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {  // Show a dialog explaining the request first.
2538                                 // Instantiate the download location permission alert dialog and set the download type to DOWNLOAD_IMAGE.
2539                                 DialogFragment downloadLocationPermissionDialogFragment = DownloadLocationPermissionDialog.downloadType(DownloadLocationPermissionDialog.DOWNLOAD_IMAGE);
2540
2541                                 // Show the download location permission alert dialog.  The permission will be requested when the dialog is closed.
2542                                 downloadLocationPermissionDialogFragment.show(fragmentManager, getString(R.string.download_location));
2543                             } else {  // Show the permission request directly.
2544                                 // Request the permission.  The download dialog will be launched by `onRequestPermissionResult().
2545                                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_IMAGE_REQUEST_CODE);
2546                             }
2547                         } else {  // The storage permission has already been granted.
2548                             // Get a handle for the download image alert dialog.
2549                             DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(imageUrl);
2550
2551                             // Show the download image alert dialog.
2552                             downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
2553                         }
2554                     }
2555                     return false;
2556                 });
2557
2558                 // Add a Copy URL entry.
2559                 menu.add(R.string.copy_url).setOnMenuItemClickListener(item -> {
2560                     // Save the image URL in a `ClipData`.
2561                     ClipData srcImageTypeClipData = ClipData.newPlainText(getString(R.string.url), imageUrl);
2562
2563                     // Set the `ClipData` as the clipboard's primary clip.
2564                     clipboardManager.setPrimaryClip(srcImageTypeClipData);
2565                     return false;
2566                 });
2567
2568                 // Add an Open with App entry.
2569                 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2570                     openWithApp(imageUrl);
2571                     return false;
2572                 });
2573
2574                 // Add an Open with Browser entry.
2575                 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2576                     openWithBrowser(imageUrl);
2577                     return false;
2578                 });
2579
2580                 // Add a `Cancel` entry, which by default closes the `ContextMenu`.
2581                 menu.add(R.string.cancel);
2582                 break;
2583
2584
2585             // `SRC_IMAGE_ANCHOR_TYPE` is an image that is also a link.
2586             case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
2587                 // Get the image URL.
2588                 imageUrl = hitTestResult.getExtra();
2589
2590                 // Set the image URL as the title of the `ContextMenu`.
2591                 menu.setHeaderTitle(imageUrl);
2592
2593                 // Add a `View Image` entry.
2594                 menu.add(R.string.view_image).setOnMenuItemClickListener(item -> {
2595                     loadUrl(imageUrl);
2596                     return false;
2597                 });
2598
2599                 // Add a `Download Image` entry.
2600                 menu.add(R.string.download_image).setOnMenuItemClickListener((MenuItem item) -> {
2601                     // Check if the download should be processed by an external app.
2602                     if (sharedPreferences.getBoolean("download_with_external_app", false)) {  // Download with an external app.
2603                         openUrlWithExternalApp(imageUrl);
2604                     } else {  // Download with Android's download manager.
2605                         // Check to see if the storage permission has already been granted.
2606                         if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {  // The storage permission needs to be requested.
2607                             // Store the image URL for use by `onRequestPermissionResult()`.
2608                             downloadImageUrl = imageUrl;
2609
2610                             // Show a dialog if the user has previously denied the permission.
2611                             if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {  // Show a dialog explaining the request first.
2612                                 // Instantiate the download location permission alert dialog and set the download type to DOWNLOAD_IMAGE.
2613                                 DialogFragment downloadLocationPermissionDialogFragment = DownloadLocationPermissionDialog.downloadType(DownloadLocationPermissionDialog.DOWNLOAD_IMAGE);
2614
2615                                 // Show the download location permission alert dialog.  The permission will be requested when the dialog is closed.
2616                                 downloadLocationPermissionDialogFragment.show(fragmentManager, getString(R.string.download_location));
2617                             } else {  // Show the permission request directly.
2618                                 // Request the permission.  The download dialog will be launched by `onRequestPermissionResult().
2619                                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_IMAGE_REQUEST_CODE);
2620                             }
2621                         } else {  // The storage permission has already been granted.
2622                             // Get a handle for the download image alert dialog.
2623                             DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(imageUrl);
2624
2625                             // Show the download image alert dialog.
2626                             downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
2627                         }
2628                     }
2629                     return false;
2630                 });
2631
2632                 // Add a `Copy URL` entry.
2633                 menu.add(R.string.copy_url).setOnMenuItemClickListener(item -> {
2634                     // Save the image URL in a `ClipData`.
2635                     ClipData srcImageAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), imageUrl);
2636
2637                     // Set the `ClipData` as the clipboard's primary clip.
2638                     clipboardManager.setPrimaryClip(srcImageAnchorTypeClipData);
2639                     return false;
2640                 });
2641
2642                 // Add an Open with App entry.
2643                 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2644                     openWithApp(imageUrl);
2645                     return false;
2646                 });
2647
2648                 // Add an Open with Browser entry.
2649                 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2650                     openWithBrowser(imageUrl);
2651                     return false;
2652                 });
2653
2654                 // Add a `Cancel` entry, which by default closes the `ContextMenu`.
2655                 menu.add(R.string.cancel);
2656                 break;
2657         }
2658     }
2659
2660     @Override
2661     public void onCreateBookmark(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
2662         // Get a handle for the bookmarks list view.
2663         ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
2664
2665         // Get the views from the dialog fragment.
2666         EditText createBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
2667         EditText createBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
2668
2669         // Extract the strings from the edit texts.
2670         String bookmarkNameString = createBookmarkNameEditText.getText().toString();
2671         String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
2672
2673         // Create a favorite icon byte array output stream.
2674         ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
2675
2676         // Convert the favorite icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
2677         favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
2678
2679         // Convert the favorite icon byte array stream to a byte array.
2680         byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
2681
2682         // Display the new bookmark below the current items in the (0 indexed) list.
2683         int newBookmarkDisplayOrder = bookmarksListView.getCount();
2684
2685         // Create the bookmark.
2686         bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentBookmarksFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
2687
2688         // Update the bookmarks cursor with the current contents of this folder.
2689         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2690
2691         // Update the list view.
2692         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2693
2694         // Scroll to the new bookmark.
2695         bookmarksListView.setSelection(newBookmarkDisplayOrder);
2696     }
2697
2698     @Override
2699     public void onCreateBookmarkFolder(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
2700         // Get a handle for the bookmarks list view.
2701         ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
2702
2703         // Get handles for the views in the dialog fragment.
2704         EditText createFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext);
2705         RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
2706         ImageView folderIconImageView = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
2707
2708         // Get new folder name string.
2709         String folderNameString = createFolderNameEditText.getText().toString();
2710
2711         // Create a folder icon bitmap.
2712         Bitmap folderIconBitmap;
2713
2714         // Set the folder icon bitmap according to the dialog.
2715         if (defaultFolderIconRadioButton.isChecked()) {  // Use the default folder icon.
2716             // Get the default folder icon drawable.
2717             Drawable folderIconDrawable = folderIconImageView.getDrawable();
2718
2719             // Convert the folder icon drawable to a bitmap drawable.
2720             BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2721
2722             // Convert the folder icon bitmap drawable to a bitmap.
2723             folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2724         } else {  // Use the WebView favorite icon.
2725             // Copy the favorite icon bitmap to the folder icon bitmap.
2726             folderIconBitmap = favoriteIconBitmap;
2727         }
2728
2729         // Create a folder icon byte array output stream.
2730         ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
2731
2732         // Convert the folder icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
2733         folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
2734
2735         // Convert the folder icon byte array stream to a byte array.
2736         byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
2737
2738         // Move all the bookmarks down one in the display order.
2739         for (int i = 0; i < bookmarksListView.getCount(); i++) {
2740             int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
2741             bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1);
2742         }
2743
2744         // Create the folder, which will be placed at the top of the `ListView`.
2745         bookmarksDatabaseHelper.createFolder(folderNameString, currentBookmarksFolder, folderIconByteArray);
2746
2747         // Update the bookmarks cursor with the current contents of this folder.
2748         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2749
2750         // Update the `ListView`.
2751         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2752
2753         // Scroll to the new folder.
2754         bookmarksListView.setSelection(0);
2755     }
2756
2757     @Override
2758     public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, Bitmap favoriteIconBitmap) {
2759         // Get handles for the views from `dialogFragment`.
2760         EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
2761         EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
2762         RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
2763
2764         // Store the bookmark strings.
2765         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
2766         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
2767
2768         // Update the bookmark.
2769         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
2770             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
2771         } else {  // Update the bookmark using the `WebView` favorite icon.
2772             // Create a favorite icon byte array output stream.
2773             ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
2774
2775             // Convert the favorite icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
2776             favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
2777
2778             // Convert the favorite icon byte array stream to a byte array.
2779             byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
2780
2781             //  Update the bookmark and the favorite icon.
2782             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, newFavoriteIconByteArray);
2783         }
2784
2785         // Update the bookmarks cursor with the current contents of this folder.
2786         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2787
2788         // Update the list view.
2789         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2790     }
2791
2792     @Override
2793     public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId, Bitmap favoriteIconBitmap) {
2794         // Get handles for the views from `dialogFragment`.
2795         EditText editFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
2796         RadioButton currentFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
2797         RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
2798         ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
2799
2800         // Get the new folder name.
2801         String newFolderNameString = editFolderNameEditText.getText().toString();
2802
2803         // Check if the favorite icon has changed.
2804         if (currentFolderIconRadioButton.isChecked()) {  // Only the name has changed.
2805             // Update the name in the database.
2806             bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
2807         } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) {  // Only the icon has changed.
2808             // Create the new folder icon Bitmap.
2809             Bitmap folderIconBitmap;
2810
2811             // Populate the new folder icon bitmap.
2812             if (defaultFolderIconRadioButton.isChecked()) {
2813                 // Get the default folder icon drawable.
2814                 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
2815
2816                 // Convert the folder icon drawable to a bitmap drawable.
2817                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2818
2819                 // Convert the folder icon bitmap drawable to a bitmap.
2820                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2821             } else {  // Use the `WebView` favorite icon.
2822                 // Copy the favorite icon bitmap to the folder icon bitmap.
2823                 folderIconBitmap = favoriteIconBitmap;
2824             }
2825
2826             // Create a folder icon byte array output stream.
2827             ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
2828
2829             // Convert the folder icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
2830             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
2831
2832             // Convert the folder icon byte array stream to a byte array.
2833             byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
2834
2835             // Update the folder icon in the database.
2836             bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
2837         } else {  // The folder icon and the name have changed.
2838             // Get the new folder icon `Bitmap`.
2839             Bitmap folderIconBitmap;
2840             if (defaultFolderIconRadioButton.isChecked()) {
2841                 // Get the default folder icon drawable.
2842                 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
2843
2844                 // Convert the folder icon drawable to a bitmap drawable.
2845                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2846
2847                 // Convert the folder icon bitmap drawable to a bitmap.
2848                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2849             } else {  // Use the `WebView` favorite icon.
2850                 // Copy the favorite icon bitmap to the folder icon bitmap.
2851                 folderIconBitmap = favoriteIconBitmap;
2852             }
2853
2854             // Create a folder icon byte array output stream.
2855             ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
2856
2857             // Convert the folder icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
2858             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
2859
2860             // Convert the folder icon byte array stream to a byte array.
2861             byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
2862
2863             // Update the folder name and icon in the database.
2864             bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
2865         }
2866
2867         // Update the bookmarks cursor with the current contents of this folder.
2868         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2869
2870         // Update the `ListView`.
2871         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2872     }
2873
2874     @Override
2875     public void onCloseDownloadLocationPermissionDialog(int downloadType) {
2876         switch (downloadType) {
2877             case DownloadLocationPermissionDialog.DOWNLOAD_FILE:
2878                 // Request the WRITE_EXTERNAL_STORAGE permission with a file request code.
2879                 ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_FILE_REQUEST_CODE);
2880                 break;
2881
2882             case DownloadLocationPermissionDialog.DOWNLOAD_IMAGE:
2883                 // Request the WRITE_EXTERNAL_STORAGE permission with an image request code.
2884                 ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_IMAGE_REQUEST_CODE);
2885                 break;
2886         }
2887     }
2888
2889     @Override
2890     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
2891         // Get a handle for the fragment manager.
2892         FragmentManager fragmentManager = getSupportFragmentManager();
2893
2894         switch (requestCode) {
2895             case DOWNLOAD_FILE_REQUEST_CODE:
2896                 // Show the download file alert dialog.  When the dialog closes, the correct command will be used based on the permission status.
2897                 DialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(downloadUrl, downloadContentDisposition, downloadContentLength);
2898
2899                 // On API 23, displaying the fragment must be delayed or the app will crash.
2900                 if (Build.VERSION.SDK_INT == 23) {
2901                     new Handler().postDelayed(() -> downloadFileDialogFragment.show(fragmentManager, getString(R.string.download)), 500);
2902                 } else {
2903                     downloadFileDialogFragment.show(fragmentManager, getString(R.string.download));
2904                 }
2905
2906                 // Reset the download variables.
2907                 downloadUrl = "";
2908                 downloadContentDisposition = "";
2909                 downloadContentLength = 0;
2910                 break;
2911
2912             case DOWNLOAD_IMAGE_REQUEST_CODE:
2913                 // Show the download image alert dialog.  When the dialog closes, the correct command will be used based on the permission status.
2914                 DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(downloadImageUrl);
2915
2916                 // On API 23, displaying the fragment must be delayed or the app will crash.
2917                 if (Build.VERSION.SDK_INT == 23) {
2918                     new Handler().postDelayed(() -> downloadImageDialogFragment.show(fragmentManager, getString(R.string.download)), 500);
2919                 } else {
2920                     downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
2921                 }
2922
2923                 // Reset the image URL variable.
2924                 downloadImageUrl = "";
2925                 break;