Bug 1117454 - Mobile usage is wrong when switching sim card. r=ethan
authorAlbert Crespell <acperez@tid.es>
Tue, 10 Feb 2015 14:57:58 +0100
changeset 228760 302d99f46ede0eb54b4c58eb83fb1f673ebc712a
parent 228759 c7e21a1fe73510792c22b0ec7b858fb1b68aa81d
child 228761 cc0142c8a550781159e5b2b7a385cce227ec18d6
push idunknown
push userunknown
push dateunknown
reviewersethan
bugs1117454
milestone38.0a1
Bug 1117454 - Mobile usage is wrong when switching sim card. r=ethan
dom/network/NetworkStatsDB.jsm
--- a/dom/network/NetworkStatsDB.jsm
+++ b/dom/network/NetworkStatsDB.jsm
@@ -542,26 +542,48 @@ NetworkStatsDB.prototype = {
       let range = IDBKeyRange.bound(lowerFilter, upperFilter, false, false);
 
       let request = aStore.openCursor(range, 'prev');
       request.onsuccess = function onsuccess(event) {
         let cursor = event.target.result;
         if (!cursor) {
           // Empty, so save first element.
 
+          if (!isAccumulative) {
+            this._saveStats(aTxn, aStore, stats);
+            return;
+          }
+
           // There could be a time delay between the point when the network
           // interface comes up and the point when the database is initialized.
           // In this short interval some traffic data are generated but are not
           // registered by the first sample.
-          if (isAccumulative) {
-            stats.rxBytes = stats.rxTotalBytes;
-            stats.txBytes = stats.txTotalBytes;
-          }
+          stats.rxBytes = stats.rxTotalBytes;
+          stats.txBytes = stats.txTotalBytes;
 
-          this._saveStats(aTxn, aStore, stats);
+          // However, if the interface is not switched on after the database is
+          // initialized (dual sim use case) stats should be set to 0.
+          let req = aStore.index("network").openKeyCursor(null, "nextunique");
+          req.onsuccess = function onsuccess(event) {
+            let cursor = event.target.result;
+            if (cursor) {
+              if (cursor.key[1] == stats.network[1]) {
+                stats.rxBytes = 0;
+                stats.txBytes = 0;
+                this._saveStats(aTxn, aStore, stats);
+                return;
+              }
+
+              cursor.continue();
+              return;
+            }
+
+            this._saveStats(aTxn, aStore, stats);
+          }.bind(this);
+
           return;
         }
 
         // There are old samples
         if (DEBUG) {
           debug("Last value " + JSON.stringify(cursor.value));
         }