2 * Copyright © 2016-2017 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.dialogs;
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.app.DialogFragment;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.net.http.SslCertificate;
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 com.stoutner.privacybrowser.activities.MainWebViewActivity;
37 import com.stoutner.privacybrowser.R;
39 import java.text.DateFormat;
40 import java.util.Calendar;
41 import java.util.Date;
43 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
44 @SuppressLint("InflateParams")
45 public class ViewSslCertificateDialog extends DialogFragment {
46 public Dialog onCreateDialog(Bundle savedInstanceState) {
47 // Get the activity's layout inflater.
48 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
50 // Create a drawable version of the favorite icon.
51 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
53 // Use `AlertDialog.Builder` to create the `AlertDialog`.
54 AlertDialog.Builder dialogBuilder;
56 // Set the style according to the theme.
57 if (MainWebViewActivity.darkTheme) {
58 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
60 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
64 dialogBuilder.setIcon(favoriteIconDrawable);
66 // Set an `onClick` listener on the negative button. Using `null` closes the dialog without doing anything else.
67 dialogBuilder.setNegativeButton(R.string.close, null);
69 // Check to see if the website is encrypted.
70 if (MainWebViewActivity.sslCertificate == null) { // The website is not encrypted.
72 dialogBuilder.setTitle(R.string.unencrypted_website);
74 // Set the Layout. The parent view is `null` because it will be assigned by `AlertDialog`.
75 dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
77 // Create an `AlertDialog` from the `AlertDialog.Builder`
78 final AlertDialog alertDialog = dialogBuilder.create();
80 // Show `alertDialog`.
83 // `onCreateDialog` requires the return of an `AlertDialog`.
86 } else { // Display the SSL certificate information
88 dialogBuilder.setTitle(R.string.ssl_certificate);
90 // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`.
91 dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
93 // Create an `AlertDialog` from the `AlertDialog.Builder`
94 final AlertDialog alertDialog = dialogBuilder.create();
96 // We need to show the `AlertDialog` before we can modify items in the layout.
99 // Get handles for the `TextViews`.
100 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname);
101 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname);
102 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname);
103 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname);
104 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname);
105 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname);
106 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date);
107 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date);
110 String cNameLabel = getString(R.string.common_name) + " ";
111 String oNameLabel = getString(R.string.organization) + " ";
112 String uNameLabel = getString(R.string.organizational_unit) + " ";
113 String startDateLabel = getString(R.string.start_date) + " ";
114 String endDateLabel = getString(R.string.end_date) + " ";
116 // Get the SSL certificate.
117 SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
119 // Get the strings from the SSL certificate.
120 String issuedToCNameString = sslCertificate.getIssuedTo().getCName();
121 String issuedToONameString = sslCertificate.getIssuedTo().getOName();
122 String issuedToUNameString = sslCertificate.getIssuedTo().getUName();
123 String issuedByCNameString = sslCertificate.getIssuedBy().getCName();
124 String issuedByONameString = sslCertificate.getIssuedBy().getOName();
125 String issuedByUNameString = sslCertificate.getIssuedBy().getUName();
126 Date startDate = sslCertificate.getValidNotBeforeDate();
127 Date endDate = sslCertificate.getValidNotAfterDate();
129 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
130 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCNameString);
131 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToONameString);
132 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUNameString);
133 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCNameString);
134 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByONameString);
135 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUNameString);
136 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
137 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
139 // Create a red `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
140 @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
142 // Create a blue `ForegroundColorSpan`.
143 ForegroundColorSpan blueColorSpan;
145 // Set `blueColorSpan` according to the theme. We have to use the deprecated `getColor()` until API >= 23.
146 if (MainWebViewActivity.darkTheme) {
147 //noinspection deprecation
148 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
150 //noinspection deprecation
151 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
154 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
155 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
156 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
157 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
158 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
159 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
160 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
162 Date currentDate = Calendar.getInstance().getTime();
164 // Format the start date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
165 if (startDate.after(currentDate)) { // The certificate start date is in the future.
166 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
167 } else { // The certificate start date is in the past.
168 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
171 // Format the end date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
172 if (endDate.before(currentDate)) { // The certificate end date is in the past.
173 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
174 } else { // The certificate end date is in the future.
175 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
178 // Display the strings.
179 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
180 issuedToONameTextView.setText(issuedToONameStringBuilder);
181 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
182 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
183 issuedByONameTextView.setText(issuedByONameStringBuilder);
184 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
185 startDateTextView.setText(startDateStringBuilder);
186 endDateTextView.setText(endDateStringBuilder);
188 // `onCreateDialog` requires the return of an `AlertDialog`.