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

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

我的博客对应文章地址

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

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

最初版本情况

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

Chrome最新版代码

实现效果

自己实现的效果图

实现代码

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

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值