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`. `R.style.LightAlertDialog` formats the color of the button text.
54 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
55 dialogBuilder.setIcon(favoriteIconDrawable);
57 // Set an `onClick` listener on the negative button. Using `null` closes the dialog without doing anything else.
58 dialogBuilder.setNegativeButton(R.string.close, null);
60 // Check to see if the website is encrypted.
61 if (MainWebViewActivity.sslCertificate == null) { // The website is not encrypted.
63 dialogBuilder.setTitle(R.string.unencrypted_website);
65 // Set the Layout. The parent view is `null` because it will be assigned by `AlertDialog`.
66 dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
68 // Create an `AlertDialog` from the `AlertDialog.Builder`
69 final AlertDialog alertDialog = dialogBuilder.create();
71 // Show `alertDialog`.
74 // `onCreateDialog` requires the return of an `AlertDialog`.
77 } else { // Display the SSL certificate information
79 dialogBuilder.setTitle(R.string.ssl_certificate);
81 // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`.
82 dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
84 // Create an `AlertDialog` from the `AlertDialog.Builder`
85 final AlertDialog alertDialog = dialogBuilder.create();
87 // We need to show the `AlertDialog` before we can modify items in the layout.
90 // Get handles for the `TextViews`.
91 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname);
92 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname);
93 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname);
94 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname);
95 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname);
96 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname);
97 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date);
98 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date);
101 String cNameLabel = getString(R.string.common_name) + " ";
102 String oNameLabel = getString(R.string.organization) + " ";
103 String uNameLabel = getString(R.string.organizational_unit) + " ";
104 String startDateLabel = getString(R.string.start_date) + " ";
105 String endDateLabel = getString(R.string.end_date) + " ";
107 // Get the SSL certificate.
108 SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
110 // Get the strings from the SSL certificate.
111 String issuedToCNameString = sslCertificate.getIssuedTo().getCName();
112 String issuedToONameString = sslCertificate.getIssuedTo().getOName();
113 String issuedToUNameString = sslCertificate.getIssuedTo().getUName();
114 String issuedByCNameString = sslCertificate.getIssuedBy().getCName();
115 String issuedByONameString = sslCertificate.getIssuedBy().getOName();
116 String issuedByUNameString = sslCertificate.getIssuedBy().getUName();
117 Date startDate = sslCertificate.getValidNotBeforeDate();
118 Date endDate = sslCertificate.getValidNotAfterDate();
120 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
121 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCNameString);
122 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToONameString);
123 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUNameString);
124 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCNameString);
125 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByONameString);
126 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUNameString);
127 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
128 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
130 // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
131 @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
132 @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
134 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
135 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
136 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
137 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
138 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
139 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
140 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
142 Date currentDate = Calendar.getInstance().getTime();
144 // Format the start date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
145 if (startDate.after(currentDate)) { // The certificate start date is in the future.
146 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
147 } else { // The certificate start date is in the past.
148 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
151 // Format the end date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
152 if (endDate.before(currentDate)) { // The certificate end date is in the past.
153 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
154 } else { // The certificate end date is in the future.
155 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
158 // Display the strings.
159 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
160 issuedToONameTextView.setText(issuedToONameStringBuilder);
161 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
162 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
163 issuedByONameTextView.setText(issuedByONameStringBuilder);
164 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
165 startDateTextView.setText(startDateStringBuilder);
166 endDateTextView.setText(endDateStringBuilder);
168 // `onCreateDialog` requires the return of an `AlertDialog`.