am e5d17d57: Highlight the text that matches the constraint

* commit 'e5d17d57be3b89a6adb7d429a4cb3cb1fbb422ff':
  Highlight the text that matches the constraint
diff --git a/res/color/chips_dropdown_subtitle_text.xml b/res/color/chips_dropdown_text.xml
similarity index 82%
rename from res/color/chips_dropdown_subtitle_text.xml
rename to res/color/chips_dropdown_text.xml
index e7b0f3e..7d333bc 100644
--- a/res/color/chips_dropdown_subtitle_text.xml
+++ b/res/color/chips_dropdown_text.xml
@@ -17,7 +17,7 @@
 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 
-    <item android:state_activated="true" android:color="@android:color/white"/>
-    <item android:color="#757575"/>
+    <item android:state_activated="true" android:color="@color/chips_dropdown_text_activated"/>
+    <item android:color="@color/chips_dropdown_text_default"/>
 
-</selector>
\ No newline at end of file
+</selector>
diff --git a/res/color/chips_dropdown_title_text.xml b/res/color/chips_dropdown_title_text.xml
deleted file mode 100644
index 7c79390..0000000
--- a/res/color/chips_dropdown_title_text.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2014 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.
--->
-
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <item android:state_activated="true" android:color="@android:color/white"/>
-    <item android:color="#212121"/>
-
-</selector>
\ No newline at end of file
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 5bdfd0f..5130a32 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -26,4 +26,8 @@
     <color name="chip_background_selected">#4285f4</color>
     <color name="chip_background_invalid">#db4437</color>
 
-</resources>
\ No newline at end of file
+    <color name="chips_dropdown_text_default">#212121</color>
+    <color name="chips_dropdown_text_activated">@android:color/white</color>
+    <color name="chips_dropdown_text_highlighted">#757575</color>
+
+</resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 5c7dcec..8cb48e0 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -59,7 +59,7 @@
         <item name="android:ellipsize">middle</item>
         <item name="android:singleLine">true</item>
         <item name="android:textAlignment">viewStart</item>
-        <item name="android:textColor">@color/chips_dropdown_title_text</item>
+        <item name="android:textColor">@color/chips_dropdown_text</item>
         <item name="android:textSize">16sp</item>
     </style>
 
@@ -70,7 +70,7 @@
         <item name="android:ellipsize">middle</item>
         <item name="android:singleLine">true</item>
         <item name="android:textAlignment">viewStart</item>
-        <item name="android:textColor">@color/chips_dropdown_subtitle_text</item>
+        <item name="android:textColor">@color/chips_dropdown_text</item>
         <item name="android:textSize">14sp</item>
     </style>
 
diff --git a/src/com/android/ex/chips/DropdownChipLayouter.java b/src/com/android/ex/chips/DropdownChipLayouter.java
index b9738dd..44d9c43 100644
--- a/src/com/android/ex/chips/DropdownChipLayouter.java
+++ b/src/com/android/ex/chips/DropdownChipLayouter.java
@@ -9,7 +9,11 @@
 import android.support.annotation.DrawableRes;
 import android.support.annotation.IdRes;
 import android.support.annotation.LayoutRes;
+import android.support.annotation.Nullable;
+import android.text.SpannableStringBuilder;
+import android.text.Spanned;
 import android.text.TextUtils;
+import android.text.style.ForegroundColorSpan;
 import android.text.util.Rfc822Tokenizer;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -83,8 +87,10 @@
     public View bindView(View convertView, ViewGroup parent, RecipientEntry entry, int position,
             AdapterType type, String constraint, StateListDrawable deleteDrawable) {
         // Default to show all the information
-        String displayName = entry.getDisplayName();
-        String destination = entry.getDestination();
+        CharSequence[] styledResults =
+                getStyledResults(constraint, entry.getDisplayName(), entry.getDestination());
+        CharSequence displayName = styledResults[0];
+        CharSequence destination = styledResults[1];
         boolean showImage = true;
         CharSequence destinationType = getDestinationType(entry);
 
@@ -331,6 +337,64 @@
     protected @IdRes int getDeleteResId() { return android.R.id.icon1; }
 
     /**
+     * Given a constraint and results, tries to find the constraint in those results, one at a time.
+     * A foreground font color style will be applied to the section that matches the constraint. As
+     * soon as a match has been found, no further matches are attempted.
+     *
+     * @param constraint A string that we will attempt to find within the results.
+     * @param results Strings that may contain the constraint. The order given is the order used to
+     *     search for the constraint.
+     *
+     * @return An array of CharSequences, the length determined by the length of results. Each
+     *     CharSequence will either be a styled SpannableString or just the input String.
+     */
+    protected CharSequence[] getStyledResults(@Nullable String constraint, String... results) {
+        if (isAllWhitespace(constraint)) {
+            return results;
+        }
+
+        CharSequence[] styledResults = new CharSequence[results.length];
+        boolean foundMatch = false;
+        for (int i = 0; i < results.length; i++) {
+            String result = results[i];
+            if (result == null) {
+                continue;
+            }
+
+            if (!foundMatch) {
+                int index = result.toLowerCase().indexOf(constraint.toLowerCase());
+                if (index != -1) {
+                    SpannableStringBuilder styled = SpannableStringBuilder.valueOf(result);
+                    ForegroundColorSpan highlightSpan =
+                            new ForegroundColorSpan(mContext.getResources().getColor(
+                                    R.color.chips_dropdown_text_highlighted));
+                    styled.setSpan(highlightSpan,
+                            index, index + constraint.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                    styledResults[i] = styled;
+                    foundMatch = true;
+                    continue;
+                }
+            }
+            styledResults[i] = result;
+        }
+        return styledResults;
+    }
+
+    private static boolean isAllWhitespace(@Nullable String string) {
+        if (TextUtils.isEmpty(string)) {
+            return true;
+        }
+
+        for (int i = 0; i < string.length(); ++i) {
+            if (!Character.isWhitespace(string.charAt(i))) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    /**
      * A holder class the view. Uses the getters in DropdownChipLayouter to find the id of the
      * corresponding views.
      */