]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewSslCertificateDialog.java
Reset the favorite icon and the `WebView` title when a new page loads. Implements...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewSslCertificateDialog.java
1 /*
2  * Copyright 2016-2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
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;
35
36 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
37 import com.stoutner.privacybrowser.R;
38
39 import java.text.DateFormat;
40 import java.util.Date;
41
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();
48
49         // Create a drawable version of the favorite icon.
50         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
51
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);
55
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);
58
59         // Check to see if the website is encrypted.
60         if (MainWebViewActivity.sslCertificate == null) {  // The website is not encrypted.
61             // Set the title.
62             dialogBuilder.setTitle(R.string.unencrypted_website);
63
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));
66
67             // Create an `AlertDialog` from the `AlertDialog.Builder`
68             final AlertDialog alertDialog = dialogBuilder.create();
69
70             // Show `alertDialog`.
71             alertDialog.show();
72
73             // `onCreateDialog` requires the return of an `AlertDialog`.
74             return alertDialog;
75
76         } else {  // Display the SSL certificate information
77             // Set the title.
78             dialogBuilder.setTitle(R.string.ssl_certificate);
79
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));
82
83             // Create an `AlertDialog` from the `AlertDialog.Builder`
84             final AlertDialog alertDialog = dialogBuilder.create();
85
86             // We need to show the `AlertDialog` before we can modify items in the layout.
87             alertDialog.show();
88
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);
98
99             // Setup the labels.
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) + "  ";
105
106             // Get the SSL certificate.
107             SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
108
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();
118
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));
128
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));
131
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);
141
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);
151
152             // `onCreateDialog` requires the return of an `AlertDialog`.
153             return alertDialog;
154         }
155     }
156 }