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