Bug 1362994 - Add support for HashMap prefs in SharedPreferences r=sebastian

MozReview-Commit-ID: Dv2lHdZj5Zn

--HG--
extra : rebase_source : 084b97899472d88d7aafed04131c4306f82f8829
This commit is contained in:
Tushar Saini (:shatur) 2017-05-29 16:28:48 +05:30
Родитель 256e9a6424
Коммит c5d48d3522
3 изменённых файлов: 18 добавлений и 3 удалений

Просмотреть файл

@ -17,6 +17,8 @@ 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.
@ -123,7 +125,7 @@ public final class SharedPreferencesHelper
* 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) {
@ -141,6 +143,9 @@ public final class SharedPreferencesHelper
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 + "]");
}
@ -155,7 +160,7 @@ public final class SharedPreferencesHelper
* 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);
@ -176,6 +181,8 @@ public final class SharedPreferencesHelper
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 + "]");
}

Просмотреть файл

@ -54,7 +54,7 @@ class MultiPrefMultiChoicePreference extends MultiChoicePreference {
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;

Просмотреть файл

@ -103,6 +103,10 @@ SharedPreferencesImpl.prototype = Object.freeze({
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");
},
@ -146,6 +150,10 @@ SharedPreferencesImpl.prototype = Object.freeze({
return this._getOne(prefName, "string");
},
getSetPref: function getSetPref(prefName) {
return this._getOne(prefName, "set");
},
getIntPref: function getIntPref(prefName) {
return this._getOne(prefName, "int");
},