RecipientEditTextView.java revision 3aa118e72c877b4c176d025c56bfad2e518bf1ce
12d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira/*
22d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * Copyright (C) 2011 The Android Open Source Project
32d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira *
42d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * Licensed under the Apache License, Version 2.0 (the "License");
52d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * you may not use this file except in compliance with the License.
62d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * You may obtain a copy of the License at
72d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira *
82d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira *      http://www.apache.org/licenses/LICENSE-2.0
92d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira *
102d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * Unless required by applicable law or agreed to in writing, software
112d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * distributed under the License is distributed on an "AS IS" BASIS,
122d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * See the License for the specific language governing permissions and
142d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * limitations under the License.
152d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira */
162d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
172d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereirapackage com.android.ex.chips;
182d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
19b8208f24b2768acf369ad58309031feac87ce79cMindy Pereiraimport android.app.Dialog;
20b8208f24b2768acf369ad58309031feac87ce79cMindy Pereiraimport android.content.ClipData;
2149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereiraimport android.content.ClipDescription;
22b8208f24b2768acf369ad58309031feac87ce79cMindy Pereiraimport android.content.ClipboardManager;
232d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.content.Context;
24b8208f24b2768acf369ad58309031feac87ce79cMindy Pereiraimport android.content.DialogInterface;
25b8208f24b2768acf369ad58309031feac87ce79cMindy Pereiraimport android.content.DialogInterface.OnDismissListener;
26a11c2b0e1ea043aa4d8745285f2dcbe20448b417Mindy Pereiraimport android.content.res.Resources;
273da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereiraimport android.content.res.TypedArray;
28c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.Bitmap;
291e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereiraimport android.graphics.BitmapFactory;
30c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.Canvas;
311e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereiraimport android.graphics.Matrix;
323e9631982511d9692e4bc0dbd724240fae91cf1fMinh Phamimport android.graphics.Point;
33c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.Rect;
341e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereiraimport android.graphics.RectF;
35c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.drawable.BitmapDrawable;
362d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.graphics.drawable.Drawable;
3777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereiraimport android.os.AsyncTask;
38156467329e276c9bc90945bea916ce3ac4849574Mindy Pereiraimport android.os.Handler;
39156467329e276c9bc90945bea916ce3ac4849574Mindy Pereiraimport android.os.Message;
4022b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereiraimport android.os.Parcelable;
412d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.text.Editable;
42572fa15d4b847c2d890b972d4f69d4d2aad5ebd7Gilles Debunneimport android.text.InputType;
43c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.Layout;
44c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.Spannable;
45c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.SpannableString;
4677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereiraimport android.text.SpannableStringBuilder;
47c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.Spanned;
48c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.TextPaint;
49c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.TextUtils;
50c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereiraimport android.text.TextWatcher;
51c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.method.QwertyKeyListener;
52c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.style.ImageSpan;
530fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereiraimport android.text.util.Rfc822Token;
540fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereiraimport android.text.util.Rfc822Tokenizer;
552d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.util.AttributeSet;
56c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.util.Log;
5782c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylorimport android.util.Patterns;
584fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.ActionMode;
59a0751874f5bbdb39f2024c69a5024df60f0344d9Andy Stadlerimport android.view.ActionMode.Callback;
603e9631982511d9692e4bc0dbd724240fae91cf1fMinh Phamimport android.view.DragEvent;
61b8208f24b2768acf369ad58309031feac87ce79cMindy Pereiraimport android.view.GestureDetector;
62c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.view.KeyEvent;
63c0f97e4f56bc35d99d181c5f604b26fbb89c113fMindy Pereiraimport android.view.LayoutInflater;
644fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.Menu;
654fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.MenuItem;
66c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.view.MotionEvent;
67c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.view.View;
68572fa15d4b847c2d890b972d4f69d4d2aad5ebd7Gilles Debunneimport android.view.View.OnClickListener;
69416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereiraimport android.view.ViewParent;
70824aef7306c6a2e07e6157c25efe925f048597edMindy Pereiraimport android.view.inputmethod.EditorInfo;
71824aef7306c6a2e07e6157c25efe925f048597edMindy Pereiraimport android.view.inputmethod.InputConnection;
72c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.widget.AdapterView;
73c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.widget.AdapterView.OnItemClickListener;
7475f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylorimport android.widget.Button;
75b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereiraimport android.widget.ListAdapter;
76c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.widget.ListPopupWindow;
77156467329e276c9bc90945bea916ce3ac4849574Mindy Pereiraimport android.widget.ListView;
782d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.widget.MultiAutoCompleteTextView;
79416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereiraimport android.widget.ScrollView;
80c0f97e4f56bc35d99d181c5f604b26fbb89c113fMindy Pereiraimport android.widget.TextView;
81b5ebb8916a1bb178062a0ccb59e80de27ec1bb2dDaisuke Miyakawa
82a0751874f5bbdb39f2024c69a5024df60f0344d9Andy Stadlerimport java.util.ArrayList;
836f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereiraimport java.util.Arrays;
84b5ebb8916a1bb178062a0ccb59e80de27ec1bb2dDaisuke Miyakawaimport java.util.Collection;
856f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereiraimport java.util.Collections;
866f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereiraimport java.util.Comparator;
8777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereiraimport java.util.HashMap;
88c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport java.util.HashSet;
89c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport java.util.Set;
9082c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylorimport java.util.regex.Matcher;
91c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
922d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira/**
932d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * RecipientEditTextView is an auto complete text view for use with applications
942d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * that use the new Chips UI for addressing a message to recipients.
952d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira */
96b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereirapublic class RecipientEditTextView extends MultiAutoCompleteTextView implements
97b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        OnItemClickListener, Callback, RecipientAlternatesAdapter.OnCheckedItemChangedListener,
98e0d95425cbadd7aaff5f8fdbb30ac40cb22c0aa4Mindy Pereira        GestureDetector.OnGestureListener, OnDismissListener, OnClickListener,
99c75bea26b81d1075fac128188a594aa511d94d9bMindy Pereira        TextView.OnEditorActionListener {
100c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
10100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private static final char COMMIT_CHAR_COMMA = ',';
10200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
10326e3f68ae8342cedd57353e399e7d0e920228a4emindyp    private static final char NAME_WRAPPER_CHAR = '"';
10426e3f68ae8342cedd57353e399e7d0e920228a4emindyp
10500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private static final char COMMIT_CHAR_SEMICOLON = ';';
10600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
10700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private static final char COMMIT_CHAR_SPACE = ' ';
10800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
109c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private static final String TAG = "RecipientEditTextView";
110c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
11100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private static int DISMISS = "dismiss".hashCode();
11200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
11300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private static final long DISMISS_DELAY = 300;
11400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
1154e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    // TODO: get correct number/ algorithm from with UX.
1168faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    // Visible for testing.
1178faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    /*package*/ static final int CHIP_LIMIT = 2;
1188faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
1198faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    private static final int MAX_CHIPS_PARSED = 50;
1204e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
12100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private static int sSelectedTextColor = -1;
12200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
12300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Resources for displaying chips.
124c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Drawable mChipBackground = null;
125c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
126c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Drawable mChipDelete = null;
127c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
12800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private Drawable mInvalidChipBackground;
12900be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
13000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private Drawable mChipBackgroundPressed;
13100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
13200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private float mChipHeight;
13300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
13400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private float mChipFontSize;
13500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
1369c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira    private float mLineSpacingExtra;
1379c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira
138c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private int mChipPadding;
139c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
140c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Tokenizer mTokenizer;
141c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
14200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private Validator mValidator;
143c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
144c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private RecipientChip mSelectedChip;
145c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
146c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private int mAlternatesLayout;
147c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1481e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    private Bitmap mDefaultContactPhoto;
1491e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
1504e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private ImageSpan mMoreChip;
1514e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
152c0f97e4f56bc35d99d181c5f604b26fbb89c113fMindy Pereira    private TextView mMoreItem;
1534e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
154054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira    private final ArrayList<String> mPendingChips = new ArrayList<String>();
155a501d9f79683be8d82c348ae50c97e2655d79cceMindy Pereira
156156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira    private Handler mHandler;
157156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira
158c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    private int mPendingChipsCount = 0;
159c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
1608faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    private boolean mNoChips = false;
1618faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
162b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private ListPopupWindow mAlternatesPopup;
163b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
1641174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    private ListPopupWindow mAddressPopup;
1651174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira
16677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira    private ArrayList<RecipientChip> mTemporaryRecipients;
16777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira
1680fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    private ArrayList<RecipientChip> mRemovedSpans;
1690fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira
170bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira    private boolean mShouldShrink = true;
171bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira
172b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    // Chip copy fields.
173b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    private GestureDetector mGestureDetector;
174b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
175b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    private Dialog mCopyDialog;
176b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
177b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    private String mCopyAddress;
178b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
179b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
180a0751874f5bbdb39f2024c69a5024df60f0344d9Andy Stadler     * Used with {@link #mAlternatesPopup}. Handles clicks to alternate addresses for a
181a0751874f5bbdb39f2024c69a5024df60f0344d9Andy Stadler     * selected chip.
182b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
183b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private OnItemClickListener mAlternatesListener;
184b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
185416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira    private int mCheckedItem;
18600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
187e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private TextWatcher mTextWatcher;
188e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira
18900be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Obtain the enclosing scroll view, if it exists, so that the view can be
19000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // scrolled to show the last line of chips content.
191416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira    private ScrollView mScrollView;
192416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira
19300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private boolean mTriedGettingScrollView;
194416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira
1953e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    private boolean mDragEnabled = false;
1963e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
197e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private final Runnable mAddTextWatcher = new Runnable() {
198e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        @Override
199e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        public void run() {
200e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            if (mTextWatcher == null) {
201e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                mTextWatcher = new RecipientTextWatcher();
202e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                addTextChangedListener(mTextWatcher);
203e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            }
204e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        }
205e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    };
206e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira
20777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira    private IndividualReplacementTask mIndividualReplacements;
20877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira
209a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira    private Runnable mHandlePendingChips = new Runnable() {
210a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira
211a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        @Override
212a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        public void run() {
213a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            handlePendingChips();
214a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        }
215a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira
216a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira    };
217a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira
218a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    private Runnable mDelayedShrink = new Runnable() {
219a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira
220a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira        @Override
221a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira        public void run() {
222a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira            shrink();
223a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira        }
224a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira
225a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    };
226a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira
2272d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    public RecipientEditTextView(Context context, AttributeSet attrs) {
2282d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira        super(context, attrs);
2293da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereira        setChipDimensions(context, attrs);
23077db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        if (sSelectedTextColor == -1) {
23177db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira            sSelectedTextColor = context.getResources().getColor(android.R.color.white);
23277db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        }
233b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        mAlternatesPopup = new ListPopupWindow(context);
2341174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        mAddressPopup = new ListPopupWindow(context);
2350436c36bd529ea8ee1864a3017f9ac29d2311c08Mindy Pereira        mCopyDialog = new Dialog(context);
236b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        mAlternatesListener = new OnItemClickListener() {
237b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            @Override
238b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            public void onItemClick(AdapterView<?> adapterView,View view, int position,
239b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    long rowId) {
240368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira                mAlternatesPopup.setOnItemClickListener(null);
241b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                replaceChip(mSelectedChip, ((RecipientAlternatesAdapter) adapterView.getAdapter())
242b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        .getRecipientEntry(position));
243b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                Message delayed = Message.obtain(mHandler, DISMISS);
244368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira                delayed.obj = mAlternatesPopup;
245b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                mHandler.sendMessageDelayed(delayed, DISMISS_DELAY);
246b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                clearComposingText();
247b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            }
248b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        };
2496f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
250c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        setOnItemClickListener(this);
2514fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        setCustomSelectionActionModeCallback(this);
252156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira        mHandler = new Handler() {
253156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira            @Override
254156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira            public void handleMessage(Message msg) {
255156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                if (msg.what == DISMISS) {
256b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    ((ListPopupWindow) msg.obj).dismiss();
257156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                    return;
258156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                }
259156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                super.handleMessage(msg);
260156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira            }
261156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira        };
2626ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        mTextWatcher = new RecipientTextWatcher();
2636ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        addTextChangedListener(mTextWatcher);
264b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mGestureDetector = new GestureDetector(context, this);
265824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        setOnEditorActionListener(this);
266824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira    }
267824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira
268824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira    @Override
269824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira    public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) {
270824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        if (action == EditorInfo.IME_ACTION_DONE) {
271824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            if (commitDefault()) {
272824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira                return true;
273824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            }
274824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            if (mSelectedChip != null) {
275824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira                clearSelectedChip();
276824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira                return true;
277824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            } else if (focusNext()) {
278824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira                return true;
279824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            }
280824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        }
281824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        return false;
282824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira    }
283824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira
284824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira    @Override
285824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
286824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        InputConnection connection = super.onCreateInputConnection(outAttrs);
287824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
288824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
289824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            // clear the existing action
290824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            outAttrs.imeOptions ^= imeActions;
291824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            // set the DONE action
292824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
293824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        }
294824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
295824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira            outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
296824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        }
2971896ac000251e74f4939421d3d4d28b69717c52aMindy Pereira        outAttrs.actionLabel = getContext().getString(R.string.done);
298824aef7306c6a2e07e6157c25efe925f048597edMindy Pereira        return connection;
299ed58ebd3f54ae5e73129f7f12c8342f177ed1fc3Erik    }
300cb041c294cd3fe41062f1b76d3bfc7d1cb1981d9Mindy Pereira
30100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /*package*/ RecipientChip getLastChip() {
30200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip last = null;
30300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip[] chips = getSortedRecipients();
30400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        if (chips != null && chips.length > 0) {
30500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            last = chips[chips.length - 1];
306ed58ebd3f54ae5e73129f7f12c8342f177ed1fc3Erik        }
30700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        return last;
3084fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    }
3094fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira
3104fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    @Override
3114fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    public void onSelectionChanged(int start, int end) {
3124fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // When selection changes, see if it is inside the chips area.
3134fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // If so, move the cursor back after the chips again.
31400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip last = getLastChip();
3158faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (last != null && start < getSpannable().getSpanEnd(last)) {
31600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            // Grab the last chip and set the cursor to after it.
31700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            setSelection(Math.min(getSpannable().getSpanEnd(last) + 1, getText().length()));
318d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        }
319d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        super.onSelectionChanged(start, end);
320d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    }
321d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira
32222b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira    @Override
32322b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira    public void onRestoreInstanceState(Parcelable state) {
32422b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira        if (!TextUtils.isEmpty(getText())) {
32522b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira            super.onRestoreInstanceState(null);
32622b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira        } else {
32722b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira            super.onRestoreInstanceState(state);
32822b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira        }
32922b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira    }
33022b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira
33100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    @Override
3327c61ef07ed148922a0b71c4e8ebb741cff2c5ae7Mindy Pereira    public Parcelable onSaveInstanceState() {
3337c61ef07ed148922a0b71c4e8ebb741cff2c5ae7Mindy Pereira        // If the user changes orientation while they are editing, just roll back the selection.
3347c61ef07ed148922a0b71c4e8ebb741cff2c5ae7Mindy Pereira        clearSelectedChip();
3357c61ef07ed148922a0b71c4e8ebb741cff2c5ae7Mindy Pereira        return super.onSaveInstanceState();
3367c61ef07ed148922a0b71c4e8ebb741cff2c5ae7Mindy Pereira    }
3377c61ef07ed148922a0b71c4e8ebb741cff2c5ae7Mindy Pereira
338c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    /**
339c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * Convenience method: Append the specified text slice to the TextView's
340c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * display buffer, upgrading it to BufferType.EDITABLE if it was
341c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * not already editable. Commas are excluded as they are added automatically
342c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * by the view.
343c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     */
344c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    @Override
345c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    public void append(CharSequence text, int start, int end) {
346001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira        // We don't care about watching text changes while appending.
347001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira        if (mTextWatcher != null) {
348001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira            removeTextChangedListener(mTextWatcher);
349001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira        }
350c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        super.append(text, start, end);
351c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        if (!TextUtils.isEmpty(text) && TextUtils.getTrimmedLength(text) > 0) {
35226e3f68ae8342cedd57353e399e7d0e920228a4emindyp            String displayString = text.toString();
3533aa118e72c877b4c176d025c56bfad2e518bf1cemindyp            int separatorPos = displayString.lastIndexOf(COMMIT_CHAR_COMMA);
35426e3f68ae8342cedd57353e399e7d0e920228a4emindyp            // Verify that the separator pos is not within ""; if it is, look
35526e3f68ae8342cedd57353e399e7d0e920228a4emindyp            // past the closing quote. If there is no comma past ", this string
35626e3f68ae8342cedd57353e399e7d0e920228a4emindyp            // will resolve to an error chip.
35726e3f68ae8342cedd57353e399e7d0e920228a4emindyp            if (separatorPos > -1) {
3583aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                String parseDisplayString = displayString.substring(separatorPos);
3593aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                int endQuotedTextPos = parseDisplayString.indexOf(NAME_WRAPPER_CHAR);
36026e3f68ae8342cedd57353e399e7d0e920228a4emindyp                if (endQuotedTextPos > separatorPos) {
3613aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                    separatorPos = parseDisplayString.lastIndexOf(COMMIT_CHAR_COMMA,
3623aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                            endQuotedTextPos);
3633aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                }
3643aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                if (separatorPos == -1) {
3653aa118e72c877b4c176d025c56bfad2e518bf1cemindyp                    separatorPos = displayString.length();
36626e3f68ae8342cedd57353e399e7d0e920228a4emindyp                }
36726e3f68ae8342cedd57353e399e7d0e920228a4emindyp            }
36826e3f68ae8342cedd57353e399e7d0e920228a4emindyp            if (separatorPos > 0 && !TextUtils.isEmpty(displayString)
369c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                    && TextUtils.getTrimmedLength(displayString) > 0) {
370c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                mPendingChipsCount++;
3715e05ba80bdb12dfb5f40f202f57c55f5b68a311aTom Taylor                mPendingChips.add(text.toString());
372c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            }
373c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        }
3743aa118e72c877b4c176d025c56bfad2e518bf1cemindyp        // Put a message on the queue to make sure we ALWAYS handle pending
3753aa118e72c877b4c176d025c56bfad2e518bf1cemindyp        // chips.
376a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        if (mPendingChipsCount > 0) {
377a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            postHandlePendingChips();
378a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        }
3796ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        mHandler.post(mAddTextWatcher);
380c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    }
381c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
382d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    @Override
383d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    public void onFocusChanged(boolean hasFocus, int direction, Rect previous) {
384b67763f7bccadc3a34037d9d47c7f3107f639195Mindy Pereira        super.onFocusChanged(hasFocus, direction, previous);
385d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        if (!hasFocus) {
3864e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            shrink();
3874fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        } else {
3884e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            expand();
3894fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        }
3902d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
3912d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
392b67763f7bccadc3a34037d9d47c7f3107f639195Mindy Pereira    @Override
393b67763f7bccadc3a34037d9d47c7f3107f639195Mindy Pereira    public void performValidation() {
394b67763f7bccadc3a34037d9d47c7f3107f639195Mindy Pereira        // Do nothing. Chips handles its own validation.
395b67763f7bccadc3a34037d9d47c7f3107f639195Mindy Pereira    }
396b67763f7bccadc3a34037d9d47c7f3107f639195Mindy Pereira
3974e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private void shrink() {
39807a230a45345b16793463e32194f820eafa2a626Mindy Pereira        if (mTokenizer == null) {
39907a230a45345b16793463e32194f820eafa2a626Mindy Pereira            return;
40007a230a45345b16793463e32194f820eafa2a626Mindy Pereira        }
4015a197cb9af43e1db6b882d89c23949577ceba9a4Mindy Pereira        if (mSelectedChip != null
4025a197cb9af43e1db6b882d89c23949577ceba9a4Mindy Pereira                && mSelectedChip.getEntry().getContactId() != RecipientEntry.INVALID_CONTACT) {
4034e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            clearSelectedChip();
4044e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        } else {
405a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira            if (getWidth() <= 0) {
406a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                // We don't have the width yet which means the view hasn't been drawn yet
407a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                // and there is no reason to attempt to commit chips yet.
408a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                // This focus lost must be the result of an orientation change
409a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                // or an initial rendering.
410a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                // Re-post the shrink for later.
411a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                mHandler.removeCallbacks(mDelayedShrink);
412a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                mHandler.post(mDelayedShrink);
413a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                return;
414a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira            }
415001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira            // Reset any pending chips as they would have been handled
416001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira            // when the field lost focus.
417001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira            if (mPendingChipsCount > 0) {
418a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                postHandlePendingChips();
419001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira            } else {
420001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                Editable editable = getText();
421001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                int end = getSelectionEnd();
422001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                int start = mTokenizer.findTokenStart(editable, end);
423001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                RecipientChip[] chips = getSpannable().getSpans(start, end, RecipientChip.class);
424001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                if ((chips == null || chips.length == 0)) {
425a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                    Editable text = getText();
426a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                    int whatEnd = mTokenizer.findTokenEnd(text, start);
427a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                    // This token was already tokenized, so skip past the ending token.
4288a518e93aef58d5ced2d0223a5fd89b996930e26Mindy Pereira                    if (whatEnd < text.length() && text.charAt(whatEnd) == ',') {
429a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                        whatEnd++;
430a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                    }
431001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                    // In the middle of chip; treat this as an edit
432001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                    // and commit the whole token.
433a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira                    int selEnd = getSelectionEnd();
4344ef6086da2f4bcac5a6cba773f828734a399a879Mindy Pereira                    if (whatEnd != selEnd) {
435001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                        handleEdit(start, whatEnd);
436001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                    } else {
437001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                        commitChip(start, end, editable);
438001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                    }
439001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira                }
440090139db34b366608b60e73f312833d84cf42259Mindy Pereira            }
441001f6063c1a2f447bb4dc90a31d2fd0a745c7fc1Mindy Pereira            mHandler.post(mAddTextWatcher);
4424e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
4430fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        createMoreChip();
4444e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
4454e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
4464e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private void expand() {
4474e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        removeMoreChip();
4484e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        setCursorVisible(true);
4494e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Editable text = getText();
4504e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        setSelection(text != null && text.length() > 0 ? text.length() : 0);
45177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        // If there are any temporary chips, try replacing them now that the user
45277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        // has expanded the field.
45377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        if (mTemporaryRecipients != null && mTemporaryRecipients.size() > 0) {
45477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            new RecipientReplacementTask().execute();
45577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            mTemporaryRecipients = null;
45677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        }
4574e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
4584e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
4591e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    private CharSequence ellipsizeText(CharSequence text, TextPaint paint, float maxWidth) {
460e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira        paint.setTextSize(mChipFontSize);
461c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        if (maxWidth <= 0 && Log.isLoggable(TAG, Log.DEBUG)) {
462c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            Log.d(TAG, "Max width is negative: " + maxWidth);
463c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        }
464c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        return TextUtils.ellipsize(text, paint, maxWidth,
465c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                TextUtils.TruncateAt.END);
4661e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    }
467c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
468e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint, Layout layout) {
4691e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // Ellipsize the text so that it takes AT MOST the entire width of the
4701e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // autocomplete text entry area. Make sure to leave space for padding
4711e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // on the sides.
472e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira        int height = (int) mChipHeight;
4731e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int deleteWidth = height;
474e73c4ce189c70e7f9f4d44b26fce458e3385988aMindy Pereira        float[] widths = new float[1];
475e73c4ce189c70e7f9f4d44b26fce458e3385988aMindy Pereira        paint.getTextWidths(" ", widths);
47600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint,
477e73c4ce189c70e7f9f4d44b26fce458e3385988aMindy Pereira                calculateAvailableWidth(true) - deleteWidth - widths[0]);
4781e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
4791e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // Make sure there is a minimum chip width so the user can ALWAYS
4801e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // tap a chip without difficulty.
4811e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int width = Math.max(deleteWidth * 2, (int) Math.floor(paint.measureText(ellipsizedText, 0,
4821e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                ellipsizedText.length()))
4831e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                + (mChipPadding * 2) + deleteWidth);
484c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
4851e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // Create the background of the chip.
4861e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
4871e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Canvas canvas = new Canvas(tmpBitmap);
4881e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        if (mChipBackgroundPressed != null) {
4891e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipBackgroundPressed.setBounds(0, 0, width, height);
4901e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipBackgroundPressed.draw(canvas);
49177db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira            paint.setColor(sSelectedTextColor);
49297b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira            // Vertically center the text in the chip.
49397b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira            canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding,
49497b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira                    getTextYOffset((String) ellipsizedText, paint, height), paint);
4951e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            // Make the delete a square.
496f9b3dcb83c869528b7679a5b181f936aa8fbecddMindy Pereira            Rect backgroundPadding = new Rect();
497f9b3dcb83c869528b7679a5b181f936aa8fbecddMindy Pereira            mChipBackgroundPressed.getPadding(backgroundPadding);
498f9b3dcb83c869528b7679a5b181f936aa8fbecddMindy Pereira            mChipDelete.setBounds(width - deleteWidth + backgroundPadding.left,
499f9b3dcb83c869528b7679a5b181f936aa8fbecddMindy Pereira                    0 + backgroundPadding.top,
500f9b3dcb83c869528b7679a5b181f936aa8fbecddMindy Pereira                    width - backgroundPadding.right,
501f9b3dcb83c869528b7679a5b181f936aa8fbecddMindy Pereira                    height - backgroundPadding.bottom);
5021e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipDelete.draw(canvas);
5031e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        } else {
5041e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            Log.w(TAG, "Unable to draw a background for the chips as it was never set");
5051e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        }
5061e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        return tmpBitmap;
5071e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    }
508c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
509045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira
510e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private Bitmap createUnselectedChip(RecipientEntry contact, TextPaint paint, Layout layout) {
511c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Ellipsize the text so that it takes AT MOST the entire width of the
512c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // autocomplete text entry area. Make sure to leave space for padding
513c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // on the sides.
514e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira        int height = (int) mChipHeight;
5151e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int iconWidth = height;
516e73c4ce189c70e7f9f4d44b26fce458e3385988aMindy Pereira        float[] widths = new float[1];
517e73c4ce189c70e7f9f4d44b26fce458e3385988aMindy Pereira        paint.getTextWidths(" ", widths);
51800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint,
519e73c4ce189c70e7f9f4d44b26fce458e3385988aMindy Pereira                calculateAvailableWidth(false) - iconWidth - widths[0]);
5201a8bbbf9aa203287f53736a680f96b16d5c878a7Mindy Pereira        // Make sure there is a minimum chip width so the user can ALWAYS
5211a8bbbf9aa203287f53736a680f96b16d5c878a7Mindy Pereira        // tap a chip without difficulty.
5221e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int width = Math.max(iconWidth * 2, (int) Math.floor(paint.measureText(ellipsizedText, 0,
5231e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                ellipsizedText.length()))
5241e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                + (mChipPadding * 2) + iconWidth);
525c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
526c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Create the background of the chip.
527c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
528c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Canvas canvas = new Canvas(tmpBitmap);
529045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        Drawable background = getChipBackground(contact);
530045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        if (background != null) {
531045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            background.setBounds(0, 0, width, height);
532045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            background.draw(canvas);
5331e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
5349024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            // Don't draw photos for recipients that have been typed in.
5351174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            if (contact.getContactId() != RecipientEntry.INVALID_CONTACT) {
5369024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                byte[] photoBytes = contact.getPhotoBytes();
53790081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                // There may not be a photo yet if anything but the first contact address
53890081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                // was selected.
53990081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                if (photoBytes == null && contact.getPhotoThumbnailUri() != null) {
54090081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    // TODO: cache this in the recipient entry?
54190081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    ((BaseRecipientAdapter) getAdapter()).fetchPhoto(contact, contact
54290081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                            .getPhotoThumbnailUri());
54390081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    photoBytes = contact.getPhotoBytes();
54490081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                }
54590081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira
5469024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                Bitmap photo;
5479024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                if (photoBytes != null) {
5489024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                    photo = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length);
5499024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                } else {
5509024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                    // TODO: can the scaled down default photo be cached?
5519024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                    photo = mDefaultContactPhoto;
5529024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                }
5539024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                // Draw the photo on the left side.
554ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                if (photo != null) {
555ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    RectF src = new RectF(0, 0, photo.getWidth(), photo.getHeight());
556ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    Rect backgroundPadding = new Rect();
557ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    mChipBackground.getPadding(backgroundPadding);
558ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    RectF dst = new RectF(width - iconWidth + backgroundPadding.left,
559ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                            0 + backgroundPadding.top,
560ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                            width - backgroundPadding.right,
561ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                            height - backgroundPadding.bottom);
562ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    Matrix matrix = new Matrix();
563ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
564ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                    canvas.drawBitmap(photo, matrix, paint);
565ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira                }
566c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            } else {
5679024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                // Don't leave any space for the icon. It isn't being drawn.
5689024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                iconWidth = 0;
569c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
570e0d95425cbadd7aaff5f8fdbb30ac40cb22c0aa4Mindy Pereira            paint.setColor(getContext().getResources().getColor(android.R.color.black));
57197b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira            // Vertically center the text in the chip.
572379c70a85c105ed2b003f0525fab2914eb79d1d1Mindy Pereira            canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding,
57397b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira                    getTextYOffset((String)ellipsizedText, paint, height), paint);
574c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        } else {
5751e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            Log.w(TAG, "Unable to draw a background for the chips as it was never set");
5761e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        }
5771e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        return tmpBitmap;
5781e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    }
579c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
58000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /**
58100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira     * Get the background drawable for a RecipientChip.
58200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira     */
58300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Visible for testing.
58400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /*package*/ Drawable getChipBackground(RecipientEntry contact) {
58500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        return (mValidator != null && mValidator.isValid(contact.getDestination())) ?
58600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                mChipBackground : mInvalidChipBackground;
58700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    }
58800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
58997b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira    private float getTextYOffset(String text, TextPaint paint, int height) {
59097b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira        Rect bounds = new Rect();
5913e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        paint.getTextBounds(text, 0, text.length(), bounds);
592c791a39d4041f0e7513c93660d6cc94fdf43e6ddMindy Pereira        int textHeight = bounds.bottom - bounds.top ;
593c791a39d4041f0e7513c93660d6cc94fdf43e6ddMindy Pereira        return height - ((height - textHeight) / 2) - (int)paint.descent();
59497b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira    }
59597b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira
59600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private RecipientChip constructChipSpan(RecipientEntry contact, int offset, boolean pressed)
5971e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            throws NullPointerException {
5981e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        if (mChipBackground == null) {
5991e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            throw new NullPointerException(
6001e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                    "Unable to render any chips as setChipDimensions was not called.");
601c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
6021e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Layout layout = getLayout();
603c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
6041e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        TextPaint paint = getPaint();
6051e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        float defaultSize = paint.getTextSize();
60677db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        int defaultColor = paint.getColor();
6071e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
6081e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Bitmap tmpBitmap;
6091e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        if (pressed) {
610e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira            tmpBitmap = createSelectedChip(contact, paint, layout);
611c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
6121e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        } else {
613045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            tmpBitmap = createUnselectedChip(contact, paint, layout);
6141e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        }
615c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
616c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Pass the full text, un-ellipsized, to the chip.
617c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Drawable result = new BitmapDrawable(getResources(), tmpBitmap);
6181e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        result.setBounds(0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight());
61977db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        RecipientChip recipientChip = new RecipientChip(result, contact, offset);
620c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Return text to the original size.
621c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        paint.setTextSize(defaultSize);
62277db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        paint.setColor(defaultColor);
623c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return recipientChip;
6242d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
6252d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
6268684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
627045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Calculate the bottom of the line the chip will be located on using:
628045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * 1) which line the chip appears on
6295519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira     * 2) the height of a chip
6305519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira     * 3) padding built into the edit text view
6318684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
632c6e6141037bf299cabf4a1ba6b3664f5bc426bd0Mindy Pereira    private int calculateOffsetFromBottom(int line) {
6335519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira        // Line offsets start at zero.
634416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira        int actualLine = getLineCount() - (line + 1);
63597b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira        return -((actualLine * ((int) mChipHeight) + getPaddingBottom()) + getPaddingTop())
63697b873c0ba4f31b7e14abad8d873c35e66f7dc41Mindy Pereira                + getDropDownVerticalOffset();
637f566dee91901e44db63df3bf393afb1d43a36f78Mindy Pereira    }
638f566dee91901e44db63df3bf393afb1d43a36f78Mindy Pereira
6398684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
6408684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Get the max amount of space a chip can take up. The formula takes into
6418684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * account the width of the EditTextView, any view padding, and padding
6428684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * that will be added to the chip.
6438684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
644c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private float calculateAvailableWidth(boolean pressed) {
6451e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        return getWidth() - getPaddingLeft() - getPaddingRight() - (mChipPadding * 2);
6462d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
6472d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
648a11c2b0e1ea043aa4d8745285f2dcbe20448b417Mindy Pereira
6493da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereira    private void setChipDimensions(Context context, AttributeSet attrs) {
650407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecipientEditTextView, 0,
651407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira                0);
652a11c2b0e1ea043aa4d8745285f2dcbe20448b417Mindy Pereira        Resources r = getContext().getResources();
6533da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereira        mChipBackground = a.getDrawable(R.styleable.RecipientEditTextView_chipBackground);
6543da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereira        if (mChipBackground == null) {
6553da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereira            mChipBackground = r.getDrawable(R.drawable.chip_background);
6563da5aa0ceb8e870d43fa6747330064d6ddf4775eMindy Pereira        }
657407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mChipBackgroundPressed = a
658407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira                .getDrawable(R.styleable.RecipientEditTextView_chipBackgroundPressed);
659407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mChipBackgroundPressed == null) {
660407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mChipBackgroundPressed = r.getDrawable(R.drawable.chip_background_selected);
661407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
662407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mChipDelete = a.getDrawable(R.styleable.RecipientEditTextView_chipDelete);
663407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mChipDelete == null) {
664407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mChipDelete = r.getDrawable(R.drawable.chip_delete);
665407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
666407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mChipPadding = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipPadding, -1);
667407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mChipPadding == -1) {
668407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mChipPadding = (int) r.getDimension(R.dimen.chip_padding);
669407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
670407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mAlternatesLayout = a.getResourceId(R.styleable.RecipientEditTextView_chipAlternatesLayout,
671407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira                -1);
672407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mAlternatesLayout == -1) {
673407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mAlternatesLayout = R.layout.chips_alternate_item;
674407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
675407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira
676407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mDefaultContactPhoto = BitmapFactory.decodeResource(r, R.drawable.ic_contact_picture);
677407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira
678a11c2b0e1ea043aa4d8745285f2dcbe20448b417Mindy Pereira        mMoreItem = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.more_item, null);
679407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira
680407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mChipHeight = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipHeight, -1);
681407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mChipHeight == -1) {
682407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mChipHeight = r.getDimension(R.dimen.chip_height);
683407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
684407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mChipFontSize = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipFontSize, -1);
685407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mChipFontSize == -1) {
686407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mChipFontSize = r.getDimension(R.dimen.chip_text_size);
687407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
688407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        mInvalidChipBackground = a
689407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira                .getDrawable(R.styleable.RecipientEditTextView_invalidChipBackground);
690407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        if (mInvalidChipBackground == null) {
691407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira            mInvalidChipBackground = r.getDrawable(R.drawable.chip_background_invalid);
692407f669c2d2108e6732c41b53f5d12355c92bb49Mindy Pereira        }
6939c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira        mLineSpacingExtra =  context.getResources().getDimension(R.dimen.line_spacing_extra);
694f1c61197ef28085c8c848cfebd8824231004d581Mindy Pereira        a.recycle();
695a11c2b0e1ea043aa4d8745285f2dcbe20448b417Mindy Pereira    }
696a11c2b0e1ea043aa4d8745285f2dcbe20448b417Mindy Pereira
697a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    // Visible for testing.
698a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    /* package */ void setMoreItem(TextView moreItem) {
699a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira        mMoreItem = moreItem;
700a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    }
701a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira
702ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira
703ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    // Visible for testing.
704ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    /* package */ void setChipBackground(Drawable chipBackground) {
705ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira        mChipBackground = chipBackground;
706ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    }
707ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira
708ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    // Visible for testing.
709ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    /* package */ void setChipHeight(int height) {
710ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira        mChipHeight = height;
711ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    }
712ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira
713bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira    /**
714bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira     * Set whether to shrink the recipients field such that at most
715bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira     * one line of recipients chips are shown when the field loses
716bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira     * focus. By default, the number of displayed recipients will be
717bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira     * limited and a "more" chip will be shown when focus is lost.
718bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira     * @param shrink
719bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira     */
720bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira    public void setOnFocusListShrinkRecipients(boolean shrink) {
721bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira        mShouldShrink = shrink;
722bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira    }
723bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira
724c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
725c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    public void onSizeChanged(int width, int height, int oldw, int oldh) {
726c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        super.onSizeChanged(width, height, oldw, oldh);
72753958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira        if (width != 0 && height != 0) {
72853958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            if (mPendingChipsCount > 0) {
72953958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                postHandlePendingChips();
73053958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            } else {
73153958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                checkChipWidths();
73253958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            }
7337bffb1a8fd8e479207e6ce1ea6c7622a079b8dc4Mindy Pereira        }
73477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        // Try to find the scroll view parent, if it exists.
73500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        if (mScrollView == null && !mTriedGettingScrollView) {
736416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira            ViewParent parent = getParent();
737416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira            while (parent != null && !(parent instanceof ScrollView)) {
738416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira                parent = parent.getParent();
739416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira            }
740416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira            if (parent != null) {
741416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira                mScrollView = (ScrollView) parent;
742416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira            }
74300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            mTriedGettingScrollView = true;
744416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira        }
745c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    }
746c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
747a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira    private void postHandlePendingChips() {
748a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        mHandler.removeCallbacks(mHandlePendingChips);
749a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        mHandler.post(mHandlePendingChips);
750a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira    }
751a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira
75253958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira    private void checkChipWidths() {
75353958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira        // Check the widths of the associated chips.
75453958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira        RecipientChip[] chips = getSortedRecipients();
75553958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira        if (chips != null) {
75653958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            Rect bounds;
75753958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            for (RecipientChip chip : chips) {
75853958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                bounds = chip.getDrawable().getBounds();
75953958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                if (getWidth() > 0 && bounds.right - bounds.left > getWidth()) {
76053958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                    // Need to redraw that chip.
76153958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                    replaceChip(chip, chip.getEntry());
76253958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira                }
76353958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            }
764a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        }
76553958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira    }
76653958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira
7678faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    // Visible for testing.
7688faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    /*package*/ void handlePendingChips() {
7698faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (getViewWidth() <= 0) {
7707bffb1a8fd8e479207e6ce1ea6c7622a079b8dc4Mindy Pereira            // The widget has not been sized yet.
7717bffb1a8fd8e479207e6ce1ea6c7622a079b8dc4Mindy Pereira            // This will be called as a result of onSizeChanged
7727bffb1a8fd8e479207e6ce1ea6c7622a079b8dc4Mindy Pereira            // at a later point.
7737bffb1a8fd8e479207e6ce1ea6c7622a079b8dc4Mindy Pereira            return;
7747bffb1a8fd8e479207e6ce1ea6c7622a079b8dc4Mindy Pereira        }
77553958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira        if (mPendingChipsCount <= 0) {
77653958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira            return;
77753958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira        }
77853958d6d0ba2a0765fd3d272295b81c6d89a68d8Mindy Pereira
779a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira        synchronized (mPendingChips) {
780a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            Editable editable = getText();
781a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            // Tokenize!
7828faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            if (mPendingChipsCount <= MAX_CHIPS_PARSED) {
7838faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                for (int i = 0; i < mPendingChips.size(); i++) {
7848faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    String current = mPendingChips.get(i);
7858faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    int tokenStart = editable.toString().indexOf(current);
7868faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    int tokenEnd = tokenStart + current.length();
7878faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    if (tokenStart >= 0) {
7888faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                        // When we have a valid token, include it with the token
7898faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                        // to the left.
7908faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                        if (tokenEnd < editable.length() - 2
7918faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                                && editable.charAt(tokenEnd) == COMMIT_CHAR_COMMA) {
7928faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                            tokenEnd++;
7938faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                        }
7948faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                        createReplacementChip(tokenStart, tokenEnd, editable);
795a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    }
7968faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    mPendingChipsCount--;
7970fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                }
7988faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                sanitizeEnd();
7998faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            } else {
8008faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                mNoChips = true;
8010fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            }
8028faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
80322b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira            if (mTemporaryRecipients != null && mTemporaryRecipients.size() > 0
804a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    && mTemporaryRecipients.size() <= RecipientAlternatesAdapter.MAX_LOOKUPS) {
805a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                if (hasFocus() || mTemporaryRecipients.size() < CHIP_LIMIT) {
806a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    new RecipientReplacementTask().execute();
807a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    mTemporaryRecipients = null;
808a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                } else {
809a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    // Create the "more" chip
810a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    mIndividualReplacements = new IndividualReplacementTask();
811a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    mIndividualReplacements.execute(new ArrayList<RecipientChip>(
812a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                            mTemporaryRecipients.subList(0, CHIP_LIMIT)));
81377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira
814a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                    createMoreChip();
815a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                }
816a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            } else {
817a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                // There are too many recipients to look up, so just fall back
81822b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira                // to showing addresses for all of them.
819a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira                mTemporaryRecipients = null;
82077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                createMoreChip();
82177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
822a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            mPendingChipsCount = 0;
823a40ef1489593ee115d2734141cacaace585755b4Mindy Pereira            mPendingChips.clear();
8240fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        }
8250fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    }
8260fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira
8278faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    // Visible for testing.
8288faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    /*package*/ int getViewWidth() {
8298faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        return getWidth();
8308faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    }
8318faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
8320fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    /**
8330fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira     * Remove any characters after the last valid chip.
8340fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira     */
83500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Visible for testing.
83600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /*package*/ void sanitizeEnd() {
8372302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira        // Don't sanitize while we are waiting for pending chips to complete.
8382302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira        if (mPendingChipsCount > 0) {
8392302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira            return;
8402302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira        }
8410fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        // Find the last chip; eliminate any commit characters after it.
84200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip[] chips = getSortedRecipients();
8430fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        if (chips != null && chips.length > 0) {
8440fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            int end;
8450fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            ImageSpan lastSpan;
84622b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira            mMoreChip = getMoreChip();
8470fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            if (mMoreChip != null) {
8480fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                lastSpan = mMoreChip;
8490fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            } else {
85000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                lastSpan = getLastChip();
8510fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            }
8520fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            end = getSpannable().getSpanEnd(lastSpan);
8530fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            Editable editable = getText();
8540fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            int length = editable.length();
8550fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            if (length > end) {
8560fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                // See what characters occur after that and eliminate them.
8570fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                if (Log.isLoggable(TAG, Log.DEBUG)) {
8580fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                    Log.d(TAG, "There were extra characters after the last tokenizable entry."
8590fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                            + editable);
8600fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                }
8610fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                editable.delete(end + 1, length);
8620fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            }
8630fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        }
8640fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    }
8650fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira
8660fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    /**
8670fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira     * Create a chip that represents just the email address of a recipient. At some later
8680fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira     * point, this chip will be attached to a real contact entry, if one exists.
8690fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira     */
8700fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    private void createReplacementChip(int tokenStart, int tokenEnd, Editable editable) {
8711e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira        if (alreadyHasChip(tokenStart, tokenEnd)) {
8721e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            // There is already a chip present at this location.
8731e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            // Don't recreate it.
8741e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            return;
8751e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira        }
876b1dd0ac0f11ff3d6352f67311f14aa95185d224bMindy Pereira        String token = editable.toString().substring(tokenStart, tokenEnd);
8771e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira        int commitCharIndex = token.trim().lastIndexOf(COMMIT_CHAR_COMMA);
8780fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        if (commitCharIndex == token.length() - 1) {
8790fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            token = token.substring(0, token.length() - 1);
8800fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        }
8810fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        RecipientEntry entry = createTokenizedEntry(token);
882d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira        if (entry != null) {
88300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            String destText = createAddressText(entry);
884d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            // Always leave a blank space at the end of a chip.
885b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira            int textLength = destText.length() - 1;
886d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            SpannableString chipText = new SpannableString(destText);
887d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            int end = getSelectionEnd();
888d086b83ffa568b592ee6f20445ad3f8d50ab834aMindy Pereira            int start = mTokenizer != null ? mTokenizer.findTokenStart(getText(), end) : 0;
889d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            RecipientChip chip = null;
890d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            try {
8918faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                if (!mNoChips) {
8928faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    chip = constructChipSpan(entry, start, false);
8938faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    chipText.setSpan(chip, 0, textLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8948faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                }
895d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            } catch (NullPointerException e) {
896d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira                Log.e(TAG, e.getMessage(), e);
897d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            }
898d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            editable.replace(tokenStart, tokenEnd, chipText);
899d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            // Add this chip to the list of entries "to replace"
900d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            if (chip != null) {
90122b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira                if (mTemporaryRecipients == null) {
90222b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira                    mTemporaryRecipients = new ArrayList<RecipientChip>();
90322b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira                }
9046f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                chip.setOriginalText(chipText.toString());
905d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira                mTemporaryRecipients.add(chip);
906d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            }
90777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        }
9080fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    }
9090fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira
91082c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor    private static boolean isPhoneNumber(String number) {
91182c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        // TODO: replace this function with libphonenumber's isPossibleNumber (see
91282c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        // PhoneNumberUtil). One complication is that it requires the sender's region which
91382c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        // comes from the CurrentCountryIso. For now, let's just do this simple match.
91482c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        if (TextUtils.isEmpty(number)) {
91582c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor            return false;
91682c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        }
91782c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor
91882c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        Matcher match = Patterns.PHONE.matcher(number);
91982c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        return match.matches();
92082c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor    }
92182c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor
9220fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    private RecipientEntry createTokenizedEntry(String token) {
923d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira        if (TextUtils.isEmpty(token)) {
924d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            return null;
925d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira        }
926577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor        if (isPhoneQuery() && isPhoneNumber(token)) {
92782c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor            return RecipientEntry
92882c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                    .constructFakeEntry(token);
92982c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        }
9300fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(token);
9311174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        String display = null;
9326ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        if (isValid(token) && tokens != null && tokens.length > 0) {
933454eaf31f312fd2f2e6bc3d30bb050d65079a9a0Mindy Pereira            // If we can get a name from tokenizing, then generate an entry from
934454eaf31f312fd2f2e6bc3d30bb050d65079a9a0Mindy Pereira            // this.
9351174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            display = tokens[0].getName();
9361174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            if (!TextUtils.isEmpty(display)) {
9371174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                return RecipientEntry.constructGeneratedEntry(display, token);
9386ccb1744887c175ced43491433b382f7c8f51e83Mindy Pereira            } else {
9396ccb1744887c175ced43491433b382f7c8f51e83Mindy Pereira                display = tokens[0].getAddress();
9406ccb1744887c175ced43491433b382f7c8f51e83Mindy Pereira                if (!TextUtils.isEmpty(display)) {
9416ccb1744887c175ced43491433b382f7c8f51e83Mindy Pereira                    return RecipientEntry.constructFakeEntry(display);
9426ccb1744887c175ced43491433b382f7c8f51e83Mindy Pereira                }
9431174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            }
9441174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        }
9456ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        // Unable to validate the token or to create a valid token from it.
9466ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        // Just create a chip the user can edit.
947a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira        String validatedToken = null;
9486ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        if (mValidator != null && !mValidator.isValid(token)) {
9496ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira            // Try fixing up the entry using the validator.
950a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira            validatedToken = mValidator.fixText(token).toString();
951a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira            if (!TextUtils.isEmpty(validatedToken)) {
952a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                if (validatedToken.contains(token)) {
953a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    // protect against the case of a validator with a null domain,
954a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    // which doesn't add a domain to the token
955a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(validatedToken);
956a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    if (tokenized.length > 0) {
957a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                        validatedToken = tokenized[0].getAddress();
958a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    }
959a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                } else {
960a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    // We ran into a case where the token was invalid and removed
961a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    // by the validator. In this case, just use the original token
962a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    // and let the user sort out the error chip.
963a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                    validatedToken = null;
964490556a764a879cd0eaff358e90705cc1335c92eErik                }
965d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira            }
9666ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        }
967454eaf31f312fd2f2e6bc3d30bb050d65079a9a0Mindy Pereira        // Otherwise, fallback to just creating an editable email address chip.
968a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira        return RecipientEntry
969a9c5969299cf942278391a569bb6fdd1c4a8bb6bMindy Pereira                .constructFakeEntry(!TextUtils.isEmpty(validatedToken) ? validatedToken : token);
9701174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    }
9711174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira
9726ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira    private boolean isValid(String text) {
9736ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira        return mValidator == null ? true : mValidator.isValid(text);
9746ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira    }
9756ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira
9761174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    private String tokenizeAddress(String destination) {
9771174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(destination);
9781174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        if (tokens != null && tokens.length > 0) {
9791174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            return tokens[0].getAddress();
9800fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        }
9811174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        return destination;
9820fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    }
9830fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira
984c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    @Override
985c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public void setTokenizer(Tokenizer tokenizer) {
986c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        mTokenizer = tokenizer;
987c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        super.setTokenizer(mTokenizer);
988c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
989c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
9908684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    @Override
9918684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    public void setValidator(Validator validator) {
9928684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira        mValidator = validator;
9938684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira        super.setValidator(validator);
9948684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    }
9958684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira
9968684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
9978684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * We cannot use the default mechanism for replaceText. Instead,
9988684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * we override onItemClickListener so we can get all the associated
9998684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * contact information including display text, address, and id.
10008684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1001c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
1002c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    protected void replaceText(CharSequence text) {
1003c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return;
1004c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1005c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
10068684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
10078684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Dismiss any selected chips when the back key is pressed.
10088684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1009c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
101095d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
1011441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira        if (keyCode == KeyEvent.KEYCODE_BACK && mSelectedChip != null) {
101295d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira            clearSelectedChip();
1013441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira            return true;
101495d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira        }
101595d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira        return super.onKeyPreIme(keyCode, event);
101695d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira    }
101795d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira
10188684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
10198684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Monitor key presses in this view to see if the user types
10208684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * any commit keys, which consist of ENTER, TAB, or DPAD_CENTER.
10218684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If the user has entered text that has contact matches and types
10228684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * a commit key, create a chip from the topmost matching contact.
10238684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If the user has entered text that has no contact matches and types
10248684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * a commit key, then create a chip from the text they have entered.
10258684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
102695d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira    @Override
1027c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public boolean onKeyUp(int keyCode, KeyEvent event) {
1028c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        switch (keyCode) {
1029c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            case KeyEvent.KEYCODE_ENTER:
1030c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            case KeyEvent.KEYCODE_DPAD_CENTER:
1031c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                if (event.hasNoModifiers()) {
1032d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira                    if (commitDefault()) {
1033c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                        return true;
1034c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    }
1035e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    if (mSelectedChip != null) {
1036e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        clearSelectedChip();
1037e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        return true;
1038e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                    } else if (focusNext()) {
1039e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                        return true;
1040e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    }
1041c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                }
104295d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira                break;
1043e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira            case KeyEvent.KEYCODE_TAB:
1044e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                if (event.hasNoModifiers()) {
1045e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    if (mSelectedChip != null) {
1046e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        clearSelectedChip();
1047e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    } else {
1048e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        commitDefault();
1049e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    }
1050e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    if (focusNext()) {
1051e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        return true;
1052e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    }
1053e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                }
1054441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira                break;
1055c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1056c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return super.onKeyUp(keyCode, event);
1057c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1058c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1059e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira    private boolean focusNext() {
1060e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        View next = focusSearch(View.FOCUS_DOWN);
1061e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        if (next != null) {
1062e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira            next.requestFocus();
1063e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira            return true;
1064e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        }
1065e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        return false;
1066e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira    }
1067e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira
1068045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira    /**
1069045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Create a chip from the default selection. If the popup is showing, the
1070045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * default is the first item in the popup suggestions list. Otherwise, it is
1071045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * whatever the user had typed in. End represents where the the tokenizer
1072045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * should search for a token to turn into a chip.
1073045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * @return If a chip was created from a real contact.
1074045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     */
10758684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    private boolean commitDefault() {
1076f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira        // If there is no tokenizer, don't try to commit.
1077f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira        if (mTokenizer == null) {
1078f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira            return false;
1079f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira        }
10804e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Editable editable = getText();
1081045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        int end = getSelectionEnd();
10824e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int start = mTokenizer.findTokenStart(editable, end);
1083dfa1d391014fac824be4ba48582f160ffcac7303Mindy Pereira
1084e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        if (shouldCreateChip(start, end)) {
1085e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            int whatEnd = mTokenizer.findTokenEnd(getText(), start);
1086e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            // In the middle of chip; treat this as an edit
1087e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            // and commit the whole token.
1088e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            if (whatEnd != getSelectionEnd()) {
1089e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                handleEdit(start, whatEnd);
1090e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                return true;
10914e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            }
1092e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            return commitChip(start, end , editable);
1093e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        }
1094e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        return false;
1095e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    }
1096e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira
1097e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private void commitByCharacter() {
1098f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira        // We can't possibly commit by character if we can't tokenize.
1099f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira        if (mTokenizer == null) {
1100f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira            return;
1101f82e176c81d15a4b33ed4918b6dd76e1e84fd8efMindy Pereira        }
1102e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        Editable editable = getText();
1103e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        int end = getSelectionEnd();
1104e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        int start = mTokenizer.findTokenStart(editable, end);
1105e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        if (shouldCreateChip(start, end)) {
1106e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            commitChip(start, end, editable);
11074e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
1108054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira        setSelection(getText().length());
1109e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    }
11104e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
1111e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private boolean commitChip(int start, int end, Editable editable) {
111249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        ListAdapter adapter = getAdapter();
111349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (adapter != null && adapter.getCount() > 0 && enoughToFilter()
1114577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor                && end == getSelectionEnd() && !isPhoneQuery()) {
1115e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            // choose the first entry.
1116e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            submitItemAtPosition(0);
1117e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            dismissDropDown();
1118e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            return true;
1119e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        } else {
1120e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            int tokenEnd = mTokenizer.findTokenEnd(editable, start);
112149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            if (editable.length() > tokenEnd + 1) {
112249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                char charAt = editable.charAt(tokenEnd + 1);
112349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                if (charAt == COMMIT_CHAR_COMMA || charAt == COMMIT_CHAR_SEMICOLON) {
112449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    tokenEnd++;
112549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                }
1126a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira            }
1127b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira            String text = editable.toString().substring(start, tokenEnd).trim();
1128e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            clearComposingText();
1129e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            if (text != null && text.length() > 0 && !text.equals(" ")) {
11301174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                RecipientEntry entry = createTokenizedEntry(text);
1131d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira                if (entry != null) {
1132d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira                    QwertyKeyListener.markAsReplaced(editable, start, end, "");
1133d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira                    CharSequence chipText = createChip(entry, false);
113471fa24bfb96744cf28d368d58172c14638e14693Mindy Pereira                    if (chipText != null && start > -1 && end > -1) {
11355ac77e6db71c0bc45bcc40cc5cb721333dcb5857Mindy Pereira                        editable.replace(start, end, chipText);
11365ac77e6db71c0bc45bcc40cc5cb721333dcb5857Mindy Pereira                    }
1137d58667fc7a460b8b56ccf9f0c35b92a2a9e0419cMindy Pereira                }
113849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                // Only dismiss the dropdown if it is related to the text we
113949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                // just committed.
114049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                // For paste, it may not be as there are possibly multiple
114149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                // tokens being added.
114249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                if (end == getSelectionEnd()) {
114349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    dismissDropDown();
114449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                }
1145f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira                sanitizeBetween();
11464e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                return true;
1147d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira            }
1148d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        }
11494e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
1150d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    }
1151d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira
11524031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira    // Visible for testing.
11534031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira    /* package */ void sanitizeBetween() {
11542302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira        // Don't sanitize while we are waiting for content to chipify.
11552302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira        if (mPendingChipsCount > 0) {
11562302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira            return;
11572302f672de1b8e7c205412397b0b79514f5426c3Mindy Pereira        }
1158f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira        // Find the last chip.
11594031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira        RecipientChip[] recips = getSortedRecipients();
1160f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira        if (recips != null && recips.length > 0) {
1161f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            RecipientChip last = recips[recips.length - 1];
1162f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            RecipientChip beforeLast = null;
1163f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            if (recips.length > 1) {
1164f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira                beforeLast = recips[recips.length - 2];
1165f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            }
1166f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            int startLooking = 0;
1167f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            int end = getSpannable().getSpanStart(last);
1168f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            if (beforeLast != null) {
1169f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira                startLooking = getSpannable().getSpanEnd(beforeLast);
1170c7afc9841ee6dd3bc5e19ee7269fecd466a8c6e9Mindy Pereira                Editable text = getText();
11714031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira                if (startLooking == -1 || startLooking > text.length() - 1) {
1172c7afc9841ee6dd3bc5e19ee7269fecd466a8c6e9Mindy Pereira                    // There is nothing after this chip.
1173c7afc9841ee6dd3bc5e19ee7269fecd466a8c6e9Mindy Pereira                    return;
1174c7afc9841ee6dd3bc5e19ee7269fecd466a8c6e9Mindy Pereira                }
1175c7afc9841ee6dd3bc5e19ee7269fecd466a8c6e9Mindy Pereira                if (text.charAt(startLooking) == ' ') {
1176f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira                    startLooking++;
1177f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira                }
1178f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            }
1179ec35d9975b61c84cda7081ebfce9e017229fb3d5Mindy Pereira            if (startLooking >= 0 && end >= 0 && startLooking < end) {
1180f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira                getText().delete(startLooking, end);
1181f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira            }
1182f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira        }
1183f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira    }
1184f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira
1185e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private boolean shouldCreateChip(int start, int end) {
11868faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        return !mNoChips && hasFocus() && enoughToFilter() && !alreadyHasChip(start, end);
11871e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira    }
11881e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira
11891e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira    private boolean alreadyHasChip(int start, int end) {
11908faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (mNoChips) {
11918faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            return true;
11928faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        }
11931e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira        RecipientChip[] chips = getSpannable().getSpans(start, end, RecipientChip.class);
11941e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira        if ((chips == null || chips.length == 0)) {
11951e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            return false;
119605522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        }
11971e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira        return true;
1198e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    }
1199e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira
1200e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private void handleEdit(int start, int end) {
120139f40bde5f9af410126c64d29c045fe8bf03c1bbMindy Pereira        if (start == -1 || end == -1) {
120239f40bde5f9af410126c64d29c045fe8bf03c1bbMindy Pereira            // This chip no longer exists in the field.
120339f40bde5f9af410126c64d29c045fe8bf03c1bbMindy Pereira            dismissDropDown();
120439f40bde5f9af410126c64d29c045fe8bf03c1bbMindy Pereira            return;
120539f40bde5f9af410126c64d29c045fe8bf03c1bbMindy Pereira        }
1206e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        // This is in the middle of a chip, so select out the whole chip
1207e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        // and commit it.
1208e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        Editable editable = getText();
1209e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        setSelection(end);
1210e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        String text = getText().toString().substring(start, end);
12113b646f13eb127b26d45ead7f819b943b2b01ce91Mindy Pereira        if (!TextUtils.isEmpty(text)) {
12123b646f13eb127b26d45ead7f819b943b2b01ce91Mindy Pereira            RecipientEntry entry = RecipientEntry.constructFakeEntry(text);
12133b646f13eb127b26d45ead7f819b943b2b01ce91Mindy Pereira            QwertyKeyListener.markAsReplaced(editable, start, end, "");
12143b646f13eb127b26d45ead7f819b943b2b01ce91Mindy Pereira            CharSequence chipText = createChip(entry, false);
12156fe9af5b1cc380243f0fad9354ab7bd41ac2983bMindy Pereira            int selEnd = getSelectionEnd();
12166fe9af5b1cc380243f0fad9354ab7bd41ac2983bMindy Pereira            if (chipText != null && start > -1 && selEnd > -1) {
12176fe9af5b1cc380243f0fad9354ab7bd41ac2983bMindy Pereira                editable.replace(start, selEnd, chipText);
1218a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira            }
12193b646f13eb127b26d45ead7f819b943b2b01ce91Mindy Pereira        }
1220054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira        dismissDropDown();
122105522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira    }
122205522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira
12238684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
12248684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If there is a selected chip, delegate the key events
12258684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * to the selected chip.
12268684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1227c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
1228c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public boolean onKeyDown(int keyCode, KeyEvent event) {
1229b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (mSelectedChip != null && keyCode == KeyEvent.KEYCODE_DEL) {
1230b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            if (mAlternatesPopup != null && mAlternatesPopup.isShowing()) {
1231b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                mAlternatesPopup.dismiss();
1232b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            }
1233b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            removeChip(mSelectedChip);
1234c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1235c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1236c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (keyCode == KeyEvent.KEYCODE_ENTER && event.hasNoModifiers()) {
1237c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            return true;
1238c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1239c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1240c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return super.onKeyDown(keyCode, event);
12412d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
12422d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
12434031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira    // Visible for testing.
12444031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira    /* package */ Spannable getSpannable() {
1245ed58ebd3f54ae5e73129f7f12c8342f177ed1fc3Erik        return getText();
1246c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1247c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1248b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private int getChipStart(RecipientChip chip) {
1249b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return getSpannable().getSpanStart(chip);
1250b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1251b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
1252b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private int getChipEnd(RecipientChip chip) {
1253b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return getSpannable().getSpanEnd(chip);
1254b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1255b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
1256c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /**
1257c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * Instead of filtering on the entire contents of the edit box,
1258c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * this subclass method filters on the range from
1259c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * {@link Tokenizer#findTokenStart} to {@link #getSelectionEnd}
1260c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * if the length of that range meets or exceeds {@link #getThreshold}
1261c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * and makes sure that the range is not already a Chip.
1262c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     */
1263c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
1264c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    protected void performFiltering(CharSequence text, int keyCode) {
126549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (enoughToFilter() && !isCompletedToken(text)) {
1266c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            int end = getSelectionEnd();
1267c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            int start = mTokenizer.findTokenStart(text, end);
1268c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            // If this is a RecipientChip, don't filter
1269c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            // on its contents.
1270c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            Spannable span = getSpannable();
1271c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            RecipientChip[] chips = span.getSpans(start, end, RecipientChip.class);
1272c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            if (chips != null && chips.length > 0) {
1273c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                return;
1274c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
1275c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1276c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        super.performFiltering(text, keyCode);
1277c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1278c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
127949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    // Visible for testing.
128049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    /*package*/ boolean isCompletedToken(CharSequence text) {
128149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (TextUtils.isEmpty(text)) {
128249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            return false;
128349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
128449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        // Check to see if this is a completed token before filtering.
128549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        int end = text.length();
128649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        int start = mTokenizer.findTokenStart(text, end);
128749ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        String token = text.toString().substring(start, end).trim();
128849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (!TextUtils.isEmpty(token)) {
128949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            char atEnd = token.charAt(token.length() - 1);
129049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            return atEnd == COMMIT_CHAR_COMMA || atEnd == COMMIT_CHAR_SEMICOLON;
129149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
129249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        return false;
129349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    }
129449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira
1295c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private void clearSelectedChip() {
1296c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (mSelectedChip != null) {
1297b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            unselectChip(mSelectedChip);
1298c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            mSelectedChip = null;
1299c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
130036d720eb2b7a8d3466d221944a00b8bae7de8e9cMindy Pereira        setCursorVisible(true);
1301c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1302c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
13038684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
13048684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Monitor touch events in the RecipientEditTextView.
13058684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If the view does not have focus, any tap on the view
13068684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * will just focus the view. If the view has focus, determine
13078684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * if the touch target is a recipient chip. If it is and the chip
13088684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * is not selected, select it and clear any other selected chips.
13098684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If it isn't, then select that chip.
13108684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1311c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
1312c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public boolean onTouchEvent(MotionEvent event) {
1313d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        if (!isFocused()) {
1314d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira            // Ignore any chip taps until this view is focused.
1315d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira            return super.onTouchEvent(event);
1316d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        }
1317c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        boolean handled = super.onTouchEvent(event);
1318d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        int action = event.getAction();
1319c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        boolean chipWasSelected = false;
13206caf49d02e7b2a719fc9fdf57e3f8e96dfdf082aMindy Pereira        if (mSelectedChip == null) {
1321b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira            mGestureDetector.onTouchEvent(event);
1322b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        }
13230436c36bd529ea8ee1864a3017f9ac29d2311c08Mindy Pereira        if (mCopyAddress == null && action == MotionEvent.ACTION_UP) {
1324c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            float x = event.getX();
1325c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            float y = event.getY();
1326c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            int offset = putOffsetInRange(getOffsetForPosition(x, y));
1327c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            RecipientChip currentChip = findChip(offset);
1328c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            if (currentChip != null) {
1329c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                if (action == MotionEvent.ACTION_UP) {
1330c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    if (mSelectedChip != null && mSelectedChip != currentChip) {
1331c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                        clearSelectedChip();
1332b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        mSelectedChip = selectChip(currentChip);
1333c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    } else if (mSelectedChip == null) {
13348684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        setSelection(getText().length());
13358684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        commitDefault();
1336b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        mSelectedChip = selectChip(currentChip);
1337c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    } else {
1338b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        onClick(mSelectedChip, offset, x, y);
1339c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    }
1340c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                }
1341c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                chipWasSelected = true;
1342416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira                handled = true;
13435753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira            } else if (mSelectedChip != null
13445753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira                    && mSelectedChip.getContactId() == RecipientEntry.INVALID_CONTACT) {
13455753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira                chipWasSelected = true;
1346c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
1347c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1348c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (action == MotionEvent.ACTION_UP && !chipWasSelected) {
1349c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            clearSelectedChip();
1350c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1351c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return handled;
1352c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1353c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1354416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira    private void scrollLineIntoView(int line) {
1355416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira        if (mScrollView != null) {
1356c6e6141037bf299cabf4a1ba6b3664f5bc426bd0Mindy Pereira            mScrollView.scrollBy(0, calculateOffsetFromBottom(line));
1357416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira        }
1358416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira    }
1359416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira
1360b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private void showAlternates(RecipientChip currentChip, ListPopupWindow alternatesPopup,
1361b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int width, Context context) {
1362b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int line = getLayout().getLineForOffset(getChipStart(currentChip));
13639c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira        int bottom;
13649c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira        if (line == getLineCount() -1) {
13659c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira            bottom = 0;
13669c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira        } else {
13679c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira            bottom = -(int) ((mChipHeight + (2 * mLineSpacingExtra)) * (Math.abs(getLineCount() - 1
13689c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira                    - line)));
13699c5e04038d482571b9373a118c9dfc50e4e18505Mindy Pereira        }
1370b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Align the alternates popup with the left side of the View,
1371b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // regardless of the position of the chip tapped.
1372b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.setWidth(width);
137377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        alternatesPopup.setAnchorView(this);
1374416e1307e57a36650bd15a06bb0ac2915a195a2eMindy Pereira        alternatesPopup.setVerticalOffset(bottom);
1375b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.setAdapter(createAlternatesAdapter(currentChip));
1376368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira        alternatesPopup.setOnItemClickListener(mAlternatesListener);
1377e9278b0f8b883a987a913c5d785455f5d0e646a9Mindy Pereira        // Clear the checked item.
1378e9278b0f8b883a987a913c5d785455f5d0e646a9Mindy Pereira        mCheckedItem = -1;
1379b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.show();
1380b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        ListView listView = alternatesPopup.getListView();
1381b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
1382b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Checked item would be -1 if the adapter has not
1383b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // loaded the view that should be checked yet. The
1384b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // variable will be set correctly when onCheckedItemChanged
1385b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // is called in a separate thread.
1386b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (mCheckedItem != -1) {
1387b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            listView.setItemChecked(mCheckedItem, true);
1388b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mCheckedItem = -1;
1389b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        }
1390b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1391b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
1392b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private ListAdapter createAlternatesAdapter(RecipientChip chip) {
1393b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return new RecipientAlternatesAdapter(getContext(), chip.getContactId(), chip.getDataId(),
1394c3af1324ab95cd3a02557b3ed82102f6be80d6edMindy Pereira                mAlternatesLayout, ((BaseRecipientAdapter)getAdapter()).getQueryType(), this);
1395b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1396b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
13971174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    private ListAdapter createSingleAddressAdapter(RecipientChip currentChip) {
13981174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        return new SingleRecipientArrayAdapter(getContext(), mAlternatesLayout, currentChip
13991174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                .getEntry());
14001174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    }
14011174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira
1402a0751874f5bbdb39f2024c69a5024df60f0344d9Andy Stadler    @Override
1403b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public void onCheckedItemChanged(int position) {
1404b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        ListView listView = mAlternatesPopup.getListView();
1405b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (listView != null && listView.getCheckedItemCount() == 0) {
1406b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            listView.setItemChecked(position, true);
1407b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        }
1408e9278b0f8b883a987a913c5d785455f5d0e646a9Mindy Pereira        mCheckedItem = position;
1409b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1410b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
1411c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    // TODO: This algorithm will need a lot of tweaking after more people have used
1412c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    // the chips ui. This attempts to be "forgiving" to fat finger touches by favoring
1413c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    // what comes before the finger.
1414c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private int putOffsetInRange(int o) {
1415c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int offset = o;
1416c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Editable text = getText();
1417c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int length = text.length();
1418c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Remove whitespace from end to find "real end"
1419c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int realLength = length;
1420c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        for (int i = length - 1; i >= 0; i--) {
1421c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            if (text.charAt(i) == ' ') {
1422c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                realLength--;
1423c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            } else {
1424c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                break;
1425c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
1426c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1427c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
14284fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // If the offset is beyond or at the end of the text,
14294fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // leave it alone.
14304fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        if (offset >= realLength) {
1431c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            return offset;
1432c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
14334fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        Editable editable = getText();
1434c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        while (offset >= 0 && findText(editable, offset) == -1 && findChip(offset) == null) {
1435c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            // Keep walking backward!
1436c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            offset--;
1437c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1438c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return offset;
1439c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1440c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
14414fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    private int findText(Editable text, int offset) {
14424fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        if (text.charAt(offset) != ' ') {
14434fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira            return offset;
14444fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        }
14454fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        return -1;
14464fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    }
14474fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira
1448c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private RecipientChip findChip(int offset) {
1449c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        RecipientChip[] chips = getSpannable().getSpans(0, getText().length(), RecipientChip.class);
1450c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Find the chip that contains this offset.
1451c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        for (int i = 0; i < chips.length; i++) {
1452c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            RecipientChip chip = chips[i];
1453b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int start = getChipStart(chip);
1454b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int end = getChipEnd(chip);
1455b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            if (offset >= start && offset <= end) {
1456c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                return chip;
1457c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
1458c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1459c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return null;
1460c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1461c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
14624031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira    // Visible for testing.
146300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Use this method to generate text to add to the list of addresses.
1464d086b83ffa568b592ee6f20445ad3f8d50ab834aMindy Pereira    /* package */String createAddressText(RecipientEntry entry) {
14658659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira        String display = entry.getDisplayName();
14668659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira        String address = entry.getDestination();
14678659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira        if (TextUtils.isEmpty(display) || TextUtils.equals(display, address)) {
14688659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira            display = null;
14698659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira        }
147082c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        String trimmedDisplayText;
1471577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor        if (isPhoneQuery() && isPhoneNumber(address)) {
147282c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor            trimmedDisplayText = address.trim();
147382c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor        } else {
147482c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor            if (address != null) {
147582c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                // Tokenize out the address in case the address already
147682c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                // contained the username as well.
147782c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(address);
147882c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                if (tokenized != null && tokenized.length > 0) {
147982c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                    address = tokenized[0].getAddress();
148082c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor                }
1481c10739cc694a0248b6187de2d7b9e8d2298bf0d3Mindy Pereira            }
148282c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor            Rfc822Token token = new Rfc822Token(display, address, null);
148382c1c753695a3bfb8928ec1a3e11d9910b5f13faTom Taylor            trimmedDisplayText = token.toString().trim();
1484a590df861d7a42cd2889ee96d0b9e51b47d28755Mindy Pereira        }
14858659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira        int index = trimmedDisplayText.indexOf(",");
1486d086b83ffa568b592ee6f20445ad3f8d50ab834aMindy Pereira        return mTokenizer != null && !TextUtils.isEmpty(trimmedDisplayText)
1487d086b83ffa568b592ee6f20445ad3f8d50ab834aMindy Pereira                && index < trimmedDisplayText.length() - 1 ? (String) mTokenizer
148800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                .terminateToken(trimmedDisplayText) : trimmedDisplayText;
148900be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    }
149000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
149100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Visible for testing.
149200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Use this method to generate text to display in a chip.
149300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /*package*/ String createChipDisplayText(RecipientEntry entry) {
149400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        String display = entry.getDisplayName();
149500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        String address = entry.getDestination();
149600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        if (TextUtils.isEmpty(display) || TextUtils.equals(display, address)) {
149700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            display = null;
149800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        }
1499577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor        if (address != null && !(isPhoneQuery() && isPhoneNumber(address))) {
150000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            // Tokenize out the address in case the address already
150100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            // contained the username as well.
150200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(address);
150300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            if (tokenized != null && tokenized.length > 0) {
150400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                address = tokenized[0].getAddress();
150500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            }
150600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        }
150700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        if (!TextUtils.isEmpty(display)) {
150800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            return display;
150900be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        } else if (!TextUtils.isEmpty(address)){
151000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            return address;
151100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        } else {
151200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            return new Rfc822Token(display, address, null).toString();
151300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        }
15148659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira    }
15158659fc7fa4a245c962f8bc29b351acc56d02dfaaMindy Pereira
1516fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira    private CharSequence createChip(RecipientEntry entry, boolean pressed) {
151700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        String displayText = createAddressText(entry);
1518a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira        if (TextUtils.isEmpty(displayText)) {
1519a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira            return null;
1520a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira        }
15218faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        SpannableString chipText = null;
1522c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Always leave a blank space at the end of a chip.
1523c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int end = getSelectionEnd();
1524c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int start = mTokenizer.findTokenStart(getText(), end);
15258faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int textLength = displayText.length()-1;
15268faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        chipText = new SpannableString(displayText);
15278faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (!mNoChips) {
15288faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            try {
15298faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                RecipientChip chip = constructChipSpan(entry, start, pressed);
15308faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                chipText.setSpan(chip, 0, textLength,
15318faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
15328faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                chip.setOriginalText(chipText.toString());
15338faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            } catch (NullPointerException e) {
15348faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                Log.e(TAG, e.getMessage(), e);
15358faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                return null;
15368faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            }
1537c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1538c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return chipText;
1539c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1540c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
15418684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
15428684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * When an item in the suggestions list has been clicked, create a chip from the
15438684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * contact information of the selected item.
15448684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1545c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
1546c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1547c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        submitItemAtPosition(position);
1548c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1549c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1550c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private void submitItemAtPosition(int position) {
15510fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        RecipientEntry entry = createValidatedEntry(
15520fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                (RecipientEntry)getAdapter().getItem(position));
15531e85502fdc04a44f76ffa9904be9ab6ab80292ceErik        if (entry == null) {
15541e85502fdc04a44f76ffa9904be9ab6ab80292ceErik            return;
15551e85502fdc04a44f76ffa9904be9ab6ab80292ceErik        }
1556c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        clearComposingText();
1557c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1558c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int end = getSelectionEnd();
1559c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int start = mTokenizer.findTokenStart(getText(), end);
1560c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1561c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Editable editable = getText();
1562c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        QwertyKeyListener.markAsReplaced(editable, start, end, "");
15634221a1d7cab69fd886c03abf53f2250025367f5eMindy Pereira        CharSequence chip = createChip(entry, false);
1564920c95167574395b1ff9ecc16e41e13853269bbcMindy Pereira        if (chip != null && start >= 0 && end >= 0) {
15654221a1d7cab69fd886c03abf53f2250025367f5eMindy Pereira            editable.replace(start, end, chip);
15664221a1d7cab69fd886c03abf53f2250025367f5eMindy Pereira        }
1567f6b6b0a26cdebf436dbee3eb4a7118081db7b4ccMindy Pereira        sanitizeBetween();
1568c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1569c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
15700fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    private RecipientEntry createValidatedEntry(RecipientEntry item) {
15710fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        if (item == null) {
15720fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            return null;
15730fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        }
15740fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        final RecipientEntry entry;
15750fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        // If the display name and the address are the same, or if this is a
15760fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        // valid contact, but the destination is invalid, then make this a fake
15770fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        // recipient that is editable.
15780fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        String destination = item.getDestination();
15791e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira        if (RecipientEntry.isCreatedRecipient(item.getContactId())
15801e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira                && (TextUtils.isEmpty(item.getDisplayName())
15811e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira                        || TextUtils.equals(item.getDisplayName(), destination)
15821e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira                        || (mValidator != null && !mValidator.isValid(destination)))) {
15830fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            entry = RecipientEntry.constructFakeEntry(destination);
15840fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        } else {
15850fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            entry = item;
15860fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        }
15870fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        return entry;
15880fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira    }
15890fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira
1590c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /** Returns a collection of contact Id for each chip inside this View. */
1591c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /* package */ Collection<Long> getContactIds() {
1592c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        final Set<Long> result = new HashSet<Long>();
159300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip[] chips = getSortedRecipients();
15947a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira        if (chips != null) {
15957a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira            for (RecipientChip chip : chips) {
15967a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira                result.add(chip.getContactId());
15977a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira            }
1598c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1599c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return result;
1600c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
1601c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
160200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira
160300be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /** Returns a collection of data Id for each chip inside this View. May be null. */
160400be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /* package */ Collection<Long> getDataIds() {
160500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        final Set<Long> result = new HashSet<Long>();
160600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip [] chips = getSortedRecipients();
160700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        if (chips != null) {
160800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            for (RecipientChip chip : chips) {
160900be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                result.add(chip.getDataId());
161000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira            }
161100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        }
161200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        return result;
161383e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira    }
161483e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira
16154031c89f46edea42c64e183e46d49484f1f203c0Mindy Pereira    // Visible for testing.
161600be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /* package */RecipientChip[] getSortedRecipients() {
161700be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira        RecipientChip[] recips = getSpannable()
161800be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                .getSpans(0, getText().length(), RecipientChip.class);
16196f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        ArrayList<RecipientChip> recipientsList = new ArrayList<RecipientChip>(Arrays
162000be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira                .asList(recips));
16216f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        final Spannable spannable = getSpannable();
16226f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        Collections.sort(recipientsList, new Comparator<RecipientChip>() {
16236f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira
16246f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            @Override
16256f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            public int compare(RecipientChip first, RecipientChip second) {
16266f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                int firstStart = spannable.getSpanStart(first);
16276f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                int secondStart = spannable.getSpanStart(second);
16286f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                if (firstStart < secondStart) {
16296f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                    return -1;
16306f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                } else if (firstStart > secondStart) {
16316f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                    return 1;
16326f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                } else {
16336f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                    return 0;
16346f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                }
16356f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            }
16366f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        });
16376f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        return recipientsList.toArray(new RecipientChip[recipientsList.size()]);
16386f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira    }
16396f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira
16404e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
16414e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
16424e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
16434e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
16444e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
16454e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
16464e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public void onDestroyActionMode(ActionMode mode) {
16474e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
16484e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
16494e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
16504e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
16514e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
16524e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
16534e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
16548684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
16558684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * No chips are selectable.
16568684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
16574e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
16584e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
16594e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
16604e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
16614e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
1662a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    // Visible for testing.
1663a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    /* package */ImageSpan getMoreChip() {
166422b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira        MoreImageSpan[] moreSpans = getSpannable().getSpans(0, getText().length(),
166522b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira                MoreImageSpan.class);
166622b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira        return moreSpans != null && moreSpans.length > 0 ? moreSpans[0] : null;
166722b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira    }
166822b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira
16698faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    private MoreImageSpan createMoreSpan(int count) {
16708faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        String moreText = String.format(mMoreItem.getText().toString(), count);
16718faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        TextPaint morePaint = new TextPaint(getPaint());
16728faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        morePaint.setTextSize(mMoreItem.getTextSize());
16738faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        morePaint.setColor(mMoreItem.getCurrentTextColor());
16748faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int width = (int)morePaint.measureText(moreText) + mMoreItem.getPaddingLeft()
16758faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                + mMoreItem.getPaddingRight();
16768faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int height = getLineHeight();
16778faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        Bitmap drawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
16788faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        Canvas canvas = new Canvas(drawable);
16798faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int adjustedHeight = height;
16808faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        Layout layout = getLayout();
16818faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (layout != null) {
16828faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            adjustedHeight -= layout.getLineDescent(0);
16838faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        }
16848faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        canvas.drawText(moreText, 0, moreText.length(), 0, adjustedHeight, morePaint);
16858faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
16868faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        Drawable result = new BitmapDrawable(getResources(), drawable);
16878faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        result.setBounds(0, 0, width, height);
16888faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        return new MoreImageSpan(result);
16898faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    }
16908faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
16918faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    // Visible for testing.
16928faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    /*package*/ void createMoreChipPlainText() {
16938faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        // Take the first <= CHIP_LIMIT addresses and get to the end of the second one.
16948faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        Editable text = getText();
16958faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int start = 0;
16968faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int end = start;
16978faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        for (int i = 0; i < CHIP_LIMIT; i++) {
16988faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            end = movePastTerminators(mTokenizer.findTokenEnd(text, start));
16998faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            start = end; // move to the next token and get its end.
17008faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        }
17018faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        // Now, count total addresses.
17028faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        start = 0;
17038faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int tokenCount = countTokens(text);
17048faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        MoreImageSpan moreSpan = createMoreSpan(tokenCount - CHIP_LIMIT);
17058faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        SpannableString chipText = new SpannableString(text.subSequence(end, text.length()));
17068faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
17078faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        text.replace(end, text.length(), chipText);
17088faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        mMoreChip = moreSpan;
17098faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    }
17108faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
17118faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    // Visible for testing.
17128faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    /* package */int countTokens(Editable text) {
17138faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int tokenCount = 0;
17148faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        int start = 0;
17158faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        while (start < text.length()) {
17168faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            start = movePastTerminators(mTokenizer.findTokenEnd(text, start));
17178faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            tokenCount++;
17188faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            if (start >= text.length()) {
17198faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                break;
17208faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            }
17218faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        }
17228faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        return tokenCount;
17238faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira    }
17248faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
17258684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
1726045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Create the more chip. The more chip is text that replaces any chips that
1727045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * do not fit in the pre-defined available space when the
1728045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * RecipientEditTextView loses focus.
17298684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1730a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    // Visible for testing.
1731a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    /* package */ void createMoreChip() {
17328faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (mNoChips) {
17338faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            createMoreChipPlainText();
17348faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            return;
17358faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        }
17368faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
1737bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira        if (!mShouldShrink) {
1738bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira            return;
1739bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira        }
1740bfa63c7c85405200eb6a049c7209657440a4b1fdMindy Pereira
17416f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        ImageSpan[] tempMore = getSpannable().getSpans(0, getText().length(), MoreImageSpan.class);
17426f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        if (tempMore.length > 0) {
17436f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            getSpannable().removeSpan(tempMore[0]);
17446f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        }
17456f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        RecipientChip[] recipients = getSortedRecipients();
17468faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira
174783e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        if (recipients == null || recipients.length <= CHIP_LIMIT) {
17480fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            mMoreChip = null;
17490fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira            return;
17504e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
17516f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        Spannable spannable = getSpannable();
175283e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        int numRecipients = recipients.length;
17534e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int overage = numRecipients - CHIP_LIMIT;
17548faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        MoreImageSpan moreSpan = createMoreSpan(overage);
1755368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira        mRemovedSpans = new ArrayList<RecipientChip>();
17564e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int totalReplaceStart = 0;
17574e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int totalReplaceEnd = 0;
17586f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        Editable text = getText();
1759368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira        for (int i = numRecipients - overage; i < recipients.length; i++) {
1760368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira            mRemovedSpans.add(recipients[i]);
17619024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            if (i == numRecipients - overage) {
1762368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira                totalReplaceStart = spannable.getSpanStart(recipients[i]);
17639024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            }
1764368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira            if (i == recipients.length - 1) {
1765368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira                totalReplaceEnd = spannable.getSpanEnd(recipients[i]);
17669024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            }
17676f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            if (mTemporaryRecipients == null || !mTemporaryRecipients.contains(recipients[i])) {
17686f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                int spanStart = spannable.getSpanStart(recipients[i]);
17696f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                int spanEnd = spannable.getSpanEnd(recipients[i]);
17706f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                recipients[i].setOriginalText(text.toString().substring(spanStart, spanEnd));
177177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
1772368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira            spannable.removeSpan(recipients[i]);
17734e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
17748faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        if (totalReplaceEnd < text.length()) {
17758faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            totalReplaceEnd = text.length();
17768faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira        }
177777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        int end = Math.max(totalReplaceStart, totalReplaceEnd);
177877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        int start = Math.min(totalReplaceStart, totalReplaceEnd);
177977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        SpannableString chipText = new SpannableString(text.subSequence(start, end));
17804e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
178177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        text.replace(start, end, chipText);
17820fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira        mMoreChip = moreSpan;
17834e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
17844e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
17858684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
17868684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Replace the more chip, if it exists, with all of the recipient chips it had
17878684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * replaced when the RecipientEditTextView gains focus.
17888684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
1789a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    // Visible for testing.
1790a3806ce9e97ef635dc6d4495f9664b690de74960Mindy Pereira    /*package*/ void removeMoreChip() {
17914e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        if (mMoreChip != null) {
17924e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            Spannable span = getSpannable();
17934e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            span.removeSpan(mMoreChip);
17944e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            mMoreChip = null;
17954e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            // Re-add the spans that were removed.
17964e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            if (mRemovedSpans != null && mRemovedSpans.size() > 0) {
17974e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                // Recreate each removed span.
1798c10739cc694a0248b6187de2d7b9e8d2298bf0d3Mindy Pereira                RecipientChip[] recipients = getSortedRecipients();
179964077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                // Start the search for tokens after the last currently visible
180064077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                // chip.
1801c10739cc694a0248b6187de2d7b9e8d2298bf0d3Mindy Pereira                if (recipients == null || recipients.length == 0) {
1802c10739cc694a0248b6187de2d7b9e8d2298bf0d3Mindy Pereira                    return;
1803c10739cc694a0248b6187de2d7b9e8d2298bf0d3Mindy Pereira                }
180464077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                int end = span.getSpanEnd(recipients[recipients.length - 1]);
18054e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                Editable editable = getText();
18064e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                for (RecipientChip chip : mRemovedSpans) {
18076f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                    int chipStart;
18080fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                    int chipEnd;
18090fc929e5fb977fe028d7db04a3ad57e74f42e654Mindy Pereira                    String token;
18106f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                    // Need to find the location of the chip, again.
18116f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                    token = (String) chip.getOriginalText();
181264077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                    // As we find the matching recipient for the remove spans,
181364077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                    // reduce the size of the string we need to search.
181464077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                    // That way, if there are duplicates, we always find the correct
181564077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                    // recipient.
181664077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                    chipStart = editable.toString().indexOf(token, end);
181764077e9710ac8e8df1b7c9828ea985cccb3d8446Mindy Pereira                    end = chipEnd = Math.min(editable.length(), chipStart + token.length());
1818bbf141b605f92729864984229e6e1062d9a462beMindy Pereira                    // Only set the span if we found a matching token.
1819bbf141b605f92729864984229e6e1062d9a462beMindy Pereira                    if (chipStart != -1) {
1820bbf141b605f92729864984229e6e1062d9a462beMindy Pereira                        editable.setSpan(chip, chipStart, chipEnd,
1821bbf141b605f92729864984229e6e1062d9a462beMindy Pereira                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
1822bbf141b605f92729864984229e6e1062d9a462beMindy Pereira                    }
18234e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                }
18244e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                mRemovedSpans.clear();
18254e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            }
18264e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
18274e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
18284e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
1829c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /**
1830b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Show specified chip as selected. If the RecipientChip is just an email address,
1831b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * selecting the chip will take the contents of the chip and place it at
1832b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * the end of the RecipientEditTextView for inline editing. If the
1833b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * RecipientChip is a complete contact, then selecting the chip
1834b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * will change the background color of the chip, show the delete icon,
1835b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * and a popup window with the address in use highlighted and any other
1836b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * alternate addresses for the contact.
1837b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * @param currentChip Chip to select.
1838b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * @return A RecipientChip in the selected state or null if the chip
1839b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * just contained an email address.
1840c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     */
184100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private RecipientChip selectChip(RecipientChip currentChip) {
18421174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        if (currentChip.getContactId() == RecipientEntry.INVALID_CONTACT) {
18431174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            CharSequence text = currentChip.getValue();
18441174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            Editable editable = getText();
18451174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            removeChip(currentChip);
18461174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            editable.append(text);
18471174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            setCursorVisible(true);
18481174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            setSelection(editable.length());
18492f5589283d93933751c20791ef42dc7eab87061aMindy Pereira            return new RecipientChip(null, RecipientEntry.constructFakeEntry((String) text), -1);
18501174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        } else if (currentChip.getContactId() == RecipientEntry.GENERATED_CONTACT) {
1851b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int start = getChipStart(currentChip);
1852b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int end = getChipEnd(currentChip);
1853b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            getSpannable().removeSpan(currentChip);
1854b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            RecipientChip newChip;
1855b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            try {
18568faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                if (mNoChips) {
18578faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    return null;
18588faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                }
1859b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                newChip = constructChipSpan(currentChip.getEntry(), start, true);
1860b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            } catch (NullPointerException e) {
1861b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                Log.e(TAG, e.getMessage(), e);
1862b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                return null;
1863b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            }
1864fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira            Editable editable = getText();
1865b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            QwertyKeyListener.markAsReplaced(editable, start, end, "");
186683e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira            if (start == -1 || end == -1) {
1867b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                Log.d(TAG, "The chip being selected no longer exists but should.");
186883e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira            } else {
18698b308552260807ba6611a4ab3bb23f0211608767Mindy Pereira                editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
187083e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira            }
1871b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            newChip.setSelected(true);
18721174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            if (newChip.getEntry().getContactId() == RecipientEntry.INVALID_CONTACT) {
18731174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip)));
1874c6e6141037bf299cabf4a1ba6b3664f5bc426bd0Mindy Pereira            }
18751174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            showAddress(newChip, mAddressPopup, getWidth(), getContext());
18766ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira            setCursorVisible(false);
1877b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            return newChip;
1878b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        } else {
18791174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            int start = getChipStart(currentChip);
18801174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            int end = getChipEnd(currentChip);
18811174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            getSpannable().removeSpan(currentChip);
18821174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            RecipientChip newChip;
18831174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            try {
18841174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                newChip = constructChipSpan(currentChip.getEntry(), start, true);
18851174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            } catch (NullPointerException e) {
18861174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                Log.e(TAG, e.getMessage(), e);
18871174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                return null;
18881174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            }
1889b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            Editable editable = getText();
18901174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            QwertyKeyListener.markAsReplaced(editable, start, end, "");
18911174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            if (start == -1 || end == -1) {
18921174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                Log.d(TAG, "The chip being selected no longer exists but should.");
18931174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            } else {
18941174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
18951174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            }
18961174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            newChip.setSelected(true);
18971174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            if (newChip.getEntry().getContactId() == RecipientEntry.INVALID_CONTACT) {
18981174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip)));
18991174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            }
19001174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            showAlternates(newChip, mAlternatesPopup, getWidth(), getContext());
19016ebf62290f81d267b85f0237a49daa4c9b3846efMindy Pereira            setCursorVisible(false);
19021174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            return newChip;
1903fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira        }
1904b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1905fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira
1906c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
19071174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    private void showAddress(final RecipientChip currentChip, final ListPopupWindow popup,
19081174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            int width, Context context) {
19091174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        int line = getLayout().getLineForOffset(getChipStart(currentChip));
19101174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        int bottom = calculateOffsetFromBottom(line);
19111174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        // Align the alternates popup with the left side of the View,
19121174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        // regardless of the position of the chip tapped.
19131174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        popup.setWidth(width);
19141174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        popup.setAnchorView(this);
19151174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        popup.setVerticalOffset(bottom);
19161174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        popup.setAdapter(createSingleAddressAdapter(currentChip));
19171174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        popup.setOnItemClickListener(new OnItemClickListener() {
19181174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            @Override
19191174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
19201174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                unselectChip(currentChip);
19211174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                popup.dismiss();
19221174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            }
19231174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        });
19241174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        popup.show();
19251174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        ListView listView = popup.getListView();
19261174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
19271174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira        listView.setItemChecked(0, true);
19281174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira    }
19291174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira
1930b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1931b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Remove selection from this chip. Unselecting a RecipientChip will render
19328faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira     * the chip without a delete icon and with an unfocused background. This is
19338faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira     * called when the RecipientChip no longer has focus.
1934b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
193500be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    private void unselectChip(RecipientChip chip) {
1936b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int start = getChipStart(chip);
1937b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int end = getChipEnd(chip);
1938b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Editable editable = getText();
1939c57ee19c891696a126fe8b6dec74aa4f6a00c6a5Mindy Pereira        mSelectedChip = null;
1940b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (start == -1 || end == -1) {
19418faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira            Log.w(TAG, "The chip doesn't exist or may be a chip a user was editing");
19425753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira            setSelection(editable.length());
19435753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira            commitDefault();
1944b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        } else {
1945c57ee19c891696a126fe8b6dec74aa4f6a00c6a5Mindy Pereira            getSpannable().removeSpan(chip);
1946b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            QwertyKeyListener.markAsReplaced(editable, start, end, "");
19478b308552260807ba6611a4ab3bb23f0211608767Mindy Pereira            editable.removeSpan(chip);
19488b308552260807ba6611a4ab3bb23f0211608767Mindy Pereira            try {
19498faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                if (!mNoChips) {
19508faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    editable.setSpan(constructChipSpan(chip.getEntry(), start, false), start, end,
19518faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
19528faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                }
19538b308552260807ba6611a4ab3bb23f0211608767Mindy Pereira            } catch (NullPointerException e) {
19548b308552260807ba6611a4ab3bb23f0211608767Mindy Pereira                Log.e(TAG, e.getMessage(), e);
19558b308552260807ba6611a4ab3bb23f0211608767Mindy Pereira            }
1956c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1957b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        setCursorVisible(true);
1958b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        setSelection(editable.length());
1959b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (mAlternatesPopup != null && mAlternatesPopup.isShowing()) {
1960b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mAlternatesPopup.dismiss();
1961c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1962b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1963c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1964b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1965b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Return whether a touch event was inside the delete target of
1966b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * a selected chip. It is in the delete target if:
1967b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * 1) the x and y points of the event are within the
1968b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * delete assset.
1969b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * 2) the point tapped would have caused a cursor to appear
1970b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * right after the selected chip.
1971b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * @return boolean
1972b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1973b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private boolean isInDelete(RecipientChip chip, int offset, float x, float y) {
1974b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Figure out the bounds of this chip and whether or not
1975b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // the user clicked in the X portion.
1976b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return chip.isSelected() && offset == getChipEnd(chip);
1977b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
19783656f7e97c58dc8443132d2d8297629b6a04cce7Mindy Pereira
1979b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1980b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Remove the chip and any text associated with it from the RecipientEditTextView.
1981b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1982ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    // Visible for testing.
1983ae7e41106f289f2668c54fff6c457c6fc91ab4a9Mindy Pereira    /*pacakge*/ void removeChip(RecipientChip chip) {
1984b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Spannable spannable = getSpannable();
1985b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int spanStart = spannable.getSpanStart(chip);
1986b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int spanEnd = spannable.getSpanEnd(chip);
1987b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Editable text = getText();
1988b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira        int toDelete = spanEnd;
1989b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        boolean wasSelected = chip == mSelectedChip;
1990b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Clear that there is a selected chip before updating any text.
1991b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
1992b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mSelectedChip = null;
1993c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1994b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira        // Always remove trailing spaces when removing a chip.
1995b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira        while (toDelete >= 0 && toDelete < text.length() && text.charAt(toDelete) == ' ') {
1996b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira            toDelete++;
1997b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira        }
1998b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        spannable.removeSpan(chip);
1999a0f2aab8f22b6f334938579cd6f336cfb338a554Mindy Pereira        if (spanStart >= 0 && toDelete > 0) {
2000a0f2aab8f22b6f334938579cd6f336cfb338a554Mindy Pereira            text.delete(spanStart, toDelete);
2001a0f2aab8f22b6f334938579cd6f336cfb338a554Mindy Pereira        }
2002b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
2003b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            clearSelectedChip();
2004c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
2005b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
2006c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
2007b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
2008b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Replace this currently selected chip with a new chip
2009b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * that uses the contact data provided.
2010b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
201100be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    // Visible for testing.
201200be4ce28569ae7d1871a2f95453b0e6a6a47852Mindy Pereira    /*package*/ void replaceChip(RecipientChip chip, RecipientEntry entry) {
2013b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        boolean wasSelected = chip == mSelectedChip;
2014b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
2015b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mSelectedChip = null;
2016c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
2017b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int start = getChipStart(chip);
2018b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int end = getChipEnd(chip);
2019b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        getSpannable().removeSpan(chip);
2020b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Editable editable = getText();
2021b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        CharSequence chipText = createChip(entry, false);
2022a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira        if (chipText != null) {
2023a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira            if (start == -1 || end == -1) {
2024a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                Log.e(TAG, "The chip to replace does not exist but should.");
2025a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                editable.insert(0, chipText);
2026a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira            } else {
2027a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                if (!TextUtils.isEmpty(chipText)) {
2028a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    // There may be a space to replace with this chip's new
2029a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    // associated
2030a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    // space. Check for it
2031a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    int toReplace = end;
2032a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    while (toReplace >= 0 && toReplace < editable.length()
2033a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                            && editable.charAt(toReplace) == ' ') {
2034a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                        toReplace++;
2035a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    }
2036a1ecf3cc31e72b56d51690ae6524e8a26e9dd358Mindy Pereira                    editable.replace(start, toReplace, chipText);
2037b3e42aaf97e476719cede047394718fd001fd79bMindy Pereira                }
2038b1dd0ac0f11ff3d6352f67311f14aa95185d224bMindy Pereira            }
2039c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
2040b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        setCursorVisible(true);
2041b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
2042b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            clearSelectedChip();
2043c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
2044b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
2045c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
2046b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
2047b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Handle click events for a chip. When a selected chip receives a click
2048b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * event, see if that event was in the delete icon. If so, delete it.
2049b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Otherwise, unselect the chip.
2050b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
2051b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public void onClick(RecipientChip chip, int offset, float x, float y) {
2052b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (chip.isSelected()) {
2053b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            if (isInDelete(chip, offset, x, y)) {
2054b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                removeChip(chip);
2055b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            } else {
2056b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                clearSelectedChip();
2057c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
2058c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
2059b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
2060c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
2061368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira    private boolean chipsPending() {
2062368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira        return mPendingChipsCount > 0 || (mRemovedSpans != null && mRemovedSpans.size() > 0);
2063368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira    }
2064368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira
2065311576adc68a068ab583d794e064329b0f71b8d1Mindy Pereira    @Override
2066311576adc68a068ab583d794e064329b0f71b8d1Mindy Pereira    public void removeTextChangedListener(TextWatcher watcher) {
2067311576adc68a068ab583d794e064329b0f71b8d1Mindy Pereira        mTextWatcher = null;
2068311576adc68a068ab583d794e064329b0f71b8d1Mindy Pereira        super.removeTextChangedListener(watcher);
2069311576adc68a068ab583d794e064329b0f71b8d1Mindy Pereira    }
2070311576adc68a068ab583d794e064329b0f71b8d1Mindy Pereira
2071e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    private class RecipientTextWatcher implements TextWatcher {
2072eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira
2073e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        @Override
2074e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        public void afterTextChanged(Editable s) {
20751e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            // If the text has been set to null or empty, make sure we remove
20761e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            // all the spans we applied.
20771e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            if (TextUtils.isEmpty(s)) {
20781e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                // Remove all the chips spans.
20791e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                Spannable spannable = getSpannable();
20801e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                RecipientChip[] chips = spannable.getSpans(0, getText().length(),
20811e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                        RecipientChip.class);
20821e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                for (RecipientChip chip : chips) {
20831e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                    spannable.removeSpan(chip);
20841e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                }
20851e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                if (mMoreChip != null) {
20861e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                    spannable.removeSpan(mMoreChip);
20871e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                }
20881e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira                return;
20891e70f1a4c71895213a1ea65a1e68ab9cc0b12ad2Mindy Pereira            }
20901174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            // Get whether there are any recipients pending addition to the
20911174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira            // view. If there are, don't do anything in the text watcher.
2092368f569ffda5dc5fb85ee1eebc023759a46e31c8Mindy Pereira            if (chipsPending()) {
2093e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                return;
2094e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            }
20955753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira            // If the user is editing a chip, don't clear it.
20965753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira            if (mSelectedChip != null
20975753143e03a6b8a61b2ffd6cbb04f1d54f406956Mindy Pereira                    && mSelectedChip.getContactId() != RecipientEntry.INVALID_CONTACT) {
2098e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                setCursorVisible(true);
2099e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                setSelection(getText().length());
2100e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                clearSelectedChip();
2101e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            }
2102e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            int length = s.length();
2103e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            // Make sure there is content there to parse and that it is
2104054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira            // not just the commit character.
2105e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            if (length > 1) {
2106054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                char last;
210776ebe80e9fc58b31452d1a0724dd88d420a5b580Mindy Pereira                int end = getSelectionEnd() == 0 ? 0 : getSelectionEnd() - 1;
2108054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                int len = length() - 1;
2109054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                if (end != len) {
2110054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                    last = s.charAt(end);
2111054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                } else {
2112054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                    last = s.charAt(len);
2113054b3caa7d695ff5bd6cebfb79067e731f334934Mindy Pereira                }
2114e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                if (last == COMMIT_CHAR_SEMICOLON || last == COMMIT_CHAR_COMMA) {
2115e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                    commitByCharacter();
2116e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                } else if (last == COMMIT_CHAR_SPACE) {
2117ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                    if (!isPhoneQuery()) {
2118ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        // Check if this is a valid email address. If it is,
2119ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        // commit it.
2120ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        String text = getText().toString();
2121ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        int tokenStart = mTokenizer.findTokenStart(text, getSelectionEnd());
2122ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        String sub = text.substring(tokenStart, mTokenizer.findTokenEnd(text,
2123ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                                tokenStart));
2124ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        if (!TextUtils.isEmpty(sub) && mValidator != null &&
2125ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                                mValidator.isValid(sub)) {
2126ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                            commitByCharacter();
2127ab2c50697fd2ff580c308984b8f313536ff83b30Tom Taylor                        }
2128e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                    }
2129e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira                }
2130e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira            }
2131e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        }
2132e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira
2133e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        @Override
2134e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        public void onTextChanged(CharSequence s, int start, int before, int count) {
2135eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira            // This is a delete; check to see if the insertion point is on a space
2136eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira            // following a chip.
2137eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira            if (before > count) {
2138eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                // If the item deleted is a space, and the thing before the
2139eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                // space is a chip, delete the entire span.
2140eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                int selStart = getSelectionStart();
2141eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                RecipientChip[] repl = getSpannable().getSpans(selStart, selStart,
2142eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                        RecipientChip.class);
2143eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                if (repl.length > 0) {
2144eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                    // There is a chip there! Just remove it.
2145eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                    Editable editable = getText();
2146eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                    // Add the separator token.
2147eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                    int tokenStart = mTokenizer.findTokenStart(editable, selStart);
2148eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                    int tokenEnd = mTokenizer.findTokenEnd(editable, tokenStart);
2149a7ae9beadd2ae677d028edbadf55b02220cb3fb6Mindy Pereira                    tokenEnd = tokenEnd + 1;
2150a7ae9beadd2ae677d028edbadf55b02220cb3fb6Mindy Pereira                    if (tokenEnd > editable.length()) {
2151a7ae9beadd2ae677d028edbadf55b02220cb3fb6Mindy Pereira                        tokenEnd = editable.length();
2152a7ae9beadd2ae677d028edbadf55b02220cb3fb6Mindy Pereira                    }
2153a7ae9beadd2ae677d028edbadf55b02220cb3fb6Mindy Pereira                    editable.delete(tokenStart, tokenEnd);
2154eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                    getSpannable().removeSpan(repl[0]);
2155eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira                }
2156a9721f38340e84806b8ffae28b858b689892f7c0Mindy Pereira            } else if (count > before) {
2157291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira                scrollBottomIntoView();
2158eb83a7dfc716851d1dc0cc160191cdda8f064e35Mindy Pereira            }
2159e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        }
2160e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira
2161e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        @Override
2162e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
216322b680b63446e2b50f0f5e7d5307c7198387cebfMindy Pereira            // Do nothing.
2164e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira        }
2165e0bf819fff34b7678dd336e4fd010f04bb343eeaMindy Pereira    }
216677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira
2167291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira    private void scrollBottomIntoView() {
2168291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira        if (mScrollView != null) {
2169291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira            mScrollView.scrollBy(0, (int)(getLineCount() * mChipHeight));
2170291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira        }
2171291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira    }
2172291e619b2b2ced9abe53fc97613f5707319325bbMindy Pereira
21733e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    /**
21743e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     * Handles pasting a {@link ClipData} to this {@link RecipientEditTextView}.
21753e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     */
21763e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    private void handlePasteClip(ClipData clip) {
21773e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        removeTextChangedListener(mTextWatcher);
21783e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
21793e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        if (clip != null && clip.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
21803e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            for (int i = 0; i < clip.getItemCount(); i++) {
21813e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                CharSequence paste = clip.getItemAt(i).getText();
21823e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                if (paste != null) {
21833e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    int start = getSelectionStart();
21843e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    int end = getSelectionEnd();
21853e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    Editable editable = getText();
21863e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    if (start >= 0 && end >= 0 && start != end) {
21873e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                        editable.append(paste, start, end);
21883e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    } else {
21893e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                        editable.insert(end, paste);
21903e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    }
21913e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                    handlePasteAndReplace();
21923e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                }
21933e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            }
21943e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        }
21953e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
21963e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        mHandler.post(mAddTextWatcher);
21973e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    }
21983e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
219949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    @Override
220049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    public boolean onTextContextMenuItem(int id) {
220149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (id == android.R.id.paste) {
220249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
220349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    Context.CLIPBOARD_SERVICE);
22043e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            handlePasteClip(clipboard.getPrimaryClip());
220549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            return true;
220649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
220749ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        return super.onTextContextMenuItem(id);
220849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    }
220949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira
22101e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira    private void handlePasteAndReplace() {
22111e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira        ArrayList<RecipientChip> created = handlePaste();
22121e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira        if (created != null && created.size() > 0) {
22131e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira            // Perform reverse lookups on the pasted contacts.
22141e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira            IndividualReplacementTask replace = new IndividualReplacementTask();
22151e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira            replace.execute(created);
22161e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira        }
22171e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira    }
22181e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira
221949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    // Visible for testing.
22201e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira    /* package */ArrayList<RecipientChip> handlePaste() {
222149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        String text = getText().toString();
222249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        int originalTokenStart = mTokenizer.findTokenStart(text, getSelectionEnd());
222349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        String lastAddress = text.substring(originalTokenStart);
222449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        int tokenStart = originalTokenStart;
222549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        int prevTokenStart = tokenStart;
222649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        RecipientChip findChip = null;
22271e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira        ArrayList<RecipientChip> created = new ArrayList<RecipientChip>();
222849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (tokenStart != 0) {
222949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            // There are things before this!
223049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            while (tokenStart != 0 && findChip == null) {
223149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                prevTokenStart = tokenStart;
223249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                tokenStart = mTokenizer.findTokenStart(text, tokenStart);
223349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                findChip = findChip(tokenStart);
223449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            }
223549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            if (tokenStart != originalTokenStart) {
223649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                if (findChip != null) {
223749ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    tokenStart = prevTokenStart;
223849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                }
223949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                int tokenEnd;
224049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                RecipientChip createdChip;
224149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                while (tokenStart < originalTokenStart) {
224249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    tokenEnd = movePastTerminators(mTokenizer.findTokenEnd(text, tokenStart));
224349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    commitChip(tokenStart, tokenEnd, getText());
224449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    createdChip = findChip(tokenStart);
224587a5af3ed31a18c66aa62154e66e3d841dc02a27Mindy Pereira                    if (createdChip == null) {
224687a5af3ed31a18c66aa62154e66e3d841dc02a27Mindy Pereira                        break;
224787a5af3ed31a18c66aa62154e66e3d841dc02a27Mindy Pereira                    }
224849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    // +1 for the space at the end.
224949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                    tokenStart = getSpannable().getSpanEnd(createdChip) + 1;
22501e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira                    created.add(createdChip);
225149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira                }
225249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            }
225349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
225449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        // Take a look at the last token. If the token has been completed with a
225549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        // commit character, create a chip.
225649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (isCompletedToken(lastAddress)) {
225749ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            Editable editable = getText();
22581e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira            tokenStart = editable.toString().indexOf(lastAddress, originalTokenStart);
22591e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira            commitChip(tokenStart, editable.length(), editable);
22601e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira            created.add(findChip(tokenStart));
226149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
22621e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira        return created;
226349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    }
226449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira
226549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    // Visible for testing.
226649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    /* package */int movePastTerminators(int tokenEnd) {
226749ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (tokenEnd >= length()) {
226849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            return tokenEnd;
226949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
227049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        char atEnd = getText().toString().charAt(tokenEnd);
227149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (atEnd == COMMIT_CHAR_COMMA || atEnd == COMMIT_CHAR_SEMICOLON) {
227249ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            tokenEnd++;
227349ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
227449ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        // This token had not only an end token character, but also a space
227549ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        // separating it from the next token.
227649ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        if (tokenEnd < length() && getText().toString().charAt(tokenEnd) == ' ') {
227749ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira            tokenEnd++;
227849ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        }
227949ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira        return tokenEnd;
228049ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira    }
228149ce9866ae314b3513e17008f107ceded23f9cf0Mindy Pereira
228277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira    private class RecipientReplacementTask extends AsyncTask<Void, Void, Void> {
228377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        private RecipientChip createFreeChip(RecipientEntry entry) {
228477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            try {
22858faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                if (mNoChips) {
22868faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                    return null;
22878faa56ff909edb20154219f8d2ba6feecf1b5238Mindy Pereira                }
228877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                return constructChipSpan(entry, -1, false);
228977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            } catch (NullPointerException e) {
229077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                Log.e(TAG, e.getMessage(), e);
229177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                return null;
229277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
229377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        }
229477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira
229577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        @Override
229677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        protected Void doInBackground(Void... params) {
229777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            if (mIndividualReplacements != null) {
229877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                mIndividualReplacements.cancel(true);
229977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
230077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            // For each chip in the list, look up the matching contact.
230177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            // If there is a match, replace that chip with the matching
230277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            // chip.
230377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            final ArrayList<RecipientChip> originalRecipients = new ArrayList<RecipientChip>();
23046f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            RecipientChip[] existingChips = getSortedRecipients();
230577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            for (int i = 0; i < existingChips.length; i++) {
230677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                originalRecipients.add(existingChips[i]);
230777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
230877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            if (mRemovedSpans != null) {
230977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                originalRecipients.addAll(mRemovedSpans);
231077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
2311441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira            ArrayList<String> addresses = new ArrayList<String>();
2312039c9266e72538fd40d382de56807408385fc3daMindy Pereira            RecipientChip chip;
231377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            for (int i = 0; i < originalRecipients.size(); i++) {
2314039c9266e72538fd40d382de56807408385fc3daMindy Pereira                chip = originalRecipients.get(i);
2315039c9266e72538fd40d382de56807408385fc3daMindy Pereira                if (chip != null) {
2316441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira                    addresses.add(createAddressText(chip.getEntry()));
2317039c9266e72538fd40d382de56807408385fc3daMindy Pereira                }
231877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
231977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            HashMap<String, RecipientEntry> entries = RecipientAlternatesAdapter
232077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    .getMatchingRecipients(getContext(), addresses);
232177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            final ArrayList<RecipientChip> replacements = new ArrayList<RecipientChip>();
232277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            for (final RecipientChip temp : originalRecipients) {
232377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                RecipientEntry entry = null;
23241174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                if (RecipientEntry.isCreatedRecipient(temp.getEntry().getContactId())
232577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        && getSpannable().getSpanStart(temp) != -1) {
232677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    // Replace this.
23271174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                    entry = createValidatedEntry(entries.get(tokenizeAddress(temp.getEntry()
23281174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                            .getDestination())));
232977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                }
233077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                if (entry != null) {
233177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    replacements.add(createFreeChip(entry));
233277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                } else {
233377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    replacements.add(temp);
233477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                }
233577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
233677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            if (replacements != null && replacements.size() > 0) {
233777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                mHandler.post(new Runnable() {
233877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    @Override
233977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    public void run() {
234077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        SpannableStringBuilder text = new SpannableStringBuilder(getText()
234177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                                .toString());
234277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        Editable oldText = getText();
234377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        int start, end;
234477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        int i = 0;
234577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        for (RecipientChip chip : originalRecipients) {
234677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            start = oldText.getSpanStart(chip);
234777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            if (start != -1) {
2348b1dd0ac0f11ff3d6352f67311f14aa95185d224bMindy Pereira                                end = oldText.getSpanEnd(chip);
23492e157c2b6e02a8648a87aa33163ecb89a641c2b0Mindy Pereira                                oldText.removeSpan(chip);
2350b1dd0ac0f11ff3d6352f67311f14aa95185d224bMindy Pereira                                // Leave a spot for the space!
23516f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                                RecipientChip replacement = replacements.get(i);
23526f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                                text.setSpan(replacement, start, end,
2353b1dd0ac0f11ff3d6352f67311f14aa95185d224bMindy Pereira                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
23546f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira                                replacement.setOriginalText(text.toString().substring(start, end));
235577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            }
235677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            i++;
235777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        }
235877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        originalRecipients.clear();
23592e157c2b6e02a8648a87aa33163ecb89a641c2b0Mindy Pereira                        setText(text);
236077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    }
236177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                });
236277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
236377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            return null;
236477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        }
236577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira    }
236677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira
236777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira    private class IndividualReplacementTask extends AsyncTask<Object, Void, Void> {
236877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        @SuppressWarnings("unchecked")
236977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        @Override
237077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        protected Void doInBackground(Object... params) {
237177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            // For each chip in the list, look up the matching contact.
237277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            // If there is a match, replace that chip with the matching
237377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            // chip.
237477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            final ArrayList<RecipientChip> originalRecipients =
237577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                (ArrayList<RecipientChip>) params[0];
2376441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira            ArrayList<String> addresses = new ArrayList<String>();
2377039c9266e72538fd40d382de56807408385fc3daMindy Pereira            RecipientChip chip;
237877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            for (int i = 0; i < originalRecipients.size(); i++) {
2379039c9266e72538fd40d382de56807408385fc3daMindy Pereira                chip = originalRecipients.get(i);
2380039c9266e72538fd40d382de56807408385fc3daMindy Pereira                if (chip != null) {
2381441b417f05bf807429281e6ad7cd43c9bdba87a6Mindy Pereira                    addresses.add(createAddressText(chip.getEntry()));
2382039c9266e72538fd40d382de56807408385fc3daMindy Pereira                }
238377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
238477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            HashMap<String, RecipientEntry> entries = RecipientAlternatesAdapter
238577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    .getMatchingRecipients(getContext(), addresses);
238677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            for (final RecipientChip temp : originalRecipients) {
23871174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                if (RecipientEntry.isCreatedRecipient(temp.getEntry().getContactId())
238877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        && getSpannable().getSpanStart(temp) != -1) {
238977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    // Replace this.
23901174eca98d1dbd263a9e72c5a9e35a1039aae979Mindy Pereira                    final RecipientEntry entry = createValidatedEntry(entries
23911e12ddb8232423b09b8df6c59f2e65189a0eea0aMindy Pereira                            .get(tokenizeAddress(temp.getEntry().getDestination()).toLowerCase()));
239277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    if (entry != null) {
239377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        mHandler.post(new Runnable() {
239477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            @Override
239577056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            public void run() {
239677056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                                replaceChip(temp, entry);
239777056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                            }
239877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                        });
239977056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                    }
240077056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira                }
240177056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            }
240277056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira            return null;
240377056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira        }
240477056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira    }
2405b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
24066f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira
24076f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira    /**
24086f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira     * MoreImageSpan is a simple class created for tracking the existence of a
24096f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira     * more chip across activity restarts/
24106f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira     */
24116f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira    private class MoreImageSpan extends ImageSpan {
24126f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        public MoreImageSpan(Drawable b) {
24136f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira            super(b);
24146f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira        }
24156f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira    }
24166f9f2858818228eaa1f2a2c562f4d2da6a4216b3Mindy Pereira
2417b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2418b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public boolean onDown(MotionEvent e) {
2419b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        return false;
2420b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2421b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2422b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2423b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
2424b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        // Do nothing.
2425b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        return false;
2426b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2427b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2428b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2429b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public void onLongPress(MotionEvent event) {
2430b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        if (mSelectedChip != null) {
2431b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira            return;
2432b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        }
2433b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        float x = event.getX();
2434b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        float y = event.getY();
2435b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        int offset = putOffsetInRange(getOffsetForPosition(x, y));
2436b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        RecipientChip currentChip = findChip(offset);
2437b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        if (currentChip != null) {
24383e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            if (mDragEnabled) {
24393e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                // Start drag-and-drop for the selected chip.
24403e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                startDrag(currentChip);
24413e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            } else {
24423e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                // Copy the selected chip email address.
24433e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                showCopyDialog(currentChip.getEntry().getDestination());
24443e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            }
24453e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        }
24463e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    }
24473e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24483e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    /**
24493e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     * Enables drag-and-drop for chips.
24503e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     */
24513e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    public void enableDrag() {
24523e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        mDragEnabled = true;
24533e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    }
24543e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24553e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    /**
24563e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     * Starts drag-and-drop for the selected chip.
24573e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     */
24583e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    private void startDrag(RecipientChip currentChip) {
24593e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        String address = currentChip.getEntry().getDestination();
24603e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        ClipData data = ClipData.newPlainText(address, address + COMMIT_CHAR_COMMA);
24613e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24623e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        // Start drag mode.
24633e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        startDrag(data, new RecipientChipShadow(currentChip), null, 0);
24643e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24653e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        // Remove the current chip, so drag-and-drop will result in a move.
24663e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        // TODO (phamm): consider readd this chip if it's dropped outside a target.
24673e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        removeChip(currentChip);
24683e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    }
24693e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24703e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    /**
24713e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     * Handles drag event.
24723e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     */
24733e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    @Override
24743e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    public boolean onDragEvent(DragEvent event) {
24753e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        switch (event.getAction()) {
24763e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            case DragEvent.ACTION_DRAG_STARTED:
24773e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                // Only handle plain text drag and drop.
24783e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                return event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
24793e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            case DragEvent.ACTION_DRAG_ENTERED:
24803e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                requestFocus();
24813e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                return true;
24823e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            case DragEvent.ACTION_DROP:
24833e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                handlePasteClip(event.getClipData());
24843e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham                return true;
24853e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        }
24863e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        return false;
24873e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    }
24883e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24893e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    /**
24903e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     * Drag shadow for a {@link RecipientChip}.
24913e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham     */
24923e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham    private final class RecipientChipShadow extends DragShadowBuilder {
24933e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        private final RecipientChip mChip;
24943e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24953e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        public RecipientChipShadow(RecipientChip chip) {
24963e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            mChip = chip;
24973e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        }
24983e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
24993e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        @Override
25003e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
25013e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            Rect rect = mChip.getDrawable().getBounds();
25023e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            shadowSize.set(rect.width(), rect.height());
25033e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            shadowTouchPoint.set(rect.centerX(), rect.centerY());
25043e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        }
25053e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham
25063e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        @Override
25073e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham        public void onDrawShadow(Canvas canvas) {
25083e9631982511d9692e4bc0dbd724240fae91cf1fMinh Pham            mChip.getDrawable().draw(canvas);
2509b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        }
2510b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2511b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2512b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    private void showCopyDialog(final String address) {
2513b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyAddress = address;
2514b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyDialog.setTitle(address);
2515bf342b7d5efcc3284f2b790b0ccd9defc1ddb7e2Mindy Pereira        mCopyDialog.setContentView(R.layout.copy_chip_dialog_layout);
2516b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyDialog.setCancelable(true);
2517b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyDialog.setCanceledOnTouchOutside(true);
251875f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        Button button = (Button)mCopyDialog.findViewById(android.R.id.button1);
251975f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        button.setOnClickListener(this);
252075f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        int btnTitleId;
2521577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor        if (isPhoneQuery()) {
252275f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor            btnTitleId = R.string.copy_number;
252375f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        } else {
252475f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor            btnTitleId = R.string.copy_email;
252575f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        }
252675f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        String buttonTitle = getContext().getResources().getString(btnTitleId);
252775f1f6994f77a6538c71df720aa2250fe71fed4bTom Taylor        button.setText(buttonTitle);
2528b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyDialog.setOnDismissListener(this);
2529b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyDialog.show();
2530b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2531b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2532b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2533b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
2534b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        // Do nothing.
2535b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        return false;
2536b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2537b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2538b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2539b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public void onShowPress(MotionEvent e) {
2540b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        // Do nothing.
2541b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2542b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2543b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2544b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public boolean onSingleTapUp(MotionEvent e) {
2545b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        // Do nothing.
2546b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        return false;
2547b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2548b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2549b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2550b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public void onDismiss(DialogInterface dialog) {
2551b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyAddress = null;
2552b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2553b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira
2554b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    @Override
2555b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    public void onClick(View v) {
2556b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        // Copy this to the clipboard.
2557b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
2558b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira                Context.CLIPBOARD_SERVICE);
2559b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        clipboard.setPrimaryClip(ClipData.newPlainText("", mCopyAddress));
2560b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira        mCopyDialog.dismiss();
2561b8208f24b2768acf369ad58309031feac87ce79cMindy Pereira    }
2562577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor
2563577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor    protected boolean isPhoneQuery() {
25641a71f76db0880a572b1443e8a0ac31a4bb584a8bMindy Pereira        return getAdapter() != null
25651a71f76db0880a572b1443e8a0ac31a4bb584a8bMindy Pereira                && ((BaseRecipientAdapter) getAdapter()).getQueryType()
25661a71f76db0880a572b1443e8a0ac31a4bb584a8bMindy Pereira                    == BaseRecipientAdapter.QUERY_TYPE_PHONE;
2567577223c7c81bd5fe0c00a1b8eeeecd50f61369a8Tom Taylor    }
256877056d7532cd26e869964a52456ef18c96f6cbd7Mindy Pereira}
2569