1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.keyboard.internal; 18 19 import android.text.TextUtils; 20 21 import com.android.inputmethod.keyboard.Keyboard; 22 23 public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { 24 public interface Constants { 25 // Argument for {@link KeyboardState#onPressKey} and {@link KeyboardState#onReleaseKey}. 26 public static final boolean NOT_SLIDING = false; 27 public static final boolean SLIDING = true; 28 // Argument for {@link KeyboardState#onCodeInput}. 29 public static final boolean SINGLE = true; 30 public static final boolean MULTI = false; 31 public static final int CAP_MODE_OFF = 32 com.android.inputmethod.latin.Constants.TextUtils.CAP_MODE_OFF; 33 public static final int CAP_MODE_WORDS = TextUtils.CAP_MODE_WORDS; 34 public static final int CAP_MODE_CHARACTERS = TextUtils.CAP_MODE_CHARACTERS; 35 36 public static final int CODE_SHIFT = Keyboard.CODE_SHIFT; 37 public static final int CODE_SYMBOL = Keyboard.CODE_SWITCH_ALPHA_SYMBOL; 38 public static final int CODE_SPACE = Keyboard.CODE_SPACE; 39 public static final int CODE_AUTO_CAPS_TRIGGER = Keyboard.CODE_SPACE; 40 41 public static final int ALPHABET_UNSHIFTED = 0; 42 public static final int ALPHABET_MANUAL_SHIFTED = 1; 43 public static final int ALPHABET_AUTOMATIC_SHIFTED = 2; 44 public static final int ALPHABET_SHIFT_LOCKED = 3; 45 public static final int ALPHABET_SHIFT_LOCK_SHIFTED = 4; 46 public static final int SYMBOLS_UNSHIFTED = 5; 47 public static final int SYMBOLS_SHIFTED = 6; 48 } 49 50 private int mLayout = Constants.ALPHABET_UNSHIFTED; 51 52 private int mAutoCapsMode = Constants.CAP_MODE_OFF; 53 // Following InputConnection's behavior. Simulating InputType.TYPE_TEXT_FLAG_CAP_WORDS. 54 private int mAutoCapsState = Constants.CAP_MODE_OFF; 55 56 private boolean mIsInDoubleTapTimeout; 57 private int mLongPressTimeoutCode; 58 59 private final KeyboardState mState = new KeyboardState(this); 60 getLayoutId()61 public int getLayoutId() { 62 return mLayout; 63 } 64 getLayoutName(int layoutId)65 public static String getLayoutName(int layoutId) { 66 switch (layoutId) { 67 case Constants.ALPHABET_UNSHIFTED: return "ALPHABET_UNSHIFTED"; 68 case Constants.ALPHABET_MANUAL_SHIFTED: return "ALPHABET_MANUAL_SHIFTED"; 69 case Constants.ALPHABET_AUTOMATIC_SHIFTED: return "ALPHABET_AUTOMATIC_SHIFTED"; 70 case Constants.ALPHABET_SHIFT_LOCKED: return "ALPHABET_SHIFT_LOCKED"; 71 case Constants.ALPHABET_SHIFT_LOCK_SHIFTED: return "ALPHABET_SHIFT_LOCK_SHIFTED"; 72 case Constants.SYMBOLS_UNSHIFTED: return "SYMBOLS_UNSHIFTED"; 73 case Constants.SYMBOLS_SHIFTED: return "SYMBOLS_SHIFTED"; 74 default: return "UNKNOWN<" + layoutId + ">"; 75 } 76 } 77 setAutoCapsMode(int autoCaps)78 public void setAutoCapsMode(int autoCaps) { 79 mAutoCapsMode = autoCaps; 80 mAutoCapsState = autoCaps; 81 } 82 expireDoubleTapTimeout()83 public void expireDoubleTapTimeout() { 84 mIsInDoubleTapTimeout = false; 85 } 86 87 @Override setAlphabetKeyboard()88 public void setAlphabetKeyboard() { 89 mLayout = Constants.ALPHABET_UNSHIFTED; 90 } 91 92 @Override setAlphabetManualShiftedKeyboard()93 public void setAlphabetManualShiftedKeyboard() { 94 mLayout = Constants.ALPHABET_MANUAL_SHIFTED; 95 } 96 97 @Override setAlphabetAutomaticShiftedKeyboard()98 public void setAlphabetAutomaticShiftedKeyboard() { 99 mLayout = Constants.ALPHABET_AUTOMATIC_SHIFTED; 100 } 101 102 @Override setAlphabetShiftLockedKeyboard()103 public void setAlphabetShiftLockedKeyboard() { 104 mLayout = Constants.ALPHABET_SHIFT_LOCKED; 105 } 106 107 @Override setAlphabetShiftLockShiftedKeyboard()108 public void setAlphabetShiftLockShiftedKeyboard() { 109 mLayout = Constants.ALPHABET_SHIFT_LOCK_SHIFTED; 110 } 111 112 @Override setSymbolsKeyboard()113 public void setSymbolsKeyboard() { 114 mLayout = Constants.SYMBOLS_UNSHIFTED; 115 } 116 117 @Override setSymbolsShiftedKeyboard()118 public void setSymbolsShiftedKeyboard() { 119 mLayout = Constants.SYMBOLS_SHIFTED; 120 } 121 122 @Override requestUpdatingShiftState()123 public void requestUpdatingShiftState() { 124 mState.onUpdateShiftState(mAutoCapsState); 125 } 126 127 @Override startDoubleTapTimer()128 public void startDoubleTapTimer() { 129 mIsInDoubleTapTimeout = true; 130 } 131 132 @Override cancelDoubleTapTimer()133 public void cancelDoubleTapTimer() { 134 mIsInDoubleTapTimeout = false; 135 } 136 137 @Override isInDoubleTapTimeout()138 public boolean isInDoubleTapTimeout() { 139 return mIsInDoubleTapTimeout; 140 } 141 142 @Override startLongPressTimer(int code)143 public void startLongPressTimer(int code) { 144 mLongPressTimeoutCode = code; 145 } 146 147 @Override cancelLongPressTimer()148 public void cancelLongPressTimer() { 149 mLongPressTimeoutCode = 0; 150 } 151 152 @Override hapticAndAudioFeedback(int code)153 public void hapticAndAudioFeedback(int code) { 154 // Nothing to do. 155 } 156 onLongPressTimeout(int code)157 public void onLongPressTimeout(int code) { 158 // TODO: Handle simultaneous long presses. 159 if (mLongPressTimeoutCode == code) { 160 mLongPressTimeoutCode = 0; 161 mState.onLongPressTimeout(code); 162 } 163 } 164 updateShiftState()165 public void updateShiftState() { 166 mState.onUpdateShiftState(mAutoCapsState); 167 } 168 loadKeyboard(String layoutSwitchBackSymbols)169 public void loadKeyboard(String layoutSwitchBackSymbols) { 170 mState.onLoadKeyboard(layoutSwitchBackSymbols); 171 } 172 saveKeyboardState()173 public void saveKeyboardState() { 174 mState.onSaveKeyboardState(); 175 } 176 onPressKey(int code, boolean isSinglePointer)177 public void onPressKey(int code, boolean isSinglePointer) { 178 mState.onPressKey(code, isSinglePointer, mAutoCapsState); 179 } 180 onReleaseKey(int code, boolean withSliding)181 public void onReleaseKey(int code, boolean withSliding) { 182 mState.onReleaseKey(code, withSliding); 183 if (mLongPressTimeoutCode == code) { 184 mLongPressTimeoutCode = 0; 185 } 186 } 187 onCodeInput(int code, boolean isSinglePointer)188 public void onCodeInput(int code, boolean isSinglePointer) { 189 if (mAutoCapsMode == Constants.CAP_MODE_WORDS) { 190 if (Keyboard.isLetterCode(code)) { 191 mAutoCapsState = (code == Constants.CODE_AUTO_CAPS_TRIGGER) 192 ? mAutoCapsMode : Constants.CAP_MODE_OFF; 193 } 194 } else { 195 mAutoCapsState = mAutoCapsMode; 196 } 197 mState.onCodeInput(code, isSinglePointer, mAutoCapsState); 198 } 199 onCancelInput(boolean isSinglePointer)200 public void onCancelInput(boolean isSinglePointer) { 201 mState.onCancelInput(isSinglePointer); 202 } 203 }