]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/AboutWebViewFragment.kt
Bump target API to 33. https://redmine.stoutner.com/issues/902
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / AboutWebViewFragment.kt
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.Intent
23 import android.content.res.Configuration
24 import android.net.Uri
25 import android.os.Build
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.fragment.app.Fragment
36 import androidx.webkit.WebSettingsCompat
37 import androidx.webkit.WebViewAssetLoader
38 import androidx.webkit.WebViewAssetLoader.AssetsPathHandler
39 import androidx.webkit.WebViewFeature
40
41 import com.stoutner.privacybrowser.R
42
43 // Define the class constants.
44 private const val TAB_NUMBER = "tab_number"
45 private const val SCROLL_X = "scroll_x"
46 private const val SCROLL_Y = "scroll_y"
47
48 class AboutWebViewFragment : Fragment() {
49     // Define the class variables.
50     private var tabNumber = 0
51
52     // Declare the class views.
53     private lateinit var webViewLayout: View
54
55     companion object {
56         fun createTab(tabNumber: Int): AboutWebViewFragment {
57             // Create an arguments bundle.
58             val argumentsBundle = Bundle()
59
60             // Store the arguments in the bundle.
61             argumentsBundle.putInt(TAB_NUMBER, tabNumber)
62
63             // Create a new instance of the tab fragment.
64             val aboutWebViewFragment = AboutWebViewFragment()
65
66             // Add the arguments bundle to the fragment.
67             aboutWebViewFragment.arguments = argumentsBundle
68
69             // Return the new fragment.
70             return aboutWebViewFragment
71         }
72     }
73
74     override fun onCreate(savedInstanceState: Bundle?) {
75         // Run the default commands.
76         super.onCreate(savedInstanceState)
77
78         // Store the tab number in a class variable.
79         tabNumber = requireArguments().getInt(TAB_NUMBER)
80     }
81
82     override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
83         // Inflate the layout.  False does not attach the inflated layout as a child of container.  The fragment will take care of attaching the root automatically.
84         webViewLayout = layoutInflater.inflate(R.layout.bare_webview, container, false)
85
86         // Get a handle for tab WebView.
87         val tabWebView = webViewLayout as WebView
88
89         // Create a WebView asset loader.
90         val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", AssetsPathHandler(requireContext())).build()
91
92         // Set a WebView client.
93         tabWebView.webViewClient = object : WebViewClient() {
94             // // Send external links back to the main Privacy Browser WebView.  The deprecated `shouldOverrideUrlLoading` must be used until API >= 24.
95             @Deprecated("Deprecated in Java")
96             override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
97                 // Create an intent to view the URL.
98                 val urlIntent = Intent(Intent.ACTION_VIEW)
99
100                 // Add the URL to the intent.
101                 urlIntent.data = Uri.parse(url)
102
103                 // Make it so.
104                 startActivity(urlIntent)
105                 
106                 // Consume the click.
107                 return true
108             }
109             
110             // Process asset requests with the asset loader.
111             override fun shouldInterceptRequest(webView: WebView, webResourceRequest: WebResourceRequest): WebResourceResponse? {
112                 // This allows using the `appassets.androidplatform.net` URL, which handles the loading of SVG files, which otherwise is prevented by the CORS policy.
113                 return webViewAssetLoader.shouldInterceptRequest(webResourceRequest.url)
114             }
115         }
116
117         // Get the current theme status.
118         val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
119
120         // Check to see if the app is in night mode.  This can be removed once the minimum API >= 33.
121         if ((Build.VERSION.SDK_INT < 33) && (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) && (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK))) {  // The app is in night mode.
122             // Apply the dark WebView theme.
123             @Suppress("DEPRECATION")
124             WebSettingsCompat.setForceDark(tabWebView.settings, WebSettingsCompat.FORCE_DARK_ON)
125         }
126
127         // Load the indicated tab.  The tab numbers start at 0, with the WebView tabs starting at 1.
128         when (tabNumber) {
129             1 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_permissions.html")
130             2 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_privacy_policy.html")
131             3 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_changelog.html")
132             4 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_licenses.html")
133             5 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_contributors.html")
134             6 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/about_links.html")
135         }
136
137         // Scroll the tab if the saved instance state is not null.
138         if (savedInstanceState != null) {
139             tabWebView.post {
140                 tabWebView.scrollX = savedInstanceState.getInt(SCROLL_X)
141                 tabWebView.scrollY = savedInstanceState.getInt(SCROLL_Y)
142             }
143         }
144
145         // Return the formatted WebView layout.
146         return webViewLayout
147     }
148
149     override fun onSaveInstanceState(savedInstanceState: Bundle) {
150         // Run the default commands.
151         super.onSaveInstanceState(savedInstanceState)
152
153         // Get a handle for the tab WebView.  A class variable cannot be used because it gets out of sync when restarting.
154         val tabWebView = webViewLayout as WebView?
155
156         // Save the scroll positions if the layout is not null, which can happen if a tab is not currently selected.
157         if (tabWebView != null) {
158             savedInstanceState.putInt(SCROLL_X, tabWebView.scrollX)
159             savedInstanceState.putInt(SCROLL_Y, tabWebView.scrollY)
160         }
161     }
162 }