]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Fix a crash when uploading files to some sites. https://redmine.stoutner.com/issues/556
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.java
index 037ed592f8cf056977defb4d7963f225cf1a82f0..708e622b2e0568e74187224406122cd841c488df 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2017-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2017-2020 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -41,6 +41,7 @@ import android.widget.Spinner;
 import android.widget.Switch;
 import android.widget.TextView;
 
+import androidx.annotation.NonNull;
 import androidx.appcompat.app.ActionBar;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.widget.Toolbar;  // The AndroidX toolbar must be used until the minimum API is >= 21.
@@ -208,6 +209,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
 
         // Configure the add domain floating action button.
         addDomainFAB.setOnClickListener((View view) -> {
+            // Remove the incorrect warning below that the current URL might be null.
+            assert currentUrl != null;
+
             // Create an add domain dialog.
             DialogFragment addDomainDialog = AddDomainDialog.addDomain(currentUrl);
 
@@ -560,7 +564,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
     }
 
     @Override
-    protected void onSaveInstanceState(Bundle outState) {
+    protected void onSaveInstanceState(@NonNull Bundle outState) {
         // Store the current `DomainSettingsFragment` state in `outState`.
         if (findViewById(R.id.domain_settings_scrollview) != null) {  // `DomainSettingsFragment` is displayed.
             // Save any changes that have been made to the domain settings.
@@ -639,14 +643,19 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
     }
 
     @Override
-    public void onAddDomain(DialogFragment dialogFragment) {
+    public void onAddDomain(@NonNull DialogFragment dialogFragment) {
         // Dismiss the undo delete snackbar if it is currently displayed.
         if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
             undoDeleteSnackbar.dismiss();
         }
 
-        // Get the new domain name String from the dialog fragment.
+        // Remove the incorrect lint warning below that the dialog might be null.
+        assert dialogFragment.getDialog() != null;
+
+        // Get a handle for the domain name edit text.
         EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
+
+        // Get the domain name string.
         String domainNameString = domainNameEditText.getText().toString();
 
         // Create the domain and store the database ID in `currentDomainDatabaseId`.
@@ -693,6 +702,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         Spinner userAgentSpinner = view.findViewById(R.id.user_agent_spinner);
         EditText customUserAgentEditText = view.findViewById(R.id.custom_user_agent_edittext);
         Spinner fontSizeSpinner = view.findViewById(R.id.font_size_spinner);
+        EditText customFontSizeEditText = view.findViewById(R.id.custom_font_size_edittext);
         Spinner swipeToRefreshSpinner = view.findViewById(R.id.swipe_to_refresh_spinner);
         Spinner nightModeSpinner = view.findViewById(R.id.night_mode_spinner);
         Spinner wideViewportSpinner = view.findViewById(R.id.wide_viewport_spinner);
@@ -716,8 +726,8 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         boolean ultraList = ultraListSwitch.isChecked();
         boolean ultraPrivacy = ultraPrivacySwitch.isChecked();
         boolean blockAllThirdPartyRequests = blockAllThirdPartyRequestsSwitch.isChecked();
-        int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
-        int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
+        int userAgentSwitchPosition = userAgentSpinner.getSelectedItemPosition();
+        int fontSizeSwitchPosition = fontSizeSpinner.getSelectedItemPosition();
         int swipeToRefreshInt = swipeToRefreshSpinner.getSelectedItemPosition();
         int nightModeInt = nightModeSpinner.getSelectedItemPosition();
         int wideViewportInt = wideViewportSpinner.getSelectedItemPosition();
@@ -729,7 +739,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         String userAgentName;
 
         // Set the user agent name.
-        switch (userAgentPosition) {
+        switch (userAgentSwitchPosition) {
             case MainWebViewActivity.DOMAINS_SYSTEM_DEFAULT_USER_AGENT:
                 // Set the user agent name to be `System default user agent`.
                 userAgentName = resources.getString(R.string.system_default_user_agent);
@@ -745,11 +755,17 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 String[] userAgentNameArray = resources.getStringArray(R.array.user_agent_names);
 
                 // 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.
-                userAgentName = userAgentNameArray[userAgentPosition - 1];
+                userAgentName = userAgentNameArray[userAgentSwitchPosition - 1];
         }
 
-        // Get the font size integer.
-        int fontSizeInt = Integer.parseInt(resources.getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePosition]);
+        // Initialize the font size integer.  `0` indicates the system default font size.
+        int fontSizeInt = 0;
+
+        // Use a custom font size if it is selected.
+        if (fontSizeSwitchPosition == 1) {  // A custom font size is specified.
+            // Get the custom font size from the edit text.
+            fontSizeInt = Integer.parseInt(customFontSizeEditText.getText().toString());
+        }
 
         // Save the domain settings.
         domainsDatabaseHelper.updateDomain(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScript, firstPartyCookies, thirdPartyCookies, domStorage, formData, easyList, easyPrivacy,