Merge "fix AppControl result code for calendar AppControl" 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         , __pLifeDurationEventListener(null)
66         , __pLifeDurationTimer(null)
67         , __lifeDuration(0)
68 {
69         __pServiceAppImpl = this;
70         SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
71 }
72
73
74 _ServiceAppImpl::~_ServiceAppImpl(void)
75 {
76         __pServiceAppImpl = null;
77         if( __pLifeDurationTimer )
78         {
79                 __pLifeDurationTimer->Cancel();
80                 delete __pLifeDurationTimer;
81         }
82 }
83
84
85 bool
86 _ServiceAppImpl::OnCreate(void)
87 {
88         SysLog(NID_APP, "Platform creation event.");
89
90         _AppInfo::SetAppState(INITIALIZING);
91
92         return true;
93 }
94
95
96 void
97 _ServiceAppImpl::OnService(service_s* service, bool initial)
98 {
99         SysLog(NID_APP, "Service requested.");
100         char* pOperation = NULL;
101         int errVal = service_get_operation(service, &pOperation);
102         result r = E_SUCCESS;
103
104         if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
105         {
106                 char* pAlarmId = NULL;
107
108                 errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
109                 if (errVal == SERVICE_ERROR_NONE)
110                 {
111                         int alarmId = atoi(pAlarmId);
112
113                         SysLog(NID_SYS, "Start to load external lib");
114                         Library lib;
115                         OnAlarmForLaunch pOnAlarmForLaunch = null;
116                         r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
117
118                         if(r == E_SUCCESS)
119                         {
120                                 SysLog(NID_SYS, "Open alarm condition library");
121                                 pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
122                                 if(pOnAlarmForLaunch != null)
123                                 {
124                                         SysLog(NID_SYS, "Function is found");
125                                         pOnAlarmForLaunch(alarmId);
126                                         SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
127                                 }
128                                 else
129                                 {
130                                         SysLog(NID_SYS, "Fail to find alarm function");
131                                 }
132                         }
133                         else
134                         {
135                                 SysLog(NID_SYS, "Fail to open alarm condition library");
136                         }
137                 }
138
139                 if (pAlarmId)
140                 {
141                         free(pAlarmId);
142                 }
143         }
144
145         if (pOperation)
146         {
147                 free(pOperation);
148         }
149
150         if( __lifeDuration > 0)
151         {
152                 SetLifeDurationTimer(__lifeDuration);
153         }
154 }
155
156 void
157 _ServiceAppImpl::OnTerminate(void)
158 {
159         SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
160
161         if (_AppInfo::GetAppState() == TERMINATED)
162         {
163                 return;
164         }
165
166         _AppInfo::SetAppState(TERMINATING);
167
168         if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
169         {
170                 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
171         }
172
173         _AppInfo::SetAppState(TERMINATED);
174 }
175
176
177 void
178 _ServiceAppImpl::OnResume(void)
179 {
180         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
181 }
182
183
184 void
185 _ServiceAppImpl::OnPause(void)
186 {
187         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
188 }
189
190
191 void
192 _ServiceAppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation)
193 {
194         SysLog(NID_APP, "System device orientation changed event on 0x%x state", _AppInfo::GetAppState());
195 }
196
197
198 long
199 _ServiceAppImpl::OnWindowHandleRequest(void)
200 {
201         return -1;
202 }
203
204 _ServiceAppImpl*
205 _ServiceAppImpl::GetInstance(void)
206 {
207         return __pServiceAppImpl;
208 }
209
210
211 ServiceApp*
212 _ServiceAppImpl::GetServiceAppInstance(void)
213 {
214         return __pServiceApp;
215 }
216
217 bool
218 _ServiceAppImpl::OnAppInitializing(void)
219 {
220         const String& packageId = _AppInfo::GetPackageId();
221         const String& exeName = _AppInfo::GetAppExecutableName();
222
223         HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
224
225         if (pInfo)
226         {
227                 _AppFeatureInfoImpl* pFeature = null;
228
229                 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
230
231                 if (r == E_SUCCESS)
232                 {
233                         const String& val = pFeature->GetValue();
234                         if (val == USE_UI_VAL_TRUE)
235                         {
236                                 SysLog(NID_APP, "Using remote ui on service application.");
237
238                                 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
239                                 result (*pInit)(void) = null;
240
241                                 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
242                                 if (pInit)
243                                 {
244                                         r = (*pInit)();
245                                         SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
246                                 }
247                         }
248                 }
249
250                 r = pInfo->GetValue(LIFE_DURATION_KEY, pFeature);
251                 if (r == E_SUCCESS)
252                 {
253                         const String& val = pFeature->GetValue();
254                         r = Integer::Parse(val, __lifeDuration);
255                         if( r == E_SUCCESS )
256                         {
257                                 __lifeDuration = ( __lifeDuration > LIFE_DURATION_MSEC_MAX ) ? LIFE_DURATION_MSEC_MAX : __lifeDuration;
258                                 SysLog(NID_APP, "LifeDuration is (%d)", __lifeDuration);
259                         }
260                 }
261
262                 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
263                 delete pInfo;
264         }
265
266         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
267         return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
268 }
269
270 bool
271 _ServiceAppImpl::OnAppInitialized(void)
272 {
273         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
274         return __pServiceApp->OnAppInitialized();
275 }
276
277 bool
278 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
279 {
280         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
281         return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
282 }
283
284 void
285 _ServiceAppImpl::SetLifeDurationTimer(int lifeDuration)
286 {
287         if( lifeDuration <= 0)
288         {
289                 return;
290         }
291
292         if( __pLifeDurationTimer == null)
293         {
294                 __pLifeDurationTimer = new Timer;
295                 __pLifeDurationTimer->Construct(*this);
296                 SysLog(NID_APP, "Life duration timer is constructed.");
297         }
298         else
299         {
300                 __pLifeDurationTimer->Cancel();
301                 SysLog(NID_APP, "Life duration timer is cancelled.", lifeDuration );
302         }
303         __pLifeDurationTimer->Start(__lifeDuration);
304         SysLog(NID_APP, "Life duration timer is started with timeout.(%d)", lifeDuration );
305 }
306
307 void
308 _ServiceAppImpl::OnTimerExpired(Timer& timer)
309 {
310         SysLog(NID_APP, "Life duration timer is expired, so terminating the application.");
311         timer.Cancel();
312         if( __pLifeDurationEventListener)
313         {
314                 __pLifeDurationEventListener->OnApplicationTerminated(L"", 0);
315         }
316         App::GetInstance()->Terminate();
317 }
318
319 void
320 _ServiceAppImpl::SetLifeDurationEventListener(_IAppEventListener* pListener)
321 {
322         __pLifeDurationEventListener = pListener;
323 }
324
325 } } //Tizen::App