diff options
Diffstat (limited to 'java/src')
4 files changed, 236 insertions, 107 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 1d9ca787f..19bfef38a 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -30,8 +30,7 @@ import android.view.View; import android.view.inputmethod.EditorInfo; import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; -import com.android.inputmethod.keyboard.internal.ModifierKeyState; -import com.android.inputmethod.keyboard.internal.ShiftKeyState; +import com.android.inputmethod.keyboard.internal.KeyboardState; import com.android.inputmethod.latin.InputView; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; @@ -69,9 +68,10 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private String mPackageName; private Resources mResources; - // TODO: Combine these key state objects with auto mode switch state. - private ShiftKeyState mShiftKeyState = new ShiftKeyState("Shift"); - private ModifierKeyState mSymbolKeyState = new ModifierKeyState("Symbol"); + private KeyboardState mState; + private static final int UNSHIFT = 0; + private static final int MANUAL_SHIFT = 1; + private static final int AUTOMATIC_SHIFT = 2; private KeyboardId mMainKeyboardId; private KeyboardId mSymbolsKeyboardId; @@ -88,7 +88,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private boolean mIsAutoCorrectionActive; // TODO: Encapsulate these state handling to separate class and combine with ShiftKeyState - // and ModifierKeyState. + // and ModifierKeyState into KeyboardState. 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; @@ -173,6 +173,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha mResources = ims.getResources(); mPrefs = prefs; mSubtypeSwitcher = SubtypeSwitcher.getInstance(); + mState = new KeyboardState(); setContextThemeWrapper(ims, getKeyboardThemeIndex(ims, prefs)); prefs.registerOnSharedPreferenceChangeListener(this); } @@ -393,7 +394,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return false; } - public boolean isAutomaticTemporaryUpperCase() { + private boolean isAutomaticTemporaryUpperCase() { LatinKeyboard latinKeyboard = getLatinKeyboard(); if (latinKeyboard != null) return latinKeyboard.isAutomaticTemporaryUpperCase(); @@ -414,13 +415,19 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return false; } - private void setManualTemporaryUpperCase(boolean shifted) { + private void setShift(int shiftMode) { LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) { + if (latinKeyboard == null) + return; + if (shiftMode == AUTOMATIC_SHIFT) { + latinKeyboard.setAutomaticTemporaryUpperCase(); + mKeyboardView.invalidateAllKeys(); + } else { + final boolean shifted = (shiftMode == 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(). + // On the other hand, on distinct multi touch panel device, turning off the shift + // locked state with shift key pressing is handled by onReleaseShift(). if (!hasDistinctMultitouch() && !shifted && latinKeyboard.isShiftLocked()) { latinKeyboard.setShiftLocked(false); } @@ -442,12 +449,13 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha */ public void toggleShift() { mInputMethodService.mHandler.cancelUpdateShiftState(); - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "toggleShift:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState); + + " state=" + mState); + } if (isAlphabetMode()) { - setManualTemporaryUpperCase(!isShiftedOrShiftLocked()); + setShift(isShiftedOrShiftLocked() ? UNSHIFT : MANUAL_SHIFT); } else { toggleShiftInSymbol(); } @@ -455,132 +463,138 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha public void toggleCapsLock() { mInputMethodService.mHandler.cancelUpdateShiftState(); - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "toggleCapsLock:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState); + + " state=" + mState); + } if (isAlphabetMode()) { if (isShiftLocked()) { // Shift key is long pressed while caps lock state, we will toggle back to normal // state. And mark as if shift key is released. setShiftLocked(false); - mShiftKeyState.onRelease(); + mState.onToggleCapsLock(); } else { setShiftLocked(true); } } } - private void setAutomaticTemporaryUpperCase() { - if (mKeyboardView == null) return; - final Keyboard keyboard = mKeyboardView.getKeyboard(); - if (keyboard == null) return; - keyboard.setAutomaticTemporaryUpperCase(); - mKeyboardView.invalidateAllKeys(); + public void changeKeyboardMode() { + if (DEBUG_STATE) { + Log.d(TAG, "changeKeyboardMode:" + + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() + + " state=" + mState); + } + toggleKeyboardMode(); + if (isShiftLocked() && isAlphabetMode()) { + setShiftLocked(true); + } + updateShiftState(); + } + + private void startIgnoringDoubleTap() { + if (mKeyboardView != null) { + mKeyboardView.startIgnoringDoubleTap(); + } } /** * Update keyboard shift state triggered by connected EditText status change. */ public void updateShiftState() { - final ShiftKeyState shiftKeyState = mShiftKeyState; - if (DEBUG_STATE) + final boolean isAlphabetMode = isAlphabetMode(); + final boolean isShiftLocked = isShiftLocked(); + if (DEBUG_STATE) { Log.d(TAG, "updateShiftState:" + " autoCaps=" + mInputMethodService.getCurrentAutoCapsState() + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + shiftKeyState - + " isAlphabetMode=" + isAlphabetMode() - + " isShiftLocked=" + isShiftLocked()); - if (isAlphabetMode()) { - if (!isShiftLocked() && !shiftKeyState.isIgnoring()) { - if (shiftKeyState.isReleasing() && mInputMethodService.getCurrentAutoCapsState()) { + + " isAlphabetMode=" + isAlphabetMode + + " isShiftLocked=" + isShiftLocked + + " state=" + mState); + } + if (isAlphabetMode) { + if (!isShiftLocked && !mState.isShiftKeyIgnoring()) { + if (mState.isShiftKeyReleasing() && mInputMethodService.getCurrentAutoCapsState()) { // Only when shift key is releasing, automatic temporary upper case will be set. - setAutomaticTemporaryUpperCase(); + setShift(AUTOMATIC_SHIFT); } else { - setManualTemporaryUpperCase(shiftKeyState.isMomentary()); + setShift(mState.isShiftKeyMomentary() ? MANUAL_SHIFT : UNSHIFT); } } - } else { - // In symbol keyboard mode, we should clear shift key state because only alphabet - // keyboard has shift key. - shiftKeyState.onRelease(); } - } - - public void changeKeyboardMode() { - if (DEBUG_STATE) - Log.d(TAG, "changeKeyboardMode:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState); - toggleKeyboardMode(); - if (isShiftLocked() && isAlphabetMode()) - setShiftLocked(true); - updateShiftState(); + mState.onUpdateShiftState(isAlphabetMode); } public void onPressShift(boolean withSliding) { if (!isKeyboardAvailable()) return; - ShiftKeyState shiftKeyState = mShiftKeyState; - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "onPressShift:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + shiftKeyState + " sliding=" + withSliding); - if (isAlphabetMode()) { - if (isShiftLocked()) { + + " state=" + mState + " sliding=" + withSliding); + } + final boolean isAlphabetMode = isAlphabetMode(); + final boolean isShiftLocked = isShiftLocked(); + final boolean isAutomaticTemporaryUpperCase = isAutomaticTemporaryUpperCase(); + final boolean isShiftedOrShiftLocked = isShiftedOrShiftLocked(); + if (isAlphabetMode) { + if (isShiftLocked) { // 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. - shiftKeyState.onPress(); - setManualTemporaryUpperCase(true); - } else if (isAutomaticTemporaryUpperCase()) { + setShift(MANUAL_SHIFT); + } else if (isAutomaticTemporaryUpperCase) { // Shift key is pressed while automatic temporary upper case, we have to move to // manual temporary upper case. - shiftKeyState.onPress(); - setManualTemporaryUpperCase(true); - } else if (isShiftedOrShiftLocked()) { + setShift(MANUAL_SHIFT); + } else if (isShiftedOrShiftLocked) { // In manual upper case state, we just record shift key has been pressing while // shifted state. - shiftKeyState.onPressOnShifted(); } else { // In base layout, chording or manual temporary upper case mode is started. - shiftKeyState.onPress(); toggleShift(); } } else { // In symbol mode, just toggle symbol and symbol more keyboard. - shiftKeyState.onPress(); toggleShift(); mSwitchState = SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE; } + mState.onPressShift(isAlphabetMode, isShiftLocked, isAutomaticTemporaryUpperCase, + isShiftedOrShiftLocked); } public void onReleaseShift(boolean withSliding) { if (!isKeyboardAvailable()) return; - ShiftKeyState shiftKeyState = mShiftKeyState; - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "onReleaseShift:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + shiftKeyState + " sliding=" + withSliding); - if (isAlphabetMode()) { - if (shiftKeyState.isMomentary()) { + + " state=" + mState + " sliding=" + withSliding); + } + final boolean isAlphabetMode = isAlphabetMode(); + final boolean isShiftLocked = isShiftLocked(); + final boolean isShiftLockShifted = isShiftLockShifted(); + final boolean isShiftedOrShiftLocked = isShiftedOrShiftLocked(); + final boolean isManualTemporaryUpperCaseFromAuto = isManualTemporaryUpperCaseFromAuto(); + if (isAlphabetMode) { + if (mState.isShiftKeyMomentary()) { // After chording input while normal state. toggleShift(); - } else if (isShiftLocked() && !isShiftLockShifted() && (shiftKeyState.isPressing() - || shiftKeyState.isPressingOnShifted()) && !withSliding) { + } else if (isShiftLocked && !isShiftLockShifted && (mState.isShiftKeyPressing() + || mState.isShiftKeyPressingOnShifted()) && !withSliding) { // Shift has been long pressed, ignore this release. - } else if (isShiftLocked() && !shiftKeyState.isIgnoring() && !withSliding) { + } else if (isShiftLocked && !mState.isShiftKeyIgnoring() && !withSliding) { // Shift has been pressed without chording while caps lock state. toggleCapsLock(); // To be able to turn off caps lock by "double tap" on shift key, we should ignore // the second tap of the "double tap" from now for a while because we just have // already turned off caps lock above. - mKeyboardView.startIgnoringDoubleTap(); - } else if (isShiftedOrShiftLocked() && shiftKeyState.isPressingOnShifted() + startIgnoringDoubleTap(); + } else if (isShiftedOrShiftLocked && mState.isShiftKeyPressingOnShifted() && !withSliding) { // Shift has been pressed without chording while shifted state. toggleShift(); - } else if (isManualTemporaryUpperCaseFromAuto() && shiftKeyState.isPressing() + } else if (isManualTemporaryUpperCaseFromAuto && mState.isShiftKeyPressing() && !withSliding) { // Shift has been pressed without chording while manual temporary upper case // transited from automatic temporary upper case. @@ -593,45 +607,46 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha toggleShift(); } } - shiftKeyState.onRelease(); + mState.onReleaseShift(); } public void onPressSymbol() { - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "onPressSymbol:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " symbolKeyState=" + mSymbolKeyState); + + " state=" + mState); + } changeKeyboardMode(); - mSymbolKeyState.onPress(); + mState.onPressSymbol(); mSwitchState = SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL; } public void onReleaseSymbol() { - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "onReleaseSymbol:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " symbolKeyState=" + mSymbolKeyState); + + " state=" + mState); + } // 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) { changeKeyboardMode(); } - mSymbolKeyState.onRelease(); + mState.onReleaseSymbol(); } public void onOtherKeyPressed() { - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "onOtherKeyPressed:" + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState - + " symbolKeyState=" + mSymbolKeyState); - mShiftKeyState.onOtherKeyPressed(); - mSymbolKeyState.onOtherKeyPressed(); + + " state=" + mState); + } + mState.onOtherKeyPressed(); } public void onCancelInput() { // Snap back to the previous keyboard mode if the user cancels sliding input. - if (getPointerCount() == 1) { + if (isSinglePointer()) { if (mSwitchState == SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL) { changeKeyboardMode(); } else if (mSwitchState == SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE) { @@ -640,6 +655,19 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } } + private boolean mPrevMainKeyboardWasShiftLocked; + + private void toggleKeyboardMode() { + if (mCurrentId.equals(mMainKeyboardId)) { + mPrevMainKeyboardWasShiftLocked = isShiftLocked(); + setKeyboard(getKeyboard(mSymbolsKeyboardId)); + } else { + setKeyboard(getKeyboard(mMainKeyboardId)); + setShiftLocked(mPrevMainKeyboardWasShiftLocked); + mPrevMainKeyboardWasShiftLocked = false; + } + } + private void toggleShiftInSymbol() { if (isAlphabetMode()) return; @@ -662,21 +690,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return mKeyboardView != null && !mKeyboardView.isInSlidingKeyInput(); } - private int getPointerCount() { - return mKeyboardView == null ? 0 : mKeyboardView.getPointerCount(); - } - - private boolean mPrevMainKeyboardWasShiftLocked; - - private void toggleKeyboardMode() { - if (mCurrentId.equals(mMainKeyboardId)) { - mPrevMainKeyboardWasShiftLocked = isShiftLocked(); - setKeyboard(getKeyboard(mSymbolsKeyboardId)); - } else { - setKeyboard(getKeyboard(mMainKeyboardId)); - setShiftLocked(mPrevMainKeyboardWasShiftLocked); - mPrevMainKeyboardWasShiftLocked = false; - } + private boolean isSinglePointer() { + return mKeyboardView != null && mKeyboardView.getPointerCount() == 1; } public boolean hasDistinctMultitouch() { @@ -697,9 +712,10 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha * Updates state machine to figure out when to automatically snap back to the previous mode. */ public void onKey(int code) { - if (DEBUG_STATE) + if (DEBUG_STATE) { Log.d(TAG, "onKey: code=" + code + " switchState=" + mSwitchState - + " pointers=" + getPointerCount()); + + " isSinglePointer=" + isSinglePointer()); + } switch (mSwitchState) { case SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL: // Only distinct multi touch devices can be in this state. @@ -715,7 +731,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } else { mSwitchState = SWITCH_STATE_SYMBOL_BEGIN; } - } else if (getPointerCount() == 1) { + } 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 @@ -731,7 +747,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha 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 (getPointerCount() == 1) { + } 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. toggleShift(); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java new file mode 100644 index 000000000..b6e4f2daa --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java @@ -0,0 +1,113 @@ +/* + * 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; + +// TODO: Add unit tests +public class KeyboardState { + // TODO: Combine these key state objects with auto mode switch state. + private ShiftKeyState mShiftKeyState = new ShiftKeyState("Shift"); + private ModifierKeyState mSymbolKeyState = new ModifierKeyState("Symbol"); + + public KeyboardState() { + } + + // TODO: Get rid of this method + public boolean isShiftKeyIgnoring() { + return mShiftKeyState.isIgnoring(); + } + + // TODO: Get rid of this method + public boolean isShiftKeyReleasing() { + return mShiftKeyState.isReleasing(); + } + + // TODO: Get rid of this method + public boolean isShiftKeyMomentary() { + return mShiftKeyState.isMomentary(); + } + + // TODO: Get rid of this method + public boolean isShiftKeyPressing() { + return mShiftKeyState.isPressing(); + } + + // TODO: Get rid of this method + public boolean isShiftKeyPressingOnShifted() { + return mShiftKeyState.isPressingOnShifted(); + } + + public void onToggleCapsLock() { + mShiftKeyState.onRelease(); + } + + public void onPressSymbol() { + mSymbolKeyState.onPress(); + } + + public void onReleaseSymbol() { + mSymbolKeyState.onRelease(); + } + + public void onOtherKeyPressed() { + mShiftKeyState.onOtherKeyPressed(); + mSymbolKeyState.onOtherKeyPressed(); + } + + public void onUpdateShiftState(boolean isAlphabetMode) { + if (!isAlphabetMode) { + // In symbol keyboard mode, we should clear shift key state because only alphabet + // keyboard has shift key. + mSymbolKeyState.onRelease(); + } + } + + // TODO: Get rid of these boolean arguments. + public void onPressShift(boolean isAlphabetMode, boolean isShiftLocked, + boolean isAutomaticTemporaryUpperCase, boolean isShiftedOrShiftLocked) { + if (isAlphabetMode) { + if (isShiftLocked) { + // 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. + mShiftKeyState.onPress(); + } else if (isAutomaticTemporaryUpperCase) { + // Shift key is pressed while automatic temporary upper case, we have to move to + // manual temporary upper case. + mShiftKeyState.onPress(); + } else if (isShiftedOrShiftLocked) { + // 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. + mShiftKeyState.onPress(); + } + } else { + // In symbol mode, just toggle symbol and symbol more keyboard. + mShiftKeyState.onPress(); + } + } + + public void onReleaseShift() { + mShiftKeyState.onRelease(); + } + + @Override + public String toString() { + return "[shift=" + mShiftKeyState + + " symbol=" + mSymbolKeyState + "]"; + } +} diff --git a/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java index dae73c4e4..f8620910a 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java @@ -20,7 +20,7 @@ import android.util.Log; import com.android.inputmethod.keyboard.KeyboardSwitcher; -public class ModifierKeyState { +/* package */ class ModifierKeyState { protected static final String TAG = "ModifierKeyState"; protected static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE; diff --git a/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java index 6617b917f..6f54b4f23 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java @@ -18,7 +18,7 @@ package com.android.inputmethod.keyboard.internal; import android.util.Log; -public class ShiftKeyState extends ModifierKeyState { +/* package */ class ShiftKeyState extends ModifierKeyState { private static final int PRESSING_ON_SHIFTED = 3; // both temporary shifted & shift locked private static final int IGNORING = 4; |