Improve robustness around unknown country codes.

pull/1/head
Matthew Chen 7 years ago
parent 73655de3d9
commit 9b4ac4073f

@ -215,7 +215,7 @@ NS_ASSUME_NONNULL_BEGIN
}
if (!countryMetadata) {
countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode];
countryCode = [PhoneNumber defaultCountryCode];
if (countryCode) {
countryMetadata = [OWSCountryMetadata countryMetadataForCountryCode:countryCode];
}

@ -98,7 +98,7 @@ NS_ASSUME_NONNULL_BEGIN
}
if (!countryMetadata) {
countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode];
countryCode = [PhoneNumber defaultCountryCode];
if (countryCode) {
countryMetadata = [OWSCountryMetadata countryMetadataForCountryCode:countryCode];
}

@ -241,8 +241,7 @@ NSString *const kKeychainKey_LastRegisteredPhoneNumber = @"kKeychainKey_LastRegi
#pragma mark - Country
- (void)populateDefaultCountryNameAndCode {
NSLocale *locale = NSLocale.currentLocale;
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
NSString *countryCode = [PhoneNumber defaultCountryCode];
#ifdef DEBUG
if ([self lastRegisteredCountryCode].length > 0) {

@ -224,8 +224,7 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien
}
if (!countryCode || !callingCode) {
NSLocale *locale = NSLocale.currentLocale;
countryCode = [locale objectForKey:NSLocaleCountryCode];
countryCode = [PhoneNumber defaultCountryCode];
callingCode = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil getCountryCodeForRegion:countryCode];
}

@ -1573,6 +1573,9 @@
/* Displayed if for some reason we can't determine a contacts phone number *or* name */
"UNKNOWN_CONTACT_NAME" = "Unknown Contact";
/* Indicates an unknown or unrecognizable value. */
"UNKNOWN_VALUE" = "Unknown";
/* button title for unlinking a device */
"UNLINK_ACTION" = "Unlink";

@ -42,4 +42,6 @@
- (NSComparisonResult)compare:(PhoneNumber *)other;
+ (NSString *)defaultCountryCode;
@end

@ -62,21 +62,28 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
+ (PhoneNumber *)phoneNumberFromUserSpecifiedText:(NSString *)text {
OWSAssert(text != nil);
return [PhoneNumber phoneNumberFromText:text andRegion:[self defaultRegionCode]];
return [PhoneNumber phoneNumberFromText:text andRegion:[self defaultCountryCode]];
}
+ (NSString *)defaultRegionCode {
NSString *defaultRegion;
+ (NSString *)defaultCountryCode
{
NSLocale *locale = [NSLocale currentLocale];
NSString *_Nullable countryCode = nil;
#if TARGET_OS_IPHONE
defaultRegion = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil countryCodeByCarrier];
countryCode = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil countryCodeByCarrier];
if ([defaultRegion isEqualToString:@"ZZ"]) {
defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
if ([countryCode isEqualToString:@"ZZ"]) {
countryCode = [locale objectForKey:NSLocaleCountryCode];
}
#else
defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
countryCode = [locale objectForKey:NSLocaleCountryCode];
#endif
return defaultRegion;
if (!countryCode) {
OWSFail(@"%@ Could not identify country code for locale: %@", self.logTag, locale);
countryCode = @"US";
}
return countryCode;
}
+ (PhoneNumber *)phoneNumberFromE164:(NSString *)text {
@ -90,7 +97,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
+ (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input {
return [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:input
withSpecifiedRegionCode:[self defaultRegionCode]];
withSpecifiedRegionCode:[self defaultCountryCode]];
}
+ (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input
@ -144,9 +151,9 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
static NSString *result = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *defaultRegionCode = [self defaultRegionCode];
NSString *defaultCountryCode = [self defaultCountryCode];
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
NBPhoneMetaData *defaultRegionMetadata = [helper getMetadataForRegion:defaultRegionCode];
NBPhoneMetaData *defaultRegionMetadata = [helper getMetadataForRegion:defaultCountryCode];
result = defaultRegionMetadata.nationalPrefixTransformRule;
});
return result;
@ -168,7 +175,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
NSString *localCallingCodePrefix = [NSString stringWithFormat:@"+%@", localCallingCode];
NSString *localCountryCode =
[PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:localCallingCodePrefix];
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) {
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) {
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
NBPhoneMetaData *localNumberRegionMetadata = [helper getMetadataForRegion:localCountryCode];
result = localNumberRegionMetadata.nationalPrefixTransformRule;
@ -248,7 +255,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
}
};
tryParsingWithCountryCode(sanitizedString, [self defaultRegionCode]);
tryParsingWithCountryCode(sanitizedString, [self defaultCountryCode]);
if ([sanitizedString hasPrefix:@"+"]) {
// If the text starts with "+", don't try prepending
@ -257,7 +264,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
}
// Try just adding "+" and parsing it.
tryParsingWithCountryCode([NSString stringWithFormat:@"+%@", sanitizedString], [self defaultRegionCode]);
tryParsingWithCountryCode([NSString stringWithFormat:@"+%@", sanitizedString], [self defaultCountryCode]);
// Order matters; better results should appear first so prefer
// matches with the same country code as this client's phone number.
@ -272,7 +279,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
NSString *callingCodePrefix = [NSString stringWithFormat:@"+%@", callingCodeForLocalNumber];
tryParsingWithCountryCode(
[callingCodePrefix stringByAppendingString:sanitizedString], [self defaultRegionCode]);
[callingCodePrefix stringByAppendingString:sanitizedString], [self defaultCountryCode]);
// Try to determine what the country code is for the local phone number
// and also try parsing the phone number using that country code if it
@ -283,7 +290,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
// phone. They're likely to have both Italian and French contacts.
NSString *localCountryCode =
[PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:callingCodePrefix];
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) {
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) {
tryParsingWithCountryCode(
[callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode);
}

@ -82,13 +82,18 @@
// country code -> country name
+ (NSString *)countryNameFromCountryCode:(NSString *)countryCode {
OWSAssert(countryCode);
NSDictionary *countryCodeComponent = @{NSLocaleCountryCode : countryCode};
NSString *identifier = [NSLocale localeIdentifierFromComponents:countryCodeComponent];
NSString *country = [NSLocale.currentLocale displayNameForKey:NSLocaleIdentifier value:identifier];
if (country.length < 1) {
country = [NSLocale.systemLocale displayNameForKey:NSLocaleIdentifier value:identifier];
NSString *countryName = [NSLocale.currentLocale displayNameForKey:NSLocaleIdentifier value:identifier];
if (countryName.length < 1) {
countryName = [NSLocale.systemLocale displayNameForKey:NSLocaleIdentifier value:identifier];
}
if (countryName.length < 1) {
countryName = NSLocalizedString(@"UNKNOWN_VALUE", "Indicates an unknown or unrecognizable value.");
}
return country;
return countryName;
}
// country code -> calling code

Loading…
Cancel
Save