2 * Copyright 2016 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;
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.graphics.drawable.Drawable;
27 import android.net.http.SslCertificate;
28 import android.os.Bundle;
29 import android.text.SpannableStringBuilder;
30 import android.text.Spanned;
31 import android.text.style.ForegroundColorSpan;
32 import android.view.LayoutInflater;
33 import android.widget.TextView;
35 import java.util.Date;
37 public class ViewSslCertificate extends DialogFragment {
38 public Dialog onCreateDialog(Bundle savedInstanceState) {
39 // Get the activity's layout inflater.
40 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
42 // Create a drawable version of the favorite icon.
43 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
45 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
46 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
47 dialogBuilder.setIcon(favoriteIconDrawable);
49 // Set an `onClick` listener on the negative button. Using `null` closes the dialog without doing anything else.
50 dialogBuilder.setNegativeButton(R.string.close, null);
52 // Check to see if the website is encrypted.
53 if (MainWebViewActivity.mainWebView.getCertificate() == null) { // The website is not encrypted.
55 dialogBuilder.setTitle(R.string.unencrypted_website);
57 // Set the Layout. The parent view is `null` because it will be assigned by `AlertDialog`.
58 dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
60 // Create an `AlertDialog` from the `AlertDialog.Builder`
61 final AlertDialog alertDialog = dialogBuilder.create();
63 // Show `alertDialog`.
66 // `onCreateDialog` requires the return of an `AlertDialog`.
69 } else { // Display the SSL certificate information
71 dialogBuilder.setTitle(R.string.ssl_certificate);
73 // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`.
74 dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
76 // Create an `AlertDialog` from the `AlertDialog.Builder`
77 final AlertDialog alertDialog = dialogBuilder.create();
79 // We need to show the `AlertDialog` before we can modify items in the layout.
82 // Get handles for the `TextViews`.
83 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname);
84 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname);
85 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname);
86 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname);
87 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname);
88 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname);
89 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date);
90 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date);
93 String cNameLabel = getString(R.string.common_name) + " ";
94 String oNameLabel = getString(R.string.organization) + " ";
95 String uNameLabel = getString(R.string.organizational_unit) + " ";
96 String startDateLabel = getString(R.string.start_date) + " ";
97 String endDateLabel = getString(R.string.end_date) + " ";
99 // Get the SSL certificate.
100 SslCertificate sslCertificate = MainWebViewActivity.mainWebView.getCertificate();
102 // Get the strings from the SSL certificate.
103 String issuedToCNameString = sslCertificate.getIssuedTo().getCName();
104 String issuedToONameString = sslCertificate.getIssuedTo().getOName();
105 String issuedToUNameString = sslCertificate.getIssuedTo().getUName();
106 String issuedByCNameString = sslCertificate.getIssuedBy().getCName();
107 String issuedByONameString = sslCertificate.getIssuedBy().getOName();
108 String issuedByUNameString = sslCertificate.getIssuedBy().getUName();
109 Date startDate = sslCertificate.getValidNotBeforeDate();
110 Date endDate = sslCertificate.getValidNotAfterDate();
112 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
113 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCNameString);
114 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToONameString);
115 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUNameString);
116 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCNameString);
117 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByONameString);
118 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUNameString);
119 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate.toString());
120 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + endDate.toString());
122 // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
123 ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
125 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
126 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
127 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
128 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
129 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
130 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
131 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
132 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
133 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
135 // Display the strings.
136 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
137 issuedToONameTextView.setText(issuedToONameStringBuilder);
138 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
139 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
140 issuedByONameTextView.setText(issuedByONameStringBuilder);
141 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
142 startDateTextView.setText(startDateStringBuilder);
143 endDateTextView.setText(endDateStringBuilder);
145 // `onCreateDialog` requires the return of an `AlertDialog`.