2 * Copyright © 2015-2021 Soren Stoutner <soren@stoutner.com>.
4 * Download cookie code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
6 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
22 package com.stoutner.privacybrowser.activities;
24 import android.annotation.SuppressLint;
25 import android.app.Activity;
26 import android.app.Dialog;
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.AsyncTask;
49 import android.os.Build;
50 import android.os.Bundle;
51 import android.os.Environment;
52 import android.os.Handler;
53 import android.os.Message;
54 import android.preference.PreferenceManager;
55 import android.print.PrintDocumentAdapter;
56 import android.print.PrintManager;
57 import android.provider.DocumentsContract;
58 import android.provider.OpenableColumns;
59 import android.text.Editable;
60 import android.text.Spanned;
61 import android.text.TextWatcher;
62 import android.text.style.ForegroundColorSpan;
63 import android.util.Patterns;
64 import android.util.TypedValue;
65 import android.view.ContextMenu;
66 import android.view.GestureDetector;
67 import android.view.KeyEvent;
68 import android.view.Menu;
69 import android.view.MenuItem;
70 import android.view.MotionEvent;
71 import android.view.View;
72 import android.view.ViewGroup;
73 import android.view.WindowManager;
74 import android.view.inputmethod.InputMethodManager;
75 import android.webkit.CookieManager;
76 import android.webkit.HttpAuthHandler;
77 import android.webkit.SslErrorHandler;
78 import android.webkit.ValueCallback;
79 import android.webkit.WebBackForwardList;
80 import android.webkit.WebChromeClient;
81 import android.webkit.WebResourceResponse;
82 import android.webkit.WebSettings;
83 import android.webkit.WebStorage;
84 import android.webkit.WebView;
85 import android.webkit.WebViewClient;
86 import android.webkit.WebViewDatabase;
87 import android.widget.ArrayAdapter;
88 import android.widget.CheckBox;
89 import android.widget.CursorAdapter;
90 import android.widget.EditText;
91 import android.widget.FrameLayout;
92 import android.widget.ImageView;
93 import android.widget.LinearLayout;
94 import android.widget.ListView;
95 import android.widget.ProgressBar;
96 import android.widget.RadioButton;
97 import android.widget.RelativeLayout;
98 import android.widget.TextView;
100 import androidx.activity.result.ActivityResultCallback;
101 import androidx.activity.result.ActivityResultLauncher;
102 import androidx.activity.result.contract.ActivityResultContracts;
103 import androidx.annotation.NonNull;
104 import androidx.appcompat.app.ActionBar;
105 import androidx.appcompat.app.ActionBarDrawerToggle;
106 import androidx.appcompat.app.AppCompatActivity;
107 import androidx.appcompat.app.AppCompatDelegate;
108 import androidx.appcompat.widget.Toolbar;
109 import androidx.coordinatorlayout.widget.CoordinatorLayout;
110 import androidx.core.content.res.ResourcesCompat;
111 import androidx.core.view.GravityCompat;
112 import androidx.drawerlayout.widget.DrawerLayout;
113 import androidx.fragment.app.DialogFragment;
114 import androidx.fragment.app.Fragment;
115 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
116 import androidx.viewpager.widget.ViewPager;
117 import androidx.webkit.WebSettingsCompat;
118 import androidx.webkit.WebViewFeature;
120 import com.google.android.material.appbar.AppBarLayout;
121 import com.google.android.material.floatingactionbutton.FloatingActionButton;
122 import com.google.android.material.navigation.NavigationView;
123 import com.google.android.material.snackbar.Snackbar;
124 import com.google.android.material.tabs.TabLayout;
126 import com.stoutner.privacybrowser.R;
127 import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter;
128 import com.stoutner.privacybrowser.asynctasks.GetHostIpAddresses;
129 import com.stoutner.privacybrowser.asynctasks.PopulateBlocklists;
130 import com.stoutner.privacybrowser.asynctasks.PrepareSaveDialog;
131 import com.stoutner.privacybrowser.asynctasks.SaveUrl;
132 import com.stoutner.privacybrowser.asynctasks.SaveWebpageImage;
133 import com.stoutner.privacybrowser.dataclasses.PendingDialog;
134 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
135 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
136 import com.stoutner.privacybrowser.dialogs.CreateHomeScreenShortcutDialog;
137 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
138 import com.stoutner.privacybrowser.dialogs.FontSizeDialog;
139 import com.stoutner.privacybrowser.dialogs.HttpAuthenticationDialog;
140 import com.stoutner.privacybrowser.dialogs.OpenDialog;
141 import com.stoutner.privacybrowser.dialogs.ProxyNotInstalledDialog;
142 import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog;
143 import com.stoutner.privacybrowser.dialogs.SaveDialog;
144 import com.stoutner.privacybrowser.dialogs.SslCertificateErrorDialog;
145 import com.stoutner.privacybrowser.dialogs.UrlHistoryDialog;
146 import com.stoutner.privacybrowser.dialogs.ViewSslCertificateDialog;
147 import com.stoutner.privacybrowser.dialogs.WaitingForProxyDialog;
148 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
149 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
150 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
151 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
152 import com.stoutner.privacybrowser.helpers.ProxyHelper;
153 import com.stoutner.privacybrowser.views.NestedScrollWebView;
155 import java.io.ByteArrayInputStream;
156 import java.io.ByteArrayOutputStream;
158 import java.io.FileInputStream;
159 import java.io.FileOutputStream;
160 import java.io.IOException;
161 import java.io.InputStream;
162 import java.io.OutputStream;
163 import java.io.UnsupportedEncodingException;
165 import java.net.MalformedURLException;
167 import java.net.URLDecoder;
168 import java.net.URLEncoder;
170 import java.text.NumberFormat;
172 import java.util.ArrayList;
173 import java.util.Calendar;
174 import java.util.Date;
175 import java.util.HashMap;
176 import java.util.HashSet;
177 import java.util.List;
178 import java.util.Map;
179 import java.util.Objects;
180 import java.util.Set;
181 import java.util.concurrent.ExecutorService;
182 import java.util.concurrent.Executors;
184 public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener,
185 EditBookmarkFolderDialog.EditBookmarkFolderListener, FontSizeDialog.UpdateFontSizeListener, NavigationView.OnNavigationItemSelectedListener, OpenDialog.OpenListener,
186 PinnedMismatchDialog.PinnedMismatchListener, PopulateBlocklists.PopulateBlocklistsListener, SaveDialog.SaveListener, UrlHistoryDialog.NavigateHistoryListener,
187 WebViewTabFragment.NewTabListener {
189 // The executor service handles background tasks. It is accessed from `ViewSourceActivity`.
190 public static ExecutorService executorService = Executors.newFixedThreadPool(4);
192 // `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`. It is also used in `onCreate()`, `onResume()`, and `applyProxy()`.
193 public static String orbotStatus = "unknown";
195 // The WebView pager adapter is accessed from `HttpAuthenticationDialog`, `PinnedMismatchDialog`, and `SslCertificateErrorDialog`. It is also used in `onCreate()`, `onResume()`, and `addTab()`.
196 public static WebViewPagerAdapter webViewPagerAdapter;
198 // `restartFromBookmarksActivity` is public static so it can be accessed from `BookmarksActivity`. It is also used in `onRestart()`.
199 public static boolean restartFromBookmarksActivity;
201 // Define the public static variables.
202 public static ArrayList<PendingDialog> pendingDialogsArrayList = new ArrayList<>();
204 // `currentBookmarksFolder` is public static so it can be accessed from `BookmarksActivity`. It is also used in `onCreate()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`,
205 // `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
206 public static String currentBookmarksFolder;
208 // The user agent constants are public static so they can be accessed from `SettingsFragment`, `DomainsActivity`, and `DomainSettingsFragment`.
209 public final static int UNRECOGNIZED_USER_AGENT = -1;
210 public final static int SETTINGS_WEBVIEW_DEFAULT_USER_AGENT = 1;
211 public final static int SETTINGS_CUSTOM_USER_AGENT = 12;
212 public final static int DOMAINS_SYSTEM_DEFAULT_USER_AGENT = 0;
213 public final static int DOMAINS_WEBVIEW_DEFAULT_USER_AGENT = 2;
214 public final static int DOMAINS_CUSTOM_USER_AGENT = 13;
216 // Define the start activity for result request codes. The public static entry is accessed from `OpenDialog()`.
217 private final int BROWSE_FILE_UPLOAD_REQUEST_CODE = 0;
218 public final static int BROWSE_OPEN_REQUEST_CODE = 1;
220 // The proxy mode is public static so it can be accessed from `ProxyHelper()`.
221 // It is also used in `onRestart()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `applyAppSettings()`, and `applyProxy()`.
222 // It will be updated in `applyAppSettings()`, but it needs to be initialized here or the first run of `onPrepareOptionsMenu()` crashes.
223 public static String proxyMode = ProxyHelper.NONE;
225 // Define the saved instance state constants.
226 private final String SAVED_STATE_ARRAY_LIST = "saved_state_array_list";
227 private final String SAVED_NESTED_SCROLL_WEBVIEW_STATE_ARRAY_LIST = "saved_nested_scroll_webview_state_array_list";
228 private final String SAVED_TAB_POSITION = "saved_tab_position";
229 private final String PROXY_MODE = "proxy_mode";
231 // Define the saved instance state variables.
232 private ArrayList<Bundle> savedStateArrayList;
233 private ArrayList<Bundle> savedNestedScrollWebViewStateArrayList;
234 private int savedTabPosition;
235 private String savedProxyMode;
237 // Define the class variables.
238 @SuppressWarnings("rawtypes")
239 AsyncTask populateBlocklists;
241 // The current WebView is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`,
242 // `findNextOnPage()`, `closeFindOnPage()`, `loadUrlFromTextBox()`, `onSslMismatchBack()`, `applyProxy()`, and `applyDomainSettings()`.
243 private NestedScrollWebView currentWebView;
245 // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateContextMenu()`, and `loadUrl()`.
246 private final Map<String, String> customHeaders = new HashMap<>();
248 // The search URL is set in `applyAppSettings()` and used in `onNewIntent()`, `loadUrlFromTextBox()`, `initializeApp()`, and `initializeWebView()`.
249 private String searchURL;
251 // The blocklists are populated in `finishedPopulatingBlocklists()` and accessed from `initializeWebView()`.
252 private ArrayList<List<String[]>> easyList;
253 private ArrayList<List<String[]>> easyPrivacy;
254 private ArrayList<List<String[]>> fanboysAnnoyanceList;
255 private ArrayList<List<String[]>> fanboysSocialList;
256 private ArrayList<List<String[]>> ultraList;
257 private ArrayList<List<String[]>> ultraPrivacy;
259 // The action bar drawer toggle is initialized in `onCreate()` and used in `onResume()`.
260 private ActionBarDrawerToggle actionBarDrawerToggle;
262 // The color spans are used in `onCreate()` and `highlightUrlText()`.
263 private ForegroundColorSpan redColorSpan;
264 private ForegroundColorSpan initialGrayColorSpan;
265 private ForegroundColorSpan finalGrayColorSpan;
267 // `bookmarksDatabaseHelper` is used in `onCreate()`, `onDestroy`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`,
268 // and `loadBookmarksFolder()`.
269 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
271 // `bookmarksCursor` is used in `onDestroy()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
272 private Cursor bookmarksCursor;
274 // `bookmarksCursorAdapter` is used in `onCreateBookmark()`, `onCreateBookmarkFolder()` `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
275 private CursorAdapter bookmarksCursorAdapter;
277 // `oldFolderNameString` is used in `onCreate()` and `onSaveEditBookmarkFolder()`.
278 private String oldFolderNameString;
280 // `fileChooserCallback` is used in `onCreate()` and `onActivityResult()`.
281 private ValueCallback<Uri[]> fileChooserCallback;
283 // The default progress view offsets are set in `onCreate()` and used in `initializeWebView()`.
284 private int appBarHeight;
285 private int defaultProgressViewStartOffset;
286 private int defaultProgressViewEndOffset;
288 // The URL sanitizers are set in `applyAppSettings()` and used in `sanitizeUrl()`.
289 private boolean sanitizeGoogleAnalytics;
290 private boolean sanitizeFacebookClickIds;
291 private boolean sanitizeTwitterAmpRedirects;
293 // Declare the class variables
294 private BroadcastReceiver orbotStatusBroadcastReceiver;
295 private String webViewDefaultUserAgent;
296 private boolean incognitoModeEnabled;
297 private boolean fullScreenBrowsingModeEnabled;
298 private boolean inFullScreenBrowsingMode;
299 private boolean downloadWithExternalApp;
300 private boolean hideAppBar;
301 private boolean scrollAppBar;
302 private boolean bottomAppBar;
303 private boolean loadingNewIntent;
304 private boolean reapplyDomainSettingsOnRestart;
305 private boolean reapplyAppSettingsOnRestart;
306 private boolean displayingFullScreenVideo;
307 private boolean waitingForProxy;
309 // Define the class variables.
310 private long lastScrollUpdate = 0;
311 private String saveUrlString = "";
313 // Declare the class views.
314 private FrameLayout rootFrameLayout;
315 private DrawerLayout drawerLayout;
316 private CoordinatorLayout coordinatorLayout;
317 private AppBarLayout appBarLayout;
318 private Toolbar toolbar;
319 private RelativeLayout urlRelativeLayout;
320 private EditText urlEditText;
321 private ActionBar actionBar;
322 private LinearLayout findOnPageLinearLayout;
323 private LinearLayout tabsLinearLayout;
324 private TabLayout tabLayout;
325 private SwipeRefreshLayout swipeRefreshLayout;
326 private ViewPager webViewPager;
327 private FrameLayout fullScreenVideoFrameLayout;
329 // Declare the class menus.
330 private Menu optionsMenu;
332 // Declare the class menu items.
333 private MenuItem navigationBackMenuItem;
334 private MenuItem navigationForwardMenuItem;
335 private MenuItem navigationHistoryMenuItem;
336 private MenuItem navigationRequestsMenuItem;
337 private MenuItem optionsPrivacyMenuItem;
338 private MenuItem optionsRefreshMenuItem;
339 private MenuItem optionsCookiesMenuItem;
340 private MenuItem optionsDomStorageMenuItem;
341 private MenuItem optionsSaveFormDataMenuItem;
342 private MenuItem optionsClearDataMenuItem;
343 private MenuItem optionsClearCookiesMenuItem;
344 private MenuItem optionsClearDomStorageMenuItem;
345 private MenuItem optionsClearFormDataMenuItem;
346 private MenuItem optionsBlocklistsMenuItem;
347 private MenuItem optionsEasyListMenuItem;
348 private MenuItem optionsEasyPrivacyMenuItem;
349 private MenuItem optionsFanboysAnnoyanceListMenuItem;
350 private MenuItem optionsFanboysSocialBlockingListMenuItem;
351 private MenuItem optionsUltraListMenuItem;
352 private MenuItem optionsUltraPrivacyMenuItem;
353 private MenuItem optionsBlockAllThirdPartyRequestsMenuItem;
354 private MenuItem optionsProxyMenuItem;
355 private MenuItem optionsProxyNoneMenuItem;
356 private MenuItem optionsProxyTorMenuItem;
357 private MenuItem optionsProxyI2pMenuItem;
358 private MenuItem optionsProxyCustomMenuItem;
359 private MenuItem optionsUserAgentMenuItem;
360 private MenuItem optionsUserAgentPrivacyBrowserMenuItem;
361 private MenuItem optionsUserAgentWebViewDefaultMenuItem;
362 private MenuItem optionsUserAgentFirefoxOnAndroidMenuItem;
363 private MenuItem optionsUserAgentChromeOnAndroidMenuItem;
364 private MenuItem optionsUserAgentSafariOnIosMenuItem;
365 private MenuItem optionsUserAgentFirefoxOnLinuxMenuItem;
366 private MenuItem optionsUserAgentChromiumOnLinuxMenuItem;
367 private MenuItem optionsUserAgentFirefoxOnWindowsMenuItem;
368 private MenuItem optionsUserAgentChromeOnWindowsMenuItem;
369 private MenuItem optionsUserAgentEdgeOnWindowsMenuItem;
370 private MenuItem optionsUserAgentInternetExplorerOnWindowsMenuItem;
371 private MenuItem optionsUserAgentSafariOnMacosMenuItem;
372 private MenuItem optionsUserAgentCustomMenuItem;
373 private MenuItem optionsSwipeToRefreshMenuItem;
374 private MenuItem optionsWideViewportMenuItem;
375 private MenuItem optionsDisplayImagesMenuItem;
376 private MenuItem optionsDarkWebViewMenuItem;
377 private MenuItem optionsFontSizeMenuItem;
378 private MenuItem optionsAddOrEditDomainMenuItem;
380 // This variable won't be needed once the class is migrated to Kotlin, as can be seen in LogcatActivity or AboutVersionFragment.
381 private Activity resultLauncherActivityHandle;
383 // Define the save URL activity result launcher. It must be defined before `onCreate()` is run or the app will crash.
384 private final ActivityResultLauncher<String> saveUrlActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.CreateDocument(),
385 new ActivityResultCallback<Uri>() {
387 public void onActivityResult(Uri fileUri) {
388 // Only save the URL if the file URI is not null, which happens if the user exited the file picker by pressing back.
389 if (fileUri != null) {
390 new SaveUrl(getApplicationContext(), resultLauncherActivityHandle, fileUri, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptCookies()).execute(saveUrlString);
393 // Reset the save URL string.
398 // Define the save webpage archive activity result launcher. It must be defined before `onCreate()` is run or the app will crash.
399 private final ActivityResultLauncher<String> saveWebpageArchiveActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.CreateDocument(),
400 new ActivityResultCallback<Uri>() {
402 public void onActivityResult(Uri fileUri) {
403 // Only save the webpage archive if the file URI is not null, which happens if the user exited the file picker by pressing back.
404 if (fileUri != null) {
406 // Create a temporary MHT file.
407 File temporaryMhtFile = File.createTempFile("temporary_mht_file", ".mht", getCacheDir());
409 // Save the temporary MHT file.
410 currentWebView.saveWebArchive(temporaryMhtFile.toString(), false, callbackValue -> {
411 if (callbackValue != null) { // The temporary MHT file was saved successfully.
413 // Create a temporary MHT file input stream.
414 FileInputStream temporaryMhtFileInputStream = new FileInputStream(temporaryMhtFile);
416 // Get an output stream for the save webpage file path.
417 OutputStream mhtOutputStream = getContentResolver().openOutputStream(fileUri);
419 // Create a transfer byte array.
420 byte[] transferByteArray = new byte[1024];
422 // Create an integer to track the number of bytes read.
425 // Copy the temporary MHT file input stream to the MHT output stream.
426 while ((bytesRead = temporaryMhtFileInputStream.read(transferByteArray)) > 0) {
427 mhtOutputStream.write(transferByteArray, 0, bytesRead);
430 // Close the streams.
431 mhtOutputStream.close();
432 temporaryMhtFileInputStream.close();
434 // Initialize the file name string from the file URI last path segment.
435 String fileNameString = fileUri.getLastPathSegment();
437 // Query the exact file name if the API >= 26.
438 if (Build.VERSION.SDK_INT >= 26) {
439 // Get a cursor from the content resolver.
440 Cursor contentResolverCursor = resultLauncherActivityHandle.getContentResolver().query(fileUri, null, null, null);
442 // Move to the fist row.
443 contentResolverCursor.moveToFirst();
445 // Get the file name from the cursor.
446 fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
449 contentResolverCursor.close();
452 // Display a snackbar.
453 Snackbar.make(currentWebView, getString(R.string.file_saved) + " " + fileNameString, Snackbar.LENGTH_SHORT).show();
454 } catch (Exception exception) {
455 // Display a snackbar with the exception.
456 Snackbar.make(currentWebView, getString(R.string.error_saving_file) + " " + exception.toString(), Snackbar.LENGTH_INDEFINITE).show();
458 // Delete the temporary MHT file.
459 //noinspection ResultOfMethodCallIgnored
460 temporaryMhtFile.delete();
462 } else { // There was an unspecified error while saving the temporary MHT file.
463 // Display an error snackbar.
464 Snackbar.make(currentWebView, getString(R.string.error_saving_file), Snackbar.LENGTH_INDEFINITE).show();
467 } catch (IOException ioException) {
468 // Display a snackbar with the IO exception.
469 Snackbar.make(currentWebView, getString(R.string.error_saving_file) + " " + ioException.toString(), Snackbar.LENGTH_INDEFINITE).show();
475 // Define the save webpage image activity result launcher. It must be defined before `onCreate()` is run or the app will crash.
476 private final ActivityResultLauncher<String> saveWebpageImageActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.CreateDocument(),
477 new ActivityResultCallback<Uri>() {
479 public void onActivityResult(Uri fileUri) {
480 // Only save the webpage image if the file URI is not null, which happens if the user exited the file picker by pressing back.
481 if (fileUri != null) {
482 // Save the webpage image.
483 new SaveWebpageImage(resultLauncherActivityHandle, fileUri, currentWebView).execute();
488 // Remove the warning about needing to override `performClick()` when using an `OnTouchListener` with WebView.
489 @SuppressLint("ClickableViewAccessibility")
491 protected void onCreate(Bundle savedInstanceState) {
492 // Run the default commands.
493 super.onCreate(savedInstanceState);
495 // Populate the result launcher activity. This will no longer be needed once the activity has transitioned to Kotlin.
496 resultLauncherActivityHandle = this;
498 // Check to see if the activity has been restarted.
499 if (savedInstanceState != null) {
500 // Store the saved instance state variables.
501 savedStateArrayList = savedInstanceState.getParcelableArrayList(SAVED_STATE_ARRAY_LIST);
502 savedNestedScrollWebViewStateArrayList = savedInstanceState.getParcelableArrayList(SAVED_NESTED_SCROLL_WEBVIEW_STATE_ARRAY_LIST);
503 savedTabPosition = savedInstanceState.getInt(SAVED_TAB_POSITION);
504 savedProxyMode = savedInstanceState.getString(PROXY_MODE);
507 // Initialize the default preference values the first time the program is run. `false` keeps this command from resetting any current preferences back to default.
508 PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
510 // Get a handle for the shared preferences.
511 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
513 // Get the preferences.
514 String appTheme = sharedPreferences.getString("app_theme", getString(R.string.app_theme_default_value));
515 boolean allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false);
516 bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false);
518 // Get the theme entry values string array.
519 String[] appThemeEntryValuesStringArray = getResources().getStringArray(R.array.app_theme_entry_values);
521 // Set the app theme according to the preference. A switch statement cannot be used because the theme entry values string array is not a compile time constant.
522 if (appTheme.equals(appThemeEntryValuesStringArray[1])) { // The light theme is selected.
523 // Apply the light theme.
524 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
525 } else if (appTheme.equals(appThemeEntryValuesStringArray[2])) { // The dark theme is selected.
526 // Apply the dark theme.
527 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
528 } else { // The system default theme is selected.
529 if (Build.VERSION.SDK_INT >= 28) { // The system default theme is supported.
530 // Follow the system default theme.
531 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
532 } else { // The system default theme is not supported.
533 // Follow the battery saver mode.
534 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
538 // Disable screenshots if not allowed.
539 if (!allowScreenshots) {
540 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
543 // Enable the drawing of the entire webpage. This makes it possible to save a website image. This must be done before anything else happens with the WebView.
544 if (Build.VERSION.SDK_INT >= 21) {
545 WebView.enableSlowWholeDocumentDraw();
549 setTheme(R.style.PrivacyBrowser);
551 // Set the content view.
553 setContentView(R.layout.main_framelayout_bottom_appbar);
555 setContentView(R.layout.main_framelayout_top_appbar);
558 // Get handles for the views.
559 rootFrameLayout = findViewById(R.id.root_framelayout);
560 drawerLayout = findViewById(R.id.drawerlayout);
561 coordinatorLayout = findViewById(R.id.coordinatorlayout);
562 appBarLayout = findViewById(R.id.appbar_layout);
563 toolbar = findViewById(R.id.toolbar);
564 findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
565 tabsLinearLayout = findViewById(R.id.tabs_linearlayout);
566 tabLayout = findViewById(R.id.tablayout);
567 swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
568 webViewPager = findViewById(R.id.webviewpager);
569 NavigationView navigationView = findViewById(R.id.navigationview);
570 fullScreenVideoFrameLayout = findViewById(R.id.full_screen_video_framelayout);
572 // Get a handle for the navigation menu.
573 Menu navigationMenu = navigationView.getMenu();
575 // Get handles for the navigation menu items.
576 navigationBackMenuItem = navigationMenu.findItem(R.id.back);
577 navigationForwardMenuItem = navigationMenu.findItem(R.id.forward);
578 navigationHistoryMenuItem = navigationMenu.findItem(R.id.history);
579 navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests);
581 // Listen for touches on the navigation menu.
582 navigationView.setNavigationItemSelectedListener(this);
584 // Get a handle for the app compat delegate.
585 AppCompatDelegate appCompatDelegate = getDelegate();
587 // Set the support action bar.
588 appCompatDelegate.setSupportActionBar(toolbar);
590 // Get a handle for the action bar.
591 actionBar = appCompatDelegate.getSupportActionBar();
593 // Remove the incorrect lint warning below that the action bar might be null.
594 assert actionBar != null;
596 // Add the custom layout, which shows the URL text bar.
597 actionBar.setCustomView(R.layout.url_app_bar);
598 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
600 // Get handles for the views in the URL app bar.
601 urlRelativeLayout = findViewById(R.id.url_relativelayout);
602 urlEditText = findViewById(R.id.url_edittext);
604 // Create the hamburger icon at the start of the AppBar.
605 actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
607 // Initially disable the sliding drawers. They will be enabled once the blocklists are loaded.
608 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
610 // Initialize the web view pager adapter.
611 webViewPagerAdapter = new WebViewPagerAdapter(getSupportFragmentManager());
613 // Set the pager adapter on the web view pager.
614 webViewPager.setAdapter(webViewPagerAdapter);
616 // Store up to 100 tabs in memory.
617 webViewPager.setOffscreenPageLimit(100);
619 // Initialize the app.
622 // Apply the app settings from the shared preferences.
625 // Populate the blocklists.
626 populateBlocklists = new PopulateBlocklists(this, this).execute();
630 protected void onNewIntent(Intent intent) {
631 // Run the default commands.
632 super.onNewIntent(intent);
634 // Replace the intent that started the app with this one.
637 // Check to see if the app is being restarted from a saved state.
638 if (savedStateArrayList == null || savedStateArrayList.size() == 0) { // The activity is not being restarted from a saved state.
639 // Get the information from the intent.
640 String intentAction = intent.getAction();
641 Uri intentUriData = intent.getData();
642 String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
644 // Determine if this is a web search.
645 boolean isWebSearch = ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH));
647 // Only process the URI if it contains data or it is a web search. If the user pressed the desktop icon after the app was already running the URI will be null.
648 if (intentUriData != null || intentStringExtra != null || isWebSearch) {
649 // Exit the full screen video if it is displayed.
650 if (displayingFullScreenVideo) {
651 // Exit full screen video mode.
652 exitFullScreenVideo();
654 // Reload the current WebView. Otherwise, it can display entirely black.
655 currentWebView.reload();
658 // Get the shared preferences.
659 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
661 // Create a URL string.
664 // If the intent action is a web search, perform the search.
665 if (isWebSearch) { // The intent is a web search.
666 // Create an encoded URL string.
667 String encodedUrlString;
669 // Sanitize the search input and convert it to a search.
671 encodedUrlString = URLEncoder.encode(intent.getStringExtra(SearchManager.QUERY), "UTF-8");
672 } catch (UnsupportedEncodingException exception) {
673 encodedUrlString = "";
676 // Add the base search URL.
677 url = searchURL + encodedUrlString;
678 } else if (intentUriData != null) { // The intent contains a URL formatted as a URI.
679 // Set the intent data as the URL.
680 url = intentUriData.toString();
681 } else { // The intent contains a string, which might be a URL.
682 // Set the intent string as the URL.
683 url = intentStringExtra;
686 // Add a new tab if specified in the preferences.
687 if (sharedPreferences.getBoolean("open_intents_in_new_tab", true)) { // Load the URL in a new tab.
688 // Set the loading new intent flag.
689 loadingNewIntent = true;
692 addNewTab(url, true);
693 } else { // Load the URL in the current tab.
695 loadUrl(currentWebView, url);
698 // Close the navigation drawer if it is open.
699 if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
700 drawerLayout.closeDrawer(GravityCompat.START);
703 // Close the bookmarks drawer if it is open.
704 if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
705 drawerLayout.closeDrawer(GravityCompat.END);
712 public void onRestart() {
713 // Run the default commands.
716 // Apply the app settings if returning from the Settings activity.
717 if (reapplyAppSettingsOnRestart) {
718 // Reset the reapply app settings on restart tracker.
719 reapplyAppSettingsOnRestart = false;
721 // Apply the app settings.
725 // Apply the domain settings if returning from the settings or domains activity.
726 if (reapplyDomainSettingsOnRestart) {
727 // Reset the reapply domain settings on restart tracker.
728 reapplyDomainSettingsOnRestart = false;
730 // Reapply the domain settings for each tab.
731 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
732 // Get the WebView tab fragment.
733 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
735 // Get the fragment view.
736 View fragmentView = webViewTabFragment.getView();
738 // Only reload the WebViews if they exist.
739 if (fragmentView != null) {
740 // Get the nested scroll WebView from the tab fragment.
741 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
743 // Reset the current domain name so the domain settings will be reapplied.
744 nestedScrollWebView.setCurrentDomainName("");
746 // Reapply the domain settings if the URL is not null, which can happen if an empty tab is active when returning from settings.
747 if (nestedScrollWebView.getUrl() != null) {
748 applyDomainSettings(nestedScrollWebView, nestedScrollWebView.getUrl(), false, true, false);
754 // Update the bookmarks drawer if returning from the Bookmarks activity.
755 if (restartFromBookmarksActivity) {
756 // Close the bookmarks drawer.
757 drawerLayout.closeDrawer(GravityCompat.END);
759 // Reload the bookmarks drawer.
760 loadBookmarksFolder();
762 // Reset `restartFromBookmarksActivity`.
763 restartFromBookmarksActivity = false;
766 // Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step. This can be important if the screen was rotated.
767 updatePrivacyIcons(true);
770 // `onStart()` runs after `onCreate()` or `onRestart()`. This is used instead of `onResume()` so the commands aren't called every time the screen is partially hidden.
772 public void onStart() {
773 // Run the default commands.
776 // Resume any WebViews.
777 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
778 // Get the WebView tab fragment.
779 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
781 // Get the fragment view.
782 View fragmentView = webViewTabFragment.getView();
784 // Only resume the WebViews if they exist (they won't when the app is first created).
785 if (fragmentView != null) {
786 // Get the nested scroll WebView from the tab fragment.
787 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
789 // Resume the nested scroll WebView.
790 nestedScrollWebView.onResume();
794 // Resume the nested scroll WebView JavaScript timers. This is a global command that resumes JavaScript timers on all WebViews.
795 if (currentWebView != null) {
796 currentWebView.resumeTimers();
799 // Reapply the proxy settings if the system is using a proxy. This redisplays the appropriate alert dialog.
800 if (!proxyMode.equals(ProxyHelper.NONE)) {
804 // Reapply any system UI flags.
805 if (displayingFullScreenVideo || inFullScreenBrowsingMode) { // The system is displaying a website or a video in full screen mode.
806 /* Hide the system bars.
807 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
808 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
809 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
810 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
812 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
813 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
816 // Show any pending dialogs.
817 for (int i = 0; i < pendingDialogsArrayList.size(); i++) {
818 // Get the pending dialog from the array list.
819 PendingDialog pendingDialog = pendingDialogsArrayList.get(i);
821 // Show the pending dialog.
822 pendingDialog.dialogFragment.show(getSupportFragmentManager(), pendingDialog.tag);
825 // Clear the pending dialogs array list.
826 pendingDialogsArrayList.clear();
829 // `onStop()` runs after `onPause()`. It is used instead of `onPause()` so the commands are not called every time the screen is partially hidden.
831 public void onStop() {
832 // Run the default commands.
835 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
836 // Get the WebView tab fragment.
837 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
839 // Get the fragment view.
840 View fragmentView = webViewTabFragment.getView();
842 // Only pause the WebViews if they exist (they won't when the app is first created).
843 if (fragmentView != null) {
844 // Get the nested scroll WebView from the tab fragment.
845 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
847 // Pause the nested scroll WebView.
848 nestedScrollWebView.onPause();
852 // Pause the WebView JavaScript timers. This is a global command that pauses JavaScript on all WebViews.
853 if (currentWebView != null) {
854 currentWebView.pauseTimers();
859 public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
860 // Run the default commands.
861 super.onSaveInstanceState(savedInstanceState);
863 // Create the saved state array lists.
864 ArrayList<Bundle> savedStateArrayList = new ArrayList<>();
865 ArrayList<Bundle> savedNestedScrollWebViewStateArrayList = new ArrayList<>();
867 // Get the URLs from each tab.
868 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
869 // Get the WebView tab fragment.
870 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
872 // Get the fragment view.
873 View fragmentView = webViewTabFragment.getView();
875 if (fragmentView != null) {
876 // Get the nested scroll WebView from the tab fragment.
877 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
879 // Create saved state bundle.
880 Bundle savedStateBundle = new Bundle();
882 // Get the current states.
883 nestedScrollWebView.saveState(savedStateBundle);
884 Bundle savedNestedScrollWebViewStateBundle = nestedScrollWebView.saveNestedScrollWebViewState();
886 // Store the saved states in the array lists.
887 savedStateArrayList.add(savedStateBundle);
888 savedNestedScrollWebViewStateArrayList.add(savedNestedScrollWebViewStateBundle);
892 // Get the current tab position.
893 int currentTabPosition = tabLayout.getSelectedTabPosition();
895 // Store the saved states in the bundle.
896 savedInstanceState.putParcelableArrayList(SAVED_STATE_ARRAY_LIST, savedStateArrayList);
897 savedInstanceState.putParcelableArrayList(SAVED_NESTED_SCROLL_WEBVIEW_STATE_ARRAY_LIST, savedNestedScrollWebViewStateArrayList);
898 savedInstanceState.putInt(SAVED_TAB_POSITION, currentTabPosition);
899 savedInstanceState.putString(PROXY_MODE, proxyMode);
903 public void onDestroy() {
904 // Unregister the orbot status broadcast receiver if it exists.
905 if (orbotStatusBroadcastReceiver != null) {
906 this.unregisterReceiver(orbotStatusBroadcastReceiver);
909 // Close the bookmarks cursor if it exists.
910 if (bookmarksCursor != null) {
911 bookmarksCursor.close();
914 // Close the bookmarks database if it exists.
915 if (bookmarksDatabaseHelper != null) {
916 bookmarksDatabaseHelper.close();
919 // Stop populating the blocklists if the AsyncTask is running in the background.
920 if (populateBlocklists != null) {
921 populateBlocklists.cancel(true);
924 // Run the default commands.
929 public boolean onCreateOptionsMenu(Menu menu) {
930 // Inflate the menu. This adds items to the action bar if it is present.
931 getMenuInflater().inflate(R.menu.webview_options_menu, menu);
933 // Store a handle for the options menu so it can be used by `onOptionsItemSelected()` and `updatePrivacyIcons()`.
936 // Get handles for the menu items.
937 optionsPrivacyMenuItem = menu.findItem(R.id.javascript);
938 optionsRefreshMenuItem = menu.findItem(R.id.refresh);
939 MenuItem bookmarksMenuItem = menu.findItem(R.id.bookmarks);
940 optionsCookiesMenuItem = menu.findItem(R.id.cookies);
941 optionsDomStorageMenuItem = menu.findItem(R.id.dom_storage);
942 optionsSaveFormDataMenuItem = menu.findItem(R.id.save_form_data); // Form data can be removed once the minimum API >= 26.
943 optionsClearDataMenuItem = menu.findItem(R.id.clear_data);
944 optionsClearCookiesMenuItem = menu.findItem(R.id.clear_cookies);
945 optionsClearDomStorageMenuItem = menu.findItem(R.id.clear_dom_storage);
946 optionsClearFormDataMenuItem = menu.findItem(R.id.clear_form_data); // Form data can be removed once the minimum API >= 26.
947 optionsBlocklistsMenuItem = menu.findItem(R.id.blocklists);
948 optionsEasyListMenuItem = menu.findItem(R.id.easylist);
949 optionsEasyPrivacyMenuItem = menu.findItem(R.id.easyprivacy);
950 optionsFanboysAnnoyanceListMenuItem = menu.findItem(R.id.fanboys_annoyance_list);
951 optionsFanboysSocialBlockingListMenuItem = menu.findItem(R.id.fanboys_social_blocking_list);
952 optionsUltraListMenuItem = menu.findItem(R.id.ultralist);
953 optionsUltraPrivacyMenuItem = menu.findItem(R.id.ultraprivacy);
954 optionsBlockAllThirdPartyRequestsMenuItem = menu.findItem(R.id.block_all_third_party_requests);
955 optionsProxyMenuItem = menu.findItem(R.id.proxy);
956 optionsProxyNoneMenuItem = menu.findItem(R.id.proxy_none);
957 optionsProxyTorMenuItem = menu.findItem(R.id.proxy_tor);
958 optionsProxyI2pMenuItem = menu.findItem(R.id.proxy_i2p);
959 optionsProxyCustomMenuItem = menu.findItem(R.id.proxy_custom);
960 optionsUserAgentMenuItem = menu.findItem(R.id.user_agent);
961 optionsUserAgentPrivacyBrowserMenuItem = menu.findItem(R.id.user_agent_privacy_browser);
962 optionsUserAgentWebViewDefaultMenuItem = menu.findItem(R.id.user_agent_webview_default);
963 optionsUserAgentFirefoxOnAndroidMenuItem = menu.findItem(R.id.user_agent_firefox_on_android);
964 optionsUserAgentChromeOnAndroidMenuItem = menu.findItem(R.id.user_agent_chrome_on_android);
965 optionsUserAgentSafariOnIosMenuItem = menu.findItem(R.id.user_agent_safari_on_ios);
966 optionsUserAgentFirefoxOnLinuxMenuItem = menu.findItem(R.id.user_agent_firefox_on_linux);
967 optionsUserAgentChromiumOnLinuxMenuItem = menu.findItem(R.id.user_agent_chromium_on_linux);
968 optionsUserAgentFirefoxOnWindowsMenuItem = menu.findItem(R.id.user_agent_firefox_on_windows);
969 optionsUserAgentChromeOnWindowsMenuItem = menu.findItem(R.id.user_agent_chrome_on_windows);
970 optionsUserAgentEdgeOnWindowsMenuItem = menu.findItem(R.id.user_agent_edge_on_windows);
971 optionsUserAgentInternetExplorerOnWindowsMenuItem = menu.findItem(R.id.user_agent_internet_explorer_on_windows);
972 optionsUserAgentSafariOnMacosMenuItem = menu.findItem(R.id.user_agent_safari_on_macos);
973 optionsUserAgentCustomMenuItem = menu.findItem(R.id.user_agent_custom);
974 optionsSwipeToRefreshMenuItem = menu.findItem(R.id.swipe_to_refresh);
975 optionsWideViewportMenuItem = menu.findItem(R.id.wide_viewport);
976 optionsDisplayImagesMenuItem = menu.findItem(R.id.display_images);
977 optionsDarkWebViewMenuItem = menu.findItem(R.id.dark_webview);
978 optionsFontSizeMenuItem = menu.findItem(R.id.font_size);
979 optionsAddOrEditDomainMenuItem = menu.findItem(R.id.add_or_edit_domain);
981 // Set the initial status of the privacy icons. `false` does not call `invalidateOptionsMenu` as the last step.
982 updatePrivacyIcons(false);
984 // Only display the form data menu items if the API < 26.
985 optionsSaveFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
986 optionsClearFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
988 // Disable the clear form data menu item if the API >= 26 so that the status of the main Clear Data is calculated correctly.
989 optionsClearFormDataMenuItem.setEnabled(Build.VERSION.SDK_INT < 26);
991 // Only display the dark WebView menu item if API >= 21.
992 optionsDarkWebViewMenuItem.setVisible(Build.VERSION.SDK_INT >= 21);
994 // Get the shared preferences.
995 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
997 // Get the dark theme and app bar preferences.
998 boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean(getString(R.string.display_additional_app_bar_icons_key), false);
1000 // 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.
1001 if (displayAdditionalAppBarIcons) {
1002 optionsRefreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
1003 bookmarksMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
1004 optionsCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
1005 } else { //Do not display the additional icons.
1006 optionsRefreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
1007 bookmarksMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
1008 optionsCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
1011 // Replace Refresh with Stop if a URL is already loading.
1012 if (currentWebView != null && currentWebView.getProgress() != 100) {
1014 optionsRefreshMenuItem.setTitle(R.string.stop);
1016 // Set the icon if it is displayed in the app bar.
1017 if (displayAdditionalAppBarIcons) {
1018 // Get the current theme status.
1019 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
1021 // Set the icon according to the current theme status.
1022 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
1023 optionsRefreshMenuItem.setIcon(R.drawable.close_blue_day);
1025 optionsRefreshMenuItem.setIcon(R.drawable.close_blue_night);
1035 public boolean onPrepareOptionsMenu(Menu menu) {
1036 // Get a handle for the cookie manager.
1037 CookieManager cookieManager = CookieManager.getInstance();
1039 // Initialize the current user agent string and the font size.
1040 String currentUserAgent = getString(R.string.user_agent_privacy_browser);
1043 // 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.
1044 if (currentWebView != null) {
1045 // Set the add or edit domain text.
1046 if (currentWebView.getDomainSettingsApplied()) {
1047 optionsAddOrEditDomainMenuItem.setTitle(R.string.edit_domain_settings);
1049 optionsAddOrEditDomainMenuItem.setTitle(R.string.add_domain_settings);
1052 // Get the current user agent from the WebView.
1053 currentUserAgent = currentWebView.getSettings().getUserAgentString();
1055 // Get the current font size from the
1056 fontSize = currentWebView.getSettings().getTextZoom();
1058 // Set the status of the menu item checkboxes.
1059 optionsDomStorageMenuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
1060 optionsSaveFormDataMenuItem.setChecked(currentWebView.getSettings().getSaveFormData()); // Form data can be removed once the minimum API >= 26.
1061 optionsEasyListMenuItem.setChecked(currentWebView.getEasyListEnabled());
1062 optionsEasyPrivacyMenuItem.setChecked(currentWebView.getEasyPrivacyEnabled());
1063 optionsFanboysAnnoyanceListMenuItem.setChecked(currentWebView.getFanboysAnnoyanceListEnabled());
1064 optionsFanboysSocialBlockingListMenuItem.setChecked(currentWebView.getFanboysSocialBlockingListEnabled());
1065 optionsUltraListMenuItem.setChecked(currentWebView.getUltraListEnabled());
1066 optionsUltraPrivacyMenuItem.setChecked(currentWebView.getUltraPrivacyEnabled());
1067 optionsBlockAllThirdPartyRequestsMenuItem.setChecked(currentWebView.getBlockAllThirdPartyRequests());
1068 optionsSwipeToRefreshMenuItem.setChecked(currentWebView.getSwipeToRefresh());
1069 optionsWideViewportMenuItem.setChecked(currentWebView.getSettings().getUseWideViewPort());
1070 optionsDisplayImagesMenuItem.setChecked(currentWebView.getSettings().getLoadsImagesAutomatically());
1072 // Initialize the display names for the blocklists with the number of blocked requests.
1073 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
1074 optionsEasyListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASYLIST) + " - " + getString(R.string.easylist));
1075 optionsEasyPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASYPRIVACY) + " - " + getString(R.string.easyprivacy));
1076 optionsFanboysAnnoyanceListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST) + " - " + getString(R.string.fanboys_annoyance_list));
1077 optionsFanboysSocialBlockingListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST) + " - " + getString(R.string.fanboys_social_blocking_list));
1078 optionsUltraListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRALIST) + " - " + getString(R.string.ultralist));
1079 optionsUltraPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRAPRIVACY) + " - " + getString(R.string.ultraprivacy));
1080 optionsBlockAllThirdPartyRequestsMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS) + " - " + getString(R.string.block_all_third_party_requests));
1082 // Enable DOM Storage if JavaScript is enabled.
1083 optionsDomStorageMenuItem.setEnabled(currentWebView.getSettings().getJavaScriptEnabled());
1085 // Set the checkbox status for dark WebView if the WebView supports it.
1086 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
1087 optionsDarkWebViewMenuItem.setChecked(WebSettingsCompat.getForceDark(currentWebView.getSettings()) == WebSettingsCompat.FORCE_DARK_ON);
1091 // Set the cookies menu item checked status.
1092 optionsCookiesMenuItem.setChecked(cookieManager.acceptCookie());
1094 // Enable Clear Cookies if there are any.
1095 optionsClearCookiesMenuItem.setEnabled(cookieManager.hasCookies());
1097 // 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`.
1098 String privateDataDirectoryString = getApplicationInfo().dataDir;
1100 // Get a count of the number of files in the Local Storage directory.
1101 File localStorageDirectory = new File (privateDataDirectoryString + "/app_webview/Local Storage/");
1102 int localStorageDirectoryNumberOfFiles = 0;
1103 if (localStorageDirectory.exists()) {
1104 // `Objects.requireNonNull` removes a lint warning that `localStorageDirectory.list` might produce a null pointed exception if it is dereferenced.
1105 localStorageDirectoryNumberOfFiles = Objects.requireNonNull(localStorageDirectory.list()).length;
1108 // Get a count of the number of files in the IndexedDB directory.
1109 File indexedDBDirectory = new File (privateDataDirectoryString + "/app_webview/IndexedDB");
1110 int indexedDBDirectoryNumberOfFiles = 0;
1111 if (indexedDBDirectory.exists()) {
1112 // `Objects.requireNonNull` removes a lint warning that `indexedDBDirectory.list` might produce a null pointed exception if it is dereferenced.
1113 indexedDBDirectoryNumberOfFiles = Objects.requireNonNull(indexedDBDirectory.list()).length;
1116 // Enable Clear DOM Storage if there is any.
1117 optionsClearDomStorageMenuItem.setEnabled(localStorageDirectoryNumberOfFiles > 0 || indexedDBDirectoryNumberOfFiles > 0);
1119 // Enable Clear Form Data is there is any. This can be removed once the minimum API >= 26.
1120 if (Build.VERSION.SDK_INT < 26) {
1121 // Get the WebView database.
1122 WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
1124 // Enable the clear form data menu item if there is anything to clear.
1125 optionsClearFormDataMenuItem.setEnabled(webViewDatabase.hasFormData());
1128 // Enable Clear Data if any of the submenu items are enabled.
1129 optionsClearDataMenuItem.setEnabled(optionsClearCookiesMenuItem.isEnabled() || optionsClearDomStorageMenuItem.isEnabled() || optionsClearFormDataMenuItem.isEnabled());
1131 // Disable Fanboy's Social Blocking List menu item if Fanboy's Annoyance List is checked.
1132 optionsFanboysSocialBlockingListMenuItem.setEnabled(!optionsFanboysAnnoyanceListMenuItem.isChecked());
1134 // Set the proxy title and check the applied proxy.
1135 switch (proxyMode) {
1136 case ProxyHelper.NONE:
1137 // Set the proxy title.
1138 optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_none));
1140 // Check the proxy None radio button.
1141 optionsProxyNoneMenuItem.setChecked(true);
1144 case ProxyHelper.TOR:
1145 // Set the proxy title.
1146 optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_tor));
1148 // Check the proxy Tor radio button.
1149 optionsProxyTorMenuItem.setChecked(true);
1152 case ProxyHelper.I2P:
1153 // Set the proxy title.
1154 optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_i2p));
1156 // Check the proxy I2P radio button.
1157 optionsProxyI2pMenuItem.setChecked(true);
1160 case ProxyHelper.CUSTOM:
1161 // Set the proxy title.
1162 optionsProxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_custom));
1164 // Check the proxy Custom radio button.
1165 optionsProxyCustomMenuItem.setChecked(true);
1169 // Select the current user agent menu item. A switch statement cannot be used because the user agents are not compile time constants.
1170 if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[0])) { // Privacy Browser.
1171 // Update the user agent menu item title.
1172 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_privacy_browser));
1174 // Select the Privacy Browser radio box.
1175 optionsUserAgentPrivacyBrowserMenuItem.setChecked(true);
1176 } else if (currentUserAgent.equals(webViewDefaultUserAgent)) { // WebView Default.
1177 // Update the user agent menu item title.
1178 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_webview_default));
1180 // Select the WebView Default radio box.
1181 optionsUserAgentWebViewDefaultMenuItem.setChecked(true);
1182 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[2])) { // Firefox on Android.
1183 // Update the user agent menu item title.
1184 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_android));
1186 // Select the Firefox on Android radio box.
1187 optionsUserAgentFirefoxOnAndroidMenuItem.setChecked(true);
1188 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[3])) { // Chrome on Android.
1189 // Update the user agent menu item title.
1190 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_android));
1192 // Select the Chrome on Android radio box.
1193 optionsUserAgentChromeOnAndroidMenuItem.setChecked(true);
1194 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[4])) { // Safari on iOS.
1195 // Update the user agent menu item title.
1196 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_ios));
1198 // Select the Safari on iOS radio box.
1199 optionsUserAgentSafariOnIosMenuItem.setChecked(true);
1200 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[5])) { // Firefox on Linux.
1201 // Update the user agent menu item title.
1202 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_linux));
1204 // Select the Firefox on Linux radio box.
1205 optionsUserAgentFirefoxOnLinuxMenuItem.setChecked(true);
1206 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[6])) { // Chromium on Linux.
1207 // Update the user agent menu item title.
1208 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chromium_on_linux));
1210 // Select the Chromium on Linux radio box.
1211 optionsUserAgentChromiumOnLinuxMenuItem.setChecked(true);
1212 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[7])) { // Firefox on Windows.
1213 // Update the user agent menu item title.
1214 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_windows));
1216 // Select the Firefox on Windows radio box.
1217 optionsUserAgentFirefoxOnWindowsMenuItem.setChecked(true);
1218 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[8])) { // Chrome on Windows.
1219 // Update the user agent menu item title.
1220 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_windows));
1222 // Select the Chrome on Windows radio box.
1223 optionsUserAgentChromeOnWindowsMenuItem.setChecked(true);
1224 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[9])) { // Edge on Windows.
1225 // Update the user agent menu item title.
1226 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_edge_on_windows));
1228 // Select the Edge on Windows radio box.
1229 optionsUserAgentEdgeOnWindowsMenuItem.setChecked(true);
1230 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[10])) { // Internet Explorer on Windows.
1231 // Update the user agent menu item title.
1232 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_internet_explorer_on_windows));
1234 // Select the Internet on Windows radio box.
1235 optionsUserAgentInternetExplorerOnWindowsMenuItem.setChecked(true);
1236 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[11])) { // Safari on macOS.
1237 // Update the user agent menu item title.
1238 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_macos));
1240 // Select the Safari on macOS radio box.
1241 optionsUserAgentSafariOnMacosMenuItem.setChecked(true);
1242 } else { // Custom user agent.
1243 // Update the user agent menu item title.
1244 optionsUserAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_custom));
1246 // Select the Custom radio box.
1247 optionsUserAgentCustomMenuItem.setChecked(true);
1250 // Set the font size title.
1251 optionsFontSizeMenuItem.setTitle(getString(R.string.font_size) + " - " + fontSize + "%");
1253 // Run all the other default commands.
1254 super.onPrepareOptionsMenu(menu);
1256 // Display the menu.
1261 public boolean onOptionsItemSelected(MenuItem menuItem) {
1262 // Get a handle for the shared preferences.
1263 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
1265 // Get a handle for the cookie manager.
1266 CookieManager cookieManager = CookieManager.getInstance();
1268 // Get the selected menu item ID.
1269 int menuItemId = menuItem.getItemId();
1271 // Run the commands that correlate to the selected menu item.
1272 if (menuItemId == R.id.javascript) { // JavaScript.
1273 // Toggle the JavaScript status.
1274 currentWebView.getSettings().setJavaScriptEnabled(!currentWebView.getSettings().getJavaScriptEnabled());
1276 // Update the privacy icon.
1277 updatePrivacyIcons(true);
1279 // Display a `Snackbar`.
1280 if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScrip is enabled.
1281 Snackbar.make(webViewPager, R.string.javascript_enabled, Snackbar.LENGTH_SHORT).show();
1282 } else if (cookieManager.acceptCookie()) { // JavaScript is disabled, but first-party cookies are enabled.
1283 Snackbar.make(webViewPager, R.string.javascript_disabled, Snackbar.LENGTH_SHORT).show();
1284 } else { // Privacy mode.
1285 Snackbar.make(webViewPager, R.string.privacy_mode, Snackbar.LENGTH_SHORT).show();
1288 // Reload the current WebView.
1289 currentWebView.reload();
1291 // Consume the event.
1293 } else if (menuItemId == R.id.refresh) { // Refresh.
1294 // Run the command that correlates to the current status of the menu item.
1295 if (menuItem.getTitle().equals(getString(R.string.refresh))) { // The refresh button was pushed.
1296 // Reload the current WebView.
1297 currentWebView.reload();
1298 } else { // The stop button was pushed.
1299 // Stop the loading of the WebView.
1300 currentWebView.stopLoading();
1303 // Consume the event.
1305 } else if (menuItemId == R.id.bookmarks) { // Bookmarks.
1306 // Open the bookmarks drawer.
1307 drawerLayout.openDrawer(GravityCompat.END);
1309 // Consume the event.
1311 } else if (menuItemId == R.id.cookies) { // Cookies.
1312 // Switch the first-party cookie status.
1313 cookieManager.setAcceptCookie(!cookieManager.acceptCookie());
1315 // Store the cookie status.
1316 currentWebView.setAcceptCookies(cookieManager.acceptCookie());
1318 // Update the menu checkbox.
1319 menuItem.setChecked(cookieManager.acceptCookie());
1321 // Update the privacy icon.
1322 updatePrivacyIcons(true);
1324 // Display a snackbar.
1325 if (cookieManager.acceptCookie()) { // Cookies are enabled.
1326 Snackbar.make(webViewPager, R.string.cookies_enabled, Snackbar.LENGTH_SHORT).show();
1327 } else if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScript is still enabled.
1328 Snackbar.make(webViewPager, R.string.cookies_disabled, Snackbar.LENGTH_SHORT).show();
1329 } else { // Privacy mode.
1330 Snackbar.make(webViewPager, R.string.privacy_mode, Snackbar.LENGTH_SHORT).show();
1333 // Reload the current WebView.
1334 currentWebView.reload();
1336 // Consume the event.
1338 } else if (menuItemId == R.id.dom_storage) { // DOM storage.
1339 // Toggle the status of domStorageEnabled.
1340 currentWebView.getSettings().setDomStorageEnabled(!currentWebView.getSettings().getDomStorageEnabled());
1342 // Update the menu checkbox.
1343 menuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
1345 // Update the privacy icon.
1346 updatePrivacyIcons(true);
1348 // Display a snackbar.
1349 if (currentWebView.getSettings().getDomStorageEnabled()) {
1350 Snackbar.make(webViewPager, R.string.dom_storage_enabled, Snackbar.LENGTH_SHORT).show();
1352 Snackbar.make(webViewPager, R.string.dom_storage_disabled, Snackbar.LENGTH_SHORT).show();
1355 // Reload the current WebView.
1356 currentWebView.reload();
1358 // Consume the event.
1360 } else if (menuItemId == R.id.save_form_data) { // Form data. This can be removed once the minimum API >= 26.
1361 // Switch the status of saveFormDataEnabled.
1362 currentWebView.getSettings().setSaveFormData(!currentWebView.getSettings().getSaveFormData());
1364 // Update the menu checkbox.
1365 menuItem.setChecked(currentWebView.getSettings().getSaveFormData());
1367 // Display a snackbar.
1368 if (currentWebView.getSettings().getSaveFormData()) {
1369 Snackbar.make(webViewPager, R.string.form_data_enabled, Snackbar.LENGTH_SHORT).show();
1371 Snackbar.make(webViewPager, R.string.form_data_disabled, Snackbar.LENGTH_SHORT).show();
1374 // Update the privacy icon.
1375 updatePrivacyIcons(true);
1377 // Reload the current WebView.
1378 currentWebView.reload();
1380 // Consume the event.
1382 } else if (menuItemId == R.id.clear_cookies) { // Clear cookies.
1383 // Create a snackbar.
1384 Snackbar.make(webViewPager, R.string.cookies_deleted, Snackbar.LENGTH_LONG)
1385 .setAction(R.string.undo, v -> {
1386 // Do nothing because everything will be handled by `onDismissed()` below.
1388 .addCallback(new Snackbar.Callback() {
1390 public void onDismissed(Snackbar snackbar, int event) {
1391 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) { // The snackbar was dismissed without the undo button being pushed.
1392 // Delete the cookies, which command varies by SDK.
1393 if (Build.VERSION.SDK_INT < 21) {
1394 cookieManager.removeAllCookie();
1396 cookieManager.removeAllCookies(null);
1403 // Consume the event.
1405 } else if (menuItemId == R.id.clear_dom_storage) { // Clear DOM storage.
1406 // Create a snackbar.
1407 Snackbar.make(webViewPager, R.string.dom_storage_deleted, Snackbar.LENGTH_LONG)
1408 .setAction(R.string.undo, v -> {
1409 // Do nothing because everything will be handled by `onDismissed()` below.
1411 .addCallback(new Snackbar.Callback() {
1413 public void onDismissed(Snackbar snackbar, int event) {
1414 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) { // The snackbar was dismissed without the undo button being pushed.
1415 // Delete the DOM Storage.
1416 WebStorage webStorage = WebStorage.getInstance();
1417 webStorage.deleteAllData();
1419 // Initialize a handler to manually delete the DOM storage files and directories.
1420 Handler deleteDomStorageHandler = new Handler();
1422 // Setup a runnable to manually delete the DOM storage files and directories.
1423 Runnable deleteDomStorageRunnable = () -> {
1425 // Get a handle for the runtime.
1426 Runtime runtime = Runtime.getRuntime();
1428 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
1429 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
1430 String privateDataDirectoryString = getApplicationInfo().dataDir;
1432 // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
1433 Process deleteLocalStorageProcess = runtime.exec(new String[]{"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
1435 // Multiple commands must be used because `Runtime.exec()` does not like `*`.
1436 Process deleteIndexProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
1437 Process deleteQuotaManagerProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
1438 Process deleteQuotaManagerJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
1439 Process deleteDatabasesProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
1441 // Wait for the processes to finish.
1442 deleteLocalStorageProcess.waitFor();
1443 deleteIndexProcess.waitFor();
1444 deleteQuotaManagerProcess.waitFor();
1445 deleteQuotaManagerJournalProcess.waitFor();
1446 deleteDatabasesProcess.waitFor();
1447 } catch (Exception exception) {
1448 // Do nothing if an error is thrown.
1452 // Manually delete the DOM storage files after 200 milliseconds.
1453 deleteDomStorageHandler.postDelayed(deleteDomStorageRunnable, 200);
1459 // Consume the event.
1461 } else if (menuItemId == R.id.clear_form_data) { // Clear form data. This can be remove once the minimum API >= 26.
1462 // Create a snackbar.
1463 Snackbar.make(webViewPager, R.string.form_data_deleted, Snackbar.LENGTH_LONG)
1464 .setAction(R.string.undo, v -> {
1465 // Do nothing because everything will be handled by `onDismissed()` below.
1467 .addCallback(new Snackbar.Callback() {
1469 public void onDismissed(Snackbar snackbar, int event) {
1470 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) { // The snackbar was dismissed without the undo button being pushed.
1471 // Get a handle for the webView database.
1472 WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(getApplicationContext());
1474 // Delete the form data.
1475 webViewDatabase.clearFormData();
1481 // Consume the event.
1483 } else if (menuItemId == R.id.easylist) { // EasyList.
1484 // Toggle the EasyList status.
1485 currentWebView.setEasyListEnabled(!currentWebView.getEasyListEnabled());
1487 // Update the menu checkbox.
1488 menuItem.setChecked(currentWebView.getEasyListEnabled());
1490 // Reload the current WebView.
1491 currentWebView.reload();
1493 // Consume the event.
1495 } else if (menuItemId == R.id.easyprivacy) { // EasyPrivacy.
1496 // Toggle the EasyPrivacy status.
1497 currentWebView.setEasyPrivacyEnabled(!currentWebView.getEasyPrivacyEnabled());
1499 // Update the menu checkbox.
1500 menuItem.setChecked(currentWebView.getEasyPrivacyEnabled());
1502 // Reload the current WebView.
1503 currentWebView.reload();
1505 // Consume the event.
1507 } else if (menuItemId == R.id.fanboys_annoyance_list) { // Fanboy's Annoyance List.
1508 // Toggle Fanboy's Annoyance List status.
1509 currentWebView.setFanboysAnnoyanceListEnabled(!currentWebView.getFanboysAnnoyanceListEnabled());
1511 // Update the menu checkbox.
1512 menuItem.setChecked(currentWebView.getFanboysAnnoyanceListEnabled());
1514 // Update the status of Fanboy's Social Blocking List.
1515 optionsFanboysSocialBlockingListMenuItem.setEnabled(!currentWebView.getFanboysAnnoyanceListEnabled());
1517 // Reload the current WebView.
1518 currentWebView.reload();
1520 // Consume the event.
1522 } else if (menuItemId == R.id.fanboys_social_blocking_list) { // Fanboy's Social Blocking List.
1523 // Toggle Fanboy's Social Blocking List status.
1524 currentWebView.setFanboysSocialBlockingListEnabled(!currentWebView.getFanboysSocialBlockingListEnabled());
1526 // Update the menu checkbox.
1527 menuItem.setChecked(currentWebView.getFanboysSocialBlockingListEnabled());
1529 // Reload the current WebView.
1530 currentWebView.reload();
1532 // Consume the event.
1534 } else if (menuItemId == R.id.ultralist) { // UltraList.
1535 // Toggle the UltraList status.
1536 currentWebView.setUltraListEnabled(!currentWebView.getUltraListEnabled());
1538 // Update the menu checkbox.
1539 menuItem.setChecked(currentWebView.getUltraListEnabled());
1541 // Reload the current WebView.
1542 currentWebView.reload();
1544 // Consume the event.
1546 } else if (menuItemId == R.id.ultraprivacy) { // UltraPrivacy.
1547 // Toggle the UltraPrivacy status.
1548 currentWebView.setUltraPrivacyEnabled(!currentWebView.getUltraPrivacyEnabled());
1550 // Update the menu checkbox.
1551 menuItem.setChecked(currentWebView.getUltraPrivacyEnabled());
1553 // Reload the current WebView.
1554 currentWebView.reload();
1556 // Consume the event.
1558 } else if (menuItemId == R.id.block_all_third_party_requests) { // Block all third-party requests.
1559 //Toggle the third-party requests blocker status.
1560 currentWebView.setBlockAllThirdPartyRequests(!currentWebView.getBlockAllThirdPartyRequests());
1562 // Update the menu checkbox.
1563 menuItem.setChecked(currentWebView.getBlockAllThirdPartyRequests());
1565 // Reload the current WebView.
1566 currentWebView.reload();
1568 // Consume the event.
1570 } else if (menuItemId == R.id.proxy_none) { // Proxy - None.
1571 // Update the proxy mode.
1572 proxyMode = ProxyHelper.NONE;
1574 // Apply the proxy mode.
1577 // Consume the event.
1579 } else if (menuItemId == R.id.proxy_tor) { // Proxy - Tor.
1580 // Update the proxy mode.
1581 proxyMode = ProxyHelper.TOR;
1583 // Apply the proxy mode.
1586 // Consume the event.
1588 } else if (menuItemId == R.id.proxy_i2p) { // Proxy - I2P.
1589 // Update the proxy mode.
1590 proxyMode = ProxyHelper.I2P;
1592 // Apply the proxy mode.
1595 // Consume the event.
1597 } else if (menuItemId == R.id.proxy_custom) { // Proxy - Custom.
1598 // Update the proxy mode.
1599 proxyMode = ProxyHelper.CUSTOM;
1601 // Apply the proxy mode.
1604 // Consume the event.
1606 } else if (menuItemId == R.id.user_agent_privacy_browser) { // User Agent - Privacy Browser.
1607 // Update the user agent.
1608 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[0]);
1610 // Reload the current WebView.
1611 currentWebView.reload();
1613 // Consume the event.
1615 } else if (menuItemId == R.id.user_agent_webview_default) { // User Agent - WebView Default.
1616 // Update the user agent.
1617 currentWebView.getSettings().setUserAgentString("");
1619 // Reload the current WebView.
1620 currentWebView.reload();
1622 // Consume the event.
1624 } else if (menuItemId == R.id.user_agent_firefox_on_android) { // User Agent - Firefox on Android.
1625 // Update the user agent.
1626 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[2]);
1628 // Reload the current WebView.
1629 currentWebView.reload();
1631 // Consume the event.
1633 } else if (menuItemId == R.id.user_agent_chrome_on_android) { // User Agent - Chrome on Android.
1634 // Update the user agent.
1635 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[3]);
1637 // Reload the current WebView.
1638 currentWebView.reload();
1640 // Consume the event.
1642 } else if (menuItemId == R.id.user_agent_safari_on_ios) { // User Agent - Safari on iOS.
1643 // Update the user agent.
1644 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[4]);
1646 // Reload the current WebView.
1647 currentWebView.reload();
1649 // Consume the event.
1651 } else if (menuItemId == R.id.user_agent_firefox_on_linux) { // User Agent - Firefox on Linux.
1652 // Update the user agent.
1653 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[5]);
1655 // Reload the current WebView.
1656 currentWebView.reload();
1658 // Consume the event.
1660 } else if (menuItemId == R.id.user_agent_chromium_on_linux) { // User Agent - Chromium on Linux.
1661 // Update the user agent.
1662 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[6]);
1664 // Reload the current WebView.
1665 currentWebView.reload();
1667 // Consume the event.
1669 } else if (menuItemId == R.id.user_agent_firefox_on_windows) { // User Agent - Firefox on Windows.
1670 // Update the user agent.
1671 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[7]);
1673 // Reload the current WebView.
1674 currentWebView.reload();
1676 // Consume the event.
1678 } else if (menuItemId == R.id.user_agent_chrome_on_windows) { // User Agent - Chrome on Windows.
1679 // Update the user agent.
1680 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[8]);
1682 // Reload the current WebView.
1683 currentWebView.reload();
1685 // Consume the event.
1687 } else if (menuItemId == R.id.user_agent_edge_on_windows) { // User Agent - Edge on Windows.
1688 // Update the user agent.
1689 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[9]);
1691 // Reload the current WebView.
1692 currentWebView.reload();
1694 // Consume the event.
1696 } else if (menuItemId == R.id.user_agent_internet_explorer_on_windows) { // User Agent - Internet Explorer on Windows.
1697 // Update the user agent.
1698 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[10]);
1700 // Reload the current WebView.
1701 currentWebView.reload();
1703 // Consume the event.
1705 } else if (menuItemId == R.id.user_agent_safari_on_macos) { // User Agent - Safari on macOS.
1706 // Update the user agent.
1707 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[11]);
1709 // Reload the current WebView.
1710 currentWebView.reload();
1712 // Consume the event.
1714 } else if (menuItemId == R.id.user_agent_custom) { // User Agent - Custom.
1715 // Update the user agent.
1716 currentWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
1718 // Reload the current WebView.
1719 currentWebView.reload();
1721 // Consume the event.
1723 } else if (menuItemId == R.id.font_size) { // Font size.
1724 // Instantiate the font size dialog.
1725 DialogFragment fontSizeDialogFragment = FontSizeDialog.displayDialog(currentWebView.getSettings().getTextZoom());
1727 // Show the font size dialog.
1728 fontSizeDialogFragment.show(getSupportFragmentManager(), getString(R.string.font_size));
1730 // Consume the event.
1732 } else if (menuItemId == R.id.swipe_to_refresh) { // Swipe to refresh.
1733 // Toggle the stored status of swipe to refresh.
1734 currentWebView.setSwipeToRefresh(!currentWebView.getSwipeToRefresh());
1736 // Update the swipe refresh layout.
1737 if (currentWebView.getSwipeToRefresh()) { // Swipe to refresh is enabled.
1738 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
1739 swipeRefreshLayout.setEnabled(currentWebView.getScrollY() == 0);
1740 } else { // Swipe to refresh is disabled.
1741 // Disable the swipe refresh layout.
1742 swipeRefreshLayout.setEnabled(false);
1745 // Consume the event.
1747 } else if (menuItemId == R.id.wide_viewport) { // Wide viewport.
1748 // Toggle the viewport.
1749 currentWebView.getSettings().setUseWideViewPort(!currentWebView.getSettings().getUseWideViewPort());
1751 // Consume the event.
1753 } else if (menuItemId == R.id.display_images) { // Display images.
1754 // Toggle the displaying of images.
1755 if (currentWebView.getSettings().getLoadsImagesAutomatically()) { // Images are currently loaded automatically.
1756 // Disable loading of images.
1757 currentWebView.getSettings().setLoadsImagesAutomatically(false);
1759 // Reload the website to remove existing images.
1760 currentWebView.reload();
1761 } else { // Images are not currently loaded automatically.
1762 // Enable loading of images. Missing images will be loaded without the need for a reload.
1763 currentWebView.getSettings().setLoadsImagesAutomatically(true);
1766 // Consume the event.
1768 } else if (menuItemId == R.id.dark_webview) { // Dark WebView.
1769 // Check to see if dark WebView is supported by this WebView.
1770 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
1771 // Toggle the dark WebView setting.
1772 if (WebSettingsCompat.getForceDark(currentWebView.getSettings()) == WebSettingsCompat.FORCE_DARK_ON) { // Dark WebView is currently enabled.
1773 // Turn off dark WebView.
1774 WebSettingsCompat.setForceDark(currentWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
1775 } else { // Dark WebView is currently disabled.
1776 // Turn on dark WebView.
1777 WebSettingsCompat.setForceDark(currentWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
1781 // Consume the event.
1783 } else if (menuItemId == R.id.find_on_page) { // Find on page.
1784 // Get a handle for the views.
1785 Toolbar toolbar = findViewById(R.id.toolbar);
1786 LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
1787 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
1789 // Set the minimum height of the find on page linear layout to match the toolbar.
1790 findOnPageLinearLayout.setMinimumHeight(toolbar.getHeight());
1792 // Hide the toolbar.
1793 toolbar.setVisibility(View.GONE);
1795 // Show the find on page linear layout.
1796 findOnPageLinearLayout.setVisibility(View.VISIBLE);
1798 // Display the keyboard. The app must wait 200 ms before running the command to work around a bug in Android.
1799 // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working
1800 findOnPageEditText.postDelayed(() -> {
1801 // Set the focus on the find on page edit text.
1802 findOnPageEditText.requestFocus();
1804 // Get a handle for the input method manager.
1805 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
1807 // Remove the lint warning below that the input method manager might be null.
1808 assert inputMethodManager != null;
1810 // Display the keyboard. `0` sets no input flags.
1811 inputMethodManager.showSoftInput(findOnPageEditText, 0);
1814 // Consume the event.
1816 } else if (menuItemId == R.id.print) { // Print.
1817 // Get a print manager instance.
1818 PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
1820 // Remove the lint error below that print manager might be null.
1821 assert printManager != null;
1823 // Create a print document adapter from the current WebView.
1824 PrintDocumentAdapter printDocumentAdapter = currentWebView.createPrintDocumentAdapter();
1826 // Print the document.
1827 printManager.print(getString(R.string.privacy_browser_webpage), printDocumentAdapter, null);
1829 // Consume the event.
1831 } else if (menuItemId == R.id.save_url) { // Save URL.
1832 // Check the download preference.
1833 if (downloadWithExternalApp) { // Download with an external app.
1834 downloadUrlWithExternalApp(currentWebView.getCurrentUrl());
1835 } else { // Handle the download inside of Privacy Browser.
1836 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
1837 new PrepareSaveDialog(this, this, getSupportFragmentManager(), currentWebView.getSettings().getUserAgentString(),
1838 currentWebView.getAcceptCookies()).execute(currentWebView.getCurrentUrl());
1841 // Consume the event.
1843 } else if (menuItemId == R.id.save_archive) {
1844 // Open the file picker with a default file name built from the current domain name.
1845 saveWebpageArchiveActivityResultLauncher.launch(currentWebView.getCurrentDomainName() + ".mht");
1847 // Consume the event.
1849 } else if (menuItemId == R.id.save_image) { // Save image.
1850 // Open the file picker with a default file name built from the current domain name.
1851 saveWebpageImageActivityResultLauncher.launch(currentWebView.getCurrentDomainName() + ".png");
1853 // Consume the event.
1855 } else if (menuItemId == R.id.add_to_homescreen) { // Add to homescreen.
1856 // Instantiate the create home screen shortcut dialog.
1857 DialogFragment createHomeScreenShortcutDialogFragment = CreateHomeScreenShortcutDialog.createDialog(currentWebView.getTitle(), currentWebView.getUrl(),
1858 currentWebView.getFavoriteOrDefaultIcon());
1860 // Show the create home screen shortcut dialog.
1861 createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut));
1863 // Consume the event.
1865 } else if (menuItemId == R.id.view_source) { // View source.
1866 // Create an intent to launch the view source activity.
1867 Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class);
1869 // Add the variables to the intent.
1870 viewSourceIntent.putExtra(ViewSourceActivityKt.CURRENT_URL, currentWebView.getUrl());
1871 viewSourceIntent.putExtra(ViewSourceActivityKt.USER_AGENT, currentWebView.getSettings().getUserAgentString());
1874 startActivity(viewSourceIntent);
1876 // Consume the event.
1878 } else if (menuItemId == R.id.share_url) { // Share URL.
1879 // Setup the share string.
1880 String shareString = currentWebView.getTitle() + " – " + currentWebView.getUrl();
1882 // Create the share intent.
1883 Intent shareIntent = new Intent(Intent.ACTION_SEND);
1885 // Add the share string to the intent.
1886 shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
1888 // Set the MIME type.
1889 shareIntent.setType("text/plain");
1891 // Set the intent to open in a new task.
1892 shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1895 startActivity(Intent.createChooser(shareIntent, getString(R.string.share_url)));
1897 // Consume the event.
1899 } else if (menuItemId == R.id.open_with_app) { // Open with app.
1900 // Open the URL with an outside app.
1901 openWithApp(currentWebView.getUrl());
1903 // Consume the event.
1905 } else if (menuItemId == R.id.open_with_browser) { // Open with browser.
1906 // Open the URL with an outside browser.
1907 openWithBrowser(currentWebView.getUrl());
1909 // Consume the event.
1911 } else if (menuItemId == R.id.add_or_edit_domain) { // Add or edit domain.
1912 // Check if domain settings currently exist.
1913 if (currentWebView.getDomainSettingsApplied()) { // Edit the current domain settings.
1914 // Reapply the domain settings on returning to `MainWebViewActivity`.
1915 reapplyDomainSettingsOnRestart = true;
1917 // Create an intent to launch the domains activity.
1918 Intent domainsIntent = new Intent(this, DomainsActivity.class);
1920 // Add the extra information to the intent.
1921 domainsIntent.putExtra("load_domain", currentWebView.getDomainSettingsDatabaseId());
1922 domainsIntent.putExtra("close_on_back", true);
1923 domainsIntent.putExtra("current_url", currentWebView.getUrl());
1924 domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
1926 // Get the current certificate.
1927 SslCertificate sslCertificate = currentWebView.getCertificate();
1929 // Check to see if the SSL certificate is populated.
1930 if (sslCertificate != null) {
1931 // Extract the certificate to strings.
1932 String issuedToCName = sslCertificate.getIssuedTo().getCName();
1933 String issuedToOName = sslCertificate.getIssuedTo().getOName();
1934 String issuedToUName = sslCertificate.getIssuedTo().getUName();
1935 String issuedByCName = sslCertificate.getIssuedBy().getCName();
1936 String issuedByOName = sslCertificate.getIssuedBy().getOName();
1937 String issuedByUName = sslCertificate.getIssuedBy().getUName();
1938 long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
1939 long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
1941 // Add the certificate to the intent.
1942 domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
1943 domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
1944 domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
1945 domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
1946 domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
1947 domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
1948 domainsIntent.putExtra("ssl_start_date", startDateLong);
1949 domainsIntent.putExtra("ssl_end_date", endDateLong);
1953 startActivity(domainsIntent);
1954 } else { // Add a new domain.
1955 // Apply the new domain settings on returning to `MainWebViewActivity`.
1956 reapplyDomainSettingsOnRestart = true;
1958 // Get the current domain
1959 Uri currentUri = Uri.parse(currentWebView.getUrl());
1960 String currentDomain = currentUri.getHost();
1962 // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
1963 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
1965 // Create the domain and store the database ID.
1966 int newDomainDatabaseId = domainsDatabaseHelper.addDomain(currentDomain);
1968 // Create an intent to launch the domains activity.
1969 Intent domainsIntent = new Intent(this, DomainsActivity.class);
1971 // Add the extra information to the intent.
1972 domainsIntent.putExtra("load_domain", newDomainDatabaseId);
1973 domainsIntent.putExtra("close_on_back", true);
1974 domainsIntent.putExtra("current_url", currentWebView.getUrl());
1975 domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
1977 // Get the current certificate.
1978 SslCertificate sslCertificate = currentWebView.getCertificate();
1980 // Check to see if the SSL certificate is populated.
1981 if (sslCertificate != null) {
1982 // Extract the certificate to strings.
1983 String issuedToCName = sslCertificate.getIssuedTo().getCName();
1984 String issuedToOName = sslCertificate.getIssuedTo().getOName();
1985 String issuedToUName = sslCertificate.getIssuedTo().getUName();
1986 String issuedByCName = sslCertificate.getIssuedBy().getCName();
1987 String issuedByOName = sslCertificate.getIssuedBy().getOName();
1988 String issuedByUName = sslCertificate.getIssuedBy().getUName();
1989 long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
1990 long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
1992 // Add the certificate to the intent.
1993 domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
1994 domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
1995 domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
1996 domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
1997 domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
1998 domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
1999 domainsIntent.putExtra("ssl_start_date", startDateLong);
2000 domainsIntent.putExtra("ssl_end_date", endDateLong);
2004 startActivity(domainsIntent);
2007 // Consume the event.
2009 } else { // There is no match with the options menu. Pass the event up to the parent method.
2010 // Don't consume the event.
2011 return super.onOptionsItemSelected(menuItem);
2015 // removeAllCookies is deprecated, but it is required for API < 21.
2017 public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
2018 // Get a handle for the shared preferences.
2019 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
2021 // Get the menu item ID.
2022 int menuItemId = menuItem.getItemId();
2024 // Run the commands that correspond to the selected menu item.
2025 if (menuItemId == R.id.clear_and_exit) { // Clear and exit.
2026 // Clear and exit Privacy Browser.
2028 } else if (menuItemId == R.id.home) { // Home.
2029 // Load the homepage.
2030 loadUrl(currentWebView, sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
2031 } else if (menuItemId == R.id.back) { // Back.
2032 // Check if the WebView can go back.
2033 if (currentWebView.canGoBack()) {
2034 // Get the current web back forward list.
2035 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
2037 // Get the previous entry URL.
2038 String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl();
2040 // Apply the domain settings.
2041 applyDomainSettings(currentWebView, previousUrl, false, false, false);
2043 // Load the previous website in the history.
2044 currentWebView.goBack();
2046 } else if (menuItemId == R.id.forward) { // Forward.
2047 // Check if the WebView can go forward.
2048 if (currentWebView.canGoForward()) {
2049 // Get the current web back forward list.
2050 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
2052 // Get the next entry URL.
2053 String nextUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() + 1).getUrl();
2055 // Apply the domain settings.
2056 applyDomainSettings(currentWebView, nextUrl, false, false, false);
2058 // Load the next website in the history.
2059 currentWebView.goForward();
2061 } else if (menuItemId == R.id.history) { // History.
2062 // Instantiate the URL history dialog.
2063 DialogFragment urlHistoryDialogFragment = UrlHistoryDialog.loadBackForwardList(currentWebView.getWebViewFragmentId());
2065 // Show the URL history dialog.
2066 urlHistoryDialogFragment.show(getSupportFragmentManager(), getString(R.string.history));
2067 } else if (menuItemId == R.id.open) { // Open.
2068 // Instantiate the open file dialog.
2069 DialogFragment openDialogFragment = new OpenDialog();
2071 // Show the open file dialog.
2072 openDialogFragment.show(getSupportFragmentManager(), getString(R.string.open));
2073 } else if (menuItemId == R.id.requests) { // Requests.
2074 // Populate the resource requests.
2075 RequestsActivity.resourceRequests = currentWebView.getResourceRequests();
2077 // Create an intent to launch the Requests activity.
2078 Intent requestsIntent = new Intent(this, RequestsActivity.class);
2080 // Add the block third-party requests status to the intent.
2081 requestsIntent.putExtra("block_all_third_party_requests", currentWebView.getBlockAllThirdPartyRequests());
2084 startActivity(requestsIntent);
2085 } else if (menuItemId == R.id.downloads) { // Downloads.
2086 // Try the default system download manager.
2088 // Launch the default system Download Manager.
2089 Intent defaultDownloadManagerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2091 // Launch as a new task so that the download manager and Privacy Browser show as separate windows in the recent tasks list.
2092 defaultDownloadManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2095 startActivity(defaultDownloadManagerIntent);
2096 } catch (Exception defaultDownloadManagerException) {
2097 // Try a generic file manager.
2099 // Create a generic file manager intent.
2100 Intent genericFileManagerIntent = new Intent(Intent.ACTION_VIEW);
2102 // Open the download directory.
2103 genericFileManagerIntent.setDataAndType(Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()), DocumentsContract.Document.MIME_TYPE_DIR);
2105 // Launch as a new task so that the file manager and Privacy Browser show as separate windows in the recent tasks list.
2106 genericFileManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2109 startActivity(genericFileManagerIntent);
2110 } catch (Exception genericFileManagerException) {
2111 // Try an alternate file manager.
2113 // Create an alternate file manager intent.
2114 Intent alternateFileManagerIntent = new Intent(Intent.ACTION_VIEW);
2116 // Open the download directory.
2117 alternateFileManagerIntent.setDataAndType(Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()), "resource/folder");
2119 // Launch as a new task so that the file manager and Privacy Browser show as separate windows in the recent tasks list.
2120 alternateFileManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2122 // Open the alternate file manager.
2123 startActivity(alternateFileManagerIntent);
2124 } catch (Exception alternateFileManagerException) {
2125 // Display a snackbar.
2126 Snackbar.make(currentWebView, R.string.no_file_manager_detected, Snackbar.LENGTH_INDEFINITE).show();
2130 } else if (menuItemId == R.id.domains) { // Domains.
2131 // Set the flag to reapply the domain settings on restart when returning from Domain Settings.
2132 reapplyDomainSettingsOnRestart = true;
2134 // Launch the domains activity.
2135 Intent domainsIntent = new Intent(this, DomainsActivity.class);
2137 // Add the extra information to the intent.
2138 domainsIntent.putExtra("current_url", currentWebView.getUrl());
2139 domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
2141 // Get the current certificate.
2142 SslCertificate sslCertificate = currentWebView.getCertificate();
2144 // Check to see if the SSL certificate is populated.
2145 if (sslCertificate != null) {
2146 // Extract the certificate to strings.
2147 String issuedToCName = sslCertificate.getIssuedTo().getCName();
2148 String issuedToOName = sslCertificate.getIssuedTo().getOName();
2149 String issuedToUName = sslCertificate.getIssuedTo().getUName();
2150 String issuedByCName = sslCertificate.getIssuedBy().getCName();
2151 String issuedByOName = sslCertificate.getIssuedBy().getOName();
2152 String issuedByUName = sslCertificate.getIssuedBy().getUName();
2153 long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
2154 long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
2156 // Add the certificate to the intent.
2157 domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
2158 domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
2159 domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
2160 domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
2161 domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
2162 domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
2163 domainsIntent.putExtra("ssl_start_date", startDateLong);
2164 domainsIntent.putExtra("ssl_end_date", endDateLong);
2168 startActivity(domainsIntent);
2169 } else if (menuItemId == R.id.settings) { // Settings.
2170 // Set the flag to reapply app settings on restart when returning from Settings.
2171 reapplyAppSettingsOnRestart = true;
2173 // Set the flag to reapply the domain settings on restart when returning from Settings.
2174 reapplyDomainSettingsOnRestart = true;
2176 // Launch the settings activity.
2177 Intent settingsIntent = new Intent(this, SettingsActivity.class);
2178 startActivity(settingsIntent);
2179 } else if (menuItemId == R.id.import_export) { // Import/Export.
2180 // Create an intent to launch the import/export activity.
2181 Intent importExportIntent = new Intent(this, ImportExportActivity.class);
2184 startActivity(importExportIntent);
2185 } else if (menuItemId == R.id.logcat) { // Logcat.
2186 // Create an intent to launch the logcat activity.
2187 Intent logcatIntent = new Intent(this, LogcatActivity.class);
2190 startActivity(logcatIntent);
2191 } else if (menuItemId == R.id.guide) { // Guide.
2192 // Create an intent to launch the guide activity.
2193 Intent guideIntent = new Intent(this, GuideActivity.class);
2196 startActivity(guideIntent);
2197 } else if (menuItemId == R.id.about) { // About
2198 // Create an intent to launch the about activity.
2199 Intent aboutIntent = new Intent(this, AboutActivity.class);
2201 // Create a string array for the blocklist versions.
2202 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],
2203 ultraList.get(0).get(0)[0], ultraPrivacy.get(0).get(0)[0]};
2205 // Add the blocklist versions to the intent.
2206 aboutIntent.putExtra(AboutActivity.BLOCKLIST_VERSIONS, blocklistVersions);
2209 startActivity(aboutIntent);
2212 // Close the navigation drawer.
2213 drawerLayout.closeDrawer(GravityCompat.START);
2218 public void onPostCreate(Bundle savedInstanceState) {
2219 // Run the default commands.
2220 super.onPostCreate(savedInstanceState);
2222 // Sync the state of the DrawerToggle after the default `onRestoreInstanceState()` has finished. This creates the navigation drawer icon.
2223 actionBarDrawerToggle.syncState();
2227 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
2228 // Get the hit test result.
2229 final WebView.HitTestResult hitTestResult = currentWebView.getHitTestResult();
2231 // Define the URL strings.
2232 final String imageUrl;
2233 final String linkUrl;
2235 // Get handles for the system managers.
2236 final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
2238 // Remove the lint errors below that the clipboard manager might be null.
2239 assert clipboardManager != null;
2241 // Process the link according to the type.
2242 switch (hitTestResult.getType()) {
2243 // `SRC_ANCHOR_TYPE` is a link.
2244 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
2245 // Get the target URL.
2246 linkUrl = hitTestResult.getExtra();
2248 // Set the target URL as the title of the `ContextMenu`.
2249 menu.setHeaderTitle(linkUrl);
2251 // Add an Open in New Tab entry.
2252 menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2253 // Load the link URL in a new tab and move to it.
2254 addNewTab(linkUrl, true);
2256 // Consume the event.
2260 // Add an Open in Background entry.
2261 menu.add(R.string.open_in_background).setOnMenuItemClickListener((MenuItem item) -> {
2262 // Load the link URL in a new tab but do not move to it.
2263 addNewTab(linkUrl, false);
2265 // Consume the event.
2269 // Add an Open with App entry.
2270 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2271 openWithApp(linkUrl);
2273 // Consume the event.
2277 // Add an Open with Browser entry.
2278 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2279 openWithBrowser(linkUrl);
2281 // Consume the event.
2285 // Add a Copy URL entry.
2286 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2287 // Save the link URL in a `ClipData`.
2288 ClipData srcAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), linkUrl);
2290 // Set the `ClipData` as the clipboard's primary clip.
2291 clipboardManager.setPrimaryClip(srcAnchorTypeClipData);
2293 // Consume the event.
2297 // Add a Save URL entry.
2298 menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> {
2299 // Check the download preference.
2300 if (downloadWithExternalApp) { // Download with an external app.
2301 downloadUrlWithExternalApp(linkUrl);
2302 } else { // Handle the download inside of Privacy Browser.
2303 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2304 new PrepareSaveDialog(this, this, getSupportFragmentManager(), currentWebView.getSettings().getUserAgentString(),
2305 currentWebView.getAcceptCookies()).execute(linkUrl);
2308 // Consume the event.
2312 // Add an empty Cancel entry, which by default closes the context menu.
2313 menu.add(R.string.cancel);
2316 // `IMAGE_TYPE` is an image.
2317 case WebView.HitTestResult.IMAGE_TYPE:
2318 // Get the image URL.
2319 imageUrl = hitTestResult.getExtra();
2321 // Remove the incorrect lint warning below that the image URL might be null.
2322 assert imageUrl != null;
2324 // Set the context menu title.
2325 if (imageUrl.startsWith("data:")) { // The image data is contained in within the URL, making it exceedingly long.
2326 // Truncate the image URL before making it the title.
2327 menu.setHeaderTitle(imageUrl.substring(0, 100));
2328 } else { // The image URL does not contain the full image data.
2329 // Set the image URL as the title of the context menu.
2330 menu.setHeaderTitle(imageUrl);
2333 // Add an Open in New Tab entry.
2334 menu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2335 // Load the image in a new tab.
2336 addNewTab(imageUrl, true);
2338 // Consume the event.
2342 // Add an Open with App entry.
2343 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2344 // Open the image URL with an external app.
2345 openWithApp(imageUrl);
2347 // Consume the event.
2351 // Add an Open with Browser entry.
2352 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2353 // Open the image URL with an external browser.
2354 openWithBrowser(imageUrl);
2356 // Consume the event.
2360 // Add a View Image entry.
2361 menu.add(R.string.view_image).setOnMenuItemClickListener(item -> {
2362 // Load the image in the current tab.
2363 loadUrl(currentWebView, imageUrl);
2365 // Consume the event.
2369 // Add a Save Image entry.
2370 menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> {
2371 // Check the download preference.
2372 if (downloadWithExternalApp) { // Download with an external app.
2373 downloadUrlWithExternalApp(imageUrl);
2374 } else { // Handle the download inside of Privacy Browser.
2375 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2376 new PrepareSaveDialog(this, this, getSupportFragmentManager(), currentWebView.getSettings().getUserAgentString(),
2377 currentWebView.getAcceptCookies()).execute(imageUrl);
2380 // Consume the event.
2384 // Add a Copy URL entry.
2385 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2386 // Save the image URL in a clip data.
2387 ClipData imageTypeClipData = ClipData.newPlainText(getString(R.string.url), imageUrl);
2389 // Set the clip data as the clipboard's primary clip.
2390 clipboardManager.setPrimaryClip(imageTypeClipData);
2392 // Consume the event.
2396 // Add an empty Cancel entry, which by default closes the context menu.
2397 menu.add(R.string.cancel);
2400 // `SRC_IMAGE_ANCHOR_TYPE` is an image that is also a link.
2401 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
2402 // Get the image URL.
2403 imageUrl = hitTestResult.getExtra();
2405 // Instantiate a handler.
2406 Handler handler = new Handler();
2408 // Get a message from the handler.
2409 Message message = handler.obtainMessage();
2411 // Request the image details from the last touched node be returned in the message.
2412 currentWebView.requestFocusNodeHref(message);
2414 // Get the link URL from the message data.
2415 linkUrl = message.getData().getString("url");
2417 // Set the link URL as the title of the context menu.
2418 menu.setHeaderTitle(linkUrl);
2420 // Add an Open in New Tab entry.
2421 menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2422 // Load the link URL in a new tab and move to it.
2423 addNewTab(linkUrl, true);
2425 // Consume the event.
2429 // Add an Open in Background entry.
2430 menu.add(R.string.open_in_background).setOnMenuItemClickListener((MenuItem item) -> {
2431 // Lod the link URL in a new tab but do not move to it.
2432 addNewTab(linkUrl, false);
2434 // Consume the event.
2438 // Add an Open Image in New Tab entry.
2439 menu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2440 // Load the image in a new tab and move to it.
2441 addNewTab(imageUrl, true);
2443 // Consume the event.
2447 // Add an Open with App entry.
2448 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2449 // Open the link URL with an external app.
2450 openWithApp(linkUrl);
2452 // Consume the event.
2456 // Add an Open with Browser entry.
2457 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2458 // Open the link URL with an external browser.
2459 openWithBrowser(linkUrl);
2461 // Consume the event.
2465 // Add a View Image entry.
2466 menu.add(R.string.view_image).setOnMenuItemClickListener((MenuItem item) -> {
2467 // View the image in the current tab.
2468 loadUrl(currentWebView, imageUrl);
2470 // Consume the event.
2474 // Add a Save Image entry.
2475 menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> {
2476 // Check the download preference.
2477 if (downloadWithExternalApp) { // Download with an external app.
2478 downloadUrlWithExternalApp(imageUrl);
2479 } else { // Handle the download inside of Privacy Browser.
2480 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2481 new PrepareSaveDialog(this, this, getSupportFragmentManager(), currentWebView.getSettings().getUserAgentString(),
2482 currentWebView.getAcceptCookies()).execute(imageUrl);
2485 // Consume the event.
2489 // Add a Copy URL entry.
2490 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2491 // Save the link URL in a clip data.
2492 ClipData srcImageAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), linkUrl);
2494 // Set the clip data as the clipboard's primary clip.
2495 clipboardManager.setPrimaryClip(srcImageAnchorTypeClipData);
2497 // Consume the event.
2501 // Add a Save URL entry.
2502 menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> {
2503 // Check the download preference.
2504 if (downloadWithExternalApp) { // Download with an external app.
2505 downloadUrlWithExternalApp(linkUrl);
2506 } else { // Handle the download inside of Privacy Browser.
2507 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2508 new PrepareSaveDialog(this, this, getSupportFragmentManager(), currentWebView.getSettings().getUserAgentString(),
2509 currentWebView.getAcceptCookies()).execute(linkUrl);
2512 // Consume the event.
2516 // Add an empty Cancel entry, which by default closes the context menu.
2517 menu.add(R.string.cancel);
2520 case WebView.HitTestResult.EMAIL_TYPE:
2521 // Get the target URL.
2522 linkUrl = hitTestResult.getExtra();
2524 // Set the target URL as the title of the `ContextMenu`.
2525 menu.setHeaderTitle(linkUrl);
2527 // Add a Write Email entry.
2528 menu.add(R.string.write_email).setOnMenuItemClickListener(item -> {
2529 // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
2530 Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
2532 // Parse the url and set it as the data for the `Intent`.
2533 emailIntent.setData(Uri.parse("mailto:" + linkUrl));
2535 // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser.
2536 emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2540 startActivity(emailIntent);
2541 } catch (ActivityNotFoundException exception) {
2542 // Display a snackbar.
2543 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
2546 // Consume the event.
2550 // Add a Copy Email Address entry.
2551 menu.add(R.string.copy_email_address).setOnMenuItemClickListener(item -> {
2552 // Save the email address in a `ClipData`.
2553 ClipData srcEmailTypeClipData = ClipData.newPlainText(getString(R.string.email_address), linkUrl);
2555 // Set the `ClipData` as the clipboard's primary clip.
2556 clipboardManager.setPrimaryClip(srcEmailTypeClipData);
2558 // Consume the event.
2562 // Add an empty Cancel entry, which by default closes the context menu.
2563 menu.add(R.string.cancel);
2569 public void onCreateBookmark(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
2570 // Get a handle for the bookmarks list view.
2571 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
2574 Dialog dialog = dialogFragment.getDialog();
2576 // Remove the incorrect lint warning below that the dialog might be null.
2577 assert dialog != null;
2579 // Get the views from the dialog fragment.
2580 EditText createBookmarkNameEditText = dialog.findViewById(R.id.create_bookmark_name_edittext);
2581 EditText createBookmarkUrlEditText = dialog.findViewById(R.id.create_bookmark_url_edittext);
2583 // Extract the strings from the edit texts.
2584 String bookmarkNameString = createBookmarkNameEditText.getText().toString();
2585 String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
2587 // Create a favorite icon byte array output stream.
2588 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
2590 // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2591 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
2593 // Convert the favorite icon byte array stream to a byte array.
2594 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
2596 // Display the new bookmark below the current items in the (0 indexed) list.
2597 int newBookmarkDisplayOrder = bookmarksListView.getCount();
2599 // Create the bookmark.
2600 bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentBookmarksFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
2602 // Update the bookmarks cursor with the current contents of this folder.
2603 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2605 // Update the list view.
2606 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2608 // Scroll to the new bookmark.
2609 bookmarksListView.setSelection(newBookmarkDisplayOrder);
2613 public void onCreateBookmarkFolder(DialogFragment dialogFragment, @NonNull Bitmap favoriteIconBitmap) {
2614 // Get a handle for the bookmarks list view.
2615 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
2618 Dialog dialog = dialogFragment.getDialog();
2620 // Remove the incorrect lint warning below that the dialog might be null.
2621 assert dialog != null;
2623 // Get handles for the views in the dialog fragment.
2624 EditText folderNameEditText = dialog.findViewById(R.id.folder_name_edittext);
2625 RadioButton defaultIconRadioButton = dialog.findViewById(R.id.default_icon_radiobutton);
2626 ImageView defaultIconImageView = dialog.findViewById(R.id.default_icon_imageview);
2628 // Get new folder name string.
2629 String folderNameString = folderNameEditText.getText().toString();
2631 // Create a folder icon bitmap.
2632 Bitmap folderIconBitmap;
2634 // Set the folder icon bitmap according to the dialog.
2635 if (defaultIconRadioButton.isChecked()) { // Use the default folder icon.
2636 // Get the default folder icon drawable.
2637 Drawable folderIconDrawable = defaultIconImageView.getDrawable();
2639 // Convert the folder icon drawable to a bitmap drawable.
2640 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2642 // Convert the folder icon bitmap drawable to a bitmap.
2643 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2644 } else { // Use the WebView favorite icon.
2645 // Copy the favorite icon bitmap to the folder icon bitmap.
2646 folderIconBitmap = favoriteIconBitmap;
2649 // Create a folder icon byte array output stream.
2650 ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
2652 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2653 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
2655 // Convert the folder icon byte array stream to a byte array.
2656 byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
2658 // Move all the bookmarks down one in the display order.
2659 for (int i = 0; i < bookmarksListView.getCount(); i++) {
2660 int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
2661 bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1);
2664 // Create the folder, which will be placed at the top of the `ListView`.
2665 bookmarksDatabaseHelper.createFolder(folderNameString, currentBookmarksFolder, folderIconByteArray);
2667 // Update the bookmarks cursor with the current contents of this folder.
2668 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2670 // Update the `ListView`.
2671 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2673 // Scroll to the new folder.
2674 bookmarksListView.setSelection(0);
2678 public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId, @NonNull Bitmap favoriteIconBitmap) {
2679 // Remove the incorrect lint warning below that the dialog fragment might be null.
2680 assert dialogFragment != null;
2683 Dialog dialog = dialogFragment.getDialog();
2685 // Remove the incorrect lint warning below that the dialog might be null.
2686 assert dialog != null;
2688 // Get handles for the views from the dialog.
2689 RadioButton currentFolderIconRadioButton = dialog.findViewById(R.id.current_icon_radiobutton);
2690 RadioButton defaultFolderIconRadioButton = dialog.findViewById(R.id.default_icon_radiobutton);
2691 ImageView defaultFolderIconImageView = dialog.findViewById(R.id.default_icon_imageview);
2692 EditText editFolderNameEditText = dialog.findViewById(R.id.folder_name_edittext);
2694 // Get the new folder name.
2695 String newFolderNameString = editFolderNameEditText.getText().toString();
2697 // Check if the favorite icon has changed.
2698 if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed.
2699 // Update the name in the database.
2700 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
2701 } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
2702 // Create the new folder icon Bitmap.
2703 Bitmap folderIconBitmap;
2705 // Populate the new folder icon bitmap.
2706 if (defaultFolderIconRadioButton.isChecked()) {
2707 // Get the default folder icon drawable.
2708 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
2710 // Convert the folder icon drawable to a bitmap drawable.
2711 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2713 // Convert the folder icon bitmap drawable to a bitmap.
2714 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2715 } else { // Use the `WebView` favorite icon.
2716 // Copy the favorite icon bitmap to the folder icon bitmap.
2717 folderIconBitmap = favoriteIconBitmap;
2720 // Create a folder icon byte array output stream.
2721 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
2723 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2724 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
2726 // Convert the folder icon byte array stream to a byte array.
2727 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
2729 // Update the folder icon in the database.
2730 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
2731 } else { // The folder icon and the name have changed.
2732 // Get the new folder icon bitmap.
2733 Bitmap folderIconBitmap;
2734 if (defaultFolderIconRadioButton.isChecked()) {
2735 // Get the default folder icon drawable.
2736 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
2738 // Convert the folder icon drawable to a bitmap drawable.
2739 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2741 // Convert the folder icon bitmap drawable to a bitmap.
2742 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2743 } else { // Use the `WebView` favorite icon.
2744 // Copy the favorite icon bitmap to the folder icon bitmap.
2745 folderIconBitmap = favoriteIconBitmap;
2748 // Create a folder icon byte array output stream.
2749 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
2751 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2752 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
2754 // Convert the folder icon byte array stream to a byte array.
2755 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
2757 // Update the folder name and icon in the database.
2758 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
2761 // Update the bookmarks cursor with the current contents of this folder.
2762 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2764 // Update the `ListView`.
2765 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2768 // Override `onBackPressed()` to handle the navigation drawer and and the WebViews.
2770 public void onBackPressed() {
2771 // Check the different options for processing `back`.
2772 if (drawerLayout.isDrawerVisible(GravityCompat.START)) { // The navigation drawer is open.
2773 // Close the navigation drawer.
2774 drawerLayout.closeDrawer(GravityCompat.START);
2775 } else if (drawerLayout.isDrawerVisible(GravityCompat.END)){ // The bookmarks drawer is open.
2776 // close the bookmarks drawer.
2777 drawerLayout.closeDrawer(GravityCompat.END);
2778 } else if (displayingFullScreenVideo) { // A full screen video is shown.
2779 // Exit the full screen video.
2780 exitFullScreenVideo();
2781 } else if (currentWebView.canGoBack()) { // There is at least one item in the current WebView history.
2782 // Get the current web back forward list.
2783 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
2785 // Get the previous entry URL.
2786 String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl();
2788 // Apply the domain settings.
2789 applyDomainSettings(currentWebView, previousUrl, false, false, false);
2792 currentWebView.goBack();
2793 } else if (tabLayout.getTabCount() > 1) { // There are at least two tabs.
2794 // Close the current tab.
2796 } else { // There isn't anything to do in Privacy Browser.
2797 // Close Privacy Browser. `finishAndRemoveTask()` also removes Privacy Browser from the recent app list.
2798 if (Build.VERSION.SDK_INT >= 21) {
2799 finishAndRemoveTask();
2804 // Manually kill Privacy Browser. Otherwise, it is glitchy when restarted.
2809 // Process the results of a file browse.
2811 public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
2812 // Run the default commands.
2813 super.onActivityResult(requestCode, resultCode, returnedIntent);
2815 // Run the commands that correlate to the specified request code.
2816 switch (requestCode) {
2817 case BROWSE_FILE_UPLOAD_REQUEST_CODE:
2818 // File uploads only work on API >= 21.
2819 if (Build.VERSION.SDK_INT >= 21) {
2820 // Pass the file to the WebView.
2821 fileChooserCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, returnedIntent));
2825 case BROWSE_OPEN_REQUEST_CODE:
2826 // Don't do anything if the user pressed back from the file picker.
2827 if (resultCode == Activity.RESULT_OK) {
2828 // Get a handle for the open dialog fragment.
2829 DialogFragment openDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.open));
2831 // Only update the file name if the dialog still exists.
2832 if (openDialogFragment != null) {
2833 // Get a handle for the open dialog.
2834 Dialog openDialog = openDialogFragment.getDialog();
2836 // Remove the incorrect lint warning below that the dialog might be null.
2837 assert openDialog != null;
2839 // Get a handle for the file name edit text.
2840 EditText fileNameEditText = openDialog.findViewById(R.id.file_name_edittext);
2842 // Get the file name URI from the intent.
2843 Uri fileNameUri = returnedIntent.getData();
2845 // Get the file name string from the URI.
2846 String fileNameString = fileNameUri.toString();
2848 // Set the file name text.
2849 fileNameEditText.setText(fileNameString);
2851 // Move the cursor to the end of the file name edit text.
2852 fileNameEditText.setSelection(fileNameString.length());
2859 private void loadUrlFromTextBox() {
2860 // Get the text from urlTextBox and convert it to a string. trim() removes white spaces from the beginning and end of the string.
2861 String unformattedUrlString = urlEditText.getText().toString().trim();
2863 // Initialize the formatted URL string.
2866 // Check to see if `unformattedUrlString` is a valid URL. Otherwise, convert it into a search.
2867 if (unformattedUrlString.startsWith("content://")) { // This is a Content URL.
2868 // Load the entire content URL.
2869 url = unformattedUrlString;
2870 } else if (Patterns.WEB_URL.matcher(unformattedUrlString).matches() || unformattedUrlString.startsWith("http://") || unformattedUrlString.startsWith("https://") ||
2871 unformattedUrlString.startsWith("file://")) { // This is a standard URL.
2872 // Add `https://` at the beginning if there is no protocol. Otherwise the app will segfault.
2873 if (!unformattedUrlString.startsWith("http") && !unformattedUrlString.startsWith("file://") && !unformattedUrlString.startsWith("content://")) {
2874 unformattedUrlString = "https://" + unformattedUrlString;
2877 // Initialize `unformattedUrl`.
2878 URL unformattedUrl = null;
2880 // Convert `unformattedUrlString` to a `URL`, then to a `URI`, and then back to a `String`, which sanitizes the input and adds in any missing components.
2882 unformattedUrl = new URL(unformattedUrlString);
2883 } catch (MalformedURLException e) {
2884 e.printStackTrace();
2887 // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if `.get` was called on a `null` value.
2888 String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
2889 String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
2890 String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
2891 String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
2892 String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
2895 Uri.Builder uri = new Uri.Builder();
2896 uri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
2898 // Decode the URI as a UTF-8 string in.
2900 url = URLDecoder.decode(uri.build().toString(), "UTF-8");
2901 } catch (UnsupportedEncodingException exception) {
2902 // Do nothing. The formatted URL string will remain blank.
2904 } else if (!unformattedUrlString.isEmpty()){ // This is not a URL, but rather a search string.
2905 // Create an encoded URL String.
2906 String encodedUrlString;
2908 // Sanitize the search input.
2910 encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
2911 } catch (UnsupportedEncodingException exception) {
2912 encodedUrlString = "";
2915 // Add the base search URL.
2916 url = searchURL + encodedUrlString;
2919 // Clear the focus from the URL edit text. Otherwise, proximate typing in the box will retain the colorized formatting instead of being reset during refocus.
2920 urlEditText.clearFocus();
2923 loadUrl(currentWebView, url);
2926 private void loadUrl(NestedScrollWebView nestedScrollWebView, String url) {
2927 // Sanitize the URL.
2928 url = sanitizeUrl(url);
2930 // Apply the domain settings and load the URL.
2931 applyDomainSettings(nestedScrollWebView, url, true, false, true);
2934 public void findPreviousOnPage(View view) {
2935 // Go to the previous highlighted phrase on the page. `false` goes backwards instead of forwards.
2936 currentWebView.findNext(false);
2939 public void findNextOnPage(View view) {
2940 // Go to the next highlighted phrase on the page. `true` goes forwards instead of backwards.
2941 currentWebView.findNext(true);
2944 public void closeFindOnPage(View view) {
2945 // Get a handle for the views.
2946 Toolbar toolbar = findViewById(R.id.toolbar);
2947 LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
2948 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
2950 // Delete the contents of `find_on_page_edittext`.
2951 findOnPageEditText.setText(null);
2953 // Clear the highlighted phrases if the WebView is not null.
2954 if (currentWebView != null) {
2955 currentWebView.clearMatches();
2958 // Hide the find on page linear layout.
2959 findOnPageLinearLayout.setVisibility(View.GONE);
2961 // Show the toolbar.
2962 toolbar.setVisibility(View.VISIBLE);
2964 // Get a handle for the input method manager.
2965 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
2967 // Remove the lint warning below that the input method manager might be null.
2968 assert inputMethodManager != null;
2970 // Hide the keyboard.
2971 inputMethodManager.hideSoftInputFromWindow(toolbar.getWindowToken(), 0);
2975 public void onApplyNewFontSize(DialogFragment dialogFragment) {
2976 // Remove the incorrect lint warning below that the dialog fragment might be null.
2977 assert dialogFragment != null;
2980 Dialog dialog = dialogFragment.getDialog();
2982 // Remove the incorrect lint warning below tha the dialog might be null.
2983 assert dialog != null;
2985 // Get a handle for the font size edit text.
2986 EditText fontSizeEditText = dialog.findViewById(R.id.font_size_edittext);
2988 // Initialize the new font size variable with the current font size.
2989 int newFontSize = currentWebView.getSettings().getTextZoom();
2991 // Get the font size from the edit text.
2993 newFontSize = Integer.parseInt(fontSizeEditText.getText().toString());
2994 } catch (Exception exception) {
2995 // If the edit text does not contain a valid font size do nothing.
2998 // Apply the new font size.
2999 currentWebView.getSettings().setTextZoom(newFontSize);
3003 public void onOpen(DialogFragment dialogFragment) {
3005 Dialog dialog = dialogFragment.getDialog();
3007 // Remove the incorrect lint warning below that the dialog might be null.
3008 assert dialog != null;
3010 // Get handles for the views.
3011 EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
3012 CheckBox mhtCheckBox = dialog.findViewById(R.id.mht_checkbox);
3014 // Get the file path string.
3015 String openFilePath = fileNameEditText.getText().toString();
3017 // Apply the domain settings. This resets the favorite icon and removes any domain settings.
3018 applyDomainSettings(currentWebView, openFilePath, true, false, false);
3020 // Open the file according to the type.
3021 if (mhtCheckBox.isChecked()) { // Force opening of an MHT file.
3023 // Get the MHT file input stream.
3024 InputStream mhtFileInputStream = getContentResolver().openInputStream(Uri.parse(openFilePath));
3026 // Create a temporary MHT file.
3027 File temporaryMhtFile = File.createTempFile("temporary_mht_file", ".mht", getCacheDir());
3029 // Get a file output stream for the temporary MHT file.
3030 FileOutputStream temporaryMhtFileOutputStream = new FileOutputStream(temporaryMhtFile);
3032 // Create a transfer byte array.
3033 byte[] transferByteArray = new byte[1024];
3035 // Create an integer to track the number of bytes read.
3038 // Copy the temporary MHT file input stream to the MHT output stream.
3039 while ((bytesRead = mhtFileInputStream.read(transferByteArray)) > 0) {
3040 temporaryMhtFileOutputStream.write(transferByteArray, 0, bytesRead);
3043 // Flush the temporary MHT file output stream.
3044 temporaryMhtFileOutputStream.flush();
3046 // Close the streams.
3047 temporaryMhtFileOutputStream.close();
3048 mhtFileInputStream.close();
3050 // Load the temporary MHT file.
3051 currentWebView.loadUrl(temporaryMhtFile.toString());
3052 } catch (Exception exception) {
3053 // Display a snackbar.
3054 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception.toString(), Snackbar.LENGTH_INDEFINITE).show();
3056 } else { // Let the WebView handle opening of the file.
3058 currentWebView.loadUrl(openFilePath);
3062 private void downloadUrlWithExternalApp(String url) {
3063 // Create a download intent. Not specifying the action type will display the maximum number of options.
3064 Intent downloadIntent = new Intent();
3066 // Set the URI and the mime type.
3067 downloadIntent.setDataAndType(Uri.parse(url), "text/html");
3069 // Flag the intent to open in a new task.
3070 downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3072 // Show the chooser.
3073 startActivity(Intent.createChooser(downloadIntent, getString(R.string.download_with_external_app)));
3076 public void onSaveUrl(@NonNull String originalUrlString, @NonNull String fileNameString, @NonNull DialogFragment dialogFragment) {
3077 // Store the URL. This will be used in the save URL activity result launcher.
3078 if (originalUrlString.startsWith("data:")) {
3079 // Save the original URL.
3080 saveUrlString = originalUrlString;
3083 Dialog dialog = dialogFragment.getDialog();
3085 // Remove the incorrect lint warning below that the dialog might be null.
3086 assert dialog != null;
3088 // Get a handle for the dialog URL edit text.
3089 EditText dialogUrlEditText = dialog.findViewById(R.id.url_edittext);
3091 // Get the URL from the edit text, which may have been modified.
3092 saveUrlString = dialogUrlEditText.getText().toString();
3095 // Open the file picker.
3096 saveUrlActivityResultLauncher.launch(fileNameString);
3099 // Remove the warning that `OnTouchListener()` needs to override `performClick()`, as the only purpose of setting the `OnTouchListener()` is to make it do nothing.
3100 @SuppressLint("ClickableViewAccessibility")
3101 private void initializeApp() {
3102 // Get a handle for the input method.
3103 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
3105 // Remove the lint warning below that the input method manager might be null.
3106 assert inputMethodManager != null;
3108 // Initialize the gray foreground color spans for highlighting the URLs. The deprecated `getResources()` must be used until API >= 23.
3109 initialGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
3110 finalGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
3112 // Get the current theme status.
3113 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
3115 // Set the red color span according to the theme.
3116 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
3117 redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
3119 redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_900));
3122 // Remove the formatting from the URL edit text when the user is editing the text.
3123 urlEditText.setOnFocusChangeListener((View v, boolean hasFocus) -> {
3124 if (hasFocus) { // The user is editing the URL text box.
3125 // Remove the highlighting.
3126 urlEditText.getText().removeSpan(redColorSpan);
3127 urlEditText.getText().removeSpan(initialGrayColorSpan);
3128 urlEditText.getText().removeSpan(finalGrayColorSpan);
3129 } else { // The user has stopped editing the URL text box.
3130 // Move to the beginning of the string.
3131 urlEditText.setSelection(0);
3133 // Reapply the highlighting.
3138 // Set the go button on the keyboard to load the URL in `urlTextBox`.
3139 urlEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
3140 // If the event is a key-down event on the `enter` button, load the URL.
3141 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
3142 // Load the URL into the mainWebView and consume the event.
3143 loadUrlFromTextBox();
3145 // If the enter key was pressed, consume the event.
3148 // If any other key was pressed, do not consume the event.
3153 // Create an Orbot status broadcast receiver.
3154 orbotStatusBroadcastReceiver = new BroadcastReceiver() {
3156 public void onReceive(Context context, Intent intent) {
3157 // Store the content of the status message in `orbotStatus`.
3158 orbotStatus = intent.getStringExtra("org.torproject.android.intent.extra.STATUS");
3160 // If Privacy Browser is waiting on the proxy, load the website now that Orbot is connected.
3161 if ((orbotStatus != null) && orbotStatus.equals("ON") && waitingForProxy) {
3162 // Reset the waiting for proxy status.
3163 waitingForProxy = false;
3165 // Get a list of the current fragments.
3166 List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
3168 // Check each fragment to see if it is a waiting for proxy dialog. Sometimes more than one is displayed.
3169 for (int i = 0; i < fragmentList.size(); i++) {
3170 // Get the fragment tag.
3171 String fragmentTag = fragmentList.get(i).getTag();
3173 // Check to see if it is the waiting for proxy dialog.
3174 if ((fragmentTag!= null) && fragmentTag.equals(getString(R.string.waiting_for_proxy_dialog))) {
3175 // Dismiss the waiting for proxy dialog.
3176 ((DialogFragment) fragmentList.get(i)).dismiss();
3180 // Reload existing URLs and load any URLs that are waiting for the proxy.
3181 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
3182 // Get the WebView tab fragment.
3183 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
3185 // Get the fragment view.
3186 View fragmentView = webViewTabFragment.getView();
3188 // Only process the WebViews if they exist.
3189 if (fragmentView != null) {
3190 // Get the nested scroll WebView from the tab fragment.
3191 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
3193 // Get the waiting for proxy URL string.
3194 String waitingForProxyUrlString = nestedScrollWebView.getWaitingForProxyUrlString();
3196 // Load the pending URL if it exists.
3197 if (!waitingForProxyUrlString.isEmpty()) { // A URL is waiting to be loaded.
3199 loadUrl(nestedScrollWebView, waitingForProxyUrlString);
3201 // Reset the waiting for proxy URL string.
3202 nestedScrollWebView.setWaitingForProxyUrlString("");
3203 } else { // No URL is waiting to be loaded.
3204 // Reload the existing URL.
3205 nestedScrollWebView.reload();
3213 // Register the Orbot status broadcast receiver on `this` context.
3214 this.registerReceiver(orbotStatusBroadcastReceiver, new IntentFilter("org.torproject.android.intent.action.STATUS"));
3216 // Get handles for views that need to be modified.
3217 LinearLayout bookmarksHeaderLinearLayout = findViewById(R.id.bookmarks_header_linearlayout);
3218 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
3219 FloatingActionButton launchBookmarksActivityFab = findViewById(R.id.launch_bookmarks_activity_fab);
3220 FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
3221 FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab);
3222 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
3224 // Update the web view pager every time a tab is modified.
3225 webViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
3227 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
3232 public void onPageSelected(int position) {
3233 // Close the find on page bar if it is open.
3234 closeFindOnPage(null);
3236 // Set the current WebView.
3237 setCurrentWebView(position);
3239 // Select the corresponding tab if it does not match the currently selected page. This will happen if the page was scrolled by creating a new tab.
3240 if (tabLayout.getSelectedTabPosition() != position) {
3241 // Wait until the new tab has been created.
3242 tabLayout.post(() -> {
3243 // Get a handle for the tab.
3244 TabLayout.Tab tab = tabLayout.getTabAt(position);
3246 // Assert that the tab is not null.
3256 public void onPageScrollStateChanged(int state) {
3261 // Display the View SSL Certificate dialog when the currently selected tab is reselected.
3262 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
3264 public void onTabSelected(TabLayout.Tab tab) {
3265 // Select the same page in the view pager.
3266 webViewPager.setCurrentItem(tab.getPosition());
3270 public void onTabUnselected(TabLayout.Tab tab) {
3275 public void onTabReselected(TabLayout.Tab tab) {
3276 // Instantiate the View SSL Certificate dialog.
3277 DialogFragment viewSslCertificateDialogFragment = ViewSslCertificateDialog.displayDialog(currentWebView.getWebViewFragmentId(), currentWebView.getFavoriteOrDefaultIcon());
3279 // Display the View SSL Certificate dialog.
3280 viewSslCertificateDialogFragment.show(getSupportFragmentManager(), getString(R.string.view_ssl_certificate));
3284 // Set a touch listener on the bookmarks header linear layout so that touches don't pass through to the button underneath.
3285 bookmarksHeaderLinearLayout.setOnTouchListener((view, motionEvent) -> {
3286 // Consume the touch.
3290 // Set the launch bookmarks activity FAB to launch the bookmarks activity.
3291 launchBookmarksActivityFab.setOnClickListener(v -> {
3292 // Get a copy of the favorite icon bitmap.
3293 Bitmap favoriteIconBitmap = currentWebView.getFavoriteOrDefaultIcon();
3295 // Create a favorite icon byte array output stream.
3296 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
3298 // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
3299 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
3301 // Convert the favorite icon byte array stream to a byte array.
3302 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
3304 // Create an intent to launch the bookmarks activity.
3305 Intent bookmarksIntent = new Intent(getApplicationContext(), BookmarksActivity.class);
3307 // Add the extra information to the intent.
3308 bookmarksIntent.putExtra("current_url", currentWebView.getUrl());
3309 bookmarksIntent.putExtra("current_title", currentWebView.getTitle());
3310 bookmarksIntent.putExtra("current_folder", currentBookmarksFolder);
3311 bookmarksIntent.putExtra("favorite_icon_byte_array", favoriteIconByteArray);
3314 startActivity(bookmarksIntent);
3317 // Set the create new bookmark folder FAB to display an alert dialog.
3318 createBookmarkFolderFab.setOnClickListener(v -> {
3319 // Create a create bookmark folder dialog.
3320 DialogFragment createBookmarkFolderDialog = CreateBookmarkFolderDialog.createBookmarkFolder(currentWebView.getFavoriteOrDefaultIcon());
3322 // Show the create bookmark folder dialog.
3323 createBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.create_folder));
3326 // Set the create new bookmark FAB to display an alert dialog.
3327 createBookmarkFab.setOnClickListener(view -> {
3328 // Instantiate the create bookmark dialog.
3329 DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebView.getUrl(), currentWebView.getTitle(), currentWebView.getFavoriteOrDefaultIcon());
3331 // Display the create bookmark dialog.
3332 createBookmarkDialog.show(getSupportFragmentManager(), getString(R.string.create_bookmark));
3335 // Search for the string on the page whenever a character changes in the `findOnPageEditText`.
3336 findOnPageEditText.addTextChangedListener(new TextWatcher() {
3338 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
3343 public void onTextChanged(CharSequence s, int start, int before, int count) {
3348 public void afterTextChanged(Editable s) {
3349 // Search for the text in the WebView if it is not null. Sometimes on resume after a period of non-use the WebView will be null.
3350 if (currentWebView != null) {
3351 currentWebView.findAllAsync(findOnPageEditText.getText().toString());
3356 // Set the `check mark` button for the `findOnPageEditText` keyboard to close the soft keyboard.
3357 findOnPageEditText.setOnKeyListener((v, keyCode, event) -> {
3358 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // The `enter` key was pressed.
3359 // Hide the soft keyboard.
3360 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
3362 // Consume the event.
3364 } else { // A different key was pressed.
3365 // Do not consume the event.
3370 // Implement swipe to refresh.
3371 swipeRefreshLayout.setOnRefreshListener(() -> {
3372 // Check the visibility of the bottom app bar. Sometimes it is hidden if the WebView is the same size as the visible screen.
3373 if (bottomAppBar && scrollAppBar && (appBarLayout.getVisibility() == View.GONE)) { // The bottom app bar is currently hidden.
3374 // Show the app bar.
3375 appBarLayout.setVisibility(View.VISIBLE);
3377 // Disable the refreshing animation.
3378 swipeRefreshLayout.setRefreshing(false);
3379 } else { // A bottom app bar is not currently hidden.
3380 // Reload the website.
3381 currentWebView.reload();
3385 // Store the default progress view offsets for use later in `initializeWebView()`.
3386 defaultProgressViewStartOffset = swipeRefreshLayout.getProgressViewStartOffset();
3387 defaultProgressViewEndOffset = swipeRefreshLayout.getProgressViewEndOffset();
3389 // Set the refresh color scheme according to the theme.
3390 swipeRefreshLayout.setColorSchemeResources(R.color.blue_text);
3392 // Initialize a color background typed value.
3393 TypedValue colorBackgroundTypedValue = new TypedValue();
3395 // Get the color background from the theme.
3396 getTheme().resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true);
3398 // Get the color background int from the typed value.
3399 int colorBackgroundInt = colorBackgroundTypedValue.data;
3401 // Set the swipe refresh background color.
3402 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt);
3404 // The drawer titles identify the drawer layouts in accessibility mode.
3405 drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));
3406 drawerLayout.setDrawerTitle(GravityCompat.END, getString(R.string.bookmarks));
3408 // Initialize the bookmarks database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
3409 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
3411 // Initialize `currentBookmarksFolder`. `""` is the home folder in the database.
3412 currentBookmarksFolder = "";
3414 // Load the home folder, which is `""` in the database.
3415 loadBookmarksFolder();
3417 bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
3418 // Convert the id from long to int to match the format of the bookmarks database.
3419 int databaseId = (int) id;
3421 // Get the bookmark cursor for this ID.
3422 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseId);
3424 // Move the bookmark cursor to the first row.
3425 bookmarkCursor.moveToFirst();
3427 // Act upon the bookmark according to the type.
3428 if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder.
3429 // Store the new folder name in `currentBookmarksFolder`.
3430 currentBookmarksFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
3432 // Load the new folder.
3433 loadBookmarksFolder();
3434 } else { // The selected bookmark is not a folder.
3435 // Load the bookmark URL.
3436 loadUrl(currentWebView, bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
3438 // Close the bookmarks drawer.
3439 drawerLayout.closeDrawer(GravityCompat.END);
3442 // Close the `Cursor`.
3443 bookmarkCursor.close();
3446 bookmarksListView.setOnItemLongClickListener((parent, view, position, id) -> {
3447 // Convert the database ID from `long` to `int`.
3448 int databaseId = (int) id;
3450 // Find out if the selected bookmark is a folder.
3451 boolean isFolder = bookmarksDatabaseHelper.isFolder(databaseId);
3453 // Check to see if the bookmark is a folder.
3454 if (isFolder) { // The bookmark is a folder.
3455 // Save the current folder name, which is used in `onSaveEditBookmarkFolder()`.
3456 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
3458 // Instantiate the edit folder bookmark dialog.
3459 DialogFragment editBookmarkFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId, currentWebView.getFavoriteOrDefaultIcon());
3461 // Show the edit folder bookmark dialog.
3462 editBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.edit_folder));
3463 } else { // The bookmark is not a folder.
3464 // Get the bookmark cursor for this ID.
3465 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseId);
3467 // Move the bookmark cursor to the first row.
3468 bookmarkCursor.moveToFirst();
3470 // Load the bookmark in a new tab but do not switch to the tab or close the drawer.
3471 addNewTab(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)), false);
3473 // Display a snackbar.
3474 Snackbar.make(currentWebView, R.string.bookmark_opened_in_background, Snackbar.LENGTH_SHORT).show();
3477 // Consume the event.
3481 // The drawer listener is used to update the navigation menu.
3482 drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
3484 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
3488 public void onDrawerOpened(@NonNull View drawerView) {
3492 public void onDrawerClosed(@NonNull View drawerView) {
3493 // Reset the drawer icon when the drawer is closed. Otherwise, it is an arrow if the drawer is open when the app is restarted.
3494 actionBarDrawerToggle.syncState();
3498 public void onDrawerStateChanged(int newState) {
3499 if ((newState == DrawerLayout.STATE_SETTLING) || (newState == DrawerLayout.STATE_DRAGGING)) { // A drawer is opening or closing.
3500 // Update the navigation menu items if the WebView is not null.
3501 if (currentWebView != null) {
3502 navigationBackMenuItem.setEnabled(currentWebView.canGoBack());
3503 navigationForwardMenuItem.setEnabled(currentWebView.canGoForward());
3504 navigationHistoryMenuItem.setEnabled((currentWebView.canGoBack() || currentWebView.canGoForward()));
3505 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
3507 // Hide the keyboard (if displayed).
3508 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
3511 // Clear the focus from from the URL text box. This removes any text selection markers and context menus, which otherwise draw above the open drawers.
3512 urlEditText.clearFocus();
3514 // Clear the focus from from the WebView if it is not null, which can happen if a user opens a drawer while the browser is being resumed.
3515 if (currentWebView != null) {
3516 // Clearing the focus from the WebView removes any text selection markers and context menus, which otherwise draw above the open drawers.
3517 currentWebView.clearFocus();
3523 // 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).
3524 customHeaders.put("X-Requested-With", "");
3526 // Inflate a bare WebView to get the default user agent. It is not used to render content on the screen.
3527 @SuppressLint("InflateParams") View webViewLayout = getLayoutInflater().inflate(R.layout.bare_webview, null, false);
3529 // Get a handle for the WebView.
3530 WebView bareWebView = webViewLayout.findViewById(R.id.bare_webview);
3532 // Store the default user agent.
3533 webViewDefaultUserAgent = bareWebView.getSettings().getUserAgentString();
3535 // Destroy the bare WebView.
3536 bareWebView.destroy();
3539 private void applyAppSettings() {
3540 // Get a handle for the shared preferences.
3541 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
3543 // Store the values from the shared preferences in variables.
3544 incognitoModeEnabled = sharedPreferences.getBoolean("incognito_mode", false);
3545 sanitizeGoogleAnalytics = sharedPreferences.getBoolean("google_analytics", true);
3546 sanitizeFacebookClickIds = sharedPreferences.getBoolean("facebook_click_ids", true);
3547 sanitizeTwitterAmpRedirects = sharedPreferences.getBoolean("twitter_amp_redirects", true);
3548 proxyMode = sharedPreferences.getString("proxy", getString(R.string.proxy_default_value));
3549 fullScreenBrowsingModeEnabled = sharedPreferences.getBoolean("full_screen_browsing_mode", false);
3550 downloadWithExternalApp = sharedPreferences.getBoolean(getString(R.string.download_with_external_app_key), false);
3551 hideAppBar = sharedPreferences.getBoolean("hide_app_bar", true);
3552 scrollAppBar = sharedPreferences.getBoolean("scroll_app_bar", true);
3554 // Apply the saved proxy mode if the app has been restarted.
3555 if (savedProxyMode != null) {
3556 // Apply the saved proxy mode.
3557 proxyMode = savedProxyMode;
3559 // Reset the saved proxy mode.
3560 savedProxyMode = null;
3563 // Get the search string.
3564 String searchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
3566 // Set the search string.
3567 if (searchString.equals("Custom URL")) { // A custom search string is used.
3568 searchURL = sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value));
3569 } else { // A custom search string is not used.
3570 searchURL = searchString;
3576 // Adjust the layout and scrolling parameters if the app bar is at the top of the screen.
3577 if (!bottomAppBar) {
3578 // Get the current layout parameters. Using coordinator layout parameters allows the `setBehavior()` command and using app bar layout parameters allows the `setScrollFlags()` command.
3579 CoordinatorLayout.LayoutParams swipeRefreshLayoutParams = (CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams();
3580 AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
3581 AppBarLayout.LayoutParams findOnPageLayoutParams = (AppBarLayout.LayoutParams) findOnPageLinearLayout.getLayoutParams();
3582 AppBarLayout.LayoutParams tabsLayoutParams = (AppBarLayout.LayoutParams) tabsLinearLayout.getLayoutParams();
3584 // Add the scrolling behavior to the layout parameters.
3586 // Enable scrolling of the app bar.
3587 swipeRefreshLayoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior());
3588 toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
3589 findOnPageLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
3590 tabsLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
3592 // Disable scrolling of the app bar.
3593 swipeRefreshLayoutParams.setBehavior(null);
3594 toolbarLayoutParams.setScrollFlags(0);
3595 findOnPageLayoutParams.setScrollFlags(0);
3596 tabsLayoutParams.setScrollFlags(0);
3598 // Expand the app bar if it is currently collapsed.
3599 appBarLayout.setExpanded(true);
3602 // Set the app bar scrolling for each WebView.
3603 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
3604 // Get the WebView tab fragment.
3605 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
3607 // Get the fragment view.
3608 View fragmentView = webViewTabFragment.getView();
3610 // Only modify the WebViews if they exist.
3611 if (fragmentView != null) {
3612 // Get the nested scroll WebView from the tab fragment.
3613 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
3615 // Set the app bar scrolling.
3616 nestedScrollWebView.setNestedScrollingEnabled(scrollAppBar);
3621 // Update the full screen browsing mode settings.
3622 if (fullScreenBrowsingModeEnabled && inFullScreenBrowsingMode) { // Privacy Browser is currently in full screen browsing mode.
3623 // Update the visibility of the app bar, which might have changed in the settings.
3625 // Hide the tab linear layout.
3626 tabsLinearLayout.setVisibility(View.GONE);
3628 // Hide the action bar.
3631 // Show the tab linear layout.
3632 tabsLinearLayout.setVisibility(View.VISIBLE);
3634 // Show the action bar.
3638 /* Hide the system bars.
3639 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
3640 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
3641 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
3642 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
3644 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
3645 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
3646 } else { // Privacy Browser is not in full screen browsing mode.
3647 // Reset the full screen tracker, which could be true if Privacy Browser was in full screen mode before entering settings and full screen browsing was disabled.
3648 inFullScreenBrowsingMode = false;
3650 // Show the tab linear layout.
3651 tabsLinearLayout.setVisibility(View.VISIBLE);
3653 // Show the action bar.
3656 // Remove the `SYSTEM_UI` flags from the root frame layout.
3657 rootFrameLayout.setSystemUiVisibility(0);
3662 public void navigateHistory(@NonNull String url, int steps) {
3663 // Apply the domain settings.
3664 applyDomainSettings(currentWebView, url, false, false, false);
3666 // Load the history entry.
3667 currentWebView.goBackOrForward(steps);
3671 public void pinnedErrorGoBack() {
3672 // Get the current web back forward list.
3673 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
3675 // Get the previous entry URL.
3676 String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl();
3678 // Apply the domain settings.
3679 applyDomainSettings(currentWebView, previousUrl, false, false, false);
3682 currentWebView.goBack();
3685 // `reloadWebsite` is used if returning from the Domains activity. Otherwise JavaScript might not function correctly if it is newly enabled.
3686 @SuppressLint("SetJavaScriptEnabled")
3687 private void applyDomainSettings(NestedScrollWebView nestedScrollWebView, String url, boolean resetTab, boolean reloadWebsite, boolean loadUrl) {
3688 // Store the current URL.
3689 nestedScrollWebView.setCurrentUrl(url);
3691 // Parse the URL into a URI.
3692 Uri uri = Uri.parse(url);
3694 // Extract the domain from `uri`.
3695 String newHostName = uri.getHost();
3697 // Strings don't like to be null.
3698 if (newHostName == null) {
3702 // Apply the domain settings if a new domain is being loaded or if the new domain is blank. This allows the user to set temporary settings for JavaScript, cookies, DOM storage, etc.
3703 if (!nestedScrollWebView.getCurrentDomainName().equals(newHostName) || newHostName.equals("")) {
3704 // Set the new host name as the current domain name.
3705 nestedScrollWebView.setCurrentDomainName(newHostName);
3707 // Reset the ignoring of pinned domain information.
3708 nestedScrollWebView.setIgnorePinnedDomainInformation(false);
3710 // Clear any pinned SSL certificate or IP addresses.
3711 nestedScrollWebView.clearPinnedSslCertificate();
3712 nestedScrollWebView.setPinnedIpAddresses("");
3714 // Reset the favorite icon if specified.
3716 // Initialize the favorite icon.
3717 nestedScrollWebView.initializeFavoriteIcon();
3719 // Get the current page position.
3720 int currentPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
3722 // Get the corresponding tab.
3723 TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition);
3725 // Update the tab if it isn't null, which sometimes happens when restarting from the background.
3727 // Get the tab custom view.
3728 View tabCustomView = tab.getCustomView();
3730 // Remove the warning below that the tab custom view might be null.
3731 assert tabCustomView != null;
3733 // Get the tab views.
3734 ImageView tabFavoriteIconImageView = tabCustomView.findViewById(R.id.favorite_icon_imageview);
3735 TextView tabTitleTextView = tabCustomView.findViewById(R.id.title_textview);
3737 // Set the default favorite icon as the favorite icon for this tab.
3738 tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(nestedScrollWebView.getFavoriteOrDefaultIcon(), 64, 64, true));
3740 // Set the loading title text.
3741 tabTitleTextView.setText(R.string.loading);
3745 // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
3746 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
3748 // Get a full cursor from `domainsDatabaseHelper`.
3749 Cursor domainNameCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
3751 // Initialize `domainSettingsSet`.
3752 Set<String> domainSettingsSet = new HashSet<>();
3754 // Get the domain name column index.
3755 int domainNameColumnIndex = domainNameCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME);
3757 // Populate `domainSettingsSet`.
3758 for (int i = 0; i < domainNameCursor.getCount(); i++) {
3759 // Move `domainsCursor` to the current row.
3760 domainNameCursor.moveToPosition(i);
3762 // Store the domain name in `domainSettingsSet`.
3763 domainSettingsSet.add(domainNameCursor.getString(domainNameColumnIndex));
3766 // Close `domainNameCursor.
3767 domainNameCursor.close();
3769 // Initialize the domain name in database variable.
3770 String domainNameInDatabase = null;
3772 // Check the hostname against the domain settings set.
3773 if (domainSettingsSet.contains(newHostName)) { // The hostname is contained in the domain settings set.
3774 // Record the domain name in the database.
3775 domainNameInDatabase = newHostName;
3777 // Set the domain settings applied tracker to true.
3778 nestedScrollWebView.setDomainSettingsApplied(true);
3779 } else { // The hostname is not contained in the domain settings set.
3780 // Set the domain settings applied tracker to false.
3781 nestedScrollWebView.setDomainSettingsApplied(false);
3784 // Check all the subdomains of the host name against wildcard domains in the domain cursor.
3785 while (!nestedScrollWebView.getDomainSettingsApplied() && newHostName.contains(".")) { // Stop checking if domain settings are already applied or there are no more `.` in the host name.
3786 if (domainSettingsSet.contains("*." + newHostName)) { // Check the host name prepended by `*.`.
3787 // Set the domain settings applied tracker to true.
3788 nestedScrollWebView.setDomainSettingsApplied(true);
3790 // Store the applied domain names as it appears in the database.
3791 domainNameInDatabase = "*." + newHostName;
3794 // Strip out the lowest subdomain of of the host name.
3795 newHostName = newHostName.substring(newHostName.indexOf(".") + 1);
3799 // Get a handle for the shared preferences.
3800 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
3802 // Store the general preference information.
3803 String defaultFontSizeString = sharedPreferences.getString("font_size", getString(R.string.font_size_default_value));
3804 String defaultUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
3805 boolean defaultSwipeToRefresh = sharedPreferences.getBoolean("swipe_to_refresh", true);
3806 String webViewTheme = sharedPreferences.getString("webview_theme", getString(R.string.webview_theme_default_value));
3807 boolean wideViewport = sharedPreferences.getBoolean("wide_viewport", true);
3808 boolean displayWebpageImages = sharedPreferences.getBoolean("display_webpage_images", true);
3810 // Get the WebView theme entry values string array.
3811 String[] webViewThemeEntryValuesStringArray = getResources().getStringArray(R.array.webview_theme_entry_values);
3813 // Get a handle for the cookie manager.
3814 CookieManager cookieManager = CookieManager.getInstance();
3816 // Initialize the user agent array adapter and string array.
3817 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(this, R.array.user_agent_names, R.layout.spinner_item);
3818 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
3820 if (nestedScrollWebView.getDomainSettingsApplied()) { // The url has custom domain settings.
3821 // Get a cursor for the current host and move it to the first position.
3822 Cursor currentDomainSettingsCursor = domainsDatabaseHelper.getCursorForDomainName(domainNameInDatabase);
3823 currentDomainSettingsCursor.moveToFirst();
3825 // Get the settings from the cursor.
3826 nestedScrollWebView.setDomainSettingsDatabaseId(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper._ID)));
3827 nestedScrollWebView.getSettings().setJavaScriptEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)) == 1);
3828 nestedScrollWebView.setAcceptCookies(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.COOKIES)) == 1);
3829 nestedScrollWebView.getSettings().setDomStorageEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)) == 1);
3830 // Form data can be removed once the minimum API >= 26.
3831 boolean saveFormData = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)) == 1);
3832 nestedScrollWebView.setEasyListEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYLIST)) == 1);
3833 nestedScrollWebView.setEasyPrivacyEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYPRIVACY)) == 1);
3834 nestedScrollWebView.setFanboysAnnoyanceListEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST)) == 1);
3835 nestedScrollWebView.setFanboysSocialBlockingListEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(
3836 DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)) == 1);
3837 nestedScrollWebView.setUltraListEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ULTRALIST)) == 1);
3838 nestedScrollWebView.setUltraPrivacyEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)) == 1);
3839 nestedScrollWebView.setBlockAllThirdPartyRequests(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)) == 1);
3840 String userAgentName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
3841 int fontSize = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
3842 int swipeToRefreshInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH));
3843 int webViewThemeInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.WEBVIEW_THEME));
3844 int wideViewportInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.WIDE_VIEWPORT));
3845 int displayWebpageImagesInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES));
3846 boolean pinnedSslCertificate = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)) == 1);
3847 String pinnedSslIssuedToCName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME));
3848 String pinnedSslIssuedToOName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION));
3849 String pinnedSslIssuedToUName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT));
3850 String pinnedSslIssuedByCName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME));
3851 String pinnedSslIssuedByOName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION));
3852 String pinnedSslIssuedByUName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT));
3853 Date pinnedSslStartDate = new Date(currentDomainSettingsCursor.getLong(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)));
3854 Date pinnedSslEndDate = new Date(currentDomainSettingsCursor.getLong(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)));
3855 boolean pinnedIpAddresses = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_IP_ADDRESSES)) == 1);
3856 String pinnedHostIpAddresses = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.IP_ADDRESSES));
3858 // Close the current host domain settings cursor.
3859 currentDomainSettingsCursor.close();
3861 // If there is a pinned SSL certificate, store it in the WebView.
3862 if (pinnedSslCertificate) {
3863 nestedScrollWebView.setPinnedSslCertificate(pinnedSslIssuedToCName, pinnedSslIssuedToOName, pinnedSslIssuedToUName, pinnedSslIssuedByCName, pinnedSslIssuedByOName, pinnedSslIssuedByUName,
3864 pinnedSslStartDate, pinnedSslEndDate);
3867 // If there is a pinned IP address, store it in the WebView.
3868 if (pinnedIpAddresses) {
3869 nestedScrollWebView.setPinnedIpAddresses(pinnedHostIpAddresses);
3872 // Apply the cookie domain settings.
3873 cookieManager.setAcceptCookie(nestedScrollWebView.getAcceptCookies());
3875 // Apply the form data setting if the API < 26.
3876 if (Build.VERSION.SDK_INT < 26) {
3877 nestedScrollWebView.getSettings().setSaveFormData(saveFormData);
3880 // Apply the font size.
3881 try { // Try the specified font size to see if it is valid.
3882 if (fontSize == 0) { // Apply the default font size.
3883 // Try to set the font size from the value in the app settings.
3884 nestedScrollWebView.getSettings().setTextZoom(Integer.parseInt(defaultFontSizeString));
3885 } else { // Apply the font size from domain settings.
3886 nestedScrollWebView.getSettings().setTextZoom(fontSize);
3888 } catch (Exception exception) { // The specified font size is invalid
3889 // Set the font size to be 100%
3890 nestedScrollWebView.getSettings().setTextZoom(100);
3893 // Set the user agent.
3894 if (userAgentName.equals(getString(R.string.system_default_user_agent))) { // Use the system default user agent.
3895 // Get the array position of the default user agent name.
3896 int defaultUserAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName);
3898 // Set the user agent according to the system default.
3899 switch (defaultUserAgentArrayPosition) {
3900 case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list.
3901 // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names.
3902 nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName);
3905 case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
3906 // Set the user agent to `""`, which uses the default value.
3907 nestedScrollWebView.getSettings().setUserAgentString("");
3910 case SETTINGS_CUSTOM_USER_AGENT:
3911 // Set the default custom user agent.
3912 nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
3916 // Get the user agent string from the user agent data array
3917 nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[defaultUserAgentArrayPosition]);
3919 } else { // Set the user agent according to the stored name.
3920 // Get the array position of the user agent name.
3921 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
3923 switch (userAgentArrayPosition) {
3924 case UNRECOGNIZED_USER_AGENT: // The user agent name contains a custom user agent.
3925 nestedScrollWebView.getSettings().setUserAgentString(userAgentName);
3928 case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
3929 // Set the user agent to `""`, which uses the default value.
3930 nestedScrollWebView.getSettings().setUserAgentString("");
3934 // Get the user agent string from the user agent data array.
3935 nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]);
3939 // Set swipe to refresh.
3940 switch (swipeToRefreshInt) {
3941 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
3942 // Store the swipe to refresh status in the nested scroll WebView.
3943 nestedScrollWebView.setSwipeToRefresh(defaultSwipeToRefresh);
3945 // Update the swipe refresh layout.
3946 if (defaultSwipeToRefresh) { // Swipe to refresh is enabled.
3947 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
3948 swipeRefreshLayout.setEnabled(currentWebView.getScrollY() == 0);
3949 } else { // Swipe to refresh is disabled.
3950 // Disable the swipe refresh layout.
3951 swipeRefreshLayout.setEnabled(false);
3955 case DomainsDatabaseHelper.ENABLED:
3956 // Store the swipe to refresh status in the nested scroll WebView.
3957 nestedScrollWebView.setSwipeToRefresh(true);
3959 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
3960 swipeRefreshLayout.setEnabled(currentWebView.getScrollY() == 0);
3963 case DomainsDatabaseHelper.DISABLED:
3964 // Store the swipe to refresh status in the nested scroll WebView.
3965 nestedScrollWebView.setSwipeToRefresh(false);
3967 // Disable swipe to refresh.
3968 swipeRefreshLayout.setEnabled(false);
3971 // Check to see if WebView themes are supported.
3972 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
3973 // Set the WebView theme.
3974 switch (webViewThemeInt) {
3975 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
3976 // Set the WebView theme. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant.
3977 if (webViewTheme.equals(webViewThemeEntryValuesStringArray[1])) { // The light theme is selected.
3978 // Turn off the WebView dark mode.
3979 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
3980 } else if (webViewTheme.equals(webViewThemeEntryValuesStringArray[2])) { // The dark theme is selected.
3981 // Turn on the WebView dark mode.
3982 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
3983 } else { // The system default theme is selected.
3984 // Get the current system theme status.
3985 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
3987 // Set the WebView theme according to the current system theme status.
3988 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The system is in day mode.
3989 // Turn off the WebView dark mode.
3990 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
3991 } else { // The system is in night mode.
3992 // Turn on the WebView dark mode.
3993 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
3998 case DomainsDatabaseHelper.LIGHT_THEME:
3999 // Turn off the WebView dark mode.
4000 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4003 case DomainsDatabaseHelper.DARK_THEME:
4004 // Turn on the WebView dark mode.
4005 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4010 // Set the viewport.
4011 switch (wideViewportInt) {
4012 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
4013 nestedScrollWebView.getSettings().setUseWideViewPort(wideViewport);
4016 case DomainsDatabaseHelper.ENABLED:
4017 nestedScrollWebView.getSettings().setUseWideViewPort(true);
4020 case DomainsDatabaseHelper.DISABLED:
4021 nestedScrollWebView.getSettings().setUseWideViewPort(false);
4025 // Set the loading of webpage images.
4026 switch (displayWebpageImagesInt) {
4027 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
4028 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImages);
4031 case DomainsDatabaseHelper.ENABLED:
4032 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(true);
4035 case DomainsDatabaseHelper.DISABLED:
4036 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(false);
4040 // Get the current theme status.
4041 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4043 // Set a background on the URL relative layout to indicate that custom domain settings are being used.
4044 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4045 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_light_green, null));
4047 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_dark_blue, null));
4049 } else { // The new URL does not have custom domain settings. Load the defaults.
4050 // Store the values from the shared preferences.
4051 nestedScrollWebView.getSettings().setJavaScriptEnabled(sharedPreferences.getBoolean("javascript", false));
4052 nestedScrollWebView.setAcceptCookies(sharedPreferences.getBoolean(getString(R.string.cookies_key), false));
4053 nestedScrollWebView.getSettings().setDomStorageEnabled(sharedPreferences.getBoolean("dom_storage", false));
4054 boolean saveFormData = sharedPreferences.getBoolean("save_form_data", false); // Form data can be removed once the minimum API >= 26.
4055 nestedScrollWebView.setEasyListEnabled(sharedPreferences.getBoolean("easylist", true));
4056 nestedScrollWebView.setEasyPrivacyEnabled(sharedPreferences.getBoolean("easyprivacy", true));
4057 nestedScrollWebView.setFanboysAnnoyanceListEnabled(sharedPreferences.getBoolean("fanboys_annoyance_list", true));
4058 nestedScrollWebView.setFanboysSocialBlockingListEnabled(sharedPreferences.getBoolean("fanboys_social_blocking_list", true));
4059 nestedScrollWebView.setUltraListEnabled(sharedPreferences.getBoolean("ultralist", true));
4060 nestedScrollWebView.setUltraPrivacyEnabled(sharedPreferences.getBoolean("ultraprivacy", true));
4061 nestedScrollWebView.setBlockAllThirdPartyRequests(sharedPreferences.getBoolean("block_all_third_party_requests", false));
4063 // Apply the default cookie setting.
4064 cookieManager.setAcceptCookie(nestedScrollWebView.getAcceptCookies());
4066 // Apply the default font size setting.
4068 // Try to set the font size from the value in the app settings.
4069 nestedScrollWebView.getSettings().setTextZoom(Integer.parseInt(defaultFontSizeString));
4070 } catch (Exception exception) {
4071 // If the app settings value is invalid, set the font size to 100%.
4072 nestedScrollWebView.getSettings().setTextZoom(100);
4075 // Apply the form data setting if the API < 26.
4076 if (Build.VERSION.SDK_INT < 26) {
4077 nestedScrollWebView.getSettings().setSaveFormData(saveFormData);
4080 // Store the swipe to refresh status in the nested scroll WebView.
4081 nestedScrollWebView.setSwipeToRefresh(defaultSwipeToRefresh);
4083 // Update the swipe refresh layout.
4084 if (defaultSwipeToRefresh) { // Swipe to refresh is enabled.
4085 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
4086 swipeRefreshLayout.setEnabled(currentWebView.getScrollY() == 0);
4087 } else { // Swipe to refresh is disabled.
4088 // Disable the swipe refresh layout.
4089 swipeRefreshLayout.setEnabled(false);
4092 // Reset the pinned variables.
4093 nestedScrollWebView.setDomainSettingsDatabaseId(-1);
4095 // Get the array position of the user agent name.
4096 int userAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName);
4098 // Set the user agent.
4099 switch (userAgentArrayPosition) {
4100 case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list.
4101 // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names.
4102 nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName);
4105 case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
4106 // Set the user agent to `""`, which uses the default value.
4107 nestedScrollWebView.getSettings().setUserAgentString("");
4110 case SETTINGS_CUSTOM_USER_AGENT:
4111 // Set the default custom user agent.
4112 nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
4116 // Get the user agent string from the user agent data array
4117 nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]);
4120 // Apply the WebView theme if supported by the installed WebView.
4121 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
4122 // Set the WebView theme. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant.
4123 if (webViewTheme.equals(webViewThemeEntryValuesStringArray[1])) { // The light theme is selected.
4124 // Turn off the WebView dark mode.
4125 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4126 } else if (webViewTheme.equals(webViewThemeEntryValuesStringArray[2])) { // The dark theme is selected.
4127 // Turn on the WebView dark mode.
4128 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4129 } else { // The system default theme is selected.
4130 // Get the current system theme status.
4131 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4133 // Set the WebView theme according to the current system theme status.
4134 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The system is in day mode.
4135 // Turn off the WebView dark mode.
4136 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4137 } else { // The system is in night mode.
4138 // Turn on the WebView dark mode.
4139 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4144 // Set the viewport.
4145 nestedScrollWebView.getSettings().setUseWideViewPort(wideViewport);
4147 // Set the loading of webpage images.
4148 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImages);
4150 // Set a transparent background on the URL relative layout.
4151 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.color.transparent, null));
4154 // Close the domains database helper.
4155 domainsDatabaseHelper.close();
4157 // Update the privacy icons.
4158 updatePrivacyIcons(true);
4161 // Reload the website if returning from the Domains activity.
4162 if (reloadWebsite) {
4163 nestedScrollWebView.reload();
4166 // Load the URL if directed. This makes sure that the domain settings are properly loaded before the URL. By using `loadUrl()`, instead of `loadUrlFromBase()`, the Referer header will never be sent.
4168 nestedScrollWebView.loadUrl(url, customHeaders);
4172 private void applyProxy(boolean reloadWebViews) {
4173 // Set the proxy according to the mode. `this` refers to the current activity where an alert dialog might be displayed.
4174 ProxyHelper.setProxy(getApplicationContext(), appBarLayout, proxyMode);
4176 // Reset the waiting for proxy tracker.
4177 waitingForProxy = false;
4179 // Get the current theme status.
4180 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4182 // Update the user interface and reload the WebViews if requested.
4183 switch (proxyMode) {
4184 case ProxyHelper.NONE:
4185 // Initialize a color background typed value.
4186 TypedValue colorBackgroundTypedValue = new TypedValue();
4188 // Get the color background from the theme.
4189 getTheme().resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true);
4191 // Get the color background int from the typed value.
4192 int colorBackgroundInt = colorBackgroundTypedValue.data;
4194 // Set the default app bar layout background.
4195 appBarLayout.setBackgroundColor(colorBackgroundInt);
4198 case ProxyHelper.TOR:
4199 // Set the app bar background to indicate proxying through Orbot is enabled.
4200 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4201 appBarLayout.setBackgroundResource(R.color.blue_50);
4203 appBarLayout.setBackgroundResource(R.color.dark_blue_30);
4206 // Check to see if Orbot is installed.
4208 // Get the package manager.
4209 PackageManager packageManager = getPackageManager();
4211 // Check to see if Orbot is in the list. This will throw an error and drop to the catch section if it isn't installed.
4212 packageManager.getPackageInfo("org.torproject.android", 0);
4214 // Check to see if the proxy is ready.
4215 if (!orbotStatus.equals("ON")) { // Orbot is not ready.
4216 // Set the waiting for proxy status.
4217 waitingForProxy = true;
4219 // Show the waiting for proxy dialog if it isn't already displayed.
4220 if (getSupportFragmentManager().findFragmentByTag(getString(R.string.waiting_for_proxy_dialog)) == null) {
4221 // Get a handle for the waiting for proxy alert dialog.
4222 DialogFragment waitingForProxyDialogFragment = new WaitingForProxyDialog();
4224 // Try to show the dialog. Sometimes the window is not yet active if returning from Settings.
4226 // Show the waiting for proxy alert dialog.
4227 waitingForProxyDialogFragment.show(getSupportFragmentManager(), getString(R.string.waiting_for_proxy_dialog));
4228 } catch (Exception waitingForTorException) {
4229 // Add the dialog to the pending dialog array list. It will be displayed in `onStart()`.
4230 pendingDialogsArrayList.add(new PendingDialog(waitingForProxyDialogFragment, getString(R.string.waiting_for_proxy_dialog)));
4234 } catch (PackageManager.NameNotFoundException exception) { // Orbot is not installed.
4235 // Show the Orbot not installed dialog if it is not already displayed.
4236 if (getSupportFragmentManager().findFragmentByTag(getString(R.string.proxy_not_installed_dialog)) == null) {
4237 // Get a handle for the Orbot not installed alert dialog.
4238 DialogFragment orbotNotInstalledDialogFragment = ProxyNotInstalledDialog.displayDialog(proxyMode);
4240 // Try to show the dialog. Sometimes the window is not yet active if returning from Settings.
4242 // Display the Orbot not installed alert dialog.
4243 orbotNotInstalledDialogFragment.show(getSupportFragmentManager(), getString(R.string.proxy_not_installed_dialog));
4244 } catch (Exception orbotNotInstalledException) {
4245 // Add the dialog to the pending dialog array list. It will be displayed in `onStart()`.
4246 pendingDialogsArrayList.add(new PendingDialog(orbotNotInstalledDialogFragment, getString(R.string.proxy_not_installed_dialog)));
4252 case ProxyHelper.I2P:
4253 // Set the app bar background to indicate proxying through Orbot is enabled.
4254 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4255 appBarLayout.setBackgroundResource(R.color.blue_50);
4257 appBarLayout.setBackgroundResource(R.color.dark_blue_30);
4260 // Check to see if I2P is installed.
4262 // Get the package manager.
4263 PackageManager packageManager = getPackageManager();
4265 // Check to see if I2P is in the list. This will throw an error and drop to the catch section if it isn't installed.
4266 packageManager.getPackageInfo("net.i2p.android.router", 0);
4267 } catch (PackageManager.NameNotFoundException exception) { // I2P is not installed.
4268 // Sow the I2P not installed dialog if it is not already displayed.
4269 if (getSupportFragmentManager().findFragmentByTag(getString(R.string.proxy_not_installed_dialog)) == null) {
4270 // Get a handle for the waiting for proxy alert dialog.
4271 DialogFragment i2pNotInstalledDialogFragment = ProxyNotInstalledDialog.displayDialog(proxyMode);
4273 // Try to show the dialog. Sometimes the window is not yet active if returning from Settings.
4275 // Display the I2P not installed alert dialog.
4276 i2pNotInstalledDialogFragment.show(getSupportFragmentManager(), getString(R.string.proxy_not_installed_dialog));
4277 } catch (Exception i2pNotInstalledException) {
4278 // Add the dialog to the pending dialog array list. It will be displayed in `onStart()`.
4279 pendingDialogsArrayList.add(new PendingDialog(i2pNotInstalledDialogFragment, getString(R.string.proxy_not_installed_dialog)));
4285 case ProxyHelper.CUSTOM:
4286 // Set the app bar background to indicate proxying through Orbot is enabled.
4287 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4288 appBarLayout.setBackgroundResource(R.color.blue_50);
4290 appBarLayout.setBackgroundResource(R.color.dark_blue_30);
4295 // Reload the WebViews if requested and not waiting for the proxy.
4296 if (reloadWebViews && !waitingForProxy) {
4297 // Reload the WebViews.
4298 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
4299 // Get the WebView tab fragment.
4300 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
4302 // Get the fragment view.
4303 View fragmentView = webViewTabFragment.getView();
4305 // Only reload the WebViews if they exist.
4306 if (fragmentView != null) {
4307 // Get the nested scroll WebView from the tab fragment.
4308 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
4310 // Reload the WebView.
4311 nestedScrollWebView.reload();
4317 private void updatePrivacyIcons(boolean runInvalidateOptionsMenu) {
4318 // Only update the privacy icons if the options menu and the current WebView have already been populated.
4319 if ((optionsMenu != null) && (currentWebView != null)) {
4320 // Update the privacy icon.
4321 if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScript is enabled.
4322 optionsPrivacyMenuItem.setIcon(R.drawable.javascript_enabled);
4323 } else if (currentWebView.getAcceptCookies()) { // JavaScript is disabled but cookies are enabled.
4324 optionsPrivacyMenuItem.setIcon(R.drawable.warning);
4325 } else { // All the dangerous features are disabled.
4326 optionsPrivacyMenuItem.setIcon(R.drawable.privacy_mode);
4329 // Get the current theme status.
4330 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4332 // Update the cookies icon.
4333 if (currentWebView.getAcceptCookies()) { // Cookies are enabled.
4334 optionsCookiesMenuItem.setIcon(R.drawable.cookies_enabled);
4335 } else { // Cookies are disabled.
4336 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4337 optionsCookiesMenuItem.setIcon(R.drawable.cookies_disabled_day);
4339 optionsCookiesMenuItem.setIcon(R.drawable.cookies_disabled_night);
4343 // Update the refresh icon.
4344 if (optionsRefreshMenuItem.getTitle() == getString(R.string.refresh)) { // The refresh icon is displayed.
4345 // Set the icon according to the theme.
4346 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4347 optionsRefreshMenuItem.setIcon(R.drawable.refresh_enabled_day);
4349 optionsRefreshMenuItem.setIcon(R.drawable.refresh_enabled_night);
4351 } else { // The stop icon is displayed.
4352 // Set the icon according to the theme.
4353 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4354 optionsRefreshMenuItem.setIcon(R.drawable.close_blue_day);
4356 optionsRefreshMenuItem.setIcon(R.drawable.close_blue_night);
4360 // `invalidateOptionsMenu()` calls `onPrepareOptionsMenu()` and redraws the icons in the app bar.
4361 if (runInvalidateOptionsMenu) {
4362 invalidateOptionsMenu();
4367 private void highlightUrlText() {
4368 // Only highlight the URL text if the box is not currently selected.
4369 if (!urlEditText.hasFocus()) {
4370 // Get the URL string.
4371 String urlString = urlEditText.getText().toString();
4373 // Highlight the URL according to the protocol.
4374 if (urlString.startsWith("file://") || urlString.startsWith("content://")) { // This is a file or content URL.
4375 // De-emphasize everything before the file name.
4376 urlEditText.getText().setSpan(initialGrayColorSpan, 0, urlString.lastIndexOf("/") + 1,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4377 } else { // This is a web URL.
4378 // Get the index of the `/` immediately after the domain name.
4379 int endOfDomainName = urlString.indexOf("/", (urlString.indexOf("//") + 2));
4381 // Create a base URL string.
4384 // Get the base URL.
4385 if (endOfDomainName > 0) { // There is at least one character after the base URL.
4386 // Get the base URL.
4387 baseUrl = urlString.substring(0, endOfDomainName);
4388 } else { // There are no characters after the base URL.
4389 // Set the base URL to be the entire URL string.
4390 baseUrl = urlString;
4393 // Get the index of the last `.` in the domain.
4394 int lastDotIndex = baseUrl.lastIndexOf(".");
4396 // Get the index of the penultimate `.` in the domain.
4397 int penultimateDotIndex = baseUrl.lastIndexOf(".", lastDotIndex - 1);
4399 // Markup the beginning of the URL.
4400 if (urlString.startsWith("http://")) { // Highlight the protocol of connections that are not encrypted.
4401 urlEditText.getText().setSpan(redColorSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4403 // De-emphasize subdomains.
4404 if (penultimateDotIndex > 0) { // There is more than one subdomain in the domain name.
4405 urlEditText.getText().setSpan(initialGrayColorSpan, 7, penultimateDotIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4407 } else if (urlString.startsWith("https://")) { // De-emphasize the protocol of connections that are encrypted.
4408 if (penultimateDotIndex > 0) { // There is more than one subdomain in the domain name.
4409 // De-emphasize the protocol and the additional subdomains.
4410 urlEditText.getText().setSpan(initialGrayColorSpan, 0, penultimateDotIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4411 } else { // There is only one subdomain in the domain name.
4412 // De-emphasize only the protocol.
4413 urlEditText.getText().setSpan(initialGrayColorSpan, 0, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4417 // De-emphasize the text after the domain name.
4418 if (endOfDomainName > 0) {
4419 urlEditText.getText().setSpan(finalGrayColorSpan, endOfDomainName, urlString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4425 private void loadBookmarksFolder() {
4426 // Update the bookmarks cursor with the contents of the bookmarks database for the current folder.
4427 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
4429 // Populate the bookmarks cursor adapter.
4430 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
4432 public View newView(Context context, Cursor cursor, ViewGroup parent) {
4433 // Inflate the individual item layout.
4434 return getLayoutInflater().inflate(R.layout.bookmarks_drawer_item_linearlayout, parent, false);
4438 public void bindView(View view, Context context, Cursor cursor) {
4439 // Get handles for the views.
4440 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmark_favorite_icon);
4441 TextView bookmarkNameTextView = view.findViewById(R.id.bookmark_name);
4443 // Get the favorite icon byte array from the cursor.
4444 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
4446 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
4447 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
4449 // Display the bitmap in `bookmarkFavoriteIcon`.
4450 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
4452 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
4453 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
4454 bookmarkNameTextView.setText(bookmarkNameString);
4456 // Make the font bold for folders.
4457 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {
4458 bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD);
4459 } else { // Reset the font to default for normal bookmarks.
4460 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
4465 // Get a handle for the bookmarks list view.
4466 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
4468 // Populate the list view with the adapter.
4469 bookmarksListView.setAdapter(bookmarksCursorAdapter);
4471 // Get a handle for the bookmarks title text view.
4472 TextView bookmarksTitleTextView = findViewById(R.id.bookmarks_title_textview);
4474 // Set the bookmarks drawer title.
4475 if (currentBookmarksFolder.isEmpty()) {
4476 bookmarksTitleTextView.setText(R.string.bookmarks);
4478 bookmarksTitleTextView.setText(currentBookmarksFolder);
4482 private void openWithApp(String url) {
4483 // Create an open with app intent with `ACTION_VIEW`.
4484 Intent openWithAppIntent = new Intent(Intent.ACTION_VIEW);
4486 // Set the URI but not the MIME type. This should open all available apps.
4487 openWithAppIntent.setData(Uri.parse(url));
4489 // Flag the intent to open in a new task.
4490 openWithAppIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4494 // Show the chooser.
4495 startActivity(openWithAppIntent);
4496 } catch (ActivityNotFoundException exception) { // There are no apps available to open the URL.
4497 // Show a snackbar with the error.
4498 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
4502 private void openWithBrowser(String url) {
4503 // Create an open with browser intent with `ACTION_VIEW`.
4504 Intent openWithBrowserIntent = new Intent(Intent.ACTION_VIEW);
4506 // Set the URI and the MIME type. `"text/html"` should load browser options.
4507 openWithBrowserIntent.setDataAndType(Uri.parse(url), "text/html");
4509 // Flag the intent to open in a new task.
4510 openWithBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4514 // Show the chooser.
4515 startActivity(openWithBrowserIntent);
4516 } catch (ActivityNotFoundException exception) { // There are no browsers available to open the URL.
4517 // Show a snackbar with the error.
4518 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
4522 private String sanitizeUrl(String url) {
4523 // Sanitize Google Analytics.
4524 if (sanitizeGoogleAnalytics) {
4526 if (url.contains("?utm_")) {
4527 url = url.substring(0, url.indexOf("?utm_"));
4531 if (url.contains("&utm_")) {
4532 url = url.substring(0, url.indexOf("&utm_"));
4536 // Sanitize Facebook Click IDs.
4537 if (sanitizeFacebookClickIds) {
4538 // Remove `?fbclid=`.
4539 if (url.contains("?fbclid=")) {
4540 url = url.substring(0, url.indexOf("?fbclid="));
4543 // Remove `&fbclid=`.
4544 if (url.contains("&fbclid=")) {
4545 url = url.substring(0, url.indexOf("&fbclid="));
4548 // Remove `?fbadid=`.
4549 if (url.contains("?fbadid=")) {
4550 url = url.substring(0, url.indexOf("?fbadid="));
4553 // Remove `&fbadid=`.
4554 if (url.contains("&fbadid=")) {
4555 url = url.substring(0, url.indexOf("&fbadid="));
4559 // Sanitize Twitter AMP redirects.
4560 if (sanitizeTwitterAmpRedirects) {
4562 if (url.contains("?amp=1")) {
4563 url = url.substring(0, url.indexOf("?amp=1"));
4567 // Return the sanitized URL.
4571 public void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
4572 // Store the blocklists.
4573 easyList = combinedBlocklists.get(0);
4574 easyPrivacy = combinedBlocklists.get(1);
4575 fanboysAnnoyanceList = combinedBlocklists.get(2);
4576 fanboysSocialList = combinedBlocklists.get(3);
4577 ultraList = combinedBlocklists.get(4);
4578 ultraPrivacy = combinedBlocklists.get(5);
4580 // Check to see if the activity has been restarted with a saved state.
4581 if ((savedStateArrayList == null) || (savedStateArrayList.size() == 0)) { // The activity has not been restarted or it was restarted on start to force the night theme.
4582 // Add the first tab.
4583 addNewTab("", true);
4584 } else { // The activity has been restarted.
4585 // Restore each tab. Once the minimum API >= 24, a `forEach()` command can be used.
4586 for (int i = 0; i < savedStateArrayList.size(); i++) {
4588 tabLayout.addTab(tabLayout.newTab());
4591 TabLayout.Tab newTab = tabLayout.getTabAt(i);
4593 // Remove the lint warning below that the current tab might be null.
4594 assert newTab != null;
4596 // Set a custom view on the new tab.
4597 newTab.setCustomView(R.layout.tab_custom_view);
4599 // Add the new page.
4600 webViewPagerAdapter.restorePage(savedStateArrayList.get(i), savedNestedScrollWebViewStateArrayList.get(i));
4603 // Reset the saved state variables.
4604 savedStateArrayList = null;
4605 savedNestedScrollWebViewStateArrayList = null;
4607 // Restore the selected tab position.
4608 if (savedTabPosition == 0) { // The first tab is selected.
4609 // Set the first page as the current WebView.
4610 setCurrentWebView(0);
4611 } else { // the first tab is not selected.
4612 // Move to the selected tab.
4613 webViewPager.setCurrentItem(savedTabPosition);
4616 // Get the intent that started the app.
4617 Intent intent = getIntent();
4619 // Reset the intent. This prevents a duplicate tab from being created on restart.
4620 setIntent(new Intent());
4622 // Get the information from the intent.
4623 String intentAction = intent.getAction();
4624 Uri intentUriData = intent.getData();
4625 String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
4627 // Determine if this is a web search.
4628 boolean isWebSearch = ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH));
4630 // Only process the URI if it contains data or it is a web search. If the user pressed the desktop icon after the app was already running the URI will be null.
4631 if (intentUriData != null || intentStringExtra != null || isWebSearch) {
4632 // Get the shared preferences.
4633 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
4635 // Create a URL string.
4638 // If the intent action is a web search, perform the search.
4639 if (isWebSearch) { // The intent is a web search.
4640 // Create an encoded URL string.
4641 String encodedUrlString;
4643 // Sanitize the search input and convert it to a search.
4645 encodedUrlString = URLEncoder.encode(intent.getStringExtra(SearchManager.QUERY), "UTF-8");
4646 } catch (UnsupportedEncodingException exception) {
4647 encodedUrlString = "";
4650 // Add the base search URL.
4651 url = searchURL + encodedUrlString;
4652 } else if (intentUriData != null) { // The intent contains a URL formatted as a URI.
4653 // Set the intent data as the URL.
4654 url = intentUriData.toString();
4655 } else { // The intent contains a string, which might be a URL.
4656 // Set the intent string as the URL.
4657 url = intentStringExtra;
4660 // Add a new tab if specified in the preferences.
4661 if (sharedPreferences.getBoolean("open_intents_in_new_tab", true)) { // Load the URL in a new tab.
4662 // Set the loading new intent flag.
4663 loadingNewIntent = true;
4666 addNewTab(url, true);
4667 } else { // Load the URL in the current tab.
4669 loadUrl(currentWebView, url);
4675 public void addTab(View view) {
4676 // Add a new tab with a blank URL.
4677 addNewTab("", true);
4680 private void addNewTab(String url, boolean moveToTab) {
4681 // Clear the focus from the URL edit text, so that it will be populated with the information from the new tab.
4682 urlEditText.clearFocus();
4684 // Get the new page number. The page numbers are 0 indexed, so the new page number will match the current count.
4685 int newTabNumber = tabLayout.getTabCount();
4688 tabLayout.addTab(tabLayout.newTab());
4691 TabLayout.Tab newTab = tabLayout.getTabAt(newTabNumber);
4693 // Remove the lint warning below that the current tab might be null.
4694 assert newTab != null;
4696 // Set a custom view on the new tab.
4697 newTab.setCustomView(R.layout.tab_custom_view);
4699 // Add the new WebView page.
4700 webViewPagerAdapter.addPage(newTabNumber, webViewPager, url, moveToTab);
4702 // Show the app bar if it is at the bottom of the screen and the new tab is taking focus.
4703 if (bottomAppBar && moveToTab) {
4704 appBarLayout.setVisibility(View.VISIBLE);
4708 public void closeTab(View view) {
4709 // Run the command according to the number of tabs.
4710 if (tabLayout.getTabCount() > 1) { // There is more than one tab open.
4711 // Close the current tab.
4713 } else { // There is only one tab open.
4718 private void closeCurrentTab() {
4719 // Get the current tab number.
4720 int currentTabNumber = tabLayout.getSelectedTabPosition();
4722 // Delete the current tab.
4723 tabLayout.removeTabAt(currentTabNumber);
4725 // Delete the current page. If the selected page number did not change during the delete (because the newly selected tab has has same number as the previously deleted tab), it will return true,
4726 // meaning that the current WebView must be reset. Otherwise it will happen automatically as the selected tab number changes.
4727 if (webViewPagerAdapter.deletePage(currentTabNumber, webViewPager)) {
4728 setCurrentWebView(currentTabNumber);
4732 private void exitFullScreenVideo() {
4733 // Re-enable the screen timeout.
4734 fullScreenVideoFrameLayout.setKeepScreenOn(false);
4736 // Unset the full screen video flag.
4737 displayingFullScreenVideo = false;
4739 // Remove all the views from the full screen video frame layout.
4740 fullScreenVideoFrameLayout.removeAllViews();
4742 // Hide the full screen video frame layout.
4743 fullScreenVideoFrameLayout.setVisibility(View.GONE);
4745 // Enable the sliding drawers.
4746 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
4748 // Show the coordinator layout.
4749 coordinatorLayout.setVisibility(View.VISIBLE);
4751 // Apply the appropriate full screen mode flags.
4752 if (fullScreenBrowsingModeEnabled && inFullScreenBrowsingMode) { // Privacy Browser is currently in full screen browsing mode.
4753 // Hide the app bar if specified.
4755 // Hide the tab linear layout.
4756 tabsLinearLayout.setVisibility(View.GONE);
4758 // Hide the action bar.
4762 /* Hide the system bars.
4763 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
4764 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
4765 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
4766 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
4768 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
4769 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
4770 } else { // Switch to normal viewing mode.
4771 // Remove the `SYSTEM_UI` flags from the root frame layout.
4772 rootFrameLayout.setSystemUiVisibility(0);
4776 private void clearAndExit() {
4777 // Get a handle for the shared preferences.
4778 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
4780 // Close the bookmarks cursor and database.
4781 bookmarksCursor.close();
4782 bookmarksDatabaseHelper.close();
4784 // Get the status of the clear everything preference.
4785 boolean clearEverything = sharedPreferences.getBoolean("clear_everything", true);
4787 // Get a handle for the runtime.
4788 Runtime runtime = Runtime.getRuntime();
4790 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
4791 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
4792 String privateDataDirectoryString = getApplicationInfo().dataDir;
4795 if (clearEverything || sharedPreferences.getBoolean("clear_cookies", true)) {
4796 // The command to remove cookies changed slightly in API 21.
4797 if (Build.VERSION.SDK_INT >= 21) {
4798 CookieManager.getInstance().removeAllCookies(null);
4800 CookieManager.getInstance().removeAllCookie();
4803 // Manually delete the cookies database, as `CookieManager` sometimes will not flush its changes to disk before `System.exit(0)` is run.
4805 // Two commands must be used because `Runtime.exec()` does not like `*`.
4806 Process deleteCookiesProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies");
4807 Process deleteCookiesJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies-journal");
4809 // Wait until the processes have finished.
4810 deleteCookiesProcess.waitFor();
4811 deleteCookiesJournalProcess.waitFor();
4812 } catch (Exception exception) {
4813 // Do nothing if an error is thrown.
4817 // Clear DOM storage.
4818 if (clearEverything || sharedPreferences.getBoolean("clear_dom_storage", true)) {
4819 // Ask `WebStorage` to clear the DOM storage.
4820 WebStorage webStorage = WebStorage.getInstance();
4821 webStorage.deleteAllData();
4823 // Manually delete the DOM storage files and directories, as `WebStorage` sometimes will not flush its changes to disk before `System.exit(0)` is run.
4825 // A `String[]` must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
4826 Process deleteLocalStorageProcess = runtime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
4828 // Multiple commands must be used because `Runtime.exec()` does not like `*`.
4829 Process deleteIndexProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
4830 Process deleteQuotaManagerProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
4831 Process deleteQuotaManagerJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
4832 Process deleteDatabaseProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
4834 // Wait until the processes have finished.
4835 deleteLocalStorageProcess.waitFor();
4836 deleteIndexProcess.waitFor();
4837 deleteQuotaManagerProcess.waitFor();
4838 deleteQuotaManagerJournalProcess.waitFor();
4839 deleteDatabaseProcess.waitFor();
4840 } catch (Exception exception) {
4841 // Do nothing if an error is thrown.
4845 // Clear form data if the API < 26.
4846 if ((Build.VERSION.SDK_INT < 26) && (clearEverything || sharedPreferences.getBoolean("clear_form_data", true))) {
4847 WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
4848 webViewDatabase.clearFormData();
4850 // Manually delete the form data database, as `WebViewDatabase` sometimes will not flush its changes to disk before `System.exit(0)` is run.
4852 // A string array must be used because the database contains a space and `Runtime.exec` will not otherwise escape the string correctly.
4853 Process deleteWebDataProcess = runtime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data"});
4854 Process deleteWebDataJournalProcess = runtime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data-journal"});
4856 // Wait until the processes have finished.
4857 deleteWebDataProcess.waitFor();
4858 deleteWebDataJournalProcess.waitFor();
4859 } catch (Exception exception) {
4860 // Do nothing if an error is thrown.
4864 // Clear the logcat.
4865 if (clearEverything || sharedPreferences.getBoolean(getString(R.string.clear_logcat_key), true)) {
4867 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
4868 Process process = Runtime.getRuntime().exec("logcat -b all -c");
4870 // Wait for the process to finish.
4872 } catch (IOException|InterruptedException exception) {
4878 if (clearEverything || sharedPreferences.getBoolean("clear_cache", true)) {
4879 // Clear the cache from each WebView.
4880 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
4881 // Get the WebView tab fragment.
4882 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
4884 // Get the WebView fragment view.
4885 View webViewFragmentView = webViewTabFragment.getView();
4887 // Only clear the cache if the WebView exists.
4888 if (webViewFragmentView != null) {
4889 // Get the nested scroll WebView from the tab fragment.
4890 NestedScrollWebView nestedScrollWebView = webViewFragmentView.findViewById(R.id.nestedscroll_webview);
4892 // Clear the cache for this WebView.
4893 nestedScrollWebView.clearCache(true);
4897 // Manually delete the cache directories.
4899 // Delete the main cache directory.
4900 Process deleteCacheProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/cache");
4902 // Delete the secondary `Service Worker` cache directory.
4903 // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
4904 Process deleteServiceWorkerProcess = runtime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Service Worker/"});
4906 // Wait until the processes have finished.
4907 deleteCacheProcess.waitFor();
4908 deleteServiceWorkerProcess.waitFor();
4909 } catch (Exception exception) {
4910 // Do nothing if an error is thrown.
4914 // Wipe out each WebView.
4915 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
4916 // Get the WebView tab fragment.
4917 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
4919 // Get the WebView frame layout.
4920 FrameLayout webViewFrameLayout = (FrameLayout) webViewTabFragment.getView();
4922 // Only wipe out the WebView if it exists.
4923 if (webViewFrameLayout != null) {
4924 // Get the nested scroll WebView from the tab fragment.
4925 NestedScrollWebView nestedScrollWebView = webViewFrameLayout.findViewById(R.id.nestedscroll_webview);
4927 // Clear SSL certificate preferences for this WebView.
4928 nestedScrollWebView.clearSslPreferences();
4930 // Clear the back/forward history for this WebView.
4931 nestedScrollWebView.clearHistory();
4933 // Remove all the views from the frame layout.
4934 webViewFrameLayout.removeAllViews();
4936 // Destroy the internal state of the WebView.
4937 nestedScrollWebView.destroy();
4941 // Clear the custom headers.
4942 customHeaders.clear();
4944 // Manually delete the `app_webview` folder, which contains the cookies, DOM storage, form data, and `Service Worker` cache.
4945 // See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`.
4946 if (clearEverything) {
4948 // Delete the folder.
4949 Process deleteAppWebviewProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview");
4951 // Wait until the process has finished.
4952 deleteAppWebviewProcess.waitFor();
4953 } catch (Exception exception) {
4954 // Do nothing if an error is thrown.
4958 // Close Privacy Browser. `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
4959 if (Build.VERSION.SDK_INT >= 21) {
4960 finishAndRemoveTask();
4965 // Remove the terminated program from RAM. The status code is `0`.
4969 public void bookmarksBack(View view) {
4970 if (currentBookmarksFolder.isEmpty()) { // The home folder is displayed.
4971 // close the bookmarks drawer.
4972 drawerLayout.closeDrawer(GravityCompat.END);
4973 } else { // A subfolder is displayed.
4974 // Place the former parent folder in `currentFolder`.
4975 currentBookmarksFolder = bookmarksDatabaseHelper.getParentFolderName(currentBookmarksFolder);
4977 // Load the new folder.
4978 loadBookmarksFolder();
4982 private void setCurrentWebView(int pageNumber) {
4983 // Stop the swipe to refresh indicator if it is running
4984 swipeRefreshLayout.setRefreshing(false);
4986 // Get the WebView tab fragment.
4987 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(pageNumber);
4989 // Get the fragment view.
4990 View webViewFragmentView = webViewTabFragment.getView();
4992 // Set the current WebView if the fragment view is not null.
4993 if (webViewFragmentView != null) { // The fragment has been populated.
4994 // Store the current WebView.
4995 currentWebView = webViewFragmentView.findViewById(R.id.nestedscroll_webview);
4997 // Update the status of swipe to refresh.
4998 if (currentWebView.getSwipeToRefresh()) { // Swipe to refresh is enabled.
4999 // Enable the swipe refresh layout if the WebView is scrolled all the way to the top. It is updated every time the scroll changes.
5000 swipeRefreshLayout.setEnabled(currentWebView.getScrollY() == 0);
5001 } else { // Swipe to refresh is disabled.
5002 // Disable the swipe refresh layout.
5003 swipeRefreshLayout.setEnabled(false);
5006 // Get a handle for the cookie manager.
5007 CookieManager cookieManager = CookieManager.getInstance();
5009 // Set the cookie status.
5010 cookieManager.setAcceptCookie(currentWebView.getAcceptCookies());
5012 // Update the privacy icons. `true` redraws the icons in the app bar.
5013 updatePrivacyIcons(true);
5015 // Get a handle for the input method manager.
5016 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
5018 // Remove the lint warning below that the input method manager might be null.
5019 assert inputMethodManager != null;
5021 // Get the current URL.
5022 String url = currentWebView.getUrl();
5024 // Update the URL edit text if not loading a new intent. Otherwise, this will be handled by `onPageStarted()` (if called) and `onPageFinished()`.
5025 if (!loadingNewIntent) { // A new intent is not being loaded.
5026 if ((url == null) || url.equals("about:blank")) { // The WebView is blank.
5027 // Display the hint in the URL edit text.
5028 urlEditText.setText("");
5030 // Request focus for the URL text box.
5031 urlEditText.requestFocus();
5033 // Display the keyboard.
5034 inputMethodManager.showSoftInput(urlEditText, 0);
5035 } else { // The WebView has a loaded URL.
5036 // Clear the focus from the URL text box.
5037 urlEditText.clearFocus();
5039 // Hide the soft keyboard.
5040 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
5042 // Display the current URL in the URL text box.
5043 urlEditText.setText(url);
5045 // Highlight the URL text.
5048 } else { // A new intent is being loaded.
5049 // Reset the loading new intent tracker.
5050 loadingNewIntent = false;
5053 // Set the background to indicate the domain settings status.
5054 if (currentWebView.getDomainSettingsApplied()) {
5055 // Get the current theme status.
5056 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
5058 // Set a green background on the URL relative layout to indicate that custom domain settings are being used.
5059 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
5060 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_light_green, null));
5062 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_dark_blue, null));
5065 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.color.transparent, null));
5067 } else { // The fragment has not been populated. Try again in 100 milliseconds.
5068 // Create a handler to set the current WebView.
5069 Handler setCurrentWebViewHandler = new Handler();
5071 // Create a runnable to set the current WebView.
5072 Runnable setCurrentWebWebRunnable = () -> {
5073 // Set the current WebView.
5074 setCurrentWebView(pageNumber);
5077 // Try setting the current WebView again after 100 milliseconds.
5078 setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 100);
5082 @SuppressLint("ClickableViewAccessibility")
5084 public void initializeWebView(NestedScrollWebView nestedScrollWebView, int pageNumber, ProgressBar progressBar, String url, Boolean restoringState) {
5085 // Get a handle for the shared preferences.
5086 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
5088 // Get the WebView theme.
5089 String webViewTheme = sharedPreferences.getString("webview_theme", getString(R.string.webview_theme_default_value));
5091 // Get the WebView theme entry values string array.
5092 String[] webViewThemeEntryValuesStringArray = getResources().getStringArray(R.array.webview_theme_entry_values);
5094 // Apply the WebView theme if supported by the installed WebView.
5095 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
5096 // Set the WebView theme. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant.
5097 if (webViewTheme.equals(webViewThemeEntryValuesStringArray[1])) { // The light theme is selected.
5098 // Turn off the WebView dark mode.
5099 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
5101 // Make the WebView visible. The WebView was created invisible in `webview_framelayout` to prevent a white background splash in night mode.
5102 // If the system is currently in night mode, showing the WebView will be handled in `onProgressChanged()`.
5103 nestedScrollWebView.setVisibility(View.VISIBLE);
5104 } else if (webViewTheme.equals(webViewThemeEntryValuesStringArray[2])) { // The dark theme is selected.
5105 // Turn on the WebView dark mode.
5106 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
5107 } else { // The system default theme is selected.
5108 // Get the current system theme status.
5109 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
5111 // Set the WebView theme according to the current system theme status.
5112 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The system is in day mode.
5113 // Turn off the WebView dark mode.
5114 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
5116 // Make the WebView visible. The WebView was created invisible in `webview_framelayout` to prevent a white background splash in night mode.
5117 // If the system is currently in night mode, showing the WebView will be handled in `onProgressChanged()`.
5118 nestedScrollWebView.setVisibility(View.VISIBLE);
5119 } else { // The system is in night mode.
5120 // Turn on the WebView dark mode.
5121 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
5126 // Get a handle for the activity
5127 Activity activity = this;
5129 // Get a handle for the input method manager.
5130 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
5132 // Instantiate the blocklist helper.
5133 BlocklistHelper blocklistHelper = new BlocklistHelper();
5135 // Remove the lint warning below that the input method manager might be null.
5136 assert inputMethodManager != null;
5138 // Set the app bar scrolling.
5139 nestedScrollWebView.setNestedScrollingEnabled(sharedPreferences.getBoolean("scroll_app_bar", true));
5141 // Allow pinch to zoom.
5142 nestedScrollWebView.getSettings().setBuiltInZoomControls(true);
5144 // Hide zoom controls.
5145 nestedScrollWebView.getSettings().setDisplayZoomControls(false);
5147 // Don't allow mixed content (HTTP and HTTPS) on the same website.
5148 if (Build.VERSION.SDK_INT >= 21) {
5149 nestedScrollWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
5152 // Set the WebView to load in overview mode (zoomed out to the maximum width).
5153 nestedScrollWebView.getSettings().setLoadWithOverviewMode(true);
5155 // Explicitly disable geolocation.
5156 nestedScrollWebView.getSettings().setGeolocationEnabled(false);
5158 // Allow loading of file:// URLs. This is necessary for opening MHT web archives, which are copies into a temporary cache location.
5159 nestedScrollWebView.getSettings().setAllowFileAccess(true);
5161 // Create a double-tap gesture detector to toggle full-screen mode.
5162 GestureDetector doubleTapGestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
5163 // Override `onDoubleTap()`. All other events are handled using the default settings.
5165 public boolean onDoubleTap(MotionEvent event) {
5166 if (fullScreenBrowsingModeEnabled) { // Only process the double-tap if full screen browsing mode is enabled.
5167 // Toggle the full screen browsing mode tracker.
5168 inFullScreenBrowsingMode = !inFullScreenBrowsingMode;
5170 // Toggle the full screen browsing mode.
5171 if (inFullScreenBrowsingMode) { // Switch to full screen mode.
5172 // Hide the app bar if specified.
5174 // Close the find on page bar if it is visible.
5175 closeFindOnPage(null);
5177 // Hide the tab linear layout.
5178 tabsLinearLayout.setVisibility(View.GONE);
5180 // Hide the action bar.
5183 // Set layout and scrolling parameters if the app bar is at the top of the screen.
5184 if (!bottomAppBar) {
5185 // Check to see if the app bar is normally scrolled.
5186 if (scrollAppBar) { // The app bar is scrolled when it is displayed.
5187 // Get the swipe refresh layout parameters.
5188 CoordinatorLayout.LayoutParams swipeRefreshLayoutParams = (CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams();
5190 // Remove the off-screen scrolling layout.
5191 swipeRefreshLayoutParams.setBehavior(null);
5192 } else { // The app bar is not scrolled when it is displayed.
5193 // Remove the padding from the top of the swipe refresh layout.
5194 swipeRefreshLayout.setPadding(0, 0, 0, 0);
5196 // The swipe refresh circle must be moved above the now removed status bar location.
5197 swipeRefreshLayout.setProgressViewOffset(false, -200, defaultProgressViewEndOffset);
5202 /* Hide the system bars.
5203 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5204 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5205 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5206 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5208 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5209 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5210 } else { // Switch to normal viewing mode.
5211 // Show the app bar if it was hidden.
5213 // Show the tab linear layout.
5214 tabsLinearLayout.setVisibility(View.VISIBLE);
5216 // Show the action bar.
5219 // Set layout and scrolling parameters if the app bar is at the top of the screen.
5220 if (!bottomAppBar) {
5221 // Check to see if the app bar is normally scrolled.
5222 if (scrollAppBar) { // The app bar is scrolled when it is displayed.
5223 // Get the swipe refresh layout parameters.
5224 CoordinatorLayout.LayoutParams swipeRefreshLayoutParams = (CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams();
5226 // Add the off-screen scrolling layout.
5227 swipeRefreshLayoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior());
5228 } else { // The app bar is not scrolled when it is displayed.
5229 // The swipe refresh layout must be manually moved below the app bar layout.
5230 swipeRefreshLayout.setPadding(0, appBarHeight, 0, 0);
5232 // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
5233 swipeRefreshLayout.setProgressViewOffset(false, defaultProgressViewStartOffset - 10 + appBarHeight, defaultProgressViewEndOffset + appBarHeight);
5238 // Remove the `SYSTEM_UI` flags from the root frame layout.
5239 rootFrameLayout.setSystemUiVisibility(0);
5242 // Consume the double-tap.
5244 } else { // Do not consume the double-tap because full screen browsing mode is disabled.
5250 // Pass all touch events on the WebView through the double-tap gesture detector.
5251 nestedScrollWebView.setOnTouchListener((View view, MotionEvent event) -> {
5252 // Call `performClick()` on the view, which is required for accessibility.
5253 view.performClick();
5255 // Send the event to the gesture detector.
5256 return doubleTapGestureDetector.onTouchEvent(event);
5259 // Register the WebView for a context menu. This is used to see link targets and download images.
5260 registerForContextMenu(nestedScrollWebView);
5262 // Allow the downloading of files.
5263 nestedScrollWebView.setDownloadListener((String downloadUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) -> {
5264 // Check the download preference.
5265 if (downloadWithExternalApp) { // Download with an external app.
5266 downloadUrlWithExternalApp(downloadUrl);
5267 } else { // Handle the download inside of Privacy Browser.
5268 // Define a formatted file size string.
5269 String formattedFileSizeString;
5271 // Process the content length if it contains data.
5272 if (contentLength > 0) { // The content length is greater than 0.
5273 // Format the content length as a string.
5274 formattedFileSizeString = NumberFormat.getInstance().format(contentLength) + " " + getString(R.string.bytes);
5275 } else { // The content length is not greater than 0.
5276 // Set the formatted file size string to be `unknown size`.
5277 formattedFileSizeString = getString(R.string.unknown_size);
5280 // Get the file name from the content disposition.
5281 String fileNameString = PrepareSaveDialog.getFileNameFromHeaders(this, contentDisposition, mimetype, downloadUrl);
5283 // Instantiate the save dialog.
5284 DialogFragment saveDialogFragment = SaveDialog.saveUrl(downloadUrl, formattedFileSizeString, fileNameString, userAgent,
5285 nestedScrollWebView.getAcceptCookies());
5287 // Try to show the dialog. The download listener continues to function even when the WebView is paused. Attempting to display a dialog in that state leads to a crash.
5289 // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name.
5290 saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog));
5291 } catch (Exception exception) { // The dialog could not be shown.
5292 // Add the dialog to the pending dialog array list. It will be displayed in `onStart()`.
5293 pendingDialogsArrayList.add(new PendingDialog(saveDialogFragment, getString(R.string.save_dialog)));
5298 // Update the find on page count.
5299 nestedScrollWebView.setFindListener(new WebView.FindListener() {
5300 // Get a handle for `findOnPageCountTextView`.
5301 final TextView findOnPageCountTextView = findViewById(R.id.find_on_page_count_textview);
5304 public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
5305 if ((isDoneCounting) && (numberOfMatches == 0)) { // There are no matches.
5306 // Set `findOnPageCountTextView` to `0/0`.
5307 findOnPageCountTextView.setText(R.string.zero_of_zero);
5308 } else if (isDoneCounting) { // There are matches.
5309 // `activeMatchOrdinal` is zero-based.
5310 int activeMatch = activeMatchOrdinal + 1;
5312 // Build the match string.
5313 String matchString = activeMatch + "/" + numberOfMatches;
5315 // Set `findOnPageCountTextView`.
5316 findOnPageCountTextView.setText(matchString);
5321 // Update the status of swipe to refresh based on the scroll position of the nested scroll WebView. Also reinforce full screen browsing mode.
5322 // On API < 23, `getViewTreeObserver().addOnScrollChangedListener()` must be used, but it is a little bit buggy and appears to get garbage collected from time to time.
5323 if (Build.VERSION.SDK_INT >= 23) {
5324 nestedScrollWebView.setOnScrollChangeListener((view, scrollX, scrollY, oldScrollX, oldScrollY) -> {
5325 // Set the swipe to refresh status.
5326 if (nestedScrollWebView.getSwipeToRefresh()) {
5327 // Only enable swipe to refresh if the WebView is scrolled to the top.
5328 swipeRefreshLayout.setEnabled(nestedScrollWebView.getScrollY() == 0);
5330 // Disable swipe to refresh.
5331 swipeRefreshLayout.setEnabled(false);
5334 // Set the visibility of the bottom app bar.
5335 if (bottomAppBar && scrollAppBar && (Calendar.getInstance().getTimeInMillis() - lastScrollUpdate > 100)) {
5336 if (scrollY - oldScrollY > 25) { // The WebView was scrolled down.
5337 appBarLayout.setVisibility(View.GONE);
5338 } else if (scrollY - oldScrollY < -25) { // The WebView was scrolled up.
5339 appBarLayout.setVisibility(View.VISIBLE);
5342 // Update the last scroll update variable. This prevents the app bar from flashing on and off at the bottom of the screen.
5343 lastScrollUpdate = Calendar.getInstance().getTimeInMillis();
5346 // Reinforce the system UI visibility flags if in full screen browsing mode.
5347 // This hides the status and navigation bars, which are displayed if other elements are shown, like dialog boxes, the options menu, or the keyboard.
5348 if (inFullScreenBrowsingMode) {
5349 /* Hide the system bars.
5350 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5351 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5352 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5353 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5355 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5356 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5360 nestedScrollWebView.getViewTreeObserver().addOnScrollChangedListener(() -> {
5361 if (nestedScrollWebView.getSwipeToRefresh()) {
5362 // Only enable swipe to refresh if the WebView is scrolled to the top.
5363 swipeRefreshLayout.setEnabled(nestedScrollWebView.getScrollY() == 0);
5365 // Disable swipe to refresh.
5366 swipeRefreshLayout.setEnabled(false);
5369 // Reinforce the system UI visibility flags if in full screen browsing mode.
5370 // This hides the status and navigation bars, which are displayed if other elements are shown, like dialog boxes, the options menu, or the keyboard.
5371 if (inFullScreenBrowsingMode) {
5372 /* Hide the system bars.
5373 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5374 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5375 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5376 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5378 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5379 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5384 // Set the web chrome client.
5385 nestedScrollWebView.setWebChromeClient(new WebChromeClient() {
5386 // Update the progress bar when a page is loading.
5388 public void onProgressChanged(WebView view, int progress) {
5389 // Update the progress bar.
5390 progressBar.setProgress(progress);
5392 // Set the visibility of the progress bar.
5393 if (progress < 100) {
5394 // Show the progress bar.
5395 progressBar.setVisibility(View.VISIBLE);
5397 // Hide the progress bar.
5398 progressBar.setVisibility(View.GONE);
5400 //Stop the swipe to refresh indicator if it is running
5401 swipeRefreshLayout.setRefreshing(false);
5403 // Make the current WebView visible. If this is a new tab, the current WebView would have been created invisible in `webview_framelayout` to prevent a white background splash in night mode.
5404 nestedScrollWebView.setVisibility(View.VISIBLE);
5408 // Set the favorite icon when it changes.
5410 public void onReceivedIcon(WebView view, Bitmap icon) {
5411 // Only update the favorite icon if the website has finished loading.
5412 if (progressBar.getVisibility() == View.GONE) {
5413 // Store the new favorite icon.
5414 nestedScrollWebView.setFavoriteOrDefaultIcon(icon);
5416 // Get the current page position.
5417 int currentPosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
5419 // Get the current tab.
5420 TabLayout.Tab tab = tabLayout.getTabAt(currentPosition);
5422 // Check to see if the tab has been populated.
5424 // Get the custom view from the tab.
5425 View tabView = tab.getCustomView();
5427 // Check to see if the custom tab view has been populated.
5428 if (tabView != null) {
5429 // Get the favorite icon image view from the tab.
5430 ImageView tabFavoriteIconImageView = tabView.findViewById(R.id.favorite_icon_imageview);
5432 // Display the favorite icon in the tab.
5433 tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
5439 // Save a copy of the title when it changes.
5441 public void onReceivedTitle(WebView view, String title) {
5442 // Get the current page position.
5443 int currentPosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
5445 // Get the current tab.
5446 TabLayout.Tab tab = tabLayout.getTabAt(currentPosition);
5448 // Only populate the title text view if the tab has been fully created.
5450 // Get the custom view from the tab.
5451 View tabView = tab.getCustomView();
5453 // Only populate the title text view if the tab view has been fully populated.
5454 if (tabView != null) {
5455 // Get the title text view from the tab.
5456 TextView tabTitleTextView = tabView.findViewById(R.id.title_textview);
5458 // Set the title according to the URL.
5459 if (title.equals("about:blank")) {
5460 // Set the title to indicate a new tab.
5461 tabTitleTextView.setText(R.string.new_tab);
5463 // Set the title as the tab text.
5464 tabTitleTextView.setText(title);
5470 // Enter full screen video.
5472 public void onShowCustomView(View video, CustomViewCallback callback) {
5473 // Set the full screen video flag.
5474 displayingFullScreenVideo = true;
5476 // Hide the keyboard.
5477 inputMethodManager.hideSoftInputFromWindow(nestedScrollWebView.getWindowToken(), 0);
5479 // Hide the coordinator layout.
5480 coordinatorLayout.setVisibility(View.GONE);
5482 /* Hide the system bars.
5483 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5484 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5485 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5486 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5488 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5489 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5491 // Disable the sliding drawers.
5492 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
5494 // Add the video view to the full screen video frame layout.
5495 fullScreenVideoFrameLayout.addView(video);
5497 // Show the full screen video frame layout.
5498 fullScreenVideoFrameLayout.setVisibility(View.VISIBLE);
5500 // Disable the screen timeout while the video is playing. YouTube does this automatically, but not all other videos do.
5501 fullScreenVideoFrameLayout.setKeepScreenOn(true);
5504 // Exit full screen video.
5506 public void onHideCustomView() {
5507 // Exit the full screen video.
5508 exitFullScreenVideo();
5513 public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
5514 // Show the file chooser if the device is running API >= 21.
5515 if (Build.VERSION.SDK_INT >= 21) {
5516 // Store the file path callback.
5517 fileChooserCallback = filePathCallback;
5519 // Create an intent to open a chooser based on the file chooser parameters.
5520 Intent fileChooserIntent = fileChooserParams.createIntent();
5522 // Get a handle for the package manager.
5523 PackageManager packageManager = getPackageManager();
5525 // Check to see if the file chooser intent resolves to an installed package.
5526 if (fileChooserIntent.resolveActivity(packageManager) != null) { // The file chooser intent is fine.
5527 // Start the file chooser intent.
5528 startActivityForResult(fileChooserIntent, BROWSE_FILE_UPLOAD_REQUEST_CODE);
5529 } else { // The file chooser intent will cause a crash.
5530 // Create a generic intent to open a chooser.
5531 Intent genericFileChooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
5533 // Request an openable file.
5534 genericFileChooserIntent.addCategory(Intent.CATEGORY_OPENABLE);
5536 // Set the file type to everything.
5537 genericFileChooserIntent.setType("*/*");
5539 // Start the generic file chooser intent.
5540 startActivityForResult(genericFileChooserIntent, BROWSE_FILE_UPLOAD_REQUEST_CODE);
5547 nestedScrollWebView.setWebViewClient(new WebViewClient() {
5548 // `shouldOverrideUrlLoading` makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
5549 // The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
5551 public boolean shouldOverrideUrlLoading(WebView view, String url) {
5552 // Sanitize the url.
5553 url = sanitizeUrl(url);
5555 // Handle the URL according to the type.
5556 if (url.startsWith("http")) { // Load the URL in Privacy Browser.
5557 // Load the URL. By using `loadUrl()`, instead of `loadUrlFromBase()`, the Referer header will never be sent.
5558 loadUrl(nestedScrollWebView, url);
5560 // Returning true indicates that Privacy Browser is manually handling the loading of the URL.
5561 // Custom headers cannot be added if false is returned and the WebView handles the loading of the URL.
5563 } else if (url.startsWith("mailto:")) { // Load the email address in an external email program.
5564 // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
5565 Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
5567 // Parse the url and set it as the data for the intent.
5568 emailIntent.setData(Uri.parse(url));
5570 // Open the email program in a new task instead of as part of Privacy Browser.
5571 emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5575 startActivity(emailIntent);
5576 } catch (ActivityNotFoundException exception) {
5577 // Display a snackbar.
5578 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
5582 // Returning true indicates Privacy Browser is handling the URL by creating an intent.
5584 } else if (url.startsWith("tel:")) { // Load the phone number in the dialer.
5585 // Open the dialer and load the phone number, but wait for the user to place the call.
5586 Intent dialIntent = new Intent(Intent.ACTION_DIAL);
5588 // Add the phone number to the intent.
5589 dialIntent.setData(Uri.parse(url));
5591 // Open the dialer in a new task instead of as part of Privacy Browser.
5592 dialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5596 startActivity(dialIntent);
5597 } catch (ActivityNotFoundException exception) {
5598 // Display a snackbar.
5599 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
5602 // Returning true indicates Privacy Browser is handling the URL by creating an intent.
5604 } else { // Load a system chooser to select an app that can handle the URL.
5605 // Open an app that can handle the URL.
5606 Intent genericIntent = new Intent(Intent.ACTION_VIEW);
5608 // Add the URL to the intent.
5609 genericIntent.setData(Uri.parse(url));
5611 // List all apps that can handle the URL instead of just opening the first one.
5612 genericIntent.addCategory(Intent.CATEGORY_BROWSABLE);
5614 // Open the app in a new task instead of as part of Privacy Browser.
5615 genericIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5617 // Start the app or display a snackbar if no app is available to handle the URL.
5619 startActivity(genericIntent);
5620 } catch (ActivityNotFoundException exception) {
5621 Snackbar.make(nestedScrollWebView, getString(R.string.unrecognized_url) + " " + url, Snackbar.LENGTH_SHORT).show();
5624 // Returning true indicates Privacy Browser is handling the URL by creating an intent.
5629 // Check requests against the block lists. The deprecated `shouldInterceptRequest()` must be used until minimum API >= 21.
5631 public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
5632 // Check to see if the resource request is for the main URL.
5633 if (url.equals(nestedScrollWebView.getCurrentUrl())) {
5634 // `return null` loads the resource request, which should never be blocked if it is the main URL.
5638 // Wait until the blocklists have been populated. When Privacy Browser is being resumed after having the process killed in the background it will try to load the URLs immediately.
5639 while (ultraPrivacy == null) {
5640 // The wait must be synchronized, which only lets one thread run on it at a time, or `java.lang.IllegalMonitorStateException` is thrown.
5641 synchronized (this) {
5643 // Check to see if the blocklists have been populated after 100 ms.
5645 } catch (InterruptedException exception) {
5651 // Sanitize the URL.
5652 url = sanitizeUrl(url);
5654 // Create an empty web resource response to be used if the resource request is blocked.
5655 WebResourceResponse emptyWebResourceResponse = new WebResourceResponse("text/plain", "utf8", new ByteArrayInputStream("".getBytes()));
5657 // Reset the whitelist results tracker.
5658 String[] whitelistResultStringArray = null;
5660 // Initialize the third party request tracker.
5661 boolean isThirdPartyRequest = false;
5663 // Get the current URL. `.getUrl()` throws an error because operations on the WebView cannot be made from this thread.
5664 String currentBaseDomain = nestedScrollWebView.getCurrentDomainName();
5666 // Store a copy of the current domain for use in later requests.
5667 String currentDomain = currentBaseDomain;
5669 // Nobody is happy when comparing null strings.
5671 // Convert the request URL to a URI.
5672 Uri requestUri = Uri.parse(url);
5674 // Get the request host name.
5675 String requestBaseDomain = requestUri.getHost();
5677 // Only check for third-party requests if the current base domain is not empty and the request domain is not null.
5678 if (!currentBaseDomain.isEmpty() && (requestBaseDomain != null)) {
5679 // Determine the current base domain.
5680 while (currentBaseDomain.indexOf(".", currentBaseDomain.indexOf(".") + 1) > 0) { // There is at least one subdomain.
5681 // Remove the first subdomain.
5682 currentBaseDomain = currentBaseDomain.substring(currentBaseDomain.indexOf(".") + 1);
5685 // Determine the request base domain.
5686 while (requestBaseDomain.indexOf(".", requestBaseDomain.indexOf(".") + 1) > 0) { // There is at least one subdomain.
5687 // Remove the first subdomain.
5688 requestBaseDomain = requestBaseDomain.substring(requestBaseDomain.indexOf(".") + 1);
5691 // Update the third party request tracker.
5692 isThirdPartyRequest = !currentBaseDomain.equals(requestBaseDomain);
5696 // Get the current WebView page position.
5697 int webViewPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
5699 // Determine if the WebView is currently displayed.
5700 boolean webViewDisplayed = (webViewPagePosition == tabLayout.getSelectedTabPosition());
5702 // Block third-party requests if enabled.
5703 if (isThirdPartyRequest && nestedScrollWebView.getBlockAllThirdPartyRequests()) {
5704 // Add the result to the resource requests.
5705 nestedScrollWebView.addResourceRequest(new String[]{BlocklistHelper.REQUEST_THIRD_PARTY, url});
5707 // Increment the blocked requests counters.
5708 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5709 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS);
5711 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5712 if (webViewDisplayed) {
5713 // Updating the UI must be run from the UI thread.
5714 activity.runOnUiThread(() -> {
5715 // Update the menu item titles.
5716 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5718 // Update the options menu if it has been populated.
5719 if (optionsMenu != null) {
5720 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5721 optionsBlockAllThirdPartyRequestsMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS) + " - " +
5722 getString(R.string.block_all_third_party_requests));
5727 // Return an empty web resource response.
5728 return emptyWebResourceResponse;
5731 // Check UltraList if it is enabled.
5732 if (nestedScrollWebView.getUltraListEnabled()) {
5733 // Check the URL against UltraList.
5734 String[] ultraListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, ultraList);
5736 // Process the UltraList results.
5737 if (ultraListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched UltraList's blacklist.
5738 // Add the result to the resource requests.
5739 nestedScrollWebView.addResourceRequest(new String[] {ultraListResults[0], ultraListResults[1], ultraListResults[2], ultraListResults[3], ultraListResults[4], ultraListResults[5]});
5741 // Increment the blocked requests counters.
5742 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5743 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.ULTRALIST);
5745 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5746 if (webViewDisplayed) {
5747 // Updating the UI must be run from the UI thread.
5748 activity.runOnUiThread(() -> {
5749 // Update the menu item titles.
5750 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5752 // Update the options menu if it has been populated.
5753 if (optionsMenu != null) {
5754 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5755 optionsUltraListMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.ULTRALIST) + " - " + getString(R.string.ultralist));
5760 // The resource request was blocked. Return an empty web resource response.
5761 return emptyWebResourceResponse;
5762 } else if (ultraListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched UltraList's whitelist.
5763 // Add a whitelist entry to the resource requests array.
5764 nestedScrollWebView.addResourceRequest(new String[] {ultraListResults[0], ultraListResults[1], ultraListResults[2], ultraListResults[3], ultraListResults[4], ultraListResults[5]});
5766 // The resource request has been allowed by UltraPrivacy. `return null` loads the requested resource.
5771 // Check UltraPrivacy if it is enabled.
5772 if (nestedScrollWebView.getUltraPrivacyEnabled()) {
5773 // Check the URL against UltraPrivacy.
5774 String[] ultraPrivacyResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, ultraPrivacy);
5776 // Process the UltraPrivacy results.
5777 if (ultraPrivacyResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched UltraPrivacy's blacklist.
5778 // Add the result to the resource requests.
5779 nestedScrollWebView.addResourceRequest(new String[] {ultraPrivacyResults[0], ultraPrivacyResults[1], ultraPrivacyResults[2], ultraPrivacyResults[3], ultraPrivacyResults[4],
5780 ultraPrivacyResults[5]});
5782 // Increment the blocked requests counters.
5783 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5784 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.ULTRAPRIVACY);
5786 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5787 if (webViewDisplayed) {
5788 // Updating the UI must be run from the UI thread.
5789 activity.runOnUiThread(() -> {
5790 // Update the menu item titles.
5791 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5793 // Update the options menu if it has been populated.
5794 if (optionsMenu != null) {
5795 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5796 optionsUltraPrivacyMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.ULTRAPRIVACY) + " - " + getString(R.string.ultraprivacy));
5801 // The resource request was blocked. Return an empty web resource response.
5802 return emptyWebResourceResponse;
5803 } else if (ultraPrivacyResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched UltraPrivacy's whitelist.
5804 // Add a whitelist entry to the resource requests array.
5805 nestedScrollWebView.addResourceRequest(new String[] {ultraPrivacyResults[0], ultraPrivacyResults[1], ultraPrivacyResults[2], ultraPrivacyResults[3], ultraPrivacyResults[4],
5806 ultraPrivacyResults[5]});
5808 // The resource request has been allowed by UltraPrivacy. `return null` loads the requested resource.
5813 // Check EasyList if it is enabled.
5814 if (nestedScrollWebView.getEasyListEnabled()) {
5815 // Check the URL against EasyList.
5816 String[] easyListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, easyList);
5818 // Process the EasyList results.
5819 if (easyListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched EasyList's blacklist.
5820 // Add the result to the resource requests.
5821 nestedScrollWebView.addResourceRequest(new String[] {easyListResults[0], easyListResults[1], easyListResults[2], easyListResults[3], easyListResults[4], easyListResults[5]});
5823 // Increment the blocked requests counters.
5824 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5825 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.EASYLIST);
5827 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5828 if (webViewDisplayed) {
5829 // Updating the UI must be run from the UI thread.
5830 activity.runOnUiThread(() -> {
5831 // Update the menu item titles.
5832 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5834 // Update the options menu if it has been populated.
5835 if (optionsMenu != null) {
5836 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5837 optionsEasyListMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.EASYLIST) + " - " + getString(R.string.easylist));
5842 // The resource request was blocked. Return an empty web resource response.
5843 return emptyWebResourceResponse;
5844 } else if (easyListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched EasyList's whitelist.
5845 // Update the whitelist result string array tracker.
5846 whitelistResultStringArray = new String[] {easyListResults[0], easyListResults[1], easyListResults[2], easyListResults[3], easyListResults[4], easyListResults[5]};
5850 // Check EasyPrivacy if it is enabled.
5851 if (nestedScrollWebView.getEasyPrivacyEnabled()) {
5852 // Check the URL against EasyPrivacy.
5853 String[] easyPrivacyResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, easyPrivacy);
5855 // Process the EasyPrivacy results.
5856 if (easyPrivacyResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched EasyPrivacy's blacklist.
5857 // Add the result to the resource requests.
5858 nestedScrollWebView.addResourceRequest(new String[] {easyPrivacyResults[0], easyPrivacyResults[1], easyPrivacyResults[2], easyPrivacyResults[3], easyPrivacyResults[4],
5859 easyPrivacyResults[5]});
5861 // Increment the blocked requests counters.
5862 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5863 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.EASYPRIVACY);
5865 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5866 if (webViewDisplayed) {
5867 // Updating the UI must be run from the UI thread.
5868 activity.runOnUiThread(() -> {
5869 // Update the menu item titles.
5870 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5872 // Update the options menu if it has been populated.
5873 if (optionsMenu != null) {
5874 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5875 optionsEasyPrivacyMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.EASYPRIVACY) + " - " + getString(R.string.easyprivacy));
5880 // The resource request was blocked. Return an empty web resource response.
5881 return emptyWebResourceResponse;
5882 } else if (easyPrivacyResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched EasyPrivacy's whitelist.
5883 // Update the whitelist result string array tracker.
5884 whitelistResultStringArray = new String[] {easyPrivacyResults[0], easyPrivacyResults[1], easyPrivacyResults[2], easyPrivacyResults[3], easyPrivacyResults[4], easyPrivacyResults[5]};
5888 // Check Fanboy’s Annoyance List if it is enabled.
5889 if (nestedScrollWebView.getFanboysAnnoyanceListEnabled()) {
5890 // Check the URL against Fanboy's Annoyance List.
5891 String[] fanboysAnnoyanceListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, fanboysAnnoyanceList);
5893 // Process the Fanboy's Annoyance List results.
5894 if (fanboysAnnoyanceListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched Fanboy's Annoyance List's blacklist.
5895 // Add the result to the resource requests.
5896 nestedScrollWebView.addResourceRequest(new String[] {fanboysAnnoyanceListResults[0], fanboysAnnoyanceListResults[1], fanboysAnnoyanceListResults[2], fanboysAnnoyanceListResults[3],
5897 fanboysAnnoyanceListResults[4], fanboysAnnoyanceListResults[5]});
5899 // Increment the blocked requests counters.
5900 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5901 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST);
5903 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5904 if (webViewDisplayed) {
5905 // Updating the UI must be run from the UI thread.
5906 activity.runOnUiThread(() -> {
5907 // Update the menu item titles.
5908 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5910 // Update the options menu if it has been populated.
5911 if (optionsMenu != null) {
5912 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5913 optionsFanboysAnnoyanceListMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST) + " - " +
5914 getString(R.string.fanboys_annoyance_list));
5919 // The resource request was blocked. Return an empty web resource response.
5920 return emptyWebResourceResponse;
5921 } else if (fanboysAnnoyanceListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)){ // The resource request matched Fanboy's Annoyance List's whitelist.
5922 // Update the whitelist result string array tracker.
5923 whitelistResultStringArray = new String[] {fanboysAnnoyanceListResults[0], fanboysAnnoyanceListResults[1], fanboysAnnoyanceListResults[2], fanboysAnnoyanceListResults[3],
5924 fanboysAnnoyanceListResults[4], fanboysAnnoyanceListResults[5]};
5926 } else if (nestedScrollWebView.getFanboysSocialBlockingListEnabled()) { // Only check Fanboy’s Social Blocking List if Fanboy’s Annoyance List is disabled.
5927 // Check the URL against Fanboy's Annoyance List.
5928 String[] fanboysSocialListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, fanboysSocialList);
5930 // Process the Fanboy's Social Blocking List results.
5931 if (fanboysSocialListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched Fanboy's Social Blocking List's blacklist.
5932 // Add the result to the resource requests.
5933 nestedScrollWebView.addResourceRequest(new String[] {fanboysSocialListResults[0], fanboysSocialListResults[1], fanboysSocialListResults[2], fanboysSocialListResults[3],
5934 fanboysSocialListResults[4], fanboysSocialListResults[5]});
5936 // Increment the blocked requests counters.
5937 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5938 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST);
5940 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5941 if (webViewDisplayed) {
5942 // Updating the UI must be run from the UI thread.
5943 activity.runOnUiThread(() -> {
5944 // Update the menu item titles.
5945 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5947 // Update the options menu if it has been populated.
5948 if (optionsMenu != null) {
5949 optionsBlocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5950 optionsFanboysSocialBlockingListMenuItem.setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST) + " - " +
5951 getString(R.string.fanboys_social_blocking_list));
5956 // The resource request was blocked. Return an empty web resource response.
5957 return emptyWebResourceResponse;
5958 } else if (fanboysSocialListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched Fanboy's Social Blocking List's whitelist.
5959 // Update the whitelist result string array tracker.
5960 whitelistResultStringArray = new String[] {fanboysSocialListResults[0], fanboysSocialListResults[1], fanboysSocialListResults[2], fanboysSocialListResults[3],
5961 fanboysSocialListResults[4], fanboysSocialListResults[5]};
5965 // Add the request to the log because it hasn't been processed by any of the previous checks.
5966 if (whitelistResultStringArray != null) { // The request was processed by a whitelist.
5967 nestedScrollWebView.addResourceRequest(whitelistResultStringArray);
5968 } else { // The request didn't match any blocklist entry. Log it as a default request.
5969 nestedScrollWebView.addResourceRequest(new String[]{BlocklistHelper.REQUEST_DEFAULT, url});
5972 // The resource request has not been blocked. `return null` loads the requested resource.
5976 // Handle HTTP authentication requests.
5978 public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
5979 // Store the handler.
5980 nestedScrollWebView.setHttpAuthHandler(handler);
5982 // Instantiate an HTTP authentication dialog.
5983 DialogFragment httpAuthenticationDialogFragment = HttpAuthenticationDialog.displayDialog(host, realm, nestedScrollWebView.getWebViewFragmentId());
5985 // Show the HTTP authentication dialog.
5986 httpAuthenticationDialogFragment.show(getSupportFragmentManager(), getString(R.string.http_authentication));
5990 public void onPageStarted(WebView view, String url, Bitmap favicon) {
5991 // Set the padding and layout settings if the app bar is at the top of the screen.
5992 if (!bottomAppBar) {
5993 // Set the top padding of the swipe refresh layout according to the app bar scrolling preference. This can't be done in `appAppSettings()` because the app bar is not yet populated there.
5994 if (scrollAppBar || (inFullScreenBrowsingMode && hideAppBar)) {
5995 // No padding is needed because it will automatically be placed below the app bar layout due to the scrolling layout behavior.
5996 swipeRefreshLayout.setPadding(0, 0, 0, 0);
5998 // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
5999 swipeRefreshLayout.setProgressViewOffset(false, defaultProgressViewStartOffset - 10, defaultProgressViewEndOffset);
6001 // Get the app bar layout height. This can't be done in `applyAppSettings()` because the app bar is not yet populated there.
6002 appBarHeight = appBarLayout.getHeight();
6004 // The swipe refresh layout must be manually moved below the app bar layout.
6005 swipeRefreshLayout.setPadding(0, appBarHeight, 0, 0);
6007 // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
6008 swipeRefreshLayout.setProgressViewOffset(false, defaultProgressViewStartOffset - 10 + appBarHeight, defaultProgressViewEndOffset + appBarHeight);
6012 // Reset the list of resource requests.
6013 nestedScrollWebView.clearResourceRequests();
6015 // Reset the requests counters.
6016 nestedScrollWebView.resetRequestsCounters();
6018 // Get the current page position.
6019 int currentPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
6021 // Update the URL text bar if the page is currently selected and the URL edit text is not currently being edited.
6022 if ((tabLayout.getSelectedTabPosition() == currentPagePosition) && !urlEditText.hasFocus()) {
6023 // Display the formatted URL text.
6024 urlEditText.setText(url);
6026 // Apply text highlighting to the URL text box.
6029 // Hide the keyboard.
6030 inputMethodManager.hideSoftInputFromWindow(nestedScrollWebView.getWindowToken(), 0);
6033 // Reset the list of host IP addresses.
6034 nestedScrollWebView.setCurrentIpAddresses("");
6036 // Get a URI for the current URL.
6037 Uri currentUri = Uri.parse(url);
6039 // Get the IP addresses for the host.
6040 new GetHostIpAddresses(activity, getSupportFragmentManager(), nestedScrollWebView).execute(currentUri.getHost());
6042 // Replace Refresh with Stop if the options menu has been created. (The first WebView typically begins loading before the menu items are instantiated.)
6043 if (optionsMenu != null) {
6045 optionsRefreshMenuItem.setTitle(R.string.stop);
6047 // Get the app bar and theme preferences.
6048 boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean(getString(R.string.display_additional_app_bar_icons_key), false);
6050 // If the icon is displayed in the AppBar, set it according to the theme.
6051 if (displayAdditionalAppBarIcons) {
6052 // Get the current theme status.
6053 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
6055 // Set the stop icon according to the theme.
6056 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
6057 optionsRefreshMenuItem.setIcon(R.drawable.close_blue_day);
6059 optionsRefreshMenuItem.setIcon(R.drawable.close_blue_night);
6066 public void onPageFinished(WebView view, String url) {
6067 // Flush any cookies to persistent storage. The cookie manager has become very lazy about flushing cookies in recent versions.
6068 if (nestedScrollWebView.getAcceptCookies() && Build.VERSION.SDK_INT >= 21) {
6069 CookieManager.getInstance().flush();
6072 // Update the Refresh menu item if the options menu has been created.
6073 if (optionsMenu != null) {
6074 // Reset the Refresh title.
6075 optionsRefreshMenuItem.setTitle(R.string.refresh);
6077 // Get the app bar and theme preferences.
6078 boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean(getString(R.string.display_additional_app_bar_icons_key), false);
6080 // If the icon is displayed in the app bar, reset it according to the theme.
6081 if (displayAdditionalAppBarIcons) {
6082 // Get the current theme status.
6083 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
6085 // Set the icon according to the theme.
6086 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
6087 optionsRefreshMenuItem.setIcon(R.drawable.refresh_enabled_day);
6089 optionsRefreshMenuItem.setIcon(R.drawable.refresh_enabled_night);
6094 // Clear the cache, history, and logcat if Incognito Mode is enabled.
6095 if (incognitoModeEnabled) {
6096 // Clear the cache. `true` includes disk files.
6097 nestedScrollWebView.clearCache(true);
6099 // Clear the back/forward history.
6100 nestedScrollWebView.clearHistory();
6102 // Manually delete cache folders.
6104 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
6105 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
6106 String privateDataDirectoryString = getApplicationInfo().dataDir;
6108 // Delete the main cache directory.
6109 Runtime.getRuntime().exec("rm -rf " + privateDataDirectoryString + "/cache");
6111 // Delete the secondary `Service Worker` cache directory.
6112 // A `String[]` must be used because the directory contains a space and `Runtime.exec` will not escape the string correctly otherwise.
6113 Runtime.getRuntime().exec(new String[]{"rm", "-rf", privateDataDirectoryString + "/app_webview/Service Worker/"});
6114 } catch (IOException exception) {
6115 // Do nothing if an error is thrown.
6118 // Clear the logcat.
6120 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
6121 Runtime.getRuntime().exec("logcat -b all -c");
6122 } catch (IOException exception) {
6127 // Get the current page position.
6128 int currentPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
6130 // Get the current URL from the nested scroll WebView. This is more accurate than using the URL passed into the method, which is sometimes not the final one.
6131 String currentUrl = nestedScrollWebView.getUrl();
6133 // Get the current tab.
6134 TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition);
6136 // Update the URL text bar if the page is currently selected and the user is not currently typing in the URL edit text.
6137 // Crash records show that, in some crazy way, it is possible for the current URL to be blank at this point.
6138 // Probably some sort of race condition when Privacy Browser is being resumed.
6139 if ((tabLayout.getSelectedTabPosition() == currentPagePosition) && !urlEditText.hasFocus() && (currentUrl != null)) {
6140 // Check to see if the URL is `about:blank`.
6141 if (currentUrl.equals("about:blank")) { // The WebView is blank.
6142 // Display the hint in the URL edit text.
6143 urlEditText.setText("");
6145 // Request focus for the URL text box.
6146 urlEditText.requestFocus();
6148 // Display the keyboard.
6149 inputMethodManager.showSoftInput(urlEditText, 0);
6151 // Apply the domain settings. This clears any settings from the previous domain.
6152 applyDomainSettings(nestedScrollWebView, "", true, false, false);
6154 // Only populate the title text view if the tab has been fully created.
6156 // Get the custom view from the tab.
6157 View tabView = tab.getCustomView();
6159 // Remove the incorrect warning below that the current tab view might be null.
6160 assert tabView != null;
6162 // Get the title text view from the tab.
6163 TextView tabTitleTextView = tabView.findViewById(R.id.title_textview);
6165 // Set the title as the tab text.
6166 tabTitleTextView.setText(R.string.new_tab);
6168 } else { // The WebView has loaded a webpage.
6169 // Update the URL edit text if it is not currently being edited.
6170 if (!urlEditText.hasFocus()) {
6171 // Sanitize the current URL. This removes unwanted URL elements that were added by redirects, so that they won't be included if the URL is shared.
6172 String sanitizedUrl = sanitizeUrl(currentUrl);
6174 // Display the final URL. Getting the URL from the WebView instead of using the one provided by `onPageFinished()` makes websites like YouTube function correctly.
6175 urlEditText.setText(sanitizedUrl);
6177 // Apply text highlighting to the URL.
6181 // Only populate the title text view if the tab has been fully created.
6183 // Get the custom view from the tab.
6184 View tabView = tab.getCustomView();
6186 // Remove the incorrect warning below that the current tab view might be null.
6187 assert tabView != null;
6189 // Get the title text view from the tab.
6190 TextView tabTitleTextView = tabView.findViewById(R.id.title_textview);
6192 // Set the title as the tab text. Sometimes `onReceivedTitle()` is not called, especially when navigating history.
6193 tabTitleTextView.setText(nestedScrollWebView.getTitle());
6199 // Handle SSL Certificate errors.
6201 public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
6202 // Get the current website SSL certificate.
6203 SslCertificate currentWebsiteSslCertificate = error.getCertificate();
6205 // Extract the individual pieces of information from the current website SSL certificate.
6206 String currentWebsiteIssuedToCName = currentWebsiteSslCertificate.getIssuedTo().getCName();
6207 String currentWebsiteIssuedToOName = currentWebsiteSslCertificate.getIssuedTo().getOName();
6208 String currentWebsiteIssuedToUName = currentWebsiteSslCertificate.getIssuedTo().getUName();
6209 String currentWebsiteIssuedByCName = currentWebsiteSslCertificate.getIssuedBy().getCName();
6210 String currentWebsiteIssuedByOName = currentWebsiteSslCertificate.getIssuedBy().getOName();
6211 String currentWebsiteIssuedByUName = currentWebsiteSslCertificate.getIssuedBy().getUName();
6212 Date currentWebsiteSslStartDate = currentWebsiteSslCertificate.getValidNotBeforeDate();
6213 Date currentWebsiteSslEndDate = currentWebsiteSslCertificate.getValidNotAfterDate();
6215 // Proceed to the website if the current SSL website certificate matches the pinned domain certificate.
6216 if (nestedScrollWebView.hasPinnedSslCertificate()) {
6217 // Get the pinned SSL certificate.
6218 ArrayList<Object> pinnedSslCertificateArrayList = nestedScrollWebView.getPinnedSslCertificate();
6220 // Extract the arrays from the array list.
6221 String[] pinnedSslCertificateStringArray = (String[]) pinnedSslCertificateArrayList.get(0);
6222 Date[] pinnedSslCertificateDateArray = (Date[]) pinnedSslCertificateArrayList.get(1);
6224 // Check if the current SSL certificate matches the pinned certificate.
6225 if (currentWebsiteIssuedToCName.equals(pinnedSslCertificateStringArray[0]) && currentWebsiteIssuedToOName.equals(pinnedSslCertificateStringArray[1]) &&
6226 currentWebsiteIssuedToUName.equals(pinnedSslCertificateStringArray[2]) && currentWebsiteIssuedByCName.equals(pinnedSslCertificateStringArray[3]) &&
6227 currentWebsiteIssuedByOName.equals(pinnedSslCertificateStringArray[4]) && currentWebsiteIssuedByUName.equals(pinnedSslCertificateStringArray[5]) &&
6228 currentWebsiteSslStartDate.equals(pinnedSslCertificateDateArray[0]) && currentWebsiteSslEndDate.equals(pinnedSslCertificateDateArray[1])) {
6230 // An SSL certificate is pinned and matches the current domain certificate. Proceed to the website without displaying an error.
6233 } else { // Either there isn't a pinned SSL certificate or it doesn't match the current website certificate.
6234 // Store the SSL error handler.
6235 nestedScrollWebView.setSslErrorHandler(handler);
6237 // Instantiate an SSL certificate error alert dialog.
6238 DialogFragment sslCertificateErrorDialogFragment = SslCertificateErrorDialog.displayDialog(error, nestedScrollWebView.getWebViewFragmentId());
6240 // Try to show the dialog. The SSL error handler continues to function even when the WebView is paused. Attempting to display a dialog in that state leads to a crash.
6242 // Show the SSL certificate error dialog.
6243 sslCertificateErrorDialogFragment.show(getSupportFragmentManager(), getString(R.string.ssl_certificate_error));
6244 } catch (Exception exception) {
6245 // Add the dialog to the pending dialog array list. It will be displayed in `onStart()`.
6246 pendingDialogsArrayList.add(new PendingDialog(sslCertificateErrorDialogFragment, getString(R.string.ssl_certificate_error)));
6252 // Check to see if the state is being restored.
6253 if (restoringState) { // The state is being restored.
6254 // Resume the nested scroll WebView JavaScript timers.
6255 nestedScrollWebView.resumeTimers();
6256 } else if (pageNumber == 0) { // The first page is being loaded.
6257 // Set this nested scroll WebView as the current WebView.
6258 currentWebView = nestedScrollWebView;
6260 // Initialize the URL to load string.
6261 String urlToLoadString;
6263 // Get the intent that started the app.
6264 Intent launchingIntent = getIntent();
6266 // Reset the intent. This prevents a duplicate tab from being created on restart.
6267 setIntent(new Intent());
6269 // Get the information from the intent.
6270 String launchingIntentAction = launchingIntent.getAction();
6271 Uri launchingIntentUriData = launchingIntent.getData();
6272 String launchingIntentStringExtra = launchingIntent.getStringExtra(Intent.EXTRA_TEXT);
6274 // Parse the launching intent URL.
6275 if ((launchingIntentAction != null) && launchingIntentAction.equals(Intent.ACTION_WEB_SEARCH)) { // The intent contains a search string.
6276 // Create an encoded URL string.
6277 String encodedUrlString;
6279 // Sanitize the search input and convert it to a search.
6281 encodedUrlString = URLEncoder.encode(launchingIntent.getStringExtra(SearchManager.QUERY), "UTF-8");
6282 } catch (UnsupportedEncodingException exception) {
6283 encodedUrlString = "";
6286 // Store the web search as the URL to load.
6287 urlToLoadString = searchURL + encodedUrlString;
6288 } else if (launchingIntentUriData != null) { // The launching intent contains a URL formatted as a URI.
6289 // Store the URI as a URL.
6290 urlToLoadString = launchingIntentUriData.toString();
6291 } else if (launchingIntentStringExtra != null) { // The launching intent contains text that might be a URL.
6293 urlToLoadString = launchingIntentStringExtra;
6294 } else if (!url.equals("")) { // The activity has been restarted.
6295 // Load the saved URL.
6296 urlToLoadString = url;
6297 } else { // The is no URL in the intent.
6298 // Store the homepage to be loaded.
6299 urlToLoadString = sharedPreferences.getString("homepage", getString(R.string.homepage_default_value));
6302 // Load the website if not waiting for the proxy.
6303 if (waitingForProxy) { // Store the URL to be loaded in the Nested Scroll WebView.
6304 nestedScrollWebView.setWaitingForProxyUrlString(urlToLoadString);
6305 } else { // Load the URL.
6306 loadUrl(nestedScrollWebView, urlToLoadString);
6309 // Reset the intent. This prevents a duplicate tab from being created on a subsequent restart if loading an link from a new intent on restart.
6310 // For example, this prevents a duplicate tab if a link is loaded from the Guide after changing the theme in the guide and then changing the theme again in the main activity.
6311 setIntent(new Intent());
6312 } else { // This is not the first tab.
6314 loadUrl(nestedScrollWebView, url);
6316 // Set the focus and display the keyboard if the URL is blank.
6317 if (url.equals("")) {
6318 // Request focus for the URL text box.
6319 urlEditText.requestFocus();
6321 // Create a display keyboard handler.
6322 Handler displayKeyboardHandler = new Handler();
6324 // Create a display keyboard runnable.
6325 Runnable displayKeyboardRunnable = () -> {
6326 // Display the keyboard.
6327 inputMethodManager.showSoftInput(urlEditText, 0);
6330 // Display the keyboard after 100 milliseconds, which leaves enough time for the tab to transition.
6331 displayKeyboardHandler.postDelayed(displayKeyboardRunnable, 100);