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.content.res.Resources;
25 import android.database.Cursor;
26 import android.net.http.SslCertificate;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.support.design.widget.FloatingActionButton;
30 import android.support.design.widget.Snackbar;
31 import android.support.v4.app.FragmentManager;
32 import android.support.v4.app.NavUtils;
33 import android.support.v7.app.ActionBar;
34 import android.support.v7.app.AppCompatActivity;
35 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
36 import android.support.v7.app.AppCompatDialogFragment;
37 import android.support.v7.widget.Toolbar;
38 import android.view.Menu;
39 import android.view.MenuItem;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.view.WindowManager;
43 import android.widget.CursorAdapter;
44 import android.widget.EditText;
45 import android.widget.ListView;
46 import android.widget.RadioButton;
47 import android.widget.Spinner;
48 import android.widget.Switch;
49 import android.widget.TextView;
51 import com.stoutner.privacybrowser.R;
52 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
53 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
54 import com.stoutner.privacybrowser.fragments.DomainsListFragment;
55 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
57 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
58 // `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`.
59 public static boolean twoPanedMode;
61 // `databaseId` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `saveDomainSettings()` and `populateDomainsListView()`.
62 public static int currentDomainDatabaseId;
64 // `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
65 public static MenuItem deleteMenuItem;
67 // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()` and `onBackPressed()`.
68 public static Snackbar undoDeleteSnackbar;
70 // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`.
71 public static boolean dismissingSnackbar;
73 // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
74 private Context context;
76 // `supportFragmentManager` is used in `onCreate()` and `onCreateOptionsMenu()`.
77 private FragmentManager supportFragmentManager;
79 // `domainsDatabaseHelper` is used in `onCreate()` and `saveDomainSettings()`.
80 private static DomainsDatabaseHelper domainsDatabaseHelper;
82 // `domainsListView` is used in `onCreate()` and `populateDomainsList()`.
83 private ListView domainsListView;
85 // `addDomainFAB` is used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
86 private FloatingActionButton addDomainFAB;
88 // `deletedDomainPosition` is used in an inner and outer class in `onOptionsItemSelected()`.
89 private int deletedDomainPosition;
91 // `restartAfterRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
92 private boolean restartAfterRotate;
94 // `domainSettingsDisplayedBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
95 private boolean domainSettingsDisplayedBeforeRotate;
97 // `domainSettingsDatabaseIdBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
98 private int domainSettingsDatabaseIdBeforeRotate;
100 // `goDirectlyToDatabaseId` is used in `onCreate()` and `onCreateOptionsMenu()`.
101 private int goDirectlyToDatabaseId;
103 // `closeOnBack` is used in `onCreate()`, `onOptionsItemSelected()` and `onBackPressed()`.
104 private boolean closeOnBack;
106 // `coordinatorLayout` is use in `onCreate()`, `onOptionsItemSelected()`, and `onSaveInstanceState()`.
107 private View coordinatorLayout;
109 // `resources` is used in `onCreate()`, `onOptionsItemSelected()`, and `onSaveInstanceState()`.
110 private Resources resources;
113 protected void onCreate(Bundle savedInstanceState) {
114 // Disable screenshots if not allowed.
115 if (!MainWebViewActivity.allowScreenshots) {
116 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
119 // Set the activity theme.
120 if (MainWebViewActivity.darkTheme) {
121 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
123 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
126 // Run the default commands.
127 super.onCreate(savedInstanceState);
129 // Extract the values from `savedInstanceState` if it is not `null`.
130 if (savedInstanceState != null) {
131 restartAfterRotate = true;
132 domainSettingsDisplayedBeforeRotate = savedInstanceState.getBoolean("domainSettingsDisplayed");
133 domainSettingsDatabaseIdBeforeRotate = savedInstanceState.getInt("domainSettingsDatabaseId");
136 // Get the launching intent
137 Intent intent = getIntent();
139 // Extract the domain to load if there is one. `-1` is the default value.
140 goDirectlyToDatabaseId = intent.getIntExtra("loadDomain", -1);
142 // Get the status of close-on-back, which is true when the domains activity is called from the options menu.
143 closeOnBack = intent.getBooleanExtra("closeOnBack", false);
145 // Set the content view.
146 setContentView(R.layout.domains_coordinatorlayout);
148 // Populate the class variables.
149 coordinatorLayout = findViewById(R.id.domains_coordinatorlayout);
150 resources = getResources();
152 supportFragmentManager = getSupportFragmentManager();
154 // `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
155 final Toolbar domainsAppBar = findViewById(R.id.domains_toolbar);
156 setSupportActionBar(domainsAppBar);
158 // Display the home arrow on `SupportActionBar`.
159 ActionBar appBar = getSupportActionBar();
160 assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
161 appBar.setDisplayHomeAsUpEnabled(true);
163 // 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`.
164 domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
166 // Determine if we are in two pane mode. `domain_settings_fragment_container` does not exist on devices with a width less than 900dp.
167 twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null);
169 // Configure `addDomainFAB`.
170 addDomainFAB = findViewById(R.id.add_domain_fab);
171 addDomainFAB.setOnClickListener((View view) -> {
172 // Show the add domain `AlertDialog`.
173 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
174 addDomainDialog.show(supportFragmentManager, resources.getString(R.string.add_domain));
179 public boolean onCreateOptionsMenu(Menu menu) {
181 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
183 // Store `deleteMenuItem` for future use.
184 deleteMenuItem = menu.findItem(R.id.delete_domain);
186 // Only display `deleteMenuItem` (initially) in two-paned mode.
187 deleteMenuItem.setVisible(twoPanedMode);
189 // Display the fragments. This must be done from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs `deleteMenuItem` to be inflated.
190 if (restartAfterRotate && domainSettingsDisplayedBeforeRotate) { // The device was rotated and domain settings were displayed previously.
191 if (twoPanedMode) { // The device is in two-paned mode.
192 // Reset `restartAfterRotate`.
193 restartAfterRotate = false;
195 // Display `DomainsListFragment`.
196 DomainsListFragment domainsListFragment = new DomainsListFragment();
197 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
198 supportFragmentManager.executePendingTransactions();
200 // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
201 populateDomainsListView(domainSettingsDatabaseIdBeforeRotate);
202 } else { // The device is in single-paned mode.
203 // Reset `restartAfterRotate`.
204 restartAfterRotate = false;
206 // Store the current domain database ID.
207 currentDomainDatabaseId = domainSettingsDatabaseIdBeforeRotate;
209 // Add `currentDomainDatabaseId` to `argumentsBundle`.
210 Bundle argumentsBundle = new Bundle();
211 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
213 // Add `argumentsBundle` to `domainSettingsFragment`.
214 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
215 domainSettingsFragment.setArguments(argumentsBundle);
217 // Show `deleteMenuItem`.
218 deleteMenuItem.setVisible(true);
220 // Hide `add_domain_fab`.
221 addDomainFAB.setVisibility(View.GONE);
223 // Display `domainSettingsFragment`.
224 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
226 } else { // The device was not rotated or, if it was, domain settings were not displayed previously.
227 if (goDirectlyToDatabaseId >=0) { // Load the indicated domain settings.
228 // Store the current domain database ID.
229 currentDomainDatabaseId = goDirectlyToDatabaseId;
231 if (twoPanedMode) { // The device is in two-paned mode.
232 // Display `DomainsListFragment`.
233 DomainsListFragment domainsListFragment = new DomainsListFragment();
234 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
235 supportFragmentManager.executePendingTransactions();
237 // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
238 populateDomainsListView(goDirectlyToDatabaseId);
239 } else { // The device is in single-paned mode.
240 // Add the domain ID to be loaded to `argumentsBundle`.
241 Bundle argumentsBundle = new Bundle();
242 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, goDirectlyToDatabaseId);
244 // Add `argumentsBundle` to `domainSettingsFragment`.
245 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
246 domainSettingsFragment.setArguments(argumentsBundle);
248 // Show `deleteMenuItem`.
249 deleteMenuItem.setVisible(true);
251 // Hide `add_domain_fab`.
252 addDomainFAB.setVisibility(View.GONE);
254 // Display `domainSettingsFragment`.
255 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
257 } else { // Highlight the first domain.
258 // Display `DomainsListFragment`.
259 DomainsListFragment domainsListFragment = new DomainsListFragment();
260 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
261 supportFragmentManager.executePendingTransactions();
263 // Populate the list of domains. `-1` highlights the first domain.
264 populateDomainsListView(-1);
273 public boolean onOptionsItemSelected(MenuItem menuItem) {
274 // Get the ID of the `MenuItem` that was selected.
275 int menuItemID = menuItem.getItemId();
277 switch (menuItemID) {
278 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
279 if (twoPanedMode) { // The device is in two-paned mode.
280 // Save the current domain settings if the domain settings fragment is displayed.
281 if (findViewById(R.id.domain_settings_scrollview) != null) {
282 saveDomainSettings(coordinatorLayout, resources);
285 // Dismiss the undo delete `SnackBar` if it is shown.
286 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
287 undoDeleteSnackbar.dismiss();
289 // Create a `Runnable` to return to the main activity.
290 Runnable navigateHomeRunnable = () -> {
292 NavUtils.navigateUpFromSameTask(this);
295 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
296 Handler handler = new Handler();
297 handler.postDelayed(navigateHomeRunnable, 300);
300 NavUtils.navigateUpFromSameTask(this);
302 } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
303 // Save the current domain settings.
304 saveDomainSettings(coordinatorLayout, resources);
307 NavUtils.navigateUpFromSameTask(this);
308 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and the domain settings fragment is displayed.
309 // Save the current domain settings.
310 saveDomainSettings(coordinatorLayout, resources);
312 // Display the domains list fragment.
313 DomainsListFragment domainsListFragment = new DomainsListFragment();
314 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
315 supportFragmentManager.executePendingTransactions();
317 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
318 populateDomainsListView(-1);
320 // Display the add domain FAB.
321 addDomainFAB.setVisibility(View.VISIBLE);
323 // Hide the delete menu item.
324 deleteMenuItem.setVisible(false);
325 } else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
326 // Dismiss the undo delete `SnackBar` if it is shown.
327 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
328 undoDeleteSnackbar.dismiss();
330 // Create a `Runnable` to return to the main activity.
331 Runnable navigateHomeRunnable = () -> {
333 NavUtils.navigateUpFromSameTask(this);
336 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
337 Handler handler = new Handler();
338 handler.postDelayed(navigateHomeRunnable, 300);
341 NavUtils.navigateUpFromSameTask(this);
346 case R.id.delete_domain:
347 // Reset close-on-back, which otherwise can cause errors if the system attempts to save settings for a domain that no longer exists.
350 // Store a copy of `currentDomainDatabaseId` because it could change while the `Snackbar` is displayed.
351 final int databaseIdToDelete = currentDomainDatabaseId;
353 // Update the fragments and menu items.
354 if (twoPanedMode) { // Two-paned mode.
355 // Store the deleted domain position, which is needed if `Undo` is selected in the `Snackbar`.
356 deletedDomainPosition = domainsListView.getCheckedItemPosition();
358 // Disable the options `MenuItems`.
359 deleteMenuItem.setEnabled(false);
360 deleteMenuItem.setIcon(R.drawable.delete_blue);
362 // Remove the domain settings fragment.
363 supportFragmentManager.beginTransaction().remove(supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)).commit();
364 } else { // Single-paned mode.
365 // Display `DomainsListFragment`.
366 DomainsListFragment domainsListFragment = new DomainsListFragment();
367 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
368 supportFragmentManager.executePendingTransactions();
370 // Display `addDomainFAB`.
371 addDomainFAB.setVisibility(View.VISIBLE);
373 // Hide `deleteMenuItem`.
374 deleteMenuItem.setVisible(false);
377 // Get a `Cursor` that does not show the domain to be deleted.
378 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
380 // Setup `domainsPendingDeleteCursorAdapter` with `this` context. `false` disables `autoRequery`.
381 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
383 public View newView(Context context, Cursor cursor, ViewGroup parent) {
384 // Inflate the individual item layout. `false` does not attach it to the root.
385 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
389 public void bindView(View view, Context context, Cursor cursor) {
390 // Set the domain name.
391 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
392 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
393 domainNameTextView.setText(domainNameString);
397 // Update the handle for the current `domains_listview`.
398 domainsListView = findViewById(R.id.domains_listview);
400 // Update the `ListView`.
401 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
403 // Display a `Snackbar`.
404 undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
405 .setAction(R.string.undo, (View v) -> {
406 // Do nothing because everything will be handled by `onDismissed()` below.
408 .addCallback(new Snackbar.Callback() {
410 public void onDismissed(Snackbar snackbar, int event) {
412 // The user pushed the `Undo` button.
413 case Snackbar.Callback.DISMISS_EVENT_ACTION:
414 // Store `databaseId` in `argumentsBundle`.
415 Bundle argumentsBundle = new Bundle();
416 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
418 // Add `argumentsBundle` to `domainSettingsFragment`.
419 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
420 domainSettingsFragment.setArguments(argumentsBundle);
422 // Display the correct fragments.
423 if (twoPanedMode) { // The device in in two-paned mode.
424 // Get a `Cursor` with the current contents of the domains database.
425 Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
427 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
428 CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
430 public View newView(Context context, Cursor cursor, ViewGroup parent) {
431 // Inflate the individual item layout. `false` does not attach it to the root.
432 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
436 public void bindView(View view, Context context, Cursor cursor) {
437 // Set the domain name.
438 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
439 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
440 domainNameTextView.setText(domainNameString);
444 // Update the `ListView`.
445 domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
446 // Select the previously deleted domain in `domainsListView`.
447 domainsListView.setItemChecked(deletedDomainPosition, true);
449 // Display `domainSettingsFragment`.
450 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
452 // Enable the options `MenuItems`.
453 deleteMenuItem.setEnabled(true);
454 deleteMenuItem.setIcon(R.drawable.delete_light);
455 } else { // The device in in one-paned mode.
456 // Display `domainSettingsFragment`.
457 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
459 // Hide `add_domain_fab`.
460 FloatingActionButton addDomainFAB = findViewById(R.id.add_domain_fab);
461 addDomainFAB.setVisibility(View.GONE);
463 // Show and enable `deleteMenuItem`.
464 deleteMenuItem.setVisible(true);
466 // Display `domainSettingsFragment`.
467 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
471 // The `Snackbar` was dismissed without the `Undo` button being pushed.
473 // Delete the selected domain.
474 domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
476 // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
477 if (dismissingSnackbar) {
478 // Create a `Runnable` to enable the delete menu item.
479 Runnable enableDeleteMenuItemRunnable = () -> {
480 // Enable `deleteMenuItem` according to the display mode.
481 if (twoPanedMode) { // Two-paned mode.
482 // Enable `deleteMenuItem`.
483 deleteMenuItem.setEnabled(true);
485 // Set the delete icon according to the theme.
486 if (MainWebViewActivity.darkTheme) {
487 deleteMenuItem.setIcon(R.drawable.delete_dark);
489 deleteMenuItem.setIcon(R.drawable.delete_light);
491 } else { // Single-paned mode.
492 // Show `deleteMenuItem`.
493 deleteMenuItem.setVisible(true);
496 // Reset `dismissingSnackbar`.
497 dismissingSnackbar = false;
500 // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
501 Handler handler = new Handler();
502 handler.postDelayed(enableDeleteMenuItemRunnable, 100);
508 undoDeleteSnackbar.show();
512 // Consume the event.
517 protected void onSaveInstanceState(Bundle outState) {
518 // Store the current `DomainSettingsFragment` state in `outState`.
519 if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed.
520 // Save any changes that have been made to the domain settings.
521 saveDomainSettings(coordinatorLayout, resources);
523 // Store `DomainSettingsDisplayed`.
524 outState.putBoolean("domainSettingsDisplayed", true);
525 outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
526 } else { // `DomainSettingsFragment` is not displayed.
527 outState.putBoolean("domainSettingsDisplayed", false);
528 outState.putInt("domainSettingsDatabaseId", -1);
531 super.onSaveInstanceState(outState);
534 // Control what the navigation bar back button does.
536 public void onBackPressed() {
537 if (twoPanedMode) { // The device is in two-paned mode.
538 // Save the current domain settings if the domain settings fragment is displayed.
539 if (findViewById(R.id.domain_settings_scrollview) != null) {
540 saveDomainSettings(coordinatorLayout, resources);
543 // Dismiss the undo delete SnackBar if it is shown.
544 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
545 undoDeleteSnackbar.dismiss();
547 // Create a runnable to return to the main activity.
548 Runnable navigateHomeRunnable = super::onBackPressed;
550 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
551 Handler handler = new Handler();
552 handler.postDelayed(navigateHomeRunnable, 300);
554 // Pass `onBackPressed()` to the system.
555 super.onBackPressed();
557 } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
558 // Save the current domain settings.
559 saveDomainSettings(coordinatorLayout, resources);
561 // Pass `onBackPressed()` to the system.
562 super.onBackPressed();
563 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and domain settings fragment is displayed.
564 // Save the current domain settings.
565 saveDomainSettings(coordinatorLayout, resources);
567 // Display the domains list fragment.
568 DomainsListFragment domainsListFragment = new DomainsListFragment();
569 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
570 supportFragmentManager.executePendingTransactions();
572 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
573 populateDomainsListView(-1);
575 // Display the add domain FAB.
576 addDomainFAB.setVisibility(View.VISIBLE);
578 // Hide the delete menu item.
579 deleteMenuItem.setVisible(false);
580 } else { // The device is in single-paned mode and the domain list fragment is displayed.
581 // Dismiss the undo delete SnackBar if it is shown.
582 if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
583 undoDeleteSnackbar.dismiss();
585 // Create a runnable to return to the main activity.
586 Runnable navigateHomeRunnable = super::onBackPressed;
588 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
589 Handler handler = new Handler();
590 handler.postDelayed(navigateHomeRunnable, 300);
592 // Pass `onBackPressed()` to the system.
593 super.onBackPressed();
599 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
600 // Dismiss the undo delete snackbar if it is currently displayed.
601 if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
602 undoDeleteSnackbar.dismiss();
605 // Get the new domain name String from the dialog fragment.
606 EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
607 String domainNameString = domainNameEditText.getText().toString();
609 // Create the domain and store the database ID in `currentDomainDatabaseId`.
610 currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
612 // Display the newly created domain.
613 if (twoPanedMode) { // The device in in two-paned mode.
614 populateDomainsListView(currentDomainDatabaseId);
615 } else { // The device is in single-paned mode.
616 // Hide `add_domain_fab`.
617 addDomainFAB.setVisibility(View.GONE);
619 // Show and enable `deleteMenuItem`.
620 DomainsActivity.deleteMenuItem.setVisible(true);
622 // Add `currentDomainDatabaseId` to `argumentsBundle`.
623 Bundle argumentsBundle = new Bundle();
624 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
626 // Add `argumentsBundle` to `domainSettingsFragment`.
627 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
628 domainSettingsFragment.setArguments(argumentsBundle);
630 // Display `domainSettingsFragment`.
631 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
635 public void saveDomainSettings(View view, Resources resources) {
636 // Get handles for the domain settings.
637 EditText domainNameEditText = view.findViewById(R.id.domain_settings_name_edittext);
638 Switch javaScriptSwitch = view.findViewById(R.id.domain_settings_javascript_switch);
639 Switch firstPartyCookiesSwitch = view.findViewById(R.id.domain_settings_first_party_cookies_switch);
640 Switch thirdPartyCookiesSwitch = view.findViewById(R.id.domain_settings_third_party_cookies_switch);
641 Switch domStorageSwitch = view.findViewById(R.id.domain_settings_dom_storage_switch);
642 Switch formDataSwitch = view.findViewById(R.id.domain_settings_form_data_switch); // Form data can be removed once the minimum API >= 26.
643 Switch easyListSwitch = view.findViewById(R.id.domain_settings_easylist_switch);
644 Switch easyPrivacySwitch = view.findViewById(R.id.domain_settings_easyprivacy_switch);
645 Switch fanboysAnnoyanceSwitch = view.findViewById(R.id.domain_settings_fanboys_annoyance_list_switch);
646 Switch fanboysSocialBlockingSwitch = view.findViewById(R.id.domain_settings_fanboys_social_blocking_list_switch);
647 Switch ultraPrivacySwitch = view.findViewById(R.id.domain_settings_ultraprivacy_switch);
648 Switch blockAllThirdPartyRequestsSwitch = view.findViewById(R.id.domain_settings_block_all_third_party_requests_switch);
649 Spinner userAgentSpinner = view.findViewById(R.id.domain_settings_user_agent_spinner);
650 EditText customUserAgentEditText = view.findViewById(R.id.domain_settings_custom_user_agent_edittext);
651 Spinner fontSizeSpinner = view.findViewById(R.id.domain_settings_font_size_spinner);
652 Spinner swipeToRefreshSpinner = view.findViewById(R.id.domain_settings_swipe_to_refresh_spinner);
653 Spinner displayWebpageImagesSpinner = view.findViewById(R.id.domain_settings_display_webpage_images_spinner);
654 Spinner nightModeSpinner = view.findViewById(R.id.domain_settings_night_mode_spinner);
655 Switch pinnedSslCertificateSwitch = view.findViewById(R.id.domain_settings_pinned_ssl_certificate_switch);
656 RadioButton savedSslCertificateRadioButton = view.findViewById(R.id.saved_ssl_certificate_radiobutton);
657 RadioButton currentWebsiteCertificateRadioButton = view.findViewById(R.id.current_website_certificate_radiobutton);
659 // Extract the data for the domain settings.
660 String domainNameString = domainNameEditText.getText().toString();
661 boolean javaScriptEnabled = javaScriptSwitch.isChecked();
662 boolean firstPartyCookiesEnabled = firstPartyCookiesSwitch.isChecked();
663 boolean thirdPartyCookiesEnabled = thirdPartyCookiesSwitch.isChecked();
664 boolean domStorageEnabled = domStorageSwitch.isChecked();
665 boolean formDataEnabled = formDataSwitch.isChecked(); // Form data can be removed once the minimum API >= 26.
666 boolean easyListEnabled = easyListSwitch.isChecked();
667 boolean easyPrivacyEnabled = easyPrivacySwitch.isChecked();
668 boolean fanboysAnnoyanceEnabled = fanboysAnnoyanceSwitch.isChecked();
669 boolean fanboysSocialBlockingEnabled = fanboysSocialBlockingSwitch.isChecked();
670 boolean ultraPrivacyEnabled = ultraPrivacySwitch.isChecked();
671 boolean blockAllThirdPartyRequests = blockAllThirdPartyRequestsSwitch.isChecked();
672 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
673 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
674 int swipeToRefreshInt = swipeToRefreshSpinner.getSelectedItemPosition();
675 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
676 int nightModeInt = nightModeSpinner.getSelectedItemPosition();
677 boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked();
679 // Initialize the user agent name string.
680 String userAgentName;
682 // Set the user agent name.
683 switch (userAgentPosition) {
684 case MainWebViewActivity.DOMAINS_SYSTEM_DEFAULT_USER_AGENT:
685 // Set the user agent name to be `System default user agent`.
686 userAgentName = resources.getString(R.string.system_default_user_agent);
689 case MainWebViewActivity.DOMAINS_CUSTOM_USER_AGENT:
690 // Set the user agent name to be the custom user agent.
691 userAgentName = customUserAgentEditText.getText().toString();
695 // Get the array of user agent names.
696 String[] userAgentNameArray = resources.getStringArray(R.array.user_agent_names);
698 // 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.
699 userAgentName = userAgentNameArray[userAgentPosition - 1];
702 // Get the font size integer.
703 int fontSizeInt = Integer.parseInt(resources.getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePosition]);
705 // Save the domain settings.
706 if (savedSslCertificateRadioButton.isChecked()) { // The current certificate is being used.
707 // Update the database except for the certificate.
708 domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled,
709 domStorageEnabled, formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, ultraPrivacyEnabled, blockAllThirdPartyRequests,
710 userAgentName, fontSizeInt, swipeToRefreshInt, nightModeInt, displayWebpageImagesInt, pinnedSslCertificate);
711 } else if (currentWebsiteCertificateRadioButton.isChecked()) { // The certificate is being updated with the current website certificate.
712 // Get the current website SSL certificate.
713 SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate;
715 // Store the values from the SSL certificate.
716 String issuedToCommonName = currentWebsiteSslCertificate.getIssuedTo().getCName();
717 String issuedToOrganization = currentWebsiteSslCertificate.getIssuedTo().getOName();
718 String issuedToOrganizationalUnit = currentWebsiteSslCertificate.getIssuedTo().getUName();
719 String issuedByCommonName = currentWebsiteSslCertificate.getIssuedBy().getCName();
720 String issuedByOrganization = currentWebsiteSslCertificate.getIssuedBy().getOName();
721 String issuedByOrganizationalUnit = currentWebsiteSslCertificate.getIssuedBy().getUName();
722 long startDateLong = currentWebsiteSslCertificate.getValidNotBeforeDate().getTime();
723 long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime();
725 // Update the database.
726 domainsDatabaseHelper.updateDomainWithCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabled,
727 formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, ultraPrivacyEnabled, blockAllThirdPartyRequests, userAgentName, fontSizeInt,
728 swipeToRefreshInt, nightModeInt, displayWebpageImagesInt, pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName,
729 issuedByOrganization, issuedByOrganizationalUnit, startDateLong, endDateLong);
731 } else { // No certificate is selected.
732 // Update the database, with PINNED_SSL_CERTIFICATE set to false.
733 domainsDatabaseHelper.updateDomainExceptCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabled,
734 formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, ultraPrivacyEnabled, blockAllThirdPartyRequests, userAgentName, fontSizeInt,
735 swipeToRefreshInt, nightModeInt, displayWebpageImagesInt,false);
739 private void populateDomainsListView(final int highlightedDomainDatabaseId) {
740 // get a handle for the current `domains_listview`.
741 domainsListView = findViewById(R.id.domains_listview);
743 // Get a `Cursor` with the current contents of the domains database.
744 Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
746 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
747 CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
749 public View newView(Context context, Cursor cursor, ViewGroup parent) {
750 // Inflate the individual item layout. `false` does not attach it to the root.
751 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
755 public void bindView(View view, Context context, Cursor cursor) {
756 // Set the domain name.
757 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
758 TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
759 domainNameTextView.setText(domainNameString);
763 // Update the `ListView`.
764 domainsListView.setAdapter(domainsCursorAdapter);
766 // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
767 if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) { // Two-paned mode is enabled and there is at least one domain.
768 // Initialize `highlightedDomainPosition`.
769 int highlightedDomainPosition = 0;
771 // Get the cursor position for the highlighted domain.
772 for (int i = 0; i < domainsCursor.getCount(); i++) {
773 // Move to position `i` in the cursor.
774 domainsCursor.moveToPosition(i);
776 // Get the database ID for this position.
777 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
779 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
780 if (highlightedDomainDatabaseId == currentDatabaseId) {
781 highlightedDomainPosition = i;
785 // Select the highlighted domain.
786 domainsListView.setItemChecked(highlightedDomainPosition, true);
788 // Get the `databaseId` for the highlighted domain.
789 domainsCursor.moveToPosition(highlightedDomainPosition);
790 currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
792 // Store `databaseId` in `argumentsBundle`.
793 Bundle argumentsBundle = new Bundle();
794 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
796 // Add `argumentsBundle` to `domainSettingsFragment`.
797 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
798 domainSettingsFragment.setArguments(argumentsBundle);
800 // Display `domainSettingsFragment`.
801 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
803 // Enable the options `MenuItems`.
804 deleteMenuItem.setEnabled(true);
806 // Set the delete icon according to the theme.
807 if (MainWebViewActivity.darkTheme) {
808 deleteMenuItem.setIcon(R.drawable.delete_dark);
810 deleteMenuItem.setIcon(R.drawable.delete_light);
812 } else if (twoPanedMode) { // Two-paned mode is enabled but there are no domains.
813 // Disable the options `MenuItems`.
814 deleteMenuItem.setEnabled(false);
815 deleteMenuItem.setIcon(R.drawable.delete_blue);