Implement a working two-paned mode for `DomainsActivity`.
[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.os.Build;
23 import android.os.Bundle;
24 import android.support.v4.app.Fragment;
25 import android.text.SpannableStringBuilder;
26 import android.text.Spanned;
27 import android.text.style.ForegroundColorSpan;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.webkit.WebView;
32 import android.widget.TextView;
33
34 import com.stoutner.privacybrowser.BuildConfig;
35 import com.stoutner.privacybrowser.R;
36
37 public class AboutTabFragment extends Fragment {
38     private int tabNumber;
39
40     // AboutTabFragment.createTab stores the tab number in the bundle arguments so it can be referenced from onCreate().
41     public static AboutTabFragment createTab(int tab) {
42         Bundle thisTabArguments = new Bundle();
43         thisTabArguments.putInt("Tab", tab);
44
45         AboutTabFragment thisTab = new AboutTabFragment();
46         thisTab.setArguments(thisTabArguments);
47         return thisTab;
48     }
49
50     @Override
51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53
54         // Store the tab number in tabNumber.
55         tabNumber = getArguments().getInt("Tab");
56     }
57
58     @Override
59     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
60         View tabLayout;
61
62         // Load the tabs.  Tab numbers start at 0.
63         if (tabNumber == 0) {  // Load the about tab.
64             // Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
65             // The fragment will take care of attaching the root automatically.
66             tabLayout = inflater.inflate(R.layout.about_tab_version, container, false);
67
68             // Get handles for the `TextViews`.
69             TextView versionNumberTextView = (TextView) tabLayout.findViewById(R.id.about_version_number);
70             TextView versionBrandTextView = (TextView) tabLayout.findViewById(R.id.about_version_brand);
71             TextView versionManufacturerTextView = (TextView) tabLayout.findViewById(R.id.about_version_manufacturer);
72             TextView versionModelTextView = (TextView) tabLayout.findViewById(R.id.about_version_model);
73             TextView versionDeviceTextView = (TextView) tabLayout.findViewById(R.id.about_version_device);
74             TextView versionBootloaderTextView = (TextView) tabLayout.findViewById(R.id.about_version_bootloader);
75             TextView versionRadioTextView = (TextView) tabLayout.findViewById(R.id.about_version_radio);
76             TextView versionAndroidTextView = (TextView) tabLayout.findViewById(R.id.about_version_android);
77             TextView versionBuildTextView = (TextView) tabLayout.findViewById(R.id.about_version_build);
78             TextView versionSecurityPatchTextView = (TextView) tabLayout.findViewById(R.id.about_version_securitypatch);
79             TextView versionWebKitTextView = (TextView) tabLayout.findViewById(R.id.about_version_webkit);
80             TextView versionChromeText = (TextView) tabLayout.findViewById(R.id.about_version_chrome);
81
82             // Setup the labels.
83             String version = getString(R.string.version) + " " + BuildConfig.VERSION_NAME + " (" + getString(R.string.version_code) + " " + Integer.toString(BuildConfig.VERSION_CODE) + ")";
84             String brandLabel = getString(R.string.brand) + "  ";
85             String manufacturerLabel = getString(R.string.manufacturer) + "  ";
86             String modelLabel = getString(R.string.model) + "  ";
87             String deviceLabel = getString(R.string.device) + "  ";
88             String bootloaderLabel = getString(R.string.bootloader) + "  ";
89             String androidLabel = getString(R.string.android) + "  ";
90             String buildLabel = getString(R.string.build) + "  ";
91             String webKitLabel = getString(R.string.webkit) + "  ";
92             String chromeLabel = getString(R.string.chrome) + "  ";
93
94             // `webViewLayout` is only used to get the default user agent from `bare_webview`.  It is not used to render content on the screen.
95             View webViewLayout = inflater.inflate(R.layout.bare_webview, container, false);
96             WebView tabLayoutWebView = (WebView) webViewLayout.findViewById(R.id.bare_webview);
97             String userAgentString =  tabLayoutWebView.getSettings().getUserAgentString();
98
99             // Get the device's information and store it in strings.
100             String brand = Build.BRAND;
101             String manufacturer = Build.MANUFACTURER;
102             String model = Build.MODEL;
103             String device = Build.DEVICE;
104             String bootloader = Build.BOOTLOADER;
105             String radio = Build.getRadioVersion();
106             String android = Build.VERSION.RELEASE + " (" + getString(R.string.api) + " " + Integer.toString(Build.VERSION.SDK_INT) + ")";
107             String build = Build.DISPLAY;
108             // Select the substring that begins after "Safari/" and goes to the end of the string.
109             String webKit = userAgentString.substring(userAgentString.indexOf("Safari/") + 7);
110             // Select the substring that begins after "Chrome/" and goes until the next " ".
111             String chrome = userAgentString.substring(userAgentString.indexOf("Chrome/") + 7, userAgentString.indexOf(" ", userAgentString.indexOf("Chrome/")));
112
113             // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
114             SpannableStringBuilder brandStringBuilder = new SpannableStringBuilder(brandLabel + brand);
115             SpannableStringBuilder manufacturerStringBuilder = new SpannableStringBuilder(manufacturerLabel + manufacturer);
116             SpannableStringBuilder modelStringBuilder = new SpannableStringBuilder(modelLabel + model);
117             SpannableStringBuilder deviceStringBuilder = new SpannableStringBuilder(deviceLabel + device);
118             SpannableStringBuilder bootloaderStringBuilder = new SpannableStringBuilder(bootloaderLabel + bootloader);
119             SpannableStringBuilder androidStringBuilder = new SpannableStringBuilder(androidLabel + android);
120             SpannableStringBuilder buildStringBuilder = new SpannableStringBuilder(buildLabel + build);
121             SpannableStringBuilder webKitStringBuilder = new SpannableStringBuilder(webKitLabel + webKit);
122             SpannableStringBuilder chromeStringBuilder = new SpannableStringBuilder(chromeLabel + chrome);
123
124             // Create a blue `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
125             @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
126
127             // Setup the spans to display the device information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
128             brandStringBuilder.setSpan(blueColorSpan, brandLabel.length(), brandStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
129             manufacturerStringBuilder.setSpan(blueColorSpan, manufacturerLabel.length(), manufacturerStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
130             modelStringBuilder.setSpan(blueColorSpan, modelLabel.length(), modelStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
131             deviceStringBuilder.setSpan(blueColorSpan, deviceLabel.length(), deviceStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
132             bootloaderStringBuilder.setSpan(blueColorSpan, bootloaderLabel.length(), bootloaderStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
133             androidStringBuilder.setSpan(blueColorSpan, androidLabel.length(), androidStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
134             buildStringBuilder.setSpan(blueColorSpan, buildLabel.length(), buildStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
135             webKitStringBuilder.setSpan(blueColorSpan, webKitLabel.length(), webKitStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
136             chromeStringBuilder.setSpan(blueColorSpan, chromeLabel.length(), chromeStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
137
138             // Display the strings.
139             versionNumberTextView.setText(version);
140             versionBrandTextView.setText(brandStringBuilder);
141             versionManufacturerTextView.setText(manufacturerStringBuilder);
142             versionModelTextView.setText(modelStringBuilder);
143             versionDeviceTextView.setText(deviceStringBuilder);
144             versionBootloaderTextView.setText(bootloaderStringBuilder);
145             versionAndroidTextView.setText(androidStringBuilder);
146             versionBuildTextView.setText(buildStringBuilder);
147             versionWebKitTextView.setText(webKitStringBuilder);
148             versionChromeText.setText(chromeStringBuilder);
149
150             // Build.VERSION.SECURITY_PATCH is only available for SDK_INT >= 23.
151             if (Build.VERSION.SDK_INT >= 23) {
152                 String securityPatchLabel = getString(R.string.security_patch) + "  ";
153                 String securityPatch = Build.VERSION.SECURITY_PATCH;
154                 SpannableStringBuilder securityPatchStringBuilder = new SpannableStringBuilder(securityPatchLabel + securityPatch);
155                 securityPatchStringBuilder.setSpan(blueColorSpan, securityPatchLabel.length(), securityPatchStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
156                 versionSecurityPatchTextView.setText(securityPatchStringBuilder);
157             } else { // Hide `versionSecurityPatchTextView`.
158                 versionSecurityPatchTextView.setVisibility(View.GONE);
159             }
160
161             // Only populate `versionRadioTextView` if there is a radio in the device.
162             if (!radio.equals("")) {
163                 String radioLabel = getString(R.string.radio) + "  ";
164                 SpannableStringBuilder radioStringBuilder = new SpannableStringBuilder(radioLabel + radio);
165                 radioStringBuilder.setSpan(blueColorSpan, radioLabel.length(), radioStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
166                 versionRadioTextView.setText(radioStringBuilder);
167             } else { // Hide `versionRadioTextView`.
168                 versionRadioTextView.setVisibility(View.GONE);
169             }
170         } else { // load a WebView for all the other tabs.  Tab numbers start at 0.
171             // Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
172             // The fragment will take care of attaching the root automatically.
173             tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
174             WebView tabWebView = (WebView) tabLayout;
175
176             switch (tabNumber) {
177                 case 1:
178                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions.html");
179                     break;
180
181                 case 2:
182                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy.html");
183                     break;
184
185                 case 3:
186                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog.html");
187                     break;
188
189                 case 4:
190                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses.html");
191                     break;
192
193                 case 5:
194                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors.html");
195                     break;
196
197                 case 6:
198                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links.html");
199                     break;
200
201                 default:
202                     break;
203             }
204         }
205
206         return tabLayout;
207     }
208 }