Bugfix/crashlytics fixes (#289)

* Add null checks for HomeAssistantSearcher.
https://github.com/home-assistant/home-assistant-android/issues/288

* Make sure to catch the error if we can't update the device name.
https://github.com/home-assistant/home-assistant-android/issues/287

* Show error before we pop back on the stack.
https://github.com/home-assistant/home-assistant-android/issues/286

* Make sure to catch exceptions and show error before poping stacks.
https://github.com/home-assistant/home-assistant-android/issues/285
https://github.com/home-assistant/home-assistant-android/issues/286

* Linting
This commit is contained in:
Justin Bassett 2020-01-22 21:45:30 -05:00 committed by Paulus Schoutsen
parent e6d5b5825b
commit baba03a757
5 changed files with 39 additions and 13 deletions

View file

@ -1,5 +1,6 @@
package io.homeassistant.companion.android.onboarding.authentication
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.view.LayoutInflater
@ -41,6 +42,7 @@ class AuthenticationFragment : Fragment(), AuthenticationView {
.inject(this)
}
@SuppressLint("SetJavaScriptEnabled")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@ -63,8 +65,7 @@ class AuthenticationFragment : Fragment(), AuthenticationView {
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
popBack()
showError(getString(R.string.error_connection_failed))
showError()
}
}
}
@ -90,13 +91,11 @@ class AuthenticationFragment : Fragment(), AuthenticationView {
super.onDestroy()
}
fun popBack() {
fragmentManager?.popBackStack()
}
fun showError(title: String) {
override fun showError() {
AlertDialog.Builder(context)
.setTitle(title)
.setTitle(R.string.error_connection_failed)
.setPositiveButton(R.string.ok) { _, _ -> }
.show()
fragmentManager?.popBackStack()
}
}

View file

@ -24,7 +24,12 @@ class AuthenticationPresenterImpl @Inject constructor(
override fun onViewReady() {
mainScope.launch {
view.loadUrl(authenticationUseCase.buildAuthenticationUrl(AUTH_CALLBACK).toString())
try {
view.loadUrl(authenticationUseCase.buildAuthenticationUrl(AUTH_CALLBACK).toString())
} catch (e: Exception) {
Log.e(TAG, "Unable to create auth url and/or load it.", e)
view.showError()
}
}
}
@ -36,6 +41,8 @@ class AuthenticationPresenterImpl @Inject constructor(
authenticationUseCase.registerAuthorizationCode(code)
} catch (e: Exception) {
Log.e(TAG, "unable to register code")
view.showError()
return@launch
}
view.openWebview()
}

View file

@ -4,5 +4,7 @@ interface AuthenticationView {
fun loadUrl(url: String)
fun showError()
fun openWebview()
}

View file

@ -49,10 +49,17 @@ class HomeAssistantSearcher constructor(
override fun onServiceResolved(resolvedService: NsdServiceInfo?) {
Log.i(TAG, "Service resolved: $resolvedService")
resolvedService?.let {
discoveryView.onInstanceFound(HomeAssistantInstance(
it.serviceName,
URL(it.attributes["base_url"]!!.commonToUtf8String()),
it.attributes["version"]!!.commonToUtf8String()))
val baseUrl = it.attributes["base_url"]
val version = it.attributes["version"]
if (baseUrl != null && version != null) {
discoveryView.onInstanceFound(
HomeAssistantInstance(
it.serviceName,
URL(baseUrl.commonToUtf8String()),
version.commonToUtf8String()
)
)
}
}
}
})

View file

@ -1,5 +1,6 @@
package io.homeassistant.companion.android.settings
import android.util.Log
import androidx.preference.PreferenceDataStore
import io.homeassistant.companion.android.domain.integration.IntegrationUseCase
import io.homeassistant.companion.android.domain.url.UrlUseCase
@ -17,6 +18,10 @@ class SettingsPresenterImpl @Inject constructor(
private val integrationUseCase: IntegrationUseCase
) : SettingsPresenter, PreferenceDataStore() {
companion object {
private const val TAG = "SettingsPresenter"
}
private val mainScope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job())
override fun getBoolean(key: String?, defValue: Boolean): Boolean {
@ -65,7 +70,13 @@ class SettingsPresenterImpl @Inject constructor(
"connection_external" -> {
urlUseCase.saveUrl(value ?: "", false)
}
"registration_name" -> integrationUseCase.updateRegistration(deviceName = value!!)
"registration_name" -> {
try {
integrationUseCase.updateRegistration(deviceName = value!!)
} catch (e: Exception) {
Log.e(TAG, "Issue updating registration with new device name", e)
}
}
else -> throw Exception()
}
}