Keep strong reference to OnSharedPreferenceChangeListener (#131)

Summary:
A strong reference to the listener should be kept when calling `registerOnSharedPreferenceChangeListener`, otherwise the listener can get garbage-collected and stop working.

Mentioned in the Android SDK docs [here](https://developer.android.com/reference/android/content/SharedPreferences.html#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)):

> Caution: The preference manager does not currently store a strong reference to the listener. You must store a strong reference to the listener, or it will be susceptible to garbage collection. We recommend you keep a reference to the listener in the instance data of an object that will exist as long as you need the listener.
Closes https://github.com/facebook/Sonar/pull/131

Differential Revision: D8751484

Pulled By: xiphirx

fbshipit-source-id: e46715d637c89491770c266fc22c27db41beb042
This commit is contained in:
Conor O'Donnell
2018-07-06 13:02:00 -07:00
committed by Facebook Github Bot
parent 3af57b1ad9
commit d45bc14cce

View File

@@ -23,6 +23,24 @@ public class SharedPreferencesSonarPlugin implements SonarPlugin {
private SonarConnection mConnection; private SonarConnection mConnection;
private final SharedPreferences mSharedPreferences; private final SharedPreferences mSharedPreferences;
private final SharedPreferences.OnSharedPreferenceChangeListener
onSharedPreferenceChangeListener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (mConnection == null) {
return;
}
mConnection.send(
"sharedPreferencesChange",
new SonarObject.Builder()
.put("name", key)
.put("deleted", !mSharedPreferences.contains(key))
.put("time", System.currentTimeMillis())
.put("value", mSharedPreferences.getAll().get(key))
.build());
}
};
/** /**
* Creates a {@link android.content.SharedPreferences} plugin for Sonar * Creates a {@link android.content.SharedPreferences} plugin for Sonar
@@ -54,23 +72,7 @@ public class SharedPreferencesSonarPlugin implements SonarPlugin {
*/ */
public SharedPreferencesSonarPlugin(Context context, String name, int mode) { public SharedPreferencesSonarPlugin(Context context, String name, int mode) {
mSharedPreferences = context.getSharedPreferences(name, mode); mSharedPreferences = context.getSharedPreferences(name, mode);
mSharedPreferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
mSharedPreferences.registerOnSharedPreferenceChangeListener(
new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (mConnection != null) {
mConnection.send(
"sharedPreferencesChange",
new SonarObject.Builder()
.put("name", key)
.put("deleted", !mSharedPreferences.contains(key))
.put("time", System.currentTimeMillis())
.put("value", mSharedPreferences.getAll().get(key))
.build());
}
}
});
} }
@Override @Override