Format phone number

This commit is contained in:
Benoit Marty 2020-08-28 19:07:41 +02:00
parent da4695ff2a
commit 58938a239e
4 changed files with 55 additions and 16 deletions

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2020 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
import com.google.i18n.phonenumbers.PhoneNumberUtil
import org.matrix.android.sdk.api.extensions.tryThis
import org.matrix.android.sdk.api.session.identity.ThreePid
fun ThreePid.getFormattedValue(): String {
return when (this) {
is ThreePid.Email -> email
is ThreePid.Msisdn -> {
tryThis(message = "Unable to parse the phone number") {
PhoneNumberUtil.getInstance().parse("+$msisdn", null)
}
?.let {
PhoneNumberUtil.getInstance().format(it, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)
}
?: msisdn
}
}
}

View file

@ -23,19 +23,18 @@ import com.airbnb.mvrx.Incomplete
import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import com.airbnb.mvrx.Uninitialized import com.airbnb.mvrx.Uninitialized
import com.google.i18n.phonenumbers.PhoneNumberUtil
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.attributes.ButtonStyle import im.vector.app.core.epoxy.attributes.ButtonStyle
import im.vector.app.core.epoxy.attributes.ButtonType import im.vector.app.core.epoxy.attributes.ButtonType
import im.vector.app.core.epoxy.attributes.IconMode import im.vector.app.core.epoxy.attributes.IconMode
import im.vector.app.core.epoxy.loadingItem import im.vector.app.core.epoxy.loadingItem
import im.vector.app.core.error.ErrorFormatter import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.extensions.getFormattedValue
import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.identity.SharedState import org.matrix.android.sdk.api.session.identity.SharedState
import org.matrix.android.sdk.api.session.identity.ThreePid import org.matrix.android.sdk.api.session.identity.ThreePid
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.net.ssl.HttpsURLConnection import javax.net.ssl.HttpsURLConnection
@ -235,16 +234,7 @@ class DiscoverySettingsController @Inject constructor(
} }
private fun buildMsisdn(pidInfo: PidInfo) { private fun buildMsisdn(pidInfo: PidInfo) {
val phoneNumber = try { val phoneNumber = pidInfo.threePid.getFormattedValue()
PhoneNumberUtil.getInstance().parse("+${pidInfo.threePid.value}", null)
} catch (t: Throwable) {
Timber.e(t, "Unable to parse the phone number")
null
}
?.let {
PhoneNumberUtil.getInstance().format(it, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)
}
?: pidInfo.threePid.value
buildThreePid(pidInfo, phoneNumber) buildThreePid(pidInfo, phoneNumber)

View file

@ -24,6 +24,8 @@ import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.loadingItem import im.vector.app.core.epoxy.loadingItem
import im.vector.app.core.epoxy.noResultItem
import im.vector.app.core.extensions.getFormattedValue
import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import im.vector.app.core.ui.list.genericButtonItem import im.vector.app.core.ui.list.genericButtonItem
@ -107,20 +109,30 @@ class ThreePidsSettingsController @Inject constructor(
?.filterIsInstance(ThreePid.Msisdn::class.java) ?.filterIsInstance(ThreePid.Msisdn::class.java)
?.forEach { buildPendingThreePid("p_msisdn ", it) } ?.forEach { buildPendingThreePid("p_msisdn ", it) }
/*
// TODO Support adding MSISDN
genericButtonItem { genericButtonItem {
id("addMsisdn") id("addMsisdn")
text(stringProvider.getString(R.string.settings_add_phone_number)) text(stringProvider.getString(R.string.settings_add_phone_number))
textColor(colorProvider.getColor(R.color.riotx_accent)) textColor(colorProvider.getColor(R.color.riotx_accent))
buttonClickAction(View.OnClickListener { interactionListener?.addMsisdn() }) buttonClickAction(View.OnClickListener { interactionListener?.addMsisdn() })
} }
*/
// Avoid empty area
if (msisdn.isEmpty()) {
noResultItem {
id("no_msisdn")
text(stringProvider.getString(R.string.settings_phone_numbers_empty))
}
}
} }
private fun buildThreePid(idPrefix: String, threePid: ThreePid) { private fun buildThreePid(idPrefix: String, threePid: ThreePid) {
threePidItem { threePidItem {
id(idPrefix + threePid.value) id(idPrefix + threePid.value)
// TODO Add an icon for emails // TODO Add an icon for emails
iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null) // iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null)
title(threePid.value) title(threePid.getFormattedValue())
deleteClickListener { interactionListener?.deleteThreePid(threePid) } deleteClickListener { interactionListener?.deleteThreePid(threePid) }
} }
} }
@ -129,8 +141,8 @@ class ThreePidsSettingsController @Inject constructor(
threePidItem { threePidItem {
id(idPrefix + threePid.value) id(idPrefix + threePid.value)
// TODO Add an icon for emails // TODO Add an icon for emails
iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null) // iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null)
title(threePid.value) title(threePid.getFormattedValue())
} }
if (threePid is ThreePid.Email) { if (threePid is ThreePid.Email) {

View file

@ -684,6 +684,7 @@
<string name="settings_emails">Email addresses</string> <string name="settings_emails">Email addresses</string>
<string name="settings_phone_numbers">Phone numbers</string> <string name="settings_phone_numbers">Phone numbers</string>
<string name="settings_phone_numbers_empty">No phone number has been added to your account</string>
<string name="settings_remove_three_pid_confirmation_content">Remove %s?</string> <string name="settings_remove_three_pid_confirmation_content">Remove %s?</string>
<string name="error_threepid_auth_failed">Ensure that you have clicked on the link in the email we have sent to you.</string> <string name="error_threepid_auth_failed">Ensure that you have clicked on the link in the email we have sent to you.</string>