Adding customized chip color support

Adding customized chip color attributes to selected and unselected states, so that
chip consumers can use their own colors for the chips to better serve their UX
requirements. Without specifying these colors, the chips UI will keep the current
default color scheme

Bug: 18293881
Change-Id: I269c478df312ea634f90bd1c97a7a08c3d14d9d7
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index d3500aa..be838bf 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -31,5 +31,9 @@
             <enum name="bottom" value = "0"/>
             <enum name="baseline" value = "1"/>
         </attr>
+        <attr name="unselectedChipBackgroundColor" format="color" />
+        <attr name="selectedChipBackgroundColor" format="color" />
+        <attr name="unselectedChipTextColor" format="color" />
+        <attr name="selectedChipTextColor" format="color" />
     </declare-styleable>
 </resources>
\ No newline at end of file
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index 7604b11..6541180 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -139,7 +139,10 @@
 
     private static final int MAX_CHIPS_PARSED = 50;
 
-    private static int sSelectedTextColor = -1;
+    private int mSelectedChipTextColor;
+    private int mUnselectedChipTextColor;
+    private int mSelectedChipBackgroundColor;
+    private int mUnselectedChipBackgroundColor;
 
     // Work variables to avoid re-allocation on every typed character.
     private final Rect mRect = new Rect();
@@ -263,9 +266,6 @@
         super(context, attrs);
         setChipDimensions(context, attrs);
         mTextHeight = calculateTextHeight();
-        if (sSelectedTextColor == -1) {
-            sSelectedTextColor = context.getResources().getColor(android.R.color.white);
-        }
         mAlternatesPopup = new ListPopupWindow(context);
         setupPopupWindow(mAlternatesPopup);
         mAddressPopup = new ListPopupWindow(context);
@@ -667,9 +667,9 @@
      * @param paint The paint to use to draw the bitmap.
      */
     private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint) {
-        paint.setColor(sSelectedTextColor);
+        paint.setColor(mSelectedChipTextColor);
         final ChipBitmapContainer bitmapContainer = createChipBitmap(contact, paint,
-                mChipBackgroundPressed, getResources().getColor(R.color.chip_background_selected));
+                mChipBackgroundPressed, mSelectedChipBackgroundColor);
 
         if (bitmapContainer.loadIcon) {
             loadAvatarIcon(contact, bitmapContainer);
@@ -685,7 +685,7 @@
      * @param paint The paint to use to draw the bitmap.
      */
     private Bitmap createUnselectedChip(RecipientEntry contact, TextPaint paint) {
-        paint.setColor(getContext().getResources().getColor(android.R.color.black));
+        paint.setColor(getDefaultChipTextColor(contact));
         ChipBitmapContainer bitmapContainer = createChipBitmap(contact, paint,
                 getChipBackground(contact), getDefaultChipBackgroundColor(contact));
 
@@ -862,9 +862,14 @@
         return contact.isValid() ? mChipBackground : mInvalidChipBackground;
     }
 
+    private int getDefaultChipTextColor(RecipientEntry contact) {
+        return contact.isValid() ? mUnselectedChipTextColor :
+                getResources().getColor(android.R.color.black);
+    }
+
     private int getDefaultChipBackgroundColor(RecipientEntry contact) {
-        return getResources().getColor(contact.isValid() ? R.color.chip_background :
-                R.color.chip_background_invalid);
+        return contact.isValid() ? mUnselectedChipBackgroundColor :
+                getResources().getColor(R.color.chip_background_invalid);
     }
 
     /**
@@ -1015,6 +1020,21 @@
         mMaxLines = r.getInteger(R.integer.chips_max_lines);
         mLineSpacingExtra = r.getDimensionPixelOffset(R.dimen.line_spacing_extra);
 
+        mUnselectedChipTextColor = a.getColor(
+                R.styleable.RecipientEditTextView_unselectedChipTextColor,
+                r.getColor(android.R.color.black));
+
+        mSelectedChipTextColor = a.getColor(
+                R.styleable.RecipientEditTextView_selectedChipTextColor,
+                r.getColor(android.R.color.white));
+
+        mUnselectedChipBackgroundColor = a.getColor(
+                R.styleable.RecipientEditTextView_unselectedChipBackgroundColor,
+                r.getColor(R.color.chip_background));
+
+        mSelectedChipBackgroundColor = a.getColor(
+                R.styleable.RecipientEditTextView_selectedChipBackgroundColor,
+                r.getColor(R.color.chip_background_selected));
         a.recycle();
     }