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