From 9ae24750f462cbd94b362fe54a952c86cfb05e5f Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Thu, 30 Jun 2011 18:34:10 +0900 Subject: Some clean up and fixes around auto-correction cancellation logic Change-Id: I07f4c3cd98d14d9f0da2de54bfa76a3867389f6f --- java/src/com/android/inputmethod/latin/TextEntryState.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index de13f3ae4..b6e261114 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -144,7 +144,7 @@ public class TextEntryState { break; case UNDO_COMMIT: if (isSpace || isSeparator) { - setState(ACCEPTED_DEFAULT); + setState(START); } else { setState(IN_WORD); } -- cgit v1.2.3-83-g751a From 9318d33b6e501d4dd9ea860de04160ccb23a7501 Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Tue, 12 Jul 2011 10:29:29 +0900 Subject: Change variable names for readability Change-Id: I9face4e90af4bcd6040eb7e303b596af7d643934 --- .../com/android/inputmethod/latin/LatinIME.java | 101 ++++++++++----------- .../android/inputmethod/latin/TextEntryState.java | 3 +- 2 files changed, 52 insertions(+), 52 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index c41ff0ee6..e6ac6dc44 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -164,8 +164,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private boolean mIsSettingsSuggestionStripOn; private boolean mApplicationSpecifiedCompletionOn; - private final StringBuilder mComposing = new StringBuilder(); - private WordComposer mWord = new WordComposer(); + private final StringBuilder mComposingStringBuilder = new StringBuilder(); + private WordComposer mWordComposer = new WordComposer(); private CharSequence mBestWord; private boolean mHasUncommittedTypedChars; private boolean mHasDictionary; @@ -233,7 +233,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar case MSG_UPDATE_OLD_SUGGESTIONS: latinIme.mRecorrection.fetchAndDisplayRecorrectionSuggestions( latinIme.mVoiceProxy, latinIme.mCandidateView, - latinIme.mSuggest, latinIme.mKeyboardSwitcher, latinIme.mWord, + latinIme.mSuggest, latinIme.mKeyboardSwitcher, latinIme.mWordComposer, latinIme.mHasUncommittedTypedChars, latinIme.mLastSelectionStart, latinIme.mLastSelectionEnd, latinIme.mSettingsValues.mWordSeparators); break; @@ -555,7 +555,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar inputView.closing(); mEnteredText = null; - mComposing.setLength(0); + mComposingStringBuilder.setLength(0); mHasUncommittedTypedChars = false; mDeleteCount = 0; mJustAddedMagicSpace = false; @@ -717,16 +717,16 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final boolean selectionChanged = (newSelStart != candidatesEnd || newSelEnd != candidatesEnd) && mLastSelectionStart != newSelStart; final boolean candidatesCleared = candidatesStart == -1 && candidatesEnd == -1; - if (((mComposing.length() > 0 && mHasUncommittedTypedChars) + if (((mComposingStringBuilder.length() > 0 && mHasUncommittedTypedChars) || mVoiceProxy.isVoiceInputHighlighted()) && (selectionChanged || candidatesCleared)) { if (candidatesCleared) { // If the composing span has been cleared, save the typed word in the history for // recorrection before we reset the candidate strip. Then, we'll be able to show // suggestions for recorrection right away. - mRecorrection.saveRecorrectionSuggestion(mWord, mComposing); + mRecorrection.saveRecorrectionSuggestion(mWordComposer, mComposingStringBuilder); } - mComposing.setLength(0); + mComposingStringBuilder.setLength(0); mHasUncommittedTypedChars = false; if (isCursorTouchingWord()) { mHandler.cancelUpdateBigramPredictions(); @@ -747,8 +747,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } } if (!mExpectingUpdateSelection) { - mJustAddedMagicSpace = false; // The user moved the cursor. - mJustReplacedDoubleSpace = false; + mJustAddedMagicSpace = false; // The user moved the cursor. + mJustReplacedDoubleSpace = false; } mExpectingUpdateSelection = false; mHandler.postUpdateShiftKeyState(); @@ -947,13 +947,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public void commitTyped(InputConnection inputConnection) { if (mHasUncommittedTypedChars) { mHasUncommittedTypedChars = false; - if (mComposing.length() > 0) { + if (mComposingStringBuilder.length() > 0) { if (inputConnection != null) { - inputConnection.commitText(mComposing, 1); + inputConnection.commitText(mComposingStringBuilder, 1); } - mCommittedLength = mComposing.length(); - TextEntryState.acceptedTyped(mComposing); - addToAutoAndUserBigramDictionaries(mComposing, AutoDictionary.FREQUENCY_FOR_TYPED); + mCommittedLength = mComposingStringBuilder.length(); + TextEntryState.acceptedTyped(mComposingStringBuilder); + addToAutoAndUserBigramDictionaries(mComposingStringBuilder, + AutoDictionary.FREQUENCY_FOR_TYPED); } updateSuggestions(); } @@ -1185,12 +1186,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final boolean deleteChar = !mHasUncommittedTypedChars; if (mHasUncommittedTypedChars) { - final int length = mComposing.length(); + final int length = mComposingStringBuilder.length(); if (length > 0) { - mComposing.delete(length - 1, length); - mWord.deleteLast(); - ic.setComposingText(mComposing, 1); - if (mComposing.length() == 0) { + mComposingStringBuilder.delete(length - 1, length); + mWordComposer.deleteLast(); + ic.setComposingText(mComposingStringBuilder, 1); + if (mComposingStringBuilder.length() == 0) { mHasUncommittedTypedChars = false; } if (1 == length) { @@ -1280,9 +1281,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (isAlphabet(code) && isSuggestionsRequested() && !isCursorTouchingWord()) { if (!mHasUncommittedTypedChars) { mHasUncommittedTypedChars = true; - mComposing.setLength(0); - mRecorrection.saveRecorrectionSuggestion(mWord, mBestWord); - mWord.reset(); + mComposingStringBuilder.setLength(0); + mRecorrection.saveRecorrectionSuggestion(mWordComposer, mBestWord); + mWordComposer.reset(); clearSuggestions(); } } @@ -1308,19 +1309,19 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } } if (mHasUncommittedTypedChars) { - if (mComposing.length() == 0 && switcher.isAlphabetMode() + if (mComposingStringBuilder.length() == 0 && switcher.isAlphabetMode() && switcher.isShiftedOrShiftLocked()) { - mWord.setFirstCharCapitalized(true); + mWordComposer.setFirstCharCapitalized(true); } - mComposing.append((char) code); - mWord.add(code, keyCodes, x, y); + mComposingStringBuilder.append((char) code); + mWordComposer.add(code, keyCodes, x, y); InputConnection ic = getCurrentInputConnection(); if (ic != null) { // If it's the first letter, make note of auto-caps state - if (mWord.size() == 1) { - mWord.setAutoCapitalized(getCurrentAutoCapsState()); + if (mWordComposer.size() == 1) { + mWordComposer.setAutoCapitalized(getCurrentAutoCapsState()); } - ic.setComposingText(mComposing, 1); + ic.setComposingText(mComposingStringBuilder, 1); } mHandler.postUpdateSuggestions(); } else { @@ -1388,7 +1389,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar TextEntryState.typedCharacter((char) primaryCode, true, x, y); if (pickedDefault) { - CharSequence typedWord = mWord.getTypedWord(); + CharSequence typedWord = mWordComposer.getTypedWord(); TextEntryState.backToAcceptedDefault(typedWord); if (!TextUtils.isEmpty(typedWord) && !typedWord.equals(mBestWord)) { InputConnectionCompatUtils.commitCorrection( @@ -1491,18 +1492,16 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar setPunctuationSuggestions(); return; } - showSuggestions(mWord); - } - private void showSuggestions(WordComposer word) { + final WordComposer wordComposer = mWordComposer; // TODO: May need a better way of retrieving previous word CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(), mSettingsValues.mWordSeparators); SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( - mKeyboardSwitcher.getKeyboardView(), word, prevWord); + mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord); boolean correctionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection(); - final CharSequence typedWord = word.getTypedWord(); + final CharSequence typedWord = wordComposer.getTypedWord(); // Here, we want to promote a whitelisted word if exists. final boolean typedWordValid = AutoCorrection.isValidWordForAutoCorrection( mSuggest.getUnigramDictionaries(), typedWord, preferCapitalization()); @@ -1511,7 +1510,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar correctionAvailable |= typedWordValid; } // Don't auto-correct words with multiple capital letter - correctionAvailable &= !word.isMostlyCaps(); + correctionAvailable &= !wordComposer.isMostlyCaps(); correctionAvailable &= !TextEntryState.isRecorrecting(); // Basically, we update the suggestion strip only when suggestion count > 1. However, @@ -1558,7 +1557,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar updateSuggestions(); } if (mBestWord != null && mBestWord.length() > 0) { - TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode); + TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, separatorCode); mExpectingUpdateSelection = true; commitBestWord(mBestWord); // Add the word to the auto dictionary if it's not a known word @@ -1625,7 +1624,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (!mHasUncommittedTypedChars) { // If we are not composing a word, then it was a suggestion inferred from // context - no user input. We should reset the word composer. - mWord.reset(); + mWordComposer.reset(); } mExpectingUpdateSelection = true; commitBestWord(suggestion); @@ -1635,9 +1634,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } else { addToOnlyBigramDictionary(suggestion, 1); } - LatinImeLogger.logOnManualSuggestion(mComposing.toString(), suggestion.toString(), - index, suggestions.mWords); - TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion); + LatinImeLogger.logOnManualSuggestion(mComposingStringBuilder.toString(), + suggestion.toString(), index, suggestions.mWords); + TextEntryState.acceptedSuggestion(mComposingStringBuilder.toString(), suggestion); // Follow it with a space if (mShouldInsertMagicSpace && !recorrecting) { sendMagicSpace(); @@ -1696,7 +1695,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan( this, bestWord, suggestedWords), 1); } - mRecorrection.saveRecorrectionSuggestion(mWord, bestWord); + mRecorrection.saveRecorrectionSuggestion(mWordComposer, bestWord); mHasUncommittedTypedChars = false; mCommittedLength = bestWord.length(); } @@ -1800,7 +1799,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } private void revertLastWord(boolean deleteChar) { - final int length = mComposing.length(); + final int length = mComposingStringBuilder.length(); if (!mHasUncommittedTypedChars && length > 0) { final InputConnection ic = getCurrentInputConnection(); final CharSequence punctuation = ic.getTextBeforeCursor(1, 0); @@ -1817,17 +1816,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (deleteChar && !TextUtils.isEmpty(punctuation) && mSettingsValues.isWordSeparator(punctuation.charAt(0)) - && !TextUtils.equals(mComposing, toTheLeft)) { - ic.commitText(mComposing, 1); - TextEntryState.acceptedTyped(mComposing); + && !TextUtils.equals(mComposingStringBuilder, toTheLeft)) { + ic.commitText(mComposingStringBuilder, 1); + TextEntryState.acceptedTyped(mComposingStringBuilder); ic.commitText(punctuation, 1); TextEntryState.typedCharacter(punctuation.charAt(0), true, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); // Clear composing text - mComposing.setLength(0); + mComposingStringBuilder.setLength(0); } else { mHasUncommittedTypedChars = true; - ic.setComposingText(mComposing, 1); + ic.setComposingText(mComposingStringBuilder, 1); TextEntryState.backspace(); } mHandler.cancelUpdateBigramPredictions(); @@ -1863,7 +1862,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } public boolean preferCapitalization() { - return mWord.isFirstCharCapitalized(); + return mWordComposer.isFirstCharCapitalized(); } // Notify that language or mode have been changed and toggleLanguage will update KeyboardID @@ -1987,7 +1986,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } public WordComposer getCurrentWord() { - return mWord; + return mWordComposer; } boolean isSoundOn() { @@ -2123,7 +2122,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final Printer p = new PrintWriterPrinter(fout); p.println("LatinIME state :"); p.println(" Keyboard mode = " + mKeyboardSwitcher.getKeyboardMode()); - p.println(" mComposing=" + mComposing.toString()); + p.println(" mComposingStringBuilder=" + mComposingStringBuilder.toString()); p.println(" mIsSuggestionsRequested=" + mIsSettingsSuggestionStripOn); p.println(" mCorrectionMode=" + mCorrectionMode); p.println(" mHasUncommittedTypedChars=" + mHasUncommittedTypedChars); diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index b6e261114..79b3bdebb 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -16,6 +16,7 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.latin.Utils.RingCharBuffer; import android.util.Log; @@ -100,7 +101,7 @@ public class TextEntryState { } public static void typedCharacter(char c, boolean isSeparator, int x, int y) { - final boolean isSpace = (c == ' '); + final boolean isSpace = (c == Keyboard.CODE_SPACE); switch (sState) { case IN_WORD: if (isSpace || isSeparator) { -- cgit v1.2.3-83-g751a From cd43edbc32d3efd4e076597aa30922c7a500a444 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 25 Oct 2011 17:12:51 +0900 Subject: Small clean up Remove an unused method and constant Change-Id: I3e99ea9fc182c83399d46aca81d1b69aab2c36d1 --- .../com/android/inputmethod/latin/TextEntryState.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index 79b3bdebb..ba93ab526 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -30,13 +30,12 @@ public class TextEntryState { private static final int IN_WORD = 2; private static final int ACCEPTED_DEFAULT = 3; private static final int PICKED_SUGGESTION = 4; - private static final int PUNCTUATION_AFTER_WORD = 5; - private static final int PUNCTUATION_AFTER_ACCEPTED = 6; - private static final int SPACE_AFTER_ACCEPTED = 7; - private static final int SPACE_AFTER_PICKED = 8; - private static final int UNDO_COMMIT = 9; - private static final int RECORRECTING = 10; - private static final int PICKED_RECORRECTION = 11; + private static final int PUNCTUATION_AFTER_ACCEPTED = 5; + private static final int SPACE_AFTER_ACCEPTED = 6; + private static final int SPACE_AFTER_PICKED = 7; + private static final int UNDO_COMMIT = 8; + private static final int RECORRECTING = 9; + private static final int PICKED_RECORRECTION = 10; private static int sState = UNKNOWN; private static int sPreviousState = UNKNOWN; @@ -136,7 +135,6 @@ public class TextEntryState { case START: case UNKNOWN: case SPACE_AFTER_ACCEPTED: - case PUNCTUATION_AFTER_WORD: if (!isSpace && !isSeparator) { setState(IN_WORD); } else { @@ -182,10 +180,6 @@ public class TextEntryState { return sState == ACCEPTED_DEFAULT; } - public static boolean isSpaceAfterPicked() { - return sState == SPACE_AFTER_PICKED; - } - public static boolean isUndoCommit() { return sState == UNDO_COMMIT; } @@ -208,7 +202,6 @@ public class TextEntryState { case IN_WORD: return "IN_WORD"; case ACCEPTED_DEFAULT: return "ACCEPTED_DEFAULT"; case PICKED_SUGGESTION: return "PICKED_SUGGESTION"; - case PUNCTUATION_AFTER_WORD: return "PUNCTUATION_AFTER_WORD"; case PUNCTUATION_AFTER_ACCEPTED: return "PUNCTUATION_AFTER_ACCEPTED"; case SPACE_AFTER_ACCEPTED: return "SPACE_AFTER_ACCEPTED"; case SPACE_AFTER_PICKED: return "SPACE_AFTER_PICKED"; -- cgit v1.2.3-83-g751a From ce668e7a0b6ae7046e1f22a162ad174d58dfe145 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 25 Oct 2011 21:35:43 +0900 Subject: Small clean up. Remove unused methods and constants. Change-Id: If72e04394a2943b416915217d22cb6e58a0508b3 --- .../android/inputmethod/latin/TextEntryState.java | 33 ++++------------------ 1 file changed, 5 insertions(+), 28 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index ba93ab526..4204b8a4f 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -34,8 +34,7 @@ public class TextEntryState { private static final int SPACE_AFTER_ACCEPTED = 6; private static final int SPACE_AFTER_PICKED = 7; private static final int UNDO_COMMIT = 8; - private static final int RECORRECTING = 9; - private static final int PICKED_RECORRECTION = 10; + private static final int PICKED_RECORRECTION = 9; private static int sState = UNKNOWN; private static int sPreviousState = UNKNOWN; @@ -78,7 +77,9 @@ public class TextEntryState { } public static void acceptedSuggestion(CharSequence typedWord, CharSequence actualWord) { - if (sState == RECORRECTING || sState == PICKED_RECORRECTION) { + if (sState == PICKED_RECORRECTION) { + // TODO: this seems to be the only place where setState(PICKED_RECORRECTION) is done + // so this state should never be reached. Check this and remove. setState(PICKED_RECORRECTION); } else { setState(PICKED_SUGGESTION); @@ -87,18 +88,6 @@ public class TextEntryState { displayState("acceptedSuggestion", "typedWord", typedWord, "actualWord", actualWord); } - public static void selectedForRecorrection() { - setState(RECORRECTING); - if (DEBUG) displayState("selectedForRecorrection"); - } - - public static void onAbortRecorrection() { - if (sState == RECORRECTING || sState == PICKED_RECORRECTION) { - setState(START); - } - if (DEBUG) displayState("onAbortRecorrection"); - } - public static void typedCharacter(char c, boolean isSeparator, int x, int y) { final boolean isSpace = (c == Keyboard.CODE_SPACE); switch (sState) { @@ -148,9 +137,6 @@ public class TextEntryState { setState(IN_WORD); } break; - case RECORRECTING: - setState(START); - break; } RingCharBuffer.getInstance().push(c, x, y); if (isSeparator) { @@ -176,20 +162,12 @@ public class TextEntryState { if (DEBUG) displayState("reset"); } - public static boolean isAcceptedDefault() { - return sState == ACCEPTED_DEFAULT; - } - public static boolean isUndoCommit() { return sState == UNDO_COMMIT; } - public static boolean isPunctuationAfterAccepted() { - return sState == PUNCTUATION_AFTER_ACCEPTED; - } - public static boolean isRecorrecting() { - return sState == RECORRECTING || sState == PICKED_RECORRECTION; + return sState == PICKED_RECORRECTION; } public static String getState() { @@ -206,7 +184,6 @@ public class TextEntryState { case SPACE_AFTER_ACCEPTED: return "SPACE_AFTER_ACCEPTED"; case SPACE_AFTER_PICKED: return "SPACE_AFTER_PICKED"; case UNDO_COMMIT: return "UNDO_COMMIT"; - case RECORRECTING: return "RECORRECTING"; case PICKED_RECORRECTION: return "PICKED_RECORRECTION"; default: return "UNKNOWN"; } -- cgit v1.2.3-83-g751a From d0c5f9395a1b94e8425982e353d090f972dc44f0 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 25 Oct 2011 21:54:35 +0900 Subject: Cleanup. Remove a state that can never be reached, and ajust all the callers that would always have received false. Change-Id: Iac025568be11743428419e0772da306a4f0a0bf1 --- java/src/com/android/inputmethod/latin/LatinIME.java | 18 +++++++----------- .../com/android/inputmethod/latin/TextEntryState.java | 15 +-------------- 2 files changed, 8 insertions(+), 25 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 90fc818bb..94be6f0fd 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1630,7 +1630,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public boolean isSuggestionsStripVisible() { if (mSuggestionsView == null) return false; - if (mSuggestionsView.isShowingAddToDictionaryHint() || TextEntryState.isRecorrecting()) + if (mSuggestionsView.isShowingAddToDictionaryHint()) return true; if (!isShowingSuggestionsStrip()) return false; @@ -1735,7 +1735,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } // Don't auto-correct words with multiple capital letter autoCorrectionAvailable &= !wordComposer.isMostlyCaps(); - autoCorrectionAvailable &= !TextEntryState.isRecorrecting(); // Basically, we update the suggestion strip only when suggestion count > 1. However, // there is an exception: We update the suggestion strip whenever typed word's length @@ -1808,7 +1807,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mVoiceProxy.flushAndLogAllTextModificationCounters(index, suggestion, mSettingsValues.mWordSeparators); - final boolean recorrecting = TextEntryState.isRecorrecting(); final InputConnection ic = getCurrentInputConnection(); if (ic != null) { ic.beginBatchEdit(); @@ -1882,7 +1880,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar suggestion.toString(), index, suggestions.mWords); TextEntryState.acceptedSuggestion(mComposingStringBuilder.toString(), suggestion); // Follow it with a space - if (mInsertSpaceOnPickSuggestionManually && !recorrecting) { + if (mInsertSpaceOnPickSuggestionManually) { sendMagicSpace(); } @@ -1902,13 +1900,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar || !AutoCorrection.isValidWord( mSuggest.getUnigramDictionaries(), suggestion, true)); - if (!recorrecting) { - // Fool the state watcher so that a subsequent backspace will not do a revert, unless - // we just did a correction, in which case we need to stay in - // TextEntryState.State.PICKED_SUGGESTION state. - TextEntryState.typedCharacter((char) Keyboard.CODE_SPACE, true, - WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); - } + // Fool the state watcher so that a subsequent backspace will not do a revert, unless + // we just did a correction, in which case we need to stay in + // TextEntryState.State.PICKED_SUGGESTION state. + TextEntryState.typedCharacter((char) Keyboard.CODE_SPACE, true, + WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); if (!showingAddToDictionaryHint) { // If we're not showing the "Touch again to save", then show corrections again. // In case the cursor position doesn't change, make sure we show the suggestions again. diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index 4204b8a4f..82242f87e 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -34,7 +34,6 @@ public class TextEntryState { private static final int SPACE_AFTER_ACCEPTED = 6; private static final int SPACE_AFTER_PICKED = 7; private static final int UNDO_COMMIT = 8; - private static final int PICKED_RECORRECTION = 9; private static int sState = UNKNOWN; private static int sPreviousState = UNKNOWN; @@ -77,13 +76,7 @@ public class TextEntryState { } public static void acceptedSuggestion(CharSequence typedWord, CharSequence actualWord) { - if (sState == PICKED_RECORRECTION) { - // TODO: this seems to be the only place where setState(PICKED_RECORRECTION) is done - // so this state should never be reached. Check this and remove. - setState(PICKED_RECORRECTION); - } else { - setState(PICKED_SUGGESTION); - } + setState(PICKED_SUGGESTION); if (DEBUG) displayState("acceptedSuggestion", "typedWord", typedWord, "actualWord", actualWord); } @@ -111,7 +104,6 @@ public class TextEntryState { } break; case PICKED_SUGGESTION: - case PICKED_RECORRECTION: if (isSpace) { setState(SPACE_AFTER_PICKED); } else if (isSeparator) { @@ -166,10 +158,6 @@ public class TextEntryState { return sState == UNDO_COMMIT; } - public static boolean isRecorrecting() { - return sState == PICKED_RECORRECTION; - } - public static String getState() { return stateName(sState); } @@ -184,7 +172,6 @@ public class TextEntryState { case SPACE_AFTER_ACCEPTED: return "SPACE_AFTER_ACCEPTED"; case SPACE_AFTER_PICKED: return "SPACE_AFTER_PICKED"; case UNDO_COMMIT: return "UNDO_COMMIT"; - case PICKED_RECORRECTION: return "PICKED_RECORRECTION"; default: return "UNKNOWN"; } } -- cgit v1.2.3-83-g751a From 6b1f500da451de56932a8b2a99c63857994ece85 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 22 Nov 2011 11:35:40 +0900 Subject: Resume suggestion when backspacing to the end of a word Bug: 5515381 Change-Id: I26fea896feaf2e9716c7ae3d4f2630360f23ac50 --- .../com/android/inputmethod/latin/LatinIME.java | 60 +++++++++++++++++++++- .../android/inputmethod/latin/TextEntryState.java | 15 ++++++ .../android/inputmethod/latin/WordComposer.java | 46 +++++++++++++++++ .../android/inputmethod/latin/SuggestHelper.java | 19 +------ 4 files changed, 120 insertions(+), 20 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index f7a77cae7..4f26a21f9 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -948,6 +948,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } mExpectingUpdateSelection = false; mHandler.postUpdateShiftKeyState(); + // TODO: Decide to call restartSuggestionsOnWordBeforeCursorIfAtEndOfWord() or not + // here. It would probably be too expensive to call directly here but we may want to post a + // message to delay it. The point would be to unify behavior between backspace to the + // end of a word and manually put the pointer at the end of the word. // Make a note of the cursor position mLastSelectionStart = newSelStart; @@ -1466,10 +1470,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // inconsistent with backspacing after selecting other suggestions. revertLastWord(ic); } else { - sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); + ic.deleteSurroundingText(1, 0); if (mDeleteCount > DELETE_ACCELERATE_AT) { - sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); + ic.deleteSurroundingText(1, 0); } + restartSuggestionsOnWordBeforeCursorIfAtEndOfWord(ic); } } ic.endBatchEdit(); @@ -2117,6 +2122,53 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar return TextUtils.equals(text, beforeText); } + // "ic" must not be null + /** + * Check if the cursor is actually at the end of a word. If so, restart suggestions on this + * word, else do nothing. + */ + private void restartSuggestionsOnWordBeforeCursorIfAtEndOfWord( + final InputConnection ic) { + // Bail out if the cursor is not at the end of a word (cursor must be preceded by + // non-whitespace, non-separator, non-start-of-text) + // Example ("|" is the cursor here) : "|a" " |a" " | " all get rejected here. + final CharSequence textBeforeCursor = ic.getTextBeforeCursor(1, 0); + if (TextUtils.isEmpty(textBeforeCursor) + || mSettingsValues.isWordSeparator(textBeforeCursor.charAt(0))) return; + + // Bail out if the cursor is in the middle of a word (cursor must be followed by whitespace, + // separator or end of line/text) + // Example: "test|" "te|st" get rejected here + final CharSequence textAfterCursor = ic.getTextAfterCursor(1, 0); + if (!TextUtils.isEmpty(textAfterCursor) + && !mSettingsValues.isWordSeparator(textAfterCursor.charAt(0))) return; + + // Bail out if word before cursor is 0-length or a single non letter (like an apostrophe) + // Example: " '|" gets rejected here but "I'|" and "I|" are okay + final CharSequence word = EditingUtils.getWordAtCursor(ic, mSettingsValues.mWordSeparators); + if (TextUtils.isEmpty(word)) return; + if (word.length() == 1 && !Character.isLetter(word.charAt(0))) return; + + // Okay, we are at the end of a word. Restart suggestions. + restartSuggestionsOnWordBeforeCursor(ic, word); + } + + // "ic" must not be null + private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic, + final CharSequence word) { + mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard()); + mComposingStringBuilder.setLength(0); + mComposingStringBuilder.append(word); + // mBestWord will be set appropriately by updateSuggestions() called by the handler + mBestWord = null; + mHasUncommittedTypedChars = true; + mComposingStateManager.onStartComposingText(); + TextEntryState.restartSuggestionsOnWordBeforeCursor(); + ic.deleteSurroundingText(word.length(), 0); + ic.setComposingText(word, 1); + mHandler.postUpdateSuggestions(); + } + // "ic" must not be null private void revertLastWord(final InputConnection ic) { if (mHasUncommittedTypedChars || mComposingStringBuilder.length() <= 0) { @@ -2143,6 +2195,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // Clear composing text mComposingStringBuilder.setLength(0); } else { + // Note: this relies on the last word still being held in the WordComposer + // Note: in the interest of code simplicity, we may want to just call + // restartSuggestionsOnWordBeforeCursorIfAtEndOfWord instead, but retrieving + // the old WordComposer allows to reuse the actual typed coordinates. mHasUncommittedTypedChars = true; ic.setComposingText(mComposingStringBuilder, 1); TextEntryState.backspace(); diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index 82242f87e..a6041b310 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -146,9 +146,24 @@ public class TextEntryState { } else if (sState == UNDO_COMMIT) { setState(IN_WORD); } + // TODO: tidy up this logic. At the moment, for example, writing a word goes to + // ACCEPTED_DEFAULT, backspace will go to UNDO_COMMIT, another backspace will go to IN_WORD, + // and subsequent backspaces will leave the status at IN_WORD, even if the user backspaces + // past the end of the word. We are not in a word any more but the state is still IN_WORD. if (DEBUG) displayState("backspace"); } + public static void restartSuggestionsOnWordBeforeCursor() { + if (UNKNOWN == sState || ACCEPTED_DEFAULT == sState) { + // Here we can come from pretty much any state, except the ones that we can't + // come from after backspace, so supposedly anything except UNKNOWN and + // ACCEPTED_DEFAULT. Note : we could be in UNDO_COMMIT if + // LatinIME#revertLastWord() was calling LatinIME#restartSuggestions...() + Log.e(TAG, "Strange state change : coming from state " + sState); + } + setState(IN_WORD); + } + public static void reset() { setState(START); if (DEBUG) displayState("reset"); diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java index 612b16071..ef2e4d378 100644 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ b/java/src/com/android/inputmethod/latin/WordComposer.java @@ -17,7 +17,9 @@ package com.android.inputmethod.latin; import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.KeyDetector; +import com.android.inputmethod.keyboard.LatinKeyboard; import java.util.ArrayList; import java.util.Arrays; @@ -136,6 +138,50 @@ public class WordComposer { mIsLastCharASingleQuote = Keyboard.CODE_SINGLE_QUOTE == primaryCode; } + /** + * Internal method to retrieve reasonable proximity info for a character. + */ + private void addKeyInfo(final int codePoint, final LatinKeyboard keyboard, + final KeyDetector keyDetector) { + for (final Key key : keyboard.mKeys) { + if (key.mCode == codePoint) { + final int x = key.mX + key.mWidth / 2; + final int y = key.mY + key.mHeight / 2; + final int[] codes = keyDetector.newCodeArray(); + keyDetector.getKeyIndexAndNearbyCodes(x, y, codes); + add(codePoint, codes, x, y); + return; + } + } + add(codePoint, new int[] { codePoint }, + WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); + } + + /** + * Set the currently composing word to the one passed as an argument. + * This will register NOT_A_COORDINATE for X and Ys, and use the passed keyboard for proximity. + */ + public void setComposingWord(final CharSequence word, final LatinKeyboard keyboard, + final KeyDetector keyDetector) { + reset(); + final int length = word.length(); + for (int i = 0; i < length; ++i) { + int codePoint = word.charAt(i); + addKeyInfo(codePoint, keyboard, keyDetector); + } + } + + /** + * Shortcut for the above method, this will create a new KeyDetector for the passed keyboard. + */ + public void setComposingWord(final CharSequence word, final LatinKeyboard keyboard) { + final KeyDetector keyDetector = new KeyDetector(0); + keyDetector.setKeyboard(keyboard, 0, 0); + keyDetector.setProximityCorrectionEnabled(true); + keyDetector.setProximityThreshold(keyboard.mMostCommonKeyWidth); + setComposingWord(word, keyboard, keyDetector); + } + /** * Swaps the first and second values in the codes array if the primary code is not the first * value in the array but the second. This happens when the preferred key is not the key that diff --git a/tests/src/com/android/inputmethod/latin/SuggestHelper.java b/tests/src/com/android/inputmethod/latin/SuggestHelper.java index 464930f4c..a1f16c9ca 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestHelper.java +++ b/tests/src/com/android/inputmethod/latin/SuggestHelper.java @@ -65,26 +65,9 @@ public class SuggestHelper { return mSuggest.hasMainDictionary(); } - private void addKeyInfo(WordComposer word, char c) { - for (final Key key : mKeyboard.mKeys) { - if (key.mCode == c) { - final int x = key.mX + key.mWidth / 2; - final int y = key.mY + key.mHeight / 2; - final int[] codes = mKeyDetector.newCodeArray(); - mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes); - word.add(c, codes, x, y); - return; - } - } - word.add(c, new int[] { c }, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); - } - protected WordComposer createWordComposer(CharSequence s) { WordComposer word = new WordComposer(); - for (int i = 0; i < s.length(); i++) { - final char c = s.charAt(i); - addKeyInfo(word, c); - } + word.setComposingWord(s, mKeyboard, mKeyDetector); return word; } -- cgit v1.2.3-83-g751a From 3651220327c051d8017045aa5e8919461507b3f8 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 9 Dec 2011 23:15:49 +0900 Subject: Remove a duplicated variable. This is awesome cleanup. Change-Id: I35ce8940a13f3084630dc26d6e7e397acfdaac36 --- .../com/android/inputmethod/latin/LatinIME.java | 56 ++++++++++------------ .../android/inputmethod/latin/TextEntryState.java | 5 +- .../android/inputmethod/latin/WordComposer.java | 3 -- 3 files changed, 27 insertions(+), 37 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 5d075b175..c47cae160 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -203,7 +203,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private boolean mIsSettingsSuggestionStripOn; private boolean mApplicationSpecifiedCompletionOn; - private final StringBuilder mComposingStringBuilder = new StringBuilder(); private WordComposer mWordComposer = new WordComposer(); private CharSequence mBestWord; private boolean mHasUncommittedTypedChars; @@ -756,7 +755,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar inputView.closing(); mEnteredText = null; - mComposingStringBuilder.setLength(0); + mWordComposer.reset(); mHasUncommittedTypedChars = false; mDeleteCount = 0; mSpaceState = SPACE_STATE_NONE; @@ -928,10 +927,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // newly inserted punctuation. mSpaceState = SPACE_STATE_NONE; } - if (((mComposingStringBuilder.length() > 0 && mHasUncommittedTypedChars) + if (((mWordComposer.size() > 0 && mHasUncommittedTypedChars) || mVoiceProxy.isVoiceInputHighlighted()) && (selectionChanged || candidatesCleared)) { - mComposingStringBuilder.setLength(0); + mWordComposer.reset(); mHasUncommittedTypedChars = false; TextEntryState.reset(); updateSuggestions(); @@ -1146,13 +1145,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public void commitTyped(final InputConnection ic) { if (!mHasUncommittedTypedChars) return; mHasUncommittedTypedChars = false; - if (mComposingStringBuilder.length() > 0) { + final CharSequence typedWord = mWordComposer.getTypedWord(); + if (typedWord.length() > 0) { if (ic != null) { - ic.commitText(mComposingStringBuilder, 1); + ic.commitText(typedWord, 1); } - mCommittedLength = mComposingStringBuilder.length(); - TextEntryState.acceptedTyped(mComposingStringBuilder); - addToUserUnigramAndBigramDictionaries(mComposingStringBuilder, + mCommittedLength = typedWord.length(); + TextEntryState.acceptedTyped(typedWord); + addToUserUnigramAndBigramDictionaries(typedWord, UserUnigramDictionary.FREQUENCY_FOR_TYPED); } updateSuggestions(); @@ -1408,17 +1408,16 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final boolean deleteChar = !mHasUncommittedTypedChars; if (mHasUncommittedTypedChars) { - final int length = mComposingStringBuilder.length(); + final int length = mWordComposer.size(); if (length > 0) { - mComposingStringBuilder.delete(length - 1, length); mWordComposer.deleteLast(); final CharSequence textWithUnderline = mComposingStateManager.isAutoCorrectionIndicatorOn() ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( - this, mComposingStringBuilder) - : mComposingStringBuilder; + this, mWordComposer.getTypedWord()) + : mWordComposer.getTypedWord(); ic.setComposingText(textWithUnderline, 1); - if (mComposingStringBuilder.length() == 0) { + if (mWordComposer.size() == 0) { mHasUncommittedTypedChars = false; } if (1 == length) { @@ -1521,7 +1520,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // Reset entirely the composing state anyway, then start composing a new word unless // the character is a single quote. mHasUncommittedTypedChars = (Keyboard.CODE_SINGLE_QUOTE != code); - mComposingStringBuilder.setLength(0); mWordComposer.reset(); clearSuggestions(); mComposingStateManager.onFinishComposingText(); @@ -1551,7 +1549,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } } if (mHasUncommittedTypedChars) { - mComposingStringBuilder.append((char) code); mWordComposer.add(code, keyCodes, x, y); if (ic != null) { // If it's the first letter, make note of auto-caps state @@ -1562,8 +1559,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final CharSequence textWithUnderline = mComposingStateManager.isAutoCorrectionIndicatorOn() ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( - this, mComposingStringBuilder) - : mComposingStringBuilder; + this, mWordComposer.getTypedWord()) + : mWordComposer.getTypedWord(); ic.setComposingText(textWithUnderline, 1); } mHandler.postUpdateSuggestions(); @@ -1751,8 +1748,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } final CharSequence textWithUnderline = newAutoCorrectionIndicator ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( - this, mComposingStringBuilder) - : mComposingStringBuilder; + this, mWordComposer.getTypedWord()) + : mWordComposer.getTypedWord(); if (!TextUtils.isEmpty(textWithUnderline)) { ic.setComposingText(textWithUnderline, 1); } @@ -1945,9 +1942,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } else { addToOnlyBigramDictionary(suggestion, 1); } - LatinImeLogger.logOnManualSuggestion(mComposingStringBuilder.toString(), + LatinImeLogger.logOnManualSuggestion(mWordComposer.getTypedWord().toString(), suggestion.toString(), index, suggestions.mWords); - TextEntryState.acceptedSuggestion(mComposingStringBuilder.toString(), suggestion); + TextEntryState.acceptedSuggestion(mWordComposer.getTypedWord().toString(), suggestion); // Follow it with a space if (mInsertSpaceOnPickSuggestionManually) { sendMagicSpace(); @@ -2157,8 +2154,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic, final CharSequence word) { mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard()); - mComposingStringBuilder.setLength(0); - mComposingStringBuilder.append(word); // mBestWord will be set appropriately by updateSuggestions() called by the handler mBestWord = null; mHasUncommittedTypedChars = true; @@ -2171,7 +2166,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // "ic" must not be null private void revertLastWord(final InputConnection ic) { - if (mHasUncommittedTypedChars || mComposingStringBuilder.length() <= 0) { + if (mHasUncommittedTypedChars || mWordComposer.size() <= 0) { sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); return; } @@ -2186,21 +2181,19 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // the cursor. if (!TextUtils.isEmpty(separator) && mSettingsValues.isWordSeparator(separator.charAt(0)) - && !TextUtils.equals(mComposingStringBuilder, textToTheLeft)) { - ic.commitText(mComposingStringBuilder, 1); - TextEntryState.acceptedTyped(mComposingStringBuilder); + && !TextUtils.equals(mWordComposer.getTypedWord(), textToTheLeft)) { + ic.commitText(mWordComposer.getTypedWord(), 1); + TextEntryState.acceptedTyped(mWordComposer.getTypedWord()); ic.commitText(separator, 1); TextEntryState.typedCharacter(separator.charAt(0), true, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); - // Clear composing text - mComposingStringBuilder.setLength(0); } else { // Note: this relies on the last word still being held in the WordComposer // Note: in the interest of code simplicity, we may want to just call // restartSuggestionsOnWordBeforeCursorIfAtEndOfWord instead, but retrieving // the old WordComposer allows to reuse the actual typed coordinates. mHasUncommittedTypedChars = true; - ic.setComposingText(mComposingStringBuilder, 1); + ic.setComposingText(mWordComposer.getTypedWord(), 1); TextEntryState.backspace(); } mHandler.cancelUpdateBigramPredictions(); @@ -2479,7 +2472,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final Keyboard keyboard = mKeyboardSwitcher.getLatinKeyboard(); final int keyboardMode = keyboard != null ? keyboard.mId.mMode : -1; p.println(" Keyboard mode = " + keyboardMode); - p.println(" mComposingStringBuilder=" + mComposingStringBuilder.toString()); p.println(" mIsSuggestionsRequested=" + mIsSettingsSuggestionStripOn); p.println(" mCorrectionMode=" + mCorrectionMode); p.println(" mHasUncommittedTypedChars=" + mHasUncommittedTypedChars); diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index a6041b310..6b44fc500 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -19,6 +19,7 @@ package com.android.inputmethod.latin; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.latin.Utils.RingCharBuffer; +import android.text.TextUtils; import android.util.Log; public class TextEntryState { @@ -45,7 +46,7 @@ public class TextEntryState { public static void acceptedDefault(CharSequence typedWord, CharSequence actualWord, int separatorCode) { - if (typedWord == null) return; + if (TextUtils.isEmpty(typedWord)) return; setState(ACCEPTED_DEFAULT); LatinImeLogger.logOnAutoCorrection( typedWord.toString(), actualWord.toString(), separatorCode); @@ -57,7 +58,7 @@ public class TextEntryState { // (see "case ACCEPTED_DEFAULT" in typedCharacter() below), // and should be restored back to State.ACCEPTED_DEFAULT after processing for each sub-state. public static void backToAcceptedDefault(CharSequence typedWord) { - if (typedWord == null) return; + if (TextUtils.isEmpty(typedWord)) return; switch (sState) { case SPACE_AFTER_ACCEPTED: case PUNCTUATION_AFTER_ACCEPTED: diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java index 44c89f73c..dfb00c8ab 100644 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ b/java/src/com/android/inputmethod/latin/WordComposer.java @@ -231,9 +231,6 @@ public class WordComposer { * @return the word that was typed so far */ public String getTypedWord() { - if (size() == 0) { - return null; - } return mTypedWord.toString(); } -- cgit v1.2.3-83-g751a From 406d192a9e8f07ed6c6a408650feb0a757ca388e Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Mon, 12 Dec 2011 16:22:07 +0900 Subject: Remove side-effects from TextEntryState Change-Id: Ib98eea9e791e426435cfdfd0c9ab3ed9061664aa --- .../com/android/inputmethod/latin/LatinIME.java | 19 +++++++++++++++-- .../android/inputmethod/latin/TextEntryState.java | 10 --------- java/src/com/android/inputmethod/latin/Utils.java | 24 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 4d60976ea..95fdcc176 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1433,6 +1433,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // TODO: Merge space state with TextEntryState TextEntryState.backspace(); if (null != mWordSavedForAutoCorrectCancellation) { + Utils.Stats.onAutoCorrectionCancellation(); cancelAutoCorrect(ic); mWordSavedForAutoCorrectCancellation = null; ic.endBatchEdit(); @@ -1576,7 +1577,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } switcher.updateShiftState(); - TextEntryState.typedCharacter((char) code, mSettingsValues.isWordSeparator(code), x, y); + if (mSettingsValues.isWordSeparator(code)) { + TextEntryState.typedCharacter((char) code, true, x, y); + Utils.Stats.onSeparator((char)code, x, y); + } else { + TextEntryState.typedCharacter((char) code, false, x, y); + Utils.Stats.onNonSeparator((char)code, x, y); + } if (null != ic) ic.endBatchEdit(); } @@ -1659,6 +1666,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } TextEntryState.typedCharacter((char) primaryCode, true, x, y); + Utils.Stats.onSeparator((char)primaryCode, x, y); if (pickedDefault) { CharSequence typedWord = mWordComposer.getTypedWord(); @@ -1870,7 +1878,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar updateSuggestions(); } if (mBestWord != null && mBestWord.length() > 0) { - TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, separatorCode); + Utils.Stats.onAutoCorrection(mWordComposer.getTypedWord(), mBestWord.toString(), + separatorCode); + TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, + separatorCode); mExpectingUpdateSelection = true; commitBestWord(mBestWord); if (!mBestWord.equals(mWordComposer.getTypedWord())) { @@ -1979,6 +1990,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // TextEntryState.State.PICKED_SUGGESTION state. TextEntryState.typedCharacter((char) Keyboard.CODE_SPACE, true, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); + Utils.Stats.onSeparator((char)Keyboard.CODE_SPACE, WordComposer.NOT_A_COORDINATE, + WordComposer.NOT_A_COORDINATE); if (!showingAddToDictionaryHint) { // If we're not showing the "Touch again to save", then show corrections again. // In case the cursor position doesn't change, make sure we show the suggestions again. @@ -2199,6 +2212,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ic.commitText(separator, 1); TextEntryState.typedCharacter(separator.charAt(0), true, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); + Utils.Stats.onSeparator(separator.charAt(0), WordComposer.NOT_A_COORDINATE, + WordComposer.NOT_A_COORDINATE); mHandler.cancelUpdateBigramPredictions(); mHandler.postUpdateSuggestions(); } diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index 6b44fc500..ebaecf7fd 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -17,7 +17,6 @@ package com.android.inputmethod.latin; import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.Utils.RingCharBuffer; import android.text.TextUtils; import android.util.Log; @@ -48,8 +47,6 @@ public class TextEntryState { int separatorCode) { if (TextUtils.isEmpty(typedWord)) return; setState(ACCEPTED_DEFAULT); - LatinImeLogger.logOnAutoCorrection( - typedWord.toString(), actualWord.toString(), separatorCode); if (DEBUG) displayState("acceptedDefault", "typedWord", typedWord, "actualWord", actualWord); } @@ -131,19 +128,12 @@ public class TextEntryState { } break; } - RingCharBuffer.getInstance().push(c, x, y); - if (isSeparator) { - LatinImeLogger.logOnInputSeparator(); - } else { - LatinImeLogger.logOnInputChar(); - } if (DEBUG) displayState("typedCharacter", "char", c, "isSeparator", isSeparator); } public static void backspace() { if (sState == ACCEPTED_DEFAULT) { setState(UNDO_COMMIT); - LatinImeLogger.logOnAutoCorrectionCancelled(); } else if (sState == UNDO_COMMIT) { setState(IN_WORD); } diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index 75bc10cf3..de29e8f74 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -843,4 +843,28 @@ public class Utils { return !suggestions.mTypedWordValid && suggestions.mHasAutoCorrectionCandidate && !suggestions.shouldBlockAutoCorrection(); } + + public static class Stats { + public static void onNonSeparator(final char code, final int x, + final int y) { + RingCharBuffer.getInstance().push(code, x, y); + LatinImeLogger.logOnInputChar(); + } + + public static void onSeparator(final char code, final int x, + final int y) { + RingCharBuffer.getInstance().push(code, x, y); + LatinImeLogger.logOnInputSeparator(); + } + + public static void onAutoCorrection(final String typedWord, final String correctedWord, + final int separatorCode) { + if (TextUtils.isEmpty(typedWord)) return; + LatinImeLogger.logOnAutoCorrection(typedWord, correctedWord, separatorCode); + } + + public static void onAutoCorrectionCancellation() { + LatinImeLogger.logOnAutoCorrectionCancelled(); + } + } } -- cgit v1.2.3-83-g751a From 7208af0ecdea9eb7de02e62926f0c57c83496f1e Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Mon, 12 Dec 2011 19:25:52 +0900 Subject: Remove a messy and unused class. Today is a happy day Change-Id: I3ef484bd258d5a59815bfcb0bd35ea3606b32430 --- .../com/android/inputmethod/latin/LatinIME.java | 25 --- .../android/inputmethod/latin/TextEntryState.java | 200 --------------------- 2 files changed, 225 deletions(-) delete mode 100644 java/src/com/android/inputmethod/latin/TextEntryState.java (limited to 'java/src/com/android/inputmethod/latin/TextEntryState.java') diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 711afdfdd..a3d3aaa50 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -738,8 +738,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mSubtypeSwitcher.updateParametersOnStartInputView(); - TextEntryState.reset(); - // Most such things we decide below in initializeInputAttributesAndGetMode, but we need to // know now whether this is a password text field, because we need to know now whether we // want to enable the voice button. @@ -932,7 +930,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar && (selectionChanged || candidatesCleared)) { mWordComposer.reset(); mHasUncommittedTypedChars = false; - TextEntryState.reset(); updateSuggestions(); final InputConnection ic = getCurrentInputConnection(); if (ic != null) { @@ -941,7 +938,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mComposingStateManager.onFinishComposingText(); mVoiceProxy.setVoiceInputHighlighted(false); } else if (!mHasUncommittedTypedChars) { - TextEntryState.reset(); updateSuggestions(); } } @@ -1150,7 +1146,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (ic != null) { ic.commitText(typedWord, 1); } - TextEntryState.acceptedTyped(typedWord); addToUserUnigramAndBigramDictionaries(typedWord, UserUnigramDictionary.FREQUENCY_FOR_TYPED); } @@ -1428,8 +1423,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } mHandler.postUpdateShiftKeyState(); - // TODO: Merge space state with TextEntryState - TextEntryState.backspace(); if (null != mWordSavedForAutoCorrectCancellation) { Utils.Stats.onAutoCorrectionCancellation(); cancelAutoCorrect(ic); @@ -1576,10 +1569,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar switcher.updateShiftState(); if (mSettingsValues.isWordSeparator(code)) { - TextEntryState.typedCharacter((char) code, true, x, y); Utils.Stats.onSeparator((char)code, x, y); } else { - TextEntryState.typedCharacter((char) code, false, x, y); Utils.Stats.onNonSeparator((char)code, x, y); } if (null != ic) ic.endBatchEdit(); @@ -1663,12 +1654,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar setPunctuationSuggestions(); } - TextEntryState.typedCharacter((char) primaryCode, true, x, y); Utils.Stats.onSeparator((char)primaryCode, x, y); if (pickedDefault) { CharSequence typedWord = mWordComposer.getTypedWord(); - TextEntryState.backToAcceptedDefault(typedWord); if (!TextUtils.isEmpty(typedWord) && !typedWord.equals(mBestWord)) { InputConnectionCompatUtils.commitCorrection( ic, mLastSelectionEnd - typedWord.length(), typedWord, mBestWord); @@ -1878,8 +1867,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (mBestWord != null && mBestWord.length() > 0) { Utils.Stats.onAutoCorrection(mWordComposer.getTypedWord(), mBestWord.toString(), separatorCode); - TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, - separatorCode); mExpectingUpdateSelection = true; commitBestWord(mBestWord); if (!mBestWord.equals(mWordComposer.getTypedWord())) { @@ -1960,7 +1947,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } LatinImeLogger.logOnManualSuggestion(mWordComposer.getTypedWord().toString(), suggestion.toString(), index, suggestions.mWords); - TextEntryState.acceptedSuggestion(mWordComposer.getTypedWord().toString(), suggestion); // Follow it with a space if (mInsertSpaceOnPickSuggestionManually) { sendMagicSpace(); @@ -1982,11 +1968,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar || !AutoCorrection.isValidWord( mSuggest.getUnigramDictionaries(), suggestion, true)); - // Fool the state watcher so that a subsequent backspace will not do a revert, unless - // we just did a correction, in which case we need to stay in - // TextEntryState.State.PICKED_SUGGESTION state. - TextEntryState.typedCharacter((char) Keyboard.CODE_SPACE, true, - WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); Utils.Stats.onSeparator((char)Keyboard.CODE_SPACE, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); if (!showingAddToDictionaryHint) { @@ -2175,7 +2156,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mBestWord = null; mHasUncommittedTypedChars = true; mComposingStateManager.onStartComposingText(); - TextEntryState.restartSuggestionsOnWordBeforeCursor(); ic.deleteSurroundingText(word.length(), 0); ic.setComposingText(word, 1); mHandler.postUpdateSuggestions(); @@ -2204,10 +2184,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // Re-insert the separator ic.commitText(mWordComposer.getTypedWord(), 1); - TextEntryState.acceptedTyped(mWordComposer.getTypedWord()); ic.commitText(separator, 1); - TextEntryState.typedCharacter(separator.charAt(0), true, - WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); Utils.Stats.onSeparator(separator.charAt(0), WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); mHandler.cancelUpdateBigramPredictions(); @@ -2238,7 +2215,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // the old WordComposer allows to reuse the actual typed coordinates. mHasUncommittedTypedChars = true; ic.setComposingText(mWordComposer.getTypedWord(), 1); - TextEntryState.backspace(); mHandler.cancelUpdateBigramPredictions(); mHandler.postUpdateSuggestions(); } @@ -2521,7 +2497,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar p.println(" mAutoCorrectEnabled=" + mSettingsValues.mAutoCorrectEnabled); p.println(" mInsertSpaceOnPickSuggestionManually=" + mInsertSpaceOnPickSuggestionManually); p.println(" mApplicationSpecifiedCompletionOn=" + mApplicationSpecifiedCompletionOn); - p.println(" TextEntryState.state=" + TextEntryState.getState()); p.println(" mSoundOn=" + mSettingsValues.mSoundOn); p.println(" mVibrateOn=" + mSettingsValues.mVibrateOn); p.println(" mKeyPreviewPopupOn=" + mSettingsValues.mKeyPreviewPopupOn); diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java deleted file mode 100644 index ebaecf7fd..000000000 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.android.inputmethod.latin; - -import com.android.inputmethod.keyboard.Keyboard; - -import android.text.TextUtils; -import android.util.Log; - -public class TextEntryState { - private static final String TAG = TextEntryState.class.getSimpleName(); - private static final boolean DEBUG = false; - - private static final int UNKNOWN = 0; - private static final int START = 1; - private static final int IN_WORD = 2; - private static final int ACCEPTED_DEFAULT = 3; - private static final int PICKED_SUGGESTION = 4; - private static final int PUNCTUATION_AFTER_ACCEPTED = 5; - private static final int SPACE_AFTER_ACCEPTED = 6; - private static final int SPACE_AFTER_PICKED = 7; - private static final int UNDO_COMMIT = 8; - - private static int sState = UNKNOWN; - private static int sPreviousState = UNKNOWN; - - private static void setState(final int newState) { - sPreviousState = sState; - sState = newState; - } - - public static void acceptedDefault(CharSequence typedWord, CharSequence actualWord, - int separatorCode) { - if (TextUtils.isEmpty(typedWord)) return; - setState(ACCEPTED_DEFAULT); - if (DEBUG) - displayState("acceptedDefault", "typedWord", typedWord, "actualWord", actualWord); - } - - // State.ACCEPTED_DEFAULT will be changed to other sub-states - // (see "case ACCEPTED_DEFAULT" in typedCharacter() below), - // and should be restored back to State.ACCEPTED_DEFAULT after processing for each sub-state. - public static void backToAcceptedDefault(CharSequence typedWord) { - if (TextUtils.isEmpty(typedWord)) return; - switch (sState) { - case SPACE_AFTER_ACCEPTED: - case PUNCTUATION_AFTER_ACCEPTED: - case IN_WORD: - setState(ACCEPTED_DEFAULT); - break; - default: - break; - } - if (DEBUG) displayState("backToAcceptedDefault", "typedWord", typedWord); - } - - public static void acceptedTyped(CharSequence typedWord) { - setState(PICKED_SUGGESTION); - if (DEBUG) displayState("acceptedTyped", "typedWord", typedWord); - } - - public static void acceptedSuggestion(CharSequence typedWord, CharSequence actualWord) { - setState(PICKED_SUGGESTION); - if (DEBUG) - displayState("acceptedSuggestion", "typedWord", typedWord, "actualWord", actualWord); - } - - public static void typedCharacter(char c, boolean isSeparator, int x, int y) { - final boolean isSpace = (c == Keyboard.CODE_SPACE); - switch (sState) { - case IN_WORD: - if (isSpace || isSeparator) { - setState(START); - } else { - // State hasn't changed. - } - break; - case ACCEPTED_DEFAULT: - case SPACE_AFTER_PICKED: - case PUNCTUATION_AFTER_ACCEPTED: - if (isSpace) { - setState(SPACE_AFTER_ACCEPTED); - } else if (isSeparator) { - // Swap - setState(PUNCTUATION_AFTER_ACCEPTED); - } else { - setState(IN_WORD); - } - break; - case PICKED_SUGGESTION: - if (isSpace) { - setState(SPACE_AFTER_PICKED); - } else if (isSeparator) { - // Swap - setState(PUNCTUATION_AFTER_ACCEPTED); - } else { - setState(IN_WORD); - } - break; - case START: - case UNKNOWN: - case SPACE_AFTER_ACCEPTED: - if (!isSpace && !isSeparator) { - setState(IN_WORD); - } else { - setState(START); - } - break; - case UNDO_COMMIT: - if (isSpace || isSeparator) { - setState(START); - } else { - setState(IN_WORD); - } - break; - } - if (DEBUG) displayState("typedCharacter", "char", c, "isSeparator", isSeparator); - } - - public static void backspace() { - if (sState == ACCEPTED_DEFAULT) { - setState(UNDO_COMMIT); - } else if (sState == UNDO_COMMIT) { - setState(IN_WORD); - } - // TODO: tidy up this logic. At the moment, for example, writing a word goes to - // ACCEPTED_DEFAULT, backspace will go to UNDO_COMMIT, another backspace will go to IN_WORD, - // and subsequent backspaces will leave the status at IN_WORD, even if the user backspaces - // past the end of the word. We are not in a word any more but the state is still IN_WORD. - if (DEBUG) displayState("backspace"); - } - - public static void restartSuggestionsOnWordBeforeCursor() { - if (UNKNOWN == sState || ACCEPTED_DEFAULT == sState) { - // Here we can come from pretty much any state, except the ones that we can't - // come from after backspace, so supposedly anything except UNKNOWN and - // ACCEPTED_DEFAULT. Note : we could be in UNDO_COMMIT if - // LatinIME#revertLastWord() was calling LatinIME#restartSuggestions...() - Log.e(TAG, "Strange state change : coming from state " + sState); - } - setState(IN_WORD); - } - - public static void reset() { - setState(START); - if (DEBUG) displayState("reset"); - } - - public static boolean isUndoCommit() { - return sState == UNDO_COMMIT; - } - - public static String getState() { - return stateName(sState); - } - - private static String stateName(int state) { - switch (state) { - case START: return "START"; - case IN_WORD: return "IN_WORD"; - case ACCEPTED_DEFAULT: return "ACCEPTED_DEFAULT"; - case PICKED_SUGGESTION: return "PICKED_SUGGESTION"; - case PUNCTUATION_AFTER_ACCEPTED: return "PUNCTUATION_AFTER_ACCEPTED"; - case SPACE_AFTER_ACCEPTED: return "SPACE_AFTER_ACCEPTED"; - case SPACE_AFTER_PICKED: return "SPACE_AFTER_PICKED"; - case UNDO_COMMIT: return "UNDO_COMMIT"; - default: return "UNKNOWN"; - } - } - - private static void displayState(String title, Object ... args) { - final StringBuilder sb = new StringBuilder(title); - sb.append(':'); - for (int i = 0; i < args.length; i += 2) { - sb.append(' '); - sb.append(args[i]); - sb.append('='); - sb.append(args[i+1].toString()); - } - sb.append(" state="); - sb.append(stateName(sState)); - sb.append(" previous="); - sb.append(stateName(sPreviousState)); - Log.d(TAG, sb.toString()); - } -} -- cgit v1.2.3-83-g751a