diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt index 9f979098f8..ec5a8bc644 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt @@ -16,6 +16,11 @@ package org.matrix.android.sdk.api.extensions +import java.util.regex.Pattern + +const val emailPattern = "^[a-zA-Z0-9_!#\$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#\$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*\$" +val emailAddress: Pattern = Pattern.compile(emailPattern) + fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence { return when { startsWith(prefix) -> this @@ -23,6 +28,11 @@ fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence { } } +/** + * Check if a CharSequence is an email. + */ +fun CharSequence.isEmail() = emailAddress.matcher(this).matches() + /** * Append a new line and then the provided string. */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt index 0a8c58de16..86341729ca 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt @@ -16,11 +16,11 @@ package org.matrix.android.sdk.internal.auth.login -import android.util.Patterns import org.matrix.android.sdk.api.auth.LoginType import org.matrix.android.sdk.api.auth.login.LoginProfileInfo import org.matrix.android.sdk.api.auth.login.LoginWizard import org.matrix.android.sdk.api.auth.registration.RegisterThreePid +import org.matrix.android.sdk.api.extensions.isEmail import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.internal.auth.AuthAPI @@ -59,7 +59,7 @@ internal class DefaultLoginWizard( initialDeviceName: String, deviceId: String? ): Session { - val loginParams = if (Patterns.EMAIL_ADDRESS.matcher(login).matches()) { + val loginParams = if (login.isEmail()) { PasswordLoginParams.thirdPartyIdentifier( medium = ThreePidMedium.EMAIL, address = login, diff --git a/vector/src/main/java/im/vector/app/core/extensions/BasicExtensions.kt b/vector/src/main/java/im/vector/app/core/extensions/BasicExtensions.kt index 6bcbfe0ed5..692c85311a 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/BasicExtensions.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/BasicExtensions.kt @@ -21,6 +21,7 @@ import com.google.i18n.phonenumbers.NumberParseException import com.google.i18n.phonenumbers.PhoneNumberUtil import org.matrix.android.sdk.api.MatrixPatterns import org.matrix.android.sdk.api.extensions.ensurePrefix +import org.matrix.android.sdk.api.extensions.isEmail fun Boolean.toOnOff() = if (this) "ON" else "OFF" @@ -29,7 +30,7 @@ inline fun T.ooi(block: (T) -> Unit): T = also(block) /** * Check if a CharSequence is an email. */ -fun CharSequence.isEmail() = Patterns.EMAIL_ADDRESS.matcher(this).matches() +fun CharSequence.isEmail() = this.isEmail() fun CharSequence.isMatrixId() = MatrixPatterns.isUserId(this.toString())