Merge "Fix System issues" 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
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;
44
45 const wchar_t USE_UI_KEY[] = L"UseUi";
46 const wchar_t USE_UI_VAL_TRUE[] = L"True";
47
48 static const RequestId  HANDLER_REQUEST_ALARMID = 2;
49
50 namespace Tizen { namespace App
51 {
52
53 static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
54 typedef void (*OnAlarmForLaunch)(int alarmId);
55
56 _ServiceAppImpl* _ServiceAppImpl::__pServiceAppImpl = null;
57
58
59 _ServiceAppImpl::_ServiceAppImpl(ServiceApp* pServiceApp)
60         : __pAppImpl(_AppImpl::GetInstance())
61         , __pServiceApp(pServiceApp)
62 {
63         __pServiceAppImpl = this;
64         SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
65 }
66
67
68 _ServiceAppImpl::~_ServiceAppImpl(void)
69 {
70         __pServiceAppImpl = null;
71 }
72
73
74 bool
75 _ServiceAppImpl::OnCreate(void)
76 {
77         SysLog(NID_APP, "Platform creation event.");
78
79         _AppInfo::SetAppState(INITIALIZING);
80
81         return true;
82 }
83
84
85 void
86 _ServiceAppImpl::OnService(service_s* service, bool initial)
87 {
88         SysLog(NID_APP, "Service requested.");
89         char* pOperation = NULL;
90         int errVal = service_get_operation(service, &pOperation);
91         result r = E_SUCCESS;
92
93         if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
94         {
95                 char* pAlarmId = NULL;
96
97                 errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
98                 if (errVal == SERVICE_ERROR_NONE)
99                 {
100                         int alarmId = atoi(pAlarmId);
101
102                         SysLog(NID_SYS, "Start to load external lib");
103                         Library lib;
104                         OnAlarmForLaunch pOnAlarmForLaunch = null;
105                         r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
106
107                         if(r == E_SUCCESS)
108                         {
109                                 SysLog(NID_SYS, "Open alarm condition library");
110                                 pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
111                                 if(pOnAlarmForLaunch != null)
112                                 {
113                                         SysLog(NID_SYS, "Function is found");
114                                         pOnAlarmForLaunch(alarmId);
115                                         SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
116                                 }
117                                 else
118                                 {
119                                         SysLog(NID_SYS, "Fail to find alarm function");
120                                 }
121                         }
122                         else
123                         {
124                                 SysLog(NID_SYS, "Fail to open alarm condition library");
125                         }
126                 }
127
128                 if (pAlarmId)
129                 {
130                         free(pAlarmId);
131                 }
132         }
133
134         if (pOperation)
135         {
136                 free(pOperation);
137         }
138 }
139
140 void
141 _ServiceAppImpl::OnTerminate(void)
142 {
143         SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
144
145         if (_AppInfo::GetAppState() == TERMINATED)
146         {
147                 return;
148         }
149
150         _AppInfo::SetAppState(TERMINATING);
151
152         if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
153         {
154                 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
155         }
156
157         _AppInfo::SetAppState(TERMINATED);
158 }
159
160
161 void
162 _ServiceAppImpl::OnResume(void)
163 {
164         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
165 }
166
167
168 void
169 _ServiceAppImpl::OnPause(void)
170 {
171         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
172 }
173
174
175 void
176 _ServiceAppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation)
177 {
178         SysLog(NID_APP, "System device orientation changed event on 0x%x state", _AppInfo::GetAppState());
179 }
180
181
182 long
183 _ServiceAppImpl::OnWindowHandleRequest(void)
184 {
185         return -1;
186 }
187
188 _ServiceAppImpl*
189 _ServiceAppImpl::GetInstance(void)
190 {
191         return __pServiceAppImpl;
192 }
193
194
195 ServiceApp*
196 _ServiceAppImpl::GetServiceAppInstance(void)
197 {
198         return __pServiceApp;
199 }
200
201 bool
202 _ServiceAppImpl::OnAppInitializing(void)
203 {
204         const String& packageId = _AppInfo::GetPackageId();
205         const String& exeName = _AppInfo::GetAppExecutableName();
206
207         HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
208
209         if (pInfo)
210         {
211                 _AppFeatureInfoImpl* pFeature = null;
212
213                 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
214
215                 if (r == E_SUCCESS)
216                 {
217                         const String& val = pFeature->GetValue();
218                         if (val == USE_UI_VAL_TRUE)
219                         {
220                                 SysLog(NID_APP, "Using remote ui on service application.");
221
222                                 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
223                                 result (*pInit)(void) = null;
224
225                                 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
226                                 if (pInit)
227                                 {
228                                         r = (*pInit)();
229                                         SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
230                                 }
231                         }
232                 }
233
234                 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
235                 delete pInfo;
236         }
237
238         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
239         return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
240 }
241
242 bool
243 _ServiceAppImpl::OnAppInitialized(void)
244 {
245         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
246         return __pServiceApp->OnAppInitialized();
247 }
248
249 bool
250 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
251 {
252         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
253         return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
254 }
255
256 } } //Tizen::App