]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/GuideWebViewFragment.java
Reenable dark WebViews on API 33. https://redmine.stoutner.com/issues/903
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / GuideWebViewFragment.java
1 /*
2  * Copyright © 2016-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments;
21
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;
34
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;
40
41 import com.stoutner.privacybrowser.R;
42
43 public class GuideWebViewFragment extends Fragment {
44     // Declare the class constants.
45     private final static String TAB_NUMBER = "tab_number";
46
47     // Declare the class variables.
48     private int tabNumber;
49
50     // Declare the class views.
51     private View webViewLayout;
52
53     // Store the tab number in the arguments bundle.
54     public static GuideWebViewFragment createTab (int tabNumber) {
55         // Create a bundle.
56         Bundle bundle = new Bundle();
57
58         // Store the tab number in the bundle.
59         bundle.putInt(TAB_NUMBER, tabNumber);
60
61         // Create a new guide tab fragment.
62         GuideWebViewFragment guideWebViewFragment = new GuideWebViewFragment();
63
64         // Add the bundle to the fragment.
65         guideWebViewFragment.setArguments(bundle);
66
67         // Return the new fragment.
68         return guideWebViewFragment;
69     }
70
71     @Override
72     public void onCreate(Bundle savedInstanceState) {
73         // Run the default commands.
74         super.onCreate(savedInstanceState);
75
76         // Get a handle for the arguments.
77         Bundle arguments = getArguments();
78
79         // Remove the lint warning below that arguments might be null.
80         assert arguments != null;
81
82         // Store the tab number in a class variable.
83         tabNumber = arguments.getInt(TAB_NUMBER);
84     }
85
86     @Override
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);
90
91         // Get a handle for the tab WebView.
92         WebView tabWebView = (WebView) webViewLayout;
93
94         // Get a handle for the context.
95         Context context = getContext();
96
97         // Remove the incorrect lint warning below that the context might be null.
98         assert context != null;
99
100         // Create a WebView asset loader.
101         final WebViewAssetLoader webViewAssetLoader = new WebViewAssetLoader.Builder().addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(context)).build();
102
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.
106             @Override
107             public boolean shouldOverrideUrlLoading(WebView view, String url) {
108                 // Create an intent to view the URL.
109                 Intent urlIntent = new Intent(Intent.ACTION_VIEW);
110
111                 // Add the URL to the intent.
112                 urlIntent.setData(Uri.parse(url));
113
114                 // Make it so.
115                 startActivity(urlIntent);
116                 return true;
117             }
118
119             @Override
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());
124             }
125         });
126
127         // Get the current theme status.
128         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
129
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);
134         }
135
136         // Load the indicated tab.  The tab numbers start at 0.
137         switch (tabNumber) {
138             case 0:
139                 // Load the Overview tab.
140                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_overview.html");
141                 break;
142
143             case 1:
144                 // Load the JavaScript tab.
145                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_javascript.html");
146                 break;
147
148             case 2:
149                 // Load the Local Storage tab.
150                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_local_storage.html");
151                 break;
152
153             case 3:
154                 // Load the User Agent tab.
155                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_user_agent.html");
156                 break;
157
158             case 4:
159                 // Load the Requests tab.
160                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_requests.html");
161                 break;
162
163             case 5:
164                 // Load the Domain Settings tab.
165                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html");
166                 break;
167
168             case 6:
169                 // Load the SSL Certificates tab.
170                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates.html");
171                 break;
172
173             case 7:
174                 // Load the Proxies tab.
175                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_proxies.html");
176                 break;
177
178             case 8:
179                 // Load the Tracking IDs tab.
180                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html");
181                 break;
182         }
183
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")));
187         }
188
189         // Return the formatted WebView layout.
190         return webViewLayout;
191     }
192
193     @Override
194     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
195         // Run the default commands.
196         super.onSaveInstanceState(savedInstanceState);
197
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;
200
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());
204         }
205     }
206 }