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