Bug 1295675 - Allow app level preferences. r?sebastian,mkaply draft
authorNevin Chen <cnevinchen@gmail.com>
Thu, 23 Mar 2017 10:57:20 +0800
changeset 552819 9be8d974d05a42fb80c0f6fe49d5ade8c03f051b
parent 502579 8744e9f8eb99f1290aae81985812d57364f18708
child 621921 9e42bf4c00cd0680d04475bae0980013f0a9de4f
push id51476
push userbmo:cnevinchen@gmail.com
push dateWed, 29 Mar 2017 02:55:48 +0000
reviewerssebastian, mkaply
bugs1295675
milestone55.0a1
Bug 1295675 - Allow app level preferences. r?sebastian,mkaply MozReview-Commit-ID: yv97PSkL5n
mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java
--- a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
+++ b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
@@ -59,16 +59,20 @@ import android.util.Log;
 /**
  * Handles distribution file loading and fetching,
  * and the corresponding hand-offs to Gecko.
  */
 @RobocopTarget
 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;
 
     private static final String FETCH_PROTOCOL = "https";
     private static final String FETCH_HOSTNAME = "mobile.cdn.mozilla.net";
     private static final String FETCH_PATH = "/distributions/1/";
     private static final String FETCH_EXTENSION = ".jar";
@@ -405,34 +409,35 @@ public class Distribution {
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error parsing preferences.json", e);
             Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION);
             return null;
         }
     }
 
     /**
-     * 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.
             return new JSONObject();
         }
 
         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);
             Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION);
             return new JSONObject();
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error parsing preferences.json", e);
             Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION);
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/DistroSharedPrefsImport.java
@@ -19,25 +19,30 @@ import java.util.Iterator;
 public class DistroSharedPrefsImport {
 
     public static final String LOGTAG = DistroSharedPrefsImport.class.getSimpleName();
 
     public static void importPreferences(final Context context, final Distribution distribution) {
         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 JSONObject profilePref = distribution.getPreferences(Distribution.PREF_KEY_PROFILE_PREFERENCES);
+        if (profilePref.length() != 0) {
+            applyPreferences(profilePref, GeckoSharedPrefs.forProfile(context).edit());
+        }
+    }
+
+    private static void applyPreferences(JSONObject preferences, SharedPreferences.Editor sharedPreferences) {
         final Iterator<?> keys = preferences.keys();
-        final SharedPreferences.Editor sharedPreferences = GeckoSharedPrefs.forProfile(context).edit();
-
         while (keys.hasNext()) {
             final String key = (String) keys.next();
             final Object value;
             try {
                 value = preferences.get(key);
             } catch (JSONException e) {
                 Log.e(LOGTAG, "Unable to completely process Android Preferences JSON.", e);
                 continue;