Skip to content

Commit bc59bc3

Browse files
Romain GuyAndroid (Google) Code Review
authored andcommitted
Merge "Properly cache color state lists."
2 parents 7e45fc0 + 5d911c3 commit bc59bc3

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

core/java/android/content/res/Resources.java

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import android.util.DisplayMetrics;
3333
import android.util.Log;
3434
import android.util.Slog;
35-
import android.util.SparseArray;
3635
import android.util.TypedValue;
3736
import android.util.LongSparseArray;
3837

@@ -86,8 +85,8 @@ public class Resources {
8685
// single-threaded, and after that these are immutable.
8786
private static final LongSparseArray<Drawable.ConstantState> sPreloadedDrawables
8887
= new LongSparseArray<Drawable.ConstantState>();
89-
private static final SparseArray<ColorStateList> mPreloadedColorStateLists
90-
= new SparseArray<ColorStateList>();
88+
private static final LongSparseArray<ColorStateList> sPreloadedColorStateLists
89+
= new LongSparseArray<ColorStateList>();
9190
private static final LongSparseArray<Drawable.ConstantState> sPreloadedColorDrawables
9291
= new LongSparseArray<Drawable.ConstantState>();
9392
private static boolean mPreloaded;
@@ -98,8 +97,8 @@ public class Resources {
9897
// These are protected by the mTmpValue lock.
9998
private final LongSparseArray<WeakReference<Drawable.ConstantState> > mDrawableCache
10099
= new LongSparseArray<WeakReference<Drawable.ConstantState> >();
101-
private final SparseArray<WeakReference<ColorStateList> > mColorStateListCache
102-
= new SparseArray<WeakReference<ColorStateList> >();
100+
private final LongSparseArray<WeakReference<ColorStateList> > mColorStateListCache
101+
= new LongSparseArray<WeakReference<ColorStateList> >();
103102
private final LongSparseArray<WeakReference<Drawable.ConstantState> > mColorDrawableCache
104103
= new LongSparseArray<WeakReference<Drawable.ConstantState> >();
105104
private boolean mPreloading;
@@ -118,22 +117,6 @@ public class Resources {
118117

119118
private CompatibilityInfo mCompatibilityInfo;
120119

121-
private static final LongSparseArray<Object> EMPTY_ARRAY = new LongSparseArray<Object>(0) {
122-
@Override
123-
public void put(long k, Object o) {
124-
throw new UnsupportedOperationException();
125-
}
126-
@Override
127-
public void append(long k, Object o) {
128-
throw new UnsupportedOperationException();
129-
}
130-
};
131-
132-
@SuppressWarnings("unchecked")
133-
private static <T> LongSparseArray<T> emptySparseArray() {
134-
return (LongSparseArray<T>) EMPTY_ARRAY;
135-
}
136-
137120
/** @hide */
138121
public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
139122
return selectSystemTheme(curTheme, targetSdkVersion,
@@ -180,9 +163,8 @@ public NotFoundException(String name) {
180163
* @param config Desired device configuration to consider when
181164
* selecting/computing resource values (optional).
182165
*/
183-
public Resources(AssetManager assets, DisplayMetrics metrics,
184-
Configuration config) {
185-
this(assets, metrics, config, (CompatibilityInfo) null);
166+
public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
167+
this(assets, metrics, config, null);
186168
}
187169

188170
/**
@@ -1883,7 +1865,8 @@ public final void finishPreloading() {
18831865
return dr;
18841866
}
18851867

1886-
Drawable.ConstantState cs = isColorDrawable ? sPreloadedColorDrawables.get(key) : sPreloadedDrawables.get(key);
1868+
Drawable.ConstantState cs = isColorDrawable ?
1869+
sPreloadedColorDrawables.get(key) : sPreloadedDrawables.get(key);
18871870
if (cs != null) {
18881871
dr = cs.newDrawable(this);
18891872
} else {
@@ -2005,21 +1988,21 @@ private Drawable getCachedDrawable(
20051988
}
20061989
}
20071990

2008-
final int key = (value.assetCookie << 24) | value.data;
1991+
final long key = (((long) value.assetCookie) << 32) | value.data;
20091992

20101993
ColorStateList csl;
20111994

20121995
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
20131996
value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
20141997

2015-
csl = mPreloadedColorStateLists.get(key);
1998+
csl = sPreloadedColorStateLists.get(key);
20161999
if (csl != null) {
20172000
return csl;
20182001
}
20192002

20202003
csl = ColorStateList.valueOf(value.data);
20212004
if (mPreloading) {
2022-
mPreloadedColorStateLists.put(key, csl);
2005+
sPreloadedColorStateLists.put(key, csl);
20232006
}
20242007

20252008
return csl;
@@ -2030,7 +2013,7 @@ private Drawable getCachedDrawable(
20302013
return csl;
20312014
}
20322015

2033-
csl = mPreloadedColorStateLists.get(key);
2016+
csl = sPreloadedColorStateLists.get(key);
20342017
if (csl != null) {
20352018
return csl;
20362019
}
@@ -2063,22 +2046,21 @@ private Drawable getCachedDrawable(
20632046

20642047
if (csl != null) {
20652048
if (mPreloading) {
2066-
mPreloadedColorStateLists.put(key, csl);
2049+
sPreloadedColorStateLists.put(key, csl);
20672050
} else {
20682051
synchronized (mTmpValue) {
20692052
//Log.i(TAG, "Saving cached color state list @ #" +
20702053
// Integer.toHexString(key.intValue())
20712054
// + " in " + this + ": " + csl);
2072-
mColorStateListCache.put(
2073-
key, new WeakReference<ColorStateList>(csl));
2055+
mColorStateListCache.put(key, new WeakReference<ColorStateList>(csl));
20742056
}
20752057
}
20762058
}
20772059

20782060
return csl;
20792061
}
20802062

2081-
private ColorStateList getCachedColorStateList(int key) {
2063+
private ColorStateList getCachedColorStateList(long key) {
20822064
synchronized (mTmpValue) {
20832065
WeakReference<ColorStateList> wr = mColorStateListCache.get(key);
20842066
if (wr != null) { // we have the key
@@ -2088,8 +2070,7 @@ private ColorStateList getCachedColorStateList(int key) {
20882070
// Integer.toHexString(((Integer)key).intValue())
20892071
// + " in " + this + ": " + entry);
20902072
return entry;
2091-
}
2092-
else { // our entry has been purged
2073+
} else { // our entry has been purged
20932074
mColorStateListCache.delete(key);
20942075
}
20952076
}

0 commit comments

Comments
 (0)