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.Context;
23 import android.content.Intent;
24 import android.content.res.Configuration;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.webkit.WebResourceResponse;
31 import android.webkit.WebView;
32 import android.webkit.WebViewClient;
34 import androidx.annotation.NonNull;
35 import androidx.fragment.app.Fragment;
36 import androidx.webkit.WebSettingsCompat;
37 import androidx.webkit.WebViewAssetLoader;
38 import androidx.webkit.WebViewFeature;
40 import com.stoutner.privacybrowser.R;
42 public class AboutWebViewFragment extends Fragment {
43 // Declare the class constants.
44 final static String TAB_NUMBER = "tab_number";
46 // Declare the class variables.
47 private int tabNumber;
49 // Declare the class views.
50 private View webViewLayout;
52 public static AboutWebViewFragment createTab(int tabNumber) {
53 // Create an arguments bundle.
54 Bundle argumentsBundle = new Bundle();
56 // Store the arguments in the bundle.
57 argumentsBundle.putInt(TAB_NUMBER, tabNumber);
59 // Create a new instance of the tab fragment.
60 AboutWebViewFragment aboutWebViewFragment = new AboutWebViewFragment();
62 // Add the arguments bundle to the fragment.
63 aboutWebViewFragment.setArguments(argumentsBundle);
65 // Return the new fragment.
66 return aboutWebViewFragment;
70 public void onCreate(Bundle savedInstanceState) {
71 // Run the default commands.
72 super.onCreate(savedInstanceState);
74 // Get a handle for the arguments.
75 Bundle arguments = getArguments();
77 // Remove the incorrect lint warning below that arguments might be null.
78 assert arguments != null;
80 // Store the tab number in a class variable.
81 tabNumber = arguments.getInt(TAB_NUMBER);
85 public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
86 // 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.
87 webViewLayout = layoutInflater.inflate(R.layout.bare_webview, container, false);
89 // Get a handle for tab WebView.
90 WebView tabWebView = (WebView) webViewLayout;
92 // Get a handle for the context.
93 Context context = getContext();
95 // Remove the incorrect lint warning below that the context might be null.
96 assert context != null;
98 // Create a WebView asset loader.
99 final WebViewAssetLoader webViewAssetLoader = new WebViewAssetLoader.Builder().addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(context)).build();
101 // Set a WebView client.
102 tabWebView.setWebViewClient(new WebViewClient() {
103 // `shouldOverrideUrlLoading` allows the sending of external links back to the main Privacy Browser WebView. The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
105 public boolean shouldOverrideUrlLoading(WebView view, String url) {
106 // Create an intent to view the URL.
107 Intent urlIntent = new Intent(Intent.ACTION_VIEW);
109 // Add the URL to the intent.
110 urlIntent.setData(Uri.parse(url));
113 startActivity(urlIntent);
118 public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
119 // Have the WebView asset loader process the request. This allows the loading of SVG files, which otherwise is prevented by the CORS policy.
120 return webViewAssetLoader.shouldInterceptRequest(Uri.parse(url));
124 // Get the current theme status.
125 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
127 // Check to see if the app is in night mode.
128 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { // The app is in night mode.
129 // Apply the dark WebView theme.
130 WebSettingsCompat.setForceDark(tabWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
133 // Load the indicated tab. The tab numbers start at 0, with the WebView tabs starting at 1.
136 // Load the Permissions tab.
137 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_permissions.html");
141 // Load the Privacy Policy tab.
142 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_privacy_policy.html");
146 // Load the Changelog tab.
147 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_changelog.html");
151 // Load the Licenses tab.
152 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_licenses.html");
156 // Load the Contributors tab.
157 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_contributors.html");
161 // Load the Links tab.
162 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_links.html");
166 // Scroll the tab if the saved instance state is not null.
167 if (savedInstanceState != null) {
168 tabWebView.post(() -> {
169 tabWebView.setScrollX(savedInstanceState.getInt("scroll_x"));
170 tabWebView.setScrollY(savedInstanceState.getInt("scroll_y"));
174 // Return the formatted WebView layout.
175 return webViewLayout;
179 public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
180 // Run the default commands.
181 super.onSaveInstanceState(savedInstanceState);
184 // Get a handle for the tab WebView. A class variable cannot be used because it gets out of sync when restarting.
185 WebView tabWebView = (WebView) webViewLayout;
187 // Save the scroll positions if the layout is not null, which can happen if a tab is not currently selected.
188 if (tabWebView != null) {
189 savedInstanceState.putInt("scroll_x", tabWebView.getScrollX());
190 savedInstanceState.putInt("scroll_y", tabWebView.getScrollY());