From b1aa39a460da25fbf29a80f727007a7e1f267440 Mon Sep 17 00:00:00 2001 From: Pasquale Anatriello Date: Fri, 8 Mar 2019 11:16:55 -0800 Subject: [PATCH] Remove Reference API Summary: Reference has been deprecated for a while. This diff completely removes it in favor of using ComparableDrawable everywhere. Reviewed By: astreet Differential Revision: D14241428 fbshipit-source-id: a986f34fda3a05c85d3d57b1e806c5e48c3f485e --- .../litho/annotations/Comparable.java | 4 +- .../java/com/facebook/litho/CommonProps.java | 5 +- .../com/facebook/litho/CommonPropsHolder.java | 8 +- .../java/com/facebook/litho/Component.java | 42 +----- .../com/facebook/litho/ComponentLayout.java | 4 +- .../com/facebook/litho/ComponentUtils.java | 6 +- .../com/facebook/litho/DebugLayoutNode.java | 4 +- .../com/facebook/litho/DrawableComponent.java | 21 ++- .../java/com/facebook/litho/InternalNode.java | 32 ++-- .../java/com/facebook/litho/LayoutState.java | 40 +++-- .../java/com/facebook/litho/MountState.java | 13 +- .../com/facebook/litho/NoOpInternalNode.java | 3 +- .../litho/SparseCommonPropsHolder.java | 9 +- .../java/com/facebook/litho/ViewNodeInfo.java | 10 +- .../litho/reference/DrawableReference.java | 71 --------- .../reference/DrawableReferenceLifecycle.java | 52 ------- .../reference/DrawableResourcesCache.java | 139 ------------------ .../facebook/litho/reference/Reference.java | 87 ----------- .../litho/reference/ReferenceLifecycle.java | 55 ------- .../src/test/java/com/facebook/litho/BUCK | 1 - .../com/facebook/litho/CommonPropsTest.java | 13 +- .../facebook/litho/ComponentUtilsTest.java | 42 ------ .../litho/LayoutStateCreateTreeTest.java | 7 +- .../facebook/litho/ResolveAttributeTest.java | 5 +- .../facebook/litho/SparseCommonPropsTest.java | 7 +- .../reference/DrawableReferenceTest.java | 47 ------ .../reference/DrawableResourcesCacheTest.java | 84 ----------- .../litho/reference/ReferenceTest.java | 80 ---------- .../specmodels/model/PropValidationTest.java | 3 +- .../generator/BuilderGenerator.java | 6 - .../generator/ComponentBodyGenerator.java | 9 +- .../generator/testing/MatcherGenerator.java | 4 - .../litho/specmodels/model/ClassNames.java | 7 +- .../specmodels/model/PropValidation.java | 8 +- .../subcomponents/InspectableComponent.java | 4 +- .../facebook/litho/widget/EditTextSpec.java | 7 +- 36 files changed, 98 insertions(+), 841 deletions(-) delete mode 100644 litho-core/src/main/java/com/facebook/litho/reference/DrawableReference.java delete mode 100644 litho-core/src/main/java/com/facebook/litho/reference/DrawableReferenceLifecycle.java delete mode 100644 litho-core/src/main/java/com/facebook/litho/reference/DrawableResourcesCache.java delete mode 100644 litho-core/src/main/java/com/facebook/litho/reference/Reference.java delete mode 100644 litho-core/src/main/java/com/facebook/litho/reference/ReferenceLifecycle.java delete mode 100644 litho-it/src/test/java/com/facebook/litho/reference/DrawableReferenceTest.java delete mode 100644 litho-it/src/test/java/com/facebook/litho/reference/DrawableResourcesCacheTest.java delete mode 100644 litho-it/src/test/java/com/facebook/litho/reference/ReferenceTest.java diff --git a/litho-annotations/src/main/java/com/facebook/litho/annotations/Comparable.java b/litho-annotations/src/main/java/com/facebook/litho/annotations/Comparable.java index d4ad2027c3e..a397ec99448 100644 --- a/litho-annotations/src/main/java/com/facebook/litho/annotations/Comparable.java +++ b/litho-annotations/src/main/java/com/facebook/litho/annotations/Comparable.java @@ -31,7 +31,7 @@ int DOUBLE = 1; int ARRAY = 2; int PRIMITIVE = 3; - int REFERENCE = 4; + int COMPARABLE_DRAWABLE = 4; int COLLECTION_COMPLEVEL_0 = 5; int COLLECTION_COMPLEVEL_1 = 6; int COLLECTION_COMPLEVEL_2 = 7; @@ -50,7 +50,7 @@ DOUBLE, ARRAY, PRIMITIVE, - REFERENCE, + COMPARABLE_DRAWABLE, COLLECTION_COMPLEVEL_0, COLLECTION_COMPLEVEL_1, COLLECTION_COMPLEVEL_2, diff --git a/litho-core/src/main/java/com/facebook/litho/CommonProps.java b/litho-core/src/main/java/com/facebook/litho/CommonProps.java index 0486913be84..6885075fc7c 100644 --- a/litho-core/src/main/java/com/facebook/litho/CommonProps.java +++ b/litho-core/src/main/java/com/facebook/litho/CommonProps.java @@ -26,7 +26,6 @@ import androidx.annotation.StyleRes; import com.facebook.infer.annotation.ThreadConfined; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaDirection; import com.facebook.yoga.YogaEdge; @@ -66,7 +65,7 @@ public interface CommonProps extends CommonPropsCopyable { void heightPx(@Px int height); - void background(@Nullable Reference background); + void background(@Nullable ComparableDrawable background); void testKey(String testKey); @@ -139,7 +138,7 @@ public interface CommonProps extends CommonPropsCopyable { void clickHandler(EventHandler clickHandler); @Nullable - Reference getBackground(); + ComparableDrawable getBackground(); void longClickHandler(EventHandler longClickHandler); diff --git a/litho-core/src/main/java/com/facebook/litho/CommonPropsHolder.java b/litho-core/src/main/java/com/facebook/litho/CommonPropsHolder.java index fca4a082ce7..fda1d95b364 100644 --- a/litho-core/src/main/java/com/facebook/litho/CommonPropsHolder.java +++ b/litho-core/src/main/java/com/facebook/litho/CommonPropsHolder.java @@ -17,7 +17,6 @@ package com.facebook.litho; import android.animation.StateListAnimator; -import android.graphics.drawable.Drawable; import android.util.SparseArray; import android.view.ViewOutlineProvider; import androidx.annotation.AttrRes; @@ -28,7 +27,6 @@ import com.facebook.infer.annotation.ThreadConfined; import com.facebook.litho.config.ComponentsConfiguration; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaConstants; import com.facebook.yoga.YogaDirection; @@ -49,7 +47,7 @@ class CommonPropsHolder implements CommonProps { @Nullable private OtherProps mOtherProps; @Nullable private NodeInfo mNodeInfo; @Nullable private LayoutProps mLayoutProps; - @Nullable private Reference mBackground; + @Nullable private ComparableDrawable mBackground; @Nullable private String mTestKey; private boolean mWrapInView; @AttrRes private int mDefStyleAttr; @@ -98,7 +96,7 @@ public void heightPx(@Px int height) { } @Override - public void background(@Nullable Reference background) { + public void background(@Nullable ComparableDrawable background) { mPrivateFlags |= PFLAG_BACKGROUND_IS_SET; mBackground = background; } @@ -286,7 +284,7 @@ public void clickHandler(EventHandler clickHandler) { @Override @Nullable - public Reference getBackground() { + public ComparableDrawable getBackground() { return mBackground; } diff --git a/litho-core/src/main/java/com/facebook/litho/Component.java b/litho-core/src/main/java/com/facebook/litho/Component.java index 8c834eb62df..70636966ee6 100644 --- a/litho-core/src/main/java/com/facebook/litho/Component.java +++ b/litho-core/src/main/java/com/facebook/litho/Component.java @@ -45,8 +45,6 @@ import com.facebook.litho.drawable.ComparableDrawable; import com.facebook.litho.drawable.ComparableResDrawable; import com.facebook.litho.drawable.DefaultComparableDrawable; -import com.facebook.litho.reference.DrawableReference; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaDirection; import com.facebook.yoga.YogaEdge; @@ -1326,27 +1324,12 @@ public T touchExpansionDip( * @see ComparableDrawable */ @Deprecated - public T background(@Nullable Reference background) { - /** - * We want to test the effect of not using References so if we come into this method with an - * actual reference (that is not a DrawableReference) we turn it into a DrawableReference to - * force it to behave as a proper Drawable. This is actually going to be slightly worse for - * memory than just having a reference to the Drawable directly. - */ - if (ComponentsConfiguration.dontUseReferences - && background != null - && !(background instanceof DrawableReference)) { - final Drawable backgroundDrawable = - Reference.acquire(getContext().getAndroidContext(), background); - - if (backgroundDrawable != null) { - background = - DrawableReference.create(DefaultComparableDrawable.create(backgroundDrawable)); - } + public T background(@Nullable Drawable background) { + if (background instanceof ComparableDrawable || background == null) { + return background((ComparableDrawable) background); } - mComponent.getOrCreateCommonProps().background(background); - return getThis(); + return background(DefaultComparableDrawable.create(background)); } /** @@ -1359,19 +1342,8 @@ public T background(@Nullable Reference background) { * @see ComparableDrawable */ public T background(@Nullable ComparableDrawable background) { - return background(background != null ? DrawableReference.create(background) : null); - } - - /** - * @deprecated use {@link #background(ComparableDrawable)} more efficient diffing of drawables. - * @see ComparableDrawable - */ - @Deprecated - public T background(@Nullable Drawable background) { - if (background instanceof ComparableDrawable) { - return background((ComparableDrawable) background); - } - return background(background != null ? DefaultComparableDrawable.create(background) : null); + mComponent.getOrCreateCommonProps().background(background); + return getThis(); } public T backgroundAttr(@AttrRes int resId, @DrawableRes int defaultResId) { @@ -1413,7 +1385,7 @@ public T foreground(@Nullable ComparableDrawable foreground) { */ @Deprecated public T foreground(@Nullable Drawable foreground) { - if (foreground instanceof ComparableDrawable) { + if (foreground instanceof ComparableDrawable || foreground == null) { return foreground((ComparableDrawable) foreground); } return foreground(foreground != null ? DefaultComparableDrawable.create(foreground) : null); diff --git a/litho-core/src/main/java/com/facebook/litho/ComponentLayout.java b/litho-core/src/main/java/com/facebook/litho/ComponentLayout.java index 01db6bbb8fe..442d89b230c 100644 --- a/litho-core/src/main/java/com/facebook/litho/ComponentLayout.java +++ b/litho-core/src/main/java/com/facebook/litho/ComponentLayout.java @@ -19,7 +19,7 @@ import android.graphics.drawable.Drawable; import androidx.annotation.Px; import com.facebook.infer.annotation.ThreadConfined; -import com.facebook.litho.reference.Reference; +import com.facebook.litho.drawable.ComparableDrawable; import com.facebook.yoga.YogaDirection; /** @@ -57,7 +57,7 @@ public interface ComponentLayout { boolean isPaddingSet(); - Reference getBackground(); + ComparableDrawable getBackground(); YogaDirection getResolvedLayoutDirection(); } diff --git a/litho-core/src/main/java/com/facebook/litho/ComponentUtils.java b/litho-core/src/main/java/com/facebook/litho/ComponentUtils.java index 9183ac49303..fd2a5225456 100644 --- a/litho-core/src/main/java/com/facebook/litho/ComponentUtils.java +++ b/litho-core/src/main/java/com/facebook/litho/ComponentUtils.java @@ -17,7 +17,7 @@ package com.facebook.litho; import com.facebook.litho.annotations.Comparable; -import com.facebook.litho.reference.Reference; +import com.facebook.litho.drawable.ComparableDrawable; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collection; @@ -86,8 +86,8 @@ public static boolean hasEquivalentFields(Object obj1, Object obj2) { } break; - case Comparable.REFERENCE: - if (Reference.shouldUpdate((Reference) val1, (Reference) val2)) { + case Comparable.COMPARABLE_DRAWABLE: + if (!((ComparableDrawable) val1).isEquivalentTo((ComparableDrawable) val2)) { return false; } break; diff --git a/litho-core/src/main/java/com/facebook/litho/DebugLayoutNode.java b/litho-core/src/main/java/com/facebook/litho/DebugLayoutNode.java index 7b5edc01f2e..bb0c324d95f 100644 --- a/litho-core/src/main/java/com/facebook/litho/DebugLayoutNode.java +++ b/litho-core/src/main/java/com/facebook/litho/DebugLayoutNode.java @@ -17,7 +17,7 @@ package com.facebook.litho; import android.graphics.drawable.Drawable; -import com.facebook.litho.reference.Reference; +import com.facebook.litho.drawable.ComparableDrawable; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaConstants; import com.facebook.yoga.YogaDirection; @@ -50,7 +50,7 @@ public void setForegroundColor(int color) { } @Nullable - public Reference getBackground() { + public ComparableDrawable getBackground() { return mNode.getBackground(); } diff --git a/litho-core/src/main/java/com/facebook/litho/DrawableComponent.java b/litho-core/src/main/java/com/facebook/litho/DrawableComponent.java index 6cacc6824f8..b9a42f4e0b7 100644 --- a/litho-core/src/main/java/com/facebook/litho/DrawableComponent.java +++ b/litho-core/src/main/java/com/facebook/litho/DrawableComponent.java @@ -18,15 +18,15 @@ import android.content.Context; import android.graphics.drawable.Drawable; -import com.facebook.litho.reference.Reference; +import com.facebook.litho.drawable.ComparableDrawable; class DrawableComponent extends Component { - Reference mDrawable; + ComparableDrawable mDrawable; int mDrawableWidth; int mDrawableHeight; - private DrawableComponent(Reference drawable) { + private DrawableComponent(ComparableDrawable drawable) { super("DrawableComponent"); mDrawable = drawable; } @@ -48,7 +48,7 @@ protected void onMount( Object content) { MatrixDrawable drawable = (MatrixDrawable) content; - drawable.mount(Reference.acquire(context.getAndroidContext(), getDrawable())); + drawable.mount(getDrawable()); } @Override @@ -65,10 +65,7 @@ protected void onUnmount( ComponentContext context, Object mountedContent) { final MatrixDrawable matrixDrawable = (MatrixDrawable) mountedContent; - final T innerMountedDrawable = matrixDrawable.getMountedDrawable(); - matrixDrawable.unmount(); - Reference.release(context.getAndroidContext(), innerMountedDrawable, getDrawable()); } @Override @@ -81,19 +78,19 @@ public MountType getMountType() { return MountType.DRAWABLE; } - public static DrawableComponent create(Reference drawable) { + public static DrawableComponent create(ComparableDrawable drawable) { return new DrawableComponent<>(drawable); } @Override protected boolean shouldUpdate(Component previous, Component next) { - final Reference previousReference = ((DrawableComponent) previous).getDrawable(); - final Reference nextReference = ((DrawableComponent) next).getDrawable(); + final ComparableDrawable previousDrawable = ((DrawableComponent) previous).getDrawable(); + final ComparableDrawable nextDrawable = ((DrawableComponent) next).getDrawable(); - return Reference.shouldUpdate(previousReference, nextReference); + return !previousDrawable.isEquivalentTo(nextDrawable); } - private Reference getDrawable() { + private ComparableDrawable getDrawable() { return mDrawable; } diff --git a/litho-core/src/main/java/com/facebook/litho/InternalNode.java b/litho-core/src/main/java/com/facebook/litho/InternalNode.java index b9513b53417..437fc5684ca 100644 --- a/litho-core/src/main/java/com/facebook/litho/InternalNode.java +++ b/litho-core/src/main/java/com/facebook/litho/InternalNode.java @@ -50,8 +50,6 @@ import com.facebook.litho.drawable.ComparableDrawable; import com.facebook.litho.drawable.ComparableResDrawable; import com.facebook.litho.drawable.DefaultComparableDrawable; -import com.facebook.litho.reference.DrawableReference; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaBaselineFunction; import com.facebook.yoga.YogaConstants; @@ -123,7 +121,7 @@ class InternalNode implements ComponentLayout { private boolean mDuplicateParentState; private long mPrivateFlags; - private @Nullable Reference mBackground; + private @Nullable ComparableDrawable mBackground; private @Nullable ComparableDrawable mForeground; private final int[] mBorderColors = new int[Border.EDGE_COUNT]; private final float[] mBorderRadius = new float[Border.RADIUS_COUNT]; @@ -290,7 +288,7 @@ public boolean isPaddingSet() { } @Override - public @Nullable Reference getBackground() { + public @Nullable ComparableDrawable getBackground() { return mBackground; } @@ -796,21 +794,13 @@ InternalNode child(InternalNode child) { return this; } - /** - * @deprecated use {@link #background(ComparableDrawable)} more efficient diffing of drawables. - */ - @Deprecated - InternalNode background(@Nullable Reference background) { + InternalNode background(@Nullable ComparableDrawable background) { mPrivateFlags |= PFLAG_BACKGROUND_IS_SET; mBackground = background; - setPaddingFromDrawableReference(background); + setPaddingFromBackground(background); return this; } - InternalNode background(@Nullable ComparableDrawable background) { - return background(background != null ? DrawableReference.create(background) : null); - } - /** * @deprecated use {@link #background(ComparableDrawable)} more efficient diffing of drawables. */ @@ -819,6 +809,7 @@ InternalNode background(@Nullable Drawable background) { if (background instanceof ComparableDrawable) { return background((ComparableDrawable) background); } + return background(background != null ? DefaultComparableDrawable.create(background) : null); } @@ -839,6 +830,10 @@ InternalNode backgroundColor(@ColorInt int backgroundColor) { */ @Deprecated InternalNode foreground(@Nullable Drawable foreground) { + if (foreground instanceof ComparableDrawable) { + return foreground((ComparableDrawable) foreground); + } + return foreground(foreground != null ? DefaultComparableDrawable.create(foreground) : null); } @@ -1616,11 +1611,8 @@ static void assertContextSpecificStyleNotSet(InternalNode node) { return mNestedTreeProps != null ? mNestedTreeProps.mPendingTreeProps : null; } - private void setPaddingFromDrawableReference(@Nullable Reference ref) { - if (ref == null) { - return; - } - final T drawable = Reference.acquire(mComponentContext.getAndroidContext(), ref); + private void setPaddingFromBackground(Drawable drawable) { + if (drawable != null) { final Rect backgroundPadding = new Rect(); if (getDrawablePadding(drawable, backgroundPadding)) { @@ -1629,8 +1621,6 @@ private void setPaddingFromDrawableReference(@Nullable Refe paddingPx(RIGHT, backgroundPadding.right); paddingPx(BOTTOM, backgroundPadding.bottom); } - - Reference.release(mComponentContext.getAndroidContext(), drawable, ref); } } diff --git a/litho-core/src/main/java/com/facebook/litho/LayoutState.java b/litho-core/src/main/java/com/facebook/litho/LayoutState.java index cfdebd42a6c..7051588a266 100644 --- a/litho-core/src/main/java/com/facebook/litho/LayoutState.java +++ b/litho-core/src/main/java/com/facebook/litho/LayoutState.java @@ -50,7 +50,6 @@ import android.content.Context; import android.content.pm.ApplicationInfo; import android.graphics.Rect; -import android.graphics.drawable.Drawable; import android.os.Build; import android.text.TextUtils; import android.view.View; @@ -64,8 +63,6 @@ import com.facebook.litho.config.ComponentsConfiguration; import com.facebook.litho.drawable.BorderColorDrawable; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.DrawableReference; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaConstants; import com.facebook.yoga.YogaDirection; import com.facebook.yoga.YogaEdge; @@ -720,7 +717,7 @@ private static void collectResults( } // 2. Add background if defined. - final Reference background = node.getBackground(); + final ComparableDrawable background = node.getBackground(); if (background != null) { if (layoutOutput != null && layoutOutput.hasViewNodeInfo()) { layoutOutput.getViewNodeInfo().setBackground(background); @@ -867,7 +864,7 @@ private static void collectResults( node, layoutState, convertForeground, - DrawableReference.create(foreground), + foreground, OutputUnitType.FOREGROUND, shouldAddHostLayoutOutput); @@ -990,7 +987,7 @@ private static void collectResults( node, layoutState, null, - DrawableReference.create(new DebugOverlayDrawable(mainThreadCalculations)), + new DebugOverlayDrawable(mainThreadCalculations), OutputUnitType.FOREGROUND, needsHostView); } @@ -1053,10 +1050,10 @@ private static LayoutOutput addDrawableComponent( InternalNode node, LayoutState layoutState, @Nullable LayoutOutput recycle, - Reference reference, + ComparableDrawable drawable, @OutputUnitType int type, boolean matchHostBoundsTransitions) { - final Component drawableComponent = DrawableComponent.create(reference); + final Component drawableComponent = DrawableComponent.create(drawable); drawableComponent.setScopedContext( ComponentContext.withComponentScope(node.getContext(), drawableComponent)); final boolean isOutputUpdated; @@ -1084,7 +1081,7 @@ private static LayoutOutput addDrawableComponent( return output; } - private static Reference getBorderColorDrawable(InternalNode node) { + private static ComparableDrawable getBorderColorDrawable(InternalNode node) { if (!node.shouldDrawBorders()) { throw new RuntimeException("This node does not support drawing border color"); } @@ -1095,19 +1092,18 @@ private static Reference getBorderColorDrawable(InternalNode final YogaEdge leftEdge = isRtl ? YogaEdge.RIGHT : YogaEdge.LEFT; final YogaEdge rightEdge = isRtl ? YogaEdge.LEFT : YogaEdge.RIGHT; - return DrawableReference.create( - new BorderColorDrawable.Builder() - .pathEffect(node.getBorderPathEffect()) - .borderLeftColor(Border.getEdgeColor(borderColors, leftEdge)) - .borderTopColor(Border.getEdgeColor(borderColors, YogaEdge.TOP)) - .borderRightColor(Border.getEdgeColor(borderColors, rightEdge)) - .borderBottomColor(Border.getEdgeColor(borderColors, YogaEdge.BOTTOM)) - .borderLeftWidth(node.getLayoutBorder(leftEdge)) - .borderTopWidth(node.getLayoutBorder(YogaEdge.TOP)) - .borderRightWidth(node.getLayoutBorder(rightEdge)) - .borderBottomWidth(node.getLayoutBorder(YogaEdge.BOTTOM)) - .borderRadius(borderRadius) - .build()); + return new BorderColorDrawable.Builder() + .pathEffect(node.getBorderPathEffect()) + .borderLeftColor(Border.getEdgeColor(borderColors, leftEdge)) + .borderTopColor(Border.getEdgeColor(borderColors, YogaEdge.TOP)) + .borderRightColor(Border.getEdgeColor(borderColors, rightEdge)) + .borderBottomColor(Border.getEdgeColor(borderColors, YogaEdge.BOTTOM)) + .borderLeftWidth(node.getLayoutBorder(leftEdge)) + .borderTopWidth(node.getLayoutBorder(YogaEdge.TOP)) + .borderRightWidth(node.getLayoutBorder(rightEdge)) + .borderBottomWidth(node.getLayoutBorder(YogaEdge.BOTTOM)) + .borderRadius(borderRadius) + .build(); } private static void addLayoutOutputIdToPositionsMap( diff --git a/litho-core/src/main/java/com/facebook/litho/MountState.java b/litho-core/src/main/java/com/facebook/litho/MountState.java index 7fb81d62cc3..96873cc7807 100644 --- a/litho-core/src/main/java/com/facebook/litho/MountState.java +++ b/litho-core/src/main/java/com/facebook/litho/MountState.java @@ -59,7 +59,7 @@ import com.facebook.infer.annotation.ThreadConfined; import com.facebook.litho.animation.AnimatedProperties; import com.facebook.litho.config.ComponentsConfiguration; -import com.facebook.litho.reference.Reference; +import com.facebook.litho.drawable.ComparableDrawable; import java.util.ArrayList; import java.util.BitSet; import java.util.Collections; @@ -2126,18 +2126,17 @@ private static void unsetViewPadding(View view, ViewNodeInfo viewNodeInfo) { } private static void setViewBackground(View view, ViewNodeInfo viewNodeInfo) { - final Reference backgroundReference = viewNodeInfo.getBackground(); - if (backgroundReference != null) { - setBackgroundCompat(view, Reference.acquire(view.getContext(), backgroundReference)); + final ComparableDrawable background = viewNodeInfo.getBackground(); + if (background != null) { + setBackgroundCompat(view, background); } } private static void unsetViewBackground(View view, ViewNodeInfo viewNodeInfo) { - final Reference backgroundReference = viewNodeInfo.getBackground(); + final ComparableDrawable background = viewNodeInfo.getBackground(); final Drawable drawable = view.getBackground(); - if (backgroundReference != null) { + if (background != null) { setBackgroundCompat(view, null); - Reference.release(view.getContext(), drawable, (Reference) backgroundReference); } } diff --git a/litho-core/src/main/java/com/facebook/litho/NoOpInternalNode.java b/litho-core/src/main/java/com/facebook/litho/NoOpInternalNode.java index c636d1f48ed..7e54dc0ed02 100644 --- a/litho-core/src/main/java/com/facebook/litho/NoOpInternalNode.java +++ b/litho-core/src/main/java/com/facebook/litho/NoOpInternalNode.java @@ -21,7 +21,6 @@ import androidx.annotation.Nullable; import androidx.annotation.Px; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaDirection; import com.facebook.yoga.YogaEdge; @@ -340,7 +339,7 @@ public InternalNode child(Component.Builder child) { } @Override - public InternalNode background(Reference builder) { + public InternalNode background(Drawable builder) { return this; } diff --git a/litho-core/src/main/java/com/facebook/litho/SparseCommonPropsHolder.java b/litho-core/src/main/java/com/facebook/litho/SparseCommonPropsHolder.java index 0ade10a875f..5c3da01c1f3 100644 --- a/litho-core/src/main/java/com/facebook/litho/SparseCommonPropsHolder.java +++ b/litho-core/src/main/java/com/facebook/litho/SparseCommonPropsHolder.java @@ -30,7 +30,6 @@ import com.facebook.litho.config.ComponentsConfiguration; import com.facebook.litho.drawable.ComparableDrawable; import com.facebook.litho.internal.SparseFloatArray; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaConstants; import com.facebook.yoga.YogaDirection; @@ -503,7 +502,7 @@ public void heightPx(@Px int height) { } @Override - public void background(@Nullable Reference background) { + public void background(ComparableDrawable background) { mPrivateFlags |= PFLAG_BACKGROUND_IS_SET; getOrCreateObjectProps().append(INDEX_Background, background); } @@ -521,8 +520,8 @@ public void wrapInView() { @Nullable @Override - public Reference getBackground() { - return (Reference) getOrCreateObjectProps().get(INDEX_Background); + public ComparableDrawable getBackground() { + return (ComparableDrawable) getOrCreateObjectProps().get(INDEX_Background); } @Override @@ -763,7 +762,7 @@ public void copyInto(ComponentContext c, InternalNode node) { } if ((mPrivateFlags & PFLAG_BACKGROUND_IS_SET) != 0L) { - node.background((Reference) mObjectProps.get(INDEX_Background)); + node.background((ComparableDrawable) mObjectProps.get(INDEX_Background)); } if ((mPrivateFlags & PFLAG_TEST_KEY_IS_SET) != 0L) { node.testKey((String) mObjectProps.get(INDEX_TestKey)); diff --git a/litho-core/src/main/java/com/facebook/litho/ViewNodeInfo.java b/litho-core/src/main/java/com/facebook/litho/ViewNodeInfo.java index b37c4e4428c..9454624e950 100644 --- a/litho-core/src/main/java/com/facebook/litho/ViewNodeInfo.java +++ b/litho-core/src/main/java/com/facebook/litho/ViewNodeInfo.java @@ -18,11 +18,9 @@ import android.animation.StateListAnimator; import android.graphics.Rect; -import android.graphics.drawable.Drawable; import android.view.View; import androidx.annotation.DrawableRes; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.Reference; import com.facebook.yoga.YogaDirection; import javax.annotation.Nullable; @@ -32,7 +30,7 @@ */ class ViewNodeInfo { - private Reference mBackground; + private ComparableDrawable mBackground; private ComparableDrawable mForeground; private Rect mPadding; private Rect mExpandedTouchBounds; @@ -40,11 +38,11 @@ class ViewNodeInfo { private @Nullable StateListAnimator mStateListAnimator; private @DrawableRes int mStateListAnimatorRes; - void setBackground(Reference background) { + void setBackground(ComparableDrawable background) { mBackground = background; } - Reference getBackground() { + ComparableDrawable getBackground() { return mBackground; } @@ -165,7 +163,7 @@ public boolean isEquivalentTo(ViewNodeInfo other) { return false; } - if (Reference.shouldUpdate(mBackground, other.mBackground)) { + if (!ComparableDrawable.isEquivalentTo(mBackground, other.mBackground)) { return false; } diff --git a/litho-core/src/main/java/com/facebook/litho/reference/DrawableReference.java b/litho-core/src/main/java/com/facebook/litho/reference/DrawableReference.java deleted file mode 100644 index 2e7a9aad33d..00000000000 --- a/litho-core/src/main/java/com/facebook/litho/reference/DrawableReference.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import android.graphics.drawable.Drawable; -import com.facebook.litho.drawable.ComparableDrawable; - -/** - * A very simple Reference for {@link Drawable} used in all the cases where it's not - * possible/desirable to use a real Reference. This will simply keep a reference to the Drawable in - * the Props and return it. Please take care when using this. It keeps the drawable in memory all - * the time and should only be used when the other built in specs are not applicable and it's not - * possible to write a custom ReferenceSpec - */ -public final class DrawableReference extends Reference { - - final ComparableDrawable mDrawable; - - private DrawableReference(ComparableDrawable drawable) { - super(DrawableReferenceLifecycle.get()); - mDrawable = drawable; - } - - @Override - public int hashCode() { - return mDrawable != null ? mDrawable.hashCode() : 0; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (!(o instanceof DrawableReference)) { - return false; - } - - DrawableReference state = (DrawableReference) o; - return mDrawable == state.mDrawable; - } - - @Override - public String getSimpleName() { - return "DrawableReference"; - } - - /** - * Utility method to create Comparable Drawable Reference - * - * @param drawable the drawable to wrap - * @return the no-op drawable reference - */ - public static Reference create(ComparableDrawable drawable) { - return new DrawableReference(drawable); - } -} diff --git a/litho-core/src/main/java/com/facebook/litho/reference/DrawableReferenceLifecycle.java b/litho-core/src/main/java/com/facebook/litho/reference/DrawableReferenceLifecycle.java deleted file mode 100644 index 245da426d81..00000000000 --- a/litho-core/src/main/java/com/facebook/litho/reference/DrawableReferenceLifecycle.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import com.facebook.litho.drawable.ComparableDrawable; - -/** - * A very simple Reference for {@link Drawable} used in all the cases where it's not - * possible/desirable to use a real Reference. This will simply keep a reference to the Drawable in - * the Props and return it. Please take care when using this. It keeps the drawable in memory all - * the time and should only be used when the other built in specs are not applicable and it's not - * possible to write a custom ReferenceSpec - */ -final class DrawableReferenceLifecycle extends ReferenceLifecycle { - - private static final DrawableReferenceLifecycle sInstance = new DrawableReferenceLifecycle(); - - private DrawableReferenceLifecycle() {} - - public static DrawableReferenceLifecycle get() { - return sInstance; - } - - @Override - protected ComparableDrawable onAcquire(Context context, Reference reference) { - return ((DrawableReference) reference).mDrawable; - } - - @Override - protected boolean shouldUpdate( - Reference previous, Reference next) { - ComparableDrawable previousDrawable = ((DrawableReference) previous).mDrawable; - ComparableDrawable nextDrawable = ((DrawableReference) next).mDrawable; - return !previousDrawable.isEquivalentTo(nextDrawable); - } -} diff --git a/litho-core/src/main/java/com/facebook/litho/reference/DrawableResourcesCache.java b/litho-core/src/main/java/com/facebook/litho/reference/DrawableResourcesCache.java deleted file mode 100644 index 4f3780ce177..00000000000 --- a/litho-core/src/main/java/com/facebook/litho/reference/DrawableResourcesCache.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import static android.os.Build.VERSION.SDK_INT; -import static android.os.Build.VERSION_CODES.HONEYCOMB; - -import android.content.res.Resources; -import android.graphics.drawable.Drawable; -import android.util.StateSet; -import androidx.annotation.Nullable; -import androidx.collection.LruCache; -import androidx.core.content.res.ResourcesCompat; -import androidx.core.util.Pools; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * A cache that holds Drawables retreived from Android {@link android.content.res.Resources} for - * each resId this class keeps a {@link androidx.core.util.Pools.SynchronizedPool} of - * DRAWABLES_POOL_MAX_ITEMS. The cache has a maximum capacity of DRAWABLES_MAX_ENTRIES. When the - * cache is full it starts clearing memory deleting the less recently used pool of resources - */ -class DrawableResourcesCache { - - private static final int DRAWABLES_MAX_ENTRIES = 200; - private static final int DRAWABLES_POOL_MAX_ITEMS = 10; - - private final LruCache> mDrawableCache; - - DrawableResourcesCache() { - mDrawableCache = new LruCache>(DRAWABLES_MAX_ENTRIES) { - @Override - protected int sizeOf(Integer key, SimplePoolWithCount value) { - return value.getPoolSize(); - } - }; - } - - /** - * @deprecated use {@link #get(int, Resources, Resources.Theme)} - */ - @Nullable - @Deprecated - public Drawable get(int resId, Resources resources) { - return get(resId, resources, null); - } - - @Nullable - public Drawable get(int resId, Resources resources, @Nullable Resources.Theme theme) { - SimplePoolWithCount drawablesPool = mDrawableCache.get(resId); - - if (drawablesPool == null) { - drawablesPool = new SimplePoolWithCount<>(DRAWABLES_POOL_MAX_ITEMS); - mDrawableCache.put(resId, drawablesPool); - } - - Drawable drawable = drawablesPool.acquire(); - - if (drawable == null) { - drawable = ResourcesCompat.getDrawable(resources, resId, theme); - } - - // We never want this pool to remain empty otherwise we would risk to resolve a new drawable - // when get is called again. So if the pool is about to drain we just put a new Drawable in it - // to keep it warm. - if (drawable != null && drawablesPool.getPoolSize() == 0) { - drawablesPool.release(drawable.getConstantState().newDrawable()); - } - - return drawable; - } - - public void release(Drawable drawable, int resId) { - SimplePoolWithCount drawablesPool = mDrawableCache.get(resId); - if (drawablesPool == null) { - drawablesPool = new SimplePoolWithCount<>(DRAWABLES_POOL_MAX_ITEMS); - mDrawableCache.put(resId, drawablesPool); - } - - // Reset a stateful drawable, and its animations, before being released. - if (drawable.isStateful()) { - drawable.setState(StateSet.WILD_CARD); - - if (SDK_INT >= HONEYCOMB) { - drawable.jumpToCurrentState(); - } - } - - drawablesPool.release(drawable); - } - - private static class SimplePoolWithCount extends Pools.SynchronizedPool { - - private final AtomicInteger mPoolSize; - - public SimplePoolWithCount(int maxPoolSize) { - super(maxPoolSize); - mPoolSize = new AtomicInteger(0); - } - - @Override - public T acquire() { - T item = super.acquire(); - if (item != null) { - mPoolSize.decrementAndGet(); - } - - return item; - } - - @Override - public boolean release(T instance) { - boolean added = super.release(instance); - if (added) { - mPoolSize.incrementAndGet(); - } - - return added; - } - - public int getPoolSize() { - return mPoolSize.get(); - } - } -} diff --git a/litho-core/src/main/java/com/facebook/litho/reference/Reference.java b/litho-core/src/main/java/com/facebook/litho/reference/Reference.java deleted file mode 100644 index 737cd4ae727..00000000000 --- a/litho-core/src/main/java/com/facebook/litho/reference/Reference.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import android.content.Context; -import com.facebook.litho.ComponentContext; -import com.facebook.litho.ResourceResolver; - -/** - * Represents a unique instance of a reference that is driven by its matching {@link - * ReferenceLifecycle} subclass. Use {@link Reference#acquire(Context, Reference)} to acquire the - * underlying resource and {@link Reference#release(Context, Object, Reference)} to release it when - * it's not needed anymore. - * - * @deprecated Just use the object directly instead. - */ -@Deprecated -public abstract class Reference { - - public abstract static class Builder { - protected ResourceResolver mResourceResolver; - - public abstract Reference build(); - - public final void init(ComponentContext c, Reference reference) { - mResourceResolver = new ResourceResolver(c); - } - - protected void release() { - mResourceResolver = null; - } - } - - private final ReferenceLifecycle mLifecycle; - - protected Reference(ReferenceLifecycle lifecycle) { - mLifecycle = lifecycle; - } - - /** - * Acquires a Reference of type T. It is responsibility of the caller to release the acquired - * object by calling {@link Reference#release(Context, Object, Reference)}. Calling acquire twice - * with the same reference does not guarantee that the same instance will be returned twice. - */ - public static T acquire(Context context, Reference reference) { - return reference.mLifecycle.onAcquire(context, reference); - } - - /** - * Releases the object previously acquired by calling {@link Reference#acquire(Context, - * Reference)}. An object that was released calling this function should not be retained or used - * in any way. - */ - public static void release(Context context, T value, Reference reference) { - reference.mLifecycle.onRelease(context, value, reference); - } - - public abstract String getSimpleName(); - - /** - * Checks whether acquiring object from two references will produce the same result. - * This is implemented by default calling {@link Reference#equals(Object)}. When defining a custom - * reference it's possible to provide custom logic for the comparison implementing a method - * annotated with the {@link com.facebook.litho.annotations.ShouldUpdate} annotation. - */ - public static boolean shouldUpdate(Reference previous, Reference next) { - if (previous != null) { - return previous.mLifecycle.shouldReferenceUpdate(previous, next); - } else { - return next != null; - } - } -} diff --git a/litho-core/src/main/java/com/facebook/litho/reference/ReferenceLifecycle.java b/litho-core/src/main/java/com/facebook/litho/reference/ReferenceLifecycle.java deleted file mode 100644 index 763454a67a2..00000000000 --- a/litho-core/src/main/java/com/facebook/litho/reference/ReferenceLifecycle.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import android.content.Context; - -/** - * ReferenceLifecycle objects which are able retreive resources at runtime without needing to keep - * them constantly in memory. References should be used any time it's necessary to include a large - * Object into a {@link com.facebook.litho.Component} in order to limit the amount of - * retained memory in ComponentTree. - * - * ReferenceLifecycle is the base class from which all the Reference types should inherit. - * A ReferenceLifecycle should take care of both acquiring a resource given its {@link Reference} - * and releasing/caching it for future use. - */ -@Deprecated -public abstract class ReferenceLifecycle { - - protected abstract T onAcquire(Context context, Reference reference); - - protected void onRelease(Context context, T value, Reference reference) {} - - protected boolean shouldUpdate(Reference previous, Reference next) { - return !previous.equals(next); - } - - public final boolean shouldReferenceUpdate(Reference previous, Reference next) { - if (previous == null) { - return next != null; - } else if (next == null) { - return true; - } - - if (previous.getClass() != next.getClass()) { - return true; - } - - return shouldUpdate(previous, next); - } -} diff --git a/litho-it/src/test/java/com/facebook/litho/BUCK b/litho-it/src/test/java/com/facebook/litho/BUCK index 5d47139168d..a3b327666c1 100644 --- a/litho-it/src/test/java/com/facebook/litho/BUCK +++ b/litho-it/src/test/java/com/facebook/litho/BUCK @@ -12,7 +12,6 @@ components_robolectric_test( "animation/*.java", "choreographercompat/*.java", "dataflow/*.java", - "reference/*Test.java", "drawable/*.java", ]), contacts = ["oncall+components_for_android@xmail.facebook.com"], diff --git a/litho-it/src/test/java/com/facebook/litho/CommonPropsTest.java b/litho-it/src/test/java/com/facebook/litho/CommonPropsTest.java index 4107f2de6cb..80960ae10c7 100644 --- a/litho-it/src/test/java/com/facebook/litho/CommonPropsTest.java +++ b/litho-it/src/test/java/com/facebook/litho/CommonPropsTest.java @@ -33,8 +33,6 @@ import com.facebook.litho.drawable.ComparableColorDrawable; import com.facebook.litho.drawable.ComparableDrawable; import com.facebook.litho.drawable.ComparableResDrawable; -import com.facebook.litho.reference.DrawableReference; -import com.facebook.litho.reference.Reference; import com.facebook.litho.testing.testrunner.ComponentsTestRunner; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaDirection; @@ -127,8 +125,7 @@ public void testSetPropsAndBuild() { mCommonProps.touchExpansionPx(YogaEdge.LEFT, 23); mCommonProps.touchExpansionPx(YogaEdge.ALL, 21); ComparableDrawable background = ComparableColorDrawable.create(Color.RED); - Reference bgRef = DrawableReference.create(background); - mCommonProps.background(bgRef); + mCommonProps.background(background); ComparableDrawable foreground = ComparableColorDrawable.create(Color.BLACK); mCommonProps.foreground(foreground); @@ -270,7 +267,7 @@ public void testSetPropsAndBuild() { verify(mNode).touchExpansionPx(YogaEdge.LEFT, 23); verify(mNode).touchExpansionPx(YogaEdge.ALL, 21); - verify(mNode).background(bgRef); + verify(mNode).background(background); verify(mNode).foreground(foreground); verify(mNode).wrapInView(); @@ -356,9 +353,8 @@ public void testPaddingFromDrawable() { final InternalNode node = spy(new InternalNode(mComponentContext)); mCommonProps.background( - DrawableReference.create( ComparableResDrawable.create( - mComponentContext.getAndroidContext(), background_with_padding))); + mComponentContext.getAndroidContext(), background_with_padding)); mCommonProps.copyInto(mComponentContext, node); @@ -373,9 +369,8 @@ public void testPaddingFromDrawableIsOverwritten() { final InternalNode node = spy(new InternalNode(mComponentContext)); mCommonProps.background( - DrawableReference.create( ComparableResDrawable.create( - mComponentContext.getAndroidContext(), background_with_padding))); + mComponentContext.getAndroidContext(), background_with_padding)); mCommonProps.paddingPx(YogaEdge.LEFT, 0); mCommonProps.paddingPx(YogaEdge.TOP, 0); mCommonProps.paddingPx(YogaEdge.RIGHT, 0); diff --git a/litho-it/src/test/java/com/facebook/litho/ComponentUtilsTest.java b/litho-it/src/test/java/com/facebook/litho/ComponentUtilsTest.java index 924cd45143b..66f51136930 100644 --- a/litho-it/src/test/java/com/facebook/litho/ComponentUtilsTest.java +++ b/litho-it/src/test/java/com/facebook/litho/ComponentUtilsTest.java @@ -19,10 +19,7 @@ import static com.facebook.litho.ComponentUtils.hasEquivalentFields; import static org.assertj.core.api.Java6Assertions.assertThat; -import android.content.Context; import com.facebook.litho.annotations.Comparable; -import com.facebook.litho.reference.Reference; -import com.facebook.litho.reference.ReferenceLifecycle; import com.facebook.litho.testing.testrunner.ComponentsTestRunner; import java.util.Arrays; import java.util.Collection; @@ -178,16 +175,6 @@ public void hasEquivalentFieldsStringPropTest() { assertThat(hasEquivalentFields(mC1, mC2)).isFalse(); } - @Test - public void hasEquivalentFieldsReferencePropTest() { - mC1.propReference = new TestReference("aa"); - mC2.propReference = new TestReference("aa"); - assertThat(hasEquivalentFields(mC1, mC2)).isTrue(); - - mC2.propReference = new TestReference("ab"); - assertThat(hasEquivalentFields(mC1, mC2)).isFalse(); - } - @Test public void hasEquivalentFieldsCollectionPropTest() { mC1.propCollection = Arrays.asList("1", "2", "3"); @@ -290,9 +277,6 @@ private static class ComponentTest extends Component { @Comparable(type = Comparable.OTHER) String propString; - @Comparable(type = Comparable.REFERENCE) - Reference propReference; - @Comparable(type = Comparable.COLLECTION_COMPLEVEL_0) Collection propCollection; @@ -330,30 +314,4 @@ private static class StateTest implements StateContainer { StateTest() {} } - - private static class TestReference extends Reference { - private String mVal; - - TestReference(String val) { - super( - new ReferenceLifecycle() { - @Override - protected String onAcquire(Context context, Reference reference) { - return ""; - } - }); - mVal = val; - } - - @Override - public boolean equals(Object o) { - TestReference other = (TestReference) o; - return this == o || mVal.equals(other.mVal); - } - - @Override - public String getSimpleName() { - return "TestReference"; - } - } } diff --git a/litho-it/src/test/java/com/facebook/litho/LayoutStateCreateTreeTest.java b/litho-it/src/test/java/com/facebook/litho/LayoutStateCreateTreeTest.java index 61aa57e0fe0..661a2b75438 100644 --- a/litho-it/src/test/java/com/facebook/litho/LayoutStateCreateTreeTest.java +++ b/litho-it/src/test/java/com/facebook/litho/LayoutStateCreateTreeTest.java @@ -33,8 +33,6 @@ import com.facebook.litho.annotations.ImportantForAccessibility; import com.facebook.litho.drawable.ComparableColorDrawable; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.DrawableReference; -import com.facebook.litho.reference.Reference; import com.facebook.litho.testing.TestComponent; import com.facebook.litho.testing.TestDrawableComponent; import com.facebook.litho.testing.TestSizeDependentComponent; @@ -324,7 +322,6 @@ protected Component onCreateLayout(ComponentContext c) { @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void testAddingAllAttributes() { final ComparableDrawable background = ComparableColorDrawable.create(Color.RED); - final Reference bgRef = DrawableReference.create(background); final ComparableDrawable foreground = ComparableColorDrawable.create(Color.BLACK); final EventHandler clickHandler = mock(EventHandler.class); final EventHandler longClickHandler = mock(EventHandler.class); @@ -411,7 +408,7 @@ protected Component onCreateLayout(ComponentContext c) { .touchExpansionPx(YogaEdge.RIGHT, 22) .touchExpansionPx(YogaEdge.LEFT, 23) .touchExpansionPx(YogaEdge.ALL, 21) - .background(bgRef) + .background(background) .foreground(foreground) .wrapInView() .clickHandler(clickHandler) @@ -515,7 +512,7 @@ protected Component onCreateLayout(ComponentContext c) { verify(node).touchExpansionPx(YogaEdge.LEFT, 23); verify(node).touchExpansionPx(YogaEdge.ALL, 21); - verify(node).background(bgRef); + verify(node).background(background); verify(node).foreground(foreground); verify(node).wrapInView(); diff --git a/litho-it/src/test/java/com/facebook/litho/ResolveAttributeTest.java b/litho-it/src/test/java/com/facebook/litho/ResolveAttributeTest.java index 7a8bf63fb94..18ff84cdedb 100644 --- a/litho-it/src/test/java/com/facebook/litho/ResolveAttributeTest.java +++ b/litho-it/src/test/java/com/facebook/litho/ResolveAttributeTest.java @@ -24,7 +24,6 @@ import static com.facebook.litho.it.R.dimen.test_dimen; import static com.facebook.litho.it.R.dimen.test_dimen_float; import static com.facebook.litho.it.R.drawable.test_bg; -import static com.facebook.litho.reference.Reference.acquire; import static com.facebook.yoga.YogaEdge.LEFT; import static org.assertj.core.api.Java6Assertions.assertThat; import static org.robolectric.Shadows.shadowOf; @@ -57,7 +56,7 @@ public void testResolveDrawableAttribute() { Drawable d = mContext.getResources().getDrawable(test_bg); ComparableDrawableWrapper comparable = - (ComparableDrawableWrapper) acquire(mContext.getAndroidContext(), node.getBackground()); + (ComparableDrawableWrapper) node.getBackground(); assertThat(shadowOf(comparable.getWrappedDrawable()).getCreatedFromResId()) .isEqualTo(shadowOf(d).getCreatedFromResId()); } @@ -82,7 +81,7 @@ public void testDefaultDrawableAttribute() { Drawable d = mContext.getResources().getDrawable(test_bg); ComparableDrawableWrapper comparable = - (ComparableDrawableWrapper) acquire(mContext.getAndroidContext(), node.getBackground()); + (ComparableDrawableWrapper) node.getBackground(); assertThat(shadowOf(comparable.getWrappedDrawable()).getCreatedFromResId()) .isEqualTo(shadowOf(d).getCreatedFromResId()); } diff --git a/litho-it/src/test/java/com/facebook/litho/SparseCommonPropsTest.java b/litho-it/src/test/java/com/facebook/litho/SparseCommonPropsTest.java index ae6810fd08e..6217c45fdac 100644 --- a/litho-it/src/test/java/com/facebook/litho/SparseCommonPropsTest.java +++ b/litho-it/src/test/java/com/facebook/litho/SparseCommonPropsTest.java @@ -29,8 +29,6 @@ import com.facebook.litho.annotations.ImportantForAccessibility; import com.facebook.litho.drawable.ComparableColorDrawable; import com.facebook.litho.drawable.ComparableDrawable; -import com.facebook.litho.reference.DrawableReference; -import com.facebook.litho.reference.Reference; import com.facebook.litho.testing.testrunner.ComponentsTestRunner; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaDirection; @@ -121,8 +119,7 @@ public void testSetPropsAndBuild() { mCommonProps.touchExpansionPx(YogaEdge.LEFT, 23); mCommonProps.touchExpansionPx(YogaEdge.ALL, 21); ComparableDrawable background = ComparableColorDrawable.create(Color.RED); - Reference bgRef = DrawableReference.create(background); - mCommonProps.background(bgRef); + mCommonProps.background(background); ComparableDrawable foreground = ComparableColorDrawable.create(Color.BLACK); mCommonProps.foreground(foreground); @@ -264,7 +261,7 @@ public void testSetPropsAndBuild() { verify(mNode).touchExpansionPx(YogaEdge.LEFT, 23); verify(mNode).touchExpansionPx(YogaEdge.ALL, 21); - verify(mNode).background(bgRef); + verify(mNode).background(background); verify(mNode).foreground(foreground); verify(mNode).wrapInView(); diff --git a/litho-it/src/test/java/com/facebook/litho/reference/DrawableReferenceTest.java b/litho-it/src/test/java/com/facebook/litho/reference/DrawableReferenceTest.java deleted file mode 100644 index 38aa6f2bd1c..00000000000 --- a/litho-it/src/test/java/com/facebook/litho/reference/DrawableReferenceTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import static com.facebook.litho.reference.Reference.acquire; -import static org.assertj.core.api.Java6Assertions.assertThat; -import static org.robolectric.RuntimeEnvironment.application; - -import android.content.Context; -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import com.facebook.litho.ComponentContext; -import com.facebook.litho.drawable.DefaultComparableDrawable; -import com.facebook.litho.testing.drawable.TestColorDrawable; -import com.facebook.litho.testing.testrunner.ComponentsTestRunner; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(ComponentsTestRunner.class) -public class DrawableReferenceTest { - - @Test - public void testAcquire() { - Drawable drawable = new TestColorDrawable(Color.BLACK); - Context context = new ComponentContext(application).getAndroidContext(); - - DefaultComparableDrawable comparable = - (DefaultComparableDrawable) - acquire(context, DrawableReference.create(DefaultComparableDrawable.create(drawable))); - - assertThat(comparable.getWrappedDrawable()).isEqualTo(drawable); - } -} diff --git a/litho-it/src/test/java/com/facebook/litho/reference/DrawableResourcesCacheTest.java b/litho-it/src/test/java/com/facebook/litho/reference/DrawableResourcesCacheTest.java deleted file mode 100644 index 002c7c20a83..00000000000 --- a/litho-it/src/test/java/com/facebook/litho/reference/DrawableResourcesCacheTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import static org.assertj.core.api.Java6Assertions.assertThat; -import static org.junit.Assert.assertNotEquals; -import static org.robolectric.RuntimeEnvironment.application; - -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; -import com.facebook.litho.testing.testrunner.ComponentsTestRunner; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(ComponentsTestRunner.class) -public class DrawableResourcesCacheTest { - - private DrawableResourcesCache mCache; - - @Before - public void setup() { - mCache = new DrawableResourcesCache(); - } - - @Test - public void testPoolIsNeverEmpty() { - - Resources resources = application.getResources(); - // This being null is ok since we are only using this drawable to test the cache. - // We still need to declare the variable though otherewise the call to the constructor would be - // ambiguous. - Bitmap bitmap = null; - BitmapDrawable drawable = new BitmapDrawable(resources, bitmap); - - mCache.release(drawable, 1); - mCache.release(new ColorDrawable(), 2); - - Drawable first = mCache.get(1, resources, null); - Drawable second = mCache.get(1, resources, null); - Drawable third = mCache.get(2, resources, null); - - assertThat(first).isNotNull(); - assertThat(second).isNotNull(); - assertThat(third).isNotNull(); - - assertThat(second.getConstantState()).isEqualTo(first.getConstantState()); - assertNotEquals(first.getConstantState(), third.getConstantState()); - } - - @Test - public void testReleaseAndGet() { - Resources resources = application.getResources(); - - ColorDrawable drawable = new ColorDrawable(); - ColorDrawable drawable2 = new ColorDrawable(); - ColorDrawable drawable3 = new ColorDrawable(); - mCache.release(drawable, 1); - mCache.release(drawable2, 1); - mCache.release(drawable3, 1); - - assertThat(mCache.get(1, resources)).isSameAs(drawable3); - assertThat(mCache.get(1, resources)).isSameAs(drawable2); - assertThat(mCache.get(1, resources)).isSameAs(drawable); - } - -} diff --git a/litho-it/src/test/java/com/facebook/litho/reference/ReferenceTest.java b/litho-it/src/test/java/com/facebook/litho/reference/ReferenceTest.java deleted file mode 100644 index 16442b288dd..00000000000 --- a/litho-it/src/test/java/com/facebook/litho/reference/ReferenceTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2014-present Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.litho.reference; - -import static com.facebook.litho.reference.Reference.acquire; -import static com.facebook.litho.reference.Reference.release; -import static org.assertj.core.api.Java6Assertions.assertThat; - -import android.content.Context; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; -import com.facebook.litho.ComponentContext; -import com.facebook.litho.testing.testrunner.ComponentsTestRunner; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; - -@RunWith(ComponentsTestRunner.class) -public class ReferenceTest { - private ComponentContext mContext; - - @Before - public void setup() { - mContext = new ComponentContext(RuntimeEnvironment.application); - } - - @Test - public void testAcquireAndRelease() { - TestReferenceLifecycle referenceLifecycle = new TestReferenceLifecycle(); - TestReference reference = new TestReference(referenceLifecycle); - - Drawable acquiredDrawable = acquire(mContext.getAndroidContext(), reference); - assertThat(referenceLifecycle.mAcquired).isTrue(); - - release(mContext.getAndroidContext(), acquiredDrawable, reference); - assertThat(referenceLifecycle.mReleased).isTrue(); - } - - private static class TestReference extends Reference { - private TestReference(ReferenceLifecycle lifecycle) { - super(lifecycle); - } - - @Override - public String getSimpleName() { - return "TestReference"; - } - } - - private static class TestReferenceLifecycle extends ReferenceLifecycle { - private boolean mAcquired; - private boolean mReleased; - - @Override - protected Drawable onAcquire(Context c, Reference reference) { - mAcquired = true; - return new ColorDrawable(0); - } - - @Override - protected void onRelease(Context c, Drawable value, Reference reference) { - mReleased = true; - } - } -} diff --git a/litho-it/src/test/java/com/facebook/litho/specmodels/model/PropValidationTest.java b/litho-it/src/test/java/com/facebook/litho/specmodels/model/PropValidationTest.java index 67a4a65fcaa..3c38b7d0dd3 100644 --- a/litho-it/src/test/java/com/facebook/litho/specmodels/model/PropValidationTest.java +++ b/litho-it/src/test/java/com/facebook/litho/specmodels/model/PropValidationTest.java @@ -188,8 +188,7 @@ public void testPropWithReservedType() { assertThat(validationErrors.get(0).message).isEqualTo( "Props may not be declared with the following argument types: " + "[com.facebook.litho.ComponentLayout, " + - "com.facebook.litho.Component.Builder, " + - "com.facebook.litho.reference.Reference.Builder]."); + "com.facebook.litho.Component.Builder]."); } @Test diff --git a/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/BuilderGenerator.java b/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/BuilderGenerator.java index 8d7d10888ad..d28759773a7 100644 --- a/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/BuilderGenerator.java +++ b/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/BuilderGenerator.java @@ -449,12 +449,6 @@ static TypeSpecDataHolder generatePropsBuilderMethods( specModel, prop, requiredIndex, ClassNames.COMPONENT_BUILDER, true)); } - if (getRawType(prop.getTypeName()).equals(ClassNames.REFERENCE)) { - dataHolder.addMethod( - builderBuilder( - specModel, prop, requiredIndex, ClassNames.REFERENCE_BUILDER, true)); - } - if (getRawType(prop.getTypeName()).equals(ClassNames.SECTION)) { dataHolder.addMethod( builderBuilder(specModel, prop, requiredIndex, ClassNames.SECTION_BUILDER, true)); diff --git a/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/ComponentBodyGenerator.java b/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/ComponentBodyGenerator.java index 79550f35354..192548b7661 100644 --- a/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/ComponentBodyGenerator.java +++ b/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/ComponentBodyGenerator.java @@ -722,10 +722,9 @@ static CodeBlock getCompareStatement( .endControlFlow(); break; - case Comparable.REFERENCE: + case Comparable.COMPARABLE_DRAWABLE: codeBlock - .beginControlFlow( - "if (Reference.shouldUpdate($L, $L))", firstComparator, secondComparator) + .beginControlFlow("if (!$L.isEquivalantTo($L))", firstComparator, secondComparator) .addStatement("return false") .endControlFlow(); break; @@ -813,8 +812,8 @@ static CodeBlock getCompareStatement( } else if (field.getTypeName().isPrimitive()) { return Comparable.PRIMITIVE; - } else if (field.getTypeName().equals(ClassNames.REFERENCE)) { - return Comparable.REFERENCE; + } else if (field.getTypeName().equals(ClassNames.COMPARABLE_DRAWABLE)) { + return Comparable.COMPARABLE_DRAWABLE; } else if (field.getTypeSpec().isSubInterface(ClassNames.COLLECTION)) { final int level = diff --git a/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/testing/MatcherGenerator.java b/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/testing/MatcherGenerator.java index 65811dde7a1..1ef6d5ab1c8 100644 --- a/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/testing/MatcherGenerator.java +++ b/litho-processor/src/main/java/com/facebook/litho/specmodels/generator/testing/MatcherGenerator.java @@ -240,10 +240,6 @@ private static TypeSpecDataHolder generatePropsBuilderMethods( dataHolder.addMethod(builderBuilder(specModel, prop, ClassNames.COMPONENT_BUILDER)); } - if (getRawType(prop.getTypeName()).equals(ClassNames.REFERENCE)) { - dataHolder.addMethod(builderBuilder(specModel, prop, ClassNames.REFERENCE_BUILDER)); - } - return dataHolder.build(); } diff --git a/litho-processor/src/main/java/com/facebook/litho/specmodels/model/ClassNames.java b/litho-processor/src/main/java/com/facebook/litho/specmodels/model/ClassNames.java index 6b46deae698..e5fea5b14c9 100644 --- a/litho-processor/src/main/java/com/facebook/litho/specmodels/model/ClassNames.java +++ b/litho-processor/src/main/java/com/facebook/litho/specmodels/model/ClassNames.java @@ -91,11 +91,8 @@ public interface ClassNames { ClassName TRANSITION_CONTAINER = ClassName.bestGuess("com.facebook.litho.ComponentLifecycle.TransitionContainer"); - ClassName REFERENCE = ClassName.bestGuess("com.facebook.litho.reference.Reference"); - ClassName REFERENCE_BUILDER = - ClassName.bestGuess("com.facebook.litho.reference.Reference.Builder"); - ClassName REFERENCE_LIFECYCLE = - ClassName.bestGuess("com.facebook.litho.reference.ReferenceLifecycle"); + ClassName COMPARABLE_DRAWABLE = + ClassName.bestGuess("com.facebook.litho.drawable.ComparableDrawable"); ClassName TREE_PROPS = ClassName.bestGuess("com.facebook.litho.TreeProps"); diff --git a/litho-processor/src/main/java/com/facebook/litho/specmodels/model/PropValidation.java b/litho-processor/src/main/java/com/facebook/litho/specmodels/model/PropValidation.java index 2af65f45cd5..cba3dd82d6b 100644 --- a/litho-processor/src/main/java/com/facebook/litho/specmodels/model/PropValidation.java +++ b/litho-processor/src/main/java/com/facebook/litho/specmodels/model/PropValidation.java @@ -168,7 +168,7 @@ public class PropValidation { new CommonPropModel( "background", ParameterizedTypeName.get( - ClassNames.REFERENCE, WildcardTypeName.subtypeOf(ClassNames.DRAWABLE))), + ClassNames.COMPARABLE_DRAWABLE, WildcardTypeName.subtypeOf(ClassNames.DRAWABLE))), new CommonPropModel("testKey", ClassNames.STRING), new CommonPropModel( "layoutDirection", ClassName.bestGuess("com.facebook.yoga.YogaDirection")), @@ -294,10 +294,8 @@ public class PropValidation { new CommonPropModel("alpha", TypeName.FLOAT), new CommonPropModel("rotation", TypeName.FLOAT)); - private static final List ILLEGAL_PROP_TYPES = Arrays.asList( - ClassNames.COMPONENT_LAYOUT, - ClassNames.COMPONENT_BUILDER, - ClassNames.REFERENCE_BUILDER); + private static final List ILLEGAL_PROP_TYPES = + Arrays.asList(ClassNames.COMPONENT_LAYOUT, ClassNames.COMPONENT_BUILDER); static List validate( SpecModel specModel, diff --git a/litho-testing/src/main/java/com/facebook/litho/testing/subcomponents/InspectableComponent.java b/litho-testing/src/main/java/com/facebook/litho/testing/subcomponents/InspectableComponent.java index 89969564483..316ac95fe6b 100644 --- a/litho-testing/src/main/java/com/facebook/litho/testing/subcomponents/InspectableComponent.java +++ b/litho-testing/src/main/java/com/facebook/litho/testing/subcomponents/InspectableComponent.java @@ -27,7 +27,7 @@ import com.facebook.litho.LithoView; import com.facebook.litho.LithoViewTestHelper; import com.facebook.litho.StateContainer; -import com.facebook.litho.reference.Reference; +import com.facebook.litho.drawable.ComparableDrawable; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -183,7 +183,7 @@ public Drawable getForeground() { /** @return The background drawable asscociated with this debug component. May be null. */ @Nullable - public Reference getBackground() { + public ComparableDrawable getBackground() { final DebugLayoutNode layout = mComponent.getLayoutNode(); return layout == null ? null : layout.getBackground(); } diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/EditTextSpec.java b/litho-widget/src/main/java/com/facebook/litho/widget/EditTextSpec.java index 5e881985716..d66cae70fe3 100644 --- a/litho-widget/src/main/java/com/facebook/litho/widget/EditTextSpec.java +++ b/litho-widget/src/main/java/com/facebook/litho/widget/EditTextSpec.java @@ -76,7 +76,6 @@ import com.facebook.litho.annotations.PropDefault; import com.facebook.litho.annotations.ResType; import com.facebook.litho.annotations.State; -import com.facebook.litho.reference.Reference; import com.facebook.litho.utils.MeasureUtils; import java.lang.reflect.Field; import java.util.ArrayList; @@ -353,13 +352,11 @@ static void onMeasure( requestFocus, cursorDrawableRes); - Reference backgroundRef = (Reference) layout.getBackground(); - Drawable background = - backgroundRef != null ? Reference.acquire(c.getAndroidContext(), backgroundRef) : null; + Drawable background = layout.getBackground(); + if (background != null) { Rect rect = new Rect(); background.getPadding(rect); - Reference.release(c.getAndroidContext(), background, backgroundRef); if (rect.left != 0 || rect.top != 0 || rect.right != 0 || rect.bottom != 0) { // Padding from the background will be added to the layout separately, so does not need to