]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/fragments/GuideTabFragment.java
Fix incorrect pinned mismatch errors. https://redmine.stoutner.com/issues/591
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / GuideTabFragment.java
index a89ced02132f5d58c6946d36930bfb3d5fb5fc54..6f3aa0d3c26b30bac6c3b3746124842038af0b8f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
 
 package com.stoutner.privacybrowser.fragments;
 
-import android.annotation.SuppressLint;
+import android.content.res.Configuration;
 import android.os.Bundle;
-import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.webkit.WebView;
 
+import androidx.annotation.NonNull;
+import androidx.fragment.app.Fragment;
+
 import com.stoutner.privacybrowser.R;
-import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 
 public class GuideTabFragment extends Fragment {
-    // `tabNumber` is used in `onCreate()` and `onCreateView()`.
+    // Define the class variables.
     private int tabNumber;
+    private View tabLayout;
 
     // Store the tab number in the arguments bundle.
-    public static GuideTabFragment createTab (int tab) {
+    public static GuideTabFragment createTab (int tabNumber) {
         // Create a bundle.
         Bundle bundle = new Bundle();
 
         // Store the tab number in the bundle.
-        bundle.putInt("Tab", tab);
+        bundle.putInt("tab_number", tabNumber);
 
-        // Add the bundle to the fragment.
+        // Create a new guide tab fragment.
         GuideTabFragment guideTabFragment = new GuideTabFragment();
+
+        // Add the bundle to the fragment.
         guideTabFragment.setArguments(bundle);
 
         // Return the new fragment.
@@ -55,23 +59,29 @@ public class GuideTabFragment extends Fragment {
         // Run the default commands.
         super.onCreate(savedInstanceState);
 
+        // Get a handle for the arguments.
+        Bundle arguments = getArguments();
+
+        // Remove the lint warning below that arguments might be null.
+        assert arguments != null;
+
         // Store the tab number in a class variable.
-        tabNumber = getArguments().getInt("Tab");
+        tabNumber = arguments.getInt("tab_number");
     }
 
-    @SuppressLint("SetJavaScriptEnabled")
     @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-        // 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.
-        View tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
+    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+        // Inflate the layout.  The fragment will take care of attaching the root automatically.
+        tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
 
-        // Get a handle for `tabWebView`.
+        // Get a handle for the tab WebView.
         WebView tabWebView = (WebView) tabLayout;
 
+        // Get the current theme status.
+        int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
         // 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 (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {  // The dark theme is applied.
             tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
 
             // Tab numbers start at 0.
@@ -93,18 +103,22 @@ public class GuideTabFragment extends Fragment {
                     break;
 
                 case 4:
-                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_dark.html");
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_requests_dark.html");
                     break;
 
                 case 5:
-                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_dark.html");
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_dark.html");
                     break;
 
                 case 6:
-                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tor_dark.html");
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_dark.html");
                     break;
 
                 case 7:
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_proxies_dark.html");
+                    break;
+
+                case 8:
                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids_dark.html");
                     break;
             }
@@ -128,24 +142,47 @@ public class GuideTabFragment extends Fragment {
                     break;
 
                 case 4:
-                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_light.html");
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_requests_light.html");
                     break;
 
                 case 5:
-                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_light.html");
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_light.html");
                     break;
 
                 case 6:
-                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tor_light.html");
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_light.html");
                     break;
 
                 case 7:
+                    tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_proxies_light.html");
+                    break;
+
+                case 8:
                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids_light.html");
                     break;
             }
         }
 
+        // Scroll the WebView if the saved instance state is not null.
+        if (savedInstanceState != null) {
+            tabWebView.post(() -> tabWebView.setScrollY(savedInstanceState.getInt("scroll_y")));
+        }
+
         // Return the formatted `tabLayout`.
         return tabLayout;
     }
-}
+
+    @Override
+    public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
+        // Run the default commands.
+        super.onSaveInstanceState(savedInstanceState);
+
+        // Get a handle for the tab WebView.  A class variable cannot be used because it gets out of sync when restarting.
+        WebView tabWebView = (WebView) tabLayout;
+
+        // Save the scroll Y position if the tab WebView is not null, which can happen if a tab is not currently selected.
+        if (tabWebView != null) {
+            savedInstanceState.putInt("scroll_y", tabWebView.getScrollY());
+        }
+    }
+}
\ No newline at end of file