Move mBestWord to the word composer.

mBestWord has a confusing name - it's actually an auto-correction.
It's cleaner if it lives in the word composer because an
auto-correction should be tied to a specific user input, and
should be reset each time the user input changes to avoid
race conditions.

Change-Id: I718d29395bc747372067e6440e090c6a181994ae
main
Jean Chalard 2011-12-13 19:38:36 +09:00
parent 7e6f4daa19
commit 117fc93f37
2 changed files with 55 additions and 19 deletions

View File

@ -203,7 +203,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private boolean mApplicationSpecifiedCompletionOn; private boolean mApplicationSpecifiedCompletionOn;
private WordComposer mWordComposer = new WordComposer(); private WordComposer mWordComposer = new WordComposer();
private CharSequence mBestWord;
private boolean mHasUncommittedTypedChars; private boolean mHasUncommittedTypedChars;
private int mCorrectionMode; private int mCorrectionMode;
@ -1019,7 +1018,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
.setHasMinimalSuggestion(false); .setHasMinimalSuggestion(false);
// When in fullscreen mode, show completions generated by the application // When in fullscreen mode, show completions generated by the application
setSuggestions(builder.build()); setSuggestions(builder.build());
mBestWord = null; mWordComposer.deleteAutoCorrection();
setSuggestionStripShown(true); setSuggestionStripShown(true);
} }
} }
@ -1649,10 +1648,18 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
Utils.Stats.onSeparator((char)primaryCode, x, y); Utils.Stats.onSeparator((char)primaryCode, x, y);
if (pickedDefault) { if (pickedDefault) {
CharSequence typedWord = mWordComposer.getTypedWord(); final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
if (!TextUtils.isEmpty(typedWord) && !typedWord.equals(mBestWord)) { final String typedWord = mWordComposer.getTypedWord();
if (TextUtils.isEmpty(typedWord)) {
throw new RuntimeException("We have non-committed chars but the typed word "
+ "is empty? Impossible! I must commit suicide.");
}
if (!typedWord.equals(autoCorrection)) {
// TODO: if the commitCorrection method is not supported by the platform
// this will do nothing and the correction will not be committed at all. What
// happens on Froyo/Gingerbread, where this API is not present?
InputConnectionCompatUtils.commitCorrection( InputConnectionCompatUtils.commitCorrection(
ic, mLastSelectionEnd - typedWord.length(), typedWord, mBestWord); ic, mLastSelectionEnd - typedWord.length(), typedWord, autoCorrection);
} }
} }
mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.updateShiftState();
@ -1847,14 +1854,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
setSuggestions(suggestedWords); setSuggestions(suggestedWords);
if (suggestedWords.size() > 0) { if (suggestedWords.size() > 0) {
if (shouldBlockAutoCorrectionBySafetyNet) { if (shouldBlockAutoCorrectionBySafetyNet) {
mBestWord = typedWord; mWordComposer.setAutoCorrection(typedWord);
} else if (suggestedWords.hasAutoCorrectionWord()) { } else if (suggestedWords.hasAutoCorrectionWord()) {
mBestWord = suggestedWords.getWord(1); mWordComposer.setAutoCorrection(suggestedWords.getWord(1));
} else { } else {
mBestWord = typedWord; mWordComposer.setAutoCorrection(typedWord);
} }
} else { } else {
mBestWord = null; // TODO: replace with mWordComposer.deleteAutoCorrection()?
mWordComposer.setAutoCorrection(null);
} }
setSuggestionStripShown(isSuggestionsStripVisible()); setSuggestionStripShown(isSuggestionsStripVisible());
} }
@ -1865,16 +1873,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mHandler.cancelUpdateSuggestions(); mHandler.cancelUpdateSuggestions();
updateSuggestions(); updateSuggestions();
} }
if (mBestWord != null && mBestWord.length() > 0) { final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
Utils.Stats.onAutoCorrection(mWordComposer.getTypedWord(), mBestWord.toString(), if (autoCorrection != null) {
separatorCode); final String typedWord = mWordComposer.getTypedWord();
Utils.Stats.onAutoCorrection(typedWord, autoCorrection.toString(), separatorCode);
mExpectingUpdateSelection = true; mExpectingUpdateSelection = true;
commitBestWord(mBestWord); commitBestWord(autoCorrection);
if (!mBestWord.equals(mWordComposer.getTypedWord())) { if (!autoCorrection.equals(typedWord)) {
mWordSavedForAutoCorrectCancellation = mBestWord.toString(); mWordSavedForAutoCorrectCancellation = autoCorrection.toString();
} }
// Add the word to the user unigram dictionary if it's not a known word // Add the word to the user unigram dictionary if it's not a known word
addToUserUnigramAndBigramDictionaries(mBestWord, addToUserUnigramAndBigramDictionaries(autoCorrection,
UserUnigramDictionary.FREQUENCY_FOR_TYPED); UserUnigramDictionary.FREQUENCY_FOR_TYPED);
return true; return true;
} }
@ -2153,8 +2162,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic, private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic,
final CharSequence word) { final CharSequence word) {
mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard()); mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard());
// mBestWord will be set appropriately by updateSuggestions() called by the handler
mBestWord = null;
mHasUncommittedTypedChars = true; mHasUncommittedTypedChars = true;
mComposingStateManager.onStartComposingText(); mComposingStateManager.onStartComposingText();
ic.deleteSurroundingText(word.length(), 0); ic.deleteSurroundingText(word.length(), 0);

View File

@ -41,6 +41,8 @@ public class WordComposer {
private int[] mYCoordinates; private int[] mYCoordinates;
private StringBuilder mTypedWord; private StringBuilder mTypedWord;
// An auto-correction for this word out of the dictionary.
private CharSequence mAutoCorrection;
private int mCapsCount; private int mCapsCount;
@ -60,6 +62,7 @@ public class WordComposer {
mXCoordinates = new int[N]; mXCoordinates = new int[N];
mYCoordinates = new int[N]; mYCoordinates = new int[N];
mTrailingSingleQuotesCount = 0; mTrailingSingleQuotesCount = 0;
mAutoCorrection = null;
} }
public WordComposer(WordComposer source) { public WordComposer(WordComposer source) {
@ -75,6 +78,7 @@ public class WordComposer {
mIsFirstCharCapitalized = source.mIsFirstCharCapitalized; mIsFirstCharCapitalized = source.mIsFirstCharCapitalized;
mAutoCapitalized = source.mAutoCapitalized; mAutoCapitalized = source.mAutoCapitalized;
mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount; mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount;
mAutoCorrection = null;
} }
/** /**
@ -86,6 +90,7 @@ public class WordComposer {
mCapsCount = 0; mCapsCount = 0;
mIsFirstCharCapitalized = false; mIsFirstCharCapitalized = false;
mTrailingSingleQuotesCount = 0; mTrailingSingleQuotesCount = 0;
mAutoCorrection = null;
} }
/** /**
@ -140,6 +145,7 @@ public class WordComposer {
} else { } else {
mTrailingSingleQuotesCount = 0; mTrailingSingleQuotesCount = 0;
} }
mAutoCorrection = null;
} }
/** /**
@ -173,6 +179,7 @@ public class WordComposer {
int codePoint = word.charAt(i); int codePoint = word.charAt(i);
addKeyInfo(codePoint, keyboard, keyDetector); addKeyInfo(codePoint, keyboard, keyDetector);
} }
mAutoCorrection = null;
} }
/** /**
@ -224,11 +231,12 @@ public class WordComposer {
++mTrailingSingleQuotesCount; ++mTrailingSingleQuotesCount;
} }
} }
mAutoCorrection = null;
} }
/** /**
* Returns the word as it was typed, without any correction applied. * Returns the word as it was typed, without any correction applied.
* @return the word that was typed so far * @return the word that was typed so far. Never returns null.
*/ */
public String getTypedWord() { public String getTypedWord() {
return mTypedWord.toString(); return mTypedWord.toString();
@ -277,4 +285,25 @@ public class WordComposer {
public boolean isAutoCapitalized() { public boolean isAutoCapitalized() {
return mAutoCapitalized; return mAutoCapitalized;
} }
/**
* Sets the auto-correction for this word.
*/
public void setAutoCorrection(final CharSequence correction) {
mAutoCorrection = correction;
}
/**
* Remove any auto-correction that may have been set.
*/
public void deleteAutoCorrection() {
mAutoCorrection = null;
}
/**
* @return the auto-correction for this world, or null if none.
*/
public CharSequence getAutoCorrectionOrNull() {
return mAutoCorrection;
}
} }