2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FApp_AppManagerImpl.cpp
19 * @brief This is the implementation for the _AppManagerImpl class.
25 #include <unique_ptr.h>
29 #include <app_manager.h>
31 #include <FBaseInteger.h>
32 #include <FBaseObject.h>
33 #include <FBaseString.h>
34 #include <FBaseColArrayList.h>
35 #include <FBaseErrors.h>
36 #include <FAppAppControl.h>
37 #include <FAppAppManager.h>
38 #include <FAppSqlDataControl.h>
39 #include <FAppMapDataControl.h>
40 #include <FAppPkgPackageInfo.h>
41 #include <FAppIActiveAppEventListener.h>
42 #include <FBaseSysLog.h>
43 #include <FAppIAppControlListener.h>
45 #include <FBase_StringConverter.h>
46 #include <FBaseRt_LibraryImpl.h>
47 #include <FIo_DataControlResultSetImpl.h>
48 #include <FSys_SystemInfoImpl.h>
50 #include "FApp_AppControlRegistry.h"
51 #include "FApp_AppImpl.h"
52 #include "FApp_AppInfo.h"
53 #include "FApp_AppManagerEventArg.h"
54 #include "FApp_AppManagerImpl.h"
55 #include "FApp_AppManagerProxy.h"
56 #include "FApp_AppMessageImpl.h"
57 #include "FApp_AppControlImpl.h"
58 #include "FApp_ConditionManagerProxy.h"
59 #include "FApp_IAppManagerEventListener.h"
60 #include "FApp_IAppEventListener.h"
61 #include "FApp_MapDataControlImpl.h"
62 #include "FApp_SqlDataControlImpl.h"
63 #include "FAppPkg_PackageManagerImpl.h"
64 #include "FAppPkg_PackageInfoImpl.h"
65 #include "FApp_AppControlManager.h"
66 #include "FApp_TemplateUtil.h"
68 #include "FApp_AppLifecycleEvent.h"
69 #include "FApp_AppLifecycleEventArg.h"
70 #include "FApp_IAppLifecycleEventListener.h"
71 #include "FApp_ActiveWindowManager.h"
73 using namespace Tizen::App::Package;
74 using namespace Tizen::Base;
75 using namespace Tizen::Base::Collection;
76 using namespace Tizen::Base::Runtime;
77 using namespace Tizen::Base::Utility;
78 using namespace Tizen::Io;
83 //const long MAX_APPCONTROL_ARGUMENT = 4096;
84 const long MAX_APPCONTROL_ARGUMENT = 1024;
85 const long MAX_CONDITION_LENGTH = 400;
88 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
91 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
95 while (pEnum->MoveNext() == E_SUCCESS)
97 String* pStr = static_cast<String*>(pEnum->GetCurrent());
102 size += pStr->GetLength();
109 } // anonymous name-space
112 namespace Tizen { namespace App
115 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
116 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
117 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
118 const int _MAX_PACKAGE_ID_LENGTH = 10;
121 _AppManagerImpl::_AppManagerImpl(void)
122 : __pConditionManager(null)
123 , __pActiveWindowManager(new (std::nothrow) _ActiveWindowManager)
129 _AppManagerImpl::~_AppManagerImpl(void)
133 delete __pConditionManager;
139 _AppManagerImpl::Construct(void)
141 __appManagerEvent.Construct();
142 __appLifecycleEvent.Construct();
143 __appListForAppLifecycle.Construct();
144 result r = __mutex.Create();
145 SysTryLog(NID_APP, r == E_SUCCESS, "Creating mutex failed.");
147 _IAppManager* pMgr = _AppManagerProxy::GetService();
148 //todo : uncomment following _SysTryReturn or put assert.
149 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
150 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
151 pMgr->InitEventListener(this);
157 _AppManagerImpl::GetInstance(void)
159 return AppManager::GetInstance()->__pAppManagerImpl;
162 _ConditionManagerProxy*
163 _AppManagerImpl::GetConditionManagerProxy(void)
165 if (__pConditionManager == null)
167 const int MAX_TRY_COUNT = 5;
168 const int TRY_SLEEP_TIME = 250;
170 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
171 SysAssert(__pConditionManager != null);
176 result r = __pConditionManager->Construct();
179 SysLog(NID_APP, "Succeeded in connecting condition manager.");
183 if (count >= MAX_TRY_COUNT)
185 SysLog(NID_APP, "Failed to connecting condition manager.");
190 Thread::Sleep(TRY_SLEEP_TIME);
194 return __pConditionManager;
198 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
200 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
202 AppControl* pAc = null;
204 pAc = pRegs->GetTizenAppControlN(aId, oId);
207 SetLastResult(E_SUCCESS);
211 pAc = pRegs->GetAppControlN(aId, oId);
213 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
214 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
215 SetLastResult(E_SUCCESS);
220 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
221 const String* pUriScheme)
223 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
224 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
227 const String* pMimeType = pDataType;
231 if ((*pDataType)[0] == L'.')
233 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
236 pDataType->SubString(1, ext);
238 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
240 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
242 pMimeType = &mimeType;
244 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
250 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
251 GetErrorMessage(E_INVALID_FORMAT));
254 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
255 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
257 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
261 SetLastResult(E_OBJ_NOT_FOUND);
265 SetLastResult(E_SUCCESS);
266 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
273 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
274 IAppControlListener* pListener)
276 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
281 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
282 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
284 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
285 "At least one of the specified argument must not be null.");
286 if (pDataList != null)
289 argSize = GetTotalSize(*pDataList);
291 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
293 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
294 "The size of pDataList exceeded the limit(%d).",
295 MAX_APPCONTROL_ARGUMENT);
298 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
300 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
301 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
303 _AppMessageImpl::SetOperation(pBundle.get(), operation);
307 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
312 String mimeType = *pDataType;
314 if ((*pDataType)[0] == L'.')
316 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
320 pDataType->SubString(1, ext);
322 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
324 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
326 pMimeType = &mimeType;
328 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
332 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
337 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
340 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
345 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
347 SqlDataControl* pDc = null;
349 String* pAppId = null;
350 String* pAccess = null;
353 // XXX: Need _NativeDataControlRegistry for SLP native app ?
354 // Try to searach SLP data control.
355 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
356 // pDc = pReg->GetNativeSqlDataControlN(providerId);
362 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
363 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
365 pAppId = dynamic_cast< String* >(pList->GetAt(0));
366 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
367 pAccess = dynamic_cast< String* >(pList->GetAt(1));
368 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
371 if (pAppId->StartsWith(L"org.tizen.", 0))
373 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
374 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
375 for (int i = 0; i < 3; ++i)
377 pStrTok->GetNextToken(appId);
380 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
384 appId.Append(*pAppId);
387 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
388 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
394 _AppManagerImpl::GetMapDataControlN(const String& providerId)
396 MapDataControl* pDc = null;
398 String* pAppId = null;
399 String* pAccess = null;
402 // XXX: Need _NativeDataControlRegistry for SLP native app ?
403 // Try to searach SLP data control.
404 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
405 // pDc = pReg->GetNativeMapDataControlN(providerId);
411 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
412 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
414 pAppId = dynamic_cast< String* >(pList->GetAt(0));
415 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
416 pAccess = dynamic_cast< String* >(pList->GetAt(1));
417 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
420 if (pAppId->StartsWith(L"org.tizen.", 0))
422 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
423 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
424 for (int i = 0; i < 3; ++i)
426 pStrTok->GetNextToken(appId);
429 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
433 appId.Append(*pAppId);
436 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
437 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
443 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
445 String pkgId = PackageManager::GetPackageIdByAppId(appId);
446 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
447 "The application is not installed. (app: %ls)", appId.GetPointer());
449 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
450 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
451 "Failed to get _PackageManagerImpl instance.");
453 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
454 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
455 "The application is not installed. (app: %ls)", appId.GetPointer());
457 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
458 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
459 "Failed to get _PackageInfoImpl instance.");
461 appRootPath = pPkgInfoImpl->GetAppRootPath();
462 appRootPath.Append(L"/");
468 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
470 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
471 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
472 "The target application(%ls) is not installed.", appId.GetPointer());
473 // SysTryReturnResult(NID_APP,
474 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
475 // "The length of appid exceeded the limit(%d).",
476 // WIDGET_APP_MAX_APPID_LENGTH);
478 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
479 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
484 argSize = GetTotalSize(*pArguments);
486 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
488 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
489 "The size of pDataList exceeded the limit(%d).",
490 MAX_APPCONTROL_ARGUMENT);
493 result r = pAc->Start(pArguments, null);
495 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
501 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
503 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
505 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
507 int ret = aul_open_app(pName.get());
511 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
516 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
517 "The target application(%ls) is not installed.", appId.GetPointer());
523 r = E_APP_NOT_INSTALLED;
533 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
539 _AppManagerImpl::TerminateApplication(const AppId& appId)
541 _IAppManager* pMgr = _AppManagerProxy::GetService();
542 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
544 return pMgr->TerminateApplication(appId);
549 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
551 const char* pStr = static_cast<const char*>(pData);
553 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
555 aul_terminate_pid(pAppInfo->pid);
556 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
562 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
564 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
565 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
567 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
572 _AppManagerImpl::IsRunning(const AppId& appId) const
574 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
576 return aul_app_is_running(pAppId.get());
580 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
582 ArrayList* pList = static_cast<ArrayList*>(pData);
583 if (pList && pAppInfo && pAppInfo->appid)
585 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
592 _AppManagerImpl::GetRunningAppListN(void) const
594 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
595 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
596 GetErrorMessage(E_OUT_OF_MEMORY));
597 pRunningAppList->Construct();
599 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
600 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
602 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
603 return pRunningAppList;
608 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
609 AppManager::LaunchOption option)
611 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
612 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
614 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
615 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
618 result r = strTok.GetNextToken(key);
619 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
622 if (key == L"Serial")
624 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
626 else if (key == L"NFC")
628 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
630 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
632 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
636 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
638 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
639 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
643 SysTryReturnResult(NID_APP,
644 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
645 "No such a condition.");
648 return pProxy->UnregisterAppLaunch(appId, pCondition);
652 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
654 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
655 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
656 GetErrorMessage(E_INVALID_STATE));
659 return pProxy->IsAppLaunchRegistered(appId, pCondition);
663 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
665 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
669 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
671 SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
673 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
674 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
675 __appManagerEvent.FireAsync(*pArg);
680 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
688 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
690 _IAppManager* pMgr = _AppManagerProxy::GetService();
691 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
693 result r = __appManagerEvent.AddListener(listener);
694 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
696 const int count = __appManagerEvent.GetListenerCount();
697 SysLog(NID_APP, "registered event listener(s) # %d", count);
704 return pMgr->AddEventListener(-1);
708 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
710 _IAppManager* pMgr = _AppManagerProxy::GetService();
711 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
713 result r = __appManagerEvent.RemoveListener(listener);
714 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
716 const int count = __appManagerEvent.GetListenerCount();
717 SysLog(NID_APP, "registered event listener(s) # %d", count);
721 return pMgr->RemoveEventListener(-1);
728 ///////////////////////////////////////////////////////////////////////////////
729 // LifecycleManager begins.
730 ///////////////////////////////////////////////////////////////////////////////
733 _AppLifecycleManager::Init(void)
735 aul_listen_app_launch_signal(LaunchCallback, this);
736 aul_listen_app_dead_signal(TerminateCallback, this);
740 _AppLifecycleManager::Fini(void)
742 aul_listen_app_launch_signal(NULL, NULL);
743 aul_listen_app_dead_signal(NULL, NULL);
747 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
749 if (__pEventList == null)
751 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
752 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
756 __pEventList = pAppEventList.release();
757 SysLog(NID_APP, "Registered app event listener.");
760 return __pEventList->Add(&listener);
764 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
766 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
768 result r = __pEventList->Remove(&listener);
770 if (__pEventList->GetCount() == 0)
782 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
784 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
785 if (pImpl == null || pImpl->__pEventList == null)
787 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
792 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
795 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
799 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
802 const String tmp = appId;
803 pImpl->__map.Add(pid, tmp);
805 while (pEnum->MoveNext() == E_SUCCESS)
807 _IAppEventListener* pListener = null;
808 pEnum->GetCurrent(pListener);
810 pListener->OnApplicationLaunched(tmp, pid);
814 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
820 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
822 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
823 if (pImpl == null || pImpl->__pEventList == null)
825 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
829 // terminate callback cannot acquire appId from pid
831 result r = pImpl->__map.GetValue(pid, tmp);
834 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
838 pImpl->__map.Remove(pid);
840 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
843 while (pEnum->MoveNext() == E_SUCCESS)
845 _IAppEventListener* pListener = null;
846 pEnum->GetCurrent(pListener);
848 pListener->OnApplicationTerminated(tmp, pid);
852 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
857 ///////////////////////////////////////////////////////////////////////////////
858 // LifecycleManager ends.
859 ///////////////////////////////////////////////////////////////////////////////
863 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
865 return __lifeManager.AddListener(listener);
869 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
871 return __lifeManager.RemoveListener(listener);
876 _AppManagerImpl::GetUiLibraryImpl(void)
878 if (__pUiLibrary == null)
880 __pUiLibrary = new (std::nothrow) _LibraryImpl;
881 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
883 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
884 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
887 return *__pUiLibrary;
892 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
894 return __pActiveWindowManager->AddActiveAppEventListener(listener);
899 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
901 return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
906 _AppManagerImpl::GetActiveApp(AppId& appId)
908 return __pActiveWindowManager->GetActiveApp(appId);
913 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
915 _IAppManager* pMgr = _AppManagerProxy::GetService();
916 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
918 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
922 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
924 _IAppManager* pMgr = _AppManagerProxy::GetService();
925 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
927 return pMgr->ClearUserPreferenceForAppControlResolution(appId);
931 _AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
933 return __appLifecycleEvent.AddListener(listener, false);
937 _AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
939 return __appLifecycleEvent.RemoveListener(listener);
943 _AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
945 SysLog(NID_APP, "Enter");
947 result r = __mutex.Acquire();
948 SysTryLog(NID_APP, r == E_SUCCESS, "Acquiring mutex failed.");
950 bool isContained = false;
951 r = __appListForAppLifecycle.ContainsKey(appId, isContained);
953 int currentRefCnt = 0;
956 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
957 r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
961 r = __appListForAppLifecycle.Add(appId, currentRefCnt);
963 _IAppManager* pMgr = _AppManagerProxy::GetService();
964 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
966 r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
967 SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
969 r = __mutex.Release();
970 SysTryLog(NID_APP, r == E_SUCCESS, "Releasing mutex failed.");
972 SysLog(NID_APP, "Exit");
978 _AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
980 SysLog(NID_APP, "Enter");
982 result r = __mutex.Acquire();
983 SysTryLog(NID_APP, r == E_SUCCESS, "Acquiring mutex failed.");
985 bool isContained = false;
986 r = __appListForAppLifecycle.ContainsKey(appId, isContained);
990 int currentRefCnt = 0;
991 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
995 if (currentRefCnt < 0)
997 r = __appListForAppLifecycle.Remove(appId);
999 _IAppManager* pMgr = _AppManagerProxy::GetService();
1000 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
1002 r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
1004 SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
1008 r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
1013 SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
1016 r = __mutex.Release();
1017 SysTryLog(NID_APP, r == E_SUCCESS, "Releasing mutex failed.");
1019 SysLog(NID_APP, "Exit");
1026 _AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
1028 SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
1030 _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
1031 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1033 __appLifecycleEvent.FireAsync(*pArg);