1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.keyboard.internal; 18 19 import android.text.TextUtils; 20 21 import com.android.inputmethod.latin.Constants; 22 import com.android.inputmethod.latin.utils.RecapitalizeStatus; 23 24 public class MockKeyboardSwitcher implements KeyboardState.SwitchActions { 25 public interface MockConstants { 26 // Argument for {@link KeyboardState#onPressKey} and {@link KeyboardState#onReleaseKey}. 27 public static final boolean NOT_SLIDING = false; 28 public static final boolean SLIDING = true; 29 // Argument for {@link KeyboardState#onCodeInput}. 30 public static final boolean SINGLE = true; 31 public static final boolean MULTI = false; 32 public static final int CAP_MODE_OFF = 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 = Constants.CODE_SHIFT; 37 public static final int CODE_SYMBOL = Constants.CODE_SWITCH_ALPHA_SYMBOL; 38 public static final int CODE_SPACE = Constants.CODE_SPACE; 39 public static final int CODE_AUTO_CAPS_TRIGGER = Constants.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 = MockConstants.ALPHABET_UNSHIFTED; 51 52 private int mAutoCapsMode = MockConstants.CAP_MODE_OFF; 53 // Following InputConnection's behavior. Simulating InputType.TYPE_TEXT_FLAG_CAP_WORDS. 54 private int mAutoCapsState = MockConstants.CAP_MODE_OFF; 55 56 private boolean mIsInDoubleTapShiftKeyTimeout; 57 private int mLongPressTimeoutCode; 58 59 private final KeyboardState mState = new KeyboardState(this); 60 61 public int getLayoutId() { 62 return mLayout; 63 } 64 65 public static String getLayoutName(final int layoutId) { 66 switch (layoutId) { 67 case MockConstants.ALPHABET_UNSHIFTED: return "ALPHABET_UNSHIFTED"; 68 case MockConstants.ALPHABET_MANUAL_SHIFTED: return "ALPHABET_MANUAL_SHIFTED"; 69 case MockConstants.ALPHABET_AUTOMATIC_SHIFTED: return "ALPHABET_AUTOMATIC_SHIFTED"; 70 case MockConstants.ALPHABET_SHIFT_LOCKED: return "ALPHABET_SHIFT_LOCKED"; 71 case MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED: return "ALPHABET_SHIFT_LOCK_SHIFTED"; 72 case MockConstants.SYMBOLS_UNSHIFTED: return "SYMBOLS_UNSHIFTED"; 73 case MockConstants.SYMBOLS_SHIFTED: return "SYMBOLS_SHIFTED"; 74 default: return "UNKNOWN<" + layoutId + ">"; 75 } 76 } 77 78 public void setAutoCapsMode(final int autoCaps) { 79 mAutoCapsMode = autoCaps; 80 mAutoCapsState = autoCaps; 81 } 82 83 public void expireDoubleTapTimeout() { 84 mIsInDoubleTapShiftKeyTimeout = false; 85 } 86 87 @Override 88 public void setAlphabetKeyboard() { 89 mLayout = MockConstants.ALPHABET_UNSHIFTED; 90 } 91 92 @Override 93 public void setAlphabetManualShiftedKeyboard() { 94 mLayout = MockConstants.ALPHABET_MANUAL_SHIFTED; 95 } 96 97 @Override 98 public void setAlphabetAutomaticShiftedKeyboard() { 99 mLayout = MockConstants.ALPHABET_AUTOMATIC_SHIFTED; 100 } 101 102 @Override 103 public void setAlphabetShiftLockedKeyboard() { 104 mLayout = MockConstants.ALPHABET_SHIFT_LOCKED; 105 } 106 107 @Override 108 public void setAlphabetShiftLockShiftedKeyboard() { 109 mLayout = MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED; 110 } 111 112 @Override 113 public void setSymbolsKeyboard() { 114 mLayout = MockConstants.SYMBOLS_UNSHIFTED; 115 } 116 117 @Override 118 public void setSymbolsShiftedKeyboard() { 119 mLayout = MockConstants.SYMBOLS_SHIFTED; 120 } 121 122 @Override 123 public void setEmojiKeyboard() { 124 // Just ignore. 125 } 126 127 @Override 128 public void requestUpdatingShiftState() { 129 mState.onUpdateShiftState(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE); 130 } 131 132 @Override 133 public void startDoubleTapShiftKeyTimer() { 134 mIsInDoubleTapShiftKeyTimeout = true; 135 } 136 137 @Override 138 public void cancelDoubleTapShiftKeyTimer() { 139 mIsInDoubleTapShiftKeyTimeout = false; 140 } 141 142 @Override 143 public boolean isInDoubleTapShiftKeyTimeout() { 144 return mIsInDoubleTapShiftKeyTimeout; 145 } 146 147 public void updateShiftState() { 148 mState.onUpdateShiftState(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE); 149 } 150 151 public void loadKeyboard() { 152 mState.onLoadKeyboard(); 153 } 154 155 public void saveKeyboardState() { 156 mState.onSaveKeyboardState(); 157 } 158 159 public void onPressKey(final int code, final boolean isSinglePointer) { 160 mState.onPressKey(code, isSinglePointer, mAutoCapsState); 161 } 162 163 public void onReleaseKey(final int code, final boolean withSliding) { 164 mState.onReleaseKey(code, withSliding); 165 if (mLongPressTimeoutCode == code) { 166 mLongPressTimeoutCode = 0; 167 } 168 } 169 170 public void onCodeInput(final int code) { 171 if (mAutoCapsMode == MockConstants.CAP_MODE_WORDS) { 172 if (Constants.isLetterCode(code)) { 173 mAutoCapsState = (code == MockConstants.CODE_AUTO_CAPS_TRIGGER) 174 ? mAutoCapsMode : MockConstants.CAP_MODE_OFF; 175 } 176 } else { 177 mAutoCapsState = mAutoCapsMode; 178 } 179 mState.onCodeInput(code, mAutoCapsState); 180 } 181 182 public void onFinishSlidingInput() { 183 mState.onFinishSlidingInput(); 184 } 185 } 186