diff options
| author | Bai Tao <michaelbai@google.com> | 2010-07-29 05:36:54 +0800 |
|---|---|---|
| committer | Bai Tao <michaelbai@google.com> | 2010-07-29 06:54:25 +0800 |
| commit | fae6ec9e9673e96085fdbde69866a41b5491fa0d (patch) | |
| tree | a5d19d95559d43fc8384e1b3366e073113f2db99 /telephony/java/android | |
| parent | ebd19e0f48b0cb246c246274a256a9bb494b7e7a (diff) | |
| download | frameworks_base-fae6ec9e9673e96085fdbde69866a41b5491fa0d.zip frameworks_base-fae6ec9e9673e96085fdbde69866a41b5491fa0d.tar.gz frameworks_base-fae6ec9e9673e96085fdbde69866a41b5491fa0d.tar.bz2 | |
Added country based formatNumber methods
a. Format the number to E164.
b. Format the number based on country convention.
c. Normailize the number.
Change-Id: I39d59b99aa1a5136371dd20c74d9d9c8ec855369
Diffstat (limited to 'telephony/java/android')
| -rw-r--r-- | telephony/java/android/telephony/PhoneNumberUtils.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java index 920f38e..8f46df7 100644 --- a/telephony/java/android/telephony/PhoneNumberUtils.java +++ b/telephony/java/android/telephony/PhoneNumberUtils.java @@ -16,6 +16,11 @@ package android.telephony; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; + import android.content.Context; import android.content.Intent; import android.database.Cursor; @@ -1319,6 +1324,88 @@ public class PhoneNumberUtils } } + /** + * Format the given phoneNumber to the E.164 representation. + * <p> + * The given phone number must have an area code and could have a country + * code. + * <p> + * The defaultCountryIso is used to validate the given number and generate + * the E.164 phone number if the given number doesn't have a country code. + * + * @param phoneNumber + * the phone number to format + * @param defaultCountryIso + * the ISO 3166-1 two letters country code + * @return the E.164 representation, or null if the given phone number is + * not valid. + * + * @hide + */ + public static String formatNumberToE164(String phoneNumber, String defaultCountryIso) { + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); + String result = null; + try { + PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso); + if (util.isValidNumber(pn)) { + result = util.format(pn, PhoneNumberFormat.E164); + } + } catch (NumberParseException e) { + } + return result; + } + + /** + * Format a phone number. + * <p> + * If the given number doesn't have the country code, the phone will be + * formatted to the default country's convention. + * + * @param phoneNumber + * the number to be formatted. + * @param defaultCountryIso + * the ISO 3166-1 two letters country code whose convention will + * be used if the given number doesn't have the country code. + * @return the formatted number, or null if the given number is not valid. + * + * @hide + */ + public static String formatNumber(String phoneNumber, String defaultCountryIso) { + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); + String result = null; + try { + PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso); + result = util.formatInOriginalFormat(pn, defaultCountryIso); + } catch (NumberParseException e) { + } + return result; + } + + /** + * 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. + * + * @param phoneNumber + * the number to be normalized. + * @return the normalized number. + * + * @hide + */ + public static String normalizeNumber(String phoneNumber) { + StringBuilder sb = new StringBuilder(); + int len = phoneNumber.length(); + for (int i = 0; i < len; i++) { + char c = phoneNumber.charAt(i); + if (PhoneNumberUtils.isISODigit(c)) { + sb.append(c); + } else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { + return normalizeNumber(PhoneNumberUtils.convertKeypadLettersToDigits(phoneNumber)); + } + } + return sb.toString(); + } + // Three and four digit phone numbers for either special services, // or 3-6 digit addresses from the network (eg carrier-originated SMS messages) should // not match. |
