]> gitweb.stoutner.com Git - PrivacyCell.git/blob - app/src/main/java/com/stoutner/privacycell/dialogs/WebViewDialog.kt
Add two more navigation menu entries.
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / dialogs / WebViewDialog.kt
1 /*
2  * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
5  *
6  * Privacy Cell 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 Cell 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.privacycell.dialogs
21
22 import android.app.Dialog
23 import android.content.res.Configuration
24 import android.os.Bundle
25 import android.webkit.WebView
26
27 import androidx.appcompat.app.AlertDialog
28 import androidx.fragment.app.DialogFragment
29 import androidx.webkit.WebSettingsCompat
30 import androidx.webkit.WebViewFeature
31
32 import com.stoutner.privacycell.R
33
34 // Define the class constants.
35 private const val DIALOG_TYPE = "dialog_type"
36 private const val SCROLL_Y = "scroll_y"
37
38 class WebViewDialog : DialogFragment() {
39     companion object {
40         // Define the public constants.
41         const val PERMISSIONS = 0
42         const val PRIVACY_POLICY = 1
43         const val CHANGELOG = 2
44     }
45
46     // Define the class views.
47     private lateinit var webView: WebView
48
49     // Populate the WebView dialog type.
50     fun type(dialogType: Int): WebViewDialog {
51         // Create an arguments bundle.
52         val argumentsBundle = Bundle()
53
54         // Add the dialog type to the bundle.
55         argumentsBundle.putInt(DIALOG_TYPE, dialogType)
56
57         // Create a new instance of the WebView dialog.
58         val webViewDialog = WebViewDialog()
59
60         // Add the arguments bundle to the new dialog.
61         webViewDialog.arguments = argumentsBundle
62
63         // Return the new dialog.
64         return webViewDialog
65     }
66
67     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
68         // Get the dialog type from the arguments bundle.
69         val dialogType = requireArguments().getInt(DIALOG_TYPE)
70
71         // Use a builder to create the alert dialog.
72         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.Theme_PrivacyCellAlertDialog)
73
74         // Set the icon and the title according to the dialog type.
75         when (dialogType) {
76             PERMISSIONS -> {
77                 // Set the icon.
78                 dialogBuilder.setIcon(R.drawable.permissions)
79
80                 // Set the title.
81                 dialogBuilder.setTitle(R.string.permissions)
82             }
83
84             PRIVACY_POLICY -> {
85                 // Set the icon.
86                 dialogBuilder.setIcon(R.drawable.privacy_policy)
87
88                 // Set the title.
89                 dialogBuilder.setTitle(R.string.privacy_policy)
90             }
91
92             CHANGELOG -> {
93                 // Set the icon.
94                 dialogBuilder.setIcon(R.drawable.changelog)
95
96                 // Set the title.
97                 dialogBuilder.setTitle(R.string.changelog)
98             }
99         }
100
101         // Set the view.
102         dialogBuilder.setView(R.layout.webview_dialog)
103
104         // Set a listener on the close button.  Using `null` as the listener closes the dialog without doing anything else.
105         dialogBuilder.setNegativeButton(R.string.close, null)
106
107         // Create an alert dialog from the builder.
108         val alertDialog = dialogBuilder.create()
109
110         // The alert dialog needs to be shown before the contents can be modified.
111         alertDialog.show()
112
113         // Get a handle for the WebView.
114         webView = alertDialog.findViewById(R.id.webview)!!
115
116         // Get the current theme status.
117         val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
118
119         // Check to see if the app is in night mode.
120         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {  // The app is in night mode.
121             // Apply the dark WebView theme.
122             WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON)
123         }
124
125         // Create a WebView asset loader.  TODO.
126         // val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(requireContext())).build()
127
128         // Load the WebView data according to the dialog type.
129         when (dialogType) {
130             PERMISSIONS -> webView.loadUrl("file:///android_asset/en/permissions.html")
131             PRIVACY_POLICY -> webView.loadUrl("file:///android_asset/en/privacy_policy.html")
132             CHANGELOG -> webView.loadUrl("file:///android_asset/en/changelog.html")
133         }
134
135         // Scroll the WebView if the saved instance state is not null.
136         if (savedInstanceState != null) {
137             webView.post {
138                 webView.scrollY = savedInstanceState.getInt(SCROLL_Y)
139             }
140         }
141
142         // Return the alert dialog.
143         return alertDialog
144     }
145
146     override fun onSaveInstanceState(savedInstanceState: Bundle) {
147         // Run the default commands.
148         super.onSaveInstanceState(savedInstanceState)
149
150         // Save the scroll position.
151         savedInstanceState.putInt(SCROLL_Y, webView.scrollY)
152     }
153 }