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 
17 package android.inputmethodservice;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.graphics.Rect;
22 import android.os.Bundle;
23 import android.os.Looper;
24 import android.os.Message;
25 import android.util.Log;
26 import android.util.SparseArray;
27 import android.view.InputChannel;
28 import android.view.InputDevice;
29 import android.view.InputEvent;
30 import android.view.InputEventReceiver;
31 import android.view.KeyEvent;
32 import android.view.MotionEvent;
33 import android.view.inputmethod.CompletionInfo;
34 import android.view.inputmethod.CursorAnchorInfo;
35 import android.view.inputmethod.ExtractedText;
36 import android.view.inputmethod.InputMethodSession;
37 
38 import com.android.internal.os.HandlerCaller;
39 import com.android.internal.os.SomeArgs;
40 import com.android.internal.view.IInputMethodSession;
41 
42 class IInputMethodSessionWrapper extends IInputMethodSession.Stub
43         implements HandlerCaller.Callback {
44     private static final String TAG = "InputMethodWrapper";
45 
46     private static final int DO_DISPLAY_COMPLETIONS = 65;
47     private static final int DO_UPDATE_EXTRACTED_TEXT = 67;
48     private static final int DO_UPDATE_SELECTION = 90;
49     private static final int DO_UPDATE_CURSOR = 95;
50     private static final int DO_UPDATE_CURSOR_ANCHOR_INFO = 99;
51     private static final int DO_APP_PRIVATE_COMMAND = 100;
52     private static final int DO_TOGGLE_SOFT_INPUT = 105;
53     private static final int DO_FINISH_SESSION = 110;
54     private static final int DO_VIEW_CLICKED = 115;
55     private static final int DO_NOTIFY_IME_HIDDEN = 120;
56     private static final int DO_REMOVE_IME_SURFACE = 130;
57 
58     @UnsupportedAppUsage
59     HandlerCaller mCaller;
60     InputMethodSession mInputMethodSession;
61     InputChannel mChannel;
62     ImeInputEventReceiver mReceiver;
63 
IInputMethodSessionWrapper(Context context, InputMethodSession inputMethodSession, InputChannel channel)64     public IInputMethodSessionWrapper(Context context,
65             InputMethodSession inputMethodSession, InputChannel channel) {
66         mCaller = new HandlerCaller(context, null,
67                 this, true /*asyncHandler*/);
68         mInputMethodSession = inputMethodSession;
69         mChannel = channel;
70         if (channel != null) {
71             mReceiver = new ImeInputEventReceiver(channel, context.getMainLooper());
72         }
73     }
74 
getInternalInputMethodSession()75     public InputMethodSession getInternalInputMethodSession() {
76         return mInputMethodSession;
77     }
78 
79     @Override
executeMessage(Message msg)80     public void executeMessage(Message msg) {
81         if (mInputMethodSession == null) {
82             // The session has been finished. Args needs to be recycled
83             // for cases below.
84             switch (msg.what) {
85                 case DO_UPDATE_SELECTION:
86                 case DO_APP_PRIVATE_COMMAND: {
87                     SomeArgs args = (SomeArgs)msg.obj;
88                     args.recycle();
89                 }
90             }
91             return;
92         }
93 
94         switch (msg.what) {
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;
102             case DO_UPDATE_SELECTION: {
103                 SomeArgs args = (SomeArgs)msg.obj;
104                 mInputMethodSession.updateSelection(args.argi1, args.argi2,
105                         args.argi3, args.argi4, args.argi5, args.argi6);
106                 args.recycle();
107                 return;
108             }
109             case DO_UPDATE_CURSOR: {
110                 mInputMethodSession.updateCursor((Rect)msg.obj);
111                 return;
112             }
113             case DO_UPDATE_CURSOR_ANCHOR_INFO: {
114                 mInputMethodSession.updateCursorAnchorInfo((CursorAnchorInfo)msg.obj);
115                 return;
116             }
117             case DO_APP_PRIVATE_COMMAND: {
118                 SomeArgs args = (SomeArgs)msg.obj;
119                 mInputMethodSession.appPrivateCommand((String)args.arg1,
120                         (Bundle)args.arg2);
121                 args.recycle();
122                 return;
123             }
124             case DO_TOGGLE_SOFT_INPUT: {
125                 mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2);
126                 return;
127             }
128             case DO_FINISH_SESSION: {
129                 doFinishSession();
130                 return;
131             }
132             case DO_VIEW_CLICKED: {
133                 mInputMethodSession.viewClicked(msg.arg1 == 1);
134                 return;
135             }
136             case DO_NOTIFY_IME_HIDDEN: {
137                 mInputMethodSession.notifyImeHidden();
138                 return;
139             }
140             case DO_REMOVE_IME_SURFACE: {
141                 mInputMethodSession.removeImeSurface();
142                 return;
143             }
144         }
145         Log.w(TAG, "Unhandled message code: " + msg.what);
146     }
147 
doFinishSession()148     private void doFinishSession() {
149         mInputMethodSession = null;
150         if (mReceiver != null) {
151             mReceiver.dispose();
152             mReceiver = null;
153         }
154         if (mChannel != null) {
155             mChannel.dispose();
156             mChannel = null;
157         }
158     }
159 
160     @Override
displayCompletions(CompletionInfo[] completions)161     public void displayCompletions(CompletionInfo[] completions) {
162         mCaller.executeOrSendMessage(mCaller.obtainMessageO(
163                 DO_DISPLAY_COMPLETIONS, completions));
164     }
165 
166     @Override
updateExtractedText(int token, ExtractedText text)167     public void updateExtractedText(int token, ExtractedText text) {
168         mCaller.executeOrSendMessage(mCaller.obtainMessageIO(
169                 DO_UPDATE_EXTRACTED_TEXT, token, text));
170     }
171 
172     @Override
updateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)173     public void updateSelection(int oldSelStart, int oldSelEnd,
174             int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) {
175         mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION,
176                 oldSelStart, oldSelEnd, newSelStart, newSelEnd,
177                 candidatesStart, candidatesEnd));
178     }
179 
180     @Override
viewClicked(boolean focusChanged)181     public void viewClicked(boolean focusChanged) {
182         mCaller.executeOrSendMessage(
183                 mCaller.obtainMessageI(DO_VIEW_CLICKED, focusChanged ? 1 : 0));
184     }
185 
186     @Override
notifyImeHidden()187     public void notifyImeHidden() {
188         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_NOTIFY_IME_HIDDEN));
189     }
190 
191     @Override
removeImeSurface()192     public void removeImeSurface() {
193         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_REMOVE_IME_SURFACE));
194     }
195 
196     @Override
updateCursor(Rect newCursor)197     public void updateCursor(Rect newCursor) {
198         mCaller.executeOrSendMessage(
199                 mCaller.obtainMessageO(DO_UPDATE_CURSOR, newCursor));
200     }
201 
202     @Override
updateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo)203     public void updateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo) {
204         mCaller.executeOrSendMessage(
205                 mCaller.obtainMessageO(DO_UPDATE_CURSOR_ANCHOR_INFO, cursorAnchorInfo));
206     }
207 
208     @Override
appPrivateCommand(String action, Bundle data)209     public void appPrivateCommand(String action, Bundle data) {
210         mCaller.executeOrSendMessage(
211                 mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data));
212     }
213 
214     @Override
toggleSoftInput(int showFlags, int hideFlags)215     public void toggleSoftInput(int showFlags, int hideFlags) {
216         mCaller.executeOrSendMessage(
217                 mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags));
218     }
219 
220     @Override
finishSession()221     public void finishSession() {
222         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION));
223     }
224 
225     private final class ImeInputEventReceiver extends InputEventReceiver
226             implements InputMethodSession.EventCallback {
227         private final SparseArray<InputEvent> mPendingEvents = new SparseArray<InputEvent>();
228 
ImeInputEventReceiver(InputChannel inputChannel, Looper looper)229         public ImeInputEventReceiver(InputChannel inputChannel, Looper looper) {
230             super(inputChannel, looper);
231         }
232 
233         @Override
onInputEvent(InputEvent event)234         public void onInputEvent(InputEvent event) {
235             if (mInputMethodSession == null) {
236                 // The session has been finished.
237                 finishInputEvent(event, false);
238                 return;
239             }
240 
241             final int seq = event.getSequenceNumber();
242             mPendingEvents.put(seq, event);
243             if (event instanceof KeyEvent) {
244                 KeyEvent keyEvent = (KeyEvent)event;
245                 mInputMethodSession.dispatchKeyEvent(seq, keyEvent, this);
246             } else {
247                 MotionEvent motionEvent = (MotionEvent)event;
248                 if (motionEvent.isFromSource(InputDevice.SOURCE_CLASS_TRACKBALL)) {
249                     mInputMethodSession.dispatchTrackballEvent(seq, motionEvent, this);
250                 } else {
251                     mInputMethodSession.dispatchGenericMotionEvent(seq, motionEvent, this);
252                 }
253             }
254         }
255 
256         @Override
finishedEvent(int seq, boolean handled)257         public void finishedEvent(int seq, boolean handled) {
258             int index = mPendingEvents.indexOfKey(seq);
259             if (index >= 0) {
260                 InputEvent event = mPendingEvents.valueAt(index);
261                 mPendingEvents.removeAt(index);
262                 finishInputEvent(event, handled);
263             }
264         }
265     }
266 }
267