]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/SslCertificateErrorDialog.java
Implement a working two-paned mode for `DomainsActivity`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SslCertificateErrorDialog.java
1 /*
2  * Copyright 2016-2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
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.support.annotation.NonNull;
31 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
32 import android.support.v7.app.AppCompatDialogFragment;
33 import android.text.SpannableStringBuilder;
34 import android.text.Spanned;
35 import android.text.style.ForegroundColorSpan;
36 import android.view.LayoutInflater;
37 import android.widget.TextView;
38
39 import com.stoutner.privacybrowser.R;
40
41 import java.text.DateFormat;
42 import java.util.Date;
43
44 public class SslCertificateErrorDialog extends AppCompatDialogFragment {
45
46     private String primaryError;
47     private String urlWithError;
48     private String issuedToCName;
49     private String issuedToOName;
50     private String issuedToUName;
51     private String issuedByCName;
52     private String issuedByOName;
53     private String issuedByUName;
54     private String startDate;
55     private String endDate;
56
57     public static SslCertificateErrorDialog displayDialog(SslError error) {
58         // Get the various components of the SSL error message.
59         int primaryErrorIntForBundle = error.getPrimaryError();
60         String urlWithErrorForBundle = error.getUrl();
61         SslCertificate sslCertificate = error.getCertificate();
62         String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
63         String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
64         String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
65         String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
66         String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
67         String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
68         Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
69         Date endDateForBundle = sslCertificate.getValidNotAfterDate();
70
71         // Store the SSL error message components in a `Bundle`.
72         Bundle argumentsBundle = new Bundle();
73         argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
74         argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
75         argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
76         argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
77         argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
78         argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
79         argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
80         argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
81         argumentsBundle.putString("StartDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDateForBundle));
82         argumentsBundle.putString("EndDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDateForBundle));
83
84         // Add `argumentsBundle` to this instance of `SslCertificateErrorDialog`.
85         SslCertificateErrorDialog thisSslCertificateErrorDialog = new SslCertificateErrorDialog();
86         thisSslCertificateErrorDialog.setArguments(argumentsBundle);
87         return thisSslCertificateErrorDialog;
88     }
89
90     @Override
91     public void onCreate(Bundle savedInstanceState) {
92         super.onCreate(savedInstanceState);
93
94         // Save the components of the SSL error message in class variables.
95         urlWithError = getArguments().getString("UrlWithError");
96         issuedToCName = getArguments().getString("IssuedToCName");
97         issuedToOName = getArguments().getString("IssuedToOName");
98         issuedToUName = getArguments().getString("IssuedToUName");
99         issuedByCName = getArguments().getString("IssuedByCName");
100         issuedByOName = getArguments().getString("IssuedByOName");
101         issuedByUName = getArguments().getString("IssuedByUName");
102         startDate = getArguments().getString("StartDate");
103         endDate = getArguments().getString("EndDate");
104
105         // Get the appropriate string for `primaryError.
106         int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
107         switch (primaryErrorInt) {
108             case SslError.SSL_NOTYETVALID:
109                 primaryError = getString(R.string.future_certificate);
110                 break;
111
112             case SslError.SSL_EXPIRED:
113                 primaryError = getString(R.string.expired_certificate);
114                 break;
115
116             case SslError.SSL_IDMISMATCH:
117                 primaryError = getString(R.string.cn_mismatch);
118                 break;
119
120             case SslError.SSL_UNTRUSTED:
121                 primaryError = getString(R.string.untrusted);
122                 break;
123
124             case SslError.SSL_DATE_INVALID:
125                 primaryError = getString(R.string.invalid_date);
126                 break;
127
128             case SslError.SSL_INVALID:
129                 primaryError = getString(R.string.invalid_certificate);
130                 break;
131         }
132     }
133
134     // The public interface is used to send information back to the parent activity.
135     public interface SslCertificateErrorListener {
136         void onSslErrorCancel();
137
138         void onSslErrorProceed();
139     }
140
141     // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
142     private SslCertificateErrorListener sslCertificateErrorListener;
143
144     // Check to make sure that the parent activity implements the listener.
145     public void onAttach(Context context) {
146         super.onAttach(context);
147
148         try {
149             sslCertificateErrorListener = (SslCertificateErrorListener) context;
150         } catch(ClassCastException exception) {
151             throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
152         }
153     }
154
155     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
156     @SuppressLint("InflateParams")
157     @Override
158     @NonNull
159     public Dialog onCreateDialog(Bundle savedInstanceState) {
160         // Get the activity's layout inflater.
161         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
162
163         // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
164         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
165         dialogBuilder.setTitle(R.string.ssl_certificate_error);
166         // The parent view is `null` because it will be assigned by `AlertDialog`.
167         dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
168
169         // Set an `onClick` listener on the negative button.  `null` doesn't do anything extra when the button is pressed.  The `Dialog` will automatically close.
170         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
171             @Override
172             public void onClick(DialogInterface dialog, int which) {
173                 sslCertificateErrorListener.onSslErrorCancel();
174             }
175         });
176
177         // Set an `onClick` listener on the positive button.
178         dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
179             @Override
180             public void onClick(DialogInterface dialog, int which) {
181                 sslCertificateErrorListener.onSslErrorProceed();
182             }
183         });
184
185
186         // Create an `AlertDialog` from the `AlertDialog.Builder`.
187         AlertDialog alertDialog = dialogBuilder.create();
188
189         // We have to show the `AlertDialog` before we can modify the content.
190         alertDialog.show();
191
192         // Get handles for the `TextViews`
193         TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
194         TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
195         TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
196         TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
197         TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
198         TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
199         TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
200         TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
201         TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
202         TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
203
204         // Setup the common strings.
205         String urlLabel = getString(R.string.url_label) + "  ";
206         String cNameLabel = getString(R.string.common_name) + "  ";
207         String oNameLabel = getString(R.string.organization) + "  ";
208         String uNameLabel = getString(R.string.organizational_unit) + "  ";
209         String startDateLabel = getString(R.string.start_date) + "  ";
210         String endDateLabel = getString(R.string.end_date) + "  ";
211
212         // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
213         SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
214         SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
215         SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
216         SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
217         SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
218         SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
219         SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
220         SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
221         SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
222
223         // Create a blue `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
224         @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
225
226         // Setup the spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
227         urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
228         issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
229         issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
230         issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
231         issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
232         issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
233         issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
234         startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
235         endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
236
237
238         // Display the strings.
239         primaryErrorTextView.setText(primaryError);
240         urlTextView.setText(urlStringBuilder);
241         issuedToCNameTextView.setText(issuedToCNameStringBuilder);
242         issuedToONameTextView.setText(issuedToONameStringBuilder);
243         issuedToUNameTextView.setText(issuedToUNameStringBuilder);
244         issuedByCNameTextView.setText(issuedByCNameStringBuilder);
245         issuedByONameTextView.setText(issuedByONameStringBuilder);
246         issuedByUNameTextView.setText(issuedByUNameStringBuilder);
247         startDateTextView.setText(startDateStringBuilder);
248         endDateTextView.setText(endDateStringBuilder);
249
250         // `onCreateDialog` requires the return of an `AlertDialog`.
251         return alertDialog;
252     }
253 }