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 | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 19 | import android.annotation.AnyThread; |
| 20 | import android.annotation.BinderThread; |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 23 | import android.inputmethodservice.AbstractInputMethodService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.os.Bundle; |
Yohei Yukawa | 612cce9 | 2016-02-11 17:47:33 -0800 | [diff] [blame] | 25 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.SystemClock; |
| 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.ExtractedText; |
| 33 | import android.view.inputmethod.ExtractedTextRequest; |
| 34 | import android.view.inputmethod.InputConnection; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 35 | import android.view.inputmethod.InputConnectionInspector; |
| 36 | import android.view.inputmethod.InputConnectionInspector.MissingMethodFlags; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 37 | import android.view.inputmethod.InputContentInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 39 | import java.lang.ref.WeakReference; |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 40 | import java.util.concurrent.atomic.AtomicBoolean; |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | public class InputConnectionWrapper implements InputConnection { |
| 43 | private static final int MAX_WAIT_TIME_MILLIS = 2000; |
| 44 | private final IInputContext mIInputContext; |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 45 | @NonNull |
| 46 | private final WeakReference<AbstractInputMethodService> mInputMethodService; |
| 47 | |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 48 | @MissingMethodFlags |
| 49 | private final int mMissingMethods; |
| 50 | |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 51 | /** |
| 52 | * {@code true} if the system already decided to take away IME focus from the target app. This |
| 53 | * can be signaled even when the corresponding signal is in the task queue and |
| 54 | * {@link InputMethodService#onUnbindInput()} is not yet called back on the UI thread. |
| 55 | */ |
| 56 | @NonNull |
| 57 | private final AtomicBoolean mIsUnbindIssued; |
| 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | static class InputContextCallback extends IInputContextCallback.Stub { |
| 60 | private static final String TAG = "InputConnectionWrapper.ICC"; |
| 61 | public int mSeq; |
| 62 | public boolean mHaveValue; |
| 63 | public CharSequence mTextBeforeCursor; |
| 64 | public CharSequence mTextAfterCursor; |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 65 | public CharSequence mSelectedText; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | public ExtractedText mExtractedText; |
| 67 | public int mCursorCapsMode; |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 68 | public boolean mRequestUpdateCursorAnchorInfoResult; |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 69 | public boolean mCommitContentResult; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | // A 'pool' of one InputContextCallback. Each ICW request will attempt to gain |
| 72 | // exclusive access to this object. |
| 73 | private static InputContextCallback sInstance = new InputContextCallback(); |
| 74 | private static int sSequenceNumber = 1; |
| 75 | |
| 76 | /** |
| 77 | * Returns an InputContextCallback object that is guaranteed not to be in use by |
| 78 | * any other thread. The returned object's 'have value' flag is cleared and its expected |
| 79 | * sequence number is set to a new integer. We use a sequence number so that replies that |
| 80 | * occur after a timeout has expired are not interpreted as replies to a later request. |
| 81 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 82 | @UnsupportedAppUsage |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 83 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private static InputContextCallback getInstance() { |
| 85 | synchronized (InputContextCallback.class) { |
| 86 | // Return sInstance if it's non-null, otherwise construct a new callback |
| 87 | InputContextCallback callback; |
| 88 | if (sInstance != null) { |
| 89 | callback = sInstance; |
| 90 | sInstance = null; |
| 91 | |
| 92 | // Reset the callback |
| 93 | callback.mHaveValue = false; |
| 94 | } else { |
| 95 | callback = new InputContextCallback(); |
| 96 | } |
| 97 | |
| 98 | // Set the sequence number |
| 99 | callback.mSeq = sSequenceNumber++; |
| 100 | return callback; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Makes the given InputContextCallback available for use in the future. |
| 106 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 107 | @UnsupportedAppUsage |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 108 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | private void dispose() { |
| 110 | synchronized (InputContextCallback.class) { |
| 111 | // If sInstance is non-null, just let this object be garbage-collected |
| 112 | if (sInstance == null) { |
| 113 | // Allow any objects being held to be gc'ed |
| 114 | mTextAfterCursor = null; |
| 115 | mTextBeforeCursor = null; |
| 116 | mExtractedText = null; |
| 117 | sInstance = this; |
| 118 | } |
| 119 | } |
| 120 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 121 | |
| 122 | @BinderThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | public void setTextBeforeCursor(CharSequence textBeforeCursor, int seq) { |
| 124 | synchronized (this) { |
| 125 | if (seq == mSeq) { |
| 126 | mTextBeforeCursor = textBeforeCursor; |
| 127 | mHaveValue = true; |
| 128 | notifyAll(); |
| 129 | } else { |
| 130 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
| 131 | + ") in setTextBeforeCursor, ignoring."); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 136 | @BinderThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | public void setTextAfterCursor(CharSequence textAfterCursor, int seq) { |
| 138 | synchronized (this) { |
| 139 | if (seq == mSeq) { |
| 140 | mTextAfterCursor = textAfterCursor; |
| 141 | mHaveValue = true; |
| 142 | notifyAll(); |
| 143 | } else { |
| 144 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
| 145 | + ") in setTextAfterCursor, ignoring."); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 150 | @BinderThread |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 151 | public void setSelectedText(CharSequence selectedText, int seq) { |
| 152 | synchronized (this) { |
| 153 | if (seq == mSeq) { |
| 154 | mSelectedText = selectedText; |
| 155 | mHaveValue = true; |
| 156 | notifyAll(); |
| 157 | } else { |
| 158 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
| 159 | + ") in setSelectedText, ignoring."); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 164 | @BinderThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | public void setCursorCapsMode(int capsMode, int seq) { |
| 166 | synchronized (this) { |
| 167 | if (seq == mSeq) { |
| 168 | mCursorCapsMode = capsMode; |
| 169 | mHaveValue = true; |
| 170 | notifyAll(); |
| 171 | } else { |
| 172 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
| 173 | + ") in setCursorCapsMode, ignoring."); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 178 | @BinderThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | public void setExtractedText(ExtractedText extractedText, int seq) { |
| 180 | synchronized (this) { |
| 181 | if (seq == mSeq) { |
| 182 | mExtractedText = extractedText; |
| 183 | mHaveValue = true; |
| 184 | notifyAll(); |
| 185 | } else { |
| 186 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
| 187 | + ") in setExtractedText, ignoring."); |
| 188 | } |
| 189 | } |
| 190 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 191 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 192 | @BinderThread |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 193 | public void setRequestUpdateCursorAnchorInfoResult(boolean result, int seq) { |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 194 | synchronized (this) { |
| 195 | if (seq == mSeq) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 196 | mRequestUpdateCursorAnchorInfoResult = result; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 197 | mHaveValue = true; |
| 198 | notifyAll(); |
| 199 | } else { |
| 200 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
| 201 | + ") in setCursorAnchorInfoRequestResult, ignoring."); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 206 | @BinderThread |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 207 | public void setCommitContentResult(boolean result, int seq) { |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 208 | synchronized (this) { |
| 209 | if (seq == mSeq) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 210 | mCommitContentResult = result; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 211 | mHaveValue = true; |
| 212 | notifyAll(); |
| 213 | } else { |
| 214 | Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 215 | + ") in setCommitContentResult, ignoring."); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | /** |
| 221 | * Waits for a result for up to {@link #MAX_WAIT_TIME_MILLIS} milliseconds. |
| 222 | * |
| 223 | * <p>The caller must be synchronized on this callback object. |
| 224 | */ |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 225 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | void waitForResultLocked() { |
| 227 | long startTime = SystemClock.uptimeMillis(); |
| 228 | long endTime = startTime + MAX_WAIT_TIME_MILLIS; |
| 229 | |
| 230 | while (!mHaveValue) { |
| 231 | long remainingTime = endTime - SystemClock.uptimeMillis(); |
| 232 | if (remainingTime <= 0) { |
| 233 | Log.w(TAG, "Timed out waiting on IInputContextCallback"); |
| 234 | return; |
| 235 | } |
| 236 | try { |
| 237 | wait(remainingTime); |
| 238 | } catch (InterruptedException e) { |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 244 | public InputConnectionWrapper( |
| 245 | @NonNull WeakReference<AbstractInputMethodService> inputMethodService, |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 246 | IInputContext inputContext, @MissingMethodFlags final int missingMethods, |
| 247 | @NonNull AtomicBoolean isUnbindIssued) { |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 248 | mInputMethodService = inputMethodService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | mIInputContext = inputContext; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 250 | mMissingMethods = missingMethods; |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 251 | mIsUnbindIssued = isUnbindIssued; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 254 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | public CharSequence getTextAfterCursor(int length, int flags) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 256 | if (mIsUnbindIssued.get()) { |
| 257 | return null; |
| 258 | } |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | CharSequence value = null; |
| 261 | try { |
| 262 | InputContextCallback callback = InputContextCallback.getInstance(); |
| 263 | mIInputContext.getTextAfterCursor(length, flags, callback.mSeq, callback); |
| 264 | synchronized (callback) { |
| 265 | callback.waitForResultLocked(); |
| 266 | if (callback.mHaveValue) { |
| 267 | value = callback.mTextAfterCursor; |
| 268 | } |
| 269 | } |
| 270 | callback.dispose(); |
| 271 | } catch (RemoteException e) { |
| 272 | return null; |
| 273 | } |
| 274 | return value; |
| 275 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 276 | |
| 277 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | public CharSequence getTextBeforeCursor(int length, int flags) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 279 | if (mIsUnbindIssued.get()) { |
| 280 | return null; |
| 281 | } |
| 282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | CharSequence value = null; |
| 284 | try { |
| 285 | InputContextCallback callback = InputContextCallback.getInstance(); |
| 286 | mIInputContext.getTextBeforeCursor(length, flags, callback.mSeq, callback); |
| 287 | synchronized (callback) { |
| 288 | callback.waitForResultLocked(); |
| 289 | if (callback.mHaveValue) { |
| 290 | value = callback.mTextBeforeCursor; |
| 291 | } |
| 292 | } |
| 293 | callback.dispose(); |
| 294 | } catch (RemoteException e) { |
| 295 | return null; |
| 296 | } |
| 297 | return value; |
| 298 | } |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 299 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 300 | @AnyThread |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 301 | public CharSequence getSelectedText(int flags) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 302 | if (mIsUnbindIssued.get()) { |
| 303 | return null; |
| 304 | } |
| 305 | |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 306 | if (isMethodMissing(MissingMethodFlags.GET_SELECTED_TEXT)) { |
| 307 | // This method is not implemented. |
| 308 | return null; |
| 309 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 310 | CharSequence value = null; |
| 311 | try { |
| 312 | InputContextCallback callback = InputContextCallback.getInstance(); |
| 313 | mIInputContext.getSelectedText(flags, callback.mSeq, callback); |
| 314 | synchronized (callback) { |
| 315 | callback.waitForResultLocked(); |
| 316 | if (callback.mHaveValue) { |
| 317 | value = callback.mSelectedText; |
| 318 | } |
| 319 | } |
| 320 | callback.dispose(); |
| 321 | } catch (RemoteException e) { |
| 322 | return null; |
| 323 | } |
| 324 | return value; |
| 325 | } |
| 326 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 327 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | public int getCursorCapsMode(int reqModes) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 329 | if (mIsUnbindIssued.get()) { |
| 330 | return 0; |
| 331 | } |
| 332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | int value = 0; |
| 334 | try { |
| 335 | InputContextCallback callback = InputContextCallback.getInstance(); |
| 336 | mIInputContext.getCursorCapsMode(reqModes, callback.mSeq, callback); |
| 337 | synchronized (callback) { |
| 338 | callback.waitForResultLocked(); |
| 339 | if (callback.mHaveValue) { |
| 340 | value = callback.mCursorCapsMode; |
| 341 | } |
| 342 | } |
| 343 | callback.dispose(); |
| 344 | } catch (RemoteException e) { |
| 345 | return 0; |
| 346 | } |
| 347 | return value; |
| 348 | } |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 349 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 350 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 352 | if (mIsUnbindIssued.get()) { |
| 353 | return null; |
| 354 | } |
| 355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | ExtractedText value = null; |
| 357 | try { |
| 358 | InputContextCallback callback = InputContextCallback.getInstance(); |
| 359 | mIInputContext.getExtractedText(request, flags, callback.mSeq, callback); |
| 360 | synchronized (callback) { |
| 361 | callback.waitForResultLocked(); |
| 362 | if (callback.mHaveValue) { |
| 363 | value = callback.mExtractedText; |
| 364 | } |
| 365 | } |
| 366 | callback.dispose(); |
| 367 | } catch (RemoteException e) { |
| 368 | return null; |
| 369 | } |
| 370 | return value; |
| 371 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 372 | |
| 373 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | public boolean commitText(CharSequence text, int newCursorPosition) { |
| 375 | try { |
| 376 | mIInputContext.commitText(text, newCursorPosition); |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 377 | notifyUserActionIfNecessary(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | return true; |
| 379 | } catch (RemoteException e) { |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 384 | @AnyThread |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 385 | private void notifyUserActionIfNecessary() { |
| 386 | final AbstractInputMethodService inputMethodService = mInputMethodService.get(); |
| 387 | if (inputMethodService == null) { |
| 388 | // This basically should not happen, because it's the the caller of this method. |
| 389 | return; |
| 390 | } |
| 391 | inputMethodService.notifyUserActionIfNecessary(); |
| 392 | } |
| 393 | |
| 394 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | public boolean commitCompletion(CompletionInfo text) { |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 396 | if (isMethodMissing(MissingMethodFlags.COMMIT_CORRECTION)) { |
| 397 | // This method is not implemented. |
| 398 | return false; |
| 399 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | try { |
| 401 | mIInputContext.commitCompletion(text); |
| 402 | return true; |
| 403 | } catch (RemoteException e) { |
| 404 | return false; |
| 405 | } |
| 406 | } |
| 407 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 408 | @AnyThread |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 409 | public boolean commitCorrection(CorrectionInfo correctionInfo) { |
| 410 | try { |
| 411 | mIInputContext.commitCorrection(correctionInfo); |
| 412 | return true; |
| 413 | } catch (RemoteException e) { |
| 414 | return false; |
| 415 | } |
| 416 | } |
| 417 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 418 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | public boolean setSelection(int start, int end) { |
| 420 | try { |
| 421 | mIInputContext.setSelection(start, end); |
| 422 | return true; |
| 423 | } catch (RemoteException e) { |
| 424 | return false; |
| 425 | } |
| 426 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 427 | |
| 428 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | public boolean performEditorAction(int actionCode) { |
| 430 | try { |
| 431 | mIInputContext.performEditorAction(actionCode); |
| 432 | return true; |
| 433 | } catch (RemoteException e) { |
| 434 | return false; |
| 435 | } |
| 436 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 437 | |
| 438 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | public boolean performContextMenuAction(int id) { |
| 440 | try { |
| 441 | mIInputContext.performContextMenuAction(id); |
| 442 | return true; |
| 443 | } catch (RemoteException e) { |
| 444 | return false; |
| 445 | } |
| 446 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 447 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 448 | @AnyThread |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 449 | public boolean setComposingRegion(int start, int end) { |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 450 | if (isMethodMissing(MissingMethodFlags.SET_COMPOSING_REGION)) { |
| 451 | // This method is not implemented. |
| 452 | return false; |
| 453 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 454 | try { |
| 455 | mIInputContext.setComposingRegion(start, end); |
| 456 | return true; |
| 457 | } catch (RemoteException e) { |
| 458 | return false; |
| 459 | } |
| 460 | } |
| 461 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 462 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | public boolean setComposingText(CharSequence text, int newCursorPosition) { |
| 464 | try { |
| 465 | mIInputContext.setComposingText(text, newCursorPosition); |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 466 | notifyUserActionIfNecessary(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | return true; |
| 468 | } catch (RemoteException e) { |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 473 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | public boolean finishComposingText() { |
| 475 | try { |
| 476 | mIInputContext.finishComposingText(); |
| 477 | return true; |
| 478 | } catch (RemoteException e) { |
| 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 483 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | public boolean beginBatchEdit() { |
| 485 | try { |
| 486 | mIInputContext.beginBatchEdit(); |
| 487 | return true; |
| 488 | } catch (RemoteException e) { |
| 489 | return false; |
| 490 | } |
| 491 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 492 | |
| 493 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | public boolean endBatchEdit() { |
| 495 | try { |
| 496 | mIInputContext.endBatchEdit(); |
| 497 | return true; |
| 498 | } catch (RemoteException e) { |
| 499 | return false; |
| 500 | } |
| 501 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 502 | |
| 503 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | public boolean sendKeyEvent(KeyEvent event) { |
| 505 | try { |
| 506 | mIInputContext.sendKeyEvent(event); |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 507 | notifyUserActionIfNecessary(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | return true; |
| 509 | } catch (RemoteException e) { |
| 510 | return false; |
| 511 | } |
| 512 | } |
| 513 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 514 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | public boolean clearMetaKeyStates(int states) { |
| 516 | try { |
| 517 | mIInputContext.clearMetaKeyStates(states); |
| 518 | return true; |
| 519 | } catch (RemoteException e) { |
| 520 | return false; |
| 521 | } |
| 522 | } |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 523 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 524 | @AnyThread |
Fabrice Di Meglio | 0c95dd3 | 2012-01-23 15:06:42 -0800 | [diff] [blame] | 525 | public boolean deleteSurroundingText(int beforeLength, int afterLength) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | try { |
Fabrice Di Meglio | 0c95dd3 | 2012-01-23 15:06:42 -0800 | [diff] [blame] | 527 | mIInputContext.deleteSurroundingText(beforeLength, afterLength); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | return true; |
| 529 | } catch (RemoteException e) { |
| 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 534 | @AnyThread |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 535 | public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 536 | if (isMethodMissing(MissingMethodFlags.DELETE_SURROUNDING_TEXT_IN_CODE_POINTS)) { |
| 537 | // This method is not implemented. |
| 538 | return false; |
| 539 | } |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 540 | try { |
| 541 | mIInputContext.deleteSurroundingTextInCodePoints(beforeLength, afterLength); |
| 542 | return true; |
| 543 | } catch (RemoteException e) { |
| 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 548 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | public boolean reportFullscreenMode(boolean enabled) { |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 550 | // Nothing should happen when called from input method. |
| 551 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 554 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | public boolean performPrivateCommand(String action, Bundle data) { |
| 556 | try { |
| 557 | mIInputContext.performPrivateCommand(action, data); |
| 558 | return true; |
| 559 | } catch (RemoteException e) { |
| 560 | return false; |
| 561 | } |
| 562 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 563 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 564 | @AnyThread |
Yohei Yukawa | d8636ea | 2014-09-02 22:03:30 -0700 | [diff] [blame] | 565 | public boolean requestCursorUpdates(int cursorUpdateMode) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 566 | if (mIsUnbindIssued.get()) { |
| 567 | return false; |
| 568 | } |
| 569 | |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 570 | boolean result = false; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 571 | if (isMethodMissing(MissingMethodFlags.REQUEST_CURSOR_UPDATES)) { |
| 572 | // This method is not implemented. |
| 573 | return false; |
| 574 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 575 | try { |
| 576 | InputContextCallback callback = InputContextCallback.getInstance(); |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 577 | mIInputContext.requestUpdateCursorAnchorInfo(cursorUpdateMode, callback.mSeq, callback); |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 578 | synchronized (callback) { |
| 579 | callback.waitForResultLocked(); |
| 580 | if (callback.mHaveValue) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 581 | result = callback.mRequestUpdateCursorAnchorInfoResult; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | callback.dispose(); |
| 585 | } catch (RemoteException e) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 586 | return false; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 587 | } |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 588 | return result; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 589 | } |
Yohei Yukawa | 612cce9 | 2016-02-11 17:47:33 -0800 | [diff] [blame] | 590 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 591 | @AnyThread |
Yohei Yukawa | 612cce9 | 2016-02-11 17:47:33 -0800 | [diff] [blame] | 592 | public Handler getHandler() { |
| 593 | // Nothing should happen when called from input method. |
| 594 | return null; |
| 595 | } |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 596 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 597 | @AnyThread |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 598 | public void closeConnection() { |
| 599 | // Nothing should happen when called from input method. |
| 600 | } |
| 601 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 602 | @AnyThread |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 603 | public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 604 | if (mIsUnbindIssued.get()) { |
| 605 | return false; |
| 606 | } |
| 607 | |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 608 | boolean result = false; |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 609 | if (isMethodMissing(MissingMethodFlags.COMMIT_CONTENT)) { |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 610 | // This method is not implemented. |
| 611 | return false; |
| 612 | } |
| 613 | try { |
Yohei Yukawa | 79d1c75 | 2016-06-30 19:24:04 +0000 | [diff] [blame] | 614 | if ((flags & InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) { |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 615 | final AbstractInputMethodService inputMethodService = mInputMethodService.get(); |
| 616 | if (inputMethodService == null) { |
| 617 | // This basically should not happen, because it's the the caller of this method. |
| 618 | return false; |
| 619 | } |
| 620 | inputMethodService.exposeContent(inputContentInfo, this); |
| 621 | } |
Yohei Yukawa | 79d1c75 | 2016-06-30 19:24:04 +0000 | [diff] [blame] | 622 | |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 623 | InputContextCallback callback = InputContextCallback.getInstance(); |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 624 | mIInputContext.commitContent(inputContentInfo, flags, opts, callback.mSeq, callback); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 625 | synchronized (callback) { |
| 626 | callback.waitForResultLocked(); |
| 627 | if (callback.mHaveValue) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 628 | result = callback.mCommitContentResult; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | callback.dispose(); |
| 632 | } catch (RemoteException e) { |
| 633 | return false; |
| 634 | } |
| 635 | return result; |
| 636 | } |
| 637 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 638 | @AnyThread |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 639 | private boolean isMethodMissing(@MissingMethodFlags final int methodFlag) { |
| 640 | return (mMissingMethods & methodFlag) == methodFlag; |
| 641 | } |
| 642 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 643 | @AnyThread |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 644 | @Override |
| 645 | public String toString() { |
| 646 | return "InputConnectionWrapper{idHash=#" |
| 647 | + Integer.toHexString(System.identityHashCode(this)) |
| 648 | + " mMissingMethods=" |
| 649 | + InputConnectionInspector.getMissingMethodFlagsAsString(mMissingMethods) + "}"; |
| 650 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | } |