2011-12-05 06:02:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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.keyboard.internal;
|
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
import android.text.TextUtils;
|
2011-12-08 05:42:12 +00:00
|
|
|
import android.util.Log;
|
2011-12-07 10:31:11 +00:00
|
|
|
|
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
|
2011-12-05 06:02:06 +00:00
|
|
|
// TODO: Add unit tests
|
2011-12-08 12:27:57 +00:00
|
|
|
/**
|
|
|
|
* Keyboard state machine.
|
|
|
|
*
|
|
|
|
* This class contains all keyboard state transition logic.
|
2011-12-09 10:53:36 +00:00
|
|
|
*
|
2011-12-09 07:09:16 +00:00
|
|
|
* 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()}.
|
2011-12-09 10:53:36 +00:00
|
|
|
*
|
2011-12-09 07:09:16 +00:00
|
|
|
* The actions are {@link SwitchActions}'s methods.
|
2011-12-08 12:27:57 +00:00
|
|
|
*/
|
2011-12-05 06:02:06 +00:00
|
|
|
public class KeyboardState {
|
2011-12-08 05:42:12 +00:00
|
|
|
private static final String TAG = KeyboardState.class.getSimpleName();
|
|
|
|
private static final boolean DEBUG_STATE = false;
|
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
public interface SwitchActions {
|
|
|
|
public void setAlphabetKeyboard();
|
2011-12-09 10:53:36 +00:00
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
public static final int UNSHIFT = 0;
|
|
|
|
public static final int MANUAL_SHIFT = 1;
|
|
|
|
public static final int AUTOMATIC_SHIFT = 2;
|
|
|
|
public void setShifted(int shiftMode);
|
2011-12-09 10:53:36 +00:00
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
public void setShiftLocked(boolean shiftLocked);
|
2011-12-09 10:53:36 +00:00
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
public void setSymbolsKeyboard();
|
2011-12-09 10:53:36 +00:00
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
public void setSymbolsShiftedKeyboard();
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:19:39 +00:00
|
|
|
private KeyboardShiftState mKeyboardShiftState = new KeyboardShiftState();
|
|
|
|
|
2011-12-05 06:02:06 +00:00
|
|
|
private ShiftKeyState mShiftKeyState = new ShiftKeyState("Shift");
|
|
|
|
private ModifierKeyState mSymbolKeyState = new ModifierKeyState("Symbol");
|
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
private static final int SWITCH_STATE_ALPHA = 0;
|
|
|
|
private static final int SWITCH_STATE_SYMBOL_BEGIN = 1;
|
|
|
|
private static final int SWITCH_STATE_SYMBOL = 2;
|
|
|
|
// The following states are used only on the distinct multi-touch panel devices.
|
|
|
|
private static final int SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL = 3;
|
|
|
|
private static final int SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE = 4;
|
|
|
|
private static final int SWITCH_STATE_CHORDING_ALPHA = 5;
|
|
|
|
private static final int SWITCH_STATE_CHORDING_SYMBOL = 6;
|
|
|
|
private int mSwitchState = SWITCH_STATE_ALPHA;
|
|
|
|
|
|
|
|
private String mLayoutSwitchBackSymbols;
|
2011-12-08 12:27:57 +00:00
|
|
|
private boolean mHasDistinctMultitouch;
|
2011-12-07 10:31:11 +00:00
|
|
|
|
|
|
|
private final SwitchActions mSwitchActions;
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
private boolean mIsAlphabetMode;
|
|
|
|
private boolean mIsSymbolShifted;
|
|
|
|
|
2011-12-08 05:42:12 +00:00
|
|
|
private final SavedKeyboardState mSavedKeyboardState = new SavedKeyboardState();
|
2011-12-08 07:30:13 +00:00
|
|
|
private boolean mPrevMainKeyboardWasShiftLocked;
|
2011-12-08 05:42:12 +00:00
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
static class SavedKeyboardState {
|
2011-12-08 05:42:12 +00:00
|
|
|
public boolean mIsValid;
|
|
|
|
public boolean mIsAlphabetMode;
|
|
|
|
public boolean mIsShiftLocked;
|
|
|
|
public boolean mIsShifted;
|
|
|
|
}
|
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
public KeyboardState(SwitchActions switchActions) {
|
|
|
|
mSwitchActions = switchActions;
|
2011-12-05 06:02:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 12:27:57 +00:00
|
|
|
public void onLoadKeyboard(String layoutSwitchBackSymbols, boolean hasDistinctMultitouch) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onLoadKeyboard");
|
|
|
|
}
|
2011-12-07 10:31:11 +00:00
|
|
|
mLayoutSwitchBackSymbols = layoutSwitchBackSymbols;
|
2011-12-08 12:27:57 +00:00
|
|
|
mHasDistinctMultitouch = hasDistinctMultitouch;
|
2011-12-07 08:55:22 +00:00
|
|
|
mKeyboardShiftState.setShifted(false);
|
|
|
|
mKeyboardShiftState.setShiftLocked(false);
|
|
|
|
mShiftKeyState.onRelease();
|
|
|
|
mSymbolKeyState.onRelease();
|
2011-12-08 07:30:13 +00:00
|
|
|
mPrevMainKeyboardWasShiftLocked = false;
|
|
|
|
onRestoreKeyboardState();
|
2011-12-07 08:55:22 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onSaveKeyboardState() {
|
2011-12-08 05:42:12 +00:00
|
|
|
final SavedKeyboardState state = mSavedKeyboardState;
|
2011-12-09 07:09:16 +00:00
|
|
|
state.mIsAlphabetMode = mIsAlphabetMode;
|
|
|
|
if (mIsAlphabetMode) {
|
2011-12-09 10:53:36 +00:00
|
|
|
state.mIsShiftLocked = mKeyboardShiftState.isShiftLocked();
|
|
|
|
state.mIsShifted = !state.mIsShiftLocked
|
|
|
|
&& mKeyboardShiftState.isShiftedOrShiftLocked();
|
2011-12-08 05:42:12 +00:00
|
|
|
} else {
|
|
|
|
state.mIsShiftLocked = false;
|
2011-12-09 07:09:16 +00:00
|
|
|
state.mIsShifted = mIsSymbolShifted;
|
2011-12-08 05:42:12 +00:00
|
|
|
}
|
|
|
|
state.mIsValid = true;
|
|
|
|
if (DEBUG_STATE) {
|
2011-12-08 13:10:27 +00:00
|
|
|
Log.d(TAG, "onSaveKeyboardState: alphabet=" + state.mIsAlphabetMode
|
2011-12-08 05:42:12 +00:00
|
|
|
+ " shiftLocked=" + state.mIsShiftLocked + " shift=" + state.mIsShifted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 07:30:13 +00:00
|
|
|
private void onRestoreKeyboardState() {
|
2011-12-08 05:42:12 +00:00
|
|
|
final SavedKeyboardState state = mSavedKeyboardState;
|
|
|
|
if (DEBUG_STATE) {
|
2011-12-08 13:10:27 +00:00
|
|
|
Log.d(TAG, "onRestoreKeyboardState: valid=" + state.mIsValid
|
|
|
|
+ " alphabet=" + state.mIsAlphabetMode
|
2011-12-08 05:42:12 +00:00
|
|
|
+ " shiftLocked=" + state.mIsShiftLocked + " shift=" + state.mIsShifted);
|
|
|
|
}
|
|
|
|
if (!state.mIsValid || state.mIsAlphabetMode) {
|
2011-12-08 12:03:25 +00:00
|
|
|
setAlphabetKeyboard();
|
2011-12-08 05:42:12 +00:00
|
|
|
} else {
|
|
|
|
if (state.mIsShifted) {
|
2011-12-08 12:03:25 +00:00
|
|
|
setSymbolsShiftedKeyboard();
|
2011-12-08 05:42:12 +00:00
|
|
|
} else {
|
2011-12-08 12:03:25 +00:00
|
|
|
setSymbolsKeyboard();
|
2011-12-08 05:42:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.mIsValid) return;
|
|
|
|
state.mIsValid = false;
|
|
|
|
|
|
|
|
if (state.mIsAlphabetMode) {
|
2011-12-08 12:27:57 +00:00
|
|
|
setShiftLocked(state.mIsShiftLocked);
|
2011-12-08 05:42:12 +00:00
|
|
|
if (!state.mIsShiftLocked) {
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(state.mIsShifted ? SwitchActions.MANUAL_SHIFT : SwitchActions.UNSHIFT);
|
2011-12-08 05:42:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 10:53:36 +00:00
|
|
|
// TODO: Remove this method.
|
2011-12-06 02:19:39 +00:00
|
|
|
public boolean isShiftLocked() {
|
|
|
|
return mKeyboardShiftState.isShiftLocked();
|
|
|
|
}
|
|
|
|
|
2011-12-08 12:27:57 +00:00
|
|
|
private void setShifted(int shiftMode) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "setShifted: shiftMode=" + shiftModeToString(shiftMode));
|
|
|
|
}
|
2011-12-08 12:27:57 +00:00
|
|
|
if (shiftMode == SwitchActions.AUTOMATIC_SHIFT) {
|
|
|
|
mKeyboardShiftState.setAutomaticTemporaryUpperCase();
|
|
|
|
} else {
|
|
|
|
// TODO: Duplicated logic in KeyboardSwitcher#setShifted()
|
|
|
|
final boolean shifted = (shiftMode == SwitchActions.MANUAL_SHIFT);
|
|
|
|
// On non-distinct multi touch panel device, we should also turn off the shift locked
|
|
|
|
// state when shift key is pressed to go to normal mode.
|
|
|
|
// On the other hand, on distinct multi touch panel device, turning off the shift
|
|
|
|
// locked state with shift key pressing is handled by onReleaseShift().
|
2011-12-09 10:53:36 +00:00
|
|
|
if (!mHasDistinctMultitouch && !shifted && mKeyboardShiftState.isShiftLocked()) {
|
2011-12-08 12:27:57 +00:00
|
|
|
mKeyboardShiftState.setShiftLocked(false);
|
|
|
|
}
|
|
|
|
mKeyboardShiftState.setShifted(shifted);
|
|
|
|
}
|
|
|
|
mSwitchActions.setShifted(shiftMode);
|
2011-12-06 02:19:39 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 12:27:57 +00:00
|
|
|
private void setShiftLocked(boolean shiftLocked) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "setShiftLocked: shiftLocked=" + shiftLocked);
|
|
|
|
}
|
2011-12-06 02:19:39 +00:00
|
|
|
mKeyboardShiftState.setShiftLocked(shiftLocked);
|
2011-12-08 12:27:57 +00:00
|
|
|
mSwitchActions.setShiftLocked(shiftLocked);
|
2011-12-06 02:19:39 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
private void toggleAlphabetAndSymbols() {
|
|
|
|
if (mIsAlphabetMode) {
|
2011-12-08 12:03:25 +00:00
|
|
|
setSymbolsKeyboard();
|
2011-12-07 10:31:11 +00:00
|
|
|
} else {
|
2011-12-08 12:03:25 +00:00
|
|
|
setAlphabetKeyboard();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
2011-12-05 06:02:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
private void toggleShiftInSymbols() {
|
|
|
|
if (mIsSymbolShifted) {
|
2011-12-08 12:03:25 +00:00
|
|
|
setSymbolsKeyboard();
|
2011-12-07 10:31:11 +00:00
|
|
|
} else {
|
2011-12-08 12:03:25 +00:00
|
|
|
setSymbolsShiftedKeyboard();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
2011-12-05 06:02:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 12:03:25 +00:00
|
|
|
private void setAlphabetKeyboard() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "setAlphabetKeyboard");
|
|
|
|
}
|
2011-12-08 12:03:25 +00:00
|
|
|
mSwitchActions.setAlphabetKeyboard();
|
2011-12-09 07:09:16 +00:00
|
|
|
mIsAlphabetMode = true;
|
|
|
|
mIsSymbolShifted = false;
|
2011-12-08 12:03:25 +00:00
|
|
|
mSwitchState = SWITCH_STATE_ALPHA;
|
2011-12-08 12:27:57 +00:00
|
|
|
setShiftLocked(mPrevMainKeyboardWasShiftLocked);
|
2011-12-08 07:30:13 +00:00
|
|
|
mPrevMainKeyboardWasShiftLocked = false;
|
|
|
|
}
|
|
|
|
|
2011-12-08 12:03:25 +00:00
|
|
|
private void setSymbolsKeyboard() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "setSymbolsKeyboard");
|
|
|
|
}
|
2011-12-09 10:53:36 +00:00
|
|
|
mPrevMainKeyboardWasShiftLocked = mKeyboardShiftState.isShiftLocked();
|
2011-12-08 12:03:25 +00:00
|
|
|
mSwitchActions.setSymbolsKeyboard();
|
2011-12-09 07:09:16 +00:00
|
|
|
mIsAlphabetMode = false;
|
|
|
|
mIsSymbolShifted = false;
|
2011-12-08 12:03:25 +00:00
|
|
|
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
|
2011-12-08 07:30:13 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 12:03:25 +00:00
|
|
|
private void setSymbolsShiftedKeyboard() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "setSymbolsShiftedKeyboard");
|
|
|
|
}
|
2011-12-08 12:03:25 +00:00
|
|
|
mSwitchActions.setSymbolsShiftedKeyboard();
|
2011-12-09 07:09:16 +00:00
|
|
|
mIsAlphabetMode = false;
|
|
|
|
mIsSymbolShifted = true;
|
2011-12-08 12:03:25 +00:00
|
|
|
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
|
2011-12-05 06:02:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onPressSymbol() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onPressSymbol: " + this);
|
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleAlphabetAndSymbols();
|
2011-12-05 06:02:06 +00:00
|
|
|
mSymbolKeyState.onPress();
|
2011-12-07 10:31:11 +00:00
|
|
|
mSwitchState = SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL;
|
2011-12-05 06:02:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onReleaseSymbol() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onReleaseSymbol: " + this);
|
|
|
|
}
|
2011-12-07 10:31:11 +00:00
|
|
|
// Snap back to the previous keyboard mode if the user chords the mode change key and
|
|
|
|
// another key, then releases the mode change key.
|
|
|
|
if (mSwitchState == SWITCH_STATE_CHORDING_ALPHA) {
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleAlphabetAndSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
2011-12-05 06:02:06 +00:00
|
|
|
mSymbolKeyState.onRelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onOtherKeyPressed() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onOtherKeyPressed: " + this);
|
|
|
|
}
|
2011-12-05 06:02:06 +00:00
|
|
|
mShiftKeyState.onOtherKeyPressed();
|
|
|
|
mSymbolKeyState.onOtherKeyPressed();
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onUpdateShiftState(boolean autoCaps) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
2011-12-09 11:18:39 +00:00
|
|
|
Log.d(TAG, "onUpdateShiftState: autoCaps=" + autoCaps + " " + this);
|
2011-12-08 13:10:27 +00:00
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
if (mIsAlphabetMode) {
|
2011-12-09 10:53:36 +00:00
|
|
|
if (!mKeyboardShiftState.isShiftLocked() && !mShiftKeyState.isIgnoring()) {
|
2011-12-07 10:31:11 +00:00
|
|
|
if (mShiftKeyState.isReleasing() && autoCaps) {
|
|
|
|
// Only when shift key is releasing, automatic temporary upper case will be set.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.AUTOMATIC_SHIFT);
|
2011-12-07 10:31:11 +00:00
|
|
|
} else {
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(mShiftKeyState.isMomentary()
|
2011-12-07 10:31:11 +00:00
|
|
|
? SwitchActions.MANUAL_SHIFT : SwitchActions.UNSHIFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2011-12-05 06:02:06 +00:00
|
|
|
// In symbol keyboard mode, we should clear shift key state because only alphabet
|
|
|
|
// keyboard has shift key.
|
|
|
|
mSymbolKeyState.onRelease();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onPressShift(boolean withSliding) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
2011-12-09 11:18:39 +00:00
|
|
|
Log.d(TAG, "onPressShift: sliding=" + withSliding + " " + this);
|
2011-12-08 13:10:27 +00:00
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
if (mIsAlphabetMode) {
|
2011-12-09 10:53:36 +00:00
|
|
|
if (mKeyboardShiftState.isShiftLocked()) {
|
2011-12-05 06:02:06 +00:00
|
|
|
// 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.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.MANUAL_SHIFT);
|
2011-12-05 06:02:06 +00:00
|
|
|
mShiftKeyState.onPress();
|
2011-12-09 10:53:36 +00:00
|
|
|
} else if (mKeyboardShiftState.isAutomaticTemporaryUpperCase()) {
|
2011-12-05 06:02:06 +00:00
|
|
|
// Shift key is pressed while automatic temporary upper case, we have to move to
|
|
|
|
// manual temporary upper case.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.MANUAL_SHIFT);
|
2011-12-05 06:02:06 +00:00
|
|
|
mShiftKeyState.onPress();
|
2011-12-09 10:53:36 +00:00
|
|
|
} else if (mKeyboardShiftState.isShiftedOrShiftLocked()) {
|
2011-12-05 06:02:06 +00:00
|
|
|
// In manual upper case state, we just record shift key has been pressing while
|
|
|
|
// shifted state.
|
|
|
|
mShiftKeyState.onPressOnShifted();
|
|
|
|
} else {
|
|
|
|
// In base layout, chording or manual temporary upper case mode is started.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.MANUAL_SHIFT);
|
2011-12-05 06:02:06 +00:00
|
|
|
mShiftKeyState.onPress();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In symbol mode, just toggle symbol and symbol more keyboard.
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleShiftInSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
mSwitchState = SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE;
|
2011-12-05 06:02:06 +00:00
|
|
|
mShiftKeyState.onPress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onReleaseShift(boolean withSliding) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
2011-12-09 11:18:39 +00:00
|
|
|
Log.d(TAG, "onReleaseShift: sliding=" + withSliding + " " + this);
|
2011-12-08 13:10:27 +00:00
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
if (mIsAlphabetMode) {
|
2011-12-09 10:53:36 +00:00
|
|
|
final boolean isShiftLocked = mKeyboardShiftState.isShiftLocked();
|
2011-12-07 10:31:11 +00:00
|
|
|
if (mShiftKeyState.isMomentary()) {
|
|
|
|
// After chording input while normal state.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.UNSHIFT);
|
2011-12-09 10:53:36 +00:00
|
|
|
} else if (isShiftLocked && !mKeyboardShiftState.isShiftLockShifted()
|
|
|
|
&& (mShiftKeyState.isPressing() || mShiftKeyState.isPressingOnShifted())
|
|
|
|
&& !withSliding) {
|
2011-12-07 10:31:11 +00:00
|
|
|
// Shift has been long pressed, ignore this release.
|
|
|
|
} else if (isShiftLocked && !mShiftKeyState.isIgnoring() && !withSliding) {
|
|
|
|
// Shift has been pressed without chording while caps lock state.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShiftLocked(false);
|
2011-12-09 10:53:36 +00:00
|
|
|
} else if (mKeyboardShiftState.isShiftedOrShiftLocked()
|
|
|
|
&& mShiftKeyState.isPressingOnShifted() && !withSliding) {
|
2011-12-07 10:31:11 +00:00
|
|
|
// Shift has been pressed without chording while shifted state.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.UNSHIFT);
|
2011-12-09 10:53:36 +00:00
|
|
|
} else if (mKeyboardShiftState.isManualTemporaryUpperCaseFromAuto()
|
|
|
|
&& mShiftKeyState.isPressing() && !withSliding) {
|
2011-12-07 10:31:11 +00:00
|
|
|
// Shift has been pressed without chording while manual temporary upper case
|
|
|
|
// transited from automatic temporary upper case.
|
2011-12-08 12:27:57 +00:00
|
|
|
setShifted(SwitchActions.UNSHIFT);
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
if (mSwitchState == SWITCH_STATE_CHORDING_SYMBOL) {
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleShiftInSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-05 06:02:06 +00:00
|
|
|
mShiftKeyState.onRelease();
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onCancelInput(boolean isSinglePointer) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onCancelInput: isSinglePointer=" + isSinglePointer + " " + this);
|
|
|
|
}
|
2011-12-07 10:31:11 +00:00
|
|
|
// Snap back to the previous keyboard mode if the user cancels sliding input.
|
|
|
|
if (isSinglePointer) {
|
|
|
|
if (mSwitchState == SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL) {
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleAlphabetAndSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
} else if (mSwitchState == SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE) {
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleShiftInSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isInMomentarySwitchState() {
|
|
|
|
return mSwitchState == SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL
|
|
|
|
|| mSwitchState == SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isSpaceCharacter(int c) {
|
|
|
|
return c == Keyboard.CODE_SPACE || c == Keyboard.CODE_ENTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isLayoutSwitchBackCharacter(int c) {
|
|
|
|
if (TextUtils.isEmpty(mLayoutSwitchBackSymbols)) return false;
|
|
|
|
if (mLayoutSwitchBackSymbols.indexOf(c) >= 0) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onCodeInput(int code, boolean isSinglePointer) {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onCodeInput: code=" + code + " isSinglePointer=" + isSinglePointer
|
|
|
|
+ " " + this);
|
|
|
|
}
|
2011-12-07 10:31:11 +00:00
|
|
|
switch (mSwitchState) {
|
|
|
|
case SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL:
|
|
|
|
// Only distinct multi touch devices can be in this state.
|
|
|
|
// On non-distinct multi touch devices, mode change key is handled by
|
|
|
|
// {@link LatinIME#onCodeInput}, not by {@link LatinIME#onPress} and
|
|
|
|
// {@link LatinIME#onRelease}. So, on such devices, {@link #mSwitchState} starts
|
|
|
|
// from {@link #SWITCH_STATE_SYMBOL_BEGIN}, or {@link #SWITCH_STATE_ALPHA}, not from
|
|
|
|
// {@link #SWITCH_STATE_MOMENTARY}.
|
|
|
|
if (code == Keyboard.CODE_SWITCH_ALPHA_SYMBOL) {
|
|
|
|
// Detected only the mode change key has been pressed, and then released.
|
2011-12-09 07:09:16 +00:00
|
|
|
if (mIsAlphabetMode) {
|
2011-12-07 10:31:11 +00:00
|
|
|
mSwitchState = SWITCH_STATE_ALPHA;
|
|
|
|
} else {
|
|
|
|
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
|
|
|
|
}
|
|
|
|
} else if (isSinglePointer) {
|
|
|
|
// Snap back to the previous keyboard mode if the user pressed the mode change key
|
|
|
|
// and slid to other key, then released the finger.
|
|
|
|
// If the user cancels the sliding input, snapping back to the previous keyboard
|
|
|
|
// mode is handled by {@link #onCancelInput}.
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleAlphabetAndSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
mSwitchState = SWITCH_STATE_CHORDING_ALPHA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE:
|
|
|
|
if (code == Keyboard.CODE_SHIFT) {
|
|
|
|
// Detected only the shift key has been pressed on symbol layout, and then released.
|
|
|
|
mSwitchState = SWITCH_STATE_SYMBOL_BEGIN;
|
|
|
|
} else if (isSinglePointer) {
|
|
|
|
// 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.
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleShiftInSymbols();
|
2011-12-07 10:31:11 +00:00
|
|
|
mSwitchState = SWITCH_STATE_SYMBOL;
|
|
|
|
} else {
|
|
|
|
// Chording input is being started. The keyboard mode will be snapped back to the
|
|
|
|
// previous mode in {@link onReleaseShift} when the shift key is released.
|
|
|
|
mSwitchState = SWITCH_STATE_CHORDING_SYMBOL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SWITCH_STATE_SYMBOL_BEGIN:
|
|
|
|
if (!isSpaceCharacter(code) && code >= 0) {
|
|
|
|
mSwitchState = SWITCH_STATE_SYMBOL;
|
|
|
|
}
|
|
|
|
// Snap back to alpha keyboard mode immediately if user types a quote character.
|
|
|
|
if (isLayoutSwitchBackCharacter(code)) {
|
2011-12-08 12:03:25 +00:00
|
|
|
setAlphabetKeyboard();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SWITCH_STATE_SYMBOL:
|
|
|
|
case SWITCH_STATE_CHORDING_SYMBOL:
|
|
|
|
// Snap back to alpha keyboard mode if user types one or more non-space/enter
|
|
|
|
// characters followed by a space/enter or a quote character.
|
|
|
|
if (isSpaceCharacter(code) || isLayoutSwitchBackCharacter(code)) {
|
2011-12-08 12:03:25 +00:00
|
|
|
setAlphabetKeyboard();
|
2011-12-07 10:31:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onToggleShift() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onToggleShift: " + this);
|
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
if (mIsAlphabetMode) {
|
2011-12-09 10:53:36 +00:00
|
|
|
setShifted(mKeyboardShiftState.isShiftedOrShiftLocked()
|
2011-12-08 12:27:57 +00:00
|
|
|
? SwitchActions.UNSHIFT : SwitchActions.MANUAL_SHIFT);
|
2011-12-08 12:03:25 +00:00
|
|
|
} else {
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleShiftInSymbols();
|
2011-12-08 12:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onToggleCapsLock() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onToggleCapsLock: " + this);
|
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
if (mIsAlphabetMode) {
|
2011-12-09 10:53:36 +00:00
|
|
|
if (mKeyboardShiftState.isShiftLocked()) {
|
2011-12-08 12:27:57 +00:00
|
|
|
setShiftLocked(false);
|
2011-12-08 12:03:25 +00:00
|
|
|
// Shift key is long pressed while caps lock state, we will toggle back to normal
|
|
|
|
// state. And mark as if shift key is released.
|
|
|
|
mShiftKeyState.onRelease();
|
|
|
|
} else {
|
2011-12-08 12:27:57 +00:00
|
|
|
setShiftLocked(true);
|
2011-12-08 12:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 07:09:16 +00:00
|
|
|
public void onToggleAlphabetAndSymbols() {
|
2011-12-08 13:10:27 +00:00
|
|
|
if (DEBUG_STATE) {
|
|
|
|
Log.d(TAG, "onToggleAlphabetAndSymbols: " + this);
|
|
|
|
}
|
2011-12-09 07:09:16 +00:00
|
|
|
toggleAlphabetAndSymbols();
|
2011-12-08 12:03:25 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 13:10:27 +00:00
|
|
|
private static String shiftModeToString(int shiftMode) {
|
|
|
|
switch (shiftMode) {
|
|
|
|
case SwitchActions.UNSHIFT: return "UNSHIFT";
|
|
|
|
case SwitchActions.MANUAL_SHIFT: return "MANUAL";
|
|
|
|
case SwitchActions.AUTOMATIC_SHIFT: return "AUTOMATIC";
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 10:53:36 +00:00
|
|
|
|
2011-12-07 10:31:11 +00:00
|
|
|
private static String switchStateToString(int switchState) {
|
|
|
|
switch (switchState) {
|
|
|
|
case SWITCH_STATE_ALPHA: return "ALPHA";
|
|
|
|
case SWITCH_STATE_SYMBOL_BEGIN: return "SYMBOL-BEGIN";
|
|
|
|
case SWITCH_STATE_SYMBOL: return "SYMBOL";
|
|
|
|
case SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL: return "MOMENTARY-ALPHA-SYMBOL";
|
|
|
|
case SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE: return "MOMENTARY-SYMBOL-MORE";
|
|
|
|
case SWITCH_STATE_CHORDING_ALPHA: return "CHORDING-ALPHA";
|
|
|
|
case SWITCH_STATE_CHORDING_SYMBOL: return "CHORDING-SYMBOL";
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 06:02:06 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2011-12-06 02:19:39 +00:00
|
|
|
return "[keyboard=" + mKeyboardShiftState
|
|
|
|
+ " shift=" + mShiftKeyState
|
2011-12-07 10:31:11 +00:00
|
|
|
+ " symbol=" + mSymbolKeyState
|
|
|
|
+ " switch=" + switchStateToString(mSwitchState) + "]";
|
2011-12-05 06:02:06 +00:00
|
|
|
}
|
|
|
|
}
|