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 
getLayoutId()61     public int getLayoutId() {
62         return mLayout;
63     }
64 
getLayoutName(final int layoutId)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 
setAutoCapsMode(final int autoCaps)78     public void setAutoCapsMode(final int autoCaps) {
79         mAutoCapsMode = autoCaps;
80         mAutoCapsState = autoCaps;
81     }
82 
expireDoubleTapTimeout()83     public void expireDoubleTapTimeout() {
84         mIsInDoubleTapShiftKeyTimeout = false;
85     }
86 
87     @Override
setAlphabetKeyboard()88     public void setAlphabetKeyboard() {
89         mLayout = MockConstants.ALPHABET_UNSHIFTED;
90     }
91 
92     @Override
setAlphabetManualShiftedKeyboard()93     public void setAlphabetManualShiftedKeyboard() {
94         mLayout = MockConstants.ALPHABET_MANUAL_SHIFTED;
95     }
96 
97     @Override
setAlphabetAutomaticShiftedKeyboard()98     public void setAlphabetAutomaticShiftedKeyboard() {
99         mLayout = MockConstants.ALPHABET_AUTOMATIC_SHIFTED;
100     }
101 
102     @Override
setAlphabetShiftLockedKeyboard()103     public void setAlphabetShiftLockedKeyboard() {
104         mLayout = MockConstants.ALPHABET_SHIFT_LOCKED;
105     }
106 
107     @Override
setAlphabetShiftLockShiftedKeyboard()108     public void setAlphabetShiftLockShiftedKeyboard() {
109         mLayout = MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED;
110     }
111 
112     @Override
setSymbolsKeyboard()113     public void setSymbolsKeyboard() {
114         mLayout = MockConstants.SYMBOLS_UNSHIFTED;
115     }
116 
117     @Override
setSymbolsShiftedKeyboard()118     public void setSymbolsShiftedKeyboard() {
119         mLayout = MockConstants.SYMBOLS_SHIFTED;
120     }
121 
122     @Override
setEmojiKeyboard()123     public void setEmojiKeyboard() {
124         // Just ignore.
125     }
126 
127     @Override
requestUpdatingShiftState(final int currentAutoCapsState, final int currentRecapitalizeState)128     public void requestUpdatingShiftState(final int currentAutoCapsState,
129             final int currentRecapitalizeState) {
130         mState.onUpdateShiftState(currentAutoCapsState, currentRecapitalizeState);
131     }
132 
133     @Override
startDoubleTapShiftKeyTimer()134     public void startDoubleTapShiftKeyTimer() {
135         mIsInDoubleTapShiftKeyTimeout = true;
136     }
137 
138     @Override
cancelDoubleTapShiftKeyTimer()139     public void cancelDoubleTapShiftKeyTimer() {
140         mIsInDoubleTapShiftKeyTimeout = false;
141     }
142 
143     @Override
isInDoubleTapShiftKeyTimeout()144     public boolean isInDoubleTapShiftKeyTimeout() {
145         return mIsInDoubleTapShiftKeyTimeout;
146     }
147 
updateShiftState()148     public void updateShiftState() {
149         mState.onUpdateShiftState(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
150     }
151 
loadKeyboard()152     public void loadKeyboard() {
153         mState.onLoadKeyboard(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
154     }
155 
saveKeyboardState()156     public void saveKeyboardState() {
157         mState.onSaveKeyboardState();
158     }
159 
onPressKey(final int code, final boolean isSinglePointer)160     public void onPressKey(final int code, final boolean isSinglePointer) {
161         mState.onPressKey(code, isSinglePointer, mAutoCapsState,
162                 RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
163     }
164 
onReleaseKey(final int code, final boolean withSliding)165     public void onReleaseKey(final int code, final boolean withSliding) {
166         onReleaseKey(code, withSliding, mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
167     }
168 
onReleaseKey(final int code, final boolean withSliding, final int currentAutoCapsState, final int currentRecapitalizeState)169     public void onReleaseKey(final int code, final boolean withSliding,
170             final int currentAutoCapsState, final int currentRecapitalizeState) {
171         mState.onReleaseKey(code, withSliding, currentAutoCapsState, currentRecapitalizeState);
172         if (mLongPressTimeoutCode == code) {
173             mLongPressTimeoutCode = 0;
174         }
175     }
176 
onCodeInput(final int code)177     public void onCodeInput(final int code) {
178         if (mAutoCapsMode == MockConstants.CAP_MODE_WORDS) {
179             if (Constants.isLetterCode(code)) {
180                 mAutoCapsState = (code == MockConstants.CODE_AUTO_CAPS_TRIGGER)
181                         ? mAutoCapsMode : MockConstants.CAP_MODE_OFF;
182             }
183         } else {
184             mAutoCapsState = mAutoCapsMode;
185         }
186         mState.onCodeInput(code, mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
187     }
188 
onFinishSlidingInput()189     public void onFinishSlidingInput() {
190         mState.onFinishSlidingInput(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
191     }
192 }
193