summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java
diff options
context:
space:
mode:
authorEric Erfanian <erfanian@google.com>2017-08-31 06:57:16 -0700
committerRoland Levillain <rpl@google.com>2017-09-04 18:05:19 +0100
commitfc0eb8ccebcc7846db5e8b5c5430070055679bfa (patch)
treea8a23c3202e4161ffd57a71095a404db1bcf5735 /java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java
parentea7b4dc89590ffa3332766a531e0eab6ffb9aebd (diff)
Update Dialer source to latest internal Google revision.
Previously, Android's Dialer app was developed in an internal Google source control system and only exported to public during AOSP drops. The Dialer team is now switching to a public development model similar to the telephony team. This CL represents all internal Google changes that were committed to Dialer between the public O release and today's tip of tree on internal master. This CL squashes those changes into a single commit. In subsequent changes, changes will be exported on a per-commit basis. (cherry picked from commit 2ca4318cc1ee57dda907ba2069bd61d162b1baef and amended to match paths of dependencies under prebuilts/maven_repo/bumptech/com/github/bumptech/glide/.) This CL was generated using these commands from a repository at stage-stage-master at revision ea7b4dc89590ffa3332766a531e0eab6ffb9aebd ("Merge "Update Dialer source to latest internal Google revision." am: c39ea3c55f -s ours"): git diff --binary 2ca4318cc1ee57dda907ba2069bd61d162b1baef | git apply -R --index git commit -c 2ca4318cc1ee57dda907ba2069bd61d162b1baef Test: make, flash install, run Change-Id: I529aaeb88535b9533c0ae4ef4e6c1222d4e0f1c8 PiperOrigin-RevId: 167068436
Diffstat (limited to 'java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java')
-rw-r--r--java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java65
1 files changed, 31 insertions, 34 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) {