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