Files
apk/FastDocumentation/app/src/main/java/MainActivity.kt
2025-12-09 11:35:41 +01:00

94 lines
3.6 KiB
Kotlin

package com.example.FastDocumentation
import android.os.Bundle
import android.webkit.HttpAuthHandler
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import android.app.AlertDialog // NEW IMPORT for the dialog
import android.widget.EditText // NEW IMPORT for text inputs
import android.widget.LinearLayout // NEW IMPORT for dialog layout
import android.view.ViewGroup.LayoutParams.MATCH_PARENT // NEW IMPORT for layout
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT // NEW IMPORT for layout
class MainActivity : AppCompatActivity() {
private lateinit var myWebView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myWebView = findViewById(R.id.webview)
// 1. Configure WebView Settings
val settings = myWebView.settings
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
// 2. Keep navigation inside the WebView AND handle HTTP Auth (MODIFIED)
myWebView.webViewClient = object : WebViewClient() {
// This method is called when the server requests HTTP authentication (like Basic Auth)
override fun onReceivedHttpAuthRequest(
view: WebView?,
handler: HttpAuthHandler?,
host: String?,
realm: String?
) {
// Manually trigger the popup using an AlertDialog
if (handler != null) {
showHttpAuthDialog(handler, host, realm)
}
}
}
// 3. Load the Target URL
val urlToLoad = "https://docs.nxs.solutions/Fast"
myWebView.loadUrl(urlToLoad)
}
// NEW FUNCTION: Manually constructs and shows the HTTP Auth dialog
private fun showHttpAuthDialog(handler: HttpAuthHandler, host: String?, realm: String?) {
val usernameInput = EditText(this).apply { hint = "Username" }
val passwordInput = EditText(this).apply {
hint = "Password"
inputType = android.text.InputType.TYPE_CLASS_TEXT or android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
}
// Create a simple vertical layout for the two text fields
val layout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
setPadding(50, 20, 50, 20) // Add some padding
addView(usernameInput, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
addView(passwordInput, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
}
AlertDialog.Builder(this)
.setTitle("Authentication Required")
.setMessage("Please enter credentials for $realm on $host")
.setView(layout) // Set the custom layout
.setPositiveButton("Log In") { _, _ ->
// When the user clicks "Log In", proceed with the provided credentials
val username = usernameInput.text.toString()
val password = passwordInput.text.toString()
handler.proceed(username, password)
}
.setNegativeButton("Cancel") { _, _ ->
// If the user cancels, cancel the authentication request
handler.cancel()
}
.setCancelable(false)
.show()
}
// 4. Handle the device's back button
override fun onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack()
} else {
super.onBackPressed()
}
}
}