My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.googlecode.tcime;

import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

/**
* Abstract class extended by ZhuyinIME and CangjieIME.
*/
public abstract class AbstractIME extends InputMethodService implements
KeyboardView.OnKeyboardActionListener, CandidateView.CandidateViewListener {

protected SoftKeyboardView inputView;
private CandidatesContainer candidatesContainer;
private KeyboardSwitch keyboardSwitch;
private Editor editor;
private WordDictionary wordDictionary;
private PhraseDictionary phraseDictionary;
private SoundMotionEffect effect;
private int orientation;

protected abstract KeyboardSwitch createKeyboardSwitch(Context context);
protected abstract Editor createEditor();
protected abstract WordDictionary createWordDictionary(Context context);

@Override
public void onCreate() {
super.onCreate();
keyboardSwitch = createKeyboardSwitch(this);
editor = createEditor();
wordDictionary = createWordDictionary(this);
phraseDictionary = new PhraseDictionary(this);
effect = new SoundMotionEffect(this);

orientation = getResources().getConfiguration().orientation;
// Use the following line to debug IME service.
//android.os.Debug.waitForDebugger();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
if (orientation != newConfig.orientation) {
// Clear composing text and candidates for orientation change.
escape();
orientation = newConfig.orientation;
}
super.onConfigurationChanged(newConfig);
}

@Override
public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart,
int newSelEnd, int candidatesStart, int candidatesEnd) {
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
candidatesStart, candidatesEnd);
if ((candidatesEnd != -1) &&
((newSelStart != candidatesEnd) || (newSelEnd != candidatesEnd))) {
// Clear composing text and its candidates for cursor movement.
escape();
}
// Update the caps-lock status for the current cursor position.
updateCursorCapsToInputView();
}

@Override
public void onComputeInsets(InputMethodService.Insets outInsets) {
super.onComputeInsets(outInsets);
outInsets.contentTopInsets = outInsets.visibleTopInsets;
}

@Override
public View onCreateInputView() {
inputView = (SoftKeyboardView) getLayoutInflater().inflate(
R.layout.input, null);
inputView.setOnKeyboardActionListener(this);
return inputView;
}

@Override
public View onCreateCandidatesView() {
candidatesContainer = (CandidatesContainer) getLayoutInflater().inflate(
R.layout.candidates, null);
candidatesContainer.setCandidateViewListener(this);
return candidatesContainer;
}

@Override
public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);

// Reset editor and candidates when the input-view is just being started.
editor.start(attribute.inputType);
clearCandidates();
effect.reset();

keyboardSwitch.initializeKeyboard(getMaxWidth());
// Select a keyboard based on the input type of the editing field.
keyboardSwitch.onStartInput(attribute.inputType);
bindKeyboardToInputView();
}

@Override
public void onFinishInput() {
// Clear composing as any active composing text will be finished, same as in
// onFinishInputView, onFinishCandidatesView, and onUnbindInput.
editor.clearComposingText(getCurrentInputConnection());
super.onFinishInput();
}

@Override
public void onFinishInputView(boolean finishingInput) {
editor.clearComposingText(getCurrentInputConnection());
super.onFinishInputView(finishingInput);
// Dismiss any pop-ups when the input-view is being finished and hidden.
inputView.closing();
}

@Override
public void onFinishCandidatesView(boolean finishingInput) {
editor.clearComposingText(getCurrentInputConnection());
super.onFinishCandidatesView(finishingInput);
}

@Override
public void onUnbindInput() {
editor.clearComposingText(getCurrentInputConnection());
super.onUnbindInput();
}

private void bindKeyboardToInputView() {
if (inputView != null) {
// Bind the selected keyboard to the input view.
inputView.setKeyboard(keyboardSwitch.getCurrentKeyboard());
updateCursorCapsToInputView();
}
}

private void updateCursorCapsToInputView() {
InputConnection ic = getCurrentInputConnection();
if ((ic != null) && (inputView != null)) {
int caps = 0;
EditorInfo ei = getCurrentInputEditorInfo();
if ((ei != null) && (ei.inputType != EditorInfo.TYPE_NULL)) {
caps = ic.getCursorCapsMode(ei.inputType);
}
inputView.updateCursorCaps(caps);
}
}

private void commitText(CharSequence text) {
if (editor.commitText(getCurrentInputConnection(), text)) {
// Clear candidates after committing any text.
clearCandidates();
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && (event.getRepeatCount() == 0)) {
// Handle the back-key to close the pop-up keyboards.
if ((inputView != null) && inputView.handleBack()) {
return true;
}
}
return super.onKeyDown(keyCode, event);
}

public void onKey(int primaryCode, int[] keyCodes) {
if (keyboardSwitch.onKey(primaryCode)) {
escape();
bindKeyboardToInputView();
return;
}
if (handleOption(primaryCode) || handleCapsLock(primaryCode)
|| handleEnter(primaryCode) || handleSpace(primaryCode)
|| handleDelete(primaryCode) || handleComposing(primaryCode)) {
return;
}
handleKey(primaryCode);
}

public void onText(CharSequence text) {
commitText(text);
}

public void onPress(int primaryCode) {
effect.vibrate();
effect.playSound();
}

public void onRelease(int primaryCode) {
// no-op
}

public void swipeLeft() {
// no-op
}

public void swipeRight() {
// no-op
}

public void swipeUp() {
// no-op
}

public void swipeDown() {
requestHideSelf(0);
}

public void onPickCandidate(String candidate) {
// Commit the picked candidate and suggest its following words.
commitText(candidate);
setCandidates(
phraseDictionary.getFollowingWords(candidate.charAt(0)), false);
}

private void clearCandidates() {
setCandidates("", false);
}

private void setCandidates(String words, boolean highlightDefault) {
if (candidatesContainer != null) {
candidatesContainer.setCandidates(words, highlightDefault);
setCandidatesViewShown((words.length() > 0) || editor.hasComposingText());
if (inputView != null) {
inputView.setEscape(candidatesContainer.isShown());
}
}
}

private boolean handleOption(int keyCode) {
if (keyCode == SoftKeyboard.KEYCODE_OPTIONS) {
// TODO: Do voice input here.
return true;
}
return false;
}

private boolean handleCapsLock(int keyCode) {
return (keyCode == Keyboard.KEYCODE_SHIFT) && inputView.toggleCapsLock();
}

private boolean handleEnter(int keyCode) {
if (keyCode == '\n') {
if (inputView.hasEscape()) {
escape();
} else if (editor.treatEnterAsLinkBreak()) {
commitText("\n");
} else {
sendKeyChar('\n');
}
return true;
}
return false;
}

private boolean handleSpace(int keyCode) {
if (keyCode == ' ') {
if ((candidatesContainer != null) && candidatesContainer.isShown()) {
// The space key could either pick the highlighted candidate or escape
// if there's no highlighted candidate and no composing-text.
if (!candidatesContainer.pickHighlighted()
&& !editor.hasComposingText()) {
escape();
}
} else {
commitText(" ");
}
return true;
}
return false;
}

private boolean handleDelete(int keyCode) {
// Handle delete-key only when no composing text.
if ((keyCode == Keyboard.KEYCODE_DELETE) && !editor.hasComposingText()) {
if (inputView.hasEscape()) {
escape();
} else {
sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
}
return true;
}
return false;
}

private boolean handleComposing(int keyCode) {
if (editor.compose(getCurrentInputConnection(), keyCode)) {
// Set the candidates for the updated composing-text and provide default
// highlight for the word candidates.
setCandidates(wordDictionary.getWords(editor.composingText()), true);
return true;
}
return false;
}

/**
* Handles input of SoftKeybaord key code that has not been consumed by
* other handling-methods.
*/
private void handleKey(int keyCode) {
if (isInputViewShown() && inputView.isShifted()) {
keyCode = Character.toUpperCase(keyCode);
}
commitText(String.valueOf((char) keyCode));
}

/**
* Simulates PC Esc-key function by clearing all composing-text or candidates.
*/
protected void escape() {
editor.clearComposingText(getCurrentInputConnection());
clearCandidates();
}
}

Change log

r13 by super.brother3 on Apr 27, 2010   Diff
Add vibration, sound, and licensing
preferences.
Go to: 
Project members, sign in to write a code review

Older revisions

r3 by super.brother3 on Mar 19, 2010   Diff
Move package
r2 by super.brother3 on Mar 18, 2010   Diff
Initial checkin.
All revisions of this file

File info

Size: 10209 bytes, 337 lines
Powered by Google Project Hosting