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; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 30 | import android.util.SparseArray; |
| 31 | import android.view.InputChannel; |
| 32 | import android.view.InputDevice; |
| 33 | import android.view.InputEvent; |
| 34 | import android.view.InputEventReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.view.KeyEvent; |
| 36 | import android.view.MotionEvent; |
| 37 | import android.view.inputmethod.CompletionInfo; |
| 38 | import android.view.inputmethod.ExtractedText; |
| 39 | import android.view.inputmethod.InputMethodSession; |
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; |
| 50 | private static final int DO_APP_PRIVATE_COMMAND = 100; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 51 | private static final int DO_TOGGLE_SOFT_INPUT = 105; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 52 | private static final int DO_FINISH_SESSION = 110; |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 53 | private static final int DO_VIEW_CLICKED = 115; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 54 | |
| 55 | HandlerCaller mCaller; |
| 56 | InputMethodSession mInputMethodSession; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 57 | InputChannel mChannel; |
| 58 | ImeInputEventReceiver mReceiver; |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | public IInputMethodSessionWrapper(Context context, |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 61 | InputMethodSession inputMethodSession, InputChannel channel) { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 62 | mCaller = new HandlerCaller(context, null, |
| 63 | this, true /*asyncHandler*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | mInputMethodSession = inputMethodSession; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 65 | mChannel = channel; |
| 66 | if (channel != null) { |
| 67 | mReceiver = new ImeInputEventReceiver(channel, context.getMainLooper()); |
| 68 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public InputMethodSession getInternalInputMethodSession() { |
| 72 | return mInputMethodSession; |
| 73 | } |
| 74 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 75 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | public void executeMessage(Message msg) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 77 | if (mInputMethodSession == null) { |
Henrik Baard | 857cdff | 2010-07-20 11:42:19 +0200 | [diff] [blame] | 78 | // The session has been finished. Args needs to be recycled |
| 79 | // for cases below. |
| 80 | switch (msg.what) { |
| 81 | case DO_UPDATE_SELECTION: |
| 82 | case DO_APP_PRIVATE_COMMAND: { |
| 83 | SomeArgs args = (SomeArgs)msg.obj; |
| 84 | args.recycle(); |
| 85 | } |
| 86 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
Ken Wakasa | a308c03 | 2011-01-25 14:02:07 +0900 | [diff] [blame] | 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | switch (msg.what) { |
| 91 | case DO_FINISH_INPUT: |
| 92 | mInputMethodSession.finishInput(); |
| 93 | return; |
| 94 | case DO_DISPLAY_COMPLETIONS: |
| 95 | mInputMethodSession.displayCompletions((CompletionInfo[])msg.obj); |
| 96 | return; |
| 97 | case DO_UPDATE_EXTRACTED_TEXT: |
| 98 | mInputMethodSession.updateExtractedText(msg.arg1, |
| 99 | (ExtractedText)msg.obj); |
| 100 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | case DO_UPDATE_SELECTION: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 102 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | mInputMethodSession.updateSelection(args.argi1, args.argi2, |
| 104 | args.argi3, args.argi4, args.argi5, args.argi6); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 105 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | case DO_UPDATE_CURSOR: { |
| 109 | mInputMethodSession.updateCursor((Rect)msg.obj); |
| 110 | return; |
| 111 | } |
| 112 | case DO_APP_PRIVATE_COMMAND: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 113 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | mInputMethodSession.appPrivateCommand((String)args.arg1, |
| 115 | (Bundle)args.arg2); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 116 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | return; |
| 118 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 119 | case DO_TOGGLE_SOFT_INPUT: { |
| 120 | mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2); |
| 121 | return; |
| 122 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 123 | case DO_FINISH_SESSION: { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 124 | doFinishSession(); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 125 | return; |
| 126 | } |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 127 | case DO_VIEW_CLICKED: { |
| 128 | mInputMethodSession.viewClicked(msg.arg1 == 1); |
| 129 | return; |
| 130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
| 132 | Log.w(TAG, "Unhandled message code: " + msg.what); |
| 133 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 134 | |
| 135 | private void doFinishSession() { |
| 136 | mInputMethodSession = null; |
| 137 | if (mReceiver != null) { |
| 138 | mReceiver.dispose(); |
| 139 | mReceiver = null; |
| 140 | } |
| 141 | if (mChannel != null) { |
| 142 | mChannel.dispose(); |
| 143 | mChannel = null; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | public void finishInput() { |
| 149 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT)); |
| 150 | } |
| 151 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 152 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | public void displayCompletions(CompletionInfo[] completions) { |
| 154 | mCaller.executeOrSendMessage(mCaller.obtainMessageO( |
| 155 | DO_DISPLAY_COMPLETIONS, completions)); |
| 156 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 157 | |
| 158 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | public void updateExtractedText(int token, ExtractedText text) { |
| 160 | mCaller.executeOrSendMessage(mCaller.obtainMessageIO( |
| 161 | DO_UPDATE_EXTRACTED_TEXT, token, text)); |
| 162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 164 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | public void updateSelection(int oldSelStart, int oldSelEnd, |
| 166 | int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) { |
| 167 | mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION, |
| 168 | oldSelStart, oldSelEnd, newSelStart, newSelEnd, |
| 169 | candidatesStart, candidatesEnd)); |
| 170 | } |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 171 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 172 | @Override |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 173 | public void viewClicked(boolean focusChanged) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 174 | mCaller.executeOrSendMessage( |
| 175 | mCaller.obtainMessageI(DO_VIEW_CLICKED, focusChanged ? 1 : 0)); |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 178 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | public void updateCursor(Rect newCursor) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 180 | mCaller.executeOrSendMessage( |
| 181 | mCaller.obtainMessageO(DO_UPDATE_CURSOR, newCursor)); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 182 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 183 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 184 | @Override |
| 185 | public void appPrivateCommand(String action, Bundle data) { |
| 186 | mCaller.executeOrSendMessage( |
| 187 | mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data)); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void toggleSoftInput(int showFlags, int hideFlags) { |
| 192 | mCaller.executeOrSendMessage( |
| 193 | mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags)); |
| 194 | } |
| 195 | |
| 196 | @Override |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 197 | public void finishSession() { |
| 198 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION)); |
| 199 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 200 | |
| 201 | private final class ImeInputEventReceiver extends InputEventReceiver |
| 202 | implements InputMethodSession.EventCallback { |
| 203 | private final SparseArray<InputEvent> mPendingEvents = new SparseArray<InputEvent>(); |
| 204 | |
| 205 | public ImeInputEventReceiver(InputChannel inputChannel, Looper looper) { |
| 206 | super(inputChannel, looper); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onInputEvent(InputEvent event) { |
| 211 | if (mInputMethodSession == null) { |
| 212 | // The session has been finished. |
| 213 | finishInputEvent(event, false); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | final int seq = event.getSequenceNumber(); |
| 218 | mPendingEvents.put(seq, event); |
| 219 | if (event instanceof KeyEvent) { |
| 220 | KeyEvent keyEvent = (KeyEvent)event; |
| 221 | mInputMethodSession.dispatchKeyEvent(seq, keyEvent, this); |
| 222 | } else { |
| 223 | MotionEvent motionEvent = (MotionEvent)event; |
| 224 | if (motionEvent.isFromSource(InputDevice.SOURCE_CLASS_TRACKBALL)) { |
| 225 | mInputMethodSession.dispatchTrackballEvent(seq, motionEvent, this); |
| 226 | } else { |
| 227 | mInputMethodSession.dispatchGenericMotionEvent(seq, motionEvent, this); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void finishedEvent(int seq, boolean handled) { |
| 234 | int index = mPendingEvents.indexOfKey(seq); |
| 235 | if (index >= 0) { |
| 236 | InputEvent event = mPendingEvents.valueAt(index); |
| 237 | mPendingEvents.removeAt(index); |
| 238 | finishInputEvent(event, handled); |
| 239 | } |
| 240 | } |
| 241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |