From 28b5b246106d94c3770fde7c656f86d65c0bfe89 Mon Sep 17 00:00:00 2001 From: calderwoodra Date: Wed, 23 Aug 2017 16:51:40 -0700 Subject: Improved new search UI matching and bolding. from the bug: 2) Entering a letter in search bar gives the search results with that letter in the string instead of words starting with that letter screenshots: http://screen/inh3JmYbQkj http://screen/xyFk6xJfM7n Bug: 64902476 Test: existing PiperOrigin-RevId: 166275561 Change-Id: I020b9af2f567361980a750b2e71c58e33d0c6374 --- .../searchfragment/common/QueryBoldingUtil.java | 65 +++++++++++----------- .../searchfragment/common/QueryFilteringUtil.java | 21 +++++-- 2 files changed, 46 insertions(+), 40 deletions(-) (limited to 'java/com/android/dialer/searchfragment/common') diff --git a/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java b/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java index 7bdd69567..4413252f4 100644 --- a/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java +++ b/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java @@ -23,6 +23,8 @@ import android.text.SpannableString; import android.text.Spanned; import android.text.TextUtils; import android.text.style.StyleSpan; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** Utility class for handling bolding queries contained in string. */ public class QueryBoldingUtil { @@ -30,7 +32,7 @@ public class QueryBoldingUtil { /** * Compares a name and query and returns a {@link CharSequence} with bolded characters. * - *

Some example: + *

Some example of matches: * *

* + *

Some examples of non-matches: + * + *

+ * * @param query containing any characters * @param name of a contact/string that query will compare to * @return name with query bolded if query can be found in the name. @@ -47,43 +56,31 @@ public class QueryBoldingUtil { return name; } - int index = -1; - int numberOfBoldedCharacters = 0; - - if (QueryFilteringUtil.nameMatchesT9Query(query, name)) { - // Bold the characters that match the t9 query - String t9 = QueryFilteringUtil.getT9Representation(name); - index = QueryFilteringUtil.indexOfQueryNonDigitsIgnored(query, t9); - if (index == -1) { - return getNameWithInitialsBolded(query, name); - } - numberOfBoldedCharacters = query.length(); - - for (int i = 0; i < query.length(); i++) { - char c = query.charAt(i); - if (!Character.isDigit(c)) { - numberOfBoldedCharacters--; - } - } - - for (int i = 0; i < index + numberOfBoldedCharacters; i++) { - if (!Character.isLetterOrDigit(name.charAt(i))) { - if (i < index) { - index++; - } else { - numberOfBoldedCharacters++; - } - } + if (!QueryFilteringUtil.nameMatchesT9Query(query, name)) { + Pattern pattern = Pattern.compile("(^|\\s)" + Pattern.quote(query.toLowerCase())); + Matcher matcher = pattern.matcher(name.toLowerCase()); + if (matcher.find()) { + // query matches the start of a name (i.e. "jo" -> "Jessica [Jo]nes") + return getBoldedString(name, matcher.start(), query.length()); + } else { + // query not found in name + return name; } } - if (index == -1) { - // Bold the query as an exact match in the name - index = name.toLowerCase().indexOf(query); - numberOfBoldedCharacters = query.length(); + Pattern pattern = Pattern.compile("(^|\\s)" + Pattern.quote(query.toLowerCase())); + Matcher matcher = pattern.matcher(QueryFilteringUtil.getT9Representation(name)); + if (matcher.find()) { + // query matches the start of a T9 name (i.e. 75 -> "Jessica [Jo]nes") + int index = matcher.start(); + // TODO(calderwoodra): investigate why this is consistently off by one. + index = index == 0 ? 0 : index + 1; + return getBoldedString(name, index, query.length()); + + } else { + // query match the T9 initials (i.e. 222 -> "[A]l [B]ob [C]harlie") + return getNameWithInitialsBolded(query, name); } - - return index == -1 ? name : getBoldedString(name, index, numberOfBoldedCharacters); } private static CharSequence getNameWithInitialsBolded(String query, String name) { diff --git a/java/com/android/dialer/searchfragment/common/QueryFilteringUtil.java b/java/com/android/dialer/searchfragment/common/QueryFilteringUtil.java index 0d22a824c..775f8deec 100644 --- a/java/com/android/dialer/searchfragment/common/QueryFilteringUtil.java +++ b/java/com/android/dialer/searchfragment/common/QueryFilteringUtil.java @@ -25,25 +25,34 @@ import java.util.regex.Pattern; public class QueryFilteringUtil { /** Matches strings with "-", "(", ")", 2-9 of at least length one. */ - static final Pattern T9_PATTERN = Pattern.compile("[\\-()2-9]+"); + private static final Pattern T9_PATTERN = Pattern.compile("[\\-()2-9]+"); /** - * @return true if the query is of T9 format and the name's T9 representation belongs to the - * query; false otherwise. + * Returns true if the query is of T9 format and the name's T9 representation belongs to the query + * + *

Examples: + * + *

*/ public static boolean nameMatchesT9Query(String query, String name) { if (!T9_PATTERN.matcher(query).matches()) { return false; } - // Substring - if (indexOfQueryNonDigitsIgnored(query, getT9Representation(name)) != -1) { + query = digitsOnly(query); + Pattern pattern = Pattern.compile("(^|\\s)" + Pattern.quote(query)); + if (pattern.matcher(getT9Representation(name)).find()) { + // query matches the start of a T9 name (i.e. 75 -> "Jessica [Jo]nes") return true; } // Check matches initials // TODO(calderwoodra) investigate faster implementation - query = digitsOnly(query); int queryIndex = 0; String[] names = name.toLowerCase().split("\\s"); -- cgit v1.2.3