diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinIME.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/LatinIME.java | 173 |
1 files changed, 53 insertions, 120 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 3d29c5a0b..6c83ac7ed 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -86,6 +86,7 @@ import com.android.inputmethod.latin.settings.SettingsActivity; import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.suggestions.SuggestionStripView; import com.android.inputmethod.latin.utils.ApplicationUtils; +import com.android.inputmethod.latin.utils.AsyncResultHolder; import com.android.inputmethod.latin.utils.AutoCorrectionUtils; import com.android.inputmethod.latin.utils.CapsModeUtils; import com.android.inputmethod.latin.utils.CollectionUtils; @@ -107,8 +108,6 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.Locale; import java.util.TreeSet; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; /** * Input method implementation for Qwerty'ish keyboard. @@ -1668,8 +1667,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return didAutoCorrect; } - // Called from the end of onTextInput - private void completeOnTextInput(final String rawText) { + // Called from PointerTracker through the KeyboardActionListener interface + @Override + public void onTextInput(final String rawText) { + mConnection.beginBatchEdit(); + if (mWordComposer.isComposingWord()) { + commitCurrentAutoCorrection(rawText); + } else { + resetComposingState(true /* alsoResetLastComposedWord */); + } mHandler.postUpdateSuggestionStrip(); if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS && ResearchLogger.RESEARCH_KEY_OUTPUT_TEXT.equals(rawText)) { @@ -1692,44 +1698,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mEnteredText = text; } - // Called from PointerTracker through the KeyboardActionListener interface - @Override - public void onTextInput(final String rawText) { - mConnection.beginBatchEdit(); - boolean isReturningAsynchronously = false; - if (mWordComposer.isComposingWord()) { - commitCurrentAutoCorrection(rawText, new Runnable() { - @Override - public void run() { - completeOnTextInput(rawText); - } - }); - isReturningAsynchronously = true; - } else { - resetComposingState(true /* alsoResetLastComposedWord */); - } - if (!isReturningAsynchronously) { - completeOnTextInput(rawText); - } - } - - private void completeOnStartBatchInput(final SettingsValues settingsValues) { - final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); - if (Character.isLetterOrDigit(codePointBeforeCursor) - || settingsValues.isUsuallyFollowedBySpace(codePointBeforeCursor)) { - mSpaceState = SPACE_STATE_PHANTOM; - } - mConnection.endBatchEdit(); - mWordComposer.setCapitalizedModeAtStartComposingTime(getActualCapsMode()); - } - @Override public void onStartBatchInput() { mInputUpdater.onStartBatchInput(); mHandler.cancelUpdateSuggestionStrip(); mConnection.beginBatchEdit(); final SettingsValues settingsValues = mSettings.getCurrent(); - boolean isReturningAsynchronously = false; if (mWordComposer.isComposingWord()) { if (settingsValues.mIsInternal) { if (mWordComposer.isBatchMode()) { @@ -1751,21 +1725,19 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // 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(LastComposedWord.NOT_A_SEPARATOR, new Runnable() { - @Override - public void run() { - completeOnStartBatchInput(settingsValues); - } - }); - isReturningAsynchronously = true; + commitCurrentAutoCorrection(LastComposedWord.NOT_A_SEPARATOR); } else { commitTyped(LastComposedWord.NOT_A_SEPARATOR); } mExpectingUpdateSelection = true; } - if (!isReturningAsynchronously) { - completeOnStartBatchInput(settingsValues); + final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); + if (Character.isLetterOrDigit(codePointBeforeCursor) + || settingsValues.isUsuallyFollowedBySpace(codePointBeforeCursor)) { + mSpaceState = SPACE_STATE_PHANTOM; } + mConnection.endBatchEdit(); + mWordComposer.setCapitalizedModeAtStartComposingTime(getActualCapsMode()); } private static final class InputUpdater implements Handler.Callback { @@ -2245,9 +2217,30 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mKeyboardSwitcher.updateShiftState(); } - private void completeHandleSeparator(final int primaryCode, final int x, final int y, - final int spaceState, final SettingsValues currentSettings, - final boolean shouldAvoidSendingCode) { + // Returns true if we do an autocorrection, false otherwise. + private boolean handleSeparator(final int primaryCode, final int x, final int y, + final int spaceState) { + boolean didAutoCorrect = false; + final SettingsValues currentSettings = mSettings.getCurrent(); + // We avoid sending spaces in languages without spaces if we were composing. + final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == primaryCode + && !currentSettings.mCurrentLanguageHasSpaces && mWordComposer.isComposingWord(); + 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 separator at the current cursor position. + resetEntireInputState(mLastSelectionStart); + } + if (mWordComposer.isComposingWord()) { // May have changed since we stored wasComposing + if (currentSettings.mCorrectionEnabled) { + final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR + : new String(new int[] { primaryCode }, 0, 1); + commitCurrentAutoCorrection(separator); + didAutoCorrect = true; + } else { + commitTyped(new String(new int[]{primaryCode}, 0, 1)); + } + } + final boolean swapWeakSpace = maybeStripSpace(primaryCode, spaceState, Constants.SUGGESTION_STRIP_COORDINATE == x); @@ -2302,44 +2295,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } mKeyboardSwitcher.updateShiftState(); - } - - // Returns true if we do an autocorrection, false otherwise. - private boolean handleSeparator(final int primaryCode, final int x, final int y, - final int spaceState) { - boolean doesAutoCorrect = false; - final SettingsValues currentSettings = mSettings.getCurrent(); - // We avoid sending spaces in languages without spaces if we were composing. - final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == primaryCode - && !currentSettings.mCurrentLanguageHasSpaces && mWordComposer.isComposingWord(); - 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 separator at the current cursor position. - resetEntireInputState(mLastSelectionStart); - } - boolean isReturningAsynchronously = false; - if (mWordComposer.isComposingWord()) { // May have changed since we stored wasComposing - if (currentSettings.mCorrectionEnabled) { - final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR - : new String(new int[] { primaryCode }, 0, 1); - commitCurrentAutoCorrection(separator, new Runnable() { - @Override - public void run() { - completeHandleSeparator(primaryCode, x, y, spaceState, currentSettings, - shouldAvoidSendingCode); - } - }); - doesAutoCorrect = true; - isReturningAsynchronously = true; - } else { - commitTyped(new String(new int[]{primaryCode}, 0, 1)); - } - } - if (!isReturningAsynchronously) { - completeHandleSeparator(primaryCode, x, y, spaceState, currentSettings, - shouldAvoidSendingCode); - } - return doesAutoCorrect; + return didAutoCorrect; } private CharSequence getTextWithUnderline(final String text) { @@ -2428,31 +2384,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return; } - final CountDownLatch latch = new CountDownLatch(1); - final SuggestedWords[] suggestedWordsArray = new SuggestedWords[1]; + final AsyncResultHolder<SuggestedWords> holder = new AsyncResultHolder<SuggestedWords>(); getSuggestedWordsOrOlderSuggestionsAsync(Suggest.SESSION_TYPING, new OnGetSuggestedWordsCallback() { @Override public void onGetSuggestedWords(final SuggestedWords suggestedWords) { - suggestedWordsArray[0] = suggestedWords; - latch.countDown(); + holder.set(suggestedWords); } } ); - // TODO: Quit blocking the main thread. - try { - // Wait for the result of getSuggestedWords - // We set the time out to avoid ANR. - latch.await(GET_SUGGESTED_WORDS_TIMEOUT, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - // TODO: Cancel all pending "getSuggestedWords" tasks when it failed. We may want to add - // "onGetSuggestionFailed" to "OnGetSuggestedWordsCallback". - Log.e(TAG, "InterruptedException while waiting for getSuggestedWords.", e); - return; - } - if (suggestedWordsArray[0] != null) { - showSuggestionStrip(suggestedWordsArray[0]); + // This line may cause the current thread to wait. + final SuggestedWords suggestedWords = holder.get(null, GET_SUGGESTED_WORDS_TIMEOUT); + if (suggestedWords != null) { + showSuggestionStrip(suggestedWords); } } @@ -2555,7 +2500,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen setSuggestionStripShown(isSuggestionsStripVisible()); } - private void completeCommitCurrentAutoCorrection(final String separator) { + private void commitCurrentAutoCorrection(final String separator) { + // Complete any pending suggestions query first + if (mHandler.hasPendingUpdateSuggestions()) { + updateSuggestionStrip(); + } final String typedAutoCorrection = mWordComposer.getAutoCorrectionOrNull(); final String typedWord = mWordComposer.getTypedWord(); final String autoCorrection = (typedAutoCorrection != null) @@ -2591,22 +2540,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } - private void commitCurrentAutoCorrection(final String separator, final Runnable callback) { - getSuggestedWordsOrOlderSuggestionsAsync(Suggest.SESSION_TYPING, - new OnGetSuggestedWordsCallback() { - @Override - public void onGetSuggestedWords(final SuggestedWords suggestedWords) { - if (suggestedWords != null) { - setAutoCorrection(suggestedWords); - } - completeCommitCurrentAutoCorrection(separator); - if (callback != null) { - callback.run(); - } - } - }); - } - // Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener} // interface @Override |