2 * Copyright © 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.content.Context;
26 import android.content.DialogInterface;
27 import android.net.http.SslCertificate;
28 import android.os.Bundle;
29 import android.support.annotation.NonNull;
30 import android.support.design.widget.TabLayout;
31 import android.support.v4.view.PagerAdapter;
32 // `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22.
33 import android.support.v7.app.AppCompatDialogFragment;
34 import android.text.SpannableStringBuilder;
35 import android.text.Spanned;
36 import android.text.style.ForegroundColorSpan;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.TextView;
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
44 import com.stoutner.privacybrowser.definitions.WrapVerticalContentViewPager;
45 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
47 import java.text.DateFormat;
48 import java.util.Date;
50 public class PinnedSslCertificateMismatchDialog extends AppCompatDialogFragment {
51 // `layoutInflater` is used in `onCreateDialog()` and `pagerAdapter`.
52 private LayoutInflater layoutInflater;
54 // The current website SSL certificate variables are used in `onCreateDialog()` and `pagerAdapter()`.
55 private String currentSslIssuedToCNameString;
56 private String currentSslIssuedToONameString;
57 private String currentSslIssuedToUNameString;
58 private String currentSslIssuedByCNameString;
59 private String currentSslIssuedByONameString;
60 private String currentSslIssuedByUNameString;
61 private Date currentSslStartDate;
62 private Date currentSslEndDate;
64 // The public interface is used to send information back to the parent activity.
65 public interface PinnedSslCertificateMismatchListener {
66 void onSslMismatchBack();
68 void onSslMismatchProceed();
71 // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
72 private PinnedSslCertificateMismatchDialog.PinnedSslCertificateMismatchListener pinnedSslCertificateMismatchListener;
74 // Check to make sure that the parent activity implements the listener.
75 public void onAttach(Context context) {
76 super.onAttach(context);
79 pinnedSslCertificateMismatchListener = (PinnedSslCertificateMismatchDialog.PinnedSslCertificateMismatchListener) context;
80 } catch(ClassCastException exception) {
81 throw new ClassCastException(context.toString() + " must implement PinnedSslCertificateMismatchListener");
85 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
86 @SuppressLint("InflateParams")
89 public Dialog onCreateDialog(Bundle savedInstanceState) {
90 // Get the activity's layout inflater.
91 layoutInflater = getActivity().getLayoutInflater();
93 // Use `AlertDialog.Builder` to create the `AlertDialog`.
94 AlertDialog.Builder dialogBuilder;
96 // Set the style according to the theme.
97 if (MainWebViewActivity.darkTheme) {
98 // Set the dialog theme.
99 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
102 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
104 // Set the dialog theme.
105 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
108 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
111 // Setup the neutral button.
112 dialogBuilder.setNeutralButton(R.string.update_ssl, new DialogInterface.OnClickListener() {
114 public void onClick(DialogInterface dialog, int which) {
115 // Initialize the `long` date variables. If the date is `null`, a long value of `0` will be stored in the Domains database entry.
116 long currentSslStartDateLong = 0;
117 long currentSslEndDateLong = 0;
119 // Convert the `Dates` into `longs`.
120 if (currentSslStartDate != null) {
121 currentSslStartDateLong = currentSslStartDate.getTime();
124 if (currentSslEndDate != null) {
125 currentSslEndDateLong = currentSslEndDate.getTime();
128 // Initialize the database handler. The two `nulls` do not specify the database name or a `CursorFactory`. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
129 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
131 // Update the pinned SSL certificate for this domain.
132 domainsDatabaseHelper.updateCertificate(MainWebViewActivity.domainSettingsDatabaseId, currentSslIssuedToCNameString, currentSslIssuedToONameString, currentSslIssuedToUNameString, currentSslIssuedByCNameString, currentSslIssuedByONameString,
133 currentSslIssuedByUNameString, currentSslStartDateLong, currentSslEndDateLong);
135 // Update the pinned SSL certificate global variables to match the information that is now in the database.
136 MainWebViewActivity.pinnedDomainSslIssuedToCNameString = currentSslIssuedToCNameString;
137 MainWebViewActivity.pinnedDomainSslIssuedToONameString = currentSslIssuedToONameString;
138 MainWebViewActivity.pinnedDomainSslIssuedToUNameString = currentSslIssuedToUNameString;
139 MainWebViewActivity.pinnedDomainSslIssuedByCNameString = currentSslIssuedByCNameString;
140 MainWebViewActivity.pinnedDomainSslIssuedByONameString = currentSslIssuedByONameString;
141 MainWebViewActivity.pinnedDomainSslIssuedByUNameString = currentSslIssuedByUNameString;
142 MainWebViewActivity.pinnedDomainSslStartDate = currentSslStartDate;
143 MainWebViewActivity.pinnedDomainSslEndDate = currentSslEndDate;
147 // Setup the negative button.
148 dialogBuilder.setNegativeButton(R.string.back, new DialogInterface.OnClickListener() {
150 public void onClick(DialogInterface dialog, int which) {
151 // Call the `onSslMismatchBack` public interface to send the `WebView` back one page.
152 pinnedSslCertificateMismatchListener.onSslMismatchBack();
156 // Setup the positive button.
157 dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
159 public void onClick(DialogInterface dialog, int which) {
160 // Call the `onSslMismatchProceed` public interface.
161 pinnedSslCertificateMismatchListener.onSslMismatchProceed();
166 dialogBuilder.setTitle(R.string.ssl_certificate_mismatch);
168 // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`.
169 dialogBuilder.setView(layoutInflater.inflate(R.layout.pinned_ssl_certificate_mismatch_linearlayout, null));
171 // Create an `AlertDialog` from the `AlertDialog.Builder`
172 final AlertDialog alertDialog = dialogBuilder.create();
174 // Show the `AlertDialog` so the items in the layout can be modified.
177 // Setup `wrapVerticalContentViewPager`.
178 WrapVerticalContentViewPager wrapVerticalContentViewPager = (WrapVerticalContentViewPager) alertDialog.findViewById(R.id.pinned_ssl_certificate_mismatch_viewpager);
179 wrapVerticalContentViewPager.setAdapter(new pagerAdapter());
181 // Setup the `TabLayout` and connect it to the `WrapVerticalContentViewPager`.
182 TabLayout tabLayout = (TabLayout) alertDialog.findViewById(R.id.pinned_ssl_certificate_mismatch_tablayout);
183 tabLayout.setupWithViewPager(wrapVerticalContentViewPager);
185 // `onCreateDialog` requires the return of an `AlertDialog`.
189 private class pagerAdapter extends PagerAdapter {
191 public boolean isViewFromObject(View view, Object object) {
192 // Check to see if the `View` and the `Object` are the same.
193 return (view == object);
197 public int getCount() {
198 // There are two tabs.
203 public CharSequence getPageTitle(int position) {
204 // Return the current tab title.
205 if (position == 0) { // The current SSL certificate tab.
206 return getString(R.string.current_ssl);
207 } else { // The pinned SSL certificate tab.
208 return getString(R.string.pinned_ssl);
213 public Object instantiateItem(ViewGroup container, int position) {
214 // Inflate the `ScrollView` for this tab.
215 ViewGroup tabViewGroup = (ViewGroup) layoutInflater.inflate(R.layout.pinned_ssl_certificate_mismatch_scrollview, container, false);
217 // Get handles for the `TextViews`.
218 TextView issuedToCNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_to_cname);
219 TextView issuedToONameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_to_oname);
220 TextView issuedToUNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_to_uname);
221 TextView issuedByCNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_by_cname);
222 TextView issuedByONameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_by_oname);
223 TextView issuedByUNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_by_uname);
224 TextView startDateTextView = (TextView) tabViewGroup.findViewById(R.id.start_date);
225 TextView endDateTextView = (TextView) tabViewGroup.findViewById(R.id.end_date);
228 String cNameLabel = getString(R.string.common_name) + " ";
229 String oNameLabel = getString(R.string.organization) + " ";
230 String uNameLabel = getString(R.string.organizational_unit) + " ";
231 String startDateLabel = getString(R.string.start_date) + " ";
232 String endDateLabel = getString(R.string.end_date) + " ";
234 // Get the current website SSL certificate.
235 SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
237 // Extract the individual pieces of information from the current website SSL certificate if it is not null.
238 if (sslCertificate != null) {
239 currentSslIssuedToCNameString = sslCertificate.getIssuedTo().getCName();
240 currentSslIssuedToONameString = sslCertificate.getIssuedTo().getOName();
241 currentSslIssuedToUNameString = sslCertificate.getIssuedTo().getUName();
242 currentSslIssuedByCNameString = sslCertificate.getIssuedBy().getCName();
243 currentSslIssuedByONameString = sslCertificate.getIssuedBy().getOName();
244 currentSslIssuedByUNameString = sslCertificate.getIssuedBy().getUName();
245 currentSslStartDate = sslCertificate.getValidNotBeforeDate();
246 currentSslEndDate = sslCertificate.getValidNotAfterDate();
248 // Initialize the current website SSL certificate variables with blank information.
249 currentSslIssuedToCNameString = "";
250 currentSslIssuedToONameString = "";
251 currentSslIssuedToUNameString = "";
252 currentSslIssuedByCNameString = "";
253 currentSslIssuedByONameString = "";
254 currentSslIssuedByUNameString = "";
257 // Initialize the `SpannableStringBuilders`.
258 SpannableStringBuilder issuedToCNameStringBuilder;
259 SpannableStringBuilder issuedToONameStringBuilder;
260 SpannableStringBuilder issuedToUNameStringBuilder;
261 SpannableStringBuilder issuedByCNameStringBuilder;
262 SpannableStringBuilder issuedByONameStringBuilder;
263 SpannableStringBuilder issuedByUNameStringBuilder;
264 SpannableStringBuilder startDateStringBuilder;
265 SpannableStringBuilder endDateStringBuilder;
267 // Setup the `SpannableStringBuilders` for each tab.
268 if (position == 0) { // Setup the current SSL certificate tab.
269 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedToCNameString);
270 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedToONameString);
271 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedToUNameString);
272 issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedByCNameString);
273 issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedByONameString);
274 issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedByUNameString);
276 // Set the dates if they aren't `null`.
277 if (currentSslStartDate == null) {
278 startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
280 startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(currentSslStartDate));
283 if (currentSslEndDate == null) {
284 endDateStringBuilder = new SpannableStringBuilder(endDateLabel);
286 endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(currentSslEndDate));
288 } else { // Setup the pinned SSL certificate tab.
289 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToCNameString);
290 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToONameString);
291 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToUNameString);
292 issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByCNameString);
293 issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByONameString);
294 issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByUNameString);
296 // Set the dates if they aren't `null`.
297 if (MainWebViewActivity.pinnedDomainSslStartDate == null) {
298 startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
300 startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(MainWebViewActivity.pinnedDomainSslStartDate));
303 if (MainWebViewActivity.pinnedDomainSslEndDate == null) {
304 endDateStringBuilder = new SpannableStringBuilder(endDateLabel);
306 endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(MainWebViewActivity.pinnedDomainSslEndDate));
310 // Create a red `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
311 @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
313 // Create a blue `ForegroundColorSpan`.
314 ForegroundColorSpan blueColorSpan;
316 // Set `blueColorSpan` according to the theme. We have to use the deprecated `getColor()` until API >= 23.
317 if (MainWebViewActivity.darkTheme) {
318 //noinspection deprecation
319 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
321 //noinspection deprecation
322 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
325 // Configure the spans to display conflicting information in red. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
326 if (currentSslIssuedToCNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedToCNameString)) {
327 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
329 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
332 if (currentSslIssuedToONameString.equals(MainWebViewActivity.pinnedDomainSslIssuedToONameString)) {
333 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
335 issuedToONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
338 if (currentSslIssuedToUNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedToUNameString)) {
339 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
341 issuedToUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
344 if (currentSslIssuedByCNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedByCNameString)) {
345 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
347 issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
350 if (currentSslIssuedByONameString.equals(MainWebViewActivity.pinnedDomainSslIssuedByONameString)) {
351 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
353 issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
356 if (currentSslIssuedByUNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedByUNameString)) {
357 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
359 issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
362 if ((currentSslStartDate != null) && (MainWebViewActivity.pinnedDomainSslStartDate != null) && currentSslStartDate.equals(MainWebViewActivity.pinnedDomainSslStartDate)) {
363 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
365 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
368 if ((currentSslEndDate != null) && (MainWebViewActivity.pinnedDomainSslEndDate != null) && currentSslEndDate.equals(MainWebViewActivity.pinnedDomainSslEndDate)) {
369 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
371 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
374 // Display the strings.
375 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
376 issuedToONameTextView.setText(issuedToONameStringBuilder);
377 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
378 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
379 issuedByONameTextView.setText(issuedByONameStringBuilder);
380 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
381 startDateTextView.setText(startDateStringBuilder);
382 endDateTextView.setText(endDateStringBuilder);
385 container.addView(tabViewGroup);