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