]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/fragments/AboutTabFragment.java
Make SSL errors tab aware.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / AboutTabFragment.java
index 9f627b6c0e42d946a086034075626953f413a2bb..4ff9a28a2d1194facd314ca20c66ba17060fbb8b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -21,12 +21,12 @@ package com.stoutner.privacybrowser.fragments;
 
 import android.annotation.SuppressLint;
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.content.pm.Signature;
 import android.os.Build;
 import android.os.Bundle;
-import android.support.annotation.NonNull;
-import android.support.v4.app.Fragment;
+import android.preference.PreferenceManager;
 import android.text.SpannableStringBuilder;
 import android.text.Spanned;
 import android.text.style.ForegroundColorSpan;
@@ -36,9 +36,11 @@ import android.view.ViewGroup;
 import android.webkit.WebView;
 import android.widget.TextView;
 
+import androidx.annotation.NonNull;
+import androidx.fragment.app.Fragment;
+
 import com.stoutner.privacybrowser.BuildConfig;
 import com.stoutner.privacybrowser.R;
-import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
@@ -51,19 +53,23 @@ import java.text.DateFormat;
 import java.util.Date;
 
 public class AboutTabFragment extends Fragment {
+    // Declare the class variables.
     private int tabNumber;
+    private String[] blocklistVersions;
 
-    // Store the tab number in the arguments bundle.
-    public static AboutTabFragment createTab(int tab) {
+    public static AboutTabFragment createTab(int tabNumber, String[] blocklistVersions) {
         // Create a bundle.
-        Bundle bundle = new Bundle();
+        Bundle argumentsBundle = new Bundle();
 
         // Store the tab number in the bundle.
-        bundle.putInt("Tab", tab);
+        argumentsBundle.putInt("tab_number", tabNumber);
+        argumentsBundle.putStringArray("blocklist_versions", blocklistVersions);
 
-        // Add the bundle to the fragment.
+        // Create a new instance of the tab fragment.
         AboutTabFragment aboutTabFragment = new AboutTabFragment();
-        aboutTabFragment.setArguments(bundle);
+
+        // Add the arguments bundle to the fragment.
+        aboutTabFragment.setArguments(argumentsBundle);
 
         // Return the new fragment.
         return aboutTabFragment;
@@ -74,15 +80,19 @@ public class AboutTabFragment extends Fragment {
         // Run the default commands.
         super.onCreate(savedInstanceState);
 
-        // Remove the lint warning that `getArguments()` might be null.
-        assert getArguments() != null;
+        // Get a handle for the arguments.
+        Bundle arguments = getArguments();
+
+        // Remove the incorrect lint warning below that arguments might be null.
+        assert arguments != null;
 
-        // Store the tab number in a class variable.
-        tabNumber = getArguments().getInt("Tab");
+        // Store the arguments in class variables.
+        tabNumber = getArguments().getInt("tab_number");
+        blocklistVersions = getArguments().getStringArray("blocklist_versions");
     }
 
     @Override
-    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+    public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
         // Create a tab layout view.
         View tabLayout;
 
@@ -90,10 +100,16 @@ public class AboutTabFragment extends Fragment {
         Context context = getContext();
         assert context != null;
 
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+
+        // Get the theme preference.
+        boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+
         // Load the tabs.  Tab numbers start at 0.
         if (tabNumber == 0) {  // Load the about tab.
             // 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.
-            tabLayout = inflater.inflate(R.layout.about_tab_version, container, false);
+            tabLayout = layoutInflater.inflate(R.layout.about_tab_version, container, false);
 
             // Get handles for the `TextViews`.
             TextView versionTextView = tabLayout.findViewById(R.id.version);
@@ -123,7 +139,7 @@ public class AboutTabFragment extends Fragment {
             TextView certificateSignatureAlgorithmTextView = tabLayout.findViewById(R.id.certificate_signature_algorithm);
 
             // Setup the labels.
-            String version = getString(R.string.version) + " " + BuildConfig.VERSION_NAME + " (" + getString(R.string.version_code) + " " + Integer.toString(BuildConfig.VERSION_CODE) + ")";
+            String version = getString(R.string.version) + " " + BuildConfig.VERSION_NAME + " (" + getString(R.string.version_code) + " " + BuildConfig.VERSION_CODE + ")";
             String brandLabel = getString(R.string.brand) + "  ";
             String manufacturerLabel = getString(R.string.manufacturer) + "  ";
             String modelLabel = getString(R.string.model) + "  ";
@@ -146,7 +162,7 @@ public class AboutTabFragment extends Fragment {
             String signatureAlgorithmLabel = getString(R.string.signature_algorithm) + "  ";
 
             // `webViewLayout` is only used to get the default user agent from `bare_webview`.  It is not used to render content on the screen.
-            View webViewLayout = inflater.inflate(R.layout.bare_webview, container, false);
+            View webViewLayout = layoutInflater.inflate(R.layout.bare_webview, container, false);
             WebView tabLayoutWebView = webViewLayout.findViewById(R.id.bare_webview);
             String userAgentString =  tabLayoutWebView.getSettings().getUserAgentString();
 
@@ -157,7 +173,7 @@ public class AboutTabFragment extends Fragment {
             String device = Build.DEVICE;
             String bootloader = Build.BOOTLOADER;
             String radio = Build.getRadioVersion();
-            String android = Build.VERSION.RELEASE + " (" + getString(R.string.api) + " " + Integer.toString(Build.VERSION.SDK_INT) + ")";
+            String android = Build.VERSION.RELEASE + " (" + getString(R.string.api) + " " + Build.VERSION.SDK_INT + ")";
             String build = Build.DISPLAY;
             // Select the substring that begins after `Chrome/` and goes until the next ` `.
             String webView = userAgentString.substring(userAgentString.indexOf("Chrome/") + 7, userAgentString.indexOf(" ", userAgentString.indexOf("Chrome/")));
@@ -189,21 +205,19 @@ public class AboutTabFragment extends Fragment {
             SpannableStringBuilder androidStringBuilder = new SpannableStringBuilder(androidLabel + android);
             SpannableStringBuilder buildStringBuilder = new SpannableStringBuilder(buildLabel + build);
             SpannableStringBuilder webViewStringBuilder = new SpannableStringBuilder(webViewLabel + webView);
-            SpannableStringBuilder easyListStringBuilder = new SpannableStringBuilder(easyListLabel + MainWebViewActivity.easyListVersion);
-            SpannableStringBuilder easyPrivacyStringBuilder = new SpannableStringBuilder(easyPrivacyLabel + MainWebViewActivity.easyPrivacyVersion);
-            SpannableStringBuilder fanboyAnnoyanceStringBuilder = new SpannableStringBuilder(fanboyAnnoyanceLabel + MainWebViewActivity.fanboysAnnoyanceVersion);
-            SpannableStringBuilder fanboySocialStringBuilder = new SpannableStringBuilder(fanboySocialLabel + MainWebViewActivity.fanboysSocialVersion);
-            SpannableStringBuilder ultraPrivacyStringBuilder = new SpannableStringBuilder(ultraPrivacyLabel + MainWebViewActivity.ultraPrivacyVersion);
+            SpannableStringBuilder easyListStringBuilder = new SpannableStringBuilder(easyListLabel + blocklistVersions[0]);
+            SpannableStringBuilder easyPrivacyStringBuilder = new SpannableStringBuilder(easyPrivacyLabel + blocklistVersions[1]);
+            SpannableStringBuilder fanboyAnnoyanceStringBuilder = new SpannableStringBuilder(fanboyAnnoyanceLabel + blocklistVersions[2]);
+            SpannableStringBuilder fanboySocialStringBuilder = new SpannableStringBuilder(fanboySocialLabel + blocklistVersions[3]);
+            SpannableStringBuilder ultraPrivacyStringBuilder = new SpannableStringBuilder(ultraPrivacyLabel + blocklistVersions[4]);
 
             // Create the `blueColorSpan` variable.
             ForegroundColorSpan blueColorSpan;
 
             // Set `blueColorSpan` according to the theme.  We have to use the deprecated `getColor()` until API >= 23.
-            if (MainWebViewActivity.darkTheme) {
-                //noinspection deprecation
+            if (darkTheme) {
                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
             } else {
-                //noinspection deprecation
                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
             }
 
@@ -249,7 +263,7 @@ public class AboutTabFragment extends Fragment {
                 securityPatchTextView.setVisibility(View.GONE);
             }
 
-            // Only populate the radio TextView if there is a radio in the device.
+            // Only populate the radio text view if there is a radio in the device.
             if (!radio.isEmpty()) {
                 String radioLabel = getString(R.string.radio) + "  ";
                 SpannableStringBuilder radioStringBuilder = new SpannableStringBuilder(radioLabel + radio);
@@ -259,7 +273,7 @@ public class AboutTabFragment extends Fragment {
                 radioTextView.setVisibility(View.GONE);
             }
 
-            // Only populate the Orbot TextView if it is installed.
+            // Only populate the Orbot text view if it is installed.
             if (!orbot.isEmpty()) {
                 String orbotLabel = getString(R.string.orbot) + "  ";
                 SpannableStringBuilder orbotStringBuilder = new SpannableStringBuilder(orbotLabel + orbot);
@@ -269,11 +283,11 @@ public class AboutTabFragment extends Fragment {
                 orbotTextView.setVisibility(View.GONE);
             }
 
-            // Only populate the EasyKeychain TextView if it is installed.
+            // Only populate the OpenKeychain text view if it is installed.
             if (!openKeychain.isEmpty()) {
-                String openKeychainlabel = getString(R.string.open_keychain) + "  ";
-                SpannableStringBuilder openKeychainStringBuilder = new SpannableStringBuilder(openKeychainlabel + openKeychain);
-                openKeychainStringBuilder.setSpan(blueColorSpan, openKeychainlabel.length(), openKeychainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+                String openKeychainLabel = getString(R.string.openkeychain) + "  ";
+                SpannableStringBuilder openKeychainStringBuilder = new SpannableStringBuilder(openKeychainLabel + openKeychain);
+                openKeychainStringBuilder.setSpan(blueColorSpan, openKeychainLabel.length(), openKeychainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                 openKeychainTextView.setText(openKeychainStringBuilder);
             } else {  //OpenKeychain is not installed.
                 openKeychainTextView.setVisibility(View.GONE);
@@ -338,15 +352,14 @@ public class AboutTabFragment extends Fragment {
             }
         } else { // load a `WebView` for all the other tabs.  Tab numbers start at 0.
             // 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.
-            tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
+            tabLayout = layoutInflater.inflate(R.layout.bare_webview, container, false);
 
             // Get a handle for `tabWebView`.
             WebView tabWebView = (WebView) tabLayout;
 
             // Load the tabs according to the theme.
-            if (MainWebViewActivity.darkTheme) {  // The dark theme is applied.
-                // Set the background color.  We have to use the deprecated `.getColor()` until API >= 23.
-                //noinspection deprecation
+            if (darkTheme) {  // The dark theme is applied.
+                // Set the background color.  The deprecated `.getColor()` must be used until the minimum API >= 23.
                 tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
 
                 switch (tabNumber) {