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