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