Merge "KeyboardState keeps track of isAlphabetMode and isSymbolShifted"

This commit is contained in:
Tadashi G. Takaoka 2011-12-09 00:43:18 -08:00 committed by Android (Google) Code Review
commit 1cc046775d
2 changed files with 70 additions and 71 deletions

View file

@ -147,7 +147,7 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
public void saveKeyboardState() { public void saveKeyboardState() {
if (mCurrentId != null) { if (mCurrentId != null) {
mState.onSaveKeyboardState(isAlphabetMode(), isSymbolShifted()); mState.onSaveKeyboardState();
} }
} }
@ -278,9 +278,8 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
return mCurrentId != null ? mCurrentId.mMode : KeyboardId.MODE_TEXT; return mCurrentId != null ? mCurrentId.mMode : KeyboardId.MODE_TEXT;
} }
// TODO: Delegate to KeyboardState
public boolean isAlphabetMode() { public boolean isAlphabetMode() {
return mCurrentId != null && mCurrentId.isAlphabetKeyboard(); return mState.isAlphabetMode();
} }
public boolean isInputViewShown() { public boolean isInputViewShown() {
@ -354,44 +353,44 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
* Toggle keyboard shift state triggered by user touch event. * Toggle keyboard shift state triggered by user touch event.
*/ */
public void toggleShift() { public void toggleShift() {
mState.onToggleShift(isAlphabetMode(), isSymbolShifted()); mState.onToggleShift();
} }
/** /**
* Toggle caps lock state triggered by user touch event. * Toggle caps lock state triggered by user touch event.
*/ */
public void toggleCapsLock() { public void toggleCapsLock() {
mState.onToggleCapsLock(isAlphabetMode()); mState.onToggleCapsLock();
} }
/** /**
* Toggle between alphabet and symbols modes triggered by user touch event. * Toggle between alphabet and symbols modes triggered by user touch event.
*/ */
public void toggleAlphabetAndSymbols() { public void toggleAlphabetAndSymbols() {
mState.onToggleAlphabetAndSymbols(isAlphabetMode()); mState.onToggleAlphabetAndSymbols();
} }
/** /**
* Update keyboard shift state triggered by connected EditText status change. * Update keyboard shift state triggered by connected EditText status change.
*/ */
public void updateShiftState() { public void updateShiftState() {
mState.onUpdateShiftState(isAlphabetMode(), mInputMethodService.getCurrentAutoCapsState()); mState.onUpdateShiftState(mInputMethodService.getCurrentAutoCapsState());
} }
public void onPressShift(boolean withSliding) { public void onPressShift(boolean withSliding) {
mState.onPressShift(isAlphabetMode(), isSymbolShifted(), withSliding); mState.onPressShift(withSliding);
} }
public void onReleaseShift(boolean withSliding) { public void onReleaseShift(boolean withSliding) {
mState.onReleaseShift(isAlphabetMode(), isSymbolShifted(), withSliding); mState.onReleaseShift(withSliding);
} }
public void onPressSymbol() { public void onPressSymbol() {
mState.onPressSymbol(isAlphabetMode()); mState.onPressSymbol();
} }
public void onReleaseSymbol() { public void onReleaseSymbol() {
mState.onReleaseSymbol(isAlphabetMode()); mState.onReleaseSymbol();
} }
public void onOtherKeyPressed() { public void onOtherKeyPressed() {
@ -399,7 +398,7 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
} }
public void onCancelInput() { public void onCancelInput() {
mState.onCancelInput(isAlphabetMode(), isSymbolShifted(), isSinglePointer()); mState.onCancelInput(isSinglePointer());
} }
// Implements {@link KeyboardState.SwitchActions}. // Implements {@link KeyboardState.SwitchActions}.
@ -414,11 +413,6 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
setKeyboard(getKeyboard(mMainKeyboardId)); setKeyboard(getKeyboard(mMainKeyboardId));
} }
// TODO: Remove this method
private boolean isSymbolShifted() {
return mCurrentId != null && mCurrentId.equals(mSymbolsShiftedKeyboardId);
}
// Implements {@link KeyboardState.SwitchActions}. // Implements {@link KeyboardState.SwitchActions}.
@Override @Override
public void setSymbolsShiftedKeyboard() { public void setSymbolsShiftedKeyboard() {
@ -445,7 +439,7 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
* Updates state machine to figure out when to automatically snap back to the previous mode. * Updates state machine to figure out when to automatically snap back to the previous mode.
*/ */
public void onCodeInput(int code) { public void onCodeInput(int code) {
mState.onCodeInput(isAlphabetMode(), isSymbolShifted(), code, isSinglePointer()); mState.onCodeInput(code, isSinglePointer());
} }
public LatinKeyboardView getKeyboardView() { public LatinKeyboardView getKeyboardView() {
@ -495,13 +489,14 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
} }
private void postSetInputView(final View newInputView) { private void postSetInputView(final View newInputView) {
mInputMethodService.mHandler.post(new Runnable() { final LatinIME latinIme = mInputMethodService;
latinIme.mHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (newInputView != null) { if (newInputView != null) {
mInputMethodService.setInputView(newInputView); latinIme.setInputView(newInputView);
} }
mInputMethodService.updateInputViewShown(); latinIme.updateInputViewShown();
} }
}); });
} }

View file

@ -26,7 +26,12 @@ import com.android.inputmethod.keyboard.Keyboard;
* Keyboard state machine. * Keyboard state machine.
* *
* This class contains all keyboard state transition logic. * This class contains all keyboard state transition logic.
* TODO: List up input events and actions. * The input events are {@link #onLoadKeyboard(String, boolean)}, {@link #onSaveKeyboardState()},
* {@link #onPressShift(boolean)}, {@link #onReleaseShift(boolean)}, {@link #onPressSymbol()},
* {@link #onReleaseSymbol()}, {@link #onOtherKeyPressed()}, {@link #onCodeInput(int, boolean)},
* {@link #onCancelInput(boolean)}, {@link #onUpdateShiftState(boolean)}, {@link #onToggleShift()},
* {@link #onToggleCapsLock()}, and {@link #onToggleAlphabetAndSymbols()}.
* The actions are {@link SwitchActions}'s methods.
*/ */
public class KeyboardState { public class KeyboardState {
private static final String TAG = KeyboardState.class.getSimpleName(); private static final String TAG = KeyboardState.class.getSimpleName();
@ -63,10 +68,13 @@ public class KeyboardState {
private final SwitchActions mSwitchActions; private final SwitchActions mSwitchActions;
private boolean mIsAlphabetMode;
private boolean mIsSymbolShifted;
private final SavedKeyboardState mSavedKeyboardState = new SavedKeyboardState(); private final SavedKeyboardState mSavedKeyboardState = new SavedKeyboardState();
private boolean mPrevMainKeyboardWasShiftLocked; private boolean mPrevMainKeyboardWasShiftLocked;
private static class SavedKeyboardState { static class SavedKeyboardState {
public boolean mIsValid; public boolean mIsValid;
public boolean mIsAlphabetMode; public boolean mIsAlphabetMode;
public boolean mIsShiftLocked; public boolean mIsShiftLocked;
@ -91,16 +99,15 @@ public class KeyboardState {
onRestoreKeyboardState(); onRestoreKeyboardState();
} }
// TODO: Get rid of isAlphabetMode and isSymbolShifted arguments. public void onSaveKeyboardState() {
public void onSaveKeyboardState(boolean isAlphabetMode, boolean isSymbolShifted) {
final SavedKeyboardState state = mSavedKeyboardState; final SavedKeyboardState state = mSavedKeyboardState;
state.mIsAlphabetMode = isAlphabetMode; state.mIsAlphabetMode = mIsAlphabetMode;
if (isAlphabetMode) { if (mIsAlphabetMode) {
state.mIsShiftLocked = isShiftLocked(); state.mIsShiftLocked = isShiftLocked();
state.mIsShifted = !state.mIsShiftLocked && isShiftedOrShiftLocked(); state.mIsShifted = !state.mIsShiftLocked && isShiftedOrShiftLocked();
} else { } else {
state.mIsShiftLocked = false; state.mIsShiftLocked = false;
state.mIsShifted = isSymbolShifted; state.mIsShifted = mIsSymbolShifted;
} }
state.mIsValid = true; state.mIsValid = true;
if (DEBUG_STATE) { if (DEBUG_STATE) {
@ -137,6 +144,10 @@ public class KeyboardState {
} }
} }
public boolean isAlphabetMode() {
return mIsAlphabetMode;
}
public boolean isShiftLocked() { public boolean isShiftLocked() {
return mKeyboardShiftState.isShiftLocked(); return mKeyboardShiftState.isShiftLocked();
} }
@ -190,16 +201,16 @@ public class KeyboardState {
mSwitchActions.setShiftLocked(shiftLocked); mSwitchActions.setShiftLocked(shiftLocked);
} }
private void toggleAlphabetAndSymbols(boolean isAlphabetMode) { private void toggleAlphabetAndSymbols() {
if (isAlphabetMode) { if (mIsAlphabetMode) {
setSymbolsKeyboard(); setSymbolsKeyboard();
} else { } else {
setAlphabetKeyboard(); setAlphabetKeyboard();
} }
} }
private void toggleShiftInSymbols(boolean isSymbolShifted) { private void toggleShiftInSymbols() {
if (isSymbolShifted) { if (mIsSymbolShifted) {
setSymbolsKeyboard(); setSymbolsKeyboard();
} else { } else {
setSymbolsShiftedKeyboard(); setSymbolsShiftedKeyboard();
@ -211,6 +222,8 @@ public class KeyboardState {
Log.d(TAG, "setAlphabetKeyboard"); Log.d(TAG, "setAlphabetKeyboard");
} }
mSwitchActions.setAlphabetKeyboard(); mSwitchActions.setAlphabetKeyboard();
mIsAlphabetMode = true;
mIsSymbolShifted = false;
mSwitchState = SWITCH_STATE_ALPHA; mSwitchState = SWITCH_STATE_ALPHA;
setShiftLocked(mPrevMainKeyboardWasShiftLocked); setShiftLocked(mPrevMainKeyboardWasShiftLocked);
mPrevMainKeyboardWasShiftLocked = false; mPrevMainKeyboardWasShiftLocked = false;
@ -222,6 +235,8 @@ public class KeyboardState {
} }
mPrevMainKeyboardWasShiftLocked = isShiftLocked(); mPrevMainKeyboardWasShiftLocked = isShiftLocked();
mSwitchActions.setSymbolsKeyboard(); mSwitchActions.setSymbolsKeyboard();
mIsAlphabetMode = false;
mIsSymbolShifted = false;
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN; mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
} }
@ -230,28 +245,28 @@ public class KeyboardState {
Log.d(TAG, "setSymbolsShiftedKeyboard"); Log.d(TAG, "setSymbolsShiftedKeyboard");
} }
mSwitchActions.setSymbolsShiftedKeyboard(); mSwitchActions.setSymbolsShiftedKeyboard();
mIsAlphabetMode = false;
mIsSymbolShifted = true;
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN; mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
} }
// TODO: Get rid of isAlphabetMode argument. public void onPressSymbol() {
public void onPressSymbol(boolean isAlphabetMode) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onPressSymbol: " + this); Log.d(TAG, "onPressSymbol: " + this);
} }
toggleAlphabetAndSymbols(isAlphabetMode); toggleAlphabetAndSymbols();
mSymbolKeyState.onPress(); mSymbolKeyState.onPress();
mSwitchState = SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL; mSwitchState = SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL;
} }
// TODO: Get rid of isAlphabetMode argument. public void onReleaseSymbol() {
public void onReleaseSymbol(boolean isAlphabetMode) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onReleaseSymbol: " + this); Log.d(TAG, "onReleaseSymbol: " + this);
} }
// Snap back to the previous keyboard mode if the user chords the mode change key and // Snap back to the previous keyboard mode if the user chords the mode change key and
// another key, then releases the mode change key. // another key, then releases the mode change key.
if (mSwitchState == SWITCH_STATE_CHORDING_ALPHA) { if (mSwitchState == SWITCH_STATE_CHORDING_ALPHA) {
toggleAlphabetAndSymbols(isAlphabetMode); toggleAlphabetAndSymbols();
} }
mSymbolKeyState.onRelease(); mSymbolKeyState.onRelease();
} }
@ -264,12 +279,11 @@ public class KeyboardState {
mSymbolKeyState.onOtherKeyPressed(); mSymbolKeyState.onOtherKeyPressed();
} }
// TODO: Get rid of isAlphabetMode argument. public void onUpdateShiftState(boolean autoCaps) {
public void onUpdateShiftState(boolean isAlphabetMode, boolean autoCaps) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onUpdateShiftState: " + this + " autoCaps=" + autoCaps); Log.d(TAG, "onUpdateShiftState: " + this + " autoCaps=" + autoCaps);
} }
if (isAlphabetMode) { if (mIsAlphabetMode) {
if (!isShiftLocked() && !mShiftKeyState.isIgnoring()) { if (!isShiftLocked() && !mShiftKeyState.isIgnoring()) {
if (mShiftKeyState.isReleasing() && autoCaps) { if (mShiftKeyState.isReleasing() && autoCaps) {
// Only when shift key is releasing, automatic temporary upper case will be set. // Only when shift key is releasing, automatic temporary upper case will be set.
@ -286,12 +300,11 @@ public class KeyboardState {
} }
} }
// TODO: Get rid of isAlphabetMode and isSymbolShifted arguments. public void onPressShift(boolean withSliding) {
public void onPressShift(boolean isAlphabetMode, boolean isSymbolShifted, boolean withSliding) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onPressShift: " + this + " sliding=" + withSliding); Log.d(TAG, "onPressShift: " + this + " sliding=" + withSliding);
} }
if (isAlphabetMode) { if (mIsAlphabetMode) {
if (isShiftLocked()) { if (isShiftLocked()) {
// Shift key is pressed while caps lock state, we will treat this state as shifted // Shift key is pressed while caps lock state, we will treat this state as shifted
// caps lock state and mark as if shift key pressed while normal state. // caps lock state and mark as if shift key pressed while normal state.
@ -313,19 +326,17 @@ public class KeyboardState {
} }
} else { } else {
// In symbol mode, just toggle symbol and symbol more keyboard. // In symbol mode, just toggle symbol and symbol more keyboard.
toggleShiftInSymbols(isSymbolShifted); toggleShiftInSymbols();
mSwitchState = SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE; mSwitchState = SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE;
mShiftKeyState.onPress(); mShiftKeyState.onPress();
} }
} }
// TODO: Get rid of isAlphabetMode and isSymbolShifted arguments. public void onReleaseShift(boolean withSliding) {
public void onReleaseShift(boolean isAlphabetMode, boolean isSymbolShifted,
boolean withSliding) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onReleaseShift: " + this + " sliding=" + withSliding); Log.d(TAG, "onReleaseShift: " + this + " sliding=" + withSliding);
} }
if (isAlphabetMode) { if (mIsAlphabetMode) {
final boolean isShiftLocked = isShiftLocked(); final boolean isShiftLocked = isShiftLocked();
if (mShiftKeyState.isMomentary()) { if (mShiftKeyState.isMomentary()) {
// After chording input while normal state. // After chording input while normal state.
@ -350,24 +361,22 @@ public class KeyboardState {
// In symbol mode, snap back to the previous keyboard mode if the user chords the shift // In symbol mode, snap back to the previous keyboard mode if the user chords the shift
// key and another key, then releases the shift key. // key and another key, then releases the shift key.
if (mSwitchState == SWITCH_STATE_CHORDING_SYMBOL) { if (mSwitchState == SWITCH_STATE_CHORDING_SYMBOL) {
toggleShiftInSymbols(isSymbolShifted); toggleShiftInSymbols();
} }
} }
mShiftKeyState.onRelease(); mShiftKeyState.onRelease();
} }
// TODO: Get rid of isAlphabetMode and isSymbolShifted arguments. public void onCancelInput(boolean isSinglePointer) {
public void onCancelInput(boolean isAlphabetMode, boolean isSymbolShifted,
boolean isSinglePointer) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onCancelInput: isSinglePointer=" + isSinglePointer + " " + this); Log.d(TAG, "onCancelInput: isSinglePointer=" + isSinglePointer + " " + this);
} }
// Snap back to the previous keyboard mode if the user cancels sliding input. // Snap back to the previous keyboard mode if the user cancels sliding input.
if (isSinglePointer) { if (isSinglePointer) {
if (mSwitchState == SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL) { if (mSwitchState == SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL) {
toggleAlphabetAndSymbols(isAlphabetMode); toggleAlphabetAndSymbols();
} else if (mSwitchState == SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE) { } else if (mSwitchState == SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE) {
toggleShiftInSymbols(isSymbolShifted); toggleShiftInSymbols();
} }
} }
} }
@ -387,9 +396,7 @@ public class KeyboardState {
return false; return false;
} }
// TODO: Get rid of isAlphabetMode and isSymbolShifted arguments. public void onCodeInput(int code, boolean isSinglePointer) {
public void onCodeInput(boolean isAlphabetMode, boolean isSymbolShifted, int code,
boolean isSinglePointer) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onCodeInput: code=" + code + " isSinglePointer=" + isSinglePointer Log.d(TAG, "onCodeInput: code=" + code + " isSinglePointer=" + isSinglePointer
+ " " + this); + " " + this);
@ -404,7 +411,7 @@ public class KeyboardState {
// {@link #SWITCH_STATE_MOMENTARY}. // {@link #SWITCH_STATE_MOMENTARY}.
if (code == Keyboard.CODE_SWITCH_ALPHA_SYMBOL) { if (code == Keyboard.CODE_SWITCH_ALPHA_SYMBOL) {
// Detected only the mode change key has been pressed, and then released. // Detected only the mode change key has been pressed, and then released.
if (isAlphabetMode) { if (mIsAlphabetMode) {
mSwitchState = SWITCH_STATE_ALPHA; mSwitchState = SWITCH_STATE_ALPHA;
} else { } else {
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN; mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
@ -414,7 +421,7 @@ public class KeyboardState {
// and slid to other key, then released the finger. // and slid to other key, then released the finger.
// If the user cancels the sliding input, snapping back to the previous keyboard // If the user cancels the sliding input, snapping back to the previous keyboard
// mode is handled by {@link #onCancelInput}. // mode is handled by {@link #onCancelInput}.
toggleAlphabetAndSymbols(isAlphabetMode); toggleAlphabetAndSymbols();
} else { } else {
// Chording input is being started. The keyboard mode will be snapped back to the // Chording input is being started. The keyboard mode will be snapped back to the
// previous mode in {@link onReleaseSymbol} when the mode change key is released. // previous mode in {@link onReleaseSymbol} when the mode change key is released.
@ -428,7 +435,7 @@ public class KeyboardState {
} else if (isSinglePointer) { } else if (isSinglePointer) {
// Snap back to the previous keyboard mode if the user pressed the shift key on // Snap back to the previous keyboard mode if the user pressed the shift key on
// symbol mode and slid to other key, then released the finger. // symbol mode and slid to other key, then released the finger.
toggleShiftInSymbols(isSymbolShifted); toggleShiftInSymbols();
mSwitchState = SWITCH_STATE_SYMBOL; mSwitchState = SWITCH_STATE_SYMBOL;
} else { } else {
// Chording input is being started. The keyboard mode will be snapped back to the // Chording input is being started. The keyboard mode will be snapped back to the
@ -456,25 +463,23 @@ public class KeyboardState {
} }
} }
// TODO: Get rid of isAlphabetMode and isSymbolShifted arguments. public void onToggleShift() {
public void onToggleShift(boolean isAlphabetMode, boolean isSymbolShifted) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onToggleShift: " + this); Log.d(TAG, "onToggleShift: " + this);
} }
if (isAlphabetMode) { if (mIsAlphabetMode) {
setShifted(isShiftedOrShiftLocked() setShifted(isShiftedOrShiftLocked()
? SwitchActions.UNSHIFT : SwitchActions.MANUAL_SHIFT); ? SwitchActions.UNSHIFT : SwitchActions.MANUAL_SHIFT);
} else { } else {
toggleShiftInSymbols(isSymbolShifted); toggleShiftInSymbols();
} }
} }
// TODO: Get rid of isAlphabetMode arguments. public void onToggleCapsLock() {
public void onToggleCapsLock(boolean isAlphabetMode) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onToggleCapsLock: " + this); Log.d(TAG, "onToggleCapsLock: " + this);
} }
if (isAlphabetMode) { if (mIsAlphabetMode) {
if (isShiftLocked()) { if (isShiftLocked()) {
setShiftLocked(false); setShiftLocked(false);
// Shift key is long pressed while caps lock state, we will toggle back to normal // Shift key is long pressed while caps lock state, we will toggle back to normal
@ -486,12 +491,11 @@ public class KeyboardState {
} }
} }
// TODO: Get rid of isAlphabetMode arguments. public void onToggleAlphabetAndSymbols() {
public void onToggleAlphabetAndSymbols(boolean isAlphabetMode) {
if (DEBUG_STATE) { if (DEBUG_STATE) {
Log.d(TAG, "onToggleAlphabetAndSymbols: " + this); Log.d(TAG, "onToggleAlphabetAndSymbols: " + this);
} }
toggleAlphabetAndSymbols(isAlphabetMode); toggleAlphabetAndSymbols();
} }
private static String shiftModeToString(int shiftMode) { private static String shiftModeToString(int shiftMode) {