|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +package com.facebook.react.views.textinput; |
| 11 | + |
| 12 | +import javax.annotation.Nullable; |
| 13 | + |
| 14 | +import android.view.KeyEvent; |
| 15 | +import android.view.inputmethod.EditorInfo; |
| 16 | +import android.view.inputmethod.InputConnection; |
| 17 | +import android.view.inputmethod.InputConnectionWrapper; |
| 18 | +import com.facebook.react.bridge.ReactContext; |
| 19 | +import com.facebook.react.uimanager.UIManagerModule; |
| 20 | +import com.facebook.react.uimanager.events.EventDispatcher; |
| 21 | + |
| 22 | +/** |
| 23 | + * A class to implement the TextInput 'onKeyPress' API on android for soft keyboards. |
| 24 | + * It is instantiated in {@link ReactEditText#onCreateInputConnection(EditorInfo)}. |
| 25 | + * |
| 26 | + * Android IMEs interface with EditText views through the {@link InputConnection} interface, |
| 27 | + * so any observable change in state of the EditText via the soft-keyboard, should be a side effect of |
| 28 | + * one or more of the methods in {@link InputConnectionWrapper}. |
| 29 | + * |
| 30 | + * {@link InputConnection#setComposingText(CharSequence, int)} is used to set the composing region |
| 31 | + * (the underlined text) in the {@link android.widget.EditText} view, i.e. when React Native's |
| 32 | + * TextInput has the property 'autoCorrect' set to true. When text is being composed in the composing |
| 33 | + * state within the EditText, each key press will result in a call to |
| 34 | + * {@link InputConnection#setComposingText(CharSequence, int)} with a CharSequence argument equal to |
| 35 | + * that of the entire composing region, rather than a single character diff. |
| 36 | + * We can reason about the keyPress based on the resultant cursor position changes of the EditText after |
| 37 | + * applying this change. For example if the cursor moved backwards by one character when composing, |
| 38 | + * it's likely it was a delete; if it moves forward by a character, likely to be a key press of that character. |
| 39 | + * |
| 40 | + * IMEs can also call {@link InputConnection#beginBatchEdit()} to signify a batch of operations. One |
| 41 | + * such example is committing a word currently in composing state with the press of the space key. |
| 42 | + * It is IME dependent but the stock Android keyboard behavior seems to be to commit the currently composing |
| 43 | + * text with {@link InputConnection#setComposingText(CharSequence, int)} and commits a space character |
| 44 | + * with a separate call to {@link InputConnection#setComposingText(CharSequence, int)}. |
| 45 | + * Here we chose to emit the last input of a batch edit as that tends to be the user input, but |
| 46 | + * it's completely arbitrary. |
| 47 | + * |
| 48 | + * Another function of this class is to detect backspaces when the cursor at the beginning of the |
| 49 | + * {@link android.widget.EditText}, i.e no text is deleted. |
| 50 | + * |
| 51 | + * N.B. this class is only applicable for soft keyboards behavior. For hardware keyboards |
| 52 | + * {@link android.view.View#onKeyDown(int, KeyEvent)} can be overridden to obtain the keycode of the |
| 53 | + * key pressed. |
| 54 | + */ |
| 55 | +class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper { |
| 56 | + public static final String NEWLINE_RAW_VALUE = "\n"; |
| 57 | + public static final String BACKSPACE_KEY_VALUE = "Backspace"; |
| 58 | + public static final String ENTER_KEY_VALUE = "Enter"; |
| 59 | + |
| 60 | + private ReactEditText mEditText; |
| 61 | + private EventDispatcher mEventDispatcher; |
| 62 | + private boolean mIsBatchEdit; |
| 63 | + private @Nullable String mKey = null; |
| 64 | + |
| 65 | + public ReactEditTextInputConnectionWrapper( |
| 66 | + InputConnection target, |
| 67 | + final ReactContext reactContext, |
| 68 | + final ReactEditText editText |
| 69 | + ) { |
| 70 | + super(target, false); |
| 71 | + mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); |
| 72 | + mEditText = editText; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean beginBatchEdit() { |
| 77 | + mIsBatchEdit = true; |
| 78 | + return super.beginBatchEdit(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean endBatchEdit() { |
| 83 | + mIsBatchEdit = false; |
| 84 | + if (mKey != null) { |
| 85 | + dispatchKeyEvent(mKey); |
| 86 | + mKey = null; |
| 87 | + } |
| 88 | + return super.endBatchEdit(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean setComposingText(CharSequence text, int newCursorPosition) { |
| 93 | + int previousSelectionStart = mEditText.getSelectionStart(); |
| 94 | + int previousSelectionEnd = mEditText.getSelectionEnd(); |
| 95 | + String key; |
| 96 | + boolean consumed = super.setComposingText(text, newCursorPosition); |
| 97 | + boolean noPreviousSelection = previousSelectionStart == previousSelectionEnd; |
| 98 | + boolean cursorDidNotMove = mEditText.getSelectionStart() == previousSelectionStart; |
| 99 | + boolean cursorMovedBackwards = mEditText.getSelectionStart() < previousSelectionStart; |
| 100 | + if ((noPreviousSelection && cursorMovedBackwards) |
| 101 | + || !noPreviousSelection && cursorDidNotMove) { |
| 102 | + key = BACKSPACE_KEY_VALUE; |
| 103 | + } else { |
| 104 | + key = String.valueOf(mEditText.getText().charAt(mEditText.getSelectionStart() - 1)); |
| 105 | + } |
| 106 | + dispatchKeyEventOrEnqueue(key); |
| 107 | + return consumed; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean commitText(CharSequence text, int newCursorPosition) { |
| 112 | + String key = text.toString(); |
| 113 | + // Assume not a keyPress if length > 1 |
| 114 | + if (key.length() <= 1) { |
| 115 | + if (key.equals("")) { |
| 116 | + key = BACKSPACE_KEY_VALUE; |
| 117 | + } |
| 118 | + dispatchKeyEventOrEnqueue(key); |
| 119 | + } |
| 120 | + |
| 121 | + return super.commitText(text, newCursorPosition); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean deleteSurroundingText(int beforeLength, int afterLength) { |
| 126 | + dispatchKeyEvent(BACKSPACE_KEY_VALUE); |
| 127 | + return super.deleteSurroundingText(beforeLength, afterLength); |
| 128 | + } |
| 129 | + |
| 130 | + // Called by SwiftKey when cursor at beginning of input when there is a delete |
| 131 | + // or when enter is pressed anywhere in the text. Whereas stock Android Keyboard calls |
| 132 | + // {@link InputConnection#deleteSurroundingText} & {@link InputConnection#commitText} |
| 133 | + // in each case, respectively. |
| 134 | + @Override |
| 135 | + public boolean sendKeyEvent(KeyEvent event) { |
| 136 | + if(event.getAction() == KeyEvent.ACTION_DOWN) { |
| 137 | + if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) { |
| 138 | + dispatchKeyEvent(BACKSPACE_KEY_VALUE); |
| 139 | + } else if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { |
| 140 | + dispatchKeyEvent(ENTER_KEY_VALUE); |
| 141 | + } |
| 142 | + } |
| 143 | + return super.sendKeyEvent(event); |
| 144 | + } |
| 145 | + |
| 146 | + private void dispatchKeyEventOrEnqueue(String key) { |
| 147 | + if (mIsBatchEdit) { |
| 148 | + mKey = key; |
| 149 | + } else { |
| 150 | + dispatchKeyEvent(key); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private void dispatchKeyEvent(String key) { |
| 155 | + if (key.equals(NEWLINE_RAW_VALUE)) { |
| 156 | + key = ENTER_KEY_VALUE; |
| 157 | + } |
| 158 | + mEventDispatcher.dispatchEvent( |
| 159 | + new ReactTextInputKeyPressEvent( |
| 160 | + mEditText.getId(), |
| 161 | + key)); |
| 162 | + } |
| 163 | +} |
0 commit comments