Don't complete onboarding on registration error (#2646)

- Add specific error if the app encounters a HTTP 404 response during onboarding registration, because that means the mobile_app integration is missing
 - After an error, clean up the session data to make sure a new registration attempt is made
This commit is contained in:
Joris Pelgröm 2022-07-01 03:58:24 +02:00 committed by GitHub
parent aa256676c4
commit 7b401abddc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 28 deletions

View File

@ -26,6 +26,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import retrofit2.HttpException
import javax.inject.Inject
import javax.net.ssl.SSLException
import javax.net.ssl.SSLHandshakeException
@ -145,15 +146,24 @@ class LaunchActivity : AppCompatActivity(), LaunchView {
)
} catch (e: Exception) {
// Fatal errors: if one of these calls fail, the app cannot proceed.
// Because this runs after the webview, the only expected errors are system
// version related in OkHttp (cryptography), or general connection issues (offline/unknown).
// Show an error, clean up the session and require new registration.
// Because this runs after the webview, the only expected errors are:
// - missing mobile_app integration
// - system version related in OkHttp (cryptography)
// - general connection issues (offline/unknown)
Log.e(TAG, "Exception while registering", e)
try {
authenticationRepository.revokeSession()
} catch (e: Exception) {
Log.e(TAG, "Can't revoke session", e)
}
AlertDialog.Builder(this@LaunchActivity)
.setTitle(commonR.string.error_connection_failed)
.setMessage(
when (e) {
is SSLHandshakeException -> commonR.string.webview_error_FAILED_SSL_HANDSHAKE
is SSLException -> commonR.string.webview_error_SSL_INVALID
when {
e is HttpException && e.code() == 404 -> commonR.string.error_with_registration
e is SSLHandshakeException -> commonR.string.webview_error_FAILED_SSL_HANDSHAKE
e is SSLException -> commonR.string.webview_error_SSL_INVALID
else -> commonR.string.webview_error
}
)

View File

@ -94,13 +94,13 @@ class IntegrationRepositoryImpl @Inject constructor(
Log.e(TAG, "Unable to register device due to missing URL")
return
}
val response =
integrationService.registerDevice(
url.newBuilder().addPathSegments("api/mobile_app/registrations").build(),
authenticationRepository.buildBearerToken(),
request
)
try {
val response =
integrationService.registerDevice(
url.newBuilder().addPathSegments("api/mobile_app/registrations").build(),
authenticationRepository.buildBearerToken(),
request
)
persistDeviceRegistration(deviceRegistration)
urlRepository.saveRegistrationUrls(
response.cloudhookUrl,
@ -109,7 +109,7 @@ class IntegrationRepositoryImpl @Inject constructor(
)
localStorage.putString(PREF_SECRET, response.secret)
} catch (e: Exception) {
Log.e(TAG, "Unable to register device", e)
Log.e(TAG, "Unable to save device registration", e)
}
}

View File

@ -193,7 +193,7 @@
<string name="error_loading_entities">Error loading entities</string>
<string name="error_onboarding_connection_failed">Unable to connect to Home Assistant.</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="error_with_registration">Please check to ensure you have the mobile_app\nintegration enabled on your home assistant instance.</string>
<string name="error_with_registration">The \'Mobile App\' integration is required to use the app, but it is not available on your Home Assistant server. Please check your server configuration and try again.</string>
<string name="errors">Errors</string>
<string name="event_error">Failed to notify HA of picked option.</string>
<string name="exit">Exit</string>

View File

@ -109,25 +109,29 @@ class PhoneSettingsListener : WearableListenerService(), DataClient.OnDataChange
}
private fun login(dataMap: DataMap) = mainScope.launch {
val url = dataMap.getString("URL")
val authCode = dataMap.getString("AuthCode")
val deviceName = dataMap.getString("DeviceName")
val deviceTrackingEnabled = dataMap.getString("LocationTracking")
try {
val url = dataMap.getString("URL")
val authCode = dataMap.getString("AuthCode")
val deviceName = dataMap.getString("DeviceName")
val deviceTrackingEnabled = dataMap.getString("LocationTracking")
urlRepository.saveUrl(url)
authenticationRepository.registerAuthorizationCode(authCode)
integrationUseCase.registerDevice(
DeviceRegistration(
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
deviceName
urlRepository.saveUrl(url)
authenticationRepository.registerAuthorizationCode(authCode)
integrationUseCase.registerDevice(
DeviceRegistration(
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
deviceName
)
)
)
val intent = HomeActivity.newInstance(applicationContext)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
} catch (e: Exception) {
Log.e(TAG, "Unable to login to Home Assistant", e)
}
sendPhoneData()
val intent = HomeActivity.newInstance(applicationContext)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
private fun saveFavorites(dataMap: DataMap) {