Release AssetManagers when ejecting storage.

When ejecting a storage device, the system process needs to rapidly
release any open FDs to prevent itself from being killed by vold.

This change examines all ResourceImpls cached inside the system
process and evicts any that reference the storage device being
ejected.  (ResourcesManager will gladly recreate any evicted entries
when asked again in the future.)

Also replace broken use of WeakHashMap, since we want the values to
be weak references, not the keys.

Bug: 28867548
Change-Id: Ib9cfc66497149b6d3f8d49213e9779408a331d2a
diff --git a/services/core/java/com/android/server/AttributeCache.java b/services/core/java/com/android/server/AttributeCache.java
index 427dbc0..57f18c0 100644
--- a/services/core/java/com/android/server/AttributeCache.java
+++ b/services/core/java/com/android/server/AttributeCache.java
@@ -24,10 +24,12 @@
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.os.UserHandle;
+import android.util.ArrayMap;
 import android.util.SparseArray;
 
-import java.util.HashMap;
-import java.util.WeakHashMap;
+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
@@ -35,17 +37,18 @@
  */
 public final class AttributeCache {
     private static AttributeCache sInstance = null;
-    
+
     private final Context mContext;
-    private final WeakHashMap<String, Package> mPackages =
-            new WeakHashMap<String, Package>();
+
+    @GuardedBy("this")
+    private final ArrayMap<String, WeakReference<Package>> mPackages = new ArrayMap<>();
+    @GuardedBy("this")
     private final Configuration mConfiguration = new Configuration();
-    
+
     public final static class Package {
         public final Context context;
-        private final SparseArray<HashMap<int[], Entry>> mMap
-                = new SparseArray<HashMap<int[], Entry>>();
-        
+        private final SparseArray<ArrayMap<int[], Entry>> mMap = new SparseArray<>();
+
         public Package(Context c) {
             context = c;
         }
@@ -59,6 +62,12 @@
             context = c;
             array = ta;
         }
+
+        void recycle() {
+            if (array != null) {
+                array.recycle();
+            }
+        }
     }
     
     public static void init(Context context) {
@@ -74,13 +83,27 @@
     public AttributeCache(Context context) {
         mContext = context;
     }
-    
+
     public void removePackage(String packageName) {
         synchronized (this) {
-            mPackages.remove(packageName);
+            final WeakReference<Package> ref = mPackages.remove(packageName);
+            final Package pkg = (ref != null) ? ref.get() : null;
+            if (pkg != null) {
+                if (pkg.mMap != null) {
+                    for (int i = 0; i < pkg.mMap.size(); i++) {
+                        final ArrayMap<int[], Entry> map = pkg.mMap.valueAt(i);
+                        for (int j = 0; j < map.size(); j++) {
+                            map.valueAt(j).recycle();
+                        }
+                    }
+                }
+
+                final Resources res = pkg.context.getResources();
+                res.flushLayoutCache();
+            }
         }
     }
-    
+
     public void updateConfiguration(Configuration config) {
         synchronized (this) {
             int changes = mConfiguration.updateFrom(config);
@@ -97,8 +120,9 @@
     
     public Entry get(String packageName, int resId, int[] styleable, int userId) {
         synchronized (this) {
-            Package pkg = mPackages.get(packageName);
-            HashMap<int[], Entry> map = null;
+            WeakReference<Package> ref = mPackages.get(packageName);
+            Package pkg = (ref != null) ? ref.get() : null;
+            ArrayMap<int[], Entry> map = null;
             Entry ent = null;
             if (pkg != null) {
                 map = pkg.mMap.get(resId);
@@ -120,11 +144,11 @@
                     return null;
                 }
                 pkg = new Package(context);
-                mPackages.put(packageName, pkg);
+                mPackages.put(packageName, new WeakReference<>(pkg));
             }
             
             if (map == null) {
-                map = new HashMap<int[], Entry>();
+                map = new ArrayMap<>();
                 pkg.mMap.put(resId, map);
             }