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