+//
+// Open Service Platform
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <sys/prctl.h>
+#include <signal.h>
+#include <unique_ptr.h>
+
+#include <vconf.h>
+#include <calendar_service.h>
+#include <calendar_types2.h>
+
+#include <FIoFile.h>
+#include <FBaseSysLog.h>
+
+#include <FApp_Aul.h>
+#include <FIo_RegistryImpl.h>
+#include <FSys_EnvironmentImpl.h>
+#include <FBase_StringConverter.h>
+#include <FBaseRt_Process.h>
+
+#include "FApp_AppInfo.h"
+#include "FApp_AppManagerImpl.h"
+#include "FApp_AppControlManager.h"
+#include "FApp_CommunicationDispatcher.h"
+#include "FApp_ContextManager.h"
+#include "FApp_AppManagerStub.h"
+#include "FApp_ConditionManagerStub.h"
+#include "FSys_DeviceManagerService.h"
+#include "FSys_SystemService.h"
+#include "FSys_AlarmService.h"
+#include "FSys_AccessoryManagerService.h"
+#include "FApp_PackageManagerStub.h"
+#include "FApp_NotificationManagerStub.h"
+#include "FAppPkg_PackageManagerImpl.h"
+#include "FIo_MmcStorageManagerStub.h"
+#include "AppService.h"
+
+
+using namespace Tizen::Base;
+using namespace Tizen::Base::Runtime;
+using namespace Tizen::Base::Collection;
+using namespace Tizen::Io;
+using namespace Tizen::System;
+using namespace Tizen::App;
+
+namespace
+{
+static const RequestId HANDLER_REQUEST_INSTALL_COMPLETE = 0;
+static const RequestId HANDLER_REQUEST_UNINSTALL_COMPLETE = 1;
+static const RequestId HANDLER_REQUEST_ID = 2;
+static const RequestId HANDLER_REQUEST_ALARMID = 2;
+
+const char _APP_PATH_FORMAT[] = "/opt/apps/0000000000/bin/%ls";
+const char _HEARTBEAT_PATH[] = "/tmp/osp-initialized";
+#ifndef VCONFKEY_APPSERVICE_STATUS
+#define VCONFKEY_APPSERVICE_STATUS "memory/appservice/status"
+#endif
+
+}
+
+
+AppService::AppService()
+: __pCommunicationDispatcher(null)
+, __pContextMgr(null)
+, __pAppManagerStub(null)
+, __pConditionManagerStub(null)
+, __handlerThread(*this)
+, __pDeviceManagerService(null)
+, __pSystemService(null)
+, __pAccessoryManagerService(null)
+, __pAlarmService(null)
+, __pPackageManagerStub(null)
+, __pNotificationManagerStub(null)
+, __pMmcStorageManagerStub(null)
+{
+ SysLog(NID_APP, "Enter.");
+
+ int pid = getpid();
+ setpgid(pid, pid);
+
+ InitializeIpc();
+
+ SysLog(NID_APP, "Exit.");
+}
+
+AppService::~AppService()
+{
+ SysLog(NID_APP, "Enter.");
+
+ vconf_set_int(VCONFKEY_APPSERVICE_STATUS, -1);
+
+ Tizen::App::Package::_PackageManagerImpl::GetInstance()->RemoveEventListener(this);
+
+ delete __pAppManagerStub;
+ delete __pConditionManagerStub;
+ delete __pPackageManagerStub;
+ delete __pNotificationManagerStub;
+ delete __pMmcStorageManagerStub;
+
+ delete __pContextMgr;
+
+ SysLog(NID_APP, "Exit.");
+}
+
+Service*
+AppService::CreateInstance(void)
+{
+ static AppService* pAppService = new (std::nothrow) AppService();
+
+ return pAppService;
+}
+
+bool
+AppService::OnAppInitializing(AppRegistry& appRegistry)
+{
+ SysLog(NID_APP, "Enter.");
+
+ result r = __handlerThread.Construct(THREAD_TYPE_EVENT_DRIVEN);
+ SysTryReturn(NID_APP, IsFailed(r) == false, false, r, "[%s] Event thread creation failure.", GetErrorMessage(r));
+
+ r = __handlerThread.Start();
+ SysTryReturn(NID_APP, IsFailed(r) == false, false, r, "[%s] Event thread Start failure.", GetErrorMessage(r));
+
+ SysLog(NID_APP, "Exit.");
+ return true;
+}
+
+bool
+AppService::OnAppInitialized(void)
+{
+ SysLog(NID_APP, "Enter.");
+
+ _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
+
+ result r = _AppManagerImpl::GetInstance()->AddEventListener(*this);
+ SysAssertf(!IsFailed(r), "[%s] Failed to add event listener.", GetErrorMessage(r));
+
+ LaunchSecurityService();
+ LaunchChannelService();
+// LaunchSystemServices();
+// LaunchUserServices();
+//
+
+ InitializeServices();
+
+ const String tmpPath = _HEARTBEAT_PATH;
+ File file;
+ (void) file.Construct(tmpPath, "w");
+
+ vconf_set_int(VCONFKEY_APPSERVICE_STATUS, 1);
+ SysLog(NID_APP, "Exit.");
+ return true;
+}
+
+void
+AppService::OnPowerOffNotiReceived(void* pData)
+{
+ SysLog(NID_APP, "'power_off_start' noti received from system-server with 0x%x.", pData);
+
+ AppService* pAppService = static_cast<AppService*>(pData);
+ if (pAppService)
+ {
+ SysLog(NID_APP, "osp-app-service is terminating now.");
+ pAppService->Terminate();
+ }
+}
+
+
+bool
+AppService::InitializeIpc(void)
+{
+ SysLog(NID_APP, "Enter.");
+ result r = E_SUCCESS;
+
+ __pContextMgr = new (std::nothrow) _ContextManager();
+ SysAssert(__pContextMgr != null);
+ SysTryCatch(NID_APP, __pContextMgr != null,, E_OUT_OF_MEMORY, "failed to _ContextManager creation.");
+
+ r = __pContextMgr->Construct();
+ SysAssert(!IsFailed(r));
+ SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
+
+ __pAppManagerStub = new (std::nothrow) _AppManagerStub();
+ SysAssert(__pAppManagerStub != null);
+ SysTryCatch(NID_APP, __pAppManagerStub != null,, E_OUT_OF_MEMORY, "failed to _AppManagerStub creation.");
+
+ r = __pAppManagerStub->Construct(__pContextMgr);
+ SysAssert(!IsFailed(r));
+ SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
+
+ __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
+ SysAssert(__pCommunicationDispatcher != null);
+ SysTryCatch(NID_APP, __pCommunicationDispatcher != null,, E_OUT_OF_MEMORY, "failed to _CommunicationDispatcher creation.");
+
+ SysLog(NID_APP, "Exit.");
+ return true;
+
+CATCH:
+ SysLog(NID_APP, "Catch.");
+
+ delete __pAppManagerStub;
+ __pAppManagerStub = null;
+
+ delete __pContextMgr;
+ __pContextMgr = null;
+
+ SysAssertf(false, "IPC services initializing is failed.");
+ return false;
+}
+
+bool
+AppService::InitializeServices(void)
+{
+ SysLog(NID_APP, "Enter.");
+ result r = E_SUCCESS;
+
+ __pConditionManagerStub = new (std::nothrow) _ConditionManagerStub();
+ SysAssert(__pConditionManagerStub != null);
+ SysTryCatch(NID_APP, __pConditionManagerStub != null,, E_OUT_OF_MEMORY, "failed to _ConditionManagerStub creation.");
+
+ r = __pConditionManagerStub->Construct();
+ SysAssert(!IsFailed(r));
+ SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
+
+ __pDeviceManagerService = _DeviceManagerService::GetInstance();
+ SysAssert( __pDeviceManagerService != null);
+ SysTryCatch(NID_APP, __pDeviceManagerService != null,, E_OUT_OF_MEMORY, "failed to _DeviceManagerService creation.");
+
+ __pSystemService = _SystemService::GetInstance();
+ SysAssert( __pSystemService != null);
+ SysTryCatch(NID_APP, __pSystemService != null,, E_OUT_OF_MEMORY, "failed to _SystemService creation.");
+
+ __pAlarmService = _AlarmService::GetInstance();
+ SysAssert( __pAlarmService != null);
+ SysTryCatch(NID_APP, __pAlarmService != null,, E_OUT_OF_MEMORY, "failed to _pAlarmService creation.");
+
+ __pAccessoryManagerService = _AccessoryManagerService::GetInstance();
+ SysAssert( __pAccessoryManagerService != null);
+ SysTryCatch(NID_APP, __pAccessoryManagerService != null,, E_OUT_OF_MEMORY, "failed to _AccessoryManagerService creation.");
+
+ __pPackageManagerStub = new (std::nothrow) _PackageManagerStub();
+ SysAssert(__pPackageManagerStub != null);
+ SysTryCatch(NID_APP, __pPackageManagerStub != null,, E_OUT_OF_MEMORY, "failed to _PackageManagerStub creation.");
+
+ r = __pPackageManagerStub->Construct();
+ SysAssert(!IsFailed(r));
+ SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
+
+ __pNotificationManagerStub = new (std::nothrow) _NotificationManagerStub();
+ SysAssert(__pNotificationManagerStub != null);
+ SysTryCatch(NID_APP, __pNotificationManagerStub != null,, E_OUT_OF_MEMORY, "failed to _pNotificationManagerStub creation.");
+
+ r = __pNotificationManagerStub->Construct();
+ SysAssert(!IsFailed(r));
+ SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
+
+ __pMmcStorageManagerStub = new (std::nothrow) _MmcStorageManagerStub();
+ SysAssert(__pMmcStorageManagerStub != null);
+ SysTryCatch(NID_APP, __pMmcStorageManagerStub != null,, E_OUT_OF_MEMORY, "failed to __pMmcStorageManagerStub creation.");
+
+ r = __pMmcStorageManagerStub->Construct();
+ SysAssert(!IsFailed(r));
+ SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
+
+ SysLog(NID_APP, "Exit.");
+ return true;
+
+CATCH:
+ SysLog(NID_APP, "Catch.");
+
+ delete __pConditionManagerStub;
+ __pConditionManagerStub = null;
+
+ delete __pPackageManagerStub;
+ __pPackageManagerStub = null;
+
+ delete __pNotificationManagerStub;
+ __pNotificationManagerStub = null;
+
+ delete __pMmcStorageManagerStub;
+ __pNotificationManagerStub = null;
+
+ SysAssertf(false, "System services initializing is failed.");
+ return false;
+}
+
+result
+AppService::SetOomAdj(int pid, int adj)
+{
+ // set oom_adj to -17 for system service
+ result r = E_SUCCESS;
+ char buf[FILENAME_MAX];
+ FILE *fP = NULL;
+
+ snprintf(buf, FILENAME_MAX, "/proc/%d/oom_adj", pid);
+ fP = fopen(buf, "w");
+ SysTryReturnResult(NID_APP, fP != NULL, E_SYSTEM, "oom_adj change failed with %s.", strerror(errno));
+
+ fprintf(fP, "%d", adj);
+ fclose(fP);
+
+ return r;
+}
+
+int
+AppService::CreateProcess(const String& appId)
+{
+ SysTryReturn(NID_APP, appId.GetLength() > 10 && appId[10] == L'.', -1, E_INVALID_ARG, "[E_INVALID_ARG] Wrong appId %ls.", appId.GetPointer());
+ int pid = fork();
+
+ if (pid == -1)
+ {
+ return -1;
+ }
+ else if (pid == 0)
+ {
+ char path[FILENAME_MAX];
+ memset(path, '\0', FILENAME_MAX);
+
+ snprintf(path, FILENAME_MAX, _APP_PATH_FORMAT, appId.GetPointer() + 11);
+
+ int ret = wcstombs(path + strlen("/opt/apps/"), appId.GetPointer(), 10);
+ if (ret == -1)
+ {
+ SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
+ _Process::Exit(1);
+ }
+
+ SysLog(NID_APP, "Launching %s.", path);
+
+ int currentPid = getpid();
+
+ SetOomAdj(currentPid, -17); // set oom_adj to -17 for system service
+
+ prctl(PR_SET_PDEATHSIG, SIGTERM);
+
+ setpgid(currentPid, currentPid);
+
+ ret = execl(path, path, static_cast<char*>(NULL));
+ if (ret < 0)
+ {
+ SysLogException(NID_APP, E_SYSTEM, "Launching service (%ls)(%s) failed with [%s].", appId.GetPointer(), path, strerror(errno));
+ _Process::Exit(1);
+ }
+ }
+
+ return pid;
+}
+
+bool
+AppService::LaunchSystemServices(void)
+{
+ const wchar_t systemServiceRegPath[] = L"res/system-services.ini";
+ const String regKeyAppId = L"AppId";
+ const String regKeyPriority = L"Priority";
+
+ SysLog(NID_APP, "Enter.");
+ _RegistryImpl reg;
+ const String& systemListPath = _AppInfo::GetAppRootPath() + systemServiceRegPath;
+
+ result r = reg.Construct(systemListPath, REG_OPEN_READ_ONLY, null);
+ // SysTryReturn(NID_APP, !IsFailed(r), r, r, "");
+ if (IsFailed(r))
+ {
+ SysLog(NID_APP, "failed to open registry '%ls'", systemListPath.GetPointer());
+ return true;
+ }
+
+ IList *pSections = null;
+ reg.GetSectionListN(&pSections);
+ SysTryReturn(NID_APP, pSections != null, false, E_SYSTEM, "[E_SYSTEM] GetSectionListN fails (%ls) ", systemListPath.GetPointer() );
+
+ String* pSecName = null;
+ String appIdValue;
+ String priorityValue;
+ std::unique_ptr<IEnumerator> pEnum(pSections->GetEnumeratorN());
+ SysTryCatch(NID_APP, pEnum != null, , E_SYSTEM, "[E_SYSTEM] pSections->GetEnumeratorN fails (%ls)", systemListPath.GetPointer() );
+
+ while (pEnum->MoveNext() == E_SUCCESS)
+ {
+ pSecName = static_cast<String*>(pEnum->GetCurrent());
+ if ( pSecName != null)
+ {
+ priorityValue = L"";
+ r = reg.GetValue(*pSecName, regKeyPriority, priorityValue);
+ if (!IsFailed(r) && priorityValue == L"High")
+ {
+ // channel and security service are launched by code already.
+ continue;
+ }
+
+ reg.GetValue(*pSecName, regKeyAppId, appIdValue);
+
+ int pid = CreateProcess(appIdValue);
+ if (pid != -1)
+ {
+ SysLog(NID_APP, "System service(%ls) is launched as pid(%d).", appIdValue.GetPointer(), pid);
+// __pContextMgr->Register(appIdValue, executableNameValue, pid, pid, true);
+ }
+ else
+ {
+ SysLog(NID_APP, "Failed to launch System service(%ls).", appIdValue.GetPointer());
+ }
+ }
+ }
+
+
+ pSections->RemoveAll(true);
+ delete pSections;
+
+ SysLog(NID_APP, "Exit.");
+ return true;
+
+CATCH:
+ pSections->RemoveAll(true);
+ delete pSections;
+ return false;
+}
+
+bool
+AppService::LaunchChannelService(void)
+{
+ SysLog(NID_APP, "Launching channel service...");
+ return CreateProcess(L"cp7ipabg4k.osp-channel-service" ) == E_SUCCESS;
+}
+
+bool
+AppService::LaunchSecurityService(void)
+{
+ SysLog(NID_APP, "Launching security service...");
+ return CreateProcess(L"q7097a278m.osp-security-service" ) == E_SUCCESS;
+}
+
+bool
+AppService::LaunchUserServices(void)
+{
+ SysLog(NID_APP, "Enter.");
+
+ IList* pAppIdList_ForLaunchOnBoot = Tizen::App::Package::_PackageManagerImpl::GetInstance()->GetFilteredAppIdListN(L"LaunchOnBoot", L"True");
+ if (pAppIdList_ForLaunchOnBoot == null)
+ {
+ SysLog(NID_APP, "No launch-on-boot entry.");
+ return true;
+ }
+
+ result r = E_SUCCESS;
+ String* pPackageNameStr = null;
+ char* pPackageName = null;
+
+ for (int i = 0; i < pAppIdList_ForLaunchOnBoot->GetCount(); i++)
+ {
+ pPackageNameStr = dynamic_cast<String*>(pAppIdList_ForLaunchOnBoot->GetAt(i));
+ if (pPackageNameStr != null)
+ {
+ pPackageName = _StringConverter::CopyToCharArrayN(*pPackageNameStr);
+
+ SysLog(NID_APP, "'%ls'", pPackageNameStr->GetPointer()) ;
+ r = _AppControlManager::GetInstance()->LaunchPkg(pPackageName, null, null, null, null, null);
+ delete[] pPackageName;
+
+ if (!IsFailed(r))
+ {
+ SysLog(NID_APP, "'%ls'is launched", pPackageNameStr->GetPointer()) ;
+ }
+ else
+ {
+ SysLog(NID_APP, "%s", GetErrorMessage(r) );
+ }
+ }
+ }
+
+ pAppIdList_ForLaunchOnBoot->RemoveAll(true);
+ delete pAppIdList_ForLaunchOnBoot;
+
+ SysLog(NID_APP, "Exit.");
+ return !IsFailed(r);
+}
+
+bool
+AppService::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
+{
+ const String tmpPath = _HEARTBEAT_PATH;
+ File::Remove(tmpPath);
+ return true;
+}
+
+void
+AppService::OnLowMemory(void)
+{
+
+}
+
+void
+AppService::OnBatteryLevelChanged(BatteryLevel batteryLevel)
+{
+
+}
+
+void
+AppService::OnUserEventReceivedN(RequestId requestId, IList *pArgs)
+{
+ if ( pArgs == null)
+ {
+ return;
+ }
+
+ SysLog(NID_APP, "OnUserEventReceivedN() is called. RequestId = %d \n", requestId);
+
+ if (requestId == HANDLER_REQUEST_ALARMID)
+ {
+ String* pArg = null;
+ for (int i = 0; (pArg = static_cast<String*>(pArgs->GetAt(i))) != null; i++)
+ {
+ int alarmId = -1;
+ Integer::Parse(*pArg, alarmId);
+ SysLog(NID_APP, "Alarm event is expired alarm Id is %d", alarmId);
+ if (__pAlarmService != NULL)
+ {
+ __pAlarmService->OnAlarmExpired(alarmId);
+ }
+ else
+ {
+ SysLog(NID_APP, "Alarm Service is not available. Alarm Event could not be announced.");
+ }
+ }
+ }
+
+ pArgs->RemoveAll(true);
+}
+
+ArrayList*
+AppService::GetPackageEventArgsN(const AppId& appId)
+{
+ ArrayList* pArray = new (std::nothrow) ArrayList;
+ SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Array allocation failure.");
+
+ pArray->Construct();
+
+
+ String tmpExecName = Tizen::App::Package::_PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
+ String tmpPackageName = appId + "." + tmpExecName;
+
+ String* pPackageName;
+ String* pAppId;
+ String* pExecName;
+
+ pPackageName = new (std::nothrow) String(tmpPackageName);
+ SysTryCatch(NID_APP, pPackageName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] packageName allocation failure.");
+ pArray->Add(*pPackageName);
+
+ pAppId = new (std::nothrow) String(appId);
+ SysTryCatch(NID_APP, pAppId != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppId %ls allocation failure.", appId.GetPointer());
+ pArray->Add(*pAppId);
+
+// pExecName = new (std::nothrow) String(_ContextManager::_Util::GetExecNameFromPackageName(packageName));
+ pExecName = new (std::nothrow) String(tmpExecName);
+ SysTryCatch(NID_APP, pExecName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
+ pArray->Add(*pExecName);
+
+ return pArray;
+
+CATCH:
+ pArray->RemoveAll(true);
+ delete pArray;
+
+ return null;
+}
+
+void
+AppService::OnPackageInstallationCompleted(const PackageId& packageId, Tizen::App::Package::PackageInstallationResult installationResult)
+{
+ SysLog(NID_APP, "Enter. packageId(%ls), installationResult(%d)", packageId.GetPointer(), installationResult);
+
+ if (installationResult == Tizen::App::Package::PACKAGE_INSTALLATION_RESULT_SUCCESS)
+ {
+ ArrayList* pArray = GetPackageEventArgsN(packageId);
+ SysTryReturnVoidResult(NID_APP, pArray != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] GetPackageEventArgsN failure.");
+
+ result r = __handlerThread.SendUserEvent(HANDLER_REQUEST_INSTALL_COMPLETE, pArray);
+ SysTryLog(NID_APP, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
+ }
+
+ SysLog(NID_APP, "Exit.");
+}
+
+void
+AppService::OnPackageUninstallationCompleted(const PackageId& packageId, bool uninstallationResult)
+{
+ SysLog(NID_APP, "Enter. packageId(%ls), uninstallationResult(%d)", packageId.GetPointer(), uninstallationResult);
+
+ if (uninstallationResult == true)
+ {
+ ArrayList* pArray = GetPackageEventArgsN(packageId);
+ SysTryReturnVoidResult(NID_APP, pArray != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] GetPackageEventArgsN failure.");
+
+ result r = __handlerThread.SendUserEvent(HANDLER_REQUEST_UNINSTALL_COMPLETE, pArray);
+ SysTryLog(NID_APP, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
+ }
+
+ SysLog(NID_APP, "Exit.");
+}
+
+void
+AppService::OnPackageInstallationInProgress(const PackageId& packageId, int progress)
+{
+}
+
+bool
+ConfirmCalendarService(void)
+{
+ int count = 0;
+ long interval = 100;
+ int ret = CALENDAR_ERROR_NONE;
+ int WAIT_COUNT = 40;
+
+ while (true)
+ {
+ int ret = calendar_connect();
+ if (ret == CALENDAR_ERROR_NONE)
+ {
+ calendar_disconnect();
+ SysLog(NID_APP, "Found calendar service on %dth trial.", count);
+ return true;
+ }
+
+ SysLog(NID_APP, "Waiting for calendar service %dth time(%d msec).", count, interval);
+
+ if (count >= WAIT_COUNT)
+ {
+ SysLog(NID_APP, "Count overflow.");
+ return false;
+ }
+
+ count++;
+ Thread::Sleep(interval);
+ interval += 150 * count;
+ }
+
+ return false;
+}
+
+void
+AppService::OnApplicationLaunched(const AppId& appId, Tizen::App::_AppType type)
+{
+ static bool channelServiceLaunched = false;
+ static bool securityServiceLaunched = false;
+
+ const String CHANNEL_SERVICE_APPID = L"cp7ipabg4k";
+ const String SECURITY_SERVICE_APPID = L"q7097a278m";
+
+ if (appId == CHANNEL_SERVICE_APPID)
+ {
+ channelServiceLaunched = true;
+ }
+ else if (appId == SECURITY_SERVICE_APPID)
+ {
+ securityServiceLaunched = true;
+ }
+
+
+ if (securityServiceLaunched && channelServiceLaunched)
+ {
+ _AppManagerImpl::GetInstance()->RemoveEventListener(*this);
+ LaunchSystemServices();
+
+ Tizen::App::Package::_PackageManagerImpl::GetInstance()->AddEventListener(this, 0);
+
+ ConfirmCalendarService();
+ vconf_set_int(VCONFKEY_APPSERVICE_STATUS, 2);
+
+ LaunchUserServices();
+ }
+}
+
+void
+AppService::OnApplicationTerminated(const AppId& appId, Tizen::App::_AppType type)
+{
+ SysLog(NID_APP, "Do nothing.");
+}
+
+
+/*
+ * AppService::_TaskHandlerThread
+ */
+
+AppService::_TaskHandlerThread::_TaskHandlerThread(AppService& mgr)
+ : __mgr(mgr)
+{
+}
+
+AppService::_TaskHandlerThread::~_TaskHandlerThread(void)
+{
+}
+
+bool
+AppService::_TaskHandlerThread::OnStart(void)
+{
+ return true;
+}
+
+void
+AppService::_TaskHandlerThread::OnStop(void)
+{
+}
+
+void
+AppService::_TaskHandlerThread::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
+{
+ SysLog(NID_APP, "Enter.");
+ SysTryReturnVoidResult(NID_APP, pArgs != null && pArgs->GetCount() == 3, E_INVALID_STATE, "Wrong argument delivered for install complete event.");
+
+ String* pPackageName= dynamic_cast<String*>( pArgs->GetAt(0) );
+ SysTryReturnVoidResult(NID_APP, pPackageName != null, E_INVALID_STATE, "Invalid packageName for install complete event.");
+
+ String* pAppId = dynamic_cast<String*>( pArgs->GetAt(1) );
+ SysTryReturnVoidResult(NID_APP, pAppId != null, E_INVALID_STATE, "Invalid appId for install complete event.");
+
+ String* pExecName= dynamic_cast<String*>( pArgs->GetAt(2) );
+ SysTryReturnVoidResult(NID_APP, pExecName != null, E_INVALID_STATE, "Invalid execName for install complete event.");
+
+ switch (reqId)
+ {
+ case HANDLER_REQUEST_INSTALL_COMPLETE:
+ {
+ String isLaunchOnBoot = _ContextManager::_Util::QueryFeatureFromPackageManager(*pAppId, *pExecName, L"LaunchOnBoot" );
+ if ( isLaunchOnBoot == L"True")
+ {
+ String realId = *pAppId + '.' + *pExecName;
+ result r = _AppManagerImpl::GetInstance()->LaunchApplication(realId, null, AppManager::LAUNCH_OPTION_DEFAULT);
+ SysTryLog(NID_APP, !IsFailed(r), "%s", GetErrorMessage(r) );
+ SysLog(NID_APP, "'%ls'is launched", pExecName->GetPointer()) ;
+ }
+
+ SysTryReturnVoidResult(NID_APP, __mgr.__pConditionManagerStub != null, E_INVALID_STATE, "Invalid condition manager stub for install complete event.");
+ __mgr.__pConditionManagerStub->OnInstallComplete(*pAppId, *pExecName, *pPackageName);
+ }
+ break;
+
+ case HANDLER_REQUEST_UNINSTALL_COMPLETE:
+ {
+ SysTryReturnVoidResult(NID_APP, __mgr.__pConditionManagerStub != null, E_INVALID_STATE, "Invalid condition manager stub for install complete event.");
+ __mgr.__pConditionManagerStub->OnUninstallComplete(*pAppId, *pExecName );
+ }
+ break;
+
+ default:
+ SysLog(NID_APP, "Wrong request Id for handler thread : 0x%x", reqId);
+ break;
+ }
+
+ pArgs->RemoveAll(true);
+ delete pArgs;
+
+ SysLog(NID_APP, "Exit.");
+}
+