From 8c0d2f06fd0c8484ecef5ada397e7b54a58d55a1 Mon Sep 17 00:00:00 2001 From: Nevin Chen Date: Thu, 23 Mar 2017 10:57:20 +0800 Subject: [PATCH] Bug 1295675 - Allow app level preferences. r=sebastian MozReview-Commit-ID: yv97PSkL5n --HG-- extra : rebase_source : 9014aa5843030f67d97ce1d33c3585cc791e54f4 --- .../gecko/distribution/Distribution.java | 13 +++++++++---- .../preferences/DistroSharedPrefsImport.java | 17 +++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java index 279766c26f91..99862bb1374a 100644 --- a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java +++ b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java @@ -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); diff --git a/mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java b/mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java index 1e235640e38b..99dec00f7940 100644 --- a/mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java +++ b/mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java @@ -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;