diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/common/ViewHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/common/ViewHelper.java index a560cafcb..acbcefd6d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/common/ViewHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/common/ViewHelper.java @@ -4,6 +4,7 @@ package com.facebook.react.views.common; import android.graphics.drawable.Drawable; import android.os.Build; +import android.support.annotation.Nullable; import android.view.View; /** Helper class for Views */ @@ -18,7 +19,7 @@ public class ViewHelper { * @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the * background */ - public static void setBackground(View view, Drawable drawable) { + public static void setBackground(View view, @Nullable Drawable drawable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(drawable); } else { diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundManager.java index 0248b93c6..1076c9ba3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundManager.java @@ -57,6 +57,10 @@ public class ReactViewBackgroundManager { getOrCreateReactViewBackground().setRadius(borderRadius); } + public boolean hasRoundedBorders() { + return getOrCreateReactViewBackground().hasRoundedBorders(); + } + public void setBorderRadius(float borderRadius, int position) { getOrCreateReactViewBackground().setRadius(borderRadius, position); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java index e0984f86e..3351c2845 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java @@ -35,6 +35,7 @@ import com.facebook.react.uimanager.ReactClippingViewGroupHelper; import com.facebook.react.uimanager.ReactPointerEventsView; import com.facebook.react.uimanager.ReactZIndexedViewGroup; import com.facebook.react.uimanager.ViewGroupDrawingOrderHelper; +import com.facebook.react.views.common.ViewHelper; import com.facebook.yoga.YogaConstants; import javax.annotation.Nullable; @@ -51,6 +52,7 @@ public class ReactViewGroup extends ViewGroup implements private static final LayoutParams sDefaultLayoutParam = new ViewGroup.LayoutParams(0, 0); /* should only be used in {@link #updateClippingToRect} */ private static final Rect sHelperRect = new Rect(); + private ReactViewBackgroundManager mReactBackgroundManager; /** * This listener will be set for child views when removeClippedSubview property is enabled. When @@ -111,6 +113,7 @@ public class ReactViewGroup extends ViewGroup implements public ReactViewGroup(Context context) { super(context); mDrawingOrderHelper = new ViewGroupDrawingOrderHelper(this); + mReactBackgroundManager = new ReactViewBackgroundManager(this); } @Override @@ -144,17 +147,19 @@ public class ReactViewGroup extends ViewGroup implements @Override public void setBackgroundColor(int color) { - if (color == Color.TRANSPARENT && mReactBackgroundDrawable == null) { - // don't do anything, no need to allocate ReactBackgroundDrawable for transparent background - } else { - getOrCreateReactViewBackground().setColor(color); - } + mReactBackgroundManager.setBackgroundColor(color); } - @Override - public void setBackground(Drawable drawable) { - throw new UnsupportedOperationException( - "This method is not supported for ReactViewGroup instances"); + 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 setBorderStyle(@Nullable String style) { + mReactBackgroundManager.setBorderStyle(style); } public void setTranslucentBackgroundDrawable(@Nullable Drawable background) { @@ -162,13 +167,13 @@ public class ReactViewGroup extends ViewGroup implements // background to be a layer drawable that contains a drawable that has been previously setup // as a background previously. This will not work correctly as the drawable callback logic is // messed up in AOSP - updateBackgroundDrawable(null); + ViewHelper.setBackground(this, null); if (mReactBackgroundDrawable != null && background != null) { LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background}); - updateBackgroundDrawable(layerDrawable); + ViewHelper.setBackground(this, layerDrawable); } else if (background != null) { - updateBackgroundDrawable(background); + ViewHelper.setBackground(this, background); } } @@ -220,22 +225,13 @@ public class ReactViewGroup extends ViewGroup implements mNeedsOffscreenAlphaCompositing = needsOffscreenAlphaCompositing; } - public void setBorderWidth(int position, float width) { - getOrCreateReactViewBackground().setBorderWidth(position, width); - } - - public void setBorderColor(int position, float rgb, float alpha) { - getOrCreateReactViewBackground().setBorderColor(position, rgb, alpha); - } - public void setBorderRadius(float borderRadius) { - ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground(); - backgroundDrawable.setRadius(borderRadius); + mReactBackgroundManager.setBorderRadius(borderRadius); if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { final int UPDATED_LAYER_TYPE = - backgroundDrawable.hasRoundedBorders() + mReactBackgroundManager.hasRoundedBorders() ? View.LAYER_TYPE_SOFTWARE : View.LAYER_TYPE_HARDWARE; @@ -246,13 +242,12 @@ public class ReactViewGroup extends ViewGroup implements } public void setBorderRadius(float borderRadius, int position) { - ReactViewBackgroundDrawable backgroundDrawable = getOrCreateReactViewBackground(); - backgroundDrawable.setRadius(borderRadius, position); + mReactBackgroundManager.setBorderRadius(borderRadius, position); if (Build.VERSION_CODES.HONEYCOMB < Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { final int UPDATED_LAYER_TYPE = - backgroundDrawable.hasRoundedBorders() + mReactBackgroundManager.hasRoundedBorders() ? View.LAYER_TYPE_SOFTWARE : View.LAYER_TYPE_HARDWARE; @@ -262,10 +257,6 @@ public class ReactViewGroup extends ViewGroup implements } } - public void setBorderStyle(@Nullable String style) { - getOrCreateReactViewBackground().setBorderStyle(style); - } - @Override public void setRemoveClippedSubviews(boolean removeClippedSubviews) { if (removeClippedSubviews == mRemoveClippedSubviews) { @@ -600,32 +591,6 @@ public class ReactViewGroup extends ViewGroup implements return DEFAULT_BACKGROUND_COLOR; } - private ReactViewBackgroundDrawable getOrCreateReactViewBackground() { - if (mReactBackgroundDrawable == null) { - mReactBackgroundDrawable = new ReactViewBackgroundDrawable(getContext()); - Drawable backgroundDrawable = getBackground(); - updateBackgroundDrawable( - null); // required so that drawable callback is cleared before we add the - // drawable back as a part of LayerDrawable - if (backgroundDrawable == null) { - updateBackgroundDrawable(mReactBackgroundDrawable); - } else { - LayerDrawable layerDrawable = - new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, backgroundDrawable}); - updateBackgroundDrawable(layerDrawable); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - mLayoutDirection = - I18nUtil.getInstance().isRTL(getContext()) - ? LAYOUT_DIRECTION_RTL - : LAYOUT_DIRECTION_LTR; - mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection); - } - } - return mReactBackgroundDrawable; - } - @Override public @Nullable Rect getHitSlopRect() { return mHitSlopRect; @@ -640,21 +605,6 @@ public class ReactViewGroup extends ViewGroup implements invalidate(); } - /** - * Set the background for the view or remove the background. It calls {@link - * #setBackground(Drawable)} or {@link #setBackgroundDrawable(Drawable)} based on the sdk version. - * - * @param drawable {@link Drawable} The Drawable to use as the background, or null to remove the - * background - */ - private void updateBackgroundDrawable(Drawable drawable) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - super.setBackground(drawable); - } else { - super.setBackgroundDrawable(drawable); - } - } - @Override protected void dispatchDraw(Canvas canvas) { if (mOverflow != null) {