Merge pull request #6862 from vector-im/feature/adm/ftue-fragment-crashes

FTUE - Crash fixes
This commit is contained in:
Adam Brown 2022-08-22 17:21:31 +01:00 committed by GitHub
commit 0be46adfda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 109 additions and 39 deletions

1
changelog.d/6855.bugfix Normal file
View file

@ -0,0 +1 @@
Fixes onboarding captcha crashing when no WebView is available by showing an error with information instead

1
changelog.d/6860.bugfix Normal file
View file

@ -0,0 +1 @@
Removes ability to continue registration after the app has been destroyed, fixes the next steps crashing due to missing information from the previous steps

1
changelog.d/6861.bugfix Normal file
View file

@ -0,0 +1 @@
Fixes crash when exiting the login or registration entry screens whilst they're loading

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.extensions
/**
* Recursive through the throwable and its causes for the given predicate.
*
* @return true when the predicate finds a match.
*/
tailrec fun Throwable?.crawlCausesFor(predicate: (Throwable) -> Boolean): Boolean {
return when {
this == null -> false
else -> {
when (predicate(this)) {
true -> true
else -> this.cause.crawlCausesFor(predicate)
}
}
}
}

View file

@ -122,9 +122,6 @@ class OnboardingViewModel @AssistedInject constructor(
private val registrationWizard: RegistrationWizard
get() = authenticationService.getRegistrationWizard()
val currentThreePid: String?
get() = registrationWizard.getCurrentThreePid()
// True when login and password has been sent with success to the homeserver
val isRegistrationStarted: Boolean
get() = authenticationService.isRegistrationStarted()
@ -479,17 +476,6 @@ class OnboardingViewModel @AssistedInject constructor(
private fun handleInitWith(action: OnboardingAction.InitWith) {
loginConfig = action.loginConfig
// If there is a pending email validation continue on this step
try {
if (registrationWizard.isRegistrationStarted()) {
currentThreePid?.let {
handle(OnboardingAction.PostViewEvent(OnboardingViewEvents.OnSendEmailSuccess(it, isRestoredSession = true)))
}
}
} catch (e: Throwable) {
// NOOP. API is designed to use wizards in a login/registration flow,
// but we need to check the state anyway.
}
}
private fun handleResetPassword(action: OnboardingAction.ResetPassword) {

View file

@ -16,15 +16,23 @@
package im.vector.app.features.onboarding.ftueauth
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import com.airbnb.mvrx.args
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.R
import im.vector.app.core.extensions.crawlCausesFor
import im.vector.app.databinding.FragmentFtueLoginCaptchaBinding
import im.vector.app.databinding.ViewStubWebviewBinding
import im.vector.app.features.onboarding.OnboardingAction
import im.vector.app.features.onboarding.OnboardingViewState
import im.vector.app.features.onboarding.RegisterAction
import kotlinx.parcelize.Parcelize
import org.matrix.android.sdk.api.extensions.orFalse
import javax.inject.Inject
@Parcelize
@ -40,10 +48,32 @@ class FtueAuthCaptchaFragment @Inject constructor(
) : AbstractFtueAuthFragment<FragmentFtueLoginCaptchaBinding>() {
private val params: FtueAuthCaptchaFragmentArgument by args()
private var webViewBinding: ViewStubWebviewBinding? = null
private var isWebViewLoaded = false
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentFtueLoginCaptchaBinding {
return FragmentFtueLoginCaptchaBinding.inflate(inflater, container, false)
return FragmentFtueLoginCaptchaBinding.inflate(inflater, container, false).also {
it.loginCaptchaWebViewStub.setOnInflateListener { _, inflated ->
webViewBinding = ViewStubWebviewBinding.bind(inflated)
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
inflateWebViewOrShowError()
}
private fun inflateWebViewOrShowError() {
views.loginCaptchaWebViewStub.inflateWebView(onError = {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.dialog_title_error)
.setMessage(it.localizedMessage)
.setPositiveButton(R.string.ok) { _, _ ->
requireActivity().recreate()
}
.show()
})
}
override fun resetViewModel() {
@ -51,11 +81,26 @@ class FtueAuthCaptchaFragment @Inject constructor(
}
override fun updateWithState(state: OnboardingViewState) {
if (!isWebViewLoaded) {
captchaWebview.setupWebView(this, views.loginCaptchaWevView, views.loginCaptchaProgress, params.siteKey, state) {
if (!isWebViewLoaded && webViewBinding != null) {
captchaWebview.setupWebView(this, webViewBinding!!.root, views.loginCaptchaProgress, params.siteKey, state) {
viewModel.handle(OnboardingAction.PostRegisterAction(RegisterAction.CaptchaDone(it)))
}
isWebViewLoaded = true
}
}
}
private fun ViewStub.inflateWebView(onError: (Throwable) -> Unit) {
try {
inflate()
} catch (e: Exception) {
val isMissingWebView = e.crawlCausesFor { it.message?.contains("MissingWebViewPackageException").orFalse() }
if (isMissingWebView) {
onError(MissingWebViewException(e))
} else {
onError(e)
}
}
}
private class MissingWebViewException(cause: Throwable) : IllegalStateException("Failed to load WebView provider: No WebView installed", cause)

View file

@ -261,11 +261,11 @@ class FtueAuthVariant(
}
private fun onStartCombinedLogin() {
addRegistrationStageFragmentToBackstack(FtueAuthCombinedLoginFragment::class.java)
addRegistrationStageFragmentToBackstack(FtueAuthCombinedLoginFragment::class.java, allowStateLoss = true)
}
private fun openStartCombinedRegister() {
addRegistrationStageFragmentToBackstack(FtueAuthCombinedRegisterFragment::class.java)
addRegistrationStageFragmentToBackstack(FtueAuthCombinedRegisterFragment::class.java, allowStateLoss = true)
}
private fun displayFallbackWebDialog() {
@ -520,13 +520,14 @@ class FtueAuthVariant(
)
}
private fun addRegistrationStageFragmentToBackstack(fragmentClass: Class<out Fragment>, params: Parcelable? = null) {
private fun addRegistrationStageFragmentToBackstack(fragmentClass: Class<out Fragment>, params: Parcelable? = null, allowStateLoss: Boolean = false) {
activity.addFragmentToBackstack(
views.loginFragmentContainer,
fragmentClass,
params,
tag = FRAGMENT_REGISTRATION_STAGE_TAG,
option = commonOption
option = commonOption,
allowStateLoss = allowStateLoss,
)
}

View file

@ -64,15 +64,27 @@
android:id="@+id/titleContentSpacing"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/loginCaptchaWevView"
app:layout_constraintBottom_toTopOf="@id/loginWebViewBarrier"
app:layout_constraintHeight_percent="0.03"
app:layout_constraintTop_toBottomOf="@id/captchaHeaderTitle" />
<WebView
android:id="@+id/loginCaptchaWevView"
<androidx.constraintlayout.widget.Barrier
android:id="@+id/loginWebViewBarrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="loginCaptchaWebViewStub,loginCaptchaWebView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/captchaGutterEnd"
app:layout_constraintStart_toStartOf="@id/captchaGutterStart"
app:layout_constraintTop_toBottomOf="@id/titleContentSpacing"/>
<ViewStub
android:id="@+id/loginCaptchaWebViewStub"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/login_a11y_captcha_container"
android:layout="@layout/view_stub_webview"
android:inflatedId="@+id/loginCaptchaWebView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/captchaGutterEnd"
app:layout_constraintStart_toStartOf="@id/captchaGutterStart"

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />

View file

@ -195,21 +195,6 @@ class OnboardingViewModelTest {
.finish()
}
@Test
fun `given registration started with currentThreePid, when handling InitWith, then emits restored session OnSendEmailSuccess`() = runTest {
val test = viewModel.test()
fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also {
it.givenRegistrationStarted(hasStarted = true)
it.givenCurrentThreePid(AN_EMAIL)
})
viewModel.handle(OnboardingAction.InitWith(LoginConfig(A_HOMESERVER_URL, identityServerUrl = null)))
test
.assertEvents(OnboardingViewEvents.OnSendEmailSuccess(AN_EMAIL, isRestoredSession = true))
.finish()
}
@Test
fun `given registration not started, when handling InitWith, then does nothing`() = runTest {
val test = viewModel.test()