Tizen 2.1 base
[platform/framework/native/app-service.git] / src / AppService.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 #include <sys/prctl.h>
19 #include <signal.h>
20 #include <unique_ptr.h>
21
22 #include <vconf.h>
23 #include <calendar_service.h>
24 #include <calendar_types2.h>
25
26 #include <FIoFile.h>
27 #include <FBaseSysLog.h>
28
29 #include <FApp_Aul.h>
30 #include <FIo_RegistryImpl.h>
31 #include <FSys_EnvironmentImpl.h>
32 #include <FBase_StringConverter.h>
33 #include <FBaseRt_Process.h>
34
35 #include "FApp_AppInfo.h"
36 #include "FApp_AppManagerImpl.h"
37 #include "FApp_AppControlManager.h"
38 #include "FApp_CommunicationDispatcher.h"
39 #include "FApp_ContextManager.h"
40 #include "FApp_AppManagerStub.h"
41 #include "FApp_ConditionManagerStub.h"
42 #include "FSys_DeviceManagerService.h"
43 #include "FSys_SystemService.h"
44 #include "FSys_AlarmService.h"
45 #include "FSys_AccessoryManagerService.h"
46 #include "FApp_PackageManagerStub.h"
47 #include "FApp_NotificationManagerStub.h"
48 #include "FAppPkg_PackageManagerImpl.h"
49 #include "FIo_MmcStorageManagerStub.h"
50 #include "AppService.h"
51
52
53 using namespace Tizen::Base;
54 using namespace Tizen::Base::Runtime;
55 using namespace Tizen::Base::Collection;
56 using namespace Tizen::Io;
57 using namespace Tizen::System;
58 using namespace Tizen::App;
59
60 namespace
61 {
62 static const RequestId  HANDLER_REQUEST_INSTALL_COMPLETE = 0;
63 static const RequestId  HANDLER_REQUEST_UNINSTALL_COMPLETE = 1;
64 static const RequestId  HANDLER_REQUEST_ID = 2;
65 static const RequestId  HANDLER_REQUEST_ALARMID = 2;
66
67 const char _APP_PATH_FORMAT[] = "/opt/apps/0000000000/bin/%ls";
68 const char _HEARTBEAT_PATH[] = "/tmp/osp-initialized";
69 #ifndef VCONFKEY_APPSERVICE_STATUS
70 #define VCONFKEY_APPSERVICE_STATUS      "memory/appservice/status"
71 #endif
72
73 }
74
75
76 AppService::AppService()
77 : __pCommunicationDispatcher(null)
78 , __pContextMgr(null)
79 , __pAppManagerStub(null)
80 , __pConditionManagerStub(null)
81 , __handlerThread(*this)
82 , __pDeviceManagerService(null)
83 , __pSystemService(null)
84 , __pAccessoryManagerService(null)
85 , __pAlarmService(null)
86 , __pPackageManagerStub(null)
87 , __pNotificationManagerStub(null)
88 , __pMmcStorageManagerStub(null)
89 {
90         SysLog(NID_APP, "Enter.");
91
92         int pid = getpid();
93         setpgid(pid, pid);
94
95         InitializeIpc();
96
97         SysLog(NID_APP, "Exit.");
98 }
99
100 AppService::~AppService()
101 {
102         SysLog(NID_APP, "Enter.");
103
104         vconf_set_int(VCONFKEY_APPSERVICE_STATUS, -1);
105
106         Tizen::App::Package::_PackageManagerImpl::GetInstance()->RemoveEventListener(this);
107
108         delete __pAppManagerStub;
109         delete __pConditionManagerStub;
110         delete __pPackageManagerStub;
111         delete __pNotificationManagerStub;
112         delete __pMmcStorageManagerStub;
113
114         delete __pContextMgr;
115
116         SysLog(NID_APP, "Exit.");
117 }
118
119 Service*
120 AppService::CreateInstance(void)
121 {
122         static AppService* pAppService = new (std::nothrow) AppService();
123
124         return pAppService;
125 }
126
127 bool
128 AppService::OnAppInitializing(AppRegistry& appRegistry)
129 {
130         SysLog(NID_APP, "Enter.");
131
132         result r = __handlerThread.Construct(THREAD_TYPE_EVENT_DRIVEN);
133         SysTryReturn(NID_APP, IsFailed(r) == false, false, r, "[%s] Event thread creation failure.", GetErrorMessage(r));
134
135         r = __handlerThread.Start();
136         SysTryReturn(NID_APP, IsFailed(r) == false, false, r, "[%s] Event thread Start failure.", GetErrorMessage(r));
137
138         SysLog(NID_APP, "Exit.");
139         return true;
140 }
141
142 bool
143 AppService::OnAppInitialized(void)
144 {
145         SysLog(NID_APP, "Enter.");
146
147         _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
148
149         result r = _AppManagerImpl::GetInstance()->AddEventListener(*this);
150         SysAssertf(!IsFailed(r), "[%s] Failed to add event listener.", GetErrorMessage(r));
151
152         LaunchSecurityService();
153         LaunchChannelService();
154 //      LaunchSystemServices();
155 //      LaunchUserServices();
156 //
157
158         InitializeServices();
159
160         const String tmpPath = _HEARTBEAT_PATH;
161         File file;
162         (void) file.Construct(tmpPath, "w");
163
164         vconf_set_int(VCONFKEY_APPSERVICE_STATUS, 1);
165         SysLog(NID_APP, "Exit.");
166         return true;
167 }
168
169 void
170 AppService::OnPowerOffNotiReceived(void* pData)
171 {
172         SysLog(NID_APP, "'power_off_start' noti received from system-server with 0x%x.", pData);
173
174         AppService* pAppService = static_cast<AppService*>(pData);
175         if (pAppService)
176         {
177                 SysLog(NID_APP, "osp-app-service is terminating now.");
178                 pAppService->Terminate();
179         }
180 }
181
182
183 bool
184 AppService::InitializeIpc(void)
185 {
186         SysLog(NID_APP, "Enter.");
187         result r = E_SUCCESS;
188
189         __pContextMgr = new (std::nothrow) _ContextManager();
190         SysAssert(__pContextMgr != null);
191         SysTryCatch(NID_APP, __pContextMgr != null,, E_OUT_OF_MEMORY, "failed to _ContextManager creation.");
192
193         r = __pContextMgr->Construct();
194         SysAssert(!IsFailed(r));
195         SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
196
197         __pAppManagerStub = new (std::nothrow) _AppManagerStub();
198         SysAssert(__pAppManagerStub != null);
199         SysTryCatch(NID_APP, __pAppManagerStub != null,, E_OUT_OF_MEMORY, "failed to _AppManagerStub creation.");
200
201         r = __pAppManagerStub->Construct(__pContextMgr);
202         SysAssert(!IsFailed(r));
203         SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
204
205         __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
206         SysAssert(__pCommunicationDispatcher != null);
207         SysTryCatch(NID_APP, __pCommunicationDispatcher != null,, E_OUT_OF_MEMORY, "failed to _CommunicationDispatcher creation.");
208
209         SysLog(NID_APP, "Exit.");
210         return true;
211
212 CATCH:
213         SysLog(NID_APP, "Catch.");
214
215         delete __pAppManagerStub;
216         __pAppManagerStub = null;
217
218         delete __pContextMgr;
219         __pContextMgr = null;
220
221         SysAssertf(false, "IPC services initializing is failed.");
222         return false;
223 }
224
225 bool
226 AppService::InitializeServices(void)
227 {
228         SysLog(NID_APP, "Enter.");
229         result r = E_SUCCESS;
230
231         __pConditionManagerStub = new (std::nothrow) _ConditionManagerStub();
232         SysAssert(__pConditionManagerStub != null);
233         SysTryCatch(NID_APP, __pConditionManagerStub != null,, E_OUT_OF_MEMORY, "failed to _ConditionManagerStub creation.");
234
235         r = __pConditionManagerStub->Construct();
236         SysAssert(!IsFailed(r));
237         SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
238
239         __pDeviceManagerService = _DeviceManagerService::GetInstance();
240         SysAssert( __pDeviceManagerService != null);
241         SysTryCatch(NID_APP, __pDeviceManagerService != null,, E_OUT_OF_MEMORY, "failed to _DeviceManagerService creation.");
242
243         __pSystemService = _SystemService::GetInstance();
244         SysAssert( __pSystemService != null);
245         SysTryCatch(NID_APP, __pSystemService != null,, E_OUT_OF_MEMORY, "failed to _SystemService creation.");
246
247         __pAlarmService = _AlarmService::GetInstance();
248         SysAssert( __pAlarmService != null);
249         SysTryCatch(NID_APP, __pAlarmService != null,, E_OUT_OF_MEMORY, "failed to _pAlarmService creation.");
250
251         __pAccessoryManagerService = _AccessoryManagerService::GetInstance();
252         SysAssert( __pAccessoryManagerService != null);
253         SysTryCatch(NID_APP, __pAccessoryManagerService != null,, E_OUT_OF_MEMORY, "failed to _AccessoryManagerService creation.");
254
255         __pPackageManagerStub = new (std::nothrow) _PackageManagerStub();
256         SysAssert(__pPackageManagerStub != null);
257         SysTryCatch(NID_APP, __pPackageManagerStub != null,, E_OUT_OF_MEMORY, "failed to _PackageManagerStub creation.");
258
259         r = __pPackageManagerStub->Construct();
260         SysAssert(!IsFailed(r));
261         SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
262
263         __pNotificationManagerStub = new (std::nothrow) _NotificationManagerStub();
264         SysAssert(__pNotificationManagerStub != null);
265         SysTryCatch(NID_APP, __pNotificationManagerStub != null,, E_OUT_OF_MEMORY, "failed to _pNotificationManagerStub creation.");
266
267         r = __pNotificationManagerStub->Construct();
268         SysAssert(!IsFailed(r));
269         SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
270
271         __pMmcStorageManagerStub = new (std::nothrow) _MmcStorageManagerStub();
272         SysAssert(__pMmcStorageManagerStub != null);
273         SysTryCatch(NID_APP, __pMmcStorageManagerStub != null,, E_OUT_OF_MEMORY, "failed to __pMmcStorageManagerStub creation.");
274
275         r = __pMmcStorageManagerStub->Construct();
276         SysAssert(!IsFailed(r));
277         SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
278
279         SysLog(NID_APP, "Exit.");
280         return true;
281
282 CATCH:
283         SysLog(NID_APP, "Catch.");
284
285         delete __pConditionManagerStub;
286         __pConditionManagerStub = null;
287
288         delete __pPackageManagerStub;
289         __pPackageManagerStub = null;
290
291         delete __pNotificationManagerStub;
292         __pNotificationManagerStub = null;
293
294         delete __pMmcStorageManagerStub;
295         __pNotificationManagerStub = null;
296
297         SysAssertf(false, "System services initializing is failed.");
298         return false;
299 }
300
301 result
302 AppService::SetOomAdj(int pid, int adj)
303 {
304         // set oom_adj to -17 for system service
305         result r = E_SUCCESS;
306         char buf[FILENAME_MAX];
307         FILE *fP = NULL;
308
309         snprintf(buf, FILENAME_MAX, "/proc/%d/oom_adj", pid);
310         fP = fopen(buf, "w");
311         SysTryReturnResult(NID_APP, fP != NULL, E_SYSTEM, "oom_adj change failed with %s.", strerror(errno));
312
313         fprintf(fP, "%d", adj);
314         fclose(fP);
315
316         return r;
317 }
318
319 int
320 AppService::CreateProcess(const String& appId)
321 {
322         SysTryReturn(NID_APP, appId.GetLength() > 10 && appId[10] == L'.', -1, E_INVALID_ARG, "[E_INVALID_ARG] Wrong appId %ls.", appId.GetPointer());
323         int pid = fork();
324
325         if (pid == -1)
326         {
327                 return -1;
328         }
329         else if (pid == 0)
330         {
331                 char path[FILENAME_MAX];
332                 memset(path, '\0', FILENAME_MAX);
333
334                 snprintf(path, FILENAME_MAX, _APP_PATH_FORMAT, appId.GetPointer() + 11);
335
336                 int ret = wcstombs(path + strlen("/opt/apps/"), appId.GetPointer(), 10);
337                 if (ret == -1)
338                 {
339                         SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
340                         _Process::Exit(1);
341                 }
342
343                 SysLog(NID_APP, "Launching %s.", path);
344
345                 int currentPid = getpid();
346
347                 SetOomAdj(currentPid, -17); // set oom_adj to -17 for system service
348
349                 prctl(PR_SET_PDEATHSIG, SIGTERM);
350
351                 setpgid(currentPid, currentPid);
352
353                 ret = execl(path, path, static_cast<char*>(NULL));
354                 if (ret < 0)
355                 {
356                         SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
357                         _Process::Exit(1);
358                 }
359         }
360
361         return pid;
362 }
363
364 bool
365 AppService::LaunchSystemServices(void)
366 {
367         const wchar_t systemServiceRegPath[] = L"res/system-services.ini";
368         const String regKeyAppId = L"AppId";
369         const String regKeyPriority = L"Priority";
370
371         SysLog(NID_APP, "Enter.");
372         _RegistryImpl reg;
373         const String& systemListPath = _AppInfo::GetAppRootPath() + systemServiceRegPath;
374
375         result r = reg.Construct(systemListPath, REG_OPEN_READ_ONLY, null);
376         //      SysTryReturn(NID_APP, !IsFailed(r), r, r, "");
377         if (IsFailed(r))
378         {
379                 SysLog(NID_APP, "failed to open registry '%ls'", systemListPath.GetPointer());
380                 return true;
381         }
382
383         IList *pSections = null;
384         reg.GetSectionListN(&pSections);
385         SysTryReturn(NID_APP,  pSections != null, false, E_SYSTEM, "[E_SYSTEM] GetSectionListN fails (%ls) ", systemListPath.GetPointer() );
386
387         String* pSecName = null;
388         String appIdValue;
389         String priorityValue;
390         std::unique_ptr<IEnumerator> pEnum(pSections->GetEnumeratorN());
391         SysTryCatch(NID_APP, pEnum != null, , E_SYSTEM, "[E_SYSTEM] pSections->GetEnumeratorN fails (%ls)", systemListPath.GetPointer() );
392
393         while (pEnum->MoveNext() == E_SUCCESS)
394         {
395                 pSecName = static_cast<String*>(pEnum->GetCurrent());
396                 if ( pSecName != null)
397                 {
398                         priorityValue = L"";
399                         r = reg.GetValue(*pSecName, regKeyPriority, priorityValue);
400                         if (!IsFailed(r) && priorityValue == L"High")
401                         {
402                                 // channel and security service are launched by code already.
403                                 continue;
404                         }
405
406                         reg.GetValue(*pSecName, regKeyAppId, appIdValue);
407
408                         int pid = CreateProcess(appIdValue);
409                         if (pid != -1)
410                         {
411                                 SysLog(NID_APP, "System service(%ls) is launched as pid(%d).", appIdValue.GetPointer(), pid);
412 //                              __pContextMgr->Register(appIdValue, executableNameValue, pid, pid, true);
413                         }
414                         else
415                         {
416                                 SysLog(NID_APP, "Failed to launch System service(%ls).", appIdValue.GetPointer());
417                         }
418                 }
419         }
420
421
422         pSections->RemoveAll(true);
423         delete pSections;
424
425         SysLog(NID_APP, "Exit.");
426         return true;
427
428 CATCH:
429         pSections->RemoveAll(true);
430         delete pSections;
431         return false;
432 }
433
434 bool
435 AppService::LaunchChannelService(void)
436 {
437         SysLog(NID_APP, "Launching channel service...");
438         return CreateProcess(L"cp7ipabg4k.osp-channel-service" ) == E_SUCCESS;
439 }
440
441 bool
442 AppService::LaunchSecurityService(void)
443 {
444         SysLog(NID_APP, "Launching security service...");
445         return CreateProcess(L"q7097a278m.osp-security-service" ) == E_SUCCESS;
446 }
447
448 bool
449 AppService::LaunchUserServices(void)
450 {
451         SysLog(NID_APP, "Enter.");
452
453         IList* pAppIdList_ForLaunchOnBoot = Tizen::App::Package::_PackageManagerImpl::GetInstance()->GetFilteredAppIdListN(L"LaunchOnBoot", L"True");
454         if (pAppIdList_ForLaunchOnBoot == null)
455         {
456                 SysLog(NID_APP, "No launch-on-boot entry.");
457                 return true;
458         }
459
460         result r = E_SUCCESS;
461         String* pPackageNameStr = null;
462         char* pPackageName = null;
463
464         for (int i = 0; i < pAppIdList_ForLaunchOnBoot->GetCount(); i++)
465         {
466                 pPackageNameStr = dynamic_cast<String*>(pAppIdList_ForLaunchOnBoot->GetAt(i));
467                 if (pPackageNameStr != null)
468                 {
469                         pPackageName = _StringConverter::CopyToCharArrayN(*pPackageNameStr);
470
471                         SysLog(NID_APP, "'%ls'", pPackageNameStr->GetPointer()) ;
472                         r = _AppControlManager::GetInstance()->LaunchPkg(pPackageName, null, null, null, null, null);
473                         delete[] pPackageName;
474
475                         if (!IsFailed(r))
476                         {
477                                 SysLog(NID_APP, "'%ls'is launched", pPackageNameStr->GetPointer()) ;
478                         }
479                         else
480                         {
481                                 SysLog(NID_APP, "%s", GetErrorMessage(r) );
482                         }
483                 }
484         }
485
486         pAppIdList_ForLaunchOnBoot->RemoveAll(true);
487         delete pAppIdList_ForLaunchOnBoot;
488
489         SysLog(NID_APP, "Exit.");
490         return !IsFailed(r);
491 }
492
493 bool
494 AppService::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
495 {
496         const String tmpPath = _HEARTBEAT_PATH;
497         File::Remove(tmpPath);
498         return true;
499 }
500
501 void
502 AppService::OnLowMemory(void)
503 {
504
505 }
506
507 void
508 AppService::OnBatteryLevelChanged(BatteryLevel batteryLevel)
509 {
510
511 }
512
513 void
514 AppService::OnUserEventReceivedN(RequestId requestId, IList *pArgs)
515 {
516         if ( pArgs == null)
517         {
518                 return;
519         }
520
521         SysLog(NID_APP, "OnUserEventReceivedN() is called. RequestId = %d \n", requestId);
522
523         if (requestId == HANDLER_REQUEST_ALARMID)
524         {
525                 String* pArg = null;
526                 for (int i = 0; (pArg = static_cast<String*>(pArgs->GetAt(i))) != null; i++)
527                 {
528                         int alarmId = -1;
529                         Integer::Parse(*pArg, alarmId);
530                         SysLog(NID_APP, "Alarm event is expired alarm Id is %d", alarmId);
531                         if (__pAlarmService != NULL)
532                         {
533                                 __pAlarmService->OnAlarmExpired(alarmId);
534                         }
535                         else
536                         {
537                                 SysLog(NID_APP, "Alarm Service is not available. Alarm Event could not be announced.");
538                         }
539                 }
540         }
541
542         pArgs->RemoveAll(true);
543 }
544
545 ArrayList*
546 AppService::GetPackageEventArgsN(const AppId& appId)
547 {
548         ArrayList* pArray = new (std::nothrow) ArrayList;
549         SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Array allocation failure.");
550
551         pArray->Construct();
552
553
554         String tmpExecName = Tizen::App::Package::_PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
555         String tmpPackageName = appId + "." + tmpExecName;
556
557         String* pPackageName;
558         String* pAppId;
559         String* pExecName;
560
561         pPackageName = new (std::nothrow) String(tmpPackageName);
562         SysTryCatch(NID_APP, pPackageName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] packageName allocation failure.");
563         pArray->Add(*pPackageName);
564
565         pAppId = new (std::nothrow) String(appId);
566         SysTryCatch(NID_APP, pAppId != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppId %ls allocation failure.", appId.GetPointer());
567         pArray->Add(*pAppId);
568
569 //      pExecName = new (std::nothrow) String(_ContextManager::_Util::GetExecNameFromPackageName(packageName));
570         pExecName = new (std::nothrow) String(tmpExecName);
571         SysTryCatch(NID_APP, pExecName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
572         pArray->Add(*pExecName);
573
574         return pArray;
575
576 CATCH:
577         pArray->RemoveAll(true);
578         delete pArray;
579
580         return null;
581 }
582
583 void
584 AppService::OnPackageInstallationCompleted(const PackageId& packageId, Tizen::App::Package::PackageInstallationResult installationResult)
585 {
586         SysLog(NID_APP, "Enter. packageId(%ls), installationResult(%d)", packageId.GetPointer(), installationResult);
587
588         if (installationResult == Tizen::App::Package::PACKAGE_INSTALLATION_RESULT_SUCCESS)
589         {
590                 ArrayList* pArray = GetPackageEventArgsN(packageId);
591                 SysTryReturnVoidResult(NID_APP, pArray != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] GetPackageEventArgsN failure.");
592
593                 result r = __handlerThread.SendUserEvent(HANDLER_REQUEST_INSTALL_COMPLETE, pArray);
594                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
595         }
596
597         SysLog(NID_APP, "Exit.");
598 }
599
600 void
601 AppService::OnPackageUninstallationCompleted(const PackageId& packageId, bool uninstallationResult)
602 {
603         SysLog(NID_APP, "Enter. packageId(%ls), uninstallationResult(%d)", packageId.GetPointer(), uninstallationResult);
604
605         if (uninstallationResult == true)
606         {
607                 ArrayList* pArray = GetPackageEventArgsN(packageId);
608                 SysTryReturnVoidResult(NID_APP, pArray != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] GetPackageEventArgsN failure.");
609
610                 result r = __handlerThread.SendUserEvent(HANDLER_REQUEST_UNINSTALL_COMPLETE, pArray);
611                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
612         }
613
614         SysLog(NID_APP, "Exit.");
615 }
616
617 void
618 AppService::OnPackageInstallationInProgress(const PackageId& packageId, int progress)
619 {
620 }
621
622 bool
623 ConfirmCalendarService(void)
624 {
625         int count = 0;
626         long interval = 100;
627         int ret = CALENDAR_ERROR_NONE;
628         int WAIT_COUNT = 40;
629
630         while (true)
631         {
632                 int ret = calendar_connect();
633                 if (ret == CALENDAR_ERROR_NONE)
634                 {
635                         calendar_disconnect();
636                         SysLog(NID_APP, "Found calendar service on %dth trial.", count);
637                         return true;
638                 }
639
640                 SysLog(NID_APP, "Waiting for calendar service %dth time(%d msec).", count, interval);
641
642                 if (count >= WAIT_COUNT)
643                 {
644                         SysLog(NID_APP, "Count overflow.");
645                         return false;
646                 }
647
648                 count++;
649                 Thread::Sleep(interval);
650                 interval += 150 * count;
651         }
652
653         return false;
654 }
655
656 void
657 AppService::OnApplicationLaunched(const AppId& appId, Tizen::App::_AppType type)
658 {
659         static bool channelServiceLaunched = false;
660         static bool securityServiceLaunched = false;
661
662         const String CHANNEL_SERVICE_APPID = L"cp7ipabg4k";
663         const String SECURITY_SERVICE_APPID = L"q7097a278m";
664
665         if (appId == CHANNEL_SERVICE_APPID)
666         {
667                 channelServiceLaunched = true;
668         }
669         else if (appId == SECURITY_SERVICE_APPID)
670         {
671                 securityServiceLaunched = true;
672         }
673
674
675         if (securityServiceLaunched && channelServiceLaunched)
676         {
677                 _AppManagerImpl::GetInstance()->RemoveEventListener(*this);
678                 LaunchSystemServices();
679
680                 Tizen::App::Package::_PackageManagerImpl::GetInstance()->AddEventListener(this, 0);
681
682                 ConfirmCalendarService();
683                 vconf_set_int(VCONFKEY_APPSERVICE_STATUS, 2);
684
685                 LaunchUserServices();
686         }
687 }
688
689 void
690 AppService::OnApplicationTerminated(const AppId& appId, Tizen::App::_AppType type)
691 {
692         SysLog(NID_APP, "Do nothing.");
693 }
694
695
696 /*
697  * AppService::_TaskHandlerThread
698  */
699
700 AppService::_TaskHandlerThread::_TaskHandlerThread(AppService& mgr)
701         : __mgr(mgr)
702 {
703 }
704
705 AppService::_TaskHandlerThread::~_TaskHandlerThread(void)
706 {
707 }
708
709 bool
710 AppService::_TaskHandlerThread::OnStart(void)
711 {
712         return true;
713 }
714
715 void
716 AppService::_TaskHandlerThread::OnStop(void)
717 {
718 }
719
720 void
721 AppService::_TaskHandlerThread::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
722 {
723         SysLog(NID_APP, "Enter.");
724         SysTryReturnVoidResult(NID_APP, pArgs != null && pArgs->GetCount() == 3, E_INVALID_STATE, "Wrong argument delivered for install complete event.");
725
726         String* pPackageName= dynamic_cast<String*>( pArgs->GetAt(0) );
727         SysTryReturnVoidResult(NID_APP, pPackageName != null, E_INVALID_STATE, "Invalid packageName for install complete event.");
728
729         String* pAppId = dynamic_cast<String*>( pArgs->GetAt(1) );
730         SysTryReturnVoidResult(NID_APP, pAppId != null, E_INVALID_STATE, "Invalid appId for install complete event.");
731
732         String* pExecName= dynamic_cast<String*>( pArgs->GetAt(2) );
733         SysTryReturnVoidResult(NID_APP, pExecName != null, E_INVALID_STATE, "Invalid execName for install complete event.");
734
735         switch (reqId)
736         {
737         case HANDLER_REQUEST_INSTALL_COMPLETE:
738         {
739                 String isLaunchOnBoot = _ContextManager::_Util::QueryFeatureFromPackageManager(*pAppId, *pExecName, L"LaunchOnBoot" );
740                 if ( isLaunchOnBoot == L"True")
741                 {
742                         String realId = *pAppId + '.' + *pExecName;
743                         result r = _AppManagerImpl::GetInstance()->LaunchApplication(realId, null, AppManager::LAUNCH_OPTION_DEFAULT);
744                         SysTryLog(NID_APP, !IsFailed(r), "%s", GetErrorMessage(r) );
745                         SysLog(NID_APP, "'%ls'is launched", pExecName->GetPointer()) ;
746                 }
747
748                 SysTryReturnVoidResult(NID_APP, __mgr.__pConditionManagerStub != null, E_INVALID_STATE, "Invalid condition manager stub for install complete event.");
749                 __mgr.__pConditionManagerStub->OnInstallComplete(*pAppId, *pExecName, *pPackageName);
750         }
751                 break;
752
753         case HANDLER_REQUEST_UNINSTALL_COMPLETE:
754         {
755                 SysTryReturnVoidResult(NID_APP, __mgr.__pConditionManagerStub != null, E_INVALID_STATE, "Invalid condition manager stub for install complete event.");
756                 __mgr.__pConditionManagerStub->OnUninstallComplete(*pAppId, *pExecName );
757         }
758                 break;
759
760         default:
761                 SysLog(NID_APP, "Wrong request Id for handler thread : 0x%x", reqId);
762                 break;
763         }
764
765         pArgs->RemoveAll(true);
766         delete pArgs;
767
768         SysLog(NID_APP, "Exit.");
769 }
770