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); |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame^] | 374 | notifyUserActionIfNecessary(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | return true; |
| 376 | } catch (RemoteException e) { |
| 377 | return false; |
| 378 | } |
| 379 | } |
| 380 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 381 | @AnyThread |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame^] | 382 | private void notifyUserActionIfNecessary() { |
| 383 | final AbstractInputMethodService inputMethodService = mInputMethodService.get(); |
| 384 | if (inputMethodService == null) { |
| 385 | // This basically should not happen, because it's the the caller of this method. |
| 386 | return; |
| 387 | } |
| 388 | inputMethodService.notifyUserActionIfNecessary(); |
| 389 | } |
| 390 | |
| 391 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | public boolean commitCompletion(CompletionInfo text) { |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 393 | if (isMethodMissing(MissingMethodFlags.COMMIT_CORRECTION)) { |
| 394 | // This method is not implemented. |
| 395 | return false; |
| 396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | try { |
| 398 | mIInputContext.commitCompletion(text); |
| 399 | return true; |
| 400 | } catch (RemoteException e) { |
| 401 | return false; |
| 402 | } |
| 403 | } |
| 404 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 405 | @AnyThread |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 406 | public boolean commitCorrection(CorrectionInfo correctionInfo) { |
| 407 | try { |
| 408 | mIInputContext.commitCorrection(correctionInfo); |
| 409 | return true; |
| 410 | } catch (RemoteException e) { |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 415 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | public boolean setSelection(int start, int end) { |
| 417 | try { |
| 418 | mIInputContext.setSelection(start, end); |
| 419 | return true; |
| 420 | } catch (RemoteException e) { |
| 421 | return false; |
| 422 | } |
| 423 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 424 | |
| 425 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | public boolean performEditorAction(int actionCode) { |
| 427 | try { |
| 428 | mIInputContext.performEditorAction(actionCode); |
| 429 | return true; |
| 430 | } catch (RemoteException e) { |
| 431 | return false; |
| 432 | } |
| 433 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 434 | |
| 435 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | public boolean performContextMenuAction(int id) { |
| 437 | try { |
| 438 | mIInputContext.performContextMenuAction(id); |
| 439 | return true; |
| 440 | } catch (RemoteException e) { |
| 441 | return false; |
| 442 | } |
| 443 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 444 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 445 | @AnyThread |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 446 | public boolean setComposingRegion(int start, int end) { |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 447 | if (isMethodMissing(MissingMethodFlags.SET_COMPOSING_REGION)) { |
| 448 | // This method is not implemented. |
| 449 | return false; |
| 450 | } |
Amith Yamasani | a90b7f0 | 2010-08-25 18:27:20 -0700 | [diff] [blame] | 451 | try { |
| 452 | mIInputContext.setComposingRegion(start, end); |
| 453 | return true; |
| 454 | } catch (RemoteException e) { |
| 455 | return false; |
| 456 | } |
| 457 | } |
| 458 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 459 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | public boolean setComposingText(CharSequence text, int newCursorPosition) { |
| 461 | try { |
| 462 | mIInputContext.setComposingText(text, newCursorPosition); |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame^] | 463 | notifyUserActionIfNecessary(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | return true; |
| 465 | } catch (RemoteException e) { |
| 466 | return false; |
| 467 | } |
| 468 | } |
| 469 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 470 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | public boolean finishComposingText() { |
| 472 | try { |
| 473 | mIInputContext.finishComposingText(); |
| 474 | return true; |
| 475 | } catch (RemoteException e) { |
| 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 480 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | public boolean beginBatchEdit() { |
| 482 | try { |
| 483 | mIInputContext.beginBatchEdit(); |
| 484 | return true; |
| 485 | } catch (RemoteException e) { |
| 486 | return false; |
| 487 | } |
| 488 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 489 | |
| 490 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | public boolean endBatchEdit() { |
| 492 | try { |
| 493 | mIInputContext.endBatchEdit(); |
| 494 | return true; |
| 495 | } catch (RemoteException e) { |
| 496 | return false; |
| 497 | } |
| 498 | } |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 499 | |
| 500 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | public boolean sendKeyEvent(KeyEvent event) { |
| 502 | try { |
| 503 | mIInputContext.sendKeyEvent(event); |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame^] | 504 | notifyUserActionIfNecessary(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | return true; |
| 506 | } catch (RemoteException e) { |
| 507 | return false; |
| 508 | } |
| 509 | } |
| 510 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 511 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | public boolean clearMetaKeyStates(int states) { |
| 513 | try { |
| 514 | mIInputContext.clearMetaKeyStates(states); |
| 515 | return true; |
| 516 | } catch (RemoteException e) { |
| 517 | return false; |
| 518 | } |
| 519 | } |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 520 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 521 | @AnyThread |
Fabrice Di Meglio | 0c95dd3 | 2012-01-23 15:06:42 -0800 | [diff] [blame] | 522 | public boolean deleteSurroundingText(int beforeLength, int afterLength) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | try { |
Fabrice Di Meglio | 0c95dd3 | 2012-01-23 15:06:42 -0800 | [diff] [blame] | 524 | mIInputContext.deleteSurroundingText(beforeLength, afterLength); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | return true; |
| 526 | } catch (RemoteException e) { |
| 527 | return false; |
| 528 | } |
| 529 | } |
| 530 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 531 | @AnyThread |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 532 | public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 533 | if (isMethodMissing(MissingMethodFlags.DELETE_SURROUNDING_TEXT_IN_CODE_POINTS)) { |
| 534 | // This method is not implemented. |
| 535 | return false; |
| 536 | } |
Yohei Yukawa | c89e22a | 2016-01-13 22:48:14 -0800 | [diff] [blame] | 537 | try { |
| 538 | mIInputContext.deleteSurroundingTextInCodePoints(beforeLength, afterLength); |
| 539 | return true; |
| 540 | } catch (RemoteException e) { |
| 541 | return false; |
| 542 | } |
| 543 | } |
| 544 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 545 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | public boolean reportFullscreenMode(boolean enabled) { |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 547 | // Nothing should happen when called from input method. |
| 548 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 551 | @AnyThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | public boolean performPrivateCommand(String action, Bundle data) { |
| 553 | try { |
| 554 | mIInputContext.performPrivateCommand(action, data); |
| 555 | return true; |
| 556 | } catch (RemoteException e) { |
| 557 | return false; |
| 558 | } |
| 559 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 560 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 561 | @AnyThread |
Yohei Yukawa | d8636ea | 2014-09-02 22:03:30 -0700 | [diff] [blame] | 562 | public boolean requestCursorUpdates(int cursorUpdateMode) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 563 | if (mIsUnbindIssued.get()) { |
| 564 | return false; |
| 565 | } |
| 566 | |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 567 | boolean result = false; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 568 | if (isMethodMissing(MissingMethodFlags.REQUEST_CURSOR_UPDATES)) { |
| 569 | // This method is not implemented. |
| 570 | return false; |
| 571 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 572 | try { |
| 573 | InputContextCallback callback = InputContextCallback.getInstance(); |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 574 | mIInputContext.requestUpdateCursorAnchorInfo(cursorUpdateMode, callback.mSeq, callback); |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 575 | synchronized (callback) { |
| 576 | callback.waitForResultLocked(); |
| 577 | if (callback.mHaveValue) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 578 | result = callback.mRequestUpdateCursorAnchorInfoResult; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | callback.dispose(); |
| 582 | } catch (RemoteException e) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 583 | return false; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 584 | } |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 585 | return result; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 586 | } |
Yohei Yukawa | 612cce9 | 2016-02-11 17:47:33 -0800 | [diff] [blame] | 587 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 588 | @AnyThread |
Yohei Yukawa | 612cce9 | 2016-02-11 17:47:33 -0800 | [diff] [blame] | 589 | public Handler getHandler() { |
| 590 | // Nothing should happen when called from input method. |
| 591 | return null; |
| 592 | } |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 593 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 594 | @AnyThread |
Yohei Yukawa | 9f9afe52 | 2016-03-30 12:03:51 -0700 | [diff] [blame] | 595 | public void closeConnection() { |
| 596 | // Nothing should happen when called from input method. |
| 597 | } |
| 598 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 599 | @AnyThread |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 600 | public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) { |
Tarandeep Singh | 1d113d0 | 2017-10-13 10:51:00 -0700 | [diff] [blame] | 601 | if (mIsUnbindIssued.get()) { |
| 602 | return false; |
| 603 | } |
| 604 | |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 605 | boolean result = false; |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 606 | if (isMethodMissing(MissingMethodFlags.COMMIT_CONTENT)) { |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 607 | // This method is not implemented. |
| 608 | return false; |
| 609 | } |
| 610 | try { |
Yohei Yukawa | 79d1c75 | 2016-06-30 19:24:04 +0000 | [diff] [blame] | 611 | if ((flags & InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) { |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 612 | final AbstractInputMethodService inputMethodService = mInputMethodService.get(); |
| 613 | if (inputMethodService == null) { |
| 614 | // This basically should not happen, because it's the the caller of this method. |
| 615 | return false; |
| 616 | } |
| 617 | inputMethodService.exposeContent(inputContentInfo, this); |
| 618 | } |
Yohei Yukawa | 79d1c75 | 2016-06-30 19:24:04 +0000 | [diff] [blame] | 619 | |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 620 | InputContextCallback callback = InputContextCallback.getInstance(); |
Yohei Yukawa | 45700fa | 2016-06-23 17:12:59 -0700 | [diff] [blame] | 621 | mIInputContext.commitContent(inputContentInfo, flags, opts, callback.mSeq, callback); |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 622 | synchronized (callback) { |
| 623 | callback.waitForResultLocked(); |
| 624 | if (callback.mHaveValue) { |
Yohei Yukawa | adebb52 | 2016-06-17 10:10:39 -0700 | [diff] [blame] | 625 | result = callback.mCommitContentResult; |
Yohei Yukawa | 152944f | 2016-06-10 19:04:34 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | callback.dispose(); |
| 629 | } catch (RemoteException e) { |
| 630 | return false; |
| 631 | } |
| 632 | return result; |
| 633 | } |
| 634 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 635 | @AnyThread |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 636 | private boolean isMethodMissing(@MissingMethodFlags final int methodFlag) { |
| 637 | return (mMissingMethods & methodFlag) == methodFlag; |
| 638 | } |
| 639 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 640 | @AnyThread |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 641 | @Override |
| 642 | public String toString() { |
| 643 | return "InputConnectionWrapper{idHash=#" |
| 644 | + Integer.toHexString(System.identityHashCode(this)) |
| 645 | + " mMissingMethods=" |
| 646 | + InputConnectionInspector.getMissingMethodFlagsAsString(mMissingMethods) + "}"; |
| 647 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | } |