Skip to content

Commit

Permalink
Nullsafe react/views/view (#46490)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46490

Fix nulls to prepare this module for further Kotlin conversion

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D62641672
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Sep 18, 2024
1 parent 7de3111 commit cc8b8fe
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void handleAddView(View view) {
*
* @param view The view that is being removed.
*/
public void handleRemoveView(View view) {
public void handleRemoveView(@Nullable View view) {
if (ViewGroupManager.getViewZIndex(view) != null) {
mNumberOfChildrenWithZIndex--;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void setViewZIndex(View view, int zIndex) {
mZIndexHash.put(view, zIndex);
}

public static @Nullable Integer getViewZIndex(View view) {
public static @Nullable Integer getViewZIndex(@Nullable View view) {
return mZIndexHash.get(view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public class ReactModalHostView(context: ThemedReactContext) :
* styleHeight on the LayoutShadowNode to be the window size. This is done through the
* UIManagerModule, and will then cause the children to layout as if they can fill the window.
*/
public class DialogRootViewGroup internal constructor(context: Context?) :
public class DialogRootViewGroup internal constructor(context: Context) :
ReactViewGroup(context), RootView {
internal var stateWrapper: StateWrapper? = null
internal var eventDispatcher: EventDispatcher? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@
import android.graphics.drawable.RippleDrawable;
import android.util.TypedValue;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.SoftAssertions;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ViewProps;

/**
* Utility class that helps with converting android drawable description used in JS to an actual
* instance of {@link Drawable}.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactDrawableHelper {

private static final TypedValue sResolveOutValue = new TypedValue();

public static Drawable createDrawableFromJSDescription(
public static @Nullable Drawable createDrawableFromJSDescription(
Context context, ReadableMap drawableDescriptionDict) {
String type = drawableDescriptionDict.getString("type");
if ("ThemeAttrAndroid".equals(type)) {
String attr = drawableDescriptionDict.getString("attribute");
if (attr == null) {
throw new JSApplicationIllegalArgumentException("JS description missing 'attribute' field");
}
int attrId = getAttrId(context, attr);
if (!context.getTheme().resolveAttribute(attrId, sResolveOutValue, true)) {
throw new JSApplicationIllegalArgumentException(
Expand All @@ -50,7 +54,6 @@ public static Drawable createDrawableFromJSDescription(
}

private static int getAttrId(Context context, String attr) {
SoftAssertions.assertNotNull(attr);
if ("selectableItemBackground".equals(attr)) {
return android.R.attr.selectableItemBackground;
} else if ("selectableItemBackgroundBorderless".equals(attr)) {
Expand All @@ -60,7 +63,7 @@ private static int getAttrId(Context context, String attr) {
}
}

private static Drawable getDefaultThemeDrawable(Context context) {
private static @Nullable Drawable getDefaultThemeDrawable(Context context) {
return context.getResources().getDrawable(sResolveOutValue.resourceId, context.getTheme());
}

Expand All @@ -74,7 +77,8 @@ private static RippleDrawable getRippleDrawable(
return new RippleDrawable(colorStateList, null, mask);
}

private static Drawable setRadius(ReadableMap drawableDescriptionDict, Drawable drawable) {
private static @Nullable Drawable setRadius(
ReadableMap drawableDescriptionDict, @Nullable Drawable drawable) {
if (drawableDescriptionDict.hasKey("rippleRadius") && drawable instanceof RippleDrawable) {
RippleDrawable rippleDrawable = (RippleDrawable) drawable;
double rippleRadius = drawableDescriptionDict.getDouble("rippleRadius");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.views.view;

import static com.facebook.infer.annotation.Assertions.nullsafeFIXME;
import static com.facebook.react.common.ReactConstants.TAG;

import android.annotation.SuppressLint;
Expand All @@ -28,6 +29,7 @@
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.R;
import com.facebook.react.bridge.ReactNoCrashSoftException;
import com.facebook.react.bridge.ReactSoftExceptionLogger;
Expand Down Expand Up @@ -63,6 +65,7 @@
* Backing for a React View. Has support for borders, but since borders aren't common, lazy
* initializes most of the storage needed for them.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactViewGroup extends ViewGroup
implements ReactInterceptingViewGroup,
ReactClippingViewGroup,
Expand Down Expand Up @@ -381,7 +384,7 @@ public boolean getRemoveClippedSubviews() {

@Override
public void getClippingRect(Rect outClippingRect) {
outClippingRect.set(mClippingRect);
outClippingRect.set(nullsafeFIXME(mClippingRect, "Fix in Kotlin"));
}

@Override
Expand Down Expand Up @@ -522,7 +525,7 @@ private void handleAddView(View view) {
}
}

private void handleRemoveView(View view) {
private void handleRemoveView(@Nullable View view) {
UiThreadUtil.assertOnUiThread();

if (!customDrawOrderDisabled()) {
Expand All @@ -546,7 +549,7 @@ private void handleRemoveViews(int start, int count) {
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
public void addView(View child, int index, @Nullable ViewGroup.LayoutParams params) {
// This will get called for every overload of addView so there is not need to override every
// method.
handleAddView(child);
Expand All @@ -561,7 +564,7 @@ protected boolean addViewInLayout(
}

@Override
public void removeView(View view) {
public void removeView(@Nullable View view) {
handleRemoveView(view);
super.removeView(view);
}
Expand Down

0 comments on commit cc8b8fe

Please sign in to comment.