2 * Copyright (C) 2008,2009 OMRON SOFTWARE Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.hiroshica.android.input.nicownn2;
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.view.inputmethod.EditorInfo;
22 import android.view.MotionEvent;
23 import android.widget.TextView;
24 import android.inputmethodservice.Keyboard;
25 import android.inputmethodservice.KeyboardView;
26 import android.content.SharedPreferences;
27 import android.preference.PreferenceManager;
28 import android.content.res.*;
29 import android.os.Vibrator;
30 import android.media.MediaPlayer;
31 import android.content.Context;
33 import android.view.GestureDetector;
34 import android.view.GestureDetector.OnGestureListener;
35 import android.util.DisplayMetrics;
36 import java.lang.Math;
37 import android.util.FloatMath;
39 import android.util.Log;
43 * The default software keyboard class.
45 * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD. All Rights Reserved.
47 public class DefaultSoftKeyboard implements InputViewManager, KeyboardView.OnKeyboardActionListener, KeyboardView.OnTouchListener, OnGestureListener {
49 *----------------------------------------------------------------------
50 * key codes for a software keyboard
51 *----------------------------------------------------------------------
53 /** Change the keyboard language */
54 public static final int KEYCODE_CHANGE_LANG = -500;
56 /* for Japanese 12-key keyboard */
57 /** Japanese 12-key keyboard [1] */
58 public static final int KEYCODE_JP12_1 = -201;
59 /** Japanese 12-key keyboard [2] */
60 public static final int KEYCODE_JP12_2 = -202;
61 /** Japanese 12-key keyboard [3] */
62 public static final int KEYCODE_JP12_3 = -203;
63 /** Japanese 12-key keyboard [4] */
64 public static final int KEYCODE_JP12_4 = -204;
65 /** Japanese 12-key keyboard [5] */
66 public static final int KEYCODE_JP12_5 = -205;
67 /** Japanese 12-key keyboard [6] */
68 public static final int KEYCODE_JP12_6 = -206;
69 /** Japanese 12-key keyboard [7] */
70 public static final int KEYCODE_JP12_7 = -207;
71 /** Japanese 12-key keyboard [8] */
72 public static final int KEYCODE_JP12_8 = -208;
73 /** Japanese 12-key keyboard [9] */
74 public static final int KEYCODE_JP12_9 = -209;
75 /** Japanese 12-key keyboard [0] */
76 public static final int KEYCODE_JP12_0 = -210;
77 /** Japanese 12-key keyboard [#] */
78 public static final int KEYCODE_JP12_SHARP = -211;
79 /** Japanese 12-key keyboard [*] */
80 public static final int KEYCODE_JP12_ASTER = -213;
81 /** Japanese 12-key keyboard [DEL] */
82 public static final int KEYCODE_JP12_BACKSPACE = -214;
83 /** Japanese 12-key keyboard [SPACE] */
84 public static final int KEYCODE_JP12_SPACE = -215;
85 /** Japanese 12-key keyboard [ENTER] */
86 public static final int KEYCODE_JP12_ENTER = -216;
87 /** Japanese 12-key keyboard [RIGHT ARROW] */
88 public static final int KEYCODE_JP12_RIGHT = -217;
89 /** Japanese 12-key keyboard [LEFT ARROW] */
90 public static final int KEYCODE_JP12_LEFT = -218;
91 /** Japanese 12-key keyboard [REVERSE TOGGLE] */
92 public static final int KEYCODE_JP12_REVERSE = -219;
93 /** Japanese 12-key keyboard [CLOSE] */
94 public static final int KEYCODE_JP12_CLOSE = -220;
95 /** Japanese 12-key keyboard [KEYBOARD TYPE CHANGE] */
96 public static final int KEYCODE_JP12_KBD = -221;
97 /** Japanese 12-key keyboard [EMOJI] */
98 public static final int KEYCODE_JP12_EMOJI = -222;
99 /** Japanese 12-key keyboard [FULL-WIDTH HIRAGANA MODE] */
100 public static final int KEYCODE_JP12_ZEN_HIRA = -223;
101 /** Japanese 12-key keyboard [FULL-WIDTH NUMBER MODE] */
102 public static final int KEYCODE_JP12_ZEN_NUM = -224;
103 /** Japanese 12-key keyboard [FULL-WIDTH ALPHABET MODE] */
104 public static final int KEYCODE_JP12_ZEN_ALPHA = -225;
105 /** Japanese 12-key keyboard [FULL-WIDTH KATAKANA MODE] */
106 public static final int KEYCODE_JP12_ZEN_KATA = -226;
107 /** Japanese 12-key keyboard [HALF-WIDTH KATAKANA MODE] */
108 public static final int KEYCODE_JP12_HAN_KATA = -227;
109 /** Japanese 12-key keyboard [HALF-WIDTH NUMBER MODE] */
110 public static final int KEYCODE_JP12_HAN_NUM = -228;
111 /** Japanese 12-key keyboard [HALF-WIDTH ALPHABET MODE] */
112 public static final int KEYCODE_JP12_HAN_ALPHA = -229;
113 /** Japanese 12-key keyboard [MODE TOOGLE CHANGE] */
114 public static final int KEYCODE_JP12_TOGGLE_MODE = -230;
116 /* for Qwerty keyboard */
117 /** Qwerty keyboard [DEL] */
118 public static final int KEYCODE_QWERTY_BACKSPACE = -100;
119 /** Qwerty keyboard [ENTER] */
120 public static final int KEYCODE_QWERTY_ENTER = -101;
121 /** Qwerty keyboard [SHIFT] */
122 public static final int KEYCODE_QWERTY_SHIFT = Keyboard.KEYCODE_SHIFT;
123 /** Qwerty keyboard [ALT] */
124 public static final int KEYCODE_QWERTY_ALT = -103;
125 /** Qwerty keyboard [KEYBOARD TYPE CHANGE] */
126 public static final int KEYCODE_QWERTY_KBD = -104;
127 /** Qwerty keyboard [CLOSE] */
128 public static final int KEYCODE_QWERTY_CLOSE = -105;
129 /** Japanese Qwerty keyboard [EMOJI] */
130 public static final int KEYCODE_QWERTY_EMOJI = -106;
131 /** Japanese Qwerty keyboard [FULL-WIDTH HIRAGANA MODE] */
132 public static final int KEYCODE_QWERTY_ZEN_HIRA = -107;
133 /** Japanese Qwerty keyboard [FULL-WIDTH NUMBER MODE] */
134 public static final int KEYCODE_QWERTY_ZEN_NUM = -108;
135 /** Japanese Qwerty keyboard [FULL-WIDTH ALPHABET MODE] */
136 public static final int KEYCODE_QWERTY_ZEN_ALPHA = -109;
137 /** Japanese Qwerty keyboard [FULL-WIDTH KATAKANA MODE] */
138 public static final int KEYCODE_QWERTY_ZEN_KATA = -110;
139 /** Japanese Qwerty keyboard [HALF-WIDTH KATAKANA MODE] */
140 public static final int KEYCODE_QWERTY_HAN_KATA = -111;
141 /** Qwerty keyboard [NUMBER MODE] */
142 public static final int KEYCODE_QWERTY_HAN_NUM = -112;
143 /** Qwerty keyboard [ALPHABET MODE] */
144 public static final int KEYCODE_QWERTY_HAN_ALPHA = -113;
145 /** Qwerty keyboard [MODE TOOGLE CHANGE] */
146 public static final int KEYCODE_QWERTY_TOGGLE_MODE = -114;
147 /** Qwerty keyboard [PINYIN MODE] */
148 public static final int KEYCODE_QWERTY_PINYIN = -115;
150 /** OpenWnn instance which hold this software keyboard*/
151 protected NicoWnn mWnn;
153 /** Current keyboard view */
154 protected KeyboardView mKeyboardView;
156 /** View objects (main side) */
157 protected ViewGroup mMainView;
158 /** View objects (sub side) */
159 protected ViewGroup mSubView;
161 /** Current keyboard definition */
162 protected Keyboard mCurrentKeyboard;
164 /** Caps lock state */
165 protected boolean mCapsLock;
167 /** Input restraint */
168 protected boolean mDisableKeyInput = true;
171 protected boolean mIsEnableFlick = false;
172 protected boolean mIsHookFlick = false;
173 protected int mStockFlickCode;
176 private static final DisplayMetrics mMetrics = new DisplayMetrics();
177 GestureDetector mDetector;
178 float mStartX, mStartY;
179 float mGestureX, mGestureY;
184 * Keyboard[language][portrait/landscape][keyboard type][shift off/on][key-mode]
186 protected Keyboard[][][][][][] mKeyboard;
187 protected boolean mCreateKeyboard = false;
190 /** Current language */
191 protected int mCurrentLanguage;
192 /** Language (English) */
193 public static final int LANG_EN = 0;
194 /** Language (Japanese) */
195 public static final int LANG_JA = 1;
196 /** Language (Chinese) */
197 public static final int LANG_CN = 2;
199 /* portrait/landscape */
200 /** State of the display */
201 protected int mDisplayMode = 0;
202 /** Display mode (Portrait) */
203 public static final int PORTRAIT = 0;
204 /** Display mode (Landscape) */
205 public static final int LANDSCAPE = 1;
208 /** Current keyboard type */
209 protected int mCurrentKeyboardType;
210 /** Keyboard (QWERTY keyboard) */
211 public static final int KEYBOARD_QWERTY = 0;
212 /** Keyboard (12-keys keyboard) */
213 public static final int KEYBOARD_12KEY = 1;
214 /** State of the shift key */
215 protected int mShiftOn = 0;
217 public static final int KEYBOARD_SHIFT_OFF = 0;
219 public static final int KEYBOARD_SHIFT_ON = 1;
222 /** Current key-mode */
223 protected int mCurrentKeyMode;
225 /* Slide key(only Nicotouch */
226 protected int mCurrentSlide = 0;
227 public static final int NICO_SLIDE_MODE_TOP = 0;
228 public static final int NICO_SLIDE_MODE_A = 1;
229 public static final int NICO_SLIDE_MODE_K = 2;
230 public static final int NICO_SLIDE_MODE_S = 3;
231 public static final int NICO_SLIDE_MODE_T = 4;
232 public static final int NICO_SLIDE_MODE_N = 5;
233 public static final int NICO_SLIDE_MODE_H = 6;
234 public static final int NICO_SLIDE_MODE_M = 7;
235 public static final int NICO_SLIDE_MODE_Y = 8;
236 public static final int NICO_SLIDE_MODE_R = 9;
237 public static final int NICO_SLIDE_MODE_W = 10;
239 /* key-modes for English */
240 /** English key-mode (alphabet) */
241 public static final int KEYMODE_EN_ALPHABET = 0;
242 /** English key-mode (number) */
243 public static final int KEYMODE_EN_NUMBER = 1;
244 /** English key-mode (phone number) */
245 public static final int KEYMODE_EN_PHONE = 2;
247 /* key-modes for Japanese */
248 /** Japanese key-mode (Full-width Nicotouch) */
249 public static final int KEYMODE_JA_FULL_NICO = 0;
250 /** Japanese key-mode (Full-width Hiragana) */
251 public static final int KEYMODE_JA_FULL_HIRAGANA = 1;
252 /** Japanese key-mode (Full-width alphabet) */
253 public static final int KEYMODE_JA_FULL_ALPHABET = 2;
254 /** Japanese key-mode (Full-width number) */
255 public static final int KEYMODE_JA_FULL_NUMBER = 3;
256 /** Japanese key-mode (Full-width Katakana) */
257 public static final int KEYMODE_JA_FULL_KATAKANA = 4;
258 /** Japanese key-mode (Half-width alphabet) */
259 public static final int KEYMODE_JA_HALF_ALPHABET = 5;
260 /** Japanese key-mode (Half-width number) */
261 public static final int KEYMODE_JA_HALF_NUMBER = 6;
262 /** Japanese key-mode (Half-width Katakana) */
263 public static final int KEYMODE_JA_HALF_KATAKANA = 7;
264 /** Japanese key-mode (Half-width phone number) */
265 public static final int KEYMODE_JA_HALF_PHONE = 8;
267 /* key-modes for Chinese */
268 /** Chinese key-mode (pinyin) */
269 public static final int KEYMODE_CN_PINYIN = 0;
270 /** Chinese key-mode (Full-width number) */
271 public static final int KEYMODE_CN_FULL_NUMBER = 1;
272 /** Chinese key-mode (alphabet) */
273 public static final int KEYMODE_CN_ALPHABET = 2;
274 /** Chinese key-mode (phone) */
275 public static final int KEYMODE_CN_PHONE = 3;
276 /** Chinese key-mode (Half-width number) */
277 public static final int KEYMODE_CN_HALF_NUMBER = 4;
279 /* key-modes for HARD */
280 /** HARD key-mode (SHIFT_OFF_ALT_OFF) */
281 public static final int HARD_KEYMODE_SHIFT_OFF_ALT_OFF = 2;
282 /** HARD key-mode (SHIFT_ON_ALT_OFF) */
283 public static final int HARD_KEYMODE_SHIFT_ON_ALT_OFF = 3;
284 /** HARD key-mode (SHIFT_OFF_ALT_ON) */
285 public static final int HARD_KEYMODE_SHIFT_OFF_ALT_ON = 4;
286 /** HARD key-mode (SHIFT_ON_ALT_ON) */
287 public static final int HARD_KEYMODE_SHIFT_ON_ALT_ON = 5;
288 /** HARD key-mode (SHIFT_LOCK_ALT_OFF) */
289 public static final int HARD_KEYMODE_SHIFT_LOCK_ALT_OFF = 6;
290 /** HARD key-mode (SHIFT_LOCK_ALT_ON) */
291 public static final int HARD_KEYMODE_SHIFT_LOCK_ALT_ON = 7;
292 /** HARD key-mode (SHIFT_LOCK_ALT_LOCK) */
293 public static final int HARD_KEYMODE_SHIFT_LOCK_ALT_LOCK = 8;
294 /** HARD key-mode (SHIFT_OFF_ALT_LOCK) */
295 public static final int HARD_KEYMODE_SHIFT_OFF_ALT_LOCK = 9;
296 /** HARD key-mode (SHIFT_ON_ALT_LOCK) */
297 public static final int HARD_KEYMODE_SHIFT_ON_ALT_LOCK = 10;
299 /** Whether the H/W keyboard is hidden. */
300 protected boolean mIsQwertyKeyboard = false;
301 protected boolean mHardKeyboardHidden = true;
302 /** option softkeyboard on/off **/
303 protected boolean mHiddenSoftKeyboard = false;
306 * Status of the composing text
308 * {@code true} if there is no composing text.
310 protected boolean mNoInput = true;
312 /** Vibratior for key click vibration */
313 protected Vibrator mVibrator = null;
315 /** MediaPlayer for key click sound */
316 protected MediaPlayer mSound = null;
318 /** Key toggle cycle table currently using */
319 protected String[] mCurrentCycleTable;
324 public DefaultSoftKeyboard() { }
327 * Create keyboard views
329 * @param parent OpenWnn using the keyboards.
331 protected void createKeyboards(NicoWnn parent) {
333 * Keyboard[# of Languages][portrait/landscape][# of keyboard type]
334 * [shift off/on][max # of key-modes][non-input/input]
336 mKeyboard = new Keyboard[3][2][4][2][9][2];
342 public void checkHiddenKeyboard() {
346 * Get the keyboard changed the specified shift state.
348 * @param shift Shift state
349 * @return Keyboard view
351 protected Keyboard getShiftChangeKeyboard(int shift) {
353 Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][shift][mCurrentKeyMode];
355 if (!mNoInput && kbd[1] != null) {
359 } catch (Exception ex) {
364 * Get the keyboard changed the specified shift state.
366 * @param shift Shift state
367 * @return Keyboard view
369 protected Keyboard getSlideChangeKeyboard(int slide) {
371 mCurrentSlide = slide;
372 Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mCurrentSlide][mCurrentKeyMode];
374 if (!mNoInput && kbd[1] != null) {
378 } catch (Exception ex) {
384 * Get the keyboard changed the specified input mode.
386 * @param mode Input mode
387 * @return Keyboard view
389 protected Keyboard getModeChangeKeyboard(int mode) {
391 Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn+mCurrentSlide][mode];
393 if (!mNoInput && kbd[1] != null) {
397 } catch (Exception ex) {
403 * Get the keyboard changed the specified keyboard type
405 * @param type Keyboard type
406 * @return Keyboard view
408 protected Keyboard getTypeChangeKeyboard(int type) {
410 Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][type][mShiftOn+mCurrentSlide][mCurrentKeyMode];
412 if (!mNoInput && kbd[1] != null) {
416 } catch (Exception ex) {
422 * Get the keyboard when some characters are input or no character is input.
424 * @param inputed {@code true} if some characters are inputed; {@code false} if no character is inputed.
425 * @return Keyboard view
427 protected Keyboard getKeyboardInputed(boolean inputed) {
429 Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn+mCurrentSlide][mCurrentKeyMode];
431 if (inputed && kbd[1] != null) {
435 } catch (Exception ex) {
441 * Change the circulative key-mode.
443 protected void toggleKeyMode() {
445 mShiftOn = KEYBOARD_SHIFT_OFF;
448 /* search next defined key-mode */
449 Keyboard[][] keyboardList = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn+mCurrentSlide];
451 if (++mCurrentKeyMode >= keyboardList.length) {
454 } while (keyboardList[mCurrentKeyMode][0] == null);
457 if (!mNoInput && keyboardList[mCurrentKeyMode][1] != null) {
458 kbd = keyboardList[mCurrentKeyMode][1];
460 kbd = keyboardList[mCurrentKeyMode][0];
464 mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE,
465 NicoWnnEvent.Mode.DEFAULT));
469 * Toggle change the shift lock state.
471 protected void toggleShiftLock() {
474 Keyboard newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_ON);
475 if (newKeyboard != null) {
477 changeKeyboard(newKeyboard);
482 Keyboard newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_OFF);
483 if (newKeyboard != null) {
485 changeKeyboard(newKeyboard);
492 * Handling Alt key event.
494 protected void processAltKey() {
495 /* invalid if it is not qwerty mode */
496 if (mCurrentKeyboardType != KEYBOARD_QWERTY) {
501 int keymode = mCurrentKeyMode;
502 switch (mCurrentLanguage) {
504 if (keymode == KEYMODE_EN_ALPHABET) {
505 mode = KEYMODE_EN_NUMBER;
506 } else if (keymode == KEYMODE_EN_NUMBER) {
507 mode = KEYMODE_EN_ALPHABET;
512 if (keymode == KEYMODE_JA_HALF_ALPHABET) {
513 mode = KEYMODE_JA_HALF_NUMBER;
514 } else if (keymode == KEYMODE_JA_HALF_NUMBER) {
515 mode = KEYMODE_JA_HALF_ALPHABET;
516 } else if (keymode == KEYMODE_JA_FULL_ALPHABET) {
517 mode = KEYMODE_JA_FULL_NUMBER;
518 } else if (keymode == KEYMODE_JA_FULL_NUMBER) {
519 mode = KEYMODE_JA_FULL_ALPHABET;
528 Keyboard kbd = getModeChangeKeyboard(mode);
530 mCurrentKeyMode = mode;
537 * Change the keyboard type.
539 * @param type Type of the keyboard
540 * @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#KEYBOARD_QWERTY
541 * @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#KEYBOARD_12KEY
543 public void changeKeyboardType(int type) {
544 /* ignore invalid parameter */
545 if (type != KEYBOARD_QWERTY && type != KEYBOARD_12KEY) {
549 /* change keyboard view */
550 Keyboard kbd = getTypeChangeKeyboard(type);
552 mCurrentKeyboardType = type;
556 /* notice that the keyboard is changed */
557 mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE,
558 NicoWnnEvent.Mode.DEFAULT));
562 * Change the keyboard.
564 * @param keyboard The new keyboard
565 * @return {@code true} if the keyboard is changed; {@code false} if not changed.
567 protected boolean changeKeyboard(Keyboard keyboard) {
569 if (keyboard == null) {
572 if (mCurrentKeyboard != keyboard) {
573 mKeyboardView.setKeyboard(keyboard);
574 mKeyboardView.setShifted((mShiftOn == 0) ? false : true);
575 mCurrentKeyboard = keyboard;
578 mKeyboardView.setShifted((mShiftOn == 0) ? false : true);
586 public void changeKeyMode(int keyMode) {
589 /** @see com.hiroshica.android.input.nicownn2.InputViewManager#initView */
590 public View initView(NicoWnn parent, int width, int height) {
593 (parent.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
594 ? LANDSCAPE : PORTRAIT;
595 // get hardkeyboard status
596 int qwertyState = parent.getResources().getConfiguration().keyboard;
597 boolean qwerty = (qwertyState == Configuration.KEYBOARD_QWERTY);
598 int hiddenState = parent.getResources().getConfiguration().hardKeyboardHidden;
599 boolean hidden = (hiddenState == Configuration.HARDKEYBOARDHIDDEN_YES);
600 setHardKeyboardHidden(hidden, qwerty);
603 * create keyboards & the view.
604 * To re-display the input view when the display mode is changed portrait <-> landscape,
605 * create keyboards every time.
607 createKeyboards(parent);
609 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(parent);
610 String skin = pref.getString("keyboard_skin",
611 mWnn.getResources().getString(R.string.keyboard_skin_id_default));
612 int id = parent.getResources().getIdentifier(skin, "layout", "com.hiroshica.android.input.nicownn2");
614 mKeyboardView = (KeyboardView) mWnn.getLayoutInflater().inflate(id, null);
615 mKeyboardView.setOnKeyboardActionListener(this);
616 mKeyboardView.setOnTouchListener(this);
617 mCurrentKeyboard = null;
619 mMainView = (ViewGroup) parent.getLayoutInflater().inflate(R.layout.keyboard_default_main, null);
620 mSubView = (ViewGroup) parent.getLayoutInflater().inflate(R.layout.keyboard_default_sub, null);
622 if (PORTRAIT == mDisplayMode) {
623 mKeyboardView.setPadding(0, 0, 0, 12);
626 mKeyboardView.setPadding(0, 0, 0, 8);
628 // if (mDisplayMode == LANDSCAPE && true == mHardKeyboardHidden) {
629 // mMainView.addView(mSubView);
631 if (mKeyboardView != null) {
632 mMainView.addView(mKeyboardView);
634 // entry gesture detector
635 mDetector = new GestureDetector(this);
636 mDetector.setIsLongpressEnabled(false);
637 mMetrics.setToDefaults();
642 * Update the SHFIT/ALT keys indicator.
644 * @param mode The state of SHIFT/ALT keys.
646 public void updateIndicator(int mode) {
647 Resources res = mWnn.getResources();
648 TextView text1 = (TextView)mSubView.findViewById(R.id.shift);
649 TextView text2 = (TextView)mSubView.findViewById(R.id.alt);
652 case HARD_KEYMODE_SHIFT_OFF_ALT_OFF:
653 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_off));
654 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_off));
655 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
656 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
658 case HARD_KEYMODE_SHIFT_ON_ALT_OFF:
659 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_on));
660 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_off));
661 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
662 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
664 case HARD_KEYMODE_SHIFT_LOCK_ALT_OFF:
665 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_lock));
666 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_off));
667 text1.setBackgroundColor(res.getColor(R.color.indicator_background_lock_caps));
668 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
670 case HARD_KEYMODE_SHIFT_OFF_ALT_ON:
671 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_off));
672 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_on));
673 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
674 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
676 case HARD_KEYMODE_SHIFT_OFF_ALT_LOCK:
677 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_off));
678 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_lock));
679 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
680 text2.setBackgroundColor(res.getColor(R.color.indicator_background_lock_alt));
682 case HARD_KEYMODE_SHIFT_ON_ALT_ON:
683 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_on));
684 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_on));
685 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
686 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
688 case HARD_KEYMODE_SHIFT_ON_ALT_LOCK:
689 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_on));
690 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_lock));
691 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
692 text2.setBackgroundColor(res.getColor(R.color.indicator_background_lock_alt));
694 case HARD_KEYMODE_SHIFT_LOCK_ALT_ON:
695 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_lock));
696 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_on));
697 text1.setBackgroundColor(res.getColor(R.color.indicator_background_lock_caps));
698 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
700 case HARD_KEYMODE_SHIFT_LOCK_ALT_LOCK:
701 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_lock));
702 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_lock));
703 text1.setBackgroundColor(res.getColor(R.color.indicator_background_lock_caps));
704 text2.setBackgroundColor(res.getColor(R.color.indicator_background_lock_alt));
707 text1.setTextColor(res.getColor(R.color.indicator_textcolor_caps_off));
708 text2.setTextColor(res.getColor(R.color.indicator_textcolor_alt_off));
709 text1.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
710 text2.setBackgroundColor(res.getColor(R.color.indicator_textbackground_default));
716 /** @see com.hiroshica.android.input.nicownn2.InputViewManager#getCurrentView */
717 public View getCurrentView() {
718 if (mCurrentKeyboard == null) {
724 /** @see com.hiroshica.android.input.nicownn2.InputViewManager#onUpdateState */
725 public void onUpdateState(NicoWnn parent) {
727 if (parent.mComposingText.size(1) == 0) {
729 /* when the mode changed to "no input" */
731 mIsHookFlick = false;
732 Keyboard newKeyboard = getKeyboardInputed(false);
733 if (mCurrentKeyboard != newKeyboard) {
734 changeKeyboard(newKeyboard);
739 /* when the mode changed to "input some characters" */
741 Keyboard newKeyboard = getKeyboardInputed(true);
742 if (mCurrentKeyboard != newKeyboard) {
743 changeKeyboard(newKeyboard);
747 } catch (Exception ex) {
751 /** @see com.hiroshica.android.input.nicownn2.InputViewManager#setPreferences */
752 public void setPreferences(SharedPreferences pref, EditorInfo editor) {
756 if (pref.getBoolean("key_vibration", false)) {
757 mVibrator = (Vibrator)mWnn.getSystemService(Context.VIBRATOR_SERVICE);
761 } catch (Exception ex) {
762 Log.d("NicoWnn", "NO VIBRATOR");
767 if (pref.getBoolean("key_sound", false)) {
768 mSound = MediaPlayer.create(mWnn, R.raw.type);
772 } catch (Exception ex) {
773 Log.d("NicoWnn", "NO SOUND");
777 mKeyboardView.setPreviewEnabled(pref.getBoolean("popup_preview", true));
780 /** @see com.hiroshica.android.input.nicownn2.InputViewManager#closing */
781 public void closing() {
782 if (mKeyboardView != null) {
783 mKeyboardView.closing();
785 mDisableKeyInput = true;
788 /***********************************************************************
789 * onKeyboardActionListener
790 ***********************************************************************/
791 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onKey */
792 public void onKey(int primaryCode, int[] keyCodes) { }
794 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeRight */
795 public void swipeRight() {
796 mIsHookFlick = false;
798 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeLeft */
799 public void swipeLeft() {
800 mIsHookFlick = false;
802 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeDown */
803 public void swipeDown() {
804 mIsHookFlick = false;
806 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeUp */
807 public void swipeUp() {
808 mIsHookFlick = false;
810 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onRelease */
811 public void onRelease(int primaryCode) {
814 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onPress */
815 public void onPress(int primaryCode) {
816 /* key click sound & vibration */
817 if (true == mIsEnableFlick) {
821 if (mVibrator != null) {
822 try { mVibrator.vibrate(30); } catch (Exception ex) { }
824 if (mSound != null) {
825 try { mSound.seekTo(0); mSound.start(); } catch (Exception ex) { }
829 /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onText */
830 public void onText(CharSequence text) {}
833 * Get current key mode.
835 * @return Current key mode
837 public int getKeyMode() {
838 return mCurrentKeyMode;
842 * Get current keyboard type.
844 * @return Current keyboard type
846 public int getKeyboardType() {
847 return mCurrentKeyboardType;
851 * Set the H/W keyboard's state.
853 * @param hidden {@code true} if hidden.
855 public void setHardKeyboardHidden(boolean hidden, boolean qwerty) {
856 mHardKeyboardHidden = hidden;
857 mIsQwertyKeyboard = qwerty;
862 public boolean isHardQwertyOnLandscape() {
863 if (mDisplayMode == DefaultSoftKeyboard.PORTRAIT) {
866 if (false == mIsQwertyKeyboard) {
869 if (true == mHardKeyboardHidden) {
876 * Get current keyboard view.
878 public View getKeyboardView() {
879 return mKeyboardView;
883 * reset kicotouch keyboard
885 public void resetNicoKeyboard() {
890 * get mHiddenSoftKeyboard
892 public boolean getHiddenSoftKeyboard() {
893 return mHiddenSoftKeyboard;
897 * Reset the current keyboard
899 public void resetCurrentKeyboard() {
901 Keyboard keyboard = mCurrentKeyboard;
902 mCurrentKeyboard = null;
903 changeKeyboard(keyboard);
906 * Change to the next input mode
908 public void nextKeyMode() {
911 * set default Keyboard
913 public void setDefaultKeyboard() {
914 changeKeyMode(KEYMODE_JA_FULL_HIRAGANA);
917 /******************************************************************************************/
918 /******************************************************************************************/
922 public boolean onTouch(View v, MotionEvent event) {
923 mDetector.onTouchEvent(event);
927 * OnGestureListener functions
929 public boolean onDown(MotionEvent ev){
930 //Log.d("NicoWnn", "onDown");
931 mStartX = ev.getRawX();
932 mStartY = ev.getRawY();
935 Log.d("NicoWnn", "onDown " + mStartX + "/" + mStartY);
938 public boolean onFling(MotionEvent ev1, MotionEvent ev2, float getX, float getY){
941 public void onLongPress(MotionEvent ev){
943 public boolean onScroll(MotionEvent ev1, MotionEvent ev2, float distX, float distY){
944 mGestureX = ev2.getRawX();
945 mGestureY = ev2.getRawY();
946 Log.d("NicoWnn", "get " + (mGestureX - mStartX) + "/" + (mGestureY - mStartY));
949 public void onShowPress(MotionEvent ev){
951 public boolean onSingleTapUp(MotionEvent ev){
955 * calc. flick keycode
957 public int checkFlickKeyCode(boolean checkFlick, int prevkeycode) {
961 calcx = (mGestureX - mStartX) * mMetrics.scaledDensity;
962 calcy = (mGestureY - mStartY) * mMetrics.scaledDensity;
963 float length = FloatMath.sqrt((calcx * calcx) + (calcy * calcy));
965 rotation = Math.atan2(calcy, calcx);
966 // change radian -> degree
967 float getrot = (float)(rotation / Math.PI * 180.0);
969 getrot = 360.0f + getrot;
971 // change rotate -> keycode
977 if (getrot >= 45.0f && getrot < 135.0f) {
980 else if (getrot >= 135.0f && getrot < 225.0f) {
983 else if (getrot >= 225.0f && getrot < 315.0f) {
991 switch (prevkeycode) {
1000 case KEYCODE_JP12_9:
1001 case KEYCODE_JP12_0:
1005 Log.d("NicoWnn", "Now retcode = " + retcode);
1010 /**************** end of file ****************/