Finish the navigation menu entries with WebView dialogs.
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / dialogs / WebViewDialog.kt
index 5961c2e51c0500773f7dbe6770b3487b4ff0658b..0559d459d0ecd21e59a6b2c76dc77eb8cd813c0d 100644 (file)
 package com.stoutner.privacycell.dialogs
 
 import android.app.Dialog
+import android.content.Intent
 import android.content.res.Configuration
 import android.os.Bundle
+import android.webkit.WebResourceRequest
+import android.webkit.WebResourceResponse
 import android.webkit.WebView
+import android.webkit.WebViewClient
 
 import androidx.appcompat.app.AlertDialog
 import androidx.fragment.app.DialogFragment
 import androidx.webkit.WebSettingsCompat
+import androidx.webkit.WebViewAssetLoader
 import androidx.webkit.WebViewFeature
 
 import com.stoutner.privacycell.R
@@ -41,6 +46,8 @@ class WebViewDialog : DialogFragment() {
         const val PERMISSIONS = 0
         const val PRIVACY_POLICY = 1
         const val CHANGELOG = 2
+        const val LICENSES = 3
+        const val CONTRIBUTORS = 4
     }
 
     // Define the class views.
@@ -96,6 +103,22 @@ class WebViewDialog : DialogFragment() {
                 // Set the title.
                 dialogBuilder.setTitle(R.string.changelog)
             }
+
+            LICENSES -> {
+                // Set the icon.
+                dialogBuilder.setIcon(R.drawable.licenses)
+
+                // Set the title.
+                dialogBuilder.setTitle(R.string.licenses)
+            }
+
+            CONTRIBUTORS -> {
+                // Set the icon.
+                dialogBuilder.setIcon(R.drawable.contributors)
+
+                // Set the tile.
+                dialogBuilder.setTitle(R.string.contributors)
+            }
         }
 
         // Set the view.
@@ -122,14 +145,40 @@ class WebViewDialog : DialogFragment() {
             WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON)
         }
 
-        // Create a WebView asset loader.  TODO.
-        // val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(requireContext())).build()
+        // Create a WebView asset loader.
+        val webViewAssetLoader = WebViewAssetLoader.Builder().addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(requireContext())).build()
+
+        // Set a WebView client.
+        webView.webViewClient = object : WebViewClient() {
+            // Send external links to a web browser.
+            override fun shouldOverrideUrlLoading(view: WebView, webResourceRequest: WebResourceRequest): Boolean {
+                // Create an intent to view the URL.
+                val urlIntent = Intent(Intent.ACTION_VIEW)
+
+                // Add the URL to the intent.
+                urlIntent.data = webResourceRequest.url
+
+                // Make it so.
+                startActivity(urlIntent)
+
+                // Consume the click.
+                return true
+            }
+
+            // Process asset requests with the asset loader.
+            override fun shouldInterceptRequest(webView: WebView, webResourceRequest: WebResourceRequest): WebResourceResponse? {
+                // This allows using the `appassets.androidplatform.net` URL, which handles the loading of SVG files, which otherwise is prevented by the CORS policy.
+                return webViewAssetLoader.shouldInterceptRequest(webResourceRequest.url)
+            }
+        }
 
         // 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")
+            PERMISSIONS -> webView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.asset_directory) + "/permissions.html")
+            PRIVACY_POLICY -> webView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.asset_directory) + "/privacy_policy.html")
+            CHANGELOG -> webView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.asset_directory) + "/changelog.html")
+            LICENSES -> webView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.asset_directory) + "/licenses.html")
+            CONTRIBUTORS -> webView.loadUrl("https://appassets.androidplatform.net/assets/" + getString(R.string.asset_directory) + "/contributors.html")
         }
 
         // Scroll the WebView if the saved instance state is not null.