2 * Copyright © 2017-2018 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.content.Context;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.net.http.SslCertificate;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.support.design.widget.FloatingActionButton;
29 import android.support.design.widget.Snackbar;
30 import android.support.v4.app.FragmentManager;
31 import android.support.v4.app.NavUtils;
32 import android.support.v7.app.ActionBar;
33 import android.support.v7.app.AppCompatActivity;
34 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
35 import android.support.v7.app.AppCompatDialogFragment;
36 import android.support.v7.widget.Toolbar;
37 import android.view.Menu;
38 import android.view.MenuItem;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.widget.CursorAdapter;
42 import android.widget.EditText;
43 import android.widget.ListView;
44 import android.widget.RadioButton;
45 import android.widget.Spinner;
46 import android.widget.Switch;
47 import android.widget.TextView;
49 import com.stoutner.privacybrowser.R;
50 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
51 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
52 import com.stoutner.privacybrowser.fragments.DomainsListFragment;
53 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
55 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
56 // `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`.
57 public static boolean twoPanedMode;
59 // `databaseId` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `saveDomainSettings()` and `populateDomainsListView()`.
60 public static int currentDomainDatabaseId;
62 // `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
63 public static MenuItem deleteMenuItem;
65 // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()` and `onBackPressed()`.
66 public static Snackbar undoDeleteSnackbar;
68 // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`.
69 public static boolean dismissingSnackbar;
71 // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
72 private Context context;
74 // `supportFragmentManager` is used in `onCreate()` and `onCreateOptionsMenu()`.
75 private FragmentManager supportFragmentManager;
77 // `domainsDatabaseHelper` is used in `onCreate()` and `saveDomainSettings()`.
78 private static DomainsDatabaseHelper domainsDatabaseHelper;
80 // `domainsListView` is used in `onCreate()` and `populateDomainsList()`.
81 private ListView domainsListView;
83 // `addDomainFAB` is used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
84 private FloatingActionButton addDomainFAB;
86 // `deletedDomainPosition` is used in an inner and outer class in `onOptionsItemSelected()`.
87 private int deletedDomainPosition;
89 // `restartAfterRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
90 private boolean restartAfterRotate;
92 // `domainSettingsDisplayedBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
93 private boolean domainSettingsDisplayedBeforeRotate;
95 // `domainSettingsDatabaseIdBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
96 private int domainSettingsDatabaseIdBeforeRotate;
98 // `goDirectlyToDatabaseId` is used in `onCreate()` and `onCreateOptionsMenu()`.
99 int goDirectlyToDatabaseId;
101 // `closeOnBack` is used in `onCreate()`, `onOptionsItemSelected()` and `onBackPressed()`.
105 protected void onCreate(Bundle savedInstanceState) {
106 // Set the activity theme.
107 if (MainWebViewActivity.darkTheme) {
108 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
110 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
113 // Run the default commands.
114 super.onCreate(savedInstanceState);
116 // Extract the values from `savedInstanceState` if it is not `null`.
117 if (savedInstanceState != null) {
118 restartAfterRotate = true;
119 domainSettingsDisplayedBeforeRotate = savedInstanceState.getBoolean("domainSettingsDisplayed");
120 domainSettingsDatabaseIdBeforeRotate = savedInstanceState.getInt("domainSettingsDatabaseId");
123 // Get the launching intent
124 Intent intent = getIntent();
126 // Extract the domain to load if there is one. `-1` is the default value.
127 goDirectlyToDatabaseId = intent.getIntExtra("loadDomain", -1);
129 // Get the status of close-on-back, which is true when the domains activity is called from the options menu.
130 closeOnBack = intent.getBooleanExtra("closeOnBack", false);
132 // Set the content view.
133 setContentView(R.layout.domains_coordinatorlayout);
135 // Get a handle for the context.
138 // Get a handle for the fragment manager.
139 supportFragmentManager = getSupportFragmentManager();
141 // `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
142 final Toolbar domainsAppBar = findViewById(R.id.domains_toolbar);
143 setSupportActionBar(domainsAppBar);
145 // Display the home arrow on `SupportActionBar`.
146 ActionBar appBar = getSupportActionBar();
147 assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
148 appBar.setDisplayHomeAsUpEnabled(true);
150 // Initialize the database handler. The two `nulls` do not specify the database name or a `CursorFactory`. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
151 domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
153 // Determine if we are in two pane mode. `domain_settings_fragment_container` does not exist on devices with a width less than 900dp.
154 twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null);
156 // Configure `addDomainFAB`.
157 addDomainFAB = findViewById(R.id.add_domain_fab);
158 addDomainFAB.setOnClickListener((View view) -> {
159 // Show the add domain `AlertDialog`.
160 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
161 addDomainDialog.show(supportFragmentManager, getResources().getString(R.string.add_domain));
166 public boolean onCreateOptionsMenu(Menu menu) {
168 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
170 // Store `deleteMenuItem` for future use.
171 deleteMenuItem = menu.findItem(R.id.delete_domain);
173 // Only display `deleteMenuItem` (initially) in two-paned mode.
174 deleteMenuItem.setVisible(twoPanedMode);
176 // Display the fragments. This must be done from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs `deleteMenuItem` to be inflated.
177 if (restartAfterRotate && domainSettingsDisplayedBeforeRotate) { // The device was rotated and domain settings were displayed previously.
178 if (twoPanedMode) { // The device is in two-paned mode.
179 // Reset `restartAfterRotate`.
180 restartAfterRotate = false;
182 // Display `DomainsListFragment`.
183 DomainsListFragment domainsListFragment = new DomainsListFragment();
184 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
185 supportFragmentManager.executePendingTransactions();
187 // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
188 populateDomainsListView(domainSettingsDatabaseIdBeforeRotate);
189 } else { // The device is in single-paned mode.
190 // Reset `restartAfterRotate`.
191 restartAfterRotate = false;
193 // Store the current domain database ID.
194 currentDomainDatabaseId = domainSettingsDatabaseIdBeforeRotate;
196 // Add `currentDomainDatabaseId` to `argumentsBundle`.
197 Bundle argumentsBundle = new Bundle();
198 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
200 // Add `argumentsBundle` to `domainSettingsFragment`.
201 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
202 domainSettingsFragment.setArguments(argumentsBundle);
204 // Show `deleteMenuItem`.
205 deleteMenuItem.setVisible(true);
207 // Hide `add_domain_fab`.
208 addDomainFAB.setVisibility(View.GONE);
210 // Display `domainSettingsFragment`.
211 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
213 } else { // The device was not rotated or, if it was, domain settings were not displayed previously.
214 if (goDirectlyToDatabaseId >=0) { // Load the indicated domain settings.
215 // Store the current domain database ID.
216 currentDomainDatabaseId = goDirectlyToDatabaseId;
218 if (twoPanedMode) { // The device is in two-paned mode.
219 // Display `DomainsListFragment`.
220 DomainsListFragment domainsListFragment = new DomainsListFragment();
221 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
222 supportFragmentManager.executePendingTransactions();
224 // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
225 populateDomainsListView(goDirectlyToDatabaseId);
226 } else { // The device is in single-paned mode.
227 // Add the domain ID to be loaded to `argumentsBundle`.
228 Bundle argumentsBundle = new Bundle();
229 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, goDirectlyToDatabaseId);
231 // Add `argumentsBundle` to `domainSettingsFragment`.
232 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
233 domainSettingsFragment.setArguments(argumentsBundle);
235 // Show `deleteMenuItem`.
236 deleteMenuItem.setVisible(true);
238 // Hide `add_domain_fab`.
239 addDomainFAB.setVisibility(View.GONE);
241 // Display `domainSettingsFragment`.
242 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
244 } else { // Highlight the first domain.
245 // Display `DomainsListFragment`.
246 DomainsListFragment domainsListFragment = new DomainsListFragment();
247 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
248 supportFragmentManager.executePendingTransactions();
250 // Populate the list of domains. `-1` highlights the first domain.
251 populateDomainsListView(-1);
260 public boolean onOptionsItemSelected(MenuItem menuItem) {
261 // Get the ID of the `MenuItem` that was selected.
262 int menuItemID = menuItem.getItemId();
264 switch (menuItemID) {
265 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
266 if (twoPanedMode) { // The device is in two-paned mode.
267 // Save the current domain settings if the domain settings fragment is displayed.
268 if (findViewById(R.id.domain_settings_scrollview) != null) {
269 saveDomainSettings();
272 // Dismiss the undo delete `SnackBar` if it is shown.
273 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
274 undoDeleteSnackbar.dismiss();
276 // Create a `Runnable` to return to the main activity.
277 Runnable navigateHomeRunnable = () -> {
279 NavUtils.navigateUpFromSameTask(this);
282 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
283 Handler handler = new Handler();
284 handler.postDelayed(navigateHomeRunnable, 300);
287 NavUtils.navigateUpFromSameTask(this);
289 } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
290 // Save the current domain settings.
291 saveDomainSettings();
294 NavUtils.navigateUpFromSameTask(this);
295 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and the domain settings fragment is displayed.
296 // Save the current domain settings.
297 saveDomainSettings();
299 // Display the domains list fragment.
300 DomainsListFragment domainsListFragment = new DomainsListFragment();
301 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
302 supportFragmentManager.executePendingTransactions();
304 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
305 populateDomainsListView(-1);
307 // Display the add domain FAB.
308 addDomainFAB.setVisibility(View.VISIBLE);
310 // Hide the delete menu item.
311 deleteMenuItem.setVisible(false);
312 } else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
313 // Dismiss the undo delete `SnackBar` if it is shown.
314 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
315 undoDeleteSnackbar.dismiss();
317 // Create a `Runnable` to return to the main activity.
318 Runnable navigateHomeRunnable = () -> {
320 NavUtils.navigateUpFromSameTask(this);
323 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
324 Handler handler = new Handler();
325 handler.postDelayed(navigateHomeRunnable, 300);
328 NavUtils.navigateUpFromSameTask(this);
333 case R.id.delete_domain:
334 // Reset close-on-back, which otherwise can cause errors if the system attempts to save settings for a domain that no longer exists.
337 // Store a copy of `currentDomainDatabaseId` because it could change while the `Snackbar` is displayed.
338 final int databaseIdToDelete = currentDomainDatabaseId;
340 // Update the fragments and menu items.
341 if (twoPanedMode) { // Two-paned mode.
342 // Store the deleted domain position, which is needed if `Undo` is selected in the `Snackbar`.
343 deletedDomainPosition = domainsListView.getCheckedItemPosition();
345 // Disable the options `MenuItems`.
346 deleteMenuItem.setEnabled(false);
347 deleteMenuItem.setIcon(R.drawable.delete_blue);
349 // Remove the domain settings fragment.
350 supportFragmentManager.beginTransaction().remove(supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)).commit();
351 } else { // Single-paned mode.
352 // Display `DomainsListFragment`.
353 DomainsListFragment domainsListFragment = new DomainsListFragment();
354 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
355 supportFragmentManager.executePendingTransactions();
357 // Display `addDomainFAB`.
358 addDomainFAB.setVisibility(View.VISIBLE);
360 // Hide `deleteMenuItem`.
361 deleteMenuItem.setVisible(false);
364 // Get a `Cursor` that does not show the domain to be deleted.
365 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
367 // Setup `domainsPendingDeleteCursorAdapter` with `this` context. `false` disables `autoRequery`.
368 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
370 public View newView(Context context, Cursor cursor, ViewGroup parent) {
371 // Inflate the individual item layout. `false` does not attach it to the root.
372 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
376 public void bindView(View view, Context context, Cursor cursor) {
377 // Set the domain name.
378 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
379 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
380 domainNameTextView.setText(domainNameString);
384 // Update the handle for the current `domains_listview`.
385 domainsListView = findViewById(R.id.domains_listview);
387 // Update the `ListView`.
388 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
390 // Display a `Snackbar`.
391 undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
392 .setAction(R.string.undo, (View v) -> {
393 // Do nothing because everything will be handled by `onDismissed()` below.
395 .addCallback(new Snackbar.Callback() {
397 public void onDismissed(Snackbar snackbar, int event) {
399 // The user pushed the `Undo` button.
400 case Snackbar.Callback.DISMISS_EVENT_ACTION:
401 // Store `databaseId` in `argumentsBundle`.
402 Bundle argumentsBundle = new Bundle();
403 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
405 // Add `argumentsBundle` to `domainSettingsFragment`.
406 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
407 domainSettingsFragment.setArguments(argumentsBundle);
409 // Display the correct fragments.
410 if (twoPanedMode) { // The device in in two-paned mode.
411 // Get a `Cursor` with the current contents of the domains database.
412 Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
414 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
415 CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
417 public View newView(Context context, Cursor cursor, ViewGroup parent) {
418 // Inflate the individual item layout. `false` does not attach it to the root.
419 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
423 public void bindView(View view, Context context, Cursor cursor) {
424 // Set the domain name.
425 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
426 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
427 domainNameTextView.setText(domainNameString);
431 // Update the `ListView`.
432 domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
433 // Select the previously deleted domain in `domainsListView`.
434 domainsListView.setItemChecked(deletedDomainPosition, true);
436 // Display `domainSettingsFragment`.
437 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
439 // Enable the options `MenuItems`.
440 deleteMenuItem.setEnabled(true);
441 deleteMenuItem.setIcon(R.drawable.delete_light);
442 } else { // The device in in one-paned mode.
443 // Display `domainSettingsFragment`.
444 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
446 // Hide `add_domain_fab`.
447 FloatingActionButton addDomainFAB = findViewById(R.id.add_domain_fab);
448 addDomainFAB.setVisibility(View.GONE);
450 // Show and enable `deleteMenuItem`.
451 deleteMenuItem.setVisible(true);
453 // Display `domainSettingsFragment`.
454 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
458 // The `Snackbar` was dismissed without the `Undo` button being pushed.
460 // Delete the selected domain.
461 domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
463 // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
464 if (dismissingSnackbar) {
465 // Create a `Runnable` to enable the delete menu item.
466 Runnable enableDeleteMenuItemRunnable = () -> {
467 // Enable `deleteMenuItem` according to the display mode.
468 if (twoPanedMode) { // Two-paned mode.
469 // Enable `deleteMenuItem`.
470 deleteMenuItem.setEnabled(true);
472 // Set the delete icon according to the theme.
473 if (MainWebViewActivity.darkTheme) {
474 deleteMenuItem.setIcon(R.drawable.delete_dark);
476 deleteMenuItem.setIcon(R.drawable.delete_light);
478 } else { // Single-paned mode.
479 // Show `deleteMenuItem`.
480 deleteMenuItem.setVisible(true);
483 // Reset `dismissingSnackbar`.
484 dismissingSnackbar = false;
487 // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
488 Handler handler = new Handler();
489 handler.postDelayed(enableDeleteMenuItemRunnable, 100);
495 undoDeleteSnackbar.show();
499 // Consume the event.
504 protected void onSaveInstanceState(Bundle outState) {
505 // Store the current `DomainSettingsFragment` state in `outState`.
506 if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed.
507 // Save any changes that have been made to the domain settings.
508 saveDomainSettings();
510 // Store `DomainSettingsDisplayed`.
511 outState.putBoolean("domainSettingsDisplayed", true);
512 outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
513 } else { // `DomainSettingsFragment` is not displayed.
514 outState.putBoolean("domainSettingsDisplayed", false);
515 outState.putInt("domainSettingsDatabaseId", -1);
518 super.onSaveInstanceState(outState);
521 // Control what the navigation bar back button does.
523 public void onBackPressed() {
524 if (twoPanedMode) { // The device is in two-paned mode.
525 // Save the current domain settings if the domain settings fragment is displayed.
526 if (findViewById(R.id.domain_settings_scrollview) != null) {
527 saveDomainSettings();
530 // Dismiss the undo delete SnackBar if it is shown.
531 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
532 undoDeleteSnackbar.dismiss();
534 // Create a runnable to return to the main activity.
535 Runnable navigateHomeRunnable = super::onBackPressed;
537 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
538 Handler handler = new Handler();
539 handler.postDelayed(navigateHomeRunnable, 300);
541 // Pass `onBackPressed()` to the system.
542 super.onBackPressed();
544 } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
545 // Save the current domain settings.
546 saveDomainSettings();
548 // Pass `onBackPressed()` to the system.
549 super.onBackPressed();
550 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and domain settings fragment is displayed.
551 // Save the current domain settings.
552 saveDomainSettings();
554 // Display the domains list fragment.
555 DomainsListFragment domainsListFragment = new DomainsListFragment();
556 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
557 supportFragmentManager.executePendingTransactions();
559 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
560 populateDomainsListView(-1);
562 // Display the add domain FAB.
563 addDomainFAB.setVisibility(View.VISIBLE);
565 // Hide the delete menu item.
566 deleteMenuItem.setVisible(false);
567 } else { // The device is in single-paned mode and the domain list fragment is displayed.
568 // Dismiss the undo delete SnackBar if it is shown.
569 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
570 undoDeleteSnackbar.dismiss();
572 // Create a runnable to return to the main activity.
573 Runnable navigateHomeRunnable = super::onBackPressed;
575 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
576 Handler handler = new Handler();
577 handler.postDelayed(navigateHomeRunnable, 300);
579 // Pass `onBackPressed()` to the system.
580 super.onBackPressed();
586 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
587 // Dismiss the undo delete snackbar if it is currently displayed.
588 if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
589 undoDeleteSnackbar.dismiss();
592 // Get the new domain name String from the dialog fragment.
593 EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
594 String domainNameString = domainNameEditText.getText().toString();
596 // Create the domain and store the database ID in `currentDomainDatabaseId`.
597 currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
599 // Display the newly created domain.
600 if (twoPanedMode) { // The device in in two-paned mode.
601 populateDomainsListView(currentDomainDatabaseId);
602 } else { // The device is in single-paned mode.
603 // Hide `add_domain_fab`.
604 addDomainFAB.setVisibility(View.GONE);
606 // Show and enable `deleteMenuItem`.
607 DomainsActivity.deleteMenuItem.setVisible(true);
609 // Add `currentDomainDatabaseId` to `argumentsBundle`.
610 Bundle argumentsBundle = new Bundle();
611 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
613 // Add `argumentsBundle` to `domainSettingsFragment`.
614 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
615 domainSettingsFragment.setArguments(argumentsBundle);
617 // Display `domainSettingsFragment`.
618 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
622 private void saveDomainSettings() {
623 // Get handles for the domain settings.
624 EditText domainNameEditText = findViewById(R.id.domain_settings_name_edittext);
625 Switch javaScriptSwitch = findViewById(R.id.domain_settings_javascript_switch);
626 Switch firstPartyCookiesSwitch = findViewById(R.id.domain_settings_first_party_cookies_switch);
627 Switch thirdPartyCookiesSwitch = findViewById(R.id.domain_settings_third_party_cookies_switch);
628 Switch domStorageSwitch = findViewById(R.id.domain_settings_dom_storage_switch);
629 Switch formDataSwitch = findViewById(R.id.domain_settings_form_data_switch);
630 Switch easyListSwitch = findViewById(R.id.domain_settings_easylist_switch);
631 Switch easyPrivacySwitch = findViewById(R.id.domain_settings_easyprivacy_switch);
632 Switch fanboysAnnoyanceSwitch = findViewById(R.id.domain_settings_fanboys_annoyance_list_switch);
633 Switch fanboysSocialBlockingSwitch = findViewById(R.id.domain_settings_fanboys_social_blocking_list_switch);
634 Spinner userAgentSpinner = findViewById(R.id.domain_settings_user_agent_spinner);
635 EditText customUserAgentEditText = findViewById(R.id.domain_settings_custom_user_agent_edittext);
636 Spinner fontSizeSpinner = findViewById(R.id.domain_settings_font_size_spinner);
637 Spinner displayWebpageImagesSpinner = findViewById(R.id.domain_settings_display_webpage_images_spinner);
638 Spinner nightModeSpinner = findViewById(R.id.domain_settings_night_mode_spinner);
639 Switch pinnedSslCertificateSwitch = findViewById(R.id.domain_settings_pinned_ssl_certificate_switch);
640 RadioButton savedSslCertificateRadioButton = findViewById(R.id.saved_ssl_certificate_radiobutton);
641 RadioButton currentWebsiteCertificateRadioButton = findViewById(R.id.current_website_certificate_radiobutton);
643 // Extract the data for the domain settings.
644 String domainNameString = domainNameEditText.getText().toString();
645 boolean javaScriptEnabled = javaScriptSwitch.isChecked();
646 boolean firstPartyCookiesEnabled = firstPartyCookiesSwitch.isChecked();
647 boolean thirdPartyCookiesEnabled = thirdPartyCookiesSwitch.isChecked();
648 boolean domStorageEnabled = domStorageSwitch.isChecked();
649 boolean formDataEnabled = formDataSwitch.isChecked();
650 boolean easyListEnabled = easyListSwitch.isChecked();
651 boolean easyPrivacyEnabled = easyPrivacySwitch.isChecked();
652 boolean fanboysAnnoyanceEnabled = fanboysAnnoyanceSwitch.isChecked();
653 boolean fanboysSocialBlockingEnabled = fanboysSocialBlockingSwitch.isChecked();
654 int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
655 int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
656 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
657 int nightModeInt = nightModeSpinner.getSelectedItemPosition();
658 boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked();
660 // Get the data for the `Spinners` from the entry values string arrays.
661 String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
662 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
664 // Check to see if we are using a custom user agent.
665 if (userAgentString.equals("Custom user agent")) {
666 // Set `userAgentString` to the custom user agent string.
667 userAgentString = customUserAgentEditText.getText().toString();
670 // Save the domain settings.
671 if (savedSslCertificateRadioButton.isChecked()) { // The current certificate is being used.
672 // Update the database except for the certificate.
673 domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled,
674 domStorageEnabled, formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt,
675 nightModeInt, pinnedSslCertificate);
676 } else if (currentWebsiteCertificateRadioButton.isChecked()) { // The certificate is being updated with the current website certificate.
677 // Get the current website SSL certificate.
678 SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate;
680 // Store the values from the SSL certificate.
681 String issuedToCommonName = currentWebsiteSslCertificate.getIssuedTo().getCName();
682 String issuedToOrganization = currentWebsiteSslCertificate.getIssuedTo().getOName();
683 String issuedToOrganizationalUnit = currentWebsiteSslCertificate.getIssuedTo().getUName();
684 String issuedByCommonName = currentWebsiteSslCertificate.getIssuedBy().getCName();
685 String issuedByOrganization = currentWebsiteSslCertificate.getIssuedBy().getOName();
686 String issuedByOrganizationalUnit = currentWebsiteSslCertificate.getIssuedBy().getUName();
687 long startDateLong = currentWebsiteSslCertificate.getValidNotBeforeDate().getTime();
688 long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime();
690 // Update the database.
691 domainsDatabaseHelper.updateDomainWithCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabled,
692 formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt,
693 pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization, issuedByOrganizationalUnit, startDateLong, endDateLong);
695 } else { // No certificate is selected.
696 // Update the database, with PINNED_SSL_CERTIFICATE set to false.
697 domainsDatabaseHelper.updateDomainExceptCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabled,
698 formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt,
703 private void populateDomainsListView(final int highlightedDomainDatabaseId) {
704 // get a handle for the current `domains_listview`.
705 domainsListView = findViewById(R.id.domains_listview);
707 // Get a `Cursor` with the current contents of the domains database.
708 Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
710 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
711 CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
713 public View newView(Context context, Cursor cursor, ViewGroup parent) {
714 // Inflate the individual item layout. `false` does not attach it to the root.
715 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
719 public void bindView(View view, Context context, Cursor cursor) {
720 // Set the domain name.
721 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
722 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
723 domainNameTextView.setText(domainNameString);
727 // Update the `ListView`.
728 domainsListView.setAdapter(domainsCursorAdapter);
730 // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
731 if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) { // Two-paned mode is enabled and there is at least one domain.
732 // Initialize `highlightedDomainPosition`.
733 int highlightedDomainPosition = 0;
735 // Get the cursor position for the highlighted domain.
736 for (int i = 0; i < domainsCursor.getCount(); i++) {
737 // Move to position `i` in the cursor.
738 domainsCursor.moveToPosition(i);
740 // Get the database ID for this position.
741 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
743 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
744 if (highlightedDomainDatabaseId == currentDatabaseId) {
745 highlightedDomainPosition = i;
749 // Select the highlighted domain.
750 domainsListView.setItemChecked(highlightedDomainPosition, true);
752 // Get the `databaseId` for the highlighted domain.
753 domainsCursor.moveToPosition(highlightedDomainPosition);
754 currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
756 // Store `databaseId` in `argumentsBundle`.
757 Bundle argumentsBundle = new Bundle();
758 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
760 // Add `argumentsBundle` to `domainSettingsFragment`.
761 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
762 domainSettingsFragment.setArguments(argumentsBundle);
764 // Display `domainSettingsFragment`.
765 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
767 // Enable the options `MenuItems`.
768 deleteMenuItem.setEnabled(true);
770 // Set the delete icon according to the theme.
771 if (MainWebViewActivity.darkTheme) {
772 deleteMenuItem.setIcon(R.drawable.delete_dark);
774 deleteMenuItem.setIcon(R.drawable.delete_light);
776 } else if (twoPanedMode) { // Two-paned mode is enabled but there are no domains.
777 // Disable the options `MenuItems`.
778 deleteMenuItem.setEnabled(false);
779 deleteMenuItem.setIcon(R.drawable.delete_blue);