b7ccc354b017639a547c63f757381648c0c539fa
[platform/core/api/webapi-plugins.git] / src / humanactivitymonitor / humanactivitymonitor_api.js
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 var utils_ = xwalk.utils;
18 var type_ = utils_.type;
19 var converter_ = utils_.converter;
20 var validator_ = utils_.validator;
21 var types_ = validator_.Types;
22 var native_ = new xwalk.utils.NativeManager(extension);
23
24 var callbackId = 0;
25 var callbacks = {};
26
27 function nextCallbackId() {
28   return callbackId++;
29 }
30
31 function SetReadOnlyProperty(obj, n, v) {
32   Object.defineProperty(obj, n, {value: v, writable: false});
33 }
34
35 var HumanActivityType = {
36   PEDOMETER: 'PEDOMETER',
37   WRIST_UP: 'WRIST_UP',
38   HRM: 'HRM',
39   GPS: 'GPS'
40 };
41
42 var PedometerStepStatus = {
43   NOT_MOVING: 'NOT_MOVING',
44   WALKING: 'WALKING',
45   RUNNING: 'RUNNING'
46 };
47
48 function convertActivityData(type, data) {
49   switch (type) {
50     case HumanActivityType.PEDOMETER:
51       // TODO(r.galka) Not Supported in current implementation
52       return undefined;
53     case HumanActivityType.WRIST_UP:
54       return null;
55     case HumanActivityType.HRM:
56       return new HumanActivityHRMData(data);
57     case HumanActivityType.GPS:
58       var gpsInfo = [];
59       for (var i = 0, max = data.length; i < max; i++) {
60         gpsInfo.push(new HumanActivityGPSInfo(data[i]));
61       }
62       return new HumanActivityGPSInfoArray(gpsInfo);
63   }
64 }
65
66 function HumanActivityMonitorManager() {
67 }
68
69 HumanActivityMonitorManager.prototype.getHumanActivityData = function(type, successCallback, errorCallback) {
70   var args = validator_.validateArgs(arguments, [
71     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
72     {name: 'successCallback', type: types_.FUNCTION},
73     {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
74   ]);
75
76   if (args.type === HumanActivityType.WRIST_UP) {
77     throw new WebAPIException(WebAPIException.NOT_SUPPORTED_ERR);
78   }
79
80   var listenerId ='HumanActivityMonitor_'  + args.type;
81   if (!native_.isListenerSet(listenerId)) {
82     throw new WebAPIException(WebAPIException.SERVICE_NOT_AVAILABLE_ERR);
83   }
84
85   var data = {
86     type: args.type
87   };
88
89   var callback = function(result) {
90     if (native_.isFailure(result)) {
91       native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
92       return;
93     }
94
95     native_.callIfPossible(args.successCallback,
96         convertActivityData(args.type, native_.getResultObject(result)));
97   };
98
99   native_.call('HumanActivityMonitorManager_getHumanActivityData', data, callback);
100 };
101
102 HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
103   // TODO(r.galka) check access
104   // HRM - http://tizen.org/privilege/healthinfo
105   // GPS - http://tizen.org/privilege/location
106
107   var args = validator_.validateArgs(arguments, [
108     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
109     {name: 'changedCallback', type: types_.FUNCTION, optional: true, nullable: true}
110   ]);
111
112   var listenerId ='HumanActivityMonitor_'  + args.type;
113
114   var data = {
115     type: args.type,
116     listenerId: listenerId
117   };
118
119   if (!native_.isListenerSet(listenerId)) {
120     var result = native_.callSync('HumanActivityMonitorManager_start', data);
121     if (native_.isFailure(result)) {
122       throw native_.getErrorObject(result);
123     }
124   }
125
126   var listener = function(result) {
127     native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result));
128   };
129   native_.addListener(listenerId, listener);
130 };
131
132 HumanActivityMonitorManager.prototype.stop = function(type) {
133   var args = validator_.validateArgs(arguments, [
134     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
135   ]);
136
137   var data = {
138     type: args.type
139   };
140
141   var listenerId ='HumanActivityMonitor_'  + args.type;
142
143   if (!native_.isListenerSet(listenerId)) {
144     return;
145   }
146
147   var result = native_.callSync('HumanActivityMonitorManager_stop', data);
148   if (native_.isFailure(result)) {
149     throw native_.getErrorObject(result);
150   }
151
152   native_.removeListener(listenerId);
153 };
154
155 HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function(changeCallback) {
156   var args = validator_.validateArgs(arguments, [
157     {name: 'changeCallback', type: types_.FUNCTION}
158   ]);
159
160   var data = {
161   };
162
163   var callback = function(result) {
164     native_.callIfPossible(args.changeCallback);
165   };
166
167   native_.call('HumanActivityMonitorManager_setAccumulativePedometerListener', data, callback);
168 };
169
170 HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
171
172   var data = {
173   };
174
175   var result = native_.callSync(
176       'HumanActivityMonitorManager_unsetAccumulativePedometerListener', data);
177
178   if (native_.isFailure(result)) {
179     throw native_.getErrorObject(result);
180   }
181 };
182
183 function StepDifference() {
184   SetReadOnlyProperty(this, 'stepCountDifference', null);
185   SetReadOnlyProperty(this, 'timestamp', null);
186 }
187
188
189 function HumanActivityData() {
190 }
191
192
193 function HumanActivityPedometerData() {
194   SetReadOnlyProperty(this, 'stepStatus', null);
195   SetReadOnlyProperty(this, 'speed', null);
196   SetReadOnlyProperty(this, 'walkingFrequency', null);
197   SetReadOnlyProperty(this, 'cumulativeDistance', null);
198   SetReadOnlyProperty(this, 'cumulativeCalorie', null);
199   SetReadOnlyProperty(this, 'cumulativeTotalStepCount', null);
200   SetReadOnlyProperty(this, 'cumulativeWalkStepCount', null);
201   SetReadOnlyProperty(this, 'cumulativeRunStepCount', null);
202   SetReadOnlyProperty(this, 'stepCountDifferences', null);
203 }
204
205 HumanActivityPedometerData.prototype = new HumanActivityData();
206 HumanActivityPedometerData.prototype.constructor = HumanActivityPedometerData;
207
208
209 function HumanActivityAccumulativePedometerData() {
210   SetReadOnlyProperty(this, 'stepStatus', null);
211   SetReadOnlyProperty(this, 'speed', null);
212   SetReadOnlyProperty(this, 'walkingFrequency', null);
213   SetReadOnlyProperty(this, 'accumulativeDistance', null);
214   SetReadOnlyProperty(this, 'accumulativeCalorie', null);
215   SetReadOnlyProperty(this, 'accumulativeTotalStepCount', null);
216   SetReadOnlyProperty(this, 'accumulativeWalkStepCount', null);
217   SetReadOnlyProperty(this, 'accumulativeRunStepCount', null);
218   SetReadOnlyProperty(this, 'stepCountDifferences', null);
219 }
220
221 HumanActivityAccumulativePedometerData.prototype = new HumanActivityData();
222 HumanActivityAccumulativePedometerData.prototype.constructor = HumanActivityAccumulativePedometerData;
223
224
225 function HumanActivityHRMData(data) {
226   SetReadOnlyProperty(this, 'heartRate', data.heartRate);
227   SetReadOnlyProperty(this, 'rRInterval', data.rRInterval);
228 }
229
230 HumanActivityHRMData.prototype = new HumanActivityData();
231 HumanActivityHRMData.prototype.constructor = HumanActivityHRMData;
232
233
234 function HumanActivityGPSInfo(data) {
235   SetReadOnlyProperty(this, 'latitude', data.latitude);
236   SetReadOnlyProperty(this, 'longitude', data.longitude);
237   SetReadOnlyProperty(this, 'altitude', data.altitude);
238   SetReadOnlyProperty(this, 'speed', data.speed);
239   SetReadOnlyProperty(this, 'errorRange', data.errorRange);
240   SetReadOnlyProperty(this, 'timestamp', data.timestamp);
241 }
242
243
244 function HumanActivityGPSInfoArray(data) {
245   SetReadOnlyProperty(this, 'gpsInfo', data);
246 }
247
248 HumanActivityGPSInfoArray.prototype = new HumanActivityData();
249 HumanActivityGPSInfoArray.prototype.constructor = HumanActivityGPSInfoArray;
250
251
252 exports = new HumanActivityMonitorManager();