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