tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / HumanActivityMonitor / JSMotionManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include <SecurityExceptions.h>
20 #include <JSUtil.h>
21 #include <JSWebAPIError.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26 #include <cstring>
27
28 #include "plugin_config.h"
29 #include "JSMotionManager.h"
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
33 using namespace std;
34
35
36 namespace DeviceAPI {
37 namespace HumanActivityMonitor {
38
39 JSClassDefinition JSMotionManager::m_classInfo = {
40     0,
41     kJSClassAttributeNone,
42     "MotionManager",
43     NULL, //ParentClass
44     NULL, //StaticValues
45     m_function, //StaticFunctions
46     initialize, //Initialize
47     finalize, //Finalize
48     NULL, //HasProperty,
49     NULL, //GetProperty,
50     NULL, //SetProperty,
51     NULL, //DeleteProperty,
52     NULL, //GetPropertyNames,
53     NULL, //CallAsFunction,
54     NULL, //CallAsConstructor,
55     NULL, //HasInstance,
56     NULL //ConvertToType
57 };
58
59 JSStaticFunction JSMotionManager::m_function[] = {
60     { MOTION_API_GET_CONTEXT_INFO, JSMotionManager::getContextInfo, kJSPropertyAttributeNone },
61     { MOTION_API_SET_ACCUMULATIVE_PEDOMETER_LISTENER, JSMotionManager::setAccumulativePedometerListener, kJSPropertyAttributeNone },
62     { MOTION_API_UNSET_ACCUMULATIVE_PEDOMETER_LISTENER, JSMotionManager::unsetAccumulativePedometerListener, kJSPropertyAttributeNone },
63     { MOTION_API_START, JSMotionManager::setChangeListener, kJSPropertyAttributeNone },
64     { MOTION_API_STOP, JSMotionManager::unsetChangeListener, kJSPropertyAttributeNone },
65     { 0, 0, 0 }
66 };
67
68 JSClassRef JSMotionManager::m_jsClassRef = JSClassCreate(JSMotionManager::getClassInfo());
69
70 const JSClassRef JSMotionManager::getClassRef()
71 {
72     if (!m_jsClassRef) {
73         m_jsClassRef = JSClassCreate(&m_classInfo);
74     }
75     return m_jsClassRef;
76 }
77
78 const JSClassDefinition* JSMotionManager::getClassInfo()
79 {
80     return &m_classInfo;
81 }
82
83 void JSMotionManager::initialize(JSContextRef context, JSObjectRef object)
84 {
85
86 }
87
88 void JSMotionManager::finalize(JSObjectRef object)
89 {
90     LOGD("enter");
91 }
92
93 JSValueRef JSMotionManager::getContextInfo(JSContextRef context,
94         JSObjectRef object,
95         JSObjectRef thisObject,
96         size_t argumentCount,
97         const JSValueRef arguments[],
98         JSValueRef* exception)
99 {
100     LOGD("enter");
101     string contextType;
102
103     try {
104
105         AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_GET_CONTEXT_INFO);
106         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
107
108         ArgumentValidator validator(context, argumentCount, arguments);
109         contextType = validator.toString(0);
110
111         if(!strcmp(MOTION_ENUM_TYPE_PEDOMETER,contextType.c_str())){
112             // successCallback
113             JSObjectRef successCB = validator.toFunction(1);
114             if (successCB) {
115                 PedometerSuccessCallbackPtr pedometerGetCB(new PedometerSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
116                 pedometerGetCB->setSuccessCallback(successCB);
117
118                 // errorCallback
119                 JSObjectRef errCB = validator.toFunction(2, true);
120                 if(errCB){
121                     pedometerGetCB->setErrorCallback(errCB);
122                 }
123
124                 // perform
125                 MotionManager::getInstance()->getContextInfo(pedometerGetCB);
126
127             }else{
128                 LOGE("argument type mismatch");
129                 throw TypeMismatchException("argument type mismatch");
130             }
131
132         }else if(!strcmp(MOTION_ENUM_TYPE_WRIST_UP,contextType.c_str()) || !strcmp(MOTION_ENUM_TYPE_HRM,contextType.c_str())){
133             LOGE("Not supported");
134             throw NotSupportedException("Not supported");
135         }else{
136             LOGE("argument type mismatch");
137             throw TypeMismatchException("Type mismatch");
138         }
139
140         return JSValueMakeUndefined(context);
141
142     } catch (const TypeMismatchException &err) {
143         LOGE("Type Mismatch Exception");
144         return JSWebAPIErrorFactory::postException(context, exception, err);
145     } catch (const DeviceAPI::Common::BasePlatformException &err) {
146         LOGE("throw exception");
147         return JSWebAPIErrorFactory::postException(context, exception, err);
148     }
149
150 }
151
152 JSValueRef JSMotionManager::setChangeListener(JSContextRef context,
153         JSObjectRef object,
154         JSObjectRef thisObject,
155         size_t argumentCount,
156         const JSValueRef arguments[],
157         JSValueRef* exception)
158 {
159
160     LOGD("enter");
161
162     try {
163         AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_START);
164         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165
166         string contextType;
167         ArgumentValidator validator(context, argumentCount, arguments);
168         JSObjectRef successCB = validator.toFunction(1, true);
169
170         contextType = validator.toString(0);
171
172         if(!strcmp(MOTION_ENUM_TYPE_PEDOMETER,contextType.c_str())){
173
174             PedometerSuccessCallbackPtr pedometerCB(new PedometerSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
175             if (successCB) {
176                 pedometerCB->setSuccessCallback(successCB);
177             }
178             MotionManager::getInstance()->setChangeListener(pedometerCB);
179
180         }else if(!strcmp(MOTION_ENUM_TYPE_WRIST_UP,contextType.c_str())){
181
182             WristUpSuccessCallbackPtr wristupCB(new WristUpSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
183             if (successCB) {
184                 wristupCB->setSuccessCallback(successCB);
185             }
186             MotionManager::getInstance()->setChangeListener(wristupCB);
187
188         }else if(!strcmp(MOTION_ENUM_TYPE_HRM,contextType.c_str())){
189
190             HRMSuccessCallbackPtr hrmCB(new HRMSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
191             if (successCB) {
192                 hrmCB->setSuccessCallback(successCB);
193             }
194             MotionManager::getInstance()->setChangeListener(hrmCB);
195
196         }else if(!strcmp(MOTION_ENUM_TYPE_GPS,contextType.c_str())){
197             AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_START);
198             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
199
200             GPSSuccessCallbackPtr gpsCB(new GPSSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
201             if (successCB) {
202                 gpsCB->setSuccessCallback(successCB);
203             }
204             MotionManager::getInstance()->setChangeListener(gpsCB);
205
206         }else{
207                throw TypeMismatchException("Type Mismatch");
208         }
209
210         return JSValueMakeUndefined(context);
211
212     } catch (const DeviceAPI::Common::BasePlatformException &err) {
213         LOGE("argument is wrong.");
214         return JSWebAPIErrorFactory::postException(context, exception, err);
215     }
216
217 }
218
219
220
221 JSValueRef JSMotionManager::unsetChangeListener(JSContextRef context,
222         JSObjectRef object,
223         JSObjectRef thisObject,
224         size_t argumentCount,
225         const JSValueRef arguments[],
226         JSValueRef* exception)
227 {
228
229     LOGD("enter");
230
231     try {
232         AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_STOP);
233         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
234
235         string contextType;
236         ArgumentValidator validator(context, argumentCount, arguments);
237         contextType = validator.toString(0);
238
239
240         if(!strcmp(MOTION_ENUM_TYPE_PEDOMETER,contextType.c_str()) || !strcmp(MOTION_ENUM_TYPE_WRIST_UP,contextType.c_str())
241             || !strcmp(MOTION_ENUM_TYPE_HRM,contextType.c_str()) || !strcmp(MOTION_ENUM_TYPE_GPS,contextType.c_str())){
242             AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_START);
243             TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
244             MotionManager::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context), contextType.c_str());
245         }else{
246                throw TypeMismatchException("Type Mismatch");
247         }
248
249         return JSValueMakeUndefined(context);
250
251     } catch (const DeviceAPI::Common::BasePlatformException &err) {
252         LOGE("argument is wrong.");
253         return JSWebAPIErrorFactory::postException(context, exception, err);
254     }
255
256 }
257
258
259
260 JSValueRef JSMotionManager::setAccumulativePedometerListener(JSContextRef context,
261         JSObjectRef object,
262         JSObjectRef thisObject,
263         size_t argumentCount,
264         const JSValueRef arguments[],
265         JSValueRef* exception)
266 {
267     LOGD("enter");
268
269
270     try {
271
272         AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_START);
273         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
274
275         ArgumentValidator validator(context, argumentCount, arguments);
276         JSObjectRef successCB = validator.toFunction(0);
277
278         PedometerSuccessCallbackPtr pedometerCB(new PedometerSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
279         if (successCB) {
280             pedometerCB->setSuccessCallback(successCB);
281         }
282         MotionManager::getInstance()->setAccumulativePedometerListener(pedometerCB);
283
284         return JSValueMakeUndefined(context);
285
286     } catch (const DeviceAPI::Common::BasePlatformException &err) {
287         LOGE("argument is wrong.");
288         return JSWebAPIErrorFactory::postException(context, exception, err);
289     }
290
291
292 }
293
294 JSValueRef JSMotionManager::unsetAccumulativePedometerListener(JSContextRef context,
295         JSObjectRef object,
296         JSObjectRef thisObject,
297         size_t argumentCount,
298         const JSValueRef arguments[],
299         JSValueRef* exception)
300 {
301     LOGD("enter");
302
303     try {
304         AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_STOP);
305         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
306
307         ArgumentValidator validator(context, argumentCount, arguments);
308         MotionManager::getInstance()->unsetAccumulativePedometerListener(GlobalContextManager::getInstance()->getGlobalContext(context));
309
310         return JSValueMakeUndefined(context);
311
312     } catch (const DeviceAPI::Common::BasePlatformException &err) {
313         LOGE("argument is wrong.");
314         return JSWebAPIErrorFactory::postException(context, exception, err);
315     }
316
317
318 }
319
320 } // Context
321 } // DeviceAPI