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