2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
27 function nextCallbackId() {
31 function SetReadOnlyProperty(obj, n, v) {
32 Object.defineProperty(obj, n, {value: v, writable: false});
35 var HumanActivityType = {
36 PEDOMETER: 'PEDOMETER',
42 var PedometerStepStatus = {
43 NOT_MOVING: 'NOT_MOVING',
48 function convertActivityData(type, data) {
50 case HumanActivityType.PEDOMETER:
51 // TODO(r.galka) Not Supported in current implementation
53 case HumanActivityType.WRIST_UP:
55 case HumanActivityType.HRM:
56 return new HumanActivityHRMData(data);
57 case HumanActivityType.GPS:
59 for (var i = 0, max = data.length; i < max; i++) {
60 gpsInfo.push(new HumanActivityGPSInfo(data[i]));
62 return new HumanActivityGPSInfoArray(gpsInfo);
66 function HumanActivityMonitorManager() {
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}
76 if (args.type === HumanActivityType.WRIST_UP) {
77 throw new WebAPIException(WebAPIException.NOT_SUPPORTED_ERR);
80 var listenerId = 'HumanActivityMonitor_' + args.type;
81 if (!native_.isListenerSet(listenerId)) {
82 throw new WebAPIException(WebAPIException.SERVICE_NOT_AVAILABLE_ERR);
89 var callback = function(result) {
90 if (native_.isFailure(result)) {
91 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
95 native_.callIfPossible(args.successCallback,
96 convertActivityData(args.type, native_.getResultObject(result)));
99 var result = native_.call('HumanActivityMonitorManager_getHumanActivityData', data, callback);
101 if (native_.isFailure(result)) {
102 throw native_.getErrorObject(result);
106 function startListener(listenerId, listener, method, data) {
107 if (!native_.isListenerSet(listenerId)) {
108 var result = native_.callSync(method, data);
109 if (native_.isFailure(result)) {
110 throw native_.getErrorObject(result);
114 // always set the listener, if it's another call to startListener() overwrite the old one
115 native_.addListener(listenerId, listener);
118 function checkPrivilegesForMethod(method, type) {
119 utils_.checkPrivilegeAccess(utils_.privilege.HEALTHINFO);
120 if ('HumanActivityMonitorManager_stop' === method && 'GPS' === type) {
121 utils_.checkPrivilegeAccess(utils_.privilege.LOCATION);
125 function stopListener(listenerId, method, data) {
126 if (!native_.isListenerSet(listenerId)) {
127 checkPrivilegesForMethod(method, data.type);
131 var result = native_.callSync(method, data);
132 if (native_.isFailure(result)) {
133 throw native_.getErrorObject(result);
136 native_.removeListener(listenerId);
139 HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
140 var args = validator_.validateArgs(arguments, [
141 {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
142 {name: 'changedCallback', type: types_.FUNCTION, optional: true, nullable: true}
145 var listenerId = 'HumanActivityMonitor_' + args.type;
147 startListener(listenerId,
149 native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result));
151 'HumanActivityMonitorManager_start',
152 { type: args.type, listenerId: listenerId });
155 HumanActivityMonitorManager.prototype.stop = function(type) {
156 var args = validator_.validateArgs(arguments, [
157 {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
160 stopListener('HumanActivityMonitor_' + args.type,
161 'HumanActivityMonitorManager_stop',
162 { type: args.type });
165 var accumulativePedometerListenerId = 'HumanActivityMonitor_AccumulativePedometerListener';
167 HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function(changeCallback) {
168 var args = validator_.validateArgs(arguments, [
169 {name: 'changeCallback', type: types_.FUNCTION}
172 var listenerId = accumulativePedometerListenerId;
174 startListener(listenerId,
176 args.changeCallback(convertActivityData(HumanActivityType.PEDOMETER, result));
178 'HumanActivityMonitorManager_setAccumulativePedometerListener',
179 { listenerId: listenerId });
182 HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
183 stopListener(accumulativePedometerListenerId,
184 'HumanActivityMonitorManager_unsetAccumulativePedometerListener',
188 function StepDifference() {
189 SetReadOnlyProperty(this, 'stepCountDifference', null);
190 SetReadOnlyProperty(this, 'timestamp', null);
194 function HumanActivityData() {
198 function HumanActivityPedometerData() {
199 SetReadOnlyProperty(this, 'stepStatus', null);
200 SetReadOnlyProperty(this, 'speed', null);
201 SetReadOnlyProperty(this, 'walkingFrequency', null);
202 SetReadOnlyProperty(this, 'cumulativeDistance', null);
203 SetReadOnlyProperty(this, 'cumulativeCalorie', null);
204 SetReadOnlyProperty(this, 'cumulativeTotalStepCount', null);
205 SetReadOnlyProperty(this, 'cumulativeWalkStepCount', null);
206 SetReadOnlyProperty(this, 'cumulativeRunStepCount', null);
207 SetReadOnlyProperty(this, 'stepCountDifferences', null);
210 HumanActivityPedometerData.prototype = new HumanActivityData();
211 HumanActivityPedometerData.prototype.constructor = HumanActivityPedometerData;
214 function HumanActivityAccumulativePedometerData() {
215 SetReadOnlyProperty(this, 'stepStatus', null);
216 SetReadOnlyProperty(this, 'speed', null);
217 SetReadOnlyProperty(this, 'walkingFrequency', null);
218 SetReadOnlyProperty(this, 'accumulativeDistance', null);
219 SetReadOnlyProperty(this, 'accumulativeCalorie', null);
220 SetReadOnlyProperty(this, 'accumulativeTotalStepCount', null);
221 SetReadOnlyProperty(this, 'accumulativeWalkStepCount', null);
222 SetReadOnlyProperty(this, 'accumulativeRunStepCount', null);
223 SetReadOnlyProperty(this, 'stepCountDifferences', null);
226 HumanActivityAccumulativePedometerData.prototype = new HumanActivityData();
227 HumanActivityAccumulativePedometerData.prototype.constructor = HumanActivityAccumulativePedometerData;
230 function HumanActivityHRMData(data) {
231 SetReadOnlyProperty(this, 'heartRate', data.heartRate);
232 SetReadOnlyProperty(this, 'rRInterval', data.rRInterval);
235 HumanActivityHRMData.prototype = new HumanActivityData();
236 HumanActivityHRMData.prototype.constructor = HumanActivityHRMData;
239 function HumanActivityGPSInfo(data) {
240 SetReadOnlyProperty(this, 'latitude', data.latitude);
241 SetReadOnlyProperty(this, 'longitude', data.longitude);
242 SetReadOnlyProperty(this, 'altitude', data.altitude);
243 SetReadOnlyProperty(this, 'speed', data.speed);
244 SetReadOnlyProperty(this, 'errorRange', data.errorRange);
245 SetReadOnlyProperty(this, 'timestamp', data.timestamp);
249 function HumanActivityGPSInfoArray(data) {
250 SetReadOnlyProperty(this, 'gpsInfo', data);
253 HumanActivityGPSInfoArray.prototype = new HumanActivityData();
254 HumanActivityGPSInfoArray.prototype.constructor = HumanActivityGPSInfoArray;
257 exports = new HumanActivityMonitorManager();