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

Commit 0daa5713 authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Fix issues with theming of preloaded ColorStateLists"

parents bcfe87f3 e0f95f39
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -9448,11 +9448,10 @@ package android.content.res {
  public class ColorStateList implements android.os.Parcelable {
    ctor public ColorStateList(int[][], int[]);
    method public void applyTheme(android.content.res.Resources.Theme);
    method public boolean canApplyTheme();
    method public static deprecated android.content.res.ColorStateList createFromXml(android.content.res.Resources, org.xmlpull.v1.XmlPullParser) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
    method public static android.content.res.ColorStateList createFromXml(android.content.res.Resources, org.xmlpull.v1.XmlPullParser, android.content.res.Resources.Theme) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
    method public int describeContents();
    method public int getChangingConfigurations();
    method public int getColorForState(int[], int);
    method public int getDefaultColor();
    method public boolean isOpaque();
+1 −2
Original line number Diff line number Diff line
@@ -9737,11 +9737,10 @@ package android.content.res {
  public class ColorStateList implements android.os.Parcelable {
    ctor public ColorStateList(int[][], int[]);
    method public void applyTheme(android.content.res.Resources.Theme);
    method public boolean canApplyTheme();
    method public static deprecated android.content.res.ColorStateList createFromXml(android.content.res.Resources, org.xmlpull.v1.XmlPullParser) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
    method public static android.content.res.ColorStateList createFromXml(android.content.res.Resources, org.xmlpull.v1.XmlPullParser, android.content.res.Resources.Theme) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
    method public int describeContents();
    method public int getChangingConfigurations();
    method public int getColorForState(int[], int);
    method public int getDefaultColor();
    method public boolean isOpaque();
+61 −19
Original line number Diff line number Diff line
@@ -71,10 +71,15 @@ import java.util.Arrays;
 */
public class ColorStateList implements Parcelable {
    private static final String TAG = "ColorStateList";

    private static final int DEFAULT_COLOR = Color.RED;
    private static final int[][] EMPTY = new int[][] { new int[0] };
    private static final SparseArray<WeakReference<ColorStateList>> sCache =
                            new SparseArray<WeakReference<ColorStateList>>();

    /** Thread-safe cache of single-color ColorStateLists. */
    private static final SparseArray<WeakReference<ColorStateList>> sCache = new SparseArray<>();

    /** Lazily-created factory for this color state list. */
    private ColorStateListFactory mFactory;

    private int[][] mThemeAttrs;
    private int mChangingConfigurations;
@@ -125,7 +130,7 @@ public class ColorStateList implements Parcelable {
            }

            final ColorStateList csl = new ColorStateList(EMPTY, new int[] { color });
            sCache.put(color, new WeakReference<ColorStateList>(csl));
            sCache.put(color, new WeakReference<>(csl));
            return csl;
        }
    }
@@ -141,11 +146,13 @@ public class ColorStateList implements Parcelable {
     */
    private ColorStateList(ColorStateList orig) {
        if (orig != null) {
            mChangingConfigurations = orig.mChangingConfigurations;
            mStateSpecs = orig.mStateSpecs;
            mDefaultColor = orig.mDefaultColor;
            mIsOpaque = orig.mIsOpaque;

            // Deep copy, this may change due to theming.
            // Deep copy, these may change due to applyTheme().
            mThemeAttrs = orig.mThemeAttrs.clone();
            mColors = orig.mColors.clone();
        }
    }
@@ -329,6 +336,7 @@ public class ColorStateList implements Parcelable {
     * attributes.
     *
     * @return whether a theme can be applied to this color state list
     * @hide only for resource preloading
     */
    public boolean canApplyTheme() {
        return mThemeAttrs != null;
@@ -336,10 +344,15 @@ public class ColorStateList implements Parcelable {

    /**
     * Applies a theme to this color state list.
     * <p>
     * <strong>Note:</strong> Applying a theme may affect the changing
     * configuration parameters of this color state list. After calling this
     * method, any dependent configurations must be updated by obtaining the
     * new configuration mask from {@link #getChangingConfigurations()}.
     *
     * @param t the theme to apply
     */
    public void applyTheme(Theme t) {
    private void applyTheme(Theme t) {
        if (mThemeAttrs == null) {
            return;
        }
@@ -376,6 +389,38 @@ public class ColorStateList implements Parcelable {
        onColorsChanged();
    }

    /**
     * Returns an appropriately themed color state list.
     *
     * @param t the theme to apply
     * @return a copy of the color state list with the theme applied, or the
     *         color state list itself if there were no unresolved theme
     *         attributes
     * @hide only for resource preloading
     */
    public ColorStateList obtainForTheme(Theme t) {
        if (t == null || !canApplyTheme()) {
            return this;
        }

        final ColorStateList clone = new ColorStateList(this);
        clone.applyTheme(t);
        return clone;
    }

    /**
     * Returns a mask of the configuration parameters for which this color
     * state list may change, requiring that it be re-created.
     *
     * @return a mask of the changing configuration parameters, as defined by
     *         {@link android.content.pm.ActivityInfo}
     *
     * @see android.content.pm.ActivityInfo
     */
    public int getChangingConfigurations() {
        return mChangingConfigurations;
    }

    private int modulateColorAlpha(int baseColor, float alphaMod) {
        if (alphaMod == 1.0f) {
            return baseColor;
@@ -383,8 +428,7 @@ public class ColorStateList implements Parcelable {

        final int baseAlpha = Color.alpha(baseColor);
        final int alpha = MathUtils.constrain((int) (baseAlpha * alphaMod + 0.5f), 0, 255);
        final int color = (baseColor & 0xFFFFFF) | (alpha << 24);
        return color;
        return (baseColor & 0xFFFFFF) | (alpha << 24);
    }

    /**
@@ -534,14 +578,18 @@ public class ColorStateList implements Parcelable {
    }

    /**
     * @return A factory that can create new instances of this ColorStateList.
     * @return a factory that can create new instances of this ColorStateList
     * @hide only for resource preloading
     */
    ColorStateListFactory getFactory() {
        return new ColorStateListFactory(this);
    public ConstantState<ColorStateList> getConstantState() {
        if (mFactory != null) {
            mFactory = new ColorStateListFactory(this);
        }
        return mFactory;
    }

    static class ColorStateListFactory extends ConstantState<ColorStateList> {
        final ColorStateList mSrc;
    private static class ColorStateListFactory extends ConstantState<ColorStateList> {
        private final ColorStateList mSrc;

        public ColorStateListFactory(ColorStateList src) {
            mSrc = src;
@@ -559,13 +607,7 @@ public class ColorStateList implements Parcelable {

        @Override
        public ColorStateList newInstance(Resources res, Theme theme) {
            if (theme == null || !mSrc.canApplyTheme()) {
                return mSrc;
            }

            final ColorStateList clone = new ColorStateList(mSrc);
            clone.applyTheme(theme);
            return clone;
            return mSrc.obtainForTheme(theme);
        }
    }

+9 −8
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ import android.annotation.RawRes;
import android.annotation.StringRes;
import android.annotation.XmlRes;
import android.content.pm.ActivityInfo;
import android.content.res.ColorStateList.ColorStateListFactory;
import android.graphics.Movie;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
@@ -112,8 +111,8 @@ public class Resources {
    private static final LongSparseArray<ConstantState>[] sPreloadedDrawables;
    private static final LongSparseArray<ConstantState> sPreloadedColorDrawables
            = new LongSparseArray<>();
    private static final LongSparseArray<ColorStateListFactory> sPreloadedColorStateLists
            = new LongSparseArray<>();
    private static final LongSparseArray<android.content.res.ConstantState<ColorStateList>>
            sPreloadedColorStateLists = new LongSparseArray<>();

    // Pool of TypedArrays targeted to this Resources object.
    final SynchronizedPool<TypedArray> mTypedArrayPool = new SynchronizedPool<>(5);
@@ -2667,7 +2666,8 @@ public class Resources {
        // Handle inline color definitions.
        if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT
                && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
            final ColorStateListFactory factory = sPreloadedColorStateLists.get(key);
            final android.content.res.ConstantState<ColorStateList> factory =
                    sPreloadedColorStateLists.get(key);
            if (factory != null) {
                return factory.newInstance();
            }
@@ -2677,7 +2677,7 @@ public class Resources {
            if (mPreloading) {
                if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId,
                        "color")) {
                    sPreloadedColorStateLists.put(key, csl.getFactory());
                    sPreloadedColorStateLists.put(key, csl.getConstantState());
                }
            }

@@ -2691,7 +2691,8 @@ public class Resources {
            return csl;
        }

        final ColorStateListFactory factory = sPreloadedColorStateLists.get(key);
        final android.content.res.ConstantState<ColorStateList> factory =
                sPreloadedColorStateLists.get(key);
        if (factory != null) {
            csl = factory.newInstance(this, theme);
        }
@@ -2704,10 +2705,10 @@ public class Resources {
            if (mPreloading) {
                if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId,
                        "color")) {
                    sPreloadedColorStateLists.put(key, csl.getFactory());
                    sPreloadedColorStateLists.put(key, csl.getConstantState());
                }
            } else {
                cache.put(key, theme, csl.getFactory());
                cache.put(key, theme, csl.getConstantState());
            }
        }

+1 −1
Original line number Diff line number Diff line
@@ -173,7 +173,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {

    @Override
    public int getChangingConfigurations() {
        return super.getChangingConfigurations() | mAnimatedVectorState.mChangingConfigurations;
        return super.getChangingConfigurations() | mAnimatedVectorState.getChangingConfigurations();
    }

    @Override
Loading