Refactor style code to use attrs.xml. Create a dark theme for Find on Page.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / AboutTabFragment.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.fragments;
21
22 import android.annotation.SuppressLint;
23 import android.content.pm.PackageManager;
24 import android.content.pm.Signature;
25 import android.graphics.ColorMatrixColorFilter;
26 import android.graphics.Paint;
27 import android.os.Build;
28 import android.os.Bundle;
29 import android.support.v4.app.Fragment;
30 import android.text.SpannableStringBuilder;
31 import android.text.Spanned;
32 import android.text.style.ForegroundColorSpan;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.webkit.WebView;
37 import android.widget.TextView;
38
39 import com.stoutner.privacybrowser.BuildConfig;
40 import com.stoutner.privacybrowser.R;
41 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
42
43 import java.io.ByteArrayInputStream;
44 import java.io.InputStream;
45 import java.math.BigInteger;
46 import java.security.Principal;
47 import java.security.cert.CertificateException;
48 import java.security.cert.CertificateFactory;
49 import java.security.cert.X509Certificate;
50 import java.text.DateFormat;
51 import java.util.Date;
52
53 public class AboutTabFragment extends Fragment {
54     private int tabNumber;
55
56     // `AboutTabFragment.createTab` stores the tab number in the bundle arguments so it can be referenced from `onCreate()`.
57     public static AboutTabFragment createTab(int tab) {
58         Bundle thisTabArguments = new Bundle();
59         thisTabArguments.putInt("Tab", tab);
60
61         AboutTabFragment thisTab = new AboutTabFragment();
62         thisTab.setArguments(thisTabArguments);
63         return thisTab;
64     }
65
66     @Override
67     public void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69
70         // Store the tab number in `tabNumber`.
71         tabNumber = getArguments().getInt("Tab");
72     }
73
74     @Override
75     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
76         View tabLayout;
77
78         // Load the tabs.  Tab numbers start at 0.
79         if (tabNumber == 0) {  // Load the about tab.
80             // Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.  The fragment will take care of attaching the root automatically.
81             tabLayout = inflater.inflate(R.layout.about_tab_version, container, false);
82
83             // Get handles for the `TextViews`.
84             TextView versionNumberTextView = (TextView) tabLayout.findViewById(R.id.about_version_number);
85             TextView versionBrandTextView = (TextView) tabLayout.findViewById(R.id.about_version_brand);
86             TextView versionManufacturerTextView = (TextView) tabLayout.findViewById(R.id.about_version_manufacturer);
87             TextView versionModelTextView = (TextView) tabLayout.findViewById(R.id.about_version_model);
88             TextView versionDeviceTextView = (TextView) tabLayout.findViewById(R.id.about_version_device);
89             TextView versionBootloaderTextView = (TextView) tabLayout.findViewById(R.id.about_version_bootloader);
90             TextView versionRadioTextView = (TextView) tabLayout.findViewById(R.id.about_version_radio);
91             TextView versionAndroidTextView = (TextView) tabLayout.findViewById(R.id.about_version_android);
92             TextView versionBuildTextView = (TextView) tabLayout.findViewById(R.id.about_version_build);
93             TextView versionSecurityPatchTextView = (TextView) tabLayout.findViewById(R.id.about_version_securitypatch);
94             TextView versionWebKitTextView = (TextView) tabLayout.findViewById(R.id.about_version_webkit);
95             TextView versionChromeTextView = (TextView) tabLayout.findViewById(R.id.about_version_chrome);
96             TextView versionOrbotTextView = (TextView) tabLayout.findViewById(R.id.about_version_orbot);
97             TextView certificateIssuerDNTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_issuer_dn);
98             TextView certificateSubjectDNTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_subject_dn);
99             TextView certificateStartDateTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_start_date);
100             TextView certificateEndDateTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_end_date);
101             TextView certificateVersionTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_version);
102             TextView certificateSerialNumberTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_serial_number);
103             TextView certificateSignatureAlgorithmTextView = (TextView) tabLayout.findViewById(R.id.about_version_certificate_signature_algorithm);
104
105             // Setup the labels.
106             String version = getString(R.string.version) + " " + BuildConfig.VERSION_NAME + " (" + getString(R.string.version_code) + " " + Integer.toString(BuildConfig.VERSION_CODE) + ")";
107             String brandLabel = getString(R.string.brand) + "  ";
108             String manufacturerLabel = getString(R.string.manufacturer) + "  ";
109             String modelLabel = getString(R.string.model) + "  ";
110             String deviceLabel = getString(R.string.device) + "  ";
111             String bootloaderLabel = getString(R.string.bootloader) + "  ";
112             String androidLabel = getString(R.string.android) + "  ";
113             String buildLabel = getString(R.string.build) + "  ";
114             String webKitLabel = getString(R.string.webkit) + "  ";
115             String chromeLabel = getString(R.string.chrome) + "  ";
116             String issuerDNLabel = getString(R.string.issuer_dn) + "  ";
117             String subjectDNLabel = getString(R.string.subject_dn) + "  ";
118             String startDateLabel = getString(R.string.start_date) + "  ";
119             String endDateLabel = getString(R.string.end_date) + "  ";
120             String certificateVersionLabel = getString(R.string.certificate_version) + "  ";
121             String serialNumberLabel = getString(R.string.serial_number) + "  ";
122             String signatureAlgorithmLabel = getString(R.string.signature_algorithm) + "  ";
123
124             // `webViewLayout` is only used to get the default user agent from `bare_webview`.  It is not used to render content on the screen.
125             View webViewLayout = inflater.inflate(R.layout.bare_webview, container, false);
126             WebView tabLayoutWebView = (WebView) webViewLayout.findViewById(R.id.bare_webview);
127             String userAgentString =  tabLayoutWebView.getSettings().getUserAgentString();
128
129             // Get the device's information and store it in strings.
130             String brand = Build.BRAND;
131             String manufacturer = Build.MANUFACTURER;
132             String model = Build.MODEL;
133             String device = Build.DEVICE;
134             String bootloader = Build.BOOTLOADER;
135             String radio = Build.getRadioVersion();
136             String android = Build.VERSION.RELEASE + " (" + getString(R.string.api) + " " + Integer.toString(Build.VERSION.SDK_INT) + ")";
137             String build = Build.DISPLAY;
138             // Select the substring that begins after "Safari/" and goes to the end of the string.
139             String webKit = userAgentString.substring(userAgentString.indexOf("Safari/") + 7);
140             // Select the substring that begins after "Chrome/" and goes until the next " ".
141             String chrome = userAgentString.substring(userAgentString.indexOf("Chrome/") + 7, userAgentString.indexOf(" ", userAgentString.indexOf("Chrome/")));
142
143             // Get the Orbot version name if Orbot is installed.
144             String orbot;
145             try {
146                 // Store the version name.
147                 orbot = getContext().getPackageManager().getPackageInfo("org.torproject.android", PackageManager.GET_CONFIGURATIONS).versionName;
148             } catch (PackageManager.NameNotFoundException e) {  // Orbot is not installed.
149                 orbot = "";
150             }
151
152             // Create a `SpannableStringBuilder` for the hardware and software `TextViews` that needs multiple colors of text.
153             SpannableStringBuilder brandStringBuilder = new SpannableStringBuilder(brandLabel + brand);
154             SpannableStringBuilder manufacturerStringBuilder = new SpannableStringBuilder(manufacturerLabel + manufacturer);
155             SpannableStringBuilder modelStringBuilder = new SpannableStringBuilder(modelLabel + model);
156             SpannableStringBuilder deviceStringBuilder = new SpannableStringBuilder(deviceLabel + device);
157             SpannableStringBuilder bootloaderStringBuilder = new SpannableStringBuilder(bootloaderLabel + bootloader);
158             SpannableStringBuilder androidStringBuilder = new SpannableStringBuilder(androidLabel + android);
159             SpannableStringBuilder buildStringBuilder = new SpannableStringBuilder(buildLabel + build);
160             SpannableStringBuilder webKitStringBuilder = new SpannableStringBuilder(webKitLabel + webKit);
161             SpannableStringBuilder chromeStringBuilder = new SpannableStringBuilder(chromeLabel + chrome);
162
163             // Create the `blueColorSpan` variable.
164             ForegroundColorSpan blueColorSpan;
165
166             // Set `blueColorSpan` according to the theme.  We have to use the deprecated `getColor()` until API >= 23.
167             if (MainWebViewActivity.darkTheme) {
168                 //noinspection deprecation
169                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
170             } else {
171                 //noinspection deprecation
172                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
173             }
174
175             // Setup the spans to display the device information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
176             brandStringBuilder.setSpan(blueColorSpan, brandLabel.length(), brandStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
177             manufacturerStringBuilder.setSpan(blueColorSpan, manufacturerLabel.length(), manufacturerStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
178             modelStringBuilder.setSpan(blueColorSpan, modelLabel.length(), modelStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
179             deviceStringBuilder.setSpan(blueColorSpan, deviceLabel.length(), deviceStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
180             bootloaderStringBuilder.setSpan(blueColorSpan, bootloaderLabel.length(), bootloaderStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
181             androidStringBuilder.setSpan(blueColorSpan, androidLabel.length(), androidStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
182             buildStringBuilder.setSpan(blueColorSpan, buildLabel.length(), buildStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
183             webKitStringBuilder.setSpan(blueColorSpan, webKitLabel.length(), webKitStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
184             chromeStringBuilder.setSpan(blueColorSpan, chromeLabel.length(), chromeStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
185
186             // Display the strings in the text boxes.
187             versionNumberTextView.setText(version);
188             versionBrandTextView.setText(brandStringBuilder);
189             versionManufacturerTextView.setText(manufacturerStringBuilder);
190             versionModelTextView.setText(modelStringBuilder);
191             versionDeviceTextView.setText(deviceStringBuilder);
192             versionBootloaderTextView.setText(bootloaderStringBuilder);
193             versionAndroidTextView.setText(androidStringBuilder);
194             versionBuildTextView.setText(buildStringBuilder);
195             versionWebKitTextView.setText(webKitStringBuilder);
196             versionChromeTextView.setText(chromeStringBuilder);
197
198             // Build.VERSION.SECURITY_PATCH is only available for SDK_INT >= 23.
199             if (Build.VERSION.SDK_INT >= 23) {
200                 String securityPatchLabel = getString(R.string.security_patch) + "  ";
201                 String securityPatch = Build.VERSION.SECURITY_PATCH;
202                 SpannableStringBuilder securityPatchStringBuilder = new SpannableStringBuilder(securityPatchLabel + securityPatch);
203                 securityPatchStringBuilder.setSpan(blueColorSpan, securityPatchLabel.length(), securityPatchStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
204                 versionSecurityPatchTextView.setText(securityPatchStringBuilder);
205             } else {  // SDK_INT < 23, so `versionSecurityPatchTextView` should be hidden.
206                 versionSecurityPatchTextView.setVisibility(View.GONE);
207             }
208
209             // Only populate `versionRadioTextView` if there is a radio in the device.
210             if (!radio.equals("")) {
211                 String radioLabel = getString(R.string.radio) + "  ";
212                 SpannableStringBuilder radioStringBuilder = new SpannableStringBuilder(radioLabel + radio);
213                 radioStringBuilder.setSpan(blueColorSpan, radioLabel.length(), radioStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
214                 versionRadioTextView.setText(radioStringBuilder);
215             } else {  // This device does not have a radio, so `versionRadioTextView` should be hidden.
216                 versionRadioTextView.setVisibility(View.GONE);
217             }
218
219             // Only populate `versionOrbotTextView` if Orbot is installed.
220             if (!orbot.equals("")) {
221                 String orbotLabel = getString(R.string.orbot) + "  ";
222                 SpannableStringBuilder orbotStringBuilder = new SpannableStringBuilder(orbotLabel + orbot);
223                 orbotStringBuilder.setSpan(blueColorSpan, orbotLabel.length(), orbotStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
224                 versionOrbotTextView.setText(orbotStringBuilder);
225             } else {  // Orbot is not installed, so the `versionOrbotTextView` should be hidden.
226                 versionOrbotTextView.setVisibility(View.GONE);
227             }
228
229             // Display the package signature.
230             try {
231                 // Get the first package signature.  Suppress the lint warning about the need to be careful in implementing comparison of certificates for security purposes.
232                 @SuppressLint("PackageManagerGetSignatures") Signature packageSignature = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), PackageManager.GET_SIGNATURES).signatures[0];
233
234                 // Convert the signature to a `byte[]` `InputStream`.
235                 InputStream certificateByteArrayInputStream = new ByteArrayInputStream(packageSignature.toByteArray());
236
237                 // Display the certificate information on the screen.
238                 try {
239                     // Instantiate a `CertificateFactory`.
240                     CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
241
242                     // Generate an `X509Certificate`.
243                     X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(certificateByteArrayInputStream);
244
245                     // Store the individual sections of the certificate that we are interested in.
246                     Principal issuerDNPrincipal = x509Certificate.getIssuerDN();
247                     Principal subjectDNPrincipal = x509Certificate.getSubjectDN();
248                     Date startDate = x509Certificate.getNotBefore();
249                     Date endDate = x509Certificate.getNotAfter();
250                     int certificateVersion = x509Certificate.getVersion();
251                     BigInteger serialNumberBigInteger = x509Certificate.getSerialNumber();
252                     String signatureAlgorithmNameString = x509Certificate.getSigAlgName();
253
254                     // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
255                     SpannableStringBuilder issuerDNStringBuilder = new SpannableStringBuilder(issuerDNLabel + issuerDNPrincipal.toString());
256                     SpannableStringBuilder subjectDNStringBuilder = new SpannableStringBuilder(subjectDNLabel + subjectDNPrincipal.toString());
257                     SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
258                     SpannableStringBuilder endDataStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
259                     SpannableStringBuilder certificateVersionStringBuilder = new SpannableStringBuilder(certificateVersionLabel + certificateVersion);
260                     SpannableStringBuilder serialNumberStringBuilder = new SpannableStringBuilder(serialNumberLabel + serialNumberBigInteger);
261                     SpannableStringBuilder signatureAlgorithmStringBuilder = new SpannableStringBuilder(signatureAlgorithmLabel + signatureAlgorithmNameString);
262
263                     // Setup the spans to display the device information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
264                     issuerDNStringBuilder.setSpan(blueColorSpan, issuerDNLabel.length(), issuerDNStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
265                     subjectDNStringBuilder.setSpan(blueColorSpan, subjectDNLabel.length(), subjectDNStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
266                     startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
267                     endDataStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDataStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
268                     certificateVersionStringBuilder.setSpan(blueColorSpan, certificateVersionLabel.length(), certificateVersionStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
269                     serialNumberStringBuilder.setSpan(blueColorSpan, serialNumberLabel.length(), serialNumberStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
270                     signatureAlgorithmStringBuilder.setSpan(blueColorSpan, signatureAlgorithmLabel.length(), signatureAlgorithmStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
271
272                     // Display the strings in the text boxes.
273                     certificateIssuerDNTextView.setText(issuerDNStringBuilder);
274                     certificateSubjectDNTextView.setText(subjectDNStringBuilder);
275                     certificateStartDateTextView.setText(startDateStringBuilder);
276                     certificateEndDateTextView.setText(endDataStringBuilder);
277                     certificateVersionTextView.setText(certificateVersionStringBuilder);
278                     certificateSerialNumberTextView.setText(serialNumberStringBuilder);
279                     certificateSignatureAlgorithmTextView.setText(signatureAlgorithmStringBuilder);
280                 } catch (CertificateException e) {
281                     // Do nothing if there is a certificate error.
282                 }
283             } catch (PackageManager.NameNotFoundException e) {
284                 // Do nothing if `PackageManager` says Privacy Browser isn't installed.
285             }
286         } else { // load a WebView for all the other tabs.  Tab numbers start at 0.
287             // Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.  The fragment will take care of attaching the root automatically.
288             tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
289
290             // Get a handle for `tabWebView`.
291             WebView tabWebView = (WebView) tabLayout;
292
293             // Filter the colors if `darkTheme` is `true`.
294             if (MainWebViewActivity.darkTheme) {
295                 // Initialize `darkPaint`.
296                 Paint darkPaint = new Paint();
297
298                 // Setup a float array that inverts and tempers the colors (no hard whites or blacks).
299                 float[] darkFilterFloatArray = {
300                         -.8f, 0, 0, 0, 255,  // Red.
301                         0, -.8f, 0, 0, 255,  // Green.
302                         0, 0, -.8f, 0, 255,  // Blue.
303                         0, 0, 0, .8f, 0      // Alpha.
304                 };
305
306                 // Set `darkPaint` to use `darkFilterFloatArray`.
307                 darkPaint.setColorFilter(new ColorMatrixColorFilter(darkFilterFloatArray));
308
309                 // Apply `darkPaint` to `tabWebView`.
310                 tabWebView.setLayerType(View.LAYER_TYPE_HARDWARE, darkPaint);
311             } else {
312                 // Reset `tabWebView` to use the normal colors.
313                 tabWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
314             }
315
316             switch (tabNumber) {
317                 case 1:
318                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions.html");
319                     break;
320
321                 case 2:
322                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy.html");
323                     break;
324
325                 case 3:
326                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog.html");
327                     break;
328
329                 case 4:
330                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses.html");
331                     break;
332
333                 case 5:
334                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors.html");
335                     break;
336
337                 case 6:
338                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links.html");
339                     break;
340
341                 default:
342                     break;
343             }
344         }
345
346         return tabLayout;
347     }
348 }