Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad7e7485 authored by Alan Viverette's avatar Alan Viverette
Browse files

Refresh opacity and statefulness on tint change

Also warns when LayerDrawable is created with an invalid child. This
is not guaranteed to fail, but it's usually a bad sign.

Bug: 33124798
Test: LayerDrawableTest, DrawableContainerTest
Change-Id: Ie3e4200b27a9814cee7f5711d7df9710db513953
parent 36db127e
Loading
Loading
Loading
Loading
+19 −2
Original line number Original line Diff line number Diff line
@@ -210,6 +210,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
    /**
    /**
     * Change the global fade duration when a new drawable is entering
     * Change the global fade duration when a new drawable is entering
     * the scene.
     * the scene.
     *
     * @param ms The amount of time to fade in milliseconds.
     * @param ms The amount of time to fade in milliseconds.
     */
     */
    public void setEnterFadeDuration(int ms) {
    public void setEnterFadeDuration(int ms) {
@@ -219,6 +220,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
    /**
    /**
     * Change the global fade duration when a new drawable is leaving
     * Change the global fade duration when a new drawable is leaving
     * the scene.
     * the scene.
     *
     * @param ms The amount of time to fade in milliseconds.
     * @param ms The amount of time to fade in milliseconds.
     */
     */
    public void setExitFadeDuration(int ms) {
    public void setExitFadeDuration(int ms) {
@@ -375,6 +377,13 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {


    @Override
    @Override
    public void invalidateDrawable(@NonNull Drawable who) {
    public void invalidateDrawable(@NonNull Drawable who) {
        // This may have been called as the result of a tint changing, in
        // which case we may need to refresh the cached statefulness or
        // opacity.
        if (mDrawableContainerState != null) {
            mDrawableContainerState.invalidateCache();
        }

        if (who == mCurrDrawable && getCallback() != null) {
        if (who == mCurrDrawable && getCallback() != null) {
            getCallback().invalidateDrawable(this);
            getCallback().invalidateDrawable(this);
        }
        }
@@ -822,8 +831,8 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
            mDrawables[pos] = dr;
            mDrawables[pos] = dr;
            mNumChildren++;
            mNumChildren++;
            mChildrenChangingConfigurations |= dr.getChangingConfigurations();
            mChildrenChangingConfigurations |= dr.getChangingConfigurations();
            mCheckedStateful = false;

            mCheckedOpacity = false;
            invalidateCache();


            mConstantPadding = null;
            mConstantPadding = null;
            mCheckedPadding = false;
            mCheckedPadding = false;
@@ -833,6 +842,14 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
            return pos;
            return pos;
        }
        }


        /**
         * Invalidates the cached opacity and statefulness.
         */
        void invalidateCache() {
            mCheckedOpacity = false;
            mCheckedStateful = false;
        }

        final int getCapacity() {
        final int getCapacity() {
            return mDrawables.length;
            return mDrawables.length;
        }
        }
+25 −12
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@ import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.DisplayMetrics;
import android.util.LayoutDirection;
import android.util.LayoutDirection;
import android.util.Log;
import android.view.Gravity;
import android.view.Gravity;
import android.view.View;
import android.view.View;


@@ -66,6 +67,8 @@ import java.io.IOException;
 * @attr ref android.R.styleable#LayerDrawableItem_id
 * @attr ref android.R.styleable#LayerDrawableItem_id
*/
*/
public class LayerDrawable extends Drawable implements Drawable.Callback {
public class LayerDrawable extends Drawable implements Drawable.Callback {
    private static final String LOG_TAG = "LayerDrawable";

    /**
    /**
     * Padding mode used to nest each layer inside the padding of the previous
     * Padding mode used to nest each layer inside the padding of the previous
     * layer.
     * layer.
@@ -89,6 +92,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
     */
     */
    public static final int INSET_UNDEFINED = Integer.MIN_VALUE;
    public static final int INSET_UNDEFINED = Integer.MIN_VALUE;


    @NonNull
    LayerState mLayerState;
    LayerState mLayerState;


    private int[] mPaddingL;
    private int[] mPaddingL;
@@ -170,13 +174,9 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
            throws XmlPullParserException, IOException {
            throws XmlPullParserException, IOException {
        super.inflate(r, parser, attrs, theme);
        super.inflate(r, parser, attrs, theme);


        final LayerState state = mLayerState;
        if (state == null) {
            return;
        }

        // The density may have changed since the last update. This will
        // The density may have changed since the last update. This will
        // apply scaling to any existing constant state properties.
        // apply scaling to any existing constant state properties.
        final LayerState state = mLayerState;
        final int density = Drawable.resolveDensity(r, 0);
        final int density = Drawable.resolveDensity(r, 0);
        state.setDensity(density);
        state.setDensity(density);


@@ -202,10 +202,6 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
        super.applyTheme(t);
        super.applyTheme(t);


        final LayerState state = mLayerState;
        final LayerState state = mLayerState;
        if (state == null) {
            return;
        }

        final int density = Drawable.resolveDensity(t.getResources(), 0);
        final int density = Drawable.resolveDensity(t.getResources(), 0);
        state.setDensity(density);
        state.setDensity(density);


@@ -403,7 +399,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {


    @Override
    @Override
    public boolean canApplyTheme() {
    public boolean canApplyTheme() {
        return (mLayerState != null && mLayerState.canApplyTheme()) || super.canApplyTheme();
        return mLayerState.canApplyTheme() || super.canApplyTheme();
    }
    }


    /**
    /**
@@ -986,6 +982,11 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
        if (mSuspendChildInvalidation) {
        if (mSuspendChildInvalidation) {
            mChildRequestedInvalidation = true;
            mChildRequestedInvalidation = true;
        } else {
        } else {
            // This may have been called as the result of a tint changing, in
            // which case we may need to refresh the cached statefulness or
            // opacity.
            mLayerState.invalidateCache();

            invalidateSelf();
            invalidateSelf();
        }
        }
    }
    }
@@ -1836,15 +1837,24 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
                final ConstantState cs = dr.getConstantState();
                final ConstantState cs = dr.getConstantState();
                if (cs == null) {
                if (cs == null) {
                    clone = dr;
                    clone = dr;
                    if (dr.getCallback() != null) {
                        // This drawable already has an owner.
                        Log.w(LOG_TAG, "Invalid drawable added to LayerDrawable! Drawable already "
                                + "belongs to another owner but does not expose a constant state.",
                                new RuntimeException());
                    }
                } else if (res != null) {
                } else if (res != null) {
                    clone = cs.newDrawable(res);
                    clone = cs.newDrawable(res);
                } else {
                } else {
                    clone = cs.newDrawable();
                    clone = cs.newDrawable();
                }
                }
                clone.setCallback(owner);
                clone.setLayoutDirection(dr.getLayoutDirection());
                clone.setLayoutDirection(dr.getLayoutDirection());
                clone.setBounds(dr.getBounds());
                clone.setBounds(dr.getBounds());
                clone.setLevel(dr.getLevel());
                clone.setLevel(dr.getLevel());

                // Set the callback last to prevent invalidation from
                // propagating before the constant state has been set.
                clone.setCallback(owner);
            } else {
            } else {
                clone = null;
                clone = null;
            }
            }
@@ -2121,7 +2131,10 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
            return true;
            return true;
        }
        }


        public void invalidateCache() {
        /**
         * Invalidates the cached opacity and statefulness.
         */
        void invalidateCache() {
            mCheckedOpacity = false;
            mCheckedOpacity = false;
            mCheckedStateful = false;
            mCheckedStateful = false;
        }
        }