2 * Copyright © 2015-2020 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.Manifest;
25 import android.annotation.SuppressLint;
26 import android.app.Activity;
27 import android.app.Dialog;
28 import android.app.DownloadManager;
29 import android.app.SearchManager;
30 import android.content.ActivityNotFoundException;
31 import android.content.BroadcastReceiver;
32 import android.content.ClipData;
33 import android.content.ClipboardManager;
34 import android.content.ContentResolver;
35 import android.content.Context;
36 import android.content.Intent;
37 import android.content.IntentFilter;
38 import android.content.SharedPreferences;
39 import android.content.pm.PackageManager;
40 import android.content.res.Configuration;
41 import android.database.Cursor;
42 import android.graphics.Bitmap;
43 import android.graphics.BitmapFactory;
44 import android.graphics.Typeface;
45 import android.graphics.drawable.BitmapDrawable;
46 import android.graphics.drawable.Drawable;
47 import android.net.Uri;
48 import android.net.http.SslCertificate;
49 import android.net.http.SslError;
50 import android.os.AsyncTask;
51 import android.os.Build;
52 import android.os.Bundle;
53 import android.os.Handler;
54 import android.os.Message;
55 import android.preference.PreferenceManager;
56 import android.print.PrintDocumentAdapter;
57 import android.print.PrintManager;
58 import android.text.Editable;
59 import android.text.Spanned;
60 import android.text.TextWatcher;
61 import android.text.style.ForegroundColorSpan;
62 import android.util.Patterns;
63 import android.util.TypedValue;
64 import android.view.ContextMenu;
65 import android.view.GestureDetector;
66 import android.view.KeyEvent;
67 import android.view.Menu;
68 import android.view.MenuItem;
69 import android.view.MotionEvent;
70 import android.view.View;
71 import android.view.ViewGroup;
72 import android.view.WindowManager;
73 import android.view.inputmethod.InputMethodManager;
74 import android.webkit.CookieManager;
75 import android.webkit.HttpAuthHandler;
76 import android.webkit.SslErrorHandler;
77 import android.webkit.ValueCallback;
78 import android.webkit.WebBackForwardList;
79 import android.webkit.WebChromeClient;
80 import android.webkit.WebResourceResponse;
81 import android.webkit.WebSettings;
82 import android.webkit.WebStorage;
83 import android.webkit.WebView;
84 import android.webkit.WebViewClient;
85 import android.webkit.WebViewDatabase;
86 import android.widget.ArrayAdapter;
87 import android.widget.CursorAdapter;
88 import android.widget.EditText;
89 import android.widget.FrameLayout;
90 import android.widget.ImageView;
91 import android.widget.LinearLayout;
92 import android.widget.ListView;
93 import android.widget.ProgressBar;
94 import android.widget.RadioButton;
95 import android.widget.RelativeLayout;
96 import android.widget.TextView;
98 import androidx.annotation.NonNull;
99 import androidx.appcompat.app.ActionBar;
100 import androidx.appcompat.app.ActionBarDrawerToggle;
101 import androidx.appcompat.app.AppCompatActivity;
102 import androidx.appcompat.app.AppCompatDelegate;
103 import androidx.appcompat.widget.Toolbar;
104 import androidx.coordinatorlayout.widget.CoordinatorLayout;
105 import androidx.core.app.ActivityCompat;
106 import androidx.core.content.ContextCompat;
107 import androidx.core.content.FileProvider;
108 import androidx.core.content.res.ResourcesCompat;
109 import androidx.core.view.GravityCompat;
110 import androidx.drawerlayout.widget.DrawerLayout;
111 import androidx.fragment.app.DialogFragment;
112 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
113 import androidx.viewpager.widget.ViewPager;
114 import androidx.webkit.WebSettingsCompat;
115 import androidx.webkit.WebViewFeature;
117 import com.google.android.material.appbar.AppBarLayout;
118 import com.google.android.material.floatingactionbutton.FloatingActionButton;
119 import com.google.android.material.navigation.NavigationView;
120 import com.google.android.material.snackbar.Snackbar;
121 import com.google.android.material.tabs.TabLayout;
123 import com.stoutner.privacybrowser.BuildConfig;
124 import com.stoutner.privacybrowser.R;
125 import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter;
126 import com.stoutner.privacybrowser.asynctasks.GetHostIpAddresses;
127 import com.stoutner.privacybrowser.asynctasks.PopulateBlocklists;
128 import com.stoutner.privacybrowser.asynctasks.PrepareSaveDialog;
129 import com.stoutner.privacybrowser.asynctasks.SaveUrl;
130 import com.stoutner.privacybrowser.asynctasks.SaveWebpageImage;
131 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
132 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
133 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
134 import com.stoutner.privacybrowser.dialogs.CreateHomeScreenShortcutDialog;
135 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
136 import com.stoutner.privacybrowser.dialogs.FontSizeDialog;
137 import com.stoutner.privacybrowser.dialogs.HttpAuthenticationDialog;
138 import com.stoutner.privacybrowser.dialogs.OpenDialog;
139 import com.stoutner.privacybrowser.dialogs.ProxyNotInstalledDialog;
140 import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog;
141 import com.stoutner.privacybrowser.dialogs.SaveWebpageDialog;
142 import com.stoutner.privacybrowser.dialogs.SslCertificateErrorDialog;
143 import com.stoutner.privacybrowser.dialogs.StoragePermissionDialog;
144 import com.stoutner.privacybrowser.dialogs.UrlHistoryDialog;
145 import com.stoutner.privacybrowser.dialogs.ViewSslCertificateDialog;
146 import com.stoutner.privacybrowser.dialogs.WaitingForProxyDialog;
147 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
148 import com.stoutner.privacybrowser.helpers.AdHelper;
149 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
150 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
151 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper;
152 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
153 import com.stoutner.privacybrowser.helpers.FileNameHelper;
154 import com.stoutner.privacybrowser.helpers.ProxyHelper;
155 import com.stoutner.privacybrowser.views.NestedScrollWebView;
157 import java.io.ByteArrayInputStream;
158 import java.io.ByteArrayOutputStream;
160 import java.io.IOException;
161 import java.io.UnsupportedEncodingException;
162 import java.net.MalformedURLException;
164 import java.net.URLDecoder;
165 import java.net.URLEncoder;
166 import java.text.NumberFormat;
167 import java.util.ArrayList;
168 import java.util.Date;
169 import java.util.HashMap;
170 import java.util.HashSet;
171 import java.util.List;
172 import java.util.Map;
173 import java.util.Objects;
174 import java.util.Set;
175 import java.util.concurrent.ExecutorService;
176 import java.util.concurrent.Executors;
178 public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener,
179 EditBookmarkFolderDialog.EditBookmarkFolderListener, FontSizeDialog.UpdateFontSizeListener, NavigationView.OnNavigationItemSelectedListener, OpenDialog.OpenListener,
180 PinnedMismatchDialog.PinnedMismatchListener, PopulateBlocklists.PopulateBlocklistsListener, SaveWebpageDialog.SaveWebpageListener, StoragePermissionDialog.StoragePermissionDialogListener,
181 UrlHistoryDialog.NavigateHistoryListener, WebViewTabFragment.NewTabListener {
183 // The executor service handles background tasks. It is accessed from `ViewSourceActivity`.
184 public static ExecutorService executorService = Executors.newFixedThreadPool(4);
186 // `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`. It is also used in `onCreate()`, `onResume()`, and `applyProxy()`.
187 public static String orbotStatus = "unknown";
189 // The WebView pager adapter is accessed from `HttpAuthenticationDialog`, `PinnedMismatchDialog`, and `SslCertificateErrorDialog`. It is also used in `onCreate()`, `onResume()`, and `addTab()`.
190 public static WebViewPagerAdapter webViewPagerAdapter;
192 // The load URL on restart variables are public static so they can be accessed from `BookmarksActivity`. They are used in `onRestart()`.
193 public static boolean loadUrlOnRestart;
194 public static String urlToLoadOnRestart;
196 // `restartFromBookmarksActivity` is public static so it can be accessed from `BookmarksActivity`. It is also used in `onRestart()`.
197 public static boolean restartFromBookmarksActivity;
199 // `currentBookmarksFolder` is public static so it can be accessed from `BookmarksActivity`. It is also used in `onCreate()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`,
200 // `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
201 public static String currentBookmarksFolder;
203 // The user agent constants are public static so they can be accessed from `SettingsFragment`, `DomainsActivity`, and `DomainSettingsFragment`.
204 public final static int UNRECOGNIZED_USER_AGENT = -1;
205 public final static int SETTINGS_WEBVIEW_DEFAULT_USER_AGENT = 1;
206 public final static int SETTINGS_CUSTOM_USER_AGENT = 12;
207 public final static int DOMAINS_SYSTEM_DEFAULT_USER_AGENT = 0;
208 public final static int DOMAINS_WEBVIEW_DEFAULT_USER_AGENT = 2;
209 public final static int DOMAINS_CUSTOM_USER_AGENT = 13;
211 // Start activity for result request codes. The public static entries are accessed from `OpenDialog()` and `SaveWebpageDialog()`.
212 public final static int BROWSE_OPEN_REQUEST_CODE = 0;
213 public final static int BROWSE_SAVE_WEBPAGE_REQUEST_CODE = 1;
214 private final int BROWSE_FILE_UPLOAD_REQUEST_CODE = 2;
216 // The proxy mode is public static so it can be accessed from `ProxyHelper()`.
217 // It is also used in `onRestart()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `applyAppSettings()`, and `applyProxy()`.
218 // It will be updated in `applyAppSettings()`, but it needs to be initialized here or the first run of `onPrepareOptionsMenu()` crashes.
219 public static String proxyMode = ProxyHelper.NONE;
221 // Define the saved instance state constants.
222 private final String SAVED_STATE_ARRAY_LIST = "saved_state_array_list";
223 private final String SAVED_NESTED_SCROLL_WEBVIEW_STATE_ARRAY_LIST = "saved_nested_scroll_webview_state_array_list";
224 private final String SAVED_TAB_POSITION = "saved_tab_position";
225 private final String PROXY_MODE = "proxy_mode";
227 // Define the saved instance state variables.
228 private ArrayList<Bundle> savedStateArrayList;
229 private ArrayList<Bundle> savedNestedScrollWebViewStateArrayList;
230 private int savedTabPosition;
231 private String savedProxyMode;
233 // Define the class variables.
234 @SuppressWarnings("rawtypes")
235 AsyncTask populateBlocklists;
237 // The current WebView is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`,
238 // `findNextOnPage()`, `closeFindOnPage()`, `loadUrlFromTextBox()`, `onSslMismatchBack()`, `applyProxy()`, and `applyDomainSettings()`.
239 private NestedScrollWebView currentWebView;
241 // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateContextMenu()`, and `loadUrl()`.
242 private final Map<String, String> customHeaders = new HashMap<>();
244 // The search URL is set in `applyAppSettings()` and used in `onNewIntent()`, `loadUrlFromTextBox()`, `initializeApp()`, and `initializeWebView()`.
245 private String searchURL;
247 // The options menu is set in `onCreateOptionsMenu()` and used in `onOptionsItemSelected()`, `updatePrivacyIcons()`, and `initializeWebView()`.
248 private Menu optionsMenu;
250 // The blocklists are populated in `finishedPopulatingBlocklists()` and accessed from `initializeWebView()`.
251 private ArrayList<List<String[]>> easyList;
252 private ArrayList<List<String[]>> easyPrivacy;
253 private ArrayList<List<String[]>> fanboysAnnoyanceList;
254 private ArrayList<List<String[]>> fanboysSocialList;
255 private ArrayList<List<String[]>> ultraList;
256 private ArrayList<List<String[]>> ultraPrivacy;
258 // `webViewDefaultUserAgent` is used in `onCreate()` and `onPrepareOptionsMenu()`.
259 private String webViewDefaultUserAgent;
261 // The incognito mode is set in `applyAppSettings()` and used in `initializeWebView()`.
262 private boolean incognitoModeEnabled;
264 // The full screen browsing mode tracker is set it `applyAppSettings()` and used in `initializeWebView()`.
265 private boolean fullScreenBrowsingModeEnabled;
267 // `inFullScreenBrowsingMode` is used in `onCreate()`, `onConfigurationChanged()`, and `applyAppSettings()`.
268 private boolean inFullScreenBrowsingMode;
270 // The app bar trackers are set in `applyAppSettings()` and used in `initializeWebView()`.
271 private boolean hideAppBar;
272 private boolean scrollAppBar;
274 // The loading new intent tracker is set in `onNewIntent()` and used in `setCurrentWebView()`.
275 private boolean loadingNewIntent;
277 // `reapplyDomainSettingsOnRestart` is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, and `onAddDomain()`, .
278 private boolean reapplyDomainSettingsOnRestart;
280 // `reapplyAppSettingsOnRestart` is used in `onNavigationItemSelected()` and `onRestart()`.
281 private boolean reapplyAppSettingsOnRestart;
283 // `displayingFullScreenVideo` is used in `onCreate()` and `onResume()`.
284 private boolean displayingFullScreenVideo;
286 // `orbotStatusBroadcastReceiver` is used in `onCreate()` and `onDestroy()`.
287 private BroadcastReceiver orbotStatusBroadcastReceiver;
289 // The waiting for proxy boolean is used in `onResume()`, `initializeApp()` and `applyProxy()`.
290 private boolean waitingForProxy = false;
292 // The action bar drawer toggle is initialized in `onCreate()` and used in `onResume()`.
293 private ActionBarDrawerToggle actionBarDrawerToggle;
295 // The color spans are used in `onCreate()` and `highlightUrlText()`.
296 private ForegroundColorSpan redColorSpan;
297 private ForegroundColorSpan initialGrayColorSpan;
298 private ForegroundColorSpan finalGrayColorSpan;
300 // `bookmarksDatabaseHelper` is used in `onCreate()`, `onDestroy`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`,
301 // and `loadBookmarksFolder()`.
302 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
304 // `bookmarksCursor` is used in `onDestroy()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
305 private Cursor bookmarksCursor;
307 // `bookmarksCursorAdapter` is used in `onCreateBookmark()`, `onCreateBookmarkFolder()` `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, and `loadBookmarksFolder()`.
308 private CursorAdapter bookmarksCursorAdapter;
310 // `oldFolderNameString` is used in `onCreate()` and `onSaveEditBookmarkFolder()`.
311 private String oldFolderNameString;
313 // `fileChooserCallback` is used in `onCreate()` and `onActivityResult()`.
314 private ValueCallback<Uri[]> fileChooserCallback;
316 // The default progress view offsets are set in `onCreate()` and used in `initializeWebView()`.
317 private int appBarHeight;
318 private int defaultProgressViewStartOffset;
319 private int defaultProgressViewEndOffset;
321 // The URL sanitizers are set in `applyAppSettings()` and used in `sanitizeUrl()`.
322 private boolean sanitizeGoogleAnalytics;
323 private boolean sanitizeFacebookClickIds;
324 private boolean sanitizeTwitterAmpRedirects;
326 // The file path strings are used in `onSaveWebpage()` and `onRequestPermissionResult()`
327 private String openFilePath;
328 private String saveWebpageUrl;
329 private String saveWebpageFilePath;
331 // Declare the class views.
332 private DrawerLayout drawerLayout;
333 private AppBarLayout appBarLayout;
334 private TabLayout tabLayout;
335 private ViewPager webViewPager;
338 // Remove the warning about needing to override `performClick()` when using an `OnTouchListener` with `WebView`.
339 @SuppressLint("ClickableViewAccessibility")
340 protected void onCreate(Bundle savedInstanceState) {
341 // Run the default commands.
342 super.onCreate(savedInstanceState);
344 // Check to see if the activity has been restarted.
345 if (savedInstanceState != null) {
346 // Store the saved instance state variables.
347 savedStateArrayList = savedInstanceState.getParcelableArrayList(SAVED_STATE_ARRAY_LIST);
348 savedNestedScrollWebViewStateArrayList = savedInstanceState.getParcelableArrayList(SAVED_NESTED_SCROLL_WEBVIEW_STATE_ARRAY_LIST);
349 savedTabPosition = savedInstanceState.getInt(SAVED_TAB_POSITION);
350 savedProxyMode = savedInstanceState.getString(PROXY_MODE);
353 // Initialize the default preference values the first time the program is run. `false` keeps this command from resetting any current preferences back to default.
354 PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
356 // Get a handle for the shared preferences.
357 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
359 // Get the screenshot preference.
360 String appTheme = sharedPreferences.getString("app_theme", getString(R.string.app_theme_default_value));
361 boolean allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false);
363 // Get the theme entry values string array.
364 String[] appThemeEntryValuesStringArray = getResources().getStringArray(R.array.app_theme_entry_values);
366 // 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.
367 if (appTheme.equals(appThemeEntryValuesStringArray[1])) { // The light theme is selected.
368 // Apply the light theme.
369 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
370 } else if (appTheme.equals(appThemeEntryValuesStringArray[2])) { // The dark theme is selected.
371 // Apply the dark theme.
372 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
373 } else { // The system default theme is selected.
374 if (Build.VERSION.SDK_INT >= 28) { // The system default theme is supported.
375 // Follow the system default theme.
376 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
377 } else { // The system default theme is not supported.
378 // Follow the battery saver mode.
379 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
383 // Disable screenshots if not allowed.
384 if (!allowScreenshots) {
385 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
388 // 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.
389 if (Build.VERSION.SDK_INT >= 21) {
390 WebView.enableSlowWholeDocumentDraw();
394 setTheme(R.style.PrivacyBrowser);
396 // Set the content view.
397 setContentView(R.layout.main_framelayout);
399 // Get handles for the views.
400 drawerLayout = findViewById(R.id.drawerlayout);
401 appBarLayout = findViewById(R.id.appbar_layout);
402 Toolbar toolbar = findViewById(R.id.toolbar);
403 tabLayout = findViewById(R.id.tablayout);
404 webViewPager = findViewById(R.id.webviewpager);
406 // Get a handle for the app compat delegate.
407 AppCompatDelegate appCompatDelegate = getDelegate();
409 // Set the support action bar.
410 appCompatDelegate.setSupportActionBar(toolbar);
412 // Get a handle for the action bar.
413 ActionBar actionBar = appCompatDelegate.getSupportActionBar();
415 // This is needed to get rid of the Android Studio warning that the action bar might be null.
416 assert actionBar != null;
418 // Add the custom layout, which shows the URL text bar.
419 actionBar.setCustomView(R.layout.url_app_bar);
420 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
422 // Create the hamburger icon at the start of the AppBar.
423 actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
425 // Initially disable the sliding drawers. They will be enabled once the blocklists are loaded.
426 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
428 // Initialize the web view pager adapter.
429 webViewPagerAdapter = new WebViewPagerAdapter(getSupportFragmentManager());
431 // Set the pager adapter on the web view pager.
432 webViewPager.setAdapter(webViewPagerAdapter);
434 // Store up to 100 tabs in memory.
435 webViewPager.setOffscreenPageLimit(100);
437 // Initialize the app.
440 // Apply the app settings from the shared preferences.
443 // Populate the blocklists.
444 populateBlocklists = new PopulateBlocklists(this, this).execute();
448 protected void onNewIntent(Intent intent) {
449 // Run the default commands.
450 super.onNewIntent(intent);
452 // Replace the intent that started the app with this one.
455 // Check to see if the app is being restarted.
456 if (savedStateArrayList == null || savedStateArrayList.size() == 0) { // The activity is running for the first time.
457 // Get the information from the intent.
458 String intentAction = intent.getAction();
459 Uri intentUriData = intent.getData();
461 // Determine if this is a web search.
462 boolean isWebSearch = ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH));
464 // 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.
465 if (intentUriData != null || isWebSearch) {
466 // Get the shared preferences.
467 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
469 // Create a URL string.
472 // If the intent action is a web search, perform the search.
473 if (isWebSearch) { // The intent is a web search.
474 // Create an encoded URL string.
475 String encodedUrlString;
477 // Sanitize the search input and convert it to a search.
479 encodedUrlString = URLEncoder.encode(intent.getStringExtra(SearchManager.QUERY), "UTF-8");
480 } catch (UnsupportedEncodingException exception) {
481 encodedUrlString = "";
484 // Add the base search URL.
485 url = searchURL + encodedUrlString;
486 } else { // The intent should contain a URL.
487 // Set the intent data as the URL.
488 url = intentUriData.toString();
491 // Add a new tab if specified in the preferences.
492 if (sharedPreferences.getBoolean("open_intents_in_new_tab", true)) { // Load the URL in a new tab.
493 // Set the loading new intent flag.
494 loadingNewIntent = true;
497 addNewTab(url, true);
498 } else { // Load the URL in the current tab.
500 loadUrl(currentWebView, url);
503 // Close the navigation drawer if it is open.
504 if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
505 drawerLayout.closeDrawer(GravityCompat.START);
508 // Close the bookmarks drawer if it is open.
509 if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
510 drawerLayout.closeDrawer(GravityCompat.END);
517 public void onRestart() {
518 // Run the default commands.
521 // Apply the app settings if returning from the Settings activity.
522 if (reapplyAppSettingsOnRestart) {
523 // Reset the reapply app settings on restart tracker.
524 reapplyAppSettingsOnRestart = false;
526 // Apply the app settings.
530 // Apply the domain settings if returning from the settings or domains activity.
531 if (reapplyDomainSettingsOnRestart) {
532 // Reset the reapply domain settings on restart tracker.
533 reapplyDomainSettingsOnRestart = false;
535 // Reapply the domain settings for each tab.
536 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
537 // Get the WebView tab fragment.
538 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
540 // Get the fragment view.
541 View fragmentView = webViewTabFragment.getView();
543 // Only reload the WebViews if they exist.
544 if (fragmentView != null) {
545 // Get the nested scroll WebView from the tab fragment.
546 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
548 // Reset the current domain name so the domain settings will be reapplied.
549 nestedScrollWebView.resetCurrentDomainName();
551 // Reapply the domain settings if the URL is not null, which can happen if an empty tab is active when returning from settings.
552 if (nestedScrollWebView.getUrl() != null) {
553 applyDomainSettings(nestedScrollWebView, nestedScrollWebView.getUrl(), false, true);
559 // Load the URL on restart (used when loading a bookmark).
560 if (loadUrlOnRestart) {
561 // Load the specified URL.
562 loadUrl(currentWebView, urlToLoadOnRestart);
564 // Reset the load on restart tracker.
565 loadUrlOnRestart = false;
568 // Update the bookmarks drawer if returning from the Bookmarks activity.
569 if (restartFromBookmarksActivity) {
570 // Close the bookmarks drawer.
571 drawerLayout.closeDrawer(GravityCompat.END);
573 // Reload the bookmarks drawer.
574 loadBookmarksFolder();
576 // Reset `restartFromBookmarksActivity`.
577 restartFromBookmarksActivity = false;
580 // Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step. This can be important if the screen was rotated.
581 updatePrivacyIcons(true);
584 // `onResume()` runs after `onStart()`, which runs after `onCreate()` and `onRestart()`.
586 public void onResume() {
587 // Run the default commands.
590 // Resume any WebViews.
591 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
592 // Get the WebView tab fragment.
593 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
595 // Get the fragment view.
596 View fragmentView = webViewTabFragment.getView();
598 // Only resume the WebViews if they exist (they won't when the app is first created).
599 if (fragmentView != null) {
600 // Get the nested scroll WebView from the tab fragment.
601 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
603 // Resume the nested scroll WebView JavaScript timers.
604 nestedScrollWebView.resumeTimers();
606 // Resume the nested scroll WebView.
607 nestedScrollWebView.onResume();
611 // Reapply the proxy settings if the system is using a proxy. This redisplays the appropriate alert dialog.
612 if (!proxyMode.equals(ProxyHelper.NONE)) {
616 // Reapply any system UI flags and the ad in the free flavor.
617 if (displayingFullScreenVideo || inFullScreenBrowsingMode) { // The system is displaying a website or a video in full screen mode.
618 // Get a handle for the root frame layouts.
619 FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout);
621 /* Hide the system bars.
622 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
623 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
624 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
625 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
627 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
628 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
629 } else if (BuildConfig.FLAVOR.contentEquals("free")) { // The system in not in full screen mode.
631 AdHelper.resumeAd(findViewById(R.id.adview));
636 public void onPause() {
637 // Run the default commands.
640 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
641 // Get the WebView tab fragment.
642 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
644 // Get the fragment view.
645 View fragmentView = webViewTabFragment.getView();
647 // Only pause the WebViews if they exist (they won't when the app is first created).
648 if (fragmentView != null) {
649 // Get the nested scroll WebView from the tab fragment.
650 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
652 // Pause the nested scroll WebView.
653 nestedScrollWebView.onPause();
655 // Pause the nested scroll WebView JavaScript timers.
656 nestedScrollWebView.pauseTimers();
660 // Pause the ad or it will continue to consume resources in the background on the free flavor.
661 if (BuildConfig.FLAVOR.contentEquals("free")) {
663 AdHelper.pauseAd(findViewById(R.id.adview));
668 public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
669 // Run the default commands.
670 super.onSaveInstanceState(savedInstanceState);
672 // Create the saved state array lists.
673 ArrayList<Bundle> savedStateArrayList = new ArrayList<>();
674 ArrayList<Bundle> savedNestedScrollWebViewStateArrayList = new ArrayList<>();
676 // Get the URLs from each tab.
677 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
678 // Get the WebView tab fragment.
679 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
681 // Get the fragment view.
682 View fragmentView = webViewTabFragment.getView();
684 if (fragmentView != null) {
685 // Get the nested scroll WebView from the tab fragment.
686 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
688 // Create saved state bundle.
689 Bundle savedStateBundle = new Bundle();
691 // Get the current states.
692 nestedScrollWebView.saveState(savedStateBundle);
693 Bundle savedNestedScrollWebViewStateBundle = nestedScrollWebView.saveNestedScrollWebViewState();
695 // Store the saved states in the array lists.
696 savedStateArrayList.add(savedStateBundle);
697 savedNestedScrollWebViewStateArrayList.add(savedNestedScrollWebViewStateBundle);
701 // Get the current tab position.
702 int currentTabPosition = tabLayout.getSelectedTabPosition();
704 // Store the saved states in the bundle.
705 savedInstanceState.putParcelableArrayList(SAVED_STATE_ARRAY_LIST, savedStateArrayList);
706 savedInstanceState.putParcelableArrayList(SAVED_NESTED_SCROLL_WEBVIEW_STATE_ARRAY_LIST, savedNestedScrollWebViewStateArrayList);
707 savedInstanceState.putInt(SAVED_TAB_POSITION, currentTabPosition);
708 savedInstanceState.putString(PROXY_MODE, proxyMode);
712 public void onDestroy() {
713 // Unregister the orbot status broadcast receiver if it exists.
714 if (orbotStatusBroadcastReceiver != null) {
715 this.unregisterReceiver(orbotStatusBroadcastReceiver);
718 // Close the bookmarks cursor if it exists.
719 if (bookmarksCursor != null) {
720 bookmarksCursor.close();
723 // Close the bookmarks database if it exists.
724 if (bookmarksDatabaseHelper != null) {
725 bookmarksDatabaseHelper.close();
728 // Stop populating the blocklists if the AsyncTask is running in the background.
729 if (populateBlocklists != null) {
730 populateBlocklists.cancel(true);
733 // Run the default commands.
738 public boolean onCreateOptionsMenu(Menu menu) {
739 // Inflate the menu. This adds items to the action bar if it is present.
740 getMenuInflater().inflate(R.menu.webview_options_menu, menu);
742 // Store a handle for the options menu so it can be used by `onOptionsItemSelected()` and `updatePrivacyIcons()`.
745 // Set the initial status of the privacy icons. `false` does not call `invalidateOptionsMenu` as the last step.
746 updatePrivacyIcons(false);
748 // Get handles for the menu items.
749 MenuItem toggleFirstPartyCookiesMenuItem = menu.findItem(R.id.toggle_first_party_cookies);
750 MenuItem toggleThirdPartyCookiesMenuItem = menu.findItem(R.id.toggle_third_party_cookies);
751 MenuItem toggleDomStorageMenuItem = menu.findItem(R.id.toggle_dom_storage);
752 MenuItem toggleSaveFormDataMenuItem = menu.findItem(R.id.toggle_save_form_data); // Form data can be removed once the minimum API >= 26.
753 MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data); // Form data can be removed once the minimum API >= 26.
754 MenuItem refreshMenuItem = menu.findItem(R.id.refresh);
755 MenuItem darkWebViewMenuItem = menu.findItem(R.id.dark_webview);
756 MenuItem adConsentMenuItem = menu.findItem(R.id.ad_consent);
758 // Only display third-party cookies if API >= 21
759 toggleThirdPartyCookiesMenuItem.setVisible(Build.VERSION.SDK_INT >= 21);
761 // Only display the form data menu items if the API < 26.
762 toggleSaveFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
763 clearFormDataMenuItem.setVisible(Build.VERSION.SDK_INT < 26);
765 // Disable the clear form data menu item if the API >= 26 so that the status of the main Clear Data is calculated correctly.
766 clearFormDataMenuItem.setEnabled(Build.VERSION.SDK_INT < 26);
768 // Only display the dark WebView menu item if API >= 21.
769 darkWebViewMenuItem.setVisible(Build.VERSION.SDK_INT >= 21);
771 // Only show Ad Consent if this is the free flavor.
772 adConsentMenuItem.setVisible(BuildConfig.FLAVOR.contentEquals("free"));
774 // Get the shared preferences.
775 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
777 // Get the dark theme and app bar preferences..
778 boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean("display_additional_app_bar_icons", false);
780 // 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.
781 if (displayAdditionalAppBarIcons) {
782 toggleFirstPartyCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
783 toggleDomStorageMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
784 refreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
785 } else { //Do not display the additional icons.
786 toggleFirstPartyCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
787 toggleDomStorageMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
788 refreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
791 // Replace Refresh with Stop if a URL is already loading.
792 if (currentWebView != null && currentWebView.getProgress() != 100) {
794 refreshMenuItem.setTitle(R.string.stop);
796 // Set the icon if it is displayed in the app bar.
797 if (displayAdditionalAppBarIcons) {
798 // Get the current theme status.
799 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
801 // Set the icon according to the current theme status.
802 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
803 refreshMenuItem.setIcon(R.drawable.close_day);
805 refreshMenuItem.setIcon(R.drawable.close_night);
815 public boolean onPrepareOptionsMenu(Menu menu) {
816 // Get handles for the menu items.
817 MenuItem addOrEditDomain = menu.findItem(R.id.add_or_edit_domain);
818 MenuItem firstPartyCookiesMenuItem = menu.findItem(R.id.toggle_first_party_cookies);
819 MenuItem thirdPartyCookiesMenuItem = menu.findItem(R.id.toggle_third_party_cookies);
820 MenuItem domStorageMenuItem = menu.findItem(R.id.toggle_dom_storage);
821 MenuItem saveFormDataMenuItem = menu.findItem(R.id.toggle_save_form_data); // Form data can be removed once the minimum API >= 26.
822 MenuItem clearDataMenuItem = menu.findItem(R.id.clear_data);
823 MenuItem clearCookiesMenuItem = menu.findItem(R.id.clear_cookies);
824 MenuItem clearDOMStorageMenuItem = menu.findItem(R.id.clear_dom_storage);
825 MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data); // Form data can be removed once the minimum API >= 26.
826 MenuItem blocklistsMenuItem = menu.findItem(R.id.blocklists);
827 MenuItem easyListMenuItem = menu.findItem(R.id.easylist);
828 MenuItem easyPrivacyMenuItem = menu.findItem(R.id.easyprivacy);
829 MenuItem fanboysAnnoyanceListMenuItem = menu.findItem(R.id.fanboys_annoyance_list);
830 MenuItem fanboysSocialBlockingListMenuItem = menu.findItem(R.id.fanboys_social_blocking_list);
831 MenuItem ultraListMenuItem = menu.findItem(R.id.ultralist);
832 MenuItem ultraPrivacyMenuItem = menu.findItem(R.id.ultraprivacy);
833 MenuItem blockAllThirdPartyRequestsMenuItem = menu.findItem(R.id.block_all_third_party_requests);
834 MenuItem proxyMenuItem = menu.findItem(R.id.proxy);
835 MenuItem userAgentMenuItem = menu.findItem(R.id.user_agent);
836 MenuItem fontSizeMenuItem = menu.findItem(R.id.font_size);
837 MenuItem swipeToRefreshMenuItem = menu.findItem(R.id.swipe_to_refresh);
838 MenuItem wideViewportMenuItem = menu.findItem(R.id.wide_viewport);
839 MenuItem displayImagesMenuItem = menu.findItem(R.id.display_images);
840 MenuItem darkWebViewMenuItem = menu.findItem(R.id.dark_webview);
842 // Get a handle for the cookie manager.
843 CookieManager cookieManager = CookieManager.getInstance();
845 // Initialize the current user agent string and the font size.
846 String currentUserAgent = getString(R.string.user_agent_privacy_browser);
849 // 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.
850 if (currentWebView != null) {
851 // Set the add or edit domain text.
852 if (currentWebView.getDomainSettingsApplied()) {
853 addOrEditDomain.setTitle(R.string.edit_domain_settings);
855 addOrEditDomain.setTitle(R.string.add_domain_settings);
858 // Get the current user agent from the WebView.
859 currentUserAgent = currentWebView.getSettings().getUserAgentString();
861 // Get the current font size from the
862 fontSize = currentWebView.getSettings().getTextZoom();
864 // Set the status of the menu item checkboxes.
865 domStorageMenuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
866 saveFormDataMenuItem.setChecked(currentWebView.getSettings().getSaveFormData()); // Form data can be removed once the minimum API >= 26.
867 easyListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASYLIST));
868 easyPrivacyMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASYPRIVACY));
869 fanboysAnnoyanceListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
870 fanboysSocialBlockingListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST));
871 ultraListMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRALIST));
872 ultraPrivacyMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRAPRIVACY));
873 blockAllThirdPartyRequestsMenuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
874 swipeToRefreshMenuItem.setChecked(currentWebView.getSwipeToRefresh());
875 wideViewportMenuItem.setChecked(currentWebView.getSettings().getUseWideViewPort());
876 displayImagesMenuItem.setChecked(currentWebView.getSettings().getLoadsImagesAutomatically());
878 // Initialize the display names for the blocklists with the number of blocked requests.
879 blocklistsMenuItem.setTitle(getString(R.string.blocklists) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
880 easyListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASYLIST) + " - " + getString(R.string.easylist));
881 easyPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.EASYPRIVACY) + " - " + getString(R.string.easyprivacy));
882 fanboysAnnoyanceListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST) + " - " + getString(R.string.fanboys_annoyance_list));
883 fanboysSocialBlockingListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST) + " - " + getString(R.string.fanboys_social_blocking_list));
884 ultraListMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRALIST) + " - " + getString(R.string.ultralist));
885 ultraPrivacyMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.ULTRAPRIVACY) + " - " + getString(R.string.ultraprivacy));
886 blockAllThirdPartyRequestsMenuItem.setTitle(currentWebView.getRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS) + " - " + getString(R.string.block_all_third_party_requests));
888 // Only modify third-party cookies if the API >= 21.
889 if (Build.VERSION.SDK_INT >= 21) {
890 // Set the status of the third-party cookies checkbox.
891 thirdPartyCookiesMenuItem.setChecked(cookieManager.acceptThirdPartyCookies(currentWebView));
893 // Enable third-party cookies if first-party cookies are enabled.
894 thirdPartyCookiesMenuItem.setEnabled(cookieManager.acceptCookie());
897 // Enable DOM Storage if JavaScript is enabled.
898 domStorageMenuItem.setEnabled(currentWebView.getSettings().getJavaScriptEnabled());
900 // Set the checkbox status for dark WebView if the WebView supports it.
901 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
902 darkWebViewMenuItem.setChecked(WebSettingsCompat.getForceDark(currentWebView.getSettings()) == WebSettingsCompat.FORCE_DARK_ON);
906 // Set the checked status of the first party cookies menu item.
907 firstPartyCookiesMenuItem.setChecked(cookieManager.acceptCookie());
909 // Enable Clear Cookies if there are any.
910 clearCookiesMenuItem.setEnabled(cookieManager.hasCookies());
912 // 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`.
913 String privateDataDirectoryString = getApplicationInfo().dataDir;
915 // Get a count of the number of files in the Local Storage directory.
916 File localStorageDirectory = new File (privateDataDirectoryString + "/app_webview/Local Storage/");
917 int localStorageDirectoryNumberOfFiles = 0;
918 if (localStorageDirectory.exists()) {
919 // `Objects.requireNonNull` removes a lint warning that `localStorageDirectory.list` might produce a null pointed exception if it is dereferenced.
920 localStorageDirectoryNumberOfFiles = Objects.requireNonNull(localStorageDirectory.list()).length;
923 // Get a count of the number of files in the IndexedDB directory.
924 File indexedDBDirectory = new File (privateDataDirectoryString + "/app_webview/IndexedDB");
925 int indexedDBDirectoryNumberOfFiles = 0;
926 if (indexedDBDirectory.exists()) {
927 // `Objects.requireNonNull` removes a lint warning that `indexedDBDirectory.list` might produce a null pointed exception if it is dereferenced.
928 indexedDBDirectoryNumberOfFiles = Objects.requireNonNull(indexedDBDirectory.list()).length;
931 // Enable Clear DOM Storage if there is any.
932 clearDOMStorageMenuItem.setEnabled(localStorageDirectoryNumberOfFiles > 0 || indexedDBDirectoryNumberOfFiles > 0);
934 // Enable Clear Form Data is there is any. This can be removed once the minimum API >= 26.
935 if (Build.VERSION.SDK_INT < 26) {
936 // Get the WebView database.
937 WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
939 // Enable the clear form data menu item if there is anything to clear.
940 clearFormDataMenuItem.setEnabled(webViewDatabase.hasFormData());
943 // Enable Clear Data if any of the submenu items are enabled.
944 clearDataMenuItem.setEnabled(clearCookiesMenuItem.isEnabled() || clearDOMStorageMenuItem.isEnabled() || clearFormDataMenuItem.isEnabled());
946 // Disable Fanboy's Social Blocking List menu item if Fanboy's Annoyance List is checked.
947 fanboysSocialBlockingListMenuItem.setEnabled(!fanboysAnnoyanceListMenuItem.isChecked());
949 // Set the proxy title and check the applied proxy.
951 case ProxyHelper.NONE:
952 // Set the proxy title.
953 proxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_none));
955 // Check the proxy None radio button.
956 menu.findItem(R.id.proxy_none).setChecked(true);
959 case ProxyHelper.TOR:
960 // Set the proxy title.
961 proxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_tor));
963 // Check the proxy Tor radio button.
964 menu.findItem(R.id.proxy_tor).setChecked(true);
967 case ProxyHelper.I2P:
968 // Set the proxy title.
969 proxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_i2p));
971 // Check the proxy I2P radio button.
972 menu.findItem(R.id.proxy_i2p).setChecked(true);
975 case ProxyHelper.CUSTOM:
976 // Set the proxy title.
977 proxyMenuItem.setTitle(getString(R.string.proxy) + " - " + getString(R.string.proxy_custom));
979 // Check the proxy Custom radio button.
980 menu.findItem(R.id.proxy_custom).setChecked(true);
984 // Select the current user agent menu item. A switch statement cannot be used because the user agents are not compile time constants.
985 if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[0])) { // Privacy Browser.
986 // Update the user agent menu item title.
987 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_privacy_browser));
989 // Select the Privacy Browser radio box.
990 menu.findItem(R.id.user_agent_privacy_browser).setChecked(true);
991 } else if (currentUserAgent.equals(webViewDefaultUserAgent)) { // WebView Default.
992 // Update the user agent menu item title.
993 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_webview_default));
995 // Select the WebView Default radio box.
996 menu.findItem(R.id.user_agent_webview_default).setChecked(true);
997 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[2])) { // Firefox on Android.
998 // Update the user agent menu item title.
999 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_android));
1001 // Select the Firefox on Android radio box.
1002 menu.findItem(R.id.user_agent_firefox_on_android).setChecked(true);
1003 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[3])) { // Chrome on Android.
1004 // Update the user agent menu item title.
1005 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_android));
1007 // Select the Chrome on Android radio box.
1008 menu.findItem(R.id.user_agent_chrome_on_android).setChecked(true);
1009 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[4])) { // Safari on iOS.
1010 // Update the user agent menu item title.
1011 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_ios));
1013 // Select the Safari on iOS radio box.
1014 menu.findItem(R.id.user_agent_safari_on_ios).setChecked(true);
1015 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[5])) { // Firefox on Linux.
1016 // Update the user agent menu item title.
1017 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_linux));
1019 // Select the Firefox on Linux radio box.
1020 menu.findItem(R.id.user_agent_firefox_on_linux).setChecked(true);
1021 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[6])) { // Chromium on Linux.
1022 // Update the user agent menu item title.
1023 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chromium_on_linux));
1025 // Select the Chromium on Linux radio box.
1026 menu.findItem(R.id.user_agent_chromium_on_linux).setChecked(true);
1027 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[7])) { // Firefox on Windows.
1028 // Update the user agent menu item title.
1029 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_windows));
1031 // Select the Firefox on Windows radio box.
1032 menu.findItem(R.id.user_agent_firefox_on_windows).setChecked(true);
1033 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[8])) { // Chrome on Windows.
1034 // Update the user agent menu item title.
1035 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_windows));
1037 // Select the Chrome on Windows radio box.
1038 menu.findItem(R.id.user_agent_chrome_on_windows).setChecked(true);
1039 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[9])) { // Edge on Windows.
1040 // Update the user agent menu item title.
1041 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_edge_on_windows));
1043 // Select the Edge on Windows radio box.
1044 menu.findItem(R.id.user_agent_edge_on_windows).setChecked(true);
1045 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[10])) { // Internet Explorer on Windows.
1046 // Update the user agent menu item title.
1047 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_internet_explorer_on_windows));
1049 // Select the Internet on Windows radio box.
1050 menu.findItem(R.id.user_agent_internet_explorer_on_windows).setChecked(true);
1051 } else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[11])) { // Safari on macOS.
1052 // Update the user agent menu item title.
1053 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_macos));
1055 // Select the Safari on macOS radio box.
1056 menu.findItem(R.id.user_agent_safari_on_macos).setChecked(true);
1057 } else { // Custom user agent.
1058 // Update the user agent menu item title.
1059 userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_custom));
1061 // Select the Custom radio box.
1062 menu.findItem(R.id.user_agent_custom).setChecked(true);
1065 // Set the font size title.
1066 fontSizeMenuItem.setTitle(getString(R.string.font_size) + " - " + fontSize + "%");
1068 // Run all the other default commands.
1069 super.onPrepareOptionsMenu(menu);
1071 // Display the menu.
1076 // Remove Android Studio's warning about the dangers of enabling JavaScript. We know. Oh, how we know.
1077 @SuppressLint("SetJavaScriptEnabled")
1078 public boolean onOptionsItemSelected(MenuItem menuItem) {
1079 // Get the selected menu item ID.
1080 int menuItemId = menuItem.getItemId();
1082 // Get a handle for the shared preferences.
1083 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
1085 // Get a handle for the cookie manager.
1086 CookieManager cookieManager = CookieManager.getInstance();
1088 // Run the commands that correlate to the selected menu item.
1089 switch (menuItemId) {
1090 case R.id.toggle_javascript:
1091 // Toggle the JavaScript status.
1092 currentWebView.getSettings().setJavaScriptEnabled(!currentWebView.getSettings().getJavaScriptEnabled());
1094 // Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step.
1095 updatePrivacyIcons(true);
1097 // Display a `Snackbar`.
1098 if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScrip is enabled.
1099 Snackbar.make(webViewPager, R.string.javascript_enabled, Snackbar.LENGTH_SHORT).show();
1100 } else if (cookieManager.acceptCookie()) { // JavaScript is disabled, but first-party cookies are enabled.
1101 Snackbar.make(webViewPager, R.string.javascript_disabled, Snackbar.LENGTH_SHORT).show();
1102 } else { // Privacy mode.
1103 Snackbar.make(webViewPager, R.string.privacy_mode, Snackbar.LENGTH_SHORT).show();
1106 // Reload the current WebView.
1107 currentWebView.reload();
1109 // Consume the event.
1113 if (menuItem.getTitle().equals(getString(R.string.refresh))) { // The refresh button was pushed.
1114 // Reload the current WebView.
1115 currentWebView.reload();
1116 } else { // The stop button was pushed.
1117 // Stop the loading of the WebView.
1118 currentWebView.stopLoading();
1121 // Consume the event.
1124 case R.id.bookmarks:
1125 // Open the bookmarks drawer.
1126 drawerLayout.openDrawer(GravityCompat.END);
1128 // Consume the event.
1131 case R.id.toggle_first_party_cookies:
1132 // Switch the first-party cookie status.
1133 cookieManager.setAcceptCookie(!cookieManager.acceptCookie());
1135 // Store the first-party cookie status.
1136 currentWebView.setAcceptFirstPartyCookies(cookieManager.acceptCookie());
1138 // Update the menu checkbox.
1139 menuItem.setChecked(cookieManager.acceptCookie());
1141 // Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step.
1142 updatePrivacyIcons(true);
1144 // Display a snackbar.
1145 if (cookieManager.acceptCookie()) { // First-party cookies are enabled.
1146 Snackbar.make(webViewPager, R.string.first_party_cookies_enabled, Snackbar.LENGTH_SHORT).show();
1147 } else if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScript is still enabled.
1148 Snackbar.make(webViewPager, R.string.first_party_cookies_disabled, Snackbar.LENGTH_SHORT).show();
1149 } else { // Privacy mode.
1150 Snackbar.make(webViewPager, R.string.privacy_mode, Snackbar.LENGTH_SHORT).show();
1153 // Reload the current WebView.
1154 currentWebView.reload();
1156 // Consume the event.
1159 case R.id.toggle_third_party_cookies:
1160 if (Build.VERSION.SDK_INT >= 21) {
1161 // Switch the status of thirdPartyCookiesEnabled.
1162 cookieManager.setAcceptThirdPartyCookies(currentWebView, !cookieManager.acceptThirdPartyCookies(currentWebView));
1164 // Update the menu checkbox.
1165 menuItem.setChecked(cookieManager.acceptThirdPartyCookies(currentWebView));
1167 // Display a snackbar.
1168 if (cookieManager.acceptThirdPartyCookies(currentWebView)) {
1169 Snackbar.make(webViewPager, R.string.third_party_cookies_enabled, Snackbar.LENGTH_SHORT).show();
1171 Snackbar.make(webViewPager, R.string.third_party_cookies_disabled, Snackbar.LENGTH_SHORT).show();
1174 // Reload the current WebView.
1175 currentWebView.reload();
1176 } // Else do nothing because SDK < 21.
1178 // Consume the event.
1181 case R.id.toggle_dom_storage:
1182 // Toggle the status of domStorageEnabled.
1183 currentWebView.getSettings().setDomStorageEnabled(!currentWebView.getSettings().getDomStorageEnabled());
1185 // Update the menu checkbox.
1186 menuItem.setChecked(currentWebView.getSettings().getDomStorageEnabled());
1188 // Update the privacy icon. `true` refreshes the app bar icons.
1189 updatePrivacyIcons(true);
1191 // Display a snackbar.
1192 if (currentWebView.getSettings().getDomStorageEnabled()) {
1193 Snackbar.make(webViewPager, R.string.dom_storage_enabled, Snackbar.LENGTH_SHORT).show();
1195 Snackbar.make(webViewPager, R.string.dom_storage_disabled, Snackbar.LENGTH_SHORT).show();
1198 // Reload the current WebView.
1199 currentWebView.reload();
1201 // Consume the event.
1204 // Form data can be removed once the minimum API >= 26.
1205 case R.id.toggle_save_form_data:
1206 // Switch the status of saveFormDataEnabled.
1207 currentWebView.getSettings().setSaveFormData(!currentWebView.getSettings().getSaveFormData());
1209 // Update the menu checkbox.
1210 menuItem.setChecked(currentWebView.getSettings().getSaveFormData());
1212 // Display a snackbar.
1213 if (currentWebView.getSettings().getSaveFormData()) {
1214 Snackbar.make(webViewPager, R.string.form_data_enabled, Snackbar.LENGTH_SHORT).show();
1216 Snackbar.make(webViewPager, R.string.form_data_disabled, Snackbar.LENGTH_SHORT).show();
1219 // Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step.
1220 updatePrivacyIcons(true);
1222 // Reload the current WebView.
1223 currentWebView.reload();
1225 // Consume the event.
1228 case R.id.clear_cookies:
1229 Snackbar.make(webViewPager, R.string.cookies_deleted, Snackbar.LENGTH_LONG)
1230 .setAction(R.string.undo, v -> {
1231 // Do nothing because everything will be handled by `onDismissed()` below.
1233 .addCallback(new Snackbar.Callback() {
1234 @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
1236 public void onDismissed(Snackbar snackbar, int event) {
1237 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) { // The snackbar was dismissed without the undo button being pushed.
1238 // Delete the cookies, which command varies by SDK.
1239 if (Build.VERSION.SDK_INT < 21) {
1240 cookieManager.removeAllCookie();
1242 cookieManager.removeAllCookies(null);
1249 // Consume the event.
1252 case R.id.clear_dom_storage:
1253 Snackbar.make(webViewPager, R.string.dom_storage_deleted, Snackbar.LENGTH_LONG)
1254 .setAction(R.string.undo, v -> {
1255 // Do nothing because everything will be handled by `onDismissed()` below.
1257 .addCallback(new Snackbar.Callback() {
1258 @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
1260 public void onDismissed(Snackbar snackbar, int event) {
1261 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) { // The snackbar was dismissed without the undo button being pushed.
1262 // Delete the DOM Storage.
1263 WebStorage webStorage = WebStorage.getInstance();
1264 webStorage.deleteAllData();
1266 // Initialize a handler to manually delete the DOM storage files and directories.
1267 Handler deleteDomStorageHandler = new Handler();
1269 // Setup a runnable to manually delete the DOM storage files and directories.
1270 Runnable deleteDomStorageRunnable = () -> {
1272 // Get a handle for the runtime.
1273 Runtime runtime = Runtime.getRuntime();
1275 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
1276 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
1277 String privateDataDirectoryString = getApplicationInfo().dataDir;
1279 // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
1280 Process deleteLocalStorageProcess = runtime.exec(new String[]{"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
1282 // Multiple commands must be used because `Runtime.exec()` does not like `*`.
1283 Process deleteIndexProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
1284 Process deleteQuotaManagerProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
1285 Process deleteQuotaManagerJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
1286 Process deleteDatabasesProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
1288 // Wait for the processes to finish.
1289 deleteLocalStorageProcess.waitFor();
1290 deleteIndexProcess.waitFor();
1291 deleteQuotaManagerProcess.waitFor();
1292 deleteQuotaManagerJournalProcess.waitFor();
1293 deleteDatabasesProcess.waitFor();
1294 } catch (Exception exception) {
1295 // Do nothing if an error is thrown.
1299 // Manually delete the DOM storage files after 200 milliseconds.
1300 deleteDomStorageHandler.postDelayed(deleteDomStorageRunnable, 200);
1306 // Consume the event.
1309 // Form data can be remove once the minimum API >= 26.
1310 case R.id.clear_form_data:
1311 Snackbar.make(webViewPager, R.string.form_data_deleted, Snackbar.LENGTH_LONG)
1312 .setAction(R.string.undo, v -> {
1313 // Do nothing because everything will be handled by `onDismissed()` below.
1315 .addCallback(new Snackbar.Callback() {
1316 @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
1318 public void onDismissed(Snackbar snackbar, int event) {
1319 if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) { // The snackbar was dismissed without the undo button being pushed.
1320 // Delete the form data.
1321 WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(getApplicationContext());
1322 mainWebViewDatabase.clearFormData();
1328 // Consume the event.
1332 // Toggle the EasyList status.
1333 currentWebView.enableBlocklist(NestedScrollWebView.EASYLIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.EASYLIST));
1335 // Update the menu checkbox.
1336 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASYLIST));
1338 // Reload the current WebView.
1339 currentWebView.reload();
1341 // Consume the event.
1344 case R.id.easyprivacy:
1345 // Toggle the EasyPrivacy status.
1346 currentWebView.enableBlocklist(NestedScrollWebView.EASYPRIVACY, !currentWebView.isBlocklistEnabled(NestedScrollWebView.EASYPRIVACY));
1348 // Update the menu checkbox.
1349 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.EASYPRIVACY));
1351 // Reload the current WebView.
1352 currentWebView.reload();
1354 // Consume the event.
1357 case R.id.fanboys_annoyance_list:
1358 // Toggle Fanboy's Annoyance List status.
1359 currentWebView.enableBlocklist(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1361 // Update the menu checkbox.
1362 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1364 // Update the staus of Fanboy's Social Blocking List.
1365 MenuItem fanboysSocialBlockingListMenuItem = optionsMenu.findItem(R.id.fanboys_social_blocking_list);
1366 fanboysSocialBlockingListMenuItem.setEnabled(!currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST));
1368 // Reload the current WebView.
1369 currentWebView.reload();
1371 // Consume the event.
1374 case R.id.fanboys_social_blocking_list:
1375 // Toggle Fanboy's Social Blocking List status.
1376 currentWebView.enableBlocklist(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST));
1378 // Update the menu checkbox.
1379 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST));
1381 // Reload the current WebView.
1382 currentWebView.reload();
1384 // Consume the event.
1387 case R.id.ultralist:
1388 // Toggle the UltraList status.
1389 currentWebView.enableBlocklist(NestedScrollWebView.ULTRALIST, !currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRALIST));
1391 // Update the menu checkbox.
1392 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRALIST));
1394 // Reload the current WebView.
1395 currentWebView.reload();
1397 // Consume the event.
1400 case R.id.ultraprivacy:
1401 // Toggle the UltraPrivacy status.
1402 currentWebView.enableBlocklist(NestedScrollWebView.ULTRAPRIVACY, !currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRAPRIVACY));
1404 // Update the menu checkbox.
1405 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.ULTRAPRIVACY));
1407 // Reload the current WebView.
1408 currentWebView.reload();
1410 // Consume the event.
1413 case R.id.block_all_third_party_requests:
1414 //Toggle the third-party requests blocker status.
1415 currentWebView.enableBlocklist(NestedScrollWebView.THIRD_PARTY_REQUESTS, !currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
1417 // Update the menu checkbox.
1418 menuItem.setChecked(currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
1420 // Reload the current WebView.
1421 currentWebView.reload();
1423 // Consume the event.
1426 case R.id.proxy_none:
1427 // Update the proxy mode.
1428 proxyMode = ProxyHelper.NONE;
1430 // Apply the proxy mode.
1433 // Consume the event.
1436 case R.id.proxy_tor:
1437 // Update the proxy mode.
1438 proxyMode = ProxyHelper.TOR;
1440 // Apply the proxy mode.
1443 // Consume the event.
1446 case R.id.proxy_i2p:
1447 // Update the proxy mode.
1448 proxyMode = ProxyHelper.I2P;
1450 // Apply the proxy mode.
1453 // Consume the event.
1456 case R.id.proxy_custom:
1457 // Update the proxy mode.
1458 proxyMode = ProxyHelper.CUSTOM;
1460 // Apply the proxy mode.
1463 // Consume the event.
1466 case R.id.user_agent_privacy_browser:
1467 // Update the user agent.
1468 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[0]);
1470 // Reload the current WebView.
1471 currentWebView.reload();
1473 // Consume the event.
1476 case R.id.user_agent_webview_default:
1477 // Update the user agent.
1478 currentWebView.getSettings().setUserAgentString("");
1480 // Reload the current WebView.
1481 currentWebView.reload();
1483 // Consume the event.
1486 case R.id.user_agent_firefox_on_android:
1487 // Update the user agent.
1488 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[2]);
1490 // Reload the current WebView.
1491 currentWebView.reload();
1493 // Consume the event.
1496 case R.id.user_agent_chrome_on_android:
1497 // Update the user agent.
1498 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[3]);
1500 // Reload the current WebView.
1501 currentWebView.reload();
1503 // Consume the event.
1506 case R.id.user_agent_safari_on_ios:
1507 // Update the user agent.
1508 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[4]);
1510 // Reload the current WebView.
1511 currentWebView.reload();
1513 // Consume the event.
1516 case R.id.user_agent_firefox_on_linux:
1517 // Update the user agent.
1518 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[5]);
1520 // Reload the current WebView.
1521 currentWebView.reload();
1523 // Consume the event.
1526 case R.id.user_agent_chromium_on_linux:
1527 // Update the user agent.
1528 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[6]);
1530 // Reload the current WebView.
1531 currentWebView.reload();
1533 // Consume the event.
1536 case R.id.user_agent_firefox_on_windows:
1537 // Update the user agent.
1538 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[7]);
1540 // Reload the current WebView.
1541 currentWebView.reload();
1543 // Consume the event.
1546 case R.id.user_agent_chrome_on_windows:
1547 // Update the user agent.
1548 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[8]);
1550 // Reload the current WebView.
1551 currentWebView.reload();
1553 // Consume the event.
1556 case R.id.user_agent_edge_on_windows:
1557 // Update the user agent.
1558 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[9]);
1560 // Reload the current WebView.
1561 currentWebView.reload();
1563 // Consume the event.
1566 case R.id.user_agent_internet_explorer_on_windows:
1567 // Update the user agent.
1568 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[10]);
1570 // Reload the current WebView.
1571 currentWebView.reload();
1573 // Consume the event.
1576 case R.id.user_agent_safari_on_macos:
1577 // Update the user agent.
1578 currentWebView.getSettings().setUserAgentString(getResources().getStringArray(R.array.user_agent_data)[11]);
1580 // Reload the current WebView.
1581 currentWebView.reload();
1583 // Consume the event.
1586 case R.id.user_agent_custom:
1587 // Update the user agent.
1588 currentWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
1590 // Reload the current WebView.
1591 currentWebView.reload();
1593 // Consume the event.
1596 case R.id.font_size:
1597 // Instantiate the font size dialog.
1598 DialogFragment fontSizeDialogFragment = FontSizeDialog.displayDialog(currentWebView.getSettings().getTextZoom());
1600 // Show the font size dialog.
1601 fontSizeDialogFragment.show(getSupportFragmentManager(), getString(R.string.font_size));
1603 // Consume the event.
1606 case R.id.swipe_to_refresh:
1607 // Toggle the stored status of swipe to refresh.
1608 currentWebView.setSwipeToRefresh(!currentWebView.getSwipeToRefresh());
1610 // Get a handle for the swipe refresh layout.
1611 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
1613 // Update the swipe refresh layout.
1614 if (currentWebView.getSwipeToRefresh()) { // Swipe to refresh is enabled.
1615 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
1616 swipeRefreshLayout.setEnabled(currentWebView.getY() == 0);
1617 } else { // Swipe to refresh is disabled.
1618 // Disable the swipe refresh layout.
1619 swipeRefreshLayout.setEnabled(false);
1622 // Consume the event.
1625 case R.id.wide_viewport:
1626 // Toggle the viewport.
1627 currentWebView.getSettings().setUseWideViewPort(!currentWebView.getSettings().getUseWideViewPort());
1629 // Consume the event.
1632 case R.id.display_images:
1633 if (currentWebView.getSettings().getLoadsImagesAutomatically()) { // Images are currently loaded automatically.
1634 // Disable loading of images.
1635 currentWebView.getSettings().setLoadsImagesAutomatically(false);
1637 // Reload the website to remove existing images.
1638 currentWebView.reload();
1639 } else { // Images are not currently loaded automatically.
1640 // Enable loading of images. Missing images will be loaded without the need for a reload.
1641 currentWebView.getSettings().setLoadsImagesAutomatically(true);
1644 // Consume the event.
1647 case R.id.dark_webview:
1648 // Check to see if dark WebView is supported by this WebView.
1649 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
1650 // Toggle the dark WebView setting.
1651 if (WebSettingsCompat.getForceDark(currentWebView.getSettings()) == WebSettingsCompat.FORCE_DARK_ON) { // Dark WebView is currently enabled.
1652 // Turn off dark WebView.
1653 WebSettingsCompat.setForceDark(currentWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
1654 } else { // Dark WebView is currently disabled.
1655 // turn on dark WebView.
1656 WebSettingsCompat.setForceDark(currentWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
1660 // Consume the event.
1663 case R.id.find_on_page:
1664 // Get a handle for the views.
1665 Toolbar toolbar = findViewById(R.id.toolbar);
1666 LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
1667 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
1669 // Set the minimum height of the find on page linear layout to match the toolbar.
1670 findOnPageLinearLayout.setMinimumHeight(toolbar.getHeight());
1672 // Hide the toolbar.
1673 toolbar.setVisibility(View.GONE);
1675 // Show the find on page linear layout.
1676 findOnPageLinearLayout.setVisibility(View.VISIBLE);
1678 // Display the keyboard. The app must wait 200 ms before running the command to work around a bug in Android.
1679 // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working
1680 findOnPageEditText.postDelayed(() -> {
1681 // Set the focus on `findOnPageEditText`.
1682 findOnPageEditText.requestFocus();
1684 // Get a handle for the input method manager.
1685 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
1687 // Remove the lint warning below that the input method manager might be null.
1688 assert inputMethodManager != null;
1690 // Display the keyboard. `0` sets no input flags.
1691 inputMethodManager.showSoftInput(findOnPageEditText, 0);
1694 // Consume the event.
1698 // Get a print manager instance.
1699 PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
1701 // Remove the lint error below that print manager might be null.
1702 assert printManager != null;
1704 // Create a print document adapter from the current WebView.
1705 PrintDocumentAdapter printDocumentAdapter = currentWebView.createPrintDocumentAdapter();
1707 // Print the document.
1708 printManager.print(getString(R.string.privacy_browser_web_page), printDocumentAdapter, null);
1710 // Consume the event.
1714 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
1715 new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
1716 currentWebView.getAcceptFirstPartyCookies()).execute(currentWebView.getCurrentUrl());
1718 // Consume the event.
1721 case R.id.save_archive:
1722 // Instantiate the save dialog.
1723 DialogFragment saveArchiveFragment = SaveWebpageDialog.saveWebpage(StoragePermissionDialog.SAVE_ARCHIVE, null, null, getString(R.string.webpage_mht), null,
1726 // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name.
1727 saveArchiveFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog));
1729 // Consume the event.
1732 case R.id.save_image:
1733 // Instantiate the save dialog.
1734 DialogFragment saveImageFragment = SaveWebpageDialog.saveWebpage(StoragePermissionDialog.SAVE_IMAGE, null, null, getString(R.string.webpage_png), null,
1737 // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name.
1738 saveImageFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog));
1740 // Consume the event.
1743 case R.id.add_to_homescreen:
1744 // Instantiate the create home screen shortcut dialog.
1745 DialogFragment createHomeScreenShortcutDialogFragment = CreateHomeScreenShortcutDialog.createDialog(currentWebView.getTitle(), currentWebView.getUrl(),
1746 currentWebView.getFavoriteOrDefaultIcon());
1748 // Show the create home screen shortcut dialog.
1749 createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut));
1751 // Consume the event.
1754 case R.id.view_source:
1755 // Create an intent to launch the view source activity.
1756 Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class);
1758 // Add the variables to the intent.
1759 viewSourceIntent.putExtra("user_agent", currentWebView.getSettings().getUserAgentString());
1760 viewSourceIntent.putExtra("current_url", currentWebView.getUrl());
1763 startActivity(viewSourceIntent);
1765 // Consume the event.
1768 case R.id.share_url:
1769 // Setup the share string.
1770 String shareString = currentWebView.getTitle() + " – " + currentWebView.getUrl();
1772 // Create the share intent.
1773 Intent shareIntent = new Intent(Intent.ACTION_SEND);
1775 // Add the share string to the intent.
1776 shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
1778 // Set the MIME type.
1779 shareIntent.setType("text/plain");
1781 // Set the intent to open in a new task.
1782 shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1785 startActivity(Intent.createChooser(shareIntent, getString(R.string.share_url)));
1787 // Consume the event.
1790 case R.id.open_with_app:
1791 // Open the URL with an outside app.
1792 openWithApp(currentWebView.getUrl());
1794 // Consume the event.
1797 case R.id.open_with_browser:
1798 // Open the URL with an outside browser.
1799 openWithBrowser(currentWebView.getUrl());
1801 // Consume the event.
1804 case R.id.add_or_edit_domain:
1805 if (currentWebView.getDomainSettingsApplied()) { // Edit the current domain settings.
1806 // Reapply the domain settings on returning to `MainWebViewActivity`.
1807 reapplyDomainSettingsOnRestart = true;
1809 // Create an intent to launch the domains activity.
1810 Intent domainsIntent = new Intent(this, DomainsActivity.class);
1812 // Add the extra information to the intent.
1813 domainsIntent.putExtra("load_domain", currentWebView.getDomainSettingsDatabaseId());
1814 domainsIntent.putExtra("close_on_back", true);
1815 domainsIntent.putExtra("current_url", currentWebView.getUrl());
1817 // Get the current certificate.
1818 SslCertificate sslCertificate = currentWebView.getCertificate();
1820 // Check to see if the SSL certificate is populated.
1821 if (sslCertificate != null) {
1822 // Extract the certificate to strings.
1823 String issuedToCName = sslCertificate.getIssuedTo().getCName();
1824 String issuedToOName = sslCertificate.getIssuedTo().getOName();
1825 String issuedToUName = sslCertificate.getIssuedTo().getUName();
1826 String issuedByCName = sslCertificate.getIssuedBy().getCName();
1827 String issuedByOName = sslCertificate.getIssuedBy().getOName();
1828 String issuedByUName = sslCertificate.getIssuedBy().getUName();
1829 long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
1830 long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
1832 // Add the certificate to the intent.
1833 domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
1834 domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
1835 domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
1836 domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
1837 domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
1838 domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
1839 domainsIntent.putExtra("ssl_start_date", startDateLong);
1840 domainsIntent.putExtra("ssl_end_date", endDateLong);
1843 // Check to see if the current IP addresses have been received.
1844 if (currentWebView.hasCurrentIpAddresses()) {
1845 // Add the current IP addresses to the intent.
1846 domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
1850 startActivity(domainsIntent);
1851 } else { // Add a new domain.
1852 // Apply the new domain settings on returning to `MainWebViewActivity`.
1853 reapplyDomainSettingsOnRestart = true;
1855 // Get the current domain
1856 Uri currentUri = Uri.parse(currentWebView.getUrl());
1857 String currentDomain = currentUri.getHost();
1859 // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
1860 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
1862 // Create the domain and store the database ID.
1863 int newDomainDatabaseId = domainsDatabaseHelper.addDomain(currentDomain);
1865 // Create an intent to launch the domains activity.
1866 Intent domainsIntent = new Intent(this, DomainsActivity.class);
1868 // Add the extra information to the intent.
1869 domainsIntent.putExtra("load_domain", newDomainDatabaseId);
1870 domainsIntent.putExtra("close_on_back", true);
1871 domainsIntent.putExtra("current_url", currentWebView.getUrl());
1873 // Get the current certificate.
1874 SslCertificate sslCertificate = currentWebView.getCertificate();
1876 // Check to see if the SSL certificate is populated.
1877 if (sslCertificate != null) {
1878 // Extract the certificate to strings.
1879 String issuedToCName = sslCertificate.getIssuedTo().getCName();
1880 String issuedToOName = sslCertificate.getIssuedTo().getOName();
1881 String issuedToUName = sslCertificate.getIssuedTo().getUName();
1882 String issuedByCName = sslCertificate.getIssuedBy().getCName();
1883 String issuedByOName = sslCertificate.getIssuedBy().getOName();
1884 String issuedByUName = sslCertificate.getIssuedBy().getUName();
1885 long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
1886 long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
1888 // Add the certificate to the intent.
1889 domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
1890 domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
1891 domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
1892 domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
1893 domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
1894 domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
1895 domainsIntent.putExtra("ssl_start_date", startDateLong);
1896 domainsIntent.putExtra("ssl_end_date", endDateLong);
1899 // Check to see if the current IP addresses have been received.
1900 if (currentWebView.hasCurrentIpAddresses()) {
1901 // Add the current IP addresses to the intent.
1902 domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
1906 startActivity(domainsIntent);
1909 // Consume the event.
1912 case R.id.ad_consent:
1913 // Instantiate the ad consent dialog.
1914 DialogFragment adConsentDialogFragment = new AdConsentDialog();
1916 // Display the ad consent dialog.
1917 adConsentDialogFragment.show(getSupportFragmentManager(), getString(R.string.ad_consent));
1919 // Consume the event.
1923 // Don't consume the event.
1924 return super.onOptionsItemSelected(menuItem);
1928 // removeAllCookies is deprecated, but it is required for API < 21.
1930 public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
1931 // Get the menu item ID.
1932 int menuItemId = menuItem.getItemId();
1934 // Get a handle for the shared preferences.
1935 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
1937 // Run the commands that correspond to the selected menu item.
1938 switch (menuItemId) {
1939 case R.id.clear_and_exit:
1940 // Clear and exit Privacy Browser.
1945 // Load the homepage.
1946 loadUrl(currentWebView, sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
1950 if (currentWebView.canGoBack()) {
1951 // Get the current web back forward list.
1952 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
1954 // Get the previous entry URL.
1955 String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl();
1957 // Apply the domain settings.
1958 applyDomainSettings(currentWebView, previousUrl, false, false);
1960 // Load the previous website in the history.
1961 currentWebView.goBack();
1966 if (currentWebView.canGoForward()) {
1967 // Get the current web back forward list.
1968 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
1970 // Get the next entry URL.
1971 String nextUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() + 1).getUrl();
1973 // Apply the domain settings.
1974 applyDomainSettings(currentWebView, nextUrl, false, false);
1976 // Load the next website in the history.
1977 currentWebView.goForward();
1982 // Instantiate the URL history dialog.
1983 DialogFragment urlHistoryDialogFragment = UrlHistoryDialog.loadBackForwardList(currentWebView.getWebViewFragmentId());
1985 // Show the URL history dialog.
1986 urlHistoryDialogFragment.show(getSupportFragmentManager(), getString(R.string.history));
1990 // Instantiate the open file dialog.
1991 DialogFragment openDialogFragment = new OpenDialog();
1993 // Show the open file dialog.
1994 openDialogFragment.show(getSupportFragmentManager(), getString(R.string.open));
1998 // Populate the resource requests.
1999 RequestsActivity.resourceRequests = currentWebView.getResourceRequests();
2001 // Create an intent to launch the Requests activity.
2002 Intent requestsIntent = new Intent(this, RequestsActivity.class);
2004 // Add the block third-party requests status to the intent.
2005 requestsIntent.putExtra("block_all_third_party_requests", currentWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS));
2008 startActivity(requestsIntent);
2011 case R.id.downloads:
2012 // Launch the system Download Manager.
2013 Intent downloadManagerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2015 // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
2016 downloadManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2019 startActivity(downloadManagerIntent);
2023 // Set the flag to reapply the domain settings on restart when returning from Domain Settings.
2024 reapplyDomainSettingsOnRestart = true;
2026 // Launch the domains activity.
2027 Intent domainsIntent = new Intent(this, DomainsActivity.class);
2029 // Add the extra information to the intent.
2030 domainsIntent.putExtra("current_url", currentWebView.getUrl());
2032 // Get the current certificate.
2033 SslCertificate sslCertificate = currentWebView.getCertificate();
2035 // Check to see if the SSL certificate is populated.
2036 if (sslCertificate != null) {
2037 // Extract the certificate to strings.
2038 String issuedToCName = sslCertificate.getIssuedTo().getCName();
2039 String issuedToOName = sslCertificate.getIssuedTo().getOName();
2040 String issuedToUName = sslCertificate.getIssuedTo().getUName();
2041 String issuedByCName = sslCertificate.getIssuedBy().getCName();
2042 String issuedByOName = sslCertificate.getIssuedBy().getOName();
2043 String issuedByUName = sslCertificate.getIssuedBy().getUName();
2044 long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
2045 long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
2047 // Add the certificate to the intent.
2048 domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
2049 domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
2050 domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
2051 domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
2052 domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
2053 domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
2054 domainsIntent.putExtra("ssl_start_date", startDateLong);
2055 domainsIntent.putExtra("ssl_end_date", endDateLong);
2058 // Check to see if the current IP addresses have been received.
2059 if (currentWebView.hasCurrentIpAddresses()) {
2060 // Add the current IP addresses to the intent.
2061 domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
2065 startActivity(domainsIntent);
2069 // Set the flag to reapply app settings on restart when returning from Settings.
2070 reapplyAppSettingsOnRestart = true;
2072 // Set the flag to reapply the domain settings on restart when returning from Settings.
2073 reapplyDomainSettingsOnRestart = true;
2075 // Launch the settings activity.
2076 Intent settingsIntent = new Intent(this, SettingsActivity.class);
2077 startActivity(settingsIntent);
2080 case R.id.import_export:
2081 // Launch the import/export activity.
2082 Intent importExportIntent = new Intent (this, ImportExportActivity.class);
2083 startActivity(importExportIntent);
2087 // Launch the logcat activity.
2088 Intent logcatIntent = new Intent(this, LogcatActivity.class);
2089 startActivity(logcatIntent);
2093 // Launch `GuideActivity`.
2094 Intent guideIntent = new Intent(this, GuideActivity.class);
2095 startActivity(guideIntent);
2099 // Create an intent to launch the about activity.
2100 Intent aboutIntent = new Intent(this, AboutActivity.class);
2102 // Create a string array for the blocklist versions.
2103 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],
2104 ultraList.get(0).get(0)[0], ultraPrivacy.get(0).get(0)[0]};
2106 // Add the blocklist versions to the intent.
2107 aboutIntent.putExtra("blocklist_versions", blocklistVersions);
2110 startActivity(aboutIntent);
2114 // Close the navigation drawer.
2115 drawerLayout.closeDrawer(GravityCompat.START);
2120 public void onPostCreate(Bundle savedInstanceState) {
2121 // Run the default commands.
2122 super.onPostCreate(savedInstanceState);
2124 // Sync the state of the DrawerToggle after the default `onRestoreInstanceState()` has finished. This creates the navigation drawer icon.
2125 actionBarDrawerToggle.syncState();
2129 public void onConfigurationChanged(@NonNull Configuration newConfig) {
2130 // Run the default commands.
2131 super.onConfigurationChanged(newConfig);
2133 // Reload the ad for the free flavor if not in full screen mode.
2134 if (BuildConfig.FLAVOR.contentEquals("free") && !inFullScreenBrowsingMode) {
2135 // Reload the ad. The AdView is destroyed and recreated, which changes the ID, every time it is reloaded to handle possible rotations.
2136 AdHelper.loadAd(findViewById(R.id.adview), getApplicationContext(), getString(R.string.ad_unit_id));
2139 // `invalidateOptionsMenu` should recalculate the number of action buttons from the menu to display on the app bar, but it doesn't because of the this bug:
2140 // https://code.google.com/p/android/issues/detail?id=20493#c8
2141 // ActivityCompat.invalidateOptionsMenu(this);
2145 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
2146 // Store the hit test result.
2147 final WebView.HitTestResult hitTestResult = currentWebView.getHitTestResult();
2149 // Define the URL strings.
2150 final String imageUrl;
2151 final String linkUrl;
2153 // Get handles for the system managers.
2154 final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
2156 // Remove the lint errors below that the clipboard manager might be null.
2157 assert clipboardManager != null;
2159 // Process the link according to the type.
2160 switch (hitTestResult.getType()) {
2161 // `SRC_ANCHOR_TYPE` is a link.
2162 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
2163 // Get the target URL.
2164 linkUrl = hitTestResult.getExtra();
2166 // Set the target URL as the title of the `ContextMenu`.
2167 menu.setHeaderTitle(linkUrl);
2169 // Add an Open in New Tab entry.
2170 menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2171 // Load the link URL in a new tab and move to it.
2172 addNewTab(linkUrl, true);
2174 // Consume the event.
2178 // Add an Open in Background entry.
2179 menu.add(R.string.open_in_background).setOnMenuItemClickListener((MenuItem item) -> {
2180 // Load the link URL in a new tab but do not move to it.
2181 addNewTab(linkUrl, false);
2183 // Consume the event.
2187 // Add an Open with App entry.
2188 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2189 openWithApp(linkUrl);
2191 // Consume the event.
2195 // Add an Open with Browser entry.
2196 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2197 openWithBrowser(linkUrl);
2199 // Consume the event.
2203 // Add a Copy URL entry.
2204 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2205 // Save the link URL in a `ClipData`.
2206 ClipData srcAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), linkUrl);
2208 // Set the `ClipData` as the clipboard's primary clip.
2209 clipboardManager.setPrimaryClip(srcAnchorTypeClipData);
2211 // Consume the event.
2215 // Add a Save URL entry.
2216 menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> {
2217 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2218 new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
2219 currentWebView.getAcceptFirstPartyCookies()).execute(linkUrl);
2221 // Consume the event.
2225 // Add an empty Cancel entry, which by default closes the context menu.
2226 menu.add(R.string.cancel);
2229 // `IMAGE_TYPE` is an image.
2230 case WebView.HitTestResult.IMAGE_TYPE:
2231 // Get the image URL.
2232 imageUrl = hitTestResult.getExtra();
2234 // Set the image URL as the title of the context menu.
2235 menu.setHeaderTitle(imageUrl);
2237 // Add an Open in New Tab entry.
2238 menu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2239 // Load the image in a new tab.
2240 addNewTab(imageUrl, true);
2242 // Consume the event.
2246 // Add an Open with App entry.
2247 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2248 // Open the image URL with an external app.
2249 openWithApp(imageUrl);
2251 // Consume the event.
2255 // Add an Open with Browser entry.
2256 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2257 // Open the image URL with an external browser.
2258 openWithBrowser(imageUrl);
2260 // Consume the event.
2264 // Add a View Image entry.
2265 menu.add(R.string.view_image).setOnMenuItemClickListener(item -> {
2266 // Load the image in the current tab.
2267 loadUrl(currentWebView, imageUrl);
2269 // Consume the event.
2273 // Add a Save Image entry.
2274 menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> {
2275 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2276 new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
2277 currentWebView.getAcceptFirstPartyCookies()).execute(imageUrl);
2279 // Consume the event.
2283 // Add a Copy URL entry.
2284 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2285 // Save the image URL in a clip data.
2286 ClipData imageTypeClipData = ClipData.newPlainText(getString(R.string.url), imageUrl);
2288 // Set the clip data as the clipboard's primary clip.
2289 clipboardManager.setPrimaryClip(imageTypeClipData);
2291 // Consume the event.
2295 // Add an empty Cancel entry, which by default closes the context menu.
2296 menu.add(R.string.cancel);
2299 // `SRC_IMAGE_ANCHOR_TYPE` is an image that is also a link.
2300 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
2301 // Get the image URL.
2302 imageUrl = hitTestResult.getExtra();
2304 // Instantiate a handler.
2305 Handler handler = new Handler();
2307 // Get a message from the handler.
2308 Message message = handler.obtainMessage();
2310 // Request the image details from the last touched node be returned in the message.
2311 currentWebView.requestFocusNodeHref(message);
2313 // Get the link URL from the message data.
2314 linkUrl = message.getData().getString("url");
2316 // Set the link URL as the title of the context menu.
2317 menu.setHeaderTitle(linkUrl);
2319 // Add an Open in New Tab entry.
2320 menu.add(R.string.open_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2321 // Load the link URL in a new tab and move to it.
2322 addNewTab(linkUrl, true);
2324 // Consume the event.
2328 // Add an Open in Background entry.
2329 menu.add(R.string.open_in_background).setOnMenuItemClickListener((MenuItem item) -> {
2330 // Lod the link URL in a new tab but do not move to it.
2331 addNewTab(linkUrl, false);
2333 // Consume the event.
2337 // Add an Open Image in New Tab entry.
2338 menu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener((MenuItem item) -> {
2339 // Load the image in a new tab and move to it.
2340 addNewTab(imageUrl, true);
2342 // Consume the event.
2346 // Add an Open with App entry.
2347 menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
2348 // Open the link URL with an external app.
2349 openWithApp(linkUrl);
2351 // Consume the event.
2355 // Add an Open with Browser entry.
2356 menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
2357 // Open the link URL with an external browser.
2358 openWithBrowser(linkUrl);
2360 // Consume the event.
2364 // Add a View Image entry.
2365 menu.add(R.string.view_image).setOnMenuItemClickListener((MenuItem item) -> {
2366 // View the image in the current tab.
2367 loadUrl(currentWebView, imageUrl);
2369 // Consume the event.
2373 // Add a Save Image entry.
2374 menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> {
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(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
2377 currentWebView.getAcceptFirstPartyCookies()).execute(imageUrl);
2379 // Consume the event.
2383 // Add a Copy URL entry.
2384 menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
2385 // Save the link URL in a clip data.
2386 ClipData srcImageAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), linkUrl);
2388 // Set the clip data as the clipboard's primary clip.
2389 clipboardManager.setPrimaryClip(srcImageAnchorTypeClipData);
2391 // Consume the event.
2395 // Add a Save URL entry.
2396 menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> {
2397 // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
2398 new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
2399 currentWebView.getAcceptFirstPartyCookies()).execute(linkUrl);
2401 // Consume the event.
2405 // Add an empty Cancel entry, which by default closes the context menu.
2406 menu.add(R.string.cancel);
2409 case WebView.HitTestResult.EMAIL_TYPE:
2410 // Get the target URL.
2411 linkUrl = hitTestResult.getExtra();
2413 // Set the target URL as the title of the `ContextMenu`.
2414 menu.setHeaderTitle(linkUrl);
2416 // Add a Write Email entry.
2417 menu.add(R.string.write_email).setOnMenuItemClickListener(item -> {
2418 // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
2419 Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
2421 // Parse the url and set it as the data for the `Intent`.
2422 emailIntent.setData(Uri.parse("mailto:" + linkUrl));
2424 // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser.
2425 emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2429 startActivity(emailIntent);
2430 } catch (ActivityNotFoundException exception) {
2431 // Display a snackbar.
2432 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
2435 // Consume the event.
2439 // Add a Copy Email Address entry.
2440 menu.add(R.string.copy_email_address).setOnMenuItemClickListener(item -> {
2441 // Save the email address in a `ClipData`.
2442 ClipData srcEmailTypeClipData = ClipData.newPlainText(getString(R.string.email_address), linkUrl);
2444 // Set the `ClipData` as the clipboard's primary clip.
2445 clipboardManager.setPrimaryClip(srcEmailTypeClipData);
2447 // Consume the event.
2451 // Add an empty Cancel entry, which by default closes the context menu.
2452 menu.add(R.string.cancel);
2458 public void onCreateBookmark(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
2459 // Get a handle for the bookmarks list view.
2460 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
2463 Dialog dialog = dialogFragment.getDialog();
2465 // Remove the incorrect lint warning below that the dialog might be null.
2466 assert dialog != null;
2468 // Get the views from the dialog fragment.
2469 EditText createBookmarkNameEditText = dialog.findViewById(R.id.create_bookmark_name_edittext);
2470 EditText createBookmarkUrlEditText = dialog.findViewById(R.id.create_bookmark_url_edittext);
2472 // Extract the strings from the edit texts.
2473 String bookmarkNameString = createBookmarkNameEditText.getText().toString();
2474 String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
2476 // Create a favorite icon byte array output stream.
2477 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
2479 // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2480 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
2482 // Convert the favorite icon byte array stream to a byte array.
2483 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
2485 // Display the new bookmark below the current items in the (0 indexed) list.
2486 int newBookmarkDisplayOrder = bookmarksListView.getCount();
2488 // Create the bookmark.
2489 bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentBookmarksFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
2491 // Update the bookmarks cursor with the current contents of this folder.
2492 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2494 // Update the list view.
2495 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2497 // Scroll to the new bookmark.
2498 bookmarksListView.setSelection(newBookmarkDisplayOrder);
2502 public void onCreateBookmarkFolder(DialogFragment dialogFragment, @NonNull Bitmap favoriteIconBitmap) {
2503 // Get a handle for the bookmarks list view.
2504 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
2507 Dialog dialog = dialogFragment.getDialog();
2509 // Remove the incorrect lint warning below that the dialog might be null.
2510 assert dialog != null;
2512 // Get handles for the views in the dialog fragment.
2513 EditText createFolderNameEditText = dialog.findViewById(R.id.create_folder_name_edittext);
2514 RadioButton defaultFolderIconRadioButton = dialog.findViewById(R.id.create_folder_default_icon_radiobutton);
2515 ImageView folderIconImageView = dialog.findViewById(R.id.create_folder_default_icon);
2517 // Get new folder name string.
2518 String folderNameString = createFolderNameEditText.getText().toString();
2520 // Create a folder icon bitmap.
2521 Bitmap folderIconBitmap;
2523 // Set the folder icon bitmap according to the dialog.
2524 if (defaultFolderIconRadioButton.isChecked()) { // Use the default folder icon.
2525 // Get the default folder icon drawable.
2526 Drawable folderIconDrawable = folderIconImageView.getDrawable();
2528 // Convert the folder icon drawable to a bitmap drawable.
2529 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2531 // Convert the folder icon bitmap drawable to a bitmap.
2532 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2533 } else { // Use the WebView favorite icon.
2534 // Copy the favorite icon bitmap to the folder icon bitmap.
2535 folderIconBitmap = favoriteIconBitmap;
2538 // Create a folder icon byte array output stream.
2539 ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
2541 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2542 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
2544 // Convert the folder icon byte array stream to a byte array.
2545 byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
2547 // Move all the bookmarks down one in the display order.
2548 for (int i = 0; i < bookmarksListView.getCount(); i++) {
2549 int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
2550 bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1);
2553 // Create the folder, which will be placed at the top of the `ListView`.
2554 bookmarksDatabaseHelper.createFolder(folderNameString, currentBookmarksFolder, folderIconByteArray);
2556 // Update the bookmarks cursor with the current contents of this folder.
2557 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2559 // Update the `ListView`.
2560 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2562 // Scroll to the new folder.
2563 bookmarksListView.setSelection(0);
2567 public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId, Bitmap favoriteIconBitmap) {
2569 Dialog dialog = dialogFragment.getDialog();
2571 // Remove the incorrect lint warning below that the dialog might be null.
2572 assert dialog != null;
2574 // Get handles for the views from `dialogFragment`.
2575 EditText editFolderNameEditText = dialog.findViewById(R.id.edit_folder_name_edittext);
2576 RadioButton currentFolderIconRadioButton = dialog.findViewById(R.id.edit_folder_current_icon_radiobutton);
2577 RadioButton defaultFolderIconRadioButton = dialog.findViewById(R.id.edit_folder_default_icon_radiobutton);
2578 ImageView defaultFolderIconImageView = dialog.findViewById(R.id.edit_folder_default_icon_imageview);
2580 // Get the new folder name.
2581 String newFolderNameString = editFolderNameEditText.getText().toString();
2583 // Check if the favorite icon has changed.
2584 if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed.
2585 // Update the name in the database.
2586 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
2587 } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
2588 // Create the new folder icon Bitmap.
2589 Bitmap folderIconBitmap;
2591 // Populate the new folder icon bitmap.
2592 if (defaultFolderIconRadioButton.isChecked()) {
2593 // Get the default folder icon drawable.
2594 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
2596 // Convert the folder icon drawable to a bitmap drawable.
2597 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2599 // Convert the folder icon bitmap drawable to a bitmap.
2600 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2601 } else { // Use the `WebView` favorite icon.
2602 // Copy the favorite icon bitmap to the folder icon bitmap.
2603 folderIconBitmap = favoriteIconBitmap;
2606 // Create a folder icon byte array output stream.
2607 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
2609 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2610 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
2612 // Convert the folder icon byte array stream to a byte array.
2613 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
2615 // Update the folder icon in the database.
2616 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
2617 } else { // The folder icon and the name have changed.
2618 // Get the new folder icon `Bitmap`.
2619 Bitmap folderIconBitmap;
2620 if (defaultFolderIconRadioButton.isChecked()) {
2621 // Get the default folder icon drawable.
2622 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
2624 // Convert the folder icon drawable to a bitmap drawable.
2625 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
2627 // Convert the folder icon bitmap drawable to a bitmap.
2628 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
2629 } else { // Use the `WebView` favorite icon.
2630 // Copy the favorite icon bitmap to the folder icon bitmap.
2631 folderIconBitmap = favoriteIconBitmap;
2634 // Create a folder icon byte array output stream.
2635 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
2637 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
2638 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
2640 // Convert the folder icon byte array stream to a byte array.
2641 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
2643 // Update the folder name and icon in the database.
2644 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
2647 // Update the bookmarks cursor with the current contents of this folder.
2648 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
2650 // Update the `ListView`.
2651 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
2654 // Override `onBackPressed()` to handle the navigation drawer and and the WebViews.
2656 public void onBackPressed() {
2657 // Check the different options for processing `back`.
2658 if (drawerLayout.isDrawerVisible(GravityCompat.START)) { // The navigation drawer is open.
2659 // Close the navigation drawer.
2660 drawerLayout.closeDrawer(GravityCompat.START);
2661 } else if (drawerLayout.isDrawerVisible(GravityCompat.END)){ // The bookmarks drawer is open.
2662 // close the bookmarks drawer.
2663 drawerLayout.closeDrawer(GravityCompat.END);
2664 } else if (displayingFullScreenVideo) { // A full screen video is shown.
2665 // Get a handle for the layouts.
2666 FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout);
2667 RelativeLayout mainContentRelativeLayout = findViewById(R.id.main_content_relativelayout);
2668 FrameLayout fullScreenVideoFrameLayout = findViewById(R.id.full_screen_video_framelayout);
2670 // Re-enable the screen timeout.
2671 fullScreenVideoFrameLayout.setKeepScreenOn(false);
2673 // Unset the full screen video flag.
2674 displayingFullScreenVideo = false;
2676 // Remove all the views from the full screen video frame layout.
2677 fullScreenVideoFrameLayout.removeAllViews();
2679 // Hide the full screen video frame layout.
2680 fullScreenVideoFrameLayout.setVisibility(View.GONE);
2682 // Enable the sliding drawers.
2683 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
2685 // Show the main content relative layout.
2686 mainContentRelativeLayout.setVisibility(View.VISIBLE);
2688 // Apply the appropriate full screen mode flags.
2689 if (fullScreenBrowsingModeEnabled && inFullScreenBrowsingMode) { // Privacy Browser is currently in full screen browsing mode.
2690 // Hide the banner ad in the free flavor.
2691 if (BuildConfig.FLAVOR.contentEquals("free")) {
2692 AdHelper.hideAd(findViewById(R.id.adview));
2695 /* Hide the system bars.
2696 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
2697 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
2698 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
2699 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
2701 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
2702 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
2704 // Reload the website if the app bar is hidden. Otherwise, there is some bug in Android that causes the WebView to be entirely black.
2706 // Reload the WebView.
2707 currentWebView.reload();
2709 } else { // Switch to normal viewing mode.
2710 // Remove the `SYSTEM_UI` flags from the root frame layout.
2711 rootFrameLayout.setSystemUiVisibility(0);
2714 // Reload the ad for the free flavor if not in full screen mode.
2715 if (BuildConfig.FLAVOR.contentEquals("free") && !inFullScreenBrowsingMode) {
2717 AdHelper.loadAd(findViewById(R.id.adview), getApplicationContext(), getString(R.string.ad_unit_id));
2719 } else if (currentWebView.canGoBack()) { // There is at least one item in the current WebView history.
2720 // Get the current web back forward list.
2721 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
2723 // Get the previous entry URL.
2724 String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl();
2726 // Apply the domain settings.
2727 applyDomainSettings(currentWebView, previousUrl, false, false);
2730 currentWebView.goBack();
2731 } else if (tabLayout.getTabCount() > 1) { // There are at least two tabs.
2732 // Close the current tab.
2734 } else { // There isn't anything to do in Privacy Browser.
2735 // Close Privacy Browser. `finishAndRemoveTask()` also removes Privacy Browser from the recent app list.
2736 if (Build.VERSION.SDK_INT >= 21) {
2737 finishAndRemoveTask();
2742 // Manually kill Privacy Browser. Otherwise, it is glitchy when restarted.
2747 // Process the results of a file browse.
2749 public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
2750 // Run the default commands.
2751 super.onActivityResult(requestCode, resultCode, returnedIntent);
2753 // Run the commands that correlate to the specified request code.
2754 switch (requestCode) {
2755 case BROWSE_FILE_UPLOAD_REQUEST_CODE:
2756 // File uploads only work on API >= 21.
2757 if (Build.VERSION.SDK_INT >= 21) {
2758 // Pass the file to the WebView.
2759 fileChooserCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, returnedIntent));
2763 case BROWSE_SAVE_WEBPAGE_REQUEST_CODE:
2764 // Don't do anything if the user pressed back from the file picker.
2765 if (resultCode == Activity.RESULT_OK) {
2766 // Get a handle for the save dialog fragment.
2767 DialogFragment saveWebpageDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_dialog));
2769 // Only update the file name if the dialog still exists.
2770 if (saveWebpageDialogFragment != null) {
2771 // Get a handle for the save webpage dialog.
2772 Dialog saveWebpageDialog = saveWebpageDialogFragment.getDialog();
2774 // Remove the incorrect lint warning below that the dialog might be null.
2775 assert saveWebpageDialog != null;
2777 // Get a handle for the file name edit text.
2778 EditText fileNameEditText = saveWebpageDialog.findViewById(R.id.file_name_edittext);
2779 TextView fileExistsWarningTextView = saveWebpageDialog.findViewById(R.id.file_exists_warning_textview);
2781 // Instantiate the file name helper.
2782 FileNameHelper fileNameHelper = new FileNameHelper();
2784 // Get the file path if it isn't null.
2785 if (returnedIntent.getData() != null) {
2786 // Convert the file name URI to a file name path.
2787 String fileNamePath = fileNameHelper.convertUriToFileNamePath(returnedIntent.getData());
2789 // Set the file name path as the text of the file name edit text.
2790 fileNameEditText.setText(fileNamePath);
2792 // Move the cursor to the end of the file name edit text.
2793 fileNameEditText.setSelection(fileNamePath.length());
2795 // Hide the file exists warning.
2796 fileExistsWarningTextView.setVisibility(View.GONE);
2802 case BROWSE_OPEN_REQUEST_CODE:
2803 // Don't do anything if the user pressed back from the file picker.
2804 if (resultCode == Activity.RESULT_OK) {
2805 // Get a handle for the open dialog fragment.
2806 DialogFragment openDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.open));
2808 // Only update the file name if the dialog still exists.
2809 if (openDialogFragment != null) {
2810 // Get a handle for the open dialog.
2811 Dialog openDialog = openDialogFragment.getDialog();
2813 // Remove the incorrect lint warning below that the dialog might be null.
2814 assert openDialog != null;
2816 // Get a handle for the file name edit text.
2817 EditText fileNameEditText = openDialog.findViewById(R.id.file_name_edittext);
2819 // Instantiate the file name helper.
2820 FileNameHelper fileNameHelper = new FileNameHelper();
2822 // Get the file path if it isn't null.
2823 if (returnedIntent.getData() != null) {
2824 // Convert the file name URI to a file name path.
2825 String fileNamePath = fileNameHelper.convertUriToFileNamePath(returnedIntent.getData());
2827 // Set the file name path as the text of the file name edit text.
2828 fileNameEditText.setText(fileNamePath);
2830 // Move the cursor to the end of the file name edit text.
2831 fileNameEditText.setSelection(fileNamePath.length());
2839 private void loadUrlFromTextBox() {
2840 // Get a handle for the URL edit text.
2841 EditText urlEditText = findViewById(R.id.url_edittext);
2843 // Get the text from urlTextBox and convert it to a string. trim() removes white spaces from the beginning and end of the string.
2844 String unformattedUrlString = urlEditText.getText().toString().trim();
2846 // Initialize the formatted URL string.
2849 // Check to see if `unformattedUrlString` is a valid URL. Otherwise, convert it into a search.
2850 if (unformattedUrlString.startsWith("content://")) { // This is a Content URL.
2851 // Load the entire content URL.
2852 url = unformattedUrlString;
2853 } else if (Patterns.WEB_URL.matcher(unformattedUrlString).matches() || unformattedUrlString.startsWith("http://") || unformattedUrlString.startsWith("https://") ||
2854 unformattedUrlString.startsWith("file://")) { // This is a standard URL.
2855 // Add `https://` at the beginning if there is no protocol. Otherwise the app will segfault.
2856 if (!unformattedUrlString.startsWith("http") && !unformattedUrlString.startsWith("file://") && !unformattedUrlString.startsWith("content://")) {
2857 unformattedUrlString = "https://" + unformattedUrlString;
2860 // Initialize `unformattedUrl`.
2861 URL unformattedUrl = null;
2863 // 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.
2865 unformattedUrl = new URL(unformattedUrlString);
2866 } catch (MalformedURLException e) {
2867 e.printStackTrace();
2870 // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if `.get` was called on a `null` value.
2871 String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
2872 String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
2873 String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
2874 String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
2875 String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
2878 Uri.Builder uri = new Uri.Builder();
2879 uri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
2881 // Decode the URI as a UTF-8 string in.
2883 url = URLDecoder.decode(uri.build().toString(), "UTF-8");
2884 } catch (UnsupportedEncodingException exception) {
2885 // Do nothing. The formatted URL string will remain blank.
2887 } else if (!unformattedUrlString.isEmpty()){ // This is not a URL, but rather a search string.
2888 // Create an encoded URL String.
2889 String encodedUrlString;
2891 // Sanitize the search input.
2893 encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
2894 } catch (UnsupportedEncodingException exception) {
2895 encodedUrlString = "";
2898 // Add the base search URL.
2899 url = searchURL + encodedUrlString;
2902 // 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.
2903 urlEditText.clearFocus();
2906 loadUrl(currentWebView, url);
2909 private void loadUrl(NestedScrollWebView nestedScrollWebView, String url) {
2910 // Sanitize the URL.
2911 url = sanitizeUrl(url);
2913 // Apply the domain settings.
2914 applyDomainSettings(nestedScrollWebView, url, true, false);
2917 nestedScrollWebView.loadUrl(url, customHeaders);
2920 public void findPreviousOnPage(View view) {
2921 // Go to the previous highlighted phrase on the page. `false` goes backwards instead of forwards.
2922 currentWebView.findNext(false);
2925 public void findNextOnPage(View view) {
2926 // Go to the next highlighted phrase on the page. `true` goes forwards instead of backwards.
2927 currentWebView.findNext(true);
2930 public void closeFindOnPage(View view) {
2931 // Get a handle for the views.
2932 Toolbar toolbar = findViewById(R.id.toolbar);
2933 LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
2934 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
2936 // Delete the contents of `find_on_page_edittext`.
2937 findOnPageEditText.setText(null);
2939 // Clear the highlighted phrases if the WebView is not null.
2940 if (currentWebView != null) {
2941 currentWebView.clearMatches();
2944 // Hide the find on page linear layout.
2945 findOnPageLinearLayout.setVisibility(View.GONE);
2947 // Show the toolbar.
2948 toolbar.setVisibility(View.VISIBLE);
2950 // Get a handle for the input method manager.
2951 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
2953 // Remove the lint warning below that the input method manager might be null.
2954 assert inputMethodManager != null;
2956 // Hide the keyboard.
2957 inputMethodManager.hideSoftInputFromWindow(toolbar.getWindowToken(), 0);
2961 public void onApplyNewFontSize(DialogFragment dialogFragment) {
2963 Dialog dialog = dialogFragment.getDialog();
2965 // Remove the incorrect lint warning below tha the dialog might be null.
2966 assert dialog != null;
2968 // Get a handle for the font size edit text.
2969 EditText fontSizeEditText = dialog.findViewById(R.id.font_size_edittext);
2971 // Initialize the new font size variable with the current font size.
2972 int newFontSize = currentWebView.getSettings().getTextZoom();
2974 // Get the font size from the edit text.
2976 newFontSize = Integer.parseInt(fontSizeEditText.getText().toString());
2977 } catch (Exception exception) {
2978 // If the edit text does not contain a valid font size do nothing.
2981 // Apply the new font size.
2982 currentWebView.getSettings().setTextZoom(newFontSize);
2986 public void onOpen(DialogFragment dialogFragment) {
2988 Dialog dialog = dialogFragment.getDialog();
2990 // Remove the incorrect lint warning below that the dialog might be null.
2991 assert dialog != null;
2993 // Get a handle for the file name edit text.
2994 EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
2996 // Get the file path string.
2997 openFilePath = fileNameEditText.getText().toString();
2999 // Check to see if the storage permission is needed.
3000 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
3002 currentWebView.loadUrl("file://" + openFilePath);
3003 } else { // The storage permission has not been granted.
3004 // Get the external private directory file.
3005 File externalPrivateDirectoryFile = getExternalFilesDir(null);
3007 // Remove the incorrect lint error below that the file might be null.
3008 assert externalPrivateDirectoryFile != null;
3010 // Get the external private directory string.
3011 String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
3013 // Check to see if the file path is in the external private directory.
3014 if (openFilePath.startsWith(externalPrivateDirectory)) { // the file path is in the external private directory.
3016 currentWebView.loadUrl("file://" + openFilePath);
3017 } else { // The file path is in a public directory.
3018 // Check if the user has previously denied the storage permission.
3019 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
3020 // Instantiate the storage permission alert dialog.
3021 DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(StoragePermissionDialog.OPEN);
3023 // Show the storage permission alert dialog. The permission will be requested the the dialog is closed.
3024 storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
3025 } else { // Show the permission request directly.
3026 // Request the write external storage permission. The file will be opened when it finishes.
3027 ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, StoragePermissionDialog.OPEN);
3034 public void onSaveWebpage(int saveType, DialogFragment dialogFragment) {
3036 Dialog dialog = dialogFragment.getDialog();
3038 // Remove the incorrect lint warning below that the dialog might be null.
3039 assert dialog != null;
3041 // Get a handle for the edit texts.
3042 EditText urlEditText = dialog.findViewById(R.id.url_edittext);
3043 EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
3045 // Get the strings from the edit texts.
3046 saveWebpageUrl = urlEditText.getText().toString();
3047 saveWebpageFilePath = fileNameEditText.getText().toString();
3049 // Check to see if the storage permission is needed.
3050 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
3051 //Save the webpage according to the save type.
3053 case StoragePermissionDialog.SAVE_URL:
3055 new SaveUrl(this, this, saveWebpageFilePath, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptFirstPartyCookies()).execute(saveWebpageUrl);
3058 case StoragePermissionDialog.SAVE_ARCHIVE:
3059 // Save the webpage archive.
3060 saveWebpageArchive();
3063 case StoragePermissionDialog.SAVE_IMAGE:
3064 // Save the webpage image.
3065 new SaveWebpageImage(this, this, saveWebpageFilePath, currentWebView).execute();
3068 } else { // The storage permission has not been granted.
3069 // Get the external private directory file.
3070 File externalPrivateDirectoryFile = getExternalFilesDir(null);
3072 // Remove the incorrect lint error below that the file might be null.
3073 assert externalPrivateDirectoryFile != null;
3075 // Get the external private directory string.
3076 String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
3078 // Check to see if the file path is in the external private directory.
3079 if (saveWebpageFilePath.startsWith(externalPrivateDirectory)) { // The file path is in the external private directory.
3080 // Save the webpage according to the save type.
3082 case StoragePermissionDialog.SAVE_URL:
3084 new SaveUrl(this, this, saveWebpageFilePath, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptFirstPartyCookies()).execute(saveWebpageUrl);
3087 case StoragePermissionDialog.SAVE_ARCHIVE:
3088 // Save the webpage archive.
3089 saveWebpageArchive();
3092 case StoragePermissionDialog.SAVE_IMAGE:
3093 // Save the webpage image.
3094 new SaveWebpageImage(this, this, saveWebpageFilePath, currentWebView).execute();
3097 } else { // The file path is in a public directory.
3098 // Check if the user has previously denied the storage permission.
3099 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
3100 // Instantiate the storage permission alert dialog.
3101 DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(saveType);
3103 // Show the storage permission alert dialog. The permission will be requested when the dialog is closed.
3104 storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
3105 } else { // Show the permission request directly.
3106 // Request the write external storage permission according to the save type. The URL will be saved when it finishes.
3107 ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, saveType);
3114 public void onCloseStoragePermissionDialog(int requestType) {
3115 // Request the write external storage permission according to the request type. The file will be opened when it finishes.
3116 ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestType);
3121 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
3122 //Only process the results if they exist (this method is triggered when a dialog is presented the first time for an app, but no grant results are included).
3123 if (grantResults.length > 0) {
3124 switch (requestCode) {
3125 case StoragePermissionDialog.OPEN:
3126 // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
3127 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
3129 currentWebView.loadUrl("file://" + openFilePath);
3130 } else { // The storage permission was not granted.
3131 // Display an error snackbar.
3132 Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
3135 // Reset the open file path.
3139 case StoragePermissionDialog.SAVE_URL:
3140 // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
3141 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
3142 // Save the raw URL.
3143 new SaveUrl(this, this, saveWebpageFilePath, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptFirstPartyCookies()).execute(saveWebpageUrl);
3144 } else { // The storage permission was not granted.
3145 // Display an error snackbar.
3146 Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
3149 // Reset the save strings.
3150 saveWebpageUrl = "";
3151 saveWebpageFilePath = "";
3154 case StoragePermissionDialog.SAVE_ARCHIVE:
3155 // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
3156 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
3157 // Save the webpage archive.
3158 saveWebpageArchive();
3159 } else { // The storage permission was not granted.
3160 // Display an error snackbar.
3161 Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
3164 // Reset the save webpage file path.
3165 saveWebpageFilePath = "";
3168 case StoragePermissionDialog.SAVE_IMAGE:
3169 // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
3170 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
3171 // Save the webpage image.
3172 new SaveWebpageImage(this, this, saveWebpageFilePath, currentWebView).execute();
3173 } else { // The storage permission was not granted.
3174 // Display an error snackbar.
3175 Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
3178 // Reset the save webpage file path.
3179 saveWebpageFilePath = "";
3185 private void initializeApp() {
3186 // Get a handle for the input method.
3187 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
3189 // Remove the lint warning below that the input method manager might be null.
3190 assert inputMethodManager != null;
3192 // Initialize the gray foreground color spans for highlighting the URLs. The deprecated `getResources()` must be used until API >= 23.
3193 initialGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
3194 finalGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
3196 // Get the current theme status.
3197 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
3199 // Set the red color span according to the theme.
3200 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
3201 redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
3203 redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_900));
3206 // Get handles for the URL views.
3207 EditText urlEditText = findViewById(R.id.url_edittext);
3209 // Remove the formatting from the URL edit text when the user is editing the text.
3210 urlEditText.setOnFocusChangeListener((View v, boolean hasFocus) -> {
3211 if (hasFocus) { // The user is editing the URL text box.
3212 // Remove the highlighting.
3213 urlEditText.getText().removeSpan(redColorSpan);
3214 urlEditText.getText().removeSpan(initialGrayColorSpan);
3215 urlEditText.getText().removeSpan(finalGrayColorSpan);
3216 } else { // The user has stopped editing the URL text box.
3217 // Move to the beginning of the string.
3218 urlEditText.setSelection(0);
3220 // Reapply the highlighting.
3225 // Set the go button on the keyboard to load the URL in `urlTextBox`.
3226 urlEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
3227 // If the event is a key-down event on the `enter` button, load the URL.
3228 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
3229 // Load the URL into the mainWebView and consume the event.
3230 loadUrlFromTextBox();
3232 // If the enter key was pressed, consume the event.
3235 // If any other key was pressed, do not consume the event.
3240 // Create an Orbot status broadcast receiver.
3241 orbotStatusBroadcastReceiver = new BroadcastReceiver() {
3243 public void onReceive(Context context, Intent intent) {
3244 // Store the content of the status message in `orbotStatus`.
3245 orbotStatus = intent.getStringExtra("org.torproject.android.intent.extra.STATUS");
3247 // If Privacy Browser is waiting on the proxy, load the website now that Orbot is connected.
3248 if ((orbotStatus != null) && orbotStatus.equals("ON") && waitingForProxy) {
3249 // Reset the waiting for proxy status.
3250 waitingForProxy = false;
3252 // Get a handle for the waiting for proxy dialog.
3253 DialogFragment waitingForProxyDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.waiting_for_proxy_dialog));
3255 // Dismiss the waiting for proxy dialog if it is displayed.
3256 if (waitingForProxyDialogFragment != null) {
3257 waitingForProxyDialogFragment.dismiss();
3260 // Reload existing URLs and load any URLs that are waiting for the proxy.
3261 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
3262 // Get the WebView tab fragment.
3263 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
3265 // Get the fragment view.
3266 View fragmentView = webViewTabFragment.getView();
3268 // Only process the WebViews if they exist.
3269 if (fragmentView != null) {
3270 // Get the nested scroll WebView from the tab fragment.
3271 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
3273 // Get the waiting for proxy URL string.
3274 String waitingForProxyUrlString = nestedScrollWebView.getWaitingForProxyUrlString();
3276 // Load the pending URL if it exists.
3277 if (!waitingForProxyUrlString.isEmpty()) { // A URL is waiting to be loaded.
3279 loadUrl(nestedScrollWebView, waitingForProxyUrlString);
3281 // Reset the waiting for proxy URL string.
3282 nestedScrollWebView.resetWaitingForProxyUrlString();
3283 } else { // No URL is waiting to be loaded.
3284 // Reload the existing URL.
3285 nestedScrollWebView.reload();
3293 // Register the Orbot status broadcast receiver on `this` context.
3294 this.registerReceiver(orbotStatusBroadcastReceiver, new IntentFilter("org.torproject.android.intent.action.STATUS"));
3296 // Get handles for views that need to be modified.
3297 NavigationView navigationView = findViewById(R.id.navigationview);
3298 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
3299 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
3300 FloatingActionButton launchBookmarksActivityFab = findViewById(R.id.launch_bookmarks_activity_fab);
3301 FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
3302 FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab);
3303 EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
3305 // Listen for touches on the navigation menu.
3306 navigationView.setNavigationItemSelectedListener(this);
3308 // Get handles for the navigation menu and the back and forward menu items.
3309 Menu navigationMenu = navigationView.getMenu();
3310 MenuItem navigationBackMenuItem = navigationMenu.findItem(R.id.back);
3311 MenuItem navigationForwardMenuItem = navigationMenu.findItem(R.id.forward);
3312 MenuItem navigationHistoryMenuItem = navigationMenu.findItem(R.id.history);
3313 MenuItem navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests);
3315 // Update the web view pager every time a tab is modified.
3316 webViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
3318 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
3323 public void onPageSelected(int position) {
3324 // Close the find on page bar if it is open.
3325 closeFindOnPage(null);
3327 // Set the current WebView.
3328 setCurrentWebView(position);
3330 // 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.
3331 if (tabLayout.getSelectedTabPosition() != position) {
3332 // Create a handler to select the tab.
3333 Handler selectTabHandler = new Handler();
3335 // Create a runnable to select the tab.
3336 Runnable selectTabRunnable = () -> {
3337 // Get a handle for the tab.
3338 TabLayout.Tab tab = tabLayout.getTabAt(position);
3340 // Assert that the tab is not null.
3347 // Select the tab layout after 150 milliseconds, which leaves enough time for a new tab to be inflated. TODO.
3348 selectTabHandler.postDelayed(selectTabRunnable, 150);
3353 public void onPageScrollStateChanged(int state) {
3358 // Display the View SSL Certificate dialog when the currently selected tab is reselected.
3359 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
3361 public void onTabSelected(TabLayout.Tab tab) {
3362 // Select the same page in the view pager.
3363 webViewPager.setCurrentItem(tab.getPosition());
3367 public void onTabUnselected(TabLayout.Tab tab) {
3372 public void onTabReselected(TabLayout.Tab tab) {
3373 // Instantiate the View SSL Certificate dialog.
3374 DialogFragment viewSslCertificateDialogFragment = ViewSslCertificateDialog.displayDialog(currentWebView.getWebViewFragmentId());
3376 // Display the View SSL Certificate dialog.
3377 viewSslCertificateDialogFragment.show(getSupportFragmentManager(), getString(R.string.view_ssl_certificate));
3381 // Set the launch bookmarks activity FAB to launch the bookmarks activity.
3382 launchBookmarksActivityFab.setOnClickListener(v -> {
3383 // Get a copy of the favorite icon bitmap.
3384 Bitmap favoriteIconBitmap = currentWebView.getFavoriteOrDefaultIcon();
3386 // Create a favorite icon byte array output stream.
3387 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
3389 // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
3390 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
3392 // Convert the favorite icon byte array stream to a byte array.
3393 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
3395 // Create an intent to launch the bookmarks activity.
3396 Intent bookmarksIntent = new Intent(getApplicationContext(), BookmarksActivity.class);
3398 // Add the extra information to the intent.
3399 bookmarksIntent.putExtra("current_url", currentWebView.getUrl());
3400 bookmarksIntent.putExtra("current_title", currentWebView.getTitle());
3401 bookmarksIntent.putExtra("current_folder", currentBookmarksFolder);
3402 bookmarksIntent.putExtra("favorite_icon_byte_array", favoriteIconByteArray);
3405 startActivity(bookmarksIntent);
3408 // Set the create new bookmark folder FAB to display an alert dialog.
3409 createBookmarkFolderFab.setOnClickListener(v -> {
3410 // Create a create bookmark folder dialog.
3411 DialogFragment createBookmarkFolderDialog = CreateBookmarkFolderDialog.createBookmarkFolder(currentWebView.getFavoriteOrDefaultIcon());
3413 // Show the create bookmark folder dialog.
3414 createBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.create_folder));
3417 // Set the create new bookmark FAB to display an alert dialog.
3418 createBookmarkFab.setOnClickListener(view -> {
3419 // Instantiate the create bookmark dialog.
3420 DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebView.getUrl(), currentWebView.getTitle(), currentWebView.getFavoriteOrDefaultIcon());
3422 // Display the create bookmark dialog.
3423 createBookmarkDialog.show(getSupportFragmentManager(), getString(R.string.create_bookmark));
3426 // Search for the string on the page whenever a character changes in the `findOnPageEditText`.
3427 findOnPageEditText.addTextChangedListener(new TextWatcher() {
3429 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
3434 public void onTextChanged(CharSequence s, int start, int before, int count) {
3439 public void afterTextChanged(Editable s) {
3440 // 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.
3441 if (currentWebView != null) {
3442 currentWebView.findAllAsync(findOnPageEditText.getText().toString());
3447 // Set the `check mark` button for the `findOnPageEditText` keyboard to close the soft keyboard.
3448 findOnPageEditText.setOnKeyListener((v, keyCode, event) -> {
3449 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // The `enter` key was pressed.
3450 // Hide the soft keyboard.
3451 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
3453 // Consume the event.
3455 } else { // A different key was pressed.
3456 // Do not consume the event.
3461 // Implement swipe to refresh.
3462 swipeRefreshLayout.setOnRefreshListener(() -> currentWebView.reload());
3464 // Store the default progress view offsets for use later in `initializeWebView()`.
3465 defaultProgressViewStartOffset = swipeRefreshLayout.getProgressViewStartOffset();
3466 defaultProgressViewEndOffset = swipeRefreshLayout.getProgressViewEndOffset();
3468 // Set the refresh color scheme according to the theme.
3469 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
3470 swipeRefreshLayout.setColorSchemeResources(R.color.blue_700);
3472 swipeRefreshLayout.setColorSchemeResources(R.color.violet_500);
3475 // Initialize a color background typed value.
3476 TypedValue colorBackgroundTypedValue = new TypedValue();
3478 // Get the color background from the theme.
3479 getTheme().resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true);
3481 // Get the color background int from the typed value.
3482 int colorBackgroundInt = colorBackgroundTypedValue.data;
3484 // Set the swipe refresh background color.
3485 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt);
3487 // The drawer titles identify the drawer layouts in accessibility mode.
3488 drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));
3489 drawerLayout.setDrawerTitle(GravityCompat.END, getString(R.string.bookmarks));
3491 // Initialize the bookmarks database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
3492 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
3494 // Initialize `currentBookmarksFolder`. `""` is the home folder in the database.
3495 currentBookmarksFolder = "";
3497 // Load the home folder, which is `""` in the database.
3498 loadBookmarksFolder();
3500 bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
3501 // Convert the id from long to int to match the format of the bookmarks database.
3502 int databaseId = (int) id;
3504 // Get the bookmark cursor for this ID.
3505 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseId);
3507 // Move the bookmark cursor to the first row.
3508 bookmarkCursor.moveToFirst();
3510 // Act upon the bookmark according to the type.
3511 if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder.
3512 // Store the new folder name in `currentBookmarksFolder`.
3513 currentBookmarksFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
3515 // Load the new folder.
3516 loadBookmarksFolder();
3517 } else { // The selected bookmark is not a folder.
3518 // Load the bookmark URL.
3519 loadUrl(currentWebView, bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
3521 // Close the bookmarks drawer.
3522 drawerLayout.closeDrawer(GravityCompat.END);
3525 // Close the `Cursor`.
3526 bookmarkCursor.close();
3529 bookmarksListView.setOnItemLongClickListener((parent, view, position, id) -> {
3530 // Convert the database ID from `long` to `int`.
3531 int databaseId = (int) id;
3533 // Find out if the selected bookmark is a folder.
3534 boolean isFolder = bookmarksDatabaseHelper.isFolder(databaseId);
3537 // Save the current folder name, which is used in `onSaveEditBookmarkFolder()`.
3538 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
3540 // Instantiate the edit folder bookmark dialog.
3541 DialogFragment editBookmarkFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId, currentWebView.getFavoriteOrDefaultIcon());
3543 // Show the edit folder bookmark dialog.
3544 editBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.edit_folder));
3546 // Get the bookmark cursor for this ID.
3547 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseId);
3549 // Move the bookmark cursor to the first row.
3550 bookmarkCursor.moveToFirst();
3552 // Load the bookmark in a new tab but do not switch to the tab or close the drawer.
3553 addNewTab(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)), false);
3556 // Consume the event.
3560 // The drawer listener is used to update the navigation menu.
3561 drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
3563 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
3567 public void onDrawerOpened(@NonNull View drawerView) {
3571 public void onDrawerClosed(@NonNull View drawerView) {
3575 public void onDrawerStateChanged(int newState) {
3576 if ((newState == DrawerLayout.STATE_SETTLING) || (newState == DrawerLayout.STATE_DRAGGING)) { // A drawer is opening or closing.
3577 // Update the navigation menu items if the WebView is not null.
3578 if (currentWebView != null) {
3579 navigationBackMenuItem.setEnabled(currentWebView.canGoBack());
3580 navigationForwardMenuItem.setEnabled(currentWebView.canGoForward());
3581 navigationHistoryMenuItem.setEnabled((currentWebView.canGoBack() || currentWebView.canGoForward()));
3582 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + currentWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
3584 // Hide the keyboard (if displayed).
3585 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
3588 // Clear the focus from from the URL text box and the WebView. This removes any text selection markers and context menus, which otherwise draw above the open drawers.
3589 urlEditText.clearFocus();
3590 currentWebView.clearFocus();
3595 // 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).
3596 customHeaders.put("X-Requested-With", "");
3598 // Inflate a bare WebView to get the default user agent. It is not used to render content on the screen.
3599 @SuppressLint("InflateParams") View webViewLayout = getLayoutInflater().inflate(R.layout.bare_webview, null, false);
3601 // Get a handle for the WebView.
3602 WebView bareWebView = webViewLayout.findViewById(R.id.bare_webview);
3604 // Store the default user agent.
3605 webViewDefaultUserAgent = bareWebView.getSettings().getUserAgentString();
3607 // Destroy the bare WebView.
3608 bareWebView.destroy();
3611 private void applyAppSettings() {
3612 // Get a handle for the shared preferences.
3613 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
3615 // Store the values from the shared preferences in variables.
3616 incognitoModeEnabled = sharedPreferences.getBoolean("incognito_mode", false);
3617 boolean doNotTrackEnabled = sharedPreferences.getBoolean("do_not_track", false);
3618 sanitizeGoogleAnalytics = sharedPreferences.getBoolean("google_analytics", true);
3619 sanitizeFacebookClickIds = sharedPreferences.getBoolean("facebook_click_ids", true);
3620 sanitizeTwitterAmpRedirects = sharedPreferences.getBoolean("twitter_amp_redirects", true);
3621 proxyMode = sharedPreferences.getString("proxy", getString(R.string.proxy_default_value));
3622 fullScreenBrowsingModeEnabled = sharedPreferences.getBoolean("full_screen_browsing_mode", false);
3623 hideAppBar = sharedPreferences.getBoolean("hide_app_bar", true);
3624 scrollAppBar = sharedPreferences.getBoolean("scroll_app_bar", true);
3626 // Apply the saved proxy mode if the app has been restarted.
3627 if (savedProxyMode != null) {
3628 // Apply the saved proxy mode.
3629 proxyMode = savedProxyMode;
3631 // Reset the saved proxy mode.
3632 savedProxyMode = null;
3635 // Get the search string.
3636 String searchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
3638 // Set the search string.
3639 if (searchString.equals("Custom URL")) { // A custom search string is used.
3640 searchURL = sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value));
3641 } else { // A custom search string is not used.
3642 searchURL = searchString;
3645 // Get a handle for the app compat delegate.
3646 AppCompatDelegate appCompatDelegate = getDelegate();
3648 // Get handles for the views that need to be modified.
3649 FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout);
3650 ActionBar actionBar = appCompatDelegate.getSupportActionBar();
3651 Toolbar toolbar = findViewById(R.id.toolbar);
3652 LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
3653 LinearLayout tabsLinearLayout = findViewById(R.id.tabs_linearlayout);
3654 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
3656 // Remove the incorrect lint warning below that the action bar might be null.
3657 assert actionBar != null;
3662 // Set Do Not Track status.
3663 if (doNotTrackEnabled) {
3664 customHeaders.put("DNT", "1");
3666 customHeaders.remove("DNT");
3669 // Get the current layout parameters. Using coordinator layout parameters allows the `setBehavior()` command and using app bar layout parameters allows the `setScrollFlags()` command.
3670 CoordinatorLayout.LayoutParams swipeRefreshLayoutParams = (CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams();
3671 AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
3672 AppBarLayout.LayoutParams findOnPageLayoutParams = (AppBarLayout.LayoutParams) findOnPageLinearLayout.getLayoutParams();
3673 AppBarLayout.LayoutParams tabsLayoutParams = (AppBarLayout.LayoutParams) tabsLinearLayout.getLayoutParams();
3675 // Add the scrolling behavior to the layout parameters.
3677 // Enable scrolling of the app bar.
3678 swipeRefreshLayoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior());
3679 toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
3680 findOnPageLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
3681 tabsLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
3683 // Disable scrolling of the app bar.
3684 swipeRefreshLayoutParams.setBehavior(null);
3685 toolbarLayoutParams.setScrollFlags(0);
3686 findOnPageLayoutParams.setScrollFlags(0);
3687 tabsLayoutParams.setScrollFlags(0);
3689 // Expand the app bar if it is currently collapsed.
3690 appBarLayout.setExpanded(true);
3693 // Apply the modified layout parameters.
3694 swipeRefreshLayout.setLayoutParams(swipeRefreshLayoutParams);
3695 toolbar.setLayoutParams(toolbarLayoutParams);
3696 findOnPageLinearLayout.setLayoutParams(findOnPageLayoutParams);
3697 tabsLinearLayout.setLayoutParams(tabsLayoutParams);
3699 // Set the app bar scrolling for each WebView.
3700 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
3701 // Get the WebView tab fragment.
3702 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
3704 // Get the fragment view.
3705 View fragmentView = webViewTabFragment.getView();
3707 // Only modify the WebViews if they exist.
3708 if (fragmentView != null) {
3709 // Get the nested scroll WebView from the tab fragment.
3710 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
3712 // Set the app bar scrolling.
3713 nestedScrollWebView.setNestedScrollingEnabled(scrollAppBar);
3717 // Update the full screen browsing mode settings.
3718 if (fullScreenBrowsingModeEnabled && inFullScreenBrowsingMode) { // Privacy Browser is currently in full screen browsing mode.
3719 // Update the visibility of the app bar, which might have changed in the settings.
3721 // Hide the tab linear layout.
3722 tabsLinearLayout.setVisibility(View.GONE);
3724 // Hide the action bar.
3727 // Show the tab linear layout.
3728 tabsLinearLayout.setVisibility(View.VISIBLE);
3730 // Show the action bar.
3734 // Hide the banner ad in the free flavor.
3735 if (BuildConfig.FLAVOR.contentEquals("free")) {
3736 AdHelper.hideAd(findViewById(R.id.adview));
3739 /* Hide the system bars.
3740 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
3741 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
3742 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
3743 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
3745 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
3746 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
3747 } else { // Privacy Browser is not in full screen browsing mode.
3748 // 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.
3749 inFullScreenBrowsingMode = false;
3751 // Show the tab linear layout.
3752 tabsLinearLayout.setVisibility(View.VISIBLE);
3754 // Show the action bar.
3757 // Show the banner ad in the free flavor.
3758 if (BuildConfig.FLAVOR.contentEquals("free")) {
3759 // Initialize the ads. If this isn't the first run, `loadAd()` will be automatically called instead.
3760 AdHelper.initializeAds(findViewById(R.id.adview), getApplicationContext(), getSupportFragmentManager(), getString(R.string.google_app_id), getString(R.string.ad_unit_id));
3763 // Remove the `SYSTEM_UI` flags from the root frame layout.
3764 rootFrameLayout.setSystemUiVisibility(0);
3769 public void navigateHistory(String url, int steps) {
3770 // Apply the domain settings.
3771 applyDomainSettings(currentWebView, url, false, false);
3773 // Load the history entry.
3774 currentWebView.goBackOrForward(steps);
3778 public void pinnedErrorGoBack() {
3779 // Get the current web back forward list.
3780 WebBackForwardList webBackForwardList = currentWebView.copyBackForwardList();
3782 // Get the previous entry URL.
3783 String previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex() - 1).getUrl();
3785 // Apply the domain settings.
3786 applyDomainSettings(currentWebView, previousUrl, false, false);
3789 currentWebView.goBack();
3792 // `reloadWebsite` is used if returning from the Domains activity. Otherwise JavaScript might not function correctly if it is newly enabled.
3793 @SuppressLint("SetJavaScriptEnabled")
3794 private void applyDomainSettings(NestedScrollWebView nestedScrollWebView, String url, boolean resetTab, boolean reloadWebsite) {
3795 // Store the current URL.
3796 nestedScrollWebView.setCurrentUrl(url);
3798 // Parse the URL into a URI.
3799 Uri uri = Uri.parse(url);
3801 // Extract the domain from `uri`.
3802 String newHostName = uri.getHost();
3804 // Strings don't like to be null.
3805 if (newHostName == null) {
3809 // 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.
3810 if (!nestedScrollWebView.getCurrentDomainName().equals(newHostName) || newHostName.equals("")) {
3811 // Set the new host name as the current domain name.
3812 nestedScrollWebView.setCurrentDomainName(newHostName);
3814 // Reset the ignoring of pinned domain information.
3815 nestedScrollWebView.setIgnorePinnedDomainInformation(false);
3817 // Clear any pinned SSL certificate or IP addresses.
3818 nestedScrollWebView.clearPinnedSslCertificate();
3819 nestedScrollWebView.clearPinnedIpAddresses();
3821 // Reset the favorite icon if specified.
3823 // Initialize the favorite icon.
3824 nestedScrollWebView.initializeFavoriteIcon();
3826 // Get the current page position.
3827 int currentPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
3829 // Get the corresponding tab.
3830 TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition);
3832 // Update the tab if it isn't null, which sometimes happens when restarting from the background.
3834 // Get the tab custom view.
3835 View tabCustomView = tab.getCustomView();
3837 // Remove the warning below that the tab custom view might be null.
3838 assert tabCustomView != null;
3840 // Get the tab views.
3841 ImageView tabFavoriteIconImageView = tabCustomView.findViewById(R.id.favorite_icon_imageview);
3842 TextView tabTitleTextView = tabCustomView.findViewById(R.id.title_textview);
3844 // Set the default favorite icon as the favorite icon for this tab.
3845 tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(nestedScrollWebView.getFavoriteOrDefaultIcon(), 64, 64, true));
3847 // Set the loading title text.
3848 tabTitleTextView.setText(R.string.loading);
3852 // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
3853 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
3855 // Get a full cursor from `domainsDatabaseHelper`.
3856 Cursor domainNameCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
3858 // Initialize `domainSettingsSet`.
3859 Set<String> domainSettingsSet = new HashSet<>();
3861 // Get the domain name column index.
3862 int domainNameColumnIndex = domainNameCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME);
3864 // Populate `domainSettingsSet`.
3865 for (int i = 0; i < domainNameCursor.getCount(); i++) {
3866 // Move `domainsCursor` to the current row.
3867 domainNameCursor.moveToPosition(i);
3869 // Store the domain name in `domainSettingsSet`.
3870 domainSettingsSet.add(domainNameCursor.getString(domainNameColumnIndex));
3873 // Close `domainNameCursor.
3874 domainNameCursor.close();
3876 // Initialize the domain name in database variable.
3877 String domainNameInDatabase = null;
3879 // Check the hostname against the domain settings set.
3880 if (domainSettingsSet.contains(newHostName)) { // The hostname is contained in the domain settings set.
3881 // Record the domain name in the database.
3882 domainNameInDatabase = newHostName;
3884 // Set the domain settings applied tracker to true.
3885 nestedScrollWebView.setDomainSettingsApplied(true);
3886 } else { // The hostname is not contained in the domain settings set.
3887 // Set the domain settings applied tracker to false.
3888 nestedScrollWebView.setDomainSettingsApplied(false);
3891 // Check all the subdomains of the host name against wildcard domains in the domain cursor.
3892 while (!nestedScrollWebView.getDomainSettingsApplied() && newHostName.contains(".")) { // Stop checking if domain settings are already applied or there are no more `.` in the host name.
3893 if (domainSettingsSet.contains("*." + newHostName)) { // Check the host name prepended by `*.`.
3894 // Set the domain settings applied tracker to true.
3895 nestedScrollWebView.setDomainSettingsApplied(true);
3897 // Store the applied domain names as it appears in the database.
3898 domainNameInDatabase = "*." + newHostName;
3901 // Strip out the lowest subdomain of of the host name.
3902 newHostName = newHostName.substring(newHostName.indexOf(".") + 1);
3906 // Get a handle for the shared preferences.
3907 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
3909 // Store the general preference information.
3910 String defaultFontSizeString = sharedPreferences.getString("font_size", getString(R.string.font_size_default_value));
3911 String defaultUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
3912 boolean defaultSwipeToRefresh = sharedPreferences.getBoolean("swipe_to_refresh", true);
3913 String webViewTheme = sharedPreferences.getString("webview_theme", getString(R.string.webview_theme_default_value));
3914 boolean wideViewport = sharedPreferences.getBoolean("wide_viewport", true);
3915 boolean displayWebpageImages = sharedPreferences.getBoolean("display_webpage_images", true);
3917 // Get the WebView theme entry values string array.
3918 String[] webViewThemeEntryValuesStringArray = getResources().getStringArray(R.array.webview_theme_entry_values);
3920 // Get a handle for the cookie manager.
3921 CookieManager cookieManager = CookieManager.getInstance();
3923 // Get handles for the views.
3924 RelativeLayout urlRelativeLayout = findViewById(R.id.url_relativelayout);
3925 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
3927 // Initialize the user agent array adapter and string array.
3928 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(this, R.array.user_agent_names, R.layout.spinner_item);
3929 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
3931 if (nestedScrollWebView.getDomainSettingsApplied()) { // The url has custom domain settings.
3932 // Get a cursor for the current host and move it to the first position.
3933 Cursor currentDomainSettingsCursor = domainsDatabaseHelper.getCursorForDomainName(domainNameInDatabase);
3934 currentDomainSettingsCursor.moveToFirst();
3936 // Get the settings from the cursor.
3937 nestedScrollWebView.setDomainSettingsDatabaseId(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper._ID)));
3938 nestedScrollWebView.getSettings().setJavaScriptEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)) == 1);
3939 nestedScrollWebView.setAcceptFirstPartyCookies(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES)) == 1);
3940 boolean domainThirdPartyCookiesEnabled = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES)) == 1);
3941 nestedScrollWebView.getSettings().setDomStorageEnabled(currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)) == 1);
3942 // Form data can be removed once the minimum API >= 26.
3943 boolean saveFormData = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)) == 1);
3944 nestedScrollWebView.enableBlocklist(NestedScrollWebView.EASYLIST,
3945 currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYLIST)) == 1);
3946 nestedScrollWebView.enableBlocklist(NestedScrollWebView.EASYPRIVACY,
3947 currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYPRIVACY)) == 1);
3948 nestedScrollWebView.enableBlocklist(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST,
3949 currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST)) == 1);
3950 nestedScrollWebView.enableBlocklist(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST,
3951 currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)) == 1);
3952 nestedScrollWebView.enableBlocklist(NestedScrollWebView.ULTRALIST, currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ULTRALIST)) == 1);
3953 nestedScrollWebView.enableBlocklist(NestedScrollWebView.ULTRAPRIVACY,
3954 currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)) == 1);
3955 nestedScrollWebView.enableBlocklist(NestedScrollWebView.THIRD_PARTY_REQUESTS,
3956 currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)) == 1);
3957 String userAgentName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
3958 int fontSize = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
3959 int swipeToRefreshInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH));
3960 int webViewThemeInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.WEBVIEW_THEME));
3961 int wideViewportInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.WIDE_VIEWPORT));
3962 int displayWebpageImagesInt = currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES));
3963 boolean pinnedSslCertificate = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)) == 1);
3964 String pinnedSslIssuedToCName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME));
3965 String pinnedSslIssuedToOName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION));
3966 String pinnedSslIssuedToUName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT));
3967 String pinnedSslIssuedByCName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME));
3968 String pinnedSslIssuedByOName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION));
3969 String pinnedSslIssuedByUName = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT));
3970 boolean pinnedIpAddresses = (currentDomainSettingsCursor.getInt(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_IP_ADDRESSES)) == 1);
3971 String pinnedHostIpAddresses = currentDomainSettingsCursor.getString(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.IP_ADDRESSES));
3973 // Get the pinned SSL date longs.
3974 long pinnedSslStartDateLong = currentDomainSettingsCursor.getLong(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE));
3975 long pinnedSslEndDateLong = currentDomainSettingsCursor.getLong(currentDomainSettingsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE));
3977 // Define the pinned SSL date variables.
3978 Date pinnedSslStartDate;
3979 Date pinnedSslEndDate;
3981 // Set the pinned SSL certificate start date to `null` if the saved date long is 0 because creating a new date results in an error if the input is 0.
3982 if (pinnedSslStartDateLong == 0) {
3983 pinnedSslStartDate = null;
3985 pinnedSslStartDate = new Date(pinnedSslStartDateLong);
3988 // Set the pinned SSL certificate end date to `null` if the saved date long is 0 because creating a new date results in an error if the input is 0.
3989 if (pinnedSslEndDateLong == 0) {
3990 pinnedSslEndDate = null;
3992 pinnedSslEndDate = new Date(pinnedSslEndDateLong);
3995 // Close the current host domain settings cursor.
3996 currentDomainSettingsCursor.close();
3998 // If there is a pinned SSL certificate, store it in the WebView.
3999 if (pinnedSslCertificate) {
4000 nestedScrollWebView.setPinnedSslCertificate(pinnedSslIssuedToCName, pinnedSslIssuedToOName, pinnedSslIssuedToUName, pinnedSslIssuedByCName, pinnedSslIssuedByOName, pinnedSslIssuedByUName,
4001 pinnedSslStartDate, pinnedSslEndDate);
4004 // If there is a pinned IP address, store it in the WebView.
4005 if (pinnedIpAddresses) {
4006 nestedScrollWebView.setPinnedIpAddresses(pinnedHostIpAddresses);
4009 // Apply the cookie domain settings.
4010 cookieManager.setAcceptCookie(nestedScrollWebView.getAcceptFirstPartyCookies());
4012 // Set third-party cookies status if API >= 21.
4013 if (Build.VERSION.SDK_INT >= 21) {
4014 cookieManager.setAcceptThirdPartyCookies(nestedScrollWebView, domainThirdPartyCookiesEnabled);
4017 // Apply the form data setting if the API < 26.
4018 if (Build.VERSION.SDK_INT < 26) {
4019 nestedScrollWebView.getSettings().setSaveFormData(saveFormData);
4022 // Apply the font size.
4023 try { // Try the specified font size to see if it is valid.
4024 if (fontSize == 0) { // Apply the default font size.
4025 // Try to set the font size from the value in the app settings.
4026 nestedScrollWebView.getSettings().setTextZoom(Integer.parseInt(defaultFontSizeString));
4027 } else { // Apply the font size from domain settings.
4028 nestedScrollWebView.getSettings().setTextZoom(fontSize);
4030 } catch (Exception exception) { // The specified font size is invalid
4031 // Set the font size to be 100%
4032 nestedScrollWebView.getSettings().setTextZoom(100);
4035 // Set the user agent.
4036 if (userAgentName.equals(getString(R.string.system_default_user_agent))) { // Use the system default user agent.
4037 // Get the array position of the default user agent name.
4038 int defaultUserAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName);
4040 // Set the user agent according to the system default.
4041 switch (defaultUserAgentArrayPosition) {
4042 case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list.
4043 // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names.
4044 nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName);
4047 case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
4048 // Set the user agent to `""`, which uses the default value.
4049 nestedScrollWebView.getSettings().setUserAgentString("");
4052 case SETTINGS_CUSTOM_USER_AGENT:
4053 // Set the default custom user agent.
4054 nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
4058 // Get the user agent string from the user agent data array
4059 nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[defaultUserAgentArrayPosition]);
4061 } else { // Set the user agent according to the stored name.
4062 // Get the array position of the user agent name.
4063 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
4065 switch (userAgentArrayPosition) {
4066 case UNRECOGNIZED_USER_AGENT: // The user agent name contains a custom user agent.
4067 nestedScrollWebView.getSettings().setUserAgentString(userAgentName);
4070 case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
4071 // Set the user agent to `""`, which uses the default value.
4072 nestedScrollWebView.getSettings().setUserAgentString("");
4076 // Get the user agent string from the user agent data array.
4077 nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]);
4081 // Set swipe to refresh.
4082 switch (swipeToRefreshInt) {
4083 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
4084 // Store the swipe to refresh status in the nested scroll WebView.
4085 nestedScrollWebView.setSwipeToRefresh(defaultSwipeToRefresh);
4087 // Update the swipe refresh layout.
4088 if (defaultSwipeToRefresh) { // Swipe to refresh is enabled.
4089 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
4090 swipeRefreshLayout.setEnabled(currentWebView.getY() == 0);
4091 } else { // Swipe to refresh is disabled.
4092 // Disable the swipe refresh layout.
4093 swipeRefreshLayout.setEnabled(false);
4097 case DomainsDatabaseHelper.ENABLED:
4098 // Store the swipe to refresh status in the nested scroll WebView.
4099 nestedScrollWebView.setSwipeToRefresh(true);
4101 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
4102 swipeRefreshLayout.setEnabled(currentWebView.getY() == 0);
4105 case DomainsDatabaseHelper.DISABLED:
4106 // Store the swipe to refresh status in the nested scroll WebView.
4107 nestedScrollWebView.setSwipeToRefresh(false);
4109 // Disable swipe to refresh.
4110 swipeRefreshLayout.setEnabled(false);
4113 // Check to see if WebView themes are supported.
4114 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
4115 // Set the WebView theme.
4116 switch (webViewThemeInt) {
4117 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
4118 // Set the WebView theme. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant.
4119 if (webViewTheme.equals(webViewThemeEntryValuesStringArray[1])) { // The light theme is selected.
4120 // Turn off the WebView dark mode.
4121 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4122 } else if (webViewTheme.equals(webViewThemeEntryValuesStringArray[2])) { // The dark theme is selected.
4123 // Turn on the WebView dark mode.
4124 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4125 } else { // The system default theme is selected.
4126 // Get the current system theme status.
4127 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4129 // Set the WebView theme according to the current system theme status.
4130 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The system is in day mode.
4131 // Turn off the WebView dark mode.
4132 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4133 } else { // The system is in night mode.
4134 // Turn on the WebView dark mode.
4135 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4140 case DomainsDatabaseHelper.LIGHT_THEME:
4141 // Turn off the WebView dark mode.
4142 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4145 case DomainsDatabaseHelper.DARK_THEME:
4146 // Turn on the WebView dark mode.
4147 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4152 // Set the viewport.
4153 switch (wideViewportInt) {
4154 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
4155 nestedScrollWebView.getSettings().setUseWideViewPort(wideViewport);
4158 case DomainsDatabaseHelper.ENABLED:
4159 nestedScrollWebView.getSettings().setUseWideViewPort(true);
4162 case DomainsDatabaseHelper.DISABLED:
4163 nestedScrollWebView.getSettings().setUseWideViewPort(false);
4167 // Set the loading of webpage images.
4168 switch (displayWebpageImagesInt) {
4169 case DomainsDatabaseHelper.SYSTEM_DEFAULT:
4170 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImages);
4173 case DomainsDatabaseHelper.ENABLED:
4174 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(true);
4177 case DomainsDatabaseHelper.DISABLED:
4178 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(false);
4182 // Get the current theme status.
4183 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4185 // Set a background on the URL relative layout to indicate that custom domain settings are being used.
4186 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4187 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_light_green, null));
4189 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_dark_blue, null));
4191 } else { // The new URL does not have custom domain settings. Load the defaults.
4192 // Store the values from the shared preferences.
4193 nestedScrollWebView.getSettings().setJavaScriptEnabled(sharedPreferences.getBoolean("javascript", false));
4194 nestedScrollWebView.setAcceptFirstPartyCookies(sharedPreferences.getBoolean("first_party_cookies", false));
4195 boolean defaultThirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies", false);
4196 nestedScrollWebView.getSettings().setDomStorageEnabled(sharedPreferences.getBoolean("dom_storage", false));
4197 boolean saveFormData = sharedPreferences.getBoolean("save_form_data", false); // Form data can be removed once the minimum API >= 26.
4198 nestedScrollWebView.enableBlocklist(NestedScrollWebView.EASYLIST, sharedPreferences.getBoolean("easylist", true));
4199 nestedScrollWebView.enableBlocklist(NestedScrollWebView.EASYPRIVACY, sharedPreferences.getBoolean("easyprivacy", true));
4200 nestedScrollWebView.enableBlocklist(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST, sharedPreferences.getBoolean("fanboys_annoyance_list", true));
4201 nestedScrollWebView.enableBlocklist(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST, sharedPreferences.getBoolean("fanboys_social_blocking_list", true));
4202 nestedScrollWebView.enableBlocklist(NestedScrollWebView.ULTRALIST, sharedPreferences.getBoolean("ultralist", true));
4203 nestedScrollWebView.enableBlocklist(NestedScrollWebView.ULTRAPRIVACY, sharedPreferences.getBoolean("ultraprivacy", true));
4204 nestedScrollWebView.enableBlocklist(NestedScrollWebView.THIRD_PARTY_REQUESTS, sharedPreferences.getBoolean("block_all_third_party_requests", false));
4206 // Apply the default first-party cookie setting.
4207 cookieManager.setAcceptCookie(nestedScrollWebView.getAcceptFirstPartyCookies());
4209 // Apply the default font size setting.
4211 // Try to set the font size from the value in the app settings.
4212 nestedScrollWebView.getSettings().setTextZoom(Integer.parseInt(defaultFontSizeString));
4213 } catch (Exception exception) {
4214 // If the app settings value is invalid, set the font size to 100%.
4215 nestedScrollWebView.getSettings().setTextZoom(100);
4218 // Apply the form data setting if the API < 26.
4219 if (Build.VERSION.SDK_INT < 26) {
4220 nestedScrollWebView.getSettings().setSaveFormData(saveFormData);
4223 // Store the swipe to refresh status in the nested scroll WebView.
4224 nestedScrollWebView.setSwipeToRefresh(defaultSwipeToRefresh);
4226 // Update the swipe refresh layout.
4227 if (defaultSwipeToRefresh) { // Swipe to refresh is enabled.
4228 // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes.
4229 swipeRefreshLayout.setEnabled(currentWebView.getY() == 0);
4230 } else { // Swipe to refresh is disabled.
4231 // Disable the swipe refresh layout.
4232 swipeRefreshLayout.setEnabled(false);
4235 // Reset the pinned variables.
4236 nestedScrollWebView.setDomainSettingsDatabaseId(-1);
4238 // Set third-party cookies status if API >= 21.
4239 if (Build.VERSION.SDK_INT >= 21) {
4240 cookieManager.setAcceptThirdPartyCookies(nestedScrollWebView, defaultThirdPartyCookiesEnabled);
4243 // Get the array position of the user agent name.
4244 int userAgentArrayPosition = userAgentNamesArray.getPosition(defaultUserAgentName);
4246 // Set the user agent.
4247 switch (userAgentArrayPosition) {
4248 case UNRECOGNIZED_USER_AGENT: // The default user agent name is not on the canonical list.
4249 // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names.
4250 nestedScrollWebView.getSettings().setUserAgentString(defaultUserAgentName);
4253 case SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
4254 // Set the user agent to `""`, which uses the default value.
4255 nestedScrollWebView.getSettings().setUserAgentString("");
4258 case SETTINGS_CUSTOM_USER_AGENT:
4259 // Set the default custom user agent.
4260 nestedScrollWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
4264 // Get the user agent string from the user agent data array
4265 nestedScrollWebView.getSettings().setUserAgentString(userAgentDataArray[userAgentArrayPosition]);
4268 // Apply the WebView theme if supported by the installed WebView.
4269 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
4270 // Set the WebView theme. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant.
4271 if (webViewTheme.equals(webViewThemeEntryValuesStringArray[1])) { // The light theme is selected.
4272 // Turn off the WebView dark mode.
4273 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4274 } else if (webViewTheme.equals(webViewThemeEntryValuesStringArray[2])) { // The dark theme is selected.
4275 // Turn on the WebView dark mode.
4276 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4277 } else { // The system default theme is selected.
4278 // Get the current system theme status.
4279 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4281 // Set the WebView theme according to the current system theme status.
4282 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The system is in day mode.
4283 // Turn off the WebView dark mode.
4284 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
4285 } else { // The system is in night mode.
4286 // Turn on the WebView dark mode.
4287 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
4292 // Set the viewport.
4293 nestedScrollWebView.getSettings().setUseWideViewPort(wideViewport);
4295 // Set the loading of webpage images.
4296 nestedScrollWebView.getSettings().setLoadsImagesAutomatically(displayWebpageImages);
4298 // Set a transparent background on URL edit text.
4299 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.color.transparent, null));
4302 // Close the domains database helper.
4303 domainsDatabaseHelper.close();
4305 // Update the privacy icons.
4306 updatePrivacyIcons(true);
4309 // Reload the website if returning from the Domains activity.
4310 if (reloadWebsite) {
4311 nestedScrollWebView.reload();
4315 private void applyProxy(boolean reloadWebViews) {
4316 // Set the proxy according to the mode. `this` refers to the current activity where an alert dialog might be displayed.
4317 ProxyHelper.setProxy(getApplicationContext(), appBarLayout, proxyMode);
4319 // Reset the waiting for proxy tracker.
4320 waitingForProxy = false;
4322 // Get the current theme status.
4323 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4325 // Update the user interface and reload the WebViews if requested.
4326 switch (proxyMode) {
4327 case ProxyHelper.NONE:
4328 // Initialize a color background typed value.
4329 TypedValue colorBackgroundTypedValue = new TypedValue();
4331 // Get the color background from the theme.
4332 getTheme().resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true);
4334 // Get the color background int from the typed value.
4335 int colorBackgroundInt = colorBackgroundTypedValue.data;
4337 // Set the default app bar layout background.
4338 appBarLayout.setBackgroundColor(colorBackgroundInt);
4341 case ProxyHelper.TOR:
4342 // Set the app bar background to indicate proxying through Orbot is enabled.
4343 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4344 appBarLayout.setBackgroundResource(R.color.blue_50);
4346 appBarLayout.setBackgroundResource(R.color.dark_blue_30);
4349 // Check to see if Orbot is installed.
4351 // Get the package manager.
4352 PackageManager packageManager = getPackageManager();
4354 // 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.
4355 packageManager.getPackageInfo("org.torproject.android", 0);
4357 // Check to see if the proxy is ready.
4358 if (!orbotStatus.equals("ON")) { // Orbot is not ready.
4359 // Set the waiting for proxy status.
4360 waitingForProxy = true;
4362 // Show the waiting for proxy dialog if it isn't already displayed.
4363 if (getSupportFragmentManager().findFragmentByTag(getString(R.string.waiting_for_proxy_dialog)) == null) {
4364 // Get a handle for the waiting for proxy alert dialog.
4365 DialogFragment waitingForProxyDialogFragment = new WaitingForProxyDialog();
4367 // Display the waiting for proxy alert dialog.
4368 waitingForProxyDialogFragment.show(getSupportFragmentManager(), getString(R.string.waiting_for_proxy_dialog));
4371 } catch (PackageManager.NameNotFoundException exception) { // Orbot is not installed.
4372 // Show the Orbot not installed dialog if it is not already displayed.
4373 if (getSupportFragmentManager().findFragmentByTag(getString(R.string.proxy_not_installed_dialog)) == null) {
4374 // Get a handle for the Orbot not installed alert dialog.
4375 DialogFragment orbotNotInstalledDialogFragment = ProxyNotInstalledDialog.displayDialog(proxyMode);
4377 // Display the Orbot not installed alert dialog.
4378 orbotNotInstalledDialogFragment.show(getSupportFragmentManager(), getString(R.string.proxy_not_installed_dialog));
4383 case ProxyHelper.I2P:
4384 // Set the app bar background to indicate proxying through Orbot is enabled.
4385 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4386 appBarLayout.setBackgroundResource(R.color.blue_50);
4388 appBarLayout.setBackgroundResource(R.color.dark_blue_30);
4391 // Check to see if I2P is installed.
4393 // Get the package manager.
4394 PackageManager packageManager = getPackageManager();
4396 // 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.
4397 packageManager.getPackageInfo("org.torproject.android", 0);
4398 } catch (PackageManager.NameNotFoundException exception) { // I2P is not installed.
4399 // Sow the I2P not installed dialog if it is not already displayed.
4400 if (getSupportFragmentManager().findFragmentByTag(getString(R.string.proxy_not_installed_dialog)) == null) {
4401 // Get a handle for the waiting for proxy alert dialog.
4402 DialogFragment i2pNotInstalledDialogFragment = ProxyNotInstalledDialog.displayDialog(proxyMode);
4404 // Display the I2P not installed alert dialog.
4405 i2pNotInstalledDialogFragment.show(getSupportFragmentManager(), getString(R.string.proxy_not_installed_dialog));
4410 case ProxyHelper.CUSTOM:
4411 // Set the app bar background to indicate proxying through Orbot is enabled.
4412 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4413 appBarLayout.setBackgroundResource(R.color.blue_50);
4415 appBarLayout.setBackgroundResource(R.color.dark_blue_30);
4420 // Reload the WebViews if requested and not waiting for the proxy.
4421 if (reloadWebViews && !waitingForProxy) {
4422 // Reload the WebViews.
4423 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
4424 // Get the WebView tab fragment.
4425 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
4427 // Get the fragment view.
4428 View fragmentView = webViewTabFragment.getView();
4430 // Only reload the WebViews if they exist.
4431 if (fragmentView != null) {
4432 // Get the nested scroll WebView from the tab fragment.
4433 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
4435 // Reload the WebView.
4436 nestedScrollWebView.reload();
4442 private void updatePrivacyIcons(boolean runInvalidateOptionsMenu) {
4443 // Only update the privacy icons if the options menu and the current WebView have already been populated.
4444 if ((optionsMenu != null) && (currentWebView != null)) {
4445 // Get handles for the menu items.
4446 MenuItem privacyMenuItem = optionsMenu.findItem(R.id.toggle_javascript);
4447 MenuItem firstPartyCookiesMenuItem = optionsMenu.findItem(R.id.toggle_first_party_cookies);
4448 MenuItem domStorageMenuItem = optionsMenu.findItem(R.id.toggle_dom_storage);
4449 MenuItem refreshMenuItem = optionsMenu.findItem(R.id.refresh);
4451 // Update the privacy icon.
4452 if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScript is enabled.
4453 privacyMenuItem.setIcon(R.drawable.javascript_enabled);
4454 } else if (currentWebView.getAcceptFirstPartyCookies()) { // JavaScript is disabled but cookies are enabled.
4455 privacyMenuItem.setIcon(R.drawable.warning);
4456 } else { // All the dangerous features are disabled.
4457 privacyMenuItem.setIcon(R.drawable.privacy_mode);
4460 // Get the current theme status.
4461 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
4463 // Update the first-party cookies icon.
4464 if (currentWebView.getAcceptFirstPartyCookies()) { // First-party cookies are enabled.
4465 firstPartyCookiesMenuItem.setIcon(R.drawable.cookies_enabled);
4466 } else { // First-party cookies are disabled.
4467 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4468 firstPartyCookiesMenuItem.setIcon(R.drawable.cookies_disabled_day);
4470 firstPartyCookiesMenuItem.setIcon(R.drawable.cookies_disabled_night);
4474 // Update the DOM storage icon.
4475 if (currentWebView.getSettings().getJavaScriptEnabled() && currentWebView.getSettings().getDomStorageEnabled()) { // Both JavaScript and DOM storage are enabled.
4476 domStorageMenuItem.setIcon(R.drawable.dom_storage_enabled);
4477 } else if (currentWebView.getSettings().getJavaScriptEnabled()) { // JavaScript is enabled but DOM storage is disabled.
4478 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4479 domStorageMenuItem.setIcon(R.drawable.dom_storage_disabled_day);
4481 domStorageMenuItem.setIcon(R.drawable.dom_storage_disabled_night);
4483 } else { // JavaScript is disabled, so DOM storage is ghosted.
4484 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4485 domStorageMenuItem.setIcon(R.drawable.dom_storage_ghosted_day);
4487 domStorageMenuItem.setIcon(R.drawable.dom_storage_ghosted_night);
4491 // Update the refresh icon.
4492 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
4493 refreshMenuItem.setIcon(R.drawable.refresh_enabled_day);
4495 refreshMenuItem.setIcon(R.drawable.refresh_enabled_night);
4498 // `invalidateOptionsMenu()` calls `onPrepareOptionsMenu()` and redraws the icons in the app bar.
4499 if (runInvalidateOptionsMenu) {
4500 invalidateOptionsMenu();
4505 private void highlightUrlText() {
4506 // Get a handle for the URL edit text.
4507 EditText urlEditText = findViewById(R.id.url_edittext);
4509 // Only highlight the URL text if the box is not currently selected.
4510 if (!urlEditText.hasFocus()) {
4511 // Get the URL string.
4512 String urlString = urlEditText.getText().toString();
4514 // Highlight the URL according to the protocol.
4515 if (urlString.startsWith("file://") || urlString.startsWith("content://")) { // This is a file or content URL.
4516 // De-emphasize everything before the file name.
4517 urlEditText.getText().setSpan(initialGrayColorSpan, 0, urlString.lastIndexOf("/") + 1,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4518 } else { // This is a web URL.
4519 // Get the index of the `/` immediately after the domain name.
4520 int endOfDomainName = urlString.indexOf("/", (urlString.indexOf("//") + 2));
4522 // Create a base URL string.
4525 // Get the base URL.
4526 if (endOfDomainName > 0) { // There is at least one character after the base URL.
4527 // Get the base URL.
4528 baseUrl = urlString.substring(0, endOfDomainName);
4529 } else { // There are no characters after the base URL.
4530 // Set the base URL to be the entire URL string.
4531 baseUrl = urlString;
4534 // Get the index of the last `.` in the domain.
4535 int lastDotIndex = baseUrl.lastIndexOf(".");
4537 // Get the index of the penultimate `.` in the domain.
4538 int penultimateDotIndex = baseUrl.lastIndexOf(".", lastDotIndex - 1);
4540 // Markup the beginning of the URL.
4541 if (urlString.startsWith("http://")) { // Highlight the protocol of connections that are not encrypted.
4542 urlEditText.getText().setSpan(redColorSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4544 // De-emphasize subdomains.
4545 if (penultimateDotIndex > 0) { // There is more than one subdomain in the domain name.
4546 urlEditText.getText().setSpan(initialGrayColorSpan, 7, penultimateDotIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4548 } else if (urlString.startsWith("https://")) { // De-emphasize the protocol of connections that are encrypted.
4549 if (penultimateDotIndex > 0) { // There is more than one subdomain in the domain name.
4550 // De-emphasize the protocol and the additional subdomains.
4551 urlEditText.getText().setSpan(initialGrayColorSpan, 0, penultimateDotIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4552 } else { // There is only one subdomain in the domain name.
4553 // De-emphasize only the protocol.
4554 urlEditText.getText().setSpan(initialGrayColorSpan, 0, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4558 // De-emphasize the text after the domain name.
4559 if (endOfDomainName > 0) {
4560 urlEditText.getText().setSpan(finalGrayColorSpan, endOfDomainName, urlString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4566 private void loadBookmarksFolder() {
4567 // Update the bookmarks cursor with the contents of the bookmarks database for the current folder.
4568 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
4570 // Populate the bookmarks cursor adapter. `this` specifies the `Context`. `false` disables `autoRequery`.
4571 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
4573 public View newView(Context context, Cursor cursor, ViewGroup parent) {
4574 // Inflate the individual item layout. `false` does not attach it to the root.
4575 return getLayoutInflater().inflate(R.layout.bookmarks_drawer_item_linearlayout, parent, false);
4579 public void bindView(View view, Context context, Cursor cursor) {
4580 // Get handles for the views.
4581 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmark_favorite_icon);
4582 TextView bookmarkNameTextView = view.findViewById(R.id.bookmark_name);
4584 // Get the favorite icon byte array from the cursor.
4585 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
4587 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
4588 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
4590 // Display the bitmap in `bookmarkFavoriteIcon`.
4591 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
4593 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
4594 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
4595 bookmarkNameTextView.setText(bookmarkNameString);
4597 // Make the font bold for folders.
4598 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {
4599 bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD);
4600 } else { // Reset the font to default for normal bookmarks.
4601 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
4606 // Get a handle for the bookmarks list view.
4607 ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
4609 // Populate the list view with the adapter.
4610 bookmarksListView.setAdapter(bookmarksCursorAdapter);
4612 // Get a handle for the bookmarks title text view.
4613 TextView bookmarksTitleTextView = findViewById(R.id.bookmarks_title_textview);
4615 // Set the bookmarks drawer title.
4616 if (currentBookmarksFolder.isEmpty()) {
4617 bookmarksTitleTextView.setText(R.string.bookmarks);
4619 bookmarksTitleTextView.setText(currentBookmarksFolder);
4623 private void openWithApp(String url) {
4624 // Create an open with app intent with `ACTION_VIEW`.
4625 Intent openWithAppIntent = new Intent(Intent.ACTION_VIEW);
4627 // Set the URI but not the MIME type. This should open all available apps.
4628 openWithAppIntent.setData(Uri.parse(url));
4630 // Flag the intent to open in a new task.
4631 openWithAppIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4635 // Show the chooser.
4636 startActivity(openWithAppIntent);
4637 } catch (ActivityNotFoundException exception) { // There are no apps available to open the URL.
4638 // Show a snackbar with the error.
4639 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
4643 private void openWithBrowser(String url) {
4644 // Create an open with browser intent with `ACTION_VIEW`.
4645 Intent openWithBrowserIntent = new Intent(Intent.ACTION_VIEW);
4647 // Set the URI and the MIME type. `"text/html"` should load browser options.
4648 openWithBrowserIntent.setDataAndType(Uri.parse(url), "text/html");
4650 // Flag the intent to open in a new task.
4651 openWithBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4655 // Show the chooser.
4656 startActivity(openWithBrowserIntent);
4657 } catch (ActivityNotFoundException exception) { // There are no browsers available to open the URL.
4658 // Show a snackbar with the error.
4659 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
4663 private String sanitizeUrl(String url) {
4664 // Sanitize Google Analytics.
4665 if (sanitizeGoogleAnalytics) {
4667 if (url.contains("?utm_")) {
4668 url = url.substring(0, url.indexOf("?utm_"));
4672 if (url.contains("&utm_")) {
4673 url = url.substring(0, url.indexOf("&utm_"));
4677 // Sanitize Facebook Click IDs.
4678 if (sanitizeFacebookClickIds) {
4679 // Remove `?fbclid=`.
4680 if (url.contains("?fbclid=")) {
4681 url = url.substring(0, url.indexOf("?fbclid="));
4684 // Remove `&fbclid=`.
4685 if (url.contains("&fbclid=")) {
4686 url = url.substring(0, url.indexOf("&fbclid="));
4689 // Remove `?fbadid=`.
4690 if (url.contains("?fbadid=")) {
4691 url = url.substring(0, url.indexOf("?fbadid="));
4694 // Remove `&fbadid=`.
4695 if (url.contains("&fbadid=")) {
4696 url = url.substring(0, url.indexOf("&fbadid="));
4700 // Sanitize Twitter AMP redirects.
4701 if (sanitizeTwitterAmpRedirects) {
4703 if (url.contains("?amp=1")) {
4704 url = url.substring(0, url.indexOf("?amp=1"));
4708 // Return the sanitized URL.
4712 public void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
4713 // Store the blocklists.
4714 easyList = combinedBlocklists.get(0);
4715 easyPrivacy = combinedBlocklists.get(1);
4716 fanboysAnnoyanceList = combinedBlocklists.get(2);
4717 fanboysSocialList = combinedBlocklists.get(3);
4718 ultraList = combinedBlocklists.get(4);
4719 ultraPrivacy = combinedBlocklists.get(5);
4721 // Check to see if the activity has been restarted.
4722 if ((savedStateArrayList == null) || (savedStateArrayList.size() == 0)) { // The activity has not been restarted or it was restarted on start to force the night theme.
4723 // Add the first tab.
4724 addNewTab("", true);
4725 } else { // The activity has been restarted.
4726 // Restore each tab. Once the minimum API >= 24, a `forEach()` command can be used.
4727 for (int i = 0; i < savedStateArrayList.size(); i++) {
4729 tabLayout.addTab(tabLayout.newTab());
4732 TabLayout.Tab newTab = tabLayout.getTabAt(i);
4734 // Remove the lint warning below that the current tab might be null.
4735 assert newTab != null;
4737 // Set a custom view on the new tab.
4738 newTab.setCustomView(R.layout.tab_custom_view);
4740 // Add the new page.
4741 webViewPagerAdapter.restorePage(savedStateArrayList.get(i), savedNestedScrollWebViewStateArrayList.get(i));
4744 // Reset the saved state variables.
4745 savedStateArrayList = null;
4746 savedNestedScrollWebViewStateArrayList = null;
4748 // Restore the selected tab position.
4749 if (savedTabPosition == 0) { // The first tab is selected.
4750 // Set the first page as the current WebView.
4751 setCurrentWebView(0);
4752 } else { // the first tab is not selected.
4753 // Move to the selected tab.
4754 webViewPager.setCurrentItem(savedTabPosition);
4757 // Get the intent that started the app.
4758 Intent intent = getIntent();
4760 // Reset the intent. This prevents a duplicate tab from being created on restart.
4761 setIntent(new Intent());
4763 // Get the information from the intent.
4764 String intentAction = intent.getAction();
4765 Uri intentUriData = intent.getData();
4767 // Determine if this is a web search.
4768 boolean isWebSearch = ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH));
4770 // 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.
4771 if (intentUriData != null || isWebSearch) {
4772 // Get the shared preferences.
4773 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
4775 // Create a URL string.
4778 // If the intent action is a web search, perform the search.
4779 if (isWebSearch) { // The intent is a web search.
4780 // Create an encoded URL string.
4781 String encodedUrlString;
4783 // Sanitize the search input and convert it to a search.
4785 encodedUrlString = URLEncoder.encode(intent.getStringExtra(SearchManager.QUERY), "UTF-8");
4786 } catch (UnsupportedEncodingException exception) {
4787 encodedUrlString = "";
4790 // Add the base search URL.
4791 url = searchURL + encodedUrlString;
4792 } else { // The intent should contain a URL.
4793 // Set the intent data as the url.
4794 url = intentUriData.toString();
4797 // Add a new tab if specified in the preferences.
4798 if (sharedPreferences.getBoolean("open_intents_in_new_tab", true)) { // Load the URL in a new tab.
4799 // Set the loading new intent flag.
4800 loadingNewIntent = true;
4803 addNewTab(url, true);
4804 } else { // Load the URL in the current tab.
4806 loadUrl(currentWebView, url);
4812 public void addTab(View view) {
4813 // Add a new tab with a blank URL.
4814 addNewTab("", true);
4817 private void addNewTab(String url, boolean moveToTab) {
4818 // Get the new page number. The page numbers are 0 indexed, so the new page number will match the current count.
4819 int newTabNumber = tabLayout.getTabCount();
4822 tabLayout.addTab(tabLayout.newTab());
4825 TabLayout.Tab newTab = tabLayout.getTabAt(newTabNumber);
4827 // Remove the lint warning below that the current tab might be null.
4828 assert newTab != null;
4830 // Set a custom view on the new tab.
4831 newTab.setCustomView(R.layout.tab_custom_view);
4833 // Add the new WebView page.
4834 webViewPagerAdapter.addPage(newTabNumber, webViewPager, url, moveToTab);
4837 public void closeTab(View view) {
4838 // Run the command according to the number of tabs.
4839 if (tabLayout.getTabCount() > 1) { // There is more than one tab open.
4840 // Close the current tab.
4842 } else { // There is only one tab open.
4847 private void closeCurrentTab() {
4848 // Pause the current WebView.
4849 currentWebView.onPause();
4851 // Pause the current WebView JavaScript timers.
4852 currentWebView.pauseTimers();
4854 // Get the current tab number.
4855 int currentTabNumber = tabLayout.getSelectedTabPosition();
4857 // Delete the current tab.
4858 tabLayout.removeTabAt(currentTabNumber);
4860 // Delete the current page. If the selected page number did not change during the delete, it will return true, meaning that the current WebView must be reset.
4861 if (webViewPagerAdapter.deletePage(currentTabNumber, webViewPager)) {
4862 setCurrentWebView(currentTabNumber);
4865 // Expand the app bar if it is currently collapsed.
4866 appBarLayout.setExpanded(true);
4869 private void saveWebpageArchive() {
4870 // Save the webpage archive.
4871 currentWebView.saveWebArchive(saveWebpageFilePath);
4873 // Display a snackbar.
4874 Snackbar saveWebpageArchiveSnackbar = Snackbar.make(currentWebView, getString(R.string.file_saved) + " " + saveWebpageFilePath, Snackbar.LENGTH_SHORT);
4876 // Add an open option to the snackbar.
4877 saveWebpageArchiveSnackbar.setAction(R.string.open, (View view) -> {
4878 // Get a file for the file name string.
4879 File file = new File(saveWebpageFilePath);
4881 // Declare a file URI variable.
4884 // Get the URI for the file according to the Android version.
4885 if (Build.VERSION.SDK_INT >= 24) { // Use a file provider.
4886 fileUri = FileProvider.getUriForFile(this, getString(R.string.file_provider), file);
4887 } else { // Get the raw file path URI.
4888 fileUri = Uri.fromFile(file);
4891 // Get a handle for the content resolver.
4892 ContentResolver contentResolver = getContentResolver();
4894 // Create an open intent with `ACTION_VIEW`.
4895 Intent openIntent = new Intent(Intent.ACTION_VIEW);
4897 // Set the URI and the MIME type.
4898 openIntent.setDataAndType(fileUri, contentResolver.getType(fileUri));
4900 // Allow the app to read the file URI.
4901 openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
4903 // Show the chooser.
4904 startActivity(Intent.createChooser(openIntent, getString(R.string.open)));
4907 // Show the snackbar.
4908 saveWebpageArchiveSnackbar.show();
4910 // Reset the save Webpage file path.
4911 saveWebpageFilePath = "";
4914 private void clearAndExit() {
4915 // Get a handle for the shared preferences.
4916 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
4918 // Close the bookmarks cursor and database.
4919 bookmarksCursor.close();
4920 bookmarksDatabaseHelper.close();
4922 // Get the status of the clear everything preference.
4923 boolean clearEverything = sharedPreferences.getBoolean("clear_everything", true);
4925 // Get a handle for the runtime.
4926 Runtime runtime = Runtime.getRuntime();
4928 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
4929 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
4930 String privateDataDirectoryString = getApplicationInfo().dataDir;
4933 if (clearEverything || sharedPreferences.getBoolean("clear_cookies", true)) {
4934 // The command to remove cookies changed slightly in API 21.
4935 if (Build.VERSION.SDK_INT >= 21) {
4936 CookieManager.getInstance().removeAllCookies(null);
4938 CookieManager.getInstance().removeAllCookie();
4941 // Manually delete the cookies database, as `CookieManager` sometimes will not flush its changes to disk before `System.exit(0)` is run.
4943 // Two commands must be used because `Runtime.exec()` does not like `*`.
4944 Process deleteCookiesProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies");
4945 Process deleteCookiesJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/Cookies-journal");
4947 // Wait until the processes have finished.
4948 deleteCookiesProcess.waitFor();
4949 deleteCookiesJournalProcess.waitFor();
4950 } catch (Exception exception) {
4951 // Do nothing if an error is thrown.
4955 // Clear DOM storage.
4956 if (clearEverything || sharedPreferences.getBoolean("clear_dom_storage", true)) {
4957 // Ask `WebStorage` to clear the DOM storage.
4958 WebStorage webStorage = WebStorage.getInstance();
4959 webStorage.deleteAllData();
4961 // Manually delete the DOM storage files and directories, as `WebStorage` sometimes will not flush its changes to disk before `System.exit(0)` is run.
4963 // A `String[]` must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
4964 Process deleteLocalStorageProcess = runtime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
4966 // Multiple commands must be used because `Runtime.exec()` does not like `*`.
4967 Process deleteIndexProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
4968 Process deleteQuotaManagerProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
4969 Process deleteQuotaManagerJournalProcess = runtime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
4970 Process deleteDatabaseProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
4972 // Wait until the processes have finished.
4973 deleteLocalStorageProcess.waitFor();
4974 deleteIndexProcess.waitFor();
4975 deleteQuotaManagerProcess.waitFor();
4976 deleteQuotaManagerJournalProcess.waitFor();
4977 deleteDatabaseProcess.waitFor();
4978 } catch (Exception exception) {
4979 // Do nothing if an error is thrown.
4983 // Clear form data if the API < 26.
4984 if ((Build.VERSION.SDK_INT < 26) && (clearEverything || sharedPreferences.getBoolean("clear_form_data", true))) {
4985 WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
4986 webViewDatabase.clearFormData();
4988 // Manually delete the form data database, as `WebViewDatabase` sometimes will not flush its changes to disk before `System.exit(0)` is run.
4990 // A string array must be used because the database contains a space and `Runtime.exec` will not otherwise escape the string correctly.
4991 Process deleteWebDataProcess = runtime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data"});
4992 Process deleteWebDataJournalProcess = runtime.exec(new String[] {"rm", "-f", privateDataDirectoryString + "/app_webview/Web Data-journal"});
4994 // Wait until the processes have finished.
4995 deleteWebDataProcess.waitFor();
4996 deleteWebDataJournalProcess.waitFor();
4997 } catch (Exception exception) {
4998 // Do nothing if an error is thrown.
5002 // Clear the logcat.
5003 if (clearEverything || sharedPreferences.getBoolean(getString(R.string.clear_logcat_key), true)) {
5005 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
5006 Process process = Runtime.getRuntime().exec("logcat -b all -c");
5008 // Wait for the process to finish.
5010 } catch (IOException|InterruptedException exception) {
5016 if (clearEverything || sharedPreferences.getBoolean("clear_cache", true)) {
5017 // Clear the cache from each WebView.
5018 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
5019 // Get the WebView tab fragment.
5020 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
5022 // Get the fragment view.
5023 View fragmentView = webViewTabFragment.getView();
5025 // Only clear the cache if the WebView exists.
5026 if (fragmentView != null) {
5027 // Get the nested scroll WebView from the tab fragment.
5028 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
5030 // Clear the cache for this WebView.
5031 nestedScrollWebView.clearCache(true);
5035 // Manually delete the cache directories.
5037 // Delete the main cache directory.
5038 Process deleteCacheProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/cache");
5040 // Delete the secondary `Service Worker` cache directory.
5041 // A string array must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
5042 Process deleteServiceWorkerProcess = runtime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Service Worker/"});
5044 // Wait until the processes have finished.
5045 deleteCacheProcess.waitFor();
5046 deleteServiceWorkerProcess.waitFor();
5047 } catch (Exception exception) {
5048 // Do nothing if an error is thrown.
5052 // Wipe out each WebView.
5053 for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
5054 // Get the WebView tab fragment.
5055 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
5057 // Get the fragment view.
5058 View fragmentView = webViewTabFragment.getView();
5060 // Only wipe out the WebView if it exists.
5061 if (fragmentView != null) {
5062 // Get the nested scroll WebView from the tab fragment.
5063 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
5065 // Clear SSL certificate preferences for this WebView.
5066 nestedScrollWebView.clearSslPreferences();
5068 // Clear the back/forward history for this WebView.
5069 nestedScrollWebView.clearHistory();
5071 // Destroy the internal state of the WebView.
5072 nestedScrollWebView.destroy();
5076 // Clear the custom headers.
5077 customHeaders.clear();
5079 // Manually delete the `app_webview` folder, which contains the cookies, DOM storage, form data, and `Service Worker` cache.
5080 // See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`.
5081 if (clearEverything) {
5083 // Delete the folder.
5084 Process deleteAppWebviewProcess = runtime.exec("rm -rf " + privateDataDirectoryString + "/app_webview");
5086 // Wait until the process has finished.
5087 deleteAppWebviewProcess.waitFor();
5088 } catch (Exception exception) {
5089 // Do nothing if an error is thrown.
5093 // Close Privacy Browser. `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
5094 if (Build.VERSION.SDK_INT >= 21) {
5095 finishAndRemoveTask();
5100 // Remove the terminated program from RAM. The status code is `0`.
5104 public void bookmarksBack(View view) {
5105 if (currentBookmarksFolder.isEmpty()) { // The home folder is displayed.
5106 // close the bookmarks drawer.
5107 drawerLayout.closeDrawer(GravityCompat.END);
5108 } else { // A subfolder is displayed.
5109 // Place the former parent folder in `currentFolder`.
5110 currentBookmarksFolder = bookmarksDatabaseHelper.getParentFolderName(currentBookmarksFolder);
5112 // Load the new folder.
5113 loadBookmarksFolder();
5117 private void setCurrentWebView(int pageNumber) {
5118 // Get handles for the URL views.
5119 RelativeLayout urlRelativeLayout = findViewById(R.id.url_relativelayout);
5120 EditText urlEditText = findViewById(R.id.url_edittext);
5121 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
5123 // Stop the swipe to refresh indicator if it is running
5124 swipeRefreshLayout.setRefreshing(false);
5126 // Get the WebView tab fragment.
5127 WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(pageNumber);
5129 // Get the fragment view.
5130 View fragmentView = webViewTabFragment.getView();
5132 // Set the current WebView if the fragment view is not null.
5133 if (fragmentView != null) { // The fragment has been populated.
5134 // Store the current WebView.
5135 currentWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
5137 // Update the status of swipe to refresh.
5138 if (currentWebView.getSwipeToRefresh()) { // Swipe to refresh is enabled.
5139 // Enable the swipe refresh layout if the WebView is scrolled all the way to the top. It is updated every time the scroll changes.
5140 swipeRefreshLayout.setEnabled(currentWebView.getY() == 0);
5141 } else { // Swipe to refresh is disabled.
5142 // Disable the swipe refresh layout.
5143 swipeRefreshLayout.setEnabled(false);
5146 // Get a handle for the cookie manager.
5147 CookieManager cookieManager = CookieManager.getInstance();
5149 // Set the first-party cookie status.
5150 cookieManager.setAcceptCookie(currentWebView.getAcceptFirstPartyCookies());
5152 // Update the privacy icons. `true` redraws the icons in the app bar.
5153 updatePrivacyIcons(true);
5155 // Get a handle for the input method manager.
5156 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
5158 // Remove the lint warning below that the input method manager might be null.
5159 assert inputMethodManager != null;
5161 // Get the current URL.
5162 String url = currentWebView.getUrl();
5164 // Update the URL edit text if not loading a new intent. Otherwise, this will be handled by `onPageStarted()` (if called) and `onPageFinished()`.
5165 if (!loadingNewIntent) { // A new intent is not being loaded.
5166 if ((url == null) || url.equals("about:blank")) { // The WebView is blank.
5167 // Display the hint in the URL edit text.
5168 urlEditText.setText("");
5170 // Request focus for the URL text box.
5171 urlEditText.requestFocus();
5173 // Display the keyboard.
5174 inputMethodManager.showSoftInput(urlEditText, 0);
5175 } else { // The WebView has a loaded URL.
5176 // Clear the focus from the URL text box.
5177 urlEditText.clearFocus();
5179 // Hide the soft keyboard.
5180 inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
5182 // Display the current URL in the URL text box.
5183 urlEditText.setText(url);
5185 // Highlight the URL text.
5188 } else { // A new intent is being loaded.
5189 // Reset the loading new intent tracker.
5190 loadingNewIntent = false;
5193 // Set the background to indicate the domain settings status.
5194 if (currentWebView.getDomainSettingsApplied()) {
5195 // Get the current theme status.
5196 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
5198 // Set a green background on the URL relative layout to indicate that custom domain settings are being used.
5199 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
5200 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_light_green, null));
5202 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.url_bar_background_dark_blue, null));
5205 urlRelativeLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.color.transparent, null));
5207 } else { // The fragment has not been populated. Try again in 100 milliseconds.
5208 // Create a handler to set the current WebView.
5209 Handler setCurrentWebViewHandler = new Handler();
5211 // Create a runnable to set the current WebView.
5212 Runnable setCurrentWebWebRunnable = () -> {
5213 // Set the current WebView.
5214 setCurrentWebView(pageNumber);
5217 // Try setting the current WebView again after 100 milliseconds.
5218 setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 100);
5223 public void initializeWebView(NestedScrollWebView nestedScrollWebView, int pageNumber, ProgressBar progressBar, String url, Boolean restoringState) {
5224 // Get a handle for the shared preferences.
5225 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
5227 // Get the WebView theme.
5228 String webViewTheme = sharedPreferences.getString("webview_theme", getString(R.string.webview_theme_default_value));
5230 // Get the WebView theme entry values string array.
5231 String[] webViewThemeEntryValuesStringArray = getResources().getStringArray(R.array.webview_theme_entry_values);
5233 // Apply the WebView theme if supported by the installed WebView.
5234 if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
5235 // Set the WebView theme. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant.
5236 if (webViewTheme.equals(webViewThemeEntryValuesStringArray[1])) { // The light theme is selected.
5237 // Turn off the WebView dark mode.
5238 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
5240 // Make the WebView visible. The WebView was created invisible in `webview_framelayout` to prevent a white background splash in night mode.
5241 // If the system is currently in night mode, showing the WebView will be handled in `onProgressChanged()`.
5242 nestedScrollWebView.setVisibility(View.VISIBLE);
5243 } else if (webViewTheme.equals(webViewThemeEntryValuesStringArray[2])) { // The dark theme is selected.
5244 // Turn on the WebView dark mode.
5245 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
5246 } else { // The system default theme is selected.
5247 // Get the current system theme status.
5248 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
5250 // Set the WebView theme according to the current system theme status.
5251 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The system is in day mode.
5252 // Turn off the WebView dark mode.
5253 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_OFF);
5255 // Make the WebView visible. The WebView was created invisible in `webview_framelayout` to prevent a white background splash in night mode.
5256 // If the system is currently in night mode, showing the WebView will be handled in `onProgressChanged()`.
5257 nestedScrollWebView.setVisibility(View.VISIBLE);
5258 } else { // The system is in night mode.
5259 // Turn on the WebView dark mode.
5260 WebSettingsCompat.setForceDark(nestedScrollWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
5265 // Get a handle for the app compat delegate.
5266 AppCompatDelegate appCompatDelegate = getDelegate();
5268 // Get handles for the activity views.
5269 FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout);
5270 RelativeLayout mainContentRelativeLayout = findViewById(R.id.main_content_relativelayout);
5271 ActionBar actionBar = appCompatDelegate.getSupportActionBar();
5272 LinearLayout tabsLinearLayout = findViewById(R.id.tabs_linearlayout);
5273 EditText urlEditText = findViewById(R.id.url_edittext);
5274 SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
5276 // Remove the incorrect lint warning below that the action bar might be null.
5277 assert actionBar != null;
5279 // Get a handle for the activity
5280 Activity activity = this;
5282 // Get a handle for the input method manager.
5283 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
5285 // Instantiate the blocklist helper.
5286 BlocklistHelper blocklistHelper = new BlocklistHelper();
5288 // Remove the lint warning below that the input method manager might be null.
5289 assert inputMethodManager != null;
5291 // Initialize the favorite icon.
5292 nestedScrollWebView.initializeFavoriteIcon();
5294 // Set the app bar scrolling.
5295 nestedScrollWebView.setNestedScrollingEnabled(sharedPreferences.getBoolean("scroll_app_bar", true));
5297 // Allow pinch to zoom.
5298 nestedScrollWebView.getSettings().setBuiltInZoomControls(true);
5300 // Hide zoom controls.
5301 nestedScrollWebView.getSettings().setDisplayZoomControls(false);
5303 // Don't allow mixed content (HTTP and HTTPS) on the same website.
5304 if (Build.VERSION.SDK_INT >= 21) {
5305 nestedScrollWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
5308 // Set the WebView to load in overview mode (zoomed out to the maximum width).
5309 nestedScrollWebView.getSettings().setLoadWithOverviewMode(true);
5311 // Explicitly disable geolocation.
5312 nestedScrollWebView.getSettings().setGeolocationEnabled(false);
5314 // Create a double-tap gesture detector to toggle full-screen mode.
5315 GestureDetector doubleTapGestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
5316 // Override `onDoubleTap()`. All other events are handled using the default settings.
5318 public boolean onDoubleTap(MotionEvent event) {
5319 if (fullScreenBrowsingModeEnabled) { // Only process the double-tap if full screen browsing mode is enabled.
5320 // Toggle the full screen browsing mode tracker.
5321 inFullScreenBrowsingMode = !inFullScreenBrowsingMode;
5323 // Toggle the full screen browsing mode.
5324 if (inFullScreenBrowsingMode) { // Switch to full screen mode.
5325 // Hide the app bar if specified.
5327 // Close the find on page bar if it is visible.
5328 closeFindOnPage(null);
5330 // Hide the tab linear layout.
5331 tabsLinearLayout.setVisibility(View.GONE);
5333 // Hide the action bar.
5336 // If the app bar is not being scrolled, the swipe refresh layout needs to be adjusted.
5337 if (!scrollAppBar) {
5338 // Remove the padding from the top of the swipe refresh layout.
5339 swipeRefreshLayout.setPadding(0, 0, 0, 0);
5341 // The swipe refresh circle must be moved above the now removed status bar location.
5342 swipeRefreshLayout.setProgressViewOffset(false, -200, defaultProgressViewEndOffset);
5346 // Hide the banner ad in the free flavor.
5347 if (BuildConfig.FLAVOR.contentEquals("free")) {
5348 AdHelper.hideAd(findViewById(R.id.adview));
5351 /* Hide the system bars.
5352 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5353 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5354 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5355 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5357 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5358 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5359 } else { // Switch to normal viewing mode.
5360 // Show the app bar if it was hidden.
5362 // Show the tab linear layout.
5363 tabsLinearLayout.setVisibility(View.VISIBLE);
5365 // Show the action bar.
5368 // If the app bar is not being scrolled, the swipe refresh layout needs to be adjusted.
5369 if (!scrollAppBar) {
5370 // The swipe refresh layout must be manually moved below the app bar layout.
5371 swipeRefreshLayout.setPadding(0, appBarHeight, 0, 0);
5373 // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
5374 swipeRefreshLayout.setProgressViewOffset(false, defaultProgressViewStartOffset - 10 + appBarHeight, defaultProgressViewEndOffset + appBarHeight);
5378 // Show the banner ad in the free flavor.
5379 if (BuildConfig.FLAVOR.contentEquals("free")) {
5381 AdHelper.loadAd(findViewById(R.id.adview), getApplicationContext(), getString(R.string.ad_unit_id));
5384 // Remove the `SYSTEM_UI` flags from the root frame layout.
5385 rootFrameLayout.setSystemUiVisibility(0);
5388 // Consume the double-tap.
5390 } else { // Do not consume the double-tap because full screen browsing mode is disabled.
5396 // Pass all touch events on the WebView through the double-tap gesture detector.
5397 nestedScrollWebView.setOnTouchListener((View view, MotionEvent event) -> {
5398 // Call `performClick()` on the view, which is required for accessibility.
5399 view.performClick();
5401 // Send the event to the gesture detector.
5402 return doubleTapGestureDetector.onTouchEvent(event);
5405 // Register the WebView for a context menu. This is used to see link targets and download images.
5406 registerForContextMenu(nestedScrollWebView);
5408 // Allow the downloading of files.
5409 nestedScrollWebView.setDownloadListener((String downloadUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) -> {
5410 // Define a formatted file size string.
5411 String formattedFileSizeString;
5413 // Process the content length if it contains data.
5414 if (contentLength > 0) { // The content length is greater than 0.
5415 // Format the content length as a string.
5416 formattedFileSizeString = NumberFormat.getInstance().format(contentLength) + " " + getString(R.string.bytes);
5417 } else { // The content length is not greater than 0.
5418 // Set the formatted file size string to be `unknown size`.
5419 formattedFileSizeString = getString(R.string.unknown_size);
5422 // Get the file name from the content disposition.
5423 String fileNameString = PrepareSaveDialog.getFileNameFromHeaders(this, contentDisposition, mimetype, downloadUrl);
5425 // Instantiate the save dialog.
5426 DialogFragment saveDialogFragment = SaveWebpageDialog.saveWebpage(StoragePermissionDialog.SAVE_URL, downloadUrl, formattedFileSizeString, fileNameString, userAgent,
5427 nestedScrollWebView.getAcceptFirstPartyCookies());
5429 // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name.
5430 saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog));
5433 // Update the find on page count.
5434 nestedScrollWebView.setFindListener(new WebView.FindListener() {
5435 // Get a handle for `findOnPageCountTextView`.
5436 final TextView findOnPageCountTextView = findViewById(R.id.find_on_page_count_textview);
5439 public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
5440 if ((isDoneCounting) && (numberOfMatches == 0)) { // There are no matches.
5441 // Set `findOnPageCountTextView` to `0/0`.
5442 findOnPageCountTextView.setText(R.string.zero_of_zero);
5443 } else if (isDoneCounting) { // There are matches.
5444 // `activeMatchOrdinal` is zero-based.
5445 int activeMatch = activeMatchOrdinal + 1;
5447 // Build the match string.
5448 String matchString = activeMatch + "/" + numberOfMatches;
5450 // Set `findOnPageCountTextView`.
5451 findOnPageCountTextView.setText(matchString);
5456 // Update the status of swipe to refresh based on the scroll position of the nested scroll WebView. Also reinforce full screen browsing mode.
5457 // 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.
5458 if (Build.VERSION.SDK_INT >= 23) {
5459 nestedScrollWebView.setOnScrollChangeListener((view, i, i1, i2, i3) -> {
5460 if (nestedScrollWebView.getSwipeToRefresh()) {
5461 // Only enable swipe to refresh if the WebView is scrolled to the top.
5462 swipeRefreshLayout.setEnabled(nestedScrollWebView.getScrollY() == 0);
5464 // Disable swipe to refresh.
5465 swipeRefreshLayout.setEnabled(false);
5468 // Reinforce the system UI visibility flags if in full screen browsing mode.
5469 // This hides the status and navigation bars, which are displayed if other elements are shown, like dialog boxes, the options menu, or the keyboard.
5470 if (inFullScreenBrowsingMode) {
5471 /* Hide the system bars.
5472 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5473 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5474 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5475 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5477 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5478 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5482 nestedScrollWebView.getViewTreeObserver().addOnScrollChangedListener(() -> {
5483 if (nestedScrollWebView.getSwipeToRefresh()) {
5484 // Only enable swipe to refresh if the WebView is scrolled to the top.
5485 swipeRefreshLayout.setEnabled(nestedScrollWebView.getScrollY() == 0);
5487 // Disable swipe to refresh.
5488 swipeRefreshLayout.setEnabled(false);
5492 // Reinforce the system UI visibility flags if in full screen browsing mode.
5493 // This hides the status and navigation bars, which are displayed if other elements are shown, like dialog boxes, the options menu, or the keyboard.
5494 if (inFullScreenBrowsingMode) {
5495 /* Hide the system bars.
5496 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5497 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5498 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5499 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5501 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5502 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5507 // Set the web chrome client.
5508 nestedScrollWebView.setWebChromeClient(new WebChromeClient() {
5509 // Update the progress bar when a page is loading.
5511 public void onProgressChanged(WebView view, int progress) {
5512 // Update the progress bar.
5513 progressBar.setProgress(progress);
5515 // Set the visibility of the progress bar.
5516 if (progress < 100) {
5517 // Show the progress bar.
5518 progressBar.setVisibility(View.VISIBLE);
5520 // Hide the progress bar.
5521 progressBar.setVisibility(View.GONE);
5523 //Stop the swipe to refresh indicator if it is running
5524 swipeRefreshLayout.setRefreshing(false);
5526 // 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.
5527 nestedScrollWebView.setVisibility(View.VISIBLE);
5531 // Set the favorite icon when it changes.
5533 public void onReceivedIcon(WebView view, Bitmap icon) {
5534 // Only update the favorite icon if the website has finished loading.
5535 if (progressBar.getVisibility() == View.GONE) {
5536 // Store the new favorite icon.
5537 nestedScrollWebView.setFavoriteOrDefaultIcon(icon);
5539 // Get the current page position.
5540 int currentPosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
5542 // Get the current tab.
5543 TabLayout.Tab tab = tabLayout.getTabAt(currentPosition);
5545 // Check to see if the tab has been populated.
5547 // Get the custom view from the tab.
5548 View tabView = tab.getCustomView();
5550 // Check to see if the custom tab view has been populated.
5551 if (tabView != null) {
5552 // Get the favorite icon image view from the tab.
5553 ImageView tabFavoriteIconImageView = tabView.findViewById(R.id.favorite_icon_imageview);
5555 // Display the favorite icon in the tab.
5556 tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
5562 // Save a copy of the title when it changes.
5564 public void onReceivedTitle(WebView view, String title) {
5565 // Get the current page position.
5566 int currentPosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
5568 // Get the current tab.
5569 TabLayout.Tab tab = tabLayout.getTabAt(currentPosition);
5571 // Only populate the title text view if the tab has been fully created.
5573 // Get the custom view from the tab.
5574 View tabView = tab.getCustomView();
5576 // Only populate the title text view if the tab view has been fully populated.
5577 if (tabView != null) {
5578 // Get the title text view from the tab.
5579 TextView tabTitleTextView = tabView.findViewById(R.id.title_textview);
5581 // Set the title according to the URL.
5582 if (title.equals("about:blank")) {
5583 // Set the title to indicate a new tab.
5584 tabTitleTextView.setText(R.string.new_tab);
5586 // Set the title as the tab text.
5587 tabTitleTextView.setText(title);
5593 // Enter full screen video.
5595 public void onShowCustomView(View video, CustomViewCallback callback) {
5596 // Get a handle for the full screen video frame layout.
5597 FrameLayout fullScreenVideoFrameLayout = findViewById(R.id.full_screen_video_framelayout);
5599 // Set the full screen video flag.
5600 displayingFullScreenVideo = true;
5602 // Pause the ad if this is the free flavor.
5603 if (BuildConfig.FLAVOR.contentEquals("free")) {
5604 // The AdView is destroyed and recreated, which changes the ID, every time it is reloaded to handle possible rotations.
5605 AdHelper.pauseAd(findViewById(R.id.adview));
5608 // Hide the keyboard.
5609 inputMethodManager.hideSoftInputFromWindow(nestedScrollWebView.getWindowToken(), 0);
5611 // Hide the main content relative layout.
5612 mainContentRelativeLayout.setVisibility(View.GONE);
5614 /* Hide the system bars.
5615 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5616 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5617 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5618 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5620 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5621 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5623 // Disable the sliding drawers.
5624 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
5626 // Add the video view to the full screen video frame layout.
5627 fullScreenVideoFrameLayout.addView(video);
5629 // Show the full screen video frame layout.
5630 fullScreenVideoFrameLayout.setVisibility(View.VISIBLE);
5632 // Disable the screen timeout while the video is playing. YouTube does this automatically, but not all other videos do.
5633 fullScreenVideoFrameLayout.setKeepScreenOn(true);
5636 // Exit full screen video.
5638 public void onHideCustomView() {
5639 // Get a handle for the full screen video frame layout.
5640 FrameLayout fullScreenVideoFrameLayout = findViewById(R.id.full_screen_video_framelayout);
5642 // Re-enable the screen timeout.
5643 fullScreenVideoFrameLayout.setKeepScreenOn(false);
5645 // Unset the full screen video flag.
5646 displayingFullScreenVideo = false;
5648 // Remove all the views from the full screen video frame layout.
5649 fullScreenVideoFrameLayout.removeAllViews();
5651 // Hide the full screen video frame layout.
5652 fullScreenVideoFrameLayout.setVisibility(View.GONE);
5654 // Enable the sliding drawers.
5655 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
5657 // Show the main content relative layout.
5658 mainContentRelativeLayout.setVisibility(View.VISIBLE);
5660 // Apply the appropriate full screen mode flags.
5661 if (fullScreenBrowsingModeEnabled && inFullScreenBrowsingMode) { // Privacy Browser is currently in full screen browsing mode.
5662 // Hide the app bar if specified.
5664 // Hide the tab linear layout.
5665 tabsLinearLayout.setVisibility(View.GONE);
5667 // Hide the action bar.
5671 // Hide the banner ad in the free flavor.
5672 if (BuildConfig.FLAVOR.contentEquals("free")) {
5673 AdHelper.hideAd(findViewById(R.id.adview));
5676 /* Hide the system bars.
5677 * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar at the top of the screen.
5678 * SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN makes the root frame layout fill the area that is normally reserved for the status bar.
5679 * SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bar on the bottom or right of the screen.
5680 * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the status and navigation bars translucent and automatically re-hides them after they are shown.
5682 rootFrameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
5683 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
5684 } else { // Switch to normal viewing mode.
5685 // Remove the `SYSTEM_UI` flags from the root frame layout.
5686 rootFrameLayout.setSystemUiVisibility(0);
5689 // Reload the ad for the free flavor if not in full screen mode.
5690 if (BuildConfig.FLAVOR.contentEquals("free") && !inFullScreenBrowsingMode) {
5692 AdHelper.loadAd(findViewById(R.id.adview), getApplicationContext(), getString(R.string.ad_unit_id));
5698 public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
5699 // Show the file chooser if the device is running API >= 21.
5700 if (Build.VERSION.SDK_INT >= 21) {
5701 // Store the file path callback.
5702 fileChooserCallback = filePathCallback;
5704 // Create an intent to open a chooser based on the file chooser parameters.
5705 Intent fileChooserIntent = fileChooserParams.createIntent();
5707 // Get a handle for the package manager.
5708 PackageManager packageManager = getPackageManager();
5710 // Check to see if the file chooser intent resolves to an installed package.
5711 if (fileChooserIntent.resolveActivity(packageManager) != null) { // The file chooser intent is fine.
5712 // Start the file chooser intent.
5713 startActivityForResult(fileChooserIntent, BROWSE_FILE_UPLOAD_REQUEST_CODE);
5714 } else { // The file chooser intent will cause a crash.
5715 // Create a generic intent to open a chooser.
5716 Intent genericFileChooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
5718 // Request an openable file.
5719 genericFileChooserIntent.addCategory(Intent.CATEGORY_OPENABLE);
5721 // Set the file type to everything.
5722 genericFileChooserIntent.setType("*/*");
5724 // Start the generic file chooser intent.
5725 startActivityForResult(genericFileChooserIntent, BROWSE_FILE_UPLOAD_REQUEST_CODE);
5732 nestedScrollWebView.setWebViewClient(new WebViewClient() {
5733 // `shouldOverrideUrlLoading` makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
5734 // The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
5736 public boolean shouldOverrideUrlLoading(WebView view, String url) {
5737 // Sanitize the url.
5738 url = sanitizeUrl(url);
5740 // Handle the URL according to the type.
5741 if (url.startsWith("http")) { // Load the URL in Privacy Browser.
5742 // Apply the domain settings for the new URL. This doesn't do anything if the domain has not changed.
5743 applyDomainSettings(nestedScrollWebView, url, true, false);
5745 // Load the URL. By using `loadUrl()`, instead of `loadUrlFromBase()`, the Referer header will never be sent.
5746 nestedScrollWebView.loadUrl(url, customHeaders);
5748 // Returning true indicates that Privacy Browser is manually handling the loading of the URL.
5749 // Custom headers cannot be added if false is returned and the WebView handles the loading of the URL.
5751 } else if (url.startsWith("mailto:")) { // Load the email address in an external email program.
5752 // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
5753 Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
5755 // Parse the url and set it as the data for the intent.
5756 emailIntent.setData(Uri.parse(url));
5758 // Open the email program in a new task instead of as part of Privacy Browser.
5759 emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5763 startActivity(emailIntent);
5764 } catch (ActivityNotFoundException exception) {
5765 // Display a snackbar.
5766 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
5770 // Returning true indicates Privacy Browser is handling the URL by creating an intent.
5772 } else if (url.startsWith("tel:")) { // Load the phone number in the dialer.
5773 // Open the dialer and load the phone number, but wait for the user to place the call.
5774 Intent dialIntent = new Intent(Intent.ACTION_DIAL);
5776 // Add the phone number to the intent.
5777 dialIntent.setData(Uri.parse(url));
5779 // Open the dialer in a new task instead of as part of Privacy Browser.
5780 dialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5784 startActivity(dialIntent);
5785 } catch (ActivityNotFoundException exception) {
5786 // Display a snackbar.
5787 Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
5790 // Returning true indicates Privacy Browser is handling the URL by creating an intent.
5792 } else { // Load a system chooser to select an app that can handle the URL.
5793 // Open an app that can handle the URL.
5794 Intent genericIntent = new Intent(Intent.ACTION_VIEW);
5796 // Add the URL to the intent.
5797 genericIntent.setData(Uri.parse(url));
5799 // List all apps that can handle the URL instead of just opening the first one.
5800 genericIntent.addCategory(Intent.CATEGORY_BROWSABLE);
5802 // Open the app in a new task instead of as part of Privacy Browser.
5803 genericIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5805 // Start the app or display a snackbar if no app is available to handle the URL.
5807 startActivity(genericIntent);
5808 } catch (ActivityNotFoundException exception) {
5809 Snackbar.make(nestedScrollWebView, getString(R.string.unrecognized_url) + " " + url, Snackbar.LENGTH_SHORT).show();
5812 // Returning true indicates Privacy Browser is handling the URL by creating an intent.
5817 // Check requests against the block lists. The deprecated `shouldInterceptRequest()` must be used until minimum API >= 21.
5819 public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
5820 // Check to see if the resource request is for the main URL.
5821 if (url.equals(nestedScrollWebView.getCurrentUrl())) {
5822 // `return null` loads the resource request, which should never be blocked if it is the main URL.
5826 // 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.
5827 while (ultraPrivacy == null) {
5828 // The wait must be synchronized, which only lets one thread run on it at a time, or `java.lang.IllegalMonitorStateException` is thrown.
5829 synchronized (this) {
5831 // Check to see if the blocklists have been populated after 100 ms.
5833 } catch (InterruptedException exception) {
5839 // Sanitize the URL.
5840 url = sanitizeUrl(url);
5842 // Get a handle for the navigation view.
5843 NavigationView navigationView = findViewById(R.id.navigationview);
5845 // Get a handle for the navigation menu.
5846 Menu navigationMenu = navigationView.getMenu();
5848 // Get a handle for the navigation requests menu item.
5849 MenuItem navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests);
5851 // Create an empty web resource response to be used if the resource request is blocked.
5852 WebResourceResponse emptyWebResourceResponse = new WebResourceResponse("text/plain", "utf8", new ByteArrayInputStream("".getBytes()));
5854 // Reset the whitelist results tracker.
5855 String[] whitelistResultStringArray = null;
5857 // Initialize the third party request tracker.
5858 boolean isThirdPartyRequest = false;
5860 // Get the current URL. `.getUrl()` throws an error because operations on the WebView cannot be made from this thread.
5861 String currentBaseDomain = nestedScrollWebView.getCurrentDomainName();
5863 // Store a copy of the current domain for use in later requests.
5864 String currentDomain = currentBaseDomain;
5866 // Nobody is happy when comparing null strings.
5867 if ((currentBaseDomain != null) && (url != null)) {
5868 // Convert the request URL to a URI.
5869 Uri requestUri = Uri.parse(url);
5871 // Get the request host name.
5872 String requestBaseDomain = requestUri.getHost();
5874 // Only check for third-party requests if the current base domain is not empty and the request domain is not null.
5875 if (!currentBaseDomain.isEmpty() && (requestBaseDomain != null)) {
5876 // Determine the current base domain.
5877 while (currentBaseDomain.indexOf(".", currentBaseDomain.indexOf(".") + 1) > 0) { // There is at least one subdomain.
5878 // Remove the first subdomain.
5879 currentBaseDomain = currentBaseDomain.substring(currentBaseDomain.indexOf(".") + 1);
5882 // Determine the request base domain.
5883 while (requestBaseDomain.indexOf(".", requestBaseDomain.indexOf(".") + 1) > 0) { // There is at least one subdomain.
5884 // Remove the first subdomain.
5885 requestBaseDomain = requestBaseDomain.substring(requestBaseDomain.indexOf(".") + 1);
5888 // Update the third party request tracker.
5889 isThirdPartyRequest = !currentBaseDomain.equals(requestBaseDomain);
5893 // Get the current WebView page position.
5894 int webViewPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
5896 // Determine if the WebView is currently displayed.
5897 boolean webViewDisplayed = (webViewPagePosition == tabLayout.getSelectedTabPosition());
5899 // Block third-party requests if enabled.
5900 if (isThirdPartyRequest && nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.THIRD_PARTY_REQUESTS)) {
5901 // Add the result to the resource requests.
5902 nestedScrollWebView.addResourceRequest(new String[]{BlocklistHelper.REQUEST_THIRD_PARTY, url});
5904 // Increment the blocked requests counters.
5905 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5906 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS);
5908 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5909 if (webViewDisplayed) {
5910 // Updating the UI must be run from the UI thread.
5911 activity.runOnUiThread(() -> {
5912 // Update the menu item titles.
5913 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5915 // Update the options menu if it has been populated.
5916 if (optionsMenu != null) {
5917 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5918 optionsMenu.findItem(R.id.block_all_third_party_requests).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.THIRD_PARTY_REQUESTS) + " - " +
5919 getString(R.string.block_all_third_party_requests));
5924 // Return an empty web resource response.
5925 return emptyWebResourceResponse;
5928 // Check UltraList if it is enabled.
5929 if (nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.ULTRALIST)) {
5930 // Check the URL against UltraList.
5931 String[] ultraListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, ultraList);
5933 // Process the UltraList results.
5934 if (ultraListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched UltraLists's blacklist.
5935 // Add the result to the resource requests.
5936 nestedScrollWebView.addResourceRequest(new String[] {ultraListResults[0], ultraListResults[1], ultraListResults[2], ultraListResults[3], ultraListResults[4], ultraListResults[5]});
5938 // Increment the blocked requests counters.
5939 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5940 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.ULTRALIST);
5942 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5943 if (webViewDisplayed) {
5944 // Updating the UI must be run from the UI thread.
5945 activity.runOnUiThread(() -> {
5946 // Update the menu item titles.
5947 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5949 // Update the options menu if it has been populated.
5950 if (optionsMenu != null) {
5951 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5952 optionsMenu.findItem(R.id.ultralist).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.ULTRALIST) + " - " + getString(R.string.ultralist));
5957 // The resource request was blocked. Return an empty web resource response.
5958 return emptyWebResourceResponse;
5959 } else if (ultraListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched UltraList's whitelist.
5960 // Add a whitelist entry to the resource requests array.
5961 nestedScrollWebView.addResourceRequest(new String[] {ultraListResults[0], ultraListResults[1], ultraListResults[2], ultraListResults[3], ultraListResults[4], ultraListResults[5]});
5963 // The resource request has been allowed by UltraPrivacy. `return null` loads the requested resource.
5968 // Check UltraPrivacy if it is enabled.
5969 if (nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.ULTRAPRIVACY)) {
5970 // Check the URL against UltraPrivacy.
5971 String[] ultraPrivacyResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, ultraPrivacy);
5973 // Process the UltraPrivacy results.
5974 if (ultraPrivacyResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched UltraPrivacy's blacklist.
5975 // Add the result to the resource requests.
5976 nestedScrollWebView.addResourceRequest(new String[] {ultraPrivacyResults[0], ultraPrivacyResults[1], ultraPrivacyResults[2], ultraPrivacyResults[3], ultraPrivacyResults[4],
5977 ultraPrivacyResults[5]});
5979 // Increment the blocked requests counters.
5980 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
5981 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.ULTRAPRIVACY);
5983 // Update the titles of the blocklist menu items if the WebView is currently displayed.
5984 if (webViewDisplayed) {
5985 // Updating the UI must be run from the UI thread.
5986 activity.runOnUiThread(() -> {
5987 // Update the menu item titles.
5988 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5990 // Update the options menu if it has been populated.
5991 if (optionsMenu != null) {
5992 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
5993 optionsMenu.findItem(R.id.ultraprivacy).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.ULTRAPRIVACY) + " - " + getString(R.string.ultraprivacy));
5998 // The resource request was blocked. Return an empty web resource response.
5999 return emptyWebResourceResponse;
6000 } else if (ultraPrivacyResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched UltraPrivacy's whitelist.
6001 // Add a whitelist entry to the resource requests array.
6002 nestedScrollWebView.addResourceRequest(new String[] {ultraPrivacyResults[0], ultraPrivacyResults[1], ultraPrivacyResults[2], ultraPrivacyResults[3], ultraPrivacyResults[4],
6003 ultraPrivacyResults[5]});
6005 // The resource request has been allowed by UltraPrivacy. `return null` loads the requested resource.
6010 // Check EasyList if it is enabled.
6011 if (nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.EASYLIST)) {
6012 // Check the URL against EasyList.
6013 String[] easyListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, easyList);
6015 // Process the EasyList results.
6016 if (easyListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched EasyList's blacklist.
6017 // Add the result to the resource requests.
6018 nestedScrollWebView.addResourceRequest(new String[] {easyListResults[0], easyListResults[1], easyListResults[2], easyListResults[3], easyListResults[4], easyListResults[5]});
6020 // Increment the blocked requests counters.
6021 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
6022 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.EASYLIST);
6024 // Update the titles of the blocklist menu items if the WebView is currently displayed.
6025 if (webViewDisplayed) {
6026 // Updating the UI must be run from the UI thread.
6027 activity.runOnUiThread(() -> {
6028 // Update the menu item titles.
6029 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6031 // Update the options menu if it has been populated.
6032 if (optionsMenu != null) {
6033 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6034 optionsMenu.findItem(R.id.easylist).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.EASYLIST) + " - " + getString(R.string.easylist));
6039 // The resource request was blocked. Return an empty web resource response.
6040 return emptyWebResourceResponse;
6041 } else if (easyListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched EasyList's whitelist.
6042 // Update the whitelist result string array tracker.
6043 whitelistResultStringArray = new String[] {easyListResults[0], easyListResults[1], easyListResults[2], easyListResults[3], easyListResults[4], easyListResults[5]};
6047 // Check EasyPrivacy if it is enabled.
6048 if (nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.EASYPRIVACY)) {
6049 // Check the URL against EasyPrivacy.
6050 String[] easyPrivacyResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, easyPrivacy);
6052 // Process the EasyPrivacy results.
6053 if (easyPrivacyResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched EasyPrivacy's blacklist.
6054 // Add the result to the resource requests.
6055 nestedScrollWebView.addResourceRequest(new String[] {easyPrivacyResults[0], easyPrivacyResults[1], easyPrivacyResults[2], easyPrivacyResults[3], easyPrivacyResults[4],
6056 easyPrivacyResults[5]});
6058 // Increment the blocked requests counters.
6059 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
6060 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.EASYPRIVACY);
6062 // Update the titles of the blocklist menu items if the WebView is currently displayed.
6063 if (webViewDisplayed) {
6064 // Updating the UI must be run from the UI thread.
6065 activity.runOnUiThread(() -> {
6066 // Update the menu item titles.
6067 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6069 // Update the options menu if it has been populated.
6070 if (optionsMenu != null) {
6071 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6072 optionsMenu.findItem(R.id.easyprivacy).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.EASYPRIVACY) + " - " + getString(R.string.easyprivacy));
6077 // The resource request was blocked. Return an empty web resource response.
6078 return emptyWebResourceResponse;
6079 } else if (easyPrivacyResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched EasyPrivacy's whitelist.
6080 // Update the whitelist result string array tracker.
6081 whitelistResultStringArray = new String[] {easyPrivacyResults[0], easyPrivacyResults[1], easyPrivacyResults[2], easyPrivacyResults[3], easyPrivacyResults[4], easyPrivacyResults[5]};
6085 // Check Fanboy’s Annoyance List if it is enabled.
6086 if (nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST)) {
6087 // Check the URL against Fanboy's Annoyance List.
6088 String[] fanboysAnnoyanceListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, fanboysAnnoyanceList);
6090 // Process the Fanboy's Annoyance List results.
6091 if (fanboysAnnoyanceListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched Fanboy's Annoyance List's blacklist.
6092 // Add the result to the resource requests.
6093 nestedScrollWebView.addResourceRequest(new String[] {fanboysAnnoyanceListResults[0], fanboysAnnoyanceListResults[1], fanboysAnnoyanceListResults[2], fanboysAnnoyanceListResults[3],
6094 fanboysAnnoyanceListResults[4], fanboysAnnoyanceListResults[5]});
6096 // Increment the blocked requests counters.
6097 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
6098 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST);
6100 // Update the titles of the blocklist menu items if the WebView is currently displayed.
6101 if (webViewDisplayed) {
6102 // Updating the UI must be run from the UI thread.
6103 activity.runOnUiThread(() -> {
6104 // Update the menu item titles.
6105 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6107 // Update the options menu if it has been populated.
6108 if (optionsMenu != null) {
6109 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6110 optionsMenu.findItem(R.id.fanboys_annoyance_list).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.FANBOYS_ANNOYANCE_LIST) + " - " +
6111 getString(R.string.fanboys_annoyance_list));
6116 // The resource request was blocked. Return an empty web resource response.
6117 return emptyWebResourceResponse;
6118 } else if (fanboysAnnoyanceListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)){ // The resource request matched Fanboy's Annoyance List's whitelist.
6119 // Update the whitelist result string array tracker.
6120 whitelistResultStringArray = new String[] {fanboysAnnoyanceListResults[0], fanboysAnnoyanceListResults[1], fanboysAnnoyanceListResults[2], fanboysAnnoyanceListResults[3],
6121 fanboysAnnoyanceListResults[4], fanboysAnnoyanceListResults[5]};
6123 } else if (nestedScrollWebView.isBlocklistEnabled(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST)) { // Only check Fanboy’s Social Blocking List if Fanboy’s Annoyance List is disabled.
6124 // Check the URL against Fanboy's Annoyance List.
6125 String[] fanboysSocialListResults = blocklistHelper.checkBlocklist(currentDomain, url, isThirdPartyRequest, fanboysSocialList);
6127 // Process the Fanboy's Social Blocking List results.
6128 if (fanboysSocialListResults[0].equals(BlocklistHelper.REQUEST_BLOCKED)) { // The resource request matched Fanboy's Social Blocking List's blacklist.
6129 // Add the result to the resource requests.
6130 nestedScrollWebView.addResourceRequest(new String[] {fanboysSocialListResults[0], fanboysSocialListResults[1], fanboysSocialListResults[2], fanboysSocialListResults[3],
6131 fanboysSocialListResults[4], fanboysSocialListResults[5]});
6133 // Increment the blocked requests counters.
6134 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS);
6135 nestedScrollWebView.incrementRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST);
6137 // Update the titles of the blocklist menu items if the WebView is currently displayed.
6138 if (webViewDisplayed) {
6139 // Updating the UI must be run from the UI thread.
6140 activity.runOnUiThread(() -> {
6141 // Update the menu item titles.
6142 navigationRequestsMenuItem.setTitle(getString(R.string.requests) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6144 // Update the options menu if it has been populated.
6145 if (optionsMenu != null) {
6146 optionsMenu.findItem(R.id.blocklists).setTitle(getString(R.string.blocklists) + " - " + nestedScrollWebView.getRequestsCount(NestedScrollWebView.BLOCKED_REQUESTS));
6147 optionsMenu.findItem(R.id.fanboys_social_blocking_list).setTitle(nestedScrollWebView.getRequestsCount(NestedScrollWebView.FANBOYS_SOCIAL_BLOCKING_LIST) + " - " +
6148 getString(R.string.fanboys_social_blocking_list));
6153 // The resource request was blocked. Return an empty web resource response.
6154 return emptyWebResourceResponse;
6155 } else if (fanboysSocialListResults[0].equals(BlocklistHelper.REQUEST_ALLOWED)) { // The resource request matched Fanboy's Social Blocking List's whitelist.
6156 // Update the whitelist result string array tracker.
6157 whitelistResultStringArray = new String[] {fanboysSocialListResults[0], fanboysSocialListResults[1], fanboysSocialListResults[2], fanboysSocialListResults[3],
6158 fanboysSocialListResults[4], fanboysSocialListResults[5]};
6162 // Add the request to the log because it hasn't been processed by any of the previous checks.
6163 if (whitelistResultStringArray != null) { // The request was processed by a whitelist.
6164 nestedScrollWebView.addResourceRequest(whitelistResultStringArray);
6165 } else { // The request didn't match any blocklist entry. Log it as a default request.
6166 nestedScrollWebView.addResourceRequest(new String[]{BlocklistHelper.REQUEST_DEFAULT, url});
6169 // The resource request has not been blocked. `return null` loads the requested resource.
6173 // Handle HTTP authentication requests.
6175 public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
6176 // Store the handler.
6177 nestedScrollWebView.setHttpAuthHandler(handler);
6179 // Instantiate an HTTP authentication dialog.
6180 DialogFragment httpAuthenticationDialogFragment = HttpAuthenticationDialog.displayDialog(host, realm, nestedScrollWebView.getWebViewFragmentId());
6182 // Show the HTTP authentication dialog.
6183 httpAuthenticationDialogFragment.show(getSupportFragmentManager(), getString(R.string.http_authentication));
6187 public void onPageStarted(WebView view, String url, Bitmap favicon) {
6188 // Get the preferences.
6189 boolean scrollAppBar = sharedPreferences.getBoolean("scroll_app_bar", true);
6191 // 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.
6192 if (scrollAppBar || (inFullScreenBrowsingMode && hideAppBar)) {
6193 // No padding is needed because it will automatically be placed below the app bar layout due to the scrolling layout behavior.
6194 swipeRefreshLayout.setPadding(0, 0, 0, 0);
6196 // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
6197 swipeRefreshLayout.setProgressViewOffset(false, defaultProgressViewStartOffset - 10, defaultProgressViewEndOffset);
6199 // Get the app bar layout height. This can't be done in `applyAppSettings()` because the app bar is not yet populated there.
6200 appBarHeight = appBarLayout.getHeight();
6202 // The swipe refresh layout must be manually moved below the app bar layout.
6203 swipeRefreshLayout.setPadding(0, appBarHeight, 0, 0);
6205 // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
6206 swipeRefreshLayout.setProgressViewOffset(false, defaultProgressViewStartOffset - 10 + appBarHeight, defaultProgressViewEndOffset + appBarHeight);
6209 // Reset the list of resource requests.
6210 nestedScrollWebView.clearResourceRequests();
6212 // Reset the requests counters.
6213 nestedScrollWebView.resetRequestsCounters();
6215 // Hide the keyboard.
6216 inputMethodManager.hideSoftInputFromWindow(nestedScrollWebView.getWindowToken(), 0);
6218 // Get the current page position.
6219 int currentPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
6221 // Update the URL text bar if the page is currently selected.
6222 if (tabLayout.getSelectedTabPosition() == currentPagePosition) {
6223 // Clear the focus from the URL edit text.
6224 urlEditText.clearFocus();
6226 // Display the formatted URL text.
6227 urlEditText.setText(url);
6229 // Apply text highlighting to `urlTextBox`.
6233 // Reset the list of host IP addresses.
6234 nestedScrollWebView.clearCurrentIpAddresses();
6236 // Get a URI for the current URL.
6237 Uri currentUri = Uri.parse(url);
6239 // Get the IP addresses for the host.
6240 new GetHostIpAddresses(activity, getSupportFragmentManager(), nestedScrollWebView).execute(currentUri.getHost());
6242 // Replace Refresh with Stop if the options menu has been created. (The first WebView typically begins loading before the menu items are instantiated.)
6243 if (optionsMenu != null) {
6244 // Get a handle for the refresh menu item.
6245 MenuItem refreshMenuItem = optionsMenu.findItem(R.id.refresh);
6248 refreshMenuItem.setTitle(R.string.stop);
6250 // Get the app bar and theme preferences.
6251 boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean("display_additional_app_bar_icons", false);
6253 // If the icon is displayed in the AppBar, set it according to the theme.
6254 if (displayAdditionalAppBarIcons) {
6255 // Get the current theme status.
6256 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
6258 // Set the stop icon according to the theme.
6259 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
6260 refreshMenuItem.setIcon(R.drawable.close_day);
6262 refreshMenuItem.setIcon(R.drawable.close_night);
6269 public void onPageFinished(WebView view, String url) {
6270 // Flush any cookies to persistent storage. The cookie manager has become very lazy about flushing cookies in recent versions.
6271 if (nestedScrollWebView.getAcceptFirstPartyCookies() && Build.VERSION.SDK_INT >= 21) {
6272 CookieManager.getInstance().flush();
6275 // Update the Refresh menu item if the options menu has been created.
6276 if (optionsMenu != null) {
6277 // Get a handle for the refresh menu item.
6278 MenuItem refreshMenuItem = optionsMenu.findItem(R.id.refresh);
6280 // Reset the Refresh title.
6281 refreshMenuItem.setTitle(R.string.refresh);
6283 // Get the app bar and theme preferences.
6284 boolean displayAdditionalAppBarIcons = sharedPreferences.getBoolean("display_additional_app_bar_icons", false);
6286 // If the icon is displayed in the app bar, reset it according to the theme.
6287 if (displayAdditionalAppBarIcons) {
6288 // Get the current theme status.
6289 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
6291 // Set the icon according to the theme.
6292 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
6293 refreshMenuItem.setIcon(R.drawable.refresh_enabled_day);
6295 refreshMenuItem.setIcon(R.drawable.refresh_enabled_night);
6300 // Clear the cache, history, and logcat if Incognito Mode is enabled.
6301 if (incognitoModeEnabled) {
6302 // Clear the cache. `true` includes disk files.
6303 nestedScrollWebView.clearCache(true);
6305 // Clear the back/forward history.
6306 nestedScrollWebView.clearHistory();
6308 // Manually delete cache folders.
6310 // Get the application's private data directory, which will be something like `/data/user/0/com.stoutner.privacybrowser.standard`,
6311 // which links to `/data/data/com.stoutner.privacybrowser.standard`.
6312 String privateDataDirectoryString = getApplicationInfo().dataDir;
6314 // Delete the main cache directory.
6315 Runtime.getRuntime().exec("rm -rf " + privateDataDirectoryString + "/cache");
6317 // Delete the secondary `Service Worker` cache directory.
6318 // A `String[]` must be used because the directory contains a space and `Runtime.exec` will not escape the string correctly otherwise.
6319 Runtime.getRuntime().exec(new String[]{"rm", "-rf", privateDataDirectoryString + "/app_webview/Service Worker/"});
6320 } catch (IOException exception) {
6321 // Do nothing if an error is thrown.
6324 // Clear the logcat.
6326 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
6327 Runtime.getRuntime().exec("logcat -b all -c");
6328 } catch (IOException exception) {
6333 // Get the current page position.
6334 int currentPagePosition = webViewPagerAdapter.getPositionForId(nestedScrollWebView.getWebViewFragmentId());
6336 // Check the current website information against any pinned domain information if the current IP addresses have been loaded.
6337 if ((nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.hasPinnedIpAddresses()) && nestedScrollWebView.hasCurrentIpAddresses() &&
6338 !nestedScrollWebView.ignorePinnedDomainInformation()) {
6339 CheckPinnedMismatchHelper.checkPinnedMismatch(getSupportFragmentManager(), nestedScrollWebView);
6342 // 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.
6343 String currentUrl = nestedScrollWebView.getUrl();
6345 // Get the current tab.
6346 TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition);
6348 // Update the URL text bar if the page is currently selected and the user is not currently typing in the URL edit text.
6349 // Crash records show that, in some crazy way, it is possible for the current URL to be blank at this point.
6350 // Probably some sort of race condition when Privacy Browser is being resumed.
6351 if ((tabLayout.getSelectedTabPosition() == currentPagePosition) && !urlEditText.hasFocus() && (currentUrl != null)) {
6352 // Check to see if the URL is `about:blank`.
6353 if (currentUrl.equals("about:blank")) { // The WebView is blank.
6354 // Display the hint in the URL edit text.
6355 urlEditText.setText("");
6357 // Request focus for the URL text box.
6358 urlEditText.requestFocus();
6360 // Display the keyboard.
6361 inputMethodManager.showSoftInput(urlEditText, 0);
6363 // Apply the domain settings. This clears any settings from the previous domain.
6364 applyDomainSettings(nestedScrollWebView, "", true, false);
6366 // Only populate the title text view if the tab has been fully created.
6368 // Get the custom view from the tab.
6369 View tabView = tab.getCustomView();
6371 // Remove the incorrect warning below that the current tab view might be null.
6372 assert tabView != null;
6374 // Get the title text view from the tab.
6375 TextView tabTitleTextView = tabView.findViewById(R.id.title_textview);
6377 // Set the title as the tab text.
6378 tabTitleTextView.setText(R.string.new_tab);
6380 } else { // The WebView has loaded a webpage.
6381 // Update the URL edit text if it is not currently being edited.
6382 if (!urlEditText.hasFocus()) {
6383 // 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.
6384 String sanitizedUrl = sanitizeUrl(currentUrl);
6386 // Display the final URL. Getting the URL from the WebView instead of using the one provided by `onPageFinished()` makes websites like YouTube function correctly.
6387 urlEditText.setText(sanitizedUrl);
6389 // Apply text highlighting to the URL.
6393 // Only populate the title text view if the tab has been fully created.
6395 // Get the custom view from the tab.
6396 View tabView = tab.getCustomView();
6398 // Remove the incorrect warning below that the current tab view might be null.
6399 assert tabView != null;
6401 // Get the title text view from the tab.
6402 TextView tabTitleTextView = tabView.findViewById(R.id.title_textview);
6404 // Set the title as the tab text. Sometimes `onReceivedTitle()` is not called, especially when navigating history.
6405 tabTitleTextView.setText(nestedScrollWebView.getTitle());
6411 // Handle SSL Certificate errors.
6413 public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
6414 // Get the current website SSL certificate.
6415 SslCertificate currentWebsiteSslCertificate = error.getCertificate();
6417 // Extract the individual pieces of information from the current website SSL certificate.
6418 String currentWebsiteIssuedToCName = currentWebsiteSslCertificate.getIssuedTo().getCName();
6419 String currentWebsiteIssuedToOName = currentWebsiteSslCertificate.getIssuedTo().getOName();
6420 String currentWebsiteIssuedToUName = currentWebsiteSslCertificate.getIssuedTo().getUName();
6421 String currentWebsiteIssuedByCName = currentWebsiteSslCertificate.getIssuedBy().getCName();
6422 String currentWebsiteIssuedByOName = currentWebsiteSslCertificate.getIssuedBy().getOName();
6423 String currentWebsiteIssuedByUName = currentWebsiteSslCertificate.getIssuedBy().getUName();
6424 Date currentWebsiteSslStartDate = currentWebsiteSslCertificate.getValidNotBeforeDate();
6425 Date currentWebsiteSslEndDate = currentWebsiteSslCertificate.getValidNotAfterDate();
6427 // Proceed to the website if the current SSL website certificate matches the pinned domain certificate.
6428 if (nestedScrollWebView.hasPinnedSslCertificate()) {
6429 // Get the pinned SSL certificate.
6430 ArrayList<Object> pinnedSslCertificateArrayList = nestedScrollWebView.getPinnedSslCertificate();
6432 // Extract the arrays from the array list.
6433 String[] pinnedSslCertificateStringArray = (String[]) pinnedSslCertificateArrayList.get(0);
6434 Date[] pinnedSslCertificateDateArray = (Date[]) pinnedSslCertificateArrayList.get(1);
6436 // Check if the current SSL certificate matches the pinned certificate.
6437 if (currentWebsiteIssuedToCName.equals(pinnedSslCertificateStringArray[0]) && currentWebsiteIssuedToOName.equals(pinnedSslCertificateStringArray[1]) &&
6438 currentWebsiteIssuedToUName.equals(pinnedSslCertificateStringArray[2]) && currentWebsiteIssuedByCName.equals(pinnedSslCertificateStringArray[3]) &&
6439 currentWebsiteIssuedByOName.equals(pinnedSslCertificateStringArray[4]) && currentWebsiteIssuedByUName.equals(pinnedSslCertificateStringArray[5]) &&
6440 currentWebsiteSslStartDate.equals(pinnedSslCertificateDateArray[0]) && currentWebsiteSslEndDate.equals(pinnedSslCertificateDateArray[1])) {
6442 // An SSL certificate is pinned and matches the current domain certificate. Proceed to the website without displaying an error.
6445 } else { // Either there isn't a pinned SSL certificate or it doesn't match the current website certificate.
6446 // Store the SSL error handler.
6447 nestedScrollWebView.setSslErrorHandler(handler);
6449 // Instantiate an SSL certificate error alert dialog.
6450 DialogFragment sslCertificateErrorDialogFragment = SslCertificateErrorDialog.displayDialog(error, nestedScrollWebView.getWebViewFragmentId());
6452 // Show the SSL certificate error dialog.
6453 sslCertificateErrorDialogFragment.show(getSupportFragmentManager(), getString(R.string.ssl_certificate_error));
6458 // Check to see if the state is being restored.
6459 if (restoringState) { // The state is being restored.
6460 // Resume the nested scroll WebView JavaScript timers.
6461 nestedScrollWebView.resumeTimers();
6462 } else if (pageNumber == 0) { // The first page is being loaded.
6463 // Set this nested scroll WebView as the current WebView.
6464 currentWebView = nestedScrollWebView;
6466 // Initialize the URL to load string.
6467 String urlToLoadString;
6469 // Get the intent that started the app.
6470 Intent launchingIntent = getIntent();
6472 // Reset the intent. This prevents a duplicate tab from being created on restart.
6473 setIntent(new Intent());
6475 // Get the information from the intent.
6476 String launchingIntentAction = launchingIntent.getAction();
6477 Uri launchingIntentUriData = launchingIntent.getData();
6479 // Parse the launching intent URL.
6480 if ((launchingIntentAction != null) && launchingIntentAction.equals(Intent.ACTION_WEB_SEARCH)) { // The intent contains a search string.
6481 // Create an encoded URL string.
6482 String encodedUrlString;
6484 // Sanitize the search input and convert it to a search.
6486 encodedUrlString = URLEncoder.encode(launchingIntent.getStringExtra(SearchManager.QUERY), "UTF-8");
6487 } catch (UnsupportedEncodingException exception) {
6488 encodedUrlString = "";
6491 // Store the web search as the URL to load.
6492 urlToLoadString = searchURL + encodedUrlString;
6493 } else if (launchingIntentUriData != null){ // The intent contains a URL.
6495 urlToLoadString = launchingIntentUriData.toString();
6496 } else if (!url.equals("")) { // The activity has been restarted.
6497 // Load the saved URL.
6498 urlToLoadString = url;
6499 } else { // The is no URL in the intent.
6500 // Store the homepage to be loaded.
6501 urlToLoadString = sharedPreferences.getString("homepage", getString(R.string.homepage_default_value));
6504 // Load the website if not waiting for the proxy.
6505 if (waitingForProxy) { // Store the URL to be loaded in the Nested Scroll WebView.
6506 nestedScrollWebView.setWaitingForProxyUrlString(urlToLoadString);
6507 } else { // Load the URL.
6508 loadUrl(nestedScrollWebView, urlToLoadString);
6510 } else { // This is not the first tab.
6512 loadUrl(nestedScrollWebView, url);
6514 // Set the focus and display the keyboard if the URL is blank.
6515 if (url.equals("")) {
6516 // Request focus for the URL text box.
6517 urlEditText.requestFocus();
6519 // Create a display keyboard handler.
6520 Handler displayKeyboardHandler = new Handler();
6522 // Create a display keyboard runnable.
6523 Runnable displayKeyboardRunnable = () -> {
6524 // Display the keyboard.
6525 inputMethodManager.showSoftInput(urlEditText, 0);
6528 // Display the keyboard after 100 milliseconds, which leaves enough time for the tab to transition.
6529 displayKeyboardHandler.postDelayed(displayKeyboardRunnable, 100);