1053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project/*
259aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma * Copyright (C) 2008-2012  OMRON SOFTWARE Co., Ltd.
3053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project *
4053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * you may not use this file except in compliance with the License.
6053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * You may obtain a copy of the License at
7053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project *
8053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project *
10053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * See the License for the specific language governing permissions and
14053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project * limitations under the License.
15053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project */
16053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
17053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectpackage jp.co.omronsoft.openwnn;
18053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
19053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport jp.co.omronsoft.openwnn.EN.*;
20053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.content.SharedPreferences;
21053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.content.Context;
22053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.content.res.Configuration;
23e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawaimport android.os.Message;
24053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.os.Handler;
25053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.preference.PreferenceManager;
26053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.text.SpannableStringBuilder;
27053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.text.Spanned;
28053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.text.method.MetaKeyKeyListener;
29053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.text.style.BackgroundColorSpan;
30053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.text.style.CharacterStyle;
31e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawaimport android.text.style.ForegroundColorSpan;
32053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.text.style.UnderlineSpan;
33053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.util.Log;
34053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.view.KeyCharacterMap;
35053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.view.KeyEvent;
36e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawaimport android.view.MotionEvent;
377b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawaimport android.view.View;
38053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectimport android.view.inputmethod.EditorInfo;
39053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
40053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project/**
413feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa * The OpenWnn English IME class.
42053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project *
433feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
44053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project */
45053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Projectpublic class OpenWnnEN extends OpenWnn {
4677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** A space character */
4777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private static final char[] SPACE = {' '};
483fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
4977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Character style of underline */
5077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private static final CharacterStyle SPAN_UNDERLINE   = new UnderlineSpan();
517b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    /** Highlight color style for the selected string  */
527b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    private static final CharacterStyle SPAN_EXACT_BGCOLOR_HL     = new BackgroundColorSpan(0xFF66CDAA);
537b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    /** Highlight color style for the composing text */
547b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    private static final CharacterStyle SPAN_REMAIN_BGCOLOR_HL    = new BackgroundColorSpan(0xFFF0FFFF);
55e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Highlight text color */
56e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private static final CharacterStyle SPAN_TEXTCOLOR  = new ForegroundColorSpan(0xFF000000);
577b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa
5877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** A private area code(ALT+SHIFT+X) to be ignore (G1 specific). */
5977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private static final int PRIVATE_AREA_CODE = 61184;
6077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Never move cursor in to the composing text (adapting to IMF's specification change) */
617b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    private static final boolean FIX_CURSOR_TEXT_END = true;
62053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
6377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Spannable string for the composing text */
6477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    protected SpannableStringBuilder mDisplayText;
6577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
6677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Characters treated as a separator */
6777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private String mWordSeparators;
6877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Previous event's code */
6977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private int mPreviousEventCode;
7077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
7177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Array of words from the user dictionary */
7277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private WnnWord[] mUserDictionaryWords = null;
7377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
7477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** The converter for English prediction/spell correction */
7577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private OpenWnnEngineEN mConverterEN;
7677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** The symbol list generator */
7777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private SymbolList mSymbolList;
7877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Whether it is displaying symbol list */
7977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mSymbolMode;
8077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Whether prediction is enabled */
8177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mOptPrediction;
8277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Whether spell correction is enabled */
8377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mOptSpellCorrection;
8477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Whether learning is enabled */
8577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mOptLearning;
863fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
8777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** SHIFT key state */
8877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private int mHardShift;
893feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    /** SHIFT key state (pressing) */
9077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mShiftPressing;
9177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** ALT key state */
9277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private int mHardAlt;
933feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    /** ALT key state (pressing) */
9477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mAltPressing;
95053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
9677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Instance of this service */
9777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private static OpenWnnEN mSelf = null;
98053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
9977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Shift lock toggle definition */
10077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private static final int[] mShiftKeyToggle = {0, MetaKeyKeyListener.META_SHIFT_ON, MetaKeyKeyListener.META_CAP_LOCKED};
101e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** ALT lock toggle definition */
10277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private static final int[] mAltKeyToggle = {0, MetaKeyKeyListener.META_ALT_ON, MetaKeyKeyListener.META_ALT_LOCKED};
10377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /** Auto caps mode */
10477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean mAutoCaps = false;
1053fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
106e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Whether dismissing the keyboard when the enter key is pressed */
107e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private boolean mEnableAutoHideKeyboard = true;
1083fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
109e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Tutorial */
110e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private TutorialEN mTutorial;
111e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
112e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Whether tutorial mode or not */
113e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private boolean mEnableTutorial;
114e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
115e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Message for {@code mHandler} (execute prediction) */
116e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private static final int MSG_PREDICTION = 0;
117e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
118e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Message for {@code mHandler} (execute tutorial) */
119e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private static final int MSG_START_TUTORIAL = 1;
120e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
121e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Message for {@code mHandler} (close) */
122e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private static final int MSG_CLOSE = 2;
123e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
124e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Delay time(msec.) to start prediction after key input when the candidates view is not shown. */
125e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private static final int PREDICTION_DELAY_MS_1ST = 200;
126e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
127e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** Delay time(msec.) to start prediction after key input when the candidates view is shown. */
128e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private static final int PREDICTION_DELAY_MS_SHOWING_CANDIDATE = 200;
1293fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
130e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** {@code Handler} for drawing candidates/displaying tutorial */
131e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    Handler mHandler = new Handler() {
132e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            @Override public void handleMessage(Message msg) {
133e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                switch (msg.what) {
134e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                case MSG_PREDICTION:
135e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    updatePrediction();
136e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    break;
137e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                case MSG_START_TUTORIAL:
138e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    if (mTutorial == null) {
139e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        if (isInputViewShown()) {
140e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                            DefaultSoftKeyboardEN inputManager = ((DefaultSoftKeyboardEN) mInputViewManager);
141e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                            View v = inputManager.getKeyboardView();
142e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                            mTutorial = new TutorialEN(OpenWnnEN.this, v, inputManager);
1433fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
144e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                            mTutorial.start();
145e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        } else {
146e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                            /* Try again soon if the view is not yet showing */
147e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                            sendMessageDelayed(obtainMessage(MSG_START_TUTORIAL), 100);
148e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        }
149e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    }
150e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    break;
151e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                case MSG_CLOSE:
152e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    if (mConverterEN != null) mConverterEN.close();
153e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    if (mSymbolList != null) mSymbolList.close();
154e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    break;
155e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                }
156e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            }
157e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        };
158053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
159053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /**
160053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     * Constructor
161053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     */
16277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    public OpenWnnEN() {
16377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        super();
164053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        mSelf = this;
165053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
16677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* used by OpenWnn */
16777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mComposingText = new ComposingText();
16877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mCandidatesViewManager = new TextCandidatesViewManager(-1);
16977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mInputViewManager = new DefaultSoftKeyboardEN();
17059aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma
17159aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        if (OpenWnn.getCurrentIme() != null) {
17259aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma            if (mConverterEN == null) {
17359aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma                mConverterEN = new OpenWnnEngineEN("/data/data/jp.co.omronsoft.openwnn/writableEN.dic");
17459aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma            }
17559aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        }
17659aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma
17777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mConverter = mConverterEN;
17877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mSymbolList = null;
17977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
18077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* etc */
18177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mDisplayText = new SpannableStringBuilder();
18277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mAutoHideMode = false;
18377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mSymbolMode = false;
18477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mOptPrediction = true;
18577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mOptSpellCorrection = true;
18677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mOptLearning = true;
18777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
188053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
189053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /**
190053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     * Constructor
191053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     *
192053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     * @param context       The context
193053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     */
194053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    public OpenWnnEN(Context context) {
195053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        this();
196053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        attachBaseContext(context);
197053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    }
19877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
19977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Get the instance of this service.
20077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * <br>
20177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Before using this method, the constructor of this service must be invoked.
2023fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng     *
20377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @return      The instance of this object
20477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
20577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    public static OpenWnnEN getInstance() {
20677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        return mSelf;
20777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
20877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
20977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
21077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Insert a character into the composing text.
21177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
21277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param chars     A array of character
21377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
21477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void insertCharToComposingText(char[] chars) {
21577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        StrSegment seg = new StrSegment(chars);
21677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
21777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (chars[0] == SPACE[0] || chars[0] == '\u0009') {
21877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* if the character is a space, commit the composing text */
21977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(1);
22077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(seg.string);
22177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mComposingText.clear();
22277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        } else if (mWordSeparators.contains(seg.string)) {
22377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* if the character is a separator, remove an auto-inserted space and commit the composing text. */
22477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (mPreviousEventCode == OpenWnnEvent.SELECT_CANDIDATE) {
22577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mInputConnection.deleteSurroundingText(1, 0);
22677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
22777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(1);
22877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(seg.string);
22977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mComposingText.clear();
23077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        } else {
23177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mComposingText.insertStrSegment(0, 1, seg);
23277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            updateComposingText(1);
23377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
23477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
23577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
23677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
23777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Insert a character into the composing text.
23877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
23977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param charCode      A character code
24077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @return              {@code true} if success; {@code false} if an error occurs.
24177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
24277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean insertCharToComposingText(int charCode) {
243053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        if (charCode == 0) {
244053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            return false;
245053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        }
246053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        insertCharToComposingText(Character.toChars(charCode));
247053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        return true;
248053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    }
249053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
25077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
25177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Get the shift key state from the editor.
25277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
25377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param editor    Editor
25477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
25577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @return          State ID of the shift key (0:off, 1:on)
25677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
25777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    protected int getShiftKeyState(EditorInfo editor) {
25877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        return (getCurrentInputConnection().getCursorCapsMode(editor.inputType) == 0) ? 0 : 1;
25977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
260053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
261053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /**
262053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     * Set the mode of the symbol list.
2633fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng     *
26477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param mode      {@code SymbolList.SYMBOL_ENGLISH} or {@code null}.
265053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     */
26677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void setSymbolMode(String mode) {
26777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mode != null) {
268e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mHandler.removeMessages(MSG_PREDICTION);
26977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mSymbolMode = true;
27077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mSymbolList.setDictionary(mode);
27177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mConverter = mSymbolList;
27277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        } else {
27377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (!mSymbolMode) {
27477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return;
27577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
276e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mHandler.removeMessages(MSG_PREDICTION);
27777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mSymbolMode = false;
27877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mConverter = mConverterEN;
27977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
28077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
28177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
28277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /***********************************************************************
28377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * InputMethodServer
28477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     ***********************************************************************/
285053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onCreate */
28677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override public void onCreate() {
28777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        super.onCreate();
28877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mWordSeparators = getResources().getString(R.string.en_word_separators);
28977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
29059aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        if (mConverterEN == null) {
29159aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma            mConverterEN = new OpenWnnEngineEN("/data/data/jp.co.omronsoft.openwnn/writableEN.dic");
29259aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        }
29359aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma
29477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mSymbolList == null) {
29577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mSymbolList = new SymbolList(this, SymbolList.LANG_EN);
29677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
29777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
2983fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
2997b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    /** @see jp.co.omronsoft.openwnn.OpenWnn#onCreateInputView */
3007b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    @Override public View onCreateInputView() {
30177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        int hiddenState = getResources().getConfiguration().hardKeyboardHidden;
30277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        boolean hidden = (hiddenState == Configuration.HARDKEYBOARDHIDDEN_YES);
30377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        ((DefaultSoftKeyboardEN) mInputViewManager).setHardKeyboardHidden(hidden);
304e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mEnableTutorial = hidden;
3057b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa
3067b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        return super.onCreateInputView();
3077b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa    }
308053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
309053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onStartInputView */
31077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
31177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        super.onStartInputView(attribute, restarting);
312053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
31377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* initialize views */
31477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mCandidatesViewManager.clearCandidates();
31577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_CLOSE);
316053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
31777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mHardShift = 0;
31877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mHardAlt   = 0;
3197b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        updateMetaKeyStateDisplay();
320053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
3217b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        /* load preferences */
32277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
323053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
32477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* auto caps mode */
32577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mAutoCaps = pref.getBoolean("auto_caps", true);
326053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
32777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* set TextCandidatesViewManager's option */
32877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        ((TextCandidatesViewManager)mCandidatesViewManager).setAutoHide(true);
3293fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
33077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* display status icon */
33177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        showStatusIcon(R.drawable.immodeic_half_alphabet);
332053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
3337b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        if (mComposingText != null) {
33477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mComposingText.clear();
3357b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }
3363feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        /* initialize the engine's state */
3373feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        fitInputType(pref, attribute);
3383fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
339e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        ((DefaultSoftKeyboard) mInputViewManager).resetCurrentKeyboard();
34059aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma
34159aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        if (OpenWnn.isXLarge()) {
34259aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma            mTextCandidatesViewManager.setPreferences(pref);
34359aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        }
34477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
345053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
346e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** @see jp.co.omronsoft.openwnn.OpenWnn#hideWindow */
347e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    @Override public void hideWindow() {
34859aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma        ((BaseInputView)((DefaultSoftKeyboard) mInputViewManager).getCurrentView()).closeDialog();
349e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mComposingText.clear();
350e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mInputViewManager.onUpdateState(this);
351e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mHandler.removeMessages(MSG_START_TUTORIAL);
352e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mInputViewManager.closing();
353e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (mTutorial != null) {
354e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mTutorial.close();
355e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mTutorial = null;
356e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        }
357e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
358e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        super.hideWindow();
35977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
360053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
361053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onUpdateSelection */
36277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override public void onUpdateSelection(int oldSelStart, int oldSelEnd,
36377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            int newSelStart, int newSelEnd, int candidatesStart,
36477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            int candidatesEnd) {
365e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
366e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        boolean isNotComposing = ((candidatesStart < 0) && (candidatesEnd < 0));
367e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (isNotComposing) {
368e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mComposingText.clear();
36977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            updateComposingText(1);
370e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        } else {
371e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            if (mComposingText.size(1) != 0) {
372e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                updateComposingText(1);
373e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            }
37477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
37577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
376053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
377053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onConfigurationChanged */
37877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override public void onConfigurationChanged(Configuration newConfig) {
37977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        try {
38077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            super.onConfigurationChanged(newConfig);
38177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (mInputConnection != null) {
38277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                updateComposingText(1);
38377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
384e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            /* Hardware keyboard */
385e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            int hiddenState = newConfig.hardKeyboardHidden;
386e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            boolean hidden = (hiddenState == Configuration.HARDKEYBOARDHIDDEN_YES);
387e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mEnableTutorial = hidden;
38877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        } catch (Exception ex) {
38977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
39077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
391053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
392053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onEvaluateFullscreenMode */
39377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override public boolean onEvaluateFullscreenMode() {
39477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        return false;
39577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
396053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
397053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onEvaluateInputViewShown */
39877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override public boolean onEvaluateInputViewShown() {
39977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        return true;
40077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
401053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
40277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /***********************************************************************
40377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * OpenWnn
40477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     ***********************************************************************/
405053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project    /** @see jp.co.omronsoft.openwnn.OpenWnn#onEvent */
40677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    @Override synchronized public boolean onEvent(OpenWnnEvent ev) {
407053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        /* handling events which are valid when InputConnection is not active. */
408053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        switch (ev.code) {
4093fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
4103feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        case OpenWnnEvent.KEYUP:
4113feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            onKeyUpEvent(ev.keyEvent);
4123feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            return true;
4133fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
414053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        case OpenWnnEvent.INITIALIZE_LEARNING_DICTIONARY:
415053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            return mConverterEN.initializeDictionary( WnnEngine.DICTIONARY_TYPE_LEARN );
416053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
417053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        case OpenWnnEvent.INITIALIZE_USER_DICTIONARY:
418053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            return mConverterEN.initializeDictionary( WnnEngine.DICTIONARY_TYPE_USER );
419053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
420053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        case OpenWnnEvent.LIST_WORDS_IN_USER_DICTIONARY:
421053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            mUserDictionaryWords = mConverterEN.getUserDictionaryWords( );
422053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            return true;
423053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
42477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.GET_WORD:
42577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if( mUserDictionaryWords != null ) {
42677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                ev.word = mUserDictionaryWords[ 0 ];
42777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                for( int i = 0 ; i < mUserDictionaryWords.length-1 ; i++ ) {
42877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    mUserDictionaryWords[ i ] = mUserDictionaryWords[ i + 1 ];
42977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
43077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mUserDictionaryWords[ mUserDictionaryWords.length-1 ] = null;
43177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if( mUserDictionaryWords[ 0 ] == null ) {
43277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    mUserDictionaryWords = null;
43377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
43477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
43577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
436053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            break;
437053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
43877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.ADD_WORD:
43977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mConverterEN.addWord(ev.word);
44077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            return true;
441053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
44277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.DELETE_WORD:
44377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mConverterEN.deleteWord(ev.word);
44477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            return true;
445053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
44677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.CHANGE_MODE:
447053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            return false;
4483fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
449e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        case OpenWnnEvent.UPDATE_CANDIDATE:
450e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            updateComposingText(ComposingText.LAYER1);
451e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            return true;
452e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
4537b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        case OpenWnnEvent.CHANGE_INPUT_VIEW:
45477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            setInputView(onCreateInputView());
4557b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            return true;
4567b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa
4577b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        case OpenWnnEvent.CANDIDATE_VIEW_TOUCH:
4587b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            boolean ret;
4597b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa                ret = ((TextCandidatesViewManager)mCandidatesViewManager).onTouchSync();
4607b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            return ret;
461053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
462053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        default:
463053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            break;
46477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
465053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
46677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        dismissPopupKeyboard();
4673feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        KeyEvent keyEvent = ev.keyEvent;
4683feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        int keyCode = 0;
4693feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        if (keyEvent != null) {
4703feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            keyCode = keyEvent.getKeyCode();
4713feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        }
47277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mDirectInputMode) {
4733feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            if (ev.code == OpenWnnEvent.INPUT_SOFT_KEY && mInputConnection != null) {
4743feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mInputConnection.sendKeyEvent(keyEvent);
47577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mInputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
4763feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                                                           keyEvent.getKeyCode()));
4773feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            }
47877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            return false;
47977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
48077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
48177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (ev.code == OpenWnnEvent.LIST_CANDIDATES_FULL) {
48277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_FULL);
48377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            return true;
484e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        } else if (ev.code == OpenWnnEvent.LIST_CANDIDATES_NORMAL) {
485e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_NORMAL);
486e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            return true;
48777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
48877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
48977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        boolean ret = false;
49077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        switch (ev.code) {
49177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.INPUT_CHAR:
49277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa             ((TextCandidatesViewManager)mCandidatesViewManager).setAutoHide(false);
49377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            EditorInfo edit = getCurrentInputEditorInfo();
49477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if( edit.inputType == EditorInfo.TYPE_CLASS_PHONE){
4953fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng                commitText(new String(ev.chars));
49677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }else{
49777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                setSymbolMode(null);
49877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                insertCharToComposingText(ev.chars);
49977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                ret = true;
50077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mPreviousEventCode = ev.code;
50177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
50277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            break;
50377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
50477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.INPUT_KEY:
50577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            keyCode = ev.keyEvent.getKeyCode();
50677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* update shift/alt state */
50777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            switch (keyCode) {
50877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_ALT_LEFT:
50977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_ALT_RIGHT:
51077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (ev.keyEvent.getRepeatCount() == 0) {
51177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if (++mHardAlt > 2) { mHardAlt = 0; }
51277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
5133feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mAltPressing   = true;
51477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                updateMetaKeyStateDisplay();
51577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
51677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
51777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_SHIFT_LEFT:
51877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_SHIFT_RIGHT:
51977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (ev.keyEvent.getRepeatCount() == 0) {
52077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if (++mHardShift > 2) { mHardShift = 0; }
52177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
5223feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mShiftPressing = true;
52377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                updateMetaKeyStateDisplay();
52477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
52577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
52677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            setSymbolMode(null);
52777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            updateComposingText(1);
52877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* handle other key event */
52977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            ret = processKeyEvent(ev.keyEvent);
53077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mPreviousEventCode = ev.code;
53177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            break;
53277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
53377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.INPUT_SOFT_KEY:
53477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            setSymbolMode(null);
53577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            updateComposingText(1);
53677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            ret = processKeyEvent(ev.keyEvent);
53777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (!ret) {
5380470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa            	int code = keyEvent.getKeyCode();
53959aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma            	if (code == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_NUMPAD_ENTER) {
5400470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa                    sendKeyChar('\n');
5410470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa            	} else {
5420470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa                    mInputConnection.sendKeyEvent(keyEvent);
5430470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa                    mInputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, code));
5440470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa            	}
545053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                ret = true;
54677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
54777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mPreviousEventCode = ev.code;
54877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            break;
549053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
55077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.SELECT_CANDIDATE:
551053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            if (mSymbolMode) {
552053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                commitText(ev.word, false);
553053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            } else {
5543fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng                if (mWordSeparators.contains(ev.word.candidate) &&
555053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                    mPreviousEventCode == OpenWnnEvent.SELECT_CANDIDATE) {
556053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                    mInputConnection.deleteSurroundingText(1, 0);
557053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                }
558053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                commitText(ev.word, true);
559053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            }
560053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            mComposingText.clear();
561053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            mPreviousEventCode = ev.code;
562053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            updateComposingText(1);
563053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            break;
564053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
56577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        case OpenWnnEvent.LIST_SYMBOLS:
56677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(1);
56777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mComposingText.clear();
56877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            setSymbolMode(SymbolList.SYMBOL_ENGLISH);
56977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            updateComposingText(1);
57077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            break;
57177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
57277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        default:
57377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            break;
57477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
57577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
57677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mCandidatesViewManager.getViewType() == CandidatesViewManager.VIEW_TYPE_FULL) {
577e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        	mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_NORMAL);
57877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
57977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
58077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        return ret;
58177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
58277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
58377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /***********************************************************************
58477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * OpenWnnEN
58577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     ***********************************************************************/
58677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
58777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Handling KeyEvent
58877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * <br>
58977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * This method is called from {@link #onEvent()}.
59077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
59177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param ev   A key event
59277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @return      {@code true} if the event is processed in this method; {@code false} if the event is not processed in this method
59377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
59477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private boolean processKeyEvent(KeyEvent ev) {
59577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
59677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        int key = ev.getKeyCode();
59777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        EditorInfo edit = getCurrentInputEditorInfo();
59877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* keys which produce a glyph */
59977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (ev.isPrintingKey()) {
60077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* do nothing if the character is not able to display or the character is dead key */
60177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if ((mHardShift > 0 && mHardAlt > 0) || (ev.isAltPressed() && ev.isShiftPressed())) {
60277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                int charCode = ev.getUnicodeChar(MetaKeyKeyListener.META_SHIFT_ON | MetaKeyKeyListener.META_ALT_ON);
60377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (charCode == 0 || (charCode & KeyCharacterMap.COMBINING_ACCENT) != 0 || charCode == PRIVATE_AREA_CODE) {
6043feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    if(mHardShift == 1){
6053feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                        mShiftPressing = false;
6063feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    }
6073feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    if(mHardAlt == 1){
6083feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                        mAltPressing   = false;
6093feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    }
61077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if(!ev.isAltPressed()){
61177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        if (mHardAlt == 1) {
61277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                            mHardAlt = 0;
61377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        }
61477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    }
61577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if(!ev.isShiftPressed()){
61677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        if (mHardShift == 1) {
61777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                            mHardShift = 0;
61877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        }
61977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    }
62077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if(!ev.isShiftPressed() && !ev.isAltPressed()){
62177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        updateMetaKeyStateDisplay();
62277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    }
62377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    return true;
62477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
62577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
6263fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
62777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            ((TextCandidatesViewManager)mCandidatesViewManager).setAutoHide(false);
628053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
629053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            /* get the key character */
63077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (mHardShift== 0  && mHardAlt == 0) {
631053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                /* no meta key is locked */
63277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                int shift = (mAutoCaps) ? getShiftKeyState(edit) : 0;
63377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (shift != mHardShift && (key >= KeyEvent.KEYCODE_A && key <= KeyEvent.KEYCODE_Z)) {
634053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                    /* handling auto caps for a alphabet character */
635053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                    insertCharToComposingText(ev.getUnicodeChar(MetaKeyKeyListener.META_SHIFT_ON));
636053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                } else {
637053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                    insertCharToComposingText(ev.getUnicodeChar());
638053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                }
63977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            } else {
640053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                insertCharToComposingText(ev.getUnicodeChar(mShiftKeyToggle[mHardShift]
641053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                                                            | mAltKeyToggle[mHardAlt]));
6423feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                if(mHardShift == 1){
6433feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    mShiftPressing = false;
6443feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                }
6453feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                if(mHardAlt == 1){
6463feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    mAltPressing   = false;
6473feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                }
648053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                /* back to 0 (off) if 1 (on/not locked) */
64977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if(!ev.isAltPressed()){
65077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if (mHardAlt == 1) {
65177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        mHardAlt = 0;
65277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    }
65377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
65477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if(!ev.isShiftPressed()){
65577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    if (mHardShift == 1) {
65677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        mHardShift = 0;
65777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    }
65877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
65977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if(!ev.isShiftPressed() && !ev.isAltPressed()){
6603feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                    updateMetaKeyStateDisplay();
66177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
66277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
663053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
664053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            if (edit.inputType == EditorInfo.TYPE_CLASS_PHONE) {
66577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                commitText(1);
66677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mComposingText.clear();
66777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
66877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
66977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            return true;
67077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
67177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        } else if (key == KeyEvent.KEYCODE_SPACE) {
67277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (ev.isAltPressed()) {
673053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                /* display the symbol list (G1 specific. same as KEYCODE_SYM) */
67477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                commitText(1);
67577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mComposingText.clear();
67677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                setSymbolMode(SymbolList.SYMBOL_ENGLISH);
6773fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng                updateComposingText(1);
67877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mHardAlt = 0;
679053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project                updateMetaKeyStateDisplay();
68077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            } else {
6813fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng                insertCharToComposingText(SPACE);
68277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
68377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            return true;
684053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project        } else if (key == KeyEvent.KEYCODE_SYM) {
685053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            /* display the symbol list */
686053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            commitText(1);
687053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            mComposingText.clear();
688053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            setSymbolMode(SymbolList.SYMBOL_ENGLISH);
6893fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng            updateComposingText(1);
690053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            mHardAlt = 0;
691053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            updateMetaKeyStateDisplay();
6923fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng        }
69377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
69477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
69577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* Functional key */
69677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mComposingText.size(1) > 0) {
69777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            switch (key) {
69877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_DEL:
69977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mComposingText.delete(1, false);
70077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                updateComposingText(1);
70177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
70277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
70377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_BACK:
70477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (mCandidatesViewManager.getViewType() == CandidatesViewManager.VIEW_TYPE_FULL) {
70577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_NORMAL);
70677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                } else {
70777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    mComposingText.clear();
70877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    updateComposingText(1);
70977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
71077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
71177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
71277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_DPAD_LEFT:
71377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mComposingText.moveCursor(1, -1);
71477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                updateComposingText(1);
71577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
71677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
71777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_DPAD_RIGHT:
71877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mComposingText.moveCursor(1, 1);
71977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                updateComposingText(1);
72077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
72177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
72277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_ENTER:
72359aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma            case KeyEvent.KEYCODE_NUMPAD_ENTER:
72477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            case KeyEvent.KEYCODE_DPAD_CENTER:
72577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                commitText(1);
72677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                mComposingText.clear();
727e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                if (mEnableAutoHideKeyboard) {
728e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    mInputViewManager.closing();
729e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    requestHideSelf(0);
730e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                }
73177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                return true;
73277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
73377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            default:
73459aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma                return !isThroughKeyCode(key);
73577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
736e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        } else {
737e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            /* if there is no composing string. */
738e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            if (mCandidatesViewManager.getCurrentView().isShown()) {
739e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            	if (key == KeyEvent.KEYCODE_BACK) {
740e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            		if (mCandidatesViewManager.getViewType() == CandidatesViewManager.VIEW_TYPE_FULL) {
741e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            			mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_NORMAL);
742e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            		} else {
743e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            			mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_CLOSE);
744e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            		}
745e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            		return true;
746e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            	}
747e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            } else {
748e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                switch (key) {
749e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                case KeyEvent.KEYCODE_DPAD_CENTER:
750e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                case KeyEvent.KEYCODE_ENTER:
75159aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma                case KeyEvent.KEYCODE_NUMPAD_ENTER:
752e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    if (mEnableAutoHideKeyboard) {
753e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        mInputViewManager.closing();
754e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        requestHideSelf(0);
755e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        return true;
756e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    }
757e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    break;
758e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                case KeyEvent.KEYCODE_BACK:
759e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    /*
760e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                     * If 'BACK' key is pressed when the SW-keyboard is shown
761e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                     * and the candidates view is not shown, dismiss the SW-keyboard.
762e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                     */
763e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    if (isInputViewShown()) {
764e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        mInputViewManager.closing();
765e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        requestHideSelf(0);
766e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                        return true;
767e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    }
768e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    break;
769e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                default:
770e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    break;
771e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                }
772e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            }
77377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
77477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
77577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        return false;
77677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
77777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
77877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
779e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     * Thread for updating the candidates view
78077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
781e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private void updatePrediction() {
782e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        int candidates = 0;
783e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (mConverter != null) {
784e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            /* normal prediction */
785e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            candidates = mConverter.predict(mComposingText, 0, -1);
78677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
787e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        /* update the candidates view */
788e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (candidates > 0) {
789e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mCandidatesViewManager.displayCandidates(mConverter);
790e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        } else {
791e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mCandidatesViewManager.clearCandidates();
792e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        }
793e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    }
79477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
79577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
79677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Update the composing text.
79777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
79877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param layer  {@link mComposingText}'s layer to display
79977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
80077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void updateComposingText(int layer) {
80177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        /* update the candidates view */
80277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (!mOptPrediction) {
80377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(1);
80477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mComposingText.clear();
805053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project            if (mSymbolMode) {
806e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mHandler.removeMessages(MSG_PREDICTION);
807e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_PREDICTION), 0);
80877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
80977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        } else {
81077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (mComposingText.size(1) != 0) {
811e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mHandler.removeMessages(MSG_PREDICTION);
812e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                if (mCandidatesViewManager.getCurrentView().isShown()) {
813e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_PREDICTION),
814e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                                                PREDICTION_DELAY_MS_SHOWING_CANDIDATE);
815e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                } else {
816e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_PREDICTION),
817e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                                                PREDICTION_DELAY_MS_1ST);
818e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                }
81977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            } else {
820e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mHandler.removeMessages(MSG_PREDICTION);
821e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_PREDICTION), 0);
82277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
82377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
82477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* notice to the input view */
82577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            this.mInputViewManager.onUpdateState(this);
82677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
82777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* set the text for displaying as the composing text */
82877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            SpannableStringBuilder disp = mDisplayText;
82977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            disp.clear();
83077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            disp.insert(0, mComposingText.toString(layer));
83177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
83277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* add decoration to the text */
83377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            int cursor = mComposingText.getCursor(layer);
83477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            if (disp.length() != 0) {
83577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (cursor > 0 && cursor < disp.length()) {
83677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                    disp.setSpan(SPAN_EXACT_BGCOLOR_HL, 0, cursor,
83777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
83877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
83977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                if (cursor < disp.length()) {
8407b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa                    mDisplayText.setSpan(SPAN_REMAIN_BGCOLOR_HL, cursor, disp.length(),
8417b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
842e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    mDisplayText.setSpan(SPAN_TEXTCOLOR, 0, disp.length(),
84359aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
84477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                }
84559aefa242169b7a51c2381daee58ff22fd1834ceJunichi Monma
84677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                disp.setSpan(SPAN_UNDERLINE, 0, disp.length(),
84777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
84877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            }
8493fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
8507b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            int displayCursor = cursor;
8517b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            if (FIX_CURSOR_TEXT_END) {
8527b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa                displayCursor = (cursor == 0) ?  0 : 1;
8533fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng            }
85477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            /* update the composing text on the EditView */
85577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mInputConnection.setComposingText(disp, displayCursor);
85677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
85777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
85877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
85977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
86077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Commit the composing text.
86177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
86277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param layer  {@link mComposingText}'s layer to commit.
86377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
86477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void commitText(int layer) {
86577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        String tmp = mComposingText.toString(layer);
86677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
86777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mOptLearning && mConverter != null && tmp.length() > 0) {
86877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            WnnWord word = new WnnWord(tmp, tmp);
86977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mConverter.learn(word);
87077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
871053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
8727b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        mInputConnection.commitText(tmp, (FIX_CURSOR_TEXT_END ? 1 : tmp.length()));
87377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mCandidatesViewManager.clearCandidates();
87477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
875053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
87677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
87777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Commit a word
87877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
87977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param word          A word to commit
88077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param withSpace     Append a space after the word if {@code true}.
88177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
88277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void commitText(WnnWord word, boolean withSpace) {
883053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
88477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (mOptLearning && mConverter != null) {
88577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            mConverter.learn(word);
88677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
887053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
8887b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        mInputConnection.commitText(word.candidate, (FIX_CURSOR_TEXT_END ? 1 : word.candidate.length()));
889053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
89077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (withSpace) {
89177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            commitText(" ");
8923fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng        }
89377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
894053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project
89577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
89677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Commit a string
897053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     * <br>
898053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project     * The string is not registered into the learning dictionary.
89977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
90077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param str  A string to commit
90177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
90277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void commitText(String str) {
9037b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        mInputConnection.commitText(str, (FIX_CURSOR_TEXT_END ? 1 : str.length()));
90477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        mCandidatesViewManager.clearCandidates();
90577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
90677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
90777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
90877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Dismiss the pop-up keyboard
90977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
91077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    protected void dismissPopupKeyboard() {
91177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        DefaultSoftKeyboardEN kbd = (DefaultSoftKeyboardEN)mInputViewManager;
91277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        if (kbd != null) {
91377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa            kbd.dismissPopupKeyboard();
91477ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa        }
91577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
91677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
91777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
91877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Display current meta-key state.
91977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
92077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    private void updateMetaKeyStateDisplay() {
9217b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        int mode = 0;
9227b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        if(mHardShift == 0 && mHardAlt == 0){
9237b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_OFF_ALT_OFF;
9247b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 1 && mHardAlt == 0){
9257b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_ON_ALT_OFF;
9267b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 2  && mHardAlt == 0){
9277b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_LOCK_ALT_OFF;
9287b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 0 && mHardAlt == 1){
9297b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_OFF_ALT_ON;
9307b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 0 && mHardAlt == 2){
9317b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_OFF_ALT_LOCK;
9327b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 1 && mHardAlt == 1){
9337b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_ON_ALT_ON;
9347b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 1 && mHardAlt == 2){
9357b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_ON_ALT_LOCK;
9367b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 2 && mHardAlt == 1){
9377b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_LOCK_ALT_ON;
9387b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else if(mHardShift == 2 && mHardAlt == 2){
9397b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_LOCK_ALT_LOCK;
9407b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }else{
9417b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa            mode = DefaultSoftKeyboard.HARD_KEYMODE_SHIFT_OFF_ALT_OFF;
9427b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        }
9433feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa
9447b3b414976b6b3d6d85459de79e843d5ab35ed06Daisuke Miyakawa        ((DefaultSoftKeyboard) mInputViewManager).updateIndicator(mode);
94577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    }
94677ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa
94777ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa    /**
94877ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * Handling KeyEvent(KEYUP)
94977ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * <br>
95077ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * This method is called from {@link #onEvent()}.
95177ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     *
95277ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param ev   An up key event
95377ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     */
9543feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    private void onKeyUpEvent(KeyEvent ev) {
9553feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        int key = ev.getKeyCode();
9563feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        if(!mShiftPressing){
9573feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            if(key == KeyEvent.KEYCODE_SHIFT_LEFT || key == KeyEvent.KEYCODE_SHIFT_RIGHT){
9583feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mHardShift = 0;
9593feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mShiftPressing = true;
9603feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                updateMetaKeyStateDisplay();
9613feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            }
9623feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        }
9633feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        if(!mAltPressing ){
9643feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            if(key == KeyEvent.KEYCODE_ALT_LEFT || key == KeyEvent.KEYCODE_ALT_RIGHT){
9653feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mHardAlt = 0;
9663feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                mAltPressing   = true;
9673feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa                updateMetaKeyStateDisplay();
9683feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            }
9693feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        }
9703feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    }
9713feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    /**
9723feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa     * Fits an editor info.
9733fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng     *
9743feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa     * @param preferences  The preference data.
97577ffa9b0b986a2d70143f63cdaa8451bf1674f84Daisuke Miyakawa     * @param info          The editor info.
9763feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa     */
9773feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    private void fitInputType(SharedPreferences preference, EditorInfo info) {
9783feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        if (info.inputType == EditorInfo.TYPE_NULL) {
9793feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            mDirectInputMode = true;
9803feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa            return;
9813feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa        }
982e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
983e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mEnableAutoHideKeyboard = false;
9843fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng
985e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        /* set prediction & spell correction mode */
986e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mOptPrediction      = preference.getBoolean("opt_en_prediction", true);
987e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mOptSpellCorrection = preference.getBoolean("opt_en_spell_correction", true);
988e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mOptLearning        = preference.getBoolean("opt_en_enable_learning", true);
989e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
990e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        /* prediction on/off */
991e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        switch (info.inputType & EditorInfo.TYPE_MASK_CLASS) {
992e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        case EditorInfo.TYPE_CLASS_NUMBER:
993e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        case EditorInfo.TYPE_CLASS_DATETIME:
994e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        case EditorInfo.TYPE_CLASS_PHONE:
995e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mOptPrediction = false;
996e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mOptLearning = false;
997e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            break;
998e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
999e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        case EditorInfo.TYPE_CLASS_TEXT:
1000e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            switch (info.inputType & EditorInfo.TYPE_MASK_VARIATION) {
1001e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            case EditorInfo.TYPE_TEXT_VARIATION_PASSWORD:
10020470b8581ba08779a7cc223c050e7b3556891f45Daisuke Miyakawa            case EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD:
1003e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mOptLearning = false;
1004e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mOptPrediction = false;
1005e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                break;
1006e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1007e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            case EditorInfo.TYPE_TEXT_VARIATION_PHONETIC:
1008e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mOptLearning = false;
1009e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                mOptPrediction = false;
1010e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                break;
1011e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            default:
1012e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                break;
1013e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            }
1014e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        }
1015e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1016e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        /* doesn't learn any word if it is not prediction mode */
1017e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (!mOptPrediction) {
1018e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mOptLearning = false;
1019e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        }
1020e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1021e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        /* set engine's mode */
1022e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (mOptSpellCorrection) {
1023e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mConverterEN.setDictionary(OpenWnnEngineEN.DICT_FOR_CORRECT_MISTYPE);
1024e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        } else {
1025e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            mConverterEN.setDictionary(OpenWnnEngineEN.DICT_DEFAULT);
1026e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        }
1027e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        checkTutorial(info.privateImeOptions);
1028e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    }
1029e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1030e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /**
1031e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     * Check and start the tutorial if it is the tutorial mode.
10323fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng     *
1033e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     * @param privateImeOptions IME's options
1034e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     */
1035e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private void checkTutorial(String privateImeOptions) {
1036e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        if (privateImeOptions == null) return;
10373fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng        if (privateImeOptions.equals("com.google.android.setupwizard:ShowTutorial")) {
1038e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            if ((mTutorial == null) && mEnableTutorial) startTutorial();
10393fbdfb6aaee678cb67c6967db4f8785f946a6ddaFreeman Ng        } else if (privateImeOptions.equals("com.google.android.setupwizard:HideTutorial")) {
1040e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            if (mTutorial != null) {
1041e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                if (mTutorial.close()) {
1042e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                    mTutorial = null;
1043e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa                }
1044e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa            }
1045e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        }
1046e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    }
1047e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1048e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /**
1049e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     * Start the tutorial
1050e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     */
1051e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    private void startTutorial() {
1052e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        DefaultSoftKeyboardEN inputManager = ((DefaultSoftKeyboardEN) mInputViewManager);
1053e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        View v = inputManager.getKeyboardView();
1054e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        v.setOnTouchListener(new View.OnTouchListener() {
1055e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa				public boolean onTouch(View v, MotionEvent event) {
1056e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa					return true;
1057e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa				}});
1058e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_TUTORIAL), 500);
1059e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    }
1060e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1061e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /**
1062e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     * Close the tutorial
1063e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa     */
1064e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    public void tutorialDone() {
1065e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mTutorial = null;
1066e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    }
1067e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa
1068e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    /** @see OpenWnn#close */
1069e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa    @Override protected void close() {
1070e83971e4dafd53e9fb00386355afdbae7e00c0cfDaisuke Miyakawa        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CLOSE), 0);
10713feb3d26d59be1626c59b72654020179e2b2ebe0Daisuke Miyakawa    }
1072053d50935e0e311286543bd7c535ae2c863c0deThe Android Open Source Project}
1073