]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.kt
Bump the target API to 36. https://redmine.stoutner.com/issues/1283
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / HttpAuthenticationDialog.kt
index 07a3f0b6dbdf1b3d510f3868b4d8b8159c199200..16df743080404be7427bbc3e951f88e0bfc596bd 100644 (file)
@@ -1,20 +1,20 @@
-/*
- * Copyright © 2017-2023 Soren Stoutner <soren@stoutner.com>.
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ * SPDX-FileCopyrightText: 2017-2023 Soren Stoutner <soren@stoutner.com>
  *
- * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
+ * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android/>.
  *
- * Privacy Browser Android 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.
+ * This program 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 Browser Android 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.
+ * This program 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 Android.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 package com.stoutner.privacybrowser.dialogs
@@ -68,7 +68,7 @@ class HttpAuthenticationDialog : DialogFragment() {
     }
 
     // Define the class variables.
-    private var dismissDialog: Boolean = false
+    private var httpAuthHandler: HttpAuthHandler? = null
 
     // Declare the class views.
     private lateinit var usernameEditText: EditText
@@ -86,10 +86,10 @@ class HttpAuthenticationDialog : DialogFragment() {
         // Try to populate the alert dialog.
         try {  // Getting the WebView tab fragment will fail if Privacy Browser has been restarted.
             // Get the current position of this WebView fragment.
-            val webViewPosition = MainWebViewActivity.webViewPagerAdapter!!.getPositionForId(webViewFragmentId)
+            val webViewPosition = MainWebViewActivity.webViewStateAdapter!!.getPositionForId(webViewFragmentId)
 
             // Get the WebView tab fragment.
-            val webViewTabFragment = MainWebViewActivity.webViewPagerAdapter!!.getPageFragment(webViewPosition)
+            val webViewTabFragment = MainWebViewActivity.webViewStateAdapter!!.getPageFragment(webViewPosition)
 
             // Get the fragment view.
             val fragmentView = webViewTabFragment.requireView()
@@ -98,7 +98,7 @@ class HttpAuthenticationDialog : DialogFragment() {
             val nestedScrollWebView = fragmentView.findViewById<NestedScrollWebView>(R.id.nestedscroll_webview)
 
             // Get a handle for the HTTP authentication handler.
-            val httpAuthHandler = nestedScrollWebView.httpAuthHandler
+            httpAuthHandler = nestedScrollWebView.httpAuthHandler
 
             // Use an alert dialog builder to create the alert dialog.
             val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
@@ -116,7 +116,7 @@ class HttpAuthenticationDialog : DialogFragment() {
             dialogBuilder.setNegativeButton(R.string.close) { _: DialogInterface?, _: Int ->
                 if (httpAuthHandler != null) {
                     // Cancel the HTTP authentication request.
-                    httpAuthHandler.cancel()
+                    httpAuthHandler!!.cancel()
 
                     // Reset the HTTP authentication handler.
                     nestedScrollWebView.resetHttpAuthHandler()
@@ -211,28 +211,25 @@ class HttpAuthenticationDialog : DialogFragment() {
             // Return the alert dialog.
             return alertDialog
         } catch (exception: Exception) {  // Privacy Browser was restarted and the HTTP auth handler no longer exists.
+            // Dismiss this new instance of the dialog as soon as it is displayed.
+            dismiss()
+
             // Use an alert dialog builder to create an empty alert dialog.
             val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
 
-            // Create an empty alert dialog from the alert dialog builder.
-            val alertDialog = dialogBuilder.create()
-
-            // Set the flag to dismiss the dialog as soon as it is resumed.
-            dismissDialog = true
-
             // Return the alert dialog.
-            return alertDialog
+            return dialogBuilder.create()
         }
     }
 
-    override fun onResume() {
+    override fun onSaveInstanceState(outState: Bundle) {
         // Run the default commands.
-        super.onResume()
+        super.onSaveInstanceState(outState)
 
-        // Dismiss the alert dialog if the activity was restarted and the HTTP auth handler no longer exists.
-        if (dismissDialog) {
-            dialog!!.dismiss()
-        }
+        // Cancel the request if the SSL error handler is not null.  This resets the WebView so it is not waiting on a response to the error handler if it is restarted in the background.
+        // Otherwise, after restart, the dialog is no longer displayed, but the error handler is still pending and there is no way to cause the dialog to redisplay for that URL in that tab.
+        if (httpAuthHandler != null)
+            httpAuthHandler!!.cancel()
     }
 
     private fun login(httpAuthHandler: HttpAuthHandler?, nestedScrollWebView: NestedScrollWebView) {
@@ -244,4 +241,4 @@ class HttpAuthenticationDialog : DialogFragment() {
             nestedScrollWebView.resetHttpAuthHandler()
         }
     }
-}
\ No newline at end of file
+}