From 1587be697752b12e18d5cadb90e56883cb93c242 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 20 Dec 2013 20:45:59 +0900 Subject: [PATCH] [IL31] Move *BatchInput to InputLogic Bug: 8636060 Change-Id: If0309be038518a71d735108b78ac9efbfe38c82b --- .../android/inputmethod/latin/LatinIME.java | 125 +++--------------- .../latin/inputlogic/InputLogic.java | 110 +++++++++++++++ 2 files changed, 128 insertions(+), 107 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 3a0ea68a6..26585d3d1 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1251,60 +1251,24 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void onStartBatchInput() { - mInputUpdater.onStartBatchInput(); - mHandler.cancelUpdateSuggestionStrip(); - mInputLogic.mConnection.beginBatchEdit(); - final SettingsValues currentSettingsValues = mSettings.getCurrent(); - if (mInputLogic.mWordComposer.isComposingWord()) { - if (currentSettingsValues.mIsInternal) { - if (mInputLogic.mWordComposer.isBatchMode()) { - LatinImeLoggerUtils.onAutoCorrection("", - mInputLogic.mWordComposer.getTypedWord(), " ", - mInputLogic.mWordComposer); - } - } - final int wordComposerSize = mInputLogic.mWordComposer.size(); - // Since isComposingWord() is true, the size is at least 1. - if (mInputLogic.mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { - // If we are in the middle of a recorrection, we need to commit the recorrection - // first so that we can insert the batch input at the current cursor position. - mInputLogic.resetEntireInputState(currentSettingsValues, - mInputLogic.mLastSelectionStart, mInputLogic.mLastSelectionEnd); - } else if (wordComposerSize <= 1) { - // We auto-correct the previous (typed, not gestured) string iff it's one character - // long. The reason for this is, even in the middle of gesture typing, you'll still - // tap one-letter words and you want them auto-corrected (typically, "i" in English - // should become "I"). However for any longer word, we assume that the reason for - // tapping probably is that the word you intend to type is not in the dictionary, - // so we do not attempt to correct, on the assumption that if that was a dictionary - // word, the user would probably have gestured instead. - mInputLogic.commitCurrentAutoCorrection(currentSettingsValues, - LastComposedWord.NOT_A_SEPARATOR, mHandler); - } else { - mInputLogic.commitTyped(currentSettingsValues, LastComposedWord.NOT_A_SEPARATOR); - } - } - final int codePointBeforeCursor = mInputLogic.mConnection.getCodePointBeforeCursor(); - if (Character.isLetterOrDigit(codePointBeforeCursor) - || currentSettingsValues.isUsuallyFollowedBySpace(codePointBeforeCursor)) { - final boolean autoShiftHasBeenOverriden = mKeyboardSwitcher.getKeyboardShiftMode() != - getCurrentAutoCapsState(); - mInputLogic.mSpaceState = SpaceState.PHANTOM; - if (!autoShiftHasBeenOverriden) { - // When we change the space state, we need to update the shift state of the - // keyboard unless it has been overridden manually. This is happening for example - // after typing some letters and a period, then gesturing; the keyboard is not in - // caps mode yet, but since a gesture is starting, it should go in caps mode, - // unless the user explictly said it should not. - mKeyboardSwitcher.updateShiftState(); - } - } - mInputLogic.mConnection.endBatchEdit(); - mInputLogic.mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime( - mInputLogic.getActualCapsMode(currentSettingsValues, mKeyboardSwitcher), - // Prev word is 1st word before cursor - mInputLogic.getNthPreviousWordForSuggestion(currentSettingsValues, - 1 /* nthPreviousWord */)); + mInputLogic.onStartBatchInput(mSettings.getCurrent(), mKeyboardSwitcher, mHandler, + mInputUpdater); + } + + @Override + public void onUpdateBatchInput(final InputPointers batchPointers) { + mInputLogic.onUpdateBatchInput(mSettings.getCurrent(), batchPointers, mKeyboardSwitcher, + mInputUpdater); + } + + @Override + public void onEndBatchInput(final InputPointers batchPointers) { + mInputLogic.onEndBatchInput(mSettings.getCurrent(), batchPointers, mInputUpdater); + } + + @Override + public void onCancelBatchInput() { + mInputLogic.onCancelBatchInput(mInputUpdater); } // TODO[IL]: Make this a package-private standalone class in inputlogic/ and remove all @@ -1450,49 +1414,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } - /* The sequence number member is only used in onUpdateBatchInput. It is increased each time - * auto-commit happens. The reason we need this is, when auto-commit happens we trim the - * input pointers that are held in a singleton, and to know how much to trim we rely on the - * results of the suggestion process that is held in mSuggestedWords. - * However, the suggestion process is asynchronous, and sometimes we may enter the - * onUpdateBatchInput method twice without having recomputed suggestions yet, or having - * received new suggestions generated from not-yet-trimmed input pointers. In this case, the - * mIndexOfTouchPointOfSecondWords member will be out of date, and we must not use it lest we - * remove an unrelated number of pointers (possibly even more than are left in the input - * pointers, leading to a crash). - * To avoid that, we increase the sequence number each time we auto-commit and trim the - * input pointers, and we do not use any suggested words that have been generated with an - * earlier sequence number. - */ - private int mAutoCommitSequenceNumber = 1; - @Override - public void onUpdateBatchInput(final InputPointers batchPointers) { - final SettingsValues settingsValues = mSettings.getCurrent(); - if (settingsValues.mPhraseGestureEnabled) { - final SuggestedWordInfo candidate = - mInputLogic.mSuggestedWords.getAutoCommitCandidate(); - // If these suggested words have been generated with out of date input pointers, then - // we skip auto-commit (see comments above on the mSequenceNumber member). - if (null != candidate - && mInputLogic.mSuggestedWords.mSequenceNumber >= mAutoCommitSequenceNumber) { - if (candidate.mSourceDict.shouldAutoCommit(candidate)) { - final String[] commitParts = candidate.mWord.split(" ", 2); - batchPointers.shift(candidate.mIndexOfTouchPointOfSecondWord); - mInputLogic.promotePhantomSpace(mSettings.getCurrent()); - mInputLogic.mConnection.commitText(commitParts[0], 0); - mInputLogic.mSpaceState = SpaceState.PHANTOM; - mKeyboardSwitcher.updateShiftState(); - mInputLogic.mWordComposer. - setCapitalizedModeAndPreviousWordAtStartComposingTime( - mInputLogic.getActualCapsMode(settingsValues, mKeyboardSwitcher), - commitParts[0]); - ++mAutoCommitSequenceNumber; - } - } - } - mInputUpdater.onUpdateBatchInput(batchPointers, mAutoCommitSequenceNumber); - } - // This method must run in UI Thread. public void onEndBatchInputAsyncInternal(final SuggestedWords suggestedWords) { final String batchInputText = suggestedWords.isEmpty() ? null : suggestedWords.getWord(0); @@ -1527,11 +1448,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mKeyboardSwitcher.updateShiftState(); } - @Override - public void onEndBatchInput(final InputPointers batchPointers) { - mInputUpdater.onEndBatchInput(batchPointers); - } - // Called from PointerTracker through the KeyboardActionListener interface @Override public void onFinishSlidingInput() { @@ -1546,11 +1462,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Nothing to do so far. } - @Override - public void onCancelBatchInput() { - mInputUpdater.onCancelBatchInput(); - } - // TODO[IL]: Move this to InputLogic and make it private // Outside LatinIME, only used by the 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 8f7bb95fb..96852f0bc 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -32,6 +32,7 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.LastComposedWord; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; @@ -296,6 +297,115 @@ public final class InputLogic { mConnection.endBatchEdit(); } + public void onStartBatchInput(final SettingsValues settingsValues, + // TODO: remove these arguments + final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler, + final LatinIME.InputUpdater inputUpdater) { + inputUpdater.onStartBatchInput(); + handler.cancelUpdateSuggestionStrip(); + mConnection.beginBatchEdit(); + if (mWordComposer.isComposingWord()) { + if (settingsValues.mIsInternal) { + if (mWordComposer.isBatchMode()) { + LatinImeLoggerUtils.onAutoCorrection("", mWordComposer.getTypedWord(), " ", + mWordComposer); + } + } + final int wordComposerSize = mWordComposer.size(); + // Since isComposingWord() is true, the size is at least 1. + if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { + // If we are in the middle of a recorrection, we need to commit the recorrection + // first so that we can insert the batch input at the current cursor position. + resetEntireInputState(settingsValues, mLastSelectionStart, mLastSelectionEnd); + } else if (wordComposerSize <= 1) { + // We auto-correct the previous (typed, not gestured) string iff it's one character + // long. The reason for this is, even in the middle of gesture typing, you'll still + // tap one-letter words and you want them auto-corrected (typically, "i" in English + // should become "I"). However for any longer word, we assume that the reason for + // tapping probably is that the word you intend to type is not in the dictionary, + // so we do not attempt to correct, on the assumption that if that was a dictionary + // word, the user would probably have gestured instead. + commitCurrentAutoCorrection(settingsValues, LastComposedWord.NOT_A_SEPARATOR, + handler); + } else { + commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR); + } + } + final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); + if (Character.isLetterOrDigit(codePointBeforeCursor) + || settingsValues.isUsuallyFollowedBySpace(codePointBeforeCursor)) { + final boolean autoShiftHasBeenOverriden = keyboardSwitcher.getKeyboardShiftMode() != + getCurrentAutoCapsState(settingsValues); + mSpaceState = SpaceState.PHANTOM; + if (!autoShiftHasBeenOverriden) { + // When we change the space state, we need to update the shift state of the + // keyboard unless it has been overridden manually. This is happening for example + // after typing some letters and a period, then gesturing; the keyboard is not in + // caps mode yet, but since a gesture is starting, it should go in caps mode, + // unless the user explictly said it should not. + keyboardSwitcher.updateShiftState(); + } + } + mConnection.endBatchEdit(); + mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime( + getActualCapsMode(settingsValues, keyboardSwitcher), + // Prev word is 1st word before cursor + getNthPreviousWordForSuggestion(settingsValues, 1 /* nthPreviousWord */)); + } + + /* The sequence number member is only used in onUpdateBatchInput. It is increased each time + * auto-commit happens. The reason we need this is, when auto-commit happens we trim the + * input pointers that are held in a singleton, and to know how much to trim we rely on the + * results of the suggestion process that is held in mSuggestedWords. + * However, the suggestion process is asynchronous, and sometimes we may enter the + * onUpdateBatchInput method twice without having recomputed suggestions yet, or having + * received new suggestions generated from not-yet-trimmed input pointers. In this case, the + * mIndexOfTouchPointOfSecondWords member will be out of date, and we must not use it lest we + * remove an unrelated number of pointers (possibly even more than are left in the input + * pointers, leading to a crash). + * To avoid that, we increase the sequence number each time we auto-commit and trim the + * input pointers, and we do not use any suggested words that have been generated with an + * earlier sequence number. + */ + private int mAutoCommitSequenceNumber = 1; + public void onUpdateBatchInput(final SettingsValues settingsValues, + final InputPointers batchPointers, + // TODO: remove these arguments + final KeyboardSwitcher keyboardSwitcher, final LatinIME.InputUpdater inputUpdater) { + if (settingsValues.mPhraseGestureEnabled) { + final SuggestedWordInfo candidate = mSuggestedWords.getAutoCommitCandidate(); + // If these suggested words have been generated with out of date input pointers, then + // we skip auto-commit (see comments above on the mSequenceNumber member). + if (null != candidate + && mSuggestedWords.mSequenceNumber >= mAutoCommitSequenceNumber) { + if (candidate.mSourceDict.shouldAutoCommit(candidate)) { + final String[] commitParts = candidate.mWord.split(" ", 2); + batchPointers.shift(candidate.mIndexOfTouchPointOfSecondWord); + promotePhantomSpace(settingsValues); + mConnection.commitText(commitParts[0], 0); + mSpaceState = SpaceState.PHANTOM; + keyboardSwitcher.updateShiftState(); + mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime( + getActualCapsMode(settingsValues, keyboardSwitcher), commitParts[0]); + ++mAutoCommitSequenceNumber; + } + } + } + inputUpdater.onUpdateBatchInput(batchPointers, mAutoCommitSequenceNumber); + } + + public void onEndBatchInput(final SettingsValues settingValues, + final InputPointers batchPointers, + // TODO: remove these arguments + final LatinIME.InputUpdater inputUpdater) { + inputUpdater.onEndBatchInput(batchPointers); + } + + // TODO: remove this argument + public void onCancelBatchInput(final LatinIME.InputUpdater inputUpdater) { + inputUpdater.onCancelBatchInput(); + } + /** * Handle inputting a code point to the editor. *