Permalink
Browse files

Fix setBackground for Android sdk version 15 and below

Reviewed By: achen1

Differential Revision: D5854430

fbshipit-source-id: 1276f3d7e94b757f9a9dd412a2ef8b72e8427ffb
  • Loading branch information...
mdvacca authored and facebook-github-bot committed Sep 19, 2017
1 parent 87a1dc4 commit 5180995666051cc047b5828b2b6d45802b1c7b8b
@@ -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 = [
],
)
@@ -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) {

This comment has been minimized.

Show comment
Hide comment
@hey99xx

hey99xx Nov 20, 2017

Isn't React Native only for API 16 (Jelly bean) and above? In that case when would this condition ever be not true.

@hey99xx

hey99xx Nov 20, 2017

Isn't React Native only for API 16 (Jelly bean) and above? In that case when would this condition ever be not true.

This comment has been minimized.

Show comment
Hide comment
@caschomburg123

caschomburg123 Nov 21, 2017

@caschomburg123

caschomburg123 via email Nov 21, 2017

This comment has been minimized.

Show comment
Hide comment
@caschomburg123

caschomburg123 Nov 21, 2017

@caschomburg123

caschomburg123 via email Nov 21, 2017

view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
}
}
@@ -16,7 +16,6 @@
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.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,14 +48,15 @@
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);
}
public ReactHorizontalScrollView(Context context, @Nullable FpsListener fpsListener) {
super(context);
mReactBackgroundManager = new ReactViewBackgroundManager(this);
mFpsListener = fpsListener;
}
@@ -325,47 +325,27 @@ private void smoothScrollToPage(int velocity) {
@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);
}
}
@@ -14,7 +14,6 @@
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.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 @@
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 ReactScrollView(ReactContext context) {
public ReactScrollView(ReactContext context, @Nullable FpsListener fpsListener) {
super(context);
mFpsListener = fpsListener;
mReactBackgroundManager = new ReactViewBackgroundManager(this);
if (!sTriedToGetScrollerField) {
sTriedToGetScrollerField = true;
@@ -394,48 +394,29 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
}
}
@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);
}
}
@@ -9,23 +9,19 @@
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;
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 @@
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 void onFinishTemporaryDetach() {
}
}
@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 void updateView() {
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);
}
}
Oops, something went wrong.

0 comments on commit 5180995

Please sign in to comment.