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.IInputMethodCallback; |
| 22 | import com.android.internal.view.IInputMethodSession; |
| 23 | |
| 24 | import android.content.Context; |
| 25 | import android.graphics.Rect; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.Message; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
| 30 | import android.view.KeyEvent; |
| 31 | import android.view.MotionEvent; |
| 32 | import android.view.inputmethod.CompletionInfo; |
| 33 | import android.view.inputmethod.ExtractedText; |
| 34 | import android.view.inputmethod.InputMethodSession; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | class IInputMethodSessionWrapper extends IInputMethodSession.Stub |
| 37 | implements HandlerCaller.Callback { |
| 38 | private static final String TAG = "InputMethodWrapper"; |
| 39 | private static final boolean DEBUG = false; |
| 40 | |
| 41 | private static final int DO_FINISH_INPUT = 60; |
| 42 | private static final int DO_DISPLAY_COMPLETIONS = 65; |
| 43 | private static final int DO_UPDATE_EXTRACTED_TEXT = 67; |
| 44 | private static final int DO_DISPATCH_KEY_EVENT = 70; |
| 45 | private static final int DO_DISPATCH_TRACKBALL_EVENT = 80; |
Victoria Lease | b38070c | 2012-08-24 13:46:02 -0700 | [diff] [blame] | 46 | private static final int DO_DISPATCH_GENERIC_MOTION_EVENT = 85; |
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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | // NOTE: we should have a cache of these. |
| 58 | static class InputMethodEventCallbackWrapper implements InputMethodSession.EventCallback { |
| 59 | final IInputMethodCallback mCb; |
| 60 | InputMethodEventCallbackWrapper(IInputMethodCallback cb) { |
| 61 | mCb = cb; |
| 62 | } |
| 63 | public void finishedEvent(int seq, boolean handled) { |
| 64 | try { |
| 65 | mCb.finishedEvent(seq, handled); |
| 66 | } catch (RemoteException e) { |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public IInputMethodSessionWrapper(Context context, |
| 72 | InputMethodSession inputMethodSession) { |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame^] | 73 | mCaller = new HandlerCaller(context, null, |
| 74 | this, true /*asyncHandler*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | mInputMethodSession = inputMethodSession; |
| 76 | } |
| 77 | |
| 78 | public InputMethodSession getInternalInputMethodSession() { |
| 79 | return mInputMethodSession; |
| 80 | } |
| 81 | |
| 82 | public void executeMessage(Message msg) { |
Ken Wakasa | a308c03 | 2011-01-25 14:02:07 +0900 | [diff] [blame] | 83 | if (mInputMethodSession == null) return; |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | switch (msg.what) { |
| 86 | case DO_FINISH_INPUT: |
| 87 | mInputMethodSession.finishInput(); |
| 88 | return; |
| 89 | case DO_DISPLAY_COMPLETIONS: |
| 90 | mInputMethodSession.displayCompletions((CompletionInfo[])msg.obj); |
| 91 | return; |
| 92 | case DO_UPDATE_EXTRACTED_TEXT: |
| 93 | mInputMethodSession.updateExtractedText(msg.arg1, |
| 94 | (ExtractedText)msg.obj); |
| 95 | return; |
| 96 | case DO_DISPATCH_KEY_EVENT: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 97 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | mInputMethodSession.dispatchKeyEvent(msg.arg1, |
| 99 | (KeyEvent)args.arg1, |
| 100 | new InputMethodEventCallbackWrapper( |
| 101 | (IInputMethodCallback)args.arg2)); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 102 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | case DO_DISPATCH_TRACKBALL_EVENT: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 106 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | mInputMethodSession.dispatchTrackballEvent(msg.arg1, |
| 108 | (MotionEvent)args.arg1, |
| 109 | new InputMethodEventCallbackWrapper( |
| 110 | (IInputMethodCallback)args.arg2)); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 111 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
Victoria Lease | b38070c | 2012-08-24 13:46:02 -0700 | [diff] [blame] | 114 | case DO_DISPATCH_GENERIC_MOTION_EVENT: { |
| 115 | SomeArgs args = (SomeArgs)msg.obj; |
| 116 | mInputMethodSession.dispatchGenericMotionEvent(msg.arg1, |
| 117 | (MotionEvent)args.arg1, |
| 118 | new InputMethodEventCallbackWrapper( |
| 119 | (IInputMethodCallback)args.arg2)); |
| 120 | args.recycle(); |
| 121 | return; |
| 122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | case DO_UPDATE_SELECTION: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 124 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | mInputMethodSession.updateSelection(args.argi1, args.argi2, |
| 126 | args.argi3, args.argi4, args.argi5, args.argi6); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 127 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | case DO_UPDATE_CURSOR: { |
| 131 | mInputMethodSession.updateCursor((Rect)msg.obj); |
| 132 | return; |
| 133 | } |
| 134 | case DO_APP_PRIVATE_COMMAND: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 135 | SomeArgs args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | mInputMethodSession.appPrivateCommand((String)args.arg1, |
| 137 | (Bundle)args.arg2); |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 138 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | return; |
| 140 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 141 | case DO_TOGGLE_SOFT_INPUT: { |
| 142 | mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2); |
| 143 | return; |
| 144 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 145 | case DO_FINISH_SESSION: { |
| 146 | mInputMethodSession = null; |
| 147 | return; |
| 148 | } |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 149 | case DO_VIEW_CLICKED: { |
| 150 | mInputMethodSession.viewClicked(msg.arg1 == 1); |
| 151 | return; |
| 152 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | Log.w(TAG, "Unhandled message code: " + msg.what); |
| 155 | } |
| 156 | |
| 157 | public void finishInput() { |
| 158 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT)); |
| 159 | } |
| 160 | |
| 161 | public void displayCompletions(CompletionInfo[] completions) { |
| 162 | mCaller.executeOrSendMessage(mCaller.obtainMessageO( |
| 163 | DO_DISPLAY_COMPLETIONS, completions)); |
| 164 | } |
| 165 | |
| 166 | public void updateExtractedText(int token, ExtractedText text) { |
| 167 | mCaller.executeOrSendMessage(mCaller.obtainMessageIO( |
| 168 | DO_UPDATE_EXTRACTED_TEXT, token, text)); |
| 169 | } |
| 170 | |
| 171 | public void dispatchKeyEvent(int seq, KeyEvent event, IInputMethodCallback callback) { |
| 172 | mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_KEY_EVENT, seq, |
| 173 | event, callback)); |
| 174 | } |
| 175 | |
| 176 | public void dispatchTrackballEvent(int seq, MotionEvent event, IInputMethodCallback callback) { |
| 177 | mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_TRACKBALL_EVENT, seq, |
| 178 | event, callback)); |
| 179 | } |
| 180 | |
Victoria Lease | b38070c | 2012-08-24 13:46:02 -0700 | [diff] [blame] | 181 | public void dispatchGenericMotionEvent(int seq, MotionEvent event, |
| 182 | IInputMethodCallback callback) { |
| 183 | mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_GENERIC_MOTION_EVENT, seq, |
| 184 | event, callback)); |
| 185 | } |
| 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | public void updateSelection(int oldSelStart, int oldSelEnd, |
| 188 | int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) { |
| 189 | mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION, |
| 190 | oldSelStart, oldSelEnd, newSelStart, newSelEnd, |
| 191 | candidatesStart, candidatesEnd)); |
| 192 | } |
satok | 863fcd6 | 2011-06-21 17:38:02 +0900 | [diff] [blame] | 193 | |
| 194 | public void viewClicked(boolean focusChanged) { |
| 195 | mCaller.executeOrSendMessage(mCaller.obtainMessageI(DO_VIEW_CLICKED, focusChanged ? 1 : 0)); |
| 196 | } |
| 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | public void updateCursor(Rect newCursor) { |
| 199 | mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_UPDATE_CURSOR, |
| 200 | newCursor)); |
| 201 | } |
| 202 | |
| 203 | public void appPrivateCommand(String action, Bundle data) { |
| 204 | mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data)); |
| 205 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 206 | |
| 207 | public void toggleSoftInput(int showFlags, int hideFlags) { |
| 208 | mCaller.executeOrSendMessage(mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags)); |
| 209 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 210 | |
| 211 | public void finishSession() { |
| 212 | mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION)); |
| 213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |