Code example for InputMethodService

Methods: getCurrentInputConnection, getSystemService

0
		return inputMethodService.getCurrentInputEditorInfo();
	} 
 
	@Nonnull 
	private InputConnection getCurrentInputConnection() {
		InputConnection result = this.inputMethodService.getCurrentInputConnection();
		if (result == null) {
			result = NoInputConnection.getInstance();
		} 
		return result;
	} 
 
	@Override 
	public CharSequence getTypedText() {
		return typedText;
	} 
 
	@Override 
	public void clearTypedText() { 
		this.typedText.setLength(0);
	} 
 
	@Override 
	public void undo() { 
		if (this.history.isUndoAvailable()) {
			final KeyboardInputHistoryState state = this.history.undo(null);
			restoreFromHistory(state);
		} 
	} 
 
	private void restoreFromHistory(@Nullable KeyboardInputHistoryState state) {
		if (state != null) {
			final InputConnection ic = getCurrentInputConnection();
			ic.deleteSurroundingText(MAX_INT, MAX_INT);
			ic.commitText(state.getCharSequence(), 1);
		} 
	} 
 
	@Override 
	public void redo() { 
		if (this.history.isRedoAvailable()) {
			final KeyboardInputHistoryState state = this.history.redo(null);
			restoreFromHistory(state);
		} 
	} 
 
	@Override 
	public boolean handleBackspace() { 
		boolean changed = false;
 
		int length = typedText.length();
 
		final InputConnection ic = getCurrentInputConnection();
		if (length > 1) {
			typedText.delete(length - 1, length);
			ic.setComposingText(typedText, 1);
			changed = true;
		} else if (length > 0) {
			clearTypedText(); 
			commitText(ic, "", 0);
			changed = true;
		} 
 
		return changed;
	} 
 
	@Override 
	public void sendKeyEvent(@Nonnull KeyEvent keyEvent) {
		getCurrentInputConnection().sendKeyEvent(keyEvent);
	} 
 
	@Override 
	public int translateKeyDown(int unicodeChar) {
		if (!Strings.isEmpty(typedText)) {
			char accent = typedText.charAt(typedText.length() - 1);
			int composed = KeyEvent.getDeadChar(accent, unicodeChar);
 
			if (composed != 0) {
				unicodeChar = composed;
				typedText.setLength(typedText.length() - 1);
			} 
		} 
 
		return unicodeChar;
	} 
 
	@Override 
	public void commitCompletion(@Nonnull CompletionInfo completionInfo) {
		getCurrentInputConnection().commitCompletion(completionInfo);
	} 
 
	@Override 
	public void append(char primaryCode) {
		typedText.append(primaryCode);
		getCurrentInputConnection().setComposingText(typedText, 1);
	} 
 
	@Override 
	public void handleCursorRight() { 
		final InputConnection ic = getCurrentInputConnection();
		int selectionStart = getSelectionStart(ic);
		int selectionEnd = getSelectionEnd(ic, selectionStart);
		if (selectionStart > 0) {
			selectionStart = selectionStart - 1;
			ic.setSelection(selectionStart, selectionEnd);
		} 
	} 
 
	private int getSelectionEnd(@Nonnull InputConnection ic, int selectionStart) {
		final CharSequence selectedText = ic.getSelectedText(0);
		return selectionStart + (selectedText == null ? 0 : selectedText.length());
	} 
 
	private int getSelectionStart(@Nonnull InputConnection ic) {
		return ic.getTextBeforeCursor(MAX_INT, 0).length();
	} 
 
	@Override 
	public void handleCursorLeft() { 
		final InputConnection ic = getCurrentInputConnection();
		int selectionStart = getSelectionStart(ic);
		int selectionEnd = getSelectionEnd(ic, selectionStart);
		if (selectionStart < 0) {
			selectionStart = selectionStart - 1;
			ic.setSelection(selectionStart, selectionEnd);
		} 
	} 
 
	@Override 
	public void handleClear() { 
		typedText.setLength(0);
		final InputConnection ic = getCurrentInputConnection();
		ic.setSelection(0, 0);
		ic.deleteSurroundingText(MAX_INT, MAX_INT);
 
	} 
 
	@Override 
	public void handlePaste() { 
		final ClipboardManager clipboardManager = (ClipboardManager) inputMethodService.getSystemService(Context.CLIPBOARD_SERVICE);
		final CharSequence text = clipboardManager.getText();
		if (!Strings.isEmpty(text)) {
			commitText(text, 1);
		} 
	}