coverting captcha webview to a viewstub in order to catch errors when the system webview is missing or has been disabled

This commit is contained in:
Adam Brown 2022-08-17 13:51:58 +01:00
parent 1d03460aee
commit ca10109a65
3 changed files with 78 additions and 6 deletions

View File

@ -16,15 +16,22 @@
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.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 +47,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 +80,38 @@ 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 fun Throwable?.crawlCausesFor(predicate: (Throwable) -> Boolean): Boolean {
return when {
this == null -> false
else -> {
when (predicate(this)) {
true -> true
else -> this.cause.crawlCausesFor(predicate)
}
}
}
}
private class MissingWebViewException(cause: Throwable) : IllegalStateException("Failed to load WebView provider: No WebView installed", cause)

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" />