Merge "Fix AttributeCache" into nyc-mr1-dev

This commit is contained in:
TreeHugger Robot
2016-06-27 23:11:32 +00:00
committed by Android (Google) Code Review

View File

@@ -25,23 +25,24 @@ import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.os.UserHandle; import android.os.UserHandle;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.LruCache;
import android.util.SparseArray; import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import java.lang.ref.WeakReference;
/** /**
* TODO: This should be better integrated into the system so it doesn't need * TODO: This should be better integrated into the system so it doesn't need
* special calls from the activity manager to clear it. * special calls from the activity manager to clear it.
*/ */
public final class AttributeCache { public final class AttributeCache {
private static final int CACHE_SIZE = 4;
private static AttributeCache sInstance = null; private static AttributeCache sInstance = null;
private final Context mContext; private final Context mContext;
@GuardedBy("this") @GuardedBy("this")
private final ArrayMap<String, WeakReference<Package>> mPackages = new ArrayMap<>(); private final LruCache<String, Package> mPackages = new LruCache<>(CACHE_SIZE);
@GuardedBy("this") @GuardedBy("this")
private final Configuration mConfiguration = new Configuration(); private final Configuration mConfiguration = new Configuration();
@@ -86,15 +87,12 @@ public final class AttributeCache {
public void removePackage(String packageName) { public void removePackage(String packageName) {
synchronized (this) { synchronized (this) {
final WeakReference<Package> ref = mPackages.remove(packageName); final Package pkg = mPackages.remove(packageName);
final Package pkg = (ref != null) ? ref.get() : null;
if (pkg != null) { if (pkg != null) {
if (pkg.mMap != null) { for (int i = 0; i < pkg.mMap.size(); i++) {
for (int i = 0; i < pkg.mMap.size(); i++) { final ArrayMap<int[], Entry> map = pkg.mMap.valueAt(i);
final ArrayMap<int[], Entry> map = pkg.mMap.valueAt(i); for (int j = 0; j < map.size(); j++) {
for (int j = 0; j < map.size(); j++) { map.valueAt(j).recycle();
map.valueAt(j).recycle();
}
} }
} }
@@ -113,15 +111,14 @@ public final class AttributeCache {
// The configurations being masked out are ones that commonly // The configurations being masked out are ones that commonly
// change so we don't want flushing the cache... all others // change so we don't want flushing the cache... all others
// will flush the cache. // will flush the cache.
mPackages.clear(); mPackages.evictAll();
} }
} }
} }
public Entry get(String packageName, int resId, int[] styleable, int userId) { public Entry get(String packageName, int resId, int[] styleable, int userId) {
synchronized (this) { synchronized (this) {
WeakReference<Package> ref = mPackages.get(packageName); Package pkg = mPackages.get(packageName);
Package pkg = (ref != null) ? ref.get() : null;
ArrayMap<int[], Entry> map = null; ArrayMap<int[], Entry> map = null;
Entry ent = null; Entry ent = null;
if (pkg != null) { if (pkg != null) {
@@ -144,7 +141,7 @@ public final class AttributeCache {
return null; return null;
} }
pkg = new Package(context); pkg = new Package(context);
mPackages.put(packageName, new WeakReference<>(pkg)); mPackages.put(packageName, pkg);
} }
if (map == null) { if (map == null) {