2 * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
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.
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.
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/>.
20 package com.stoutner.privacycell.dialogs
22 import android.app.Dialog
23 import android.content.res.Configuration
24 import android.os.Bundle
25 import android.webkit.WebView
27 import androidx.appcompat.app.AlertDialog
28 import androidx.fragment.app.DialogFragment
29 import androidx.webkit.WebSettingsCompat
30 import androidx.webkit.WebViewFeature
32 import com.stoutner.privacycell.R
34 // Define the class constants.
35 private const val DIALOG_TYPE = "dialog_type"
36 private const val SCROLL_Y = "scroll_y"
38 class WebViewDialog : DialogFragment() {
40 // Define the public constants.
41 const val PERMISSIONS = 0
42 const val PRIVACY_POLICY = 1
43 const val CHANGELOG = 2
46 // Define the class views.
47 private lateinit var webView: WebView
49 // Populate the WebView dialog type.
50 fun type(dialogType: Int): WebViewDialog {
51 // Create an arguments bundle.
52 val argumentsBundle = Bundle()
54 // Add the dialog type to the bundle.
55 argumentsBundle.putInt(DIALOG_TYPE, dialogType)
57 // Create a new instance of the WebView dialog.
58 val webViewDialog = WebViewDialog()
60 // Add the arguments bundle to the new dialog.
61 webViewDialog.arguments = argumentsBundle
63 // Return the new dialog.
67 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
68 // Get the dialog type from the arguments bundle.
69 val dialogType = requireArguments().getInt(DIALOG_TYPE)
71 // Use a builder to create the alert dialog.
72 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.Theme_PrivacyCellAlertDialog)
74 // Set the icon and the title according to the dialog type.
78 dialogBuilder.setIcon(R.drawable.permissions)
81 dialogBuilder.setTitle(R.string.permissions)
86 dialogBuilder.setIcon(R.drawable.privacy_policy)
89 dialogBuilder.setTitle(R.string.privacy_policy)
94 dialogBuilder.setIcon(R.drawable.changelog)
97 dialogBuilder.setTitle(R.string.changelog)
102 dialogBuilder.setView(R.layout.webview_dialog)
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)
107 // Create an alert dialog from the builder.
108 val alertDialog = dialogBuilder.create()
110 // The alert dialog needs to be shown before the contents can be modified.
113 // Get a handle for the WebView.
114 webView = alertDialog.findViewById(R.id.webview)!!
116 // Get the current theme status.
117 val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
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)
125 // Create a WebView asset loader. TODO.
126 // val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(requireContext())).build()
128 // Load the WebView data according to the dialog type.
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")
135 // Scroll the WebView if the saved instance state is not null.
136 if (savedInstanceState != null) {
138 webView.scrollY = savedInstanceState.getInt(SCROLL_Y)
142 // Return the alert dialog.
146 override fun onSaveInstanceState(savedInstanceState: Bundle) {
147 // Run the default commands.
148 super.onSaveInstanceState(savedInstanceState)
150 // Save the scroll position.
151 savedInstanceState.putInt(SCROLL_Y, webView.scrollY)