forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReactTextView.java
548 lines (480 loc) · 20.5 KB
/
ReactTextView.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
/*
* Copyright (c) Facebook, Inc. and its 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.text;
import static com.facebook.react.views.text.TextAttributeProps.UNSET;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Layout;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.TintContextWrapper;
import android.graphics.Canvas;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactCompoundView;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewDefaults;
import com.facebook.react.uimanager.common.UIManagerType;
import com.facebook.react.uimanager.common.ViewUtil;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ReactTextView extends AppCompatTextView implements ReactCompoundView {
private static final ViewGroup.LayoutParams EMPTY_LAYOUT_PARAMS =
new ViewGroup.LayoutParams(0, 0);
private boolean mContainsImages;
private int mDefaultGravityHorizontal;
private int mDefaultGravityVertical;
private int mTextAlign = Gravity.NO_GRAVITY;
private int mNumberOfLines = ViewDefaults.NUMBER_OF_LINES;
private TextUtils.TruncateAt mEllipsizeLocation = TextUtils.TruncateAt.END;
private boolean mAdjustsFontSizeToFit = false;
private int mLinkifyMaskType = 0;
private boolean mNotifyOnInlineViewLayout;
private boolean mSelectableText = false;
private ReactViewBackgroundManager mReactBackgroundManager;
private Spannable mSpanned;
public ReactTextView(Context context) {
super(context);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
mDefaultGravityHorizontal =
getGravity() & (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
mDefaultGravityVertical = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
}
private static WritableMap inlineViewJson(
int visibility, int index, int left, int top, int right, int bottom) {
WritableMap json = Arguments.createMap();
if (visibility == View.GONE) {
json.putString("visibility", "gone");
json.putInt("index", index);
} else if (visibility == View.VISIBLE) {
json.putString("visibility", "visible");
json.putInt("index", index);
json.putDouble("left", PixelUtil.toDIPFromPixel(left));
json.putDouble("top", PixelUtil.toDIPFromPixel(top));
json.putDouble("right", PixelUtil.toDIPFromPixel(right));
json.putDouble("bottom", PixelUtil.toDIPFromPixel(bottom));
} else {
json.putString("visibility", "unknown");
json.putInt("index", index);
}
return json;
}
private ReactContext getReactContext() {
Context context = getContext();
return (context instanceof TintContextWrapper)
? (ReactContext) ((TintContextWrapper) context).getBaseContext()
: (ReactContext) context;
}
@Override
protected void onLayout(
boolean changed, int textViewLeft, int textViewTop, int textViewRight, int textViewBottom) {
// TODO T62882314: Delete this method when Fabric is fully released in OSS
if (!(getText() instanceof Spanned)
|| ViewUtil.getUIManagerType(getId()) == UIManagerType.FABRIC) {
/**
* In general, {@link #setText} is called via {@link ReactTextViewManager#updateExtraData}
* before we are laid out. This ordering is a requirement because we utilize the data from
* setText in onLayout.
*
* <p>However, it's possible for us to get an extra layout before we've received our setText
* call. If this happens before the initial setText call, then getText() will have its default
* value which isn't a Spanned and we need to bail out. That's fine because we'll get a
* setText followed by a layout later.
*
* <p>The cause for the extra early layout is that an ancestor gets transitioned from a
* layout-only node to a non layout-only node.
*/
return;
}
ReactContext reactContext = getReactContext();
UIManagerModule uiManager =
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class));
Spanned text = (Spanned) getText();
Layout layout = getLayout();
TextInlineViewPlaceholderSpan[] placeholders =
text.getSpans(0, text.length(), TextInlineViewPlaceholderSpan.class);
ArrayList inlineViewInfoArray =
mNotifyOnInlineViewLayout ? new ArrayList(placeholders.length) : null;
int textViewWidth = textViewRight - textViewLeft;
int textViewHeight = textViewBottom - textViewTop;
for (TextInlineViewPlaceholderSpan placeholder : placeholders) {
View child = uiManager.resolveView(placeholder.getReactTag());
int start = text.getSpanStart(placeholder);
int line = layout.getLineForOffset(start);
boolean isLineTruncated = layout.getEllipsisCount(line) > 0;
if ( // This truncation check works well on recent versions of Android (tested on 5.1.1 and
// 6.0.1) but not on Android 4.4.4. The reason is that getEllipsisCount is buggy on
// Android 4.4.4. Specifically, it incorrectly returns 0 if an inline view is the first
// thing to be truncated.
(isLineTruncated && start >= layout.getLineStart(line) + layout.getEllipsisStart(line))
||
// This truncation check works well on Android 4.4.4 but not on others (e.g. 6.0.1).
// On Android 4.4.4, getLineEnd returns the first truncated character whereas on 6.0.1,
// it appears to return the position after the last character on the line even if that
// character is truncated.
line >= mNumberOfLines
|| start >= layout.getLineEnd(line)) {
// On some versions of Android (e.g. 4.4.4, 5.1.1), getPrimaryHorizontal can infinite
// loop when called on a character that appears after the ellipsis. Avoid this bug by
// special casing the character truncation case.
child.setVisibility(View.GONE);
if (mNotifyOnInlineViewLayout) {
inlineViewInfoArray.add(inlineViewJson(View.GONE, start, -1, -1, -1, -1));
}
} else {
int width = placeholder.getWidth();
int height = placeholder.getHeight();
// Calculate if the direction of the placeholder character is Right-To-Left.
boolean isRtlChar = layout.isRtlCharAt(start);
boolean isRtlParagraph = layout.getParagraphDirection(line) == Layout.DIR_RIGHT_TO_LEFT;
int placeholderHorizontalPosition;
// There's a bug on Samsung devices where calling getPrimaryHorizontal on
// the last offset in the layout will result in an endless loop. Work around
// this bug by avoiding getPrimaryHorizontal in that case.
if (start == text.length() - 1) {
placeholderHorizontalPosition =
isRtlParagraph
// Equivalent to `layout.getLineLeft(line)` but `getLineLeft` returns incorrect
// values when the paragraph is RTL and `setSingleLine(true)`.
? textViewWidth - (int) layout.getLineWidth(line)
: (int) layout.getLineRight(line) - width;
} else {
// The direction of the paragraph may not be exactly the direction the string is heading
// in at the
// position of the placeholder. So, if the direction of the character is the same as the
// paragraph
// use primary, secondary otherwise.
boolean characterAndParagraphDirectionMatch = isRtlParagraph == isRtlChar;
placeholderHorizontalPosition =
characterAndParagraphDirectionMatch
? (int) layout.getPrimaryHorizontal(start)
: (int) layout.getSecondaryHorizontal(start);
if (isRtlParagraph) {
// Adjust `placeholderHorizontalPosition` to work around an Android bug.
// The bug is when the paragraph is RTL and `setSingleLine(true)`, some layout
// methods such as `getPrimaryHorizontal`, `getSecondaryHorizontal`, and
// `getLineRight` return incorrect values. Their return values seem to be off
// by the same number of pixels so subtracting these values cancels out the error.
//
// The result is equivalent to bugless versions of
// `getPrimaryHorizontal`/`getSecondaryHorizontal`.
placeholderHorizontalPosition =
textViewWidth - ((int) layout.getLineRight(line) - placeholderHorizontalPosition);
}
if (isRtlChar) {
placeholderHorizontalPosition -= width;
}
}
int leftRelativeToTextView =
isRtlChar
? placeholderHorizontalPosition + getTotalPaddingRight()
: placeholderHorizontalPosition + getTotalPaddingLeft();
int left = textViewLeft + leftRelativeToTextView;
// Vertically align the inline view to the baseline of the line of text.
int topRelativeToTextView = getTotalPaddingTop() + layout.getLineBaseline(line) - height;
int top = textViewTop + topRelativeToTextView;
boolean isFullyClipped =
textViewWidth <= leftRelativeToTextView || textViewHeight <= topRelativeToTextView;
int layoutVisibility = isFullyClipped ? View.GONE : View.VISIBLE;
int layoutLeft = left;
int layoutTop = top;
int layoutRight = left + width;
int layoutBottom = top + height;
// Keep these parameters in sync with what goes into `inlineViewInfoArray`.
child.setVisibility(layoutVisibility);
child.layout(layoutLeft, layoutTop, layoutRight, layoutBottom);
if (mNotifyOnInlineViewLayout) {
inlineViewInfoArray.add(
inlineViewJson(
layoutVisibility, start, layoutLeft, layoutTop, layoutRight, layoutBottom));
}
}
}
if (mNotifyOnInlineViewLayout) {
Collections.sort(
inlineViewInfoArray,
new Comparator() {
@Override
public int compare(Object o1, Object o2) {
WritableMap m1 = (WritableMap) o1;
WritableMap m2 = (WritableMap) o2;
return m1.getInt("index") - m2.getInt("index");
}
});
WritableArray inlineViewInfoArray2 = Arguments.createArray();
for (Object item : inlineViewInfoArray) {
inlineViewInfoArray2.pushMap((WritableMap) item);
}
WritableMap event = Arguments.createMap();
event.putArray("inlineViews", inlineViewInfoArray2);
reactContext
.getJSModule(RCTEventEmitter.class)
.receiveEvent(getId(), "topInlineViewLayout", event);
}
}
public void setText(ReactTextUpdate update) {
mContainsImages = update.containsImages();
// Android's TextView crashes when it tries to relayout if LayoutParams are
// null; explicitly set the LayoutParams to prevent this crash. See:
// https://github.com/facebook/react-native/pull/7011
if (getLayoutParams() == null) {
setLayoutParams(EMPTY_LAYOUT_PARAMS);
}
Spannable spannable = update.getText();
if (mLinkifyMaskType > 0) {
Linkify.addLinks(spannable, mLinkifyMaskType);
setMovementMethod(LinkMovementMethod.getInstance());
}
setText(spannable);
float paddingLeft = update.getPaddingLeft();
float paddingTop = update.getPaddingTop();
float paddingRight = update.getPaddingRight();
float paddingBottom = update.getPaddingBottom();
// In Fabric padding is set by the update of Layout Metrics and not as part of the "setText"
// operation
// TODO T56559197: remove this condition when we migrate 100% to Fabric
if (paddingLeft != UNSET
&& paddingBottom != UNSET
&& paddingRight != UNSET
&& paddingBottom != UNSET) {
setPadding(
(int) Math.floor(paddingLeft),
(int) Math.floor(paddingTop),
(int) Math.floor(paddingRight),
(int) Math.floor(paddingBottom));
}
int nextTextAlign = update.getTextAlign();
if (mTextAlign != nextTextAlign) {
mTextAlign = nextTextAlign;
}
setGravityHorizontal(mTextAlign);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (getBreakStrategy() != update.getTextBreakStrategy()) {
setBreakStrategy(update.getTextBreakStrategy());
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (getJustificationMode() != update.getJustificationMode()) {
setJustificationMode(update.getJustificationMode());
}
}
// Ensure onLayout is called so the inline views can be repositioned.
requestLayout();
}
@Override
public int reactTagForTouch(float touchX, float touchY) {
CharSequence text = getText();
int target = getId();
int x = (int) touchX;
int y = (int) touchY;
Layout layout = getLayout();
if (layout == null) {
// If the layout is null, the view hasn't been properly laid out yet. Therefore, we can't find
// the exact text tag that has been touched, and the correct tag to return is the default one.
return target;
}
int line = layout.getLineForVertical(y);
int lineStartX = (int) layout.getLineLeft(line);
int lineEndX = (int) layout.getLineRight(line);
// TODO(5966918): Consider extending touchable area for text spans by some DP constant
if (text instanceof Spanned && x >= lineStartX && x <= lineEndX) {
Spanned spannedText = (Spanned) text;
int index = -1;
try {
index = layout.getOffsetForHorizontal(line, x);
} catch (ArrayIndexOutOfBoundsException e) {
// https://issuetracker.google.com/issues/113348914
FLog.e(ReactConstants.TAG, "Crash in HorizontalMeasurementProvider: " + e.getMessage());
return target;
}
// We choose the most inner span (shortest) containing character at the given index
// if no such span can be found we will send the textview's react id as a touch handler
// In case when there are more than one spans with same length we choose the last one
// from the spans[] array, since it correspond to the most inner react element
ReactTagSpan[] spans = spannedText.getSpans(index, index, ReactTagSpan.class);
if (spans != null) {
int targetSpanTextLength = text.length();
for (int i = 0; i < spans.length; i++) {
int spanStart = spannedText.getSpanStart(spans[i]);
int spanEnd = spannedText.getSpanEnd(spans[i]);
if (spanEnd > index && (spanEnd - spanStart) <= targetSpanTextLength) {
target = spans[i].getReactTag();
targetSpanTextLength = (spanEnd - spanStart);
}
}
}
}
return target;
}
@Override
protected boolean verifyDrawable(Drawable drawable) {
if (mContainsImages && getText() instanceof Spanned) {
Spanned text = (Spanned) 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 && getText() instanceof Spanned) {
Spanned text = (Spanned) 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 && getText() instanceof Spanned) {
Spanned text = (Spanned) getText();
TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);
for (TextInlineImageSpan span : spans) {
span.onDetachedFromWindow();
}
}
}
@Override
public void onStartTemporaryDetach() {
super.onStartTemporaryDetach();
if (mContainsImages && getText() instanceof Spanned) {
Spanned text = (Spanned) getText();
TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);
for (TextInlineImageSpan span : spans) {
span.onStartTemporaryDetach();
}
}
}
public void setSelectableText(boolean value) {
mSelectableText = value;
invalidate();
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
if (mContainsImages && getText() instanceof Spanned) {
Spanned text = (Spanned) getText();
TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);
for (TextInlineImageSpan span : spans) {
span.onAttachedToWindow();
}
}
}
@Override
public void onFinishTemporaryDetach() {
super.onFinishTemporaryDetach();
if (mContainsImages && getText() instanceof Spanned) {
Spanned text = (Spanned) getText();
TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);
for (TextInlineImageSpan span : spans) {
span.onFinishTemporaryDetach();
}
}
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
setTextIsSelectable(mSelectableText);
}
@Override
public boolean hasOverlappingRendering() {
return false;
}
/* 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);
}
public void setNumberOfLines(int numberOfLines) {
mNumberOfLines = numberOfLines == 0 ? ViewDefaults.NUMBER_OF_LINES : numberOfLines;
setSingleLine(mNumberOfLines == 1);
setMaxLines(mNumberOfLines);
}
public void setAdjustFontSizeToFit(boolean adjustsFontSizeToFit) {
mAdjustsFontSizeToFit = adjustsFontSizeToFit;
}
public void setEllipsizeLocation(TextUtils.TruncateAt ellipsizeLocation) {
mEllipsizeLocation = ellipsizeLocation;
}
public void setNotifyOnInlineViewLayout(boolean notifyOnInlineViewLayout) {
mNotifyOnInlineViewLayout = notifyOnInlineViewLayout;
}
public void updateView() {
@Nullable
TextUtils.TruncateAt ellipsizeLocation =
mNumberOfLines == ViewDefaults.NUMBER_OF_LINES || mAdjustsFontSizeToFit
? null
: mEllipsizeLocation;
setEllipsize(ellipsizeLocation);
}
@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 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 setSpanned(Spannable spanned) {
mSpanned = spanned;
}
public Spannable getSpanned() {
return mSpanned;
}
public void setLinkifyMask(int mask) {
mLinkifyMaskType = mask;
}
}