Skip to content

Commit

Permalink
Hide 'Wait' if WebViewActivity error is not due to external bus timeo…
Browse files Browse the repository at this point in the history
…ut (#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
  • Loading branch information
jpelgrom authored Jun 14, 2024
1 parent 6bd3ac2 commit ebd800d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebd800d

Please sign in to comment.