Merge branch 'tizen_4.0' into tizen_5.0
[platform/core/api/webapi-plugins.git] / src / humanactivitymonitor / humanactivitymonitor_api.js
index 4f635f4..8397d1a 100755 (executable)
@@ -15,6 +15,7 @@
  */
 
 var utils_ = xwalk.utils;
+var privilege_ = utils_.privilege;
 var type_ = utils_.type;
 var converter_ = utils_.converter;
 var validator_ = utils_.validator;
@@ -43,7 +44,9 @@ var HumanActivityType = {
     WRIST_UP: 'WRIST_UP',
     HRM: 'HRM',
     GPS: 'GPS',
-    SLEEP_MONITOR: 'SLEEP_MONITOR'
+    SLEEP_MONITOR: 'SLEEP_MONITOR',
+    SLEEP_DETECTOR: 'SLEEP_DETECTOR',
+    STRESS_MONITOR: 'STRESS_MONITOR'
 };
 
 var HumanActivityRecorderType = {
@@ -108,6 +111,10 @@ function convertActivityData(type, data) {
         return new HumanActivityGPSInfoArray(gpsInfo);
     case HumanActivityType.SLEEP_MONITOR:
         return new HumanActivitySleepMonitorData(data);
+    case HumanActivityType.SLEEP_DETECTOR:
+        return new HumanActivitySleepDetectorData(data);
+    case HumanActivityType.STRESS_MONITOR:
+        return new HumanActivityStressMonitorData(data);
     default:
         utils_.error('Uknown human activity type: ' + type);
     }
@@ -146,6 +153,50 @@ function convertActivityRecorderData(type, data) {
     return createRecorderData(func, data);
 }
 
+function StressMonitorDataRange(label, min, max) {
+    validator_.validateConstructorCall(this, tizen.StressMonitorDataRange);
+
+    var args = validator_.validateArgs(arguments, [
+        { name: 'label', type: types_.STRING, optional: true, nullable: false },
+        { name: 'min', type: types_.UNSIGNED_LONG, optional: true, nullable: false },
+        { name: 'max', type: types_.UNSIGNED_LONG, optional: true, nullable: false }
+    ]);
+
+    var _label = !type_.isNullOrUndefined(args.label) ? args.label : '';
+    var _min = !type_.isNullOrUndefined(args.min) ? args.min : 0;
+    var _max = !type_.isNull(args.max) ? args.max : undefined;
+
+    Object.defineProperties(this, {
+        label: {
+            get: function() {
+                return _label;
+            },
+            set: function(v) {
+                _label = !type_.isNullOrUndefined(v) ? v : _label;
+            },
+            enumerable: true
+        },
+        min: {
+            get: function() {
+                return _min;
+            },
+            set: function(v) {
+                _min = !type_.isNullOrUndefined(v) ? converter_.toUnsignedLong(v) : _min;
+            },
+            enumerable: true
+        },
+        max: {
+            get: function() {
+                return _max;
+            },
+            set: function(v) {
+                _max = !type_.isNullOrUndefined(v) ? converter_.toUnsignedLong(v) : _max;
+            },
+            enumerable: true
+        }
+    });
+}
+
 function ActivityRecognitionListenerManager() {
     this.listeners = {};
     this.nextId = 1;
@@ -254,7 +305,7 @@ function startListener(listenerId, listener, method, data) {
     }
 
     // always set the listener
-    //if it's another call to startListener() overwrite the old one
+    // if it's another call to startListener() overwrite the old one
     native_.addListener(listenerId, listener);
 }
 
@@ -306,6 +357,8 @@ function GPSCallback(result) {
     }
 }
 
+var stressListener = null;
+
 HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
     var args = validator_.validateArgs(arguments, [
         { name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType) },
@@ -364,6 +417,9 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
     case HumanActivityType.GPS:
         listener = GPSCallback;
         break;
+    case HumanActivityType.STRESS_MONITOR:
+        listener = stressMonitorListener.onListener;
+        break;
     default:
         listener = function(result) {
             native_.callIfPossible(
@@ -387,7 +443,10 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
         pedometerListener = args.changedCallback;
     }
 
-    if (HumanActivityType.GPS === args.type) {
+    if (
+        HumanActivityType.GPS === args.type ||
+        HumanActivityType.STRESS_MONITOR === args.type
+    ) {
         var callback = function(result) {
             if (native_.isFailure(result)) {
                 native_.callIfPossible(
@@ -402,7 +461,11 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
             }
         };
 
-        GPSListener = callback;
+        if (HumanActivityType.GPS === args.type) {
+            GPSListener = callback;
+        } else if (HumanActivityType.STRESS_MONITOR === args.type) {
+            stressListener = callback;
+        }
     }
 };
 
@@ -439,6 +502,10 @@ HumanActivityMonitorManager.prototype.stop = function(type) {
     if (HumanActivityType.GPS === args.type) {
         GPSListener = null;
     }
+
+    if (HumanActivityType.STRESS_MONITOR === args.type) {
+        stressListener = null;
+    }
 };
 
 HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function() {
@@ -794,6 +861,91 @@ HumanActivityMonitorManager.prototype.removeGestureRecognitionListener = functio
     gestureRecognitionListener.removeListener(args.watchId);
 };
 
+function StressMonitorListenerManager() {
+    this.listeners = {};
+    this.nextId = 1;
+}
+
+StressMonitorListenerManager.prototype.onListener = function(data) {
+    if (stressListener) {
+        stressListener(data);
+    }
+    var score = data.stressScore;
+    for (var watchId in stressMonitorListener.listeners) {
+        if (stressMonitorListener.listeners.hasOwnProperty(watchId)) {
+            var _listener = stressMonitorListener.listeners[watchId];
+            var rangeArray = _listener.ranges;
+            for (var id in rangeArray) {
+                var _min = rangeArray[id].min;
+                var _max = !type_.isUndefined(rangeArray[id].max)
+                    ? rangeArray[id].max
+                    : Number.MAX_VALUE;
+                if (
+                    score >= _min &&
+                    score < _max &&
+                    (_listener.lastStressScore < _min ||
+                        _listener.lastStressScore >= _max)
+                ) {
+                    _listener.listener(rangeArray[id].label);
+                }
+            }
+            _listener.lastStressScore = score;
+        }
+    }
+};
+
+StressMonitorListenerManager.prototype.addListener = function(
+    ranges,
+    listener,
+    errorCallback
+) {
+    var id = this.nextId++;
+
+    this.listeners[id] = {
+        ranges: ranges,
+        listener: listener,
+        lastStressScore: -1
+    };
+
+    return id;
+};
+
+StressMonitorListenerManager.prototype.removeListener = function(watchId) {
+    if (this.listeners.hasOwnProperty(watchId)) {
+        delete this.listeners[watchId];
+    }
+};
+
+var stressMonitorListener = new StressMonitorListenerManager();
+
+HumanActivityMonitorManager.prototype.addStressMonitorChangeListener = function() {
+    utils_.checkPrivilegeAccess(privilege_.HEALTHINFO);
+    var args = validator_.validateMethod(arguments, [
+        {
+            name: 'ranges',
+            type: types_.ARRAY,
+            values: StressMonitorDataRange
+        },
+        {
+            name: 'listener',
+            type: types_.FUNCTION
+        }
+    ]);
+
+    return stressMonitorListener.addListener(args.ranges, args.listener);
+};
+
+HumanActivityMonitorManager.prototype.removeStressMonitorChangeListener = function() {
+    var args = validator_.validateMethod(arguments, [
+        {
+            name: 'watchId',
+            type: types_.LONG
+        }
+    ]);
+
+    stressMonitorListener.removeListener(args.watchId);
+};
+
 function StepDifference(data) {
     SetReadOnlyProperty(this, 'stepCountDifference', data.stepCountDifference);
     SetReadOnlyProperty(this, 'timestamp', data.timestamp);
@@ -892,6 +1044,20 @@ function HumanActivitySleepMonitorData(data) {
 HumanActivitySleepMonitorData.prototype = new HumanActivityData();
 HumanActivitySleepMonitorData.prototype.constructor = HumanActivitySleepMonitorData;
 
+function HumanActivitySleepDetectorData(data) {
+    SetReadOnlyProperty(this, 'status', data.status);
+}
+
+HumanActivitySleepDetectorData.prototype = new HumanActivityData();
+HumanActivitySleepDetectorData.prototype.constructor = HumanActivitySleepMonitorData;
+
+function HumanActivityStressMonitorData(data) {
+    SetReadOnlyProperty(this, 'stressScore', data.stressScore);
+}
+
+HumanActivityStressMonitorData.prototype = new HumanActivityData();
+HumanActivityStressMonitorData.prototype.constructor = HumanActivityStressMonitorData;
+
 //Recorded data
 function HumanActivityRecorderData(data) {
     if (data) {
@@ -960,4 +1126,6 @@ HumanActivityRecorderPressureData.prototype = new HumanActivityRecorderData();
 HumanActivityRecorderPressureData.prototype.constructor =
     HumanActivityRecorderPressureData;
 
+tizen.StressMonitorDataRange = StressMonitorDataRange;
+
 exports = new HumanActivityMonitorManager();