2 * Copyright © 2017-2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser 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 Browser 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.privacybrowser.dialogs
22 import android.annotation.SuppressLint
23 import android.app.Dialog
24 import android.content.DialogInterface
25 import android.content.res.Configuration
26 import android.os.Bundle
27 import android.text.SpannableStringBuilder
28 import android.text.Spanned
29 import android.text.style.ForegroundColorSpan
30 import android.view.KeyEvent
31 import android.view.View
32 import android.view.WindowManager
33 import android.webkit.HttpAuthHandler
34 import android.widget.EditText
35 import android.widget.TextView
37 import androidx.appcompat.app.AlertDialog
38 import androidx.fragment.app.DialogFragment
39 import androidx.preference.PreferenceManager
41 import com.stoutner.privacybrowser.R
42 import com.stoutner.privacybrowser.activities.MainWebViewActivity
43 import com.stoutner.privacybrowser.views.NestedScrollWebView
45 // Define the class constants.
46 private const val HOST = "host"
47 private const val REALM = "realm"
48 private const val WEBVIEW_FRAGMENT_ID = "webview_fragment_id"
50 class HttpAuthenticationDialog : DialogFragment() {
51 // Define the class variables.
52 private var dismissDialog: Boolean = false
54 // Declare the class views.
55 private lateinit var usernameEditText: EditText
56 private lateinit var passwordEditText: EditText
59 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
61 fun displayDialog(host: String, realm: String, webViewFragmentId: Long): HttpAuthenticationDialog {
62 // Create an arguments bundle.
63 val argumentsBundle = Bundle()
65 // Store the variables in the bundle.
66 argumentsBundle.putString(HOST, host)
67 argumentsBundle.putString(REALM, realm)
68 argumentsBundle.putLong(WEBVIEW_FRAGMENT_ID, webViewFragmentId)
70 // Create a new instance of the HTTP authentication dialog.
71 val httpAuthenticationDialog = HttpAuthenticationDialog()
73 // Add the arguments bundle to the dialog.
74 httpAuthenticationDialog.arguments = argumentsBundle
76 // Return the new dialog.
77 return httpAuthenticationDialog
81 // `@SuppressLint("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
82 @SuppressLint("InflateParams")
83 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
84 // Get a handle for the arguments.
85 val arguments = requireArguments()
87 // Get the variables from the bundle.
88 val httpAuthHost = arguments.getString(HOST)
89 val httpAuthRealm = arguments.getString(REALM)
90 val webViewFragmentId = arguments.getLong(WEBVIEW_FRAGMENT_ID)
92 // Try to populate the alert dialog.
93 try { // Getting the WebView tab fragment will fail if Privacy Browser has been restarted.
94 // Get the current position of this WebView fragment.
95 val webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId)
97 // Get the WebView tab fragment.
98 val webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition)
100 // Get the fragment view.
101 val fragmentView = webViewTabFragment.requireView()
103 // Get a handle for the current WebView.
104 val nestedScrollWebView = fragmentView.findViewById<NestedScrollWebView>(R.id.nestedscroll_webview)
106 // Get a handle for the HTTP authentication handler.
107 val httpAuthHandler = nestedScrollWebView.httpAuthHandler
109 // Use an alert dialog builder to create the alert dialog.
110 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
112 // Set the icon according to the theme.
113 dialogBuilder.setIconAttribute(R.attr.lockBlueIcon)
116 dialogBuilder.setTitle(R.string.http_authentication)
118 // Set the view. The parent view is `null` because it will be assigned by the alert dialog.
119 dialogBuilder.setView(layoutInflater.inflate(R.layout.http_authentication_dialog, null))
121 // Set the close button listener.
122 dialogBuilder.setNegativeButton(R.string.close) { _: DialogInterface?, _: Int ->
123 // Cancel the HTTP authentication request.
124 httpAuthHandler.cancel()
126 // Reset the HTTP authentication handler.
127 nestedScrollWebView.resetHttpAuthHandler()
128 }// Set the proceed button listener.
129 dialogBuilder.setPositiveButton(R.string.proceed) { _: DialogInterface?, _: Int ->
130 // Send the login information
131 login(httpAuthHandler)
133 // Reset the HTTP authentication handler.
134 nestedScrollWebView.resetHttpAuthHandler()
137 // Create an alert dialog from the alert dialog builder.
138 val alertDialog = dialogBuilder.create()
140 // Get the alert dialog window.
141 val dialogWindow = alertDialog.window!!
143 // Get a handle for the shared preferences.
144 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
146 // Get the screenshot preference.
147 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
149 // Disable screenshots if not allowed.
150 if (!allowScreenshots) {
151 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
154 // Display the keyboard.
155 dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
157 // The alert dialog needs to be shown before the contents can be modified.
160 // Get handles for the views.
161 val realmTextView = alertDialog.findViewById<TextView>(R.id.http_authentication_realm)!!
162 val hostTextView = alertDialog.findViewById<TextView>(R.id.http_authentication_host)!!
163 usernameEditText = alertDialog.findViewById(R.id.http_authentication_username)!!
164 passwordEditText = alertDialog.findViewById(R.id.http_authentication_password)!!
166 // Set the realm text.
167 realmTextView.text = httpAuthRealm
169 // Initialize the host label and the spannable string builder.
170 val hostLabel = getString(R.string.host) + " "
171 val hostStringBuilder = SpannableStringBuilder(hostLabel + httpAuthHost)
173 // Get the current theme status.
174 val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
176 // Set the blue color span according to the theme. The deprecated `getColor()` must be used until API >= 23.
177 val blueColorSpan = if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
178 @Suppress("DEPRECATION")
179 ForegroundColorSpan(resources.getColor(R.color.blue_700))
181 @Suppress("DEPRECATION")
182 ForegroundColorSpan(resources.getColor(R.color.violet_700))
185 // Setup the span to display the host name in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
186 hostStringBuilder.setSpan(blueColorSpan, hostLabel.length, hostStringBuilder.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
188 // Set the host text.
189 hostTextView.text = hostStringBuilder
191 // Allow the enter key on the keyboard to send the login information from the username edit text.
192 usernameEditText.setOnKeyListener { _: View?, keyCode: Int, event: KeyEvent ->
193 // Check the key code and event.
194 if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN) { // The enter key was pressed.
195 // Send the login information.
196 login(httpAuthHandler)
198 // Manually dismiss the alert dialog.
199 alertDialog.dismiss()
201 // Consume the event.
202 return@setOnKeyListener true
203 } else { // If any other key was pressed, do not consume the event.
204 return@setOnKeyListener false
208 // Allow the enter key on the keyboard to send the login information from the password edit text.
209 passwordEditText.setOnKeyListener { _: View?, keyCode: Int, event: KeyEvent ->
210 // Check the key code and event.
211 if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN) { // The enter key was pressed.
212 // Send the login information.
213 login(httpAuthHandler)
215 // Manually dismiss the alert dialog.
216 alertDialog.dismiss()
218 // Consume the event.
219 return@setOnKeyListener true
220 } else { // If any other key was pressed, do not consume the event.
221 return@setOnKeyListener false
225 // Return the alert dialog.
227 } catch (exception: Exception) { // Privacy Browser was restarted and the HTTP auth handler no longer exists.
228 // Use an alert dialog builder to create an empty alert dialog.
229 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
231 // Create an empty alert dialog from the alert dialog builder.
232 val alertDialog = dialogBuilder.create()
234 // Set the flag to dismiss the dialog as soon as it is resumed.
237 // Return the alert dialog.
242 override fun onResume() {
243 // Run the default commands.
246 // Dismiss the alert dialog if the activity was restarted and the HTTP auth handler no longer exists.
252 private fun login(httpAuthHandler: HttpAuthHandler) {
253 // Send the login information.
254 httpAuthHandler.proceed(usernameEditText.text.toString(), passwordEditText.text.toString())