aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/LatinIME.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinIME.java')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java175
1 files changed, 90 insertions, 85 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 6ea642c92..2d7eed7ab 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 mApplicationSpecifiedCompletionOn;
private WordComposer mWordComposer = new WordComposer();
- private CharSequence mBestWord;
private boolean mHasUncommittedTypedChars;
private int mCorrectionMode;
@@ -1019,7 +1018,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
.setHasMinimalSuggestion(false);
// When in fullscreen mode, show completions generated by the application
setSuggestions(builder.build());
- mBestWord = null;
+ mWordComposer.deleteAutoCorrection();
setSuggestionStripShown(true);
}
}
@@ -1386,13 +1385,20 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private void handleBackspace(final int spaceState) {
if (mVoiceProxy.logAndRevertVoiceInput()) return;
-
final InputConnection ic = getCurrentInputConnection();
if (ic == null) return;
ic.beginBatchEdit();
+ handleBackspaceWhileInBatchEdit(spaceState, ic);
+ ic.endBatchEdit();
+ }
+ // "ic" may not be null.
+ private void handleBackspaceWhileInBatchEdit(final int spaceState, final InputConnection ic) {
mVoiceProxy.handleBackspace();
+ // In many cases, we may have to put the keyboard in auto-shift state again.
+ mHandler.postUpdateShiftKeyState();
+
if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) {
// Cancel multi-character input: remove the text we just entered.
// This is triggered on backspace after a key that inputs multiple characters,
@@ -1401,27 +1407,18 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// If we have mEnteredText, then we know that mHasUncommittedTypedChars == false.
// In addition we know that spaceState is false, and that we should not be
// reverting any autocorrect at this point. So we can safely return.
- ic.endBatchEdit();
return;
}
- final boolean deleteChar = !mHasUncommittedTypedChars;
if (mHasUncommittedTypedChars) {
final int length = mWordComposer.size();
if (length > 0) {
mWordComposer.deleteLast();
- final CharSequence textWithUnderline =
- mComposingStateManager.isAutoCorrectionIndicatorOn()
- ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
- this, mWordComposer.getTypedWord())
- : mWordComposer.getTypedWord();
- ic.setComposingText(textWithUnderline, 1);
+ ic.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1);
if (mWordComposer.size() == 0) {
mHasUncommittedTypedChars = false;
- }
- if (1 == length) {
- // 1 == length means we are about to erase the last character of the word,
- // so we can show bigrams.
+ // Remaining size equals zero means we just erased the last character of the
+ // word, so we can show bigrams.
mHandler.postUpdateBigramPredictions();
} else {
// length > 1, so we still have letters to deduce a suggestion from.
@@ -1430,42 +1427,29 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} else {
ic.deleteSurroundingText(1, 0);
}
- // If we deleted the last remaining char of a word, we may have to put the keyboard
- // in auto-shift state again.
- mHandler.postUpdateShiftKeyState();
- // If we had uncommitted chars then we know it's not time to revert any auto-correct
- // and that spaceState is NONE.
- ic.endBatchEdit();
- return;
- }
- mHandler.postUpdateShiftKeyState();
-
- if (null != mWordSavedForAutoCorrectCancellation) {
- Utils.Stats.onAutoCorrectionCancellation();
- cancelAutoCorrect(ic);
- mWordSavedForAutoCorrectCancellation = null;
- ic.endBatchEdit();
- return;
} else {
- mWordSavedForAutoCorrectCancellation = null;
- }
-
- if (SPACE_STATE_DOUBLE == spaceState) {
- if (revertDoubleSpace(ic)) {
- ic.endBatchEdit();
- // No need to reset mSpaceState, it has already be done (that's why we
- // receive it as a parameter)
+ if (null != mWordSavedForAutoCorrectCancellation) {
+ Utils.Stats.onAutoCorrectionCancellation();
+ cancelAutoCorrect(ic);
+ mWordSavedForAutoCorrectCancellation = null;
return;
+ } else {
+ mWordSavedForAutoCorrectCancellation = null;
}
- } else if (SPACE_STATE_SWAP_PUNCTUATION == spaceState) {
- if (revertSwapPunctuation(ic)) {
- ic.endBatchEdit();
- // Likewise
- return;
+
+ if (SPACE_STATE_DOUBLE == spaceState) {
+ if (revertDoubleSpace(ic)) {
+ // No need to reset mSpaceState, it has already be done (that's why we
+ // receive it as a parameter)
+ return;
+ }
+ } else if (SPACE_STATE_SWAP_PUNCTUATION == spaceState) {
+ if (revertSwapPunctuation(ic)) {
+ // Likewise
+ return;
+ }
}
- }
- if (deleteChar) {
if (mSuggestionsView != null && mSuggestionsView.dismissAddToDictionaryHint()) {
// Go back to the suggestion mode if the user canceled the
// "Touch again to save".
@@ -1483,7 +1467,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
restartSuggestionsOnWordBeforeCursorIfAtEndOfWord(ic);
}
}
- ic.endBatchEdit();
}
private void handleTab() {
@@ -1512,12 +1495,19 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private void handleCharacter(final int primaryCode, final int[] keyCodes, final int x,
final int y, final int spaceState) {
mVoiceProxy.handleCharacter();
-
final InputConnection ic = getCurrentInputConnection();
- if (ic != null) ic.beginBatchEdit();
+ if (null != ic) ic.beginBatchEdit();
+ // TODO: if ic is null, does it make any sense to call this?
+ handleCharacterWhileInBatchEdit(primaryCode, keyCodes, x, y, spaceState, ic);
+ if (null != ic) ic.endBatchEdit();
+ }
+
+ // "ic" may be null without this crashing, but the behavior will be really strange
+ private void handleCharacterWhileInBatchEdit(final int primaryCode, final int[] keyCodes,
+ final int x, final int y, final int spaceState, final InputConnection ic) {
if (SPACE_STATE_MAGIC == spaceState
&& mSettingsValues.isMagicSpaceStripper(primaryCode)) {
- removeTrailingSpaceWhileInBatchEdit(ic);
+ if (null != ic) removeTrailingSpaceWhileInBatchEdit(ic);
}
int code = primaryCode;
@@ -1536,7 +1526,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (switcher.isShiftedOrShiftLocked()) {
if (keyCodes == null || keyCodes[0] < Character.MIN_CODE_POINT
|| keyCodes[0] > Character.MAX_CODE_POINT) {
- if (null != ic) ic.endBatchEdit();
return;
}
code = keyCodes[0];
@@ -1550,7 +1539,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} else {
// Some keys, such as [eszett], have upper case as multi-characters.
onTextInput(upperCaseString);
- if (null != ic) ic.endBatchEdit();
return;
}
}
@@ -1563,12 +1551,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mWordComposer.setAutoCapitalized(getCurrentAutoCapsState());
mComposingStateManager.onStartComposingText();
}
- final CharSequence textWithUnderline =
- mComposingStateManager.isAutoCorrectionIndicatorOn()
- ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
- this, mWordComposer.getTypedWord())
- : mWordComposer.getTypedWord();
- ic.setComposingText(textWithUnderline, 1);
+ ic.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1);
}
mHandler.postUpdateSuggestions();
} else {
@@ -1585,7 +1568,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} else {
Utils.Stats.onNonSeparator((char)code, x, y);
}
- if (null != ic) ic.endBatchEdit();
}
private void handleSeparator(final int primaryCode, final int x, final int y,
@@ -1670,10 +1652,18 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
Utils.Stats.onSeparator((char)primaryCode, x, y);
if (pickedDefault) {
- CharSequence typedWord = mWordComposer.getTypedWord();
- if (!TextUtils.isEmpty(typedWord) && !typedWord.equals(mBestWord)) {
+ final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
+ 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(
- ic, mLastSelectionEnd - typedWord.length(), typedWord, mBestWord);
+ ic, mLastSelectionEnd - typedWord.length(), typedWord, autoCorrection);
}
}
mKeyboardSwitcher.updateShiftState();
@@ -1682,6 +1672,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
}
}
+ private CharSequence getTextWithUnderline(final CharSequence text) {
+ return mComposingStateManager.isAutoCorrectionIndicatorOn()
+ ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(this, text)
+ : mWordComposer.getTypedWord();
+ }
+
private void handleClose() {
commitTyped(getCurrentInputConnection());
mVoiceProxy.handleClose();
@@ -1755,18 +1751,21 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mComposingStateManager.isAutoCorrectionIndicatorOn();
final boolean newAutoCorrectionIndicator = Utils.willAutoCorrect(words);
if (oldAutoCorrectionIndicator != newAutoCorrectionIndicator) {
- if (LatinImeLogger.sDBG) {
+ mComposingStateManager.setAutoCorrectionIndicatorOn(newAutoCorrectionIndicator);
+ if (DEBUG) {
Log.d(TAG, "Flip the indicator. " + oldAutoCorrectionIndicator
+ " -> " + newAutoCorrectionIndicator);
+ if (newAutoCorrectionIndicator
+ != mComposingStateManager.isAutoCorrectionIndicatorOn()) {
+ throw new RuntimeException("Couldn't flip the indicator! We are not "
+ + "composing a word right now.");
+ }
}
- final CharSequence textWithUnderline = newAutoCorrectionIndicator
- ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
- this, mWordComposer.getTypedWord())
- : mWordComposer.getTypedWord();
+ final CharSequence textWithUnderline =
+ getTextWithUnderline(mWordComposer.getTypedWord());
if (!TextUtils.isEmpty(textWithUnderline)) {
ic.setComposingText(textWithUnderline, 1);
}
- mComposingStateManager.setAutoCorrectionIndicatorOn(newAutoCorrectionIndicator);
}
}
}
@@ -1859,14 +1858,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
setSuggestions(suggestedWords);
if (suggestedWords.size() > 0) {
if (shouldBlockAutoCorrectionBySafetyNet) {
- mBestWord = typedWord;
+ mWordComposer.setAutoCorrection(typedWord);
} else if (suggestedWords.hasAutoCorrectionWord()) {
- mBestWord = suggestedWords.getWord(1);
+ mWordComposer.setAutoCorrection(suggestedWords.getWord(1));
} else {
- mBestWord = typedWord;
+ mWordComposer.setAutoCorrection(typedWord);
}
} else {
- mBestWord = null;
+ // TODO: replace with mWordComposer.deleteAutoCorrection()?
+ mWordComposer.setAutoCorrection(null);
}
setSuggestionStripShown(isSuggestionsStripVisible());
}
@@ -1877,16 +1877,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mHandler.cancelUpdateSuggestions();
updateSuggestions();
}
- if (mBestWord != null && mBestWord.length() > 0) {
- Utils.Stats.onAutoCorrection(mWordComposer.getTypedWord(), mBestWord.toString(),
- separatorCode);
+ final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
+ if (autoCorrection != null) {
+ final String typedWord = mWordComposer.getTypedWord();
+ Utils.Stats.onAutoCorrection(typedWord, autoCorrection.toString(), separatorCode);
mExpectingUpdateSelection = true;
- commitBestWord(mBestWord);
- if (!mBestWord.equals(mWordComposer.getTypedWord())) {
- mWordSavedForAutoCorrectCancellation = mBestWord.toString();
+ commitBestWord(autoCorrection);
+ if (!autoCorrection.equals(typedWord)) {
+ mWordSavedForAutoCorrectCancellation = autoCorrection.toString();
}
// Add the word to the user unigram dictionary if it's not a known word
- addToUserUnigramAndBigramDictionaries(mBestWord,
+ addToUserUnigramAndBigramDictionaries(autoCorrection,
UserUnigramDictionary.FREQUENCY_FOR_TYPED);
return true;
}
@@ -2165,8 +2166,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic,
final CharSequence word) {
mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard());
- // mBestWord will be set appropriately by updateSuggestions() called by the handler
- mBestWord = null;
mHasUncommittedTypedChars = true;
mComposingStateManager.onStartComposingText();
ic.deleteSurroundingText(word.length(), 0);
@@ -2237,8 +2236,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// Here we test whether we indeed have a period and a space before us. This should not
// be needed, but it's there just in case something went wrong.
final CharSequence textBeforeCursor = ic.getTextBeforeCursor(2, 0);
- if (!". ".equals(textBeforeCursor))
- return false;
+ if (!". ".equals(textBeforeCursor)) {
+ // We should not have come here if we aren't just after a ". ".
+ throw new RuntimeException("Tried to revert double-space combo but we didn't find "
+ + "\". \" just before the cursor.");
+ }
ic.beginBatchEdit();
ic.deleteSurroundingText(2, 0);
ic.commitText(" ", 1);
@@ -2252,8 +2254,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final CharSequence textBeforeCursor = ic.getTextBeforeCursor(2, 0);
// NOTE: This does not work with surrogate pairs. Hopefully when the keyboard is able to
// enter surrogate pairs this code will have been removed.
- if (Keyboard.CODE_SPACE != textBeforeCursor.charAt(1))
- return false;
+ if (Keyboard.CODE_SPACE != textBeforeCursor.charAt(1)) {
+ // We should not have come here if the text before the cursor is not a space.
+ throw new RuntimeException("Tried to revert a swap of punctiation but we didn't "
+ + "find a space just before the cursor.");
+ }
ic.beginBatchEdit();
ic.deleteSurroundingText(2, 0);
ic.commitText(" " + textBeforeCursor.subSequence(0, 1), 1);