From ebd800ded3e656221f93d05b362045cca2dff704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Fri, 14 Jun 2024 23:21:24 +0200 Subject: [PATCH] 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 --- .../companion/android/webview/WebView.kt | 9 +++-- .../android/webview/WebViewActivity.kt | 36 +++++++++++-------- .../android/webview/WebViewPresenterImpl.kt | 2 +- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebView.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebView.kt index 354620566..ef308d988 100644 --- a/app/src/main/java/io/homeassistant/companion/android/webview/WebView.kt +++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebView.kt @@ -8,7 +8,12 @@ interface WebView { AUTHENTICATION, SSL, 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) @@ -23,5 +28,5 @@ interface WebView { 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) } diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt index e8481f7a7..90f7a2b3e 100644 --- a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt @@ -112,7 +112,6 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.withContext import okhttp3.HttpUrl.Companion.toHttpUrl @@ -1310,7 +1309,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi } if (tlsWebViewClient?.isTLSClientAuthNeeded == true && - errorType == ErrorType.TIMEOUT && + (errorType == ErrorType.TIMEOUT_GENERAL || errorType == ErrorType.TIMEOUT_EXTERNAL_BUS) && !tlsWebViewClient.hasUserDeniedAccess ) { // Ignore if a timeout occurs but the user has not denied access @@ -1395,26 +1394,33 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi startActivity(SettingsActivity.newInstance(this)) } val isInternal = serverManager.getServer(presenter.getActiveServer())?.connection?.isInternal() == true + val buttonRefreshesInternal = failedConnection == "external" && isInternal alert.setNegativeButton( - if (failedConnection == "external" && isInternal) { + if (buttonRefreshesInternal) { commonR.string.refresh_internal } else { commonR.string.refresh_external } ) { _, _ -> - runBlocking { - failedConnection = if (failedConnection == "external") { - serverManager.getServer(presenter.getActiveServer())?.let { webView.loadUrl(it.connection.getUrl(true).toString()) } - "internal" - } else { - serverManager.getServer(presenter.getActiveServer())?.let { webView.loadUrl(it.connection.getUrl(false).toString()) } - "external" - } + val url = serverManager.getServer(presenter.getActiveServer())?.let { + val base = it.connection.getUrl(buttonRefreshesInternal) ?: return@let null + Uri.parse(base.toString()) + .buildUpon() + .appendQueryParameter("external_auth", "1") + .build() + .toString() + } + failedConnection = if (buttonRefreshesInternal) "internal" else "external" + if (url != null) { + loadUrl(url = url, keepHistory = true, openInApp = true) + } else { + waitForConnection() } - waitForConnection() } - alert.setNeutralButton(commonR.string.wait) { _, _ -> - waitForConnection() + if (errorType == ErrorType.TIMEOUT_EXTERNAL_BUS) { + alert.setNeutralButton(commonR.string.wait) { _, _ -> + waitForConnection() + } } } alertDialog = alert.create() @@ -1518,7 +1524,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi !loadedUrl.toHttpUrl().pathSegments.first().contains("api") && !loadedUrl.toHttpUrl().pathSegments.first().contains("local") ) { - showError() + showError(errorType = ErrorType.TIMEOUT_EXTERNAL_BUS) } }, CONNECTION_DELAY diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt index 161d6a344..f0ae26703 100644 --- a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt +++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt @@ -211,7 +211,7 @@ class WebViewPresenterImpl @Inject constructor( errorType = when { anonymousSession -> WebView.ErrorType.AUTHENTICATION 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 { anonymousSession -> null