Bug 1362994 - Add support for HashMap prefs in SharedPreferences
MozReview-Commit-ID: Dv2lHdZj5Zn
--- a/mobile/android/base/java/org/mozilla/gecko/SharedPreferencesHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/SharedPreferencesHelper.java
@@ -12,16 +12,18 @@ import org.mozilla.gecko.util.GeckoBundl
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.Map;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Arrays;
/**
* Helper class to get, set, and observe Android Shared Preferences.
*/
public final class SharedPreferencesHelper
implements BundleEventListener
{
public static final String LOGTAG = "GeckoAndSharedPrefs";
@@ -118,17 +120,17 @@ public final class SharedPreferencesHelp
}
/**
* Set many SharedPreferences in Android.
*
* message.branch must exist, and should be a String SharedPreferences
* branch name, or null for the default branch.
* message.preferences should be an array of preferences. Each preference
- * must include a String name, a String type in ["bool", "int", "string"],
+ * must include a String name, a String type in ["bool", "int", "string", "set"],
* and an Object value.
*/
private void handleSet(final GeckoBundle message) {
SharedPreferences.Editor editor = getSharedPreferences(message).edit();
final GeckoBundle[] bundlePrefs = message.getBundleArray("preferences");
for (int i = 0; i < bundlePrefs.length; i++) {
@@ -136,31 +138,34 @@ public final class SharedPreferencesHelp
final String name = pref.getString("name");
final String type = pref.getString("type");
if ("bool".equals(type)) {
editor.putBoolean(name, pref.getBoolean("value"));
} else if ("int".equals(type)) {
editor.putInt(name, pref.getInt("value"));
} else if ("string".equals(type)) {
editor.putString(name, pref.getString("value"));
+ } else if ("set".equals(type)) {
+ HashSet<String> mySet = new HashSet<String>(Arrays.asList(pref.getStringArray("value")));
+ editor.putStringSet(name, mySet);
} else {
Log.w(LOGTAG, "Unknown pref value type [" + type + "] for pref [" + name + "]");
}
}
editor.apply();
}
/**
* Get many SharedPreferences from Android.
*
* message.branch must exist, and should be a String SharedPreferences
* branch name, or null for the default branch.
* message.preferences should be an array of preferences. Each preference
* must include a String name, and a String type in ["bool", "int",
- * "string"].
+ * "string", "set"].
*/
private GeckoBundle[] handleGet(final GeckoBundle message) {
final SharedPreferences prefs = getSharedPreferences(message);
final GeckoBundle[] bundlePrefs = message.getBundleArray("preferences");
final GeckoBundle[] bundleValues = new GeckoBundle[bundlePrefs.length];
for (int i = 0; i < bundlePrefs.length; i++) {
final GeckoBundle pref = bundlePrefs[i];
@@ -171,16 +176,18 @@ public final class SharedPreferencesHelp
bundleValue.putString("type", type);
try {
if ("bool".equals(type)) {
bundleValue.putBoolean("value", prefs.getBoolean(name, false));
} else if ("int".equals(type)) {
bundleValue.putInt("value", prefs.getInt(name, 0));
} else if ("string".equals(type)) {
bundleValue.putString("value", prefs.getString(name, ""));
+ } else if ("set".equals(type)) {
+ bundleValue.putStringArray("value", prefs.getStringSet(name, new HashSet<String>()));
} else {
Log.w(LOGTAG, "Unknown pref value type [" + type + "] for pref [" + name + "]");
}
} catch (final ClassCastException e) {
// Thrown if there is a preference with the given name that is
// not the right type.
Log.w(LOGTAG, "Wrong pref value type [" + type + "] for pref [" + name + "]", e);
}
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/MultiPrefMultiChoicePreference.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/MultiPrefMultiChoicePreference.java
@@ -49,17 +49,17 @@ class MultiPrefMultiChoicePreference ext
// Overridden to do a one time import for the old preference type to the new one.
@Override
protected synchronized void loadPersistedValues() {
// This will load the new pref if it exists.
super.loadPersistedValues();
// First check if we've already done the import the old data. If so, nothing to load.
- final SharedPreferences prefs = GeckoSharedPrefs.forApp(getContext());
+ final SharedPreferences prefs = GeckoSharedPrefs.forProfile(getContext());
final boolean imported = getPersistedBoolean(prefs, getKey() + IMPORT_SUFFIX, false);
if (imported) {
return;
}
// Load the data we'll need to find the old style prefs
final CharSequence[] init = getInitialValues();
final CharSequence[] entries = getEntries();
--- a/mobile/android/modules/SharedPreferences.jsm
+++ b/mobile/android/modules/SharedPreferences.jsm
@@ -98,16 +98,20 @@ SharedPreferencesImpl.prototype = Object
setBoolPref: function setBoolPref(prefName, value) {
this._setOne(prefName, value, "bool");
},
setCharPref: function setCharPref(prefName, value) {
this._setOne(prefName, value, "string");
},
+ setSetPref: function setCharPref(prefName, value) {
+ this._setOne(prefName, value, "set");
+ },
+
setIntPref: function setIntPref(prefName, value) {
this._setOne(prefName, value, "int");
},
_get: function _get(prefs, callback) {
let result = null;
// Use dispatch instead of sendRequestForResult because callbacks for
@@ -141,16 +145,20 @@ SharedPreferencesImpl.prototype = Object
getBoolPref: function getBoolPref(prefName) {
return this._getOne(prefName, "bool");
},
getCharPref: function getCharPref(prefName) {
return this._getOne(prefName, "string");
},
+ getSetPref: function getSetPref(prefName) {
+ return this._getOne(prefName, "set");
+ },
+
getIntPref: function getIntPref(prefName) {
return this._getOne(prefName, "int");
},
/**
* Invoke `observer` after a change to the preference `domain` in
* the current branch.
*