mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-24 13:08:46 +00:00
Fix duplicate e164 short codes cleanup bug.
This commit is contained in:
@@ -173,6 +173,37 @@ object E164Util {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strictly checks if a given number is a valid short code for a given region. Short code length varies by region and some
|
||||
* require specific prefixes.
|
||||
*
|
||||
* This will check the input with and without a leading '+' sign.
|
||||
*
|
||||
* If the number cannot be parsed or is otherwise invalid, false is returned.
|
||||
*/
|
||||
private fun isValidShortNumber(regionCode: String, input: String): Boolean {
|
||||
try {
|
||||
val correctedInput = input.e164CharsOnly().stripLeadingZerosFromInput()
|
||||
val correctedWithoutLeading = correctedInput.trimStart('+', '0')
|
||||
|
||||
var parsedNumber: PhoneNumber = PhoneNumberUtil.getInstance().parse(correctedInput, regionCode)
|
||||
|
||||
val isShortCode = ShortNumberInfo.getInstance().isValidShortNumberForRegion(parsedNumber, regionCode) || correctedWithoutLeading.length <= 6
|
||||
if (isShortCode) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (correctedInput != correctedWithoutLeading) {
|
||||
parsedNumber = PhoneNumberUtil.getInstance().parse(correctedInput.trimStart('+', '0'), regionCode)
|
||||
return ShortNumberInfo.getInstance().isValidShortNumberForRegion(parsedNumber, regionCode) || correctedInput.length <= 6
|
||||
}
|
||||
|
||||
return false
|
||||
} catch (_: NumberParseException) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to parse the area code out of an e164-formatted number provided that it's in one of the supported countries.
|
||||
*/
|
||||
@@ -273,6 +304,10 @@ object E164Util {
|
||||
}
|
||||
}
|
||||
|
||||
fun isValidShortNumber(input: String): Boolean {
|
||||
return isValidShortNumber(localRegionCode, input)
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the number for human-readable display. e.g. "(555) 555-5555"
|
||||
*/
|
||||
|
||||
@@ -155,4 +155,25 @@ class E164UtilTest {
|
||||
val formatter: E164Util.Formatter = E164Util.createFormatterForRegionCode("US")
|
||||
Assert.assertEquals("+14151111122", formatter.formatAsE164("(415) 111-1122"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isValidShortNumber - multiple regions`() {
|
||||
// India
|
||||
var formatter: E164Util.Formatter = E164Util.createFormatterForE164("+911234567890")
|
||||
Assert.assertTrue(formatter.isValidShortNumber("543212601"))
|
||||
Assert.assertTrue(formatter.isValidShortNumber("+543212601"))
|
||||
Assert.assertFalse(formatter.isValidShortNumber("1234567890"))
|
||||
|
||||
// Australia
|
||||
formatter = E164Util.createFormatterForE164("+61111111111")
|
||||
Assert.assertTrue(formatter.isValidShortNumber("1258881"))
|
||||
Assert.assertTrue(formatter.isValidShortNumber("+1258881"))
|
||||
Assert.assertFalse(formatter.isValidShortNumber("+111111111"))
|
||||
|
||||
// US
|
||||
formatter = E164Util.createFormatterForE164("+15555555555")
|
||||
Assert.assertTrue(formatter.isValidShortNumber("125811"))
|
||||
Assert.assertTrue(formatter.isValidShortNumber("+121581"))
|
||||
Assert.assertFalse(formatter.isValidShortNumber("+15555555555"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user