blob: ed223d1fd503dc5dbaf6bd66d98fb270f5c6263b [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.inputmethodservice;
18
19import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070020import com.android.internal.os.SomeArgs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.view.IInputMethodSession;
22
23import android.content.Context;
24import android.graphics.Rect;
25import android.os.Bundle;
Jeff Brownc28867a2013-03-26 15:42:39 -070026import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.util.Log;
Jeff Brownc28867a2013-03-26 15:42:39 -070029import android.util.SparseArray;
30import android.view.InputChannel;
31import android.view.InputDevice;
32import android.view.InputEvent;
33import android.view.InputEventReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.view.KeyEvent;
35import android.view.MotionEvent;
36import android.view.inputmethod.CompletionInfo;
37import android.view.inputmethod.ExtractedText;
38import android.view.inputmethod.InputMethodSession;
Yohei Yukawac2ddd602014-05-06 21:22:49 +090039import android.view.inputmethod.CursorAnchorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41class IInputMethodSessionWrapper extends IInputMethodSession.Stub
42 implements HandlerCaller.Callback {
43 private static final String TAG = "InputMethodWrapper";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45 private static final int DO_FINISH_INPUT = 60;
46 private static final int DO_DISPLAY_COMPLETIONS = 65;
47 private static final int DO_UPDATE_EXTRACTED_TEXT = 67;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 private static final int DO_UPDATE_SELECTION = 90;
49 private static final int DO_UPDATE_CURSOR = 95;
Yohei Yukawac2ddd602014-05-06 21:22:49 +090050 private static final int DO_UPDATE_CURSOR_ANCHOR_INFO = 99;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 private static final int DO_APP_PRIVATE_COMMAND = 100;
The Android Open Source Project4df24232009-03-05 14:34:35 -080052 private static final int DO_TOGGLE_SOFT_INPUT = 105;
Devin Taylor0c33ed22010-02-23 13:26:46 -060053 private static final int DO_FINISH_SESSION = 110;
satok863fcd62011-06-21 17:38:02 +090054 private static final int DO_VIEW_CLICKED = 115;
Devin Taylor0c33ed22010-02-23 13:26:46 -060055
56 HandlerCaller mCaller;
57 InputMethodSession mInputMethodSession;
Jeff Brownc28867a2013-03-26 15:42:39 -070058 InputChannel mChannel;
59 ImeInputEventReceiver mReceiver;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 public IInputMethodSessionWrapper(Context context,
Jeff Brownc28867a2013-03-26 15:42:39 -070062 InputMethodSession inputMethodSession, InputChannel channel) {
Mita Yuned218c72012-12-06 17:18:25 -080063 mCaller = new HandlerCaller(context, null,
64 this, true /*asyncHandler*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 mInputMethodSession = inputMethodSession;
Jeff Brownc28867a2013-03-26 15:42:39 -070066 mChannel = channel;
67 if (channel != null) {
68 mReceiver = new ImeInputEventReceiver(channel, context.getMainLooper());
69 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 }
71
72 public InputMethodSession getInternalInputMethodSession() {
73 return mInputMethodSession;
74 }
75
Jeff Brownc28867a2013-03-26 15:42:39 -070076 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public void executeMessage(Message msg) {
Jeff Brownc28867a2013-03-26 15:42:39 -070078 if (mInputMethodSession == null) {
Henrik Baard857cdff2010-07-20 11:42:19 +020079 // The session has been finished. Args needs to be recycled
80 // for cases below.
81 switch (msg.what) {
82 case DO_UPDATE_SELECTION:
83 case DO_APP_PRIVATE_COMMAND: {
84 SomeArgs args = (SomeArgs)msg.obj;
85 args.recycle();
86 }
87 }
Jeff Brownc28867a2013-03-26 15:42:39 -070088 return;
89 }
Ken Wakasaa308c032011-01-25 14:02:07 +090090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 switch (msg.what) {
92 case DO_FINISH_INPUT:
93 mInputMethodSession.finishInput();
94 return;
95 case DO_DISPLAY_COMPLETIONS:
96 mInputMethodSession.displayCompletions((CompletionInfo[])msg.obj);
97 return;
98 case DO_UPDATE_EXTRACTED_TEXT:
99 mInputMethodSession.updateExtractedText(msg.arg1,
100 (ExtractedText)msg.obj);
101 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 case DO_UPDATE_SELECTION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700103 SomeArgs args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 mInputMethodSession.updateSelection(args.argi1, args.argi2,
105 args.argi3, args.argi4, args.argi5, args.argi6);
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700106 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 return;
108 }
109 case DO_UPDATE_CURSOR: {
110 mInputMethodSession.updateCursor((Rect)msg.obj);
111 return;
112 }
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900113 case DO_UPDATE_CURSOR_ANCHOR_INFO: {
114 mInputMethodSession.updateCursorAnchorInfo((CursorAnchorInfo)msg.obj);
115 return;
116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 case DO_APP_PRIVATE_COMMAND: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700118 SomeArgs args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 mInputMethodSession.appPrivateCommand((String)args.arg1,
120 (Bundle)args.arg2);
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700121 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 return;
123 }
The Android Open Source Project4df24232009-03-05 14:34:35 -0800124 case DO_TOGGLE_SOFT_INPUT: {
125 mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2);
126 return;
127 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600128 case DO_FINISH_SESSION: {
Jeff Brownc28867a2013-03-26 15:42:39 -0700129 doFinishSession();
Devin Taylor0c33ed22010-02-23 13:26:46 -0600130 return;
131 }
satok863fcd62011-06-21 17:38:02 +0900132 case DO_VIEW_CLICKED: {
133 mInputMethodSession.viewClicked(msg.arg1 == 1);
134 return;
135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
137 Log.w(TAG, "Unhandled message code: " + msg.what);
138 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700139
140 private void doFinishSession() {
141 mInputMethodSession = null;
142 if (mReceiver != null) {
143 mReceiver.dispose();
144 mReceiver = null;
145 }
146 if (mChannel != null) {
147 mChannel.dispose();
148 mChannel = null;
149 }
150 }
151
152 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public void finishInput() {
154 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT));
155 }
156
Jeff Brownc28867a2013-03-26 15:42:39 -0700157 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public void displayCompletions(CompletionInfo[] completions) {
159 mCaller.executeOrSendMessage(mCaller.obtainMessageO(
160 DO_DISPLAY_COMPLETIONS, completions));
161 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700162
163 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public void updateExtractedText(int token, ExtractedText text) {
165 mCaller.executeOrSendMessage(mCaller.obtainMessageIO(
166 DO_UPDATE_EXTRACTED_TEXT, token, text));
167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Jeff Brownc28867a2013-03-26 15:42:39 -0700169 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public void updateSelection(int oldSelStart, int oldSelEnd,
171 int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) {
172 mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION,
173 oldSelStart, oldSelEnd, newSelStart, newSelEnd,
174 candidatesStart, candidatesEnd));
175 }
satok863fcd62011-06-21 17:38:02 +0900176
Jeff Brownc28867a2013-03-26 15:42:39 -0700177 @Override
satok863fcd62011-06-21 17:38:02 +0900178 public void viewClicked(boolean focusChanged) {
Jeff Brownc28867a2013-03-26 15:42:39 -0700179 mCaller.executeOrSendMessage(
180 mCaller.obtainMessageI(DO_VIEW_CLICKED, focusChanged ? 1 : 0));
satok863fcd62011-06-21 17:38:02 +0900181 }
182
Jeff Brownc28867a2013-03-26 15:42:39 -0700183 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public void updateCursor(Rect newCursor) {
Jeff Brownc28867a2013-03-26 15:42:39 -0700185 mCaller.executeOrSendMessage(
186 mCaller.obtainMessageO(DO_UPDATE_CURSOR, newCursor));
The Android Open Source Project4df24232009-03-05 14:34:35 -0800187 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600188
Jeff Brownc28867a2013-03-26 15:42:39 -0700189 @Override
Yohei Yukawac2ddd602014-05-06 21:22:49 +0900190 public void updateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo) {
191 mCaller.executeOrSendMessage(
192 mCaller.obtainMessageO(DO_UPDATE_CURSOR_ANCHOR_INFO, cursorAnchorInfo));
193 }
194
195 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700196 public void appPrivateCommand(String action, Bundle data) {
197 mCaller.executeOrSendMessage(
198 mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data));
199 }
200
201 @Override
202 public void toggleSoftInput(int showFlags, int hideFlags) {
203 mCaller.executeOrSendMessage(
204 mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags));
205 }
206
207 @Override
Devin Taylor0c33ed22010-02-23 13:26:46 -0600208 public void finishSession() {
209 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION));
210 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700211
212 private final class ImeInputEventReceiver extends InputEventReceiver
213 implements InputMethodSession.EventCallback {
214 private final SparseArray<InputEvent> mPendingEvents = new SparseArray<InputEvent>();
215
216 public ImeInputEventReceiver(InputChannel inputChannel, Looper looper) {
217 super(inputChannel, looper);
218 }
219
220 @Override
221 public void onInputEvent(InputEvent event) {
222 if (mInputMethodSession == null) {
223 // The session has been finished.
224 finishInputEvent(event, false);
225 return;
226 }
227
228 final int seq = event.getSequenceNumber();
229 mPendingEvents.put(seq, event);
230 if (event instanceof KeyEvent) {
231 KeyEvent keyEvent = (KeyEvent)event;
232 mInputMethodSession.dispatchKeyEvent(seq, keyEvent, this);
233 } else {
234 MotionEvent motionEvent = (MotionEvent)event;
235 if (motionEvent.isFromSource(InputDevice.SOURCE_CLASS_TRACKBALL)) {
236 mInputMethodSession.dispatchTrackballEvent(seq, motionEvent, this);
237 } else {
238 mInputMethodSession.dispatchGenericMotionEvent(seq, motionEvent, this);
239 }
240 }
241 }
242
243 @Override
244 public void finishedEvent(int seq, boolean handled) {
245 int index = mPendingEvents.indexOfKey(seq);
246 if (index >= 0) {
247 InputEvent event = mPendingEvents.valueAt(index);
248 mPendingEvents.removeAt(index);
249 finishInputEvent(event, handled);
250 }
251 }
252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253}