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(); } } }
private void doubleSpace() { //if (!mAutoPunctuate) return; if (mCorrectionMode == Suggest.CORRECTION_NONE) return; final InputConnection ic = getCurrentInputConnection(); if (ic == null) return; CharSequence lastThree = ic.getTextBeforeCursor(3, 0); if (lastThree != null && lastThree.length() == 3 && Character.isLetterOrDigit(lastThree.charAt(0)) && lastThree.charAt(1) == KEYCODE_SPACE && lastThree.charAt(2) == KEYCODE_SPACE) { ic.beginBatchEdit(); ic.deleteSurroundingText(2, 0); ic.commitText(". ", 1); ic.endBatchEdit(); updateShiftKeyState(getCurrentInputEditorInfo()); mJustAddedAutoSpace = true; } }
public void onText(CharSequence text) { InputConnection ic = getCurrentInputConnection(); if (ic == null) return; if (text == null) { Log.e("KP2AK", "text = null!"); return; } abortCorrection(false); ic.beginBatchEdit(); if (mPredicting) { commitTyped(ic); } maybeRemovePreviousPeriod(text); ic.commitText(text, 1); ic.endBatchEdit(); updateShiftKeyState(getCurrentInputEditorInfo()); mKeyboardSwitcher.onKey(0); // dummy key code. mJustRevertedSeparator = null; mJustAddedAutoSpace = false; mEnteredText = text; }
public void revertLastWord(boolean deleteChar) { final int length = mComposing.length(); if (!mPredicting && length > 0) { final InputConnection ic = getCurrentInputConnection(); mPredicting = true; mJustRevertedSeparator = ic.getTextBeforeCursor(1, 0); if (deleteChar) ic.deleteSurroundingText(1, 0); int toDelete = mCommittedLength; CharSequence toTheLeft = ic.getTextBeforeCursor(mCommittedLength, 0); if (toTheLeft != null && toTheLeft.length() > 0 && isWordSeparator(toTheLeft.charAt(0))) { toDelete--; } ic.deleteSurroundingText(toDelete, 0); ic.setComposingText(mComposing, 1); TextEntryState.backspace(); postUpdateSuggestions(); } else { sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); mJustRevertedSeparator = null; } }
public static CharSequence getPreviousWord(InputConnection connection, String sentenceSeperators) { //TODO: Should fix this. This could be slow! CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0); if (prev == null) { return null; } String[] w = spaceRegex.split(prev); if (w.length >= 2 && w[w.length-2].length() > 0) { char lastChar = w[w.length-2].charAt(w[w.length-2].length() -1); if (sentenceSeperators.contains(String.valueOf(lastChar))) { return null; } return w[w.length-2]; } else { return null; } }
@Override public void onRequestNewKeyboard(String keyboardDisplayName) { Keyboard newKeyboard = null; for (Keyboard keyboard : mKeyboardCandidates) { if (keyboard.getDisplayName().equals(keyboardDisplayName)) { newKeyboard = keyboard; } } if (newKeyboard == null) return; this.removeView(mCurrentKeyboard); newKeyboard.setKeyboardListener(this); this.addView(newKeyboard); mCurrentKeyboard = newKeyboard; InputConnection ic = getInputConnection(); mCurrentKeyboard.setInputConnection(ic); }
private boolean isCommitContentSupported(@Nullable EditorInfo editorInfo, @NonNull String mimeType) { if (editorInfo == null) { return false; } final InputConnection ic = getCurrentInputConnection(); if (ic == null) { return false; } if (!validatePackageName(editorInfo)) { return false; } final String[] supportedMimeTypes = EditorInfoCompat.getContentMimeTypes(editorInfo); for (String supportedMimeType : supportedMimeTypes) { if (ClipDescription.compareMimeTypes(mimeType, supportedMimeType)) { return true; } } return false; }
/** * 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(); } } }
@Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection() ; playclick(primaryCode); switch (primaryCode) { case Keyboard.KEYCODE_DELETE: ic.deleteSurroundingText(1, 0); break; case Keyboard.KEYCODE_SHIFT: caps = !caps; keyboard.setShifted(caps); kv.invalidateAllKeys(); break; case Keyboard.KEYCODE_DONE: ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)); break; default: char code = (char) primaryCode; if(Character.isLetter(code) && caps) { code = Character.toUpperCase(code); } ic.commitText(String.valueOf(code), 1); break; } }
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { /* Taken from: https://android.googlesource.com/platform/frameworks/support.git/+/master/design/src/android/support/design/widget/TextInputEditText.java to extend TextInputEditText functionality to AppCompatAutoCompleteTextView. */ final InputConnection ic = super.onCreateInputConnection(outAttrs); if (ic != null && outAttrs.hintText == null) { // If we don't have a hint and our parent is a TextInputLayout, use it's hint for the // EditorInfo. This allows us to display a hint in 'extract mode'. ViewParent parent = getParent(); while (parent instanceof View) { if (parent instanceof TextInputLayout) { outAttrs.hintText = ((TextInputLayout) parent).getHint(); break; } parent = parent.getParent(); } } return ic; }
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { ic = new SDLInputConnection(this, true); outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI | 33554432 /* API 11: EditorInfo.IME_FLAG_NO_FULLSCREEN */; return ic; }
private static boolean requestCursorUpdatesImpl(final InputConnection inputConnection, final int cursorUpdateMode) { if (!isRequestCursorUpdatesAvailable()) { return false; } return sRequestCursorUpdatesMethod.invoke(inputConnection, cursorUpdateMode); }
String readBackwardsUntil(String p, boolean eof) { final InputConnection ic = Ime.getCurrentInputConnection(); if(ic == null) return null; int size = 32; String c = null; while(size < 4096) { c = ic.getTextBeforeCursor(size, 0).toString(); int idx = c.lastIndexOf(p); if(idx >= 0) { return c.substring(idx + 1); } size *= 2; } return eof ? c : null; }
private boolean deleteSurroundingText(int before, int after) { final InputConnection ic = Ime.getCurrentInputConnection(); if(ic != null) { ic.deleteSurroundingText(before, after); return true; } return false; }
/** * 输入法 * @param outAttrs * @return */ @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new mInputConnecttion(super.onCreateInputConnection(outAttrs), false); }
@Override public void onConfigurationChanged(Configuration conf) { // 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); if (ic != null) ic.finishComposingText(); // For voice input mOrientation = conf.orientation; reloadKeyboards(); } mConfigurationChanging = true; super.onConfigurationChanged(conf); mConfigurationChanging = false; }
private void commitTyped(InputConnection inputConnection) { if (mPredicting) { mPredicting = false; if (mComposing.length() > 0) { if (inputConnection != null) { inputConnection.commitText(mComposing, 1); } mCommittedLength = mComposing.length(); TextEntryState.acceptedTyped(mComposing); addToDictionaries(mComposing, AutoDictionary.FREQUENCY_FOR_TYPED); } updateSuggestions(); } }
private int getCursorCapsMode(InputConnection ic, EditorInfo attr) { int caps = 0; EditorInfo ei = getCurrentInputEditorInfo(); if (mAutoCap && ei != null && ei.inputType != EditorInfo.TYPE_NULL) { caps = ic.getCursorCapsMode(attr.inputType); } return caps; }
private void swapPunctuationAndSpace() { final InputConnection ic = getCurrentInputConnection(); if (ic == null) return; CharSequence lastTwo = ic.getTextBeforeCursor(2, 0); if (lastTwo != null && lastTwo.length() == 2 && lastTwo.charAt(0) == KEYCODE_SPACE && isSentenceSeparator(lastTwo.charAt(1))) { ic.beginBatchEdit(); ic.deleteSurroundingText(2, 0); ic.commitText(lastTwo.charAt(1) + " ", 1); ic.endBatchEdit(); updateShiftKeyState(getCurrentInputEditorInfo()); mJustAddedAutoSpace = true; } }
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection inputConnection = super.onCreateInputConnection(outAttrs); if (isIncognitoModeEnabled) { outAttrs.imeOptions = outAttrs.imeOptions | EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING; } return inputConnection; }
private void removeTrailingSpace() { final InputConnection ic = getCurrentInputConnection(); if (ic == null) return; CharSequence lastOne = ic.getTextBeforeCursor(1, 0); if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == KEYCODE_SPACE) { ic.deleteSurroundingText(1, 0); } }
public void onText(CharSequence text) { // Log.v("SpartacusRex","SOFT : onText "+text.toString()); InputConnection ic = getCurrentInputConnection(); if (ic == null) return; ic.beginBatchEdit(); if (mComposing.length() > 0) { commitTyped(ic); } ic.commitText(text, 0); ic.endBatchEdit(); updateShiftKeyState(getCurrentInputEditorInfo()); }
@Override public InputConnection onCreateInputConnection(EditorInfo editorInfo) { InputConnection inputConnection = super.onCreateInputConnection(editorInfo); if(TextSecurePreferences.isEnterSendsEnabled(getContext())) { editorInfo.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; } if (Build.VERSION.SDK_INT < 21) return inputConnection; if (mediaListener == null) return inputConnection; if (inputConnection == null) return null; EditorInfoCompat.setContentMimeTypes(editorInfo, new String[] {"image/jpeg", "image/png", "image/gif"}); return InputConnectionCompat.createWrapper(inputConnection, editorInfo, new CommitContentListener(mediaListener)); }
public void onText(CharSequence text) { InputConnection ic = getCurrentInputConnection(); if (ic == null) return; ic.beginBatchEdit(); if (mComposing.length() > 0) { commitTyped(ic); } ic.commitText(text, 0); ic.endBatchEdit(); updateShiftKeyState(getCurrentInputEditorInfo()); }
private void setOldSuggestions() { if (mCandidateView != null && mCandidateView.isShowingAddToDictionaryHint()) { return; } InputConnection ic = getCurrentInputConnection(); if (ic == null) return; if (!mPredicting) { // Extract the selected or touching text EditingUtil.SelectedWord touching = EditingUtil.getWordAtCursorOrSelection(ic, mLastSelectionStart, mLastSelectionEnd, mWordSeparators); if (touching != null && touching.word.length() > 1) { ic.beginBatchEdit(); if (!applyTypedAlternatives(touching)) { abortCorrection(true); } else { TextEntryState.selectedForCorrection(); EditingUtil.underlineWord(ic, touching); } ic.endBatchEdit(); } else { abortCorrection(true); setNextSuggestions(); // Show the punctuation suggestions list } } else { abortCorrection(true); } }
public boolean showPunctuationHintIfNecessary(InputConnection ic) { if (!mVoiceResultContainedPunctuation && ic != null && getAndIncrementPref(PREF_VOICE_PUNCTUATION_HINT_VIEW_COUNT) < mPunctuationHintMaxDisplays) { CharSequence charBeforeCursor = ic.getTextBeforeCursor(1, 0); if (SPEAKABLE_PUNCTUATION.containsKey(charBeforeCursor)) { showHint(R.layout.voice_punctuation_hint); return true; } } return false; }
private static int getCursorPosition(InputConnection connection) { ExtractedText extracted = connection.getExtractedText( new ExtractedTextRequest(), 0); if (extracted == null) { return -1; } return extracted.startOffset + extracted.selectionStart; }
public void onText(CharSequence text) { InputConnection ic = getCurrentInputConnection(); if (ic == null) return; ic.beginBatchEdit(); if (mComposing.length() > 0) { commitTyped(ic); } ic.commitText(text, 0); ic.endBatchEdit(); }
@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 (mComposing.length() > 0 && (newSelStart != candidatesEnd || newSelEnd != candidatesEnd)) { mComposing.setLength(0); InputConnection ic = getCurrentInputConnection(); if (ic != null) { ic.finishComposingText(); } } }
private static Range getWordRangeAtCursor( InputConnection connection, String sep, Range range) { if (connection == null || sep == null) { return null; } CharSequence before = connection.getTextBeforeCursor(1000, 0); CharSequence after = connection.getTextAfterCursor(1000, 0); if (before == null || after == null) { return null; } // Find first word separator before the cursor int start = before.length(); while (start > 0 && !isWhitespace(before.charAt(start - 1), sep)) start--; // Find last word separator after the cursor int end = -1; while (++end < after.length() && !isWhitespace(after.charAt(end), sep)); int cursor = getCursorPosition(connection); if (start >= 0 && cursor + end <= after.length() + before.length()) { String word = before.toString().substring(start, before.length()) + after.toString().substring(0, end); Range returnRange = range != null? range : new Range(); returnRange.charsBefore = before.length() - start; returnRange.charsAfter = end; returnRange.word = word; return returnRange; } return null; }
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection ic = super.onCreateInputConnection(outAttrs); if (ic != null && outAttrs.hintText == null) { ViewParent parent = getParent(); if (parent instanceof TextInputLayout) { outAttrs.hintText = ((TextInputLayout) parent).getHint(); } } return ic; }
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { outAttrs.initialSelStart = getSelectionStart(); outAttrs.initialSelEnd = getSelectionEnd(); // TODO allow user to set type class outAttrs.inputType = InputType.TYPE_CLASS_TEXT; return new MetInputConnection(this, true); }
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { outAttrs.inputType = InputType.TYPE_CLASS_NUMBER; outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE; outAttrs.initialSelStart = 0; return editCodeInputConnection; }
/** * This translates incoming hard key events in to edit operations on an * InputConnection. It is only needed when using the * PROCESS_HARD_KEYS option. */ private boolean translateKeyDown(int keyCode, KeyEvent event) { mMetaState = MetaKeyKeyListener.handleKeyDown(mMetaState, keyCode, event); int c = event.getUnicodeChar(MetaKeyKeyListener.getMetaState(mMetaState)); mMetaState = MetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState); InputConnection ic = getCurrentInputConnection(); if (c == 0 || ic == null) { return false; } boolean dead = false; if ((c & KeyCharacterMap.COMBINING_ACCENT) != 0) { dead = true; c = c & KeyCharacterMap.COMBINING_ACCENT_MASK; } if (mComposing.length() > 0) { char accent = mComposing.charAt(mComposing.length() -1 ); int composed = KeyEvent.getDeadChar(accent, c); if (composed != 0) { c = composed; mComposing.setLength(mComposing.length()-1); } } onKey(c, null); return true; }
private int getCursorCapsMode(InputConnection ic, EditorInfo attr) { /** A rudimentary method to find out whether we should start with caps on or not. * */ // TODO: Perform additional checks. int caps = 0; EditorInfo ei = getCurrentInputEditorInfo(); if (ei != null && ei.inputType != EditorInfo.TYPE_NULL) { caps = ic.getCursorCapsMode(attr.inputType); } return caps; }