2 * Copyright © 2017-2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.activities;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.content.res.Configuration;
27 import android.content.res.Resources;
28 import android.database.Cursor;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.preference.PreferenceManager;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.view.WindowManager;
37 import android.widget.CursorAdapter;
38 import android.widget.EditText;
39 import android.widget.ListView;
40 import android.widget.RadioButton;
41 import android.widget.ScrollView;
42 import android.widget.Spinner;
43 import android.widget.TextView;
45 import androidx.annotation.NonNull;
46 import androidx.appcompat.app.ActionBar;
47 import androidx.appcompat.app.AppCompatActivity;
48 import androidx.appcompat.widget.SwitchCompat;
49 import androidx.appcompat.widget.Toolbar;
50 import androidx.core.app.NavUtils;
51 import androidx.fragment.app.DialogFragment;
52 import androidx.fragment.app.FragmentManager;
54 import com.google.android.material.floatingactionbutton.FloatingActionButton;
55 import com.google.android.material.snackbar.Snackbar;
57 import com.stoutner.privacybrowser.R;
58 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
59 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
60 import com.stoutner.privacybrowser.fragments.DomainsListFragment;
61 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
63 import java.util.Objects;
65 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener, DomainsListFragment.DismissSnackbarInterface {
66 // Define the public static variables.
67 public static int domainsListViewPosition;
68 public static boolean twoPanedMode;
69 public static int currentDomainDatabaseId;
70 public static MenuItem deleteMenuItem;
71 public static boolean dismissingSnackbar;
73 // The SSL certificate and IP address information are accessed from `DomainSettingsFragment` and `saveDomainSettings()`.
74 public static String sslIssuedToCName;
75 public static String sslIssuedToOName;
76 public static String sslIssuedToUName;
77 public static String sslIssuedByCName;
78 public static String sslIssuedByOName;
79 public static String sslIssuedByUName;
80 public static long sslStartDateLong;
81 public static long sslEndDateLong;
82 public static String currentIpAddresses;
85 // Initialize the class constants.
86 private final String LISTVIEW_POSITION = "listview_position";
87 private final String DOMAIN_SETTINGS_DISPLAYED = "domain_settings_displayed";
88 private final String DOMAIN_SETTINGS_DATABASE_ID = "domain_settings_database_is";
89 private final String DOMAIN_SETTINGS_SCROLL_Y = "domain_settings_scroll_y";
91 // Initialize the class variables.
92 private boolean restartAfterRotate;
93 private boolean domainSettingsDisplayedBeforeRotate;
94 private int domainSettingsDatabaseIdBeforeRotate;
95 private int domainSettingsScrollY = 0;
97 // Defile the class views.
98 private ListView domainsListView;
100 // `closeActivityAfterDismissingSnackbar` is used in `onOptionsItemSelected()`, and `onBackPressed()`.
101 private boolean closeActivityAfterDismissingSnackbar;
103 // The undelete snackbar is used in `onOptionsItemSelected()` and `onBackPressed()`.
104 private Snackbar undoDeleteSnackbar;
106 // `domainsDatabaseHelper` is used in `onCreate()`, `saveDomainSettings()`, and `onDestroy()`.
107 private static DomainsDatabaseHelper domainsDatabaseHelper;
109 // `addDomainFAB` is used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
110 private FloatingActionButton addDomainFAB;
112 // `deletedDomainPosition` is used in an inner and outer class in `onOptionsItemSelected()`.
113 private int deletedDomainPosition;
115 // `goDirectlyToDatabaseId` is used in `onCreate()` and `onCreateOptionsMenu()`.
116 private int goDirectlyToDatabaseId;
118 // `closeOnBack` is used in `onCreate()`, `onOptionsItemSelected()` and `onBackPressed()`.
119 private boolean closeOnBack;
121 // `coordinatorLayout` is use in `onCreate()`, `onOptionsItemSelected()`, and `onSaveInstanceState()`.
122 private View coordinatorLayout;
124 // `resources` is used in `onCreate()`, `onOptionsItemSelected()`, and `onSaveInstanceState()`.
125 private Resources resources;
128 protected void onCreate(Bundle savedInstanceState) {
129 // Get a handle for the shared preferences.
130 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
132 // Get the preferences.
133 boolean allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false);
134 boolean bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false);
136 // Disable screenshots if not allowed.
137 if (!allowScreenshots) {
138 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
142 setTheme(R.style.PrivacyBrowser);
144 // Run the default commands.
145 super.onCreate(savedInstanceState);
147 // Initialize the domains listview position.
148 domainsListViewPosition = 0;
150 // Extract the values from the saved instance state if it is not null.
151 if (savedInstanceState != null) {
152 domainsListViewPosition = savedInstanceState.getInt(LISTVIEW_POSITION);
153 restartAfterRotate = true;
154 domainSettingsDisplayedBeforeRotate = savedInstanceState.getBoolean(DOMAIN_SETTINGS_DISPLAYED);
155 domainSettingsDatabaseIdBeforeRotate = savedInstanceState.getInt(DOMAIN_SETTINGS_DATABASE_ID);
156 domainSettingsScrollY = savedInstanceState.getInt(DOMAIN_SETTINGS_SCROLL_Y);
159 // Get the launching intent
160 Intent intent = getIntent();
162 // Extract the domain to load if there is one. `-1` is the default value.
163 goDirectlyToDatabaseId = intent.getIntExtra("load_domain", -1);
165 // Get the status of close-on-back, which is true when the domains activity is called from the options menu.
166 closeOnBack = intent.getBooleanExtra("close_on_back", false);
168 // Get the current URL.
169 String currentUrl = intent.getStringExtra("current_url");
171 // Store the current SSL certificate information in class variables.
172 sslIssuedToCName = intent.getStringExtra("ssl_issued_to_cname");
173 sslIssuedToOName = intent.getStringExtra("ssl_issued_to_oname");
174 sslIssuedToUName = intent.getStringExtra("ssl_issued_to_uname");
175 sslIssuedByCName = intent.getStringExtra("ssl_issued_by_cname");
176 sslIssuedByOName = intent.getStringExtra("ssl_issued_by_oname");
177 sslIssuedByUName = intent.getStringExtra("ssl_issued_by_uname");
178 sslStartDateLong = intent.getLongExtra("ssl_start_date", 0);
179 sslEndDateLong = intent.getLongExtra("ssl_end_date", 0);
180 currentIpAddresses = intent.getStringExtra("current_ip_addresses");
184 setContentView(R.layout.domains_coordinatorlayout_bottom_appbar);
186 setContentView(R.layout.domains_coordinatorlayout_top_appbar);
189 // Populate the class variables.
190 coordinatorLayout = findViewById(R.id.domains_coordinatorlayout);
191 resources = getResources();
193 // `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
194 final Toolbar toolbar = findViewById(R.id.domains_toolbar);
195 setSupportActionBar(toolbar);
197 // Get a handle for the action bar.
198 ActionBar actionBar = getSupportActionBar();
200 // Remove the incorrect lint warning that the action bar might be null.
201 assert actionBar != null;
203 // Set the back arrow on the action bar.
204 actionBar.setDisplayHomeAsUpEnabled(true);
206 // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
207 domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
209 // Determine if we are in two pane mode. `domain_settings_fragment_container` does not exist on devices with a width less than 900dp.
210 twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null);
212 // Get a handle for the add domain floating action button.
213 addDomainFAB = findViewById(R.id.add_domain_fab);
215 // Configure the add domain floating action button.
216 addDomainFAB.setOnClickListener((View view) -> {
217 // Remove the incorrect warning below that the current URL might be null.
218 assert currentUrl != null;
220 // Create an add domain dialog.
221 DialogFragment addDomainDialog = AddDomainDialog.addDomain(currentUrl);
223 // Show the add domain dialog.
224 addDomainDialog.show(getSupportFragmentManager(), resources.getString(R.string.add_domain));
229 public boolean onCreateOptionsMenu(Menu menu) {
231 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
233 // Store `deleteMenuItem` for future use.
234 deleteMenuItem = menu.findItem(R.id.delete_domain);
236 // Only display `deleteMenuItem` (initially) in two-paned mode.
237 deleteMenuItem.setVisible(twoPanedMode);
239 // Get a handle for the fragment manager.
240 FragmentManager fragmentManager = getSupportFragmentManager();
242 // Display the fragments. This must be done from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs `deleteMenuItem` to be inflated.
243 if (restartAfterRotate && domainSettingsDisplayedBeforeRotate) { // The device was rotated and domain settings were displayed previously.
244 if (twoPanedMode) { // The device is in two-paned mode.
245 // Reset `restartAfterRotate`.
246 restartAfterRotate = false;
248 // Display `DomainsListFragment`.
249 DomainsListFragment domainsListFragment = new DomainsListFragment();
250 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
251 fragmentManager.executePendingTransactions();
253 // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
254 populateDomainsListView(domainSettingsDatabaseIdBeforeRotate, domainsListViewPosition);
255 } else { // The device is in single-paned mode.
256 // Reset `restartAfterRotate`.
257 restartAfterRotate = false;
259 // Store the current domain database ID.
260 currentDomainDatabaseId = domainSettingsDatabaseIdBeforeRotate;
262 // Create an arguments bundle.
263 Bundle argumentsBundle = new Bundle();
265 // Add the domain settings arguments.
266 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
267 argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
269 // Instantiate a new domain settings fragment.
270 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
272 // Add the arguments bundle to the domain settings fragment.
273 domainSettingsFragment.setArguments(argumentsBundle);
275 // Show the delete menu item.
276 deleteMenuItem.setVisible(true);
278 // Hide the add domain floating action button.
281 // Display the domain settings fragment.
282 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
284 } else { // The device was not rotated or, if it was, domain settings were not displayed previously.
285 if (goDirectlyToDatabaseId >=0) { // Load the indicated domain settings.
286 // Store the current domain database ID.
287 currentDomainDatabaseId = goDirectlyToDatabaseId;
289 if (twoPanedMode) { // The device is in two-paned mode.
290 // Display `DomainsListFragment`.
291 DomainsListFragment domainsListFragment = new DomainsListFragment();
292 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
293 fragmentManager.executePendingTransactions();
295 // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
296 populateDomainsListView(goDirectlyToDatabaseId, domainsListViewPosition);
297 } else { // The device is in single-paned mode.
298 // Create an arguments bundle.
299 Bundle argumentsBundle = new Bundle();
301 // Add the domain settings to arguments bundle.
302 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, goDirectlyToDatabaseId);
303 argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
305 // Instantiate a new domain settings fragment.
306 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
308 // Add the arguments bundle to the domain settings fragment`.
309 domainSettingsFragment.setArguments(argumentsBundle);
311 // Show the delete menu item.
312 deleteMenuItem.setVisible(true);
314 // Hide the add domain floating action button.
317 // Display the domain settings fragment.
318 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
320 } else { // Highlight the first domain.
321 // Display `DomainsListFragment`.
322 DomainsListFragment domainsListFragment = new DomainsListFragment();
323 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
324 fragmentManager.executePendingTransactions();
326 // Populate the list of domains. `-1` highlights the first domain.
327 populateDomainsListView(-1, domainsListViewPosition);
336 public boolean onOptionsItemSelected(MenuItem menuItem) {
337 // Get the ID of the menu item that was selected.
338 int menuItemId = menuItem.getItemId();
340 // Get a handle for the fragment manager.
341 FragmentManager fragmentManager = getSupportFragmentManager();
343 // Run the command according to the selected menu item.
344 if (menuItemId == android.R.id.home) { // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
345 // Check if the device is in two-paned mode.
346 if (twoPanedMode) { // The device is in two-paned mode.
347 // Save the current domain settings if the domain settings fragment is displayed.
348 if (findViewById(R.id.domain_settings_scrollview) != null) {
349 saveDomainSettings(coordinatorLayout, resources);
352 // Dismiss the undo delete snackbar if it is shown.
353 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
354 // Set the close flag.
355 closeActivityAfterDismissingSnackbar = true;
357 // Dismiss the snackbar.
358 undoDeleteSnackbar.dismiss();
361 NavUtils.navigateUpFromSameTask(this);
363 } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
364 // Save the current domain settings.
365 saveDomainSettings(coordinatorLayout, resources);
368 NavUtils.navigateUpFromSameTask(this);
369 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and the domain settings fragment is displayed.
370 // Save the current domain settings.
371 saveDomainSettings(coordinatorLayout, resources);
373 // Display the domains list fragment.
374 DomainsListFragment domainsListFragment = new DomainsListFragment();
375 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
376 fragmentManager.executePendingTransactions();
378 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
379 populateDomainsListView(-1, domainsListViewPosition);
381 // Show the add domain floating action button.
384 // Hide the delete menu item.
385 deleteMenuItem.setVisible(false);
386 } else { // The device is in single-paned mode and domains list fragment is displayed.
387 // Dismiss the undo delete `SnackBar` if it is shown.
388 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
389 // Set the close flag.
390 closeActivityAfterDismissingSnackbar = true;
392 // Dismiss the snackbar.
393 undoDeleteSnackbar.dismiss();
396 NavUtils.navigateUpFromSameTask(this);
399 } else if (menuItemId == R.id.delete_domain) { // Delete.
400 // Get a handle for the activity.
401 Activity activity = this;
403 // Check to see if the domain settings were loaded directly for editing of this app in single-paned mode.
404 if (closeOnBack && !twoPanedMode) { // The activity should delete the domain settings and exit straight to the the main WebView activity.
405 // Delete the selected domain.
406 domainsDatabaseHelper.deleteDomain(currentDomainDatabaseId);
409 NavUtils.navigateUpFromSameTask(activity);
410 } else { // A snackbar should be shown before deleting the domain settings.
411 // Reset close-on-back, which otherwise can cause errors if the system attempts to save settings for a domain that no longer exists.
414 // Store a copy of the current domain database ID because it could change while the snackbar is displayed.
415 final int databaseIdToDelete = currentDomainDatabaseId;
417 // Update the fragments and menu items.
418 if (twoPanedMode) { // Two-paned mode.
419 // Store the deleted domain position, which is needed if undo is selected in the snackbar.
420 deletedDomainPosition = domainsListView.getCheckedItemPosition();
422 // Disable the options menu items.
423 deleteMenuItem.setEnabled(false);
424 deleteMenuItem.setIcon(R.drawable.delete_disabled);
426 // Remove the domain settings fragment.
427 fragmentManager.beginTransaction().remove(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.domain_settings_fragment_container))).commit();
428 } else { // Single-paned mode.
429 // Display the domains list fragment.
430 DomainsListFragment domainsListFragment = new DomainsListFragment();
431 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
432 fragmentManager.executePendingTransactions();
434 // Show the add domain floating action button.
437 // Hide `deleteMenuItem`.
438 deleteMenuItem.setVisible(false);
441 // Get a cursor that does not show the domain to be deleted.
442 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
444 // Setup the domains pending delete cursor adapter.
445 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
447 public View newView(Context context, Cursor cursor, ViewGroup parent) {
448 // Inflate the individual item layout.
449 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
453 public void bindView(View view, Context context, Cursor cursor) {
454 // Get the domain name string.
455 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
457 // Get a handle for the domain name text view.
458 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
460 // Display the domain name.
461 domainNameTextView.setText(domainNameString);
465 // Update the handle for the current domains list view.
466 domainsListView = findViewById(R.id.domains_listview);
468 // Update the list view.
469 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
471 // Display a snackbar.
472 undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
473 .setAction(R.string.undo, (View v) -> {
474 // Do nothing because everything will be handled by `onDismissed()` below.
476 .addCallback(new Snackbar.Callback() {
478 public void onDismissed(Snackbar snackbar, int event) {
479 // Run commands based on the event.
480 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { // The user pushed the `Undo` button.
481 // Create an arguments bundle.
482 Bundle argumentsBundle = new Bundle();
484 // Store the domains settings in the arguments bundle.
485 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
486 argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
488 // Instantiate a new domain settings fragment.
489 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
491 // Add the arguments bundle to the domain settings fragment.
492 domainSettingsFragment.setArguments(argumentsBundle);
494 // Display the correct fragments.
495 if (twoPanedMode) { // The device in in two-paned mode.
496 // Get a cursor with the current contents of the domains database.
497 Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
499 // Setup the domains cursor adapter.
500 CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(getApplicationContext(), undoDeleteDomainsCursor, false) {
502 public View newView(Context context, Cursor cursor, ViewGroup parent) {
503 // Inflate the individual item layout.
504 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
508 public void bindView(View view, Context context, Cursor cursor) {
509 /// Get the domain name string.
510 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
512 // Get a handle for the domain name text view.
513 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
515 // Display the domain name.
516 domainNameTextView.setText(domainNameString);
520 // Update the domains list view.
521 domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
523 // Select the previously deleted domain in the list view.
524 domainsListView.setItemChecked(deletedDomainPosition, true);
526 // Display the domain settings fragment.
527 fragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
529 // Enable the options delete menu item.
530 deleteMenuItem.setEnabled(true);
532 // Get the current theme status.
533 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
535 // Set the delete menu item icon according to the theme.
536 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
537 deleteMenuItem.setIcon(R.drawable.delete_night);
539 deleteMenuItem.setIcon(R.drawable.delete_day);
541 } else { // The device in in one-paned mode.
542 // Display the domain settings fragment.
543 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
545 // Hide the add domain floating action button.
548 // Show and enable the delete menu item.
549 deleteMenuItem.setVisible(true);
551 // Display the domain settings fragment.
552 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
554 } else { // The snackbar was dismissed without the undo button being pushed.
555 // Delete the selected domain.
556 domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
558 // Enable the delete menu item if the system was waiting for a snackbar to be dismissed.
559 if (dismissingSnackbar) {
560 // Create a `Runnable` to enable the delete menu item.
561 Runnable enableDeleteMenuItemRunnable = () -> {
562 // Enable the delete menu item according to the display mode.
563 if (twoPanedMode) { // Two-paned mode.
564 // Enable the delete menu item.
565 deleteMenuItem.setEnabled(true);
567 // Get the current theme status.
568 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
570 // Set the delete menu item icon according to the theme.
571 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
572 deleteMenuItem.setIcon(R.drawable.delete_night);
574 deleteMenuItem.setIcon(R.drawable.delete_day);
576 } else { // Single-paned mode.
577 // Show the delete menu item.
578 deleteMenuItem.setVisible(true);
581 // Reset the dismissing snackbar tracker.
582 dismissingSnackbar = false;
585 // Enable the delete menu icon after 100 milliseconds to make sure that the previous domain has been deleted from the database.
586 Handler handler = new Handler();
587 handler.postDelayed(enableDeleteMenuItemRunnable, 100);
590 // Close the activity if back was pressed.
591 if (closeActivityAfterDismissingSnackbar) {
593 NavUtils.navigateUpFromSameTask(activity);
599 // Show the Snackbar.
600 undoDeleteSnackbar.show();
604 // Consume the event.
609 protected void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
610 // Run the default commands.
611 super.onSaveInstanceState(savedInstanceState);
613 // Get a handle for the domain settings scrollview.
614 ScrollView domainSettingsScrollView = findViewById(R.id.domain_settings_scrollview);
616 // Check to see if the domain settings scrollview exists.
617 if (domainSettingsScrollView == null) { // The domain settings are not displayed.
618 // Store the domain settings status in the bundle.
619 savedInstanceState.putBoolean(DOMAIN_SETTINGS_DISPLAYED, false);
620 savedInstanceState.putInt(DOMAIN_SETTINGS_DATABASE_ID, -1);
621 savedInstanceState.putInt(DOMAIN_SETTINGS_SCROLL_Y, 0);
622 } else { // The domain settings are displayed.
623 // Save any changes that have been made to the domain settings.
624 saveDomainSettings(coordinatorLayout, resources);
626 // Get the domain settings scroll Y.
627 int domainSettingsScrollY = domainSettingsScrollView.getScrollY();
629 // Store the domain settings status in the bundle.
630 savedInstanceState.putBoolean(DOMAIN_SETTINGS_DISPLAYED, true);
631 savedInstanceState.putInt(DOMAIN_SETTINGS_DATABASE_ID, DomainSettingsFragment.databaseId);
632 savedInstanceState.putInt(DOMAIN_SETTINGS_SCROLL_Y, domainSettingsScrollY);
635 // Check to see if the domains listview exists.
636 if (domainsListView != null) {
637 // Get the domains listview position.
638 int domainsListViewPosition = domainsListView.getFirstVisiblePosition();
640 // Store the listview position in the bundle.
641 savedInstanceState.putInt(LISTVIEW_POSITION, domainsListViewPosition);
645 // Control what the navigation bar back button does.
647 public void onBackPressed() {
648 // Get a handle for the fragment manager.
649 FragmentManager fragmentManager = getSupportFragmentManager();
651 if (twoPanedMode) { // The device is in two-paned mode.
652 // Save the current domain settings if the domain settings fragment is displayed.
653 if (findViewById(R.id.domain_settings_scrollview) != null) {
654 saveDomainSettings(coordinatorLayout, resources);
657 // Dismiss the undo delete SnackBar if it is shown.
658 if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
659 // Set the close flag.
660 closeActivityAfterDismissingSnackbar = true;
662 // Dismiss the snackbar.
663 undoDeleteSnackbar.dismiss();
665 // Pass `onBackPressed()` to the system.
666 super.onBackPressed();
668 } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
669 // Save the current domain settings.
670 saveDomainSettings(coordinatorLayout, resources);
672 // Pass `onBackPressed()` to the system.
673 super.onBackPressed();
674 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and domain settings fragment is displayed.
675 // Save the current domain settings.
676 saveDomainSettings(coordinatorLayout, resources);
678 // Display the domains list fragment.
679 DomainsListFragment domainsListFragment = new DomainsListFragment();
680 fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
681 fragmentManager.executePendingTransactions();
683 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
684 populateDomainsListView(-1, domainsListViewPosition);
686 // Show the add domain floating action button.
689 // Hide the delete menu item.
690 deleteMenuItem.setVisible(false);
691 } else { // The device is in single-paned mode and the domain list fragment is displayed.
692 // Dismiss the undo delete SnackBar if it is shown.
693 if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
694 // Set the close flag.
695 closeActivityAfterDismissingSnackbar = true;
697 // Dismiss the snackbar.
698 undoDeleteSnackbar.dismiss();
700 // Pass `onBackPressed()` to the system.
701 super.onBackPressed();
707 public void onAddDomain(@NonNull DialogFragment dialogFragment) {
708 // Dismiss the undo delete snackbar if it is currently displayed.
709 if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
710 undoDeleteSnackbar.dismiss();
713 // Remove the incorrect lint warning below that the dialog might be null.
714 assert dialogFragment.getDialog() != null;
716 // Get a handle for the domain name edit text.
717 EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
719 // Get the domain name string.
720 String domainNameString = domainNameEditText.getText().toString();
722 // Create the domain and store the database ID in `currentDomainDatabaseId`.
723 currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
725 // Display the newly created domain.
726 if (twoPanedMode) { // The device in in two-paned mode.
727 populateDomainsListView(currentDomainDatabaseId, 0);
728 } else { // The device is in single-paned mode.
729 // Hide the add domain floating action button.
732 // Show and enable the delete menu item.
733 DomainsActivity.deleteMenuItem.setVisible(true);
735 // Create an arguments bundle.
736 Bundle argumentsBundle = new Bundle();
738 // Add the domain settings to the arguments bundle. The scroll Y should always be `0` on a new domain.
739 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
740 argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, 0);
742 // Instantiate a new domain settings fragment.
743 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
745 // Add the arguments bundle to the domain setting fragment.
746 domainSettingsFragment.setArguments(argumentsBundle);
748 // Display the domain settings fragment.
749 getSupportFragmentManager().beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
753 public void saveDomainSettings(View view, Resources resources) {
754 // Get handles for the domain settings.
755 EditText domainNameEditText = view.findViewById(R.id.domain_settings_name_edittext);
756 SwitchCompat javaScriptSwitch = view.findViewById(R.id.javascript_switch);
757 SwitchCompat cookiesSwitch = view.findViewById(R.id.cookies_switch);
758 SwitchCompat domStorageSwitch = view.findViewById(R.id.dom_storage_switch);
759 SwitchCompat formDataSwitch = view.findViewById(R.id.form_data_switch); // Form data can be removed once the minimum API >= 26.
760 SwitchCompat easyListSwitch = view.findViewById(R.id.easylist_switch);
761 SwitchCompat easyPrivacySwitch = view.findViewById(R.id.easyprivacy_switch);
762 SwitchCompat fanboysAnnoyanceSwitch = view.findViewById(R.id.fanboys_annoyance_list_switch);
763 SwitchCompat fanboysSocialBlockingSwitch = view.findViewById(R.id.fanboys_social_blocking_list_switch);
764 SwitchCompat ultraListSwitch = view.findViewById(R.id.ultralist_switch);
765 SwitchCompat ultraPrivacySwitch = view.findViewById(R.id.ultraprivacy_switch);
766 SwitchCompat blockAllThirdPartyRequestsSwitch = view.findViewById(R.id.block_all_third_party_requests_switch);
767 Spinner userAgentSpinner = view.findViewById(R.id.user_agent_spinner);
768 EditText customUserAgentEditText = view.findViewById(R.id.custom_user_agent_edittext);
769 Spinner fontSizeSpinner = view.findViewById(R.id.font_size_spinner);
770 EditText customFontSizeEditText = view.findViewById(R.id.custom_font_size_edittext);
771 Spinner swipeToRefreshSpinner = view.findViewById(R.id.swipe_to_refresh_spinner);
772 Spinner webViewThemeSpinner = view.findViewById(R.id.webview_theme_spinner);
773 Spinner wideViewportSpinner = view.findViewById(R.id.wide_viewport_spinner);
774 Spinner displayWebpageImagesSpinner = view.findViewById(R.id.display_webpage_images_spinner);
775 SwitchCompat pinnedSslCertificateSwitch = view.findViewById(R.id.pinned_ssl_certificate_switch);
776 RadioButton currentWebsiteCertificateRadioButton = view.findViewById(R.id.current_website_certificate_radiobutton);
777 SwitchCompat pinnedIpAddressesSwitch = view.findViewById(R.id.pinned_ip_addresses_switch);
778 RadioButton currentIpAddressesRadioButton = view.findViewById(R.id.current_ip_addresses_radiobutton);
780 // Extract the data for the domain settings.
781 String domainNameString = domainNameEditText.getText().toString();
782 boolean javaScript = javaScriptSwitch.isChecked();
783 boolean cookies = cookiesSwitch.isChecked();
784 boolean domStorage = domStorageSwitch.isChecked();
785 boolean formData = formDataSwitch.isChecked(); // Form data can be removed once the minimum API >= 26.
786 boolean easyList = easyListSwitch.isChecked();
787 boolean easyPrivacy = easyPrivacySwitch.isChecked();
788 boolean fanboysAnnoyance = fanboysAnnoyanceSwitch.isChecked();
789 boolean fanboysSocialBlocking = fanboysSocialBlockingSwitch.isChecked();
790 boolean ultraList = ultraListSwitch.isChecked();
791 boolean ultraPrivacy = ultraPrivacySwitch.isChecked();
792 boolean blockAllThirdPartyRequests = blockAllThirdPartyRequestsSwitch.isChecked();
793 int userAgentSwitchPosition = userAgentSpinner.getSelectedItemPosition();
794 int fontSizeSwitchPosition = fontSizeSpinner.getSelectedItemPosition();
795 int swipeToRefreshInt = swipeToRefreshSpinner.getSelectedItemPosition();
796 int webViewThemeInt = webViewThemeSpinner.getSelectedItemPosition();
797 int wideViewportInt = wideViewportSpinner.getSelectedItemPosition();
798 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
799 boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked();
800 boolean pinnedIpAddress = pinnedIpAddressesSwitch.isChecked();
802 // Initialize the user agent name string.
803 String userAgentName;
805 // Set the user agent name.
806 switch (userAgentSwitchPosition) {
807 case MainWebViewActivity.DOMAINS_SYSTEM_DEFAULT_USER_AGENT:
808 // Set the user agent name to be `System default user agent`.
809 userAgentName = resources.getString(R.string.system_default_user_agent);
812 case MainWebViewActivity.DOMAINS_CUSTOM_USER_AGENT:
813 // Set the user agent name to be the custom user agent.
814 userAgentName = customUserAgentEditText.getText().toString();
818 // Get the array of user agent names.
819 String[] userAgentNameArray = resources.getStringArray(R.array.user_agent_names);
821 // Set the user agent name from the array. The domain spinner has one more entry than the name array, so the position must be decremented.
822 userAgentName = userAgentNameArray[userAgentSwitchPosition - 1];
825 // Initialize the font size integer. `0` indicates the system default font size.
828 // Use a custom font size if it is selected.
829 if (fontSizeSwitchPosition == 1) { // A custom font size is specified.
830 // Get the custom font size from the edit text.
831 fontSizeInt = Integer.parseInt(customFontSizeEditText.getText().toString());
834 // Save the domain settings.
835 domainsDatabaseHelper.updateDomain(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScript, cookies, domStorage, formData, easyList, easyPrivacy,
836 fanboysAnnoyance, fanboysSocialBlocking, ultraList, ultraPrivacy, blockAllThirdPartyRequests, userAgentName, fontSizeInt, swipeToRefreshInt, webViewThemeInt, wideViewportInt,
837 displayWebpageImagesInt, pinnedSslCertificate, pinnedIpAddress);
839 // Update the pinned SSL certificate if a new one is checked.
840 if (currentWebsiteCertificateRadioButton.isChecked()) {
841 // Update the database.
842 domainsDatabaseHelper.updatePinnedSslCertificate(currentDomainDatabaseId, sslIssuedToCName, sslIssuedToOName, sslIssuedToUName, sslIssuedByCName, sslIssuedByOName, sslIssuedByUName,
843 sslStartDateLong, sslEndDateLong);
846 // Update the pinned IP addresses if new ones are checked.
847 if (currentIpAddressesRadioButton.isChecked()) {
848 // Update the database.
849 domainsDatabaseHelper.updatePinnedIpAddresses(currentDomainDatabaseId, currentIpAddresses);
853 private void populateDomainsListView(final int highlightedDomainDatabaseId, int domainsListViewPosition) {
854 // get a handle for the current `domains_listview`.
855 domainsListView = findViewById(R.id.domains_listview);
857 // Get a `Cursor` with the current contents of the domains database.
858 Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
860 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
861 CursorAdapter domainsCursorAdapter = new CursorAdapter(getApplicationContext(), domainsCursor, false) {
863 public View newView(Context context, Cursor cursor, ViewGroup parent) {
864 // Inflate the individual item layout. `false` does not attach it to the root.
865 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
869 public void bindView(View view, Context context, Cursor cursor) {
870 // Set the domain name.
871 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
872 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
873 domainNameTextView.setText(domainNameString);
877 // Update the list view.
878 domainsListView.setAdapter(domainsCursorAdapter);
880 // Restore the scroll position.
881 domainsListView.setSelection(domainsListViewPosition);
883 // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
884 if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) { // Two-paned mode is enabled and there is at least one domain.
885 // Initialize `highlightedDomainPosition`.
886 int highlightedDomainPosition = 0;
888 // Get the cursor position for the highlighted domain.
889 for (int i = 0; i < domainsCursor.getCount(); i++) {
890 // Move to position `i` in the cursor.
891 domainsCursor.moveToPosition(i);
893 // Get the database ID for this position.
894 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
896 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
897 if (highlightedDomainDatabaseId == currentDatabaseId) {
898 highlightedDomainPosition = i;
902 // Select the highlighted domain.
903 domainsListView.setItemChecked(highlightedDomainPosition, true);
905 // Get the database ID for the highlighted domain.
906 domainsCursor.moveToPosition(highlightedDomainPosition);
907 currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
909 // Create an arguments bundle.
910 Bundle argumentsBundle = new Bundle();
912 // Store the domain settings in the arguments bundle.
913 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
914 argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
916 // Instantiate a new domain settings fragment.
917 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
919 // Add the arguments bundle to the domain settings fragment.
920 domainSettingsFragment.setArguments(argumentsBundle);
922 // Display the domain settings fragment.
923 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
925 // Enable the delete options menu items.
926 deleteMenuItem.setEnabled(true);
928 // Get the current theme status.
929 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
931 // Set the delete icon according to the theme.
932 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
933 deleteMenuItem.setIcon(R.drawable.delete_night);
935 deleteMenuItem.setIcon(R.drawable.delete_day);
937 } else if (twoPanedMode) { // Two-paned mode is enabled but there are no domains.
938 // Disable the options `MenuItems`.
939 deleteMenuItem.setEnabled(false);
940 deleteMenuItem.setIcon(R.drawable.delete_disabled);
945 public void dismissSnackbar() {
946 // Dismiss the undo delete snackbar if it is shown.
947 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
948 // Dismiss the snackbar.
949 undoDeleteSnackbar.dismiss();
954 public void onDestroy() {
955 // Close the domains database helper.
956 domainsDatabaseHelper.close();
958 // Run the default commands.