Merge "Revert "add workaround code for LifeDuration"" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_ServiceAppImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 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  * @file        FApp_ServiceAppImpl.cpp
20  * @brief       This is the implementation for the _ServiceAppImpl class.
21  */
22
23 #include <cstdio>
24 #include <linux/limits.h>
25
26 #include <FAppAppRegistry.h>
27 #include <FBaseErrors.h>
28 #include <FBaseSysLog.h>
29
30 #include <FBaseRt_LibraryImpl.h>
31
32 #include "FApp_AppInfo.h"
33 #include "FApp_AppImpl.h"
34 #include "FApp_ServiceAppImpl.h"
35 #include "FAppPkg_PackageManagerImpl.h"
36 #include "FApp_AppManagerImpl.h"
37 #include "FApp_TemplateUtil.h"
38 #include "FApp_IAppEventListener.h"
39
40 using namespace Tizen::App::Package;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Base::Runtime;
44 using namespace Tizen::System;
45
46 const wchar_t USE_UI_KEY[] = L"UseUi";
47 const wchar_t USE_UI_VAL_TRUE[] = L"True";
48 const wchar_t LIFE_DURATION_KEY[] = L"LifeDuration";
49 const int LIFE_DURATION_MSEC_MAX = 30000;
50
51 static const RequestId  HANDLER_REQUEST_ALARMID = 2;
52
53 namespace Tizen { namespace App
54 {
55
56 static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
57 typedef void (*OnAlarmForLaunch)(int alarmId);
58
59 _ServiceAppImpl* _ServiceAppImpl::__pServiceAppImpl = null;
60
61
62 _ServiceAppImpl::_ServiceAppImpl(ServiceApp* pServiceApp)
63         : __pAppImpl(_AppImpl::GetInstance())
64         , __pServiceApp(pServiceApp)
65         , __pAppTerminatingInternalEventListener(null)
66         , __pLifeDurationTimer(null)
67         , __lifeDuration(0)
68         , __pauseLifeDurationTimer(false)
69 {
70         __pServiceAppImpl = this;
71         SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
72 }
73
74
75 _ServiceAppImpl::~_ServiceAppImpl(void)
76 {
77         __pServiceAppImpl = null;
78         if( __pLifeDurationTimer )
79         {
80                 __pLifeDurationTimer->Cancel();
81                 delete __pLifeDurationTimer;
82         }
83 }
84
85
86 bool
87 _ServiceAppImpl::OnCreate(void)
88 {
89         SysLog(NID_APP, "Platform creation event.");
90
91         _AppInfo::SetAppState(INITIALIZING);
92
93         return true;
94 }
95
96
97 void
98 _ServiceAppImpl::OnService(service_s* service, bool initial)
99 {
100         SysLog(NID_APP, "Service requested.");
101         char* pOperation = NULL;
102         int errVal = service_get_operation(service, &pOperation);
103         result r = E_SUCCESS;
104
105         if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
106         {
107                 char* pAlarmId = NULL;
108
109                 errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
110                 if (errVal == SERVICE_ERROR_NONE)
111                 {
112                         int alarmId = atoi(pAlarmId);
113
114                         SysLog(NID_SYS, "Start to load external lib");
115                         Library lib;
116                         OnAlarmForLaunch pOnAlarmForLaunch = null;
117                         r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
118
119                         if(r == E_SUCCESS)
120                         {
121                                 SysLog(NID_SYS, "Open alarm condition library");
122                                 pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
123                                 if(pOnAlarmForLaunch != null)
124                                 {
125                                         SysLog(NID_SYS, "Function is found");
126                                         pOnAlarmForLaunch(alarmId);
127                                         SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
128                                 }
129                                 else
130                                 {
131                                         SysLog(NID_SYS, "Fail to find alarm function");
132                                 }
133                         }
134                         else
135                         {
136                                 SysLog(NID_SYS, "Fail to open alarm condition library");
137                         }
138                 }
139
140                 if (pAlarmId)
141                 {
142                         free(pAlarmId);
143                 }
144         }
145
146         if (pOperation)
147         {
148                 free(pOperation);
149         }
150
151         if( __lifeDuration > 0 && __pauseLifeDurationTimer == false)
152         {
153                 SetLifeDurationTimer(__lifeDuration);
154         }
155 }
156
157 void
158 _ServiceAppImpl::OnTerminate(void)
159 {
160         SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
161
162         if (_AppInfo::GetAppState() == TERMINATED)
163         {
164                 return;
165         }
166
167         _AppInfo::SetAppState(TERMINATING);
168
169         if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
170         {
171                 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
172         }
173
174         _AppInfo::SetAppState(TERMINATED);
175 }
176
177
178 void
179 _ServiceAppImpl::OnResume(void)
180 {
181         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
182 }
183
184
185 void
186 _ServiceAppImpl::OnPause(void)
187 {
188         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
189 }
190
191
192 void
193 _ServiceAppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation)
194 {
195         SysLog(NID_APP, "System device orientation changed event on 0x%x state", _AppInfo::GetAppState());
196 }
197
198
199 long
200 _ServiceAppImpl::OnWindowHandleRequest(void)
201 {
202         return -1;
203 }
204
205 _ServiceAppImpl*
206 _ServiceAppImpl::GetInstance(void)
207 {
208         return __pServiceAppImpl;
209 }
210
211
212 ServiceApp*
213 _ServiceAppImpl::GetServiceAppInstance(void)
214 {
215         return __pServiceApp;
216 }
217
218 bool
219 _ServiceAppImpl::OnAppInitializing(void)
220 {
221         const String& packageId = _AppInfo::GetPackageId();
222         const String& exeName = _AppInfo::GetAppExecutableName();
223
224         HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
225
226         if (pInfo)
227         {
228                 _AppFeatureInfoImpl* pFeature = null;
229
230                 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
231
232                 if (r == E_SUCCESS)
233                 {
234                         const String& val = pFeature->GetValue();
235                         if (val == USE_UI_VAL_TRUE)
236                         {
237                                 SysLog(NID_APP, "Using remote ui on service application.");
238
239                                 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
240                                 result (*pInit)(void) = null;
241
242                                 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
243                                 if (pInit)
244                                 {
245                                         r = (*pInit)();
246                                         SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
247                                 }
248                         }
249                 }
250
251                 r = pInfo->GetValue(LIFE_DURATION_KEY, pFeature);
252                 if (r == E_SUCCESS)
253                 {
254                         const String& val = pFeature->GetValue();
255                         r = Integer::Parse(val, __lifeDuration);
256                         if( r == E_SUCCESS )
257                         {
258                                 __lifeDuration = ( __lifeDuration > LIFE_DURATION_MSEC_MAX ) ? LIFE_DURATION_MSEC_MAX : __lifeDuration;
259                                 SysLog(NID_APP, "LifeDuration is (%d)", __lifeDuration);
260                         }
261                 }
262
263                 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
264                 delete pInfo;
265         }
266
267         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
268         return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
269 }
270
271 bool
272 _ServiceAppImpl::OnAppInitialized(void)
273 {
274         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
275         return __pServiceApp->OnAppInitialized();
276 }
277
278 bool
279 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
280 {
281         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
282
283         if( __pAppTerminatingInternalEventListener)
284         {
285                 __pAppTerminatingInternalEventListener->OnApplicationTerminated(L"", 0);
286         }
287         return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
288 }
289
290 void
291 _ServiceAppImpl::SetLifeDurationTimer(int lifeDuration)
292 {
293         if( lifeDuration <= 0)
294         {
295                 return;
296         }
297
298         if( __pLifeDurationTimer == null)
299         {
300                 __pLifeDurationTimer = new Timer;
301                 __pLifeDurationTimer->Construct(*this);
302                 SysLog(NID_APP, "Life duration timer is constructed.");
303         }
304         else
305         {
306                 __pLifeDurationTimer->Cancel();
307                 SysLog(NID_APP, "Life duration timer is cancelled.", lifeDuration );
308         }
309
310         if( __pauseLifeDurationTimer == false)
311         {
312                 __pLifeDurationTimer->Start(lifeDuration);
313                 SysLog(NID_APP, "Life duration timer is started with timeout.(%d)", lifeDuration );
314         }
315 }
316
317 void
318 _ServiceAppImpl::OnTimerExpired(Timer& timer)
319 {
320         SysLog(NID_APP, "Life duration timer is expired, so terminating the application.");
321         timer.Cancel();
322         App::GetInstance()->Terminate();
323 }
324
325 void
326 _ServiceAppImpl::SetAppTerminatingInternalEventListener(_IAppEventListener* pListener)
327 {
328         __pAppTerminatingInternalEventListener = pListener;
329 }
330
331 void
332 _ServiceAppImpl::PauseLifeDurationTimer(void)
333 {
334         __pauseLifeDurationTimer = true;
335         if( __pLifeDurationTimer)
336         {
337                 __pLifeDurationTimer->Cancel();
338                 SysLog(NID_APP, "Life duration timer is paused." );
339         }
340 }
341
342 void
343 _ServiceAppImpl::ResumeLifeDurationTimer(void)
344 {
345         __pauseLifeDurationTimer = false;
346 //      SetLifeDurationTimer(__lifeDuration);
347         SysLog(NID_APP, "Life duration timer will be resumed." );
348 }
349
350 } } //Tizen::App