Bug 1014517 - Execute the single variant customization the first time a configured SIM is inserted. r=ferjm, a=2.0+
authorCarmen Jimenez <cjc@tid.es>
Tue, 17 Jun 2014 05:00:00 -0400
changeset 207140 d51a335b7c2df6f3b901a423c72a69d50a8857f3
parent 207139 40567789c30a12ebefea08057e171265ba52fa13
child 207141 12161a842a8e1972cdb27ed36928b8f4a8999290
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersferjm, 2
bugs1014517
milestone32.0a2
Bug 1014517 - Execute the single variant customization the first time a configured SIM is inserted. r=ferjm, a=2.0+
dom/apps/src/OperatorApps.jsm
--- a/dom/apps/src/OperatorApps.jsm
+++ b/dom/apps/src/OperatorApps.jsm
@@ -117,16 +117,21 @@ this.OperatorAppsRegistry = {
           if (iccInfo && iccInfo.mcc) {
             mcc = iccInfo.mcc;
           }
           if (iccInfo && iccInfo.mnc) {
             mnc = iccInfo.mnc;
           }
           if (mcc && mnc) {
             this._installOperatorApps(mcc, mnc);
+            let messenger = Cc["@mozilla.org/system-message-internal;1"]
+                              .getService(Ci.nsISystemMessagesInternal);
+            messenger.broadcastMessage("first-run-with-sim", { mcc: mcc,
+                                                               mnc: mnc });
+
           } else {
             iccProvider.registerIccMsg(clientId, iccListener);
           }
         } catch (e) {
           debug("Error Initializing OperatorApps. " + e);
         }
       }.bind(this));
     } else {
@@ -230,17 +235,17 @@ this.OperatorAppsRegistry = {
     }
   },
 
   get appsDir() {
     return this._baseDirectory;
   },
 
   eraseVariantAppsNotInList: function(aIdsApp) {
-    if (!aIdsApp || !Array.isArray(aIdsApp)) {
+    if (!aIdsApp) {
       aIdsApp = [ ];
     }
 
     let svDir;
     try {
       svDir = this.appsDir.clone();
     } catch (e) {
       debug("eraseVariantAppsNotInList --> Error getting Dir "+
@@ -301,24 +306,60 @@ this.OperatorAppsRegistry = {
     } else {
       debug("aId:" + aId + ". Installing as hosted app.");
       appData.app.manifest = aManifest;
       DOMApplicationRegistry.confirmInstall(appData);
     }
   },
 
   _installOperatorApps: function(aMcc, aMnc) {
+    function normalizeCode(aCode) {
+      let ncode = "" + aCode;
+      while (ncode.length < 3) {
+        ncode = "0" + ncode;
+      }
+      return ncode;
+    }
+
     Task.spawn(function() {
       debug("Install operator apps ---> mcc:"+ aMcc + ", mnc:" + aMnc);
       if (!isFirstRunWithSIM()) {
         debug("Operator apps already installed.");
         return;
       }
 
-      let aIdsApp = yield this._getSingleVariantApps(aMcc, aMnc);
+      let key = normalizeCode(aMcc) + "-" + normalizeCode(aMnc);
+      let aIdsApp = yield this._getSingleVariantDatas();
+
+      // aIdsApp will be undefined if the singleVariant config file not exist
+      // or will have the following format:
+      // {"mmc1-mnc1": [ap11,...,ap1N],..., "mmcM-mncM": [apM1,...,apMN]}
+      // Behavior:
+      // * If the configuration file does not exist, it's equivalent to
+      //   passing []
+      // * If the configuration file has data and the phone boots with a SIM
+      //   that isn't on the configuration file then we must have the same
+      //   behavior as if the phone had booted without a SIM inserted
+      //   (that is, don't do anything)
+      // * If the phone boots with a configured SIM (mcc-mnc exists on
+      //   configuration file) then recover the app list to install
+      if (!aIdsApp) {
+        debug("No " + SINGLE_VARIANT_CONF_FILE + " in " + this.appsDir.path);
+        aIdsApp = [];
+      } else if (aIdsApp[key] === undefined) {
+        debug("First Run with SIM not configured");
+        return;
+      } else {
+        debug("First Run with configured SIM ");
+        aIdsApp = aIdsApp[key];
+        if (!Array.isArray(aIdsApp)) {
+          aIdsApp = [aIdsApp];
+        }
+      }
+
       debug("installOperatorApps --> aIdsApp:" + JSON.stringify(aIdsApp));
       for (let i = 0; i < aIdsApp.length; i++) {
         let aId = aIdsApp[i];
         let aMetadata = yield AppsUtils.loadJSONAsync(
                            Path.join(this.appsDir.path, aId, METADATA));
         if (!aMetadata) {
           debug("Error reading metadata file");
           return;
@@ -348,33 +389,18 @@ this.OperatorAppsRegistry = {
       this.eraseVariantAppsNotInList(aIdsApp);
       Services.prefs.setBoolPref(PREF_FIRST_RUN_WITH_SIM, false);
       Services.prefs.savePrefFile(null);
     }.bind(this)).then(null, function(aError) {
         debug("Error: " + aError);
     });
   },
 
-  _getSingleVariantApps: function(aMcc, aMnc) {
-
-    function normalizeCode(aCode) {
-      let ncode = "" + aCode;
-      while (ncode.length < 3) {
-        ncode = "0" + ncode;
-      }
-      return ncode;
-    }
-
+  _getSingleVariantDatas: function() {
     return Task.spawn(function*() {
-      let key = normalizeCode(aMcc) + "-" + normalizeCode(aMnc);
       let file = Path.join(this.appsDir.path, SINGLE_VARIANT_CONF_FILE);
       let aData = yield AppsUtils.loadJSONAsync(file);
-
-      if (!aData || !(key in aData)) {
-        return [];
-      }
-
-      return aData[key];
+      return aData;
     }.bind(this));
   }
 };
 
 OperatorAppsRegistry.init();