]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/PinnedMismatchDialog.java
Make pinned settings tab aware.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / PinnedMismatchDialog.java
index 7e86bbdb740589a78c182ab1c0c2710313b6e013..c8a0a78844ebe4e9427fe3641a15f82171cce8f5 100644 (file)
@@ -32,7 +32,6 @@ import android.os.Bundle;
 import android.text.SpannableStringBuilder;
 import android.text.Spanned;
 import android.text.style.ForegroundColorSpan;
-import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
@@ -42,10 +41,13 @@ import com.google.android.material.tabs.TabLayout;
 
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
+import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
+import com.stoutner.privacybrowser.views.NestedScrollWebView;
 import com.stoutner.privacybrowser.views.WrapVerticalContentViewPager;
 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
 
 import java.text.DateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 
 import androidx.annotation.NonNull;
@@ -53,9 +55,9 @@ import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment mu
 import androidx.viewpager.widget.PagerAdapter;
 
 public class PinnedMismatchDialog extends DialogFragment {
-    // Instantiate the class variables.
+    // Declare the class variables.
     private PinnedMismatchListener pinnedMismatchListener;
-    private LayoutInflater layoutInflater;
+    private NestedScrollWebView nestedScrollWebView;
     private String currentSslIssuedToCName;
     private String currentSslIssuedToOName;
     private String currentSslIssuedToUName;
@@ -64,8 +66,6 @@ public class PinnedMismatchDialog extends DialogFragment {
     private String currentSslIssuedByUName;
     private Date currentSslStartDate;
     private Date currentSslEndDate;
-    private boolean pinnedSslCertificate;
-    private boolean pinnedIpAddresses;
 
     // The public interface is used to send information back to the parent activity.
     public interface PinnedMismatchListener {
@@ -83,19 +83,21 @@ public class PinnedMismatchDialog extends DialogFragment {
         pinnedMismatchListener = (PinnedMismatchListener) context;
     }
 
-    public static PinnedMismatchDialog displayDialog(int domainSettingsDatabaseId, boolean pinnedSslCertificate, boolean pinnedIpAddresses) {
+    public static PinnedMismatchDialog displayDialog(long webViewFragmentId) {
         // Create an arguments bundle.
         Bundle argumentsBundle = new Bundle();
 
-        // Store the variables in the bundle.
-        argumentsBundle.putInt("domain_settings_database_id", domainSettingsDatabaseId);
-        argumentsBundle.putBoolean("pinned_sss_certificate", pinnedSslCertificate);
-        argumentsBundle.putBoolean("pinned_ip_addresses", pinnedIpAddresses);
+        // Store the WebView position in the bundle.
+        argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
 
-        // Add the arguments bundle to this instance of `PinnedMismatchDialog`.
-        PinnedMismatchDialog thisPinnedMismatchDialog = new PinnedMismatchDialog();
-        thisPinnedMismatchDialog.setArguments(argumentsBundle);
-        return thisPinnedMismatchDialog;
+        // Create a new instance of the pinned mismatch dialog.
+        PinnedMismatchDialog pinnedMismatchDialog = new PinnedMismatchDialog();
+
+        // Add the arguments bundle to the new instance.
+        pinnedMismatchDialog.setArguments(argumentsBundle);
+
+        // Make it so.
+        return pinnedMismatchDialog;
     }
 
     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
@@ -103,11 +105,23 @@ public class PinnedMismatchDialog extends DialogFragment {
     @Override
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        // Remove the incorrect lint warning that `getActivity()` might be null.
-        assert getActivity() != null;
+        // Remove the incorrect lint warning below that `.getArguments().getInt()` might be null.
+        assert getArguments() != null;
+
+        // Get the current position of this WebView fragment.
+        int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(getArguments().getLong("webview_fragment_id"));
+
+        // Get the WebView tab fragment.
+        WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
+
+        // Get the fragment view.
+        View fragmentView = webViewTabFragment.getView();
+
+        // Remove the incorrect lint warning below that the fragment view might be null.
+        assert fragmentView != null;
 
-        // Get the activity's layout inflater.
-        layoutInflater = getActivity().getLayoutInflater();
+        // Get a handle for the current WebView.
+        nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
 
         // Use an alert dialog builder to create the alert dialog.
         AlertDialog.Builder dialogBuilder;
@@ -121,14 +135,6 @@ public class PinnedMismatchDialog extends DialogFragment {
             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
         }
 
-        // Remove the incorrect lint warning below that `.getArguments.getBoolean()` might be null.
-        assert getArguments() != null;
-
-        // Get the variables from the bundle.
-        int domainSettingsDatabaseId = getArguments().getInt("domain_settings_database_id");
-        pinnedSslCertificate = getArguments().getBoolean("pinned_ssl_certificate");
-        pinnedIpAddresses = getArguments().getBoolean("pinned_ip_addresses");
-
         // Set the favorite icon as the dialog icon if it exists.
         if (MainWebViewActivity.favoriteIconBitmap.equals(MainWebViewActivity.favoriteIconDefaultBitmap)) {  // There is no favorite icon.
             // Set the icon according to the theme.
@@ -164,29 +170,23 @@ public class PinnedMismatchDialog extends DialogFragment {
             DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
 
             // Update the SSL certificate if it is pinned.
-            if (pinnedSslCertificate) {
+            if (nestedScrollWebView.hasPinnedSslCertificate()) {
                 // Update the pinned SSL certificate in the domain database.
-                domainsDatabaseHelper.updatePinnedSslCertificate(domainSettingsDatabaseId, currentSslIssuedToCName, currentSslIssuedToOName, currentSslIssuedToUName,
+                domainsDatabaseHelper.updatePinnedSslCertificate(nestedScrollWebView.getDomainSettingsDatabaseId(), currentSslIssuedToCName, currentSslIssuedToOName, currentSslIssuedToUName,
                         currentSslIssuedByCName, currentSslIssuedByOName, currentSslIssuedByUName, currentSslStartDateLong, currentSslEndDateLong);
 
-                // Update the pinned SSL certificate class variables to match the information that is now in the database.
-                MainWebViewActivity.pinnedSslIssuedToCName = currentSslIssuedToCName;
-                MainWebViewActivity.pinnedSslIssuedToOName = currentSslIssuedToOName;
-                MainWebViewActivity.pinnedSslIssuedToUName = currentSslIssuedToUName;
-                MainWebViewActivity.pinnedSslIssuedByCName = currentSslIssuedByCName;
-                MainWebViewActivity.pinnedSslIssuedByOName = currentSslIssuedByOName;
-                MainWebViewActivity.pinnedSslIssuedByUName = currentSslIssuedByUName;
-                MainWebViewActivity.pinnedSslStartDate = currentSslStartDate;
-                MainWebViewActivity.pinnedSslEndDate = currentSslEndDate;
+                // Update the pinned SSL certificate in the nested scroll WebView.
+                nestedScrollWebView.setPinnedSslCertificate(currentSslIssuedToCName, currentSslIssuedToOName, currentSslIssuedToUName, currentSslIssuedByCName, currentSslIssuedByOName, currentSslIssuedByUName,
+                        currentSslStartDate, currentSslEndDate);
             }
 
             // Update the IP addresses if they are pinned.
-            if (pinnedIpAddresses) {
+            if (nestedScrollWebView.hasPinnedIpAddresses()) {
                 // Update the pinned IP addresses in the domain database.
-                domainsDatabaseHelper.updatePinnedIpAddresses(domainSettingsDatabaseId, MainWebViewActivity.currentHostIpAddresses);
+                domainsDatabaseHelper.updatePinnedIpAddresses(nestedScrollWebView.getDomainSettingsDatabaseId(), nestedScrollWebView.getCurrentIpAddresses());
 
-                // Update the pinned IP addresses class variable to match the information that is now in the database.
-                MainWebViewActivity.pinnedHostIpAddresses = MainWebViewActivity.currentHostIpAddresses;
+                // Update the pinned IP addresses in the nested scroll WebView.
+                nestedScrollWebView.setPinnedIpAddresses(nestedScrollWebView.getCurrentIpAddresses());
             }
         });
 
@@ -205,8 +205,12 @@ public class PinnedMismatchDialog extends DialogFragment {
         // Set the title.
         dialogBuilder.setTitle(R.string.pinned_mismatch);
 
+        // Remove the incorrect lint warning below that `getLayoutInflater()` might be null.
+        assert getActivity() != null;
+
         // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
-        dialogBuilder.setView(layoutInflater.inflate(R.layout.pinned_mismatch_linearlayout, null));
+        // For some reason, `getLayoutInflater()` without `getActivity()` produces an endless loop (probably a bug that will be fixed at some point in the future).
+        dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.pinned_mismatch_linearlayout, null));
 
         // Create an alert dialog from the alert dialog builder.
         final AlertDialog alertDialog = dialogBuilder.create();
@@ -262,7 +266,7 @@ public class PinnedMismatchDialog extends DialogFragment {
         @NonNull
         public Object instantiateItem(@NonNull ViewGroup container, int position) {
             // Inflate the scroll view for this tab.
-            ViewGroup tabViewGroup = (ViewGroup) layoutInflater.inflate(R.layout.pinned_mismatch_scrollview, container, false);
+            ViewGroup tabViewGroup = (ViewGroup) getLayoutInflater().inflate(R.layout.pinned_mismatch_scrollview, container, false);
 
             // Get handles for the `TextViews`.
             TextView domainNameTextView = tabViewGroup.findViewById(R.id.domain_name);
@@ -292,7 +296,7 @@ public class PinnedMismatchDialog extends DialogFragment {
             String domainName = currentUri.getHost();
 
             // Get the current website SSL certificate.
-            SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
+            SslCertificate sslCertificate = nestedScrollWebView.getCertificate();
 
             // Extract the individual pieces of information from the current website SSL certificate if it is not null.
             if (sslCertificate != null) {
@@ -314,6 +318,13 @@ public class PinnedMismatchDialog extends DialogFragment {
                 currentSslIssuedByUName = "";
             }
 
+            // Get the pinned SSL certificate.
+            ArrayList<Object> pinnedSslCertificateArrayList = nestedScrollWebView.getPinnedSslCertificate();
+
+            // Extract the arrays from the array list.
+            String[] pinnedSslCertificateStringArray = (String[]) pinnedSslCertificateArrayList.get(0);
+            Date[] pinnedSslCertificateDateArray = (Date[]) pinnedSslCertificateArrayList.get(1);
+
             // Setup the domain name spannable string builder.
             SpannableStringBuilder domainNameStringBuilder = new SpannableStringBuilder(domainNameLabel + domainName);
 
@@ -331,7 +342,7 @@ public class PinnedMismatchDialog extends DialogFragment {
             // Setup the spannable string builders for each tab.
             if (position == 0) {  // Setup the current settings tab.
                 // Create the string builders.
-                ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + MainWebViewActivity.currentHostIpAddresses);
+                ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + nestedScrollWebView.getCurrentIpAddresses());
                 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedToCName);
                 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedToOName);
                 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedToUName);
@@ -353,26 +364,25 @@ public class PinnedMismatchDialog extends DialogFragment {
                 }
             } else {  // Setup the pinned settings tab.
                 // Create the string builders.
-                ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + MainWebViewActivity.pinnedHostIpAddresses);
-                issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedSslIssuedToCName);
-                issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedSslIssuedToOName);
-                issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedSslIssuedToUName);
-                issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedSslIssuedByCName);
-                issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedSslIssuedByOName);
-                issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedSslIssuedByUName);
+                ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + nestedScrollWebView.getPinnedIpAddresses());
+                issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + pinnedSslCertificateStringArray[0]);
+                issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + pinnedSslCertificateStringArray[1]);
+                issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + pinnedSslCertificateStringArray[2]);
+                issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + pinnedSslCertificateStringArray[3]);
+                issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + pinnedSslCertificateStringArray[4]);
+                issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + pinnedSslCertificateStringArray[5]);
 
                 // Set the dates if they aren't `null`.
-                if (MainWebViewActivity.pinnedSslStartDate == null) {
+                if (pinnedSslCertificateDateArray[0] == null) {
                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
                 } else {
-                    startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG)
-                            .format(MainWebViewActivity.pinnedSslStartDate));
+                    startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(pinnedSslCertificateDateArray[0]));
                 }
 
-                if (MainWebViewActivity.pinnedSslEndDate == null) {
+                if (pinnedSslCertificateDateArray[1] == null) {
                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel);
                 } else {
-                    endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(MainWebViewActivity.pinnedSslEndDate));
+                    endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(pinnedSslCertificateDateArray[1]));
                 }
             }
 
@@ -395,8 +405,8 @@ public class PinnedMismatchDialog extends DialogFragment {
             domainNameStringBuilder.setSpan(blueColorSpan, domainNameLabel.length(), domainNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 
             // Color coordinate the IP addresses if they are pinned.
-            if (pinnedIpAddresses) {
-                if (MainWebViewActivity.currentHostIpAddresses.equals(MainWebViewActivity.pinnedHostIpAddresses)) {
+            if (nestedScrollWebView.hasPinnedIpAddresses()) {
+                if (nestedScrollWebView.getCurrentIpAddresses().equals(nestedScrollWebView.getPinnedIpAddresses())) {
                     ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     ipAddressesStringBuilder.setSpan(redColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
@@ -404,50 +414,50 @@ public class PinnedMismatchDialog extends DialogFragment {
             }
 
             // Color coordinate the SSL certificate fields if they are pinned.
-            if (pinnedSslCertificate) {
-                if (currentSslIssuedToCName.equals(MainWebViewActivity.pinnedSslIssuedToCName)) {
+            if (nestedScrollWebView.hasPinnedSslCertificate()) {
+                if (currentSslIssuedToCName.equals(pinnedSslCertificateStringArray[0])) {
                     issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if (currentSslIssuedToOName.equals(MainWebViewActivity.pinnedSslIssuedToOName)) {
+                if (currentSslIssuedToOName.equals(pinnedSslCertificateStringArray[1])) {
                     issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     issuedToONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if (currentSslIssuedToUName.equals(MainWebViewActivity.pinnedSslIssuedToUName)) {
+                if (currentSslIssuedToUName.equals(pinnedSslCertificateStringArray[2])) {
                     issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     issuedToUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if (currentSslIssuedByCName.equals(MainWebViewActivity.pinnedSslIssuedByCName)) {
+                if (currentSslIssuedByCName.equals(pinnedSslCertificateStringArray[3])) {
                     issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if (currentSslIssuedByOName.equals(MainWebViewActivity.pinnedSslIssuedByOName)) {
+                if (currentSslIssuedByOName.equals(pinnedSslCertificateStringArray[4])) {
                     issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if (currentSslIssuedByUName.equals(MainWebViewActivity.pinnedSslIssuedByUName)) {
+                if (currentSslIssuedByUName.equals(pinnedSslCertificateStringArray[5])) {
                     issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if ((currentSslStartDate != null) && currentSslStartDate.equals(MainWebViewActivity.pinnedSslStartDate)) {
+                if ((currentSslStartDate != null) && currentSslStartDate.equals(pinnedSslCertificateDateArray[0])) {
                     startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 }
 
-                if ((currentSslEndDate != null) && currentSslEndDate.equals(MainWebViewActivity.pinnedSslEndDate)) {
+                if ((currentSslEndDate != null) && currentSslEndDate.equals(pinnedSslCertificateDateArray[1])) {
                     endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 } else {
                     endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
@@ -473,4 +483,4 @@ public class PinnedMismatchDialog extends DialogFragment {
             return tabViewGroup;
         }
     }
-}
+}
\ No newline at end of file