Search in sources :

Example 11 with ExtractedTextRequest

use of android.view.inputmethod.ExtractedTextRequest 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 12 with ExtractedTextRequest

use of android.view.inputmethod.ExtractedTextRequest in project android_frameworks_base by ParanoidAndroid.

the class Editor method reportExtractedText.

boolean reportExtractedText() {
    final Editor.InputMethodState ims = mInputMethodState;
    if (ims != null) {
        final boolean contentChanged = ims.mContentChanged;
        if (contentChanged || ims.mSelectionModeChanged) {
            ims.mContentChanged = false;
            ims.mSelectionModeChanged = false;
            final ExtractedTextRequest req = ims.mExtractedTextRequest;
            if (req != null) {
                InputMethodManager imm = InputMethodManager.peekInstance();
                if (imm != null) {
                    if (TextView.DEBUG_EXTRACT)
                        Log.v(TextView.LOG_TAG, "Retrieving extracted start=" + ims.mChangedStart + " end=" + ims.mChangedEnd + " delta=" + ims.mChangedDelta);
                    if (ims.mChangedStart < 0 && !contentChanged) {
                        ims.mChangedStart = EXTRACT_NOTHING;
                    }
                    if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, ims.mChangedDelta, ims.mExtractedText)) {
                        if (TextView.DEBUG_EXTRACT)
                            Log.v(TextView.LOG_TAG, "Reporting extracted start=" + ims.mExtractedText.partialStartOffset + " end=" + ims.mExtractedText.partialEndOffset + ": " + ims.mExtractedText.text);
                        imm.updateExtractedText(mTextView, req.token, ims.mExtractedText);
                        ims.mChangedStart = EXTRACT_UNKNOWN;
                        ims.mChangedEnd = EXTRACT_UNKNOWN;
                        ims.mChangedDelta = 0;
                        ims.mContentChanged = false;
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ExtractedTextRequest(android.view.inputmethod.ExtractedTextRequest) InputMethodManager(android.view.inputmethod.InputMethodManager)

Example 13 with ExtractedTextRequest

use of android.view.inputmethod.ExtractedTextRequest in project platform_frameworks_base by android.

the class Editor method reportExtractedText.

boolean reportExtractedText() {
    final Editor.InputMethodState ims = mInputMethodState;
    if (ims != null) {
        final boolean contentChanged = ims.mContentChanged;
        if (contentChanged || ims.mSelectionModeChanged) {
            ims.mContentChanged = false;
            ims.mSelectionModeChanged = false;
            final ExtractedTextRequest req = ims.mExtractedTextRequest;
            if (req != null) {
                InputMethodManager imm = InputMethodManager.peekInstance();
                if (imm != null) {
                    if (TextView.DEBUG_EXTRACT)
                        Log.v(TextView.LOG_TAG, "Retrieving extracted start=" + ims.mChangedStart + " end=" + ims.mChangedEnd + " delta=" + ims.mChangedDelta);
                    if (ims.mChangedStart < 0 && !contentChanged) {
                        ims.mChangedStart = EXTRACT_NOTHING;
                    }
                    if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, ims.mChangedDelta, ims.mExtractedText)) {
                        if (TextView.DEBUG_EXTRACT)
                            Log.v(TextView.LOG_TAG, "Reporting extracted start=" + ims.mExtractedText.partialStartOffset + " end=" + ims.mExtractedText.partialEndOffset + ": " + ims.mExtractedText.text);
                        imm.updateExtractedText(mTextView, req.token, ims.mExtractedText);
                        ims.mChangedStart = EXTRACT_UNKNOWN;
                        ims.mChangedEnd = EXTRACT_UNKNOWN;
                        ims.mChangedDelta = 0;
                        ims.mContentChanged = false;
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ExtractedTextRequest(android.view.inputmethod.ExtractedTextRequest) InputMethodManager(android.view.inputmethod.InputMethodManager)

Example 14 with ExtractedTextRequest

use of android.view.inputmethod.ExtractedTextRequest in project wifikeyboard by IvanVolosyuk.

the class WiFiInputMethod method getText.

String getText() {
    String text = "";
    try {
        InputConnection conn = getCurrentInputConnection();
        ExtractedTextRequest req = new ExtractedTextRequest();
        req.hintMaxChars = 1000000;
        req.hintMaxLines = 10000;
        req.flags = 0;
        req.token = 1;
        text = conn.getExtractedText(req, 0).text.toString();
    } catch (Throwable t) {
    }
    return text;
}
Also used : InputConnection(android.view.inputmethod.InputConnection) ExtractedTextRequest(android.view.inputmethod.ExtractedTextRequest)

Example 15 with ExtractedTextRequest

use of android.view.inputmethod.ExtractedTextRequest in project android_frameworks_base by AOSPA.

the class InputMethodService method startExtractingText.

void startExtractingText(boolean inputChanged) {
    final ExtractEditText eet = mExtractEditText;
    if (eet != null && getCurrentInputStarted() && isFullscreenMode()) {
        mExtractedToken++;
        ExtractedTextRequest req = new ExtractedTextRequest();
        req.token = mExtractedToken;
        req.flags = InputConnection.GET_TEXT_WITH_STYLES;
        req.hintMaxLines = 10;
        req.hintMaxChars = 10000;
        InputConnection ic = getCurrentInputConnection();
        mExtractedText = ic == null ? null : ic.getExtractedText(req, InputConnection.GET_EXTRACTED_TEXT_MONITOR);
        if (mExtractedText == null || ic == null) {
            Log.e(TAG, "Unexpected null in startExtractingText : mExtractedText = " + mExtractedText + ", input connection = " + ic);
        }
        final EditorInfo ei = getCurrentInputEditorInfo();
        try {
            eet.startInternalChanges();
            onUpdateExtractingVisibility(ei);
            onUpdateExtractingViews(ei);
            int inputType = ei.inputType;
            if ((inputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
                if ((inputType & EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE) != 0) {
                    inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
                }
            }
            eet.setInputType(inputType);
            eet.setHint(ei.hintText);
            if (mExtractedText != null) {
                eet.setEnabled(true);
                eet.setExtractedText(mExtractedText);
            } else {
                eet.setEnabled(false);
                eet.setText("");
            }
        } finally {
            eet.finishInternalChanges();
        }
        if (inputChanged) {
            onExtractingInputChanged(ei);
        }
    }
}
Also used : InputConnection(android.view.inputmethod.InputConnection) EditorInfo(android.view.inputmethod.EditorInfo) ExtractedTextRequest(android.view.inputmethod.ExtractedTextRequest)

Aggregations

ExtractedTextRequest (android.view.inputmethod.ExtractedTextRequest)19 InputConnection (android.view.inputmethod.InputConnection)9 EditorInfo (android.view.inputmethod.EditorInfo)8 InputMethodManager (android.view.inputmethod.InputMethodManager)8 KeyEvent (android.view.KeyEvent)2 ExtractedText (android.view.inputmethod.ExtractedText)2 Paint (android.graphics.Paint)1 Bundle (android.os.Bundle)1 Pair (android.support.v4.util.Pair)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 BaseInputConnection (android.view.inputmethod.BaseInputConnection)1 CompletionInfo (android.view.inputmethod.CompletionInfo)1 CorrectionInfo (android.view.inputmethod.CorrectionInfo)1 IOException (java.io.IOException)1