diff options
author | Yorke Lee <yorkelee@google.com> | 2013-04-30 17:49:03 -0700 |
---|---|---|
committer | Yorke Lee <yorkelee@google.com> | 2013-05-01 13:41:22 -0700 |
commit | 006d071bd477768b4685385d15b3385f6d2e834d (patch) | |
tree | 864213e1c7f10a01e07c8108365684811790640d /src | |
parent | ec2a6103d517a1896abffc493e5f883049872ca9 (diff) |
Apply NANP logic to numbers with a +1 country code
Bug 8769688
Change-Id: I4aadd0f9e5495fb1b604910306dfd918d1540136
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/dialer/dialpad/SmartDialNameMatcher.java | 25 | ||||
-rw-r--r-- | src/com/android/dialer/dialpad/SmartDialTrie.java | 57 |
2 files changed, 55 insertions, 27 deletions
diff --git a/src/com/android/dialer/dialpad/SmartDialNameMatcher.java b/src/com/android/dialer/dialpad/SmartDialNameMatcher.java index f8877c665..f7ae1c232 100644 --- a/src/com/android/dialer/dialpad/SmartDialNameMatcher.java +++ b/src/com/android/dialer/dialpad/SmartDialNameMatcher.java @@ -18,6 +18,8 @@ package com.android.dialer.dialpad; import android.text.TextUtils; +import com.android.dialer.dialpad.SmartDialTrie.CountryCodeWithOffset; + import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; @@ -426,8 +428,19 @@ public class SmartDialNameMatcher { * @return Phone number consisting of digits from 0-9 */ public static String normalizeNumber(String number) { + return normalizeNumber(number, 0); + } + + /** + * Strips a phone number of unnecessary characters (spaces, dashes, etc.) + * + * @param number Phone number we want to normalize + * @param offset Offset to start from + * @return Phone number consisting of digits from 0-9 + */ + public static String normalizeNumber(String number, int offset) { final StringBuilder s = new StringBuilder(); - for (int i = 0; i < number.length(); i++) { + for (int i = offset; i < number.length(); i++) { char ch = number.charAt(i); if (ch >= '0' && ch <= '9') { s.append(ch); @@ -452,10 +465,12 @@ public class SmartDialNameMatcher { SmartDialMatchPosition matchPos = matchesNumberWithOffset(phoneNumber, query, 0); if (matchPos == null) { // Try matching the number without the '+' prefix, if any - int offset = SmartDialTrie.getOffsetWithoutCountryCode(phoneNumber); - if (offset > 0) { - matchPos = matchesNumberWithOffset(phoneNumber, query, offset); - } else if (matchNanp) { + final CountryCodeWithOffset code = SmartDialTrie.getOffsetWithoutCountryCode( + phoneNumber); + if (code != null) { + matchPos = matchesNumberWithOffset(phoneNumber, query, code.offset); + } + if (matchPos == null && matchNanp) { // Try matching NANP numbers final int[] offsets = SmartDialTrie.getOffsetForNANPNumbers(phoneNumber); for (int i = 0; i < offsets.length; i++) { diff --git a/src/com/android/dialer/dialpad/SmartDialTrie.java b/src/com/android/dialer/dialpad/SmartDialTrie.java index 04ff64c5b..ab935b73a 100644 --- a/src/com/android/dialer/dialpad/SmartDialTrie.java +++ b/src/com/android/dialer/dialpad/SmartDialTrie.java @@ -45,13 +45,33 @@ public class SmartDialTrie { int nthLastTokenPos; } - private static final int LAST_TOKENS_FOR_INITIALS = 2; - private static final int FIRST_TOKENS_FOR_INITIALS = 2; + /** + * A country code and integer offset pair that represents the parsed country code in a + * phone number. The country code is a string containing the numeric country-code prefix in + * a phone number (e.g. 1 or 852). The offset is the integer position of where the country code + * ends in a phone number. + */ + public static class CountryCodeWithOffset { + public static final CountryCodeWithOffset NO_COUNTRY_CODE = new CountryCodeWithOffset(0, + ""); + + final String countryCode; + final int offset; + + public CountryCodeWithOffset(int offset, String countryCode) { + this.countryCode = countryCode; + this.offset = offset; + } + } + final Node mRoot = new Node(); private int mSize = 0; private final char[] mCharacterMap; private final boolean mFormatNanp; + private static final int LAST_TOKENS_FOR_INITIALS = 2; + private static final int FIRST_TOKENS_FOR_INITIALS = 2; + // Static set of all possible country codes in the world public static Set<String> sCountryCodes = null; @@ -134,12 +154,14 @@ public class SmartDialTrie { // Strip the calling code from the phone number here if (!TextUtils.isEmpty(contact.phoneNumber)) { // Handle country codes for numbers with a + prefix - final int offset = getOffsetWithoutCountryCode(contact.phoneNumber); - if (offset > 0) { - putNumber(contact, contact.phoneNumber, offset); - } else if (mFormatNanp) { + final CountryCodeWithOffset code = getOffsetWithoutCountryCode(contact.phoneNumber); + if (code.offset != 0) { + putNumber(contact, contact.phoneNumber, code.offset); + } + if ((code.countryCode.equals("1") || code.offset == 0) && mFormatNanp) { // Special case handling for NANP numbers (1-xxx-xxx-xxxx) - final String stripped = SmartDialNameMatcher.normalizeNumber(contact.phoneNumber); + final String stripped = SmartDialNameMatcher.normalizeNumber( + contact.phoneNumber, code.offset); if (!TextUtils.isEmpty(stripped)) { int trunkPrefixOffset = 0; if (stripped.charAt(0) == '1') { @@ -159,24 +181,25 @@ public class SmartDialTrie { } } } - putNumber(contact, contact.phoneNumber); + putNumber(contact, contact.phoneNumber, 0); } mSize++; } - public static int getOffsetWithoutCountryCode(String number) { + public static CountryCodeWithOffset getOffsetWithoutCountryCode(String number) { if (!TextUtils.isEmpty(number)) { if (number.charAt(0) == '+') { // check for international code here for (int i = 1; i <= 1 + 3; i++) { if (number.length() <= i) break; - if (isValidCountryCode(number.substring(1, i))) { - return i; + final String countryCode = number.substring(1, i); + if (isValidCountryCode(countryCode)) { + return new CountryCodeWithOffset(i, countryCode); } } } } - return -1; + return CountryCodeWithOffset.NO_COUNTRY_CODE; } /** @@ -278,16 +301,6 @@ public class SmartDialTrie { * * @param contact - Contact to add to the trie * @param phoneNumber - Phone number of the contact - */ - private void putNumber(ContactNumber contact, String phoneNumber) { - putNumber(contact, phoneNumber, 0); - } - - /** - * Puts a phone number and its associated contact into the prefix trie. - * - * @param contact - Contact to add to the trie - * @param phoneNumber - Phone number of the contact * @param offSet - The nth character of the phone number to start from */ private void putNumber(ContactNumber contact, String phoneNumber, int offSet) { |