Skip to content

Commit

Permalink
Nullsafe react/views/view (facebook#46490)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#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 16, 2024
1 parent 40c875d commit 7f7964e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 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 @@ -8,11 +8,13 @@
package com.facebook.react.views.view;

import android.content.Context;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable;

/**
* @deprecated Please use {@link CSSBackgroundDrawable} instead
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
@Deprecated(since = "0.75.0", forRemoval = true)
public class ReactViewBackgroundDrawable extends CSSBackgroundDrawable {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.facebook.react.views.view;

import static com.facebook.infer.annotation.Assertions.nullsafeFIXME;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
Expand All @@ -17,10 +19,12 @@
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.common.annotations.UnstableReactNativeAPI;
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable;

/** Class that manages the background for views and borders. */
@Nullsafe(Nullsafe.Mode.LOCAL)
@UnstableReactNativeAPI
public class ReactViewBackgroundManager {
private static enum Overflow {
Expand All @@ -40,7 +44,7 @@ public ReactViewBackgroundManager(View view) {

public void cleanup() {
ViewCompat.setBackground(mView, null);
mView = null;
mView = nullsafeFIXME(null, "Fix in Kotlin");
mCSSBackgroundDrawable = null;
}

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 @@ -30,6 +31,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.ReactContext;
import com.facebook.react.bridge.ReactNoCrashSoftException;
Expand Down Expand Up @@ -75,6 +77,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 @@ -446,7 +449,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 @@ -587,7 +590,7 @@ private void handleAddView(View view) {
}
}

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

if (!customDrawOrderDisabled()) {
Expand All @@ -611,7 +614,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 @@ -626,7 +629,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 7f7964e

Please sign in to comment.