2 * Copyright 2015-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.app.Activity;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.app.DialogFragment;
26 import android.content.DialogInterface;
27 import android.net.http.SslCertificate;
28 import android.net.http.SslError;
29 import android.os.Bundle;
30 import android.text.SpannableStringBuilder;
31 import android.text.Spanned;
32 import android.text.style.ForegroundColorSpan;
33 import android.view.LayoutInflater;
34 import android.widget.TextView;
36 import java.util.Date;
38 public class SslCertificateError extends DialogFragment{
39 private String primaryError;
40 private String urlWithError;
41 private String issuedToCName;
42 private String issuedToOName;
43 private String issuedToUName;
44 private String issuedByCName;
45 private String issuedByOName;
46 private String issuedByUName;
47 private String startDate;
48 private String endDate;
50 public static SslCertificateError displayDialog(SslError error) {
51 // Get the various components of the SSL error message.
52 int primaryErrorIntForBundle = error.getPrimaryError();
53 String urlWithErrorForBundle = error.getUrl();
54 SslCertificate sslCertificate = error.getCertificate();
55 String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
56 String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
57 String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
58 String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
59 String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
60 String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
61 Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
62 Date endDateForBundle = sslCertificate.getValidNotAfterDate();
64 // Store the SSL error message components in a `Bundle`.
65 Bundle argumentsBundle = new Bundle();
66 argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
67 argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
68 argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
69 argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
70 argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
71 argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
72 argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
73 argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
74 argumentsBundle.putString("StartDate", startDateForBundle.toString());
75 argumentsBundle.putString("EndDate", endDateForBundle.toString());
77 // Add the `Bundle` to this instance of `SslCertificateError`.
78 SslCertificateError thisSslCertificateErrorDialog = new SslCertificateError();
79 thisSslCertificateErrorDialog.setArguments(argumentsBundle);
80 return thisSslCertificateErrorDialog;
83 public void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
86 // Save the components of the SSL error message in class variables.
87 urlWithError = getArguments().getString("UrlWithError");
88 issuedToCName = getArguments().getString("IssuedToCName");
89 issuedToOName = getArguments().getString("IssuedToOName");
90 issuedToUName = getArguments().getString("IssuedToUName");
91 issuedByCName = getArguments().getString("IssuedByCName");
92 issuedByOName = getArguments().getString("IssuedByOName");
93 issuedByUName = getArguments().getString("IssuedByUName");
94 startDate = getArguments().getString("StartDate");
95 endDate = getArguments().getString("EndDate");
97 // Get the appropriate string for `primaryError.
98 int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
99 switch (primaryErrorInt) {
100 case SslError.SSL_NOTYETVALID:
101 primaryError = getString(R.string.future_certificate);
104 case SslError.SSL_EXPIRED:
105 primaryError = getString(R.string.expired_certificate);
108 case SslError.SSL_IDMISMATCH:
109 primaryError = getString(R.string.cn_mismatch);
112 case SslError.SSL_UNTRUSTED:
113 primaryError = getString(R.string.untrusted);
116 case SslError.SSL_DATE_INVALID:
117 primaryError = getString(R.string.invalid_date);
120 case SslError.SSL_INVALID:
121 primaryError = getString(R.string.invalid_certificate);
126 // The public interface is used to send information back to the parent activity.
127 public interface SslCertificateErrorListener {
128 void onSslErrorCancel();
130 void onSslErrorProceed();
133 // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
134 private SslCertificateErrorListener sslCertificateErrorListener;
136 // Check to make sure that the parent activity implements the listener.
137 public void onAttach(Activity parentActivity) {
138 super.onAttach(parentActivity);
141 sslCertificateErrorListener = (SslCertificateErrorListener) parentActivity;
142 } catch(ClassCastException exception) {
143 throw new ClassCastException(parentActivity.toString() + " must implement SslCertificateErrorListener");
148 public Dialog onCreateDialog(Bundle savedInstanceState) {
149 // Get the activity's layout inflater.
150 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
152 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
153 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
154 dialogBuilder.setTitle(R.string.ssl_certificate_error);
155 // The parent view is `null` because it will be assigned by `AlertDialog`.
156 dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
158 // Set an `onClick` listener on the negative button. `null` doesn't do anything extra when the button is pressed. The `Dialog` will automatically close.
159 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
161 public void onClick(DialogInterface dialog, int which) {
162 sslCertificateErrorListener.onSslErrorCancel();
166 // Set an `onClick` listener on the positive button.
167 dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
169 public void onClick(DialogInterface dialog, int which) {
170 sslCertificateErrorListener.onSslErrorProceed();
175 // Create an `AlertDialog` from the `AlertDialog.Builder`.
176 AlertDialog alertDialog = dialogBuilder.create();
178 // We have to show the `AlertDialog` before we can modify the content.
181 // Get handles for the `TextViews`
182 TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
183 TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
184 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
185 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
186 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
187 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
188 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
189 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
190 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
191 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
193 // Setup the common strings.
194 String urlLabel = getString(R.string.url_label) + " ";
195 String cNameLabel = getString(R.string.common_name) + " ";
196 String oNameLabel = getString(R.string.organization) + " ";
197 String uNameLabel = getString(R.string.organizational_unit) + " ";
198 String startDateLabel = getString(R.string.start_date) + " ";
199 String endDateLabel = getString(R.string.end_date) + " ";
201 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
202 SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
203 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
204 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
205 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
206 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
207 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
208 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
209 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
210 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
212 // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
213 ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue));
215 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
216 urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
217 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
218 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
219 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
220 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
221 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
222 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
223 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
224 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
227 // Display the strings.
228 primaryErrorTextView.setText(primaryError);
229 urlTextView.setText(urlStringBuilder);
230 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
231 issuedToONameTextView.setText(issuedToONameStringBuilder);
232 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
233 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
234 issuedByONameTextView.setText(issuedByONameStringBuilder);
235 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
236 startDateTextView.setText(startDateStringBuilder);
237 endDateTextView.setText(endDateStringBuilder);
239 // `onCreateDialog` requires the return of an `AlertDialog`.