Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 1 | /* |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2 | |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 3 | * Copyright (C) 2011 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package com.android.ex.chips; |
| 19 | |
Jin Cao | 14efc57 | 2014-09-10 10:27:01 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 21 | import android.app.Dialog; |
| 22 | import android.content.ClipData; |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 23 | import android.content.ClipDescription; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 24 | import android.content.ClipboardManager; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 26 | import android.content.DialogInterface; |
| 27 | import android.content.DialogInterface.OnDismissListener; |
Mindy Pereira | 52c441e | 2011-12-15 13:35:37 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
Mindy Pereira | 22faaa6 | 2011-12-16 14:30:34 -0800 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 31 | import android.graphics.BitmapFactory; |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 32 | import android.graphics.BitmapShader; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 33 | import android.graphics.Canvas; |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 34 | import android.graphics.Color; |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 35 | import android.graphics.Matrix; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 36 | import android.graphics.Paint; |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 37 | import android.graphics.Paint.Style; |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 38 | import android.graphics.Point; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 40 | import android.graphics.RectF; |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 41 | import android.graphics.Shader.TileMode; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 42 | import android.graphics.drawable.BitmapDrawable; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 43 | import android.graphics.drawable.Drawable; |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 44 | import android.graphics.drawable.StateListDrawable; |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 45 | import android.os.AsyncTask; |
Scott Kennedy | 1650ef6 | 2013-08-02 21:56:47 -0700 | [diff] [blame] | 46 | import android.os.Build; |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 47 | import android.os.Handler; |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 48 | import android.os.Looper; |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 49 | import android.os.Message; |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 50 | import android.os.Parcelable; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 51 | import android.text.Editable; |
Gilles Debunne | 61b48cc | 2011-08-16 14:03:00 -0700 | [diff] [blame] | 52 | import android.text.InputType; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 53 | import android.text.Layout; |
| 54 | import android.text.Spannable; |
| 55 | import android.text.SpannableString; |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 56 | import android.text.SpannableStringBuilder; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 57 | import android.text.Spanned; |
| 58 | import android.text.TextPaint; |
| 59 | import android.text.TextUtils; |
Mindy Pereira | b88ee45 | 2011-06-02 11:07:43 -0700 | [diff] [blame] | 60 | import android.text.TextWatcher; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 61 | import android.text.method.QwertyKeyListener; |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 62 | import android.text.util.Rfc822Token; |
| 63 | import android.text.util.Rfc822Tokenizer; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 64 | import android.util.AttributeSet; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 65 | import android.util.Log; |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 66 | import android.util.TypedValue; |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 67 | import android.view.ActionMode; |
Andy Stadler | aa2afff | 2011-08-04 16:22:47 -0700 | [diff] [blame] | 68 | import android.view.ActionMode.Callback; |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 69 | import android.view.DragEvent; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 70 | import android.view.GestureDetector; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 71 | import android.view.KeyEvent; |
Mindy Pereira | 750e6e5 | 2011-07-25 11:51:30 -0700 | [diff] [blame] | 72 | import android.view.LayoutInflater; |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 73 | import android.view.Menu; |
| 74 | import android.view.MenuItem; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 75 | import android.view.MotionEvent; |
| 76 | import android.view.View; |
Gilles Debunne | 61b48cc | 2011-08-16 14:03:00 -0700 | [diff] [blame] | 77 | import android.view.View.OnClickListener; |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 78 | import android.view.ViewParent; |
Jin Cao | 04f06ea | 2014-09-22 14:58:28 -0700 | [diff] [blame] | 79 | import android.view.accessibility.AccessibilityEvent; |
| 80 | import android.view.accessibility.AccessibilityManager; |
Mindy Pereira | 93364a6 | 2012-04-27 14:17:22 -0700 | [diff] [blame] | 81 | import android.view.inputmethod.EditorInfo; |
| 82 | import android.view.inputmethod.InputConnection; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 83 | import android.widget.AdapterView; |
| 84 | import android.widget.AdapterView.OnItemClickListener; |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 85 | import android.widget.Button; |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 86 | import android.widget.Filterable; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 87 | import android.widget.ListAdapter; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 88 | import android.widget.ListPopupWindow; |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 89 | import android.widget.ListView; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 90 | import android.widget.MultiAutoCompleteTextView; |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 91 | import android.widget.ScrollView; |
Mindy Pereira | 750e6e5 | 2011-07-25 11:51:30 -0700 | [diff] [blame] | 92 | import android.widget.TextView; |
Daisuke Miyakawa | 7537f84 | 2011-05-31 15:47:25 -0700 | [diff] [blame] | 93 | |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 94 | import com.android.ex.chips.RecipientAlternatesAdapter.RecipientMatchCallback; |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 95 | import com.android.ex.chips.recipientchip.DrawableRecipientChip; |
| 96 | import com.android.ex.chips.recipientchip.InvisibleRecipientChip; |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 97 | import com.android.ex.chips.recipientchip.ReplacementDrawableSpan; |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 98 | import com.android.ex.chips.recipientchip.VisibleRecipientChip; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 99 | |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 100 | import java.util.ArrayList; |
| 101 | import java.util.Arrays; |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 102 | import java.util.Collections; |
| 103 | import java.util.Comparator; |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 104 | import java.util.List; |
| 105 | import java.util.Map; |
| 106 | import java.util.Set; |
| 107 | import java.util.regex.Matcher; |
| 108 | import java.util.regex.Pattern; |
| 109 | |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 110 | /** |
| 111 | * RecipientEditTextView is an auto complete text view for use with applications |
| 112 | * that use the new Chips UI for addressing a message to recipients. |
| 113 | */ |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 114 | public class RecipientEditTextView extends MultiAutoCompleteTextView implements |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 115 | OnItemClickListener, Callback, RecipientAlternatesAdapter.OnCheckedItemChangedListener, |
Mindy Pereira | ecee50c | 2011-09-09 16:30:20 -0700 | [diff] [blame] | 116 | GestureDetector.OnGestureListener, OnDismissListener, OnClickListener, |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 117 | TextView.OnEditorActionListener, DropdownChipLayouter.ChipDeleteListener { |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 118 | private static final String TAG = "RecipientEditTextView"; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 119 | |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 120 | private static final char COMMIT_CHAR_COMMA = ','; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 121 | private static final char COMMIT_CHAR_SEMICOLON = ';'; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 122 | private static final char COMMIT_CHAR_SPACE = ' '; |
Scott Kennedy | 03e9e98 | 2013-03-04 13:26:22 -0800 | [diff] [blame] | 123 | private static final String SEPARATOR = String.valueOf(COMMIT_CHAR_COMMA) |
| 124 | + String.valueOf(COMMIT_CHAR_SPACE); |
| 125 | |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 126 | // This pattern comes from android.util.Patterns. It has been tweaked to handle a "1" before |
| 127 | // parens, so numbers such as "1 (425) 222-2342" match. |
| 128 | private static final Pattern PHONE_PATTERN |
| 129 | = Pattern.compile( // sdd = space, dot, or dash |
| 130 | "(\\+[0-9]+[\\- \\.]*)?" // +<digits><sdd>* |
| 131 | + "(1?[ ]*\\([0-9]+\\)[\\- \\.]*)?" // 1(<digits>)<sdd>* |
| 132 | + "([0-9][0-9\\- \\.][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit> |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 133 | |
Scott Kennedy | f11483a | 2014-04-18 13:36:27 -0700 | [diff] [blame] | 134 | private static final int DISMISS = "dismiss".hashCode(); |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 135 | private static final long DISMISS_DELAY = 300; |
| 136 | |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 137 | // TODO: get correct number/ algorithm from with UX. |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 138 | // Visible for testing. |
| 139 | /*package*/ static final int CHIP_LIMIT = 2; |
| 140 | |
| 141 | private static final int MAX_CHIPS_PARSED = 50; |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 142 | |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 143 | private static int sSelectedTextColor = -1; |
Jin Cao | 14efc57 | 2014-09-10 10:27:01 -0700 | [diff] [blame] | 144 | private static int sVisibleDisplayFrameTop = -1; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 145 | |
| 146 | // Resources for displaying chips. |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 147 | private Drawable mChipBackground = null; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 148 | private Drawable mChipDelete = null; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 149 | private Drawable mInvalidChipBackground; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 150 | private Drawable mChipBackgroundPressed; |
| 151 | |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 152 | // Possible attr overrides |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 153 | private float mChipHeight; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 154 | private float mChipFontSize; |
Mindy Pereira | ffd270a | 2012-05-23 16:11:47 -0700 | [diff] [blame] | 155 | private float mLineSpacingExtra; |
Jin Cao | 58d541d | 2014-07-29 16:23:03 -0700 | [diff] [blame] | 156 | private int mChipTextStartPadding; |
| 157 | private int mChipTextEndPadding; |
Scott Kennedy | 15259d5 | 2014-07-09 17:28:47 -0700 | [diff] [blame] | 158 | private final int mTextHeight; |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 159 | private boolean mDisableDelete; |
| 160 | private int mMaxLines; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 161 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Enumerator for avatar position. See attr.xml for more details. |
Andrew Sapperstein | c0e6023 | 2014-01-24 19:03:35 -0800 | [diff] [blame] | 164 | * 0 for end, 1 for start. |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 165 | */ |
| 166 | private int mAvatarPosition; |
Andrew Sapperstein | c0e6023 | 2014-01-24 19:03:35 -0800 | [diff] [blame] | 167 | private static final int AVATAR_POSITION_END = 0; |
Andrew Sapperstein | c0e6023 | 2014-01-24 19:03:35 -0800 | [diff] [blame] | 168 | private static final int AVATAR_POSITION_START = 1; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 169 | |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 170 | private Paint mWorkPaint = new Paint(); |
| 171 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 172 | private Tokenizer mTokenizer; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 173 | private Validator mValidator; |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 174 | private Handler mHandler; |
| 175 | private TextWatcher mTextWatcher; |
| 176 | private DropdownChipLayouter mDropdownChipLayouter; |
| 177 | |
Jin Cao | 93d69f4 | 2014-10-07 12:27:49 -0700 | [diff] [blame^] | 178 | private View mDropdownAnchor = this; |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 179 | private ListPopupWindow mAlternatesPopup; |
| 180 | private ListPopupWindow mAddressPopup; |
| 181 | private View mAlternatePopupAnchor; |
| 182 | private OnItemClickListener mAlternatesListener; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 183 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 184 | private DrawableRecipientChip mSelectedChip; |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 185 | private Bitmap mDefaultContactPhoto; |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 186 | private ReplacementDrawableSpan mMoreChip; |
Mindy Pereira | 750e6e5 | 2011-07-25 11:51:30 -0700 | [diff] [blame] | 187 | private TextView mMoreItem; |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 188 | |
Jin Cao | 04f06ea | 2014-09-22 14:58:28 -0700 | [diff] [blame] | 189 | private boolean mIsAccessibilityOn; |
Jin Cao | b1aeb7b | 2014-09-01 23:48:05 -0700 | [diff] [blame] | 190 | private int mCurrentSuggestionCount; |
| 191 | |
Scott Kennedy | 20c8aa7 | 2013-03-06 22:23:59 -0800 | [diff] [blame] | 192 | // VisibleForTesting |
| 193 | final ArrayList<String> mPendingChips = new ArrayList<String>(); |
Mindy Pereira | f97eb41 | 2011-06-24 11:26:20 -0700 | [diff] [blame] | 194 | |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 195 | private int mPendingChipsCount = 0; |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 196 | private int mCheckedItem; |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 197 | private boolean mNoChips = false; |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 198 | private boolean mShouldShrink = true; |
Jin Cao | 4de6a53 | 2014-08-08 11:34:10 -0700 | [diff] [blame] | 199 | |
Scott Kennedy | 20c8aa7 | 2013-03-06 22:23:59 -0800 | [diff] [blame] | 200 | // VisibleForTesting |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 201 | ArrayList<DrawableRecipientChip> mTemporaryRecipients; |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 202 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 203 | private ArrayList<DrawableRecipientChip> mRemovedSpans; |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 204 | |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 205 | // Chip copy fields. |
| 206 | private GestureDetector mGestureDetector; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 207 | private Dialog mCopyDialog; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 208 | private String mCopyAddress; |
| 209 | |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 210 | // Obtain the enclosing scroll view, if it exists, so that the view can be |
| 211 | // scrolled to show the last line of chips content. |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 212 | private ScrollView mScrollView; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 213 | private boolean mTriedGettingScrollView; |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 214 | private boolean mDragEnabled = false; |
| 215 | |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 216 | private boolean mAttachedToWindow; |
Tom Taylor | 4491a06 | 2012-11-15 13:57:41 -0800 | [diff] [blame] | 217 | |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 218 | private final Runnable mAddTextWatcher = new Runnable() { |
| 219 | @Override |
| 220 | public void run() { |
| 221 | if (mTextWatcher == null) { |
| 222 | mTextWatcher = new RecipientTextWatcher(); |
| 223 | addTextChangedListener(mTextWatcher); |
| 224 | } |
| 225 | } |
| 226 | }; |
| 227 | |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 228 | private IndividualReplacementTask mIndividualReplacements; |
| 229 | |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 230 | private Runnable mHandlePendingChips = new Runnable() { |
| 231 | |
| 232 | @Override |
| 233 | public void run() { |
| 234 | handlePendingChips(); |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | }; |
| 238 | |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 239 | private Runnable mDelayedShrink = new Runnable() { |
| 240 | |
| 241 | @Override |
| 242 | public void run() { |
| 243 | shrink(); |
| 244 | } |
| 245 | |
| 246 | }; |
| 247 | |
Andrew Sapperstein | 0f2b174 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 248 | private RecipientEntryItemClickedListener mRecipientEntryItemClickedListener; |
| 249 | |
| 250 | public interface RecipientEntryItemClickedListener { |
| 251 | /** |
| 252 | * Callback that occurs whenever an auto-complete suggestion is clicked. |
| 253 | * @param charactersTyped the number of characters typed by the user to provide the |
| 254 | * auto-complete suggestions. |
| 255 | * @param position the position in the dropdown list that the user clicked |
| 256 | */ |
| 257 | void onRecipientEntryItemClicked(int charactersTyped, int position); |
| 258 | } |
| 259 | |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 260 | public RecipientEditTextView(Context context, AttributeSet attrs) { |
| 261 | super(context, attrs); |
Mindy Pereira | 22faaa6 | 2011-12-16 14:30:34 -0800 | [diff] [blame] | 262 | setChipDimensions(context, attrs); |
Scott Kennedy | 15259d5 | 2014-07-09 17:28:47 -0700 | [diff] [blame] | 263 | mTextHeight = calculateTextHeight(); |
Mindy Pereira | e50b0a1 | 2011-06-15 16:16:38 -0700 | [diff] [blame] | 264 | if (sSelectedTextColor == -1) { |
| 265 | sSelectedTextColor = context.getResources().getColor(android.R.color.white); |
| 266 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 267 | mAlternatesPopup = new ListPopupWindow(context); |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 268 | mAlternatesPopup.setBackgroundDrawable(null); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 269 | mAddressPopup = new ListPopupWindow(context); |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 270 | mAddressPopup.setBackgroundDrawable(null); |
Mindy Pereira | bfedc1e | 2011-09-07 12:17:12 -0700 | [diff] [blame] | 271 | mCopyDialog = new Dialog(context); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 272 | mAlternatesListener = new OnItemClickListener() { |
| 273 | @Override |
| 274 | public void onItemClick(AdapterView<?> adapterView,View view, int position, |
| 275 | long rowId) { |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 276 | mAlternatesPopup.setOnItemClickListener(null); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 277 | replaceChip(mSelectedChip, ((RecipientAlternatesAdapter) adapterView.getAdapter()) |
| 278 | .getRecipientEntry(position)); |
| 279 | Message delayed = Message.obtain(mHandler, DISMISS); |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 280 | delayed.obj = mAlternatesPopup; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 281 | mHandler.sendMessageDelayed(delayed, DISMISS_DELAY); |
| 282 | clearComposingText(); |
| 283 | } |
| 284 | }; |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 285 | setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); |
Mindy Pereira | b88ee45 | 2011-06-02 11:07:43 -0700 | [diff] [blame] | 286 | setOnItemClickListener(this); |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 287 | setCustomSelectionActionModeCallback(this); |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 288 | mHandler = new Handler() { |
| 289 | @Override |
| 290 | public void handleMessage(Message msg) { |
| 291 | if (msg.what == DISMISS) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 292 | ((ListPopupWindow) msg.obj).dismiss(); |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | super.handleMessage(msg); |
| 296 | } |
| 297 | }; |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 298 | mTextWatcher = new RecipientTextWatcher(); |
| 299 | addTextChangedListener(mTextWatcher); |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 300 | mGestureDetector = new GestureDetector(context, this); |
Mindy Pereira | 93364a6 | 2012-04-27 14:17:22 -0700 | [diff] [blame] | 301 | setOnEditorActionListener(this); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 302 | |
Jin Cao | a20a8d0 | 2014-08-06 17:41:49 -0700 | [diff] [blame] | 303 | setDropdownChipLayouter(new DropdownChipLayouter(LayoutInflater.from(context), context)); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Jin Cao | 04f06ea | 2014-09-22 14:58:28 -0700 | [diff] [blame] | 306 | @Override |
| 307 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 308 | super.onLayout(changed, left, top, right, bottom); |
| 309 | |
| 310 | final AccessibilityManager accessibilityManager = |
| 311 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 312 | mIsAccessibilityOn = accessibilityManager.isEnabled(); |
| 313 | } |
| 314 | |
Scott Kennedy | 15259d5 | 2014-07-09 17:28:47 -0700 | [diff] [blame] | 315 | private int calculateTextHeight() { |
| 316 | final Rect textBounds = new Rect(); |
| 317 | final TextPaint paint = getPaint(); |
| 318 | |
| 319 | textBounds.setEmpty(); |
| 320 | // First measure the bounds of a sample text. |
| 321 | final String textHeightSample = "a"; |
| 322 | paint.getTextBounds(textHeightSample, 0, textHeightSample.length(), textBounds); |
| 323 | |
| 324 | textBounds.left = 0; |
| 325 | textBounds.right = 0; |
| 326 | |
| 327 | return textBounds.height(); |
| 328 | } |
| 329 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 330 | public void setDropdownChipLayouter(DropdownChipLayouter dropdownChipLayouter) { |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 331 | mDropdownChipLayouter = dropdownChipLayouter; |
Jin Cao | a20a8d0 | 2014-08-06 17:41:49 -0700 | [diff] [blame] | 332 | mDropdownChipLayouter.setDeleteListener(this); |
Mindy Pereira | 93364a6 | 2012-04-27 14:17:22 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Andrew Sapperstein | 0f2b174 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 335 | public void setRecipientEntryItemClickedListener(RecipientEntryItemClickedListener listener) { |
| 336 | mRecipientEntryItemClickedListener = listener; |
| 337 | } |
| 338 | |
Mindy Pereira | 93364a6 | 2012-04-27 14:17:22 -0700 | [diff] [blame] | 339 | @Override |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 340 | protected void onDetachedFromWindow() { |
Tony Mantler | 2b2de7c | 2014-03-12 11:59:31 -0700 | [diff] [blame] | 341 | super.onDetachedFromWindow(); |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 342 | mAttachedToWindow = false; |
Jin Cao | 808d713 | 2014-08-24 11:13:02 -0700 | [diff] [blame] | 343 | dismissPopups(); |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
| 347 | protected void onAttachedToWindow() { |
Tony Mantler | 2b2de7c | 2014-03-12 11:59:31 -0700 | [diff] [blame] | 348 | super.onAttachedToWindow(); |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 349 | mAttachedToWindow = true; |
Jin Cao | 93d69f4 | 2014-10-07 12:27:49 -0700 | [diff] [blame^] | 350 | |
| 351 | final int anchorId = getDropDownAnchor(); |
| 352 | if (anchorId != View.NO_ID) { |
| 353 | mDropdownAnchor = getRootView().findViewById(anchorId); |
| 354 | } |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | @Override |
Mindy Pereira | 93364a6 | 2012-04-27 14:17:22 -0700 | [diff] [blame] | 358 | public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) { |
| 359 | if (action == EditorInfo.IME_ACTION_DONE) { |
| 360 | if (commitDefault()) { |
| 361 | return true; |
| 362 | } |
| 363 | if (mSelectedChip != null) { |
| 364 | clearSelectedChip(); |
| 365 | return true; |
| 366 | } else if (focusNext()) { |
| 367 | return true; |
| 368 | } |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| 375 | InputConnection connection = super.onCreateInputConnection(outAttrs); |
| 376 | int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION; |
| 377 | if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) { |
| 378 | // clear the existing action |
| 379 | outAttrs.imeOptions ^= imeActions; |
| 380 | // set the DONE action |
| 381 | outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE; |
| 382 | } |
| 383 | if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { |
| 384 | outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; |
| 385 | } |
Scott Kennedy | 1af243f | 2013-01-29 23:38:43 -0800 | [diff] [blame] | 386 | |
| 387 | outAttrs.actionId = EditorInfo.IME_ACTION_DONE; |
Kaikai Wang | a5461b7 | 2014-09-18 17:38:46 -0700 | [diff] [blame] | 388 | |
| 389 | // Custom action labels are discouraged in L; a checkmark icon is shown in place of the |
| 390 | // custom text in this case. |
| 391 | outAttrs.actionLabel = Build.VERSION.SDK_INT >= Build.VERSION_CODES.L ? null : |
| 392 | getContext().getString(R.string.action_label); |
Mindy Pereira | 93364a6 | 2012-04-27 14:17:22 -0700 | [diff] [blame] | 393 | return connection; |
Erik | e33555f | 2011-06-21 14:20:20 -0700 | [diff] [blame] | 394 | } |
Mindy Pereira | 7afe160 | 2011-06-20 10:53:52 -0700 | [diff] [blame] | 395 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 396 | /*package*/ DrawableRecipientChip getLastChip() { |
| 397 | DrawableRecipientChip last = null; |
| 398 | DrawableRecipientChip[] chips = getSortedRecipients(); |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 399 | if (chips != null && chips.length > 0) { |
| 400 | last = chips[chips.length - 1]; |
Erik | e33555f | 2011-06-21 14:20:20 -0700 | [diff] [blame] | 401 | } |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 402 | return last; |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Ken Thai | 1248e53 | 2014-05-01 09:05:02 -0700 | [diff] [blame] | 405 | /** |
| 406 | * @return The list of {@link RecipientEntry}s that have been selected by the user. |
| 407 | */ |
| 408 | public List<RecipientEntry> getSelectedRecipients() { |
| 409 | DrawableRecipientChip[] chips = |
| 410 | getText().getSpans(0, getText().length(), DrawableRecipientChip.class); |
| 411 | List<RecipientEntry> results = new ArrayList(); |
| 412 | if (chips == null) { |
| 413 | return results; |
| 414 | } |
| 415 | |
| 416 | for (DrawableRecipientChip c : chips) { |
| 417 | results.add(c.getEntry()); |
| 418 | } |
| 419 | |
| 420 | return results; |
| 421 | } |
| 422 | |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 423 | @Override |
| 424 | public void onSelectionChanged(int start, int end) { |
| 425 | // When selection changes, see if it is inside the chips area. |
| 426 | // If so, move the cursor back after the chips again. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 427 | DrawableRecipientChip last = getLastChip(); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 428 | if (last != null && start < getSpannable().getSpanEnd(last)) { |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 429 | // Grab the last chip and set the cursor to after it. |
| 430 | setSelection(Math.min(getSpannable().getSpanEnd(last) + 1, getText().length())); |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 431 | } |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 432 | super.onSelectionChanged(start, end); |
| 433 | } |
| 434 | |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 435 | @Override |
| 436 | public void onRestoreInstanceState(Parcelable state) { |
| 437 | if (!TextUtils.isEmpty(getText())) { |
| 438 | super.onRestoreInstanceState(null); |
| 439 | } else { |
| 440 | super.onRestoreInstanceState(state); |
| 441 | } |
| 442 | } |
| 443 | |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 444 | @Override |
Mindy Pereira | daa640c | 2011-10-03 14:18:30 -0700 | [diff] [blame] | 445 | public Parcelable onSaveInstanceState() { |
| 446 | // If the user changes orientation while they are editing, just roll back the selection. |
| 447 | clearSelectedChip(); |
| 448 | return super.onSaveInstanceState(); |
| 449 | } |
| 450 | |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Convenience method: Append the specified text slice to the TextView's |
| 453 | * display buffer, upgrading it to BufferType.EDITABLE if it was |
| 454 | * not already editable. Commas are excluded as they are added automatically |
| 455 | * by the view. |
| 456 | */ |
| 457 | @Override |
| 458 | public void append(CharSequence text, int start, int end) { |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 459 | // We don't care about watching text changes while appending. |
| 460 | if (mTextWatcher != null) { |
| 461 | removeTextChangedListener(mTextWatcher); |
| 462 | } |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 463 | super.append(text, start, end); |
| 464 | if (!TextUtils.isEmpty(text) && TextUtils.getTrimmedLength(text) > 0) { |
mindyp | 0124468 | 2012-08-27 10:32:17 -0700 | [diff] [blame] | 465 | String displayString = text.toString(); |
Scott Kennedy | 03e9e98 | 2013-03-04 13:26:22 -0800 | [diff] [blame] | 466 | |
| 467 | if (!displayString.trim().endsWith(String.valueOf(COMMIT_CHAR_COMMA))) { |
| 468 | // We have no separator, so we should add it |
| 469 | super.append(SEPARATOR, 0, SEPARATOR.length()); |
| 470 | displayString += SEPARATOR; |
mindyp | 0124468 | 2012-08-27 10:32:17 -0700 | [diff] [blame] | 471 | } |
Scott Kennedy | 03e9e98 | 2013-03-04 13:26:22 -0800 | [diff] [blame] | 472 | |
mindyp | 20d7af7 | 2012-09-10 15:47:40 -0700 | [diff] [blame] | 473 | if (!TextUtils.isEmpty(displayString) |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 474 | && TextUtils.getTrimmedLength(displayString) > 0) { |
| 475 | mPendingChipsCount++; |
Scott Kennedy | 03e9e98 | 2013-03-04 13:26:22 -0800 | [diff] [blame] | 476 | mPendingChips.add(displayString); |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
mindyp | abb864d | 2012-09-10 12:06:35 -0700 | [diff] [blame] | 479 | // Put a message on the queue to make sure we ALWAYS handle pending |
| 480 | // chips. |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 481 | if (mPendingChipsCount > 0) { |
| 482 | postHandlePendingChips(); |
| 483 | } |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 484 | mHandler.post(mAddTextWatcher); |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 487 | @Override |
| 488 | public void onFocusChanged(boolean hasFocus, int direction, Rect previous) { |
Mindy Pereira | d5d86aa | 2011-08-17 10:37:59 -0700 | [diff] [blame] | 489 | super.onFocusChanged(hasFocus, direction, previous); |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 490 | if (!hasFocus) { |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 491 | shrink(); |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 492 | } else { |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 493 | expand(); |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 494 | } |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Jin Cao | 14efc57 | 2014-09-10 10:27:01 -0700 | [diff] [blame] | 497 | // sVisibleDisplayFrameTop is computed on a on-demand basis because the view needs to be fully |
| 498 | // measured and created in order to calculate the visible display frame. |
| 499 | private int getVisibleDisplayFrameTop() { |
| 500 | if (sVisibleDisplayFrameTop == -1) { |
| 501 | final TypedValue tv = new TypedValue(); |
| 502 | final Context context = getContext(); |
| 503 | // Visible top is our visible display (due to status bar) plus the height of action bar. |
| 504 | if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { |
| 505 | sVisibleDisplayFrameTop = TypedValue.complexToDimensionPixelSize(tv.data, |
| 506 | getResources().getDisplayMetrics()); |
| 507 | } |
| 508 | // Compute the status bar height, or rather where our visible display starts |
| 509 | if (context instanceof Activity) { |
| 510 | final Rect visibleRect = new Rect(); |
| 511 | ((Activity) context).getWindow().getDecorView() |
| 512 | .getWindowVisibleDisplayFrame(visibleRect); |
| 513 | sVisibleDisplayFrameTop += visibleRect.top; |
| 514 | } |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 515 | } |
Jin Cao | 14efc57 | 2014-09-10 10:27:01 -0700 | [diff] [blame] | 516 | return sVisibleDisplayFrameTop; |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Alice Yang | 6b7110f | 2013-04-26 12:50:35 -0700 | [diff] [blame] | 519 | @Override |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 520 | public <T extends ListAdapter & Filterable> void setAdapter(T adapter) { |
| 521 | super.setAdapter(adapter); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 522 | BaseRecipientAdapter baseAdapter = (BaseRecipientAdapter) adapter; |
| 523 | baseAdapter.registerUpdateObserver(new BaseRecipientAdapter.EntriesUpdatedObserver() { |
| 524 | @Override |
| 525 | public void onChanged(List<RecipientEntry> entries) { |
| 526 | // Scroll the chips field to the top of the screen so |
| 527 | // that the user can see as many results as possible. |
| 528 | if (entries != null && entries.size() > 0) { |
| 529 | scrollBottomIntoView(); |
Jin Cao | b1aeb7b | 2014-09-01 23:48:05 -0700 | [diff] [blame] | 530 | // Here the current suggestion count is still the old one since we update |
| 531 | // the count at the bottom of this function. |
| 532 | if (mCurrentSuggestionCount == 0) { |
| 533 | // Announce the new number of possible choices for accessibility. |
Jin Cao | 04f06ea | 2014-09-22 14:58:28 -0700 | [diff] [blame] | 534 | announceForAccessibilityCompat(getContext().getString( |
Jin Cao | b1aeb7b | 2014-09-01 23:48:05 -0700 | [diff] [blame] | 535 | R.string.accessbility_suggestion_dropdown_opened)); |
| 536 | } |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 537 | } |
Jin Cao | 93d69f4 | 2014-10-07 12:27:49 -0700 | [diff] [blame^] | 538 | |
| 539 | // Set the dropdown height to be the remaining height from the anchor to the bottom. |
Jin Cao | bd31231 | 2014-09-12 15:57:08 -0700 | [diff] [blame] | 540 | final int[] coords = new int[2]; |
Jin Cao | 93d69f4 | 2014-10-07 12:27:49 -0700 | [diff] [blame^] | 541 | mDropdownAnchor.getLocationInWindow(coords); |
Jin Cao | bd31231 | 2014-09-12 15:57:08 -0700 | [diff] [blame] | 542 | final Rect displayFrame = new Rect(); |
| 543 | getWindowVisibleDisplayFrame(displayFrame); |
Jin Cao | 93d69f4 | 2014-10-07 12:27:49 -0700 | [diff] [blame^] | 544 | setDropDownHeight(displayFrame.bottom - coords[1] - mDropdownAnchor.getHeight()); |
Jin Cao | b1aeb7b | 2014-09-01 23:48:05 -0700 | [diff] [blame] | 545 | |
| 546 | mCurrentSuggestionCount = entries == null ? 0 : entries.size(); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 547 | } |
| 548 | }); |
| 549 | baseAdapter.setDropdownChipLayouter(mDropdownChipLayouter); |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Jin Cao | 04f06ea | 2014-09-22 14:58:28 -0700 | [diff] [blame] | 552 | private void announceForAccessibilityCompat(String text) { |
| 553 | if (mIsAccessibilityOn && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 554 | final ViewParent parent = getParent(); |
| 555 | if (parent != null) { |
| 556 | AccessibilityEvent event = AccessibilityEvent.obtain( |
| 557 | AccessibilityEvent.TYPE_ANNOUNCEMENT); |
| 558 | onInitializeAccessibilityEvent(event); |
| 559 | event.getText().add(text); |
| 560 | event.setContentDescription(null); |
| 561 | parent.requestSendAccessibilityEvent(this, event); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Kevin Lin | 53e718a | 2014-02-04 17:40:33 -0800 | [diff] [blame] | 566 | protected void scrollBottomIntoView() { |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 567 | if (mScrollView != null && mShouldShrink) { |
Jin Cao | 14efc57 | 2014-09-10 10:27:01 -0700 | [diff] [blame] | 568 | final int[] location = new int[2]; |
| 569 | getLocationInWindow(location); |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 570 | // Desired position shows at least 1 line of chips below the action |
| 571 | // bar. We add excess padding to make sure this is always below other |
| 572 | // content. |
Jin Cao | 14efc57 | 2014-09-10 10:27:01 -0700 | [diff] [blame] | 573 | final int height = getHeight(); |
| 574 | final int currentPos = location[1] + height; |
| 575 | final int desiredPos = getVisibleDisplayFrameTop() + height / getLineCount(); |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 576 | if (currentPos > desiredPos) { |
| 577 | mScrollView.scrollBy(0, currentPos - desiredPos); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Kevin Lin | 53e718a | 2014-02-04 17:40:33 -0800 | [diff] [blame] | 582 | protected ScrollView getScrollView() { |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 583 | return mScrollView; |
Kevin Lin | 53e718a | 2014-02-04 17:40:33 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Mindy Pereira | d5d86aa | 2011-08-17 10:37:59 -0700 | [diff] [blame] | 586 | @Override |
| 587 | public void performValidation() { |
| 588 | // Do nothing. Chips handles its own validation. |
| 589 | } |
| 590 | |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 591 | private void shrink() { |
Mindy Pereira | 5c125af | 2011-12-13 11:04:13 -0800 | [diff] [blame] | 592 | if (mTokenizer == null) { |
| 593 | return; |
| 594 | } |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 595 | long contactId = mSelectedChip != null ? mSelectedChip.getEntry().getContactId() : -1; |
| 596 | if (mSelectedChip != null && contactId != RecipientEntry.INVALID_CONTACT |
| 597 | && (!isPhoneQuery() && contactId != RecipientEntry.GENERATED_CONTACT)) { |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 598 | clearSelectedChip(); |
| 599 | } else { |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 600 | if (getWidth() <= 0) { |
| 601 | // We don't have the width yet which means the view hasn't been drawn yet |
| 602 | // and there is no reason to attempt to commit chips yet. |
| 603 | // This focus lost must be the result of an orientation change |
| 604 | // or an initial rendering. |
| 605 | // Re-post the shrink for later. |
| 606 | mHandler.removeCallbacks(mDelayedShrink); |
| 607 | mHandler.post(mDelayedShrink); |
| 608 | return; |
| 609 | } |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 610 | // Reset any pending chips as they would have been handled |
| 611 | // when the field lost focus. |
| 612 | if (mPendingChipsCount > 0) { |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 613 | postHandlePendingChips(); |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 614 | } else { |
| 615 | Editable editable = getText(); |
| 616 | int end = getSelectionEnd(); |
| 617 | int start = mTokenizer.findTokenStart(editable, end); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 618 | DrawableRecipientChip[] chips = |
| 619 | getSpannable().getSpans(start, end, DrawableRecipientChip.class); |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 620 | if ((chips == null || chips.length == 0)) { |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 621 | Editable text = getText(); |
| 622 | int whatEnd = mTokenizer.findTokenEnd(text, start); |
| 623 | // This token was already tokenized, so skip past the ending token. |
Mindy Pereira | 448e90b | 2011-09-22 11:49:32 -0700 | [diff] [blame] | 624 | if (whatEnd < text.length() && text.charAt(whatEnd) == ',') { |
mindyp | 4f82d88 | 2012-11-08 16:30:27 -0800 | [diff] [blame] | 625 | whatEnd = movePastTerminators(whatEnd); |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 626 | } |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 627 | // In the middle of chip; treat this as an edit |
| 628 | // and commit the whole token. |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 629 | int selEnd = getSelectionEnd(); |
Mindy Pereira | 6337231 | 2011-11-15 15:23:07 -0800 | [diff] [blame] | 630 | if (whatEnd != selEnd) { |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 631 | handleEdit(start, whatEnd); |
| 632 | } else { |
| 633 | commitChip(start, end, editable); |
| 634 | } |
| 635 | } |
Mindy Pereira | a74f40d | 2011-07-12 09:07:17 -0700 | [diff] [blame] | 636 | } |
Mindy Pereira | e81f8a3 | 2011-07-12 16:11:43 -0700 | [diff] [blame] | 637 | mHandler.post(mAddTextWatcher); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 638 | } |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 639 | createMoreChip(); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | private void expand() { |
mindyp | 374ccb2 | 2012-11-06 10:54:05 -0800 | [diff] [blame] | 643 | if (mShouldShrink) { |
| 644 | setMaxLines(Integer.MAX_VALUE); |
| 645 | } |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 646 | removeMoreChip(); |
| 647 | setCursorVisible(true); |
| 648 | Editable text = getText(); |
| 649 | setSelection(text != null && text.length() > 0 ? text.length() : 0); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 650 | // If there are any temporary chips, try replacing them now that the user |
| 651 | // has expanded the field. |
| 652 | if (mTemporaryRecipients != null && mTemporaryRecipients.size() > 0) { |
| 653 | new RecipientReplacementTask().execute(); |
| 654 | mTemporaryRecipients = null; |
| 655 | } |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 658 | private CharSequence ellipsizeText(CharSequence text, TextPaint paint, float maxWidth) { |
Mindy Pereira | 6e8e8e8 | 2011-06-07 14:41:32 -0700 | [diff] [blame] | 659 | paint.setTextSize(mChipFontSize); |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 660 | if (maxWidth <= 0 && Log.isLoggable(TAG, Log.DEBUG)) { |
| 661 | Log.d(TAG, "Max width is negative: " + maxWidth); |
| 662 | } |
| 663 | return TextUtils.ellipsize(text, paint, maxWidth, |
| 664 | TextUtils.TruncateAt.END); |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 667 | /** |
| 668 | * Creates a bitmap of the given contact on a selected chip. |
| 669 | * |
| 670 | * @param contact The recipient entry to pull data from. |
| 671 | * @param paint The paint to use to draw the bitmap. |
| 672 | */ |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 673 | private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint) { |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 674 | paint.setColor(sSelectedTextColor); |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 675 | final ChipBitmapContainer bitmapContainer = createChipBitmap(contact, paint, |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 676 | mChipBackgroundPressed, getResources().getColor(R.color.chip_background_selected)); |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 677 | |
| 678 | if (bitmapContainer.loadIcon) { |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 679 | loadAvatarIcon(contact, bitmapContainer); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 680 | } |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 681 | |
| 682 | return bitmapContainer.bitmap; |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 685 | /** |
| 686 | * Creates a bitmap of the given contact on a selected chip. |
| 687 | * |
| 688 | * @param contact The recipient entry to pull data from. |
| 689 | * @param paint The paint to use to draw the bitmap. |
| 690 | */ |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 691 | private Bitmap createUnselectedChip(RecipientEntry contact, TextPaint paint) { |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 692 | paint.setColor(getContext().getResources().getColor(android.R.color.black)); |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 693 | ChipBitmapContainer bitmapContainer = createChipBitmap(contact, paint, |
| 694 | getChipBackground(contact), getDefaultChipBackgroundColor(contact)); |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 695 | |
| 696 | if (bitmapContainer.loadIcon) { |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 697 | loadAvatarIcon(contact, bitmapContainer); |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 698 | } |
| 699 | return bitmapContainer.bitmap; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 702 | private ChipBitmapContainer createChipBitmap(RecipientEntry contact, TextPaint paint, |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 703 | Drawable overrideBackgroundDrawable, int backgroundColor) { |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 704 | final ChipBitmapContainer result = new ChipBitmapContainer(); |
| 705 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 706 | Rect backgroundPadding = new Rect(); |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 707 | if (overrideBackgroundDrawable != null) { |
| 708 | overrideBackgroundDrawable.getPadding(backgroundPadding); |
| 709 | } |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 710 | |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 711 | // Ellipsize the text so that it takes AT MOST the entire width of the |
| 712 | // autocomplete text entry area. Make sure to leave space for padding |
| 713 | // on the sides. |
Mindy Pereira | 6e8e8e8 | 2011-06-07 14:41:32 -0700 | [diff] [blame] | 714 | int height = (int) mChipHeight; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 715 | // Since the icon is a square, it's width is equal to the maximum height it can be inside |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 716 | // the chip. Don't include iconWidth for invalid contacts. |
| 717 | int iconWidth = contact.isValid() ? |
| 718 | height - backgroundPadding.top - backgroundPadding.bottom : 0; |
Mindy Pereira | 6ed7ded | 2011-10-14 16:31:31 -0700 | [diff] [blame] | 719 | float[] widths = new float[1]; |
| 720 | paint.getTextWidths(" ", widths); |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 721 | CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint, |
Kevin Lin | 9b398e3 | 2014-02-26 12:29:10 -0800 | [diff] [blame] | 722 | calculateAvailableWidth() - iconWidth - widths[0] - backgroundPadding.left |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 723 | - backgroundPadding.right); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 724 | int textWidth = (int) paint.measureText(ellipsizedText, 0, ellipsizedText.length()); |
| 725 | |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 726 | // Chip start padding is the same as the end padding if there is no contact image. |
| 727 | final int startPadding = contact.isValid() ? mChipTextStartPadding : mChipTextEndPadding; |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 728 | // Make sure there is a minimum chip width so the user can ALWAYS |
| 729 | // tap a chip without difficulty. |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 730 | int width = Math.max(iconWidth * 2, textWidth + startPadding + mChipTextEndPadding |
Jin Cao | 58d541d | 2014-07-29 16:23:03 -0700 | [diff] [blame] | 731 | + iconWidth + backgroundPadding.left + backgroundPadding.right); |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 732 | |
| 733 | // Create the background of the chip. |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 734 | result.bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 735 | final Canvas canvas = new Canvas(result.bitmap); |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 736 | |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 737 | // Check if the background drawable is set via attr |
| 738 | if (overrideBackgroundDrawable != null) { |
| 739 | overrideBackgroundDrawable.setBounds(0, 0, width, height); |
| 740 | overrideBackgroundDrawable.draw(canvas); |
| 741 | } else { |
| 742 | // Draw the default chip background |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 743 | mWorkPaint.reset(); |
| 744 | mWorkPaint.setColor(backgroundColor); |
Jin Cao | 568fb22 | 2014-08-19 13:50:59 -0700 | [diff] [blame] | 745 | mWorkPaint.setAntiAlias(true); |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 746 | final float radius = height / 2; |
| 747 | canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 748 | mWorkPaint); |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 751 | // Draw the text vertically aligned |
Andrew Sapperstein | c0e6023 | 2014-01-24 19:03:35 -0800 | [diff] [blame] | 752 | int textX = shouldPositionAvatarOnRight() ? |
Jin Cao | 58d541d | 2014-07-29 16:23:03 -0700 | [diff] [blame] | 753 | mChipTextEndPadding + backgroundPadding.left : |
| 754 | width - backgroundPadding.right - mChipTextEndPadding - textWidth; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 755 | canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), |
Scott Kennedy | 15259d5 | 2014-07-09 17:28:47 -0700 | [diff] [blame] | 756 | textX, getTextYOffset(height), paint); |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 757 | |
| 758 | // Set the variables that are needed to draw the icon bitmap once it's loaded |
| 759 | int iconX = shouldPositionAvatarOnRight() ? width - backgroundPadding.right - iconWidth : |
| 760 | backgroundPadding.left; |
| 761 | result.left = iconX; |
| 762 | result.top = backgroundPadding.top; |
| 763 | result.right = iconX + iconWidth; |
| 764 | result.bottom = height - backgroundPadding.bottom; |
| 765 | |
| 766 | return result; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Helper function that draws the loaded icon bitmap into the chips bitmap |
| 771 | */ |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 772 | private void drawIcon(ChipBitmapContainer bitMapResult, Bitmap icon) { |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 773 | final Canvas canvas = new Canvas(bitMapResult.bitmap); |
| 774 | final RectF src = new RectF(0, 0, icon.getWidth(), icon.getHeight()); |
| 775 | final RectF dst = new RectF(bitMapResult.left, bitMapResult.top, bitMapResult.right, |
| 776 | bitMapResult.bottom); |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 777 | drawIconOnCanvas(icon, canvas, src, dst); |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 780 | /** |
Andrew Sapperstein | c0e6023 | 2014-01-24 19:03:35 -0800 | [diff] [blame] | 781 | * Returns true if the avatar should be positioned at the right edge of the chip. |
| 782 | * Takes into account both the set avatar position (start or end) as well as whether |
| 783 | * the layout direction is LTR or RTL. |
| 784 | */ |
| 785 | private boolean shouldPositionAvatarOnRight() { |
| 786 | final boolean isRtl = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 ? |
| 787 | getLayoutDirection() == LAYOUT_DIRECTION_RTL : false; |
| 788 | final boolean assignedPosition = mAvatarPosition == AVATAR_POSITION_END; |
| 789 | // If in Rtl mode, the position should be flipped. |
| 790 | return isRtl ? !assignedPosition : assignedPosition; |
| 791 | } |
| 792 | |
| 793 | /** |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 794 | * Returns the avatar icon to use for this recipient entry. Returns null if we don't want to |
| 795 | * draw an icon for this recipient. |
| 796 | */ |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 797 | private void loadAvatarIcon(final RecipientEntry contact, |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 798 | final ChipBitmapContainer bitmapContainer) { |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 799 | // Don't draw photos for recipients that have been typed in OR generated on the fly. |
| 800 | long contactId = contact.getContactId(); |
| 801 | boolean drawPhotos = isPhoneQuery() ? |
| 802 | contactId != RecipientEntry.INVALID_CONTACT |
| 803 | : (contactId != RecipientEntry.INVALID_CONTACT |
Jin Cao | bf84bd8 | 2014-07-31 10:59:27 -0700 | [diff] [blame] | 804 | && contactId != RecipientEntry.GENERATED_CONTACT); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 805 | |
| 806 | if (drawPhotos) { |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 807 | final byte[] origPhotoBytes = contact.getPhotoBytes(); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 808 | // There may not be a photo yet if anything but the first contact address |
| 809 | // was selected. |
Jin Cao | 82d3885 | 2014-06-03 10:53:46 -0700 | [diff] [blame] | 810 | if (origPhotoBytes == null) { |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 811 | // TODO: cache this in the recipient entry? |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 812 | getAdapter().fetchPhoto(contact, new PhotoManager.PhotoManagerCallback() { |
Jin Cao | 52e4814 | 2014-06-04 18:01:20 -0700 | [diff] [blame] | 813 | @Override |
Andrew Sapperstein | 50429c5 | 2014-06-12 15:12:10 -0700 | [diff] [blame] | 814 | public void onPhotoBytesPopulated() { |
| 815 | // Call through to the async version which will ensure |
| 816 | // proper threading. |
| 817 | onPhotoBytesAsynchronouslyPopulated(); |
| 818 | } |
| 819 | |
| 820 | @Override |
Jin Cao | 52e4814 | 2014-06-04 18:01:20 -0700 | [diff] [blame] | 821 | public void onPhotoBytesAsynchronouslyPopulated() { |
| 822 | final byte[] loadedPhotoBytes = contact.getPhotoBytes(); |
| 823 | final Bitmap icon = BitmapFactory.decodeByteArray(loadedPhotoBytes, 0, |
| 824 | loadedPhotoBytes.length); |
| 825 | tryDrawAndInvalidate(icon); |
| 826 | } |
Jin Cao | 0efdc53 | 2014-06-04 15:35:16 -0700 | [diff] [blame] | 827 | |
Jin Cao | 52e4814 | 2014-06-04 18:01:20 -0700 | [diff] [blame] | 828 | @Override |
| 829 | public void onPhotoBytesAsyncLoadFailed() { |
| 830 | // TODO: can the scaled down default photo be cached? |
| 831 | tryDrawAndInvalidate(mDefaultContactPhoto); |
| 832 | } |
| 833 | |
| 834 | private void tryDrawAndInvalidate(Bitmap icon) { |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 835 | drawIcon(bitmapContainer, icon); |
Jin Cao | 52e4814 | 2014-06-04 18:01:20 -0700 | [diff] [blame] | 836 | // The caller might originated from a background task. However, if the |
| 837 | // background task has already completed, the view might be already drawn |
| 838 | // on the UI but the callback would happen on the background thread. |
| 839 | // So if we are on a background thread, post an invalidate call to the UI. |
| 840 | if (Looper.myLooper() == Looper.getMainLooper()) { |
| 841 | // The view might not redraw itself since it's loaded asynchronously |
Jin Cao | 0efdc53 | 2014-06-04 15:35:16 -0700 | [diff] [blame] | 842 | invalidate(); |
Jin Cao | 52e4814 | 2014-06-04 18:01:20 -0700 | [diff] [blame] | 843 | } else { |
| 844 | post(new Runnable() { |
| 845 | @Override |
| 846 | public void run() { |
| 847 | invalidate(); |
| 848 | } |
| 849 | }); |
Jin Cao | 0efdc53 | 2014-06-04 15:35:16 -0700 | [diff] [blame] | 850 | } |
Jin Cao | 52e4814 | 2014-06-04 18:01:20 -0700 | [diff] [blame] | 851 | } |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 852 | }); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 853 | } else { |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 854 | final Bitmap icon = BitmapFactory.decodeByteArray(origPhotoBytes, 0, |
| 855 | origPhotoBytes.length); |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 856 | drawIcon(bitmapContainer, icon); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 857 | } |
| 858 | } |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 862 | * Get the background drawable for a RecipientChip. |
| 863 | */ |
| 864 | // Visible for testing. |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 865 | /* package */Drawable getChipBackground(RecipientEntry contact) { |
| 866 | return contact.isValid() ? mChipBackground : mInvalidChipBackground; |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 869 | private int getDefaultChipBackgroundColor(RecipientEntry contact) { |
| 870 | return getResources().getColor(contact.isValid() ? R.color.chip_background : |
| 871 | R.color.chip_background_invalid); |
| 872 | } |
| 873 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 874 | /** |
| 875 | * Given a height, returns a Y offset that will draw the text in the middle of the height. |
| 876 | */ |
Scott Kennedy | 15259d5 | 2014-07-09 17:28:47 -0700 | [diff] [blame] | 877 | protected float getTextYOffset(int height) { |
| 878 | return height - ((height - mTextHeight) / 2); |
Mindy Pereira | 1d37d97 | 2011-08-17 10:37:59 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 881 | /** |
Scott Kennedy | b8985b7 | 2014-01-28 16:36:19 -0800 | [diff] [blame] | 882 | * Draws the icon onto the canvas given the source rectangle of the bitmap and the destination |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 883 | * rectangle of the canvas. |
| 884 | */ |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 885 | protected void drawIconOnCanvas(Bitmap icon, Canvas canvas, RectF src, RectF dst) { |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 886 | final Matrix matrix = new Matrix(); |
| 887 | |
| 888 | // Draw bitmap through shader first. |
| 889 | final BitmapShader shader = new BitmapShader(icon, TileMode.CLAMP, TileMode.CLAMP); |
| 890 | matrix.reset(); |
| 891 | |
| 892 | // Fit bitmap to bounds. |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 893 | matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL); |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 894 | |
| 895 | shader.setLocalMatrix(matrix); |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 896 | mWorkPaint.reset(); |
| 897 | mWorkPaint.setShader(shader); |
| 898 | mWorkPaint.setAntiAlias(true); |
| 899 | mWorkPaint.setFilterBitmap(true); |
| 900 | mWorkPaint.setDither(true); |
| 901 | canvas.drawCircle(dst.centerX(), dst.centerY(), dst.width() / 2f, mWorkPaint); |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 902 | |
| 903 | // Then draw the border. |
| 904 | final float borderWidth = 1f; |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 905 | mWorkPaint.reset(); |
| 906 | mWorkPaint.setColor(Color.TRANSPARENT); |
| 907 | mWorkPaint.setStyle(Style.STROKE); |
| 908 | mWorkPaint.setStrokeWidth(borderWidth); |
| 909 | mWorkPaint.setAntiAlias(true); |
| 910 | canvas.drawCircle(dst.centerX(), dst.centerY(), dst.width() / 2f - borderWidth / 2, mWorkPaint); |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 911 | |
Jin Cao | 809c595 | 2014-08-08 12:28:17 -0700 | [diff] [blame] | 912 | mWorkPaint.reset(); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 915 | private DrawableRecipientChip constructChipSpan(RecipientEntry contact, boolean pressed) { |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 916 | TextPaint paint = getPaint(); |
| 917 | float defaultSize = paint.getTextSize(); |
Mindy Pereira | e50b0a1 | 2011-06-15 16:16:38 -0700 | [diff] [blame] | 918 | int defaultColor = paint.getColor(); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 919 | |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 920 | Bitmap tmpBitmap; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 921 | if (pressed) { |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 922 | tmpBitmap = createSelectedChip(contact, paint); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 923 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 924 | } else { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 925 | tmpBitmap = createUnselectedChip(contact, paint); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 928 | // Pass the full text, un-ellipsized, to the chip. |
| 929 | Drawable result = new BitmapDrawable(getResources(), tmpBitmap); |
Mindy Pereira | 2bfe48e | 2011-06-03 11:44:43 -0700 | [diff] [blame] | 930 | result.setBounds(0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight()); |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 931 | VisibleRecipientChip recipientChip = |
| 932 | new VisibleRecipientChip(result, contact); |
| 933 | recipientChip.setExtraMargin(mLineSpacingExtra); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 934 | // Return text to the original size. |
| 935 | paint.setTextSize(defaultSize); |
Mindy Pereira | e50b0a1 | 2011-06-15 16:16:38 -0700 | [diff] [blame] | 936 | paint.setColor(defaultColor); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 937 | return recipientChip; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 940 | /** |
Mindy Pereira | 3421420 | 2011-06-09 20:05:32 -0700 | [diff] [blame] | 941 | * Calculate the bottom of the line the chip will be located on using: |
| 942 | * 1) which line the chip appears on |
Mindy Pereira | 21625f8 | 2011-06-16 18:10:58 -0700 | [diff] [blame] | 943 | * 2) the height of a chip |
| 944 | * 3) padding built into the edit text view |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 945 | */ |
Mindy Pereira | 81fd3d1 | 2011-07-06 14:40:50 -0700 | [diff] [blame] | 946 | private int calculateOffsetFromBottom(int line) { |
Mindy Pereira | 21625f8 | 2011-06-16 18:10:58 -0700 | [diff] [blame] | 947 | // Line offsets start at zero. |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 948 | int actualLine = getLineCount() - (line + 1); |
Mindy Pereira | 1d37d97 | 2011-08-17 10:37:59 -0700 | [diff] [blame] | 949 | return -((actualLine * ((int) mChipHeight) + getPaddingBottom()) + getPaddingTop()) |
| 950 | + getDropDownVerticalOffset(); |
Mindy Pereira | f621a60 | 2011-05-31 10:09:35 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 953 | /** |
Jin Cao | 6fc1fee | 2014-07-29 17:56:44 -0700 | [diff] [blame] | 954 | * Calculate the offset from bottom of the EditText to top of the provided line. |
| 955 | */ |
| 956 | private int calculateOffsetFromBottomToTop(int line) { |
| 957 | return -(int) ((mChipHeight + (2 * mLineSpacingExtra)) * (Math |
| 958 | .abs(getLineCount() - line)) + getPaddingBottom()); |
| 959 | } |
| 960 | |
| 961 | /** |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 962 | * Get the max amount of space a chip can take up. The formula takes into |
| 963 | * account the width of the EditTextView, any view padding, and padding |
| 964 | * that will be added to the chip. |
| 965 | */ |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 966 | private float calculateAvailableWidth() { |
Jin Cao | 58d541d | 2014-07-29 16:23:03 -0700 | [diff] [blame] | 967 | return getWidth() - getPaddingLeft() - getPaddingRight() - mChipTextStartPadding |
| 968 | - mChipTextEndPadding; |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Mindy Pereira | 52c441e | 2011-12-15 13:35:37 -0800 | [diff] [blame] | 971 | |
Mindy Pereira | 22faaa6 | 2011-12-16 14:30:34 -0800 | [diff] [blame] | 972 | private void setChipDimensions(Context context, AttributeSet attrs) { |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 973 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecipientEditTextView, 0, |
| 974 | 0); |
Mindy Pereira | 52c441e | 2011-12-15 13:35:37 -0800 | [diff] [blame] | 975 | Resources r = getContext().getResources(); |
mindyp | 8c474ec | 2012-12-14 10:58:44 -0800 | [diff] [blame] | 976 | |
Mindy Pereira | 22faaa6 | 2011-12-16 14:30:34 -0800 | [diff] [blame] | 977 | mChipBackground = a.getDrawable(R.styleable.RecipientEditTextView_chipBackground); |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 978 | mChipBackgroundPressed = a |
| 979 | .getDrawable(R.styleable.RecipientEditTextView_chipBackgroundPressed); |
Jin Cao | 33515e7 | 2014-08-05 21:13:27 -0700 | [diff] [blame] | 980 | mInvalidChipBackground = a |
| 981 | .getDrawable(R.styleable.RecipientEditTextView_invalidChipBackground); |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 982 | mChipDelete = a.getDrawable(R.styleable.RecipientEditTextView_chipDelete); |
| 983 | if (mChipDelete == null) { |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 984 | mChipDelete = r.getDrawable(R.drawable.ic_cancel_wht_24dp); |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 985 | } |
Jin Cao | 58d541d | 2014-07-29 16:23:03 -0700 | [diff] [blame] | 986 | mChipTextStartPadding = mChipTextEndPadding |
| 987 | = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipPadding, -1); |
| 988 | if (mChipTextStartPadding == -1) { |
| 989 | mChipTextStartPadding = mChipTextEndPadding = |
| 990 | (int) r.getDimension(R.dimen.chip_padding); |
| 991 | } |
| 992 | // xml-overrides for each individual padding |
| 993 | // TODO: add these to attr? |
| 994 | int overridePadding = (int) r.getDimension(R.dimen.chip_padding_start); |
| 995 | if (overridePadding >= 0) { |
| 996 | mChipTextStartPadding = overridePadding; |
| 997 | } |
| 998 | overridePadding = (int) r.getDimension(R.dimen.chip_padding_end); |
| 999 | if (overridePadding >= 0) { |
| 1000 | mChipTextEndPadding = overridePadding; |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 1001 | } |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 1002 | |
| 1003 | mDefaultContactPhoto = BitmapFactory.decodeResource(r, R.drawable.ic_contact_picture); |
| 1004 | |
Mindy Pereira | 52c441e | 2011-12-15 13:35:37 -0800 | [diff] [blame] | 1005 | mMoreItem = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.more_item, null); |
Mindy Pereira | 5da0234 | 2011-12-16 14:56:27 -0800 | [diff] [blame] | 1006 | |
| 1007 | mChipHeight = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipHeight, -1); |
| 1008 | if (mChipHeight == -1) { |
| 1009 | mChipHeight = r.getDimension(R.dimen.chip_height); |
| 1010 | } |
| 1011 | mChipFontSize = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipFontSize, -1); |
| 1012 | if (mChipFontSize == -1) { |
| 1013 | mChipFontSize = r.getDimension(R.dimen.chip_text_size); |
| 1014 | } |
Scott Kennedy | b232e5c | 2014-07-09 16:29:35 -0700 | [diff] [blame] | 1015 | mAvatarPosition = |
| 1016 | a.getInt(R.styleable.RecipientEditTextView_avatarPosition, AVATAR_POSITION_START); |
Scott Kennedy | b8985b7 | 2014-01-28 16:36:19 -0800 | [diff] [blame] | 1017 | mDisableDelete = a.getBoolean(R.styleable.RecipientEditTextView_disableDelete, false); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 1018 | |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1019 | mMaxLines = r.getInteger(R.integer.chips_max_lines); |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 1020 | mLineSpacingExtra = r.getDimensionPixelOffset(R.dimen.line_spacing_extra); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 1021 | |
Mindy Pereira | ef79800 | 2012-05-02 11:04:25 -0700 | [diff] [blame] | 1022 | a.recycle(); |
Mindy Pereira | 52c441e | 2011-12-15 13:35:37 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 1025 | // Visible for testing. |
| 1026 | /* package */ void setMoreItem(TextView moreItem) { |
| 1027 | mMoreItem = moreItem; |
| 1028 | } |
| 1029 | |
Mindy Pereira | 97cb259 | 2011-09-26 16:11:41 -0700 | [diff] [blame] | 1030 | |
| 1031 | // Visible for testing. |
| 1032 | /* package */ void setChipBackground(Drawable chipBackground) { |
| 1033 | mChipBackground = chipBackground; |
| 1034 | } |
| 1035 | |
| 1036 | // Visible for testing. |
| 1037 | /* package */ void setChipHeight(int height) { |
| 1038 | mChipHeight = height; |
| 1039 | } |
| 1040 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 1041 | public float getChipHeight() { |
| 1042 | return mChipHeight; |
| 1043 | } |
| 1044 | |
Mindy Pereira | 076d1f9 | 2011-08-08 11:53:12 -0700 | [diff] [blame] | 1045 | /** |
| 1046 | * Set whether to shrink the recipients field such that at most |
| 1047 | * one line of recipients chips are shown when the field loses |
| 1048 | * focus. By default, the number of displayed recipients will be |
| 1049 | * limited and a "more" chip will be shown when focus is lost. |
| 1050 | * @param shrink |
| 1051 | */ |
| 1052 | public void setOnFocusListShrinkRecipients(boolean shrink) { |
| 1053 | mShouldShrink = shrink; |
| 1054 | } |
| 1055 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1056 | @Override |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 1057 | public void onSizeChanged(int width, int height, int oldw, int oldh) { |
| 1058 | super.onSizeChanged(width, height, oldw, oldh); |
Mindy Pereira | db1ca65 | 2011-09-26 08:49:35 -0700 | [diff] [blame] | 1059 | if (width != 0 && height != 0) { |
| 1060 | if (mPendingChipsCount > 0) { |
| 1061 | postHandlePendingChips(); |
| 1062 | } else { |
| 1063 | checkChipWidths(); |
| 1064 | } |
Mindy Pereira | 8005f42 | 2011-07-20 13:16:53 -0700 | [diff] [blame] | 1065 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 1066 | // Try to find the scroll view parent, if it exists. |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 1067 | if (mScrollView == null && !mTriedGettingScrollView) { |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 1068 | ViewParent parent = getParent(); |
| 1069 | while (parent != null && !(parent instanceof ScrollView)) { |
| 1070 | parent = parent.getParent(); |
| 1071 | } |
| 1072 | if (parent != null) { |
| 1073 | mScrollView = (ScrollView) parent; |
| 1074 | } |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 1075 | mTriedGettingScrollView = true; |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 1076 | } |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1079 | private void postHandlePendingChips() { |
| 1080 | mHandler.removeCallbacks(mHandlePendingChips); |
| 1081 | mHandler.post(mHandlePendingChips); |
| 1082 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 1083 | |
Mindy Pereira | db1ca65 | 2011-09-26 08:49:35 -0700 | [diff] [blame] | 1084 | private void checkChipWidths() { |
| 1085 | // Check the widths of the associated chips. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1086 | DrawableRecipientChip[] chips = getSortedRecipients(); |
Mindy Pereira | db1ca65 | 2011-09-26 08:49:35 -0700 | [diff] [blame] | 1087 | if (chips != null) { |
| 1088 | Rect bounds; |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1089 | for (DrawableRecipientChip chip : chips) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1090 | bounds = chip.getBounds(); |
Kevin Lin | c61c13f | 2014-02-28 15:04:40 -0800 | [diff] [blame] | 1091 | if (getWidth() > 0 && bounds.right - bounds.left > |
| 1092 | getWidth() - getPaddingLeft() - getPaddingRight()) { |
Mindy Pereira | db1ca65 | 2011-09-26 08:49:35 -0700 | [diff] [blame] | 1093 | // Need to redraw that chip. |
| 1094 | replaceChip(chip, chip.getEntry()); |
| 1095 | } |
| 1096 | } |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1097 | } |
Mindy Pereira | db1ca65 | 2011-09-26 08:49:35 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1100 | // Visible for testing. |
| 1101 | /*package*/ void handlePendingChips() { |
| 1102 | if (getViewWidth() <= 0) { |
Mindy Pereira | 8005f42 | 2011-07-20 13:16:53 -0700 | [diff] [blame] | 1103 | // The widget has not been sized yet. |
| 1104 | // This will be called as a result of onSizeChanged |
| 1105 | // at a later point. |
| 1106 | return; |
| 1107 | } |
Mindy Pereira | db1ca65 | 2011-09-26 08:49:35 -0700 | [diff] [blame] | 1108 | if (mPendingChipsCount <= 0) { |
| 1109 | return; |
| 1110 | } |
| 1111 | |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1112 | synchronized (mPendingChips) { |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1113 | Editable editable = getText(); |
| 1114 | // Tokenize! |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1115 | if (mPendingChipsCount <= MAX_CHIPS_PARSED) { |
| 1116 | for (int i = 0; i < mPendingChips.size(); i++) { |
| 1117 | String current = mPendingChips.get(i); |
| 1118 | int tokenStart = editable.toString().indexOf(current); |
mindyp | 488718e | 2012-12-18 11:48:07 -0800 | [diff] [blame] | 1119 | // Always leave a space at the end between tokens. |
| 1120 | int tokenEnd = tokenStart + current.length() - 1; |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1121 | if (tokenStart >= 0) { |
| 1122 | // When we have a valid token, include it with the token |
| 1123 | // to the left. |
| 1124 | if (tokenEnd < editable.length() - 2 |
| 1125 | && editable.charAt(tokenEnd) == COMMIT_CHAR_COMMA) { |
| 1126 | tokenEnd++; |
| 1127 | } |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1128 | createReplacementChip(tokenStart, tokenEnd, editable, i < CHIP_LIMIT |
| 1129 | || !mShouldShrink); |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1130 | } |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1131 | mPendingChipsCount--; |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1132 | } |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1133 | sanitizeEnd(); |
| 1134 | } else { |
| 1135 | mNoChips = true; |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1136 | } |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1137 | |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 1138 | if (mTemporaryRecipients != null && mTemporaryRecipients.size() > 0 |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1139 | && mTemporaryRecipients.size() <= RecipientAlternatesAdapter.MAX_LOOKUPS) { |
| 1140 | if (hasFocus() || mTemporaryRecipients.size() < CHIP_LIMIT) { |
| 1141 | new RecipientReplacementTask().execute(); |
| 1142 | mTemporaryRecipients = null; |
| 1143 | } else { |
| 1144 | // Create the "more" chip |
| 1145 | mIndividualReplacements = new IndividualReplacementTask(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1146 | mIndividualReplacements.execute(new ArrayList<DrawableRecipientChip>( |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1147 | mTemporaryRecipients.subList(0, CHIP_LIMIT))); |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1148 | if (mTemporaryRecipients.size() > CHIP_LIMIT) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1149 | mTemporaryRecipients = new ArrayList<DrawableRecipientChip>( |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1150 | mTemporaryRecipients.subList(CHIP_LIMIT, |
| 1151 | mTemporaryRecipients.size())); |
| 1152 | } else { |
| 1153 | mTemporaryRecipients = null; |
| 1154 | } |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1155 | createMoreChip(); |
| 1156 | } |
| 1157 | } else { |
| 1158 | // There are too many recipients to look up, so just fall back |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 1159 | // to showing addresses for all of them. |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1160 | mTemporaryRecipients = null; |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 1161 | createMoreChip(); |
| 1162 | } |
Mindy Pereira | 2cac231 | 2011-07-20 11:38:59 -0700 | [diff] [blame] | 1163 | mPendingChipsCount = 0; |
| 1164 | mPendingChips.clear(); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1168 | // Visible for testing. |
| 1169 | /*package*/ int getViewWidth() { |
| 1170 | return getWidth(); |
| 1171 | } |
| 1172 | |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1173 | /** |
| 1174 | * Remove any characters after the last valid chip. |
| 1175 | */ |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 1176 | // Visible for testing. |
| 1177 | /*package*/ void sanitizeEnd() { |
Mindy Pereira | 3c42baf | 2012-01-10 13:20:17 -0800 | [diff] [blame] | 1178 | // Don't sanitize while we are waiting for pending chips to complete. |
| 1179 | if (mPendingChipsCount > 0) { |
| 1180 | return; |
| 1181 | } |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1182 | // Find the last chip; eliminate any commit characters after it. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1183 | DrawableRecipientChip[] chips = getSortedRecipients(); |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1184 | Spannable spannable = getSpannable(); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1185 | if (chips != null && chips.length > 0) { |
| 1186 | int end; |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 1187 | mMoreChip = getMoreChip(); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1188 | if (mMoreChip != null) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1189 | end = spannable.getSpanEnd(mMoreChip); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1190 | } else { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1191 | end = getSpannable().getSpanEnd(getLastChip()); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1192 | } |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1193 | Editable editable = getText(); |
| 1194 | int length = editable.length(); |
| 1195 | if (length > end) { |
| 1196 | // See what characters occur after that and eliminate them. |
| 1197 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1198 | Log.d(TAG, "There were extra characters after the last tokenizable entry." |
| 1199 | + editable); |
| 1200 | } |
| 1201 | editable.delete(end + 1, length); |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * Create a chip that represents just the email address of a recipient. At some later |
| 1208 | * point, this chip will be attached to a real contact entry, if one exists. |
| 1209 | */ |
Scott Kennedy | 20c8aa7 | 2013-03-06 22:23:59 -0800 | [diff] [blame] | 1210 | // VisibleForTesting |
| 1211 | void createReplacementChip(int tokenStart, int tokenEnd, Editable editable, |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1212 | boolean visible) { |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 1213 | if (alreadyHasChip(tokenStart, tokenEnd)) { |
| 1214 | // There is already a chip present at this location. |
| 1215 | // Don't recreate it. |
| 1216 | return; |
| 1217 | } |
Mindy Pereira | 7ebb40f | 2011-07-06 17:21:14 -0700 | [diff] [blame] | 1218 | String token = editable.toString().substring(tokenStart, tokenEnd); |
Scott Kennedy | 20c8aa7 | 2013-03-06 22:23:59 -0800 | [diff] [blame] | 1219 | final String trimmedToken = token.trim(); |
| 1220 | int commitCharIndex = trimmedToken.lastIndexOf(COMMIT_CHAR_COMMA); |
Tom Taylor | 41137c2 | 2013-03-26 12:22:56 -0700 | [diff] [blame] | 1221 | if (commitCharIndex != -1 && commitCharIndex == trimmedToken.length() - 1) { |
Scott Kennedy | 20c8aa7 | 2013-03-06 22:23:59 -0800 | [diff] [blame] | 1222 | token = trimmedToken.substring(0, trimmedToken.length() - 1); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1223 | } |
| 1224 | RecipientEntry entry = createTokenizedEntry(token); |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1225 | if (entry != null) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1226 | DrawableRecipientChip chip = null; |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1227 | try { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1228 | if (!mNoChips) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1229 | chip = visible ? |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 1230 | constructChipSpan(entry, false) : new InvisibleRecipientChip(entry); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1231 | } |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1232 | } catch (NullPointerException e) { |
| 1233 | Log.e(TAG, e.getMessage(), e); |
| 1234 | } |
mindyp | cb76b4d | 2012-11-09 14:05:19 -0800 | [diff] [blame] | 1235 | editable.setSpan(chip, tokenStart, tokenEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1236 | // Add this chip to the list of entries "to replace" |
| 1237 | if (chip != null) { |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 1238 | if (mTemporaryRecipients == null) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1239 | mTemporaryRecipients = new ArrayList<DrawableRecipientChip>(); |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 1240 | } |
Scott Kennedy | 0613ff8 | 2013-03-05 14:02:46 -0800 | [diff] [blame] | 1241 | chip.setOriginalText(token); |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1242 | mTemporaryRecipients.add(chip); |
| 1243 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 1244 | } |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Tom Taylor | abff606 | 2012-04-17 09:04:56 -0700 | [diff] [blame] | 1247 | private static boolean isPhoneNumber(String number) { |
| 1248 | // TODO: replace this function with libphonenumber's isPossibleNumber (see |
| 1249 | // PhoneNumberUtil). One complication is that it requires the sender's region which |
| 1250 | // comes from the CurrentCountryIso. For now, let's just do this simple match. |
| 1251 | if (TextUtils.isEmpty(number)) { |
| 1252 | return false; |
| 1253 | } |
| 1254 | |
Tom Taylor | 4491a06 | 2012-11-15 13:57:41 -0800 | [diff] [blame] | 1255 | Matcher match = PHONE_PATTERN.matcher(number); |
Tom Taylor | abff606 | 2012-04-17 09:04:56 -0700 | [diff] [blame] | 1256 | return match.matches(); |
| 1257 | } |
| 1258 | |
Scott Kennedy | 983c99b | 2013-03-22 09:42:03 -0700 | [diff] [blame] | 1259 | // VisibleForTesting |
| 1260 | RecipientEntry createTokenizedEntry(final String token) { |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1261 | if (TextUtils.isEmpty(token)) { |
| 1262 | return null; |
| 1263 | } |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1264 | if (isPhoneQuery() && isPhoneNumber(token)) { |
Tom Taylor | 0ba9133 | 2013-03-21 10:28:50 -0700 | [diff] [blame] | 1265 | return RecipientEntry.constructFakePhoneEntry(token, true); |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1266 | } |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1267 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(token); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1268 | String display = null; |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1269 | boolean isValid = isValid(token); |
| 1270 | if (isValid && tokens != null && tokens.length > 0) { |
Mindy Pereira | ee58f49 | 2011-08-08 13:16:46 -0700 | [diff] [blame] | 1271 | // If we can get a name from tokenizing, then generate an entry from |
| 1272 | // this. |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1273 | display = tokens[0].getName(); |
| 1274 | if (!TextUtils.isEmpty(display)) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1275 | return RecipientEntry.constructGeneratedEntry(display, tokens[0].getAddress(), |
| 1276 | isValid); |
Mindy Pereira | 401cd96 | 2011-08-09 11:29:58 -0700 | [diff] [blame] | 1277 | } else { |
| 1278 | display = tokens[0].getAddress(); |
| 1279 | if (!TextUtils.isEmpty(display)) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1280 | return RecipientEntry.constructFakeEntry(display, isValid); |
Mindy Pereira | 401cd96 | 2011-08-09 11:29:58 -0700 | [diff] [blame] | 1281 | } |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1282 | } |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1283 | } |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 1284 | // Unable to validate the token or to create a valid token from it. |
| 1285 | // Just create a chip the user can edit. |
Mindy Pereira | dbb8560 | 2011-09-16 14:02:14 -0700 | [diff] [blame] | 1286 | String validatedToken = null; |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1287 | if (mValidator != null && !isValid) { |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 1288 | // Try fixing up the entry using the validator. |
Mindy Pereira | dbb8560 | 2011-09-16 14:02:14 -0700 | [diff] [blame] | 1289 | validatedToken = mValidator.fixText(token).toString(); |
| 1290 | if (!TextUtils.isEmpty(validatedToken)) { |
| 1291 | if (validatedToken.contains(token)) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1292 | // protect against the case of a validator with a null |
| 1293 | // domain, |
Mindy Pereira | dbb8560 | 2011-09-16 14:02:14 -0700 | [diff] [blame] | 1294 | // which doesn't add a domain to the token |
| 1295 | Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(validatedToken); |
| 1296 | if (tokenized.length > 0) { |
| 1297 | validatedToken = tokenized[0].getAddress(); |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1298 | isValid = true; |
Mindy Pereira | dbb8560 | 2011-09-16 14:02:14 -0700 | [diff] [blame] | 1299 | } |
| 1300 | } else { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1301 | // We ran into a case where the token was invalid and |
| 1302 | // removed |
| 1303 | // by the validator. In this case, just use the original |
| 1304 | // token |
Mindy Pereira | dbb8560 | 2011-09-16 14:02:14 -0700 | [diff] [blame] | 1305 | // and let the user sort out the error chip. |
| 1306 | validatedToken = null; |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1307 | isValid = false; |
Erik | 184d377 | 2011-08-04 16:07:00 -0700 | [diff] [blame] | 1308 | } |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1309 | } |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 1310 | } |
Mindy Pereira | ee58f49 | 2011-08-08 13:16:46 -0700 | [diff] [blame] | 1311 | // Otherwise, fallback to just creating an editable email address chip. |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1312 | return RecipientEntry.constructFakeEntry( |
| 1313 | !TextUtils.isEmpty(validatedToken) ? validatedToken : token, isValid); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 1316 | private boolean isValid(String text) { |
| 1317 | return mValidator == null ? true : mValidator.isValid(text); |
| 1318 | } |
| 1319 | |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 1320 | private static String tokenizeAddress(String destination) { |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1321 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(destination); |
| 1322 | if (tokens != null && tokens.length > 0) { |
| 1323 | return tokens[0].getAddress(); |
| 1324 | } |
| 1325 | return destination; |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Mindy Pereira | 02a1f5f | 2011-06-10 11:03:07 -0700 | [diff] [blame] | 1328 | @Override |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1329 | public void setTokenizer(Tokenizer tokenizer) { |
| 1330 | mTokenizer = tokenizer; |
| 1331 | super.setTokenizer(mTokenizer); |
| 1332 | } |
| 1333 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1334 | @Override |
| 1335 | public void setValidator(Validator validator) { |
| 1336 | mValidator = validator; |
| 1337 | super.setValidator(validator); |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * We cannot use the default mechanism for replaceText. Instead, |
| 1342 | * we override onItemClickListener so we can get all the associated |
| 1343 | * contact information including display text, address, and id. |
| 1344 | */ |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1345 | @Override |
| 1346 | protected void replaceText(CharSequence text) { |
| 1347 | return; |
| 1348 | } |
| 1349 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1350 | /** |
| 1351 | * Dismiss any selected chips when the back key is pressed. |
| 1352 | */ |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1353 | @Override |
Mindy Pereira | 4c5fc2c | 2011-06-07 10:02:30 -0700 | [diff] [blame] | 1354 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 1355 | if (keyCode == KeyEvent.KEYCODE_BACK && mSelectedChip != null) { |
Mindy Pereira | 4c5fc2c | 2011-06-07 10:02:30 -0700 | [diff] [blame] | 1356 | clearSelectedChip(); |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 1357 | return true; |
Mindy Pereira | 4c5fc2c | 2011-06-07 10:02:30 -0700 | [diff] [blame] | 1358 | } |
| 1359 | return super.onKeyPreIme(keyCode, event); |
| 1360 | } |
| 1361 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1362 | /** |
| 1363 | * Monitor key presses in this view to see if the user types |
| 1364 | * any commit keys, which consist of ENTER, TAB, or DPAD_CENTER. |
| 1365 | * If the user has entered text that has contact matches and types |
| 1366 | * a commit key, create a chip from the topmost matching contact. |
| 1367 | * If the user has entered text that has no contact matches and types |
| 1368 | * a commit key, then create a chip from the text they have entered. |
| 1369 | */ |
Mindy Pereira | 4c5fc2c | 2011-06-07 10:02:30 -0700 | [diff] [blame] | 1370 | @Override |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1371 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 1372 | switch (keyCode) { |
Mindy Pereira | 0f0df29 | 2011-06-16 20:49:51 -0700 | [diff] [blame] | 1373 | case KeyEvent.KEYCODE_TAB: |
| 1374 | if (event.hasNoModifiers()) { |
| 1375 | if (mSelectedChip != null) { |
| 1376 | clearSelectedChip(); |
| 1377 | } else { |
| 1378 | commitDefault(); |
| 1379 | } |
Mindy Pereira | 0f0df29 | 2011-06-16 20:49:51 -0700 | [diff] [blame] | 1380 | } |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 1381 | break; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1382 | } |
| 1383 | return super.onKeyUp(keyCode, event); |
| 1384 | } |
| 1385 | |
Mindy Pereira | 0f0df29 | 2011-06-16 20:49:51 -0700 | [diff] [blame] | 1386 | private boolean focusNext() { |
| 1387 | View next = focusSearch(View.FOCUS_DOWN); |
| 1388 | if (next != null) { |
| 1389 | next.requestFocus(); |
| 1390 | return true; |
| 1391 | } |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
Mindy Pereira | 3421420 | 2011-06-09 20:05:32 -0700 | [diff] [blame] | 1395 | /** |
| 1396 | * Create a chip from the default selection. If the popup is showing, the |
Scott Kennedy | e4945e9 | 2014-04-21 20:17:43 -0700 | [diff] [blame] | 1397 | * default is the selected item (if one is selected), or the first item, in the popup |
| 1398 | * suggestions list. Otherwise, it is whatever the user had typed in. End represents where the |
| 1399 | * tokenizer should search for a token to turn into a chip. |
Mindy Pereira | 3421420 | 2011-06-09 20:05:32 -0700 | [diff] [blame] | 1400 | * @return If a chip was created from a real contact. |
| 1401 | */ |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1402 | private boolean commitDefault() { |
Mindy Pereira | 60d96dc | 2012-04-27 09:15:48 -0700 | [diff] [blame] | 1403 | // If there is no tokenizer, don't try to commit. |
| 1404 | if (mTokenizer == null) { |
| 1405 | return false; |
| 1406 | } |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 1407 | Editable editable = getText(); |
Mindy Pereira | 3421420 | 2011-06-09 20:05:32 -0700 | [diff] [blame] | 1408 | int end = getSelectionEnd(); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 1409 | int start = mTokenizer.findTokenStart(editable, end); |
Mindy Pereira | 2b4ffc5 | 2011-06-20 13:20:09 -0700 | [diff] [blame] | 1410 | |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1411 | if (shouldCreateChip(start, end)) { |
| 1412 | int whatEnd = mTokenizer.findTokenEnd(getText(), start); |
| 1413 | // In the middle of chip; treat this as an edit |
| 1414 | // and commit the whole token. |
mindyp | 4f82d88 | 2012-11-08 16:30:27 -0800 | [diff] [blame] | 1415 | whatEnd = movePastTerminators(whatEnd); |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1416 | if (whatEnd != getSelectionEnd()) { |
| 1417 | handleEdit(start, whatEnd); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 1418 | return true; |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1419 | } |
| 1420 | return commitChip(start, end , editable); |
| 1421 | } |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
| 1425 | private void commitByCharacter() { |
Mindy Pereira | 60d96dc | 2012-04-27 09:15:48 -0700 | [diff] [blame] | 1426 | // We can't possibly commit by character if we can't tokenize. |
| 1427 | if (mTokenizer == null) { |
| 1428 | return; |
| 1429 | } |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1430 | Editable editable = getText(); |
| 1431 | int end = getSelectionEnd(); |
| 1432 | int start = mTokenizer.findTokenStart(editable, end); |
| 1433 | if (shouldCreateChip(start, end)) { |
| 1434 | commitChip(start, end, editable); |
| 1435 | } |
Mindy Pereira | ab5a964 | 2011-06-23 17:23:22 -0700 | [diff] [blame] | 1436 | setSelection(getText().length()); |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | private boolean commitChip(int start, int end, Editable editable) { |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 1440 | ListAdapter adapter = getAdapter(); |
| 1441 | if (adapter != null && adapter.getCount() > 0 && enoughToFilter() |
Tom Taylor | c7a87f0 | 2012-05-03 15:21:08 -0700 | [diff] [blame] | 1442 | && end == getSelectionEnd() && !isPhoneQuery()) { |
Scott Kennedy | e4945e9 | 2014-04-21 20:17:43 -0700 | [diff] [blame] | 1443 | // let's choose the selected or first entry if only the input text is NOT an email |
| 1444 | // address so we won't try to replace the user's potentially correct but |
| 1445 | // new/unencountered email input |
Jin Cao | b3b248a | 2014-04-11 10:43:50 -0700 | [diff] [blame] | 1446 | if (!isValidEmailAddress(editable.toString().substring(start, end).trim())) { |
Scott Kennedy | e4945e9 | 2014-04-21 20:17:43 -0700 | [diff] [blame] | 1447 | final int selectedPosition = getListSelection(); |
| 1448 | if (selectedPosition == -1) { |
| 1449 | // Nothing is selected; use the first item |
| 1450 | submitItemAtPosition(0); |
| 1451 | } else { |
| 1452 | submitItemAtPosition(selectedPosition); |
| 1453 | } |
Jin Cao | b3b248a | 2014-04-11 10:43:50 -0700 | [diff] [blame] | 1454 | } |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1455 | dismissDropDown(); |
| 1456 | return true; |
| 1457 | } else { |
| 1458 | int tokenEnd = mTokenizer.findTokenEnd(editable, start); |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 1459 | if (editable.length() > tokenEnd + 1) { |
| 1460 | char charAt = editable.charAt(tokenEnd + 1); |
| 1461 | if (charAt == COMMIT_CHAR_COMMA || charAt == COMMIT_CHAR_SEMICOLON) { |
| 1462 | tokenEnd++; |
| 1463 | } |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 1464 | } |
Mindy Pereira | 62397a5 | 2011-10-03 13:46:43 -0700 | [diff] [blame] | 1465 | String text = editable.toString().substring(start, tokenEnd).trim(); |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1466 | clearComposingText(); |
| 1467 | if (text != null && text.length() > 0 && !text.equals(" ")) { |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1468 | RecipientEntry entry = createTokenizedEntry(text); |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1469 | if (entry != null) { |
| 1470 | QwertyKeyListener.markAsReplaced(editable, start, end, ""); |
| 1471 | CharSequence chipText = createChip(entry, false); |
Mindy Pereira | ee6d83f | 2011-10-03 14:25:07 -0700 | [diff] [blame] | 1472 | if (chipText != null && start > -1 && end > -1) { |
Mindy Pereira | f177bda | 2011-09-15 11:23:49 -0700 | [diff] [blame] | 1473 | editable.replace(start, end, chipText); |
| 1474 | } |
Mindy Pereira | ba5642a | 2011-07-28 13:04:33 -0700 | [diff] [blame] | 1475 | } |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 1476 | // Only dismiss the dropdown if it is related to the text we |
| 1477 | // just committed. |
| 1478 | // For paste, it may not be as there are possibly multiple |
| 1479 | // tokens being added. |
| 1480 | if (end == getSelectionEnd()) { |
| 1481 | dismissDropDown(); |
| 1482 | } |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1483 | sanitizeBetween(); |
Mindy Pereira | 0f0df29 | 2011-06-16 20:49:51 -0700 | [diff] [blame] | 1484 | return true; |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | return false; |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Mindy Pereira | 01162ce | 2011-09-20 12:21:55 -0700 | [diff] [blame] | 1490 | // Visible for testing. |
| 1491 | /* package */ void sanitizeBetween() { |
Mindy Pereira | 3c42baf | 2012-01-10 13:20:17 -0800 | [diff] [blame] | 1492 | // Don't sanitize while we are waiting for content to chipify. |
| 1493 | if (mPendingChipsCount > 0) { |
| 1494 | return; |
| 1495 | } |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1496 | // Find the last chip. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1497 | DrawableRecipientChip[] recips = getSortedRecipients(); |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1498 | if (recips != null && recips.length > 0) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1499 | DrawableRecipientChip last = recips[recips.length - 1]; |
| 1500 | DrawableRecipientChip beforeLast = null; |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1501 | if (recips.length > 1) { |
| 1502 | beforeLast = recips[recips.length - 2]; |
| 1503 | } |
| 1504 | int startLooking = 0; |
| 1505 | int end = getSpannable().getSpanStart(last); |
| 1506 | if (beforeLast != null) { |
| 1507 | startLooking = getSpannable().getSpanEnd(beforeLast); |
Mindy Pereira | 399bda8 | 2011-09-20 11:18:26 -0700 | [diff] [blame] | 1508 | Editable text = getText(); |
Mindy Pereira | 01162ce | 2011-09-20 12:21:55 -0700 | [diff] [blame] | 1509 | if (startLooking == -1 || startLooking > text.length() - 1) { |
Mindy Pereira | 399bda8 | 2011-09-20 11:18:26 -0700 | [diff] [blame] | 1510 | // There is nothing after this chip. |
| 1511 | return; |
| 1512 | } |
| 1513 | if (text.charAt(startLooking) == ' ') { |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1514 | startLooking++; |
| 1515 | } |
| 1516 | } |
Mindy Pereira | e13775c | 2011-11-17 11:14:12 -0800 | [diff] [blame] | 1517 | if (startLooking >= 0 && end >= 0 && startLooking < end) { |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1518 | getText().delete(startLooking, end); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1523 | private boolean shouldCreateChip(int start, int end) { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1524 | return !mNoChips && hasFocus() && enoughToFilter() && !alreadyHasChip(start, end); |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | private boolean alreadyHasChip(int start, int end) { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1528 | if (mNoChips) { |
| 1529 | return true; |
| 1530 | } |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1531 | DrawableRecipientChip[] chips = |
| 1532 | getSpannable().getSpans(start, end, DrawableRecipientChip.class); |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 1533 | if ((chips == null || chips.length == 0)) { |
| 1534 | return false; |
Mindy Pereira | 64af2da | 2011-06-15 11:14:07 -0700 | [diff] [blame] | 1535 | } |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 1536 | return true; |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | private void handleEdit(int start, int end) { |
Mindy Pereira | a49e7fa | 2011-09-13 08:01:36 -0700 | [diff] [blame] | 1540 | if (start == -1 || end == -1) { |
| 1541 | // This chip no longer exists in the field. |
| 1542 | dismissDropDown(); |
| 1543 | return; |
| 1544 | } |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 1545 | // This is in the middle of a chip, so select out the whole chip |
| 1546 | // and commit it. |
| 1547 | Editable editable = getText(); |
| 1548 | setSelection(end); |
| 1549 | String text = getText().toString().substring(start, end); |
Mindy Pereira | fe52b97 | 2011-09-16 11:55:57 -0700 | [diff] [blame] | 1550 | if (!TextUtils.isEmpty(text)) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1551 | RecipientEntry entry = RecipientEntry.constructFakeEntry(text, isValid(text)); |
Mindy Pereira | fe52b97 | 2011-09-16 11:55:57 -0700 | [diff] [blame] | 1552 | QwertyKeyListener.markAsReplaced(editable, start, end, ""); |
| 1553 | CharSequence chipText = createChip(entry, false); |
Mindy Pereira | 5cfd6fe | 2011-10-20 15:36:24 -0700 | [diff] [blame] | 1554 | int selEnd = getSelectionEnd(); |
| 1555 | if (chipText != null && start > -1 && selEnd > -1) { |
| 1556 | editable.replace(start, selEnd, chipText); |
Mindy Pereira | 2d7709d | 2011-10-05 13:24:43 -0700 | [diff] [blame] | 1557 | } |
Mindy Pereira | fe52b97 | 2011-09-16 11:55:57 -0700 | [diff] [blame] | 1558 | } |
Mindy Pereira | ab5a964 | 2011-06-23 17:23:22 -0700 | [diff] [blame] | 1559 | dismissDropDown(); |
Mindy Pereira | 64af2da | 2011-06-15 11:14:07 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1562 | /** |
| 1563 | * If there is a selected chip, delegate the key events |
| 1564 | * to the selected chip. |
| 1565 | */ |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1566 | @Override |
| 1567 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1568 | if (mSelectedChip != null && keyCode == KeyEvent.KEYCODE_DEL) { |
| 1569 | if (mAlternatesPopup != null && mAlternatesPopup.isShowing()) { |
| 1570 | mAlternatesPopup.dismiss(); |
| 1571 | } |
| 1572 | removeChip(mSelectedChip); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Scott Kennedy | 2fe0cff | 2013-02-14 18:39:37 -0800 | [diff] [blame] | 1575 | switch (keyCode) { |
| 1576 | case KeyEvent.KEYCODE_ENTER: |
| 1577 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 1578 | if (event.hasNoModifiers()) { |
| 1579 | if (commitDefault()) { |
| 1580 | return true; |
| 1581 | } |
| 1582 | if (mSelectedChip != null) { |
| 1583 | clearSelectedChip(); |
| 1584 | return true; |
| 1585 | } else if (focusNext()) { |
| 1586 | return true; |
| 1587 | } |
| 1588 | } |
| 1589 | break; |
| 1590 | } |
| 1591 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1592 | return super.onKeyDown(keyCode, event); |
| 1593 | } |
| 1594 | |
Mindy Pereira | 01162ce | 2011-09-20 12:21:55 -0700 | [diff] [blame] | 1595 | // Visible for testing. |
| 1596 | /* package */ Spannable getSpannable() { |
Erik | e33555f | 2011-06-21 14:20:20 -0700 | [diff] [blame] | 1597 | return getText(); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1600 | private int getChipStart(DrawableRecipientChip chip) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1601 | return getSpannable().getSpanStart(chip); |
| 1602 | } |
| 1603 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1604 | private int getChipEnd(DrawableRecipientChip chip) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1605 | return getSpannable().getSpanEnd(chip); |
| 1606 | } |
| 1607 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1608 | /** |
| 1609 | * Instead of filtering on the entire contents of the edit box, |
| 1610 | * this subclass method filters on the range from |
| 1611 | * {@link Tokenizer#findTokenStart} to {@link #getSelectionEnd} |
| 1612 | * if the length of that range meets or exceeds {@link #getThreshold} |
| 1613 | * and makes sure that the range is not already a Chip. |
| 1614 | */ |
| 1615 | @Override |
| 1616 | protected void performFiltering(CharSequence text, int keyCode) { |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 1617 | boolean isCompletedToken = isCompletedToken(text); |
| 1618 | if (enoughToFilter() && !isCompletedToken) { |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1619 | int end = getSelectionEnd(); |
| 1620 | int start = mTokenizer.findTokenStart(text, end); |
| 1621 | // If this is a RecipientChip, don't filter |
| 1622 | // on its contents. |
| 1623 | Spannable span = getSpannable(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1624 | DrawableRecipientChip[] chips = span.getSpans(start, end, DrawableRecipientChip.class); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1625 | if (chips != null && chips.length > 0) { |
Kevin Lin | 41e93fb | 2014-02-03 17:35:29 -0800 | [diff] [blame] | 1626 | dismissDropDown(); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1627 | return; |
| 1628 | } |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 1629 | } else if (isCompletedToken) { |
Kevin Lin | 41e93fb | 2014-02-03 17:35:29 -0800 | [diff] [blame] | 1630 | dismissDropDown(); |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 1631 | return; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1632 | } |
| 1633 | super.performFiltering(text, keyCode); |
| 1634 | } |
| 1635 | |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 1636 | // Visible for testing. |
| 1637 | /*package*/ boolean isCompletedToken(CharSequence text) { |
| 1638 | if (TextUtils.isEmpty(text)) { |
| 1639 | return false; |
| 1640 | } |
| 1641 | // Check to see if this is a completed token before filtering. |
| 1642 | int end = text.length(); |
| 1643 | int start = mTokenizer.findTokenStart(text, end); |
| 1644 | String token = text.toString().substring(start, end).trim(); |
| 1645 | if (!TextUtils.isEmpty(token)) { |
| 1646 | char atEnd = token.charAt(token.length() - 1); |
| 1647 | return atEnd == COMMIT_CHAR_COMMA || atEnd == COMMIT_CHAR_SEMICOLON; |
| 1648 | } |
| 1649 | return false; |
| 1650 | } |
| 1651 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1652 | private void clearSelectedChip() { |
| 1653 | if (mSelectedChip != null) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1654 | unselectChip(mSelectedChip); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1655 | mSelectedChip = null; |
| 1656 | } |
Mindy Pereira | b86dcd5 | 2011-06-02 13:52:30 -0700 | [diff] [blame] | 1657 | setCursorVisible(true); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1660 | /** |
| 1661 | * Monitor touch events in the RecipientEditTextView. |
| 1662 | * If the view does not have focus, any tap on the view |
| 1663 | * will just focus the view. If the view has focus, determine |
| 1664 | * if the touch target is a recipient chip. If it is and the chip |
| 1665 | * is not selected, select it and clear any other selected chips. |
| 1666 | * If it isn't, then select that chip. |
| 1667 | */ |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1668 | @Override |
| 1669 | public boolean onTouchEvent(MotionEvent event) { |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 1670 | if (!isFocused()) { |
| 1671 | // Ignore any chip taps until this view is focused. |
| 1672 | return super.onTouchEvent(event); |
| 1673 | } |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1674 | boolean handled = super.onTouchEvent(event); |
Mindy Pereira | 05dbd33 | 2011-06-03 09:37:44 -0700 | [diff] [blame] | 1675 | int action = event.getAction(); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1676 | boolean chipWasSelected = false; |
Mindy Pereira | 2e90590 | 2011-09-08 16:17:04 -0700 | [diff] [blame] | 1677 | if (mSelectedChip == null) { |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 1678 | mGestureDetector.onTouchEvent(event); |
| 1679 | } |
Mindy Pereira | bfedc1e | 2011-09-07 12:17:12 -0700 | [diff] [blame] | 1680 | if (mCopyAddress == null && action == MotionEvent.ACTION_UP) { |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1681 | float x = event.getX(); |
| 1682 | float y = event.getY(); |
Scott Kennedy | 1650ef6 | 2013-08-02 21:56:47 -0700 | [diff] [blame] | 1683 | int offset = putOffsetInRange(x, y); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1684 | DrawableRecipientChip currentChip = findChip(offset); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1685 | if (currentChip != null) { |
| 1686 | if (action == MotionEvent.ACTION_UP) { |
| 1687 | if (mSelectedChip != null && mSelectedChip != currentChip) { |
| 1688 | clearSelectedChip(); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1689 | mSelectedChip = selectChip(currentChip); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1690 | } else if (mSelectedChip == null) { |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1691 | setSelection(getText().length()); |
| 1692 | commitDefault(); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1693 | mSelectedChip = selectChip(currentChip); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1694 | } else { |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 1695 | onClick(mSelectedChip); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1696 | } |
| 1697 | } |
| 1698 | chipWasSelected = true; |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 1699 | handled = true; |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1700 | } else if (mSelectedChip != null && shouldShowEditableText(mSelectedChip)) { |
Mindy Pereira | c0a34ab | 2011-09-13 15:17:39 -0700 | [diff] [blame] | 1701 | chipWasSelected = true; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | if (action == MotionEvent.ACTION_UP && !chipWasSelected) { |
| 1705 | clearSelectedChip(); |
| 1706 | } |
| 1707 | return handled; |
| 1708 | } |
| 1709 | |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 1710 | private void scrollLineIntoView(int line) { |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 1711 | if (mScrollView != null) { |
mindyp | 093ecdd | 2012-12-14 10:40:51 -0800 | [diff] [blame] | 1712 | mScrollView.smoothScrollBy(0, calculateOffsetFromBottom(line)); |
Mindy Pereira | c1fed8b | 2011-07-01 13:46:27 -0700 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
Scott Kennedy | 272aae0 | 2013-04-05 17:17:24 -0700 | [diff] [blame] | 1716 | private void showAlternates(final DrawableRecipientChip currentChip, |
Jin Cao | 9b16620 | 2014-08-08 13:26:14 -0700 | [diff] [blame] | 1717 | final ListPopupWindow alternatesPopup) { |
Scott Kennedy | 272aae0 | 2013-04-05 17:17:24 -0700 | [diff] [blame] | 1718 | new AsyncTask<Void, Void, ListAdapter>() { |
| 1719 | @Override |
| 1720 | protected ListAdapter doInBackground(final Void... params) { |
| 1721 | return createAlternatesAdapter(currentChip); |
| 1722 | } |
| 1723 | |
Alice Yang | 6b7110f | 2013-04-26 12:50:35 -0700 | [diff] [blame] | 1724 | @Override |
Scott Kennedy | 272aae0 | 2013-04-05 17:17:24 -0700 | [diff] [blame] | 1725 | protected void onPostExecute(final ListAdapter result) { |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 1726 | if (!mAttachedToWindow) { |
| 1727 | return; |
| 1728 | } |
Scott Kennedy | 272aae0 | 2013-04-05 17:17:24 -0700 | [diff] [blame] | 1729 | int line = getLayout().getLineForOffset(getChipStart(currentChip)); |
Jin Cao | 6fc1fee | 2014-07-29 17:56:44 -0700 | [diff] [blame] | 1730 | int bottomOffset = calculateOffsetFromBottomToTop(line); |
| 1731 | |
Scott Kennedy | 272aae0 | 2013-04-05 17:17:24 -0700 | [diff] [blame] | 1732 | // Align the alternates popup with the left side of the View, |
| 1733 | // regardless of the position of the chip tapped. |
Jin Cao | 4de6a53 | 2014-08-08 11:34:10 -0700 | [diff] [blame] | 1734 | alternatesPopup.setAnchorView((mAlternatePopupAnchor != null) ? |
| 1735 | mAlternatePopupAnchor : RecipientEditTextView.this); |
Jin Cao | 6fc1fee | 2014-07-29 17:56:44 -0700 | [diff] [blame] | 1736 | alternatesPopup.setVerticalOffset(bottomOffset); |
Scott Kennedy | 272aae0 | 2013-04-05 17:17:24 -0700 | [diff] [blame] | 1737 | alternatesPopup.setAdapter(result); |
| 1738 | alternatesPopup.setOnItemClickListener(mAlternatesListener); |
| 1739 | // Clear the checked item. |
| 1740 | mCheckedItem = -1; |
| 1741 | alternatesPopup.show(); |
| 1742 | ListView listView = alternatesPopup.getListView(); |
| 1743 | listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
| 1744 | // Checked item would be -1 if the adapter has not |
| 1745 | // loaded the view that should be checked yet. The |
| 1746 | // variable will be set correctly when onCheckedItemChanged |
| 1747 | // is called in a separate thread. |
| 1748 | if (mCheckedItem != -1) { |
| 1749 | listView.setItemChecked(mCheckedItem, true); |
| 1750 | mCheckedItem = -1; |
| 1751 | } |
| 1752 | } |
| 1753 | }.execute((Void[]) null); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1756 | private ListAdapter createAlternatesAdapter(DrawableRecipientChip chip) { |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 1757 | return new RecipientAlternatesAdapter(getContext(), chip.getContactId(), |
| 1758 | chip.getDirectoryId(), chip.getLookupKey(), chip.getDataId(), |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 1759 | getAdapter().getQueryType(), this, mDropdownChipLayouter, |
| 1760 | constructStateListDeleteDrawable()); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1763 | private ListAdapter createSingleAddressAdapter(DrawableRecipientChip currentChip) { |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 1764 | return new SingleRecipientArrayAdapter(getContext(), currentChip.getEntry(), |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 1765 | mDropdownChipLayouter, constructStateListDeleteDrawable()); |
| 1766 | } |
| 1767 | |
| 1768 | private StateListDrawable constructStateListDeleteDrawable() { |
| 1769 | // Construct the StateListDrawable from deleteDrawable |
| 1770 | StateListDrawable deleteDrawable = new StateListDrawable(); |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 1771 | if (!mDisableDelete) { |
| 1772 | deleteDrawable.addState(new int[]{android.R.attr.state_activated}, mChipDelete); |
| 1773 | } |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 1774 | deleteDrawable.addState(new int[0], null); |
| 1775 | return deleteDrawable; |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Andy Stadler | aa2afff | 2011-08-04 16:22:47 -0700 | [diff] [blame] | 1778 | @Override |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1779 | public void onCheckedItemChanged(int position) { |
| 1780 | ListView listView = mAlternatesPopup.getListView(); |
| 1781 | if (listView != null && listView.getCheckedItemCount() == 0) { |
| 1782 | listView.setItemChecked(position, true); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1783 | } |
Mindy Pereira | 98b547f | 2011-09-08 09:27:39 -0700 | [diff] [blame] | 1784 | mCheckedItem = position; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Scott Kennedy | 1650ef6 | 2013-08-02 21:56:47 -0700 | [diff] [blame] | 1787 | private int putOffsetInRange(final float x, final float y) { |
| 1788 | final int offset; |
| 1789 | |
| 1790 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 1791 | offset = getOffsetForPosition(x, y); |
| 1792 | } else { |
| 1793 | offset = supportGetOffsetForPosition(x, y); |
| 1794 | } |
| 1795 | |
| 1796 | return putOffsetInRange(offset); |
| 1797 | } |
| 1798 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1799 | // TODO: This algorithm will need a lot of tweaking after more people have used |
| 1800 | // the chips ui. This attempts to be "forgiving" to fat finger touches by favoring |
| 1801 | // what comes before the finger. |
| 1802 | private int putOffsetInRange(int o) { |
| 1803 | int offset = o; |
| 1804 | Editable text = getText(); |
| 1805 | int length = text.length(); |
| 1806 | // Remove whitespace from end to find "real end" |
| 1807 | int realLength = length; |
| 1808 | for (int i = length - 1; i >= 0; i--) { |
| 1809 | if (text.charAt(i) == ' ') { |
| 1810 | realLength--; |
| 1811 | } else { |
| 1812 | break; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 1816 | // If the offset is beyond or at the end of the text, |
| 1817 | // leave it alone. |
| 1818 | if (offset >= realLength) { |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1819 | return offset; |
| 1820 | } |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 1821 | Editable editable = getText(); |
Mindy Pereira | b88ee45 | 2011-06-02 11:07:43 -0700 | [diff] [blame] | 1822 | while (offset >= 0 && findText(editable, offset) == -1 && findChip(offset) == null) { |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1823 | // Keep walking backward! |
| 1824 | offset--; |
| 1825 | } |
| 1826 | return offset; |
| 1827 | } |
| 1828 | |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 1829 | private static int findText(Editable text, int offset) { |
Mindy Pereira | fd68ea6 | 2011-06-02 10:40:19 -0700 | [diff] [blame] | 1830 | if (text.charAt(offset) != ' ') { |
| 1831 | return offset; |
| 1832 | } |
| 1833 | return -1; |
| 1834 | } |
| 1835 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1836 | private DrawableRecipientChip findChip(int offset) { |
| 1837 | DrawableRecipientChip[] chips = |
| 1838 | getSpannable().getSpans(0, getText().length(), DrawableRecipientChip.class); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1839 | // Find the chip that contains this offset. |
| 1840 | for (int i = 0; i < chips.length; i++) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1841 | DrawableRecipientChip chip = chips[i]; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 1842 | int start = getChipStart(chip); |
| 1843 | int end = getChipEnd(chip); |
| 1844 | if (offset >= start && offset <= end) { |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1845 | return chip; |
| 1846 | } |
| 1847 | } |
| 1848 | return null; |
| 1849 | } |
| 1850 | |
Mindy Pereira | 01162ce | 2011-09-20 12:21:55 -0700 | [diff] [blame] | 1851 | // Visible for testing. |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 1852 | // Use this method to generate text to add to the list of addresses. |
Mindy Pereira | 32aff5f | 2012-01-11 14:35:12 -0800 | [diff] [blame] | 1853 | /* package */String createAddressText(RecipientEntry entry) { |
Mindy Pereira | 3ea9955 | 2011-09-19 10:50:31 -0700 | [diff] [blame] | 1854 | String display = entry.getDisplayName(); |
| 1855 | String address = entry.getDestination(); |
| 1856 | if (TextUtils.isEmpty(display) || TextUtils.equals(display, address)) { |
| 1857 | display = null; |
| 1858 | } |
Tom Taylor | abff606 | 2012-04-17 09:04:56 -0700 | [diff] [blame] | 1859 | String trimmedDisplayText; |
Tom Taylor | c7a87f0 | 2012-05-03 15:21:08 -0700 | [diff] [blame] | 1860 | if (isPhoneQuery() && isPhoneNumber(address)) { |
Tom Taylor | abff606 | 2012-04-17 09:04:56 -0700 | [diff] [blame] | 1861 | trimmedDisplayText = address.trim(); |
| 1862 | } else { |
| 1863 | if (address != null) { |
| 1864 | // Tokenize out the address in case the address already |
| 1865 | // contained the username as well. |
| 1866 | Rfc822Token[] tokenized = Rfc822Tokenizer.tokenize(address); |
| 1867 | if (tokenized != null && tokenized.length > 0) { |
| 1868 | address = tokenized[0].getAddress(); |
| 1869 | } |
Mindy Pereira | ee48f73 | 2011-09-20 08:13:39 -0700 | [diff] [blame] | 1870 | } |
Tom Taylor | abff606 | 2012-04-17 09:04:56 -0700 | [diff] [blame] | 1871 | Rfc822Token token = new Rfc822Token(display, address, null); |
| 1872 | trimmedDisplayText = token.toString().trim(); |
Mindy Pereira | d8c15c3 | 2011-09-19 15:02:05 -0700 | [diff] [blame] | 1873 | } |
Mindy Pereira | 3ea9955 | 2011-09-19 10:50:31 -0700 | [diff] [blame] | 1874 | int index = trimmedDisplayText.indexOf(","); |
Mindy Pereira | 32aff5f | 2012-01-11 14:35:12 -0800 | [diff] [blame] | 1875 | return mTokenizer != null && !TextUtils.isEmpty(trimmedDisplayText) |
| 1876 | && index < trimmedDisplayText.length() - 1 ? (String) mTokenizer |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 1877 | .terminateToken(trimmedDisplayText) : trimmedDisplayText; |
| 1878 | } |
| 1879 | |
| 1880 | // Visible for testing. |
| 1881 | // Use this method to generate text to display in a chip. |
| 1882 | /*package*/ String createChipDisplayText(RecipientEntry entry) { |
| 1883 | String display = entry.getDisplayName(); |
| 1884 | String address = entry.getDestination(); |
| 1885 | if (TextUtils.isEmpty(display) || TextUtils.equals(display, address)) { |
| 1886 | display = null; |
| 1887 | } |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 1888 | if (!TextUtils.isEmpty(display)) { |
| 1889 | return display; |
| 1890 | } else if (!TextUtils.isEmpty(address)){ |
| 1891 | return address; |
| 1892 | } else { |
| 1893 | return new Rfc822Token(display, address, null).toString(); |
| 1894 | } |
Mindy Pereira | 3ea9955 | 2011-09-19 10:50:31 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Mindy Pereira | 0ab7e73 | 2011-06-08 10:17:39 -0700 | [diff] [blame] | 1897 | private CharSequence createChip(RecipientEntry entry, boolean pressed) { |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 1898 | final String displayText = createAddressText(entry); |
Mindy Pereira | 2d7709d | 2011-10-05 13:24:43 -0700 | [diff] [blame] | 1899 | if (TextUtils.isEmpty(displayText)) { |
| 1900 | return null; |
| 1901 | } |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1902 | // Always leave a blank space at the end of a chip. |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 1903 | final int textLength = displayText.length() - 1; |
| 1904 | final SpannableString chipText = new SpannableString(displayText); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1905 | if (!mNoChips) { |
| 1906 | try { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 1907 | DrawableRecipientChip chip = constructChipSpan(entry, pressed); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 1908 | chipText.setSpan(chip, 0, textLength, |
| 1909 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 1910 | chip.setOriginalText(chipText.toString()); |
| 1911 | } catch (NullPointerException e) { |
| 1912 | Log.e(TAG, e.getMessage(), e); |
| 1913 | return null; |
| 1914 | } |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1915 | } |
Kevin Lin | 3e05cfa | 2014-06-05 12:27:17 -0700 | [diff] [blame] | 1916 | onChipCreated(entry); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1917 | return chipText; |
| 1918 | } |
| 1919 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1920 | /** |
Kevin Lin | 3e05cfa | 2014-06-05 12:27:17 -0700 | [diff] [blame] | 1921 | * A callback for subclasses to use to know when a chip was created with the |
| 1922 | * given RecipientEntry. |
| 1923 | */ |
| 1924 | protected void onChipCreated(RecipientEntry entry) {} |
| 1925 | |
| 1926 | /** |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 1927 | * When an item in the suggestions list has been clicked, create a chip from the |
| 1928 | * contact information of the selected item. |
| 1929 | */ |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1930 | @Override |
| 1931 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Tom Taylor | 114c893 | 2013-04-10 13:11:05 -0700 | [diff] [blame] | 1932 | if (position < 0) { |
| 1933 | return; |
| 1934 | } |
Andrew Sapperstein | 0f2b174 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 1935 | |
| 1936 | final int charactersTyped = submitItemAtPosition(position); |
| 1937 | if (charactersTyped > -1 && mRecipientEntryItemClickedListener != null) { |
| 1938 | mRecipientEntryItemClickedListener |
| 1939 | .onRecipientEntryItemClicked(charactersTyped, position); |
| 1940 | } |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Andrew Sapperstein | 0f2b174 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 1943 | private int submitItemAtPosition(int position) { |
Scott Kennedy | 858e094 | 2013-10-10 11:50:01 -0700 | [diff] [blame] | 1944 | RecipientEntry entry = createValidatedEntry(getAdapter().getItem(position)); |
Erik | f7eaa5f | 2011-07-14 17:02:07 -0700 | [diff] [blame] | 1945 | if (entry == null) { |
Andrew Sapperstein | 0f2b174 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 1946 | return -1; |
Erik | f7eaa5f | 2011-07-14 17:02:07 -0700 | [diff] [blame] | 1947 | } |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1948 | clearComposingText(); |
| 1949 | |
| 1950 | int end = getSelectionEnd(); |
| 1951 | int start = mTokenizer.findTokenStart(getText(), end); |
| 1952 | |
| 1953 | Editable editable = getText(); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1954 | QwertyKeyListener.markAsReplaced(editable, start, end, ""); |
Mindy Pereira | e90bfad | 2011-07-26 14:51:41 -0700 | [diff] [blame] | 1955 | CharSequence chip = createChip(entry, false); |
Mindy Pereira | 005b2e2 | 2011-11-01 10:26:58 -0700 | [diff] [blame] | 1956 | if (chip != null && start >= 0 && end >= 0) { |
Mindy Pereira | e90bfad | 2011-07-26 14:51:41 -0700 | [diff] [blame] | 1957 | editable.replace(start, end, chip); |
| 1958 | } |
Mindy Pereira | 5df0aa8 | 2011-09-19 16:29:03 -0700 | [diff] [blame] | 1959 | sanitizeBetween(); |
Andrew Sapperstein | 0f2b174 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 1960 | |
| 1961 | return end - start; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1964 | private RecipientEntry createValidatedEntry(RecipientEntry item) { |
| 1965 | if (item == null) { |
| 1966 | return null; |
| 1967 | } |
| 1968 | final RecipientEntry entry; |
| 1969 | // If the display name and the address are the same, or if this is a |
| 1970 | // valid contact, but the destination is invalid, then make this a fake |
| 1971 | // recipient that is editable. |
| 1972 | String destination = item.getDestination(); |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1973 | if (!isPhoneQuery() && item.getContactId() == RecipientEntry.GENERATED_CONTACT) { |
| 1974 | entry = RecipientEntry.constructGeneratedEntry(item.getDisplayName(), |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1975 | destination, item.isValid()); |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 1976 | } else if (RecipientEntry.isCreatedRecipient(item.getContactId()) |
Mindy Pereira | b4e244a | 2011-10-14 11:13:29 -0700 | [diff] [blame] | 1977 | && (TextUtils.isEmpty(item.getDisplayName()) |
| 1978 | || TextUtils.equals(item.getDisplayName(), destination) |
| 1979 | || (mValidator != null && !mValidator.isValid(destination)))) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 1980 | entry = RecipientEntry.constructFakeEntry(destination, item.isValid()); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 1981 | } else { |
| 1982 | entry = item; |
| 1983 | } |
| 1984 | return entry; |
| 1985 | } |
| 1986 | |
Mindy Pereira | 01162ce | 2011-09-20 12:21:55 -0700 | [diff] [blame] | 1987 | // Visible for testing. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1988 | /* package */DrawableRecipientChip[] getSortedRecipients() { |
| 1989 | DrawableRecipientChip[] recips = getSpannable() |
| 1990 | .getSpans(0, getText().length(), DrawableRecipientChip.class); |
| 1991 | ArrayList<DrawableRecipientChip> recipientsList = new ArrayList<DrawableRecipientChip>( |
| 1992 | Arrays.asList(recips)); |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 1993 | final Spannable spannable = getSpannable(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1994 | Collections.sort(recipientsList, new Comparator<DrawableRecipientChip>() { |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 1995 | |
| 1996 | @Override |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 1997 | public int compare(DrawableRecipientChip first, DrawableRecipientChip second) { |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 1998 | int firstStart = spannable.getSpanStart(first); |
| 1999 | int secondStart = spannable.getSpanStart(second); |
| 2000 | if (firstStart < secondStart) { |
| 2001 | return -1; |
| 2002 | } else if (firstStart > secondStart) { |
| 2003 | return 1; |
| 2004 | } else { |
| 2005 | return 0; |
| 2006 | } |
| 2007 | } |
| 2008 | }); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2009 | return recipientsList.toArray(new DrawableRecipientChip[recipientsList.size()]); |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2012 | @Override |
| 2013 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 2014 | return false; |
| 2015 | } |
| 2016 | |
| 2017 | @Override |
| 2018 | public void onDestroyActionMode(ActionMode mode) { |
| 2019 | } |
| 2020 | |
| 2021 | @Override |
| 2022 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 2023 | return false; |
| 2024 | } |
| 2025 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 2026 | /** |
| 2027 | * No chips are selectable. |
| 2028 | */ |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2029 | @Override |
| 2030 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 2031 | return false; |
| 2032 | } |
| 2033 | |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 2034 | // Visible for testing. |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2035 | /* package */ReplacementDrawableSpan getMoreChip() { |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 2036 | MoreImageSpan[] moreSpans = getSpannable().getSpans(0, getText().length(), |
| 2037 | MoreImageSpan.class); |
| 2038 | return moreSpans != null && moreSpans.length > 0 ? moreSpans[0] : null; |
| 2039 | } |
| 2040 | |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2041 | private MoreImageSpan createMoreSpan(int count) { |
| 2042 | String moreText = String.format(mMoreItem.getText().toString(), count); |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2043 | mWorkPaint.set(getPaint()); |
| 2044 | mWorkPaint.setTextSize(mMoreItem.getTextSize()); |
| 2045 | mWorkPaint.setColor(mMoreItem.getCurrentTextColor()); |
| 2046 | final int width = (int) mWorkPaint.measureText(moreText) + mMoreItem.getPaddingLeft() |
Mindy Pereira | 750e6e5 | 2011-07-25 11:51:30 -0700 | [diff] [blame] | 2047 | + mMoreItem.getPaddingRight(); |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2048 | final int height = (int) mChipHeight; |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2049 | Bitmap drawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 2050 | Canvas canvas = new Canvas(drawable); |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 2051 | int adjustedHeight = height; |
| 2052 | Layout layout = getLayout(); |
| 2053 | if (layout != null) { |
| 2054 | adjustedHeight -= layout.getLineDescent(0); |
| 2055 | } |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2056 | canvas.drawText(moreText, 0, moreText.length(), 0, adjustedHeight, mWorkPaint); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2057 | |
| 2058 | Drawable result = new BitmapDrawable(getResources(), drawable); |
| 2059 | result.setBounds(0, 0, width, height); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2060 | return new MoreImageSpan(result); |
| 2061 | } |
| 2062 | |
| 2063 | // Visible for testing. |
| 2064 | /*package*/ void createMoreChipPlainText() { |
| 2065 | // Take the first <= CHIP_LIMIT addresses and get to the end of the second one. |
| 2066 | Editable text = getText(); |
| 2067 | int start = 0; |
| 2068 | int end = start; |
| 2069 | for (int i = 0; i < CHIP_LIMIT; i++) { |
| 2070 | end = movePastTerminators(mTokenizer.findTokenEnd(text, start)); |
| 2071 | start = end; // move to the next token and get its end. |
| 2072 | } |
| 2073 | // Now, count total addresses. |
| 2074 | start = 0; |
| 2075 | int tokenCount = countTokens(text); |
| 2076 | MoreImageSpan moreSpan = createMoreSpan(tokenCount - CHIP_LIMIT); |
| 2077 | SpannableString chipText = new SpannableString(text.subSequence(end, text.length())); |
| 2078 | chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 2079 | text.replace(end, text.length(), chipText); |
| 2080 | mMoreChip = moreSpan; |
| 2081 | } |
| 2082 | |
| 2083 | // Visible for testing. |
| 2084 | /* package */int countTokens(Editable text) { |
| 2085 | int tokenCount = 0; |
| 2086 | int start = 0; |
| 2087 | while (start < text.length()) { |
| 2088 | start = movePastTerminators(mTokenizer.findTokenEnd(text, start)); |
| 2089 | tokenCount++; |
| 2090 | if (start >= text.length()) { |
| 2091 | break; |
| 2092 | } |
| 2093 | } |
| 2094 | return tokenCount; |
| 2095 | } |
| 2096 | |
| 2097 | /** |
| 2098 | * Create the more chip. The more chip is text that replaces any chips that |
| 2099 | * do not fit in the pre-defined available space when the |
| 2100 | * RecipientEditTextView loses focus. |
| 2101 | */ |
| 2102 | // Visible for testing. |
| 2103 | /* package */ void createMoreChip() { |
| 2104 | if (mNoChips) { |
| 2105 | createMoreChipPlainText(); |
| 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | if (!mShouldShrink) { |
| 2110 | return; |
| 2111 | } |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2112 | ReplacementDrawableSpan[] tempMore = getSpannable().getSpans(0, getText().length(), |
| 2113 | MoreImageSpan.class); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2114 | if (tempMore.length > 0) { |
| 2115 | getSpannable().removeSpan(tempMore[0]); |
| 2116 | } |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2117 | DrawableRecipientChip[] recipients = getSortedRecipients(); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2118 | |
| 2119 | if (recipients == null || recipients.length <= CHIP_LIMIT) { |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 2120 | mMoreChip = null; |
| 2121 | return; |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2122 | } |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2123 | Spannable spannable = getSpannable(); |
| 2124 | int numRecipients = recipients.length; |
| 2125 | int overage = numRecipients - CHIP_LIMIT; |
| 2126 | MoreImageSpan moreSpan = createMoreSpan(overage); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2127 | mRemovedSpans = new ArrayList<DrawableRecipientChip>(); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2128 | int totalReplaceStart = 0; |
| 2129 | int totalReplaceEnd = 0; |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2130 | Editable text = getText(); |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 2131 | for (int i = numRecipients - overage; i < recipients.length; i++) { |
| 2132 | mRemovedSpans.add(recipients[i]); |
Mindy Pereira | 364e7cf | 2011-06-07 11:17:36 -0700 | [diff] [blame] | 2133 | if (i == numRecipients - overage) { |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 2134 | totalReplaceStart = spannable.getSpanStart(recipients[i]); |
Mindy Pereira | 364e7cf | 2011-06-07 11:17:36 -0700 | [diff] [blame] | 2135 | } |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 2136 | if (i == recipients.length - 1) { |
| 2137 | totalReplaceEnd = spannable.getSpanEnd(recipients[i]); |
Mindy Pereira | 364e7cf | 2011-06-07 11:17:36 -0700 | [diff] [blame] | 2138 | } |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2139 | if (mTemporaryRecipients == null || !mTemporaryRecipients.contains(recipients[i])) { |
| 2140 | int spanStart = spannable.getSpanStart(recipients[i]); |
| 2141 | int spanEnd = spannable.getSpanEnd(recipients[i]); |
| 2142 | recipients[i].setOriginalText(text.toString().substring(spanStart, spanEnd)); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2143 | } |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 2144 | spannable.removeSpan(recipients[i]); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2145 | } |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2146 | if (totalReplaceEnd < text.length()) { |
| 2147 | totalReplaceEnd = text.length(); |
| 2148 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2149 | int end = Math.max(totalReplaceStart, totalReplaceEnd); |
| 2150 | int start = Math.min(totalReplaceStart, totalReplaceEnd); |
| 2151 | SpannableString chipText = new SpannableString(text.subSequence(start, end)); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2152 | chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2153 | text.replace(start, end, chipText); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 2154 | mMoreChip = moreSpan; |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 2155 | // If adding the +more chip goes over the limit, resize accordingly. |
| 2156 | if (!isPhoneQuery() && getLineCount() > mMaxLines) { |
| 2157 | setMaxLines(getLineCount()); |
| 2158 | } |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Mindy Pereira | a816690 | 2011-06-09 17:53:23 -0700 | [diff] [blame] | 2161 | /** |
| 2162 | * Replace the more chip, if it exists, with all of the recipient chips it had |
| 2163 | * replaced when the RecipientEditTextView gains focus. |
| 2164 | */ |
Mindy Pereira | d719250 | 2011-09-22 08:04:45 -0700 | [diff] [blame] | 2165 | // Visible for testing. |
| 2166 | /*package*/ void removeMoreChip() { |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2167 | if (mMoreChip != null) { |
| 2168 | Spannable span = getSpannable(); |
| 2169 | span.removeSpan(mMoreChip); |
| 2170 | mMoreChip = null; |
| 2171 | // Re-add the spans that were removed. |
| 2172 | if (mRemovedSpans != null && mRemovedSpans.size() > 0) { |
| 2173 | // Recreate each removed span. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2174 | DrawableRecipientChip[] recipients = getSortedRecipients(); |
Mindy Pereira | 54effe9 | 2011-09-09 09:46:38 -0700 | [diff] [blame] | 2175 | // Start the search for tokens after the last currently visible |
| 2176 | // chip. |
Mindy Pereira | ee48f73 | 2011-09-20 08:13:39 -0700 | [diff] [blame] | 2177 | if (recipients == null || recipients.length == 0) { |
| 2178 | return; |
| 2179 | } |
Mindy Pereira | 54effe9 | 2011-09-09 09:46:38 -0700 | [diff] [blame] | 2180 | int end = span.getSpanEnd(recipients[recipients.length - 1]); |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2181 | Editable editable = getText(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2182 | for (DrawableRecipientChip chip : mRemovedSpans) { |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2183 | int chipStart; |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 2184 | int chipEnd; |
| 2185 | String token; |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2186 | // Need to find the location of the chip, again. |
| 2187 | token = (String) chip.getOriginalText(); |
Mindy Pereira | 54effe9 | 2011-09-09 09:46:38 -0700 | [diff] [blame] | 2188 | // As we find the matching recipient for the remove spans, |
| 2189 | // reduce the size of the string we need to search. |
| 2190 | // That way, if there are duplicates, we always find the correct |
| 2191 | // recipient. |
| 2192 | chipStart = editable.toString().indexOf(token, end); |
Mindy Pereira | 54effe9 | 2011-09-09 09:46:38 -0700 | [diff] [blame] | 2193 | end = chipEnd = Math.min(editable.length(), chipStart + token.length()); |
Mindy Pereira | 0d8b77a | 2011-07-08 10:26:30 -0700 | [diff] [blame] | 2194 | // Only set the span if we found a matching token. |
| 2195 | if (chipStart != -1) { |
| 2196 | editable.setSpan(chip, chipStart, chipEnd, |
| 2197 | Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 2198 | } |
Mindy Pereira | 12cf3fc | 2011-06-06 11:08:17 -0700 | [diff] [blame] | 2199 | } |
| 2200 | mRemovedSpans.clear(); |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 2205 | /** |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2206 | * Show specified chip as selected. If the RecipientChip is just an email address, |
| 2207 | * selecting the chip will take the contents of the chip and place it at |
| 2208 | * the end of the RecipientEditTextView for inline editing. If the |
| 2209 | * RecipientChip is a complete contact, then selecting the chip |
| 2210 | * will change the background color of the chip, show the delete icon, |
| 2211 | * and a popup window with the address in use highlighted and any other |
| 2212 | * alternate addresses for the contact. |
| 2213 | * @param currentChip Chip to select. |
| 2214 | * @return A RecipientChip in the selected state or null if the chip |
| 2215 | * just contained an email address. |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 2216 | */ |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2217 | private DrawableRecipientChip selectChip(DrawableRecipientChip currentChip) { |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 2218 | if (shouldShowEditableText(currentChip)) { |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2219 | CharSequence text = currentChip.getValue(); |
| 2220 | Editable editable = getText(); |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2221 | Spannable spannable = getSpannable(); |
| 2222 | int spanStart = spannable.getSpanStart(currentChip); |
| 2223 | int spanEnd = spannable.getSpanEnd(currentChip); |
| 2224 | spannable.removeSpan(currentChip); |
Jin Cao | 15a5ebc | 2014-09-18 16:43:32 -0700 | [diff] [blame] | 2225 | // Don't need leading space if it's the only chip |
| 2226 | if (spanEnd - spanStart == editable.length() - 1) { |
| 2227 | spanEnd++; |
| 2228 | } |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2229 | editable.delete(spanStart, spanEnd); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2230 | setCursorVisible(true); |
| 2231 | setSelection(editable.length()); |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2232 | editable.append(text); |
| 2233 | return constructChipSpan( |
| 2234 | RecipientEntry.constructFakeEntry((String) text, isValid(text.toString())), |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 2235 | true); |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 2236 | } else { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2237 | int start = getChipStart(currentChip); |
| 2238 | int end = getChipEnd(currentChip); |
| 2239 | getSpannable().removeSpan(currentChip); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2240 | DrawableRecipientChip newChip; |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 2241 | final boolean showAddress = |
| 2242 | currentChip.getContactId() == RecipientEntry.GENERATED_CONTACT || |
| 2243 | getAdapter().forceShowAddress(); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2244 | try { |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 2245 | if (showAddress && mNoChips) { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2246 | return null; |
| 2247 | } |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 2248 | newChip = constructChipSpan(currentChip.getEntry(), true); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2249 | } catch (NullPointerException e) { |
| 2250 | Log.e(TAG, e.getMessage(), e); |
Mindy Pereira | 0ab7e73 | 2011-06-08 10:17:39 -0700 | [diff] [blame] | 2251 | return null; |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 2252 | } |
Mindy Pereira | 0ab7e73 | 2011-06-08 10:17:39 -0700 | [diff] [blame] | 2253 | Editable editable = getText(); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2254 | QwertyKeyListener.markAsReplaced(editable, start, end, ""); |
Mindy Pereira | 0ab7e73 | 2011-06-08 10:17:39 -0700 | [diff] [blame] | 2255 | if (start == -1 || end == -1) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2256 | Log.d(TAG, "The chip being selected no longer exists but should."); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 2257 | } else { |
Mindy Pereira | 04da325 | 2011-07-06 16:09:25 -0700 | [diff] [blame] | 2258 | editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 2259 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2260 | newChip.setSelected(true); |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 2261 | if (shouldShowEditableText(newChip)) { |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2262 | scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip))); |
| 2263 | } |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 2264 | if (showAddress) { |
Jin Cao | 9b16620 | 2014-08-08 13:26:14 -0700 | [diff] [blame] | 2265 | showAddress(newChip, mAddressPopup); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2266 | } else { |
Jin Cao | 9b16620 | 2014-08-08 13:26:14 -0700 | [diff] [blame] | 2267 | showAlternates(newChip, mAlternatesPopup); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2268 | } |
Mindy Pereira | 4e18d8b | 2011-07-25 11:08:41 -0700 | [diff] [blame] | 2269 | setCursorVisible(false); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2270 | return newChip; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2274 | private boolean shouldShowEditableText(DrawableRecipientChip currentChip) { |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 2275 | long contactId = currentChip.getContactId(); |
| 2276 | return contactId == RecipientEntry.INVALID_CONTACT |
| 2277 | || (!isPhoneQuery() && contactId == RecipientEntry.GENERATED_CONTACT); |
| 2278 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2279 | |
Jin Cao | 9b16620 | 2014-08-08 13:26:14 -0700 | [diff] [blame] | 2280 | private void showAddress(final DrawableRecipientChip currentChip, final ListPopupWindow popup) { |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 2281 | if (!mAttachedToWindow) { |
| 2282 | return; |
| 2283 | } |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2284 | int line = getLayout().getLineForOffset(getChipStart(currentChip)); |
Jin Cao | 6fc1fee | 2014-07-29 17:56:44 -0700 | [diff] [blame] | 2285 | int bottomOffset = calculateOffsetFromBottomToTop(line); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2286 | // Align the alternates popup with the left side of the View, |
| 2287 | // regardless of the position of the chip tapped. |
Jin Cao | 4de6a53 | 2014-08-08 11:34:10 -0700 | [diff] [blame] | 2288 | popup.setAnchorView((mAlternatePopupAnchor != null) ? mAlternatePopupAnchor : this); |
Jin Cao | 6fc1fee | 2014-07-29 17:56:44 -0700 | [diff] [blame] | 2289 | popup.setVerticalOffset(bottomOffset); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2290 | popup.setAdapter(createSingleAddressAdapter(currentChip)); |
| 2291 | popup.setOnItemClickListener(new OnItemClickListener() { |
| 2292 | @Override |
| 2293 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 2294 | unselectChip(currentChip); |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2295 | } |
| 2296 | }); |
| 2297 | popup.show(); |
| 2298 | ListView listView = popup.getListView(); |
| 2299 | listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
| 2300 | listView.setItemChecked(0, true); |
| 2301 | } |
| 2302 | |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2303 | /** |
| 2304 | * Remove selection from this chip. Unselecting a RecipientChip will render |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2305 | * the chip without a delete icon and with an unfocused background. This is |
| 2306 | * called when the RecipientChip no longer has focus. |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2307 | */ |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2308 | private void unselectChip(DrawableRecipientChip chip) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2309 | int start = getChipStart(chip); |
| 2310 | int end = getChipEnd(chip); |
| 2311 | Editable editable = getText(); |
Mindy Pereira | e82e61f | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 2312 | mSelectedChip = null; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2313 | if (start == -1 || end == -1) { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2314 | Log.w(TAG, "The chip doesn't exist or may be a chip a user was editing"); |
Mindy Pereira | c0a34ab | 2011-09-13 15:17:39 -0700 | [diff] [blame] | 2315 | setSelection(editable.length()); |
| 2316 | commitDefault(); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2317 | } else { |
Mindy Pereira | e82e61f | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 2318 | getSpannable().removeSpan(chip); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2319 | QwertyKeyListener.markAsReplaced(editable, start, end, ""); |
Mindy Pereira | 04da325 | 2011-07-06 16:09:25 -0700 | [diff] [blame] | 2320 | editable.removeSpan(chip); |
| 2321 | try { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2322 | if (!mNoChips) { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 2323 | editable.setSpan(constructChipSpan(chip.getEntry(), false), |
mindyp | 5e60c2b | 2012-11-06 17:47:40 -0800 | [diff] [blame] | 2324 | start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2325 | } |
Mindy Pereira | 04da325 | 2011-07-06 16:09:25 -0700 | [diff] [blame] | 2326 | } catch (NullPointerException e) { |
| 2327 | Log.e(TAG, e.getMessage(), e); |
| 2328 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2329 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2330 | setCursorVisible(true); |
| 2331 | setSelection(editable.length()); |
Jin Cao | 568fb22 | 2014-08-19 13:50:59 -0700 | [diff] [blame] | 2332 | dismissPopups(); |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 2335 | @Override |
| 2336 | public void onChipDelete() { |
| 2337 | if (mSelectedChip != null) { |
| 2338 | removeChip(mSelectedChip); |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 2339 | } |
Jin Cao | 568fb22 | 2014-08-19 13:50:59 -0700 | [diff] [blame] | 2340 | dismissPopups(); |
| 2341 | } |
| 2342 | |
| 2343 | private void dismissPopups() { |
| 2344 | if (mAlternatesPopup != null && mAlternatesPopup.isShowing()) { |
| 2345 | mAlternatesPopup.dismiss(); |
| 2346 | } |
| 2347 | if (mAddressPopup != null && mAddressPopup.isShowing()) { |
| 2348 | mAddressPopup.dismiss(); |
| 2349 | } |
Jin Cao | 4db8ccc | 2014-07-30 10:11:07 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2352 | /** |
| 2353 | * Remove the chip and any text associated with it from the RecipientEditTextView. |
| 2354 | */ |
Mindy Pereira | 97cb259 | 2011-09-26 16:11:41 -0700 | [diff] [blame] | 2355 | // Visible for testing. |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 2356 | /* package */void removeChip(DrawableRecipientChip chip) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2357 | Spannable spannable = getSpannable(); |
| 2358 | int spanStart = spannable.getSpanStart(chip); |
| 2359 | int spanEnd = spannable.getSpanEnd(chip); |
| 2360 | Editable text = getText(); |
Mindy Pereira | 62397a5 | 2011-10-03 13:46:43 -0700 | [diff] [blame] | 2361 | int toDelete = spanEnd; |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2362 | boolean wasSelected = chip == mSelectedChip; |
| 2363 | // Clear that there is a selected chip before updating any text. |
| 2364 | if (wasSelected) { |
| 2365 | mSelectedChip = null; |
| 2366 | } |
Mindy Pereira | 62397a5 | 2011-10-03 13:46:43 -0700 | [diff] [blame] | 2367 | // Always remove trailing spaces when removing a chip. |
| 2368 | while (toDelete >= 0 && toDelete < text.length() && text.charAt(toDelete) == ' ') { |
| 2369 | toDelete++; |
| 2370 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2371 | spannable.removeSpan(chip); |
Mindy Pereira | f6d4bfc | 2012-04-24 15:13:34 -0700 | [diff] [blame] | 2372 | if (spanStart >= 0 && toDelete > 0) { |
| 2373 | text.delete(spanStart, toDelete); |
| 2374 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2375 | if (wasSelected) { |
| 2376 | clearSelectedChip(); |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | /** |
| 2381 | * Replace this currently selected chip with a new chip |
| 2382 | * that uses the contact data provided. |
| 2383 | */ |
Mindy Pereira | aca23c4 | 2011-10-13 16:22:09 -0700 | [diff] [blame] | 2384 | // Visible for testing. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2385 | /*package*/ void replaceChip(DrawableRecipientChip chip, RecipientEntry entry) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2386 | boolean wasSelected = chip == mSelectedChip; |
| 2387 | if (wasSelected) { |
| 2388 | mSelectedChip = null; |
| 2389 | } |
| 2390 | int start = getChipStart(chip); |
| 2391 | int end = getChipEnd(chip); |
| 2392 | getSpannable().removeSpan(chip); |
| 2393 | Editable editable = getText(); |
| 2394 | CharSequence chipText = createChip(entry, false); |
Mindy Pereira | 2d7709d | 2011-10-05 13:24:43 -0700 | [diff] [blame] | 2395 | if (chipText != null) { |
| 2396 | if (start == -1 || end == -1) { |
| 2397 | Log.e(TAG, "The chip to replace does not exist but should."); |
| 2398 | editable.insert(0, chipText); |
| 2399 | } else { |
| 2400 | if (!TextUtils.isEmpty(chipText)) { |
| 2401 | // There may be a space to replace with this chip's new |
mindyp | b5afbc7 | 2012-10-03 11:57:42 -0700 | [diff] [blame] | 2402 | // associated space. Check for it |
Mindy Pereira | 2d7709d | 2011-10-05 13:24:43 -0700 | [diff] [blame] | 2403 | int toReplace = end; |
| 2404 | while (toReplace >= 0 && toReplace < editable.length() |
| 2405 | && editable.charAt(toReplace) == ' ') { |
| 2406 | toReplace++; |
| 2407 | } |
| 2408 | editable.replace(start, toReplace, chipText); |
Mindy Pereira | 62397a5 | 2011-10-03 13:46:43 -0700 | [diff] [blame] | 2409 | } |
Mindy Pereira | 7ebb40f | 2011-07-06 17:21:14 -0700 | [diff] [blame] | 2410 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2411 | } |
| 2412 | setCursorVisible(true); |
| 2413 | if (wasSelected) { |
| 2414 | clearSelectedChip(); |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | /** |
| 2419 | * Handle click events for a chip. When a selected chip receives a click |
| 2420 | * event, see if that event was in the delete icon. If so, delete it. |
| 2421 | * Otherwise, unselect the chip. |
| 2422 | */ |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 2423 | public void onClick(DrawableRecipientChip chip) { |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2424 | if (chip.isSelected()) { |
Jin Cao | a21fd88 | 2014-08-10 13:07:11 -0700 | [diff] [blame] | 2425 | clearSelectedChip(); |
Mindy Pereira | cd61195 | 2011-06-01 17:02:20 -0700 | [diff] [blame] | 2426 | } |
Mindy Pereira | 9159cd2 | 2011-05-25 17:06:44 -0700 | [diff] [blame] | 2427 | } |
Mindy Pereira | 95a6925 | 2011-06-17 15:02:03 -0700 | [diff] [blame] | 2428 | |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 2429 | private boolean chipsPending() { |
| 2430 | return mPendingChipsCount > 0 || (mRemovedSpans != null && mRemovedSpans.size() > 0); |
| 2431 | } |
| 2432 | |
Mindy Pereira | 883c499 | 2011-08-04 18:01:30 -0700 | [diff] [blame] | 2433 | @Override |
| 2434 | public void removeTextChangedListener(TextWatcher watcher) { |
| 2435 | mTextWatcher = null; |
| 2436 | super.removeTextChangedListener(watcher); |
| 2437 | } |
| 2438 | |
Jin Cao | b3b248a | 2014-04-11 10:43:50 -0700 | [diff] [blame] | 2439 | private boolean isValidEmailAddress(String input) { |
| 2440 | return !TextUtils.isEmpty(input) && mValidator != null && |
| 2441 | mValidator.isValid(input); |
| 2442 | } |
| 2443 | |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2444 | private class RecipientTextWatcher implements TextWatcher { |
Mindy Pereira | 72a2339 | 2012-02-06 10:19:51 -0800 | [diff] [blame] | 2445 | |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2446 | @Override |
| 2447 | public void afterTextChanged(Editable s) { |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 2448 | // If the text has been set to null or empty, make sure we remove |
| 2449 | // all the spans we applied. |
| 2450 | if (TextUtils.isEmpty(s)) { |
| 2451 | // Remove all the chips spans. |
| 2452 | Spannable spannable = getSpannable(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2453 | DrawableRecipientChip[] chips = spannable.getSpans(0, getText().length(), |
| 2454 | DrawableRecipientChip.class); |
| 2455 | for (DrawableRecipientChip chip : chips) { |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 2456 | spannable.removeSpan(chip); |
| 2457 | } |
| 2458 | if (mMoreChip != null) { |
| 2459 | spannable.removeSpan(mMoreChip); |
| 2460 | } |
Kevin Lin | 41e93fb | 2014-02-03 17:35:29 -0800 | [diff] [blame] | 2461 | clearSelectedChip(); |
Mindy Pereira | 32366d4 | 2011-07-24 12:39:30 -0700 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Mindy Pereira | 01382d7 | 2011-07-15 16:52:32 -0700 | [diff] [blame] | 2464 | // Get whether there are any recipients pending addition to the |
| 2465 | // view. If there are, don't do anything in the text watcher. |
Mindy Pereira | 21cc0a2 | 2011-06-23 17:11:06 -0700 | [diff] [blame] | 2466 | if (chipsPending()) { |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2467 | return; |
| 2468 | } |
Mindy Pereira | c0a34ab | 2011-09-13 15:17:39 -0700 | [diff] [blame] | 2469 | // If the user is editing a chip, don't clear it. |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2470 | if (mSelectedChip != null) { |
| 2471 | if (!isGeneratedContact(mSelectedChip)) { |
| 2472 | setCursorVisible(true); |
| 2473 | setSelection(getText().length()); |
| 2474 | clearSelectedChip(); |
| 2475 | } else { |
| 2476 | return; |
| 2477 | } |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2478 | } |
| 2479 | int length = s.length(); |
| 2480 | // Make sure there is content there to parse and that it is |
Mindy Pereira | ab5a964 | 2011-06-23 17:23:22 -0700 | [diff] [blame] | 2481 | // not just the commit character. |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2482 | if (length > 1) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2483 | if (lastCharacterIsCommitCharacter(s)) { |
| 2484 | commitByCharacter(); |
| 2485 | return; |
| 2486 | } |
Mindy Pereira | ab5a964 | 2011-06-23 17:23:22 -0700 | [diff] [blame] | 2487 | char last; |
Mindy Pereira | 11a2adb | 2011-06-28 09:57:43 -0700 | [diff] [blame] | 2488 | int end = getSelectionEnd() == 0 ? 0 : getSelectionEnd() - 1; |
Mindy Pereira | ab5a964 | 2011-06-23 17:23:22 -0700 | [diff] [blame] | 2489 | int len = length() - 1; |
| 2490 | if (end != len) { |
| 2491 | last = s.charAt(end); |
| 2492 | } else { |
| 2493 | last = s.charAt(len); |
| 2494 | } |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2495 | if (last == COMMIT_CHAR_SPACE) { |
Tom Taylor | 4afc73e | 2012-05-04 10:21:17 -0700 | [diff] [blame] | 2496 | if (!isPhoneQuery()) { |
| 2497 | // Check if this is a valid email address. If it is, |
| 2498 | // commit it. |
| 2499 | String text = getText().toString(); |
| 2500 | int tokenStart = mTokenizer.findTokenStart(text, getSelectionEnd()); |
| 2501 | String sub = text.substring(tokenStart, mTokenizer.findTokenEnd(text, |
| 2502 | tokenStart)); |
Jin Cao | b3b248a | 2014-04-11 10:43:50 -0700 | [diff] [blame] | 2503 | if (isValidEmailAddress(sub)) { |
Tom Taylor | 4afc73e | 2012-05-04 10:21:17 -0700 | [diff] [blame] | 2504 | commitByCharacter(); |
| 2505 | } |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | @Override |
| 2512 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2513 | // The user deleted some text OR some text was replaced; check to |
| 2514 | // see if the insertion point is on a space |
Mindy Pereira | 72a2339 | 2012-02-06 10:19:51 -0800 | [diff] [blame] | 2515 | // following a chip. |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2516 | if (before - count == 1) { |
Mindy Pereira | 72a2339 | 2012-02-06 10:19:51 -0800 | [diff] [blame] | 2517 | // If the item deleted is a space, and the thing before the |
| 2518 | // space is a chip, delete the entire span. |
| 2519 | int selStart = getSelectionStart(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2520 | DrawableRecipientChip[] repl = getSpannable().getSpans(selStart, selStart, |
| 2521 | DrawableRecipientChip.class); |
Mindy Pereira | 72a2339 | 2012-02-06 10:19:51 -0800 | [diff] [blame] | 2522 | if (repl.length > 0) { |
| 2523 | // There is a chip there! Just remove it. |
| 2524 | Editable editable = getText(); |
| 2525 | // Add the separator token. |
| 2526 | int tokenStart = mTokenizer.findTokenStart(editable, selStart); |
| 2527 | int tokenEnd = mTokenizer.findTokenEnd(editable, tokenStart); |
Mindy Pereira | 62fce93 | 2012-02-16 15:00:30 -0800 | [diff] [blame] | 2528 | tokenEnd = tokenEnd + 1; |
| 2529 | if (tokenEnd > editable.length()) { |
| 2530 | tokenEnd = editable.length(); |
| 2531 | } |
| 2532 | editable.delete(tokenStart, tokenEnd); |
Mindy Pereira | 72a2339 | 2012-02-06 10:19:51 -0800 | [diff] [blame] | 2533 | getSpannable().removeSpan(repl[0]); |
| 2534 | } |
Mindy Pereira | 4e7d20f | 2012-07-16 08:09:01 -0700 | [diff] [blame] | 2535 | } else if (count > before) { |
mindyp | 7e10c86 | 2012-11-27 11:13:26 -0800 | [diff] [blame] | 2536 | if (mSelectedChip != null |
| 2537 | && isGeneratedContact(mSelectedChip)) { |
| 2538 | if (lastCharacterIsCommitCharacter(s)) { |
| 2539 | commitByCharacter(); |
| 2540 | return; |
| 2541 | } |
| 2542 | } |
Mindy Pereira | 72a2339 | 2012-02-06 10:19:51 -0800 | [diff] [blame] | 2543 | } |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | @Override |
| 2547 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
Mindy Pereira | dc04cd7 | 2011-09-16 15:42:09 -0700 | [diff] [blame] | 2548 | // Do nothing. |
Mindy Pereira | 79fb436 | 2011-06-21 14:37:07 -0700 | [diff] [blame] | 2549 | } |
| 2550 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2551 | |
Alice Yang | c1b5258 | 2013-03-04 19:54:00 -0800 | [diff] [blame] | 2552 | public boolean lastCharacterIsCommitCharacter(CharSequence s) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2553 | char last; |
| 2554 | int end = getSelectionEnd() == 0 ? 0 : getSelectionEnd() - 1; |
| 2555 | int len = length() - 1; |
| 2556 | if (end != len) { |
| 2557 | last = s.charAt(end); |
| 2558 | } else { |
| 2559 | last = s.charAt(len); |
| 2560 | } |
| 2561 | return last == COMMIT_CHAR_COMMA || last == COMMIT_CHAR_SEMICOLON; |
| 2562 | } |
| 2563 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2564 | public boolean isGeneratedContact(DrawableRecipientChip chip) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 2565 | long contactId = chip.getContactId(); |
| 2566 | return contactId == RecipientEntry.INVALID_CONTACT |
| 2567 | || (!isPhoneQuery() && contactId == RecipientEntry.GENERATED_CONTACT); |
| 2568 | } |
| 2569 | |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 2570 | /** |
| 2571 | * Handles pasting a {@link ClipData} to this {@link RecipientEditTextView}. |
| 2572 | */ |
Régis Décamps | 219cbfb | 2014-06-04 14:29:54 +0200 | [diff] [blame] | 2573 | // Visible for testing. |
| 2574 | void handlePasteClip(ClipData clip) { |
| 2575 | if (clip == null) { |
| 2576 | // Do nothing. |
| 2577 | return; |
| 2578 | } |
| 2579 | |
| 2580 | final ClipDescription clipDesc = clip.getDescription(); |
| 2581 | boolean containsSupportedType = clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) || |
| 2582 | clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML); |
| 2583 | if (!containsSupportedType) { |
| 2584 | return; |
| 2585 | } |
| 2586 | |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 2587 | removeTextChangedListener(mTextWatcher); |
| 2588 | |
Régis Décamps | 219cbfb | 2014-06-04 14:29:54 +0200 | [diff] [blame] | 2589 | final ClipDescription clipDescription = clip.getDescription(); |
| 2590 | for (int i = 0; i < clip.getItemCount(); i++) { |
| 2591 | final String mimeType = clipDescription.getMimeType(i); |
| 2592 | final boolean supportedType = ClipDescription.MIMETYPE_TEXT_PLAIN.equals(mimeType) || |
| 2593 | ClipDescription.MIMETYPE_TEXT_HTML.equals(mimeType); |
| 2594 | if (!supportedType) { |
| 2595 | // Only plain text and html can be pasted. |
| 2596 | continue; |
| 2597 | } |
| 2598 | |
| 2599 | final CharSequence pastedItem = clip.getItemAt(i).getText(); |
| 2600 | if (!TextUtils.isEmpty(pastedItem)) { |
| 2601 | final Editable editable = getText(); |
| 2602 | final int start = getSelectionStart(); |
| 2603 | final int end = getSelectionEnd(); |
| 2604 | if (start < 0 || end < 1) { |
| 2605 | // No selection. |
| 2606 | editable.append(pastedItem); |
| 2607 | } else if (start == end) { |
| 2608 | // Insert at position. |
| 2609 | editable.insert(start, pastedItem); |
| 2610 | } else { |
| 2611 | editable.append(pastedItem, start, end); |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 2612 | } |
Régis Décamps | 219cbfb | 2014-06-04 14:29:54 +0200 | [diff] [blame] | 2613 | handlePasteAndReplace(); |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | mHandler.post(mAddTextWatcher); |
| 2618 | } |
| 2619 | |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2620 | @Override |
| 2621 | public boolean onTextContextMenuItem(int id) { |
| 2622 | if (id == android.R.id.paste) { |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2623 | ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService( |
| 2624 | Context.CLIPBOARD_SERVICE); |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 2625 | handlePasteClip(clipboard.getPrimaryClip()); |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2626 | return true; |
| 2627 | } |
| 2628 | return super.onTextContextMenuItem(id); |
| 2629 | } |
| 2630 | |
Mindy Pereira | b4e244a | 2011-10-14 11:13:29 -0700 | [diff] [blame] | 2631 | private void handlePasteAndReplace() { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2632 | ArrayList<DrawableRecipientChip> created = handlePaste(); |
Mindy Pereira | b4e244a | 2011-10-14 11:13:29 -0700 | [diff] [blame] | 2633 | if (created != null && created.size() > 0) { |
| 2634 | // Perform reverse lookups on the pasted contacts. |
| 2635 | IndividualReplacementTask replace = new IndividualReplacementTask(); |
| 2636 | replace.execute(created); |
| 2637 | } |
| 2638 | } |
| 2639 | |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2640 | // Visible for testing. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2641 | /* package */ArrayList<DrawableRecipientChip> handlePaste() { |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2642 | String text = getText().toString(); |
| 2643 | int originalTokenStart = mTokenizer.findTokenStart(text, getSelectionEnd()); |
| 2644 | String lastAddress = text.substring(originalTokenStart); |
| 2645 | int tokenStart = originalTokenStart; |
jli119X | 0cae850 | 2012-10-18 12:55:47 +0800 | [diff] [blame] | 2646 | int prevTokenStart = 0; |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2647 | DrawableRecipientChip findChip = null; |
| 2648 | ArrayList<DrawableRecipientChip> created = new ArrayList<DrawableRecipientChip>(); |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2649 | if (tokenStart != 0) { |
| 2650 | // There are things before this! |
jli119X | 0cae850 | 2012-10-18 12:55:47 +0800 | [diff] [blame] | 2651 | while (tokenStart != 0 && findChip == null && tokenStart != prevTokenStart) { |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2652 | prevTokenStart = tokenStart; |
| 2653 | tokenStart = mTokenizer.findTokenStart(text, tokenStart); |
| 2654 | findChip = findChip(tokenStart); |
Tom Taylor | fda0170 | 2013-01-14 16:52:25 -0800 | [diff] [blame] | 2655 | if (tokenStart == originalTokenStart && findChip == null) { |
| 2656 | break; |
| 2657 | } |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2658 | } |
| 2659 | if (tokenStart != originalTokenStart) { |
| 2660 | if (findChip != null) { |
| 2661 | tokenStart = prevTokenStart; |
| 2662 | } |
| 2663 | int tokenEnd; |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2664 | DrawableRecipientChip createdChip; |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2665 | while (tokenStart < originalTokenStart) { |
mindyp | b5afbc7 | 2012-10-03 11:57:42 -0700 | [diff] [blame] | 2666 | tokenEnd = movePastTerminators(mTokenizer.findTokenEnd(getText().toString(), |
| 2667 | tokenStart)); |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2668 | commitChip(tokenStart, tokenEnd, getText()); |
| 2669 | createdChip = findChip(tokenStart); |
Mindy Pereira | c52ea8e | 2012-06-04 15:24:55 -0700 | [diff] [blame] | 2670 | if (createdChip == null) { |
| 2671 | break; |
| 2672 | } |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2673 | // +1 for the space at the end. |
| 2674 | tokenStart = getSpannable().getSpanEnd(createdChip) + 1; |
Mindy Pereira | b4e244a | 2011-10-14 11:13:29 -0700 | [diff] [blame] | 2675 | created.add(createdChip); |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2676 | } |
| 2677 | } |
| 2678 | } |
| 2679 | // Take a look at the last token. If the token has been completed with a |
| 2680 | // commit character, create a chip. |
| 2681 | if (isCompletedToken(lastAddress)) { |
| 2682 | Editable editable = getText(); |
Mindy Pereira | b4e244a | 2011-10-14 11:13:29 -0700 | [diff] [blame] | 2683 | tokenStart = editable.toString().indexOf(lastAddress, originalTokenStart); |
| 2684 | commitChip(tokenStart, editable.length(), editable); |
| 2685 | created.add(findChip(tokenStart)); |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2686 | } |
Mindy Pereira | b4e244a | 2011-10-14 11:13:29 -0700 | [diff] [blame] | 2687 | return created; |
Mindy Pereira | 20c9d62 | 2011-10-12 13:18:04 -0700 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | // Visible for testing. |
| 2691 | /* package */int movePastTerminators(int tokenEnd) { |
| 2692 | if (tokenEnd >= length()) { |
| 2693 | return tokenEnd; |
| 2694 | } |
| 2695 | char atEnd = getText().toString().charAt(tokenEnd); |
| 2696 | if (atEnd == COMMIT_CHAR_COMMA || atEnd == COMMIT_CHAR_SEMICOLON) { |
| 2697 | tokenEnd++; |
| 2698 | } |
| 2699 | // This token had not only an end token character, but also a space |
| 2700 | // separating it from the next token. |
| 2701 | if (tokenEnd < length() && getText().toString().charAt(tokenEnd) == ' ') { |
| 2702 | tokenEnd++; |
| 2703 | } |
| 2704 | return tokenEnd; |
| 2705 | } |
| 2706 | |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2707 | private class RecipientReplacementTask extends AsyncTask<Void, Void, Void> { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2708 | private DrawableRecipientChip createFreeChip(RecipientEntry entry) { |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2709 | try { |
Mindy Pereira | f6519d2 | 2011-11-02 13:33:18 -0700 | [diff] [blame] | 2710 | if (mNoChips) { |
| 2711 | return null; |
| 2712 | } |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 2713 | return constructChipSpan(entry, false); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2714 | } catch (NullPointerException e) { |
| 2715 | Log.e(TAG, e.getMessage(), e); |
| 2716 | return null; |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | @Override |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2721 | protected void onPreExecute() { |
| 2722 | // Ensure everything is in chip-form already, so we don't have text that slowly gets |
| 2723 | // replaced |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2724 | final List<DrawableRecipientChip> originalRecipients = |
| 2725 | new ArrayList<DrawableRecipientChip>(); |
| 2726 | final DrawableRecipientChip[] existingChips = getSortedRecipients(); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2727 | for (int i = 0; i < existingChips.length; i++) { |
| 2728 | originalRecipients.add(existingChips[i]); |
| 2729 | } |
| 2730 | if (mRemovedSpans != null) { |
| 2731 | originalRecipients.addAll(mRemovedSpans); |
| 2732 | } |
| 2733 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2734 | final List<DrawableRecipientChip> replacements = |
| 2735 | new ArrayList<DrawableRecipientChip>(originalRecipients.size()); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2736 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2737 | for (final DrawableRecipientChip chip : originalRecipients) { |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2738 | if (RecipientEntry.isCreatedRecipient(chip.getEntry().getContactId()) |
| 2739 | && getSpannable().getSpanStart(chip) != -1) { |
| 2740 | replacements.add(createFreeChip(chip.getEntry())); |
| 2741 | } else { |
| 2742 | replacements.add(null); |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | processReplacements(originalRecipients, replacements); |
| 2747 | } |
| 2748 | |
| 2749 | @Override |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2750 | protected Void doInBackground(Void... params) { |
| 2751 | if (mIndividualReplacements != null) { |
| 2752 | mIndividualReplacements.cancel(true); |
| 2753 | } |
| 2754 | // For each chip in the list, look up the matching contact. |
| 2755 | // If there is a match, replace that chip with the matching |
| 2756 | // chip. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2757 | final ArrayList<DrawableRecipientChip> recipients = |
| 2758 | new ArrayList<DrawableRecipientChip>(); |
| 2759 | DrawableRecipientChip[] existingChips = getSortedRecipients(); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2760 | for (int i = 0; i < existingChips.length; i++) { |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2761 | recipients.add(existingChips[i]); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2762 | } |
| 2763 | if (mRemovedSpans != null) { |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2764 | recipients.addAll(mRemovedSpans); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2765 | } |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 2766 | ArrayList<String> addresses = new ArrayList<String>(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2767 | DrawableRecipientChip chip; |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2768 | for (int i = 0; i < recipients.size(); i++) { |
| 2769 | chip = recipients.get(i); |
Mindy Pereira | f07e8b2 | 2011-12-06 16:48:29 -0800 | [diff] [blame] | 2770 | if (chip != null) { |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 2771 | addresses.add(createAddressText(chip.getEntry())); |
Mindy Pereira | f07e8b2 | 2011-12-06 16:48:29 -0800 | [diff] [blame] | 2772 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2773 | } |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 2774 | final BaseRecipientAdapter adapter = getAdapter(); |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 2775 | adapter.getMatchingRecipients(addresses, new RecipientMatchCallback() { |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2776 | @Override |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2777 | public void matchesFound(Map<String, RecipientEntry> entries) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2778 | final ArrayList<DrawableRecipientChip> replacements = |
| 2779 | new ArrayList<DrawableRecipientChip>(); |
| 2780 | for (final DrawableRecipientChip temp : recipients) { |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2781 | RecipientEntry entry = null; |
Alice Yang | f0579ee | 2013-03-04 14:10:21 -0800 | [diff] [blame] | 2782 | if (temp != null && RecipientEntry.isCreatedRecipient( |
| 2783 | temp.getEntry().getContactId()) |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2784 | && getSpannable().getSpanStart(temp) != -1) { |
| 2785 | // Replace this. |
| 2786 | entry = createValidatedEntry( |
| 2787 | entries.get(tokenizeAddress(temp.getEntry() |
| 2788 | .getDestination()))); |
| 2789 | } |
| 2790 | if (entry != null) { |
| 2791 | replacements.add(createFreeChip(entry)); |
| 2792 | } else { |
| 2793 | replacements.add(null); |
| 2794 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2795 | } |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2796 | processReplacements(recipients, replacements); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2797 | } |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2798 | |
| 2799 | @Override |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 2800 | public void matchesNotFound(final Set<String> unfoundAddresses) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2801 | final List<DrawableRecipientChip> replacements = |
| 2802 | new ArrayList<DrawableRecipientChip>(unfoundAddresses.size()); |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2803 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2804 | for (final DrawableRecipientChip temp : recipients) { |
Alice Yang | ad8781e | 2013-03-04 14:45:38 -0800 | [diff] [blame] | 2805 | if (temp != null && RecipientEntry.isCreatedRecipient( |
| 2806 | temp.getEntry().getContactId()) |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2807 | && getSpannable().getSpanStart(temp) != -1) { |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 2808 | if (unfoundAddresses.contains( |
| 2809 | temp.getEntry().getDestination())) { |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2810 | replacements.add(createFreeChip(temp.getEntry())); |
| 2811 | } else { |
| 2812 | replacements.add(null); |
| 2813 | } |
| 2814 | } else { |
| 2815 | replacements.add(null); |
| 2816 | } |
| 2817 | } |
| 2818 | |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2819 | processReplacements(recipients, replacements); |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2820 | } |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2821 | }); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2822 | return null; |
| 2823 | } |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2824 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2825 | private void processReplacements(final List<DrawableRecipientChip> recipients, |
| 2826 | final List<DrawableRecipientChip> replacements) { |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2827 | if (replacements != null && replacements.size() > 0) { |
| 2828 | final Runnable runnable = new Runnable() { |
| 2829 | @Override |
| 2830 | public void run() { |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2831 | final Editable text = new SpannableStringBuilder(getText()); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2832 | int i = 0; |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2833 | for (final DrawableRecipientChip chip : recipients) { |
| 2834 | final DrawableRecipientChip replacement = replacements.get(i); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2835 | if (replacement != null) { |
| 2836 | final RecipientEntry oldEntry = chip.getEntry(); |
| 2837 | final RecipientEntry newEntry = replacement.getEntry(); |
| 2838 | final boolean isBetter = |
| 2839 | RecipientAlternatesAdapter.getBetterRecipient( |
| 2840 | oldEntry, newEntry) == newEntry; |
| 2841 | |
| 2842 | if (isBetter) { |
| 2843 | // Find the location of the chip in the text currently shown. |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2844 | final int start = text.getSpanStart(chip); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2845 | if (start != -1) { |
| 2846 | // Replacing the entirety of what the chip represented, |
| 2847 | // including the extra space dividing it from other chips. |
Paul Westbrook | b023f69 | 2013-04-22 12:27:35 -0700 | [diff] [blame] | 2848 | final int end = |
| 2849 | Math.min(text.getSpanEnd(chip) + 1, text.length()); |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2850 | text.removeSpan(chip); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2851 | // Make sure we always have just 1 space at the end to |
| 2852 | // separate this chip from the next chip. |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2853 | final SpannableString displayText = |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2854 | new SpannableString(createAddressText( |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2855 | replacement.getEntry()).trim() + " "); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2856 | displayText.setSpan(replacement, 0, |
| 2857 | displayText.length() - 1, |
| 2858 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 2859 | // Replace the old text we found with with the new display |
| 2860 | // text, which now may also contain the display name of the |
| 2861 | // recipient. |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2862 | text.replace(start, end, displayText); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2863 | replacement.setOriginalText(displayText.toString()); |
| 2864 | replacements.set(i, null); |
| 2865 | |
| 2866 | recipients.set(i, replacement); |
| 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | i++; |
| 2871 | } |
Scott Kennedy | 0e12896 | 2013-04-11 10:52:08 -0700 | [diff] [blame] | 2872 | setText(text); |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2873 | } |
| 2874 | }; |
| 2875 | |
| 2876 | if (Looper.myLooper() == Looper.getMainLooper()) { |
| 2877 | runnable.run(); |
| 2878 | } else { |
| 2879 | mHandler.post(runnable); |
| 2880 | } |
| 2881 | } |
| 2882 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Scott Kennedy | 78f38a0 | 2013-02-27 11:21:40 -0800 | [diff] [blame] | 2885 | private class IndividualReplacementTask |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2886 | extends AsyncTask<ArrayList<DrawableRecipientChip>, Void, Void> { |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2887 | @Override |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2888 | protected Void doInBackground(ArrayList<DrawableRecipientChip>... params) { |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2889 | // For each chip in the list, look up the matching contact. |
| 2890 | // If there is a match, replace that chip with the matching |
| 2891 | // chip. |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2892 | final ArrayList<DrawableRecipientChip> originalRecipients = params[0]; |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 2893 | ArrayList<String> addresses = new ArrayList<String>(); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2894 | DrawableRecipientChip chip; |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2895 | for (int i = 0; i < originalRecipients.size(); i++) { |
Mindy Pereira | f07e8b2 | 2011-12-06 16:48:29 -0800 | [diff] [blame] | 2896 | chip = originalRecipients.get(i); |
| 2897 | if (chip != null) { |
Mindy Pereira | 03cfe3e | 2012-05-24 13:21:59 -0700 | [diff] [blame] | 2898 | addresses.add(createAddressText(chip.getEntry())); |
Mindy Pereira | f07e8b2 | 2011-12-06 16:48:29 -0800 | [diff] [blame] | 2899 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2900 | } |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 2901 | final BaseRecipientAdapter adapter = getAdapter(); |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 2902 | adapter.getMatchingRecipients(addresses, new RecipientMatchCallback() { |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2903 | |
| 2904 | @Override |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2905 | public void matchesFound(Map<String, RecipientEntry> entries) { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2906 | for (final DrawableRecipientChip temp : originalRecipients) { |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2907 | if (RecipientEntry.isCreatedRecipient(temp.getEntry() |
| 2908 | .getContactId()) |
| 2909 | && getSpannable().getSpanStart(temp) != -1) { |
| 2910 | // Replace this. |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 2911 | final RecipientEntry entry = createValidatedEntry(entries |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2912 | .get(tokenizeAddress(temp.getEntry().getDestination()) |
| 2913 | .toLowerCase())); |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 2914 | if (entry != null) { |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2915 | mHandler.post(new Runnable() { |
| 2916 | @Override |
| 2917 | public void run() { |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 2918 | replaceChip(temp, entry); |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2919 | } |
| 2920 | }); |
| 2921 | } |
| 2922 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2923 | } |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2924 | } |
| 2925 | |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2926 | @Override |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 2927 | public void matchesNotFound(final Set<String> unfoundAddresses) { |
Scott Kennedy | 94fa301 | 2013-02-15 18:22:05 -0800 | [diff] [blame] | 2928 | // No action required |
| 2929 | } |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 2930 | }); |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 2931 | return null; |
| 2932 | } |
| 2933 | } |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 2934 | |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2935 | |
| 2936 | /** |
| 2937 | * MoreImageSpan is a simple class created for tracking the existence of a |
| 2938 | * more chip across activity restarts/ |
| 2939 | */ |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2940 | private class MoreImageSpan extends ReplacementDrawableSpan { |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2941 | public MoreImageSpan(Drawable b) { |
| 2942 | super(b); |
Jin Cao | 6c2a016 | 2014-08-17 15:00:50 -0700 | [diff] [blame] | 2943 | setExtraMargin(mLineSpacingExtra); |
Mindy Pereira | e1bfc92 | 2011-08-16 14:00:01 -0700 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 2947 | @Override |
| 2948 | public boolean onDown(MotionEvent e) { |
| 2949 | return false; |
| 2950 | } |
| 2951 | |
| 2952 | @Override |
| 2953 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { |
| 2954 | // Do nothing. |
| 2955 | return false; |
| 2956 | } |
| 2957 | |
| 2958 | @Override |
| 2959 | public void onLongPress(MotionEvent event) { |
| 2960 | if (mSelectedChip != null) { |
| 2961 | return; |
| 2962 | } |
| 2963 | float x = event.getX(); |
| 2964 | float y = event.getY(); |
Scott Kennedy | 1650ef6 | 2013-08-02 21:56:47 -0700 | [diff] [blame] | 2965 | final int offset = putOffsetInRange(x, y); |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 2966 | DrawableRecipientChip currentChip = findChip(offset); |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 2967 | if (currentChip != null) { |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 2968 | if (mDragEnabled) { |
| 2969 | // Start drag-and-drop for the selected chip. |
| 2970 | startDrag(currentChip); |
| 2971 | } else { |
| 2972 | // Copy the selected chip email address. |
| 2973 | showCopyDialog(currentChip.getEntry().getDestination()); |
| 2974 | } |
| 2975 | } |
| 2976 | } |
| 2977 | |
Scott Kennedy | 1650ef6 | 2013-08-02 21:56:47 -0700 | [diff] [blame] | 2978 | // The following methods are used to provide some functionality on older versions of Android |
| 2979 | // These methods were copied out of JB MR2's TextView |
| 2980 | ///////////////////////////////////////////////// |
| 2981 | private int supportGetOffsetForPosition(float x, float y) { |
| 2982 | if (getLayout() == null) return -1; |
| 2983 | final int line = supportGetLineAtCoordinate(y); |
| 2984 | final int offset = supportGetOffsetAtCoordinate(line, x); |
| 2985 | return offset; |
| 2986 | } |
| 2987 | |
| 2988 | private float supportConvertToLocalHorizontalCoordinate(float x) { |
| 2989 | x -= getTotalPaddingLeft(); |
| 2990 | // Clamp the position to inside of the view. |
| 2991 | x = Math.max(0.0f, x); |
| 2992 | x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); |
| 2993 | x += getScrollX(); |
| 2994 | return x; |
| 2995 | } |
| 2996 | |
| 2997 | private int supportGetLineAtCoordinate(float y) { |
| 2998 | y -= getTotalPaddingLeft(); |
| 2999 | // Clamp the position to inside of the view. |
| 3000 | y = Math.max(0.0f, y); |
| 3001 | y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); |
| 3002 | y += getScrollY(); |
| 3003 | return getLayout().getLineForVertical((int) y); |
| 3004 | } |
| 3005 | |
| 3006 | private int supportGetOffsetAtCoordinate(int line, float x) { |
| 3007 | x = supportConvertToLocalHorizontalCoordinate(x); |
| 3008 | return getLayout().getOffsetForHorizontal(line, x); |
| 3009 | } |
| 3010 | ///////////////////////////////////////////////// |
| 3011 | |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 3012 | /** |
| 3013 | * Enables drag-and-drop for chips. |
| 3014 | */ |
| 3015 | public void enableDrag() { |
| 3016 | mDragEnabled = true; |
| 3017 | } |
| 3018 | |
| 3019 | /** |
| 3020 | * Starts drag-and-drop for the selected chip. |
| 3021 | */ |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 3022 | private void startDrag(DrawableRecipientChip currentChip) { |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 3023 | String address = currentChip.getEntry().getDestination(); |
| 3024 | ClipData data = ClipData.newPlainText(address, address + COMMIT_CHAR_COMMA); |
| 3025 | |
| 3026 | // Start drag mode. |
| 3027 | startDrag(data, new RecipientChipShadow(currentChip), null, 0); |
| 3028 | |
| 3029 | // Remove the current chip, so drag-and-drop will result in a move. |
| 3030 | // TODO (phamm): consider readd this chip if it's dropped outside a target. |
| 3031 | removeChip(currentChip); |
| 3032 | } |
| 3033 | |
| 3034 | /** |
| 3035 | * Handles drag event. |
| 3036 | */ |
| 3037 | @Override |
| 3038 | public boolean onDragEvent(DragEvent event) { |
| 3039 | switch (event.getAction()) { |
| 3040 | case DragEvent.ACTION_DRAG_STARTED: |
| 3041 | // Only handle plain text drag and drop. |
| 3042 | return event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN); |
| 3043 | case DragEvent.ACTION_DRAG_ENTERED: |
| 3044 | requestFocus(); |
| 3045 | return true; |
| 3046 | case DragEvent.ACTION_DROP: |
| 3047 | handlePasteClip(event.getClipData()); |
| 3048 | return true; |
| 3049 | } |
| 3050 | return false; |
| 3051 | } |
| 3052 | |
| 3053 | /** |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 3054 | * Drag shadow for a {@link DrawableRecipientChip}. |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 3055 | */ |
| 3056 | private final class RecipientChipShadow extends DragShadowBuilder { |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 3057 | private final DrawableRecipientChip mChip; |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 3058 | |
Scott Kennedy | 194d427 | 2013-03-06 22:06:45 -0800 | [diff] [blame] | 3059 | public RecipientChipShadow(DrawableRecipientChip chip) { |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 3060 | mChip = chip; |
| 3061 | } |
| 3062 | |
| 3063 | @Override |
| 3064 | public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 3065 | Rect rect = mChip.getBounds(); |
Minh Pham | e51c722 | 2011-10-12 11:33:26 -0700 | [diff] [blame] | 3066 | shadowSize.set(rect.width(), rect.height()); |
| 3067 | shadowTouchPoint.set(rect.centerX(), rect.centerY()); |
| 3068 | } |
| 3069 | |
| 3070 | @Override |
| 3071 | public void onDrawShadow(Canvas canvas) { |
mindyp | f30a428 | 2012-12-03 12:16:33 -0800 | [diff] [blame] | 3072 | mChip.draw(canvas); |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | private void showCopyDialog(final String address) { |
Tony Mantler | fbeca0e | 2013-10-15 14:53:00 -0700 | [diff] [blame] | 3077 | if (!mAttachedToWindow) { |
| 3078 | return; |
| 3079 | } |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 3080 | mCopyAddress = address; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 3081 | mCopyDialog.setTitle(address); |
Mindy Pereira | 76e62e3 | 2012-02-03 15:38:50 -0800 | [diff] [blame] | 3082 | mCopyDialog.setContentView(R.layout.copy_chip_dialog_layout); |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 3083 | mCopyDialog.setCancelable(true); |
| 3084 | mCopyDialog.setCanceledOnTouchOutside(true); |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 3085 | Button button = (Button)mCopyDialog.findViewById(android.R.id.button1); |
| 3086 | button.setOnClickListener(this); |
| 3087 | int btnTitleId; |
Tom Taylor | c7a87f0 | 2012-05-03 15:21:08 -0700 | [diff] [blame] | 3088 | if (isPhoneQuery()) { |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 3089 | btnTitleId = R.string.copy_number; |
| 3090 | } else { |
| 3091 | btnTitleId = R.string.copy_email; |
| 3092 | } |
| 3093 | String buttonTitle = getContext().getResources().getString(btnTitleId); |
| 3094 | button.setText(buttonTitle); |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 3095 | mCopyDialog.setOnDismissListener(this); |
| 3096 | mCopyDialog.show(); |
| 3097 | } |
| 3098 | |
| 3099 | @Override |
| 3100 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { |
| 3101 | // Do nothing. |
| 3102 | return false; |
| 3103 | } |
| 3104 | |
| 3105 | @Override |
| 3106 | public void onShowPress(MotionEvent e) { |
| 3107 | // Do nothing. |
| 3108 | } |
| 3109 | |
| 3110 | @Override |
| 3111 | public boolean onSingleTapUp(MotionEvent e) { |
| 3112 | // Do nothing. |
| 3113 | return false; |
| 3114 | } |
| 3115 | |
| 3116 | @Override |
| 3117 | public void onDismiss(DialogInterface dialog) { |
| 3118 | mCopyAddress = null; |
Mindy Pereira | 1d1ec85 | 2011-08-09 16:49:33 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | @Override |
| 3122 | public void onClick(View v) { |
| 3123 | // Copy this to the clipboard. |
| 3124 | ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService( |
| 3125 | Context.CLIPBOARD_SERVICE); |
| 3126 | clipboard.setPrimaryClip(ClipData.newPlainText("", mCopyAddress)); |
| 3127 | mCopyDialog.dismiss(); |
| 3128 | } |
Tom Taylor | c7a87f0 | 2012-05-03 15:21:08 -0700 | [diff] [blame] | 3129 | |
| 3130 | protected boolean isPhoneQuery() { |
Mindy Pereira | ca52271 | 2012-06-28 11:47:48 -0700 | [diff] [blame] | 3131 | return getAdapter() != null |
Scott Kennedy | 858e094 | 2013-10-10 11:50:01 -0700 | [diff] [blame] | 3132 | && getAdapter().getQueryType() == BaseRecipientAdapter.QUERY_TYPE_PHONE; |
| 3133 | } |
| 3134 | |
| 3135 | @Override |
| 3136 | public BaseRecipientAdapter getAdapter() { |
| 3137 | return (BaseRecipientAdapter) super.getAdapter(); |
Tom Taylor | c7a87f0 | 2012-05-03 15:21:08 -0700 | [diff] [blame] | 3138 | } |
Kaikai Wang | 972def7 | 2014-05-27 21:59:52 -0700 | [diff] [blame] | 3139 | |
| 3140 | /** |
| 3141 | * Append a new {@link RecipientEntry} to the end of the recipient chips, leaving any |
| 3142 | * unfinished text at the end. |
| 3143 | */ |
| 3144 | public void appendRecipientEntry(final RecipientEntry entry) { |
| 3145 | clearComposingText(); |
| 3146 | |
| 3147 | final Editable editable = getText(); |
| 3148 | int chipInsertionPoint = 0; |
| 3149 | |
| 3150 | // Find the end of last chip and see if there's any unchipified text. |
| 3151 | final DrawableRecipientChip[] recips = getSortedRecipients(); |
| 3152 | if (recips != null && recips.length > 0) { |
| 3153 | final DrawableRecipientChip last = recips[recips.length - 1]; |
| 3154 | // The chip will be inserted at the end of last chip + 1. All the unfinished text after |
| 3155 | // the insertion point will be kept untouched. |
| 3156 | chipInsertionPoint = editable.getSpanEnd(last) + 1; |
| 3157 | } |
| 3158 | |
| 3159 | final CharSequence chip = createChip(entry, false); |
| 3160 | if (chip != null) { |
| 3161 | editable.insert(chipInsertionPoint, chip); |
| 3162 | } |
| 3163 | } |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 3164 | |
Kaikai Wang | cd980d9 | 2014-06-09 21:08:13 -0700 | [diff] [blame] | 3165 | /** |
| 3166 | * Remove all chips matching the given RecipientEntry. |
| 3167 | */ |
| 3168 | public void removeRecipientEntry(final RecipientEntry entry) { |
| 3169 | final DrawableRecipientChip[] recips = getText() |
| 3170 | .getSpans(0, getText().length(), DrawableRecipientChip.class); |
| 3171 | |
| 3172 | for (final DrawableRecipientChip recipient : recips) { |
| 3173 | final RecipientEntry existingEntry = recipient.getEntry(); |
| 3174 | if (existingEntry != null && existingEntry.isValid() && |
| 3175 | existingEntry.isSamePerson(entry)) { |
| 3176 | removeChip(recipient); |
| 3177 | } |
| 3178 | } |
| 3179 | } |
| 3180 | |
Jin Cao | 4de6a53 | 2014-08-08 11:34:10 -0700 | [diff] [blame] | 3181 | public void setAlternatePopupAnchor(View v) { |
| 3182 | mAlternatePopupAnchor = v; |
| 3183 | } |
| 3184 | |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 3185 | private static class ChipBitmapContainer { |
| 3186 | Bitmap bitmap; |
| 3187 | // information used for positioning the loaded icon |
| 3188 | boolean loadIcon = true; |
| 3189 | float left; |
| 3190 | float top; |
| 3191 | float right; |
| 3192 | float bottom; |
| 3193 | } |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame] | 3194 | } |