Refactor shift key state into KeyboardSwitcher

Change-Id: If484d3d7e7a0794ee7fc88f0771229d6f90db466
main
Tadashi G. Takaoka 2010-11-16 01:47:39 -08:00
parent d7641636db
commit 889691eca1
4 changed files with 83 additions and 36 deletions

View File

@ -75,7 +75,9 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
private final LatinIME mInputMethodService;
private final LanguageSwitcher mLanguageSwitcher;
private ShiftKeyState mShiftState = new ShiftKeyState();
private ModifierKeyState mSymbolKeyState = new ModifierKeyState();
private KeyboardId mSymbolsId;
private KeyboardId mSymbolsShiftedId;
@ -383,6 +385,30 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
mInputView.setShiftLocked(shiftLocked);
}
public void onPressShift() {
mShiftState.onPress();
}
public void onPressShiftOnShifted() {
mShiftState.onPressOnShifted();
}
public void onReleaseShift() {
mShiftState.onRelease();
}
public boolean isShiftMomentary() {
return mShiftState.isMomentary();
}
public boolean isShiftPressingOnShifted() {
return mShiftState.isPressingOnShifted();
}
public boolean isShiftIgnoring() {
return mShiftState.isIgnoring();
}
public void onPressSymbol() {
mSymbolKeyState.onPress();
}
@ -396,7 +422,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
}
public void onOtherKeyPressed() {
// TODO: shift key state will be handled too.
mShiftState.onOtherKeyPressed();
mSymbolKeyState.onOtherKeyPressed();
}

View File

@ -235,9 +235,6 @@ public class LatinIME extends InputMethodService
private int mDeleteCount;
private long mLastKeyTime;
// Modifier keys state
private final ModifierKeyState mShiftKeyState = new ModifierKeyState();
private Tutorial mTutorial;
private AudioManager mAudioManager;
@ -1091,8 +1088,8 @@ public class LatinIME extends InputMethodService
if (!switcher.isKeyboardAvailable())
return;
if (ic != null && attr != null && switcher.isAlphabetMode()
&& !mShiftKeyState.isIgnoring()) {
switcher.setShifted(mShiftKeyState.isMomentary()
&& !switcher.isShiftIgnoring()) {
switcher.setShifted(switcher.isShiftMomentary()
|| switcher.isShiftLocked() || getCursorCapsMode(ic, attr) != 0);
}
}
@ -2342,16 +2339,15 @@ public class LatinIME extends InputMethodService
// In alphabet mode, we call handleShift() to go into the shifted mode in this
// method, onPress(), only when we are in the small letter mode.
if (switcher.isAlphabetMode() && switcher.isShifted()) {
mShiftKeyState.onPressOnShifted();
switcher.onPressShiftOnShifted();
} else {
mShiftKeyState.onPress();
switcher.onPressShift();
handleShift();
}
} else if (distinctMultiTouch && primaryCode == BaseKeyboard.KEYCODE_MODE_CHANGE) {
switcher.onPressSymbol();
changeKeyboardMode();
} else {
mShiftKeyState.onOtherKeyPressed();
switcher.onOtherKeyPressed();
}
}
@ -2364,18 +2360,18 @@ public class LatinIME extends InputMethodService
switcher.keyReleased();
final boolean distinctMultiTouch = switcher.hasDistinctMultitouch();
if (distinctMultiTouch && primaryCode == BaseKeyboard.KEYCODE_SHIFT) {
if (mShiftKeyState.isMomentary()) {
if (switcher.isShiftMomentary()) {
resetShift();
}
if (switcher.isAlphabetMode()) {
// In alphabet mode, we call handleShift() to go into the small letter mode in this
// method, onRelease(), only when we are in the shifted modes -- temporary shifted
// mode or caps lock mode.
if (switcher.isShifted() && mShiftKeyState.isPressingOnShifted()) {
if (switcher.isShifted() && switcher.isShiftPressingOnShifted()) {
handleShift();
}
}
mShiftKeyState.onRelease();
switcher.onReleaseShift();
} else if (distinctMultiTouch && primaryCode == BaseKeyboard.KEYCODE_MODE_CHANGE) {
if (switcher.isSymbolMomentary()) {
changeKeyboardMode();

View File

@ -16,44 +16,26 @@
package com.android.inputmethod.latin;
class ModifierKeyState {
private static final int RELEASING = 0;
private static final int PRESSING = 1;
private static final int PRESSING_ON_SHIFTED = 2; // both temporary shifted & shift locked
private static final int MOMENTARY = 3;
private static final int IGNORING = 4;
public class ModifierKeyState {
protected static final int RELEASING = 0;
protected static final int PRESSING = 1;
protected static final int MOMENTARY = 2;
private int mState = RELEASING;
protected int mState = RELEASING;
public void onPress() {
mState = PRESSING;
}
public void onPressOnShifted() {
mState = PRESSING_ON_SHIFTED;
}
public void onRelease() {
mState = RELEASING;
}
public void onOtherKeyPressed() {
if (mState == PRESSING) {
mState = MOMENTARY;
} else if (mState == PRESSING_ON_SHIFTED) {
mState = IGNORING;
}
mState = MOMENTARY;
}
public boolean isMomentary() {
return mState == MOMENTARY;
}
public boolean isPressingOnShifted() {
return mState == PRESSING_ON_SHIFTED;
}
public boolean isIgnoring() {
return mState == IGNORING;
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2010 Google Inc.
*
* 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.latin;
public class ShiftKeyState extends ModifierKeyState {
private static final int PRESSING_ON_SHIFTED = 3; // both temporary shifted & shift locked
private static final int IGNORING = 4;
@Override
public void onOtherKeyPressed() {
if (mState == PRESSING) {
mState = MOMENTARY;
} else if (mState == PRESSING_ON_SHIFTED) {
mState = IGNORING;
}
}
public void onPressOnShifted() {
mState = PRESSING_ON_SHIFTED;
}
public boolean isPressingOnShifted() {
return mState == PRESSING_ON_SHIFTED;
}
public boolean isIgnoring() {
return mState == IGNORING;
}
}