Handle the case where a user is editing an invalid contact chip correctly.

We now treat an invalid contact chip (ie just text the user is editing
that was a chip) as being a selected chip. Make sure we handle the
edit cases for this correctly.

Fixes bug:5288176 Email address not distinguished in Email application

Change-Id: Ia9d3803f22d6d2f92cf280fc65b9d95b1a21c713
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index 930d9a2..692ce30 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -1045,6 +1045,9 @@
                 }
                 chipWasSelected = true;
                 handled = true;
+            } else if (mSelectedChip != null
+                    && mSelectedChip.getContactId() == RecipientEntry.INVALID_CONTACT) {
+                chipWasSelected = true;
             }
         }
         if (action == MotionEvent.ACTION_UP && !chipWasSelected) {
@@ -1525,7 +1528,10 @@
         Editable editable = getText();
         mSelectedChip = null;
         if (start == -1 || end == -1) {
-            Log.e(TAG, "The chip being unselected no longer exists.");
+            Log.w(TAG,
+                    "The chip doesn't exist or may be a chip a user was editing");
+            setSelection(editable.length());
+            commitDefault();
         } else {
             getSpannable().removeSpan(chip);
             QwertyKeyListener.markAsReplaced(editable, start, end, "");
@@ -1544,7 +1550,6 @@
         }
     }
 
-
     /**
      * Return whether this chip contains the position passed in.
      */
@@ -1679,7 +1684,9 @@
             if (chipsPending()) {
                 return;
             }
-            if (mSelectedChip != null) {
+            // If the user is editing a chip, don't clear it.
+            if (mSelectedChip != null
+                    && mSelectedChip.getContactId() != RecipientEntry.INVALID_CONTACT) {
                 setCursorVisible(true);
                 setSelection(getText().length());
                 clearSelectedChip();