-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactEditText.java
1170 lines (1011 loc) · 40.6 KB
/
ReactEditText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 static com.facebook.react.views.text.TextAttributeProps.UNSET;
import android.content.Context;
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.TextUtils;
import android.text.TextWatcher;
import android.text.method.KeyListener;
import android.text.method.QwertyKeyListener;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
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.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.build.ReactBuildConfig;
import com.facebook.react.uimanager.FabricViewStateManager;
import com.facebook.react.uimanager.ReactAccessibilityDelegate;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.text.CustomLetterSpacingSpan;
import com.facebook.react.views.text.CustomLineHeightSpan;
import com.facebook.react.views.text.CustomStyleSpan;
import com.facebook.react.views.text.ReactAbsoluteSizeSpan;
import com.facebook.react.views.text.ReactSpan;
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.TextInlineImageSpan;
import com.facebook.react.views.text.TextLayoutManager;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import java.util.ArrayList;
import java.util.List;
/**
* 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.
*
* <p>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.
*
* <p>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.
* VisibleForTesting from {@link TextInputEventsTestCase}.
*/
public class ReactEditText extends AppCompatEditText
implements FabricViewStateManager.HasFabricViewStateManager {
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;
protected boolean mIsSettingTextFromCacheUpdate = false;
private int mDefaultGravityHorizontal;
private int mDefaultGravityVertical;
/** A count of events sent to JS or C++. */
protected int mNativeEventCount;
private static final int UNSET = -1;
private @Nullable ArrayList<TextWatcher> mListeners;
private @Nullable TextWatcherDelegator mTextWatcherDelegator;
private int mStagedInputType;
protected boolean mContainsImages;
private @Nullable Boolean mBlurOnSubmit;
private boolean mDisableFullscreen;
private @Nullable String mReturnKeyType;
private @Nullable SelectionWatcher mSelectionWatcher;
private @Nullable ContentSizeWatcher mContentSizeWatcher;
private @Nullable ScrollWatcher mScrollWatcher;
private final 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 = UNSET;
private int mFontStyle = UNSET;
private boolean mAutoFocus = false;
private boolean mDidAttachToWindow = false;
private ReactViewBackgroundManager mReactBackgroundManager;
private final @Nullable FabricViewStateManager mFabricViewStateManager =
new FabricViewStateManager();
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);
setFocusableInTouchMode(false);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
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;
mBlurOnSubmit = null;
mDisableFullscreen = false;
mListeners = null;
mTextWatcherDelegator = null;
mStagedInputType = getInputType();
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);
}
ViewCompat.setAccessibilityDelegate(
this,
new ReactAccessibilityDelegate() {
@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 requestFocusInternal();
}
return super.performAccessibilityAction(host, action, args);
}
});
}
@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();
}
@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
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() && getBlurOnSubmit()) {
// Remove IME_FLAG_NO_ENTER_ACTION to keep the original IME_OPTION
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return inputConnection;
}
@Override
public void clearFocus() {
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 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;
}
@Override
public void addTextChangedListener(TextWatcher watcher) {
if (mListeners == null) {
mListeners = new ArrayList<>();
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(ContentSizeWatcher contentSizeWatcher) {
mContentSizeWatcher = contentSizeWatcher;
}
public void setScrollWatcher(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;
}
if (start != UNSET && end != 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 (!mIsSettingTextFromCacheUpdate && 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(SelectionWatcher selectionWatcher) {
mSelectionWatcher = selectionWatcher;
}
public void setBlurOnSubmit(@Nullable Boolean blurOnSubmit) {
mBlurOnSubmit = blurOnSubmit;
}
public void setOnKeyPress(boolean onKeyPress) {
mOnKeyPress = onKeyPress;
}
public boolean getBlurOnSubmit() {
if (mBlurOnSubmit == null) {
// Default blurOnSubmit
return isMultiline() ? false : true;
}
return mBlurOnSubmit;
}
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);
setSelection(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 <a
* href='https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/TextView.java'>TextView.java</a>}
*/
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
mKeyListener.setInputType(type);
setKeyListener(mKeyListener);
}
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;
}
}
public void maybeUpdateTypeface() {
if (!mTypefaceDirty) {
return;
}
mTypefaceDirty = false;
Typeface newTypeface =
ReactTypefaceUtils.applyStyles(
getTypeface(), mFontStyle, mFontWeight, mFontFamily, getContext().getAssets());
setTypeface(newTypeface);
}
// VisibleForTesting from {@link TextInputEventsTestCase}.
public void requestFocusFromJS() {
requestFocusInternal();
}
/* package */ void clearFocusFromJS() {
clearFocus();
}
// VisibleForTesting from {@link TextInputEventsTestCase}.
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;
}
// VisibleForTesting from {@link TextInputEventsTestCase}.
public 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, reactTextUpdate.mContainsMultipleFragments);
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.
int startPosition = getSelectionStart();
int endPosition = getSelectionEnd();
setText(spannableStringBuilder);
maybeSetSelection(mNativeEventCount, startPosition, endPosition);
}
mDisableTextDiffing = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (getBreakStrategy() != reactTextUpdate.getTextBreakStrategy()) {
setBreakStrategy(reactTextUpdate.getTextBreakStrategy());
}
}
// Update cached spans (in Fabric only).
updateCachedSpannable(false);
}
/**
* 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, boolean skipAddSpansForMeasurements) {
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);
}
}
// In Fabric only, apply necessary styles to entire span
// If the Spannable was constructed from multiple fragments, we don't apply any spans that could
// impact the whole Spannable, because that would override "local" styles per-fragment
if (!skipAddSpansForMeasurements) {
addSpansForMeasurement(getText());
}
}
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;
}
// This is hacked in for Fabric. When we delete non-Fabric code, we might be able to simplify or
// clean this up a bit.
private void addSpansForMeasurement(Spannable spannable) {
if (!mFabricViewStateManager.hasStateWrapper()) {
return;
}
boolean originalDisableTextDiffing = mDisableTextDiffing;
mDisableTextDiffing = true;
int start = 0;
int end = spannable.length();
// Remove duplicate spans we might add here
Object[] spans = spannable.getSpans(0, length(), Object.class);
for (Object span : spans) {
int spanFlags = spannable.getSpanFlags(span);
boolean isInclusive =
(spanFlags & Spanned.SPAN_INCLUSIVE_INCLUSIVE) == Spanned.SPAN_INCLUSIVE_INCLUSIVE
|| (spanFlags & Spanned.SPAN_INCLUSIVE_EXCLUSIVE) == Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
if (isInclusive
&& span instanceof ReactSpan
&& spannable.getSpanStart(span) == start
&& spannable.getSpanEnd(span) == end) {
spannable.removeSpan(span);
}
}
List<TextLayoutManager.SetSpanOperation> ops = new ArrayList<>();
if (!Float.isNaN(mTextAttributes.getLetterSpacing())) {
ops.add(
new TextLayoutManager.SetSpanOperation(
start, end, new CustomLetterSpacingSpan(mTextAttributes.getLetterSpacing())));
}
ops.add(
new TextLayoutManager.SetSpanOperation(
start, end, new ReactAbsoluteSizeSpan((int) mTextAttributes.getEffectiveFontSize())));
if (mFontStyle != UNSET || mFontWeight != UNSET || mFontFamily != null) {
ops.add(
new TextLayoutManager.SetSpanOperation(
start,
end,
new CustomStyleSpan(
mFontStyle,
mFontWeight,
null, // TODO: do we need to support FontFeatureSettings / fontVariant?
mFontFamily,
getReactContext(ReactEditText.this).getAssets())));
}
if (!Float.isNaN(mTextAttributes.getEffectiveLineHeight())) {
ops.add(
new TextLayoutManager.SetSpanOperation(
start, end, new CustomLineHeightSpan(mTextAttributes.getEffectiveLineHeight())));
}
int priority = 0;
for (TextLayoutManager.SetSpanOperation op : ops) {
// Actual order of calling {@code execute} does NOT matter,
// but the {@code priority} DOES matter.
op.execute(spannable, priority);
priority++;
}
mDisableTextDiffing = originalDisableTextDiffing;
}
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 (mFabricViewStateManager != null
&& !mFabricViewStateManager.hasStateWrapper()
&& !reactContext.isBridgeless()) {
final ReactTextInputLocalData localData = new ReactTextInputLocalData(this);
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
if (uiManager != null) {
uiManager.setViewLocalData(getId(), localData);
}
}
}
/* 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 onAttachedToWindow() {
super.onAttachedToWindow();
// 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);
if (mContainsImages) {
Spanned text = getText();
TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);
for (TextInlineImageSpan span : spans) {
span.onAttachedToWindow();
}
}
if (mAutoFocus && !mDidAttachToWindow) {
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) {
mReactBackgroundManager.setBackgroundColor(color);
}
public void setBorderWidth(int position, float width) {
mReactBackgroundManager.setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
mReactBackgroundManager.setBorderColor(position, color, alpha);
}
public int getBorderColor(int position) {
return mReactBackgroundManager.getBorderColor(position);
}
public void setBorderRadius(float borderRadius) {
mReactBackgroundManager.setBorderRadius(borderRadius);
}
public void setBorderRadius(float borderRadius, int position) {
mReactBackgroundManager.setBorderRadius(borderRadius, position);
}
public void setBorderStyle(@Nullable String style) {
mReactBackgroundManager.setBorderStyle(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;
}
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);
}
}
@Override
public FabricViewStateManager getFabricViewStateManager() {
return mFabricViewStateManager;
}
/**
* 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(boolean resetStyles) {
// Noops in non-Fabric
if (mFabricViewStateManager != null && !mFabricViewStateManager.hasStateWrapper()) {
return;
}
// If this view doesn't have an ID yet, we don't have a cache key, so bail here
if (getId() == -1) {
return;
}
if (resetStyles) {
mIsSettingTextFromCacheUpdate = true;