diff options
Diffstat (limited to 'src/com/android/providers/contacts/FastScrollingIndexCache.java')
-rw-r--r-- | src/com/android/providers/contacts/FastScrollingIndexCache.java | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/src/com/android/providers/contacts/FastScrollingIndexCache.java b/src/com/android/providers/contacts/FastScrollingIndexCache.java index f07a855..9b3908e 100644 --- a/src/com/android/providers/contacts/FastScrollingIndexCache.java +++ b/src/com/android/providers/contacts/FastScrollingIndexCache.java @@ -85,19 +85,29 @@ public class FastScrollingIndexCache { */ private final Map<String, String> mCache = Maps.newHashMap(); - public FastScrollingIndexCache(Context context) { - this(PreferenceManager.getDefaultSharedPreferences(context)); + private static FastScrollingIndexCache sSingleton; - // At this point, the SharedPreferences might just have been generated and may still be - // loading from the file, in which case loading from the preferences would be blocked. - // To avoid that, we load lazily. + public static FastScrollingIndexCache getInstance(Context context) { + return getInstance(PreferenceManager.getDefaultSharedPreferences(context)); } - @VisibleForTesting - FastScrollingIndexCache(SharedPreferences prefs) { + public static synchronized FastScrollingIndexCache getInstance( + SharedPreferences prefs) { + if (sSingleton == null) { + sSingleton = new FastScrollingIndexCache(prefs); + } + return sSingleton; + } + + private FastScrollingIndexCache(SharedPreferences prefs) { mPrefs = prefs; } + @VisibleForTesting + protected static synchronized void releaseInstance() { + sSingleton = null; + } + /** * Append a {@link String} to a {@link StringBuilder}. * @@ -238,7 +248,7 @@ public class FastScrollingIndexCache { public void invalidate() { synchronized (mCache) { - mPrefs.edit().remove(PREFERENCE_KEY).apply(); + mPrefs.edit().remove(PREFERENCE_KEY).commit(); mCache.clear(); mPreferenceLoaded = true; |