RecipientEditTextView.java revision b3f8574ad18184f5a9392fc7773863f59f9f5192
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
192d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.content.Context;
20c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.Bitmap;
211e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereiraimport android.graphics.BitmapFactory;
22c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.Canvas;
231e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereiraimport android.graphics.Matrix;
24c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.Rect;
251e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereiraimport android.graphics.RectF;
26c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.graphics.drawable.BitmapDrawable;
272d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.graphics.drawable.Drawable;
28156467329e276c9bc90945bea916ce3ac4849574Mindy Pereiraimport android.os.Handler;
29156467329e276c9bc90945bea916ce3ac4849574Mindy Pereiraimport android.os.Message;
302d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.text.Editable;
31c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.Layout;
32c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.Spannable;
33c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.SpannableString;
34c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.Spanned;
35c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.TextPaint;
36c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.TextUtils;
37c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereiraimport android.text.TextWatcher;
38c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.method.QwertyKeyListener;
39c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.text.style.ImageSpan;
402d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.util.AttributeSet;
41c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.util.Log;
424fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.ActionMode;
43c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.view.KeyEvent;
444fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.Menu;
454fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.MenuItem;
46c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.view.MotionEvent;
47c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.view.View;
484fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereiraimport android.view.ActionMode.Callback;
49c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.widget.AdapterView;
50c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.widget.AdapterView.OnItemClickListener;
51b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereiraimport android.widget.ListAdapter;
52c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport android.widget.ListPopupWindow;
53156467329e276c9bc90945bea916ce3ac4849574Mindy Pereiraimport android.widget.ListView;
542d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereiraimport android.widget.MultiAutoCompleteTextView;
55b5ebb8916a1bb178062a0ccb59e80de27ec1bb2dDaisuke Miyakawa
56b5ebb8916a1bb178062a0ccb59e80de27ec1bb2dDaisuke Miyakawaimport java.util.Collection;
57c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport java.util.HashSet;
58c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport java.util.Set;
59c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
60c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereiraimport java.util.ArrayList;
610f93fba1c7da8cb2d209e5e37831600c3b86852cMindy Pereira
622d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira/**
632d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * RecipientEditTextView is an auto complete text view for use with applications
642d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira * that use the new Chips UI for addressing a message to recipients.
652d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira */
66b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereirapublic class RecipientEditTextView extends MultiAutoCompleteTextView implements
67b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        OnItemClickListener, Callback, RecipientAlternatesAdapter.OnCheckedItemChangedListener {
68c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
69c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private static final String TAG = "RecipientEditTextView";
70c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
714e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    // TODO: get correct number/ algorithm from with UX.
724e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private static final int CHIP_LIMIT = 2;
734e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
748684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    private static final int INVALID_CONTACT = -1;
758684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira
764e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    // TODO: get correct size from UX.
774e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private static final float MORE_WIDTH_FACTOR = 0.25f;
784e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
79c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Drawable mChipBackground = null;
80c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
81c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Drawable mChipDelete = null;
82c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
83c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private int mChipPadding;
84c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
85c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Tokenizer mTokenizer;
86c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
87c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Drawable mChipBackgroundPressed;
88c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
89c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private RecipientChip mSelectedChip;
90c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
91c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private int mAlternatesLayout;
92c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
931e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    private Bitmap mDefaultContactPhoto;
941e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
954e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private ImageSpan mMoreChip;
964e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
974e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private int mMoreString;
984e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
994e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private ArrayList<RecipientChip> mRemovedSpans;
1004e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
101e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private float mChipHeight;
102e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira
103e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private float mChipFontSize;
104e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira
1058684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    private Validator mValidator;
1068684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira
107045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira    private Drawable mInvalidChipBackground;
108045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira
109156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira    private Handler mHandler;
110156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira
111156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira    private static int DISMISS = "dismiss".hashCode();
112156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira
113156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira    private static final long DISMISS_DELAY = 300;
114156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira
115c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    private int mPendingChipsCount = 0;
116c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
11777db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira    private static int sSelectedTextColor = -1;
11877db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira
11905522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira    private static final char COMMIT_CHAR_COMMA = ',';
12005522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira
12105522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira    private static final char COMMIT_CHAR_SEMICOLON = ';';
122c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
123b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private ListPopupWindow mAlternatesPopup;
124b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
125b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
126b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Used with {@link mAlternatesPopup}. Handles clicks to alternate addresses for a selected chip.
127b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
128b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private OnItemClickListener mAlternatesListener;
129b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
130b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private int mCheckedItem;
131b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
1322d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    public RecipientEditTextView(Context context, AttributeSet attrs) {
1332d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira        super(context, attrs);
13477db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        if (sSelectedTextColor == -1) {
13577db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira            sSelectedTextColor = context.getResources().getColor(android.R.color.white);
13677db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        }
137b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        mAlternatesPopup = new ListPopupWindow(context);
138b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        mAlternatesListener = new OnItemClickListener() {
139b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            @Override
140b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            public void onItemClick(AdapterView<?> adapterView,View view, int position,
141b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    long rowId) {
142b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                replaceChip(mSelectedChip, ((RecipientAlternatesAdapter) adapterView.getAdapter())
143b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        .getRecipientEntry(position));
144b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                Message delayed = Message.obtain(mHandler, DISMISS);
145b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                delayed.obj = RecipientEditTextView.this.mAlternatesPopup;
146b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                mHandler.sendMessageDelayed(delayed, DISMISS_DELAY);
147b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                clearComposingText();
148b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            }
149b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        };
150d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        setSuggestionsEnabled(false);
151c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        setOnItemClickListener(this);
1524fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        setCustomSelectionActionModeCallback(this);
153c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        // When the user starts typing, make sure we unselect any selected
154c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        // chips.
155c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        addTextChangedListener(new TextWatcher() {
156c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            @Override
157c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            public void afterTextChanged(Editable s) {
15874e701b8da11fb1840169945b4b494f28d44ca79Mindy Pereira                if (mSelectedChip != null) {
15974e701b8da11fb1840169945b4b494f28d44ca79Mindy Pereira                    setCursorVisible(true);
16074e701b8da11fb1840169945b4b494f28d44ca79Mindy Pereira                    setSelection(getText().length());
16174e701b8da11fb1840169945b4b494f28d44ca79Mindy Pereira                    clearSelectedChip();
16274e701b8da11fb1840169945b4b494f28d44ca79Mindy Pereira                }
163c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            }
16405522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira
165c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            @Override
166c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            public void onTextChanged(CharSequence s, int start, int before, int count) {
16705522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                int length = s.length();
168b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                // Make sure there is content there to parse and that it is not
169b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                // just the commit character.
17005522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                if (length > 1) {
17105522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                    char last = s.charAt(length() - 1);
17205522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                    if (last == COMMIT_CHAR_SEMICOLON || last == COMMIT_CHAR_COMMA) {
17305522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                        commitDefault();
17405522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                    }
17505522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                }
176c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            }
17705522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira
178c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            @Override
179c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
18074e701b8da11fb1840169945b4b494f28d44ca79Mindy Pereira                // Do nothing.
181c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira            }
182c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        });
183156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira        mHandler = new Handler() {
184156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira            @Override
185156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira            public void handleMessage(Message msg) {
186156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                if (msg.what == DISMISS) {
187b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    ((ListPopupWindow) msg.obj).dismiss();
188156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                    return;
189156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                }
190156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira                super.handleMessage(msg);
191156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira            }
192156467329e276c9bc90945bea916ce3ac4849574Mindy Pereira        };
1934fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    }
1944fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira
1954fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    @Override
1964fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    public void onSelectionChanged(int start, int end) {
1974fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // When selection changes, see if it is inside the chips area.
1984fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // If so, move the cursor back after the chips again.
19983e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        Spannable span = getSpannable();
20083e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        int textLength = getText().length();
20183e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        RecipientChip[] chips = span.getSpans(start, textLength, RecipientChip.class);
20283e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        if (chips != null && chips.length > 0) {
2034fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira            if (chips != null && chips.length > 0) {
2044fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira                // Grab the last chip and set the cursor to after it.
205b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                setSelection(Math.min(span.getSpanEnd(chips[chips.length - 1]) + 1, textLength));
2064fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira            }
207d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        }
208d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        super.onSelectionChanged(start, end);
209d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    }
210d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira
211c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    /**
212c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * Convenience method: Append the specified text slice to the TextView's
213c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * display buffer, upgrading it to BufferType.EDITABLE if it was
214c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * not already editable. Commas are excluded as they are added automatically
215c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     * by the view.
216c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira     */
217c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    @Override
218c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    public void append(CharSequence text, int start, int end) {
219c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        super.append(text, start, end);
220c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        if (!TextUtils.isEmpty(text) && TextUtils.getTrimmedLength(text) > 0) {
2214f5f0328efbd5f72e30adf08ba7d89a66b4659ceMindy Pereira            final String displayString = (String) text;
22205522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira            int seperatorPos = displayString.indexOf(COMMIT_CHAR_COMMA);
223c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            if (seperatorPos != 0 && !TextUtils.isEmpty(displayString)
224c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                    && TextUtils.getTrimmedLength(displayString) > 0) {
225c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                mPendingChipsCount++;
226c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            }
227c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        }
228c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    }
229c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
230d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    @Override
231d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    public void onFocusChanged(boolean hasFocus, int direction, Rect previous) {
232d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        if (!hasFocus) {
2334e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            shrink();
2341585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira            // Reset any pending chips as they would have been handled
2351585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira            // when the field lost focus.
2361585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira            mPendingChipsCount = 0;
2374fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        } else {
2384e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            expand();
2394fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        }
240d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        super.onFocusChanged(hasFocus, direction, previous);
2412d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
2422d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
2434e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private void shrink() {
2444e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        if (mSelectedChip != null) {
2454e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            clearSelectedChip();
2464e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        } else {
2474e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            commitDefault();
2484e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
2494e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        mMoreChip = createMoreChip();
2504e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
2514e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
2524e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private void expand() {
2534e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        removeMoreChip();
2544e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        setCursorVisible(true);
2554e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Editable text = getText();
2564e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        setSelection(text != null && text.length() > 0 ? text.length() : 0);
2574e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
2584e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
2591e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    private CharSequence ellipsizeText(CharSequence text, TextPaint paint, float maxWidth) {
260e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira        paint.setTextSize(mChipFontSize);
261c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        if (maxWidth <= 0 && Log.isLoggable(TAG, Log.DEBUG)) {
262c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            Log.d(TAG, "Max width is negative: " + maxWidth);
263c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        }
264c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        return TextUtils.ellipsize(text, paint, maxWidth,
265c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                TextUtils.TruncateAt.END);
2661e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    }
267c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
268e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint, Layout layout) {
2691e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // Ellipsize the text so that it takes AT MOST the entire width of the
2701e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // autocomplete text entry area. Make sure to leave space for padding
2711e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // on the sides.
272e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira        int height = (int) mChipHeight;
2731e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int deleteWidth = height;
2741e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        CharSequence ellipsizedText = ellipsizeText(contact.getDisplayName(), paint,
2751e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                calculateAvailableWidth(true) - deleteWidth);
2761e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
2771e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // Make sure there is a minimum chip width so the user can ALWAYS
2781e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // tap a chip without difficulty.
2791e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int width = Math.max(deleteWidth * 2, (int) Math.floor(paint.measureText(ellipsizedText, 0,
2801e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                ellipsizedText.length()))
2811e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                + (mChipPadding * 2) + deleteWidth);
282c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
2831e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        // Create the background of the chip.
2841e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2851e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Canvas canvas = new Canvas(tmpBitmap);
2861e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        if (mChipBackgroundPressed != null) {
2871e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipBackgroundPressed.setBounds(0, 0, width, height);
2881e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipBackgroundPressed.draw(canvas);
28977db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira            paint.setColor(sSelectedTextColor);
2901e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            // Align the display text with where the user enters text.
2911e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding, height
292e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira                    - Math.abs(height - mChipFontSize)/2, paint);
2931e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            // Make the delete a square.
2941e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipDelete.setBounds(width - deleteWidth, 0, width, height);
2951e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            mChipDelete.draw(canvas);
2961e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        } else {
2971e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            Log.w(TAG, "Unable to draw a background for the chips as it was never set");
2981e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        }
2991e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        return tmpBitmap;
3001e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    }
301c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
302045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira
303045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira    /**
304045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Get the background drawable for a RecipientChip.
305045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     */
306045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira    public Drawable getChipBackground(RecipientEntry contact) {
307045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        return mValidator != null && mValidator.isValid(contact.getDestination()) ?
308045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira                mChipBackground : mInvalidChipBackground;
309045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira    }
310045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira
311e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private Bitmap createUnselectedChip(RecipientEntry contact, TextPaint paint, Layout layout) {
312c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Ellipsize the text so that it takes AT MOST the entire width of the
313c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // autocomplete text entry area. Make sure to leave space for padding
314c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // on the sides.
315e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira        int height = (int) mChipHeight;
3161e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int iconWidth = height;
3171e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        CharSequence ellipsizedText = ellipsizeText(contact.getDisplayName(), paint,
3181e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                calculateAvailableWidth(false) - iconWidth);
3191a8bbbf9aa203287f53736a680f96b16d5c878a7Mindy Pereira        // Make sure there is a minimum chip width so the user can ALWAYS
3201a8bbbf9aa203287f53736a680f96b16d5c878a7Mindy Pereira        // tap a chip without difficulty.
3211e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        int width = Math.max(iconWidth * 2, (int) Math.floor(paint.measureText(ellipsizedText, 0,
3221e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                ellipsizedText.length()))
3231e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                + (mChipPadding * 2) + iconWidth);
324c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
325c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Create the background of the chip.
326c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
327c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Canvas canvas = new Canvas(tmpBitmap);
328045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        Drawable background = getChipBackground(contact);
329045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        if (background != null) {
330045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            background.setBounds(0, 0, width, height);
331045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            background.draw(canvas);
3321e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
3339024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            // Don't draw photos for recipients that have been typed in.
3348684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira            if (contact.getContactId() != INVALID_CONTACT) {
3359024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                byte[] photoBytes = contact.getPhotoBytes();
33690081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                // There may not be a photo yet if anything but the first contact address
33790081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                // was selected.
33890081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                if (photoBytes == null && contact.getPhotoThumbnailUri() != null) {
33990081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    // TODO: cache this in the recipient entry?
34090081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    ((BaseRecipientAdapter) getAdapter()).fetchPhoto(contact, contact
34190081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                            .getPhotoThumbnailUri());
34290081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    photoBytes = contact.getPhotoBytes();
34390081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                }
34490081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira
3459024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                Bitmap photo;
3469024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                if (photoBytes != null) {
3479024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                    photo = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length);
3489024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                } else {
3499024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                    // TODO: can the scaled down default photo be cached?
3509024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                    photo = mDefaultContactPhoto;
3519024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                }
3529024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                // Draw the photo on the left side.
3539024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                Matrix matrix = new Matrix();
3549024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                RectF src = new RectF(0, 0, photo.getWidth(), photo.getHeight());
3559024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                RectF dst = new RectF(0, 0, iconWidth, height);
3569024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
3579024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                canvas.drawBitmap(photo, matrix, paint);
358c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            } else {
3599024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                // Don't leave any space for the icon. It isn't being drawn.
3609024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira                iconWidth = 0;
361c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
3621e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
3631e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            // Align the display text with where the user enters text.
3641e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding + iconWidth,
36590081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                    height - Math.abs(height - mChipFontSize) / 2, paint);
366c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        } else {
3671e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            Log.w(TAG, "Unable to draw a background for the chips as it was never set");
3681e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        }
3691e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        return tmpBitmap;
3701e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    }
371c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
3721e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira    public RecipientChip constructChipSpan(RecipientEntry contact, int offset, boolean pressed)
3731e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            throws NullPointerException {
3741e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        if (mChipBackground == null) {
3751e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira            throw new NullPointerException(
3761e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira                    "Unable to render any chips as setChipDimensions was not called.");
377c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
3781e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Layout layout = getLayout();
379c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
3801e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        TextPaint paint = getPaint();
3811e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        float defaultSize = paint.getTextSize();
38277db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        int defaultColor = paint.getColor();
3831e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira
3841e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        Bitmap tmpBitmap;
3851e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        if (pressed) {
386e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira            tmpBitmap = createSelectedChip(contact, paint, layout);
387c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
3881e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        } else {
389045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            tmpBitmap = createUnselectedChip(contact, paint, layout);
3901e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        }
391c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
392c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Pass the full text, un-ellipsized, to the chip.
393c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Drawable result = new BitmapDrawable(getResources(), tmpBitmap);
3941e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        result.setBounds(0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight());
39577db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        RecipientChip recipientChip = new RecipientChip(result, contact, offset);
396c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Return text to the original size.
397c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        paint.setTextSize(defaultSize);
39877db539da02cf42742330f1969fd94bb94a61db8Mindy Pereira        paint.setColor(defaultColor);
399c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return recipientChip;
4002d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
4012d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
4028684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
403045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Calculate the bottom of the line the chip will be located on using:
404045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * 1) which line the chip appears on
4055519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira     * 2) the height of a chip
4065519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira     * 3) padding built into the edit text view
4078684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * 4) the position of the autocomplete view on the screen, taking into account
4088684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * that any top padding will move this down visually
4098684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
410e6f595ab1f8409f23978baae564ad3cb9e0b5f41Mindy Pereira    private int calculateLineBottom(int yOffset, int line, int chipHeight) {
4115519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira        // Line offsets start at zero.
4125519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira        int actualLine = line + 1;
4135519208c37aea65dab7edf555ab27844e12cdd71Mindy Pereira        return yOffset + (actualLine * (chipHeight + getPaddingBottom())) + getPaddingTop();
414f566dee91901e44db63df3bf393afb1d43a36f78Mindy Pereira    }
415f566dee91901e44db63df3bf393afb1d43a36f78Mindy Pereira
4168684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
4178684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Get the max amount of space a chip can take up. The formula takes into
4188684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * account the width of the EditTextView, any view padding, and padding
4198684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * that will be added to the chip.
4208684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
421c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private float calculateAvailableWidth(boolean pressed) {
4221e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira        return getWidth() - getPaddingLeft() - getPaddingRight() - (mChipPadding * 2);
4232d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
4242d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
425c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /**
4264f7412c084ad344e94a320b51270ac6480a47a84Mindy Pereira     * Set all chip dimensions and resources. This has to be done from the
4274f7412c084ad344e94a320b51270ac6480a47a84Mindy Pereira     * application as this is a static library.
4284f7412c084ad344e94a320b51270ac6480a47a84Mindy Pereira     * @param chipBackground
4291e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira     * @param chipBackgroundPressed
4304f7412c084ad344e94a320b51270ac6480a47a84Mindy Pereira     * @param invalidChip
4311e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira     * @param chipDelete
4321e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira     * @param defaultContact
4331426d714da911130b643f7039590eceaa6420aa7Mindy Pereira     * @param moreResource
4341e9f1deb091a9552d746a69685e59a2bf48b6971Mindy Pereira     * @param alternatesLayout
4351426d714da911130b643f7039590eceaa6420aa7Mindy Pereira     * @param chipHeight
436c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * @param padding Padding around the text in a chip
437c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     */
43843876560a507b0672cd2732c74bdbdec84ed3893Mindy Pereira    public void setChipDimensions(Drawable chipBackground, Drawable chipBackgroundPressed,
439045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira            Drawable invalidChip, Drawable chipDelete, Bitmap defaultContact, int moreResource,
440d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira            int alternatesLayout, float chipHeight, float padding,
441d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira            float chipFontSize) {
442d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mChipBackground = chipBackground;
443d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mChipBackgroundPressed = chipBackgroundPressed;
444d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mChipDelete = chipDelete;
445d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mChipPadding = (int) padding;
446d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mAlternatesLayout = alternatesLayout;
447d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mDefaultContactPhoto = defaultContact;
448d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mMoreString = moreResource;
449d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mChipHeight = chipHeight;
450d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mChipFontSize = chipFontSize;
451d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira        mInvalidChipBackground = invalidChip;
452d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira    }
453d79e1a0e1a12944c6b9bae1dcfd5c602693281c0Mindy Pereira
454c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
455c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    public void onSizeChanged(int width, int height, int oldw, int oldh) {
456c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        super.onSizeChanged(width, height, oldw, oldh);
45705522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        // Check for any pending tokens created before layout had been completed
45805522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        // on the view.
459c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        if (width != 0 && height != 0 && mPendingChipsCount > 0) {
460c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            Editable editable = getText();
461c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            // Tokenize!
462c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            int startingPos = 0;
463c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            while (startingPos < editable.length() && mPendingChipsCount > 0) {
464c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                int tokenEnd = mTokenizer.findTokenEnd(editable, startingPos);
465c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                int tokenStart = mTokenizer.findTokenStart(editable, tokenEnd);
4661585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                if (findChip(tokenStart) == null) {
4671585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    // Always include seperators with the token to the left.
4681585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    if (tokenEnd < editable.length() - 1
4691585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                            && editable.charAt(tokenEnd) == COMMIT_CHAR_COMMA) {
4701585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                        tokenEnd++;
4711585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    }
4721585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    startingPos = tokenEnd;
4731585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    String token = (String) editable.toString().substring(tokenStart, tokenEnd);
4741585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    int seperatorPos = token.indexOf(COMMIT_CHAR_COMMA);
4751585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    if (seperatorPos != -1) {
4761585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                        token = token.substring(0, seperatorPos);
4771585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    }
4781585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                    editable.replace(tokenStart, tokenEnd, createChip(RecipientEntry
4791585be84a5d70550b8f2871899dab3528a51fae5Mindy Pereira                            .constructFakeEntry(token), false));
4804f5f0328efbd5f72e30adf08ba7d89a66b4659ceMindy Pereira                }
481c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                mPendingChipsCount--;
482c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            }
483c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira            mPendingChipsCount = 0;
484c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        }
485c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    }
486c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira
487c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira    @Override
488c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public void setTokenizer(Tokenizer tokenizer) {
489c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        mTokenizer = tokenizer;
490c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        super.setTokenizer(mTokenizer);
491c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
492c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
4938684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    @Override
4948684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    public void setValidator(Validator validator) {
4958684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira        mValidator = validator;
4968684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira        super.setValidator(validator);
4978684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    }
4988684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira
4998684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
5008684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * We cannot use the default mechanism for replaceText. Instead,
5018684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * we override onItemClickListener so we can get all the associated
5028684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * contact information including display text, address, and id.
5038684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
504c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
505c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    protected void replaceText(CharSequence text) {
506c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return;
507c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
508c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
5098684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
5108684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Dismiss any selected chips when the back key is pressed.
5118684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
512c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
51395d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
51495d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira        if (keyCode == KeyEvent.KEYCODE_BACK) {
51595d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira            clearSelectedChip();
51695d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira        }
51795d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira        return super.onKeyPreIme(keyCode, event);
51895d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira    }
51995d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira
5208684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
5218684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Monitor key presses in this view to see if the user types
5228684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * any commit keys, which consist of ENTER, TAB, or DPAD_CENTER.
5238684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If the user has entered text that has contact matches and types
5248684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * a commit key, create a chip from the topmost matching contact.
5258684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If the user has entered text that has no contact matches and types
5268684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * a commit key, then create a chip from the text they have entered.
5278684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
52895d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira    @Override
529c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public boolean onKeyUp(int keyCode, KeyEvent event) {
530c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        switch (keyCode) {
531c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            case KeyEvent.KEYCODE_ENTER:
532c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            case KeyEvent.KEYCODE_DPAD_CENTER:
533c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                if (event.hasNoModifiers()) {
534d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira                    if (commitDefault()) {
535c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                        return true;
536c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    }
537e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    if (mSelectedChip != null) {
538e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        clearSelectedChip();
539e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        return true;
540e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                    } else if (focusNext()) {
541e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                        return true;
542e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    }
543c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                }
54495d81e62a0abb2f81624796f1fca9665cdb1a79eMindy Pereira                break;
545e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira            case KeyEvent.KEYCODE_TAB:
546e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                if (event.hasNoModifiers()) {
547e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    if (mSelectedChip != null) {
548e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        clearSelectedChip();
549e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    } else {
550e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        commitDefault();
551e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    }
552e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    if (focusNext()) {
553e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                        return true;
554e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                    }
555e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                }
556c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
557c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return super.onKeyUp(keyCode, event);
558c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
559c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
560e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira    private boolean focusNext() {
561e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        View next = focusSearch(View.FOCUS_DOWN);
562e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        if (next != null) {
563e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira            next.requestFocus();
564e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira            return true;
565e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        }
566e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira        return false;
567e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira    }
568e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira
569045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira    /**
570045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Create a chip from the default selection. If the popup is showing, the
571045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * default is the first item in the popup suggestions list. Otherwise, it is
572045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * whatever the user had typed in. End represents where the the tokenizer
573045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * should search for a token to turn into a chip.
574045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * @return If a chip was created from a real contact.
575045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     */
5768684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    private boolean commitDefault() {
5774e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Editable editable = getText();
5784e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        boolean enough = enoughToFilter();
5794e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        boolean shouldSubmitAtPosition = false;
580045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        int end = getSelectionEnd();
5814e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int start = mTokenizer.findTokenStart(editable, end);
582e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira        boolean submitText = false;
5834e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        if (enough) {
5844e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            RecipientChip[] chips = getSpannable().getSpans(start, end, RecipientChip.class);
5854e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            if ((chips == null || chips.length == 0)) {
5864e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                // There's something being filtered or typed that has not been
5874e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                // completed yet.
588e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                // Check for the end of the token.
589e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                end = mTokenizer.findTokenEnd(editable, start);
590e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                // The user has tapped somewhere in the middle of the text
591e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                // and started editing. In this case, we always want to
592e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                // submit the full text token and not what may be in the
593e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                // suggestions popup.
594e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                if (end != getSelectionEnd()) {
595e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                    submitText = true;
596e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                    setSelection(end);
597e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira                }
5984e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                shouldSubmitAtPosition = true;
5994e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            }
6004e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
6014e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
6024e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        if (shouldSubmitAtPosition) {
603e98f079e157d2872100174ba67beef8ebbc0cec0Mindy Pereira            if (!submitText && getAdapter().getCount() > 0) {
6044e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                // choose the first entry.
6054e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                submitItemAtPosition(0);
606d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira                dismissDropDown();
6074e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                return true;
6084e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            } else {
6094e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                String text = editable.toString().substring(start, end);
6104e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                clearComposingText();
61190081ee88c7eb216ea22f426aa6856e310a867e1Mindy Pereira                if (text != null && text.length() > 0 && !text.equals(" ")) {
61205522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira                    text = removeCommitChars(text);
6134e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                    RecipientEntry entry = RecipientEntry.constructFakeEntry(text);
6144e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                    QwertyKeyListener.markAsReplaced(editable, start, end, "");
615fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira                    editable.replace(start, end, createChip(entry, false));
6164e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                    dismissDropDown();
6174e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                }
618e24cb318a0e07fce3db99e1ef6234a196f256befMindy Pereira                return true;
619d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira            }
620d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        }
6214e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
622d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira    }
623d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira
62405522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira    private String removeCommitChars(String text) {
62505522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        int commitCharPosition = text.indexOf(COMMIT_CHAR_COMMA);
62605522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        if (commitCharPosition != -1) {
62705522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira            text = text.substring(0, commitCharPosition);
62805522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        }
62905522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        commitCharPosition = text.indexOf(COMMIT_CHAR_SEMICOLON);
63005522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        if (commitCharPosition != -1) {
63105522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira            text = text.substring(0, commitCharPosition);
63205522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        }
63305522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira        return text;
63405522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira    }
63505522aeae1948f776e618f69d34d4a5e48a0471cMindy Pereira
6368684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
6378684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If there is a selected chip, delegate the key events
6388684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * to the selected chip.
6398684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
640c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
641c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public boolean onKeyDown(int keyCode, KeyEvent event) {
642b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (mSelectedChip != null && keyCode == KeyEvent.KEYCODE_DEL) {
643b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            if (mAlternatesPopup != null && mAlternatesPopup.isShowing()) {
644b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                mAlternatesPopup.dismiss();
645b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            }
646b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            removeChip(mSelectedChip);
647c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
648c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
649c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (keyCode == KeyEvent.KEYCODE_ENTER && event.hasNoModifiers()) {
650c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            return true;
651c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
652c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
653c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return super.onKeyDown(keyCode, event);
6542d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira    }
6552d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira
656c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private Spannable getSpannable() {
657c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return (Spannable) getText();
658c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
659c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
660b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private int getChipStart(RecipientChip chip) {
661b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return getSpannable().getSpanStart(chip);
662b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
663b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
664b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private int getChipEnd(RecipientChip chip) {
665b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return getSpannable().getSpanEnd(chip);
666b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
667b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
668c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /**
669c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * Instead of filtering on the entire contents of the edit box,
670c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * this subclass method filters on the range from
671c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * {@link Tokenizer#findTokenStart} to {@link #getSelectionEnd}
672c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * if the length of that range meets or exceeds {@link #getThreshold}
673c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     * and makes sure that the range is not already a Chip.
674c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     */
675c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
676c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    protected void performFiltering(CharSequence text, int keyCode) {
677c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (enoughToFilter()) {
678c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            int end = getSelectionEnd();
679c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            int start = mTokenizer.findTokenStart(text, end);
680c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            // If this is a RecipientChip, don't filter
681c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            // on its contents.
682c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            Spannable span = getSpannable();
683c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            RecipientChip[] chips = span.getSpans(start, end, RecipientChip.class);
684c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            if (chips != null && chips.length > 0) {
685c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                return;
686c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
687c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
688c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        super.performFiltering(text, keyCode);
689c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
690c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
691c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private void clearSelectedChip() {
692c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (mSelectedChip != null) {
693b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            unselectChip(mSelectedChip);
694c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            mSelectedChip = null;
695c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
69636d720eb2b7a8d3466d221944a00b8bae7de8e9cMindy Pereira        setCursorVisible(true);
697c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
698c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
6998684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
7008684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Monitor touch events in the RecipientEditTextView.
7018684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If the view does not have focus, any tap on the view
7028684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * will just focus the view. If the view has focus, determine
7038684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * if the touch target is a recipient chip. If it is and the chip
7048684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * is not selected, select it and clear any other selected chips.
7058684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * If it isn't, then select that chip.
7068684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
707c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
708c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public boolean onTouchEvent(MotionEvent event) {
709d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        if (!isFocused()) {
710d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira            // Ignore any chip taps until this view is focused.
711d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira            return super.onTouchEvent(event);
712d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        }
713d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira
714c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        boolean handled = super.onTouchEvent(event);
715d9b57273c1f5c3bcd94e662136446cd6fd465ebcMindy Pereira        int action = event.getAction();
716c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        boolean chipWasSelected = false;
717c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
718c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
719c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            float x = event.getX();
720c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            float y = event.getY();
721c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            int offset = putOffsetInRange(getOffsetForPosition(x, y));
722c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            RecipientChip currentChip = findChip(offset);
723c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            if (currentChip != null) {
724c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                if (action == MotionEvent.ACTION_UP) {
725c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    if (mSelectedChip != null && mSelectedChip != currentChip) {
726c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                        clearSelectedChip();
727b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        mSelectedChip = selectChip(currentChip);
728c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    } else if (mSelectedChip == null) {
7298684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        // Selection may have moved due to the tap event,
7308684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        // but make sure we correctly reset selection to the
7318684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        // end so that any unfinished chips are committed.
7328684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        setSelection(getText().length());
7338684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira                        commitDefault();
734b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        mSelectedChip = selectChip(currentChip);
735c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    } else {
736b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        onClick(mSelectedChip, offset, x, y);
737c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    }
738c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                }
739c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                chipWasSelected = true;
740c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
741c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
742c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        if (action == MotionEvent.ACTION_UP && !chipWasSelected) {
743c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            clearSelectedChip();
744c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
745c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return handled;
746c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
747c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
748b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private void showAlternates(RecipientChip currentChip, ListPopupWindow alternatesPopup,
749b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int width, Context context) {
750b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int line = getLayout().getLineForOffset(getChipStart(currentChip));
751b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int[] xy = getLocationOnScreen();
752b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int bottom = calculateLineBottom(xy[1], line, (int) mChipHeight);
753b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        View anchorView = new View(context);
754b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        anchorView.setBottom(bottom);
755b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        anchorView.setTop(bottom);
756b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Align the alternates popup with the left side of the View,
757b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // regardless of the position of the chip tapped.
758b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.setWidth(width);
759b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.setAnchorView(anchorView);
760b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.setAdapter(createAlternatesAdapter(currentChip));
761b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.setOnItemClickListener(mAlternatesListener); // currentChip);
762b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        alternatesPopup.show();
763b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        ListView listView = alternatesPopup.getListView();
764b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
765b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Checked item would be -1 if the adapter has not
766b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // loaded the view that should be checked yet. The
767b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // variable will be set correctly when onCheckedItemChanged
768b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // is called in a separate thread.
769b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (mCheckedItem != -1) {
770b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            listView.setItemChecked(mCheckedItem, true);
771b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mCheckedItem = -1;
772b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        }
773b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
774b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
775b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private int[] getLocationOnScreen() {
776b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int[] xy = new int[2];
777b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        getLocationOnScreen(xy);
778b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return xy;
779b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
780b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
781b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private ListAdapter createAlternatesAdapter(RecipientChip chip) {
782b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return new RecipientAlternatesAdapter(getContext(), chip.getContactId(), chip.getDataId(),
783b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                mAlternatesLayout, this);
784b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
785b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
786b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public void onCheckedItemChanged(int position) {
787b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        ListView listView = mAlternatesPopup.getListView();
788b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (listView != null && listView.getCheckedItemCount() == 0) {
789b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            listView.setItemChecked(position, true);
790b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        } else {
791b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mCheckedItem = position;
792b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        }
793b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
794b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira
795c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    // TODO: This algorithm will need a lot of tweaking after more people have used
796c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    // the chips ui. This attempts to be "forgiving" to fat finger touches by favoring
797c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    // what comes before the finger.
798c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private int putOffsetInRange(int o) {
799c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int offset = o;
800c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Editable text = getText();
801c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int length = text.length();
802c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Remove whitespace from end to find "real end"
803c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int realLength = length;
804c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        for (int i = length - 1; i >= 0; i--) {
805c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            if (text.charAt(i) == ' ') {
806c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                realLength--;
807c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            } else {
808c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                break;
809c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
810c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
811c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
8124fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // If the offset is beyond or at the end of the text,
8134fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        // leave it alone.
8144fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        if (offset >= realLength) {
815c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            return offset;
816c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
8174fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        Editable editable = getText();
818c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        while (offset >= 0 && findText(editable, offset) == -1 && findChip(offset) == null) {
819c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            // Keep walking backward!
820c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            offset--;
821c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
822c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return offset;
823c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
824c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
8254fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    private int findText(Editable text, int offset) {
8264fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        if (text.charAt(offset) != ' ') {
8274fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira            return offset;
8284fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        }
8294fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira        return -1;
8304fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira    }
8314fef2782b06043dbf73c896c4d1962be0b8dcd50Mindy Pereira
832c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private RecipientChip findChip(int offset) {
833c4ad472c11324428ac9c75bcf7d52263ff4699f1Mindy Pereira        RecipientChip[] chips = getSpannable().getSpans(0, getText().length(), RecipientChip.class);
834c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Find the chip that contains this offset.
835c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        for (int i = 0; i < chips.length; i++) {
836c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            RecipientChip chip = chips[i];
837b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int start = getChipStart(chip);
838b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int end = getChipEnd(chip);
839b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            if (offset >= start && offset <= end) {
840c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                return chip;
841c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
842c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
843c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return null;
844c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
845c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
846fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira    private CharSequence createChip(RecipientEntry entry, boolean pressed) {
847c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        String displayText = entry.getDestination();
8484f5f0328efbd5f72e30adf08ba7d89a66b4659ceMindy Pereira        displayText = (String) mTokenizer.terminateToken(displayText);
849c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        // Always leave a blank space at the end of a chip.
850c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira        int textLength = displayText.length() - 1;
851c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        SpannableString chipText = new SpannableString(displayText);
852c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int end = getSelectionEnd();
853c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int start = mTokenizer.findTokenStart(getText(), end);
854c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        try {
855fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira            chipText.setSpan(constructChipSpan(entry, start, pressed), 0, textLength,
856c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
857c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        } catch (NullPointerException e) {
858c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            Log.e(TAG, e.getMessage(), e);
859c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            return null;
860c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
861c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
862c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return chipText;
863c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
864c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
8658684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
8668684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * When an item in the suggestions list has been clicked, create a chip from the
8678684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * contact information of the selected item.
8688684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
869c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    @Override
870c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
871c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        submitItemAtPosition(position);
872c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
873c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
874c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    private void submitItemAtPosition(int position) {
875c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        RecipientEntry entry = (RecipientEntry) getAdapter().getItem(position);
8769024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira        // If the display name and the address are the same, then make this
8779024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira        // a fake recipient that is editable.
8789024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira        if (TextUtils.equals(entry.getDisplayName(), entry.getDestination())) {
8799024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            entry = RecipientEntry.constructFakeEntry(entry.getDestination());
8809024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira        }
881c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        clearComposingText();
882c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
883c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int end = getSelectionEnd();
884c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        int start = mTokenizer.findTokenStart(getText(), end);
885c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
886c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        Editable editable = getText();
887c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        QwertyKeyListener.markAsReplaced(editable, start, end, "");
888fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira        editable.replace(start, end, createChip(entry, false));
889c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
890c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
891c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /** Returns a collection of contact Id for each chip inside this View. */
892c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /* package */ Collection<Long> getContactIds() {
893c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        final Set<Long> result = new HashSet<Long>();
8947a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira        RecipientChip[] chips = getRecipients();
8957a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira        if (chips != null) {
8967a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira            for (RecipientChip chip : chips) {
8977a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira                result.add(chip.getContactId());
8987a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira            }
899c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
900c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return result;
901c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
902c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
90383e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira    private RecipientChip[] getRecipients() {
90483e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        return getSpannable().getSpans(0, getText().length(), RecipientChip.class);
90583e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira    }
90683e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira
907c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /** Returns a collection of data Id for each chip inside this View. May be null. */
908c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /* package */ Collection<Long> getDataIds() {
909c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        final Set<Long> result = new HashSet<Long>();
91083e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        RecipientChip [] chips = getRecipients();
9117a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira        if (chips != null) {
9127a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira            for (RecipientChip chip : chips) {
9137a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira                result.add(chip.getDataId());
9147a424984849627457aa27ac19c0d23cbe0887c85Mindy Pereira            }
915c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
916c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        return result;
917c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    }
918c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
9194e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9204e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
9214e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
9224e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
9234e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
9244e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9254e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
9264e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public void onDestroyActionMode(ActionMode mode) {
9274e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
9284e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9294e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
9304e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
9314e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
9324e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
9334e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9348684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
9358684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * No chips are selectable.
9368684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
9374e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    @Override
9384e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
9394e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return false;
9404e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
9414e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9428684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
943045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * Create the more chip. The more chip is text that replaces any chips that
944045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * do not fit in the pre-defined available space when the
945045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira     * RecipientEditTextView loses focus.
9468684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
9474e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private ImageSpan createMoreChip() {
94883e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        RecipientChip[] recipients = getRecipients();
94983e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        if (recipients == null || recipients.length <= CHIP_LIMIT) {
9504e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            return null;
9514e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
95283e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira        int numRecipients = recipients.length;
9534e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int overage = numRecipients - CHIP_LIMIT;
9544e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Editable text = getText();
9554e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        // TODO: get the correct size from visual design.
9564e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int width = (int) Math.floor(getWidth() * MORE_WIDTH_FACTOR);
9574e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int height = getLineHeight();
9584e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Bitmap drawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
9594e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Canvas canvas = new Canvas(drawable);
9604e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        String moreText = getResources().getString(mMoreString, overage);
9614e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        canvas.drawText(moreText, 0, moreText.length(), 0, height - getLayout().getLineDescent(0),
9624e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                getPaint());
9634e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9644e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Drawable result = new BitmapDrawable(getResources(), drawable);
9654e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        result.setBounds(0, 0, width, height);
9664e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        ImageSpan moreSpan = new ImageSpan(result);
9674e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        Spannable spannable = getSpannable();
9684e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        // Remove the overage chips.
9694e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        RecipientChip[] chips = spannable.getSpans(0, text.length(), RecipientChip.class);
9704e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        if (chips == null || chips.length == 0) {
9714e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            Log.w(TAG,
9724e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                "We have recipients. Tt should not be possible to have zero RecipientChips.");
9734e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            return null;
9744e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
975045e80b59ef5e9a709b6e5843d2301a02e0872f2Mindy Pereira        mRemovedSpans = new ArrayList<RecipientChip>(chips.length);
9764e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int totalReplaceStart = 0;
9774e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        int totalReplaceEnd = 0;
9784e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        for (int i = numRecipients - overage; i < chips.length; i++) {
9794e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            mRemovedSpans.add(chips[i]);
9809024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            if (i == numRecipients - overage) {
981b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                totalReplaceStart = spannable.getSpanStart(chips[i]);
9829024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            }
9839024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            if (i == chips.length - 1) {
984b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                totalReplaceEnd = spannable.getSpanEnd(chips[i]);
9859024e5c88fde2f878eea4bca6923ad57a3f0cfe0Mindy Pereira            }
986b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            chips[i].storeChipStart(spannable.getSpanStart(chips[i]));
987b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            chips[i].storeChipEnd(spannable.getSpanEnd(chips[i]));
9884e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            spannable.removeSpan(chips[i]);
9894e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
9904e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        SpannableString chipText = new SpannableString(text.subSequence(totalReplaceStart,
9914e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                totalReplaceEnd));
9924e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
9934e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        text.replace(totalReplaceStart, totalReplaceEnd, chipText);
9944e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        return moreSpan;
9954e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
9964e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
9978684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira    /**
9988684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * Replace the more chip, if it exists, with all of the recipient chips it had
9998684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     * replaced when the RecipientEditTextView gains focus.
10008684974e4befb4c9dcc21c995c4ff3af7103ab10Mindy Pereira     */
10014e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    private void removeMoreChip() {
10024e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        if (mMoreChip != null) {
10034e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            Spannable span = getSpannable();
10044e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            span.removeSpan(mMoreChip);
10054e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            mMoreChip = null;
10064e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            // Re-add the spans that were removed.
10074e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            if (mRemovedSpans != null && mRemovedSpans.size() > 0) {
10084e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                // Recreate each removed span.
10094e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                Editable editable = getText();
10104e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                SpannableString associatedText;
10114e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                for (RecipientChip chip : mRemovedSpans) {
1012b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    int chipStart = chip.getStoredChipStart();
1013b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    int chipEnd = Math.min(editable.length(), chip.getStoredChipEnd());
1014b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                    if (Log.isLoggable(TAG, Log.DEBUG) && chipEnd != chip.getStoredChipEnd()) {
1015c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                        Log.d(TAG,
1016c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                                "Unexpectedly, the chip ended after the end of the editable text. "
1017b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                                        + "Chip End " + chip.getStoredChipEnd()
1018c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                                        + "Editable length " + editable.length());
1019c72429f17e12ec39e61a7efdb7312b7f5d8a5cbcMindy Pereira                    }
10204e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                    associatedText = new SpannableString(editable.subSequence(chipStart, chipEnd));
10214e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                    associatedText.setSpan(chip, 0, associatedText.length(),
10224e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
10234e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                    editable.replace(chipStart, chipEnd, associatedText);
10244e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                }
10254e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira                mRemovedSpans.clear();
10264e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira            }
10274e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira        }
10284e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira    }
10294e1f6098a731be3d0be1f9e02a1fa2677e4de187Mindy Pereira
1030c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira    /**
1031b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Show specified chip as selected. If the RecipientChip is just an email address,
1032b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * selecting the chip will take the contents of the chip and place it at
1033b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * the end of the RecipientEditTextView for inline editing. If the
1034b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * RecipientChip is a complete contact, then selecting the chip
1035b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * will change the background color of the chip, show the delete icon,
1036b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * and a popup window with the address in use highlighted and any other
1037b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * alternate addresses for the contact.
1038b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * @param currentChip Chip to select.
1039b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * @return A RecipientChip in the selected state or null if the chip
1040b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * just contained an email address.
1041c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira     */
1042b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public RecipientChip selectChip(RecipientChip currentChip) {
1043b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (currentChip.getContactId() != INVALID_CONTACT) {
1044b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int start = getChipStart(currentChip);
1045b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int end = getChipEnd(currentChip);
1046b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            getSpannable().removeSpan(currentChip);
1047b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            RecipientChip newChip;
1048b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            CharSequence displayText = mTokenizer.terminateToken(currentChip.getValue());
1049b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            // Always leave a blank space at the end of a chip.
1050b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            int textLength = displayText.length() - 1;
1051b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            SpannableString chipText = new SpannableString(displayText);
1052b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            try {
1053b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                newChip = constructChipSpan(currentChip.getEntry(), start, true);
1054b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                chipText.setSpan(newChip, 0, textLength,
1055b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
1056b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            } catch (NullPointerException e) {
1057b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                Log.e(TAG, e.getMessage(), e);
1058b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                return null;
1059b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            }
1060fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira            Editable editable = getText();
1061b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            QwertyKeyListener.markAsReplaced(editable, start, end, "");
106283e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira            if (start == -1 || end == -1) {
1063b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                Log.d(TAG, "The chip being selected no longer exists but should.");
106483e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira            } else {
1065b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                editable.replace(start, end, chipText);
106683e4ae382ffbec673b16c85c35e3cfe19ada98dcMindy Pereira            }
1067b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            newChip.setSelected(true);
1068b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            showAlternates(newChip, mAlternatesPopup, getWidth(), getContext());
1069b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            setCursorVisible(false);
1070b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            return newChip;
1071b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        } else {
1072b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            CharSequence text = currentChip.getValue();
1073b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            Editable editable = getText();
1074b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            removeChip(currentChip);
1075b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            editable.append(text);
1076fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira            setCursorVisible(true);
1077fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira            setSelection(editable.length());
1078b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            return null;
1079fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira        }
1080b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1081fab0afdc6742dcba55cfbe802cd143434d48f413Mindy Pereira
1082c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1083b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1084b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Remove selection from this chip. Unselecting a RecipientChip will render
1085b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * the chip without a delete icon and with an unfocused background. This
1086b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * is called when the RecipientChip no longer has focus.
1087b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1088b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public void unselectChip(RecipientChip chip) {
1089b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int start = getChipStart(chip);
1090b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int end = getChipEnd(chip);
1091b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Editable editable = getText();
1092b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (start == -1 || end == -1) {
1093b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            Log.e(TAG, "The chip being unselected no longer exists but should.");
1094b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        } else {
1095b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            getSpannable().removeSpan(this);
1096b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            QwertyKeyListener.markAsReplaced(editable, start, end, "");
1097b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            editable.replace(start, end, createChip(chip.getEntry(), false));
1098c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1099b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        mSelectedChip = null;
1100b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        clearSelectedChip();
1101b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        setCursorVisible(true);
1102b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        setSelection(editable.length());
1103b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (mAlternatesPopup != null && mAlternatesPopup.isShowing()) {
1104b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mAlternatesPopup.dismiss();
1105c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1106b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1107c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1108c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1109b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1110b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Return whether this chip contains the position passed in.
1111b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1112b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public boolean matchesChip(RecipientChip chip, int offset) {
1113b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int start = getChipStart(chip);
1114b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int end = getChipEnd(chip);
1115b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (start == -1 || end == -1) {
1116b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            return false;
1117c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1118b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return (offset >= start && offset <= end);
1119b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1120c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1121c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1122b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1123b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Return whether a touch event was inside the delete target of
1124b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * a selected chip. It is in the delete target if:
1125b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * 1) the x and y points of the event are within the
1126b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * delete assset.
1127b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * 2) the point tapped would have caused a cursor to appear
1128b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * right after the selected chip.
1129b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * @return boolean
1130b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1131b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private boolean isInDelete(RecipientChip chip, int offset, float x, float y) {
1132b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Figure out the bounds of this chip and whether or not
1133b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // the user clicked in the X portion.
1134b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        return chip.isSelected() && offset == getChipEnd(chip);
1135b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
11363656f7e97c58dc8443132d2d8297629b6a04cce7Mindy Pereira
1137b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1138b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Remove the chip and any text associated with it from the RecipientEditTextView.
1139b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1140b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    private void removeChip(RecipientChip chip) {
1141b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Spannable spannable = getSpannable();
1142b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int spanStart = spannable.getSpanStart(chip);
1143b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int spanEnd = spannable.getSpanEnd(chip);
1144b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Editable text = getText();
1145b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int toDelete = spanEnd;
1146b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        boolean wasSelected = chip == mSelectedChip;
1147b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Clear that there is a selected chip before updating any text.
1148b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
1149b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mSelectedChip = null;
1150c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1151b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        // Always remove trailing spaces when removing a chip.
1152b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        while (toDelete >= 0 && toDelete < text.length() - 1 && text.charAt(toDelete) == ' ') {
1153b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            toDelete++;
1154c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1155b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        spannable.removeSpan(chip);
1156b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        text.delete(spanStart, toDelete);
1157b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
1158b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            clearSelectedChip();
1159c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1160b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1161c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1162b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1163b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Replace this currently selected chip with a new chip
1164b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * that uses the contact data provided.
1165b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1166b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public void replaceChip(RecipientChip chip, RecipientEntry entry) {
1167b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        boolean wasSelected = chip == mSelectedChip;
1168b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
1169b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            mSelectedChip = null;
1170c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1171b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int start = getChipStart(chip);
1172b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        int end = getChipEnd(chip);
1173b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        getSpannable().removeSpan(chip);
1174b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        Editable editable = getText();
1175b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        CharSequence chipText = createChip(entry, false);
1176b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (start == -1 || end == -1) {
1177b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            Log.e(TAG, "The chip to replace does not exist but should.");
1178b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            editable.insert(0, chipText);
1179b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        } else {
1180b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            editable.replace(start, end, chipText);
1181c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1182b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        setCursorVisible(true);
1183b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (wasSelected) {
1184b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            clearSelectedChip();
1185c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1186b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1187c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1188b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    /**
1189b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Handle click events for a chip. When a selected chip receives a click
1190b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * event, see if that event was in the delete icon. If so, delete it.
1191b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     * Otherwise, unselect the chip.
1192b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira     */
1193b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    public void onClick(RecipientChip chip, int offset, float x, float y) {
1194b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira        if (chip.isSelected()) {
1195b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            if (isInDelete(chip, offset, x, y)) {
1196b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                removeChip(chip);
1197b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira            } else {
1198b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira                clearSelectedChip();
1199c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira            }
1200c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira        }
1201b3f8574ad18184f5a9392fc7773863f59f9f5192Mindy Pereira    }
1202c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
1203c7ea4602c84a8fed20488337b8d1a00d8a205f6aMindy Pereira
12042d4ee907769ccfc94dc315e932ff235198958c69Mindy Pereira}
1205