IInputMethodSessionWrapper.java revision dea3ef7967228f0ddcc03f2455a4f1254758e584
1/*
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
17package android.inputmethodservice;
18
19import com.android.internal.os.HandlerCaller;
20import com.android.internal.view.IInputMethodCallback;
21import com.android.internal.view.IInputMethodSession;
22
23import android.content.Context;
24import android.graphics.Rect;
25import android.os.Bundle;
26import android.os.Message;
27import android.os.RemoteException;
28import android.util.Log;
29import android.view.KeyEvent;
30import android.view.MotionEvent;
31import android.view.inputmethod.CompletionInfo;
32import android.view.inputmethod.ExtractedText;
33import android.view.inputmethod.InputMethodSession;
34
35class IInputMethodSessionWrapper extends IInputMethodSession.Stub
36        implements HandlerCaller.Callback {
37    private static final String TAG = "InputMethodWrapper";
38    private static final boolean DEBUG = false;
39
40    private static final int DO_FINISH_INPUT = 60;
41    private static final int DO_DISPLAY_COMPLETIONS = 65;
42    private static final int DO_UPDATE_EXTRACTED_TEXT = 67;
43    private static final int DO_DISPATCH_KEY_EVENT = 70;
44    private static final int DO_DISPATCH_TRACKBALL_EVENT = 80;
45    private static final int DO_UPDATE_SELECTION = 90;
46    private static final int DO_UPDATE_CURSOR = 95;
47    private static final int DO_APP_PRIVATE_COMMAND = 100;
48    private static final int DO_TOGGLE_SOFT_INPUT = 105;
49    private static final int DO_FINISH_SESSION = 110;
50
51    HandlerCaller mCaller;
52    InputMethodSession mInputMethodSession;
53
54    // NOTE: we should have a cache of these.
55    static class InputMethodEventCallbackWrapper implements InputMethodSession.EventCallback {
56        final IInputMethodCallback mCb;
57        InputMethodEventCallbackWrapper(IInputMethodCallback cb) {
58            mCb = cb;
59        }
60        public void finishedEvent(int seq, boolean handled) {
61            try {
62                mCb.finishedEvent(seq, handled);
63            } catch (RemoteException e) {
64            }
65        }
66    }
67
68    public IInputMethodSessionWrapper(Context context,
69            InputMethodSession inputMethodSession) {
70        mCaller = new HandlerCaller(context, this);
71        mInputMethodSession = inputMethodSession;
72    }
73
74    public InputMethodSession getInternalInputMethodSession() {
75        return mInputMethodSession;
76    }
77
78    public void executeMessage(Message msg) {
79        switch (msg.what) {
80            case DO_FINISH_INPUT:
81                mInputMethodSession.finishInput();
82                return;
83            case DO_DISPLAY_COMPLETIONS:
84                mInputMethodSession.displayCompletions((CompletionInfo[])msg.obj);
85                return;
86            case DO_UPDATE_EXTRACTED_TEXT:
87                mInputMethodSession.updateExtractedText(msg.arg1,
88                        (ExtractedText)msg.obj);
89                return;
90            case DO_DISPATCH_KEY_EVENT: {
91                HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
92                mInputMethodSession.dispatchKeyEvent(msg.arg1,
93                        (KeyEvent)args.arg1,
94                        new InputMethodEventCallbackWrapper(
95                                (IInputMethodCallback)args.arg2));
96                mCaller.recycleArgs(args);
97                return;
98            }
99            case DO_DISPATCH_TRACKBALL_EVENT: {
100                HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
101                mInputMethodSession.dispatchTrackballEvent(msg.arg1,
102                        (MotionEvent)args.arg1,
103                        new InputMethodEventCallbackWrapper(
104                                (IInputMethodCallback)args.arg2));
105                mCaller.recycleArgs(args);
106                return;
107            }
108            case DO_UPDATE_SELECTION: {
109                HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
110                mInputMethodSession.updateSelection(args.argi1, args.argi2,
111                        args.argi3, args.argi4, args.argi5, args.argi6);
112                mCaller.recycleArgs(args);
113                return;
114            }
115            case DO_UPDATE_CURSOR: {
116                mInputMethodSession.updateCursor((Rect)msg.obj);
117                return;
118            }
119            case DO_APP_PRIVATE_COMMAND: {
120                HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
121                mInputMethodSession.appPrivateCommand((String)args.arg1,
122                        (Bundle)args.arg2);
123                mCaller.recycleArgs(args);
124                return;
125            }
126            case DO_TOGGLE_SOFT_INPUT: {
127                mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2);
128                return;
129            }
130            case DO_FINISH_SESSION: {
131                mInputMethodSession = null;
132                return;
133            }
134        }
135        Log.w(TAG, "Unhandled message code: " + msg.what);
136    }
137
138    public void finishInput() {
139        mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT));
140    }
141
142    public void displayCompletions(CompletionInfo[] completions) {
143        mCaller.executeOrSendMessage(mCaller.obtainMessageO(
144                DO_DISPLAY_COMPLETIONS, completions));
145    }
146
147    public void updateExtractedText(int token, ExtractedText text) {
148        mCaller.executeOrSendMessage(mCaller.obtainMessageIO(
149                DO_UPDATE_EXTRACTED_TEXT, token, text));
150    }
151
152    public void dispatchKeyEvent(int seq, KeyEvent event, IInputMethodCallback callback) {
153        mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_KEY_EVENT, seq,
154                event, callback));
155    }
156
157    public void dispatchTrackballEvent(int seq, MotionEvent event, IInputMethodCallback callback) {
158        mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_TRACKBALL_EVENT, seq,
159                event, callback));
160    }
161
162    public void updateSelection(int oldSelStart, int oldSelEnd,
163            int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) {
164        mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION,
165                oldSelStart, oldSelEnd, newSelStart, newSelEnd,
166                candidatesStart, candidatesEnd));
167    }
168
169    public void updateCursor(Rect newCursor) {
170        mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_UPDATE_CURSOR,
171                newCursor));
172    }
173
174    public void appPrivateCommand(String action, Bundle data) {
175        mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data));
176    }
177
178    public void toggleSoftInput(int showFlags, int hideFlags) {
179        mCaller.executeOrSendMessage(mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags));
180    }
181
182    public void finishSession() {
183        mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION));
184    }
185}
186