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 versionChromeTextView = tabLayout.findViewById(R.id.about_version_chrome);
104 TextView versionOrbotTextView = tabLayout.findViewById(R.id.about_version_orbot);
105 TextView versionEasyListTextView = tabLayout.findViewById(R.id.about_version_easylist);
106 TextView versionEasyPrivacyTextView = tabLayout.findViewById(R.id.about_version_easyprivacy);
107 TextView versionFanboyAnnoyanceTextView = tabLayout.findViewById(R.id.about_version_fanboy_annoyance);
108 TextView versionFanboySocialTextView = tabLayout.findViewById(R.id.about_version_fanboy_social);
109 TextView versionUltraPrivacyTextView = tabLayout.findViewById(R.id.about_version_ultraprivacy);
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 chromeLabel = getString(R.string.chrome) + " ";
128 String easyListLabel = getString(R.string.easylist_label) + " ";
129 String easyPrivacyLabel = getString(R.string.easyprivacy_label) + " ";
130 String fanboyAnnoyanceLabel = getString(R.string.fanboy_annoyance_label) + " ";
131 String fanboySocialLabel = getString(R.string.fanboy_social_label) + " ";
132 String ultraPrivacyLabel = getString(R.string.ultraprivacy_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 `Chrome/` and goes until the next ` `.
156 String chrome = userAgentString.substring(userAgentString.indexOf("Chrome/") + 7, userAgentString.indexOf(" ", userAgentString.indexOf("Chrome/")));
158 // Get the Orbot version name if Orbot is installed.
161 // Remove the lint warning that `getContext()` might be null.
162 assert getContext() != null;
164 // Store the version name.
165 orbot = getContext().getPackageManager().getPackageInfo("org.torproject.android", PackageManager.GET_CONFIGURATIONS).versionName;
166 } catch (PackageManager.NameNotFoundException e) { // Orbot is not installed.
170 // Create a `SpannableStringBuilder` for the hardware and software `TextViews` that needs multiple colors of text.
171 SpannableStringBuilder brandStringBuilder = new SpannableStringBuilder(brandLabel + brand);
172 SpannableStringBuilder manufacturerStringBuilder = new SpannableStringBuilder(manufacturerLabel + manufacturer);
173 SpannableStringBuilder modelStringBuilder = new SpannableStringBuilder(modelLabel + model);
174 SpannableStringBuilder deviceStringBuilder = new SpannableStringBuilder(deviceLabel + device);
175 SpannableStringBuilder bootloaderStringBuilder = new SpannableStringBuilder(bootloaderLabel + bootloader);
176 SpannableStringBuilder androidStringBuilder = new SpannableStringBuilder(androidLabel + android);
177 SpannableStringBuilder buildStringBuilder = new SpannableStringBuilder(buildLabel + build);
178 SpannableStringBuilder chromeStringBuilder = new SpannableStringBuilder(chromeLabel + chrome);
179 SpannableStringBuilder easyListStringBuilder = new SpannableStringBuilder(easyListLabel + MainWebViewActivity.easyListVersion);
180 SpannableStringBuilder easyPrivacyStringBuilder = new SpannableStringBuilder(easyPrivacyLabel + MainWebViewActivity.easyPrivacyVersion);
181 SpannableStringBuilder fanboyAnnoyanceStringBuilder = new SpannableStringBuilder(fanboyAnnoyanceLabel + MainWebViewActivity.fanboysAnnoyanceVersion);
182 SpannableStringBuilder fanboySocialStringBuilder = new SpannableStringBuilder(fanboySocialLabel + MainWebViewActivity.fanboysSocialVersion);
183 SpannableStringBuilder ultraPrivacyStringBuilder = new SpannableStringBuilder(ultraPrivacyLabel + MainWebViewActivity.ultraPrivacyVersion);
185 // Create the `blueColorSpan` variable.
186 ForegroundColorSpan blueColorSpan;
188 // Set `blueColorSpan` according to the theme. We have to use the deprecated `getColor()` until API >= 23.
189 if (MainWebViewActivity.darkTheme) {
190 //noinspection deprecation
191 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
193 //noinspection deprecation
194 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
197 // Setup the spans to display the device information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
198 brandStringBuilder.setSpan(blueColorSpan, brandLabel.length(), brandStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
199 manufacturerStringBuilder.setSpan(blueColorSpan, manufacturerLabel.length(), manufacturerStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
200 modelStringBuilder.setSpan(blueColorSpan, modelLabel.length(), modelStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
201 deviceStringBuilder.setSpan(blueColorSpan, deviceLabel.length(), deviceStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
202 bootloaderStringBuilder.setSpan(blueColorSpan, bootloaderLabel.length(), bootloaderStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
203 androidStringBuilder.setSpan(blueColorSpan, androidLabel.length(), androidStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
204 buildStringBuilder.setSpan(blueColorSpan, buildLabel.length(), buildStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
205 chromeStringBuilder.setSpan(blueColorSpan, chromeLabel.length(), chromeStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
206 easyListStringBuilder.setSpan(blueColorSpan, easyListLabel.length(), easyListStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
207 easyPrivacyStringBuilder.setSpan(blueColorSpan, easyPrivacyLabel.length(), easyPrivacyStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
208 fanboyAnnoyanceStringBuilder.setSpan(blueColorSpan, fanboyAnnoyanceLabel.length(), fanboyAnnoyanceStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
209 fanboySocialStringBuilder.setSpan(blueColorSpan, fanboySocialLabel.length(), fanboySocialStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
210 ultraPrivacyStringBuilder.setSpan(blueColorSpan, ultraPrivacyLabel.length(), ultraPrivacyStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
212 // Display the strings in the text boxes.
213 versionNumberTextView.setText(version);
214 versionBrandTextView.setText(brandStringBuilder);
215 versionManufacturerTextView.setText(manufacturerStringBuilder);
216 versionModelTextView.setText(modelStringBuilder);
217 versionDeviceTextView.setText(deviceStringBuilder);
218 versionBootloaderTextView.setText(bootloaderStringBuilder);
219 versionAndroidTextView.setText(androidStringBuilder);
220 versionBuildTextView.setText(buildStringBuilder);
221 versionChromeTextView.setText(chromeStringBuilder);
222 versionEasyListTextView.setText(easyListStringBuilder);
223 versionEasyPrivacyTextView.setText(easyPrivacyStringBuilder);
224 versionFanboyAnnoyanceTextView.setText(fanboyAnnoyanceStringBuilder);
225 versionFanboySocialTextView.setText(fanboySocialStringBuilder);
226 versionUltraPrivacyTextView.setText(ultraPrivacyStringBuilder);
228 // Build.VERSION.SECURITY_PATCH is only available for SDK_INT >= 23.
229 if (Build.VERSION.SDK_INT >= 23) {
230 String securityPatchLabel = getString(R.string.security_patch) + " ";
231 String securityPatch = Build.VERSION.SECURITY_PATCH;
232 SpannableStringBuilder securityPatchStringBuilder = new SpannableStringBuilder(securityPatchLabel + securityPatch);
233 securityPatchStringBuilder.setSpan(blueColorSpan, securityPatchLabel.length(), securityPatchStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
234 versionSecurityPatchTextView.setText(securityPatchStringBuilder);
235 } else { // SDK_INT < 23, so `versionSecurityPatchTextView` should be hidden.
236 versionSecurityPatchTextView.setVisibility(View.GONE);
239 // Only populate `versionRadioTextView` if there is a radio in the device.
240 if (!radio.equals("")) {
241 String radioLabel = getString(R.string.radio) + " ";
242 SpannableStringBuilder radioStringBuilder = new SpannableStringBuilder(radioLabel + radio);
243 radioStringBuilder.setSpan(blueColorSpan, radioLabel.length(), radioStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
244 versionRadioTextView.setText(radioStringBuilder);
245 } else { // This device does not have a radio, so `versionRadioTextView` should be hidden.
246 versionRadioTextView.setVisibility(View.GONE);
249 // Only populate `versionOrbotTextView` if Orbot is installed.
250 if (!orbot.equals("")) {
251 String orbotLabel = getString(R.string.orbot) + " ";
252 SpannableStringBuilder orbotStringBuilder = new SpannableStringBuilder(orbotLabel + orbot);
253 orbotStringBuilder.setSpan(blueColorSpan, orbotLabel.length(), orbotStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
254 versionOrbotTextView.setText(orbotStringBuilder);
255 } else { // Orbot is not installed, so the `versionOrbotTextView` should be hidden.
256 versionOrbotTextView.setVisibility(View.GONE);
259 // Display the package signature.
261 // Get the first package signature. Suppress the lint warning about the need to be careful in implementing comparison of certificates for security purposes.
262 @SuppressLint("PackageManagerGetSignatures") Signature packageSignature = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), PackageManager.GET_SIGNATURES).signatures[0];
264 // Convert the signature to a `byte[]` `InputStream`.
265 InputStream certificateByteArrayInputStream = new ByteArrayInputStream(packageSignature.toByteArray());
267 // Display the certificate information on the screen.
269 // Instantiate a `CertificateFactory`.
270 CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
272 // Generate an `X509Certificate`.
273 X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(certificateByteArrayInputStream);
275 // Store the individual sections of the certificate that we are interested in.
276 Principal issuerDNPrincipal = x509Certificate.getIssuerDN();
277 Principal subjectDNPrincipal = x509Certificate.getSubjectDN();
278 Date startDate = x509Certificate.getNotBefore();
279 Date endDate = x509Certificate.getNotAfter();
280 int certificateVersion = x509Certificate.getVersion();
281 BigInteger serialNumberBigInteger = x509Certificate.getSerialNumber();
282 String signatureAlgorithmNameString = x509Certificate.getSigAlgName();
284 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
285 SpannableStringBuilder issuerDNStringBuilder = new SpannableStringBuilder(issuerDNLabel + issuerDNPrincipal.toString());
286 SpannableStringBuilder subjectDNStringBuilder = new SpannableStringBuilder(subjectDNLabel + subjectDNPrincipal.toString());
287 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
288 SpannableStringBuilder endDataStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
289 SpannableStringBuilder certificateVersionStringBuilder = new SpannableStringBuilder(certificateVersionLabel + certificateVersion);
290 SpannableStringBuilder serialNumberStringBuilder = new SpannableStringBuilder(serialNumberLabel + serialNumberBigInteger);
291 SpannableStringBuilder signatureAlgorithmStringBuilder = new SpannableStringBuilder(signatureAlgorithmLabel + signatureAlgorithmNameString);
293 // Setup the spans to display the device information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
294 issuerDNStringBuilder.setSpan(blueColorSpan, issuerDNLabel.length(), issuerDNStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
295 subjectDNStringBuilder.setSpan(blueColorSpan, subjectDNLabel.length(), subjectDNStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
296 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
297 endDataStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDataStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
298 certificateVersionStringBuilder.setSpan(blueColorSpan, certificateVersionLabel.length(), certificateVersionStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
299 serialNumberStringBuilder.setSpan(blueColorSpan, serialNumberLabel.length(), serialNumberStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
300 signatureAlgorithmStringBuilder.setSpan(blueColorSpan, signatureAlgorithmLabel.length(), signatureAlgorithmStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
302 // Display the strings in the text boxes.
303 certificateIssuerDNTextView.setText(issuerDNStringBuilder);
304 certificateSubjectDNTextView.setText(subjectDNStringBuilder);
305 certificateStartDateTextView.setText(startDateStringBuilder);
306 certificateEndDateTextView.setText(endDataStringBuilder);
307 certificateVersionTextView.setText(certificateVersionStringBuilder);
308 certificateSerialNumberTextView.setText(serialNumberStringBuilder);
309 certificateSignatureAlgorithmTextView.setText(signatureAlgorithmStringBuilder);
310 } catch (CertificateException e) {
311 // Do nothing if there is a certificate error.
313 } catch (PackageManager.NameNotFoundException e) {
314 // Do nothing if `PackageManager` says Privacy Browser isn't installed.
316 } else { // load a `WebView` for all the other tabs. Tab numbers start at 0.
317 // 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.
318 tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
320 // Get a handle for `tabWebView`.
321 WebView tabWebView = (WebView) tabLayout;
323 // Load the tabs according to the theme.
324 if (MainWebViewActivity.darkTheme) { // The dark theme is applied.
325 // Set the background color. We have to use the deprecated `.getColor()` until API >= 23.
326 //noinspection deprecation
327 tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
331 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_dark.html");
335 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_dark.html");
339 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_dark.html");
343 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_dark.html");
347 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_dark.html");
351 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_dark.html");
354 } else { // The light theme is applied.
357 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_light.html");
361 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_light.html");
365 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_light.html");
369 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_light.html");
373 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_light.html");
377 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_light.html");
383 // Return the formatted `tabLayout`.