/
ConversationMultiAutoCompleteTextView.java
628 lines (556 loc) · 22.6 KB
/
ConversationMultiAutoCompleteTextView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
* Copyright (C) 2008 Esmertec AG.
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.borqs.common.view;
import android.content.Context;
import android.database.Cursor;
import android.text.Annotation;
import android.text.Editable;
import android.text.Layout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.MotionEvent;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
import com.borqs.common.api.BpcApiUtils;
import com.borqs.common.util.HtmlUtils;
import com.borqs.qiupu.R;
import java.util.ArrayList;
import java.util.List;
/**
* Provide UI for editing the recipients of multi-media messages.
*/
public class ConversationMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
private int mLongPressedPosition = -1;
private final RecipientsEditorTokenizer mTokenizer;
public ConversationMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs, android.R.attr.autoCompleteTextViewStyle);
mTokenizer = new RecipientsEditorTokenizer(context, this);
setTokenizer(mTokenizer);
// For the focus to move to the message body when soft Next is pressed
setImeOptions(EditorInfo.IME_ACTION_NEXT);
setThreshold(1);
final ConversationAutoCompleteTextView.ConversationAdapter adapter = new ConversationAutoCompleteTextView.ConversationAdapter(context);
setAdapter(adapter);
setOnItemClickListener(adapter.getItemClickListener());
/*
* The point of this TextWatcher is that when the user chooses
* an address completion from the AutoCompleteTextView menu, it
* is marked up with Annotation objects to tie it back to the
* address book entry that it came from. If the user then goes
* back and edits that part of the text, it no longer corresponds
* to that address book entry and needs to have the Annotations
* claiming that it does removed.
*/
addTextChangedListener(new TextWatcher() {
private Annotation[] mAffected;
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
mAffected = ((Spanned) s).getSpans(start, start + count,
Annotation.class);
}
public void onTextChanged(CharSequence s, int start,
int before, int after) {
if (before == 0 && after == 1) { // inserting a character
char c = s.charAt(start);
if (isMentionTrigger(c)) {
// Remember the delimiter the user typed to end this recipient. We'll
// need it shortly in terminateToken().
// PREFIX_SIGN = c;
}
}
}
public void afterTextChanged(Editable s) {
if (mAffected != null) {
for (Annotation a : mAffected) {
s.removeSpan(a);
String userId = getAnnotation(mAffected, "number");
adapter.rejectUser(userId);
}
}
mAffected = null;
}
});
}
@Override
public boolean enoughToFilter() {
if (!super.enoughToFilter()) {
return false;
}
// If the user is in the middle of editing an existing recipient, don't offer the
// auto-complete menu. Without this, when the user selects an auto-complete menu item,
// it will get added to the list of recipients so we end up with the old before-editing
// recipient and the new post-editing recipient. As a precedent, gmail does not show
// the auto-complete menu when editing an existing recipient.
// int end = getSelectionEnd();
// int len = getText().length();
// return end == len;
return true;
}
public int getRecipientCount() {
return mTokenizer.getNumbers().size();
}
public List<String> getNumbers() {
return mTokenizer.getNumbers();
}
// public ContactList constructContactsFromInput() {
// List<String> numbers = mTokenizer.getNumbers();
// ContactList list = new ContactList();
// for (String number : numbers) {
// Contact contact = Contact.get(number, false);
// contact.setNumber(number);
// list.add(contact);
// }
// return list;
// }
//
// private boolean isValidAddress(String number, boolean isMms) {
// if (isMms) {
// return MessageUtils.isValidMmsAddress(number);
// } else {
// // TODO: PhoneNumberUtils.isWellFormedSmsAddress() only check if the number is a valid
// // GSM SMS address. If the address contains a dialable char, it considers it a well
// // formed SMS addr. CDMA doesn't work that way and has a different parser for SMS
// // address (see CdmaSmsAddress.parse(String address)). We should definitely fix this!!!
// return PhoneNumberUtils.isWellFormedSmsAddress(number)
// || Mms.isEmailAddress(number);
// }
// }
//
// public boolean hasValidRecipient(boolean isMms) {
// for (String number : mTokenizer.getNumbers()) {
// if (isValidAddress(number, isMms))
// return true;
// }
// return false;
// }
//
// public boolean hasInvalidRecipient(boolean isMms) {
// for (String number : mTokenizer.getNumbers()) {
// if (!isValidAddress(number, isMms)) {
// if (MmsConfig.getEmailGateway() == null) {
// return true;
// } else if (!MessageUtils.isAlias(number)) {
// return true;
// }
// }
// }
// return false;
// }
//
// public String formatInvalidNumbers(boolean isMms) {
// StringBuilder sb = new StringBuilder();
// for (String number : mTokenizer.getNumbers()) {
// if (!isValidAddress(number, isMms)) {
// if (sb.length() != 0) {
// sb.append(", ");
// }
// sb.append(number);
// }
// }
// return sb.toString();
// }
//
// public boolean containsEmail() {
// if (TextUtils.indexOf(getText(), '@') == -1)
// return false;
//
// List<String> numbers = mTokenizer.getNumbers();
// for (String number : numbers) {
// if (Mms.isEmailAddress(number))
// return true;
// }
// return false;
// }
// public static CharSequence contactToToken(Contact c) {
// SpannableString s = new SpannableString(c.getNameAndNumber());
// int len = s.length();
//
// if (len == 0) {
// return s;
// }
//
// s.setSpan(new Annotation("number", c.getNumber()), 0, len,
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//
// return s;
// }
// public void populate(ContactList list) {
// SpannableStringBuilder sb = new SpannableStringBuilder();
//
// for (Contact c : list) {
// if (sb.length() != 0) {
// sb.append(", ");
// }
//
// sb.append(contactToToken(c));
// }
//
// setText(sb);
// }
private int pointToPosition(int x, int y) {
x -= getCompoundPaddingLeft();
y -= getExtendedPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
if (layout == null) {
return -1;
}
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
return off;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
final int x = (int) ev.getX();
final int y = (int) ev.getY();
if (action == MotionEvent.ACTION_DOWN) {
mLongPressedPosition = pointToPosition(x, y);
}
return super.onTouchEvent(ev);
}
@Override
protected ContextMenuInfo getContextMenuInfo() {
if ((mLongPressedPosition >= 0)) {
Spanned text = getText();
if (mLongPressedPosition <= text.length()) {
int start = mTokenizer.findTokenStart(text, mLongPressedPosition);
int end = mTokenizer.findTokenEnd(text, start);
if (end != start) {
String number = getNumberAt(getText(), start, end, getContext());
// Contact c = Contact.get(number, false);
// return new RecipientContextMenuInfo(c);
return new RecipientContextMenuInfo();
}
}
}
return null;
}
private static String getNumberAt(Spanned sp, int start, int end, Context context) {
return getFieldAt("number", sp, start, end, context);
}
private static String getNameAt(Spanned sp, int start, int end, Context context) {
return getFieldAt("name", sp, start, end, context);
}
private static int getSpanLength(Spanned sp, int start, int end, Context context) {
// TODO: there's a situation where the span can lose its annotations:
// - add an auto-complete contact
// - add another auto-complete contact
// - delete that second contact and keep deleting into the first
// - we lose the annotation and can no longer get the span.
// Need to fix this case because it breaks auto-complete contacts with commas in the name.
Annotation[] a = sp.getSpans(start, end, Annotation.class);
if (a.length > 0) {
return sp.getSpanEnd(a[0]);
}
return 0;
}
private static String getFieldAt(String field, Spanned sp, int start, int end,
Context context) {
Annotation[] a = sp.getSpans(start, end, Annotation.class);
String fieldValue = getAnnotation(a, field);
if (TextUtils.isEmpty(fieldValue)) {
fieldValue = TextUtils.substring(sp, start, end);
}
return fieldValue;
}
private static String getAnnotation(Annotation[] a, String key) {
for (int i = 0; i < a.length; i++) {
if (a[i].getKey().equals(key)) {
return a[i].getValue();
}
}
return "";
}
private class RecipientsEditorTokenizer
implements MultiAutoCompleteTextView.Tokenizer {
private final MultiAutoCompleteTextView mList;
private final Context mContext;
RecipientsEditorTokenizer(Context context, MultiAutoCompleteTextView list) {
mList = list;
mContext = context;
}
/**
* Returns the start of the token that ends at offset
* <code>cursor</code> within <code>text</code>.
* It is a method from the MultiAutoCompleteTextView.Tokenizer interface.
*/
public int findTokenStart(CharSequence text, int cursor) {
if (cursor > 0 && Character.isSpace(text.charAt(cursor - 1))) {
return cursor;
}
boolean wasTrigger = false;
int i = cursor;
while (i > 0) {
--i;
char ch = text.charAt(i);
if (wasTrigger = isMentionTrigger(ch)) {
if (i == 0 || Character.isSpace(text.charAt(i - 1))) {
wasTrigger = true;
break;
}
}
}
while (i < cursor && text.charAt(i) == ' ') {
i++;
}
if (wasTrigger) {
final int len = text.length();
if (cursor == len) {
return i;
} else {
char ch = text.charAt(cursor);
if (Character.isSpaceChar(ch)) {
return i;
}
}
}
return cursor;
}
/**
* Returns the end of the token (minus trailing punctuation)
* that begins at offset <code>cursor</code> within <code>text</code>.
* It is a method from the MultiAutoCompleteTextView.Tokenizer interface.
*/
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (isMentionTrigger(text.charAt(i))) {
return i;
} else {
i++;
}
}
return len;
}
/**
* Returns <code>text</code>, modified, if necessary, to ensure that
* it ends with a token terminator (for example a space or comma).
* It is a method from the MultiAutoCompleteTextView.Tokenizer interface.
*/
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
// char c;
if (i > 0 && isMentionTrigger(text.charAt(i - 1))) {
return text;
} else {
return encodeTerminateToken(text);
}
}
private static final String MENTION_PLUS_ONE_FORMATTER = "<a href='%1$s%2$s'>+%3$s</a>";
public List<String> getNumbers() {
Spanned sp = mList.getText();
int len = sp.length();
List<String> list = new ArrayList<String>();
int start = 0;
int i = 0;
while (i < len + 1) {
if ((i == len) || isMentionTrigger(sp.charAt(i))) {
if (i > start) {
list.add(HtmlUtils.text2html(String.format(
MENTION_PLUS_ONE_FORMATTER,
BpcApiUtils.PROFILE_SEARCH_USERID_PREFIX,
getNumberAt(sp, start, i, mContext),
getNameAt(sp, start, i, mContext)
)));
// calculate the recipients total length. This is so if the name contains
// commas or semis, we'll skip over the whole name to the next
// recipient, rather than parsing this single name into multiple
// recipients.
int spanLen = getSpanLength(sp, start, i, mContext);
if (spanLen > i) {
i = spanLen;
}
}
i++;
while ((i < len) && (sp.charAt(i) == ' ')) {
i++;
}
start = i;
} else {
i++;
}
}
return list;
}
}
// TODO : implement it if necessary
static class RecipientContextMenuInfo implements ContextMenuInfo {
// final Contact recipient;
RecipientContextMenuInfo(/*Contact r*/) {
// recipient = r;
}
}
boolean isMentionTrigger(char paramChar) {
if ((paramChar == '+') || (paramChar == '@'))
return true;
else
return false;
}
public void destroy() {
}
CharSequence encodeTerminateToken(CharSequence text) {
CharSequence encodedText = text + " ";
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(encodedText);
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return encodedText;
}
}
// public String getMentions() {
// List<String> list = getNumbers();
// return list.toString();
// }
// @Override
// protected CharSequence convertSelectionToString(Object paramObject) {
// SpannableString localSpannableString = new SpannableString("+" + super.convertSelectionToString(paramObject));
// Cursor localCursor = (Cursor) paramObject;
// int i = localCursor.getColumnIndex("person_id");
// if (i != -1)
// localSpannableString.setSpan(new MentionSpan(localCursor.getString(i)), 0, localSpannableString.length(), 33);
// return localSpannableString;
// return "+" + super.convertSelectionToString(paramObject);
// }
public static String getConversationText(ConversationMultiAutoCompleteTextView conversationTextView) {
String content = null;
if (null != conversationTextView) {
CharSequence text = conversationTextView.getText();
if (text instanceof Spanned) {
Spanned spannedText = (Spanned) text;
URLSpan[] urlSpans = spannedText.getSpans(0, text.length(), URLSpan.class);
if (null != urlSpans && urlSpans.length > 0) {
int spanStart = 0, spanEnd = 0;
String url;
String name;
StringBuilder stringBuilder = new StringBuilder();
for(URLSpan item : urlSpans) {
spanStart = spannedText.getSpanStart(item);
// Append non-spanned text after the end of last URLSpan item.
if (spanEnd < spanStart) {
stringBuilder.append(text.subSequence(spanEnd, spanStart));
}
spanEnd = spannedText.getSpanEnd(item);
name = text.subSequence(spanStart, spanEnd).toString();
url = item.getURL();
// Append the encoding spanned text.
stringBuilder.append("<a href='").append(url).append("'>").
append(name).append("</a>");
}
// Append the non-spanned text on the tail of spans.
stringBuilder.append(text.subSequence(spanEnd, text.length()));
content = stringBuilder.toString();
} else {
content = text.toString();
}
} else {
content = text.toString();
}
}
return content;
}
public static OnClickListener instanceMentionButtonClickListener(Context context, AutoCompleteTextView editText) {
return new MentionButtonClickListener(context, editText);
}
private static class MentionButtonClickListener implements OnClickListener {
private Context mContext;
private AutoCompleteTextView mEditText;
public MentionButtonClickListener(Context context, AutoCompleteTextView editText) {
mContext = context;
mEditText = editText;
}
private boolean isMentionTrigger(char paramChar) {
if ((paramChar == '+') || (paramChar == '@'))
return true;
else
return false;
}
private void showPrompt() {
Toast.makeText(mContext, R.string.prompt_mention_typing_start, Toast.LENGTH_SHORT).show();
}
public void onClick(View arg0) {
if (null != mEditText) {
mEditText.clearComposingText();
int index = mEditText.getSelectionStart();
Editable editable = mEditText.getText();
if (index < 0) {
editable.append("+");
} else if (index >= editable.length()) {
if (0 == index) {
editable.append("+");
} else if (isMentionTrigger(editable.charAt(index - 1))) {
showPrompt();
} else {
editable.append(" +");
}
} else {
final char preChar = index > 0 ? editable.charAt(index - 1) : ' ';
final char curChar = editable.charAt(index);
if (Character.isSpaceChar(preChar)) {
if (Character.isSpaceChar(curChar)) {
editable.insert(index, "+");
} else {
if (isMentionTrigger(curChar)) {
showPrompt();
} else {
editable.insert(index, "+ ");
mEditText.setSelection(index + 1);
}
}
// TODO : reform this code and consider bellowing cases.
// } else if (isMentionTrigger(preChar)) {
// if (Character.isSpaceChar(curChar)) {
// editable.insert(index, " +");
// } else {
// editable.insert(index, " + ");
// mEditText.setSelection(index + 2);
// }
} else {
if (Character.isSpaceChar(curChar)) {
editable.insert(index, " +");
} else {
editable.insert(index, " + ");
mEditText.setSelection(index + 2);
}
}
}
mEditText.requestFocus();
// InputMethodManager imm = (InputMethodManager)mEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(mEditText, 0);
// imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
mEditText.showDropDown();
}
}
}
}