From 09401ed56f974184e36e31bc9e457a3e3349d054 Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Thu, 13 Jul 2017 16:07:53 -0700 Subject: [PATCH] Android: Support setting background color on ARTSurfaceView Summary: This fixes support for the `backgroundColor` style prop on an `ART.Surface`. `ARTSurfaceViewManager` inherits its `setBackgroundColor` implementation from `BaseViewManager`. This implementation broke in API 24 because API 24 removed support for `setBackgroundColor`on `TextureViews`. `ARTSurfaceView` inherits from `TextureView` so it also lost support for `setBackgroundColor`. To fix this, the implementation of `ART.Surface's` `setBackgroundColor` was moved to the shadow node. The implementation now draws the background color on the canvas. In a test app, verified that initializing and changing the background color on an `ART.Surface` works. Verified that the background renders as transparent when a background color isn't set. Also, this change is being used in my team's app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14117 Differential Revision: D5419574 Pulled By: hramos fbshipit-source-id: 022bfed553e33e2d493f68b4bf5aa16dd304934d --- .../react/views/art/ARTSurfaceViewManager.java | 8 ++++++++ .../react/views/art/ARTSurfaceViewShadowNode.java | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewManager.java index 13745a58a..88046a62c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewManager.java @@ -64,4 +64,12 @@ public class ARTSurfaceViewManager extends public void updateExtraData(ARTSurfaceView root, Object extraData) { root.setSurfaceTextureListener((ARTSurfaceViewShadowNode) extraData); } + + @Override + public void setBackgroundColor(ARTSurfaceView view, int backgroundColor) { + // As of Android N TextureView does not support calling setBackground on it. + // It will also throw an exception when target SDK is set to N or higher. + + // Setting the background color for this view is handled in the shadow node. + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewShadowNode.java index 538c0792f..e3c78600b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/art/ARTSurfaceViewShadowNode.java @@ -24,6 +24,8 @@ import com.facebook.react.common.ReactConstants; import com.facebook.react.uimanager.LayoutShadowNode; import com.facebook.react.uimanager.UIViewOperationQueue; import com.facebook.react.uimanager.ReactShadowNode; +import com.facebook.react.uimanager.ViewProps; +import com.facebook.react.uimanager.annotations.ReactProp; /** * Shadow node for ART virtual tree root - ARTSurfaceView @@ -33,6 +35,14 @@ public class ARTSurfaceViewShadowNode extends LayoutShadowNode private @Nullable Surface mSurface; + private @Nullable Integer mBackgroundColor; + + @ReactProp(name = ViewProps.BACKGROUND_COLOR, customType = "Color") + public void setBackgroundColor(Integer color) { + mBackgroundColor = color; + markUpdated(); + } + @Override public boolean isVirtual() { return false; @@ -59,6 +69,9 @@ public class ARTSurfaceViewShadowNode extends LayoutShadowNode try { Canvas canvas = mSurface.lockCanvas(null); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); + if (mBackgroundColor != null) { + canvas.drawColor(mBackgroundColor); + } Paint paint = new Paint(); for (int i = 0; i < getChildCount(); i++) {