]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/AboutWebViewFragment.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 / AboutWebViewFragment.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 AboutWebViewFragment extends Fragment {
43     // Declare the class constants.
44     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     public static AboutWebViewFragment createTab(int tabNumber) {
53         // Create an arguments bundle.
54         Bundle argumentsBundle = new Bundle();
55
56         // Store the arguments in the bundle.
57         argumentsBundle.putInt(TAB_NUMBER, tabNumber);
58
59         // Create a new instance of the tab fragment.
60         AboutWebViewFragment aboutWebViewFragment = new AboutWebViewFragment();
61
62         // Add the arguments bundle to the fragment.
63         aboutWebViewFragment.setArguments(argumentsBundle);
64
65         // Return the new fragment.
66         return aboutWebViewFragment;
67     }
68
69     @Override
70     public void onCreate(Bundle savedInstanceState) {
71         // Run the default commands.
72         super.onCreate(savedInstanceState);
73
74         // Get a handle for the arguments.
75         Bundle arguments = getArguments();
76
77         // Remove the incorrect lint warning below that arguments might be null.
78         assert arguments != null;
79
80         // Store the tab number in a class variable.
81         tabNumber = arguments.getInt(TAB_NUMBER);
82     }
83
84     @Override
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);
88
89         // Get a handle for tab WebView.
90         WebView tabWebView = (WebView) webViewLayout;
91
92         // Get a handle for the context.
93         Context context = getContext();
94
95         // Remove the incorrect lint warning below that the context might be null.
96         assert context != null;
97
98         // Create a WebView asset loader.
99         final WebViewAssetLoader webViewAssetLoader = new WebViewAssetLoader.Builder().addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(context)).build();
100
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.
104             @Override
105             public boolean shouldOverrideUrlLoading(WebView view, String url) {
106                 // Create an intent to view the URL.
107                 Intent urlIntent = new Intent(Intent.ACTION_VIEW);
108
109                 // Add the URL to the intent.
110                 urlIntent.setData(Uri.parse(url));
111
112                 // Make it so.
113                 startActivity(urlIntent);
114                 return true;
115             }
116
117             @Override
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));
121             }
122         });
123
124         // Get the current theme status.
125         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
126
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);
131         }
132
133         // Load the indicated tab.  The tab numbers start at 0, with the WebView tabs starting at 1.
134         switch (tabNumber) {
135             case 1:
136                 // Load the Permissions tab.
137                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_permissions.html");
138                 break;
139
140             case 2:
141                 // Load the Privacy Policy tab.
142                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_privacy_policy.html");
143                 break;
144
145             case 3:
146                 // Load the Changelog tab.
147                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_changelog.html");
148                 break;
149
150             case 4:
151                 // Load the Licenses tab.
152                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_licenses.html");
153                 break;
154
155             case 5:
156                 // Load the Contributors tab.
157                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_contributors.html");
158                 break;
159
160             case 6:
161                 // Load the Links tab.
162                 tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_links.html");
163                 break;
164         }
165
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"));
171             });
172         }
173
174         // Return the formatted WebView layout.
175         return webViewLayout;
176     }
177
178     @Override
179     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
180         // Run the default commands.
181         super.onSaveInstanceState(savedInstanceState);
182
183
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;
186
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());
191         }
192     }
193 }