From 7170543e8012250b7643a960b54cce7fd6d3a1e9 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Wed, 8 Nov 2017 10:27:39 -0800 Subject: [PATCH] Make direction-aware borders work with APIs >= 17 Reviewed By: achen1 Differential Revision: D6189497 fbshipit-source-id: 848ca35540c5a4eb1581e0b7c39f8fa880540317 --- .../view/ReactViewBackgroundDrawable.java | 38 ++++++++++++++----- .../react/views/view/ReactViewGroup.java | 10 ++--- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java index 477526d86acf5f..b32855b1450625 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java @@ -109,6 +109,7 @@ private static enum BorderStyle { private @Nullable float[] mBorderCornerRadii; private final Context mContext; + private int mLayoutDirection; public enum BorderRadiusLocation { TOP_LEFT, @@ -290,6 +291,25 @@ public void setColor(int color) { invalidateSelf(); } + /** Similar to Drawable.getLayoutDirection, but available in APIs < 23. */ + public int getResolvedLayoutDirection() { + return mLayoutDirection; + } + + /** Similar to Drawable.setLayoutDirection, but available in APIs < 23. */ + public boolean setResolvedLayoutDirection(int layoutDirection) { + if (mLayoutDirection != layoutDirection) { + mLayoutDirection = layoutDirection; + return onResolvedLayoutDirectionChanged(layoutDirection); + } + return false; + } + + /** Similar to Drawable.onLayoutDirectionChanged, but available in APIs < 23. */ + public boolean onResolvedLayoutDirectionChanged(int layoutDirection) { + return false; + } + @VisibleForTesting public int getColor() { return mColor; @@ -323,8 +343,8 @@ private void drawRoundedBackgroundWithBorders(Canvas canvas) { int colorRight = getBorderColor(Spacing.RIGHT); int colorBottom = getBorderColor(Spacing.BOTTOM); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL; int colorStart = getBorderColor(Spacing.START); int colorEnd = getBorderColor(Spacing.END); @@ -478,13 +498,13 @@ private void updatePath() { float bottomRightRadius = getBorderRadiusOrDefaultTo(borderRadius, BorderRadiusLocation.BOTTOM_RIGHT); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL; float topStartRadius = getBorderRadius(BorderRadiusLocation.TOP_START); float topEndRadius = getBorderRadius(BorderRadiusLocation.TOP_END); float bottomStartRadius = getBorderRadius(BorderRadiusLocation.BOTTOM_START); float bottomEndRadius = getBorderRadius(BorderRadiusLocation.BOTTOM_END); - + if (I18nUtil.getInstance().doLeftAndRightSwapInRTL(mContext)) { if (YogaConstants.isUndefined(topStartRadius)) { topStartRadius = topLeftRadius; @@ -930,8 +950,8 @@ private void drawRectangularBackgroundWithBorders(Canvas canvas) { int colorRight = getBorderColor(Spacing.RIGHT); int colorBottom = getBorderColor(Spacing.BOTTOM); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL; int colorStart = getBorderColor(Spacing.START); int colorEnd = getBorderColor(Spacing.END); @@ -1140,8 +1160,8 @@ public RectF getDirectionAwareBorderInsets() { float borderLeftWidth = getBorderWidthOrDefaultTo(borderWidth, Spacing.LEFT); float borderRightWidth = getBorderWidthOrDefaultTo(borderWidth, Spacing.RIGHT); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mBorderWidth != null) { - final boolean isRTL = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mBorderWidth != null) { + final boolean isRTL = getResolvedLayoutDirection() == View.LAYOUT_DIRECTION_RTL; float borderStartWidth = mBorderWidth.getRaw(Spacing.START); float borderEndWidth = mBorderWidth.getRaw(Spacing.END); 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 592f328e9663ff..8e80168a33b29e 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 @@ -129,9 +129,9 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto @Override public void onRtlPropertiesChanged(int layoutDirection) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (mReactBackgroundDrawable != null) { - mReactBackgroundDrawable.setLayoutDirection(mLayoutDirection); + mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection); } } } @@ -589,12 +589,12 @@ private ReactViewBackgroundDrawable getOrCreateReactViewBackground() { updateBackgroundDrawable(layerDrawable); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { mLayoutDirection = I18nUtil.getInstance().isRTL(getContext()) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR; - mReactBackgroundDrawable.setLayoutDirection(mLayoutDirection); + mReactBackgroundDrawable.setResolvedLayoutDirection(mLayoutDirection); } } return mReactBackgroundDrawable; @@ -671,7 +671,7 @@ protected void dispatchDraw(Canvas canvas) { mReactBackgroundDrawable.getBorderRadiusOrDefaultTo( borderRadius, ReactViewBackgroundDrawable.BorderRadiusLocation.BOTTOM_RIGHT); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { final boolean isRTL = mLayoutDirection == View.LAYOUT_DIRECTION_RTL; float topStartBorderRadius = mReactBackgroundDrawable.getBorderRadius(