make sure wificonfiguration scan cache doesnt grow unbounded
Bug:18703749

Change-Id: If6452e7dbaaabb164cf2a99b48b3964f4cb86ee5
diff --git a/service/java/com/android/server/wifi/WifiConfigStore.java b/service/java/com/android/server/wifi/WifiConfigStore.java
index cf41635..cd7cb57 100644
--- a/service/java/com/android/server/wifi/WifiConfigStore.java
+++ b/service/java/com/android/server/wifi/WifiConfigStore.java
@@ -389,6 +389,8 @@
     public int currentNetworkBoost = 25;
     public int scanResultRssiLevelPatchUp = -85;
 
+    public static final int maxNumScanCacheEntries = 128;
+
     /**
      * Regex pattern for extracting a connect choice.
      * Matches a strings like the following:
@@ -2103,7 +2105,6 @@
                         }
 
                         if (key.startsWith(BSSID_KEY_END)) {
-
                             if ((bssid != null) && (ssid != null)) {
 
                                 if (config.scanResultCache == null) {
@@ -3358,6 +3359,13 @@
                     scanResult.untrusted = true;
                 }
 
+                if (config.scanResultCache.size() > (maxNumScanCacheEntries+64)) {
+                    // Trim the scan result cache to maxNumScanCacheEntries entries max
+                    // Since this operation is expensive, make sure it is not performed
+                    // until the cache has grown significantly above the trim treshold
+                    config.trimScanResultsCache(maxNumScanCacheEntries);
+                }
+
                 // Add the scan result to this WifiConfiguration
                 config.scanResultCache.put(scanResult.BSSID, scanResult);
                 // Since we added a scan result to this configuration, re-attempt linking