FileDocCategorySizeDatePackage
ConfigurationBoundResourceCache.javaAPI DocAndroid 5.1 API5195Thu Mar 12 22:22:10 GMT 2015android.content.res

ConfigurationBoundResourceCache

public 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.
hide

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.

param
resources The Resource which can be used when creating new instances.


                              
       
        mResources = resources;
    
Methods Summary
public Tget(long key, Resources.Theme theme)
If the resource is cached, creates a new instance of it and returns.

param
key The long key which can be used to uniquely identify the resource.
param
theme The The Theme instance where we want to load this resource.
return
If this resources was loaded before, returns a new instance of it. Otherwise, returns null.

        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 voidonConfigurationChange(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.

param
configChanges The configuration changes

        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 voidonConfigurationChangeInt(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 voidput(long key, Resources.Theme theme, ConstantState constantState)
Adds a new item to the cache.

param
key A custom key that uniquely identifies the resource.
param
theme The Theme instance where this resource was loaded.
param
constantState The constant state that can create new instances of the resource.

        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));
        }