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