/*
- * Copyright 2016-2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
*
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.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 {
- private String primaryError;
+ // The private variables are used in `onCreate()` and `onCreateDialog()`.
+ private int primaryErrorInt;
private String urlWithError;
private String issuedToCName;
private String issuedToOName;
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");
issuedByUName = getArguments().getString("IssuedByUName");
startDate = getArguments().getString("StartDate");
endDate = getArguments().getString("EndDate");
-
- // Get the appropriate string for `primaryError.
- int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
- switch (primaryErrorInt) {
- case SslError.SSL_NOTYETVALID:
- primaryError = getString(R.string.future_certificate);
- break;
-
- case SslError.SSL_EXPIRED:
- primaryError = getString(R.string.expired_certificate);
- break;
-
- case SslError.SSL_IDMISMATCH:
- primaryError = getString(R.string.cn_mismatch);
- break;
-
- case SslError.SSL_UNTRUSTED:
- primaryError = getString(R.string.untrusted);
- break;
-
- case SslError.SSL_DATE_INVALID:
- primaryError = getString(R.string.invalid_date);
- break;
-
- case SslError.SSL_INVALID:
- primaryError = getString(R.string.invalid_certificate);
- break;
- }
}
// The public interface is used to send information back to the parent activity.
// `@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) {
// 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 `AlertDialog.Builder` to create the `AlertDialog`.
+ 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.
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);
SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
- // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
- @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
+ // 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);
startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ // Initialize `primaryErrorString`.
+ String primaryErrorString = "";
+
+ // Highlight the primary error in red and store the primary error string in `primaryErrorString`.
+ switch (primaryErrorInt) {
+ case SslError.SSL_IDMISMATCH:
+ // Change the URL span colors to red.
+ urlStringBuilder.setSpan(redColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+
+ // Store the primary error string.
+ primaryErrorString = getString(R.string.cn_mismatch);
+ break;
+
+ case SslError.SSL_UNTRUSTED:
+ // Change the `issuesByTextView` text to red. We have to use the deprecated `getColor()` until API >= 23.
+ issuedByTextView.setTextColor(getResources().getColor(R.color.red_a700));
+
+ // Change the issued by span color to red.
+ issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+
+ // Store the primary error string.
+ primaryErrorString = getString(R.string.untrusted);
+ break;
+
+ case SslError.SSL_DATE_INVALID:
+ // Change the `validDatesTextView` text to red. We have to use the deprecated `getColor()` until API >= 23.
+ validDatesTextView.setTextColor(getResources().getColor(R.color.red_a700));
+
+ // Change the date span colors to red.
+ startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+
+ // Store the primary error string.
+ primaryErrorString = getString(R.string.invalid_date);
+ break;
+
+ case SslError.SSL_NOTYETVALID:
+ // Change the start date span color to red.
+ startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+
+ // Store the primary error string.
+ primaryErrorString = getString(R.string.future_certificate);
+ break;
+
+ case SslError.SSL_EXPIRED:
+ // Change the end date span color to red.
+ endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+
+ // Store the primary error string.
+ primaryErrorString = getString(R.string.expired_certificate);
+ break;
+
+ case SslError.SSL_INVALID:
+ // Store the primary error string.
+ primaryErrorString = getString(R.string.invalid_certificate);
+ break;
+ }
+
// Display the strings.
- primaryErrorTextView.setText(primaryError);
+ primaryErrorTextView.setText(primaryErrorString);
urlTextView.setText(urlStringBuilder);
issuedToCNameTextView.setText(issuedToCNameStringBuilder);
issuedToONameTextView.setText(issuedToONameStringBuilder);