From ceb16db812c0fb3b8810067bef8baa3ecf70e078 Mon Sep 17 00:00:00 2001 From: Hilal Alsibai Date: Mon, 2 Jul 2018 11:01:27 -0700 Subject: [PATCH] Make the shared preferences plugin more flexible Summary: You don't necessarily need / have your shared preferences defined in a file that is your package name. This adds the ability to pass in the name + mode you need to read the correct shared preferences file. Reviewed By: sjkirby Differential Revision: D8661573 fbshipit-source-id: 49e57b0371228eca7fc4f06e8ba65ff8cc059b11 --- .../SharedPreferencesSonarPlugin.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/android/plugins/sharedpreferences/SharedPreferencesSonarPlugin.java b/android/plugins/sharedpreferences/SharedPreferencesSonarPlugin.java index fef931a48..0e987c7e1 100644 --- a/android/plugins/sharedpreferences/SharedPreferencesSonarPlugin.java +++ b/android/plugins/sharedpreferences/SharedPreferencesSonarPlugin.java @@ -24,8 +24,36 @@ public class SharedPreferencesSonarPlugin implements SonarPlugin { private SonarConnection mConnection; private final SharedPreferences mSharedPreferences; + /** + * Creates a {@link android.content.SharedPreferences} plugin for Sonar + * + * @param context The context to retrieve the file from. Will use the package name as the file + * name with {@link Context#MODE_PRIVATE}. + */ public SharedPreferencesSonarPlugin(Context context) { - mSharedPreferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE); + this(context, context.getPackageName()); + } + + /** + * Creates a {@link android.content.SharedPreferences} plugin for Sonar + * + * @param context The context to retrieve the file from. Will use the name as the file name with + * {@link Context#MODE_PRIVATE}. + * @param name The preference file name. + */ + public SharedPreferencesSonarPlugin(Context context, String name) { + this(context, name, MODE_PRIVATE); + } + + /** + * Creates a {@link android.content.SharedPreferences} plugin for Sonar + * + * @param context The context to retrieve the file from. + * @param name The preference file name. + * @param mode The Context mode to utilize. + */ + public SharedPreferencesSonarPlugin(Context context, String name, int mode) { + mSharedPreferences = context.getSharedPreferences(name, mode); mSharedPreferences.registerOnSharedPreferenceChangeListener( new SharedPreferences.OnSharedPreferenceChangeListener() {