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

The following examples show how to use android.view.inputmethod.InputConnection#finishComposingText() . 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: TerminalKeyboard.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Deal with the editor reporting movement of its cursor.
 */
@Override
public void onUpdateSelection(int oldSelStart, int oldSelEnd,
                              int newSelStart, int newSelEnd,
                              int candidatesStart, int candidatesEnd) {
    super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
            candidatesStart, candidatesEnd);

    // If the current selection in the text view changes, we should
    // clear whatever candidate text we have.
    if (mComposing.length() > 0 && (newSelStart != candidatesEnd
            || newSelEnd != candidatesEnd)) {
        mComposing.setLength(0);
        updateCandidates();
        InputConnection ic = getCurrentInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    }
}
 
Example 2
Source File: SoftKeyboard.java    From AndroidKeyboard with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deal with the editor reporting movement of its cursor.
 */
@Override
public void onUpdateSelection(int oldSelStart, int oldSelEnd,
                              int newSelStart, int newSelEnd,
                              int candidatesStart, int candidatesEnd) {
    super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
            candidatesStart, candidatesEnd);

    // If the current selection in the text view changes, we should
    // clear whatever candidate text we have.
    if (mComposing.length() > 0 && (newSelStart != candidatesEnd
            || newSelEnd != candidatesEnd)) {
        mComposing.setLength(0);
        updateCandidates();
        InputConnection ic = getCurrentInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    }
}
 
Example 3
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
/**
 * Append newText to the text field represented by connection.
 * The new text becomes selected.
 */
public static void appendText(InputConnection connection, String newText) {
    if (connection == null) {
        return;
    }

    // Commit the composing text
    connection.finishComposingText();

    // Add a space if the field already has text.
    CharSequence charBeforeCursor = connection.getTextBeforeCursor(1, 0);
    if (charBeforeCursor != null
            && !charBeforeCursor.equals(" ")
            && (charBeforeCursor.length() > 0)) {
        newText = " " + newText;
    }

    connection.setComposingText(newText, 1);
}
 
Example 4
Source File: Pathfinder.java    From sinovoice-pathfinder with MIT License 6 votes vote down vote up
/**
 * Deal with the editor reporting movement of its cursor.
 */
@Override 
public void onUpdateSelection(int oldSelStart, int oldSelEnd,
        int newSelStart, int newSelEnd,
        int candidatesStart, int candidatesEnd) {
    super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
            candidatesStart, candidatesEnd);
    
    // If the current selection in the text view changes, we should
    // clear whatever candidate text we have.
    if (mComposing.length() > 0 && (newSelStart != candidatesEnd
            || newSelEnd != candidatesEnd)) {
        mComposing.setLength(0);
        updateCandidates();
        InputConnection ic = getCurrentInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    }
}
 
Example 5
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 6
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @hide
 */
public void onExtractedDeleteText(int start, int end) {
    InputConnection conn = getCurrentInputConnection();
    if (conn != null) {
        conn.finishComposingText();
        conn.setSelection(start, start);
        conn.deleteSurroundingText(0, end - start);
    }
}
 
Example 7
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
/**
 * @param oldSelStart     the selection start before the update
 * @param oldSelEnd       the selection end before the update
 * @param newSelStart     the selection start after the update
 * @param newSelEnd       the selection end after the update
 * @param candidatesStart (todo what is this for?)
 * @param candidatesEnd   (todo what is this for?)
 */
@SuppressWarnings("unused")
public void onUpdateSelection(int oldSelStart,
                              int oldSelEnd,
                              int newSelStart,
                              int newSelEnd,
                              int candidatesStart,
                              int candidatesEnd) {

    // TODO in the Android source InputMethodService also handles Extracted Text here

    // currently we are only using composing for popup glyph selection.
    // If we want to be more like the standard keyboards we could do
    // composing on the whole word.
    if (!TextUtils.isEmpty(mComposing) &&
            (newSelStart != candidatesEnd
                    || newSelEnd != candidatesEnd)) {
        mComposing = "";
        InputConnection ic = getInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    }
    if (mCandidatesView != null && mCandidatesView.hasCandidates()) {
        mCandidatesView.clearCandidates();
    }

}
 
Example 8
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
private void handleOldComposingText(boolean newInputIsMongol) {
    InputConnection ic = getInputConnection();
    if (TextUtils.isEmpty(mComposing)) return;
    if (newInputIsMongol) {
        ic.commitText(mComposing, 1);
    } else {
        ic.finishComposingText();
    }
    mComposing = "";
}
 
Example 9
Source File: BrailleIME.java    From brailleback with Apache License 2.0 5 votes vote down vote up
/**
 * Cancels the text composition by leaving the already-composed text there
 * and clearing the composition state. Use this when the user tries to
 * interact with the edit field before composition has finished.
 */
private boolean cancelComposingText() {
    InputConnection ic = getCurrentInputConnection();
    if (ic == null) {
        LogUtils.log(this, Log.WARN, "missing IC %s", ic);
        return false;
    }

    mComposingBraille.clear();
    return ic.finishComposingText();
}
 
Example 10
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
@Override
public void onConfigurationChanged(Configuration conf) {
    Log.i("PCKeyboard", "onConfigurationChanged()");
    // If the system locale changes and is different from the saved
    // locale (mSystemLocale), then reload the input locale list from the
    // latin ime settings (shared prefs) and reset the input locale
    // to the first one.
    final String systemLocale = conf.locale.toString();
    if (!TextUtils.equals(systemLocale, mSystemLocale)) {
        mSystemLocale = systemLocale;
        if (mLanguageSwitcher != null) {
            mLanguageSwitcher.loadLocales(PreferenceManager
                    .getDefaultSharedPreferences(this));
            mLanguageSwitcher.setSystemLocale(conf.locale);
            toggleLanguage(true, true);
        } else {
            reloadKeyboards();
        }
    }
    // If orientation changed while predicting, commit the change
    if (conf.orientation != mOrientation) {
        InputConnection ic = getCurrentInputConnection();
        commitTyped(ic, true);
        if (ic != null)
            ic.finishComposingText(); // For voice input
        mOrientation = conf.orientation;
        reloadKeyboards();
        removeCandidateViewContainer();
    }
    mConfigurationChanging = true;
    super.onConfigurationChanged(conf);
    mConfigurationChanging = false;
}
 
Example 11
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the word surrounding the cursor. Parameters are identical to
 * getWordAtCursor.
 */
public static void deleteWordAtCursor(
    InputConnection connection, String separators) {

    Range range = getWordRangeAtCursor(connection, separators, null);
    if (range == null) return;

    connection.finishComposingText();
    // Move cursor to beginning of word, to avoid crash when cursor is outside
    // of valid range after deleting text.
    int newCursor = getCursorPosition(connection) - range.charsBefore;
    connection.setSelection(newCursor, newCursor);
    connection.deleteSurroundingText(0, range.charsBefore + range.charsAfter);
}
 
Example 12
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 13
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 4 votes vote down vote up
@Override
public void onUpdateSelection(int oldSelStart, int oldSelEnd,
        int newSelStart, int newSelEnd, int candidatesStart,
        int candidatesEnd) {
    super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
            candidatesStart, candidatesEnd);

    // If the current selection in the text view changes, we should
    // clear whatever candidate text we have.
    if ((((mComposing.length() > 0 && mPredicting))
            && (newSelStart != candidatesEnd || newSelEnd != candidatesEnd) && mLastSelectionStart != newSelStart)) {
        mComposing.setLength(0);
        mPredicting = false;
        postUpdateSuggestions();
        TextEntryState.reset();
        InputConnection ic = getCurrentInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    } else if (!mPredicting && !mJustAccepted) {
        switch (TextEntryState.getState()) {
        case ACCEPTED_DEFAULT:
            TextEntryState.reset();
            // fall through
        case SPACE_AFTER_PICKED:
            mJustAddedAutoSpace = false; // The user moved the cursor.
            break;
        }
    }
    mJustAccepted = false;
    postUpdateShiftKeyState();

    // Make a note of the cursor position
    mLastSelectionStart = newSelStart;
    mLastSelectionEnd = newSelEnd;

    if (mReCorrectionEnabled) {
        // Don't look for corrections if the keyboard is not visible
        if (mKeyboardSwitcher != null
                && mKeyboardSwitcher.getInputView() != null
                && mKeyboardSwitcher.getInputView().isShown()) {
            // Check if we should go in or out of correction mode.
            if (isPredictionOn()
                    && mJustRevertedSeparator == null
                    && (candidatesStart == candidatesEnd
                            || newSelStart != oldSelStart || TextEntryState
                            .isCorrecting())
                    && (newSelStart < newSelEnd - 1 || (!mPredicting))) {
                if (isCursorTouchingWord()
                        || mLastSelectionStart < mLastSelectionEnd) {
                    postUpdateOldSuggestions();
                } else {
                    abortCorrection(false);
                    // Show the punctuation suggestions list if the current
                    // one is not
                    // and if not showing "Touch again to save".
                    if (mCandidateView != null
                            && !mSuggestPuncList.equals(mCandidateView
                                    .getSuggestions())
                            && !mCandidateView
                                    .isShowingAddToDictionaryHint()) {
                        setNextSuggestions();
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Called when the input view is being hidden from the user.  This will
 * be called either prior to hiding the window, or prior to switching to
 * another target for editing.
 * 
 * <p>The default
 * implementation uses the InputConnection to clear any active composing
 * text; you can override this (not calling the base class implementation)
 * to perform whatever behavior you would like.
 * 
 * @param finishingInput If true, {@link #onFinishInput} will be
 * called immediately after.
 */
public void onFinishInputView(boolean finishingInput) {
    if (!finishingInput) {
        InputConnection ic = getCurrentInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    }
}
 
Example 15
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Called when the candidates view is being hidden from the user.  This will
 * be called either prior to hiding the window, or prior to switching to
 * another target for editing.
 * 
 * <p>The default
 * implementation uses the InputConnection to clear any active composing
 * text; you can override this (not calling the base class implementation)
 * to perform whatever behavior you would like.
 * 
 * @param finishingInput If true, {@link #onFinishInput} will be
 * called immediately after.
 */
public void onFinishCandidatesView(boolean finishingInput) {
    if (!finishingInput) {
        InputConnection ic = getCurrentInputConnection();
        if (ic != null) {
            ic.finishComposingText();
        }
    }
}
 
Example 16
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Called to inform the input method that text input has finished in
 * the last editor.  At this point there may be a call to
 * {@link #onStartInput(EditorInfo, boolean)} to perform input in a
 * new editor, or the input method may be left idle.  This method is
 * <em>not</em> called when input restarts in the same editor.
 * 
 * <p>The default
 * implementation uses the InputConnection to clear any active composing
 * text; you can override this (not calling the base class implementation)
 * to perform whatever behavior you would like.
 */
public void onFinishInput() {
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.finishComposingText();
    }
}