]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Add a Download URL option to the context menu. https://redmine.stoutner.com/issues/269
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.java
index 3090d8aaccde5e902478bc2f4034ea99460e74b3..4016c7612dc91abcc6276ba6eb55fd56a4533a07 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2017-2018 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -98,6 +98,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
     // `goDirectlyToDatabaseId` is used in `onCreate()` and `onCreateOptionsMenu()`.
     int goDirectlyToDatabaseId;
 
+    // `closeOnBack` is used in `onCreate()`, `onOptionsItemSelected()` and `onBackPressed()`.
+    boolean closeOnBack;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // Set the activity theme.
@@ -121,7 +124,10 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         Intent intent = getIntent();
 
         // Extract the domain to load if there is one.  `-1` is the default value.
-        goDirectlyToDatabaseId = intent.getIntExtra("LoadDomain", -1);
+        goDirectlyToDatabaseId = intent.getIntExtra("loadDomain", -1);
+
+        // Get the status of close-on-back, which is true when the domains activity is called from the options menu.
+        closeOnBack = intent.getBooleanExtra("closeOnBack", false);
 
         // Set the content view.
         setContentView(R.layout.domains_coordinatorlayout);
@@ -280,11 +286,17 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                         // Go home.
                         NavUtils.navigateUpFromSameTask(this);
                     }
-                } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
+                } else if (closeOnBack) {  // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
                     // Save the current domain settings.
                     saveDomainSettings();
 
-                    // Display `DomainsListFragment`.
+                    // Go home.
+                    NavUtils.navigateUpFromSameTask(this);
+                } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and the domain settings fragment is displayed.
+                    // Save the current domain settings.
+                    saveDomainSettings();
+
+                    // Display the domains list fragment.
                     DomainsListFragment domainsListFragment = new DomainsListFragment();
                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
                     supportFragmentManager.executePendingTransactions();
@@ -292,10 +304,10 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                     // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
                     populateDomainsListView(-1);
 
-                    // Display `addDomainFAB`.
+                    // Display the add domain FAB.
                     addDomainFAB.setVisibility(View.VISIBLE);
 
-                    // Hide `deleteMenuItem`.
+                    // Hide the delete menu item.
                     deleteMenuItem.setVisible(false);
                 } else {  // The device is in single-paned mode and `DomainsListFragment` is displayed.
                     // Dismiss the undo delete `SnackBar` if it is shown.
@@ -319,6 +331,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 break;
 
             case R.id.delete_domain:
+                // Reset close-on-back, which otherwise can cause errors if the system attempts to save settings for a domain that no longer exists.
+                closeOnBack = false;
+
                 // Store a copy of `currentDomainDatabaseId` because it could change while the `Snackbar` is displayed.
                 final int databaseIdToDelete = currentDomainDatabaseId;
 
@@ -512,15 +527,12 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 saveDomainSettings();
             }
 
-            // Dismiss the undo delete `SnackBar` if it is shown.
+            // Dismiss the undo delete SnackBar if it is shown.
             if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
                 undoDeleteSnackbar.dismiss();
 
-                // Create a `Runnable` to return to the main activity.
-                Runnable navigateHomeRunnable = () -> {
-                    // Pass `onBackPressed()` to the system.
-                    super.onBackPressed();
-                };
+                // Create a runnable to return to the main activity.
+                Runnable navigateHomeRunnable = super::onBackPressed;
 
                 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
                 Handler handler = new Handler();
@@ -529,11 +541,17 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 // Pass `onBackPressed()` to the system.
                 super.onBackPressed();
             }
-        } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
+        } else if (closeOnBack) {  // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
             // Save the current domain settings.
             saveDomainSettings();
 
-            // Display `DomainsListFragment`.
+            // Pass `onBackPressed()` to the system.
+            super.onBackPressed();
+        } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and domain settings fragment is displayed.
+            // Save the current domain settings.
+            saveDomainSettings();
+
+            // Display the domains list fragment.
             DomainsListFragment domainsListFragment = new DomainsListFragment();
             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
             supportFragmentManager.executePendingTransactions();
@@ -541,21 +559,18 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
             populateDomainsListView(-1);
 
-            // Display `addDomainFAB`.
+            // Display the add domain FAB.
             addDomainFAB.setVisibility(View.VISIBLE);
 
-            // Hide `deleteMenuItem`.
+            // Hide the delete menu item.
             deleteMenuItem.setVisible(false);
-        } else {  // The device is in single-paned mode and `DomainsListFragment` is displayed.
-            // Dismiss the undo delete `SnackBar` if it is shown.
+        } else {  // The device is in single-paned mode and the domain list fragment is displayed.
+            // Dismiss the undo delete SnackBar if it is shown.
             if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
                 undoDeleteSnackbar.dismiss();
 
-                // Create a `Runnable` to return to the main activity.
-                Runnable navigateHomeRunnable = () -> {
-                    // Pass `onBackPressed()` to the system.
-                    super.onBackPressed();
-                };
+                // Create a runnable to return to the main activity.
+                Runnable navigateHomeRunnable = super::onBackPressed;
 
                 // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
                 Handler handler = new Handler();
@@ -569,12 +584,12 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
 
     @Override
     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
-        // Dismiss `undoDeleteSnackbar` if it is currently displayed.
+        // Dismiss the undo delete snackbar if it is currently displayed.
         if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
             undoDeleteSnackbar.dismiss();
         }
 
-        // Get the new domain name `String` from `dialogFragment`.
+        // Get the new domain name String from the dialog fragment.
         EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
         String domainNameString = domainNameEditText.getText().toString();
 
@@ -607,11 +622,15 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
     private void saveDomainSettings() {
         // Get handles for the domain settings.
         EditText domainNameEditText = findViewById(R.id.domain_settings_name_edittext);
-        Switch javaScriptEnabledSwitch = findViewById(R.id.domain_settings_javascript_switch);
-        Switch firstPartyCookiesEnabledSwitch = findViewById(R.id.domain_settings_first_party_cookies_switch);
-        Switch thirdPartyCookiesEnabledSwitch = findViewById(R.id.domain_settings_third_party_cookies_switch);
-        Switch domStorageEnabledSwitch = findViewById(R.id.domain_settings_dom_storage_switch);
-        Switch formDataEnabledSwitch = findViewById(R.id.domain_settings_form_data_switch);
+        Switch javaScriptSwitch = findViewById(R.id.domain_settings_javascript_switch);
+        Switch firstPartyCookiesSwitch = findViewById(R.id.domain_settings_first_party_cookies_switch);
+        Switch thirdPartyCookiesSwitch = findViewById(R.id.domain_settings_third_party_cookies_switch);
+        Switch domStorageSwitch = findViewById(R.id.domain_settings_dom_storage_switch);
+        Switch formDataSwitch = findViewById(R.id.domain_settings_form_data_switch);
+        Switch easyListSwitch = findViewById(R.id.domain_settings_easylist_switch);
+        Switch easyPrivacySwitch = findViewById(R.id.domain_settings_easyprivacy_switch);
+        Switch fanboysAnnoyanceSwitch = findViewById(R.id.domain_settings_fanboys_annoyance_list_switch);
+        Switch fanboysSocialBlockingSwitch = findViewById(R.id.domain_settings_fanboys_social_blocking_list_switch);
         Spinner userAgentSpinner = findViewById(R.id.domain_settings_user_agent_spinner);
         EditText customUserAgentEditText = findViewById(R.id.domain_settings_custom_user_agent_edittext);
         Spinner fontSizeSpinner = findViewById(R.id.domain_settings_font_size_spinner);
@@ -623,11 +642,15 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
 
         // Extract the data for the domain settings.
         String domainNameString = domainNameEditText.getText().toString();
-        boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
-        boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
-        boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
-        boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
-        boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
+        boolean javaScriptEnabled = javaScriptSwitch.isChecked();
+        boolean firstPartyCookiesEnabled = firstPartyCookiesSwitch.isChecked();
+        boolean thirdPartyCookiesEnabled = thirdPartyCookiesSwitch.isChecked();
+        boolean domStorageEnabled  = domStorageSwitch.isChecked();
+        boolean formDataEnabled = formDataSwitch.isChecked();
+        boolean easyListEnabled = easyListSwitch.isChecked();
+        boolean easyPrivacyEnabled = easyPrivacySwitch.isChecked();
+        boolean fanboysAnnoyanceEnabled = fanboysAnnoyanceSwitch.isChecked();
+        boolean fanboysSocialBlockingEnabled = fanboysSocialBlockingSwitch.isChecked();
         int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
         int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
         int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
@@ -647,8 +670,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         // Save the domain settings.
         if (savedSslCertificateRadioButton.isChecked()) {  // The current certificate is being used.
             // Update the database except for the certificate.
-            domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean,
-                    formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, pinnedSslCertificate);
+            domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled,
+                    domStorageEnabled, formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt,
+                    nightModeInt, pinnedSslCertificate);
         } else if (currentWebsiteCertificateRadioButton.isChecked()) {  // The certificate is being updated with the current website certificate.
             // Get the current website SSL certificate.
             SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate;
@@ -664,13 +688,15 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime();
 
             // Update the database.
-            domainsDatabaseHelper.updateDomainWithCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean,
-                    userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization, issuedByOrganizationalUnit,
-                    startDateLong, endDateLong);
+            domainsDatabaseHelper.updateDomainWithCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabled,
+                    formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt,
+                    pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization, issuedByOrganizationalUnit, startDateLong, endDateLong);
+
         } else {  // No certificate is selected.
             // Update the database, with PINNED_SSL_CERTIFICATE set to false.
-            domainsDatabaseHelper.updateDomainExceptCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean,
-                    userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, false);
+            domainsDatabaseHelper.updateDomainExceptCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabled,
+                    formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt,
+                    false);
         }
     }