Java Code Examples for android.view.inputmethod.InputConnection#getExtractedText()

The following examples show how to use android.view.inputmethod.InputConnection#getExtractedText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CtrlInputAction.java    From remotekeyboard with Apache License 2.0 6 votes vote down vote up
/**
 * Try to replace the current word with its substitution.
 */
private void replaceText(InputConnection con) {
	ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
	if (txt != null) {
		int end = txt.text.toString().indexOf(" ", txt.selectionEnd);
		if (end == -1) {
			end = txt.text.length();
		}
		int start = txt.text.toString().lastIndexOf(" ", txt.selectionEnd - 1);
		start++;
		String sel = txt.text.subSequence(start, end).toString();
		String rep = myService.replacements.get(sel);
		if (rep != null) {
			con.setComposingRegion(start, end);
			con.setComposingText(rep, 1);
			con.finishComposingText();
		}
		else {
			String err = myService.getResources().getString(
					R.string.err_no_replacement, sel);
			Toast.makeText(myService, err, Toast.LENGTH_SHORT).show();
		}
	}
}
 
Example 2
Source File: KeyboardWidget.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
private String getTextBeforeCursor(InputConnection aConnection) {
    if (aConnection == null) {
        return "";
    }

    ExtractedText extracted = aConnection.getExtractedText(new ExtractedTextRequest(),0);
    if ((extracted == null) || extracted.text == null) {
        return "";
    }

    String fullText = extracted.text.toString();
    return aConnection.getTextBeforeCursor(fullText.length(),0).toString();
}
 
Example 3
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
@Override
public void moveCursorEnd() {
    InputConnection ic = getInputConnection();
    if (ic == null) return;
    ExtractedText extractedText = ic.getExtractedText(new ExtractedTextRequest(), 0);
    if (extractedText == null || extractedText.text == null) return;
    int length = extractedText.text.length();
    ic.setSelection(length, length);
}
 
Example 4
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
@Override
public void selectWordBack() {
    InputConnection ic = getInputConnection();
    if (ic == null) return;
    ExtractedText extractedText = ic.getExtractedText(new ExtractedTextRequest(), 0);
    int previousWordBoundary = getPreviousWordBoundary(extractedText.text, extractedText.selectionStart);
    int start = extractedText.startOffset + previousWordBoundary;
    int end = extractedText.startOffset + extractedText.selectionEnd;
    ic.setSelection(start, end);
}
 
Example 5
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
@Override
public void selectWordForward() {
    InputConnection ic = getInputConnection();
    if (ic == null) return;
    ExtractedText extractedText = ic.getExtractedText(new ExtractedTextRequest(), 0);
    int nextWordBoundary = getNextWordBoundary(extractedText.text, extractedText.selectionEnd);
    int start = extractedText.startOffset + extractedText.selectionStart;
    int end = extractedText.startOffset + nextWordBoundary;
    ic.setSelection(start, end);
}
 
Example 6
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
private void checkReCorrectionOnStart() {
    if (mReCorrectionEnabled && isPredictionOn()) {
        // First get the cursor position. This is required by
        // setOldSuggestions(), so that
        // it can pass the correct range to setComposingRegion(). At this
        // point, we don't
        // have valid values for mLastSelectionStart/Stop because
        // onUpdateSelection() has
        // not been called yet.
        InputConnection ic = getCurrentInputConnection();
        if (ic == null)
            return;
        ExtractedTextRequest etr = new ExtractedTextRequest();
        etr.token = 0; // anything is fine here
        ExtractedText et = ic.getExtractedText(etr, 0);
        if (et == null)
            return;

        mLastSelectionStart = et.startOffset + et.selectionStart;
        mLastSelectionEnd = et.startOffset + et.selectionEnd;

        // Then look for possible corrections in a delayed fashion
        if (!TextUtils.isEmpty(et.text) && isCursorTouchingWord()) {
            postUpdateOldSuggestions();
        }
    }
}
 
Example 7
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
private static int getCursorPosition(InputConnection connection) {
    ExtractedText extracted = connection.getExtractedText(
        new ExtractedTextRequest(), 0);
    if (extracted == null) {
      return -1;
    }
    return extracted.startOffset + extracted.selectionStart;
}
 
Example 8
Source File: CtrlInputAction.java    From remotekeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Place the cursor on the next occurance of a symbol
 * 
 * @param con
 *          driver
 * @param symbol
 *          the symbol to jump to
 */
private void jumpForward(InputConnection con, int symbol) {
	ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
	if (txt != null) {
		int pos = txt.text.toString().indexOf(symbol, txt.selectionEnd + 1);
		if (pos == -1) {
			pos = txt.text.length();
		}
		con.setSelection(pos, pos);
	}
}
 
Example 9
Source File: CtrlInputAction.java    From remotekeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Place the cursor on the last occusrance of a symbol
 * 
 * @param con
 *          driver
 * @param symbol
 *          the symbol to jump to
 */
private void jumpBackward(InputConnection con, int symbol) {
	ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
	if (txt != null) {
		int pos = txt.text.toString().lastIndexOf(symbol, txt.selectionEnd - 2);
		pos++;

		con.setSelection(pos, pos);
	}

}
 
Example 10
Source File: CtrlInputAction.java    From remotekeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * use ROT13 to scramble the contents of the editor
 */
private void scramble(InputConnection con) {
	char[] buffer = null;
	CharSequence selected = con.getSelectedText(0);
	if (selected != null) {
		buffer = selected.toString().toCharArray();
	}
	else {
		ExtractedText txt = con.getExtractedText(new ExtractedTextRequest(), 0);
		if (txt == null) {
			return;
		}
		buffer = txt.text.toString().toCharArray();
		if (buffer.length == 0)
			return;
	}
	// char[] buffer = con.getSelectedText(0).toString().toCharArray();
	// //con.getExtractedText(new
	// ExtractedTextRequest(),0).text.toString().toCharArray();
	for (int i = 0; i < buffer.length; i++) {
		if (buffer[i] >= 'a' && buffer[i] <= 'm')
			buffer[i] += 13;
		else if (buffer[i] >= 'A' && buffer[i] <= 'M')
			buffer[i] += 13;
		else if (buffer[i] >= 'n' && buffer[i] <= 'z')
			buffer[i] -= 13;
		else if (buffer[i] >= 'N' && buffer[i] <= 'Z')
			buffer[i] -= 13;
	}
	if (selected == null) {
		con.setComposingRegion(0, buffer.length);
	}
	con.setComposingText(new String(buffer), 1);
	con.finishComposingText();
}
 
Example 11
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
}