]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - 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
diff --git a/app/src/main/java/com/stoutner/privacycell/dialogs/WebViewDialog.kt b/app/src/main/java/com/stoutner/privacycell/dialogs/WebViewDialog.kt
new file mode 100644 (file)
index 0000000..5961c2e
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
+ *
+ * Privacy Cell is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Cell is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.stoutner.privacycell.dialogs
+
+import android.app.Dialog
+import android.content.res.Configuration
+import android.os.Bundle
+import android.webkit.WebView
+
+import androidx.appcompat.app.AlertDialog
+import androidx.fragment.app.DialogFragment
+import androidx.webkit.WebSettingsCompat
+import androidx.webkit.WebViewFeature
+
+import com.stoutner.privacycell.R
+
+// Define the class constants.
+private const val DIALOG_TYPE = "dialog_type"
+private const val SCROLL_Y = "scroll_y"
+
+class WebViewDialog : DialogFragment() {
+    companion object {
+        // Define the public constants.
+        const val PERMISSIONS = 0
+        const val PRIVACY_POLICY = 1
+        const val CHANGELOG = 2
+    }
+
+    // Define the class views.
+    private lateinit var webView: WebView
+
+    // Populate the WebView dialog type.
+    fun type(dialogType: Int): WebViewDialog {
+        // Create an arguments bundle.
+        val argumentsBundle = Bundle()
+
+        // Add the dialog type to the bundle.
+        argumentsBundle.putInt(DIALOG_TYPE, dialogType)
+
+        // Create a new instance of the WebView dialog.
+        val webViewDialog = WebViewDialog()
+
+        // Add the arguments bundle to the new dialog.
+        webViewDialog.arguments = argumentsBundle
+
+        // Return the new dialog.
+        return webViewDialog
+    }
+
+    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
+        // Get the dialog type from the arguments bundle.
+        val dialogType = requireArguments().getInt(DIALOG_TYPE)
+
+        // Use a builder to create the alert dialog.
+        val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.Theme_PrivacyCellAlertDialog)
+
+        // Set the icon and the title according to the dialog type.
+        when (dialogType) {
+            PERMISSIONS -> {
+                // Set the icon.
+                dialogBuilder.setIcon(R.drawable.permissions)
+
+                // Set the title.
+                dialogBuilder.setTitle(R.string.permissions)
+            }
+
+            PRIVACY_POLICY -> {
+                // Set the icon.
+                dialogBuilder.setIcon(R.drawable.privacy_policy)
+
+                // Set the title.
+                dialogBuilder.setTitle(R.string.privacy_policy)
+            }
+
+            CHANGELOG -> {
+                // Set the icon.
+                dialogBuilder.setIcon(R.drawable.changelog)
+
+                // Set the title.
+                dialogBuilder.setTitle(R.string.changelog)
+            }
+        }
+
+        // Set the view.
+        dialogBuilder.setView(R.layout.webview_dialog)
+
+        // Set a listener on the close button.  Using `null` as the listener closes the dialog without doing anything else.
+        dialogBuilder.setNegativeButton(R.string.close, null)
+
+        // Create an alert dialog from the builder.
+        val alertDialog = dialogBuilder.create()
+
+        // The alert dialog needs to be shown before the contents can be modified.
+        alertDialog.show()
+
+        // Get a handle for the WebView.
+        webView = alertDialog.findViewById(R.id.webview)!!
+
+        // Get the current theme status.
+        val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
+
+        // Check to see if the app is in night mode.
+        if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {  // The app is in night mode.
+            // Apply the dark WebView theme.
+            WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON)
+        }
+
+        // Create a WebView asset loader.  TODO.
+        // val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(requireContext())).build()
+
+        // Load the WebView data according to the dialog type.
+        when (dialogType) {
+            PERMISSIONS -> webView.loadUrl("file:///android_asset/en/permissions.html")
+            PRIVACY_POLICY -> webView.loadUrl("file:///android_asset/en/privacy_policy.html")
+            CHANGELOG -> webView.loadUrl("file:///android_asset/en/changelog.html")
+        }
+
+        // Scroll the WebView if the saved instance state is not null.
+        if (savedInstanceState != null) {
+            webView.post {
+                webView.scrollY = savedInstanceState.getInt(SCROLL_Y)
+            }
+        }
+
+        // Return the alert dialog.
+        return alertDialog
+    }
+
+    override fun onSaveInstanceState(savedInstanceState: Bundle) {
+        // Run the default commands.
+        super.onSaveInstanceState(savedInstanceState)
+
+        // Save the scroll position.
+        savedInstanceState.putInt(SCROLL_Y, webView.scrollY)
+    }
+}
\ No newline at end of file