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