summaryrefslogtreecommitdiffstats
path: root/core/java/android
diff options
context:
space:
mode:
authorAlan Viverette <alanv@google.com>2015-04-02 18:02:03 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2015-04-02 18:02:04 +0000
commit0daa5713ee2df841ea22d88a1393ca11e0138621 (patch)
treec7edb73e7c5ac99785550722ae889d5f8d6b7cf3 /core/java/android
parentbcfe87f3e05ac65d7ecb7156cd0de580586df5e9 (diff)
parente0f95f39c5a669a48ee3ebb8dc45bf2d7ee940f1 (diff)
downloadframeworks_base-0daa5713ee2df841ea22d88a1393ca11e0138621.zip
frameworks_base-0daa5713ee2df841ea22d88a1393ca11e0138621.tar.gz
frameworks_base-0daa5713ee2df841ea22d88a1393ca11e0138621.tar.bz2
Merge "Fix issues with theming of preloaded ColorStateLists"
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/content/res/ColorStateList.java80
-rw-r--r--core/java/android/content/res/Resources.java17
2 files changed, 70 insertions, 27 deletions
diff --git a/core/java/android/content/res/ColorStateList.java b/core/java/android/content/res/ColorStateList.java
index 7d8dff3..fdafb04 100644
--- a/core/java/android/content/res/ColorStateList.java
+++ b/core/java/android/content/res/ColorStateList.java
@@ -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);
}
}
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java
index 44018ff..7ce3721 100644
--- a/core/java/android/content/res/Resources.java
+++ b/core/java/android/content/res/Resources.java
@@ -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());
}
}