1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.pinyin;
18
19import android.view.KeyEvent;
20import android.view.inputmethod.InputConnection;
21
22/**
23 * Class to handle English input.
24 */
25public class EnglishInputProcessor {
26
27    private int mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN;
28
29    public boolean processKey(InputConnection inputContext, KeyEvent event,
30            boolean upperCase, boolean realAction) {
31        if (null == inputContext || null == event) return false;
32
33        int keyCode = event.getKeyCode();
34
35        CharSequence prefix = null;
36        prefix = inputContext.getTextBeforeCursor(2, 0);
37
38        int keyChar;
39        keyChar = 0;
40        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
41            keyChar = keyCode - KeyEvent.KEYCODE_A + 'a';
42            if (upperCase) {
43                keyChar = keyChar + 'A' - 'a';
44            }
45        } else if (keyCode >= KeyEvent.KEYCODE_0
46                && keyCode <= KeyEvent.KEYCODE_9)
47            keyChar = keyCode - KeyEvent.KEYCODE_0 + '0';
48        else if (keyCode == KeyEvent.KEYCODE_COMMA)
49            keyChar = ',';
50        else if (keyCode == KeyEvent.KEYCODE_PERIOD)
51            keyChar = '.';
52        else if (keyCode == KeyEvent.KEYCODE_APOSTROPHE)
53            keyChar = '\'';
54        else if (keyCode == KeyEvent.KEYCODE_AT)
55            keyChar = '@';
56        else if (keyCode == KeyEvent.KEYCODE_SLASH) keyChar = '/';
57
58        if (0 == keyChar) {
59            mLastKeyCode = keyCode;
60
61            String insert = null;
62            if (KeyEvent.KEYCODE_DEL == keyCode) {
63                if (realAction)  {
64                    inputContext.deleteSurroundingText(1, 0);
65                }
66            } else if (KeyEvent.KEYCODE_ENTER == keyCode) {
67                insert = "\n";
68            } else if (KeyEvent.KEYCODE_SPACE == keyCode) {
69                insert = " ";
70            } else {
71                return false;
72            }
73
74            if (null != insert && realAction)
75                inputContext.commitText(insert, insert.length());
76
77            return true;
78        }
79
80        if (!realAction)
81            return true;
82
83        if (KeyEvent.KEYCODE_SHIFT_LEFT == mLastKeyCode
84                || KeyEvent.KEYCODE_SHIFT_LEFT == mLastKeyCode) {
85            if (keyChar >= 'a' && keyChar <= 'z')
86                keyChar = keyChar - 'a' + 'A';
87        } else if (KeyEvent.KEYCODE_ALT_LEFT == mLastKeyCode) {
88        }
89
90        String result = String.valueOf((char) keyChar);
91        inputContext.commitText(result, result.length());
92        mLastKeyCode = keyCode;
93        return true;
94    }
95}
96