2 * Copyright (c) 2011 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.
18 #include <Commons/Exception.h>
19 #include <Tizen/Common/JSTizenExceptionFactory.h>
20 #include <Tizen/Common/JSTizenException.h>
21 #include <JavaScriptCore/JavaScript.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "ApplicationConverter.h"
25 #include "JSApplicationInformation.h"
26 #include "JSApplicationEvent.h"
27 #include "ApplicationListener.h"
28 #include "API/Application/ApplicationInformation.h"
29 #include "ApplicationInformationEventPrivateData.h"
34 namespace Application {
37 using namespace TizenApis::Commons;
38 using namespace TizenApis::Api::Application;
39 using namespace WrtDeviceApis;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
43 ApplicationListener& ApplicationListener::getInstance()
45 static ApplicationListener instance;
49 ApplicationListener::ApplicationListener() :
50 WrtDeviceApis::Commons::EventListener<EventInstalledApplicationChanged>(WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD)
54 void ApplicationListener::onAnswerReceived(const EventInstalledApplicationChangedPtr& event)
56 LogError(">>> onAnswerReceived for EventInstalledApplicationChanged");
57 ApplicationInformationEventPrivateDataPtr priveData = DPL::DynamicPointerCast<ApplicationInformationEventPrivateData>(event->getPrivateData());
58 JSContextRef context = priveData->getOnInstalled()->getContext();
59 JSValueRef errorObject = NULL;
60 JSValueRef result = NULL;
62 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
64 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
65 switch(event->getEventCode()) {
66 case EventInstalledApplicationChanged::APPLICATON_CHANGED_EVENT_CODE_INSTALLED:
67 LogError(">>> installed event is come");
68 result = converter->toJSValueRef(event->getApplicationInformation());
69 priveData->getOnInstalled()->callOnSuccess(result);
71 case EventInstalledApplicationChanged::APPLICATON_CHANGED_EVENT_CODE_UPDATED:
72 LogError(">>> updated event is come");
73 result = converter->toJSValueRef(event->getApplicationInformation());
74 priveData->getOnUpdated()->callOnSuccess(result);
76 case EventInstalledApplicationChanged::APPLICATON_CHANGED_EVENT_CODE_UNINSTALLED:
77 LogError(">>> uninstalled event is come");
78 result = converter->toJSValueRef(event->getApplicationInformation()->getAppId());
79 priveData->getOnUninstalled()->callOnSuccess(result);
82 // TODO: if error callback is not exist???
83 LogError(">>> unknown event is come");
84 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
85 priveData->getOnInstalled()->callOnError(errorObject);
88 } Catch (WrtDeviceApis::Commons::Exception) {
89 LogError("error during function executing");
90 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
91 priveData->getOnInstalled()->callOnError(errorObject);
94 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
95 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
96 priveData->getOnInstalled()->callOnError(errorObject);