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