Merge "Enable reverting the auto-conversion of double space."

main
Jean Chalard 2011-06-08 22:39:49 -07:00 committed by Android (Google) Code Review
commit cb6d82da44
1 changed files with 52 additions and 9 deletions

View File

@ -173,6 +173,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// Magic space: a space that should disappear on space/apostrophe insertion, move after the // Magic space: a space that should disappear on space/apostrophe insertion, move after the
// punctuation on punctuation insertion, and become a real space on alpha char insertion. // punctuation on punctuation insertion, and become a real space on alpha char insertion.
private boolean mJustAddedMagicSpace; // This indicates whether the last char is a magic space. private boolean mJustAddedMagicSpace; // This indicates whether the last char is a magic space.
// This indicates whether the last keypress resulted in processing of double space replacement
// with period-space.
private boolean mJustReplacedDoubleSpace;
private int mCorrectionMode; private int mCorrectionMode;
private int mCommittedLength; private int mCommittedLength;
@ -181,8 +184,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private int mLastSelectionStart; private int mLastSelectionStart;
private int mLastSelectionEnd; private int mLastSelectionEnd;
// Indicates whether the suggestion strip is to be on in landscape // Whether we are expecting an onUpdateSelection event to fire. If it does when we don't
private boolean mJustAccepted; // "expect" it, it means the user actually moved the cursor.
private boolean mExpectingUpdateSelection;
private int mDeleteCount; private int mDeleteCount;
private long mLastKeyTime; private long mLastKeyTime;
@ -535,6 +539,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mHasUncommittedTypedChars = false; mHasUncommittedTypedChars = false;
mDeleteCount = 0; mDeleteCount = 0;
mJustAddedMagicSpace = false; mJustAddedMagicSpace = false;
mJustReplacedDoubleSpace = false;
loadSettings(); loadSettings();
updateCorrectionMode(); updateCorrectionMode();
@ -714,14 +719,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
ic.finishComposingText(); ic.finishComposingText();
} }
mVoiceProxy.setVoiceInputHighlighted(false); mVoiceProxy.setVoiceInputHighlighted(false);
} else if (!mHasUncommittedTypedChars && !mJustAccepted) { } else if (!mHasUncommittedTypedChars && !mExpectingUpdateSelection) {
if (TextEntryState.isAcceptedDefault() || TextEntryState.isSpaceAfterPicked()) { if (TextEntryState.isAcceptedDefault() || TextEntryState.isSpaceAfterPicked()) {
if (TextEntryState.isAcceptedDefault()) if (TextEntryState.isAcceptedDefault())
TextEntryState.reset(); TextEntryState.reset();
}
}
if (!mExpectingUpdateSelection) {
mJustAddedMagicSpace = false; // The user moved the cursor. mJustAddedMagicSpace = false; // The user moved the cursor.
mJustReplacedDoubleSpace = false;
} }
} mExpectingUpdateSelection = false;
mJustAccepted = false;
mHandler.postUpdateShiftKeyState(); mHandler.postUpdateShiftKeyState();
// Make a note of the cursor position // Make a note of the cursor position
@ -971,6 +979,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
ic.commitText(". ", 1); ic.commitText(". ", 1);
ic.endBatchEdit(); ic.endBatchEdit();
mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.updateShiftState();
mJustReplacedDoubleSpace = true;
} else { } else {
mHandler.startDoubleSpacesTimer(); mHandler.startDoubleSpacesTimer();
} }
@ -1054,10 +1063,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mLastKeyTime = when; mLastKeyTime = when;
KeyboardSwitcher switcher = mKeyboardSwitcher; KeyboardSwitcher switcher = mKeyboardSwitcher;
final boolean distinctMultiTouch = switcher.hasDistinctMultitouch(); final boolean distinctMultiTouch = switcher.hasDistinctMultitouch();
final boolean lastStateOfJustReplacedDoubleSpace = mJustReplacedDoubleSpace;
mJustReplacedDoubleSpace = false;
switch (primaryCode) { switch (primaryCode) {
case Keyboard.CODE_DELETE: case Keyboard.CODE_DELETE:
handleBackspace(); handleBackspace(lastStateOfJustReplacedDoubleSpace);
mDeleteCount++; mDeleteCount++;
mExpectingUpdateSelection = true;
LatinImeLogger.logOnDelete(); LatinImeLogger.logOnDelete();
break; break;
case Keyboard.CODE_SHIFT: case Keyboard.CODE_SHIFT:
@ -1095,6 +1107,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
break; break;
case Keyboard.CODE_TAB: case Keyboard.CODE_TAB:
handleTab(); handleTab();
// There are two cases for tab. Either we send a "next" event, that may change the
// focus but will never move the cursor. Or, we send a real tab keycode, which some
// applications may accept or ignore, and we don't know whether this will move the
// cursor or not. So actually, we don't really know.
// So to go with the safer option, we'd rather behave as if the user moved the
// cursor when they didn't than the opposite. We also expect that most applications
// will actually use tab only for focus movement.
// To sum it up: do not update mExpectingUpdateSelection here.
break; break;
default: default:
if (mSettingsValues.isWordSeparator(primaryCode)) { if (mSettingsValues.isWordSeparator(primaryCode)) {
@ -1102,6 +1122,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} else { } else {
handleCharacter(primaryCode, keyCodes, x, y); handleCharacter(primaryCode, keyCodes, x, y);
} }
mExpectingUpdateSelection = true;
break;
} }
switcher.onKey(primaryCode); switcher.onKey(primaryCode);
// Reset after any single keystroke // Reset after any single keystroke
@ -1131,7 +1153,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mKeyboardSwitcher.onCancelInput(); mKeyboardSwitcher.onCancelInput();
} }
private void handleBackspace() { private void handleBackspace(boolean justReplacedDoubleSpace) {
if (mVoiceProxy.logAndRevertVoiceInput()) return; if (mVoiceProxy.logAndRevertVoiceInput()) return;
final InputConnection ic = getCurrentInputConnection(); final InputConnection ic = getCurrentInputConnection();
@ -1172,6 +1194,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
ic.endBatchEdit(); ic.endBatchEdit();
return; return;
} }
if (justReplacedDoubleSpace) {
if (revertDoubleSpace()) {
ic.endBatchEdit();
return;
}
}
if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) { if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) {
ic.deleteSurroundingText(mEnteredText.length(), 0); ic.deleteSurroundingText(mEnteredText.length(), 0);
@ -1516,7 +1544,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
if (mBestWord != null && mBestWord.length() > 0) { if (mBestWord != null && mBestWord.length() > 0) {
TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode); TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode);
mJustAccepted = true; mExpectingUpdateSelection = true;
commitBestWord(mBestWord); commitBestWord(mBestWord);
// Add the word to the auto dictionary if it's not a known word // Add the word to the auto dictionary if it's not a known word
addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED); addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED);
@ -1584,7 +1612,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// context - no user input. We should reset the word composer. // context - no user input. We should reset the word composer.
mWord.reset(); mWord.reset();
} }
mJustAccepted = true; mExpectingUpdateSelection = true;
commitBestWord(suggestion); commitBestWord(suggestion);
// Add the word to the auto dictionary if it's not a known word // Add the word to the auto dictionary if it's not a known word
if (index == 0) { if (index == 0) {
@ -1794,6 +1822,21 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
} }
public boolean revertDoubleSpace() {
mHandler.cancelDoubleSpacesTimer();
final InputConnection ic = getCurrentInputConnection();
// 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;
ic.beginBatchEdit();
ic.deleteSurroundingText(2, 0);
ic.commitText(" ", 1);
ic.endBatchEdit();
return true;
}
public boolean isWordSeparator(int code) { public boolean isWordSeparator(int code) {
return mSettingsValues.isWordSeparator(code); return mSettingsValues.isWordSeparator(code);
} }