2 * Copyright 2016-2024 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android/>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.fragments
22 import android.content.Intent
23 import android.content.res.Configuration
24 import android.os.Build
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
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
40 import com.stoutner.privacybrowser.R
42 // Define the class constants.
43 private const val TAB_NUMBER = "A"
44 private const val SCROLL_Y = "B"
46 class GuideWebViewFragment : Fragment() {
47 // Define the class variables.
48 private var tabNumber = 0
50 // Declare the class views.
51 private lateinit var webViewLayout: View
54 fun createTab(tabNumber: Int): GuideWebViewFragment {
55 // Create an arguments bundle.
56 val argumentsBundle = Bundle()
58 // Store the tab number in the bundle.
59 argumentsBundle.putInt(TAB_NUMBER, tabNumber)
61 // Create a new instance of the tab fragment.
62 val guideWebViewFragment = GuideWebViewFragment()
64 // Add the arguments bundle to the fragment.
65 guideWebViewFragment.arguments = argumentsBundle
67 // Return the new fragment.
68 return guideWebViewFragment
72 override fun onCreate(savedInstanceState: Bundle?) {
73 // Run the default commands.
74 super.onCreate(savedInstanceState)
76 // Store the tab number in a class variable.
77 tabNumber = requireArguments().getInt(TAB_NUMBER)
80 override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
81 // Inflate the layout. The fragment will take care of attaching the root automatically.
82 webViewLayout = layoutInflater.inflate(R.layout.bare_webview, container, false)
84 // Get a handle for the tab WebView.
85 val tabWebView = webViewLayout as WebView
87 // Create a WebView asset loader.
88 val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", AssetsPathHandler(requireContext())).build()
90 // Set a WebView client.
91 tabWebView.webViewClient = object : WebViewClient() {
92 // Send external links back to the main Privacy Browser WebView.
93 override fun shouldOverrideUrlLoading(view: WebView, webResourceRequest: WebResourceRequest): Boolean {
94 // Create an intent to view the URL.
95 val urlIntent = Intent(Intent.ACTION_VIEW)
97 // Add the URL to the intent.
98 urlIntent.data = webResourceRequest.url
101 startActivity(urlIntent)
103 // Consume the click.
107 // Process asset requests with the asset loader.
108 override fun shouldInterceptRequest(webView: WebView, webResourceRequest: WebResourceRequest): WebResourceResponse? {
109 // This allows using the `appassets.androidplatform.net` URL, which handles the loading of SVG files, which otherwise is prevented by the CORS policy.
110 return webViewAssetLoader.shouldInterceptRequest(webResourceRequest.url)
114 // Get the current theme status.
115 val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
117 // Check to see if the app is in night mode. This can be removed once the minimum API >= 33.
118 if ((Build.VERSION.SDK_INT < 33) && (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { // The app is in night mode.
119 // Apply the dark WebView theme.
120 @Suppress("DEPRECATION")
121 WebSettingsCompat.setForceDark(tabWebView.settings, WebSettingsCompat.FORCE_DARK_ON)
124 // Load the indicated tab. The tab numbers start at 0.
126 0 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_overview.html")
127 1 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_javascript.html")
128 2 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_local_storage.html")
129 3 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_user_agent.html")
130 4 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_requests.html")
131 5 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html")
132 6 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates.html")
133 7 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_proxies.html")
134 8 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html")
135 9 -> tabWebView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.android_asset_path) + "/guide_interface.html")
138 // Restore the WebView scroll position if the activity has been restarted.
139 if (savedInstanceState != null) {
141 tabWebView.scrollY = savedInstanceState.getInt(SCROLL_Y)
145 // Return the formatted WebView layout.
149 override fun onSaveInstanceState(savedInstanceState: Bundle) {
150 // Run the default commands.
151 super.onSaveInstanceState(savedInstanceState)
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?
156 // Save the scroll Y position if the tab WebView is not null, which can happen if a tab is not currently selected.
157 if (tabWebView != null) {
158 savedInstanceState.putInt(SCROLL_Y, tabWebView.scrollY)