1 /* 2 * Copyright (C) 2007 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 17 package android.view.inputmethod; 18 19 import android.annotation.IntRange; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.view.KeyEvent; 25 26 import com.android.internal.util.Preconditions; 27 28 /** 29 * <p>Wrapper class for proxying calls to another InputConnection. Subclass and have fun! 30 */ 31 public class InputConnectionWrapper implements InputConnection { 32 private InputConnection mTarget; 33 final boolean mMutable; 34 35 /** 36 * Initializes a wrapper. 37 * 38 * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some 39 * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that 40 * has {@code null} in {@code target}.</p> 41 * @param target the {@link InputConnection} to be proxied. 42 * @param mutable set {@code true} to protect this object from being reconfigured to target 43 * another {@link InputConnection}. Note that this is ignored while the target is {@code null}. 44 */ InputConnectionWrapper(InputConnection target, boolean mutable)45 public InputConnectionWrapper(InputConnection target, boolean mutable) { 46 mMutable = mutable; 47 mTarget = target; 48 } 49 50 /** 51 * Change the target of the input connection. 52 * 53 * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some 54 * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that 55 * has {@code null} in {@code target}.</p> 56 * @param target the {@link InputConnection} to be proxied. 57 * @throws SecurityException when this wrapper has non-null target and is immutable. 58 */ setTarget(InputConnection target)59 public void setTarget(InputConnection target) { 60 if (mTarget != null && !mMutable) { 61 throw new SecurityException("not mutable"); 62 } 63 mTarget = target; 64 } 65 66 /** 67 * {@inheritDoc} 68 * @throws NullPointerException if the target is {@code null}. 69 * @throws IllegalArgumentException if {@code length} is negative. 70 */ 71 @Nullable 72 @Override getTextBeforeCursor(@ntRangefrom = 0) int n, int flags)73 public CharSequence getTextBeforeCursor(@IntRange(from = 0) int n, int flags) { 74 Preconditions.checkArgumentNonnegative(n); 75 return mTarget.getTextBeforeCursor(n, flags); 76 } 77 78 /** 79 * {@inheritDoc} 80 * @throws NullPointerException if the target is {@code null}. 81 * @throws IllegalArgumentException if {@code length} is negative. 82 */ 83 @Nullable 84 @Override getTextAfterCursor(@ntRangefrom = 0) int n, int flags)85 public CharSequence getTextAfterCursor(@IntRange(from = 0) int n, int flags) { 86 Preconditions.checkArgumentNonnegative(n); 87 return mTarget.getTextAfterCursor(n, flags); 88 } 89 90 /** 91 * {@inheritDoc} 92 * @throws NullPointerException if the target is {@code null}. 93 */ 94 @Override getSelectedText(int flags)95 public CharSequence getSelectedText(int flags) { 96 return mTarget.getSelectedText(flags); 97 } 98 99 /** 100 * {@inheritDoc} 101 * @throws NullPointerException if the target is {@code null}. 102 * @throws IllegalArgumentException if {@code beforeLength} or {@code afterLength} is negative. 103 */ 104 @Nullable 105 @Override getSurroundingText(int beforeLength, int afterLength, int flags)106 public SurroundingText getSurroundingText(int beforeLength, int afterLength, int flags) { 107 Preconditions.checkArgumentNonnegative(beforeLength); 108 Preconditions.checkArgumentNonnegative(afterLength); 109 return mTarget.getSurroundingText(beforeLength, afterLength, flags); 110 } 111 112 /** 113 * {@inheritDoc} 114 * @throws NullPointerException if the target is {@code null}. 115 */ 116 @Override getCursorCapsMode(int reqModes)117 public int getCursorCapsMode(int reqModes) { 118 return mTarget.getCursorCapsMode(reqModes); 119 } 120 121 /** 122 * {@inheritDoc} 123 * @throws NullPointerException if the target is {@code null}. 124 */ 125 @Override getExtractedText(ExtractedTextRequest request, int flags)126 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { 127 return mTarget.getExtractedText(request, flags); 128 } 129 130 /** 131 * {@inheritDoc} 132 * @throws NullPointerException if the target is {@code null}. 133 */ 134 @Override deleteSurroundingTextInCodePoints(int beforeLength, int afterLength)135 public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { 136 return mTarget.deleteSurroundingTextInCodePoints(beforeLength, afterLength); 137 } 138 139 /** 140 * {@inheritDoc} 141 * @throws NullPointerException if the target is {@code null}. 142 */ 143 @Override deleteSurroundingText(int beforeLength, int afterLength)144 public boolean deleteSurroundingText(int beforeLength, int afterLength) { 145 return mTarget.deleteSurroundingText(beforeLength, afterLength); 146 } 147 148 /** 149 * {@inheritDoc} 150 * @throws NullPointerException if the target is {@code null}. 151 */ 152 @Override setComposingText(CharSequence text, int newCursorPosition)153 public boolean setComposingText(CharSequence text, int newCursorPosition) { 154 return mTarget.setComposingText(text, newCursorPosition); 155 } 156 157 /** 158 * {@inheritDoc} 159 * @throws NullPointerException if the target is {@code null}. 160 */ 161 @Override setComposingText(@onNull CharSequence text, int newCursorPosition, @Nullable TextAttribute textAttribute)162 public boolean setComposingText(@NonNull CharSequence text, 163 int newCursorPosition, @Nullable TextAttribute textAttribute) { 164 return mTarget.setComposingText(text, newCursorPosition, textAttribute); 165 } 166 167 /** 168 * {@inheritDoc} 169 * @throws NullPointerException if the target is {@code null}. 170 */ 171 @Override setComposingRegion(int start, int end)172 public boolean setComposingRegion(int start, int end) { 173 return mTarget.setComposingRegion(start, end); 174 } 175 176 /** 177 * {@inheritDoc} 178 * @throws NullPointerException if the target is {@code null}. 179 */ 180 @Override setComposingRegion(int start, int end, @Nullable TextAttribute textAttribute)181 public boolean setComposingRegion(int start, int end, @Nullable TextAttribute textAttribute) { 182 return mTarget.setComposingRegion(start, end, textAttribute); 183 } 184 185 /** 186 * {@inheritDoc} 187 * @throws NullPointerException if the target is {@code null}. 188 */ 189 @Override finishComposingText()190 public boolean finishComposingText() { 191 return mTarget.finishComposingText(); 192 } 193 194 /** 195 * {@inheritDoc} 196 * @throws NullPointerException if the target is {@code null}. 197 */ 198 @Override commitText(CharSequence text, int newCursorPosition)199 public boolean commitText(CharSequence text, int newCursorPosition) { 200 return mTarget.commitText(text, newCursorPosition); 201 } 202 203 /** 204 * {@inheritDoc} 205 * @throws NullPointerException if the target is {@code null}. 206 */ 207 @Override commitText(@onNull CharSequence text, int newCursorPosition, @Nullable TextAttribute textAttribute)208 public boolean commitText(@NonNull CharSequence text, int newCursorPosition, 209 @Nullable TextAttribute textAttribute) { 210 return mTarget.commitText(text, newCursorPosition, textAttribute); 211 } 212 213 /** 214 * {@inheritDoc} 215 * @throws NullPointerException if the target is {@code null}. 216 */ 217 @Override commitCompletion(CompletionInfo text)218 public boolean commitCompletion(CompletionInfo text) { 219 return mTarget.commitCompletion(text); 220 } 221 222 /** 223 * {@inheritDoc} 224 * @throws NullPointerException if the target is {@code null}. 225 */ 226 @Override commitCorrection(CorrectionInfo correctionInfo)227 public boolean commitCorrection(CorrectionInfo correctionInfo) { 228 return mTarget.commitCorrection(correctionInfo); 229 } 230 231 /** 232 * {@inheritDoc} 233 * @throws NullPointerException if the target is {@code null}. 234 */ 235 @Override setSelection(int start, int end)236 public boolean setSelection(int start, int end) { 237 return mTarget.setSelection(start, end); 238 } 239 240 /** 241 * {@inheritDoc} 242 * @throws NullPointerException if the target is {@code null}. 243 */ 244 @Override performEditorAction(int editorAction)245 public boolean performEditorAction(int editorAction) { 246 return mTarget.performEditorAction(editorAction); 247 } 248 249 /** 250 * {@inheritDoc} 251 * @throws NullPointerException if the target is {@code null}. 252 */ 253 @Override performContextMenuAction(int id)254 public boolean performContextMenuAction(int id) { 255 return mTarget.performContextMenuAction(id); 256 } 257 258 /** 259 * {@inheritDoc} 260 * @throws NullPointerException if the target is {@code null}. 261 */ 262 @Override beginBatchEdit()263 public boolean beginBatchEdit() { 264 return mTarget.beginBatchEdit(); 265 } 266 267 /** 268 * {@inheritDoc} 269 * @throws NullPointerException if the target is {@code null}. 270 */ 271 @Override endBatchEdit()272 public boolean endBatchEdit() { 273 return mTarget.endBatchEdit(); 274 } 275 276 /** 277 * {@inheritDoc} 278 * @throws NullPointerException if the target is {@code null}. 279 */ 280 @Override sendKeyEvent(KeyEvent event)281 public boolean sendKeyEvent(KeyEvent event) { 282 return mTarget.sendKeyEvent(event); 283 } 284 285 /** 286 * {@inheritDoc} 287 * @throws NullPointerException if the target is {@code null}. 288 */ 289 @Override clearMetaKeyStates(int states)290 public boolean clearMetaKeyStates(int states) { 291 return mTarget.clearMetaKeyStates(states); 292 } 293 294 /** 295 * {@inheritDoc} 296 * @throws NullPointerException if the target is {@code null}. 297 */ 298 @Override reportFullscreenMode(boolean enabled)299 public boolean reportFullscreenMode(boolean enabled) { 300 return mTarget.reportFullscreenMode(enabled); 301 } 302 303 /** 304 * {@inheritDoc} 305 * @throws NullPointerException if the target is {@code null}. 306 */ 307 @Override performSpellCheck()308 public boolean performSpellCheck() { 309 return mTarget.performSpellCheck(); 310 } 311 312 /** 313 * {@inheritDoc} 314 * @throws NullPointerException if the target is {@code null}. 315 */ 316 @Override performPrivateCommand(String action, Bundle data)317 public boolean performPrivateCommand(String action, Bundle data) { 318 return mTarget.performPrivateCommand(action, data); 319 } 320 321 /** 322 * {@inheritDoc} 323 * @throws NullPointerException if the target is {@code null}. 324 */ 325 @Override requestCursorUpdates(int cursorUpdateMode)326 public boolean requestCursorUpdates(int cursorUpdateMode) { 327 return mTarget.requestCursorUpdates(cursorUpdateMode); 328 } 329 330 /** 331 * {@inheritDoc} 332 * @throws NullPointerException if the target is {@code null}. 333 */ 334 @Override getHandler()335 public Handler getHandler() { 336 return mTarget.getHandler(); 337 } 338 339 /** 340 * {@inheritDoc} 341 * @throws NullPointerException if the target is {@code null}. 342 */ 343 @Override closeConnection()344 public void closeConnection() { 345 mTarget.closeConnection(); 346 } 347 348 /** 349 * {@inheritDoc} 350 * @throws NullPointerException if the target is {@code null}. 351 */ 352 @Override commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts)353 public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) { 354 return mTarget.commitContent(inputContentInfo, flags, opts); 355 } 356 357 /** 358 * {@inheritDoc} 359 * @throws NullPointerException if the target is {@code null}. 360 */ 361 @Override setImeConsumesInput(boolean imeConsumesInput)362 public boolean setImeConsumesInput(boolean imeConsumesInput) { 363 return mTarget.setImeConsumesInput(imeConsumesInput); 364 } 365 } 366