Fixed bug where appended chips might be added twice.

Since email focuses the compose field instead of the to field
the to field received an immediate unfocus event, which caused any
defaults in the field to be committed. This caused double commits
of a single contact as a chip. This makes sure we dont commit
a pending chip in onSizeChanged that has already been handled.
Integrating into Email revealed this bug.

Change-Id: I65f0f38f1dabe0b1434f0d2e4bcd2ff9a579e621
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index 53f7fad..dbd1988 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -211,6 +211,9 @@
     public void onFocusChanged(boolean hasFocus, int direction, Rect previous) {
         if (!hasFocus) {
             shrink();
+            // Reset any pending chips as they would have been handled
+            // when the field lost focus.
+            mPendingChipsCount = 0;
         } else {
             expand();
         }
@@ -448,19 +451,21 @@
             while (startingPos < editable.length() && mPendingChipsCount > 0) {
                 int tokenEnd = mTokenizer.findTokenEnd(editable, startingPos);
                 int tokenStart = mTokenizer.findTokenStart(editable, tokenEnd);
-                // Always include seperators with the token to the left.
-                if (tokenEnd < editable.length() - 1
-                        && editable.charAt(tokenEnd) == COMMIT_CHAR_COMMA) {
-                    tokenEnd++;
+                if (findChip(tokenStart) == null) {
+                    // Always include seperators with the token to the left.
+                    if (tokenEnd < editable.length() - 1
+                            && editable.charAt(tokenEnd) == COMMIT_CHAR_COMMA) {
+                        tokenEnd++;
+                    }
+                    startingPos = tokenEnd;
+                    String token = (String) editable.toString().substring(tokenStart, tokenEnd);
+                    int seperatorPos = token.indexOf(COMMIT_CHAR_COMMA);
+                    if (seperatorPos != -1) {
+                        token = token.substring(0, seperatorPos);
+                    }
+                    editable.replace(tokenStart, tokenEnd, createChip(RecipientEntry
+                            .constructFakeEntry(token), false));
                 }
-                startingPos = tokenEnd;
-                String token = (String) editable.toString().substring(tokenStart, tokenEnd);
-                int seperatorPos = token.indexOf(COMMIT_CHAR_COMMA);
-                if (seperatorPos != -1) {
-                    token = token.substring(0, seperatorPos);
-                }
-                editable.replace(tokenStart, tokenEnd, createChip(RecipientEntry
-                        .constructFakeEntry(token), false));
                 mPendingChipsCount--;
             }
             mPendingChipsCount = 0;