diff options
author | Jeff Sharkey <jsharkey@android.com> | 2013-06-17 18:26:37 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2013-06-17 18:26:37 -0700 |
commit | 8e3ddab1fcc246442484c1b443810a1ba6f35296 (patch) | |
tree | b1172e298c9778ff337961dac1eddd1cfeabe98e | |
parent | a2934d5ff0b5c4098408cd583c2c259edf37c118 (diff) | |
download | frameworks_base-8e3ddab1fcc246442484c1b443810a1ba6f35296.zip frameworks_base-8e3ddab1fcc246442484c1b443810a1ba6f35296.tar.gz frameworks_base-8e3ddab1fcc246442484c1b443810a1ba6f35296.tar.bz2 |
Use package name when caching SharedPreferences.
Otherwise multiple applications using the same process can end up
leaking SharedPreferences instances between the apps.
Change-Id: I2eb0e693cebea1daf87a9176eb8c5a22b9e9ec0b
-rw-r--r-- | core/java/android/app/ContextImpl.java | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 7968431..c6f3fb8 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -94,6 +94,7 @@ import android.os.storage.StorageManager; import android.telephony.TelephonyManager; import android.content.ClipboardManager; import android.util.AndroidRuntimeException; +import android.util.ArrayMap; import android.util.Log; import android.util.Slog; import android.view.CompatibilityInfoHolder; @@ -170,8 +171,10 @@ class ContextImpl extends Context { private final static String TAG = "ContextImpl"; private final static boolean DEBUG = false; - private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs = - new HashMap<String, SharedPreferencesImpl>(); + /** + * Map from package name, to preference name, to cached preferences. + */ + private static ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>> sSharedPrefs; /*package*/ LoadedApk mPackageInfo; private String mBasePackageName; @@ -677,12 +680,23 @@ class ContextImpl extends Context { @Override public SharedPreferences getSharedPreferences(String name, int mode) { SharedPreferencesImpl sp; - synchronized (sSharedPrefs) { - sp = sSharedPrefs.get(name); + synchronized (mSync) { + if (sSharedPrefs == null) { + sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>(); + } + + final String packageName = getPackageName(); + ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName); + if (packagePrefs == null) { + packagePrefs = new ArrayMap<String, SharedPreferencesImpl>(); + sSharedPrefs.put(packageName, packagePrefs); + } + + sp = packagePrefs.get(name); if (sp == null) { File prefsFile = getSharedPrefsFile(name); sp = new SharedPreferencesImpl(prefsFile, mode); - sSharedPrefs.put(name, sp); + packagePrefs.put(name, sp); return sp; } } |