From cac27d15bef39e1a4bf7e3279e85a7bae4ee3a9c Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Wed, 16 Apr 2025 09:14:55 -0700 Subject: [PATCH] Convert to Kotlin - ReactEditText Summary: This diff converts the last java file inside `com.facebook.react.views.textinput` to Kotlin. As `ReactEditText` is quite involved, reviewing this one is going to be quite critical. I'm marking this as breaking as a number of nullability types has changed for OSS users. Changelog: [Android] [Breaking] - com.facebook.react.views.textinput.ReactEditText is now in Kotlin. If you're subclassing this type you'll need to adjust your signatures. Reviewed By: rshest Differential Revision: D72972921 fbshipit-source-id: 92ed112444cbc20daed5466ad20f651479bfac6f --- .../ReactAndroid/api/ReactAndroid.api | 107 +- .../react/views/textinput/ReactEditText.java | 1444 ----------------- .../react/views/textinput/ReactEditText.kt | 1288 +++++++++++++++ .../views/textinput/ReactTextInputManager.kt | 10 +- .../textinput/ReactTextInputTextWatcher.kt | 2 +- 5 files changed, 1352 insertions(+), 1499 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f4e2c82f6b1..e95c0a6505d 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6674,31 +6674,31 @@ public abstract interface class com/facebook/react/views/textinput/ContentSizeWa } public class com/facebook/react/views/textinput/ReactEditText : androidx/appcompat/widget/AppCompatEditText { - public static final field DEBUG_MODE Z - protected field mContainsImages Z - protected field mDisableTextDiffing Z - protected field mIsSettingTextFromJS Z - protected field mIsSettingTextFromState Z - protected field mNativeEventCount I + public static final field Companion Lcom/facebook/react/views/textinput/ReactEditText$Companion; public fun (Landroid/content/Context;)V public fun addTextChangedListener (Landroid/text/TextWatcher;)V - protected fun applyTextAttributes ()V - public fun canUpdateWithEventCount (I)Z + protected final fun applyTextAttributes ()V + public final fun canUpdateWithEventCount (I)Z public fun clearFocus ()V - protected fun finalize ()V - public fun getBorderColor (I)I - public fun getDisableFullscreenUI ()Z - public fun getReturnKeyType ()Ljava/lang/String; - public fun getStateWrapper ()Lcom/facebook/react/uimanager/StateWrapper; - public fun getSubmitBehavior ()Ljava/lang/String; - protected fun hideSoftKeyboard ()V - public fun incrementAndGetEventCounter ()I + protected final fun finalize ()V + public final fun getBorderColor (I)I + protected final fun getContainsImages ()Z + public final fun getDisableFullscreenUI ()Z + protected final fun getNativeEventCount ()I + public final fun getReturnKeyType ()Ljava/lang/String; + public final fun getStagedInputType ()I + public final fun getStateWrapper ()Lcom/facebook/react/uimanager/StateWrapper; + public final fun getSubmitBehavior ()Ljava/lang/String; + protected final fun hideSoftKeyboard ()V + public final fun incrementAndGetEventCounter ()I public fun invalidateDrawable (Landroid/graphics/drawable/Drawable;)V public fun isLayoutRequested ()Z - public fun maybeSetSelection (III)V - public fun maybeSetTextFromJS (Lcom/facebook/react/views/text/ReactTextUpdate;)V - public fun maybeSetTextFromState (Lcom/facebook/react/views/text/ReactTextUpdate;)V - public fun maybeUpdateTypeface ()V + protected final fun isSettingTextFromJS ()Z + protected final fun isSettingTextFromState ()Z + public final fun maybeSetSelection (III)V + public final fun maybeSetTextFromJS (Lcom/facebook/react/views/text/ReactTextUpdate;)V + public final fun maybeSetTextFromState (Lcom/facebook/react/views/text/ReactTextUpdate;)V + public final fun maybeUpdateTypeface ()V public fun onAttachedToWindow ()V public fun onConfigurationChanged (Landroid/content/res/Configuration;)V public fun onCreateInputConnection (Landroid/view/inputmethod/EditorInfo;)Landroid/view/inputmethod/InputConnection; @@ -6715,43 +6715,52 @@ public class com/facebook/react/views/textinput/ReactEditText : androidx/appcomp public fun onTouchEvent (Landroid/view/MotionEvent;)Z public fun removeTextChangedListener (Landroid/text/TextWatcher;)V public fun requestFocus (ILandroid/graphics/Rect;)Z - public fun requestFocusFromJS ()V - public fun setAllowFontScaling (Z)V - public fun setAutoFocus (Z)V + public final fun requestFocusFromJS ()V + public final fun setAllowFontScaling (Z)V + public final fun setAutoFocus (Z)V public fun setBackgroundColor (I)V - public fun setBorderColor (ILjava/lang/Integer;)V - public fun setBorderRadius (F)V - public fun setBorderRadius (FI)V - public fun setBorderStyle (Ljava/lang/String;)V - public fun setBorderWidth (IF)V - public fun setContentSizeWatcher (Lcom/facebook/react/views/textinput/ContentSizeWatcher;)V - public fun setContextMenuHidden (Z)V - public fun setDisableFullscreenUI (Z)V - public fun setFontFamily (Ljava/lang/String;)V + public final fun setBorderColor (ILjava/lang/Integer;)V + public final fun setBorderRadius (F)V + public final fun setBorderRadius (FI)V + public final fun setBorderStyle (Ljava/lang/String;)V + public final fun setBorderWidth (IF)V + protected final fun setContainsImages (Z)V + public final fun setContentSizeWatcher (Lcom/facebook/react/views/textinput/ContentSizeWatcher;)V + public final fun setContextMenuHidden (Z)V + public final fun setDisableFullscreenUI (Z)V + public final fun setEventDispatcher (Lcom/facebook/react/uimanager/events/EventDispatcher;)V + public final fun setFontFamily (Ljava/lang/String;)V public fun setFontFeatureSettings (Ljava/lang/String;)V - public fun setFontSize (F)V - public fun setFontStyle (Ljava/lang/String;)V - public fun setFontWeight (Ljava/lang/String;)V + public final fun setFontSize (F)V + public final fun setFontStyle (Ljava/lang/String;)V + public final fun setFontWeight (Ljava/lang/String;)V public fun setInputType (I)V - public fun setLetterSpacingPt (F)V + public final fun setLetterSpacingPt (F)V public fun setLineHeight (I)V - public fun setMaxFontSizeMultiplier (F)V - public fun setOnKeyPress (Z)V - public fun setOverflow (Ljava/lang/String;)V - public fun setPlaceholder (Ljava/lang/String;)V - public fun setReturnKeyType (Ljava/lang/String;)V - public fun setScrollWatcher (Lcom/facebook/react/views/textinput/ScrollWatcher;)V - public fun setSelectTextOnFocus (Z)V + public final fun setMaxFontSizeMultiplier (F)V + protected final fun setNativeEventCount (I)V + public final fun setOnKeyPress (Z)V + public final fun setOverflow (Ljava/lang/String;)V + public final fun setPlaceholder (Ljava/lang/String;)V + public final fun setReturnKeyType (Ljava/lang/String;)V + public final fun setScrollWatcher (Lcom/facebook/react/views/textinput/ScrollWatcher;)V + public final fun setSelectTextOnFocus (Z)V public fun setSelection (II)V - public fun setSelectionWatcher (Lcom/facebook/react/views/textinput/SelectionWatcher;)V - public fun setStateWrapper (Lcom/facebook/react/uimanager/StateWrapper;)V - public fun setSubmitBehavior (Ljava/lang/String;)V - public fun shouldBlurOnReturn ()Z - public fun shouldSubmitOnReturn ()Z - protected fun showSoftKeyboard ()Z + protected final fun setSettingTextFromJS (Z)V + protected final fun setSettingTextFromState (Z)V + public final fun setStagedInputType (I)V + public final fun setStateWrapper (Lcom/facebook/react/uimanager/StateWrapper;)V + public final fun setSubmitBehavior (Ljava/lang/String;)V + public final fun shouldBlurOnReturn ()Z + public final fun shouldSubmitOnReturn ()Z + protected final fun showSoftKeyboard ()Z protected fun verifyDrawable (Landroid/graphics/drawable/Drawable;)Z } +public final class com/facebook/react/views/textinput/ReactEditText$Companion { + public final fun getDEBUG_MODE ()Z +} + public final class com/facebook/react/views/textinput/ReactTextInputLocalData { public fun (Landroid/widget/EditText;)V public final fun apply (Landroid/widget/EditText;)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java deleted file mode 100644 index 568d50cb1b5..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ /dev/null @@ -1,1444 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.views.textinput; - -import static com.facebook.react.uimanager.UIManagerHelper.getReactContext; - -import android.content.Context; -import android.content.res.Configuration; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Rect; -import android.graphics.Typeface; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.os.Bundle; -import android.text.Editable; -import android.text.InputType; -import android.text.Spannable; -import android.text.SpannableStringBuilder; -import android.text.Spanned; -import android.text.TextPaint; -import android.text.TextUtils; -import android.text.TextWatcher; -import android.text.method.KeyListener; -import android.text.method.QwertyKeyListener; -import android.util.TypedValue; -import android.view.ActionMode; -import android.view.Gravity; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.View; -import android.view.accessibility.AccessibilityNodeInfo; -import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputConnection; -import android.view.inputmethod.InputMethodManager; -import androidx.annotation.Nullable; -import androidx.appcompat.widget.AppCompatEditText; -import androidx.core.util.Predicate; -import androidx.core.view.ViewCompat; -import com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Assertions; -import com.facebook.react.bridge.ReactContext; -import com.facebook.react.bridge.ReactSoftExceptionLogger; -import com.facebook.react.common.ReactConstants; -import com.facebook.react.common.build.ReactBuildConfig; -import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags; -import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlags; -import com.facebook.react.uimanager.BackgroundStyleApplicator; -import com.facebook.react.uimanager.LengthPercentage; -import com.facebook.react.uimanager.LengthPercentageType; -import com.facebook.react.uimanager.PixelUtil; -import com.facebook.react.uimanager.ReactAccessibilityDelegate; -import com.facebook.react.uimanager.StateWrapper; -import com.facebook.react.uimanager.UIManagerModule; -import com.facebook.react.uimanager.common.UIManagerType; -import com.facebook.react.uimanager.common.ViewUtil; -import com.facebook.react.uimanager.events.EventDispatcher; -import com.facebook.react.uimanager.style.BorderRadiusProp; -import com.facebook.react.uimanager.style.BorderStyle; -import com.facebook.react.uimanager.style.LogicalEdge; -import com.facebook.react.uimanager.style.Overflow; -import com.facebook.react.views.text.ReactTextUpdate; -import com.facebook.react.views.text.ReactTypefaceUtils; -import com.facebook.react.views.text.TextAttributes; -import com.facebook.react.views.text.TextLayoutManager; -import com.facebook.react.views.text.internal.span.CustomLetterSpacingSpan; -import com.facebook.react.views.text.internal.span.CustomLineHeightSpan; -import com.facebook.react.views.text.internal.span.CustomStyleSpan; -import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan; -import com.facebook.react.views.text.internal.span.ReactBackgroundColorSpan; -import com.facebook.react.views.text.internal.span.ReactForegroundColorSpan; -import com.facebook.react.views.text.internal.span.ReactSpan; -import com.facebook.react.views.text.internal.span.ReactStrikethroughSpan; -import com.facebook.react.views.text.internal.span.ReactTextPaintHolderSpan; -import com.facebook.react.views.text.internal.span.ReactUnderlineSpan; -import com.facebook.react.views.text.internal.span.TextInlineImageSpan; -import java.util.Objects; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * A wrapper around the EditText that lets us better control what happens when an EditText gets - * focused or blurred, and when to display the soft keyboard and when not to. - * - *

ReactEditTexts have setFocusableInTouchMode set to false automatically because touches on the - * EditText are managed on the JS side. This also removes the nasty side effect that EditTexts have, - * which is that focus is always maintained on one of the EditTexts. - * - *

The wrapper stops the EditText from triggering *TextChanged events, in the case where JS has - * called this explicitly. This is the default behavior on other platforms as well. - */ -public class ReactEditText extends AppCompatEditText { - - private final InputMethodManager mInputMethodManager; - private final String TAG = ReactEditText.class.getSimpleName(); - public static final boolean DEBUG_MODE = ReactBuildConfig.DEBUG && false; - - // This flag is set to true when we set the text of the EditText explicitly. In that case, no - // *TextChanged events should be triggered. This is less expensive than removing the text - // listeners and adding them back again after the text change is completed. - protected boolean mIsSettingTextFromJS; - private final int mDefaultGravityHorizontal; - private final int mDefaultGravityVertical; - - /** A count of events sent to JS or C++. */ - protected int mNativeEventCount; - - private @Nullable CopyOnWriteArrayList mListeners; - private @Nullable TextWatcherDelegator mTextWatcherDelegator; - private int mStagedInputType; - protected boolean mContainsImages; - private @Nullable String mSubmitBehavior = null; - private boolean mDisableFullscreen; - private @Nullable String mReturnKeyType; - private @Nullable SelectionWatcher mSelectionWatcher; - private @Nullable ContentSizeWatcher mContentSizeWatcher; - private @Nullable ScrollWatcher mScrollWatcher; - private InternalKeyListener mKeyListener; - private boolean mDetectScrollMovement = false; - private boolean mOnKeyPress = false; - private TextAttributes mTextAttributes; - private boolean mTypefaceDirty = false; - private @Nullable String mFontFamily = null; - private int mFontWeight = ReactConstants.UNSET; - private int mFontStyle = ReactConstants.UNSET; - private boolean mAutoFocus = false; - private boolean mContextMenuHidden = false; - private boolean mDidAttachToWindow = false; - private boolean mSelectTextOnFocus = false; - private @Nullable String mPlaceholder = null; - private Overflow mOverflow = Overflow.VISIBLE; - - private StateWrapper mStateWrapper = null; - protected boolean mDisableTextDiffing = false; - - protected boolean mIsSettingTextFromState = false; - - private static final KeyListener sKeyListener = QwertyKeyListener.getInstanceForFullKeyboard(); - private @Nullable EventDispatcher mEventDispatcher; - - public ReactEditText(Context context) { - super(context); - if (!ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()) { - setFocusableInTouchMode(false); - } - - mInputMethodManager = - (InputMethodManager) - Assertions.assertNotNull(context.getSystemService(Context.INPUT_METHOD_SERVICE)); - mDefaultGravityHorizontal = - getGravity() & (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK); - mDefaultGravityVertical = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; - mNativeEventCount = 0; - mIsSettingTextFromJS = false; - mDisableFullscreen = false; - mListeners = null; - mTextWatcherDelegator = null; - mStagedInputType = getInputType(); - if (mKeyListener == null) { - mKeyListener = new InternalKeyListener(); - } - mScrollWatcher = null; - mTextAttributes = new TextAttributes(); - - applyTextAttributes(); - - // Turn off hardware acceleration for Oreo (T40484798) - // see https://issuetracker.google.com/issues/67102093 - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - && Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) { - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - - ReactAccessibilityDelegate editTextAccessibilityDelegate = - new ReactAccessibilityDelegate( - this, this.isFocusable(), this.getImportantForAccessibility()) { - @Override - public boolean performAccessibilityAction(View host, int action, Bundle args) { - if (action == AccessibilityNodeInfo.ACTION_CLICK) { - int length = getText().length(); - if (length > 0) { - // For some reason, when you swipe to focus on a text input that already has text in - // it, it clears the selection and resets the cursor to the beginning of the input. - // Since this is not typically (ever?) what you want, let's just explicitly set the - // selection on accessibility click to undo that. - setSelection(length); - } - return ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior() - ? requestFocusProgramatically() - : requestFocusInternal(); - } - return super.performAccessibilityAction(host, action, args); - } - }; - ViewCompat.setAccessibilityDelegate(this, editTextAccessibilityDelegate); - ActionMode.Callback customActionModeCallback = - new ActionMode.Callback() { - /* - * Editor onCreateActionMode adds the cut, copy, paste, share, autofill, - * and paste as plain text items to the context menu. - */ - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - if (mContextMenuHidden) { - return false; - } - menu.removeItem(android.R.id.pasteAsPlainText); - return true; - } - - @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - return true; - } - - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - return false; - } - - @Override - public void onDestroyActionMode(ActionMode mode) {} - }; - setCustomSelectionActionModeCallback(customActionModeCallback); - setCustomInsertionActionModeCallback(customActionModeCallback); - } - - @Override - protected void finalize() { - if (DEBUG_MODE) { - FLog.e(TAG, "finalize[" + getId() + "] delete cached spannable"); - } - TextLayoutManager.deleteCachedSpannableForTag(getId()); - } - - // After the text changes inside an EditText, TextView checks if a layout() has been requested. - // If it has, it will not scroll the text to the end of the new text inserted, but wait for the - // next layout() to be called. However, we do not perform a layout() after a requestLayout(), so - // we need to override isLayoutRequested to force EditText to scroll to the end of the new text - // immediately. - // TODO: t6408636 verify if we should schedule a layout after a View does a requestLayout() - @Override - public boolean isLayoutRequested() { - return false; - } - - @Override - protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - onContentSizeChange(); - if (mSelectTextOnFocus && isFocused()) { - // Explicitly call this method to select text when layout is drawn - selectAll(); - // Prevent text on being selected for next layout pass - mSelectTextOnFocus = false; - } - } - - @Override - public boolean onTouchEvent(MotionEvent ev) { - switch (ev.getAction()) { - case MotionEvent.ACTION_DOWN: - mDetectScrollMovement = true; - // Disallow parent views to intercept touch events, until we can detect if we should be - // capturing these touches or not. - this.getParent().requestDisallowInterceptTouchEvent(true); - break; - case MotionEvent.ACTION_MOVE: - if (mDetectScrollMovement) { - if (!canScrollVertically(-1) - && !canScrollVertically(1) - && !canScrollHorizontally(-1) - && !canScrollHorizontally(1)) { - // We cannot scroll, let parent views take care of these touches. - this.getParent().requestDisallowInterceptTouchEvent(false); - } - mDetectScrollMovement = false; - } - break; - } - return super.onTouchEvent(ev); - } - - // Consume 'Enter' key events: TextView tries to give focus to the next TextInput, but it can't - // since we only allow JS to change focus, which in turn causes TextView to crash. - @Override - public boolean onKeyUp(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_ENTER && !isMultiline()) { - hideSoftKeyboard(); - return true; - } - return super.onKeyUp(keyCode, event); - } - - @Override - public void setLineHeight(int lineHeight) { - mTextAttributes.setLineHeight(lineHeight); - // We don't call super.setLineHeight() because LineHeight is fully managed by ReactNative - } - - @Override - protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { - super.onScrollChanged(horiz, vert, oldHoriz, oldVert); - - if (mScrollWatcher != null) { - mScrollWatcher.onScrollChanged(horiz, vert, oldHoriz, oldVert); - } - } - - @Override - public InputConnection onCreateInputConnection(EditorInfo outAttrs) { - ReactContext reactContext = getReactContext(this); - InputConnection inputConnection = super.onCreateInputConnection(outAttrs); - if (inputConnection != null && mOnKeyPress) { - inputConnection = - new ReactEditTextInputConnectionWrapper( - inputConnection, reactContext, this, mEventDispatcher); - } - - if (isMultiline() && (shouldBlurOnReturn() || shouldSubmitOnReturn())) { - // Remove IME_FLAG_NO_ENTER_ACTION to keep the original IME_OPTION - outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; - } - return inputConnection; - } - - /* - * Called when a context menu option for the text view is selected. - * React Native replaces copy (as rich text) with copy as plain text. - */ - @Override - public boolean onTextContextMenuItem(int id) { - if (id == android.R.id.paste) { - id = android.R.id.pasteAsPlainText; - } - return super.onTextContextMenuItem(id); - } - - @Override - public void clearFocus() { - boolean useStockFocusBehavior = ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior(); - if (!useStockFocusBehavior) { - setFocusableInTouchMode(false); - } - super.clearFocus(); - hideSoftKeyboard(); - } - - @Override - public boolean requestFocus(int direction, Rect previouslyFocusedRect) { - // This is a no-op so that when the OS calls requestFocus(), nothing will happen. ReactEditText - // is a controlled component, which means its focus is controlled by JS, with two exceptions: - // autofocus when it's attached to the window, and responding to accessibility events. In both - // of these cases, we call requestFocusInternal() directly. - return ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior() - ? super.requestFocus(direction, previouslyFocusedRect) - : isFocused(); - } - - private boolean requestFocusInternal() { - setFocusableInTouchMode(true); - // We must explicitly call this method on the super class; if we call requestFocus() without - // any arguments, it will call into the overridden requestFocus(int, Rect) above, which no-ops. - boolean focused = super.requestFocus(View.FOCUS_DOWN, null); - if (getShowSoftInputOnFocus()) { - showSoftKeyboard(); - } - - return focused; - } - - // For cases like autoFocus, or ref.focus() where we request focus programatically and not through - // interacting with the EditText directly (like clicking on it). We cannot use stock - // requestFocus() because it will not pop up the soft keyboard, only clicking the input will do - // that. This method will eventually replace requestFocusInternal() - private boolean requestFocusProgramatically() { - boolean focused = super.requestFocus(View.FOCUS_DOWN, null); - if (isInTouchMode() && getShowSoftInputOnFocus()) { - showSoftKeyboard(); - } - - return focused; - } - - @Override - public void addTextChangedListener(TextWatcher watcher) { - if (mListeners == null) { - mListeners = new CopyOnWriteArrayList<>(); - super.addTextChangedListener(getTextWatcherDelegator()); - } - - mListeners.add(watcher); - } - - @Override - public void removeTextChangedListener(TextWatcher watcher) { - if (mListeners != null) { - mListeners.remove(watcher); - - if (mListeners.isEmpty()) { - mListeners = null; - super.removeTextChangedListener(getTextWatcherDelegator()); - } - } - } - - public void setContentSizeWatcher(@Nullable ContentSizeWatcher contentSizeWatcher) { - mContentSizeWatcher = contentSizeWatcher; - } - - public void setScrollWatcher(@Nullable ScrollWatcher scrollWatcher) { - mScrollWatcher = scrollWatcher; - } - - /** - * Attempt to set a selection or fail silently. Intentionally meant to handle bad inputs. - * EventCounter is the same one used as with text. - * - * @param eventCounter - * @param start - * @param end - */ - public void maybeSetSelection(int eventCounter, int start, int end) { - if (!canUpdateWithEventCount(eventCounter)) { - return; - } - - maybeSetSelection(start, end); - } - - private void maybeSetSelection(int start, int end) { - if (start != ReactConstants.UNSET && end != ReactConstants.UNSET) { - // clamp selection values for safety - start = clampToTextLength(start); - end = clampToTextLength(end); - - setSelection(start, end); - } - } - - private int clampToTextLength(int value) { - int textLength = getText() == null ? 0 : getText().length(); - - return Math.max(0, Math.min(value, textLength)); - } - - @Override - public void setSelection(int start, int end) { - if (DEBUG_MODE) { - FLog.e(TAG, "setSelection[" + getId() + "]: " + start + " " + end); - } - super.setSelection(start, end); - } - - @Override - protected void onSelectionChanged(int selStart, int selEnd) { - if (DEBUG_MODE) { - FLog.e(TAG, "onSelectionChanged[" + getId() + "]: " + selStart + " " + selEnd); - } - - super.onSelectionChanged(selStart, selEnd); - if (mSelectionWatcher != null && hasFocus()) { - mSelectionWatcher.onSelectionChanged(selStart, selEnd); - } - } - - @Override - protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { - super.onFocusChanged(focused, direction, previouslyFocusedRect); - if (focused && mSelectionWatcher != null) { - mSelectionWatcher.onSelectionChanged(getSelectionStart(), getSelectionEnd()); - } - } - - public void setSelectionWatcher(@Nullable SelectionWatcher selectionWatcher) { - mSelectionWatcher = selectionWatcher; - } - - public void setOnKeyPress(boolean onKeyPress) { - mOnKeyPress = onKeyPress; - } - - public boolean shouldBlurOnReturn() { - String submitBehavior = getSubmitBehavior(); - boolean shouldBlur; - - // Default shouldBlur - if (submitBehavior == null) { - if (!isMultiline()) { - shouldBlur = true; - } else { - shouldBlur = false; - } - } else { - shouldBlur = submitBehavior.equals("blurAndSubmit"); - } - - return shouldBlur; - } - - public boolean shouldSubmitOnReturn() { - String submitBehavior = getSubmitBehavior(); - boolean shouldSubmit; - - // Default shouldSubmit - if (submitBehavior == null) { - if (!isMultiline()) { - shouldSubmit = true; - } else { - shouldSubmit = false; - } - } else { - shouldSubmit = submitBehavior.equals("submit") || submitBehavior.equals("blurAndSubmit"); - } - - return shouldSubmit; - } - - public String getSubmitBehavior() { - return mSubmitBehavior; - } - - public void setSubmitBehavior(@Nullable String submitBehavior) { - mSubmitBehavior = submitBehavior; - } - - public void setDisableFullscreenUI(boolean disableFullscreenUI) { - mDisableFullscreen = disableFullscreenUI; - updateImeOptions(); - } - - public boolean getDisableFullscreenUI() { - return mDisableFullscreen; - } - - public void setReturnKeyType(String returnKeyType) { - mReturnKeyType = returnKeyType; - updateImeOptions(); - } - - public String getReturnKeyType() { - return mReturnKeyType; - } - - /*protected*/ int getStagedInputType() { - return mStagedInputType; - } - - /*package*/ void setStagedInputType(int stagedInputType) { - mStagedInputType = stagedInputType; - } - - /*package*/ void commitStagedInputType() { - if (getInputType() != mStagedInputType) { - int selectionStart = getSelectionStart(); - int selectionEnd = getSelectionEnd(); - setInputType(mStagedInputType); - // Restore the selection - maybeSetSelection(selectionStart, selectionEnd); - } - } - - @Override - public void setInputType(int type) { - Typeface tf = super.getTypeface(); - super.setInputType(type); - mStagedInputType = type; - // Input type password defaults to monospace font, so we need to re-apply the font - super.setTypeface(tf); - - /** - * If set forces multiline on input, because of a restriction on Android source that enables - * multiline only for inputs of type Text and Multiline on method {@link - * android.widget.TextView#isMultilineInputType(int)}} Source: {@Link TextView.java} - */ - if (isMultiline()) { - setSingleLine(false); - } - - // We override the KeyListener so that all keys on the soft input keyboard as well as hardware - // keyboards work. Some KeyListeners like DigitsKeyListener will display the keyboard but not - // accept all input from it - if (mKeyListener == null) { - mKeyListener = new InternalKeyListener(); - } - - mKeyListener.setInputType(type); - setKeyListener(mKeyListener); - } - - public void setPlaceholder(@Nullable String placeholder) { - if (!Objects.equals(placeholder, mPlaceholder)) { - mPlaceholder = placeholder; - setHint(placeholder); - } - } - - public void setFontFamily(String fontFamily) { - mFontFamily = fontFamily; - mTypefaceDirty = true; - } - - public void setFontWeight(String fontWeightString) { - int fontWeight = ReactTypefaceUtils.parseFontWeight(fontWeightString); - if (fontWeight != mFontWeight) { - mFontWeight = fontWeight; - mTypefaceDirty = true; - } - } - - public void setFontStyle(String fontStyleString) { - int fontStyle = ReactTypefaceUtils.parseFontStyle(fontStyleString); - if (fontStyle != mFontStyle) { - mFontStyle = fontStyle; - mTypefaceDirty = true; - } - } - - @Override - public void setFontFeatureSettings(String fontFeatureSettings) { - if (!Objects.equals(fontFeatureSettings, getFontFeatureSettings())) { - super.setFontFeatureSettings(fontFeatureSettings); - mTypefaceDirty = true; - } - } - - public void maybeUpdateTypeface() { - if (!mTypefaceDirty) { - return; - } - - mTypefaceDirty = false; - - Typeface newTypeface = - ReactTypefaceUtils.applyStyles( - getTypeface(), mFontStyle, mFontWeight, mFontFamily, getContext().getAssets()); - setTypeface(newTypeface); - - // Match behavior of CustomStyleSpan and enable SUBPIXEL_TEXT_FLAG when setting anything - // nonstandard - if (mFontStyle != ReactConstants.UNSET - || mFontWeight != ReactConstants.UNSET - || mFontFamily != null - || getFontFeatureSettings() != null) { - setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); - } else { - setPaintFlags(getPaintFlags() & (~Paint.SUBPIXEL_TEXT_FLAG)); - } - } - - public void requestFocusFromJS() { - if (ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()) { - requestFocusProgramatically(); - } else { - requestFocusInternal(); - } - } - - /* package */ void clearFocusFromJS() { - clearFocus(); - } - - public int incrementAndGetEventCounter() { - return ++mNativeEventCount; - } - - public void maybeSetTextFromJS(ReactTextUpdate reactTextUpdate) { - mIsSettingTextFromJS = true; - maybeSetText(reactTextUpdate); - mIsSettingTextFromJS = false; - } - - public void maybeSetTextFromState(ReactTextUpdate reactTextUpdate) { - mIsSettingTextFromState = true; - maybeSetText(reactTextUpdate); - mIsSettingTextFromState = false; - } - - public boolean canUpdateWithEventCount(int eventCounter) { - return eventCounter >= mNativeEventCount; - } - - private void maybeSetText(ReactTextUpdate reactTextUpdate) { - if (isSecureText() && TextUtils.equals(getText(), reactTextUpdate.getText())) { - return; - } - - // Only set the text if it is up to date. - if (!canUpdateWithEventCount(reactTextUpdate.getJsEventCounter())) { - return; - } - - if (DEBUG_MODE) { - FLog.e( - TAG, - "maybeSetText[" - + getId() - + "]: current text: " - + getText() - + " update: " - + reactTextUpdate.getText()); - } - - // The current text gets replaced with the text received from JS. However, the spans on the - // current text need to be adapted to the new text. Since TextView#setText() will remove or - // reset some of these spans even if they are set directly, SpannableStringBuilder#replace() is - // used instead (this is also used by the keyboard implementation underneath the covers). - SpannableStringBuilder spannableStringBuilder = - new SpannableStringBuilder(reactTextUpdate.getText()); - - manageSpans(spannableStringBuilder); - stripStyleEquivalentSpans(spannableStringBuilder); - - mContainsImages = reactTextUpdate.containsImages(); - - // When we update text, we trigger onChangeText code that will - // try to update state if the wrapper is available. Temporarily disable - // to prevent an (asynchronous) infinite loop. - mDisableTextDiffing = true; - - // On some devices, when the text is cleared, buggy keyboards will not clear the composing - // text so, we have to set text to null, which will clear the currently composing text. - if (reactTextUpdate.getText().length() == 0) { - setText(null); - } else { - // When we update text, we trigger onChangeText code that will - // try to update state if the wrapper is available. Temporarily disable - // to prevent an infinite loop. - getText().replace(0, length(), spannableStringBuilder); - } - mDisableTextDiffing = false; - - if (getBreakStrategy() != reactTextUpdate.getTextBreakStrategy()) { - setBreakStrategy(reactTextUpdate.getTextBreakStrategy()); - } - - // Update cached spans (in Fabric only). - updateCachedSpannable(); - } - - /** - * Remove and/or add {@link Spanned.SPAN_EXCLUSIVE_EXCLUSIVE} spans, since they should only exist - * as long as the text they cover is the same. All other spans will remain the same, since they - * will adapt to the new text, hence why {@link SpannableStringBuilder#replace} never removes - * them. - */ - private void manageSpans(SpannableStringBuilder spannableStringBuilder) { - Object[] spans = getText().getSpans(0, length(), Object.class); - for (int spanIdx = 0; spanIdx < spans.length; spanIdx++) { - Object span = spans[spanIdx]; - int spanFlags = getText().getSpanFlags(span); - boolean isExclusiveExclusive = - (spanFlags & Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) == Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; - - // Remove all styling spans we might have previously set - if (span instanceof ReactSpan) { - getText().removeSpan(span); - } - - // We only add spans back for EXCLUSIVE_EXCLUSIVE spans - if (!isExclusiveExclusive) { - continue; - } - - final int spanStart = getText().getSpanStart(span); - final int spanEnd = getText().getSpanEnd(span); - - // Make sure the span is removed from existing text, otherwise the spans we set will be - // ignored or it will cover text that has changed. - getText().removeSpan(span); - if (sameTextForSpan(getText(), spannableStringBuilder, spanStart, spanEnd)) { - spannableStringBuilder.setSpan(span, spanStart, spanEnd, spanFlags); - } - } - } - - /** - * Remove spans from the SpannableStringBuilder which can be represented by TextAppearance - * attributes on the underlying EditText. This works around instability on Samsung devices with - * the presence of spans https://github.com/facebook/react-native/issues/35936 (S318090) - */ - private void stripStyleEquivalentSpans(SpannableStringBuilder sb) { - stripSpansOfKind( - sb, - ReactAbsoluteSizeSpan.class, - (span) -> span.getSize() == mTextAttributes.getEffectiveFontSize()); - - stripSpansOfKind( - sb, - ReactBackgroundColorSpan.class, - (span) -> - Objects.equals( - span.getBackgroundColor(), BackgroundStyleApplicator.getBackgroundColor(this))); - - stripSpansOfKind( - sb, - ReactForegroundColorSpan.class, - (span) -> span.getForegroundColor() == getCurrentTextColor()); - - stripSpansOfKind( - sb, - ReactStrikethroughSpan.class, - (span) -> (getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) != 0); - - stripSpansOfKind( - sb, ReactUnderlineSpan.class, (span) -> (getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) != 0); - - stripSpansOfKind( - sb, - CustomLetterSpacingSpan.class, - (span) -> span.getSpacing() == mTextAttributes.getEffectiveLetterSpacing()); - - stripSpansOfKind( - sb, - CustomStyleSpan.class, - (span) -> { - return span.getStyle() == mFontStyle - && Objects.equals(span.getFontFamily(), mFontFamily) - && span.getWeight() == mFontWeight - && Objects.equals(span.getFontFeatureSettings(), getFontFeatureSettings()); - }); - } - - private void stripSpansOfKind( - SpannableStringBuilder sb, Class clazz, Predicate shouldStrip) { - T[] spans = sb.getSpans(0, sb.length(), clazz); - - for (T span : spans) { - if (shouldStrip.test(span)) { - sb.removeSpan(span); - } - } - } - - /** - * Copy styles represented as attributes to the underlying span, for later measurement or other - * usage outside the ReactEditText. - */ - private void addSpansFromStyleAttributes(SpannableStringBuilder workingText) { - int spanFlags = Spannable.SPAN_INCLUSIVE_INCLUSIVE; - - // Set all bits for SPAN_PRIORITY so that this span has the highest possible priority - // (least precedence). This ensures the span is behind any overlapping spans. - spanFlags |= Spannable.SPAN_PRIORITY; - - workingText.setSpan( - new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()), - 0, - workingText.length(), - spanFlags); - - workingText.setSpan( - new ReactForegroundColorSpan(getCurrentTextColor()), 0, workingText.length(), spanFlags); - - @Nullable Integer backgroundColor = BackgroundStyleApplicator.getBackgroundColor(this); - if (backgroundColor != null && backgroundColor != Color.TRANSPARENT) { - workingText.setSpan( - new ReactBackgroundColorSpan(backgroundColor), 0, workingText.length(), spanFlags); - } - - if ((getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) != 0) { - workingText.setSpan(new ReactStrikethroughSpan(), 0, workingText.length(), spanFlags); - } - - if ((getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) != 0) { - workingText.setSpan(new ReactUnderlineSpan(), 0, workingText.length(), spanFlags); - } - - float effectiveLetterSpacing = mTextAttributes.getEffectiveLetterSpacing(); - if (!Float.isNaN(effectiveLetterSpacing)) { - workingText.setSpan( - new CustomLetterSpacingSpan(effectiveLetterSpacing), 0, workingText.length(), spanFlags); - } - - if (mFontStyle != ReactConstants.UNSET - || mFontWeight != ReactConstants.UNSET - || mFontFamily != null - || getFontFeatureSettings() != null) { - workingText.setSpan( - new CustomStyleSpan( - mFontStyle, - mFontWeight, - getFontFeatureSettings(), - mFontFamily, - getContext().getAssets()), - 0, - workingText.length(), - spanFlags); - } - - float lineHeight = mTextAttributes.getEffectiveLineHeight(); - if (!Float.isNaN(lineHeight)) { - workingText.setSpan(new CustomLineHeightSpan(lineHeight), 0, workingText.length(), spanFlags); - } - } - - private static boolean sameTextForSpan( - final Editable oldText, - final SpannableStringBuilder newText, - final int start, - final int end) { - if (start > newText.length() || end > newText.length()) { - return false; - } - for (int charIdx = start; charIdx < end; charIdx++) { - if (oldText.charAt(charIdx) != newText.charAt(charIdx)) { - return false; - } - } - return true; - } - - protected boolean showSoftKeyboard() { - return mInputMethodManager.showSoftInput(this, 0); - } - - protected void hideSoftKeyboard() { - mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); - } - - private TextWatcherDelegator getTextWatcherDelegator() { - if (mTextWatcherDelegator == null) { - mTextWatcherDelegator = new TextWatcherDelegator(); - } - return mTextWatcherDelegator; - } - - /* package */ boolean isMultiline() { - return (getInputType() & InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0; - } - - private boolean isSecureText() { - return (getInputType() - & (InputType.TYPE_NUMBER_VARIATION_PASSWORD | InputType.TYPE_TEXT_VARIATION_PASSWORD)) - != 0; - } - - private void onContentSizeChange() { - if (mContentSizeWatcher != null) { - mContentSizeWatcher.onLayout(); - } - - setIntrinsicContentSize(); - } - - // TODO T58784068: delete this method - private void setIntrinsicContentSize() { - // This serves as a check for whether we're running under Paper or Fabric. - // By the time this is called, in Fabric we will have a state - // wrapper 100% of the time. - // Since the LocalData object is constructed by getting values from the underlying EditText - // view, we don't need to construct one or apply it at all - it provides no use in Fabric. - ReactContext reactContext = getReactContext(this); - - if (!ReactBuildConfig.UNSTABLE_ENABLE_MINIFY_LEGACY_ARCHITECTURE) { - if (mStateWrapper == null && !reactContext.isBridgeless()) { - - final ReactTextInputLocalData localData = new ReactTextInputLocalData(this); - UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); - if (uiManager != null) { - uiManager.setViewLocalData(getId(), localData); - } - } - } - } - - /* package */ int getGravityHorizontal() { - return getGravity() - & (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK); - } - - /* package */ void setGravityHorizontal(int gravityHorizontal) { - if (gravityHorizontal == 0) { - gravityHorizontal = mDefaultGravityHorizontal; - } - setGravity( - (getGravity() - & ~Gravity.HORIZONTAL_GRAVITY_MASK - & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) - | gravityHorizontal); - } - - /* package */ void setGravityVertical(int gravityVertical) { - if (gravityVertical == 0) { - gravityVertical = mDefaultGravityVertical; - } - setGravity((getGravity() & ~Gravity.VERTICAL_GRAVITY_MASK) | gravityVertical); - } - - private void updateImeOptions() { - // Default to IME_ACTION_DONE - int returnKeyFlag = EditorInfo.IME_ACTION_DONE; - if (mReturnKeyType != null) { - switch (mReturnKeyType) { - case "go": - returnKeyFlag = EditorInfo.IME_ACTION_GO; - break; - case "next": - returnKeyFlag = EditorInfo.IME_ACTION_NEXT; - break; - case "none": - returnKeyFlag = EditorInfo.IME_ACTION_NONE; - break; - case "previous": - returnKeyFlag = EditorInfo.IME_ACTION_PREVIOUS; - break; - case "search": - returnKeyFlag = EditorInfo.IME_ACTION_SEARCH; - break; - case "send": - returnKeyFlag = EditorInfo.IME_ACTION_SEND; - break; - case "done": - returnKeyFlag = EditorInfo.IME_ACTION_DONE; - break; - } - } - - if (mDisableFullscreen) { - setImeOptions(returnKeyFlag | EditorInfo.IME_FLAG_NO_FULLSCREEN); - } else { - setImeOptions(returnKeyFlag); - } - } - - @Override - protected boolean verifyDrawable(Drawable drawable) { - if (mContainsImages) { - Spanned text = getText(); - TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class); - for (TextInlineImageSpan span : spans) { - if (span.getDrawable() == drawable) { - return true; - } - } - } - return super.verifyDrawable(drawable); - } - - @Override - public void invalidateDrawable(Drawable drawable) { - if (mContainsImages) { - Spanned text = getText(); - TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class); - for (TextInlineImageSpan span : spans) { - if (span.getDrawable() == drawable) { - invalidate(); - } - } - } - super.invalidateDrawable(drawable); - } - - @Override - public void onDetachedFromWindow() { - super.onDetachedFromWindow(); - if (mContainsImages) { - Spanned text = getText(); - TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class); - for (TextInlineImageSpan span : spans) { - span.onDetachedFromWindow(); - } - } - } - - @Override - public void onStartTemporaryDetach() { - super.onStartTemporaryDetach(); - if (mContainsImages) { - Spanned text = getText(); - TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class); - for (TextInlineImageSpan span : spans) { - span.onStartTemporaryDetach(); - } - } - } - - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - - if (ReactNativeNewArchitectureFeatureFlags.enableBridgelessArchitecture() - && ReactNativeFeatureFlags.enableFontScaleChangesUpdatingLayout()) { - applyTextAttributes(); - } - } - - @Override - public void onAttachedToWindow() { - super.onAttachedToWindow(); - - int selectionStart = getSelectionStart(); - int selectionEnd = getSelectionEnd(); - - // Used to ensure that text is selectable inside of removeClippedSubviews - // See https://github.com/facebook/react-native/issues/6805 for original - // fix that was ported to here. - - super.setTextIsSelectable(true); - - // Restore the selection since `setTextIsSelectable` changed it. - maybeSetSelection(selectionStart, selectionEnd); - - if (mContainsImages) { - Spanned text = getText(); - TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class); - for (TextInlineImageSpan span : spans) { - span.onAttachedToWindow(); - } - } - - if (mAutoFocus && !mDidAttachToWindow) { - if (ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()) { - requestFocusProgramatically(); - } else { - requestFocusInternal(); - } - } - - mDidAttachToWindow = true; - } - - @Override - public void onFinishTemporaryDetach() { - super.onFinishTemporaryDetach(); - if (mContainsImages) { - Spanned text = getText(); - TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class); - for (TextInlineImageSpan span : spans) { - span.onFinishTemporaryDetach(); - } - } - } - - @Override - public void setBackgroundColor(int color) { - BackgroundStyleApplicator.setBackgroundColor(this, color); - } - - public void setBorderWidth(int position, float width) { - BackgroundStyleApplicator.setBorderWidth( - this, LogicalEdge.values()[position], PixelUtil.toDIPFromPixel(width)); - } - - public void setBorderColor(int position, @Nullable Integer color) { - BackgroundStyleApplicator.setBorderColor(this, LogicalEdge.values()[position], color); - } - - public int getBorderColor(int position) { - @Nullable - Integer borderColor = - BackgroundStyleApplicator.getBorderColor(this, LogicalEdge.values()[position]); - return borderColor == null ? Color.TRANSPARENT : borderColor; - } - - public void setBorderRadius(float borderRadius) { - setBorderRadius(borderRadius, BorderRadiusProp.BORDER_RADIUS.ordinal()); - } - - public void setBorderRadius(float borderRadius, int position) { - @Nullable - LengthPercentage radius = - Float.isNaN(borderRadius) - ? null - : new LengthPercentage( - PixelUtil.toDIPFromPixel(borderRadius), LengthPercentageType.POINT); - BackgroundStyleApplicator.setBorderRadius(this, BorderRadiusProp.values()[position], radius); - } - - public void setBorderStyle(@Nullable String style) { - BackgroundStyleApplicator.setBorderStyle( - this, style == null ? null : BorderStyle.fromString(style)); - } - - public void setLetterSpacingPt(float letterSpacingPt) { - mTextAttributes.setLetterSpacing(letterSpacingPt); - applyTextAttributes(); - } - - public void setAllowFontScaling(boolean allowFontScaling) { - if (mTextAttributes.getAllowFontScaling() != allowFontScaling) { - mTextAttributes.setAllowFontScaling(allowFontScaling); - applyTextAttributes(); - } - } - - public void setFontSize(float fontSize) { - mTextAttributes.setFontSize(fontSize); - applyTextAttributes(); - } - - public void setMaxFontSizeMultiplier(float maxFontSizeMultiplier) { - if (maxFontSizeMultiplier != mTextAttributes.getMaxFontSizeMultiplier()) { - mTextAttributes.setMaxFontSizeMultiplier(maxFontSizeMultiplier); - applyTextAttributes(); - } - } - - public void setAutoFocus(boolean autoFocus) { - mAutoFocus = autoFocus; - } - - public void setSelectTextOnFocus(boolean selectTextOnFocus) { - super.setSelectAllOnFocus(selectTextOnFocus); - mSelectTextOnFocus = selectTextOnFocus; - } - - public void setContextMenuHidden(boolean contextMenuHidden) { - mContextMenuHidden = contextMenuHidden; - } - - protected void applyTextAttributes() { - // In general, the `getEffective*` functions return `Float.NaN` if the - // property hasn't been set. - - // `getEffectiveFontSize` always returns a value so don't need to check for anything like - // `Float.NaN`. - setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextAttributes.getEffectiveFontSize()); - - float effectiveLetterSpacing = mTextAttributes.getEffectiveLetterSpacing(); - if (!Float.isNaN(effectiveLetterSpacing)) { - setLetterSpacing(effectiveLetterSpacing); - } - } - - @Nullable - public StateWrapper getStateWrapper() { - return mStateWrapper; - } - - public void setStateWrapper(StateWrapper stateWrapper) { - mStateWrapper = stateWrapper; - } - - /** - * Update the cached Spannable used in TextLayoutManager to measure the text in Fabric. This is - * mostly copied from ReactTextInputShadowNode.java (the non-Fabric version) and - * TextLayoutManager.java with some very minor modifications. There's some duplication between - * here and TextLayoutManager, so there might be an opportunity for refactor. - */ - private void updateCachedSpannable() { - // Noops in non-Fabric - if (mStateWrapper == null) { - return; - } - // If this view doesn't have an ID yet, we don't have a cache key, so bail here - if (getId() == -1) { - return; - } - - Editable currentText = getText(); - boolean haveText = currentText != null && currentText.length() > 0; - - SpannableStringBuilder sb = new SpannableStringBuilder(); - - // A note of caution: appending currentText to sb appends all the spans of currentText - not - // copies of the Spans, but the actual span objects. Any modifications to sb after that point - // can modify the spans of sb/currentText, impact the text or spans visible on screen, and - // also call the TextChangeWatcher methods. - if (haveText) { - // This is here as a workaround for T76236115, which looks like this: - // Hopefully we can delete all this stuff if we can get rid of the soft errors. - // - android.text.SpannableStringBuilder.charAt (SpannableStringBuilder.java:123) - // - android.text.CharSequenceCharacterIterator.current - // (CharSequenceCharacterIterator.java:58) - // - android.text.CharSequenceCharacterIterator.setIndex - // (CharSequenceCharacterIterator.java:83) - // - android.icu.text.RuleBasedBreakIterator.CISetIndex32 (RuleBasedBreakIterator.java:1126) - // - android.icu.text.RuleBasedBreakIterator.isBoundary (RuleBasedBreakIterator.java:503) - // - android.text.method.WordIterator.isBoundary (WordIterator.java:95) - // - android.widget.Editor$SelectionHandleView.positionAtCursorOffset (Editor.java:6666) - // - android.widget.Editor$HandleView.invalidate (Editor.java:5241) - // - android.widget.Editor$SelectionModifierCursorController.invalidateHandles - // (Editor.java:7442) - // - android.widget.Editor.invalidateHandlesAndActionMode (Editor.java:2112) - // - android.widget.TextView.spanChange (TextView.java:11189) - // - android.widget.TextView$ChangeWatcher.onSpanAdded (TextView.java:14189) - // - android.text.SpannableStringBuilder.sendSpanAdded (SpannableStringBuilder.java:1283) - // - android.text.SpannableStringBuilder.sendToSpanWatchers (SpannableStringBuilder.java:663) - // - android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:579) - // - android.text.SpannableStringBuilder.append (SpannableStringBuilder.java:269) - // - ReactEditText.updateCachedSpannable (ReactEditText.java:995) - // - ReactEditText$TextWatcherDelegator.onTextChanged (ReactEditText.java:1044) - // - android.widget.TextView.sendOnTextChanged (TextView.java:10972) - // ... - // - android.text.method.BaseKeyListener.onKeyDown (BaseKeyListener.java:479) - // - android.text.method.QwertyKeyListener.onKeyDown (QwertyKeyListener.java:362) - // - ReactEditText$InternalKeyListener.onKeyDown (ReactEditText.java:1094) - // ... - // - android.app.Activity.dispatchKeyEvent (Activity.java:3447) - try { - sb.append(currentText.subSequence(0, currentText.length())); - } catch (IndexOutOfBoundsException e) { - ReactSoftExceptionLogger.logSoftException(TAG, e); - } - } - - // If we don't have text, make sure we have *something* to measure. - // Hint has the same dimensions - the only thing that's different is background or foreground - // color - if (!haveText) { - if (getHint() != null && getHint().length() > 0) { - sb.append(getHint()); - } else if (ViewUtil.getUIManagerType(this) != UIManagerType.FABRIC) { - // Measure something so we have correct height, even if there's no string. - sb.append("I"); - } - } - - addSpansFromStyleAttributes(sb); - sb.setSpan( - new ReactTextPaintHolderSpan(new TextPaint(getPaint())), - 0, - sb.length(), - Spannable.SPAN_INCLUSIVE_INCLUSIVE); - TextLayoutManager.setCachedSpannableForTag(getId(), sb); - } - - void setEventDispatcher(@Nullable EventDispatcher eventDispatcher) { - mEventDispatcher = eventDispatcher; - } - - public void setOverflow(@Nullable String overflow) { - if (overflow == null) { - mOverflow = Overflow.VISIBLE; - } else { - @Nullable Overflow parsedOverflow = Overflow.fromString(overflow); - mOverflow = parsedOverflow == null ? Overflow.VISIBLE : parsedOverflow; - } - - invalidate(); - } - - @Override - public void onDraw(Canvas canvas) { - if (mOverflow != Overflow.VISIBLE) { - BackgroundStyleApplicator.clipToPaddingBox(this, canvas); - } - - super.onDraw(canvas); - } - - /** - * This class will redirect *TextChanged calls to the listeners only in the case where the text is - * changed by the user, and not explicitly set by JS. - */ - private class TextWatcherDelegator implements TextWatcher { - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - if (!mIsSettingTextFromJS && mListeners != null) { - for (TextWatcher listener : mListeners) { - listener.beforeTextChanged(s, start, count, after); - } - } - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - if (DEBUG_MODE) { - FLog.e( - TAG, "onTextChanged[" + getId() + "]: " + s + " " + start + " " + before + " " + count); - } - - if (!mIsSettingTextFromJS && mListeners != null) { - for (TextWatcher listener : mListeners) { - listener.onTextChanged(s, start, before, count); - } - } - - updateCachedSpannable(); - - onContentSizeChange(); - } - - @Override - public void afterTextChanged(Editable s) { - if (!mIsSettingTextFromJS && mListeners != null) { - for (TextWatcher listener : mListeners) { - listener.afterTextChanged(s); - } - } - } - } - - /* - * This class is set as the KeyListener for the underlying TextView - * It does two things - * 1) Provides the same answer to getInputType() as the real KeyListener would have which allows - * the proper keyboard to pop up on screen - * 2) Permits all keyboard input through - */ - private static class InternalKeyListener implements KeyListener { - - private int mInputType = 0; - - public InternalKeyListener() {} - - public void setInputType(int inputType) { - mInputType = inputType; - } - - /* - * getInputType will return whatever value is passed in. This will allow the proper keyboard - * to be shown on screen but without the actual filtering done by other KeyListeners - */ - @Override - public int getInputType() { - return mInputType; - } - - /* - * All overrides of key handling defer to the underlying KeyListener which is shared by all - * ReactEditText instances. It will basically allow any/all keyboard input whether from - * physical keyboard or from soft input. - */ - @Override - public boolean onKeyDown(View view, Editable text, int keyCode, KeyEvent event) { - return sKeyListener.onKeyDown(view, text, keyCode, event); - } - - @Override - public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) { - return sKeyListener.onKeyUp(view, text, keyCode, event); - } - - @Override - public boolean onKeyOther(View view, Editable text, KeyEvent event) { - return sKeyListener.onKeyOther(view, text, event); - } - - @Override - public void clearMetaKeyState(View view, Editable content, int states) { - sKeyListener.clearMetaKeyState(view, content, states); - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt new file mode 100644 index 00000000000..3c9e4cbba28 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -0,0 +1,1288 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.textinput + +import android.annotation.SuppressLint +import android.content.Context +import android.content.res.Configuration +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.os.Build +import android.os.Bundle +import android.text.Editable +import android.text.InputType +import android.text.Spannable +import android.text.SpannableStringBuilder +import android.text.Spanned +import android.text.TextPaint +import android.text.TextWatcher +import android.text.method.KeyListener +import android.text.method.QwertyKeyListener +import android.util.TypedValue +import android.view.ActionMode +import android.view.Gravity +import android.view.KeyEvent +import android.view.Menu +import android.view.MenuItem +import android.view.MotionEvent +import android.view.View +import android.view.accessibility.AccessibilityNodeInfo +import android.view.inputmethod.EditorInfo +import android.view.inputmethod.InputConnection +import android.view.inputmethod.InputMethodManager +import androidx.appcompat.widget.AppCompatEditText +import androidx.core.util.Predicate +import androidx.core.view.ViewCompat +import com.facebook.common.logging.FLog +import com.facebook.react.bridge.ReactSoftExceptionLogger.logSoftException +import com.facebook.react.common.ReactConstants +import com.facebook.react.common.build.ReactBuildConfig +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior +import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlags +import com.facebook.react.uimanager.BackgroundStyleApplicator.clipToPaddingBox +import com.facebook.react.uimanager.BackgroundStyleApplicator.getBackgroundColor +import com.facebook.react.uimanager.BackgroundStyleApplicator.getBorderColor +import com.facebook.react.uimanager.BackgroundStyleApplicator.setBackgroundColor +import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderColor +import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderRadius +import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderStyle +import com.facebook.react.uimanager.BackgroundStyleApplicator.setBorderWidth +import com.facebook.react.uimanager.LengthPercentage +import com.facebook.react.uimanager.LengthPercentageType +import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel +import com.facebook.react.uimanager.ReactAccessibilityDelegate +import com.facebook.react.uimanager.StateWrapper +import com.facebook.react.uimanager.UIManagerHelper +import com.facebook.react.uimanager.UIManagerModule +import com.facebook.react.uimanager.common.UIManagerType +import com.facebook.react.uimanager.common.ViewUtil.getUIManagerType +import com.facebook.react.uimanager.events.EventDispatcher +import com.facebook.react.uimanager.style.BorderRadiusProp +import com.facebook.react.uimanager.style.BorderStyle +import com.facebook.react.uimanager.style.LogicalEdge +import com.facebook.react.uimanager.style.Overflow +import com.facebook.react.views.text.ReactTextUpdate +import com.facebook.react.views.text.ReactTypefaceUtils.applyStyles +import com.facebook.react.views.text.ReactTypefaceUtils.parseFontStyle +import com.facebook.react.views.text.ReactTypefaceUtils.parseFontWeight +import com.facebook.react.views.text.TextAttributes +import com.facebook.react.views.text.TextLayoutManager +import com.facebook.react.views.text.internal.span.CustomLetterSpacingSpan +import com.facebook.react.views.text.internal.span.CustomLineHeightSpan +import com.facebook.react.views.text.internal.span.CustomStyleSpan +import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan +import com.facebook.react.views.text.internal.span.ReactBackgroundColorSpan +import com.facebook.react.views.text.internal.span.ReactForegroundColorSpan +import com.facebook.react.views.text.internal.span.ReactSpan +import com.facebook.react.views.text.internal.span.ReactStrikethroughSpan +import com.facebook.react.views.text.internal.span.ReactTextPaintHolderSpan +import com.facebook.react.views.text.internal.span.ReactUnderlineSpan +import com.facebook.react.views.text.internal.span.TextInlineImageSpan +import java.util.concurrent.CopyOnWriteArrayList +import kotlin.math.max +import kotlin.math.min + +/** + * A wrapper around the EditText that lets us better control what happens when an EditText gets + * focused or blurred, and when to display the soft keyboard and when not to. + * + * ReactEditTexts have setFocusableInTouchMode set to false automatically because touches on the + * EditText are managed on the JS side. This also removes the nasty side effect that EditTexts have, + * which is that focus is always maintained on one of the EditTexts. + * + * The wrapper stops the EditText from triggering *TextChanged events, in the case where JS has + * called this explicitly. This is the default behavior on other platforms as well. + */ +public open class ReactEditText public constructor(context: Context) : AppCompatEditText(context) { + private val inputMethodManager: InputMethodManager + private val TAG: String = ReactEditText::class.java.simpleName + + // This flag is set to true when we set the text of the EditText explicitly. In that case, no + // *TextChanged events should be triggered. This is less expensive than removing the text + // listeners and adding them back again after the text change is completed. + protected var isSettingTextFromJS: Boolean + private val defaultGravityHorizontal: Int + private val defaultGravityVertical: Int + + /** A count of events sent to JS or C++. */ + protected var nativeEventCount: Int + + private var listeners: CopyOnWriteArrayList? + + public var stagedInputType: Int + protected var containsImages: Boolean = false + public var submitBehavior: String? = null + + private var disableFullscreen: Boolean + private var selectionWatcher: SelectionWatcher? = null + private var contentSizeWatcher: ContentSizeWatcher? = null + private var scrollWatcher: ScrollWatcher? + private var keyListener: InternalKeyListener? = null + private var detectScrollMovement = false + private var onKeyPress = false + private val textAttributes: TextAttributes + private var typefaceDirty = false + private var fontFamily: String? = null + private var fontWeight = ReactConstants.UNSET + private var fontStyle = ReactConstants.UNSET + private var autoFocus = false + private var contextMenuHidden = false + private var didAttachToWindow = false + private var selectTextOnFocus = false + private var placeholder: String? = null + private var overflow = Overflow.VISIBLE + + public var stateWrapper: StateWrapper? = null + internal var disableTextDiffing: Boolean = false + protected var isSettingTextFromState: Boolean = false + + private var eventDispatcher: EventDispatcher? = null + + private var textWatcherDelegator: TextWatcherDelegator? = null + get() { + if (field == null) { + field = TextWatcherDelegator() + } + return field + } + + internal val isMultiline: Boolean + get() = (inputType and InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0 + + private val isSecureText: Boolean + get() = + ((inputType and + (InputType.TYPE_NUMBER_VARIATION_PASSWORD or InputType.TYPE_TEXT_VARIATION_PASSWORD)) != + 0) + + public var disableFullscreenUI: Boolean + get() = disableFullscreen + set(disableFullscreenUI) { + disableFullscreen = disableFullscreenUI + updateImeOptions() + } + + public var returnKeyType: String? = null + set(value) { + field = value + updateImeOptions() + } + + internal var gravityHorizontal: Int + get() = + (gravity and (Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK)) + set(value) { + val newValue = if (value == 0) defaultGravityHorizontal else value + gravity = + ((gravity and + Gravity.HORIZONTAL_GRAVITY_MASK.inv() and + Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK.inv()) or newValue) + } + + internal var gravityVertical: Int + get() = gravity and (Gravity.VERTICAL_GRAVITY_MASK) + set(value) { + val newValue = if (value == 0) defaultGravityVertical else value + gravity = (gravity and Gravity.VERTICAL_GRAVITY_MASK.inv()) or newValue + } + + init { + if (!useEditTextStockAndroidFocusBehavior()) { + isFocusableInTouchMode = false + } + + inputMethodManager = + context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + defaultGravityHorizontal = + gravity and (Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) + defaultGravityVertical = gravity and Gravity.VERTICAL_GRAVITY_MASK + nativeEventCount = 0 + isSettingTextFromJS = false + disableFullscreen = false + listeners = null + stagedInputType = inputType + if (keyListener == null) { + keyListener = InternalKeyListener() + } + scrollWatcher = null + textAttributes = TextAttributes() + + applyTextAttributes() + + // Turn off hardware acceleration for Oreo (T40484798) + // see https://issuetracker.google.com/issues/67102093 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && + Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) { + setLayerType(LAYER_TYPE_SOFTWARE, null) + } + + val editTextAccessibilityDelegate: ReactAccessibilityDelegate = + object : + ReactAccessibilityDelegate(this, this.isFocusable, this.importantForAccessibility) { + override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean { + if (action == AccessibilityNodeInfo.ACTION_CLICK) { + val length = checkNotNull(text).length + if (length > 0) { + // For some reason, when you swipe to focus on a text input that already has text in + // it, it clears the selection and resets the cursor to the beginning of the input. + // Since this is not typically (ever?) what you want, let's just explicitly set the + // selection on accessibility click to undo that. + setSelection(length) + } + return if (useEditTextStockAndroidFocusBehavior()) requestFocusProgrammatically() + else requestFocusInternal() + } + return super.performAccessibilityAction(host, action, args) + } + } + ViewCompat.setAccessibilityDelegate(this, editTextAccessibilityDelegate) + val customActionModeCallback: ActionMode.Callback = + object : ActionMode.Callback { + /* + * Editor onCreateActionMode adds the cut, copy, paste, share, autofill, + * and paste as plain text items to the context menu. + */ + override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { + if (contextMenuHidden) { + return false + } + menu.removeItem(android.R.id.pasteAsPlainText) + return true + } + + override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean = true + + override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean = false + + override fun onDestroyActionMode(mode: ActionMode) = Unit + } + customSelectionActionModeCallback = customActionModeCallback + customInsertionActionModeCallback = customActionModeCallback + } + + @SuppressLint("ClassImplementsFinalize") + protected fun finalize() { + if (DEBUG_MODE) { + FLog.e(TAG, "finalize[$id] delete cached spannable") + } + TextLayoutManager.deleteCachedSpannableForTag(id) + } + + // After the text changes inside an EditText, TextView checks if a layout() has been requested. + // If it has, it will not scroll the text to the end of the new text inserted, but wait for the + // next layout() to be called. However, we do not perform a layout() after a requestLayout(), so + // we need to override isLayoutRequested to force EditText to scroll to the end of the new text + // immediately. + // TODO: t6408636 verify if we should schedule a layout after a View does a requestLayout() + override fun isLayoutRequested(): Boolean = false + + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { + onContentSizeChange() + if (selectTextOnFocus && isFocused) { + // Explicitly call this method to select text when layout is drawn + selectAll() + // Prevent text on being selected for next layout pass + selectTextOnFocus = false + } + } + + override fun onTouchEvent(ev: MotionEvent): Boolean { + when (ev.action) { + MotionEvent.ACTION_DOWN -> { + detectScrollMovement = true + // Disallow parent views to intercept touch events, until we can detect if we should be + // capturing these touches or not. + this.parent.requestDisallowInterceptTouchEvent(true) + } + + MotionEvent.ACTION_MOVE -> + if (detectScrollMovement) { + if (!canScrollVertically(-1) && + !canScrollVertically(1) && + !canScrollHorizontally(-1) && + !canScrollHorizontally(1)) { + // We cannot scroll, let parent views take care of these touches. + this.parent.requestDisallowInterceptTouchEvent(false) + } + detectScrollMovement = false + } + } + return super.onTouchEvent(ev) + } + + // Consume 'Enter' key events: TextView tries to give focus to the next TextInput, but it can't + // since we only allow JS to change focus, which in turn causes TextView to crash. + override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean { + if (keyCode == KeyEvent.KEYCODE_ENTER && !isMultiline) { + hideSoftKeyboard() + return true + } + return super.onKeyUp(keyCode, event) + } + + override fun setLineHeight(lineHeight: Int) { + textAttributes.lineHeight = lineHeight.toFloat() + // We don't call super.setLineHeight() because LineHeight is fully managed by ReactNative + } + + override fun onScrollChanged(horiz: Int, vert: Int, oldHoriz: Int, oldVert: Int) { + super.onScrollChanged(horiz, vert, oldHoriz, oldVert) + scrollWatcher?.onScrollChanged(horiz, vert, oldHoriz, oldVert) + } + + override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? { + val reactContext = UIManagerHelper.getReactContext(this) + var inputConnection = super.onCreateInputConnection(outAttrs) + if (inputConnection != null && onKeyPress) { + inputConnection = + ReactEditTextInputConnectionWrapper( + inputConnection, reactContext, this, checkNotNull(eventDispatcher)) + } + + if (isMultiline && (shouldBlurOnReturn() || shouldSubmitOnReturn())) { + // Remove IME_FLAG_NO_ENTER_ACTION to keep the original IME_OPTION + outAttrs.imeOptions = outAttrs.imeOptions and EditorInfo.IME_FLAG_NO_ENTER_ACTION.inv() + } + return inputConnection + } + + /* + * Called when a context menu option for the text view is selected. + * React Native replaces copy (as rich text) with copy as plain text. + */ + override fun onTextContextMenuItem(id: Int): Boolean = + super.onTextContextMenuItem( + if (id == android.R.id.paste) android.R.id.pasteAsPlainText else id) + + override fun clearFocus() { + val useStockFocusBehavior = useEditTextStockAndroidFocusBehavior() + if (!useStockFocusBehavior) { + isFocusableInTouchMode = false + } + super.clearFocus() + hideSoftKeyboard() + } + + override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean = + // This is a no-op so that when the OS calls requestFocus(), nothing will happen. + // ReactEditText + // is a controlled component, which means its focus is controlled by JS, with two exceptions: + // autofocus when it's attached to the window, and responding to accessibility events. In both + // of these cases, we call requestFocusInternal() directly. + if (useEditTextStockAndroidFocusBehavior()) { + super.requestFocus(direction, previouslyFocusedRect) + } else { + isFocused + } + + private fun requestFocusInternal(): Boolean { + isFocusableInTouchMode = true + // We must explicitly call this method on the super class; if we call requestFocus() without + // any arguments, it will call into the overridden requestFocus(int, Rect) above, which no-ops. + val focused = super.requestFocus(FOCUS_DOWN, null) + if (showSoftInputOnFocus) { + showSoftKeyboard() + } + + return focused + } + + // For cases like autoFocus, or ref.focus() where we request focus programmatically and not + // through + // interacting with the EditText directly (like clicking on it). We cannot use stock + // requestFocus() because it will not pop up the soft keyboard, only clicking the input will do + // that. This method will eventually replace requestFocusInternal() + private fun requestFocusProgrammatically(): Boolean { + val focused = super.requestFocus(FOCUS_DOWN, null) + if (isInTouchMode && showSoftInputOnFocus) { + showSoftKeyboard() + } + + return focused + } + + override fun addTextChangedListener(watcher: TextWatcher) { + if (listeners == null) { + listeners = CopyOnWriteArrayList() + super.addTextChangedListener(textWatcherDelegator) + } + listeners?.add(watcher) + } + + override fun removeTextChangedListener(watcher: TextWatcher) { + listeners?.let { nonNullListeners -> + nonNullListeners.remove(watcher) + + if (nonNullListeners.isEmpty()) { + this.listeners = null + super.removeTextChangedListener(textWatcherDelegator) + } + } + } + + public fun setContentSizeWatcher(contentSizeWatcher: ContentSizeWatcher?) { + this.contentSizeWatcher = contentSizeWatcher + } + + public fun setScrollWatcher(scrollWatcher: ScrollWatcher?) { + this.scrollWatcher = scrollWatcher + } + + /** + * Attempt to set a selection or fail silently. Intentionally meant to handle bad inputs. + * EventCounter is the same one used as with text. + * + * @param eventCounter + * @param start + * @param end + */ + public fun maybeSetSelection(eventCounter: Int, start: Int, end: Int) { + if (!canUpdateWithEventCount(eventCounter)) { + return + } + + maybeSetSelection(start, end) + } + + private fun maybeSetSelection(start: Int, end: Int) { + var start = start + var end = end + if (start != ReactConstants.UNSET && end != ReactConstants.UNSET) { + // clamp selection values for safety + start = clampToTextLength(start) + end = clampToTextLength(end) + + setSelection(start, end) + } + } + + private fun clampToTextLength(value: Int): Int { + val textLength = if (text == null) 0 else checkNotNull(text).length + return max(0.0, min(value.toDouble(), textLength.toDouble())).toInt() + } + + override fun setSelection(start: Int, end: Int) { + if (DEBUG_MODE) { + FLog.e(TAG, "setSelection[$id]: $start $end") + } + super.setSelection(start, end) + } + + override fun onSelectionChanged(selStart: Int, selEnd: Int) { + if (DEBUG_MODE) { + FLog.e(TAG, "onSelectionChanged[$id]: $selStart $selEnd") + } + + super.onSelectionChanged(selStart, selEnd) + if (selectionWatcher != null && hasFocus()) { + selectionWatcher?.onSelectionChanged(selStart, selEnd) + } + } + + override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) { + super.onFocusChanged(focused, direction, previouslyFocusedRect) + if (focused && selectionWatcher != null) { + selectionWatcher?.onSelectionChanged(selectionStart, selectionEnd) + } + } + + internal fun setSelectionWatcher(selectionWatcher: SelectionWatcher?) { + this.selectionWatcher = selectionWatcher + } + + public fun setOnKeyPress(onKeyPress: Boolean) { + this.onKeyPress = onKeyPress + } + + public fun shouldBlurOnReturn(): Boolean { + val submitBehavior = submitBehavior + + // Default shouldBlur + val shouldBlur = + if (submitBehavior == null) { + !isMultiline + } else { + submitBehavior == "blurAndSubmit" + } + + return shouldBlur + } + + public fun shouldSubmitOnReturn(): Boolean { + val submitBehavior = submitBehavior + + // Default shouldSubmit + val shouldSubmit = + if (submitBehavior == null) { + !isMultiline + } else { + submitBehavior == "submit" || submitBehavior == "blurAndSubmit" + } + + return shouldSubmit + } + + internal fun commitStagedInputType() { + if (inputType != stagedInputType) { + val selectionStart = selectionStart + val selectionEnd = selectionEnd + inputType = stagedInputType + // Restore the selection + maybeSetSelection(selectionStart, selectionEnd) + } + } + + override fun setInputType(type: Int) { + val tf = super.getTypeface() + super.setInputType(type) + stagedInputType = type + // Input type password defaults to monospace font, so we need to re-apply the font + super.setTypeface(tf) + + /** + * If set forces multiline on input, because of a restriction on Android source that enables + * multiline only for inputs of type Text and Multiline on method + * [android.widget.TextView.isMultilineInputType]} Source: + * [TextView.java](https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/TextView.java) + */ + if (isMultiline) { + isSingleLine = false + } + + // We override the KeyListener so that all keys on the soft input keyboard as well as hardware + // keyboards work. Some KeyListeners like DigitsKeyListener will display the keyboard but not + // accept all input from it + if (keyListener == null) { + keyListener = InternalKeyListener() + } + keyListener?.inputType = type + super.setKeyListener(keyListener) + } + + public fun setPlaceholder(placeholder: String?) { + if (placeholder != this.placeholder) { + this.placeholder = placeholder + hint = placeholder + } + } + + public fun setFontFamily(fontFamily: String?) { + this.fontFamily = fontFamily + typefaceDirty = true + } + + public fun setFontWeight(fontWeightString: String?) { + val fontWeight = parseFontWeight(fontWeightString) + if (fontWeight != this.fontWeight) { + this.fontWeight = fontWeight + typefaceDirty = true + } + } + + public fun setFontStyle(fontStyleString: String?) { + val fontStyle = parseFontStyle(fontStyleString) + if (fontStyle != this.fontStyle) { + this.fontStyle = fontStyle + typefaceDirty = true + } + } + + override fun setFontFeatureSettings(fontFeatureSettings: String?) { + if (fontFeatureSettings != getFontFeatureSettings()) { + super.setFontFeatureSettings(fontFeatureSettings) + typefaceDirty = true + } + } + + public fun maybeUpdateTypeface() { + if (!typefaceDirty) { + return + } + + typefaceDirty = false + + val newTypeface = applyStyles(typeface, fontStyle, fontWeight, fontFamily, context.assets) + typeface = newTypeface + + // Match behavior of CustomStyleSpan and enable SUBPIXEL_TEXT_FLAG when setting anything + // nonstandard + paintFlags = + if (fontStyle != ReactConstants.UNSET || + fontWeight != ReactConstants.UNSET || + fontFamily != null || + fontFeatureSettings != null) { + paintFlags or Paint.SUBPIXEL_TEXT_FLAG + } else { + paintFlags and (Paint.SUBPIXEL_TEXT_FLAG.inv()) + } + } + + public fun requestFocusFromJS() { + if (useEditTextStockAndroidFocusBehavior()) { + requestFocusProgrammatically() + } else { + requestFocusInternal() + } + } + + internal fun clearFocusFromJS() { + clearFocus() + } + + public fun incrementAndGetEventCounter(): Int = ++nativeEventCount + + public fun maybeSetTextFromJS(reactTextUpdate: ReactTextUpdate) { + isSettingTextFromJS = true + maybeSetText(reactTextUpdate) + isSettingTextFromJS = false + } + + public fun maybeSetTextFromState(reactTextUpdate: ReactTextUpdate) { + isSettingTextFromState = true + maybeSetText(reactTextUpdate) + isSettingTextFromState = false + } + + public fun canUpdateWithEventCount(eventCounter: Int): Boolean = eventCounter >= nativeEventCount + + private fun maybeSetText(reactTextUpdate: ReactTextUpdate) { + if (isSecureText && (text == reactTextUpdate.text)) { + return + } + + // Only set the text if it is up to date. + if (!canUpdateWithEventCount(reactTextUpdate.jsEventCounter)) { + return + } + + if (DEBUG_MODE) { + FLog.e( + TAG, + ("maybeSetText[" + id + "]: current text: " + text + " update: " + reactTextUpdate.text)) + } + + // The current text gets replaced with the text received from JS. However, the spans on the + // current text need to be adapted to the new text. Since TextView#setText() will remove or + // reset some of these spans even if they are set directly, SpannableStringBuilder#replace() is + // used instead (this is also used by the keyboard implementation underneath the covers). + val spannableStringBuilder = SpannableStringBuilder(reactTextUpdate.text) + + manageSpans(spannableStringBuilder) + stripStyleEquivalentSpans(spannableStringBuilder) + + @Suppress("DEPRECATION") + containsImages = reactTextUpdate.containsImages() + + // When we update text, we trigger onChangeText code that will + // try to update state if the wrapper is available. Temporarily disable + // to prevent an (asynchronous) infinite loop. + disableTextDiffing = true + + // On some devices, when the text is cleared, buggy keyboards will not clear the composing + // text so, we have to set text to null, which will clear the currently composing text. + if (reactTextUpdate.text.length == 0) { + text = null + } else { + // When we update text, we trigger onChangeText code that will + // try to update state if the wrapper is available. Temporarily disable + // to prevent an infinite loop. + checkNotNull(text).replace(0, length(), spannableStringBuilder) + } + disableTextDiffing = false + + if (breakStrategy != reactTextUpdate.textBreakStrategy) { + breakStrategy = reactTextUpdate.textBreakStrategy + } + + // Update cached spans (in Fabric only). + updateCachedSpannable() + } + + /** + * Remove and/or add [Spanned.SPAN_EXCLUSIVE_EXCLUSIVE] spans, since they should only exist as + * long as the text they cover is the same. All other spans will remain the same, since they will + * adapt to the new text, hence why [SpannableStringBuilder.replace] never removes them. + */ + private fun manageSpans(spannableStringBuilder: SpannableStringBuilder) { + val text = checkNotNull(text) + val spans = text.getSpans(0, length(), Any::class.java) + for (spanIdx in spans.indices) { + val span = spans[spanIdx] + val spanFlags = text.getSpanFlags(span) + val isExclusiveExclusive = + (spanFlags and Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) == Spanned.SPAN_EXCLUSIVE_EXCLUSIVE + + // Remove all styling spans we might have previously set + if (span is ReactSpan) { + text.removeSpan(span) + } + + // We only add spans back for EXCLUSIVE_EXCLUSIVE spans + if (!isExclusiveExclusive) { + continue + } + + val spanStart = text.getSpanStart(span) + val spanEnd = text.getSpanEnd(span) + + // Make sure the span is removed from existing text, otherwise the spans we set will be + // ignored or it will cover text that has changed. + text.removeSpan(span) + if (sameTextForSpan(text, spannableStringBuilder, spanStart, spanEnd)) { + spannableStringBuilder.setSpan(span, spanStart, spanEnd, spanFlags) + } + } + } + + /** + * Remove spans from the SpannableStringBuilder which can be represented by TextAppearance + * attributes on the underlying EditText. This works around instability on Samsung devices with + * the presence of spans https://github.com/facebook/react-native/issues/35936 (S318090) + */ + private fun stripStyleEquivalentSpans(sb: SpannableStringBuilder) { + stripSpansOfKind(sb, ReactAbsoluteSizeSpan::class.java) { span: ReactAbsoluteSizeSpan -> + span.size == textAttributes.effectiveFontSize + } + + stripSpansOfKind(sb, ReactBackgroundColorSpan::class.java) { span: ReactBackgroundColorSpan -> + span.backgroundColor == getBackgroundColor(this) + } + + stripSpansOfKind(sb, ReactForegroundColorSpan::class.java) { span: ReactForegroundColorSpan -> + span.foregroundColor == currentTextColor + } + + stripSpansOfKind(sb, ReactStrikethroughSpan::class.java) { + (paintFlags and Paint.STRIKE_THRU_TEXT_FLAG) != 0 + } + + stripSpansOfKind(sb, ReactUnderlineSpan::class.java) { + (paintFlags and Paint.UNDERLINE_TEXT_FLAG) != 0 + } + + stripSpansOfKind(sb, CustomLetterSpacingSpan::class.java) { span: CustomLetterSpacingSpan -> + span.spacing == textAttributes.effectiveLetterSpacing + } + + stripSpansOfKind(sb, CustomStyleSpan::class.java) { span: CustomStyleSpan -> + span.style == fontStyle && + span.fontFamily == fontFamily && + span.weight == fontWeight && + span.fontFeatureSettings == fontFeatureSettings + } + } + + private fun stripSpansOfKind( + sb: SpannableStringBuilder, + clazz: Class, + shouldStrip: Predicate + ) { + val spans = sb.getSpans(0, sb.length, clazz) + + for (span in spans) { + if (shouldStrip.test(span)) { + sb.removeSpan(span) + } + } + } + + /** + * Copy styles represented as attributes to the underlying span, for later measurement or other + * usage outside the ReactEditText. + */ + private fun addSpansFromStyleAttributes(workingText: SpannableStringBuilder) { + var spanFlags = Spannable.SPAN_INCLUSIVE_INCLUSIVE + + // Set all bits for SPAN_PRIORITY so that this span has the highest possible priority + // (least precedence). This ensures the span is behind any overlapping spans. + spanFlags = spanFlags or Spannable.SPAN_PRIORITY + + workingText.setSpan( + ReactAbsoluteSizeSpan(textAttributes.effectiveFontSize), 0, workingText.length, spanFlags) + + workingText.setSpan( + ReactForegroundColorSpan(currentTextColor), 0, workingText.length, spanFlags) + + val backgroundColor = getBackgroundColor(this) + if (backgroundColor != null && backgroundColor != Color.TRANSPARENT) { + workingText.setSpan( + ReactBackgroundColorSpan(backgroundColor), 0, workingText.length, spanFlags) + } + + if ((paintFlags and Paint.STRIKE_THRU_TEXT_FLAG) != 0) { + workingText.setSpan(ReactStrikethroughSpan(), 0, workingText.length, spanFlags) + } + + if ((paintFlags and Paint.UNDERLINE_TEXT_FLAG) != 0) { + workingText.setSpan(ReactUnderlineSpan(), 0, workingText.length, spanFlags) + } + + val effectiveLetterSpacing = textAttributes.effectiveLetterSpacing + if (!java.lang.Float.isNaN(effectiveLetterSpacing)) { + workingText.setSpan( + CustomLetterSpacingSpan(effectiveLetterSpacing), 0, workingText.length, spanFlags) + } + + if (fontStyle != ReactConstants.UNSET || + fontWeight != ReactConstants.UNSET || + fontFamily != null || + fontFeatureSettings != null) { + workingText.setSpan( + CustomStyleSpan(fontStyle, fontWeight, fontFeatureSettings, fontFamily, context.assets), + 0, + workingText.length, + spanFlags) + } + + val lineHeight = textAttributes.effectiveLineHeight + if (!java.lang.Float.isNaN(lineHeight)) { + workingText.setSpan(CustomLineHeightSpan(lineHeight), 0, workingText.length, spanFlags) + } + } + + protected fun showSoftKeyboard(): Boolean = inputMethodManager.showSoftInput(this, 0) + + protected fun hideSoftKeyboard() { + inputMethodManager.hideSoftInputFromWindow(windowToken, 0) + } + + private fun onContentSizeChange() { + contentSizeWatcher?.onLayout() + + setIntrinsicContentSize() + } + + // TODO T58784068: delete this method + private fun setIntrinsicContentSize() { + // This serves as a check for whether we're running under Paper or Fabric. + // By the time this is called, in Fabric we will have a state + // wrapper 100% of the time. + // Since the LocalData object is constructed by getting values from the underlying EditText + // view, we don't need to construct one or apply it at all - it provides no use in Fabric. + val reactContext = UIManagerHelper.getReactContext(this) + + if (!ReactBuildConfig.UNSTABLE_ENABLE_MINIFY_LEGACY_ARCHITECTURE) { + @Suppress("DEPRECATION") + if (stateWrapper == null && !reactContext.isBridgeless) { + val localData = ReactTextInputLocalData(this) + val uiManager = reactContext.getNativeModule(UIManagerModule::class.java) + uiManager?.setViewLocalData(id, localData) + } + } + } + + private fun updateImeOptions() { + // Default to IME_ACTION_DONE + var returnKeyFlag = EditorInfo.IME_ACTION_DONE + when (returnKeyType) { + "go" -> returnKeyFlag = EditorInfo.IME_ACTION_GO + "next" -> returnKeyFlag = EditorInfo.IME_ACTION_NEXT + "none" -> returnKeyFlag = EditorInfo.IME_ACTION_NONE + "previous" -> returnKeyFlag = EditorInfo.IME_ACTION_PREVIOUS + "search" -> returnKeyFlag = EditorInfo.IME_ACTION_SEARCH + "send" -> returnKeyFlag = EditorInfo.IME_ACTION_SEND + "done" -> returnKeyFlag = EditorInfo.IME_ACTION_DONE + } + + imeOptions = + if (disableFullscreen) { + returnKeyFlag or EditorInfo.IME_FLAG_NO_FULLSCREEN + } else { + returnKeyFlag + } + } + + override fun verifyDrawable(drawable: Drawable): Boolean { + if (containsImages) { + val text: Spanned? = text + val spans = checkNotNull(text).getSpans(0, text.length, TextInlineImageSpan::class.java) + for (span in spans) { + if (span.drawable === drawable) { + return true + } + } + } + return super.verifyDrawable(drawable) + } + + override fun invalidateDrawable(drawable: Drawable) { + if (containsImages) { + val text: Spanned? = text + val spans = checkNotNull(text).getSpans(0, text.length, TextInlineImageSpan::class.java) + for (span in spans) { + if (span.drawable === drawable) { + invalidate() + } + } + } + super.invalidateDrawable(drawable) + } + + public override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + if (containsImages) { + val text: Spanned? = text + val spans = checkNotNull(text).getSpans(0, text.length, TextInlineImageSpan::class.java) + for (span in spans) { + span.onDetachedFromWindow() + } + } + } + + override fun onStartTemporaryDetach() { + super.onStartTemporaryDetach() + if (containsImages) { + val text: Spanned? = text + val spans = checkNotNull(text).getSpans(0, text.length, TextInlineImageSpan::class.java) + for (span in spans) { + span.onStartTemporaryDetach() + } + } + } + + public override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + + if (ReactNativeNewArchitectureFeatureFlags.enableBridgelessArchitecture() && + ReactNativeFeatureFlags.enableFontScaleChangesUpdatingLayout()) { + applyTextAttributes() + } + } + + public override fun onAttachedToWindow() { + super.onAttachedToWindow() + + val selectionStart = selectionStart + val selectionEnd = selectionEnd + + // Used to ensure that text is selectable inside of removeClippedSubviews + // See https://github.com/facebook/react-native/issues/6805 for original + // fix that was ported to here. + super.setTextIsSelectable(true) + + // Restore the selection since `setTextIsSelectable` changed it. + maybeSetSelection(selectionStart, selectionEnd) + + if (containsImages) { + val text: Spanned? = text + val spans = checkNotNull(text).getSpans(0, text.length, TextInlineImageSpan::class.java) + for (span in spans) { + span.onAttachedToWindow() + } + } + + if (autoFocus && !didAttachToWindow) { + if (useEditTextStockAndroidFocusBehavior()) { + requestFocusProgrammatically() + } else { + requestFocusInternal() + } + } + + didAttachToWindow = true + } + + override fun onFinishTemporaryDetach() { + super.onFinishTemporaryDetach() + if (containsImages) { + val text: Spanned? = text + val spans = checkNotNull(text).getSpans(0, text.length, TextInlineImageSpan::class.java) + for (span in spans) { + span.onFinishTemporaryDetach() + } + } + } + + override fun setBackgroundColor(color: Int) { + setBackgroundColor(this, color) + } + + public fun setBorderWidth(position: Int, width: Float) { + setBorderWidth(this, LogicalEdge.entries[position], toDIPFromPixel(width)) + } + + public fun setBorderColor(position: Int, color: Int?) { + setBorderColor(this, LogicalEdge.entries[position], color) + } + + public fun getBorderColor(position: Int): Int = + getBorderColor(this, LogicalEdge.entries[position]) ?: Color.TRANSPARENT + + public fun setBorderRadius(borderRadius: Float) { + setBorderRadius(borderRadius, BorderRadiusProp.BORDER_RADIUS.ordinal) + } + + public fun setBorderRadius(borderRadius: Float, position: Int) { + val radius = + if (java.lang.Float.isNaN(borderRadius)) { + null + } else { + LengthPercentage(toDIPFromPixel(borderRadius), LengthPercentageType.POINT) + } + setBorderRadius(this, BorderRadiusProp.entries[position], radius) + } + + public fun setBorderStyle(style: String?) { + setBorderStyle(this, if (style == null) null else BorderStyle.fromString(style)) + } + + public fun setLetterSpacingPt(letterSpacingPt: Float) { + textAttributes.letterSpacing = letterSpacingPt + applyTextAttributes() + } + + public fun setAllowFontScaling(allowFontScaling: Boolean) { + if (textAttributes.allowFontScaling != allowFontScaling) { + textAttributes.allowFontScaling = allowFontScaling + applyTextAttributes() + } + } + + public fun setFontSize(fontSize: Float) { + textAttributes.fontSize = fontSize + applyTextAttributes() + } + + public fun setMaxFontSizeMultiplier(maxFontSizeMultiplier: Float) { + if (maxFontSizeMultiplier != textAttributes.maxFontSizeMultiplier) { + textAttributes.maxFontSizeMultiplier = maxFontSizeMultiplier + applyTextAttributes() + } + } + + public fun setAutoFocus(autoFocus: Boolean) { + this.autoFocus = autoFocus + } + + public fun setSelectTextOnFocus(selectTextOnFocus: Boolean) { + super.setSelectAllOnFocus(selectTextOnFocus) + this.selectTextOnFocus = selectTextOnFocus + } + + public fun setContextMenuHidden(contextMenuHidden: Boolean) { + this.contextMenuHidden = contextMenuHidden + } + + protected fun applyTextAttributes() { + // In general, the `getEffective*` functions return `Float.NaN` if the + // property hasn't been set. + // `getEffectiveFontSize` always returns a value so don't need to check for anything like + // `Float.NaN`. + setTextSize(TypedValue.COMPLEX_UNIT_PX, textAttributes.effectiveFontSize.toFloat()) + + val effectiveLetterSpacing = textAttributes.effectiveLetterSpacing + if (!java.lang.Float.isNaN(effectiveLetterSpacing)) { + letterSpacing = effectiveLetterSpacing + } + } + + /** + * Update the cached Spannable used in TextLayoutManager to measure the text in Fabric. This is + * mostly copied from ReactTextInputShadowNode.java (the non-Fabric version) and + * TextLayoutManager.java with some very minor modifications. There's some duplication between + * here and TextLayoutManager, so there might be an opportunity for refactor. + */ + private fun updateCachedSpannable() { + // Noops in non-Fabric + if (stateWrapper == null) { + return + } + // If this view doesn't have an ID yet, we don't have a cache key, so bail here + if (id == -1) { + return + } + + val currentText = text + val haveText = !currentText.isNullOrEmpty() + + val sb = SpannableStringBuilder() + + // A note of caution: appending currentText to sb appends all the spans of currentText - not + // copies of the Spans, but the actual span objects. Any modifications to sb after that point + // can modify the spans of sb/currentText, impact the text or spans visible on screen, and + // also call the TextChangeWatcher methods. + if (currentText != null && haveText) { + // This is here as a workaround for T76236115, which looks like this: + // Hopefully we can delete all this stuff if we can get rid of the soft errors. + // - android.text.SpannableStringBuilder.charAt (SpannableStringBuilder.java:123) + // - android.text.CharSequenceCharacterIterator.current + // (CharSequenceCharacterIterator.java:58) + // - android.text.CharSequenceCharacterIterator.setIndex + // (CharSequenceCharacterIterator.java:83) + // - android.icu.text.RuleBasedBreakIterator.CISetIndex32 (RuleBasedBreakIterator.java:1126) + // - android.icu.text.RuleBasedBreakIterator.isBoundary (RuleBasedBreakIterator.java:503) + // - android.text.method.WordIterator.isBoundary (WordIterator.java:95) + // - android.widget.Editor$SelectionHandleView.positionAtCursorOffset (Editor.java:6666) + // - android.widget.Editor$HandleView.invalidate (Editor.java:5241) + // - android.widget.Editor$SelectionModifierCursorController.invalidateHandles + // (Editor.java:7442) + // - android.widget.Editor.invalidateHandlesAndActionMode (Editor.java:2112) + // - android.widget.TextView.spanChange (TextView.java:11189) + // - android.widget.TextView$ChangeWatcher.onSpanAdded (TextView.java:14189) + // - android.text.SpannableStringBuilder.sendSpanAdded (SpannableStringBuilder.java:1283) + // - android.text.SpannableStringBuilder.sendToSpanWatchers (SpannableStringBuilder.java:663) + // - android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:579) + // - android.text.SpannableStringBuilder.append (SpannableStringBuilder.java:269) + // - ReactEditText.updateCachedSpannable (ReactEditText.java:995) + // - ReactEditText$TextWatcherDelegator.onTextChanged (ReactEditText.java:1044) + // - android.widget.TextView.sendOnTextChanged (TextView.java:10972) + // ... + // - android.text.method.BaseKeyListener.onKeyDown (BaseKeyListener.java:479) + // - android.text.method.QwertyKeyListener.onKeyDown (QwertyKeyListener.java:362) + // - ReactEditText$InternalKeyListener.onKeyDown (ReactEditText.java:1094) + // ... + // - android.app.Activity.dispatchKeyEvent (Activity.java:3447) + try { + sb.append(currentText.subSequence(0, currentText.length)) + } catch (e: IndexOutOfBoundsException) { + logSoftException(TAG, e) + } + } + + // If we don't have text, make sure we have *something* to measure. + // Hint has the same dimensions - the only thing that's different is background or foreground + // color + if (!haveText) { + if (hint != null && hint.isNotEmpty()) { + sb.append(hint) + } else if (getUIManagerType(this) != UIManagerType.FABRIC) { + // Measure something so we have correct height, even if there's no string. + sb.append("I") + } + } + + addSpansFromStyleAttributes(sb) + sb.setSpan( + ReactTextPaintHolderSpan(TextPaint(paint)), + 0, + sb.length, + Spannable.SPAN_INCLUSIVE_INCLUSIVE) + TextLayoutManager.setCachedSpannableForTag(id, sb) + } + + public fun setEventDispatcher(eventDispatcher: EventDispatcher?) { + this.eventDispatcher = eventDispatcher + } + + public fun setOverflow(overflow: String?) { + if (overflow == null) { + this.overflow = Overflow.VISIBLE + } else { + val parsedOverflow = Overflow.fromString(overflow) + this.overflow = parsedOverflow ?: Overflow.VISIBLE + } + + invalidate() + } + + public override fun onDraw(canvas: Canvas) { + if (overflow != Overflow.VISIBLE) { + clipToPaddingBox(this, canvas) + } + + super.onDraw(canvas) + } + + /** + * This class will redirect *TextChanged calls to the listeners only in the case where the text is + * changed by the user, and not explicitly set by JS. + */ + private inner class TextWatcherDelegator : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { + if (!isSettingTextFromJS) { + listeners?.forEach { listener -> listener.beforeTextChanged(s, start, count, after) } + } + } + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + if (DEBUG_MODE) { + FLog.e(TAG, "onTextChanged[$id]: $s $start $before $count") + } + + if (!isSettingTextFromJS) { + listeners?.forEach { listener -> listener.onTextChanged(s, start, before, count) } + } + + updateCachedSpannable() + + onContentSizeChange() + } + + override fun afterTextChanged(s: Editable) { + if (!isSettingTextFromJS) { + listeners?.forEach { listener -> listener.afterTextChanged(s) } + } + } + } + + /* + * This class is set as the KeyListener for the underlying TextView + * It does two things + * 1) Provides the same answer to getInputType() as the real KeyListener would have which allows + * the proper keyboard to pop up on screen + * 2) Permits all keyboard input through + */ + private class InternalKeyListener : KeyListener { + private var _inputType = 0 + + /* + * getInputType will return whatever value is passed in. This will allow the proper keyboard + * to be shown on screen but without the actual filtering done by other KeyListeners + */ + override fun getInputType() = _inputType + + public fun setInputType(inputType: Int) { + _inputType = inputType + } + + /* + * All overrides of key handling defer to the underlying KeyListener which is shared by all + * ReactEditText instances. It will basically allow any/all keyboard input whether from + * physical keyboard or from soft input. + */ + override fun onKeyDown(view: View, text: Editable, keyCode: Int, event: KeyEvent): Boolean = + keyListener.onKeyDown(view, text, keyCode, event) + + override fun onKeyUp(view: View, text: Editable, keyCode: Int, event: KeyEvent): Boolean = + keyListener.onKeyUp(view, text, keyCode, event) + + override fun onKeyOther(view: View, text: Editable, event: KeyEvent): Boolean = + keyListener.onKeyOther(view, text, event) + + override fun clearMetaKeyState(view: View, content: Editable, states: Int) { + keyListener.clearMetaKeyState(view, content, states) + } + } + + public companion object { + public val DEBUG_MODE: Boolean = ReactBuildConfig.DEBUG && false + + private val keyListener: KeyListener = QwertyKeyListener.getInstanceForFullKeyboard() + + private fun sameTextForSpan( + oldText: Editable, + newText: SpannableStringBuilder, + start: Int, + end: Int + ): Boolean { + if (start > newText.length || end > newText.length) { + return false + } + for (charIdx in start.. view.setGravityVertical(Gravity.NO_GRAVITY) - "top" -> view.setGravityVertical(Gravity.TOP) - "bottom" -> view.setGravityVertical(Gravity.BOTTOM) - "center" -> view.setGravityVertical(Gravity.CENTER_VERTICAL) + "auto" -> view.gravityVertical = Gravity.NO_GRAVITY + "top" -> view.gravityVertical = Gravity.TOP + "bottom" -> view.gravityVertical = Gravity.BOTTOM + "center" -> view.gravityVertical = Gravity.CENTER_VERTICAL else -> { FLog.w(ReactConstants.TAG, "Invalid textAlignVertical: $textAlignVertical") - view.setGravityVertical(Gravity.NO_GRAVITY) + view.gravityVertical = Gravity.NO_GRAVITY } } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt index 4dc427bbb47..bb3650ed5bf 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt @@ -30,7 +30,7 @@ internal class ReactTextInputTextWatcher( } override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - if (editText.mDisableTextDiffing) { + if (editText.disableTextDiffing) { return }