From 060e292628aa8ebbdc9dc951393336f4bb39bc69 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 19 Dec 2013 18:30:21 +0900 Subject: [PATCH] [IL10] Move revertCommit to InputLogic Bug: 8636060 Change-Id: Ia5788b365f05ae880bad957cbef00ecc93abb5d1 --- .../android/inputmethod/latin/LatinIME.java | 62 +---------------- .../latin/inputlogic/InputLogic.java | 66 ++++++++++++++++++- 2 files changed, 67 insertions(+), 61 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index b837f3f71..8d628a757 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -117,7 +117,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen Suggest.SuggestInitializationListener { private static final String TAG = LatinIME.class.getSimpleName(); private static final boolean TRACE = false; - private static boolean DEBUG; + // TODO[IL]: Make this private + public static boolean DEBUG; private static final int EXTENDED_TOUCHABLE_REGION_HEIGHT = 100; @@ -2280,65 +2281,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (tryResumeSuggestions) mHandler.postResumeSuggestions(); } - // TODO[IL]: Move this to InputLogic and make it private again. - public void revertCommit() { - final String previousWord = mInputLogic.mLastComposedWord.mPrevWord; - final String originallyTypedWord = mInputLogic.mLastComposedWord.mTypedWord; - final String committedWord = mInputLogic.mLastComposedWord.mCommittedWord; - final int cancelLength = committedWord.length(); - // We want java chars, not codepoints for the following. - final int separatorLength = mInputLogic.mLastComposedWord.mSeparatorString.length(); - // TODO: should we check our saved separator against the actual contents of the text view? - final int deleteLength = cancelLength + separatorLength; - if (DEBUG) { - if (mInputLogic.mWordComposer.isComposingWord()) { - throw new RuntimeException("revertCommit, but we are composing a word"); - } - final CharSequence wordBeforeCursor = - mInputLogic.mConnection.getTextBeforeCursor(deleteLength, 0).subSequence(0, - cancelLength); - if (!TextUtils.equals(committedWord, wordBeforeCursor)) { - throw new RuntimeException("revertCommit check failed: we thought we were " - + "reverting \"" + committedWord - + "\", but before the cursor we found \"" + wordBeforeCursor + "\""); - } - } - mInputLogic.mConnection.deleteSurroundingText(deleteLength, 0); - if (!TextUtils.isEmpty(previousWord) && !TextUtils.isEmpty(committedWord)) { - if (mInputLogic.mSuggest != null) { - mInputLogic.mSuggest.cancelAddingUserHistory(previousWord, committedWord); - } - } - final String stringToCommit = - originallyTypedWord + mInputLogic.mLastComposedWord.mSeparatorString; - if (mSettings.getCurrent().mCurrentLanguageHasSpaces) { - // For languages with spaces, we revert to the typed string, but the cursor is still - // after the separator so we don't resume suggestions. If the user wants to correct - // the word, they have to press backspace again. - mInputLogic.mConnection.commitText(stringToCommit, 1); - } else { - // For languages without spaces, we revert the typed string but the cursor is flush - // with the typed word, so we need to resume suggestions right away. - mInputLogic.mWordComposer.setComposingWord(stringToCommit, previousWord, - mKeyboardSwitcher.getKeyboard()); - mInputLogic.mConnection.setComposingText(stringToCommit, 1); - } - if (mSettings.isInternal()) { - LatinImeLoggerUtils.onSeparator(mInputLogic.mLastComposedWord.mSeparatorString, - Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); - } - if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { - ResearchLogger.latinIME_revertCommit(committedWord, originallyTypedWord, - mInputLogic.mWordComposer.isBatchMode(), - mInputLogic.mLastComposedWord.mSeparatorString); - } - // Don't restart suggestion yet. We'll restart if the user deletes the - // separator. - mInputLogic.mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; - // We have a separator between the word and the cursor: we should show predictions. - mHandler.postUpdateSuggestionStrip(); - } - // TODO: Make this private // Outside LatinIME, only used by the {@link InputTestsBase} test suite. @UsedForTesting diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index 78f8885bc..15643819d 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -511,7 +511,7 @@ public final class InputLogic { if (settingsValues.mIsInternal) { LatinImeLoggerUtils.onAutoCorrectionCancellation(); } - mLatinIME.revertCommit(); + revertCommit(settingsValues, keyboardSwitcher, handler); return; } if (mEnteredText != null && mConnection.sameAsTextBeforeCursor(mEnteredText)) { @@ -756,6 +756,70 @@ public final class InputLogic { keyboardSwitcher.updateShiftState(); } + /** + * Reverts a previous commit with auto-correction. + * + * This is triggered upon pressing backspace just after a commit with auto-correction. + * + * @param settingsValues the current settings values. + */ + private void revertCommit(final SettingsValues settingsValues, + // TODO: remove these arguments + final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) { + final String previousWord = mLastComposedWord.mPrevWord; + final String originallyTypedWord = mLastComposedWord.mTypedWord; + final String committedWord = mLastComposedWord.mCommittedWord; + final int cancelLength = committedWord.length(); + // We want java chars, not codepoints for the following. + final int separatorLength = mLastComposedWord.mSeparatorString.length(); + // TODO: should we check our saved separator against the actual contents of the text view? + final int deleteLength = cancelLength + separatorLength; + if (LatinIME.DEBUG) { + if (mWordComposer.isComposingWord()) { + throw new RuntimeException("revertCommit, but we are composing a word"); + } + final CharSequence wordBeforeCursor = + mConnection.getTextBeforeCursor(deleteLength, 0).subSequence(0, cancelLength); + if (!TextUtils.equals(committedWord, wordBeforeCursor)) { + throw new RuntimeException("revertCommit check failed: we thought we were " + + "reverting \"" + committedWord + + "\", but before the cursor we found \"" + wordBeforeCursor + "\""); + } + } + mConnection.deleteSurroundingText(deleteLength, 0); + if (!TextUtils.isEmpty(previousWord) && !TextUtils.isEmpty(committedWord)) { + if (mSuggest != null) { + mSuggest.cancelAddingUserHistory(previousWord, committedWord); + } + } + final String stringToCommit = originallyTypedWord + mLastComposedWord.mSeparatorString; + if (settingsValues.mCurrentLanguageHasSpaces) { + // For languages with spaces, we revert to the typed string, but the cursor is still + // after the separator so we don't resume suggestions. If the user wants to correct + // the word, they have to press backspace again. + mConnection.commitText(stringToCommit, 1); + } else { + // For languages without spaces, we revert the typed string but the cursor is flush + // with the typed word, so we need to resume suggestions right away. + mWordComposer.setComposingWord(stringToCommit, previousWord, + keyboardSwitcher.getKeyboard()); + mConnection.setComposingText(stringToCommit, 1); + } + if (settingsValues.mIsInternal) { + LatinImeLoggerUtils.onSeparator(mLastComposedWord.mSeparatorString, + Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); + } + if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { + ResearchLogger.latinIME_revertCommit(committedWord, originallyTypedWord, + mWordComposer.isBatchMode(), mLastComposedWord.mSeparatorString); + } + // Don't restart suggestion yet. We'll restart if the user deletes the + // separator. + mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; + // We have a separator between the word and the cursor: we should show predictions. + handler.postUpdateSuggestionStrip(); + } + /** * Factor in auto-caps and manual caps and compute the current caps mode. * @param keyboardSwitcher the keyboard switcher. Caps mode depends on its mode.