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 native_.call('HumanActivityMonitorManager_getHumanActivityData', data, callback);
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
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}
112 var listenerId ='HumanActivityMonitor_' + args.type;
116 listenerId: listenerId
119 if (!native_.isListenerSet(listenerId)) {
120 var result = native_.callSync('HumanActivityMonitorManager_start', data);
121 if (native_.isFailure(result)) {
122 throw native_.getErrorObject(result);
126 var listener = function(result) {
127 native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result));
129 native_.addListener(listenerId, listener);
132 HumanActivityMonitorManager.prototype.stop = function(type) {
133 var args = validator_.validateArgs(arguments, [
134 {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
141 var listenerId ='HumanActivityMonitor_' + args.type;
143 if (!native_.isListenerSet(listenerId)) {
147 var result = native_.callSync('HumanActivityMonitorManager_stop', data);
148 if (native_.isFailure(result)) {
149 throw native_.getErrorObject(result);
152 native_.removeListener(listenerId);
155 HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function(changeCallback) {
156 var args = validator_.validateArgs(arguments, [
157 {name: 'changeCallback', type: types_.FUNCTION}
163 var callback = function(result) {
164 native_.callIfPossible(args.changeCallback);
167 native_.call('HumanActivityMonitorManager_setAccumulativePedometerListener', data, callback);
170 HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
175 var result = native_.callSync(
176 'HumanActivityMonitorManager_unsetAccumulativePedometerListener', data);
178 if (native_.isFailure(result)) {
179 throw native_.getErrorObject(result);
183 function StepDifference() {
184 SetReadOnlyProperty(this, 'stepCountDifference', null);
185 SetReadOnlyProperty(this, 'timestamp', null);
189 function HumanActivityData() {
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);
205 HumanActivityPedometerData.prototype = new HumanActivityData();
206 HumanActivityPedometerData.prototype.constructor = HumanActivityPedometerData;
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);
221 HumanActivityAccumulativePedometerData.prototype = new HumanActivityData();
222 HumanActivityAccumulativePedometerData.prototype.constructor = HumanActivityAccumulativePedometerData;
225 function HumanActivityHRMData(data) {
226 SetReadOnlyProperty(this, 'heartRate', data.heartRate);
227 SetReadOnlyProperty(this, 'rRInterval', data.rRInterval);
230 HumanActivityHRMData.prototype = new HumanActivityData();
231 HumanActivityHRMData.prototype.constructor = HumanActivityHRMData;
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);
244 function HumanActivityGPSInfoArray(data) {
245 SetReadOnlyProperty(this, 'gpsInfo', data);
248 HumanActivityGPSInfoArray.prototype = new HumanActivityData();
249 HumanActivityGPSInfoArray.prototype.constructor = HumanActivityGPSInfoArray;
252 exports = new HumanActivityMonitorManager();