OSDN Git Service

b360cf2d79c77fd3c5c4e28f47af63dfd757ed2f
[nicownn/NicoWnn.git] / src / com / hiroshica / android / input / nicownn2 / DefaultSoftKeyboard.java
1 /*
2  * Copyright (C) 2008,2009  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 com.hiroshica.android.input.nicownn2;
18
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;
32
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;
38
39 import android.util.Log;
40
41
42 /**
43  * The default software keyboard class.
44  *
45  * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
46  */
47 public class DefaultSoftKeyboard implements InputViewManager, KeyboardView.OnKeyboardActionListener, KeyboardView.OnTouchListener, OnGestureListener {
48         /*
49          *----------------------------------------------------------------------
50          * key codes for a software keyboard
51          *----------------------------------------------------------------------
52          */
53         /** Change the keyboard language */
54         public static final int KEYCODE_CHANGE_LANG = -500;
55
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;
115     
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;
149     
150         /** OpenWnn instance which hold this software keyboard*/
151         protected NicoWnn      mWnn;
152     
153         /** Current keyboard view */
154         protected KeyboardView mKeyboardView;
155     
156         /** View objects (main side) */
157         protected ViewGroup mMainView;
158         /** View objects (sub side) */
159         protected ViewGroup mSubView;
160     
161         /** Current keyboard definition */
162         protected Keyboard mCurrentKeyboard;
163     
164         /** Caps lock state */
165         protected boolean mCapsLock;
166     
167         /** Input restraint */
168         protected boolean mDisableKeyInput = true;
169
170         /** flick mode **/
171         protected boolean mIsEnableFlick   = false;
172         protected boolean mIsHookFlick     = false;
173         protected int     mStockFlickCode;
174
175         /** gecture  **/
176         private static final DisplayMetrics mMetrics = new DisplayMetrics();
177         GestureDetector   mDetector;
178         float             mStartX, mStartY;
179         float             mGestureX, mGestureY;
180
181         /**
182          * Keyboard surfaces 
183          * <br>
184          * Keyboard[language][portrait/landscape][keyboard type][shift off/on][key-mode]
185          */
186         protected Keyboard[][][][][][] mKeyboard;
187         protected boolean              mCreateKeyboard = false;
188
189         /* languages */
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;
198
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;
206
207         /* keyboard type */
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;
216         /** Shift key off */
217         public static final int KEYBOARD_SHIFT_OFF = 0;
218         /** Shift key on */
219         public static final int KEYBOARD_SHIFT_ON  = 1;
220
221         /* key-modes */
222         /** Current key-mode */
223         protected int mCurrentKeyMode;
224
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;
238
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;
246
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;
266
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;
278     
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;
298
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;
304
305         /**
306          * Status of the composing text
307          * <br>
308          * {@code true} if there is no composing text.
309          */
310         protected boolean mNoInput = true;
311     
312         /** Vibratior for key click vibration */
313         protected Vibrator mVibrator = null;
314     
315         /** MediaPlayer for key click sound */
316         protected MediaPlayer mSound = null;
317     
318         /** Key toggle cycle table currently using */
319         protected String[] mCurrentCycleTable;
320
321         /**
322          * Constructor
323          */
324         public DefaultSoftKeyboard() { }
325
326         /**
327          * Create keyboard views
328          *
329          * @param parent   OpenWnn using the keyboards.
330          */
331         protected void createKeyboards(NicoWnn parent) {
332                 /*
333                  *  Keyboard[# of Languages][portrait/landscape][# of keyboard type]
334                  *          [shift off/on][max # of key-modes][non-input/input]
335                  */
336                 mKeyboard = new Keyboard[3][2][4][2][9][2];
337         }
338
339         /**
340          *
341          */
342         public void checkHiddenKeyboard() {
343         }
344
345         /**
346          * Get the keyboard changed the specified shift state.
347          *
348          * @param shift     Shift state
349          * @return          Keyboard view
350          */
351         protected Keyboard getShiftChangeKeyboard(int shift) {
352                 try {
353                         Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][shift][mCurrentKeyMode];
354
355                         if (!mNoInput && kbd[1] != null) {
356                                 return kbd[1];
357                         }
358                         return kbd[0];
359                 } catch (Exception ex) {
360                         return null;
361                 }
362         }
363         /**
364          * Get the keyboard changed the specified shift state.
365          *
366          * @param shift     Shift state
367          * @return          Keyboard view
368          */
369         protected Keyboard getSlideChangeKeyboard(int slide) {
370                 try {
371                         mCurrentSlide = slide;
372                         Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mCurrentSlide][mCurrentKeyMode];
373
374                         if (!mNoInput && kbd[1] != null) {
375                                 return kbd[1];
376                         }
377                         return kbd[0];
378                 } catch (Exception ex) {
379                         return null;
380                 }
381         }
382
383         /**
384          * Get the keyboard changed the specified input mode.
385          *
386          * @param mode      Input mode
387          * @return          Keyboard view
388          */
389         protected Keyboard getModeChangeKeyboard(int mode) {
390                 try {
391                         Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn+mCurrentSlide][mode];
392
393                         if (!mNoInput && kbd[1] != null) {
394                                 return kbd[1];
395                         }
396                         return kbd[0];
397                 } catch (Exception ex) {
398                         return null;
399                 }
400         }
401
402         /**
403          * Get the keyboard changed the specified keyboard type
404          *
405          * @param type      Keyboard type
406          * @return          Keyboard view
407          */
408         protected Keyboard getTypeChangeKeyboard(int type) {
409                 try {
410                         Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][type][mShiftOn+mCurrentSlide][mCurrentKeyMode];
411
412                         if (!mNoInput && kbd[1] != null) {
413                                 return kbd[1];
414                         }
415                         return kbd[0];
416                 } catch (Exception ex) {
417                         return null;
418                 }
419         }
420
421         /**
422          * Get the keyboard when some characters are input or no character is input.
423          *
424          * @param inputed   {@code true} if some characters are inputed; {@code false} if no character is inputed.
425          * @return          Keyboard view
426          */
427         protected Keyboard getKeyboardInputed(boolean inputed) {
428                 try {
429                         Keyboard[] kbd = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn+mCurrentSlide][mCurrentKeyMode];
430
431                         if (inputed && kbd[1] != null) {
432                                 return kbd[1];
433                         }
434                         return kbd[0];
435                 } catch (Exception ex) {
436                         return null;
437                 }
438         }
439
440         /**
441          * Change the circulative key-mode.
442          */
443         protected void toggleKeyMode() {
444                 /* unlock shift */
445                 mShiftOn = KEYBOARD_SHIFT_OFF;
446                 mCurrentSlide = 0;
447
448                 /* search next defined key-mode */
449                 Keyboard[][] keyboardList = mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn+mCurrentSlide];
450                 do {
451                         if (++mCurrentKeyMode >= keyboardList.length) {
452                                 mCurrentKeyMode = 0;
453                         }
454                 } while (keyboardList[mCurrentKeyMode][0] == null);
455
456                 Keyboard kbd;
457                 if (!mNoInput && keyboardList[mCurrentKeyMode][1] != null) {
458                         kbd = keyboardList[mCurrentKeyMode][1];
459                 } else {
460                         kbd = keyboardList[mCurrentKeyMode][0];
461                 }
462                 changeKeyboard(kbd);
463
464                 mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE,
465                                                                           NicoWnnEvent.Mode.DEFAULT));
466         }
467
468         /**
469          * Toggle change the shift lock state.
470          */
471         protected void toggleShiftLock() {
472                 if (mShiftOn == 0) {
473                         /* turn shift on */
474                         Keyboard newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_ON);
475                         if (newKeyboard != null) {
476                                 mShiftOn = 1;
477                                 changeKeyboard(newKeyboard);
478                         }
479                         mCapsLock = true;
480                 } else {
481                         /* turn shift off */
482                         Keyboard newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_OFF);
483                         if (newKeyboard != null) {
484                                 mShiftOn = 0;
485                                 changeKeyboard(newKeyboard);
486                         }
487                         mCapsLock = false;
488                 }
489         }
490
491         /**
492          * Handling Alt key event.
493          */
494         protected void processAltKey() {
495                 /* invalid if it is not qwerty mode */
496                 if (mCurrentKeyboardType != KEYBOARD_QWERTY) {
497                         return;
498                 }
499
500                 int mode = -1;
501                 int keymode = mCurrentKeyMode;
502                 switch (mCurrentLanguage) {
503                 case LANG_EN:
504                         if (keymode == KEYMODE_EN_ALPHABET) {
505                                 mode = KEYMODE_EN_NUMBER;
506                         } else if (keymode == KEYMODE_EN_NUMBER) {
507                                 mode = KEYMODE_EN_ALPHABET;
508                         }
509                         break;
510
511                 case LANG_JA:
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;
520                         }
521                         break;
522
523                 default:
524                         /* invalid */
525                 }
526
527                 if (mode >= 0) {
528                         Keyboard kbd = getModeChangeKeyboard(mode);
529                         if (kbd != null) {
530                                 mCurrentKeyMode = mode;
531                                 changeKeyboard(kbd);
532                         }
533                 }
534         }
535
536         /**
537          * Change the keyboard type.
538          *
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
542          */
543         public void changeKeyboardType(int type) {
544                 /* ignore invalid parameter */
545                 if (type != KEYBOARD_QWERTY && type != KEYBOARD_12KEY) {
546                         return;
547                 }
548         
549                 /* change keyboard view */
550                 Keyboard kbd = getTypeChangeKeyboard(type);
551                 if (kbd != null) {
552                         mCurrentKeyboardType = type;
553                         changeKeyboard(kbd);
554                 }
555
556                 /* notice that the keyboard is changed */
557                 mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE,
558                                                                           NicoWnnEvent.Mode.DEFAULT));
559         }
560
561         /**
562          * Change the keyboard.
563          *
564          * @param keyboard  The new keyboard
565          * @return          {@code true} if the keyboard is changed; {@code false} if not changed.
566          */
567         protected boolean changeKeyboard(Keyboard keyboard) {
568
569                 if (keyboard == null) {
570                         return false;
571                 }
572                 if (mCurrentKeyboard != keyboard) {
573                         mKeyboardView.setKeyboard(keyboard);
574                         mKeyboardView.setShifted((mShiftOn == 0) ? false : true);
575                         mCurrentKeyboard = keyboard;
576                         return true;
577                 } else {
578                         mKeyboardView.setShifted((mShiftOn == 0) ? false : true);
579                         return false;
580                 }
581         }
582         
583         /*
584          * 
585          */
586         public void changeKeyMode(int keyMode) {
587         }
588         
589         /** @see com.hiroshica.android.input.nicownn2.InputViewManager#initView */
590         public View initView(NicoWnn parent, int width, int height) {
591                 mWnn = parent;
592                 mDisplayMode = 
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);
601
602                 /*
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.
606                  */
607                 createKeyboards(parent);
608
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");
613
614                 mKeyboardView = (KeyboardView) mWnn.getLayoutInflater().inflate(id, null);
615                 mKeyboardView.setOnKeyboardActionListener(this);
616                 mKeyboardView.setOnTouchListener(this);
617                 mCurrentKeyboard = null;
618
619                 mMainView = (ViewGroup) parent.getLayoutInflater().inflate(R.layout.keyboard_default_main, null);
620                 mSubView = (ViewGroup) parent.getLayoutInflater().inflate(R.layout.keyboard_default_sub, null);
621                 
622                 if (PORTRAIT == mDisplayMode) {
623                         mKeyboardView.setPadding(0, 0, 0, 12);
624                 }
625                 else{
626                         mKeyboardView.setPadding(0, 0, 0, 8);
627                 }
628         // if (mDisplayMode == LANDSCAPE && true == mHardKeyboardHidden) { 
629         //     mMainView.addView(mSubView);
630         // }
631                 if (mKeyboardView != null) {
632                         mMainView.addView(mKeyboardView);
633                 }
634                 // entry gesture detector
635                 mDetector = new GestureDetector(this);
636                 mDetector.setIsLongpressEnabled(false);
637                 mMetrics.setToDefaults();
638                 return mMainView;
639         }
640     
641         /**
642          * Update the SHFIT/ALT keys indicator.
643          * 
644          * @param mode  The state of SHIFT/ALT keys.
645          */
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);
650
651                 switch (mode) {
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));
657                         break;
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));
663                         break;
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));
669                         break;
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));
675                         break;
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));
681                         break;
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));
687                         break;
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));
693                         break;
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));
699                         break;
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));
705                         break;
706                 default:
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));
711                         break;
712                 }
713                 return;
714         }
715     
716         /** @see com.hiroshica.android.input.nicownn2.InputViewManager#getCurrentView */
717         public View getCurrentView() {
718                 if (mCurrentKeyboard == null) {
719                         return null;
720                 }
721                 return mMainView;
722         }
723
724         /** @see com.hiroshica.android.input.nicownn2.InputViewManager#onUpdateState */
725         public void onUpdateState(NicoWnn parent) {
726                 try {
727                         if (parent.mComposingText.size(1) == 0) {
728                                 if (!mNoInput) {
729                                         /* when the mode changed to "no input" */
730                                         mNoInput = true;
731                                         mIsHookFlick = false;
732                                         Keyboard newKeyboard = getKeyboardInputed(false);
733                                         if (mCurrentKeyboard != newKeyboard) {
734                                                 changeKeyboard(newKeyboard);
735                                         }
736                                 }
737                         } else {
738                                 if (mNoInput) {
739                                         /* when the mode changed to "input some characters" */
740                                         mNoInput = false;
741                                         Keyboard newKeyboard = getKeyboardInputed(true);
742                                         if (mCurrentKeyboard != newKeyboard) {
743                                                 changeKeyboard(newKeyboard);
744                                         }
745                                 }
746                         }
747                 } catch (Exception ex) {
748                 }
749         }
750
751         /** @see com.hiroshica.android.input.nicownn2.InputViewManager#setPreferences */
752         public void setPreferences(SharedPreferences pref, EditorInfo editor) {
753
754                 /* vibrator */
755                 try {
756                         if (pref.getBoolean("key_vibration", false)) {
757                                 mVibrator = (Vibrator)mWnn.getSystemService(Context.VIBRATOR_SERVICE);
758                         } else {
759                                 mVibrator = null;
760                         }
761                 } catch (Exception ex) {
762                         Log.d("NicoWnn", "NO VIBRATOR");
763                 }
764
765                 /* sound */
766                 try {
767                         if (pref.getBoolean("key_sound", false)) {
768                                 mSound = MediaPlayer.create(mWnn, R.raw.type);
769                         } else {
770                                 mSound = null;
771                         }
772                 } catch (Exception ex) {
773                         Log.d("NicoWnn", "NO SOUND");
774                 }
775
776                 /* pop-up preview */
777                 mKeyboardView.setPreviewEnabled(pref.getBoolean("popup_preview", true));
778         }
779
780         /** @see com.hiroshica.android.input.nicownn2.InputViewManager#closing */
781         public void closing() {
782                 if (mKeyboardView != null) {
783                         mKeyboardView.closing();
784                 }
785                 mDisableKeyInput = true;
786         }
787
788         /***********************************************************************
789          * onKeyboardActionListener
790          ***********************************************************************/
791         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onKey */
792         public void onKey(int primaryCode, int[] keyCodes) { }
793
794         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeRight */
795         public void swipeRight() {
796                 mIsHookFlick = false;
797         }
798         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeLeft */
799         public void swipeLeft() {
800                 mIsHookFlick = false;
801         }
802         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeDown */
803         public void swipeDown() {
804                 mIsHookFlick = false;
805         }
806         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeUp */
807         public void swipeUp() {
808                 mIsHookFlick = false;
809         }
810         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onRelease */
811         public void onRelease(int primaryCode) {
812         }
813
814         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onPress */
815         public void onPress(int primaryCode) {
816                 /* key click sound & vibration */
817                 if (true == mIsEnableFlick) {
818                         mIsHookFlick = true;
819                 }
820
821                 if (mVibrator != null) {
822                         try { mVibrator.vibrate(30); } catch (Exception ex) { }
823                 }
824                 if (mSound != null) {
825                         try { mSound.seekTo(0); mSound.start(); } catch (Exception ex) { }
826                 }
827         }
828     
829         /** @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onText */
830         public void onText(CharSequence text) {}
831
832         /**
833          * Get current key mode.
834          * 
835          * @return Current key mode
836          */
837         public int getKeyMode() {
838                 return mCurrentKeyMode;
839         }
840
841         /**
842          * Get current keyboard type.
843          * 
844          * @return Current keyboard type
845          */
846         public int getKeyboardType() {
847                 return mCurrentKeyboardType;
848         }
849
850         /**
851          * Set the H/W keyboard's state.
852          * 
853          * @param hidden {@code true} if hidden.
854          */
855         public void setHardKeyboardHidden(boolean hidden, boolean qwerty) {
856                 mHardKeyboardHidden = hidden;
857                 mIsQwertyKeyboard   = qwerty;
858         }
859         /**
860          *
861          */
862         public boolean isHardQwertyOnLandscape() {
863                 if (mDisplayMode == DefaultSoftKeyboard.PORTRAIT) {
864                         return false;
865                 }
866                 if (false == mIsQwertyKeyboard) {
867                         return false;
868                 }
869                 if (true == mHardKeyboardHidden) {
870                         return false;
871                 }
872                 return true;
873         }
874
875         /**
876          * Get current keyboard view.
877          */
878         public View getKeyboardView() {
879                 return mKeyboardView;
880         }
881
882         /**
883          * reset kicotouch keyboard
884          */
885         public void resetNicoKeyboard() {
886                 // no operation
887         }
888
889         /**
890          * get mHiddenSoftKeyboard
891          */
892         public boolean getHiddenSoftKeyboard() {
893                 return mHiddenSoftKeyboard;
894         }
895
896         /**
897          * Reset the current keyboard
898          */
899         public void resetCurrentKeyboard() {
900                 closing();
901                 Keyboard keyboard = mCurrentKeyboard;
902                 mCurrentKeyboard = null;
903                 changeKeyboard(keyboard);
904         }
905         /**
906          * Change to the next input mode
907          */
908         public void nextKeyMode() {
909         }
910         /**
911          * set default Keyboard
912          */
913         public void setDefaultKeyboard() {
914                 changeKeyMode(KEYMODE_JA_FULL_HIRAGANA);
915         }
916
917         /******************************************************************************************/
918         /******************************************************************************************/
919         /***
920          * gesture control
921          */
922         public boolean onTouch(View v, MotionEvent event) {
923                 mDetector.onTouchEvent(event);
924                 return false;
925         }
926         /**
927          * OnGestureListener functions
928          */
929         public boolean onDown(MotionEvent ev){
930                 //Log.d("NicoWnn", "onDown");
931                 mStartX = ev.getRawX();
932                 mStartY = ev.getRawY();
933                 mGestureX = mStartX;
934                 mGestureY = mStartY;
935                 Log.d("NicoWnn", "onDown " + mStartX + "/" + mStartY);
936                 return true;
937         }
938         public boolean onFling(MotionEvent ev1, MotionEvent ev2, float getX, float getY){
939                 return true;
940         }
941         public void    onLongPress(MotionEvent ev){
942         }
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));
947                 return true;
948         }
949         public void    onShowPress(MotionEvent ev){
950         }
951         public boolean onSingleTapUp(MotionEvent ev){
952                 return true;
953         }
954         /**
955          * calc. flick keycode
956          */
957         public int checkFlickKeyCode(boolean checkFlick, int prevkeycode) {
958                 int retcode = -1;
959
960                 float calcx, calcy;
961                 calcx = (mGestureX - mStartX) * mMetrics.scaledDensity;
962                 calcy = (mGestureY - mStartY) * mMetrics.scaledDensity;
963                 float length = FloatMath.sqrt((calcx * calcx) + (calcy * calcy));
964                 double rotation;
965                 rotation = Math.atan2(calcy, calcx);
966                 // change radian -> degree
967                 float getrot = (float)(rotation / Math.PI * 180.0);
968                 if (getrot < 0.0f) {
969                         getrot = 360.0f + getrot;
970                 }
971                 // change rotate -> keycode
972                 int keycode = -1;
973                 if (length < 30.0) {
974                         keycode = 0;
975                 }
976                 else {
977                         if (getrot >= 45.0f && getrot < 135.0f) {
978                                 keycode = 1;
979                         }
980                         else if (getrot >= 135.0f && getrot < 225.0f) {
981                                 keycode = 2;
982                         }
983                         else if (getrot >= 225.0f && getrot < 315.0f) {
984                                 keycode = 3;
985                         }
986                         else {
987                                 keycode = 4;
988                         }
989                 }
990                 // convert keycode
991                 switch (prevkeycode) {
992                 case KEYCODE_JP12_1:
993                 case KEYCODE_JP12_2:
994                 case KEYCODE_JP12_3:
995                 case KEYCODE_JP12_4:
996                 case KEYCODE_JP12_5:
997                 case KEYCODE_JP12_6:
998                 case KEYCODE_JP12_7:
999                 case KEYCODE_JP12_8:
1000                 case KEYCODE_JP12_9:
1001                 case KEYCODE_JP12_0:
1002                         retcode = keycode;
1003                         break;
1004                 }
1005                 Log.d("NicoWnn", "Now retcode = " + retcode);
1006                 //return retcode;
1007                 return -1;
1008         }
1009 }
1010 /**************** end of file ****************/