2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <SecurityExceptions.h>
21 #include <JSWebAPIError.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
28 #include "plugin_config.h"
29 #include "JSMotionManager.h"
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
37 namespace HumanActivityMonitor {
39 JSClassDefinition JSMotionManager::m_classInfo = {
41 kJSClassAttributeNone,
45 m_function, //StaticFunctions
46 initialize, //Initialize
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
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 },
68 JSClassRef JSMotionManager::m_jsClassRef = JSClassCreate(JSMotionManager::getClassInfo());
70 const JSClassRef JSMotionManager::getClassRef()
73 m_jsClassRef = JSClassCreate(&m_classInfo);
78 const JSClassDefinition* JSMotionManager::getClassInfo()
83 void JSMotionManager::initialize(JSContextRef context, JSObjectRef object)
88 void JSMotionManager::finalize(JSObjectRef object)
93 JSValueRef JSMotionManager::getContextInfo(JSContextRef context,
95 JSObjectRef thisObject,
97 const JSValueRef arguments[],
98 JSValueRef* exception)
105 AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_GET_CONTEXT_INFO);
106 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
108 ArgumentValidator validator(context, argumentCount, arguments);
109 contextType = validator.toString(0);
111 if(!strcmp(MOTION_ENUM_TYPE_PEDOMETER,contextType.c_str())){
113 JSObjectRef successCB = validator.toFunction(1);
115 PedometerSuccessCallbackPtr pedometerGetCB(new PedometerSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
116 pedometerGetCB->setSuccessCallback(successCB);
119 JSObjectRef errCB = validator.toFunction(2, true);
121 pedometerGetCB->setErrorCallback(errCB);
125 MotionManager::getInstance()->getContextInfo(pedometerGetCB);
128 LOGE("argument type mismatch");
129 throw TypeMismatchException("argument type mismatch");
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");
136 LOGE("argument type mismatch");
137 throw TypeMismatchException("Type mismatch");
140 return JSValueMakeUndefined(context);
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);
152 JSValueRef JSMotionManager::setChangeListener(JSContextRef context,
154 JSObjectRef thisObject,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
163 AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_START);
164 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
167 ArgumentValidator validator(context, argumentCount, arguments);
168 JSObjectRef successCB = validator.toFunction(1, true);
170 contextType = validator.toString(0);
172 if(!strcmp(MOTION_ENUM_TYPE_PEDOMETER,contextType.c_str())){
174 PedometerSuccessCallbackPtr pedometerCB(new PedometerSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
176 pedometerCB->setSuccessCallback(successCB);
178 MotionManager::getInstance()->setChangeListener(pedometerCB);
180 }else if(!strcmp(MOTION_ENUM_TYPE_WRIST_UP,contextType.c_str())){
182 WristUpSuccessCallbackPtr wristupCB(new WristUpSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
184 wristupCB->setSuccessCallback(successCB);
186 MotionManager::getInstance()->setChangeListener(wristupCB);
188 }else if(!strcmp(MOTION_ENUM_TYPE_HRM,contextType.c_str())){
190 HRMSuccessCallbackPtr hrmCB(new HRMSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
192 hrmCB->setSuccessCallback(successCB);
194 MotionManager::getInstance()->setChangeListener(hrmCB);
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);
200 GPSSuccessCallbackPtr gpsCB(new GPSSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
202 gpsCB->setSuccessCallback(successCB);
204 MotionManager::getInstance()->setChangeListener(gpsCB);
207 throw TypeMismatchException("Type Mismatch");
210 return JSValueMakeUndefined(context);
212 } catch (const DeviceAPI::Common::BasePlatformException &err) {
213 LOGE("argument is wrong.");
214 return JSWebAPIErrorFactory::postException(context, exception, err);
221 JSValueRef JSMotionManager::unsetChangeListener(JSContextRef context,
223 JSObjectRef thisObject,
224 size_t argumentCount,
225 const JSValueRef arguments[],
226 JSValueRef* exception)
232 AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_STOP);
233 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
236 ArgumentValidator validator(context, argumentCount, arguments);
237 contextType = validator.toString(0);
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());
246 throw TypeMismatchException("Type Mismatch");
249 return JSValueMakeUndefined(context);
251 } catch (const DeviceAPI::Common::BasePlatformException &err) {
252 LOGE("argument is wrong.");
253 return JSWebAPIErrorFactory::postException(context, exception, err);
260 JSValueRef JSMotionManager::setAccumulativePedometerListener(JSContextRef context,
262 JSObjectRef thisObject,
263 size_t argumentCount,
264 const JSValueRef arguments[],
265 JSValueRef* exception)
272 AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_START);
273 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
275 ArgumentValidator validator(context, argumentCount, arguments);
276 JSObjectRef successCB = validator.toFunction(0);
278 PedometerSuccessCallbackPtr pedometerCB(new PedometerSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
280 pedometerCB->setSuccessCallback(successCB);
282 MotionManager::getInstance()->setAccumulativePedometerListener(pedometerCB);
284 return JSValueMakeUndefined(context);
286 } catch (const DeviceAPI::Common::BasePlatformException &err) {
287 LOGE("argument is wrong.");
288 return JSWebAPIErrorFactory::postException(context, exception, err);
294 JSValueRef JSMotionManager::unsetAccumulativePedometerListener(JSContextRef context,
296 JSObjectRef thisObject,
297 size_t argumentCount,
298 const JSValueRef arguments[],
299 JSValueRef* exception)
304 AceSecurityStatus status = HUMANACTIVITYMONITOR_CHECK_ACCESS(MOTION_API_STOP);
305 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
307 ArgumentValidator validator(context, argumentCount, arguments);
308 MotionManager::getInstance()->unsetAccumulativePedometerListener(GlobalContextManager::getInstance()->getGlobalContext(context));
310 return JSValueMakeUndefined(context);
312 } catch (const DeviceAPI::Common::BasePlatformException &err) {
313 LOGE("argument is wrong.");
314 return JSWebAPIErrorFactory::postException(context, exception, err);