Skip to content

Commit

Permalink
Fix null safety in views/text/frescosupport (facebook#48612)
Browse files Browse the repository at this point in the history
Summary:

Manual fixes to resolve nullsafe issues.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D67992722
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Jan 14, 2025
1 parent 95a5d84 commit 7516c75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.res.Resources;
import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.core.util.Preconditions;
import com.facebook.common.logging.FLog;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
Expand All @@ -32,8 +33,7 @@
class FrescoBasedReactTextInlineImageShadowNode extends ReactTextInlineImageShadowNode {

private @Nullable Uri mUri;
// NULLSAFE_FIXME[Field Not Initialized]
private ReadableMap mHeaders;
private @Nullable ReadableMap mHeaders;
private final AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private final @Nullable Object mCallerContext;
private float mWidth = YogaConstants.UNDEFINED;
Expand All @@ -49,9 +49,10 @@ public FrescoBasedReactTextInlineImageShadowNode(

@ReactProp(name = "src")
public void setSource(@Nullable ReadableArray sources) {
final String source =
// NULLSAFE_FIXME[Nullable Dereference]
(sources == null || sources.size() == 0) ? null : sources.getMap(0).getString("uri");
final @Nullable String source =
(sources == null || sources.size() == 0 || sources.getType(0) != ReadableType.Map)
? null
: Preconditions.checkNotNull(sources.getMap(0)).getString("uri");
Uri uri = null;
if (source != null) {
try {
Expand All @@ -74,7 +75,7 @@ public void setSource(@Nullable ReadableArray sources) {
}

@ReactProp(name = "headers")
public void setHeaders(ReadableMap headers) {
public void setHeaders(@Nullable ReadableMap headers) {
mHeaders = headers;
}

Expand Down Expand Up @@ -113,7 +114,7 @@ public void setResizeMode(@Nullable String resizeMode) {
return mUri;
}

public ReadableMap getHeaders() {
public @Nullable ReadableMap getHeaders() {
return mHeaders;
}

Expand Down Expand Up @@ -149,7 +150,6 @@ public TextInlineImageSpan buildInlineImageSpan() {
getHeaders(),
getDraweeControllerBuilder(),
getCallerContext(),
// NULLSAFE_FIXME[Parameter Not Nullable]
mResizeMode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import android.net.Uri;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.core.util.Preconditions;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
Expand Down Expand Up @@ -53,8 +53,8 @@ class FrescoBasedReactTextInlineImageSpan extends TextInlineImageSpan {
private int mTintColor;
private Uri mUri;
private int mWidth;
private ReadableMap mHeaders;
private String mResizeMode;
private @Nullable ReadableMap mHeaders;
private @Nullable String mResizeMode;

private @Nullable TextView mTextView;

Expand All @@ -64,10 +64,10 @@ public FrescoBasedReactTextInlineImageSpan(
int width,
int tintColor,
@Nullable Uri uri,
ReadableMap headers,
@Nullable ReadableMap headers,
AbstractDraweeControllerBuilder draweeControllerBuilder,
@Nullable Object callerContext,
String resizeMode) {
@Nullable String resizeMode) {
mDraweeHolder = new DraweeHolder(GenericDraweeHierarchyBuilder.newInstance(resources).build());
mDraweeControllerBuilder = draweeControllerBuilder;
mCallerContext = callerContext;
Expand Down Expand Up @@ -118,8 +118,8 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon
return mWidth;
}

// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
public void setTextView(TextView textView) {
@Override
public void setTextView(@Nullable TextView textView) {
mTextView = textView;
}

Expand All @@ -138,26 +138,24 @@ public void draw(
ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(mUri);
ImageRequest imageRequest =
ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, mHeaders);
mDraweeHolder.getHierarchy().setActualImageScaleType(getResizeMode(mResizeMode));
DraweeController draweeController =
mDraweeControllerBuilder
.reset()
.setOldController(mDraweeHolder.getController())
// NULLSAFE_FIXME[Parameter Not Nullable]
.setCallerContext(mCallerContext)
.setImageRequest(imageRequest)
.build();
mDraweeHolder
.getHierarchy()
.setActualImageScaleType(ImageResizeMode.toScaleType(mResizeMode));
mDraweeControllerBuilder.reset();
mDraweeControllerBuilder.setOldController(mDraweeHolder.getController());
if (mCallerContext != null) {
mDraweeControllerBuilder.setCallerContext(mCallerContext);
}
mDraweeControllerBuilder.setImageRequest(imageRequest);
DraweeController draweeController = mDraweeControllerBuilder.build();
mDraweeHolder.setController(draweeController);
mDraweeControllerBuilder.reset();

mDrawable = mDraweeHolder.getTopLevelDrawable();
// NULLSAFE_FIXME[Nullable Dereference]
mDrawable = Preconditions.checkNotNull(mDraweeHolder.getTopLevelDrawable());
mDrawable.setBounds(0, 0, mWidth, mHeight);
if (mTintColor != 0) {
// NULLSAFE_FIXME[Nullable Dereference]
mDrawable.setColorFilter(mTintColor, PorterDuff.Mode.SRC_IN);
}
// NULLSAFE_FIXME[Nullable Dereference]
mDrawable.setCallback(mTextView);
}

Expand All @@ -168,19 +166,13 @@ public void draw(
// Align to center
int fontHeight = (int) (paint.descent() - paint.ascent());
int centerY = y + (int) paint.descent() - fontHeight / 2;
// NULLSAFE_FIXME[Nullable Dereference]
int transY = centerY - (mDrawable.getBounds().bottom - mDrawable.getBounds().top) / 2;

canvas.translate(x, transY);
// NULLSAFE_FIXME[Nullable Dereference]
mDrawable.draw(canvas);
canvas.restore();
}

private ScalingUtils.ScaleType getResizeMode(String resizeMode) {
return ImageResizeMode.toScaleType(resizeMode);
}

@Override
public int getWidth() {
return mWidth;
Expand Down

0 comments on commit 7516c75

Please sign in to comment.