Create a dark theme for the `AlertDialogs`.
[PrivacyBrowser.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.Calendar;
41 import java.util.Date;
42
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();
49
50         // Create a drawable version of the favorite icon.
51         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
52
53         // Use `AlertDialog.Builder` to create the `AlertDialog`.
54         AlertDialog.Builder dialogBuilder;
55
56         // Set the style according to the theme.
57         if (MainWebViewActivity.darkTheme) {
58             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
59         } else {
60             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
61         }
62
63         // Set the icon.
64         dialogBuilder.setIcon(favoriteIconDrawable);
65
66         // Set an `onClick` listener on the negative button.  Using `null` closes the dialog without doing anything else.
67         dialogBuilder.setNegativeButton(R.string.close, null);
68
69         // Check to see if the website is encrypted.
70         if (MainWebViewActivity.sslCertificate == null) {  // The website is not encrypted.
71             // Set the title.
72             dialogBuilder.setTitle(R.string.unencrypted_website);
73
74             // Set the Layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
75             dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
76
77             // Create an `AlertDialog` from the `AlertDialog.Builder`
78             final AlertDialog alertDialog = dialogBuilder.create();
79
80             // Show `alertDialog`.
81             alertDialog.show();
82
83             // `onCreateDialog` requires the return of an `AlertDialog`.
84             return alertDialog;
85
86         } else {  // Display the SSL certificate information
87             // Set the title.
88             dialogBuilder.setTitle(R.string.ssl_certificate);
89
90             // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
91             dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
92
93             // Create an `AlertDialog` from the `AlertDialog.Builder`
94             final AlertDialog alertDialog = dialogBuilder.create();
95
96             // We need to show the `AlertDialog` before we can modify items in the layout.
97             alertDialog.show();
98
99             // Get handles for the `TextViews`.
100             TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname);
101             TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname);
102             TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname);
103             TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname);
104             TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname);
105             TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname);
106             TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date);
107             TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date);
108
109             // Setup the labels.
110             String cNameLabel = getString(R.string.common_name) + "  ";
111             String oNameLabel = getString(R.string.organization) + "  ";
112             String uNameLabel = getString(R.string.organizational_unit) + "  ";
113             String startDateLabel = getString(R.string.start_date) + "  ";
114             String endDateLabel = getString(R.string.end_date) + "  ";
115
116             // Get the SSL certificate.
117             SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
118
119             // Get the strings from the SSL certificate.
120             String issuedToCNameString = sslCertificate.getIssuedTo().getCName();
121             String issuedToONameString = sslCertificate.getIssuedTo().getOName();
122             String issuedToUNameString = sslCertificate.getIssuedTo().getUName();
123             String issuedByCNameString = sslCertificate.getIssuedBy().getCName();
124             String issuedByONameString = sslCertificate.getIssuedBy().getOName();
125             String issuedByUNameString = sslCertificate.getIssuedBy().getUName();
126             Date startDate = sslCertificate.getValidNotBeforeDate();
127             Date endDate = sslCertificate.getValidNotAfterDate();
128
129             // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
130             SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCNameString);
131             SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToONameString);
132             SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUNameString);
133             SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCNameString);
134             SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByONameString);
135             SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUNameString);
136             SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
137             SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
138
139             // Create a red `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
140             @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
141
142             // Create a blue `ForegroundColorSpan`.
143             ForegroundColorSpan blueColorSpan;
144
145             // Set `blueColorSpan` according to the theme.  We have to use the deprecated `getColor()` until API >= 23.
146             if (MainWebViewActivity.darkTheme) {
147                 //noinspection deprecation
148                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
149             } else {
150                 //noinspection deprecation
151                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
152             }
153
154             // Setup the spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
155             issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
156             issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
157             issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
158             issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
159             issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
160             issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
161
162             Date currentDate = Calendar.getInstance().getTime();
163
164             //  Format the start date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
165             if (startDate.after(currentDate)) {  // The certificate start date is in the future.
166                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
167             } else {  // The certificate start date is in the past.
168                 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
169             }
170
171             // Format the end date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
172             if (endDate.before(currentDate)) {  // The certificate end date is in the past.
173                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
174             } else {  // The certificate end date is in the future.
175                 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
176             }
177
178             // Display the strings.
179             issuedToCNameTextView.setText(issuedToCNameStringBuilder);
180             issuedToONameTextView.setText(issuedToONameStringBuilder);
181             issuedToUNameTextView.setText(issuedToUNameStringBuilder);
182             issuedByCNameTextView.setText(issuedByCNameStringBuilder);
183             issuedByONameTextView.setText(issuedByONameStringBuilder);
184             issuedByUNameTextView.setText(issuedByUNameStringBuilder);
185             startDateTextView.setText(startDateStringBuilder);
186             endDateTextView.setText(endDateStringBuilder);
187
188             // `onCreateDialog` requires the return of an `AlertDialog`.
189             return alertDialog;
190         }
191     }
192 }