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