diff options
Diffstat (limited to 'src/com/android/dialer/dialpad/SmartDialNameMatcher.java')
| -rw-r--r-- | src/com/android/dialer/dialpad/SmartDialNameMatcher.java | 146 |
1 files changed, 99 insertions, 47 deletions
diff --git a/src/com/android/dialer/dialpad/SmartDialNameMatcher.java b/src/com/android/dialer/dialpad/SmartDialNameMatcher.java index 01268641d..532a06994 100644 --- a/src/com/android/dialer/dialpad/SmartDialNameMatcher.java +++ b/src/com/android/dialer/dialpad/SmartDialNameMatcher.java @@ -16,13 +16,17 @@ package com.android.dialer.dialpad; +import android.content.Context; import android.text.TextUtils; +import android.util.Log; +import com.android.dialer.database.DialerDatabaseHelper; import com.android.dialer.dialpad.SmartDialPrefix.PhoneNumberTokens; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; +import java.lang.reflect.Method; import java.util.ArrayList; /** @@ -34,6 +38,8 @@ import java.util.ArrayList; */ public class SmartDialNameMatcher { + private final static String TAG = "SmartDialNameMatcher"; + private String mQuery; // Whether or not we allow matches like 57 - (J)ohn (S)mith @@ -52,14 +58,24 @@ public class SmartDialNameMatcher { private String mNameMatchMask = ""; private String mPhoneNumberMatchMask = ""; + private Context mContext; + private String mSchar = "+*#-.(,)/ "; + private Object mMultiMatchObject; + private Method mMultiMatchMethod; + @VisibleForTesting - public SmartDialNameMatcher(String query) { - this(query, LATIN_SMART_DIAL_MAP); + public SmartDialNameMatcher(String query, Context context) { + this(query, LATIN_SMART_DIAL_MAP, context); } - public SmartDialNameMatcher(String query, SmartDialMap map) { + public SmartDialNameMatcher(String query, SmartDialMap map, Context context) { mQuery = query; mMap = map; + mContext = context; + mMultiMatchObject = DialerDatabaseHelper.getInstance(mContext) + .getMultiMatchObject(); + mMultiMatchMethod = DialerDatabaseHelper.getInstance(mContext) + .getMultiMatchMethod(); } /** @@ -130,22 +146,6 @@ public class SmartDialNameMatcher { // Try matching the number as is SmartDialMatchPosition matchPos = matchesNumberWithOffset(phoneNumber, query, 0); - if (matchPos == null) { - final PhoneNumberTokens phoneNumberTokens = - SmartDialPrefix.parsePhoneNumber(phoneNumber); - - if (phoneNumberTokens == null) { - return matchPos; - } - if (phoneNumberTokens.countryCodeOffset != 0) { - matchPos = matchesNumberWithOffset(phoneNumber, query, - phoneNumberTokens.countryCodeOffset); - } - if (matchPos == null && phoneNumberTokens.nanpCodeOffset != 0 && useNanp) { - matchPos = matchesNumberWithOffset(phoneNumber, query, - phoneNumberTokens.nanpCodeOffset); - } - } if (matchPos != null) { replaceBitInMask(builder, matchPos); mPhoneNumberMatchMask = builder.toString(); @@ -190,40 +190,47 @@ public class SmartDialNameMatcher { */ private SmartDialMatchPosition matchesNumberWithOffset(String phoneNumber, String query, int offset) { - if (TextUtils.isEmpty(phoneNumber) || TextUtils.isEmpty(query)) { + if (TextUtils.isEmpty(phoneNumber) || TextUtils.isEmpty(query) + || query.length() > phoneNumber.length()) { return null; } - int queryAt = 0; - int numberAt = offset; - for (int i = offset; i < phoneNumber.length(); i++) { - if (queryAt == query.length()) { - break; + + String phoneNum = phoneNumber.replaceAll("[\\+\\*\\#\\-\\.\\(\\,\\)\\/ ]", ""); + if (!TextUtils.isEmpty(phoneNum) && phoneNum.contains(query)) { + // firstly, find the start position in original phone number. + int start = phoneNum.indexOf(query); + int length = phoneNumber.length(); + for (int i = start; i < length; i++) { + char ch = phoneNumber.charAt(i); + if (ch != phoneNum.charAt(start)) { + continue; + } + if (phoneNumber.substring(i).replaceAll("[\\+\\*\\#\\-\\.\\(\\,\\)\\/ ]", "") + .indexOf(query) == 0) { + start = i; + break; + } } - char ch = phoneNumber.charAt(i); - if (mMap.isValidDialpadNumericChar(ch)) { - if (ch != query.charAt(queryAt)) { - return null; + // secondly, find the end position in original phone number. + int specialCount = 0; + int queryLength = query.length(); + int end = start + queryLength; + for (int i = start; i < length; i++) { + char ch = phoneNumber.charAt(i); + if (mSchar.indexOf(ch) != -1) { + specialCount++; + continue; } - queryAt++; - } else { - if (queryAt == 0) { - // Found a separator before any part of the query was matched, so advance the - // offset to avoid prematurely highlighting separators before the rest of the - // query. - // E.g. don't highlight the first '-' if we're matching 1-510-111-1111 with - // '510'. - // However, if the current offset is 0, just include the beginning separators - // anyway, otherwise the highlighting ends up looking weird. - // E.g. if we're matching (510)-111-1111 with '510', we should include the - // first '('. - if (offset != 0) { - offset++; - } + + if (i - start + 1 - specialCount == queryLength) { + end = i + 1; + break; } } - numberAt++; + return new SmartDialMatchPosition(start, end); + } else { + return null; } - return new SmartDialMatchPosition(0 + offset, numberAt); } /** @@ -407,7 +414,11 @@ public class SmartDialNameMatcher { public boolean matches(String displayName) { mMatchPositions.clear(); - return matchesCombination(displayName, mQuery, mMatchPositions); + if (mMultiMatchObject != null && mMultiMatchMethod != null) { + return matchesMultiLanguage(displayName, mQuery, mMatchPositions); + } else { + return matchesCombination(displayName, mQuery, mMatchPositions); + } } public ArrayList<SmartDialMatchPosition> getMatchPositions() { @@ -431,4 +442,45 @@ public class SmartDialNameMatcher { public String getQuery() { return mQuery; } + + boolean matchesMultiLanguage(String displayName, String query, + ArrayList<SmartDialMatchPosition> matchList) { + StringBuilder builder = new StringBuilder(); + constructEmptyMask(builder, displayName.length()); + mNameMatchMask = builder.toString(); + final int nameLength = displayName.length(); + final int queryLength = query.length(); + + if (queryLength == 0) { + return false; + } + // contains the start, not the end poing + try { + int[] indexs = (int[]) mMultiMatchMethod.invoke(mMultiMatchObject, + query, displayName, 0); + // mMultimatch.getMatchStringIndex(query, displayName, 0); + if (indexs == null) { + return false; + } + for (int i = 0; i < indexs.length; i = i + 2) { + int start = indexs[i]; + int end = indexs[i + 1]; + if (start >= 0 && end >= 0) { + matchList.add(new SmartDialMatchPosition(start, end + 1)); + } else { + Log.d(TAG, "Invalid index, start is:" + start + " end is:" + + end + " for name:" + displayName); + } + } + } catch (Exception e) { + Log.d(TAG, "Exception:" + e); + return false; + } + + for (SmartDialMatchPosition match : matchList) { + replaceBitInMask(builder, match); + } + mNameMatchMask = builder.toString(); + return true; + } } |
