Chrome for Android AutoComplete地址栏地址自动补全功能调研和更新

Chrome地址栏地址自动补全功能调研和更新

CSDN文章对应地址

Chrome地址栏地址自动补全功能预览

Chrome
补全前提:

  1. 使用Gboard输入法
  2. Gboard输入法打开了【文字更正】功能里面的现实建议栏
    补全逻辑:
  3. 输入一个字符后,将推荐的第一个链接补全,补全的文字为蓝色背景选中状态,并且用户自己输入的文本下面有下划线(这个下划线不是自己通过SpannableString来实现的,应该是EditText自己为待用户选择推荐(commitText)的的文本默认添加的)
  4. 输入一个非字母类型的,会自动commitText,但是此时把非字母类型的字符删除后,输入法的输入建议会再次出现,并且之前的用户输入文本会再次出现下划线,这种状态其实就是带选择状态,可以通过输入法最顶部的推荐直接替换文字。
  5. 选择了输入法的输入建议文本,浏览器也能继续进行输入推荐和自动补全

最初版本情况

Chrome之前的版本其实不是这样的,而是类似于禁用了输入法的输入建议,而直接替换输入框的文本,再将一部分文字设置为select状态。
代码实现参考,Android EditText 通过TextWatcher实现自动补全的注意点

Chrome最新版代码

实现效果

自己实现的效果图

实现代码

主要核心代码,忽略了一些变量的定义,详细参考Chrome源码即可

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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
public void onAutoComplete(String suggestion) {
String userText = getTextWithoutAutocomplete();
suggestion = suggestion.startsWith(userText) ? suggestion.replaceFirst(userText, "") : "";
if (shouldAutocomplete()) {
setAutocompleteText(userText, suggestion);
}
}

private void limitDisplayableLength() {
// To limit displayable length we replace middle portion of the string with ellipsis.
// That affects only presentation of the text, and doesn't affect other aspects like
// copying to the clipboard, getting text with getText(), etc.
final int maxLength = MAX_DISPLAYABLE_LENGTH_END;

Editable text = getText();
int textLength = text.length();
if (textLength <= maxLength) {
if (mDidEllipsizeTextHint) {
EllipsisSpan[] spans = text.getSpans(0, textLength, EllipsisSpan.class);
if (spans != null && spans.length > 0) {
assert spans.length == 1 : "Should never apply more than a single EllipsisSpan";
for (int i = 0; i < spans.length; i++) {
text.removeSpan(spans[i]);
}
}
}
mDidEllipsizeTextHint = false;
return;
}

mDidEllipsizeTextHint = true;

int spanLeft = text.nextSpanTransition(0, textLength, EllipsisSpan.class);
if (spanLeft != textLength) return;

spanLeft = maxLength / 2;
text.setSpan(EllipsisSpan.INSTANCE, spanLeft, textLength - spanLeft,
Editable.SPAN_INCLUSIVE_EXCLUSIVE);
}

public boolean shouldAutocomplete() {
boolean retVal = mBatchEditNestCount == 0 && mLastEditWasTyping
&& mCurrentState.isCursorAtEndOfUserText() && !isKeyboardBlacklisted()
&& isNonCompositionalText(getTextWithoutAutocomplete());
Log.i(TAG, "shouldAutocomplete: " + retVal);
return retVal;
}

private boolean isKeyboardBlacklisted() {
String pkgName = getKeyboardPackageName();
return pkgName.contains(".iqqi") // crbug.com/767016
|| pkgName.contains("omronsoft") || pkgName.contains(".iwnn"); // crbug.com/758443
}

public String getKeyboardPackageName() {
String defaultIme = Settings.Secure.getString(
getContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
return defaultIme == null ? "" : defaultIme;
}

public static boolean isNonCompositionalText(String text) {
// To start with, we are only activating this for English alphabets, European characters,
// numbers and URLs to avoid potential bad interactions with more complex IMEs.
// The rationale for including character sets with diacritical marks is that backspacing on
// a letter with a diacritical mark most likely deletes the whole character instead of
// removing the diacritical mark.
// TODO(changwan): also scan for other traditionally non-IME charsets.
return NON_COMPOSITIONAL_TEXT_PATTERN.matcher(text).matches();
}

@Override
protected void onSelectionChanged(int selStart, int selEnd) {
if (mCurrentState == null) {
return;
}
if (mCurrentState.getSelStart() == selStart && mCurrentState.getSelEnd() == selEnd) return;

mCurrentState.setSelection(selStart, selEnd);
if (mBatchEditNestCount > 0) return;
int len = mCurrentState.getUserText().length();
if (mCurrentState.hasAutocompleteText()) {
if (selStart > len || selEnd > len) {
Log.i(TAG, "Autocomplete text is being touched. Make it real.");
if (mInputConnection != null) mInputConnection.commitAutocomplete();
} else {
Log.i(TAG, "Touching before the cursor removes autocomplete.");
clearAutocompleteTextAndUpdateSpanCursor();
}
}
notifyAutocompleteTextStateChanged();
}

private void clearAutocompleteTextAndUpdateSpanCursor() {
Log.i(TAG, "clearAutocompleteAndUpdateSpanCursor");
clearAutocompleteText();
// Take effect and notify if not already in a batch edit.
if (mInputConnection != null) {
mInputConnection.onBeginImeCommand();
mInputConnection.onEndImeCommand();
} else {
mSpanCursorController.removeSpan();
notifyAutocompleteTextStateChanged();
}
}
class AutocompleteState {
private String mUserText;
private String mAutocompleteText;
private int mSelStart;
private int mSelEnd;

public AutocompleteState(AutocompleteState a) {
copyFrom(a);
}

public AutocompleteState(String userText, String autocompleteText, int selStart, int selEnd) {
set(userText, autocompleteText, selStart, selEnd);
}

public void set(String userText, String autocompleteText, int selStart, int selEnd) {
mUserText = userText;
mAutocompleteText = autocompleteText;
mSelStart = selStart;
mSelEnd = selEnd;
}

public void copyFrom(AutocompleteState a) {
set(a.mUserText, a.mAutocompleteText, a.mSelStart, a.mSelEnd);
}

public String getUserText() {
return mUserText;
}

public String getAutocompleteText() {
return mAutocompleteText;
}

public boolean hasAutocompleteText() {
return !TextUtils.isEmpty(mAutocompleteText);
}

/** @return The whole text including autocomplete text. */
public String getText() {
return mUserText + mAutocompleteText;
}

public int getSelStart() {
return mSelStart;
}

public int getSelEnd() {
return mSelEnd;
}

public void setSelection(int selStart, int selEnd) {
mSelStart = selStart;
mSelEnd = selEnd;
}

public void setUserText(String userText) {
mUserText = userText;
}

public void setAutocompleteText(String autocompleteText) {
mAutocompleteText = autocompleteText;
}

public void clearAutocompleteText() {
mAutocompleteText = "";
}

public boolean isCursorAtEndOfUserText() {
return mSelStart == mUserText.length() && mSelEnd == mUserText.length();
}

public boolean isWholeUserTextSelected() {
return mSelStart == 0 && mSelEnd == mUserText.length();
}

/**
* @param prevState The previous state to compare the current state with.
* @return Whether the current state is backward-deleted from prevState.
*/
public boolean isBackwardDeletedFrom(AutocompleteState prevState) {
return isCursorAtEndOfUserText() && prevState.isCursorAtEndOfUserText()
&& isPrefix(mUserText, prevState.mUserText);
}

/**
* @param prevState The previous state to compare the current state with.
* @return Whether the current state is forward-typed from prevState.
*/
public boolean isForwardTypedFrom(AutocompleteState prevState) {
return isCursorAtEndOfUserText() && prevState.isCursorAtEndOfUserText()
&& isPrefix(prevState.mUserText, mUserText);
}

/**
* @param prevState The previous state to compare the current state with.
* @return The differential string that has been backward deleted.
*/
public String getBackwardDeletedTextFrom(AutocompleteState prevState) {
if (!isBackwardDeletedFrom(prevState)) return null;
return prevState.mUserText.substring(mUserText.length());
}

public boolean isPrefix(String a, String b) {
return b.startsWith(a) && b.length() > a.length();
}

/**
* When the user manually types the next character that was already suggested in the previous
* autocomplete, then the suggestion is still valid if we simply remove one character from the
* beginning of it. For example, if prev = "a[bc]" and current text is "ab", this method
* constructs "ab[c]".
* @param prevState The previous state.
* @return Whether the shifting was successful.
*/
public boolean reuseAutocompleteTextIfPrefixExtension(AutocompleteState prevState) {
// Shift when user text has grown or remains the same, but still prefix of prevState's whole
// text.
int diff = mUserText.length() - prevState.mUserText.length();
if (diff < 0) return false;
if (!isPrefix(mUserText, prevState.getText())) return false;
mAutocompleteText = prevState.mAutocompleteText.substring(diff);
return true;
}

public void commitAutocompleteText() {
mUserText += mAutocompleteText;
mAutocompleteText = "";
}

@Override
public boolean equals(Object o) {
if (!(o instanceof AutocompleteState)) return false;
if (o == this) return true;
AutocompleteState a = (AutocompleteState) o;
return mUserText.equals(a.mUserText) && mAutocompleteText.equals(a.mAutocompleteText)
&& mSelStart == a.mSelStart && mSelEnd == a.mSelEnd;
}

@Override
public int hashCode() {
return mUserText.hashCode() * 2 + mAutocompleteText.hashCode() * 3 + mSelStart * 5
+ mSelEnd * 7;
}

@Override
public String toString() {
return String.format(Locale.US, "AutocompleteState {[%s][%s] [%d-%d]}", mUserText,
mAutocompleteText, mSelStart, mSelEnd);
}
}
public boolean hasAutocomplete() {
boolean retVal = mCurrentState.hasAutocompleteText();
Log.i(TAG, "hasAutocomplete: " + retVal);
return retVal;
}


private class AutocompleteInputConnection extends InputConnectionWrapper {
private final AutocompleteState mPreBatchEditState;

public AutocompleteInputConnection() {
super(null, true);
mPreBatchEditState = new AutocompleteState(mCurrentState);
}

private boolean incrementBatchEditCount() {
++mBatchEditNestCount;
// After the outermost super.beginBatchEdit(), EditText will stop selection change
// update to the IME app.
return super.beginBatchEdit();
}

private boolean decrementBatchEditCount() {
--mBatchEditNestCount;
return super.endBatchEdit();
}

public void commitAutocomplete() {
Log.i(TAG, "commitAutocomplete");
if (!hasAutocomplete()) return;
mCurrentState.commitAutocompleteText();
// Invalidate mPreviouslySetState.
mPreviouslySetState.copyFrom(mCurrentState);
mLastEditWasTyping = false;
incrementBatchEditCount(); // avoids additional notifyAutocompleteTextStateChanged()
mSpanCursorController.commitSpan();
decrementBatchEditCount();
}

@Override
public boolean beginBatchEdit() {
Log.i(TAG, "beginBatchEdit");
onBeginImeCommand();
boolean retVal = incrementBatchEditCount();
onEndImeCommand();
return retVal;
}

/**
* Always call this at the beginning of any IME command. Compare this with beginBatchEdit()
* which is by itself an IME command.
* @return Whether the call was successful.
*/
public boolean onBeginImeCommand() {
Log.i(TAG, "onBeginImeCommand: " + mBatchEditNestCount);
boolean retVal = incrementBatchEditCount();
if (mBatchEditNestCount == 1) {
mPreBatchEditState.copyFrom(mCurrentState);
} else if (mDeletePostfixOnNextBeginImeCommand > 0) {
int len = getText().length();
getText().delete(len - mDeletePostfixOnNextBeginImeCommand, len);
}
mDeletePostfixOnNextBeginImeCommand = 0;
mSpanCursorController.removeSpan();
return retVal;
}

private void restoreBackspacedText(String diff) {
Log.i(TAG, "restoreBackspacedText. diff: " + diff);

if (mBatchEditNestCount > 0) {
// If batch edit hasn't finished, we will restore backspaced text only for visual
// effects. However, for internal operations to work correctly, we need to remove
// the restored diff at the beginning of next IME operation.
mDeletePostfixOnNextBeginImeCommand = diff.length();
}
if (mBatchEditNestCount == 0) { // only at the outermost batch edit
if (shouldFinishCompositionOnDeletion()) super.finishComposingText();
}
incrementBatchEditCount(); // avoids additional notifyAutocompleteTextStateChanged()
Editable editable = getEditableText();
editable.append(diff);
decrementBatchEditCount();
}

private boolean setAutocompleteSpan() {
mSpanCursorController.removeSpan();
if (!mCurrentState.isCursorAtEndOfUserText()) return false;

if (mCurrentState.reuseAutocompleteTextIfPrefixExtension(mPreviouslySetState)) {
mSpanCursorController.setSpan(mCurrentState);
return true;
} else {
return false;
}
}

@Override
public boolean endBatchEdit() {
Log.i(TAG, "endBatchEdit");
onBeginImeCommand();
boolean retVal = decrementBatchEditCount();
onEndImeCommand();
return retVal;
}

/**
* Always call this at the end of an IME command. Compare this with endBatchEdit()
* which is by itself an IME command.
* @return Whether the call was successful.
*/
public boolean onEndImeCommand() {
Log.i(TAG, "onEndImeCommand: " + (mBatchEditNestCount - 1));
String diff = mCurrentState.getBackwardDeletedTextFrom(mPreBatchEditState);
if (diff != null) {
// Update selection first such that keyboard app gets what it expects.
boolean retVal = decrementBatchEditCount();

if (mPreBatchEditState.hasAutocompleteText()) {
// Undo delete to retain the last character and only remove autocomplete text.
restoreBackspacedText(diff);
}
mLastEditWasTyping = false;
clearAutocompleteText();
notifyAutocompleteTextStateChanged();
return retVal;
}
if (!setAutocompleteSpan()) {
clearAutocompleteText();
}
boolean retVal = decrementBatchEditCount();
// Simply typed some characters or whole text selection has been overridden.
if (mCurrentState.isForwardTypedFrom(mPreBatchEditState)
|| (mPreBatchEditState.isWholeUserTextSelected()
&& mCurrentState.getUserText().length() > 0
&& mCurrentState.isCursorAtEndOfUserText())) {
mLastEditWasTyping = true;
}
notifyAutocompleteTextStateChanged();
return retVal;
}

@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
Log.i(TAG, "commitText: " + text);
onBeginImeCommand();
boolean retVal = super.commitText(text, newCursorPosition);
onEndImeCommand();
return retVal;
}

@Override
public boolean setComposingText(CharSequence text, int newCursorPosition) {
Log.i(TAG, "setComposingText: " + text);
onBeginImeCommand();
boolean retVal = super.setComposingText(text, newCursorPosition);
onEndImeCommand();
return retVal;
}

@Override
public boolean setComposingRegion(int start, int end) {
onBeginImeCommand();
boolean retVal = super.setComposingRegion(start, end);
onEndImeCommand();
return retVal;
}

@Override
public boolean finishComposingText() {
Log.i(TAG, "finishComposingText");
onBeginImeCommand();
boolean retVal = super.finishComposingText();
onEndImeCommand();
return retVal;
}

@Override
public boolean deleteSurroundingText(final int beforeLength, final int afterLength) {
onBeginImeCommand();
boolean retVal = super.deleteSurroundingText(beforeLength, afterLength);
onEndImeCommand();
return retVal;
}

@Override
public boolean setSelection(final int start, final int end) {
onBeginImeCommand();
boolean retVal = super.setSelection(start, end);
onEndImeCommand();
return retVal;
}

@Override
public boolean performEditorAction(final int editorAction) {
Log.i(TAG, "performEditorAction: " + editorAction);
onBeginImeCommand();
commitAutocomplete();
boolean retVal = super.performEditorAction(editorAction);
onEndImeCommand();
return retVal;
}

@Override
public boolean sendKeyEvent(final KeyEvent event) {
Log.i(TAG, "sendKeyEvent: " + event.getKeyCode());
onBeginImeCommand();
boolean retVal = super.sendKeyEvent(event);
onEndImeCommand();
return retVal;
}

@Override
public ExtractedText getExtractedText(final ExtractedTextRequest request, final int flags) {
Log.i(TAG, "getExtractedText");
onBeginImeCommand();
ExtractedText retVal = super.getExtractedText(request, flags);
onEndImeCommand();
return retVal;
}

@Override
public CharSequence getTextAfterCursor(final int n, final int flags) {
Log.i(TAG, "getTextAfterCursor");
onBeginImeCommand();
CharSequence retVal = super.getTextAfterCursor(n, flags);
onEndImeCommand();
return retVal;
}

@Override
public CharSequence getTextBeforeCursor(final int n, final int flags) {
Log.i(TAG, "getTextBeforeCursor");
onBeginImeCommand();
CharSequence retVal = super.getTextBeforeCursor(n, flags);
onEndImeCommand();
return retVal;
}

@Override
public CharSequence getSelectedText(final int flags) {
Log.i(TAG, "getSelectedText");
onBeginImeCommand();
CharSequence retVal = super.getSelectedText(flags);
onEndImeCommand();
return retVal;
}

@Override
public boolean commitCompletion(CompletionInfo text) {
Log.i(TAG, "commitCompletion");
onBeginImeCommand();
boolean retVal = super.commitCompletion(text);
onEndImeCommand();
return retVal;
}

@Override
public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
Log.i(TAG, "commitContent");
onBeginImeCommand();
boolean retVal = super.commitContent(inputContentInfo, flags, opts);
onEndImeCommand();
return retVal;
}

@Override
public boolean commitCorrection(CorrectionInfo correctionInfo) {
Log.i(TAG, "commitCorrection");
onBeginImeCommand();
boolean retVal = super.commitCorrection(correctionInfo);
onEndImeCommand();
return retVal;
}

@Override
public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
Log.i(TAG, "deleteSurroundingTextInCodePoints");
onBeginImeCommand();
boolean retVal = super.deleteSurroundingTextInCodePoints(beforeLength, afterLength);
onEndImeCommand();
return retVal;
}

@Override
public int getCursorCapsMode(int reqModes) {
Log.i(TAG, "getCursorCapsMode");
onBeginImeCommand();
int retVal = super.getCursorCapsMode(reqModes);
onEndImeCommand();
return retVal;
}

@Override
public boolean requestCursorUpdates(int cursorUpdateMode) {
Log.i(TAG, "requestCursorUpdates");
onBeginImeCommand();
boolean retVal = super.requestCursorUpdates(cursorUpdateMode);
onEndImeCommand();
return retVal;
}

@Override
public boolean clearMetaKeyStates(int states) {
Log.i(TAG, "clearMetaKeyStates");
onBeginImeCommand();
boolean retVal = super.clearMetaKeyStates(states);
onEndImeCommand();
return retVal;
}
}

private boolean shouldFinishCompositionOnDeletion() {
String defaultIme = Settings.Secure.getString(
getContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
defaultIme = defaultIme == null ? "" : defaultIme;

return !defaultIme.contains("com.sec.android.inputmethod");
}

private int mDeletePostfixOnNextBeginImeCommand;

private void clearAutocompleteText() {
Log.i(TAG, "clearAutocomplete");
mPreviouslySetState.clearAutocompleteText();
mCurrentState.clearAutocompleteText();
}

private final AutocompleteState mPreviouslyNotifiedState;
private void notifyAutocompleteTextStateChanged() {

if (mBatchEditNestCount > 0) {
// crbug.com/764749
Log.w(TAG, "Did not notify - in batch edit.");
return;
}
if (mCurrentState.equals(mPreviouslyNotifiedState)) {
// crbug.com/764749
Log.w(TAG, "Did not notify - no change.");
return;
}
if (mCurrentState.getUserText().equals(mPreviouslyNotifiedState.getUserText())
&& (mCurrentState.hasAutocompleteText()
|| !mPreviouslyNotifiedState.hasAutocompleteText())) {
// Nothing has changed except that autocomplete text has been set or modified. Or
// selection change did not affect autocomplete text. Autocomplete text is set by the
// controller, so only text change or deletion of autocomplete text should be notified.
mPreviouslyNotifiedState.copyFrom(mCurrentState);
return;
}
mPreviouslyNotifiedState.copyFrom(mCurrentState);
if (mIgnoreTextChangeFromAutocomplete) {
// crbug.com/764749
Log.w(TAG, "Did not notify - ignored.");
return;
}
// The current model's mechanism always moves the cursor at the end of user text, so we
// don't need to update the display.
onAutocompleteTextStateChanged(false /* updateDisplay */);
}
private static class SpanCursorController {
private final PopUpWindowInAutoCompleteTextView mDelegate;
private BackgroundColorSpan mSpan;

public SpanCursorController(PopUpWindowInAutoCompleteTextView delegate) {
mDelegate = delegate;
}

public void setSpan(AutocompleteState state) {
int sel = state.getSelStart();

if (mSpan == null) mSpan = new BackgroundColorSpan(mDelegate.getHighlightColor());
SpannableString spanString = new SpannableString(state.getAutocompleteText());
// The flag here helps make sure that span does not get spill to other part of the text.
spanString.setSpan(mSpan, 0, state.getAutocompleteText().length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Editable editable = mDelegate.getEditableText();
editable.append(spanString);

// Keep the original selection before adding spannable string.
Selection.setSelection(editable, sel, sel);
setCursorVisible(false);
Log.i(TAG, "setSpan: " + getEditableDebugString(editable));
}

private void setCursorVisible(boolean visible) {
if (mDelegate.isFocused()) mDelegate.setCursorVisible(visible);
}

private int getSpanIndex(Editable editable) {
if (editable == null || mSpan == null) return -1;
return editable.getSpanStart(mSpan); // returns -1 if mSpan is not attached
}

public void reset() {
setCursorVisible(true);
Editable editable = mDelegate.getEditableText();
int idx = getSpanIndex(editable);
if (idx != -1) {
editable.removeSpan(mSpan);
}
mSpan = null;
}

public boolean removeSpan() {
setCursorVisible(true);
Editable editable = mDelegate.getEditableText();
int idx = getSpanIndex(editable);
if (idx == -1) return false;
editable.removeSpan(mSpan);
editable.delete(idx, editable.length());
mSpan = null;
{
Log.i(TAG, "removeSpan - after removal: " + getEditableDebugString(editable));
}
return true;
}

public void commitSpan() {
mDelegate.getEditableText().removeSpan(mSpan);
setCursorVisible(true);
}

public void reflectTextUpdateInState(AutocompleteState state, CharSequence text) {
if (text instanceof Editable) {
Editable editable = (Editable) text;
int idx = getSpanIndex(editable);
if (idx != -1) {
// We do not set autocomplete text here as model should solely control it.
state.setUserText(editable.subSequence(0, idx).toString());
return;
}
}
state.setUserText(text.toString());
}
}
/**
* @param editable The editable.
* @return Debug string for the given {@Editable}.
*/
private static String getEditableDebugString(Editable editable) {
return String.format(Locale.US, "Editable {[%s] SEL[%d %d] COM[%d %d]}",
editable.toString(), Selection.getSelectionStart(editable),
Selection.getSelectionEnd(editable),
BaseInputConnection.getComposingSpanStart(editable),
BaseInputConnection.getComposingSpanEnd(editable));
}

@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
Log.i(TAG, "onTextChanged: " + text);
if(mSpanCursorController==null){
return;
}
mSpanCursorController.reflectTextUpdateInState(mCurrentState, text);
if (mBatchEditNestCount > 0) return; // let endBatchEdit() handles changes from IME.
// An external change such as text paste occurred.
mLastEditWasTyping = false;
clearAutocompleteTextAndUpdateSpanCursor();
}

public String getTextWithoutAutocomplete() {
String retVal = mCurrentState.getUserText();
Log.i(TAG, "getTextWithoutAutocomplete: " + retVal);
return retVal;
}

public void onAutocompleteTextStateChanged(boolean updateDisplay) {
if (updateDisplay) limitDisplayableLength();
onTextChangedForAutocomplete();
}

public void onTextChangedForAutocomplete() {
//触发搜索建议获取的逻辑
}

/**
* Sets whether text changes should trigger autocomplete.
* <p>
* enabling autocomplete.
*
* @param ignoreAutocomplete Whether text changes should be ignored and no auto complete
* triggered.
*/
public void setIgnoreTextChangeFromAutocomplete(boolean ignoreAutocomplete) {

mIgnoreTextChangeFromAutocomplete = ignoreAutocomplete;
}

public void setAutocompleteText(CharSequence userText, CharSequence inlineAutocompleteText) {
setAutocompleteTextInternal(userText.toString(), inlineAutocompleteText.toString());
}

private void setAutocompleteTextInternal(String userText, String autocompleteText) {
mPreviouslySetState.set(userText, autocompleteText, userText.length(), userText.length());
// TODO(changwan): avoid any unnecessary removal and addition of autocomplete text when it
// is not changed or when it is appended to the existing autocomplete text.
if (mInputConnection != null) {
mInputConnection.onBeginImeCommand();
mInputConnection.onEndImeCommand();
}
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
//创建InputConnection
mBatchEditNestCount = 0;
mInputConnection = new AutocompleteInputConnection();
mInputConnection.setTarget(super.onCreateInputConnection(outAttrs));
return mInputConnection;
}


Have Fun :)