Bug 1295675 - Allow app level preferences. r=sebastian

MozReview-Commit-ID: yv97PSkL5n

--HG--
extra : rebase_source : 9014aa5843030f67d97ce1d33c3585cc791e54f4
This commit is contained in:
Nevin Chen 2017-03-23 10:57:20 +08:00
parent e7aab5cc93
commit 8c0d2f06fd
2 changed files with 20 additions and 10 deletions

View File

@ -64,6 +64,10 @@ import android.util.Log;
public class Distribution {
private static final String LOGTAG = "GeckoDistribution";
// We use "AndroidPreferences" for profile-scoped pref for backward compatibility(bug 1295675)
public static final String PREF_KEY_PROFILE_PREFERENCES = "AndroidPreferences";
public static final String PREF_KEY_APPLICATION_PREFERENCES = "ApplicationPreferences";
private static final int STATE_UNKNOWN = 0;
private static final int STATE_NONE = 1;
private static final int STATE_SET = 2;
@ -410,10 +414,11 @@ public class Distribution {
}
/**
* Get the Android preferences from the preferences.json file, if any exist.
* Get the preferences from the preferences.json file, if any exist.
* There are two types of preferences : Application-scoped and profile-scoped (bug 1295675)
* @return The preferences in a JSONObject, or an empty JSONObject if no preferences are defined.
*/
public JSONObject getAndroidPreferences() {
public JSONObject getPreferences(String key) {
final File descFile = getDistributionFile("preferences.json");
if (descFile == null) {
// Logging and existence checks are handled in getDistributionFile.
@ -423,11 +428,11 @@ public class Distribution {
try {
final JSONObject all = FileUtils.readJSONObjectFromFile(descFile);
if (!all.has("AndroidPreferences")) {
if (!all.has(key)) {
return new JSONObject();
}
return all.getJSONObject("AndroidPreferences");
return all.getJSONObject(key);
} catch (IOException e) {
Log.e(LOGTAG, "Error getting distribution descriptor file.", e);

View File

@ -24,15 +24,20 @@ public class DistroSharedPrefsImport {
if (distribution == null) {
return;
}
final JSONObject preferences = distribution.getAndroidPreferences();
if (preferences.length() == 0) {
return;
// There are two types of preferences : Application-scoped and profile-scoped (bug 1295675)
final JSONObject appPref = distribution.getPreferences(Distribution.PREF_KEY_APPLICATION_PREFERENCES);
if (appPref.length() != 0) {
applyPreferences(appPref, GeckoSharedPrefs.forApp(context).edit());
}
final Iterator<?> keys = preferences.keys();
final SharedPreferences.Editor sharedPreferences = GeckoSharedPrefs.forProfile(context).edit();
final JSONObject profilePref = distribution.getPreferences(Distribution.PREF_KEY_PROFILE_PREFERENCES);
if (appPref.length() != 0) {
applyPreferences(profilePref, GeckoSharedPrefs.forProfile(context).edit());
}
}
private static void applyPreferences(JSONObject preferences, SharedPreferences.Editor sharedPreferences) {
final Iterator<?> keys = preferences.keys();
while (keys.hasNext()) {
final String key = (String) keys.next();
final Object value;