mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Add better handling for slow connections. (#455)
* Add better handling for slow connections. * Add ability to auto close alert on connection. * Fix linting. * Only cancel alert if we actually connect.
This commit is contained in:
parent
652ac084ff
commit
e540e1df9c
|
@ -59,6 +59,7 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
|
||||
private var isConnected = false
|
||||
private var isShowingError = false
|
||||
private var alertDialog: AlertDialog? = null
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -227,6 +228,9 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
"connection-status" -> {
|
||||
isConnected = json.getJSONObject("payload")
|
||||
.getString("event") == "connected"
|
||||
if (isConnected) {
|
||||
alertDialog?.cancel()
|
||||
}
|
||||
}
|
||||
"config/get" -> {
|
||||
val script = "externalBus(" +
|
||||
|
@ -312,11 +316,7 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
override fun loadUrl(url: String) {
|
||||
loadedUrl = url
|
||||
webView.loadUrl(url)
|
||||
Handler().postDelayed({
|
||||
if (!isConnected) {
|
||||
showError()
|
||||
}
|
||||
}, 5000)
|
||||
waitForConnection()
|
||||
}
|
||||
|
||||
override fun setStatusBarColor(color: Int) {
|
||||
|
@ -339,21 +339,38 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
return
|
||||
isShowingError = true
|
||||
|
||||
AlertDialog.Builder(this)
|
||||
val alert = AlertDialog.Builder(this)
|
||||
.setTitle(R.string.error_connection_failed)
|
||||
.setMessage(if (isAuthenticationError) R.string.error_auth_revoked else R.string.webview_error)
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
if (isAuthenticationError) {
|
||||
presenter.clearKnownUrls()
|
||||
openOnBoarding()
|
||||
} else {
|
||||
startActivity(SettingsActivity.newInstance(this))
|
||||
}
|
||||
.setOnDismissListener {
|
||||
isShowingError = false
|
||||
alertDialog = null
|
||||
waitForConnection()
|
||||
}
|
||||
.setOnDismissListener { isShowingError = false }
|
||||
.show()
|
||||
|
||||
if (isAuthenticationError) {
|
||||
alert.setMessage(R.string.error_auth_revoked)
|
||||
alert.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
presenter.clearKnownUrls()
|
||||
openOnBoarding()
|
||||
}
|
||||
} else {
|
||||
alert.setMessage(R.string.webview_error)
|
||||
alert.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
startActivity(SettingsActivity.newInstance(this))
|
||||
}
|
||||
alert.setNegativeButton(R.string.refresh) { _, _ ->
|
||||
webView.reload()
|
||||
waitForConnection()
|
||||
}
|
||||
alert.setNeutralButton(R.string.wait) { _, _ ->
|
||||
waitForConnection()
|
||||
}
|
||||
}
|
||||
alertDialog = alert.create()
|
||||
alertDialog?.show()
|
||||
}
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
override fun authenticationDialog(handler: HttpAuthHandler) {
|
||||
val inflater = layoutInflater
|
||||
val dialogLayout = inflater.inflate(R.layout.dialog_authentication, null)
|
||||
|
@ -390,4 +407,12 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
cutout = true
|
||||
return cutout
|
||||
}
|
||||
|
||||
private fun waitForConnection() {
|
||||
Handler().postDelayed({
|
||||
if (!isConnected) {
|
||||
showError()
|
||||
}
|
||||
}, 5000)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,5 +70,7 @@ like to connect to:</string>
|
|||
<string name="add_service_data_field">Add Field</string>
|
||||
<string name="widget_config_service_error">A custom component is preventing service data from loading.</string>
|
||||
<string name="error_ssl">Unable to communicate with Home Assistant because of a SSL error. Please ensure your certificate is valid.</string>
|
||||
<string name="wait">Wait</string>
|
||||
<string name="refresh">Refresh</string>
|
||||
<string name="event_error">Failed to notify HA of picked option.</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue