2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 * @file FApp_ServiceAppImpl.cpp
19 * @brief This is the implementation for the _ServiceAppImpl class.
23 #include <linux/limits.h>
25 #include <FAppAppRegistry.h>
26 #include <FBaseErrors.h>
27 #include <FBaseSysLog.h>
29 #include <FBaseRt_LibraryImpl.h>
31 #include "FApp_AppInfo.h"
32 #include "FApp_AppImpl.h"
33 #include "FApp_ServiceAppImpl.h"
34 #include "FAppPkg_PackageManagerImpl.h"
35 #include "FApp_AppManagerImpl.h"
36 #include "FApp_TemplateUtil.h"
37 #include "FApp_IAppEventListener.h"
39 using namespace Tizen::App::Package;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::System;
45 const wchar_t USE_UI_KEY[] = L"UseUi";
46 const wchar_t USE_UI_VAL_TRUE[] = L"True";
47 const wchar_t LIFE_DURATION_KEY[] = L"LifeDuration";
48 const int LIFE_DURATION_MSEC_MAX = 30000;
50 static const RequestId HANDLER_REQUEST_ALARMID = 2;
52 namespace Tizen { namespace App
55 static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
56 typedef void (*OnAlarmForLaunch)(int alarmId);
58 _ServiceAppImpl* _ServiceAppImpl::__pServiceAppImpl = null;
61 _ServiceAppImpl::_ServiceAppImpl(ServiceApp* pServiceApp)
62 : __pAppImpl(_AppImpl::GetInstance())
63 , __pServiceApp(pServiceApp)
64 , __pAppTerminatingInternalEventListener(null)
65 , __pLifeDurationTimer(null)
67 , __pauseLifeDurationTimer(false)
69 __pServiceAppImpl = this;
70 SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
74 _ServiceAppImpl::~_ServiceAppImpl(void)
76 __pServiceAppImpl = null;
77 if( __pLifeDurationTimer )
79 __pLifeDurationTimer->Cancel();
80 delete __pLifeDurationTimer;
86 _ServiceAppImpl::OnCreate(void)
88 SysLog(NID_APP, "Platform creation event.");
90 _AppInfo::SetAppState(INITIALIZING);
97 _ServiceAppImpl::OnService(service_s* service, bool initial)
99 SysLog(NID_APP, "Service requested.");
100 char* pOperation = NULL;
101 int errVal = service_get_operation(service, &pOperation);
102 result r = E_SUCCESS;
104 if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
106 char* pAlarmId = NULL;
108 errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
109 if (errVal == SERVICE_ERROR_NONE)
111 int alarmId = atoi(pAlarmId);
113 SysLog(NID_SYS, "Start to load external lib");
115 OnAlarmForLaunch pOnAlarmForLaunch = null;
116 r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
120 SysLog(NID_SYS, "Open alarm condition library");
121 pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
122 if(pOnAlarmForLaunch != null)
124 SysLog(NID_SYS, "Function is found");
125 pOnAlarmForLaunch(alarmId);
126 SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
130 SysLog(NID_SYS, "Fail to find alarm function");
135 SysLog(NID_SYS, "Fail to open alarm condition library");
150 if( __lifeDuration > 0 && __pauseLifeDurationTimer == false)
152 SetLifeDurationTimer(__lifeDuration);
157 _ServiceAppImpl::OnTerminate(void)
159 SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
161 if (_AppInfo::GetAppState() == TERMINATED)
166 _AppInfo::SetAppState(TERMINATING);
168 if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
170 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
173 _AppInfo::SetAppState(TERMINATED);
178 _ServiceAppImpl::OnResume(void)
180 SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
185 _ServiceAppImpl::OnPause(void)
187 SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
192 _ServiceAppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation)
194 SysLog(NID_APP, "System device orientation changed event on 0x%x state", _AppInfo::GetAppState());
199 _ServiceAppImpl::OnWindowHandleRequest(void)
205 _ServiceAppImpl::GetInstance(void)
207 return __pServiceAppImpl;
212 _ServiceAppImpl::GetServiceAppInstance(void)
214 return __pServiceApp;
218 _ServiceAppImpl::OnAppInitializing(void)
220 const String& packageId = _AppInfo::GetPackageId();
221 const String& exeName = _AppInfo::GetAppExecutableName();
223 HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
227 _AppFeatureInfoImpl* pFeature = null;
229 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
233 const String& val = pFeature->GetValue();
234 if (val == USE_UI_VAL_TRUE)
236 SysLog(NID_APP, "Using remote ui on service application.");
238 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
239 result (*pInit)(void) = null;
241 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
245 SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
250 r = pInfo->GetValue(LIFE_DURATION_KEY, pFeature);
253 const String& val = pFeature->GetValue();
254 r = Integer::Parse(val, __lifeDuration);
257 __lifeDuration = ( __lifeDuration > LIFE_DURATION_MSEC_MAX ) ? LIFE_DURATION_MSEC_MAX : __lifeDuration;
258 SysLog(NID_APP, "LifeDuration is (%d)", __lifeDuration);
262 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
266 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
267 return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
271 _ServiceAppImpl::OnAppInitialized(void)
273 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
274 return __pServiceApp->OnAppInitialized();
278 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
280 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
282 if( __pAppTerminatingInternalEventListener)
284 __pAppTerminatingInternalEventListener->OnApplicationTerminated(L"", 0);
286 return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
290 _ServiceAppImpl::SetLifeDurationTimer(int lifeDuration)
292 if( lifeDuration <= 0)
297 if( __pLifeDurationTimer == null)
299 __pLifeDurationTimer = new Timer;
300 __pLifeDurationTimer->Construct(*this);
301 SysLog(NID_APP, "Life duration timer is constructed.");
305 __pLifeDurationTimer->Cancel();
306 SysLog(NID_APP, "Life duration timer is cancelled.", lifeDuration );
309 if( __pauseLifeDurationTimer == false)
311 __pLifeDurationTimer->Start(lifeDuration);
312 SysLog(NID_APP, "Life duration timer is started with timeout.(%d)", lifeDuration );
317 _ServiceAppImpl::OnTimerExpired(Timer& timer)
319 SysLog(NID_APP, "Life duration timer is expired, so terminating the application.");
321 App::GetInstance()->Terminate();
325 _ServiceAppImpl::SetAppTerminatingInternalEventListener(_IAppEventListener* pListener)
327 __pAppTerminatingInternalEventListener = pListener;
331 _ServiceAppImpl::PauseLifeDurationTimer(void)
333 __pauseLifeDurationTimer = true;
334 if( __pLifeDurationTimer)
336 __pLifeDurationTimer->Cancel();
337 SysLog(NID_APP, "Life duration timer is paused." );
342 _ServiceAppImpl::ResumeLifeDurationTimer(void)
344 __pauseLifeDurationTimer = false;
345 // SetLifeDurationTimer(__lifeDuration);
346 SysLog(NID_APP, "Life duration timer will be resumed." );