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