• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.event.Event;
22 import com.android.inputmethod.latin.common.Constants;
23 import com.android.inputmethod.latin.utils.RecapitalizeStatus;
24 
25 public class MockKeyboardSwitcher implements KeyboardState.SwitchActions {
26     public interface MockConstants {
27         // Argument for {@link KeyboardState#onPressKey} and {@link KeyboardState#onReleaseKey}.
28         public static final boolean NOT_SLIDING = false;
29         public static final boolean SLIDING = true;
30         // Argument for {@link KeyboardState#onEvent}.
31         public static final boolean SINGLE = true;
32         public static final boolean MULTI = false;
33         public static final int CAP_MODE_OFF = Constants.TextUtils.CAP_MODE_OFF;
34         public static final int CAP_MODE_WORDS = TextUtils.CAP_MODE_WORDS;
35         public static final int CAP_MODE_CHARACTERS = TextUtils.CAP_MODE_CHARACTERS;
36 
37         public static final int CODE_SHIFT = Constants.CODE_SHIFT;
38         public static final int CODE_SYMBOL = Constants.CODE_SWITCH_ALPHA_SYMBOL;
39         public static final int CODE_SPACE = Constants.CODE_SPACE;
40         public static final int CODE_AUTO_CAPS_TRIGGER = Constants.CODE_SPACE;
41 
42         public static final int ALPHABET_UNSHIFTED = 0;
43         public static final int ALPHABET_MANUAL_SHIFTED = 1;
44         public static final int ALPHABET_AUTOMATIC_SHIFTED = 2;
45         public static final int ALPHABET_SHIFT_LOCKED = 3;
46         public static final int ALPHABET_SHIFT_LOCK_SHIFTED = 4;
47         public static final int SYMBOLS_UNSHIFTED = 5;
48         public static final int SYMBOLS_SHIFTED = 6;
49     }
50 
51     private int mLayout = MockConstants.ALPHABET_UNSHIFTED;
52 
53     private int mAutoCapsMode = MockConstants.CAP_MODE_OFF;
54     // Following InputConnection's behavior. Simulating InputType.TYPE_TEXT_FLAG_CAP_WORDS.
55     private int mAutoCapsState = MockConstants.CAP_MODE_OFF;
56 
57     private boolean mIsInDoubleTapShiftKeyTimeout;
58     private int mLongPressTimeoutCode;
59 
60     private final KeyboardState mState = new KeyboardState(this);
61 
getLayoutId()62     public int getLayoutId() {
63         return mLayout;
64     }
65 
getLayoutName(final int layoutId)66     public static String getLayoutName(final int layoutId) {
67         switch (layoutId) {
68         case MockConstants.ALPHABET_UNSHIFTED: return "ALPHABET_UNSHIFTED";
69         case MockConstants.ALPHABET_MANUAL_SHIFTED: return "ALPHABET_MANUAL_SHIFTED";
70         case MockConstants.ALPHABET_AUTOMATIC_SHIFTED: return "ALPHABET_AUTOMATIC_SHIFTED";
71         case MockConstants.ALPHABET_SHIFT_LOCKED: return "ALPHABET_SHIFT_LOCKED";
72         case MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED: return "ALPHABET_SHIFT_LOCK_SHIFTED";
73         case MockConstants.SYMBOLS_UNSHIFTED: return "SYMBOLS_UNSHIFTED";
74         case MockConstants.SYMBOLS_SHIFTED: return "SYMBOLS_SHIFTED";
75         default: return "UNKNOWN<" + layoutId + ">";
76         }
77     }
78 
setAutoCapsMode(final int autoCaps)79     public void setAutoCapsMode(final int autoCaps) {
80         mAutoCapsMode = autoCaps;
81         mAutoCapsState = autoCaps;
82     }
83 
expireDoubleTapTimeout()84     public void expireDoubleTapTimeout() {
85         mIsInDoubleTapShiftKeyTimeout = false;
86     }
87 
88     @Override
setAlphabetKeyboard()89     public void setAlphabetKeyboard() {
90         mLayout = MockConstants.ALPHABET_UNSHIFTED;
91     }
92 
93     @Override
setAlphabetManualShiftedKeyboard()94     public void setAlphabetManualShiftedKeyboard() {
95         mLayout = MockConstants.ALPHABET_MANUAL_SHIFTED;
96     }
97 
98     @Override
setAlphabetAutomaticShiftedKeyboard()99     public void setAlphabetAutomaticShiftedKeyboard() {
100         mLayout = MockConstants.ALPHABET_AUTOMATIC_SHIFTED;
101     }
102 
103     @Override
setAlphabetShiftLockedKeyboard()104     public void setAlphabetShiftLockedKeyboard() {
105         mLayout = MockConstants.ALPHABET_SHIFT_LOCKED;
106     }
107 
108     @Override
setAlphabetShiftLockShiftedKeyboard()109     public void setAlphabetShiftLockShiftedKeyboard() {
110         mLayout = MockConstants.ALPHABET_SHIFT_LOCK_SHIFTED;
111     }
112 
113     @Override
setSymbolsKeyboard()114     public void setSymbolsKeyboard() {
115         mLayout = MockConstants.SYMBOLS_UNSHIFTED;
116     }
117 
118     @Override
setSymbolsShiftedKeyboard()119     public void setSymbolsShiftedKeyboard() {
120         mLayout = MockConstants.SYMBOLS_SHIFTED;
121     }
122 
123     @Override
setEmojiKeyboard()124     public void setEmojiKeyboard() {
125         // Just ignore.
126     }
127 
128     @Override
requestUpdatingShiftState(final int currentAutoCapsState, final int currentRecapitalizeState)129     public void requestUpdatingShiftState(final int currentAutoCapsState,
130             final int currentRecapitalizeState) {
131         mState.onUpdateShiftState(currentAutoCapsState, currentRecapitalizeState);
132     }
133 
134     @Override
startDoubleTapShiftKeyTimer()135     public void startDoubleTapShiftKeyTimer() {
136         mIsInDoubleTapShiftKeyTimeout = true;
137     }
138 
139     @Override
cancelDoubleTapShiftKeyTimer()140     public void cancelDoubleTapShiftKeyTimer() {
141         mIsInDoubleTapShiftKeyTimeout = false;
142     }
143 
144     @Override
isInDoubleTapShiftKeyTimeout()145     public boolean isInDoubleTapShiftKeyTimeout() {
146         return mIsInDoubleTapShiftKeyTimeout;
147     }
148 
updateShiftState()149     public void updateShiftState() {
150         mState.onUpdateShiftState(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
151     }
152 
loadKeyboard()153     public void loadKeyboard() {
154         mState.onLoadKeyboard(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
155     }
156 
saveKeyboardState()157     public void saveKeyboardState() {
158         mState.onSaveKeyboardState();
159     }
160 
onPressKey(final int code, final boolean isSinglePointer)161     public void onPressKey(final int code, final boolean isSinglePointer) {
162         mState.onPressKey(code, isSinglePointer, mAutoCapsState,
163                 RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
164     }
165 
onReleaseKey(final int code, final boolean withSliding)166     public void onReleaseKey(final int code, final boolean withSliding) {
167         onReleaseKey(code, withSliding, mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
168     }
169 
onReleaseKey(final int code, final boolean withSliding, final int currentAutoCapsState, final int currentRecapitalizeState)170     public void onReleaseKey(final int code, final boolean withSliding,
171             final int currentAutoCapsState, final int currentRecapitalizeState) {
172         mState.onReleaseKey(code, withSliding, currentAutoCapsState, currentRecapitalizeState);
173         if (mLongPressTimeoutCode == code) {
174             mLongPressTimeoutCode = 0;
175         }
176     }
177 
onCodeInput(final int code)178     public void onCodeInput(final int code) {
179         if (mAutoCapsMode == MockConstants.CAP_MODE_WORDS) {
180             if (Constants.isLetterCode(code)) {
181                 mAutoCapsState = (code == MockConstants.CODE_AUTO_CAPS_TRIGGER)
182                         ? mAutoCapsMode : MockConstants.CAP_MODE_OFF;
183             }
184         } else {
185             mAutoCapsState = mAutoCapsMode;
186         }
187         final Event event =
188                 Event.createSoftwareKeypressEvent(code /* codePoint */, code /* keyCode */,
189                         Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE,
190                         false /* isKeyRepeat */);
191         mState.onEvent(event, mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
192     }
193 
onFinishSlidingInput()194     public void onFinishSlidingInput() {
195         mState.onFinishSlidingInput(mAutoCapsState, RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE);
196     }
197 }
198