2 * Copyright © 2016-2021 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 GuideWebViewFragment extends Fragment {
43 // Declare the class constants.
44 private 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 // Store the tab number in the arguments bundle.
53 public static GuideWebViewFragment createTab (int tabNumber) {
55 Bundle bundle = new Bundle();
57 // Store the tab number in the bundle.
58 bundle.putInt(TAB_NUMBER, tabNumber);
60 // Create a new guide tab fragment.
61 GuideWebViewFragment guideWebViewFragment = new GuideWebViewFragment();
63 // Add the bundle to the fragment.
64 guideWebViewFragment.setArguments(bundle);
66 // Return the new fragment.
67 return guideWebViewFragment;
71 public void onCreate(Bundle savedInstanceState) {
72 // Run the default commands.
73 super.onCreate(savedInstanceState);
75 // Get a handle for the arguments.
76 Bundle arguments = getArguments();
78 // Remove the lint warning below that arguments might be null.
79 assert arguments != null;
81 // Store the tab number in a class variable.
82 tabNumber = arguments.getInt(TAB_NUMBER);
86 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
87 // Inflate the layout. The fragment will take care of attaching the root automatically.
88 webViewLayout = inflater.inflate(R.layout.bare_webview, container, false);
90 // Get a handle for the tab WebView.
91 WebView tabWebView = (WebView) webViewLayout;
93 // Get a handle for the context.
94 Context context = getContext();
96 // Remove the incorrect lint warning below that the context might be null.
97 assert context != null;
99 // Create a WebView asset loader.
100 final WebViewAssetLoader webViewAssetLoader = new WebViewAssetLoader.Builder().addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(context)).build();
102 // Set a WebView client.
103 tabWebView.setWebViewClient(new WebViewClient() {
104 // `shouldOverrideUrlLoading` allows sending of external links back to the main Privacy Browser WebView. The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
106 public boolean shouldOverrideUrlLoading(WebView view, String url) {
107 // Create an intent to view the URL.
108 Intent urlIntent = new Intent(Intent.ACTION_VIEW);
110 // Add the URL to the intent.
111 urlIntent.setData(Uri.parse(url));
114 startActivity(urlIntent);
119 public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
120 // Have the WebView asset loader process the request.
121 // This allows using the `appassets.androidplatform.net` URL, which handles the loading of SVG files, which otherwise is prevented by the CORS policy.
122 return webViewAssetLoader.shouldInterceptRequest(Uri.parse(url));
126 // Get the current theme status.
127 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
129 // Check to see if the app is in night mode.
130 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { // The app is in night mode.
131 // Apply the dark WebView theme.
132 WebSettingsCompat.setForceDark(tabWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
135 // Load the indicated tab. The tab numbers start at 0.
138 // Load the Overview tab.
139 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_overview.html");
143 // Load the JavaScript tab.
144 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_javascript.html");
148 // Load the Local Storage tab.
149 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_local_storage.html");
153 // Load the User Agent tab.
154 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_user_agent.html");
158 // Load the Requests tab.
159 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_requests.html");
163 // Load the Domain Settings tab.
164 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html");
168 // Load the SSL Certificates tab.
169 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates.html");
173 // Load the Proxies tab.
174 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_proxies.html");
178 // Load the Tracking IDs tab.
179 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html");
183 // Scroll the WebView if the saved instance state is not null.
184 if (savedInstanceState != null) {
185 tabWebView.post(() -> tabWebView.setScrollY(savedInstanceState.getInt("scroll_y")));
188 // Return the formatted WebView layout.
189 return webViewLayout;
193 public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
194 // Run the default commands.
195 super.onSaveInstanceState(savedInstanceState);
197 // Get a handle for the tab WebView. A class variable cannot be used because it gets out of sync when restarting.
198 WebView tabWebView = (WebView) webViewLayout;
200 // Save the scroll Y position if the tab WebView is not null, which can happen if a tab is not currently selected.
201 if (tabWebView != null) {
202 savedInstanceState.putInt("scroll_y", tabWebView.getScrollY());