Login screens: MSISDN: check format and compute country code

This commit is contained in:
Benoit Marty 2019-11-21 12:11:58 +01:00
parent eb4355890e
commit 2789268c23
4 changed files with 44 additions and 3 deletions

View File

@ -257,6 +257,9 @@ dependencies {
// Debug
implementation 'com.facebook.stetho:stetho:1.5.1'
// Phone number https://github.com/google/libphonenumber
implementation 'com.googlecode.libphonenumber:libphonenumber:8.10.23'
// rx
implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

View File

@ -25,12 +25,15 @@ import androidx.autofill.HintConstants
import androidx.core.view.isVisible
import butterknife.OnClick
import com.airbnb.mvrx.args
import com.google.i18n.phonenumbers.NumberParseException
import com.google.i18n.phonenumbers.PhoneNumberUtil
import com.jakewharton.rxbinding3.widget.textChanges
import im.vector.matrix.android.api.auth.registration.RegisterThreePid
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.MatrixError
import im.vector.riotx.R
import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.setTextOrHide
import kotlinx.android.parcel.Parcelize
import kotlinx.android.synthetic.main.fragment_login_generic_text_input_form.*
import javax.inject.Inject
@ -91,6 +94,7 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
TextInputFormFragmentMode.SetEmail -> {
loginGenericTextInputFormTitle.text = getString(R.string.login_set_email_title)
loginGenericTextInputFormNotice.text = getString(R.string.login_set_email_notice)
loginGenericTextInputFormNotice2.setTextOrHide(null)
loginGenericTextInputFormTil.hint = getString(if (params.mandatory) R.string.login_set_email_mandatory_hint else R.string.login_set_email_optional_hint)
loginGenericTextInputFormTextInput.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
loginGenericTextInputFormOtherButton.isVisible = false
@ -99,6 +103,7 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
TextInputFormFragmentMode.SetMsisdn -> {
loginGenericTextInputFormTitle.text = getString(R.string.login_set_msisdn_title)
loginGenericTextInputFormNotice.text = getString(R.string.login_set_msisdn_notice)
loginGenericTextInputFormNotice2.setTextOrHide(getString(R.string.login_set_msisdn_notice2))
loginGenericTextInputFormTil.hint = getString(if (params.mandatory) R.string.login_set_msisdn_mandatory_hint else R.string.login_set_msisdn_optional_hint)
loginGenericTextInputFormTextInput.inputType = InputType.TYPE_CLASS_PHONE
loginGenericTextInputFormOtherButton.isVisible = false
@ -107,6 +112,7 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
TextInputFormFragmentMode.ConfirmMsisdn -> {
loginGenericTextInputFormTitle.text = getString(R.string.login_msisdn_confirm_title)
loginGenericTextInputFormNotice.text = getString(R.string.login_msisdn_confirm_notice, params.extra)
loginGenericTextInputFormNotice2.setTextOrHide(null)
loginGenericTextInputFormTil.hint = getString(R.string.login_msisdn_confirm_hint)
loginGenericTextInputFormTextInput.inputType = InputType.TYPE_CLASS_NUMBER
loginGenericTextInputFormOtherButton.isVisible = true
@ -134,8 +140,9 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
loginViewModel.handle(LoginAction.AddThreePid(RegisterThreePid.Email(text)))
}
TextInputFormFragmentMode.SetMsisdn -> {
// TODO Country code
loginViewModel.handle(LoginAction.AddThreePid(RegisterThreePid.Msisdn(text, "FR")))
getCountryCodeOrShowError(text)?.let { countryCode ->
loginViewModel.handle(LoginAction.AddThreePid(RegisterThreePid.Msisdn(text, countryCode)))
}
}
TextInputFormFragmentMode.ConfirmMsisdn -> {
loginViewModel.handle(LoginAction.ValidateThreePid(text))
@ -144,6 +151,23 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
}
}
private fun getCountryCodeOrShowError(text: String): String? {
// We expect an international format for the moment (see https://github.com/vector-im/riotX-android/issues/693)
if (text.startsWith("+")) {
try {
val phoneNumber = PhoneNumberUtil.getInstance().parse(text, null)
return PhoneNumberUtil.getInstance().getRegionCodeForCountryCode(phoneNumber.countryCode)
} catch (e: NumberParseException) {
loginGenericTextInputFormTil.error = getString(R.string.login_msisdn_error_other)
}
} else {
loginGenericTextInputFormTil.error = getString(R.string.login_msisdn_error_not_international)
}
// Error
return null
}
private fun setupSubmitButton() {
if (params.mandatory) {
loginGenericTextInputFormSubmit.isEnabled = false

View File

@ -24,16 +24,26 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_vertical_margin"
android:layout_marginBottom="26dp"
android:gravity="start"
android:textAppearance="@style/TextAppearance.Vector.Login.Text.Small"
tools:text="@string/login_set_email_notice" />
<TextView
android:id="@+id/loginGenericTextInputFormNotice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:textAppearance="@style/TextAppearance.Vector.Login.Text.Small"
android:visibility="gone"
tools:text="@string/login_set_msisdn_notice2"
tools:visibility="visible" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loginGenericTextInputFormTil"
style="@style/VectorTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
app:errorEnabled="true"
tools:hint="@string/login_set_email_optional_hint">

View File

@ -80,6 +80,7 @@
<string name="login_set_msisdn_title">Set phone number</string>
<string name="login_set_msisdn_notice">Set a phone number to optionally allow people you know to discover you.</string>
<string name="login_set_msisdn_notice2">Please use the international format.</string>
<string name="login_set_msisdn_mandatory_hint">Phone number</string>
<string name="login_set_msisdn_optional_hint">Phone number (optional)</string>
<string name="login_set_msisdn_submit">Next</string>
@ -91,6 +92,9 @@
<string name="login_msisdn_confirm_send_again">Send again</string>
<string name="login_msisdn_confirm_submit">Next</string>
<string name="login_msisdn_error_not_international">"International phone numbers must start with '+'"</string>
<string name="login_msisdn_error_other">"Phone number seems invalid. Please check it"</string>
<!-- Replaced string is the homeserver url -->
<string name="login_signup_to">Sign up to %1$s</string>
<string name="login_signup_username_hint">Username</string>