Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.inputmethodservice; |
| 18 | |
| 19 | import com.android.internal.os.HandlerCaller; |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 20 | import com.android.internal.os.SomeArgs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.internal.view.IInputMethodSession; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.graphics.Rect; |
| 25 | import android.os.Bundle; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 26 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 29 | import android.util.SparseArray; |
| 30 | import android.view.InputChannel; |
| 31 | import android.view.InputDevice; |
| 32 | import android.view.InputEvent; |
| 33 | import android.view.InputEventReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.view.KeyEvent; |
| 35 | import android.view.MotionEvent; |
| 36 | import android.view.inputmethod.CompletionInfo; |
| 37 | import android.view.inputmethod.ExtractedText; |
| 38 | import android.view.inputmethod.InputMethodSession; |
Yohei Yukawa | c2ddd60 | 2014-05-06 21:22:49 +0900 | [diff] [blame] | 39 | import android.view.inputmethod.CursorAnchorInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | class IInputMethodSessionWrapper extends IInputMethodSession.Stub |
| 42 | implements HandlerCaller.Callback { |
| 43 | private static final String TAG = "InputMethodWrapper"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | private static final int DO_UPDATE_SELECTION = 90; |
| 49 | private static final int DO_UPDATE_CURSOR = 95; |
Yohei Yukawa | c2ddd60 | 2014-05-06 21:22:49 +0900 | [diff] [blame] | 50 | private static final int DO_UPDATE_CURSOR_ANCHOR_INFO = 99; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | private static final int DO_APP_PRIVATE_COMMAND = 100; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 52 | private static final int DO_TOGGLE_SOFT_INPUT = 105; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 53 | private static final int DO_FINISH_SESSION = 110; |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 54 | private static final int DO_VIEW_CLICKED = 115; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 55 | |
| 56 | HandlerCaller mCaller; |
| 57 | InputMethodSession mInputMethodSession; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 58 | InputChannel mChannel; |
| 59 | ImeInputEventReceiver mReceiver; |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | public IInputMethodSessionWrapper(Context context, |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 62 | InputMethodSession inputMethodSession, InputChannel channel) { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 63 | mCaller = new HandlerCaller(context, null, |
| 64 | this, true /*asyncHandler*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | mInputMethodSession = inputMethodSession; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 66 | mChannel = channel; |
| 67 | if (channel != null) { |
| 68 | mReceiver = new ImeInputEventReceiver(channel, context.getMainLooper()); |
| 69 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public InputMethodSession getInternalInputMethodSession() { |
| 73 | return mInputMethodSession; |
| 74 | } |
| 75 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 76 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | public void executeMessage(Message msg) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 78 | if (mInputMethodSession == null) { |
Henrik Baard | 857cdff | 2010-07-20 11:42:19 +0200 | [diff] [blame] | 79 | // 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 Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 88 | return; |
| 89 | } |
Ken Wakasa | a308c03 | 2011-01-25 14:02:07 +0900 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | case DO_UPDATE_SELECTION: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 103 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | mInputMethodSession.updateSelection(args.argi1, args.argi2, |
| 105 | args.argi3, args.argi4, args.argi5, args.argi6); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 106 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | case DO_UPDATE_CURSOR: { |
| 110 | mInputMethodSession.updateCursor((Rect)msg.obj); |
| 111 | return; |
| 112 | } |
Yohei Yukawa | c2ddd60 | 2014-05-06 21:22:49 +0900 | [diff] [blame] | 113 | case DO_UPDATE_CURSOR_ANCHOR_INFO: { |
| 114 | mInputMethodSession.updateCursorAnchorInfo((CursorAnchorInfo)msg.obj); |
| 115 | return; |
| 116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | case DO_APP_PRIVATE_COMMAND: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 118 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | mInputMethodSession.appPrivateCommand((String)args.arg1, |
| 120 | (Bundle)args.arg2); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 121 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | return; |
| 123 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 124 | case DO_TOGGLE_SOFT_INPUT: { |
| 125 | mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2); |
| 126 | return; |
| 127 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 128 | case DO_FINISH_SESSION: { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 129 | doFinishSession(); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 130 | return; |
| 131 | } |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 132 | case DO_VIEW_CLICKED: { |
| 133 | mInputMethodSession.viewClicked(msg.arg1 == 1); |
| 134 | return; |
| 135 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
| 137 | Log.w(TAG, "Unhandled message code: " + msg.what); |
| 138 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 139 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | public void finishInput() { |
| 154 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT)); |
| 155 | } |
| 156 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 157 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | public void displayCompletions(CompletionInfo[] completions) { |
| 159 | mCaller.executeOrSendMessage(mCaller.obtainMessageO( |
| 160 | DO_DISPLAY_COMPLETIONS, completions)); |
| 161 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 162 | |
| 163 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | public void updateExtractedText(int token, ExtractedText text) { |
| 165 | mCaller.executeOrSendMessage(mCaller.obtainMessageIO( |
| 166 | DO_UPDATE_EXTRACTED_TEXT, token, text)); |
| 167 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 169 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | 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 | } |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 176 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 177 | @Override |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 178 | public void viewClicked(boolean focusChanged) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 179 | mCaller.executeOrSendMessage( |
| 180 | mCaller.obtainMessageI(DO_VIEW_CLICKED, focusChanged ? 1 : 0)); |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 181 | } |
| 182 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 183 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | public void updateCursor(Rect newCursor) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 185 | mCaller.executeOrSendMessage( |
| 186 | mCaller.obtainMessageO(DO_UPDATE_CURSOR, newCursor)); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 187 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 188 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 189 | @Override |
Yohei Yukawa | c2ddd60 | 2014-05-06 21:22:49 +0900 | [diff] [blame] | 190 | public void updateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo) { |
| 191 | mCaller.executeOrSendMessage( |
| 192 | mCaller.obtainMessageO(DO_UPDATE_CURSOR_ANCHOR_INFO, cursorAnchorInfo)); |
| 193 | } |
| 194 | |
| 195 | @Override |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 196 | 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 Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 208 | public void finishSession() { |
| 209 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION)); |
| 210 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 211 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |