Hide 'Wait' if WebViewActivity error is not due to external bus timeout (#4412)

* Hide wait if WebViewActivity error is not due to external bus timeout

 - The error pop up in WebViewActivity can show up for various reasons. The option to wait is only relevant when it is due to a timeout waiting on the external bus, otherwise it is an error for page loading/ssl/...  where waiting won't change the outcome. To prevent confusion, hide the wait button when the error is not triggered by the external bus timeout.

* Align refresh button text and action

 - Have the refresh button trigger refresh errors instead of timeouts by using the correct url + function instead of interacting with WebView directly (for previous commit to work as expected)
 - Make the text on the refresh button and the action do the same thing by using the same check for internal
This commit is contained in:
Joris Pelgröm 2024-06-14 23:21:24 +02:00 committed by GitHub
parent 6bd3ac21f9
commit ebd800ded3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 18 deletions

View File

@ -8,7 +8,12 @@ interface WebView {
AUTHENTICATION, AUTHENTICATION,
SSL, SSL,
SECURITY_WARNING, SECURITY_WARNING,
TIMEOUT
/** Timeout or general loading error */
TIMEOUT_GENERAL,
/** Timeout due to no 'connection-status: connected' event on the external bus */
TIMEOUT_EXTERNAL_BUS
} }
fun loadUrl(url: String, keepHistory: Boolean, openInApp: Boolean) fun loadUrl(url: String, keepHistory: Boolean, openInApp: Boolean)
@ -23,5 +28,5 @@ interface WebView {
fun unlockAppIfNeeded() fun unlockAppIfNeeded()
fun showError(errorType: ErrorType = ErrorType.TIMEOUT, error: SslError? = null, description: String? = null) fun showError(errorType: ErrorType = ErrorType.TIMEOUT_GENERAL, error: SslError? = null, description: String? = null)
} }

View File

@ -112,7 +112,6 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
@ -1310,7 +1309,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
} }
if (tlsWebViewClient?.isTLSClientAuthNeeded == true && if (tlsWebViewClient?.isTLSClientAuthNeeded == true &&
errorType == ErrorType.TIMEOUT && (errorType == ErrorType.TIMEOUT_GENERAL || errorType == ErrorType.TIMEOUT_EXTERNAL_BUS) &&
!tlsWebViewClient.hasUserDeniedAccess !tlsWebViewClient.hasUserDeniedAccess
) { ) {
// Ignore if a timeout occurs but the user has not denied access // Ignore if a timeout occurs but the user has not denied access
@ -1395,28 +1394,35 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
startActivity(SettingsActivity.newInstance(this)) startActivity(SettingsActivity.newInstance(this))
} }
val isInternal = serverManager.getServer(presenter.getActiveServer())?.connection?.isInternal() == true val isInternal = serverManager.getServer(presenter.getActiveServer())?.connection?.isInternal() == true
val buttonRefreshesInternal = failedConnection == "external" && isInternal
alert.setNegativeButton( alert.setNegativeButton(
if (failedConnection == "external" && isInternal) { if (buttonRefreshesInternal) {
commonR.string.refresh_internal commonR.string.refresh_internal
} else { } else {
commonR.string.refresh_external commonR.string.refresh_external
} }
) { _, _ -> ) { _, _ ->
runBlocking { val url = serverManager.getServer(presenter.getActiveServer())?.let {
failedConnection = if (failedConnection == "external") { val base = it.connection.getUrl(buttonRefreshesInternal) ?: return@let null
serverManager.getServer(presenter.getActiveServer())?.let { webView.loadUrl(it.connection.getUrl(true).toString()) } Uri.parse(base.toString())
"internal" .buildUpon()
.appendQueryParameter("external_auth", "1")
.build()
.toString()
}
failedConnection = if (buttonRefreshesInternal) "internal" else "external"
if (url != null) {
loadUrl(url = url, keepHistory = true, openInApp = true)
} else { } else {
serverManager.getServer(presenter.getActiveServer())?.let { webView.loadUrl(it.connection.getUrl(false).toString()) }
"external"
}
}
waitForConnection() waitForConnection()
} }
}
if (errorType == ErrorType.TIMEOUT_EXTERNAL_BUS) {
alert.setNeutralButton(commonR.string.wait) { _, _ -> alert.setNeutralButton(commonR.string.wait) { _, _ ->
waitForConnection() waitForConnection()
} }
} }
}
alertDialog = alert.create() alertDialog = alert.create()
alertDialog?.show() alertDialog?.show()
} }
@ -1518,7 +1524,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
!loadedUrl.toHttpUrl().pathSegments.first().contains("api") && !loadedUrl.toHttpUrl().pathSegments.first().contains("api") &&
!loadedUrl.toHttpUrl().pathSegments.first().contains("local") !loadedUrl.toHttpUrl().pathSegments.first().contains("local")
) { ) {
showError() showError(errorType = ErrorType.TIMEOUT_EXTERNAL_BUS)
} }
}, },
CONNECTION_DELAY CONNECTION_DELAY

View File

@ -211,7 +211,7 @@ class WebViewPresenterImpl @Inject constructor(
errorType = when { errorType = when {
anonymousSession -> WebView.ErrorType.AUTHENTICATION anonymousSession -> WebView.ErrorType.AUTHENTICATION
e is SSLException || (e is SocketTimeoutException && e.suppressed.any { it is SSLException }) -> WebView.ErrorType.SSL e is SSLException || (e is SocketTimeoutException && e.suppressed.any { it is SSLException }) -> WebView.ErrorType.SSL
else -> WebView.ErrorType.TIMEOUT else -> WebView.ErrorType.TIMEOUT_GENERAL
}, },
description = when { description = when {
anonymousSession -> null anonymousSession -> null