Adopt LruCache in SettingsProvider.

Change-Id: I223ed2a4bd90234ea7e3447f19e18c68beae2763
http://b/3184897
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index f8809d7..9017ca3 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -17,11 +17,7 @@
 package com.android.providers.settings;
 
 import java.io.FileNotFoundException;
-import java.io.UnsupportedEncodingException;
-import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
-import java.util.LinkedHashMap;
-import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -47,6 +43,7 @@
 import android.provider.Settings;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.LruCache;
 
 public class SettingsProvider extends ContentProvider {
     private static final String TAG = "SettingsProvider";
@@ -293,7 +290,7 @@
             "" + (MAX_CACHE_ENTRIES + 1) /* limit */);
         try {
             synchronized (cache) {
-                cache.clear();
+                cache.evictAll();
                 cache.setFullyMatchesDisk(true);  // optimistic
                 int rows = 0;
                 while (c.moveToNext()) {
@@ -359,8 +356,8 @@
     // possibly with a null value, or null on failure.
     private Bundle lookupValue(String table, SettingsCache cache, String key) {
         synchronized (cache) {
-            if (cache.containsKey(key)) {
-                Bundle value = cache.get(key);
+            Bundle value = cache.get(key);
+            if (value != null) {
                 if (value != TOO_LARGE_TO_CACHE_MARKER) {
                     return value;
                 }
@@ -725,13 +722,13 @@
      * associated helper functions to keep cache coherent with the
      * database.
      */
-    private static final class SettingsCache extends LinkedHashMap<String, Bundle> {
+    private static final class SettingsCache extends LruCache<String, Bundle> {
 
         private final String mCacheName;
         private boolean mCacheFullyMatchesDisk = false;  // has the whole database slurped.
 
         public SettingsCache(String name) {
-            super(MAX_CACHE_ENTRIES, 0.75f /* load factor */, true /* access ordered */);
+            super(MAX_CACHE_ENTRIES);
             mCacheName = name;
         }
 
@@ -751,14 +748,8 @@
         }
 
         @Override
-        protected boolean removeEldestEntry(Map.Entry eldest) {
-            if (size() <= MAX_CACHE_ENTRIES) {
-                return false;
-            }
-            synchronized (this) {
-                mCacheFullyMatchesDisk = false;
-            }
-            return true;
+        protected synchronized void entryEvicted(String key, Bundle value) {
+            mCacheFullyMatchesDisk = false;
         }
 
         /**
@@ -772,7 +763,7 @@
             Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
             if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
                 synchronized (this) {
-                    if (!containsKey(key)) {
+                    if (get(key) == null) {
                         put(key, bundle);
                     }
                 }
@@ -826,7 +817,7 @@
                 return;
             }
             synchronized (cache) {
-                cache.clear();
+                cache.evictAll();
                 cache.mCacheFullyMatchesDisk = false;
             }
         }