]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/GuideWebViewFragment.java
Combine the light and dark Guide and About pages. https://redmine.stoutner.com/issue...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / GuideWebViewFragment.java
1 /*
2  * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
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.WebResourceResponse;
31 import android.webkit.WebView;
32 import android.webkit.WebViewClient;
33
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;
39
40 import com.stoutner.privacybrowser.R;
41
42 public class GuideWebViewFragment extends Fragment {
43     // Declare the class constants.
44     private final static String TAB_NUMBER = "tab_number";
45
46     // Declare the class variables.
47     private int tabNumber;
48
49     // Declare the class views.
50     private View webViewLayout;
51
52     // Store the tab number in the arguments bundle.
53     public static GuideWebViewFragment createTab (int tabNumber) {
54         // Create a bundle.
55         Bundle bundle = new Bundle();
56
57         // Store the tab number in the bundle.
58         bundle.putInt(TAB_NUMBER, tabNumber);
59
60         // Create a new guide tab fragment.
61         GuideWebViewFragment guideWebViewFragment = new GuideWebViewFragment();
62
63         // Add the bundle to the fragment.
64         guideWebViewFragment.setArguments(bundle);
65
66         // Return the new fragment.
67         return guideWebViewFragment;
68     }
69
70     @Override
71     public void onCreate(Bundle savedInstanceState) {
72         // Run the default commands.
73         super.onCreate(savedInstanceState);
74
75         // Get a handle for the arguments.
76         Bundle arguments = getArguments();
77
78         // Remove the lint warning below that arguments might be null.
79         assert arguments != null;
80
81         // Store the tab number in a class variable.
82         tabNumber = arguments.getInt(TAB_NUMBER);
83     }
84
85     @Override
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);
89
90         // Get a handle for the tab WebView.
91         WebView tabWebView = (WebView) webViewLayout;
92
93         // Get a handle for the context.
94         Context context = getContext();
95
96         // Remove the incorrect lint warning below that the context might be null.
97         assert context != null;
98
99         // Create a WebView asset loader.
100         final WebViewAssetLoader webViewAssetLoader = new WebViewAssetLoader.Builder().addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(context)).build();
101
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.
105             @Override
106             public boolean shouldOverrideUrlLoading(WebView view, String url) {
107                 // Create an intent to view the URL.
108                 Intent urlIntent = new Intent(Intent.ACTION_VIEW);
109
110                 // Add the URL to the intent.
111                 urlIntent.setData(Uri.parse(url));
112
113                 // Make it so.
114                 startActivity(urlIntent);
115                 return true;
116             }
117
118             @Override
119             public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
120                 // Have the WebView asset loader process the request.  This allows loading of SVG files, which otherwise is prevented by the CORS policy.
121                 return webViewAssetLoader.shouldInterceptRequest(Uri.parse(url));
122             }
123         });
124
125         // Get the current theme status.
126         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
127
128         // Check to see if the app is in night mode.
129         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {  // The app is in night mode.
130             // Apply the dark WebView theme.
131             WebSettingsCompat.setForceDark(tabWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
132         }
133
134         // Load the indicated tab.  The tab numbers start at 0.
135         switch (tabNumber) {
136             case 0:
137                 // Load the Overview tab.
138                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_overview.html");
139                 break;
140
141             case 1:
142                 // Load the JavaScript tab.
143                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_javascript.html");
144                 break;
145
146             case 2:
147                 // Load the Local Storage tab.
148                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_local_storage.html");
149                 break;
150
151             case 3:
152                 // Load the User Agent tab.
153                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_user_agent.html");
154                 break;
155
156             case 4:
157                 // Load the Requests tab.
158                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_requests.html");
159                 break;
160
161             case 5:
162                 // Load the Domain Settings tab.
163                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html");
164                 break;
165
166             case 6:
167                 // Load the SSL Certificates tab.
168                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates.html");
169                 break;
170
171             case 7:
172                 // Load the Proxies tab.
173                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_proxies.html");
174                 break;
175
176             case 8:
177                 // Load the Tracking IDs tab.
178                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html");
179                 break;
180         }
181
182         // Scroll the WebView if the saved instance state is not null.
183         if (savedInstanceState != null) {
184             tabWebView.post(() -> tabWebView.setScrollY(savedInstanceState.getInt("scroll_y")));
185         }
186
187         // Return the formatted WebView layout.
188         return webViewLayout;
189     }
190
191     @Override
192     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
193         // Run the default commands.
194         super.onSaveInstanceState(savedInstanceState);
195
196         // Get a handle for the tab WebView.  A class variable cannot be used because it gets out of sync when restarting.
197         WebView tabWebView = (WebView) webViewLayout;
198
199         // Save the scroll Y position if the tab WebView is not null, which can happen if a tab is not currently selected.
200         if (tabWebView != null) {
201             savedInstanceState.putInt("scroll_y", tabWebView.getScrollY());
202         }
203     }
204 }