1 /*
2  * Copyright (C) 2008-2012  OMRON SOFTWARE Co., Ltd.
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 jp.co.omronsoft.openwnn.EN;
18 
19 import jp.co.omronsoft.openwnn.*;
20 import android.content.SharedPreferences;
21 import android.view.KeyEvent;
22 import android.view.View;
23 import android.view.inputmethod.EditorInfo;
24 import android.view.inputmethod.InputConnection;
25 
26 import jp.co.omronsoft.openwnn.Keyboard;
27 
28 import android.util.Log;
29 
30 /**
31  * The default Software Keyboard class for English IME.
32  *
33  * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
34  */
35 public class DefaultSoftKeyboardEN extends DefaultSoftKeyboard {
36     /** 12-key keyboard [PHONE MODE] */
37     public static final int KEYCODE_PHONE  = -116;
38 
39 	/**
40      * Keyboards toggled by ALT key.
41      * <br>
42      * The normal keyboard(KEYMODE_EN_ALPHABET) and the number/symbol
43      * keyboard(KEYMODE_EN_NUMBER) is active.  The phone number
44      * keyboard(KEYMODE_EN_PHONE) is disabled.
45      */
46     private static final boolean[] TOGGLE_KEYBOARD = {true, true, false};
47 
48     /** Auto caps mode */
49     private boolean mAutoCaps = false;
50 
51 	/**
52      * Default constructor
53      */
DefaultSoftKeyboardEN()54     public DefaultSoftKeyboardEN() { }
55 
56     /**
57      * Dismiss the pop-up keyboard.
58      * <br>
59      * Nothing will be done if no pop-up keyboard is displaying.
60      */
dismissPopupKeyboard()61     public void dismissPopupKeyboard() {
62     	try {
63     		if (mKeyboardView != null) {
64     			mKeyboardView.handleBack();
65     		}
66     	} catch (Exception ex) {
67     		/* ignore */
68     	}
69     }
70 
71     /** @see jp.co.omronsoft.openwnn.DefaultSoftKeyboard#createKeyboards */
createKeyboards(OpenWnn parent)72     @Override protected void createKeyboards(OpenWnn parent) {
73         mKeyboard = new Keyboard[3][2][4][2][7][2];
74 
75         Keyboard[][] keyList;
76         /***********************************************************************
77          * English
78          ***********************************************************************/
79         /* qwerty shift_off */
80         keyList = mKeyboard[LANG_EN][PORTRAIT][KEYBOARD_QWERTY][KEYBOARD_SHIFT_OFF];
81         keyList[KEYMODE_EN_ALPHABET][0] = new Keyboard(parent, R.xml.default_en_qwerty);
82         keyList[KEYMODE_EN_NUMBER][0]   = new Keyboard(parent, R.xml.default_en_symbols);
83         keyList[KEYMODE_EN_PHONE][0]    = new Keyboard(parent, R.xml.keyboard_12key_phone);
84 
85         /* qwerty shift_on */
86         keyList = mKeyboard[LANG_EN][PORTRAIT][KEYBOARD_QWERTY][KEYBOARD_SHIFT_ON];
87         keyList[KEYMODE_EN_ALPHABET][0] =
88             mKeyboard[LANG_EN][PORTRAIT][KEYBOARD_QWERTY][KEYBOARD_SHIFT_OFF][KEYMODE_EN_ALPHABET][0];
89         keyList[KEYMODE_EN_NUMBER][0]   = new Keyboard(parent, R.xml.default_en_symbols_shift);
90         keyList[KEYMODE_EN_PHONE][0]    = new Keyboard(parent, R.xml.keyboard_12key_phone);
91     }
92 
93     /**
94      * Get the shift key state from the editor.
95      *
96      * @param editor	The information of editor
97      * @return 		state ID of the shift key (0:off, 1:on)
98      */
getShiftKeyState(EditorInfo editor)99     private int getShiftKeyState(EditorInfo editor) {
100         InputConnection connection = mWnn.getCurrentInputConnection();
101         if (connection != null) {
102             int caps = connection.getCursorCapsMode(editor.inputType);
103             return (caps == 0) ? 0 : 1;
104         } else {
105             return 0;
106         }
107     }
108 
109     /**
110      * Switch the keymode
111      *
112      * @param keyMode		Keymode
113      */
changeKeyMode(int keyMode)114 	private void changeKeyMode(int keyMode) {
115 		Keyboard keyboard = super.getModeChangeKeyboard(keyMode);
116     	if (keyboard != null) {
117     		mCurrentKeyMode = keyMode;
118     		super.changeKeyboard(keyboard);
119 		}
120 	}
121 
122     /***********************************************************************
123      * from DefaultSoftKeyboard
124      ***********************************************************************/
125     /** @see jp.co.omronsoft.openwnn.DefaultSoftKeyboard#initView */
initView(OpenWnn parent, int width, int height)126     @Override public View initView(OpenWnn parent, int width, int height) {
127         View view = super.initView(parent, width, height);
128 
129     	/* default setting */
130     	mCurrentLanguage     = LANG_EN;
131     	mCurrentKeyboardType = KEYBOARD_QWERTY;
132     	mShiftOn             = KEYBOARD_SHIFT_OFF;
133     	mCurrentKeyMode      = KEYMODE_EN_ALPHABET;
134 
135     	Keyboard kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn][mCurrentKeyMode][0];
136     	if (kbd == null) {
137     		if(mDisplayMode == LANDSCAPE){
138     			return view;
139     		}
140     		return null;
141     	}
142     	mCurrentKeyboard = null;
143     	changeKeyboard(kbd);
144     	return view;
145     }
146 
147     /** @see jp.co.omronsoft.openwnn.DefaultSoftKeyboard#setPreferences */
setPreferences(SharedPreferences pref, EditorInfo editor)148     @Override public void setPreferences(SharedPreferences pref, EditorInfo editor) {
149         super.setPreferences(pref, editor);
150 
151         /* auto caps mode */
152         mAutoCaps = pref.getBoolean("auto_caps", true);
153 
154         switch (editor.inputType & EditorInfo.TYPE_MASK_CLASS) {
155         case EditorInfo.TYPE_CLASS_NUMBER:
156         case EditorInfo.TYPE_CLASS_DATETIME:
157             mCurrentLanguage     = LANG_EN;
158             mCurrentKeyboardType = KEYBOARD_QWERTY;
159             mShiftOn             = KEYBOARD_SHIFT_OFF;
160             mCurrentKeyMode      = KEYMODE_EN_NUMBER;
161 
162             Keyboard kbdn =
163                 mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn][mCurrentKeyMode][0];
164 
165             changeKeyboard(kbdn);
166             break;
167 
168         case EditorInfo.TYPE_CLASS_PHONE:
169             mCurrentLanguage     = LANG_EN;
170             mCurrentKeyboardType = KEYBOARD_QWERTY;
171             mShiftOn             = KEYBOARD_SHIFT_OFF;
172             mCurrentKeyMode      = KEYMODE_EN_PHONE;
173 
174             Keyboard kbdp =
175                 mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn][mCurrentKeyMode][0];
176 
177             changeKeyboard(kbdp);
178 
179             break;
180 
181         default:
182             mCurrentLanguage     = LANG_EN;
183             mCurrentKeyboardType = KEYBOARD_QWERTY;
184             mShiftOn             = KEYBOARD_SHIFT_OFF;
185             mCurrentKeyMode      = KEYMODE_EN_ALPHABET;
186 
187             Keyboard kbdq =
188                 mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn][mCurrentKeyMode][0];
189 
190             changeKeyboard(kbdq);
191              break;
192         }
193 
194         int shift = (mAutoCaps)? getShiftKeyState(mWnn.getCurrentInputEditorInfo()) : 0;
195         if (shift != mShiftOn) {
196             Keyboard kbd = getShiftChangeKeyboard(shift);
197             mShiftOn = shift;
198             changeKeyboard(kbd);
199         }
200     }
201 
202     /** @see jp.co.omronsoft.openwnn.DefaultSoftKeyboard#onKey */
onKey(int primaryCode, int[] keyCodes)203     @Override public void onKey(int primaryCode, int[] keyCodes) {
204         switch (primaryCode) {
205         case KEYCODE_QWERTY_HAN_ALPHA:
206             this.changeKeyMode(KEYMODE_EN_ALPHABET);
207             break;
208 
209         case KEYCODE_QWERTY_HAN_NUM:
210             this.changeKeyMode(KEYMODE_EN_NUMBER);
211             break;
212 
213         case KEYCODE_PHONE:
214             this.changeKeyMode(KEYMODE_EN_PHONE);
215             break;
216 
217         case KEYCODE_QWERTY_EMOJI:
218             mWnn.onEvent(new OpenWnnEvent(OpenWnnEvent.LIST_SYMBOLS));
219             break;
220 
221         case KEYCODE_QWERTY_TOGGLE_MODE:
222             switch(mCurrentKeyMode){
223             case KEYMODE_EN_ALPHABET:
224                 if (TOGGLE_KEYBOARD[KEYMODE_EN_NUMBER]){
225                     mCurrentKeyMode = KEYMODE_EN_NUMBER;
226                 } else if (TOGGLE_KEYBOARD[KEYMODE_EN_PHONE]) {
227                     mCurrentKeyMode = KEYMODE_EN_PHONE;
228                 }
229                 break;
230             case KEYMODE_EN_NUMBER:
231                 if (TOGGLE_KEYBOARD[KEYMODE_EN_PHONE]) {
232                     mCurrentKeyMode = KEYMODE_EN_PHONE;
233                 } else if(TOGGLE_KEYBOARD[KEYMODE_EN_ALPHABET]) {
234                     mCurrentKeyMode = KEYMODE_EN_ALPHABET;
235                 }
236                 break;
237             case KEYMODE_EN_PHONE:
238                 if (TOGGLE_KEYBOARD[KEYMODE_EN_ALPHABET]) {
239                     mCurrentKeyMode = KEYMODE_EN_ALPHABET;
240                 } else if (TOGGLE_KEYBOARD[KEYMODE_EN_NUMBER]) {
241                     mCurrentKeyMode = KEYMODE_EN_NUMBER;
242                 }
243                 break;
244             }
245             Keyboard kbdp =
246                 mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn][mCurrentKeyMode][0];
247             super.changeKeyboard(kbdp);
248             break;
249 
250         case DefaultSoftKeyboard.KEYCODE_QWERTY_BACKSPACE:
251         case DefaultSoftKeyboard.KEYCODE_JP12_BACKSPACE:
252             mWnn.onEvent(new OpenWnnEvent(OpenWnnEvent.INPUT_SOFT_KEY,
253                                           new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)));
254             break;
255 
256         case DefaultSoftKeyboard.KEYCODE_QWERTY_SHIFT:
257             toggleShiftLock();
258             break;
259 
260         case DefaultSoftKeyboard.KEYCODE_QWERTY_ALT:
261             processAltKey();
262             break;
263 
264         case KEYCODE_QWERTY_ENTER:
265         case KEYCODE_JP12_ENTER:
266             mWnn.onEvent(new OpenWnnEvent(OpenWnnEvent.INPUT_SOFT_KEY,
267                                           new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)));
268             break;
269 
270         case KEYCODE_JP12_LEFT:
271             mWnn.onEvent(new OpenWnnEvent(OpenWnnEvent.INPUT_SOFT_KEY,
272                                           new KeyEvent(KeyEvent.ACTION_DOWN,
273                                                        KeyEvent.KEYCODE_DPAD_LEFT)));
274             break;
275 
276         case KEYCODE_JP12_RIGHT:
277             mWnn.onEvent(new OpenWnnEvent(OpenWnnEvent.INPUT_SOFT_KEY,
278                                           new KeyEvent(KeyEvent.ACTION_DOWN,
279                                                        KeyEvent.KEYCODE_DPAD_RIGHT)));
280         default:
281             if (primaryCode >= 0) {
282                 if (mKeyboardView.isShifted()) {
283                     primaryCode = Character.toUpperCase(primaryCode);
284                 }
285                 mWnn.onEvent(new OpenWnnEvent(OpenWnnEvent.INPUT_CHAR, (char)primaryCode));
286             }
287         }
288 
289         /* update shift key's state */
290         if (!mCapsLock && primaryCode != KEYCODE_QWERTY_SHIFT) {
291             if(mCurrentKeyMode != KEYMODE_EN_NUMBER){
292                 int shift = (mAutoCaps)? getShiftKeyState(mWnn.getCurrentInputEditorInfo()) : 0;
293                 if (shift != mShiftOn) {
294                     Keyboard kbd = getShiftChangeKeyboard(shift);
295                     mShiftOn = shift;
296                     changeKeyboard(kbd);
297                 }
298             }else{
299                 mShiftOn = KEYBOARD_SHIFT_OFF;
300                 Keyboard kbd = getShiftChangeKeyboard(mShiftOn);
301                 changeKeyboard(kbd);
302             }
303         }
304     }
305 }
306 
307 
308 
309