OpenWnn.java revision e83971e4dafd53e9fb00386355afdbae7e00c0cf
1/*
2 * Copyright (C) 2008,2009  OMRON SOFTWARE Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package jp.co.omronsoft.openwnn;
18
19import android.inputmethodservice.InputMethodService;
20import android.view.WindowManager;
21import android.content.Context;
22import android.view.View;
23import android.view.KeyEvent;
24import android.content.SharedPreferences;
25import android.preference.PreferenceManager;
26
27import android.util.Log;
28import android.os.*;
29import android.view.inputmethod.*;
30import android.content.res.Configuration;
31import android.graphics.*;
32import android.graphics.drawable.*;
33import android.view.MotionEvent;
34
35/**
36 * The OpenWnn IME's base class.
37 *
38 * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
39 */
40public class OpenWnn extends InputMethodService {
41
42    /** Candidate view */
43    protected CandidatesViewManager  mCandidatesViewManager = null;
44    /** Input view (software keyboard) */
45    protected InputViewManager  mInputViewManager = null;
46    /** Conversion engine */
47    protected WnnEngine  mConverter = null;
48    /** Pre-converter (for Romaji-to-Kana input, Hangul input, etc.) */
49    protected LetterConverter  mPreConverter = null;
50    /** The inputing/editing string */
51    protected ComposingText  mComposingText = null;
52    /** The input connection */
53    protected InputConnection mInputConnection = null;
54    /** Auto hide candidate view */
55    protected boolean mAutoHideMode = true;
56    /** Direct input mode */
57    protected boolean mDirectInputMode = true;
58
59    /** Flag for checking if the previous down key event is consumed by OpenWnn  */
60    private boolean mConsumeDownEvent;
61
62    /**
63     * Constructor
64     */
65    public OpenWnn() {
66        super();
67    }
68
69    /***********************************************************************
70     * InputMethodService
71     **********************************************************************/
72    /** @see android.inputmethodservice.InputMethodService#onCreate */
73    @Override public void onCreate() {
74        super.onCreate();
75
76        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
77
78
79        if (mConverter != null) { mConverter.init(); }
80        if (mComposingText != null) { mComposingText.clear(); }
81    }
82
83    /** @see android.inputmethodservice.InputMethodService#onCreateCandidatesView */
84    @Override public View onCreateCandidatesView() {
85        if (mCandidatesViewManager != null) {
86            WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
87            View view = mCandidatesViewManager.initView(this,
88                                                        wm.getDefaultDisplay().getWidth(),
89                                                        wm.getDefaultDisplay().getHeight());
90            mCandidatesViewManager.setViewType(CandidatesViewManager.VIEW_TYPE_NORMAL);
91            return view;
92        } else {
93            return super.onCreateCandidatesView();
94        }
95    }
96
97    /** @see android.inputmethodservice.InputMethodService#onCreateInputView */
98    @Override public View onCreateInputView() {
99        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
100
101
102        if (mInputViewManager != null) {
103            WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
104            return mInputViewManager.initView(this,
105                                              wm.getDefaultDisplay().getWidth(),
106                                              wm.getDefaultDisplay().getHeight());
107        } else {
108            return super.onCreateInputView();
109        }
110    }
111
112    /** @see android.inputmethodservice.InputMethodService#onDestroy */
113    @Override public void onDestroy() {
114        super.onDestroy();
115
116        close();
117    }
118
119    /** @see android.inputmethodservice.InputMethodService#onKeyDown */
120    @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
121        mConsumeDownEvent = onEvent(new OpenWnnEvent(event));
122        if (!mConsumeDownEvent) {
123            return super.onKeyDown(keyCode, event);
124        }
125        return mConsumeDownEvent;
126    }
127
128    /** @see android.inputmethodservice.InputMethodService#onKeyUp */
129    @Override public boolean onKeyUp(int keyCode, KeyEvent event) {
130        boolean ret = mConsumeDownEvent;
131        if (!ret) {
132            ret = super.onKeyUp(keyCode, event);
133        }else{
134            onEvent(new OpenWnnEvent(event));
135        }
136        return ret;
137    }
138
139    /** @see android.inputmethodservice.InputMethodService#onStartInput */
140    @Override public void onStartInput(EditorInfo attribute, boolean restarting) {
141        super.onStartInput(attribute, restarting);
142        mInputConnection = getCurrentInputConnection();
143        if (mComposingText != null) {
144            mComposingText.clear();
145        }
146    }
147
148    /** @see android.inputmethodservice.InputMethodService#onStartInputView */
149    @Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
150        super.onStartInputView(attribute, restarting);
151        mInputConnection = getCurrentInputConnection();
152
153        setCandidatesViewShown(false);
154        if (mInputConnection != null) {
155            mDirectInputMode = false;
156            if (mConverter != null) { mConverter.init(); }
157        } else {
158            mDirectInputMode = true;
159        }
160        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
161        if (mCandidatesViewManager != null) { mCandidatesViewManager.setPreferences(pref);  }
162        if (mInputViewManager != null) { mInputViewManager.setPreferences(pref, attribute);  }
163        if (mPreConverter != null) { mPreConverter.setPreferences(pref);  }
164        if (mConverter != null) { mConverter.setPreferences(pref);  }
165    }
166
167    /** @see android.inputmethodservice.InputMethodService#requestHideSelf */
168    @Override public void requestHideSelf(int flag) {
169        super.requestHideSelf(flag);
170        if (mInputViewManager == null) {
171            hideWindow();
172        }
173    }
174
175    /** @see android.inputmethodservice.InputMethodService#setCandidatesViewShown */
176    @Override public void setCandidatesViewShown(boolean shown) {
177        super.setCandidatesViewShown(shown);
178        if (shown) {
179            showWindow(true);
180        } else {
181            if (mAutoHideMode && mInputViewManager == null) {
182                hideWindow();
183            }
184        }
185    }
186
187    /** @see android.inputmethodservice.InputMethodService#hideWindow */
188    @Override public void hideWindow() {
189        super.hideWindow();
190        mDirectInputMode = true;
191        hideStatusIcon();
192    }
193    /** @see android.inputmethodservice.InputMethodService#onComputeInsets */
194    @Override public void onComputeInsets(InputMethodService.Insets outInsets) {
195        super.onComputeInsets(outInsets);
196        outInsets.contentTopInsets = outInsets.visibleTopInsets;
197    }
198
199
200    /**********************************************************************
201     * OpenWnn
202     **********************************************************************/
203    /**
204     * Process an event.
205     *
206     * @param  ev  An event
207     * @return  {@code true} if the event is processed in this method; {@code false} if not.
208     */
209    public boolean onEvent(OpenWnnEvent ev) {
210        return false;
211    }
212
213    /**
214     * Search a character for toggle input.
215     *
216     * @param prevChar     The character input previous
217     * @param toggleTable  Toggle table
218     * @param reverse      {@code false} if toggle direction is forward, {@code true} if toggle direction is backward
219     * @return          A character ({@code null} if no character is found)
220     */
221    protected String searchToggleCharacter(String prevChar, String[] toggleTable, boolean reverse) {
222        for (int i = 0; i < toggleTable.length; i++) {
223            if (prevChar.equals(toggleTable[i])) {
224                if (reverse) {
225                    i--;
226                    if (i < 0) {
227                        return toggleTable[toggleTable.length - 1];
228                    } else {
229                        return toggleTable[i];
230                    }
231                } else {
232                    i++;
233                    if (i == toggleTable.length) {
234                        return toggleTable[0];
235                    } else {
236                        return toggleTable[i];
237                    }
238                }
239            }
240        }
241        return null;
242    }
243
244    /**
245     * Processing of resource open when IME ends.
246     */
247    protected void close() {
248        if (mConverter != null) { mConverter.close(); }
249    }
250}
251