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.Date;
42 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
43 @SuppressLint("InflateParams")
44 public class ViewSslCertificateDialog extends DialogFragment {
45 public Dialog onCreateDialog(Bundle savedInstanceState) {
46 // Get the activity's layout inflater.
47 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
49 // Create a drawable version of the favorite icon.
50 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
52 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
53 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
54 dialogBuilder.setIcon(favoriteIconDrawable);
56 // Set an `onClick` listener on the negative button. Using `null` closes the dialog without doing anything else.
57 dialogBuilder.setNegativeButton(R.string.close, null);
59 // Check to see if the website is encrypted.
60 if (MainWebViewActivity.sslCertificate == null) { // The website is not encrypted.
62 dialogBuilder.setTitle(R.string.unencrypted_website);
64 // Set the Layout. The parent view is `null` because it will be assigned by `AlertDialog`.
65 dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
67 // Create an `AlertDialog` from the `AlertDialog.Builder`
68 final AlertDialog alertDialog = dialogBuilder.create();
70 // Show `alertDialog`.
73 // `onCreateDialog` requires the return of an `AlertDialog`.
76 } else { // Display the SSL certificate information
78 dialogBuilder.setTitle(R.string.ssl_certificate);
80 // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`.
81 dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
83 // Create an `AlertDialog` from the `AlertDialog.Builder`
84 final AlertDialog alertDialog = dialogBuilder.create();
86 // We need to show the `AlertDialog` before we can modify items in the layout.
89 // Get handles for the `TextViews`.
90 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname);
91 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname);
92 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname);
93 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname);
94 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname);
95 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname);
96 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date);
97 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date);
100 String cNameLabel = getString(R.string.common_name) + " ";
101 String oNameLabel = getString(R.string.organization) + " ";
102 String uNameLabel = getString(R.string.organizational_unit) + " ";
103 String startDateLabel = getString(R.string.start_date) + " ";
104 String endDateLabel = getString(R.string.end_date) + " ";
106 // Get the SSL certificate.
107 SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
109 // Get the strings from the SSL certificate.
110 String issuedToCNameString = sslCertificate.getIssuedTo().getCName();
111 String issuedToONameString = sslCertificate.getIssuedTo().getOName();
112 String issuedToUNameString = sslCertificate.getIssuedTo().getUName();
113 String issuedByCNameString = sslCertificate.getIssuedBy().getCName();
114 String issuedByONameString = sslCertificate.getIssuedBy().getOName();
115 String issuedByUNameString = sslCertificate.getIssuedBy().getUName();
116 Date startDate = sslCertificate.getValidNotBeforeDate();
117 Date endDate = sslCertificate.getValidNotAfterDate();
119 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
120 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCNameString);
121 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToONameString);
122 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUNameString);
123 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCNameString);
124 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByONameString);
125 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUNameString);
126 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
127 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
129 // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
130 @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
132 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
133 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
134 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
135 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
136 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
137 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
138 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
139 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
140 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
142 // Display the strings.
143 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
144 issuedToONameTextView.setText(issuedToONameStringBuilder);
145 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
146 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
147 issuedByONameTextView.setText(issuedByONameStringBuilder);
148 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
149 startDateTextView.setText(startDateStringBuilder);
150 endDateTextView.setText(endDateStringBuilder);
152 // `onCreateDialog` requires the return of an `AlertDialog`.