diff options
| author | Bai Tao <michaelbai@google.com> | 2010-08-25 14:10:10 +0800 |
|---|---|---|
| committer | Bai Tao <michaelbai@google.com> | 2010-08-30 10:52:15 +0800 |
| commit | 2a4e96067d074ef78aeafea13469a85690346b60 (patch) | |
| tree | ad15d48473a553a3efd23fcd4bda267001ca4b4b /telephony/java/android | |
| parent | 1604ae64564ff088a46efbc072c68024bfc8325f (diff) | |
| download | frameworks_base-2a4e96067d074ef78aeafea13469a85690346b60.zip frameworks_base-2a4e96067d074ef78aeafea13469a85690346b60.tar.gz frameworks_base-2a4e96067d074ef78aeafea13469a85690346b60.tar.bz2 | |
Added new method to format the phone number when neccessary.
Change-Id: I90fb38c1d36dc1ccf7eaa3b4cc34cd29f5151493
Diffstat (limited to 'telephony/java/android')
| -rw-r--r-- | telephony/java/android/telephony/PhoneNumberUtils.java | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java index b0fa0f5..4f2d90f 100644 --- a/telephony/java/android/telephony/PhoneNumberUtils.java +++ b/telephony/java/android/telephony/PhoneNumberUtils.java @@ -1374,7 +1374,7 @@ public class PhoneNumberUtils PhoneNumberUtil util = PhoneNumberUtil.getInstance(); String result = null; try { - PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso); + PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso); result = util.formatInOriginalFormat(pn, defaultCountryIso); } catch (NumberParseException e) { } @@ -1382,6 +1382,50 @@ public class PhoneNumberUtils } /** + * Format the phone number only if the given number hasn't been formatted. + * <p> + * The number which has only dailable character is treated as not being + * formatted. + * + * @param phoneNumber + * the number to be formatted. + * @param phoneNumberE164 + * the E164 format number whose country code is used if the given + * phoneNumber doesn't have the country code. + * @param defaultCountryIso + * the ISO 3166-1 two letters country code whose convention will + * be used if the phoneNumberE164 is null or invalid. + * @return the formatted number if the given number has been formatted, + * otherwise, return the given number. + * + * @hide + */ + public static String formatNumber( + String phoneNumber, String phoneNumberE164, String defaultCountryIso) { + int len = phoneNumber.length(); + for (int i = 0; i < len; i++) { + if (!isDialable(phoneNumber.charAt(i))) { + return phoneNumber; + } + } + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); + // Get the country code from phoneNumberE164 + if (phoneNumberE164 != null && phoneNumberE164.length() >= 2 + && phoneNumberE164.charAt(0) == '+') { + try { + PhoneNumber pn = util.parse(phoneNumberE164, defaultCountryIso); + String regionCode = util.getRegionCodeForNumber(pn); + if (!TextUtils.isEmpty(regionCode)) { + defaultCountryIso = regionCode; + } + } catch (NumberParseException e) { + } + } + String result = formatNumber(phoneNumber, defaultCountryIso); + return result != null ? result : phoneNumber; + } + + /** * Normalize a phone number by removing the characters other than digits. If * the given number has keypad letters, the letters will be converted to * digits first. |
