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