aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/inputlogic
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/inputlogic')
-rw-r--r--java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java203
-rw-r--r--java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java125
2 files changed, 243 insertions, 85 deletions
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 59b722134..b365003a5 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -88,12 +88,12 @@ public final class InputLogic {
public int mLastSelectionStart = Constants.NOT_A_CURSOR_POSITION;
public int mLastSelectionEnd = Constants.NOT_A_CURSOR_POSITION;
- public int mDeleteCount;
+ private int mDeleteCount;
private long mLastKeyTime;
public final TreeSet<Long> mCurrentlyPressedHardwareKeys = CollectionUtils.newTreeSet();
// Keeps track of most recently inserted text (multi-character key) for reverting
- public String mEnteredText;
+ private String mEnteredText;
// TODO: This boolean is persistent state and causes large side effects at unexpected times.
// Find a way to remove it for readability.
@@ -116,16 +116,33 @@ public final class InputLogic {
* some things must not be done (for example, the keyboard should not be reset to the
* alphabetic layout), so do not send false to this just in case.
*
- * @param restarting whether input is starting in the same field as before.
+ * @param restarting whether input is starting in the same field as before. Unused for now.
+ * @param editorInfo the editorInfo associated with the editor.
*/
- public void startInput(final boolean restarting) {
- mInputLogicHandler = new InputLogicHandler();
+ public void startInput(final boolean restarting, final EditorInfo editorInfo) {
+ mEnteredText = null;
+ resetComposingState(true /* alsoResetLastComposedWord */);
+ mDeleteCount = 0;
+ mSpaceState = SpaceState.NONE;
+ mRecapitalizeStatus.deactivate();
+ mCurrentlyPressedHardwareKeys.clear();
+ mSuggestedWords = SuggestedWords.EMPTY;
+ mLastSelectionStart = editorInfo.initialSelStart;
+ mLastSelectionEnd = editorInfo.initialSelEnd;
+ // In some cases (namely, after rotation of the device) editorInfo.initialSelStart is lying
+ // so we try using some heuristics to find out about these and fix them.
+ tryFixLyingCursorPosition();
+ mInputLogicHandler = new InputLogicHandler(mLatinIME, this);
}
/**
* Clean up the input logic after input is finished.
*/
public void finishInput() {
+ if (mWordComposer.isComposingWord()) {
+ mConnection.finishComposingText();
+ }
+ resetComposingState(true /* alsoResetLastComposedWord */);
mInputLogicHandler.destroy();
mInputLogicHandler = null;
}
@@ -140,11 +157,11 @@ public final class InputLogic {
* @param rawText the text to input.
*/
public void onTextInput(final SettingsValues settingsValues, final String rawText,
- // TODO: remove these arguments
- final LatinIME.UIHandler handler, final LatinIME.InputUpdater inputUpdater) {
+ // TODO: remove this argument
+ final LatinIME.UIHandler handler) {
mConnection.beginBatchEdit();
if (mWordComposer.isComposingWord()) {
- commitCurrentAutoCorrection(settingsValues, rawText, handler, inputUpdater);
+ commitCurrentAutoCorrection(settingsValues, rawText, handler);
} else {
resetComposingState(true /* alsoResetLastComposedWord */);
}
@@ -180,8 +197,8 @@ public final class InputLogic {
* @param y the y-coordinate where the user pressed the key, or NOT_A_COORDINATE.
*/
public void onCodeInput(final int code, final int x, final int y,
- // TODO: remove these four arguments
- final LatinIME.UIHandler handler, final LatinIME.InputUpdater inputUpdater,
+ // TODO: remove these three arguments
+ final LatinIME.UIHandler handler,
final KeyboardSwitcher keyboardSwitcher, final SubtypeSwitcher subtypeSwitcher) {
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.latinIME_onCodeInput(code, x, y);
@@ -274,16 +291,16 @@ public final class InputLogic {
// No action label, and the action from imeOptions is NONE: this is a regular
// enter key that should input a carriage return.
didAutoCorrect = handleNonSpecialCharacter(settingsValues, Constants.CODE_ENTER,
- x, y, spaceState, keyboardSwitcher, handler, inputUpdater);
+ x, y, spaceState, keyboardSwitcher, handler);
}
break;
case Constants.CODE_SHIFT_ENTER:
didAutoCorrect = handleNonSpecialCharacter(settingsValues, Constants.CODE_ENTER,
- x, y, spaceState, keyboardSwitcher, handler, inputUpdater);
+ x, y, spaceState, keyboardSwitcher, handler);
break;
default:
didAutoCorrect = handleNonSpecialCharacter(settingsValues,
- code, x, y, spaceState, keyboardSwitcher, handler, inputUpdater);
+ code, x, y, spaceState, keyboardSwitcher, handler);
break;
}
keyboardSwitcher.onCodeInput(code);
@@ -300,9 +317,8 @@ public final class InputLogic {
public void onStartBatchInput(final SettingsValues settingsValues,
// TODO: remove these arguments
- final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler,
- final LatinIME.InputUpdater inputUpdater) {
- inputUpdater.onStartBatchInput();
+ final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) {
+ mInputLogicHandler.onStartBatchInput();
handler.showGesturePreviewAndSuggestionStrip(
SuggestedWords.EMPTY, false /* dismissGestureFloatingPreviewText */);
handler.cancelUpdateSuggestionStrip();
@@ -329,7 +345,7 @@ public final class InputLogic {
// 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, inputUpdater);
+ handler);
} else {
commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR);
}
@@ -374,7 +390,7 @@ public final class InputLogic {
public void onUpdateBatchInput(final SettingsValues settingsValues,
final InputPointers batchPointers,
// TODO: remove these arguments
- final KeyboardSwitcher keyboardSwitcher, final LatinIME.InputUpdater inputUpdater) {
+ final KeyboardSwitcher keyboardSwitcher) {
if (settingsValues.mPhraseGestureEnabled) {
final SuggestedWordInfo candidate = mSuggestedWords.getAutoCommitCandidate();
// If these suggested words have been generated with out of date input pointers, then
@@ -395,20 +411,17 @@ public final class InputLogic {
}
}
}
- inputUpdater.onUpdateBatchInput(batchPointers, mAutoCommitSequenceNumber);
+ mInputLogicHandler.onUpdateBatchInput(batchPointers, mAutoCommitSequenceNumber);
}
public void onEndBatchInput(final SettingsValues settingValues,
- final InputPointers batchPointers,
- // TODO: remove these arguments
- final LatinIME.InputUpdater inputUpdater) {
- inputUpdater.onEndBatchInput(batchPointers);
+ final InputPointers batchPointers) {
+ mInputLogicHandler.onEndBatchInput(batchPointers, mAutoCommitSequenceNumber);
}
- // TODO: remove these arguments
- public void onCancelBatchInput(final LatinIME.UIHandler handler,
- final LatinIME.InputUpdater inputUpdater) {
- inputUpdater.onCancelBatchInput();
+ // TODO: remove this argument
+ public void onCancelBatchInput(final LatinIME.UIHandler handler) {
+ mInputLogicHandler.onCancelBatchInput();
handler.showGesturePreviewAndSuggestionStrip(
SuggestedWords.EMPTY, true /* dismissGestureFloatingPreviewText */);
}
@@ -431,14 +444,13 @@ public final class InputLogic {
private boolean handleNonSpecialCharacter(final SettingsValues settingsValues,
final int codePoint, final int x, final int y, final int spaceState,
// TODO: remove these arguments
- final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler,
- final LatinIME.InputUpdater inputUpdater) {
+ final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) {
mSpaceState = SpaceState.NONE;
final boolean didAutoCorrect;
if (settingsValues.isWordSeparator(codePoint)
|| Character.getType(codePoint) == Character.OTHER_SYMBOL) {
didAutoCorrect = handleSeparator(settingsValues, codePoint, x, y, spaceState,
- keyboardSwitcher, handler, inputUpdater);
+ keyboardSwitcher, handler);
} else {
didAutoCorrect = false;
if (SpaceState.PHANTOM == spaceState) {
@@ -578,8 +590,7 @@ public final class InputLogic {
private boolean handleSeparator(final SettingsValues settingsValues,
final int codePoint, final int x, final int y, final int spaceState,
// TODO: remove these arguments
- final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler,
- final LatinIME.InputUpdater inputUpdater) {
+ final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) {
boolean didAutoCorrect = false;
// We avoid sending spaces in languages without spaces if we were composing.
final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint
@@ -595,7 +606,7 @@ public final class InputLogic {
if (settingsValues.mCorrectionEnabled) {
final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR
: StringUtils.newSingleCodePointString(codePoint);
- commitCurrentAutoCorrection(settingsValues, separator, handler, inputUpdater);
+ commitCurrentAutoCorrection(settingsValues, separator, handler);
didAutoCorrect = true;
} else {
commitTyped(settingsValues, StringUtils.newSingleCodePointString(codePoint));
@@ -669,6 +680,7 @@ public final class InputLogic {
// TODO: remove these arguments
final LatinIME.UIHandler handler, final KeyboardSwitcher keyboardSwitcher) {
mSpaceState = SpaceState.NONE;
+ final int deleteCountAtStart = mDeleteCount;
mDeleteCount++;
// In many cases, we may have to put the keyboard in auto-shift state again. However
@@ -800,12 +812,13 @@ public final class InputLogic {
}
}
}
- if (settingsValues.isSuggestionsRequested()
+ if (settingsValues.isSuggestionStripVisible()
&& settingsValues.mCurrentLanguageHasSpaces) {
- restartSuggestionsOnWordBeforeCursorIfAtEndOfWord(settingsValues, keyboardSwitcher,
- handler);
+ restartSuggestionsOnWordTouchedByCursor(settingsValues,
+ deleteCountAtStart - mDeleteCount /* offset */,
+ true /* includeResumedWordInSuggestions */, keyboardSwitcher);
}
- // We just removed a character. We need to update the auto-caps state.
+ // We just removed at least one character. We need to update the auto-caps state.
keyboardSwitcher.updateShiftState();
}
}
@@ -991,8 +1004,8 @@ public final class InputLogic {
}
public void performUpdateSuggestionStripSync(final SettingsValues settingsValues,
- // TODO: Remove this variable
- final LatinIME.UIHandler handler, final LatinIME.InputUpdater inputUpdater) {
+ // TODO: Remove this argument
+ final LatinIME.UIHandler handler) {
handler.cancelUpdateSuggestionStrip();
// Check if we have a suggestion engine attached.
@@ -1010,7 +1023,7 @@ public final class InputLogic {
}
final AsyncResultHolder<SuggestedWords> holder = new AsyncResultHolder<SuggestedWords>();
- inputUpdater.getSuggestedWords(Suggest.SESSION_TYPING,
+ mInputLogicHandler.getSuggestedWords(Suggest.SESSION_TYPING,
SuggestedWords.NOT_A_SEQUENCE_NUMBER, new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
@@ -1031,45 +1044,19 @@ public final class InputLogic {
}
/**
- * Check if the cursor is actually at the end of a word. If so, restart suggestions on this
- * word, otherwise do nothing.
- * @param settingsValues the current values of the settings.
- */
- private void restartSuggestionsOnWordBeforeCursorIfAtEndOfWord(
- final SettingsValues settingsValues,
- // TODO: remove these two arguments
- final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) {
- final CharSequence word = mConnection.getWordBeforeCursorIfAtEndOfWord(settingsValues);
- if (null != word) {
- final String wordString = word.toString();
- mWordComposer.setComposingWord(word,
- // Previous word is the 2nd word before cursor because we are restarting on the
- // 1st word before cursor.
- getNthPreviousWordForSuggestion(settingsValues, 2 /* nthPreviousWord */),
- keyboardSwitcher.getKeyboard());
- final int length = word.length();
- mConnection.deleteSurroundingText(length, 0);
- mConnection.setComposingText(word, 1);
- handler.postUpdateSuggestionStrip();
- // TODO: Handle the case where the user manually moves the cursor and then backs up over
- // a separator. In that case, the current log unit should not be uncommitted.
- if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
- ResearchLogger.getInstance().uncommitCurrentLogUnit(wordString,
- true /* dumpCurrentLogUnit */);
- }
- }
- }
-
- /**
* Check if the cursor is touching a word. If so, restart suggestions on this word, else
* do nothing.
*
* @param settingsValues the current values of the settings.
+ * @param offset how much the cursor is expected to have moved since the last updateSelection.
+ * @param includeResumedWordInSuggestions whether to include the word on which we resume
+ * suggestions in the suggestion list.
*/
// TODO: make this private.
public void restartSuggestionsOnWordTouchedByCursor(final SettingsValues settingsValues,
- // TODO: Remove these argument.
- final KeyboardSwitcher keyboardSwitcher, final LatinIME.InputUpdater inputUpdater) {
+ final int offset, final boolean includeResumedWordInSuggestions,
+ // TODO: Remove this argument.
+ final KeyboardSwitcher keyboardSwitcher) {
// HACK: We may want to special-case some apps that exhibit bad behavior in case of
// recorrection. This is a temporary, stopgap measure that will be removed later.
// TODO: remove this.
@@ -1083,6 +1070,7 @@ public final class InputLogic {
if (mLastSelectionStart != mLastSelectionEnd) return;
// If we don't know the cursor location, return.
if (mLastSelectionStart < 0) return;
+ final int expectedCursorPosition = mLastSelectionStart + offset; // We know Start == End
if (!mConnection.isCursorTouchingWord(settingsValues)) return;
final TextRange range = mConnection.getWordRangeAtCursor(
settingsValues.mWordSeparators, 0 /* additionalPrecedingWordsCount */);
@@ -1091,9 +1079,16 @@ public final class InputLogic {
// If for some strange reason (editor bug or so) we measure the text before the cursor as
// longer than what the entire text is supposed to be, the safe thing to do is bail out.
final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor();
- if (numberOfCharsInWordBeforeCursor > mLastSelectionStart) return;
+ if (numberOfCharsInWordBeforeCursor > expectedCursorPosition) return;
final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
final String typedWord = range.mWord.toString();
+ if (includeResumedWordInSuggestions) {
+ suggestions.add(new SuggestedWordInfo(typedWord,
+ SuggestionStripView.MAX_SUGGESTIONS + 1,
+ SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED,
+ SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
+ SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
+ }
if (!isResumableWord(settingsValues, typedWord)) return;
int i = 0;
for (final SuggestionSpan span : range.getSuggestionSpansAtWord()) {
@@ -1118,18 +1113,19 @@ public final class InputLogic {
keyboardSwitcher.getKeyboard());
mWordComposer.setCursorPositionWithinWord(
typedWord.codePointCount(0, numberOfCharsInWordBeforeCursor));
- mConnection.setComposingRegion(mLastSelectionStart - numberOfCharsInWordBeforeCursor,
- mLastSelectionEnd + range.getNumberOfCharsInWordAfterCursor());
+ mConnection.setComposingRegion(expectedCursorPosition - numberOfCharsInWordBeforeCursor,
+ expectedCursorPosition + range.getNumberOfCharsInWordAfterCursor());
if (suggestions.isEmpty()) {
// We come here if there weren't any suggestion spans on this word. We will try to
// compute suggestions for it instead.
- inputUpdater.getSuggestedWords(Suggest.SESSION_TYPING,
+ mInputLogicHandler.getSuggestedWords(Suggest.SESSION_TYPING,
SuggestedWords.NOT_A_SEQUENCE_NUMBER, new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(
final SuggestedWords suggestedWordsIncludingTypedWord) {
final SuggestedWords suggestedWords;
- if (suggestedWordsIncludingTypedWord.size() > 1) {
+ if (suggestedWordsIncludingTypedWord.size() > 1
+ && !includeResumedWordInSuggestions) {
// We were able to compute new suggestions for this word.
// Remove the typed word, since we don't want to display it in this
// case. The #getSuggestedWordsExcludingTypedWord() method sets
@@ -1509,6 +1505,47 @@ public final class InputLogic {
}
/**
+ * Do the final processing after a batch input has ended. This commits the word to the editor.
+ * @param settingsValues the current values of the settings.
+ * @param suggestedWords suggestedWords to use.
+ */
+ public void endBatchInputAsyncInternal(final SettingsValues settingsValues,
+ final SuggestedWords suggestedWords,
+ // TODO: remove this argument
+ final KeyboardSwitcher keyboardSwitcher) {
+ final String batchInputText = suggestedWords.isEmpty() ? null : suggestedWords.getWord(0);
+ if (TextUtils.isEmpty(batchInputText)) {
+ return;
+ }
+ mConnection.beginBatchEdit();
+ if (SpaceState.PHANTOM == mSpaceState) {
+ promotePhantomSpace(settingsValues);
+ }
+ if (settingsValues.mPhraseGestureEnabled) {
+ // Find the last space
+ final int indexOfLastSpace = batchInputText.lastIndexOf(Constants.CODE_SPACE) + 1;
+ if (0 != indexOfLastSpace) {
+ mConnection.commitText(batchInputText.substring(0, indexOfLastSpace), 1);
+ mLatinIME.showSuggestionStrip(
+ suggestedWords.getSuggestedWordsForLastWordOfPhraseGesture());
+ }
+ final String lastWord = batchInputText.substring(indexOfLastSpace);
+ mWordComposer.setBatchInputWord(lastWord);
+ mConnection.setComposingText(lastWord, 1);
+ } else {
+ mWordComposer.setBatchInputWord(batchInputText);
+ mConnection.setComposingText(batchInputText, 1);
+ }
+ mConnection.endBatchEdit();
+ if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
+ ResearchLogger.latinIME_onEndBatchInput(batchInputText, 0, suggestedWords);
+ }
+ // Space state must be updated before calling updateShiftState
+ mSpaceState = SpaceState.PHANTOM;
+ keyboardSwitcher.updateShiftState();
+ }
+
+ /**
* Commit the typed string to the editor.
*
* This is typically called when we should commit the currently composing word without applying
@@ -1552,14 +1589,13 @@ public final class InputLogic {
* @param settingsValues the current value of the settings.
* @param separator the separator that's causing the commit to happen.
*/
- // TODO: Make this private
- public void commitCurrentAutoCorrection(final SettingsValues settingsValues,
+ private void commitCurrentAutoCorrection(final SettingsValues settingsValues,
final String separator,
- // TODO: Remove these arguments.
- final LatinIME.UIHandler handler, final LatinIME.InputUpdater inputUpdater) {
+ // TODO: Remove this argument.
+ final LatinIME.UIHandler handler) {
// Complete any pending suggestions query first
if (handler.hasPendingUpdateSuggestions()) {
- performUpdateSuggestionStripSync(settingsValues, handler, inputUpdater);
+ performUpdateSuggestionStripSync(settingsValues, handler);
}
final String typedAutoCorrection = mWordComposer.getAutoCorrectionOrNull();
final String typedWord = mWordComposer.getTypedWord();
@@ -1645,8 +1681,7 @@ public final class InputLogic {
* detect the most damaging cases: when the cursor position is declared to be much smaller
* than it really is.
*/
- // TODO: make this private
- public void tryFixLyingCursorPosition() {
+ private void tryFixLyingCursorPosition() {
final CharSequence textBeforeCursor = mConnection.getTextBeforeCursor(
Constants.EDITOR_CONTENTS_CACHE_SIZE, 0);
if (null == textBeforeCursor) {
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java
index d611e4bf8..3258dcdfb 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java
@@ -20,18 +20,33 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
+import com.android.inputmethod.latin.InputPointers;
+import com.android.inputmethod.latin.LatinIME;
+import com.android.inputmethod.latin.Suggest;
+import com.android.inputmethod.latin.SuggestedWords;
+import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback;
+
/**
* A helper to manage deferred tasks for the input logic.
*/
// TODO: Make this package private
public class InputLogicHandler implements Handler.Callback {
final Handler mNonUIThreadHandler;
+ // TODO: remove this reference.
+ final LatinIME mLatinIME;
+ final InputLogic mInputLogic;
+ private final Object mLock = new Object();
+ private boolean mInBatchInput; // synchronized using {@link #mLock}.
+
+ private static final int MSG_GET_SUGGESTED_WORDS = 1;
- public InputLogicHandler() {
+ public InputLogicHandler(final LatinIME latinIME, final InputLogic inputLogic) {
final HandlerThread handlerThread = new HandlerThread(
InputLogicHandler.class.getSimpleName());
handlerThread.start();
mNonUIThreadHandler = new Handler(handlerThread.getLooper(), this);
+ mLatinIME = latinIME;
+ mInputLogic = inputLogic;
}
public void destroy() {
@@ -42,8 +57,116 @@ public class InputLogicHandler implements Handler.Callback {
* Handle a message.
* @see android.os.Handler.Callback#handleMessage(android.os.Message)
*/
+ // Called on the Non-UI handler thread by the Handler code.
@Override
public boolean handleMessage(final Message msg) {
+ switch (msg.what) {
+ case MSG_GET_SUGGESTED_WORDS:
+ mLatinIME.getSuggestedWords(msg.arg1 /* sessionId */,
+ msg.arg2 /* sequenceNumber */, (OnGetSuggestedWordsCallback) msg.obj);
+ break;
+ }
return true;
}
+
+ // Called on the UI thread by InputLogic.
+ public void onStartBatchInput() {
+ synchronized (mLock) {
+ mInBatchInput = true;
+ }
+ }
+
+ /**
+ * Fetch suggestions corresponding to an update of a batch input.
+ * @param batchPointers the updated pointers, including the part that was passed last time.
+ * @param sequenceNumber the sequence number associated with this batch input.
+ * @param forEnd true if this is the end of a batch input, false if it's an update.
+ */
+ // This method can be called from any thread and will see to it that the correct threads
+ // are used for parts that require it. This method will send a message to the Non-UI handler
+ // thread to pull suggestions, and get the inlined callback to get called on the Non-UI
+ // handler thread. If this is the end of a batch input, the callback will then proceed to
+ // send a message to the UI handler in LatinIME so that showing suggestions can be done on
+ // the UI thread.
+ private void updateBatchInput(final InputPointers batchPointers,
+ final int sequenceNumber, final boolean forEnd) {
+ synchronized (mLock) {
+ if (!mInBatchInput) {
+ // Batch input has ended or canceled while the message was being delivered.
+ return;
+ }
+ mInputLogic.mWordComposer.setBatchInputPointers(batchPointers);
+ getSuggestedWords(Suggest.SESSION_GESTURE, sequenceNumber,
+ new OnGetSuggestedWordsCallback() {
+ @Override
+ public void onGetSuggestedWords(SuggestedWords suggestedWords) {
+ // We're now inside the callback. This always runs on the Non-UI thread,
+ // no matter what thread updateBatchInput was originally called on.
+ if (suggestedWords.isEmpty()) {
+ // Use old suggestions if we don't have any new ones.
+ // Previous suggestions are found in InputLogic#mSuggestedWords.
+ // Since these are the most recent ones and we just recomputed
+ // new ones to update them, then the previous ones are there.
+ suggestedWords = mInputLogic.mSuggestedWords;
+ }
+ mLatinIME.mHandler.showGesturePreviewAndSuggestionStrip(suggestedWords,
+ forEnd /* dismissGestureFloatingPreviewText */);
+ if (forEnd) {
+ mInBatchInput = false;
+ // The following call schedules onEndBatchInputAsyncInternal
+ // to be called on the UI thread.
+ mLatinIME.mHandler.onEndBatchInput(suggestedWords);
+ }
+ }
+ });
+ }
+ }
+
+ /**
+ * Update a batch input.
+ *
+ * This fetches suggestions and updates the suggestion strip and the floating text preview.
+ *
+ * @param batchPointers the updated batch pointers.
+ * @param sequenceNumber the sequence number associated with this batch input.
+ */
+ // Called on the UI thread by InputLogic.
+ public void onUpdateBatchInput(final InputPointers batchPointers,
+ final int sequenceNumber) {
+ updateBatchInput(batchPointers, sequenceNumber, false /* forEnd */);
+ }
+
+ /**
+ * Cancel a batch input.
+ *
+ * Note that as opposed to onEndBatchInput, we do the UI side of this immediately on the
+ * same thread, rather than get this to call a method in LatinIME. This is because
+ * canceling a batch input does not necessitate the long operation of pulling suggestions.
+ */
+ // Called on the UI thread by InputLogic.
+ public void onCancelBatchInput() {
+ synchronized (mLock) {
+ mInBatchInput = false;
+ }
+ }
+
+ /**
+ * Finish a batch input.
+ *
+ * This fetches suggestions, updates the suggestion strip and commits the first suggestion.
+ * It also dismisses the floating text preview.
+ *
+ * @param batchPointers the updated batch pointers.
+ * @param sequenceNumber the sequence number associated with this batch input.
+ */
+ // Called on the UI thread by InputLogic.
+ public void onEndBatchInput(final InputPointers batchPointers, final int sequenceNumber) {
+ updateBatchInput(batchPointers, sequenceNumber, true /* forEnd */);
+ }
+
+ public void getSuggestedWords(final int sessionId, final int sequenceNumber,
+ final OnGetSuggestedWordsCallback callback) {
+ mNonUIThreadHandler.obtainMessage(
+ MSG_GET_SUGGESTED_WORDS, sessionId, sequenceNumber, callback).sendToTarget();
+ }
}