]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/PinnedMismatchDialog.java
Add a context menu entry to Open in New Tab.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / PinnedMismatchDialog.java
index c8a0a78844ebe4e9427fe3641a15f82171cce8f5..5d2fdc7afbe15e4abdfa85e96576c7311ef089f3 100644 (file)
@@ -24,6 +24,7 @@ import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
@@ -51,6 +52,7 @@ import java.util.ArrayList;
 import java.util.Date;
 
 import androidx.annotation.NonNull;
+import androidx.core.content.ContextCompat;
 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
 import androidx.viewpager.widget.PagerAdapter;
 
@@ -87,7 +89,7 @@ public class PinnedMismatchDialog extends DialogFragment {
         // Create an arguments bundle.
         Bundle argumentsBundle = new Bundle();
 
-        // Store the WebView position in the bundle.
+        // Store the WebView fragment ID in the bundle.
         argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
 
         // Create a new instance of the pinned mismatch dialog.
@@ -105,11 +107,14 @@ public class PinnedMismatchDialog extends DialogFragment {
     @Override
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
+        // Get the arguments.
+        Bundle arguments = getArguments();
+
         // Remove the incorrect lint warning below that `.getArguments().getInt()` might be null.
-        assert getArguments() != null;
+        assert arguments != null;
 
         // Get the current position of this WebView fragment.
-        int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(getArguments().getLong("webview_fragment_id"));
+        int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(arguments.getLong("webview_fragment_id"));
 
         // Get the WebView tab fragment.
         WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
@@ -135,8 +140,29 @@ public class PinnedMismatchDialog extends DialogFragment {
             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
         }
 
+        // Get the context.
+        Context context = getContext();
+
+        // Remove the incorrect lint warning below that the context might be null.
+        assert context != null;
+
+        // Get the favorite icon.
+        Bitmap favoriteIconBitmap = nestedScrollWebView.getFavoriteOrDefaultIcon();
+
+        // Get the default favorite icon drawable.  `ContextCompat` must be used until API >= 21.
+        Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(context, R.drawable.world);
+
+        // Cast the favorite icon drawable to a bitmap drawable.
+        BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable;
+
+        // Remove the incorrect warning below that the favorite icon bitmap drawable might be null.
+        assert defaultFavoriteIconBitmapDrawable != null;
+
+        // Store the default icon bitmap.
+        Bitmap defaultFavoriteIconBitmap = defaultFavoriteIconBitmapDrawable.getBitmap();
+
         // Set the favorite icon as the dialog icon if it exists.
-        if (MainWebViewActivity.favoriteIconBitmap.equals(MainWebViewActivity.favoriteIconDefaultBitmap)) {  // There is no favorite icon.
+        if (favoriteIconBitmap.sameAs(defaultFavoriteIconBitmap)) {  // There is no website favorite icon.
             // Set the icon according to the theme.
             if (MainWebViewActivity.darkTheme) {
                 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
@@ -145,7 +171,7 @@ public class PinnedMismatchDialog extends DialogFragment {
             }
         } else {  // There is a favorite icon.
             // Create a drawable version of the favorite icon.
-            Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
+            Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), favoriteIconBitmap);
 
             // Set the icon.
             dialogBuilder.setIcon(favoriteIconDrawable);
@@ -167,7 +193,7 @@ public class PinnedMismatchDialog extends DialogFragment {
             }
 
             // Initialize the database handler.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
-            DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
+            DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
 
             // Update the SSL certificate if it is pinned.
             if (nestedScrollWebView.hasPinnedSslCertificate()) {
@@ -289,8 +315,8 @@ public class PinnedMismatchDialog extends DialogFragment {
             String startDateLabel = getString(R.string.start_date) + "  ";
             String endDateLabel = getString(R.string.end_date) + "  ";
 
-            // Get a URI for the URL.
-            Uri currentUri = Uri.parse(MainWebViewActivity.formattedUrlString);
+            // Convert the URL to a URI.
+            Uri currentUri = Uri.parse(nestedScrollWebView.getUrl());
 
             // Get the current host from the URI.
             String domainName = currentUri.getHost();