]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/SslCertificateErrorDialog.java
Add a requests activity. https://redmine.stoutner.com/issues/170
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SslCertificateErrorDialog.java
index 78108d0a4d6ac9770b376d34e1c6e08a410e2e98..0c9302599bc4b48b1cf53298a1196abf42f9ee95 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -28,31 +28,39 @@ import android.net.http.SslCertificate;
 import android.net.http.SslError;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
-// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
+// `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22.
 import android.support.v7.app.AppCompatDialogFragment;
 import android.text.SpannableStringBuilder;
 import android.text.Spanned;
 import android.text.style.ForegroundColorSpan;
 import android.view.LayoutInflater;
+import android.view.WindowManager;
 import android.widget.TextView;
 
 import com.stoutner.privacybrowser.R;
+import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 
 import java.text.DateFormat;
 import java.util.Date;
 
 public class SslCertificateErrorDialog extends AppCompatDialogFragment {
+    // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
+    private SslCertificateErrorListener sslCertificateErrorListener;
+
+    // The public interface is used to send information back to the parent activity.
+    public interface SslCertificateErrorListener {
+        void onSslErrorCancel();
 
-    private int primaryErrorInt;
-    private String urlWithError;
-    private String issuedToCName;
-    private String issuedToOName;
-    private String issuedToUName;
-    private String issuedByCName;
-    private String issuedByOName;
-    private String issuedByUName;
-    private String startDate;
-    private String endDate;
+        void onSslErrorProceed();
+    }
+
+    public void onAttach(Context context) {
+        // Run the default commands.
+        super.onAttach(context);
+
+        // Get a handle for `SslCertificateErrorListener` from the launching context.
+        sslCertificateErrorListener = (SslCertificateErrorListener) context;
+    }
 
     public static SslCertificateErrorDialog displayDialog(SslError error) {
         // Get the various components of the SSL error message.
@@ -87,95 +95,92 @@ public class SslCertificateErrorDialog extends AppCompatDialogFragment {
         return thisSslCertificateErrorDialog;
     }
 
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        // Save the components of the SSL error message in class variables.
-        primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
-        urlWithError = getArguments().getString("UrlWithError");
-        issuedToCName = getArguments().getString("IssuedToCName");
-        issuedToOName = getArguments().getString("IssuedToOName");
-        issuedToUName = getArguments().getString("IssuedToUName");
-        issuedByCName = getArguments().getString("IssuedByCName");
-        issuedByOName = getArguments().getString("IssuedByOName");
-        issuedByUName = getArguments().getString("IssuedByUName");
-        startDate = getArguments().getString("StartDate");
-        endDate = getArguments().getString("EndDate");
-    }
-
-    // The public interface is used to send information back to the parent activity.
-    public interface SslCertificateErrorListener {
-        void onSslErrorCancel();
-
-        void onSslErrorProceed();
-    }
-
-    // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
-    private SslCertificateErrorListener sslCertificateErrorListener;
-
-    // Check to make sure that the parent activity implements the listener.
-    public void onAttach(Context context) {
-        super.onAttach(context);
-
-        try {
-            sslCertificateErrorListener = (SslCertificateErrorListener) context;
-        } catch(ClassCastException exception) {
-            throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
-        }
-    }
-
     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
     @SuppressLint("InflateParams")
     @SuppressWarnings("deprecation")
     @Override
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
+        // Remove the incorrect lint warning that `getArguments()` might be null.
+        assert getArguments() != null;
+
+        // Get the components of the SSL error message from the bundle.
+        int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
+        String urlWithError = getArguments().getString("UrlWithError");
+        String issuedToCName = getArguments().getString("IssuedToCName");
+        String issuedToOName = getArguments().getString("IssuedToOName");
+        String issuedToUName = getArguments().getString("IssuedToUName");
+        String issuedByCName = getArguments().getString("IssuedByCName");
+        String issuedByOName = getArguments().getString("IssuedByOName");
+        String issuedByUName = getArguments().getString("IssuedByUName");
+        String startDate = getArguments().getString("StartDate");
+        String endDate = getArguments().getString("EndDate");
+
+        // Remove the incorrect lint warning that `getActivity()` might be null.
+        assert getActivity() != null;
+
         // Get the activity's layout inflater.
         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
 
-        // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
-        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
+        // Use an alert dialog builder to create the alert dialog.
+        AlertDialog.Builder dialogBuilder;
+
+        // Set the style and icon according to the theme.
+        if (MainWebViewActivity.darkTheme) {
+            // Set the style.
+            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
+
+            // Set the icon.
+            dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
+        } else {
+            // Set the style.
+            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
+
+            // Set the icon.
+            dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
+        }
+
+        // Set the title.
         dialogBuilder.setTitle(R.string.ssl_certificate_error);
-        // The parent view is `null` because it will be assigned by `AlertDialog`.
+
+        // Set the view.  The parent view is `null` because it will be assigned by `AlertDialog`.
         dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
 
-        // Set an `onClick` listener on the negative button.  `null` doesn't do anything extra when the button is pressed.  The `Dialog` will automatically close.
-        dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-                sslCertificateErrorListener.onSslErrorCancel();
-            }
-        });
+        // Set a listener on the negative button.
+        dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> sslCertificateErrorListener.onSslErrorCancel());
 
-        // Set an `onClick` listener on the positive button.
-        dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-                sslCertificateErrorListener.onSslErrorProceed();
-            }
-        });
+        // Set a listener on the positive button.
+        dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> sslCertificateErrorListener.onSslErrorProceed());
 
 
-        // Create an `AlertDialog` from the `AlertDialog.Builder`.
+        // Create an alert dialog from the alert dialog builder.
         AlertDialog alertDialog = dialogBuilder.create();
 
-        // We have to show the `AlertDialog` before we can modify the content.
+        // Disable screenshots if not allowed.
+        if (!MainWebViewActivity.allowScreenshots) {
+            // Remove the warning below that `getWindow()` might be null.
+            assert alertDialog.getWindow() != null;
+
+            // Disable screenshots.
+            alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
+        }
+
+        // We have to show the alert dialog before we can modify the content.
         alertDialog.show();
 
         // Get handles for the `TextViews`
-        TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
-        TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
-        TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
-        TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
-        TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
-        TextView issuedByTextView = (TextView) alertDialog.findViewById(R.id.issued_by_textview);
-        TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
-        TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
-        TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
-        TextView validDatesTextView = (TextView) alertDialog.findViewById(R.id.valid_dates_textview);
-        TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
-        TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
+        TextView primaryErrorTextView = alertDialog.findViewById(R.id.primary_error);
+        TextView urlTextView = alertDialog.findViewById(R.id.url_error_dialog);
+        TextView issuedToCNameTextView = alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
+        TextView issuedToONameTextView = alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
+        TextView issuedToUNameTextView = alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
+        TextView issuedByTextView = alertDialog.findViewById(R.id.issued_by_textview);
+        TextView issuedByCNameTextView = alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
+        TextView issuedByONameTextView = alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
+        TextView issuedByUNameTextView = alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
+        TextView validDatesTextView = alertDialog.findViewById(R.id.valid_dates_textview);
+        TextView startDateTextView = alertDialog.findViewById(R.id.start_date_error_dialog);
+        TextView endDateTextView = alertDialog.findViewById(R.id.end_date_error_dialog);
 
         // Setup the common strings.
         String urlLabel = getString(R.string.url_label) + "  ";
@@ -196,9 +201,20 @@ public class SslCertificateErrorDialog extends AppCompatDialogFragment {
         SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
         SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
 
-        // Create the `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
-        ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
-        ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
+        // Create a red `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
+        @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
+
+        // Create a blue `ForegroundColorSpan`.
+        ForegroundColorSpan blueColorSpan;
+
+        // Set `blueColorSpan` according to the theme.  We have to use the deprecated `getColor()` until API >= 23.
+        if (MainWebViewActivity.darkTheme) {
+            //noinspection deprecation
+            blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
+        } else {
+            //noinspection deprecation
+            blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
+        }
 
         // Setup the spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
         urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);