diff options
author | calderwoodra <calderwoodra@google.com> | 2017-08-23 16:51:40 -0700 |
---|---|---|
committer | Eric Erfanian <erfanian@google.com> | 2017-08-30 21:29:00 +0000 |
commit | 28b5b246106d94c3770fde7c656f86d65c0bfe89 (patch) | |
tree | bc3fb43d1588599392821efdd009a326856bbef9 /java/com/android | |
parent | 9cbf3ce9c73eca62a8b45d7b4e35135e7d476fef (diff) |
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
Diffstat (limited to 'java/com/android')
3 files changed, 50 insertions, 42 deletions
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. * - * <p>Some example: + * <p>Some example of matches: * * <ul> * <li>"query" would bold "John [query] Smith" @@ -38,6 +40,13 @@ public class QueryBoldingUtil { * <li>"222" would bold "[A]llen [A]lex [A]aron" * </ul> * + * <p>Some examples of non-matches: + * + * <ul> + * <li>"ss" would not match "Jessica Jones" + * <li>"77" would not match "Jessica Jones" + * </ul> + * * @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 + * + * <p>Examples: + * + * <ul> + * <li>#nameMatchesT9Query("7", "John Smith") returns true, 7 -> 'S' + * <li>#nameMatchesT9Query("55", "Jessica Jones") returns true, 55 -> 'JJ' + * <li>#nameMatchesT9Query("56", "Jessica Jones") returns true, 56 -> 'Jo' + * <li>#nameMatchesT9Query("7", "Jessica Jones") returns false, no names start with P,Q,R or S + * </ul> */ 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"); diff --git a/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml b/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml index 07113c044..cde9cb6a5 100644 --- a/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml +++ b/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml @@ -22,11 +22,13 @@ <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" - android:layout_height="match_parent"/> + android:layout_height="match_parent" + android:background="@color/background_dialer_light"/> <com.android.dialer.widget.EmptyContentView android:id="@+id/empty_view" android:layout_width="match_parent" android:layout_height="match_parent" - android:visibility="gone"/> + android:visibility="gone" + android:background="@color/background_dialer_light"/> </FrameLayout> |