2 * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser;
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.app.DialogFragment;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.net.http.SslCertificate;
29 import android.net.http.SslError;
30 import android.os.Bundle;
31 import android.text.SpannableStringBuilder;
32 import android.text.Spanned;
33 import android.text.style.ForegroundColorSpan;
34 import android.view.LayoutInflater;
35 import android.widget.TextView;
37 import java.util.Date;
39 public class SslCertificateError extends DialogFragment {
41 private String primaryError;
42 private String urlWithError;
43 private String issuedToCName;
44 private String issuedToOName;
45 private String issuedToUName;
46 private String issuedByCName;
47 private String issuedByOName;
48 private String issuedByUName;
49 private String startDate;
50 private String endDate;
52 public static SslCertificateError displayDialog(SslError error) {
53 // Get the various components of the SSL error message.
54 int primaryErrorIntForBundle = error.getPrimaryError();
55 String urlWithErrorForBundle = error.getUrl();
56 SslCertificate sslCertificate = error.getCertificate();
57 String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
58 String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
59 String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
60 String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
61 String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
62 String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
63 Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
64 Date endDateForBundle = sslCertificate.getValidNotAfterDate();
66 // Store the SSL error message components in a `Bundle`.
67 Bundle argumentsBundle = new Bundle();
68 argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
69 argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
70 argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
71 argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
72 argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
73 argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
74 argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
75 argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
76 argumentsBundle.putString("StartDate", startDateForBundle.toString());
77 argumentsBundle.putString("EndDate", endDateForBundle.toString());
79 // Add `argumentsBundle` to this instance of `SslCertificateError`.
80 SslCertificateError thisSslCertificateErrorDialog = new SslCertificateError();
81 thisSslCertificateErrorDialog.setArguments(argumentsBundle);
82 return thisSslCertificateErrorDialog;
86 public void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
89 // Save the components of the SSL error message in class variables.
90 urlWithError = getArguments().getString("UrlWithError");
91 issuedToCName = getArguments().getString("IssuedToCName");
92 issuedToOName = getArguments().getString("IssuedToOName");
93 issuedToUName = getArguments().getString("IssuedToUName");
94 issuedByCName = getArguments().getString("IssuedByCName");
95 issuedByOName = getArguments().getString("IssuedByOName");
96 issuedByUName = getArguments().getString("IssuedByUName");
97 startDate = getArguments().getString("StartDate");
98 endDate = getArguments().getString("EndDate");
100 // Get the appropriate string for `primaryError.
101 int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
102 switch (primaryErrorInt) {
103 case SslError.SSL_NOTYETVALID:
104 primaryError = getString(R.string.future_certificate);
107 case SslError.SSL_EXPIRED:
108 primaryError = getString(R.string.expired_certificate);
111 case SslError.SSL_IDMISMATCH:
112 primaryError = getString(R.string.cn_mismatch);
115 case SslError.SSL_UNTRUSTED:
116 primaryError = getString(R.string.untrusted);
119 case SslError.SSL_DATE_INVALID:
120 primaryError = getString(R.string.invalid_date);
123 case SslError.SSL_INVALID:
124 primaryError = getString(R.string.invalid_certificate);
129 // The public interface is used to send information back to the parent activity.
130 public interface SslCertificateErrorListener {
131 void onSslErrorCancel();
133 void onSslErrorProceed();
136 // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
137 private SslCertificateErrorListener sslCertificateErrorListener;
139 // Check to make sure that the parent activity implements the listener.
140 public void onAttach(Context context) {
141 super.onAttach(context);
144 sslCertificateErrorListener = (SslCertificateErrorListener) context;
145 } catch(ClassCastException exception) {
146 throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
150 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
151 @SuppressLint("InflateParams")
153 public Dialog onCreateDialog(Bundle savedInstanceState) {
154 // Get the activity's layout inflater.
155 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
157 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
158 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
159 dialogBuilder.setTitle(R.string.ssl_certificate_error);
160 // The parent view is `null` because it will be assigned by `AlertDialog`.
161 dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
163 // Set an `onClick` listener on the negative button. `null` doesn't do anything extra when the button is pressed. The `Dialog` will automatically close.
164 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
166 public void onClick(DialogInterface dialog, int which) {
167 sslCertificateErrorListener.onSslErrorCancel();
171 // Set an `onClick` listener on the positive button.
172 dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
174 public void onClick(DialogInterface dialog, int which) {
175 sslCertificateErrorListener.onSslErrorProceed();
180 // Create an `AlertDialog` from the `AlertDialog.Builder`.
181 AlertDialog alertDialog = dialogBuilder.create();
183 // We have to show the `AlertDialog` before we can modify the content.
186 // Get handles for the `TextViews`
187 TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
188 TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
189 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
190 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
191 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
192 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
193 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
194 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
195 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
196 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
198 // Setup the common strings.
199 String urlLabel = getString(R.string.url_label) + " ";
200 String cNameLabel = getString(R.string.common_name) + " ";
201 String oNameLabel = getString(R.string.organization) + " ";
202 String uNameLabel = getString(R.string.organizational_unit) + " ";
203 String startDateLabel = getString(R.string.start_date) + " ";
204 String endDateLabel = getString(R.string.end_date) + " ";
206 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
207 SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
208 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
209 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
210 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
211 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
212 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
213 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
214 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
215 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
217 // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
218 @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
220 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
221 urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
222 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
223 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
224 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
225 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
226 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
227 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
228 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
229 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
232 // Display the strings.
233 primaryErrorTextView.setText(primaryError);
234 urlTextView.setText(urlStringBuilder);
235 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
236 issuedToONameTextView.setText(issuedToONameStringBuilder);
237 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
238 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
239 issuedByONameTextView.setText(issuedByONameStringBuilder);
240 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
241 startDateTextView.setText(startDateStringBuilder);
242 endDateTextView.setText(endDateStringBuilder);
244 // `onCreateDialog` requires the return of an `AlertDialog`.