2 * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.content.res.Configuration;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.webkit.WebView;
29 import androidx.annotation.NonNull;
30 import androidx.fragment.app.Fragment;
32 import com.stoutner.privacybrowser.R;
34 public class AboutWebViewFragment extends Fragment {
35 // Declare the class constants.
36 final static String TAB_NUMBER = "tab_number";
38 // Declare the class variables.
39 private int tabNumber;
41 // Declare the class views.
42 private View aboutWebViewLayout;
44 public static AboutWebViewFragment createTab(int tabNumber) {
45 // Create an arguments bundle.
46 Bundle argumentsBundle = new Bundle();
48 // Store the arguments in the bundle.
49 argumentsBundle.putInt(TAB_NUMBER, tabNumber);
51 // Create a new instance of the tab fragment.
52 AboutWebViewFragment aboutWebViewFragment = new AboutWebViewFragment();
54 // Add the arguments bundle to the fragment.
55 aboutWebViewFragment.setArguments(argumentsBundle);
57 // Return the new fragment.
58 return aboutWebViewFragment;
62 public void onCreate(Bundle savedInstanceState) {
63 // Run the default commands.
64 super.onCreate(savedInstanceState);
66 // Get a handle for the arguments.
67 Bundle arguments = getArguments();
69 // Remove the incorrect lint warning below that arguments might be null.
70 assert arguments != null;
72 // Store the arguments in class variables.
73 tabNumber = arguments.getInt(TAB_NUMBER);
77 public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
78 // Get the current theme status.
79 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
81 // Inflate the layout. 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.
82 aboutWebViewLayout = layoutInflater.inflate(R.layout.bare_webview, container, false);
84 // Get a handle for tab WebView.
85 WebView tabWebView = (WebView) aboutWebViewLayout;
87 // Load the tabs according to the theme.
88 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { // The light theme is applied.
91 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_light.html");
95 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_light.html");
99 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_light.html");
103 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_light.html");
107 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_light.html");
111 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_light.html");
114 } else { // The dark theme is applied.
115 // Set the background color. The deprecated `.getColor()` must be used until the minimum API >= 23.
116 tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
118 // Tab numbers start at 0, with the WebView tabs starting at 1.
121 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_dark.html");
125 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_dark.html");
129 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_dark.html");
133 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_dark.html");
137 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_dark.html");
141 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_dark.html");
146 // Scroll the tab if the saved instance state is not null.
147 if (savedInstanceState != null) {
148 aboutWebViewLayout.post(() -> {
149 aboutWebViewLayout.setScrollX(savedInstanceState.getInt("scroll_x"));
150 aboutWebViewLayout.setScrollY(savedInstanceState.getInt("scroll_y"));
154 // Return the tab layout.
155 return aboutWebViewLayout;
159 public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
160 // Run the default commands.
161 super.onSaveInstanceState(savedInstanceState);
163 // Save the scroll positions if the layout is not null, which can happen if a tab is not currently selected.
164 if (aboutWebViewLayout != null) {
165 savedInstanceState.putInt("scroll_x", aboutWebViewLayout.getScrollX());
166 savedInstanceState.putInt("scroll_y", aboutWebViewLayout.getScrollY());