2 * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.fragments;
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;
37 import com.stoutner.privacybrowser.BuildConfig;
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
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;
51 public class AboutTabFragment extends Fragment {
52 private int tabNumber;
54 // Store the tab number in the arguments bundle.
55 public static AboutTabFragment createTab(int tab) {
57 Bundle bundle = new Bundle();
59 // Store the tab number in the bundle.
60 bundle.putInt("Tab", tab);
62 // Add the bundle to the fragment.
63 AboutTabFragment aboutTabFragment = new AboutTabFragment();
64 aboutTabFragment.setArguments(bundle);
66 // Return the new fragment.
67 return aboutTabFragment;
71 public void onCreate(Bundle savedInstanceState) {
72 // Run the default commands.
73 super.onCreate(savedInstanceState);
75 // Store the tab number in a class variable.
76 tabNumber = getArguments().getInt("Tab");
80 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
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);
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);
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) + " ";
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();
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/")));
150 // Get the Orbot version name if Orbot is installed.
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.
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);
171 // Create the `blueColorSpan` variable.
172 ForegroundColorSpan blueColorSpan;
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));
179 //noinspection deprecation
180 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
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);
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);
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);
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);
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);
239 // Display the package signature.
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];
244 // Convert the signature to a `byte[]` `InputStream`.
245 InputStream certificateByteArrayInputStream = new ByteArrayInputStream(packageSignature.toByteArray());
247 // Display the certificate information on the screen.
249 // Instantiate a `CertificateFactory`.
250 CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
252 // Generate an `X509Certificate`.
253 X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(certificateByteArrayInputStream);
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();
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);
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);
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.
293 } catch (PackageManager.NameNotFoundException e) {
294 // Do nothing if `PackageManager` says Privacy Browser isn't installed.
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);
300 // Get a handle for `tabWebView`.
301 WebView tabWebView = (WebView) tabLayout;
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));
311 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_dark.html");
315 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_dark.html");
319 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_dark.html");
323 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_dark.html");
327 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_dark.html");
331 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_dark.html");
334 } else { // The light theme is applied.
337 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_light.html");
341 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_light.html");
345 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_light.html");
349 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_light.html");
353 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_light.html");
357 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_light.html");
363 // Return the formatted `tabLayout`.