+ protected void onSaveInstanceState(@NonNull Bundle outState) {
+ // Store the current `DomainSettingsFragment` state in `outState`.
+ if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed.
+ // Save any changes that have been made to the domain settings.
+ saveDomainSettings(coordinatorLayout, resources);
+
+ // Store `DomainSettingsDisplayed`.
+ outState.putBoolean("domain_settings_displayed", true);
+ outState.putInt("domain_settings_database_id", DomainSettingsFragment.databaseId);
+ } else { // `DomainSettingsFragment` is not displayed.
+ outState.putBoolean("domain_settings_displayed", false);
+ outState.putInt("domain_settings_database_id", -1);
+ }
+
+ super.onSaveInstanceState(outState);
+ }
+
+ // Control what the navigation bar back button does.
+ @Override
+ public void onBackPressed() {
+ // Get a handle for the fragment manager.
+ FragmentManager fragmentManager = getSupportFragmentManager();
+
+ if (twoPanedMode) { // The device is in two-paned mode.
+ // Save the current domain settings if the domain settings fragment is displayed.
+ if (findViewById(R.id.domain_settings_scrollview) != null) {
+ saveDomainSettings(coordinatorLayout, resources);
+ }
+
+ // Dismiss the undo delete SnackBar if it is shown.
+ if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
+
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
+ } else {
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ }
+ } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
+ // Save the current domain settings.
+ saveDomainSettings(coordinatorLayout, resources);
+
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and domain settings fragment is displayed.
+ // Save the current domain settings.
+ saveDomainSettings(coordinatorLayout, resources);
+
+ // Display the domains list fragment.
+ DomainsListFragment domainsListFragment = new DomainsListFragment();
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
+ fragmentManager.executePendingTransactions();
+
+ // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
+ populateDomainsListView(-1);
+
+ // Show the add domain floating action button.
+ addDomainFAB.show();
+
+ // Hide the delete menu item.
+ deleteMenuItem.setVisible(false);
+ } else { // The device is in single-paned mode and the domain list fragment is displayed.
+ // Dismiss the undo delete SnackBar if it is shown.
+ if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
+
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
+ } else {
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ }
+ }
+ }
+
+ @Override
+ public void onAddDomain(@NonNull DialogFragment dialogFragment) {
+ // Dismiss the undo delete snackbar if it is currently displayed.
+ if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
+ undoDeleteSnackbar.dismiss();
+ }
+
+ // Remove the incorrect lint warning below that the dialog might be null.
+ assert dialogFragment.getDialog() != null;
+
+ // Get a handle for the domain name edit text.
+ EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
+
+ // Get the domain name string.
+ String domainNameString = domainNameEditText.getText().toString();
+
+ // Create the domain and store the database ID in `currentDomainDatabaseId`.
+ currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
+
+ // Display the newly created domain.
+ if (twoPanedMode) { // The device in in two-paned mode.
+ populateDomainsListView(currentDomainDatabaseId);
+ } else { // The device is in single-paned mode.
+ // Hide the add domain floating action button.
+ addDomainFAB.hide();
+
+ // Show and enable `deleteMenuItem`.
+ DomainsActivity.deleteMenuItem.setVisible(true);
+
+ // Add the current domain database ID to the arguments bundle.
+ Bundle argumentsBundle = new Bundle();
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
+
+ // Add and arguments bundle to the domain setting fragment.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+ domainSettingsFragment.setArguments(argumentsBundle);
+
+ // Display the domain settings fragment.
+ getSupportFragmentManager().beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+ }
+ }
+
+ public void saveDomainSettings(View view, Resources resources) {
+ // Get handles for the domain settings.
+ EditText domainNameEditText = view.findViewById(R.id.domain_settings_name_edittext);
+ Switch javaScriptSwitch = view.findViewById(R.id.javascript_switch);
+ Switch firstPartyCookiesSwitch = view.findViewById(R.id.first_party_cookies_switch);
+ Switch thirdPartyCookiesSwitch = view.findViewById(R.id.third_party_cookies_switch);
+ Switch domStorageSwitch = view.findViewById(R.id.dom_storage_switch);
+ Switch formDataSwitch = view.findViewById(R.id.form_data_switch); // Form data can be removed once the minimum API >= 26.
+ Switch easyListSwitch = view.findViewById(R.id.easylist_switch);
+ Switch easyPrivacySwitch = view.findViewById(R.id.easyprivacy_switch);
+ Switch fanboysAnnoyanceSwitch = view.findViewById(R.id.fanboys_annoyance_list_switch);
+ Switch fanboysSocialBlockingSwitch = view.findViewById(R.id.fanboys_social_blocking_list_switch);
+ Switch ultraListSwitch = view.findViewById(R.id.ultralist_switch);
+ Switch ultraPrivacySwitch = view.findViewById(R.id.ultraprivacy_switch);
+ Switch blockAllThirdPartyRequestsSwitch = view.findViewById(R.id.block_all_third_party_requests_switch);
+ Spinner userAgentSpinner = view.findViewById(R.id.user_agent_spinner);
+ EditText customUserAgentEditText = view.findViewById(R.id.custom_user_agent_edittext);
+ Spinner fontSizeSpinner = view.findViewById(R.id.font_size_spinner);
+ EditText customFontSizeEditText = view.findViewById(R.id.custom_font_size_edittext);
+ Spinner swipeToRefreshSpinner = view.findViewById(R.id.swipe_to_refresh_spinner);
+ Spinner nightModeSpinner = view.findViewById(R.id.night_mode_spinner);
+ Spinner wideViewportSpinner = view.findViewById(R.id.wide_viewport_spinner);
+ Spinner displayWebpageImagesSpinner = view.findViewById(R.id.display_webpage_images_spinner);
+ Switch pinnedSslCertificateSwitch = view.findViewById(R.id.pinned_ssl_certificate_switch);
+ RadioButton currentWebsiteCertificateRadioButton = view.findViewById(R.id.current_website_certificate_radiobutton);
+ Switch pinnedIpAddressesSwitch = view.findViewById(R.id.pinned_ip_addresses_switch);
+ RadioButton currentIpAddressesRadioButton = view.findViewById(R.id.current_ip_addresses_radiobutton);
+
+ // Extract the data for the domain settings.