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
committerEric Erfanian <erfanian@google.com>2017-08-31 16:13:53 +0000
commit2ca4318cc1ee57dda907ba2069bd61d162b1baef (patch)
treee282668a9587cf6c1ec7b604dea860400c75c6c7 /java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java
parent68038172793ee0e2ab3e2e56ddfbeb82879d1f58 (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. Test: make, flash install, run Merged-In: I45270eaa8ce732d71a1bd84b08c7fa0e99af3160 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.java151
1 files changed, 151 insertions, 0 deletions
diff --git a/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java b/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java
new file mode 100644
index 000000000..4413252f4
--- /dev/null
+++ b/java/com/android/dialer/searchfragment/common/QueryBoldingUtil.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.dialer.searchfragment.common;
+
+import android.graphics.Typeface;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+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 {
+
+ /**
+ * Compares a name and query and returns a {@link CharSequence} with bolded characters.
+ *
+ * <p>Some example of matches:
+ *
+ * <ul>
+ * <li>"query" would bold "John [query] Smith"
+ * <li>"222" would bold "[AAA] Mom"
+ * <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.
+ */
+ public static CharSequence getNameWithQueryBolded(@Nullable String query, @NonNull String name) {
+ if (TextUtils.isEmpty(query)) {
+ return name;
+ }
+
+ 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;
+ }
+ }
+
+ 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);
+ }
+ }
+
+ private static CharSequence getNameWithInitialsBolded(String query, String name) {
+ SpannableString boldedInitials = new SpannableString(name);
+ name = name.toLowerCase();
+ int initialsBolded = 0;
+ int nameIndex = -1;
+
+ while (++nameIndex < name.length() && initialsBolded < query.length()) {
+ if ((nameIndex == 0 || name.charAt(nameIndex - 1) == ' ')
+ && QueryFilteringUtil.getDigit(name.charAt(nameIndex)) == query.charAt(initialsBolded)) {
+ boldedInitials.setSpan(
+ new StyleSpan(Typeface.BOLD),
+ nameIndex,
+ nameIndex + 1,
+ Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ initialsBolded++;
+ }
+ }
+ return boldedInitials;
+ }
+
+ /**
+ * Compares a number and a query and returns a {@link CharSequence} with bolded characters.
+ *
+ * <ul>
+ * <li>"123" would bold "(650)34[1-23]24"
+ * <li>"123" would bold "+1([123])111-2222
+ * </ul>
+ *
+ * @param query containing only numbers and phone number related characters "(", ")", "-", "+"
+ * @param number phone number of a contact that the query will compare to.
+ * @return number with query bolded if query can be found in the number.
+ */
+ public static CharSequence getNumberWithQueryBolded(
+ @Nullable String query, @NonNull String number) {
+ if (TextUtils.isEmpty(query) || !QueryFilteringUtil.numberMatchesNumberQuery(query, number)) {
+ return number;
+ }
+
+ int index = QueryFilteringUtil.indexOfQueryNonDigitsIgnored(query, number);
+ int boldedCharacters = query.length();
+
+ for (char c : query.toCharArray()) {
+ if (!Character.isDigit(c)) {
+ boldedCharacters--;
+ }
+ }
+
+ for (int i = 0; i < index + boldedCharacters; i++) {
+ if (!Character.isDigit(number.charAt(i))) {
+ if (i <= index) {
+ index++;
+ } else {
+ boldedCharacters++;
+ }
+ }
+ }
+ return getBoldedString(number, index, boldedCharacters);
+ }
+
+ private static SpannableString getBoldedString(String s, int index, int numBolded) {
+ SpannableString span = new SpannableString(s);
+ span.setSpan(
+ new StyleSpan(Typeface.BOLD), index, index + numBolded, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
+ return span;
+ }
+}