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