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