[HAM] - fixing pedometer listeners with correct privileges check 75/108375/1
authorPiotr Kosko <p.kosko@samsung.com>
Tue, 3 Jan 2017 11:54:30 +0000 (12:54 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Wed, 4 Jan 2017 06:59:21 +0000 (07:59 +0100)
This reverts commit f60b0dd58d49a3cb85df7ca63aeda384abf0bcd2 and adds some special code
  to correctly handle privileges check.

[Verification] TCT for humanactivitymonitor passrate didn't change.
  All TCT privilege HAM-related testcases pass.

Change-Id: Ica915e1f198765ccd43d41df316804bfbcb201d2

src/humanactivitymonitor/humanactivitymonitor_api.js

index d1393cdc18ca956bc0de03467e47cd7c0033f52b..736b9a49af70a5e9dd0438836a8fec1884501c07 100755 (executable)
@@ -237,18 +237,20 @@ function checkPrivilegesForMethod(method, type) {
   }
 }
 
-function stopListener(listenerId, method, data) {
+function stopListener(listenerId, method, data, doRemoval) {
   if (!native_.isListenerSet(listenerId)) {
     checkPrivilegesForMethod(method, data.type);
     return;
   }
 
-  var result = native_.callSync(method, data);
-  if (native_.isFailure(result)) {
-    throw native_.getErrorObject(result);
-  }
+  if (doRemoval) {
+    var result = native_.callSync(method, data);
+    if (native_.isFailure(result)) {
+      throw native_.getErrorObject(result);
+    }
 
-  native_.removeListener(listenerId);
+    native_.removeListener(listenerId);
+  }
 }
 
 // Pedometer listener and accumulative pedometer listener are handled by a single
@@ -351,12 +353,16 @@ HumanActivityMonitorManager.prototype.stop = function(type) {
     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
   ]);
 
-  stopListener('HumanActivityMonitor_'  + args.type,
-               'HumanActivityMonitorManager_stop',
-               { type: args.type });
-
   if (HumanActivityType.PEDOMETER === args.type) {
+    stopListener('HumanActivityMonitor_PEDOMETER',
+                 'HumanActivityMonitorManager_stop',
+                 { type: HumanActivityType.PEDOMETER },
+                 pedometerListener && !accumulativePedometerListener);
     pedometerListener = null;
+  } else {
+    stopListener('HumanActivityMonitor_'  + args.type,
+                 'HumanActivityMonitorManager_stop',
+                 { type: args.type }, true);
   }
 
   if (HumanActivityType.GPS === args.type) {
@@ -379,13 +385,11 @@ HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = functio
 };
 
 HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
-  var oldPedometerListener = pedometerListener;
-
-  // calling stop() will overwrite pedometerListener, needs to be restored afterwards
-  this.stop(HumanActivityType.PEDOMETER);
-
+  stopListener('HumanActivityMonitor_PEDOMETER',
+               'HumanActivityMonitorManager_stop',
+               { type: HumanActivityType.PEDOMETER },
+               accumulativePedometerListener && !pedometerListener);
   accumulativePedometerListener = null;
-  pedometerListener = oldPedometerListener;
 };