Search in sources :

Example 1 with CorrectionInfo

use of android.view.inputmethod.CorrectionInfo in project Android-Terminal-Emulator by jackpal.

the class EmulatorView method onCreateInputConnection.

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    outAttrs.inputType = mUseCookedIme ? EditorInfo.TYPE_CLASS_TEXT : EditorInfo.TYPE_NULL;
    return new BaseInputConnection(this, true) {

        /**
             * Used to handle composing text requests
             */
        private int mCursor;

        private int mComposingTextStart;

        private int mComposingTextEnd;

        private int mSelectedTextStart;

        private int mSelectedTextEnd;

        private void sendText(CharSequence text) {
            int n = text.length();
            char c;
            try {
                for (int i = 0; i < n; i++) {
                    c = text.charAt(i);
                    if (Character.isHighSurrogate(c)) {
                        int codePoint;
                        if (++i < n) {
                            codePoint = Character.toCodePoint(c, text.charAt(i));
                        } else {
                            // Unicode Replacement Glyph, aka white question mark in black diamond.
                            codePoint = '�';
                        }
                        mapAndSend(codePoint);
                    } else {
                        mapAndSend(c);
                    }
                }
            } catch (IOException e) {
                Log.e(TAG, "error writing ", e);
            }
        }

        private void mapAndSend(int c) throws IOException {
            int result = mKeyListener.mapControlChar(c);
            if (result < TermKeyListener.KEYCODE_OFFSET) {
                mTermSession.write(result);
            } else {
                mKeyListener.handleKeyCode(result - TermKeyListener.KEYCODE_OFFSET, null, getKeypadApplicationMode());
            }
            clearSpecialKeyStatus();
        }

        public boolean beginBatchEdit() {
            if (LOG_IME) {
                Log.w(TAG, "beginBatchEdit");
            }
            setImeBuffer("");
            mCursor = 0;
            mComposingTextStart = 0;
            mComposingTextEnd = 0;
            return true;
        }

        public boolean clearMetaKeyStates(int arg0) {
            if (LOG_IME) {
                Log.w(TAG, "clearMetaKeyStates " + arg0);
            }
            return false;
        }

        public boolean commitCompletion(CompletionInfo arg0) {
            if (LOG_IME) {
                Log.w(TAG, "commitCompletion " + arg0);
            }
            return false;
        }

        public boolean endBatchEdit() {
            if (LOG_IME) {
                Log.w(TAG, "endBatchEdit");
            }
            return true;
        }

        public boolean finishComposingText() {
            if (LOG_IME) {
                Log.w(TAG, "finishComposingText");
            }
            sendText(mImeBuffer);
            setImeBuffer("");
            mComposingTextStart = 0;
            mComposingTextEnd = 0;
            mCursor = 0;
            return true;
        }

        public int getCursorCapsMode(int reqModes) {
            if (LOG_IME) {
                Log.w(TAG, "getCursorCapsMode(" + reqModes + ")");
            }
            int mode = 0;
            if ((reqModes & TextUtils.CAP_MODE_CHARACTERS) != 0) {
                mode |= TextUtils.CAP_MODE_CHARACTERS;
            }
            return mode;
        }

        public ExtractedText getExtractedText(ExtractedTextRequest arg0, int arg1) {
            if (LOG_IME) {
                Log.w(TAG, "getExtractedText" + arg0 + "," + arg1);
            }
            return null;
        }

        public CharSequence getTextAfterCursor(int n, int flags) {
            if (LOG_IME) {
                Log.w(TAG, "getTextAfterCursor(" + n + "," + flags + ")");
            }
            int len = Math.min(n, mImeBuffer.length() - mCursor);
            if (len <= 0 || mCursor < 0 || mCursor >= mImeBuffer.length()) {
                return "";
            }
            return mImeBuffer.substring(mCursor, mCursor + len);
        }

        public CharSequence getTextBeforeCursor(int n, int flags) {
            if (LOG_IME) {
                Log.w(TAG, "getTextBeforeCursor(" + n + "," + flags + ")");
            }
            int len = Math.min(n, mCursor);
            if (len <= 0 || mCursor < 0 || mCursor >= mImeBuffer.length()) {
                return "";
            }
            return mImeBuffer.substring(mCursor - len, mCursor);
        }

        public boolean performContextMenuAction(int arg0) {
            if (LOG_IME) {
                Log.w(TAG, "performContextMenuAction" + arg0);
            }
            return true;
        }

        public boolean performPrivateCommand(String arg0, Bundle arg1) {
            if (LOG_IME) {
                Log.w(TAG, "performPrivateCommand" + arg0 + "," + arg1);
            }
            return true;
        }

        public boolean reportFullscreenMode(boolean arg0) {
            if (LOG_IME) {
                Log.w(TAG, "reportFullscreenMode" + arg0);
            }
            return true;
        }

        public boolean commitCorrection(CorrectionInfo correctionInfo) {
            if (LOG_IME) {
                Log.w(TAG, "commitCorrection");
            }
            return true;
        }

        public boolean commitText(CharSequence text, int newCursorPosition) {
            if (LOG_IME) {
                Log.w(TAG, "commitText(\"" + text + "\", " + newCursorPosition + ")");
            }
            clearComposingText();
            sendText(text);
            setImeBuffer("");
            mCursor = 0;
            return true;
        }

        private void clearComposingText() {
            int len = mImeBuffer.length();
            if (mComposingTextStart > len || mComposingTextEnd > len) {
                mComposingTextEnd = mComposingTextStart = 0;
                return;
            }
            setImeBuffer(mImeBuffer.substring(0, mComposingTextStart) + mImeBuffer.substring(mComposingTextEnd));
            if (mCursor < mComposingTextStart) {
            // do nothing
            } else if (mCursor < mComposingTextEnd) {
                mCursor = mComposingTextStart;
            } else {
                mCursor -= mComposingTextEnd - mComposingTextStart;
            }
            mComposingTextEnd = mComposingTextStart = 0;
        }

        public boolean deleteSurroundingText(int leftLength, int rightLength) {
            if (LOG_IME) {
                Log.w(TAG, "deleteSurroundingText(" + leftLength + "," + rightLength + ")");
            }
            if (leftLength > 0) {
                for (int i = 0; i < leftLength; i++) {
                    sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                }
            } else if ((leftLength == 0) && (rightLength == 0)) {
                // Delete key held down / repeating
                sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
            }
            // TODO: handle forward deletes.
            return true;
        }

        public boolean performEditorAction(int actionCode) {
            if (LOG_IME) {
                Log.w(TAG, "performEditorAction(" + actionCode + ")");
            }
            if (actionCode == EditorInfo.IME_ACTION_UNSPECIFIED) {
                // The "return" key has been pressed on the IME.
                sendText("\r");
            }
            return true;
        }

        public boolean sendKeyEvent(KeyEvent event) {
            if (LOG_IME) {
                Log.w(TAG, "sendKeyEvent(" + event + ")");
            }
            // Some keys are sent here rather than to commitText.
            // In particular, del and the digit keys are sent here.
            // (And I have reports that the HTC Magic also sends Return here.)
            // As a bit of defensive programming, handle every key.
            dispatchKeyEvent(event);
            return true;
        }

        public boolean setComposingText(CharSequence text, int newCursorPosition) {
            if (LOG_IME) {
                Log.w(TAG, "setComposingText(\"" + text + "\", " + newCursorPosition + ")");
            }
            int len = mImeBuffer.length();
            if (mComposingTextStart > len || mComposingTextEnd > len) {
                return false;
            }
            setImeBuffer(mImeBuffer.substring(0, mComposingTextStart) + text + mImeBuffer.substring(mComposingTextEnd));
            mComposingTextEnd = mComposingTextStart + text.length();
            mCursor = newCursorPosition > 0 ? mComposingTextEnd + newCursorPosition - 1 : mComposingTextStart - newCursorPosition;
            return true;
        }

        public boolean setSelection(int start, int end) {
            if (LOG_IME) {
                Log.w(TAG, "setSelection" + start + "," + end);
            }
            int length = mImeBuffer.length();
            if (start == end && start > 0 && start < length) {
                mSelectedTextStart = mSelectedTextEnd = 0;
                mCursor = start;
            } else if (start < end && start > 0 && end < length) {
                mSelectedTextStart = start;
                mSelectedTextEnd = end;
                mCursor = start;
            }
            return true;
        }

        public boolean setComposingRegion(int start, int end) {
            if (LOG_IME) {
                Log.w(TAG, "setComposingRegion " + start + "," + end);
            }
            if (start < end && start > 0 && end < mImeBuffer.length()) {
                clearComposingText();
                mComposingTextStart = start;
                mComposingTextEnd = end;
            }
            return true;
        }

        public CharSequence getSelectedText(int flags) {
            if (LOG_IME) {
                Log.w(TAG, "getSelectedText " + flags);
            }
            int len = mImeBuffer.length();
            if (mSelectedTextEnd >= len || mSelectedTextStart > mSelectedTextEnd) {
                return "";
            }
            return mImeBuffer.substring(mSelectedTextStart, mSelectedTextEnd + 1);
        }
    };
}
Also used : BaseInputConnection(android.view.inputmethod.BaseInputConnection) CompletionInfo(android.view.inputmethod.CompletionInfo) KeyEvent(android.view.KeyEvent) Bundle(android.os.Bundle) ExtractedTextRequest(android.view.inputmethod.ExtractedTextRequest) IOException(java.io.IOException) CorrectionInfo(android.view.inputmethod.CorrectionInfo) Paint(android.graphics.Paint)

Example 2 with CorrectionInfo

use of android.view.inputmethod.CorrectionInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class InputLogic method commitCurrentAutoCorrection.

/**
     * Commit the current auto-correction.
     *
     * This will commit the best guess of the keyboard regarding what the user meant by typing
     * the currently composing word. The IME computes suggestions and assigns a confidence score
     * to each of them; when it's confident enough in one suggestion, it replaces the typed string
     * by this suggestion at commit time. When it's not confident enough, or when it has no
     * suggestions, or when the settings or environment does not allow for auto-correction, then
     * this method just commits the typed string.
     * Note that if suggestions are currently being computed in the background, this method will
     * block until the computation returns. This is necessary for consistency (it would be very
     * strange if pressing space would commit a different word depending on how fast you press).
     *
     * @param settingsValues the current value of the settings.
     * @param separator the separator that's causing the commit to happen.
     */
private void commitCurrentAutoCorrection(final SettingsValues settingsValues, final String separator, final LatinIME.UIHandler handler) {
    // Complete any pending suggestions query first
    if (handler.hasPendingUpdateSuggestions()) {
        handler.cancelUpdateSuggestionStrip();
        // To know the input style here, we should retrieve the in-flight "update suggestions"
        // message and read its arg1 member here. However, the Handler class does not let
        // us retrieve this message, so we can't do that. But in fact, we notice that
        // we only ever come here when the input style was typing. In the case of batch
        // input, we update the suggestions synchronously when the tail batch comes. Likewise
        // for application-specified completions. As for recorrections, we never auto-correct,
        // so we don't come here either. Hence, the input style is necessarily
        // INPUT_STYLE_TYPING.
        performUpdateSuggestionStripSync(settingsValues, SuggestedWords.INPUT_STYLE_TYPING);
    }
    final SuggestedWordInfo autoCorrectionOrNull = mWordComposer.getAutoCorrectionOrNull();
    final String typedWord = mWordComposer.getTypedWord();
    final String stringToCommit = (autoCorrectionOrNull != null) ? autoCorrectionOrNull.mWord : typedWord;
    if (stringToCommit != null) {
        if (TextUtils.isEmpty(typedWord)) {
            throw new RuntimeException("We have an auto-correction but the typed word " + "is empty? Impossible! I must commit suicide.");
        }
        final boolean isBatchMode = mWordComposer.isBatchMode();
        commitChosenWord(settingsValues, stringToCommit, LastComposedWord.COMMIT_TYPE_DECIDED_WORD, separator);
        if (!typedWord.equals(stringToCommit)) {
            // This will make the correction flash for a short while as a visual clue
            // to the user that auto-correction happened. It has no other effect; in particular
            // note that this won't affect the text inside the text field AT ALL: it only makes
            // the segment of text starting at the supplied index and running for the length
            // of the auto-correction flash. At this moment, the "typedWord" argument is
            // ignored by TextView.
            mConnection.commitCorrection(new CorrectionInfo(mConnection.getExpectedSelectionEnd() - stringToCommit.length(), typedWord, stringToCommit));
            String prevWordsContext = (autoCorrectionOrNull != null) ? autoCorrectionOrNull.mPrevWordsContext : "";
            StatsUtils.onAutoCorrection(typedWord, stringToCommit, isBatchMode, mDictionaryFacilitator, prevWordsContext);
            StatsUtils.onWordCommitAutoCorrect(stringToCommit, isBatchMode);
        } else {
            StatsUtils.onWordCommitUserTyped(stringToCommit, isBatchMode);
        }
    }
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) SpannableString(android.text.SpannableString) CorrectionInfo(android.view.inputmethod.CorrectionInfo)

Aggregations

CorrectionInfo (android.view.inputmethod.CorrectionInfo)2 Paint (android.graphics.Paint)1 Bundle (android.os.Bundle)1 SpannableString (android.text.SpannableString)1 KeyEvent (android.view.KeyEvent)1 BaseInputConnection (android.view.inputmethod.BaseInputConnection)1 CompletionInfo (android.view.inputmethod.CompletionInfo)1 ExtractedTextRequest (android.view.inputmethod.ExtractedTextRequest)1 SuggestedWordInfo (com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)1 IOException (java.io.IOException)1