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 com.android.internal.view; |
| 18 | |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 19 | import com.android.internal.annotations.GuardedBy; |
| 20 | |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Message; |
| 27 | import android.os.RemoteException; |
| 28 | import android.util.Log; |
| 29 | import android.view.KeyEvent; |
| 30 | import android.view.inputmethod.CompletionInfo; |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 31 | import android.view.inputmethod.CorrectionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.view.inputmethod.ExtractedTextRequest; |
| 33 | import android.view.inputmethod.InputConnection; |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 34 | import android.view.inputmethod.InputConnectionInspector; |
| 35 | import android.view.inputmethod.InputConnectionInspector.MissingMethodFlags; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 36 | import android.view.inputmethod.InputContentInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 38 | public abstract class IInputConnectionWrapper extends IInputContext.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | static final String TAG = "IInputConnectionWrapper"; |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | private static final int DO_GET_TEXT_AFTER_CURSOR = 10; |
| 42 | private static final int DO_GET_TEXT_BEFORE_CURSOR = 20; |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 43 | private static final int DO_GET_SELECTED_TEXT = 25; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | private static final int DO_GET_CURSOR_CAPS_MODE = 30; |
| 45 | private static final int DO_GET_EXTRACTED_TEXT = 40; |
| 46 | private static final int DO_COMMIT_TEXT = 50; |
| 47 | private static final int DO_COMMIT_COMPLETION = 55; |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 48 | private static final int DO_COMMIT_CORRECTION = 56; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | private static final int DO_SET_SELECTION = 57; |
| 50 | private static final int DO_PERFORM_EDITOR_ACTION = 58; |
| 51 | private static final int DO_PERFORM_CONTEXT_MENU_ACTION = 59; |
| 52 | private static final int DO_SET_COMPOSING_TEXT = 60; |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 53 | private static final int DO_SET_COMPOSING_REGION = 63; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | private static final int DO_FINISH_COMPOSING_TEXT = 65; |
| 55 | private static final int DO_SEND_KEY_EVENT = 70; |
| 56 | private static final int DO_DELETE_SURROUNDING_TEXT = 80; |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 57 | private static final int DO_DELETE_SURROUNDING_TEXT_IN_CODE_POINTS = 81; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | private static final int DO_BEGIN_BATCH_EDIT = 90; |
| 59 | private static final int DO_END_BATCH_EDIT = 95; |
| 60 | private static final int DO_REPORT_FULLSCREEN_MODE = 100; |
| 61 | private static final int DO_PERFORM_PRIVATE_COMMAND = 120; |
| 62 | private static final int DO_CLEAR_META_KEY_STATES = 130; |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 63 | private static final int DO_REQUEST_UPDATE_CURSOR_ANCHOR_INFO = 140; |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 64 | private static final int DO_CLOSE_CONNECTION = 150; |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 65 | private static final int DO_COMMIT_CONTENT = 160; |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 66 | |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 67 | @GuardedBy("mLock") |
| 68 | @Nullable |
| 69 | private InputConnection mInputConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| 71 | private Looper mMainLooper; |
| 72 | private Handler mH; |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 73 | private Object mLock = new Object(); |
| 74 | @GuardedBy("mLock") |
| 75 | private boolean mFinished = false; |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 76 | @GuardedBy("mLock") |
| 77 | private String mInputMethodId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
| 79 | static class SomeArgs { |
| 80 | Object arg1; |
| 81 | Object arg2; |
| 82 | IInputContextCallback callback; |
| 83 | int seq; |
| 84 | } |
| 85 | |
| 86 | class MyHandler extends Handler { |
| 87 | MyHandler(Looper looper) { |
| 88 | super(looper); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void handleMessage(Message msg) { |
| 93 | executeMessage(msg); |
| 94 | } |
| 95 | } |
| 96 | |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 97 | public IInputConnectionWrapper(Looper mainLooper, @NonNull InputConnection inputConnection) { |
| 98 | mInputConnection = inputConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | mMainLooper = mainLooper; |
| 100 | mH = new MyHandler(mMainLooper); |
| 101 | } |
| 102 | |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 103 | @Nullable |
| 104 | public InputConnection getInputConnection() { |
| 105 | synchronized (mLock) { |
| 106 | return mInputConnection; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | protected boolean isFinished() { |
| 111 | synchronized (mLock) { |
| 112 | return mFinished; |
| 113 | } |
| 114 | } |
| 115 | |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 116 | public String getInputMethodId() { |
| 117 | synchronized (mLock) { |
| 118 | return mInputMethodId; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public void setInputMethodId(final String inputMethodId) { |
| 123 | synchronized (mLock) { |
| 124 | mInputMethodId = inputMethodId; |
| 125 | } |
| 126 | } |
| 127 | |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 128 | abstract protected boolean isActive(); |
| 129 | |
| 130 | /** |
| 131 | * Called when the user took some actions that should be taken into consideration to update the |
| 132 | * LRU list for input method rotation. |
| 133 | */ |
| 134 | abstract protected void onUserAction(); |
| 135 | |
| 136 | /** |
| 137 | * Called when the input method started or stopped full-screen mode. |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 138 | * @param enabled {@code true} if the input method starts full-screen mode. |
| 139 | * @param calledInBackground {@code true} if this input connection is in a state when incoming |
| 140 | * events are usually ignored. |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 141 | */ |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 142 | abstract protected void onReportFullscreenMode(boolean enabled, boolean calledInBackground); |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | public void getTextAfterCursor(int length, int flags, int seq, IInputContextCallback callback) { |
| 145 | dispatchMessage(obtainMessageIISC(DO_GET_TEXT_AFTER_CURSOR, length, flags, seq, callback)); |
| 146 | } |
| 147 | |
| 148 | public void getTextBeforeCursor(int length, int flags, int seq, IInputContextCallback callback) { |
| 149 | dispatchMessage(obtainMessageIISC(DO_GET_TEXT_BEFORE_CURSOR, length, flags, seq, callback)); |
| 150 | } |
| 151 | |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 152 | public void getSelectedText(int flags, int seq, IInputContextCallback callback) { |
| 153 | dispatchMessage(obtainMessageISC(DO_GET_SELECTED_TEXT, flags, seq, callback)); |
| 154 | } |
| 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | public void getCursorCapsMode(int reqModes, int seq, IInputContextCallback callback) { |
| 157 | dispatchMessage(obtainMessageISC(DO_GET_CURSOR_CAPS_MODE, reqModes, seq, callback)); |
| 158 | } |
| 159 | |
| 160 | public void getExtractedText(ExtractedTextRequest request, |
| 161 | int flags, int seq, IInputContextCallback callback) { |
| 162 | dispatchMessage(obtainMessageIOSC(DO_GET_EXTRACTED_TEXT, flags, |
| 163 | request, seq, callback)); |
| 164 | } |
| 165 | |
| 166 | public void commitText(CharSequence text, int newCursorPosition) { |
| 167 | dispatchMessage(obtainMessageIO(DO_COMMIT_TEXT, newCursorPosition, text)); |
| 168 | } |
| 169 | |
| 170 | public void commitCompletion(CompletionInfo text) { |
| 171 | dispatchMessage(obtainMessageO(DO_COMMIT_COMPLETION, text)); |
| 172 | } |
| 173 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 174 | public void commitCorrection(CorrectionInfo info) { |
| 175 | dispatchMessage(obtainMessageO(DO_COMMIT_CORRECTION, info)); |
| 176 | } |
| 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | public void setSelection(int start, int end) { |
| 179 | dispatchMessage(obtainMessageII(DO_SET_SELECTION, start, end)); |
| 180 | } |
| 181 | |
| 182 | public void performEditorAction(int id) { |
| 183 | dispatchMessage(obtainMessageII(DO_PERFORM_EDITOR_ACTION, id, 0)); |
| 184 | } |
| 185 | |
| 186 | public void performContextMenuAction(int id) { |
| 187 | dispatchMessage(obtainMessageII(DO_PERFORM_CONTEXT_MENU_ACTION, id, 0)); |
| 188 | } |
| 189 | |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 190 | public void setComposingRegion(int start, int end) { |
| 191 | dispatchMessage(obtainMessageII(DO_SET_COMPOSING_REGION, start, end)); |
| 192 | } |
| 193 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | public void setComposingText(CharSequence text, int newCursorPosition) { |
| 195 | dispatchMessage(obtainMessageIO(DO_SET_COMPOSING_TEXT, newCursorPosition, text)); |
| 196 | } |
| 197 | |
| 198 | public void finishComposingText() { |
| 199 | dispatchMessage(obtainMessage(DO_FINISH_COMPOSING_TEXT)); |
| 200 | } |
| 201 | |
| 202 | public void sendKeyEvent(KeyEvent event) { |
| 203 | dispatchMessage(obtainMessageO(DO_SEND_KEY_EVENT, event)); |
| 204 | } |
| 205 | |
| 206 | public void clearMetaKeyStates(int states) { |
| 207 | dispatchMessage(obtainMessageII(DO_CLEAR_META_KEY_STATES, states, 0)); |
| 208 | } |
| 209 | |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 210 | public void deleteSurroundingText(int beforeLength, int afterLength) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | dispatchMessage(obtainMessageII(DO_DELETE_SURROUNDING_TEXT, |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 212 | beforeLength, afterLength)); |
| 213 | } |
| 214 | |
| 215 | public void deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { |
| 216 | dispatchMessage(obtainMessageII(DO_DELETE_SURROUNDING_TEXT_IN_CODE_POINTS, |
| 217 | beforeLength, afterLength)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | public void beginBatchEdit() { |
| 221 | dispatchMessage(obtainMessage(DO_BEGIN_BATCH_EDIT)); |
| 222 | } |
| 223 | |
| 224 | public void endBatchEdit() { |
| 225 | dispatchMessage(obtainMessage(DO_END_BATCH_EDIT)); |
| 226 | } |
| 227 | |
| 228 | public void reportFullscreenMode(boolean enabled) { |
| 229 | dispatchMessage(obtainMessageII(DO_REPORT_FULLSCREEN_MODE, enabled ? 1 : 0, 0)); |
| 230 | } |
| 231 | |
| 232 | public void performPrivateCommand(String action, Bundle data) { |
| 233 | dispatchMessage(obtainMessageOO(DO_PERFORM_PRIVATE_COMMAND, action, data)); |
| 234 | } |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 235 | |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 236 | public void requestUpdateCursorAnchorInfo(int cursorUpdateMode, int seq, |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 237 | IInputContextCallback callback) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 238 | dispatchMessage(obtainMessageISC(DO_REQUEST_UPDATE_CURSOR_ANCHOR_INFO, cursorUpdateMode, |
| 239 | seq, callback)); |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 242 | public void closeConnection() { |
| 243 | dispatchMessage(obtainMessage(DO_CLOSE_CONNECTION)); |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 246 | public void commitContent(InputContentInfo inputContentInfo, Bundle opts, |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 247 | int seq, IInputContextCallback callback) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 248 | dispatchMessage(obtainMessageOOSC(DO_COMMIT_CONTENT, inputContentInfo, opts, seq, callback)); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 249 | } |
| 250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | void dispatchMessage(Message msg) { |
| 252 | // If we are calling this from the main thread, then we can call |
| 253 | // right through. Otherwise, we need to send the message to the |
| 254 | // main thread. |
| 255 | if (Looper.myLooper() == mMainLooper) { |
| 256 | executeMessage(msg); |
| 257 | msg.recycle(); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | mH.sendMessage(msg); |
| 262 | } |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | void executeMessage(Message msg) { |
| 265 | switch (msg.what) { |
| 266 | case DO_GET_TEXT_AFTER_CURSOR: { |
| 267 | SomeArgs args = (SomeArgs)msg.obj; |
| 268 | try { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 269 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | if (ic == null || !isActive()) { |
| 271 | Log.w(TAG, "getTextAfterCursor on inactive InputConnection"); |
| 272 | args.callback.setTextAfterCursor(null, args.seq); |
| 273 | return; |
| 274 | } |
| 275 | args.callback.setTextAfterCursor(ic.getTextAfterCursor( |
| 276 | msg.arg1, msg.arg2), args.seq); |
| 277 | } catch (RemoteException e) { |
| 278 | Log.w(TAG, "Got RemoteException calling setTextAfterCursor", e); |
| 279 | } |
| 280 | return; |
| 281 | } |
| 282 | case DO_GET_TEXT_BEFORE_CURSOR: { |
| 283 | SomeArgs args = (SomeArgs)msg.obj; |
| 284 | try { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 285 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | if (ic == null || !isActive()) { |
| 287 | Log.w(TAG, "getTextBeforeCursor on inactive InputConnection"); |
| 288 | args.callback.setTextBeforeCursor(null, args.seq); |
| 289 | return; |
| 290 | } |
| 291 | args.callback.setTextBeforeCursor(ic.getTextBeforeCursor( |
| 292 | msg.arg1, msg.arg2), args.seq); |
| 293 | } catch (RemoteException e) { |
| 294 | Log.w(TAG, "Got RemoteException calling setTextBeforeCursor", e); |
| 295 | } |
| 296 | return; |
| 297 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 298 | case DO_GET_SELECTED_TEXT: { |
| 299 | SomeArgs args = (SomeArgs)msg.obj; |
| 300 | try { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 301 | InputConnection ic = getInputConnection(); |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 302 | if (ic == null || !isActive()) { |
| 303 | Log.w(TAG, "getSelectedText on inactive InputConnection"); |
| 304 | args.callback.setSelectedText(null, args.seq); |
| 305 | return; |
| 306 | } |
| 307 | args.callback.setSelectedText(ic.getSelectedText( |
| 308 | msg.arg1), args.seq); |
| 309 | } catch (RemoteException e) { |
| 310 | Log.w(TAG, "Got RemoteException calling setSelectedText", e); |
| 311 | } |
| 312 | return; |
| 313 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | case DO_GET_CURSOR_CAPS_MODE: { |
| 315 | SomeArgs args = (SomeArgs)msg.obj; |
| 316 | try { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 317 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | if (ic == null || !isActive()) { |
| 319 | Log.w(TAG, "getCursorCapsMode on inactive InputConnection"); |
| 320 | args.callback.setCursorCapsMode(0, args.seq); |
| 321 | return; |
| 322 | } |
| 323 | args.callback.setCursorCapsMode(ic.getCursorCapsMode(msg.arg1), |
| 324 | args.seq); |
| 325 | } catch (RemoteException e) { |
| 326 | Log.w(TAG, "Got RemoteException calling setCursorCapsMode", e); |
| 327 | } |
| 328 | return; |
| 329 | } |
| 330 | case DO_GET_EXTRACTED_TEXT: { |
| 331 | SomeArgs args = (SomeArgs)msg.obj; |
| 332 | try { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 333 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | if (ic == null || !isActive()) { |
| 335 | Log.w(TAG, "getExtractedText on inactive InputConnection"); |
| 336 | args.callback.setExtractedText(null, args.seq); |
| 337 | return; |
| 338 | } |
| 339 | args.callback.setExtractedText(ic.getExtractedText( |
| 340 | (ExtractedTextRequest)args.arg1, msg.arg1), args.seq); |
| 341 | } catch (RemoteException e) { |
| 342 | Log.w(TAG, "Got RemoteException calling setExtractedText", e); |
| 343 | } |
| 344 | return; |
| 345 | } |
| 346 | case DO_COMMIT_TEXT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 347 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | if (ic == null || !isActive()) { |
| 349 | Log.w(TAG, "commitText on inactive InputConnection"); |
| 350 | return; |
| 351 | } |
| 352 | ic.commitText((CharSequence)msg.obj, msg.arg1); |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 353 | onUserAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | return; |
| 355 | } |
| 356 | case DO_SET_SELECTION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 357 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | if (ic == null || !isActive()) { |
| 359 | Log.w(TAG, "setSelection on inactive InputConnection"); |
| 360 | return; |
| 361 | } |
| 362 | ic.setSelection(msg.arg1, msg.arg2); |
| 363 | return; |
| 364 | } |
| 365 | case DO_PERFORM_EDITOR_ACTION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 366 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | if (ic == null || !isActive()) { |
| 368 | Log.w(TAG, "performEditorAction on inactive InputConnection"); |
| 369 | return; |
| 370 | } |
| 371 | ic.performEditorAction(msg.arg1); |
| 372 | return; |
| 373 | } |
| 374 | case DO_PERFORM_CONTEXT_MENU_ACTION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 375 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | if (ic == null || !isActive()) { |
| 377 | Log.w(TAG, "performContextMenuAction on inactive InputConnection"); |
| 378 | return; |
| 379 | } |
| 380 | ic.performContextMenuAction(msg.arg1); |
| 381 | return; |
| 382 | } |
| 383 | case DO_COMMIT_COMPLETION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 384 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | if (ic == null || !isActive()) { |
| 386 | Log.w(TAG, "commitCompletion on inactive InputConnection"); |
| 387 | return; |
| 388 | } |
| 389 | ic.commitCompletion((CompletionInfo)msg.obj); |
| 390 | return; |
| 391 | } |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 392 | case DO_COMMIT_CORRECTION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 393 | InputConnection ic = getInputConnection(); |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 394 | if (ic == null || !isActive()) { |
| 395 | Log.w(TAG, "commitCorrection on inactive InputConnection"); |
| 396 | return; |
| 397 | } |
| 398 | ic.commitCorrection((CorrectionInfo)msg.obj); |
| 399 | return; |
| 400 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | case DO_SET_COMPOSING_TEXT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 402 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | if (ic == null || !isActive()) { |
| 404 | Log.w(TAG, "setComposingText on inactive InputConnection"); |
| 405 | return; |
| 406 | } |
| 407 | ic.setComposingText((CharSequence)msg.obj, msg.arg1); |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 408 | onUserAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | return; |
| 410 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 411 | case DO_SET_COMPOSING_REGION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 412 | InputConnection ic = getInputConnection(); |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 413 | if (ic == null || !isActive()) { |
| 414 | Log.w(TAG, "setComposingRegion on inactive InputConnection"); |
| 415 | return; |
| 416 | } |
| 417 | ic.setComposingRegion(msg.arg1, msg.arg2); |
| 418 | return; |
| 419 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | case DO_FINISH_COMPOSING_TEXT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 421 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | // Note we do NOT check isActive() here, because this is safe |
| 423 | // for an IME to call at any time, and we need to allow it |
| 424 | // through to clean up our state after the IME has switched to |
| 425 | // another client. |
| 426 | if (ic == null) { |
| 427 | Log.w(TAG, "finishComposingText on inactive InputConnection"); |
| 428 | return; |
| 429 | } |
| 430 | ic.finishComposingText(); |
| 431 | return; |
| 432 | } |
| 433 | case DO_SEND_KEY_EVENT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 434 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | if (ic == null || !isActive()) { |
| 436 | Log.w(TAG, "sendKeyEvent on inactive InputConnection"); |
| 437 | return; |
| 438 | } |
| 439 | ic.sendKeyEvent((KeyEvent)msg.obj); |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 440 | onUserAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | case DO_CLEAR_META_KEY_STATES: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 444 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | if (ic == null || !isActive()) { |
| 446 | Log.w(TAG, "clearMetaKeyStates on inactive InputConnection"); |
| 447 | return; |
| 448 | } |
| 449 | ic.clearMetaKeyStates(msg.arg1); |
| 450 | return; |
| 451 | } |
| 452 | case DO_DELETE_SURROUNDING_TEXT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 453 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | if (ic == null || !isActive()) { |
| 455 | Log.w(TAG, "deleteSurroundingText on inactive InputConnection"); |
| 456 | return; |
| 457 | } |
| 458 | ic.deleteSurroundingText(msg.arg1, msg.arg2); |
| 459 | return; |
| 460 | } |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 461 | case DO_DELETE_SURROUNDING_TEXT_IN_CODE_POINTS: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 462 | InputConnection ic = getInputConnection(); |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 463 | if (ic == null || !isActive()) { |
| 464 | Log.w(TAG, "deleteSurroundingTextInCodePoints on inactive InputConnection"); |
| 465 | return; |
| 466 | } |
| 467 | ic.deleteSurroundingTextInCodePoints(msg.arg1, msg.arg2); |
| 468 | return; |
| 469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | case DO_BEGIN_BATCH_EDIT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 471 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | if (ic == null || !isActive()) { |
| 473 | Log.w(TAG, "beginBatchEdit on inactive InputConnection"); |
| 474 | return; |
| 475 | } |
| 476 | ic.beginBatchEdit(); |
| 477 | return; |
| 478 | } |
| 479 | case DO_END_BATCH_EDIT: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 480 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | if (ic == null || !isActive()) { |
| 482 | Log.w(TAG, "endBatchEdit on inactive InputConnection"); |
| 483 | return; |
| 484 | } |
| 485 | ic.endBatchEdit(); |
| 486 | return; |
| 487 | } |
| 488 | case DO_REPORT_FULLSCREEN_MODE: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 489 | InputConnection ic = getInputConnection(); |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 490 | boolean isBackground = false; |
| 491 | if (ic == null || !isActive()) { |
Andrei Stingaceanu | da589df | 2015-06-17 10:38:08 +0100 | [diff] [blame] | 492 | Log.w(TAG, "reportFullscreenMode on inexistent InputConnection"); |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 493 | isBackground = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
Yohei Yukawa | 159dd47 | 2016-01-07 16:52:33 -0800 | [diff] [blame] | 495 | final boolean enabled = msg.arg1 == 1; |
Yohei Yukawa | 1544def | 2016-04-26 17:13:38 -0700 | [diff] [blame] | 496 | if (!isBackground) { |
| 497 | ic.reportFullscreenMode(enabled); |
| 498 | } |
| 499 | // Due to the nature of asynchronous event handling, currently InputMethodService |
| 500 | // has relied on the fact that #reportFullscreenMode() can be handled even when the |
| 501 | // InputConnection is inactive. We have to notify this event to InputMethodManager. |
| 502 | onReportFullscreenMode(enabled, isBackground); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | case DO_PERFORM_PRIVATE_COMMAND: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 506 | InputConnection ic = getInputConnection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | if (ic == null || !isActive()) { |
| 508 | Log.w(TAG, "performPrivateCommand on inactive InputConnection"); |
| 509 | return; |
| 510 | } |
| 511 | SomeArgs args = (SomeArgs)msg.obj; |
| 512 | ic.performPrivateCommand((String)args.arg1, |
| 513 | (Bundle)args.arg2); |
| 514 | return; |
| 515 | } |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 516 | case DO_REQUEST_UPDATE_CURSOR_ANCHOR_INFO: { |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 517 | SomeArgs args = (SomeArgs)msg.obj; |
| 518 | try { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 519 | InputConnection ic = getInputConnection(); |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 520 | if (ic == null || !isActive()) { |
| 521 | Log.w(TAG, "requestCursorAnchorInfo on inactive InputConnection"); |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 522 | args.callback.setRequestUpdateCursorAnchorInfoResult(false, args.seq); |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 523 | return; |
| 524 | } |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 525 | args.callback.setRequestUpdateCursorAnchorInfoResult( |
Yohei Yukawa | d8636ea | 2014-09-02 22:03:30 -0700 | [diff] [blame] | 526 | ic.requestCursorUpdates(msg.arg1), args.seq); |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 527 | } catch (RemoteException e) { |
| 528 | Log.w(TAG, "Got RemoteException calling requestCursorAnchorInfo", e); |
| 529 | } |
| 530 | return; |
| 531 | } |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 532 | case DO_CLOSE_CONNECTION: { |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 533 | // Note that we do not need to worry about race condition here, because 1) mFinished |
| 534 | // is updated only inside this block, and 2) the code here is running on a Handler |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 535 | // hence we assume multiple DO_CLOSE_CONNECTION messages will not be handled at the |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 536 | // same time. |
| 537 | if (isFinished()) { |
| 538 | return; |
| 539 | } |
| 540 | try { |
| 541 | InputConnection ic = getInputConnection(); |
| 542 | // Note we do NOT check isActive() here, because this is safe |
| 543 | // for an IME to call at any time, and we need to allow it |
| 544 | // through to clean up our state after the IME has switched to |
| 545 | // another client. |
| 546 | if (ic == null) { |
| 547 | return; |
| 548 | } |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 549 | @MissingMethodFlags |
| 550 | final int missingMethods = InputConnectionInspector.getMissingMethodFlags(ic); |
| 551 | if ((missingMethods & MissingMethodFlags.CLOSE_CONNECTION) == 0) { |
| 552 | ic.closeConnection(); |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 553 | } |
| 554 | } finally { |
| 555 | synchronized (mLock) { |
| 556 | mInputConnection = null; |
| 557 | mFinished = true; |
| 558 | } |
| 559 | } |
| 560 | return; |
| 561 | } |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 562 | case DO_COMMIT_CONTENT: { |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 563 | SomeArgs args = (SomeArgs) msg.obj; |
| 564 | try { |
| 565 | InputConnection ic = getInputConnection(); |
| 566 | if (ic == null || !isActive()) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 567 | Log.w(TAG, "commitContent on inactive InputConnection"); |
| 568 | args.callback.setCommitContentResult(false, args.seq); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | final InputContentInfo inputContentInfo = (InputContentInfo) args.arg1; |
| 572 | if (inputContentInfo == null || !inputContentInfo.validate()) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 573 | Log.w(TAG, "commitContent with invalid inputContentInfo=" |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 574 | + inputContentInfo); |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 575 | args.callback.setCommitContentResult(false, args.seq); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 576 | return; |
| 577 | } |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 578 | args.callback.setCommitContentResult( |
| 579 | ic.commitContent(inputContentInfo, (Bundle) args.arg2), args.seq); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 580 | } catch (RemoteException e) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame^] | 581 | Log.w(TAG, "Got RemoteException calling commitContent", e); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 582 | } |
| 583 | return; |
| 584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | } |
| 586 | Log.w(TAG, "Unhandled message code: " + msg.what); |
| 587 | } |
| 588 | |
| 589 | Message obtainMessage(int what) { |
| 590 | return mH.obtainMessage(what); |
| 591 | } |
| 592 | |
| 593 | Message obtainMessageII(int what, int arg1, int arg2) { |
| 594 | return mH.obtainMessage(what, arg1, arg2); |
| 595 | } |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 596 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | Message obtainMessageO(int what, Object arg1) { |
| 598 | return mH.obtainMessage(what, 0, 0, arg1); |
| 599 | } |
| 600 | |
| 601 | Message obtainMessageISC(int what, int arg1, int seq, IInputContextCallback callback) { |
| 602 | SomeArgs args = new SomeArgs(); |
| 603 | args.callback = callback; |
| 604 | args.seq = seq; |
| 605 | return mH.obtainMessage(what, arg1, 0, args); |
| 606 | } |
| 607 | |
| 608 | Message obtainMessageIISC(int what, int arg1, int arg2, int seq, IInputContextCallback callback) { |
| 609 | SomeArgs args = new SomeArgs(); |
| 610 | args.callback = callback; |
| 611 | args.seq = seq; |
| 612 | return mH.obtainMessage(what, arg1, arg2, args); |
| 613 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 614 | |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 615 | Message obtainMessageOOSC(int what, Object arg1, Object arg2, int seq, |
| 616 | IInputContextCallback callback) { |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 617 | SomeArgs args = new SomeArgs(); |
| 618 | args.arg1 = arg1; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 619 | args.arg2 = arg2; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 620 | args.callback = callback; |
| 621 | args.seq = seq; |
| 622 | return mH.obtainMessage(what, 0, 0, args); |
| 623 | } |
| 624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | Message obtainMessageIOSC(int what, int arg1, Object arg2, int seq, |
| 626 | IInputContextCallback callback) { |
| 627 | SomeArgs args = new SomeArgs(); |
| 628 | args.arg1 = arg2; |
| 629 | args.callback = callback; |
| 630 | args.seq = seq; |
| 631 | return mH.obtainMessage(what, arg1, 0, args); |
| 632 | } |
| 633 | |
| 634 | Message obtainMessageIO(int what, int arg1, Object arg2) { |
| 635 | return mH.obtainMessage(what, arg1, 0, arg2); |
| 636 | } |
| 637 | |
| 638 | Message obtainMessageOO(int what, Object arg1, Object arg2) { |
| 639 | SomeArgs args = new SomeArgs(); |
| 640 | args.arg1 = arg1; |
| 641 | args.arg2 = arg2; |
| 642 | return mH.obtainMessage(what, 0, 0, args); |
| 643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | } |