Don't autocomplete unknown email addresses

The user often manually inputs a correct email address that Gmail does
not know about. In this case, we don't want to autocorrect it to the
closest match we find (even when the user shifts the focus away) from
the contacts list.

b/12443656

Change-Id: I1d7694f2bdd1bc876889dd9b74999accb853cac8
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index 4339b9e..cea1988 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -1290,8 +1290,12 @@
         ListAdapter adapter = getAdapter();
         if (adapter != null && adapter.getCount() > 0 && enoughToFilter()
                 && end == getSelectionEnd() && !isPhoneQuery()) {
-            // choose the first entry.
-            submitItemAtPosition(0);
+            // let's choose the first entry if only the input text is NOT an email address
+            // so we won't try to replace the user's potentially correct but new/unencountered
+            // email input
+            if (!isValidEmailAddress(editable.toString().substring(start, end).trim())) {
+                submitItemAtPosition(0);
+            }
             dismissDropDown();
             return true;
         } else {
@@ -2310,6 +2314,11 @@
         super.removeTextChangedListener(watcher);
     }
 
+    private boolean isValidEmailAddress(String input) {
+        return !TextUtils.isEmpty(input) && mValidator != null &&
+                mValidator.isValid(input);
+    }
+
     private class RecipientTextWatcher implements TextWatcher {
 
         @Override
@@ -2369,8 +2378,7 @@
                         int tokenStart = mTokenizer.findTokenStart(text, getSelectionEnd());
                         String sub = text.substring(tokenStart, mTokenizer.findTokenEnd(text,
                                 tokenStart));
-                        if (!TextUtils.isEmpty(sub) && mValidator != null &&
-                                mValidator.isValid(sub)) {
+                        if (isValidEmailAddress(sub)) {
                             commitByCharacter();
                         }
                     }