ConfigurationBoundResourceCachepublic class ConfigurationBoundResourceCache extends Object A Cache class which can be used to cache resource objects that are easy to clone but more
expensive to inflate. |
Fields Summary |
---|
private final android.util.ArrayMap | mCache | final Resources | mResources |
Constructors Summary |
---|
public ConfigurationBoundResourceCache(Resources resources)Creates a Resource cache for the given Resources instance.
mResources = resources;
|
Methods Summary |
---|
public T | get(long key, Resources.Theme theme)If the resource is cached, creates a new instance of it and returns.
final String themeKey = theme != null ? theme.getKey() : "";
final LongSparseArray<WeakReference<ConstantState<T>>> themedCache;
final WeakReference<ConstantState<T>> wr;
synchronized (this) {
themedCache = mCache.get(themeKey);
if (themedCache == null) {
return null;
}
wr = themedCache.get(key);
}
if (wr == null) {
return null;
}
final ConstantState entry = wr.get();
if (entry != null) {
return (T) entry.newInstance(mResources, theme);
} else { // our entry has been purged
synchronized (this) {
// there is a potential race condition here where this entry may be put in
// another thread. But we prefer it to minimize lock duration
themedCache.delete(key);
}
}
return null;
| public void | onConfigurationChange(int configChanges)Users of ConfigurationBoundResourceCache must call this method whenever a configuration
change happens. On this callback, the cache invalidates all resources that are not valid
anymore.
synchronized (this) {
final int size = mCache.size();
for (int i = size - 1; i >= 0; i--) {
final LongSparseArray<WeakReference<ConstantState<T>>>
themeCache = mCache.valueAt(i);
onConfigurationChangeInt(themeCache, configChanges);
if (themeCache.size() == 0) {
mCache.removeAt(i);
}
}
}
| private void | onConfigurationChangeInt(android.util.LongSparseArray themeCache, int configChanges)
final int size = themeCache.size();
for (int i = size - 1; i >= 0; i--) {
final WeakReference<ConstantState<T>> wr = themeCache.valueAt(i);
final ConstantState<T> constantState = wr.get();
if (constantState == null || Configuration.needNewResources(
configChanges, constantState.getChangingConfigurations())) {
themeCache.removeAt(i);
}
}
| public void | put(long key, Resources.Theme theme, ConstantState constantState)Adds a new item to the cache.
if (constantState == null) {
return;
}
final String themeKey = theme == null ? "" : theme.getKey();
LongSparseArray<WeakReference<ConstantState<T>>> themedCache;
synchronized (this) {
themedCache = mCache.get(themeKey);
if (themedCache == null) {
themedCache = new LongSparseArray<WeakReference<ConstantState<T>>>(1);
mCache.put(themeKey, themedCache);
}
themedCache.put(key, new WeakReference<ConstantState<T>>(constantState));
}
|
|