2 * Copyright © 2016-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android 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 Android 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 Android. 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.WebResourceRequest;
31 import android.webkit.WebResourceResponse;
32 import android.webkit.WebView;
33 import android.webkit.WebViewClient;
35 import androidx.annotation.NonNull;
36 import androidx.fragment.app.Fragment;
37 import androidx.webkit.WebSettingsCompat;
38 import androidx.webkit.WebViewAssetLoader;
39 import androidx.webkit.WebViewFeature;
41 import com.stoutner.privacybrowser.R;
43 public class GuideWebViewFragment extends Fragment {
44 // Declare the class constants.
45 private final static String TAB_NUMBER = "tab_number";
47 // Declare the class variables.
48 private int tabNumber;
50 // Declare the class views.
51 private View webViewLayout;
53 // Store the tab number in the arguments bundle.
54 public static GuideWebViewFragment createTab (int tabNumber) {
56 Bundle bundle = new Bundle();
58 // Store the tab number in the bundle.
59 bundle.putInt(TAB_NUMBER, tabNumber);
61 // Create a new guide tab fragment.
62 GuideWebViewFragment guideWebViewFragment = new GuideWebViewFragment();
64 // Add the bundle to the fragment.
65 guideWebViewFragment.setArguments(bundle);
67 // Return the new fragment.
68 return guideWebViewFragment;
72 public void onCreate(Bundle savedInstanceState) {
73 // Run the default commands.
74 super.onCreate(savedInstanceState);
76 // Get a handle for the arguments.
77 Bundle arguments = getArguments();
79 // Remove the lint warning below that arguments might be null.
80 assert arguments != null;
82 // Store the tab number in a class variable.
83 tabNumber = arguments.getInt(TAB_NUMBER);
87 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
88 // Inflate the layout. The fragment will take care of attaching the root automatically.
89 webViewLayout = inflater.inflate(R.layout.bare_webview, container, false);
91 // Get a handle for the tab WebView.
92 WebView tabWebView = (WebView) webViewLayout;
94 // Get a handle for the context.
95 Context context = getContext();
97 // Remove the incorrect lint warning below that the context might be null.
98 assert context != null;
100 // Create a WebView asset loader.
101 final WebViewAssetLoader webViewAssetLoader = new WebViewAssetLoader.Builder().addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(context)).build();
103 // Set a WebView client.
104 tabWebView.setWebViewClient(new WebViewClient() {
105 // `shouldOverrideUrlLoading` allows sending of external links back to the main Privacy Browser WebView. The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
107 public boolean shouldOverrideUrlLoading(WebView view, String url) {
108 // Create an intent to view the URL.
109 Intent urlIntent = new Intent(Intent.ACTION_VIEW);
111 // Add the URL to the intent.
112 urlIntent.setData(Uri.parse(url));
115 startActivity(urlIntent);
120 public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
121 // Have the WebView asset loader process the request.
122 // This allows using the `appassets.androidplatform.net` URL, which handles the loading of SVG files, which otherwise is prevented by the CORS policy.
123 return webViewAssetLoader.shouldInterceptRequest(webResourceRequest.getUrl());
127 // Get the current theme status.
128 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
130 // Check to see if the app is in night mode.
131 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { // The app is in night mode.
132 // Apply the dark WebView theme.
133 WebSettingsCompat.setForceDark(tabWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
136 // Load the indicated tab. The tab numbers start at 0.
139 // Load the Overview tab.
140 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_overview.html");
144 // Load the JavaScript tab.
145 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_javascript.html");
149 // Load the Local Storage tab.
150 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_local_storage.html");
154 // Load the User Agent tab.
155 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_user_agent.html");
159 // Load the Requests tab.
160 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_requests.html");
164 // Load the Domain Settings tab.
165 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html");
169 // Load the SSL Certificates tab.
170 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates.html");
174 // Load the Proxies tab.
175 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_proxies.html");
179 // Load the Tracking IDs tab.
180 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html");
184 // Scroll the WebView if the saved instance state is not null.
185 if (savedInstanceState != null) {
186 tabWebView.post(() -> tabWebView.setScrollY(savedInstanceState.getInt("scroll_y")));
189 // Return the formatted WebView layout.
190 return webViewLayout;
194 public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
195 // Run the default commands.
196 super.onSaveInstanceState(savedInstanceState);
198 // Get a handle for the tab WebView. A class variable cannot be used because it gets out of sync when restarting.
199 WebView tabWebView = (WebView) webViewLayout;
201 // Save the scroll Y position if the tab WebView is not null, which can happen if a tab is not currently selected.
202 if (tabWebView != null) {
203 savedInstanceState.putInt("scroll_y", tabWebView.getScrollY());