Fix setBackground for Android sdk version 15 and below

Reviewed By: achen1

Differential Revision: D5854430

fbshipit-source-id: 1276f3d7e94b757f9a9dd412a2ef8b72e8427ffb
This commit is contained in:
David Vacca 2017-09-19 09:20:40 -07:00 committed by Facebook Github Bot
parent 87a1dc4b76
commit 5180995666
9 changed files with 178 additions and 138 deletions

View File

@ -0,0 +1,14 @@
include_defs("//ReactAndroid/DEFS")
android_library(
name = "common",
srcs = glob(["*.java"]),
provided_deps = [
react_native_dep("third-party/android/support/v4:lib-support-v4"),
],
visibility = [
"PUBLIC",
],
deps = [
],
)

View File

@ -0,0 +1,28 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.views.common;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
/** Helper class for Views */
public class ViewHelper {
/**
* Set the background to a given Drawable, or remove the background. It calls {@link
* View#setBackground(Drawable)} or {@link View#setBackgroundDrawable(Drawable)} based on the sdk
* version.
*
* @param view {@link View} to apply the background.
* @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) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
}
}

View File

@ -16,7 +16,6 @@ import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
@ -25,7 +24,7 @@ import com.facebook.react.uimanager.MeasureSpecAssertions;
import com.facebook.react.uimanager.ReactClippingViewGroup;
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import javax.annotation.Nullable;
/**
@ -49,7 +48,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
private @Nullable String mScrollPerfTag;
private @Nullable Drawable mEndBackground;
private int mEndFillColor = Color.TRANSPARENT;
private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable;
private ReactViewBackgroundManager mReactBackgroundManager;
public ReactHorizontalScrollView(Context context) {
this(context, null);
@ -57,6 +56,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
public ReactHorizontalScrollView(Context context, @Nullable FpsListener fpsListener) {
super(context);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
mFpsListener = fpsListener;
}
@ -325,47 +325,27 @@ public class ReactHorizontalScrollView extends HorizontalScrollView 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);
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
mReactBackgroundManager.setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
getOrCreateReactViewBackground().setBorderColor(position, color, alpha);
mReactBackgroundManager.setBorderColor(position, color, alpha);
}
public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius);
mReactBackgroundManager.setBorderRadius(borderRadius);
}
public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
mReactBackgroundManager.setBorderRadius(borderRadius, position);
}
public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
mReactBackgroundManager.setBorderStyle(style);
}
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable();
Drawable backgroundDrawable = getBackground();
super.setBackground(null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
super.setBackground(mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[]{mReactBackgroundDrawable, backgroundDrawable});
super.setBackground(layerDrawable);
}
}
return mReactBackgroundDrawable;
}
}

View File

@ -14,7 +14,6 @@ import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
@ -28,7 +27,7 @@ import com.facebook.react.uimanager.MeasureSpecAssertions;
import com.facebook.react.uimanager.ReactClippingViewGroup;
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import java.lang.reflect.Field;
import javax.annotation.Nullable;
@ -60,7 +59,7 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
private @Nullable Drawable mEndBackground;
private int mEndFillColor = Color.TRANSPARENT;
private View mContentView;
private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable;
private ReactViewBackgroundManager mReactBackgroundManager;
public ReactScrollView(ReactContext context) {
this(context, null);
@ -69,6 +68,7 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
public ReactScrollView(ReactContext context, @Nullable FpsListener fpsListener) {
super(context);
mFpsListener = fpsListener;
mReactBackgroundManager = new ReactViewBackgroundManager(this);
if (!sTriedToGetScrollerField) {
sTriedToGetScrollerField = true;
@ -394,48 +394,29 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
}
}
@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);
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
mReactBackgroundManager.setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
getOrCreateReactViewBackground().setBorderColor(position, color, alpha);
mReactBackgroundManager.setBorderColor(position, color, alpha);
}
public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius);
mReactBackgroundManager.setBorderRadius(borderRadius);
}
public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
mReactBackgroundManager.setBorderRadius(borderRadius, position);
}
public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
mReactBackgroundManager.setBorderStyle(style);
}
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable();
Drawable backgroundDrawable = getBackground();
super.setBackground(null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
super.setBackground(mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[]{mReactBackgroundDrawable, backgroundDrawable});
super.setBackground(layerDrawable);
}
}
return mReactBackgroundDrawable;
}
}

View File

@ -9,12 +9,8 @@
package com.facebook.react.views.text;
import javax.annotation.Nullable;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.text.Layout;
import android.text.Spanned;
@ -22,10 +18,10 @@ import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.react.uimanager.ReactCompoundView;
import com.facebook.react.uimanager.ViewDefaults;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import javax.annotation.Nullable;
public class ReactTextView extends TextView implements ReactCompoundView {
@ -41,10 +37,11 @@ public class ReactTextView extends TextView implements ReactCompoundView {
private int mNumberOfLines = ViewDefaults.NUMBER_OF_LINES;
private TextUtils.TruncateAt mEllipsizeLocation = TextUtils.TruncateAt.END;
private ReactViewBackgroundDrawable mReactBackgroundDrawable;
private ReactViewBackgroundManager mReactBackgroundManager;
public ReactTextView(Context context) {
super(context);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
mDefaultGravityHorizontal =
getGravity() & (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
mDefaultGravityVertical = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
@ -204,15 +201,6 @@ public class ReactTextView extends TextView implements ReactCompoundView {
}
}
@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);
}
}
/* package */ void setGravityHorizontal(int gravityHorizontal) {
if (gravityHorizontal == 0) {
gravityHorizontal = mDefaultGravityHorizontal;
@ -244,40 +232,28 @@ public class ReactTextView extends TextView implements ReactCompoundView {
setEllipsize(ellipsizeLocation);
}
@Override
public void setBackgroundColor(int color) {
mReactBackgroundManager.setBackgroundColor(color);
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
mReactBackgroundManager.setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
getOrCreateReactViewBackground().setBorderColor(position, color, alpha);
mReactBackgroundManager.setBorderColor(position, color, alpha);
}
public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius);
mReactBackgroundManager.setBorderRadius(borderRadius);
}
public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
mReactBackgroundManager.setBorderRadius(borderRadius, position);
}
public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
}
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable();
Drawable backgroundDrawable = getBackground();
super.setBackground(null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
super.setBackground(mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[]{mReactBackgroundDrawable, backgroundDrawable});
super.setBackground(layerDrawable);
}
}
return mReactBackgroundDrawable;
mReactBackgroundManager.setBorderStyle(style);
}
}

View File

@ -10,11 +10,9 @@
package com.facebook.react.views.textinput;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
@ -39,7 +37,7 @@ import com.facebook.react.views.text.CustomStyleSpan;
import com.facebook.react.views.text.ReactTagSpan;
import com.facebook.react.views.text.ReactTextUpdate;
import com.facebook.react.views.text.TextInlineImageSpan;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;
import com.facebook.react.views.view.ReactViewBackgroundManager;
import java.util.ArrayList;
import javax.annotation.Nullable;
@ -82,7 +80,7 @@ public class ReactEditText extends EditText {
private final InternalKeyListener mKeyListener;
private boolean mDetectScrollMovement = false;
private ReactViewBackgroundDrawable mReactBackgroundDrawable;
private ReactViewBackgroundManager mReactBackgroundManager;
private static final KeyListener sKeyListener = QwertyKeyListener.getInstanceForFullKeyboard();
@ -90,6 +88,7 @@ public class ReactEditText extends EditText {
super(context);
setFocusableInTouchMode(false);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
mInputMethodManager = (InputMethodManager)
Assertions.assertNotNull(getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
mDefaultGravityHorizontal =
@ -577,48 +576,27 @@ public class ReactEditText extends EditText {
@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);
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
mReactBackgroundManager.setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
getOrCreateReactViewBackground().setBorderColor(position, color, alpha);
mReactBackgroundManager.setBorderColor(position, color, alpha);
}
public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius);
mReactBackgroundManager.setBorderRadius(borderRadius);
}
public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
mReactBackgroundManager.setBorderRadius(borderRadius, position);
}
public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
}
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable();
Drawable backgroundDrawable = getBackground();
super.setBackground(null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
super.setBackground(mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[]{mReactBackgroundDrawable, backgroundDrawable});
super.setBackground(layerDrawable);
}
}
return mReactBackgroundDrawable;
mReactBackgroundManager.setBorderStyle(style);
}
/**

View File

@ -14,6 +14,7 @@ android_library(
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/touch:touch"),
react_native_target("java/com/facebook/react/views/common:common"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
],

View File

@ -0,0 +1,67 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.views.view;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.View;
import com.facebook.react.views.common.ViewHelper;
import javax.annotation.Nullable;
/** Class that manages the background for views and borders. */
public class ReactViewBackgroundManager {
private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable;
private View mView;
public ReactViewBackgroundManager(View view) {
this.mView = view;
}
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() {
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable();
Drawable backgroundDrawable = mView.getBackground();
ViewHelper.setBackground(
mView, null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
ViewHelper.setBackground(mView, mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, backgroundDrawable});
ViewHelper.setBackground(mView, layerDrawable);
}
}
return mReactBackgroundDrawable;
}
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);
}
}
public void setBorderWidth(int position, float width) {
getOrCreateReactViewBackground().setBorderWidth(position, width);
}
public void setBorderColor(int position, float color, float alpha) {
getOrCreateReactViewBackground().setBorderColor(position, color, alpha);
}
public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius);
}
public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position);
}
public void setBorderStyle(@Nullable String style) {
getOrCreateReactViewBackground().setBorderStyle(style);
}
}

View File

@ -9,23 +9,21 @@
package com.facebook.react.views.view;
import javax.annotation.Nullable;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.animation.Animation;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.touch.OnInterceptTouchEventListener;
import com.facebook.react.touch.ReactHitSlopView;
import com.facebook.react.touch.ReactInterceptingViewGroup;
import com.facebook.react.touch.OnInterceptTouchEventListener;
import com.facebook.react.uimanager.MeasureSpecAssertions;
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ReactClippingViewGroup;
@ -33,6 +31,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 javax.annotation.Nullable;
/**
* Backing for a React View. Has support for borders, but since borders aren't common, lazy
@ -147,13 +146,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
super.setBackground(null);
updateBackgroundDrawable(null);
if (mReactBackgroundDrawable != null && background != null) {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, background});
super.setBackground(layerDrawable);
updateBackgroundDrawable(layerDrawable);
} else if (background != null) {
super.setBackground(background);
updateBackgroundDrawable(background);
}
}
@ -562,14 +561,15 @@ public class ReactViewGroup extends ViewGroup implements
if (mReactBackgroundDrawable == null) {
mReactBackgroundDrawable = new ReactViewBackgroundDrawable();
Drawable backgroundDrawable = getBackground();
super.setBackground(null); // required so that drawable callback is cleared before we add the
updateBackgroundDrawable(
null); // required so that drawable callback is cleared before we add the
// drawable back as a part of LayerDrawable
if (backgroundDrawable == null) {
super.setBackground(mReactBackgroundDrawable);
updateBackgroundDrawable(mReactBackgroundDrawable);
} else {
LayerDrawable layerDrawable =
new LayerDrawable(new Drawable[] {mReactBackgroundDrawable, backgroundDrawable});
super.setBackground(layerDrawable);
updateBackgroundDrawable(layerDrawable);
}
}
return mReactBackgroundDrawable;
@ -584,4 +584,19 @@ public class ReactViewGroup extends ViewGroup implements
mHitSlopRect = rect;
}
/**
* 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);
}
}
}