/*
- * 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.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;
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 tabNumber) {
+ // Create a bundle.
+ Bundle bundle = new Bundle();
+
+ // Store the tab number in the bundle.
+ bundle.putInt("tab_number", tabNumber);
- // GuideTabFragment.createTab stores the tab number in the bundle arguments so it can be referenced from onCreate().
- public static GuideTabFragment createTab (int tab) {
- Bundle thisTabArguments = new Bundle();
- thisTabArguments.putInt("Tab", tab);
+ // Create a new guide tab fragment.
+ GuideTabFragment guideTabFragment = new GuideTabFragment();
- GuideTabFragment thisTab = new GuideTabFragment();
- thisTab.setArguments(thisTabArguments);
- return thisTab;
+ // Add the bundle to the fragment.
+ guideTabFragment.setArguments(bundle);
+
+ // Return the new fragment.
+ return guideTabFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
+ // Run the default commands.
super.onCreate(savedInstanceState);
- // Store the tab number in `tabNumber`.
- tabNumber = getArguments().getInt("Tab");
- }
+ // Get a handle for the arguments.
+ Bundle arguments = getArguments();
- @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.guide_tab_webview, container, false);
- WebView tabWebView = (WebView) tabLayout;
+ // Remove the lint warning below that arguments might be null.
+ assert arguments != null;
- // Tab numbers start at 0.
- switch (tabNumber) {
- case 0:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview.html");
- break;
+ // Store the tab number in a class variable.
+ tabNumber = arguments.getInt("tab_number");
+ }
- case 1:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript.html");
- break;
+ @Override
+ 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);
- case 2:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage.html");
- break;
+ // Get a handle for the tab WebView.
+ WebView tabWebView = (WebView) tabLayout;
- case 3:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent.html");
- break;
+ // Get the current theme status.
+ int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
+ // Load the tabs according to the theme.
+ 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.
+ switch (tabNumber) {
+ case 0:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview_dark.html");
+ break;
+
+ case 1:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript_dark.html");
+ break;
+
+ case 2:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage_dark.html");
+ break;
+
+ case 3:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent_dark.html");
+ break;
+
+ case 4:
+ 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_domain_settings_dark.html");
+ break;
+
+ case 6:
+ 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;
+ }
+ } else { // The light theme is applied.
+ // Tab numbers start at 0.
+ switch (tabNumber) {
+ case 0:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview_light.html");
+ break;
+
+ case 1:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript_light.html");
+ break;
+
+ case 2:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage_light.html");
+ break;
+
+ case 3:
+ tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent_light.html");
+ break;
+
+ case 4:
+ 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_domain_settings_light.html");
+ break;
+
+ case 6:
+ 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;
+ }
+ }
- case 4:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html");
- break;
+ // Scroll the WebView if the saved instance state is not null.
+ if (savedInstanceState != null) {
+ tabWebView.post(() -> tabWebView.setScrollY(savedInstanceState.getInt("scroll_y")));
+ }
- case 5:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tor.html");
- break;
+ // Return the formatted `tabLayout`.
+ return tabLayout;
+ }
- case 6:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html");
- break;
+ @Override
+ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
+ // Run the default commands.
+ super.onSaveInstanceState(savedInstanceState);
- case 7:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_clear_and_exit.html");
- break;
+ // 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;
- case 8:
- tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_planned_features.html");
- break;
+ // 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());
}
-
- return tabLayout;
}
-}
+}
\ No newline at end of file