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>
30 #include <FBaseObject.h>
31 #include <FBaseString.h>
32 #include <FBaseColArrayList.h>
33 #include <FBaseErrors.h>
34 #include <FAppAppControl.h>
35 #include <FAppAppManager.h>
36 #include <FAppSqlDataControl.h>
37 #include <FAppMapDataControl.h>
38 #include <FAppPkgPackageInfo.h>
39 #include <FAppIActiveAppEventListener.h>
40 #include <FBaseSysLog.h>
41 #include <FAppIAppControlListener.h>
43 #include <FBase_StringConverter.h>
44 #include <FBaseRt_LibraryImpl.h>
45 #include <FSys_SystemInfoImpl.h>
47 #include "FApp_AppControlRegistry.h"
48 #include "FApp_AppImpl.h"
49 #include "FApp_AppInfo.h"
50 #include "FApp_AppManagerEventArg.h"
51 #include "FApp_AppManagerImpl.h"
52 #include "FApp_AppManagerProxy.h"
53 #include "FApp_AppMessageImpl.h"
54 #include "FApp_AppControlImpl.h"
55 #include "FApp_ConditionManagerProxy.h"
56 #include "FApp_IAppManagerEventListener.h"
57 #include "FApp_IAppEventListener.h"
58 #include "FApp_MapDataControlImpl.h"
59 #include "FApp_SqlDataControlImpl.h"
60 #include "FAppPkg_PackageManagerImpl.h"
61 #include "FAppPkg_PackageInfoImpl.h"
62 #include "FApp_AppControlManager.h"
64 #include "FApp_AppLifecycleEvent.h"
65 #include "FApp_AppLifecycleEventArg.h"
66 #include "FApp_IAppLifecycleEventListener.h"
67 #include "FApp_ActiveWindowManager.h"
69 using namespace Tizen::App::Package;
70 using namespace Tizen::Base;
71 using namespace Tizen::Base::Collection;
72 using namespace Tizen::Base::Runtime;
73 using namespace Tizen::Base::Utility;
74 using namespace Tizen::Io;
79 //const long MAX_APPCONTROL_ARGUMENT = 4096;
80 const long MAX_APPCONTROL_ARGUMENT = 1024;
81 const long MAX_CONDITION_LENGTH = 400;
84 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
87 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
91 while (pEnum->MoveNext() == E_SUCCESS)
93 String* pStr = static_cast<String*>(pEnum->GetCurrent());
98 size += pStr->GetLength();
105 } // anonymous name-space
108 namespace Tizen { namespace App
111 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
112 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
113 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
114 const int _MAX_PACKAGE_ID_LENGTH = 10;
117 _AppManagerImpl::_AppManagerImpl(void)
118 : __pConditionManager(null)
119 , __pActiveWindowManager(new (std::nothrow) _ActiveWindowManager)
125 _AppManagerImpl::~_AppManagerImpl(void)
129 delete __pConditionManager;
135 _AppManagerImpl::Construct(void)
137 __appManagerEvent.Construct();
138 __appLifecycleEvent.Construct();
139 __appListForAppLifecycle.Construct(0, 0, __strHashCodeProvider, __comparer);
140 result r = __mutex.Create();
141 SysTryLog(NID_APP, r == E_SUCCESS, "Creating mutex failed.");
143 _IAppManager* pMgr = _AppManagerProxy::GetService();
144 //todo : uncomment following _SysTryReturn or put assert.
145 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
146 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
147 pMgr->InitEventListener(this);
153 _AppManagerImpl::GetInstance(void)
155 return AppManager::GetInstance()->__pAppManagerImpl;
158 _ConditionManagerProxy*
159 _AppManagerImpl::GetConditionManagerProxy(void)
161 if (__pConditionManager == null)
163 const int MAX_TRY_COUNT = 5;
164 const int TRY_SLEEP_TIME = 250;
166 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
167 SysAssert(__pConditionManager != null);
172 result r = __pConditionManager->Construct();
175 SysLog(NID_APP, "Succeeded in connecting condition manager.");
179 if (count >= MAX_TRY_COUNT)
181 SysLog(NID_APP, "Failed to connecting condition manager.");
186 Thread::Sleep(TRY_SLEEP_TIME);
190 return __pConditionManager;
194 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
196 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
198 AppControl* pAc = null;
200 pAc = pRegs->GetTizenAppControlN(aId, oId);
203 SetLastResult(E_SUCCESS);
207 pAc = pRegs->GetAppControlN(aId, oId);
209 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
210 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
211 SetLastResult(E_SUCCESS);
216 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
217 const String* pUriScheme)
219 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
220 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
223 const String* pMimeType = pDataType;
227 if ((*pDataType)[0] == L'.')
229 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
232 pDataType->SubString(1, ext);
234 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
236 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
238 pMimeType = &mimeType;
240 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
246 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
247 GetErrorMessage(E_INVALID_FORMAT));
250 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
251 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
253 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
257 SetLastResult(E_OBJ_NOT_FOUND);
261 SetLastResult(E_SUCCESS);
262 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
269 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
270 IAppControlListener* pListener)
272 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
277 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
278 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
280 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
281 "At least one of the specified argument must not be null.");
282 if (pDataList != null)
285 argSize = GetTotalSize(*pDataList);
287 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
289 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
290 "The size of pDataList exceeded the limit(%d).",
291 MAX_APPCONTROL_ARGUMENT);
294 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
296 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
297 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
299 _AppMessageImpl::SetOperation(pBundle.get(), operation);
303 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
308 String mimeType = *pDataType;
310 if ((*pDataType)[0] == L'.')
312 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
316 pDataType->SubString(1, ext);
318 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
320 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
322 pMimeType = &mimeType;
324 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
328 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
333 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
336 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
341 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
343 SqlDataControl* pDc = null;
345 String* pAppId = null;
346 String* pAccess = null;
349 // XXX: Need _NativeDataControlRegistry for SLP native app ?
350 // Try to searach SLP data control.
351 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
352 // pDc = pReg->GetNativeSqlDataControlN(providerId);
358 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
359 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
361 pAppId = dynamic_cast< String* >(pList->GetAt(0));
362 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
363 pAccess = dynamic_cast< String* >(pList->GetAt(1));
364 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
367 if (pAppId->StartsWith(L"org.tizen.", 0))
369 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
370 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
371 for (int i = 0; i < 3; ++i)
373 pStrTok->GetNextToken(appId);
376 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
380 appId.Append(*pAppId);
383 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
384 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
390 _AppManagerImpl::GetMapDataControlN(const String& providerId)
392 MapDataControl* pDc = null;
394 String* pAppId = null;
395 String* pAccess = null;
398 // XXX: Need _NativeDataControlRegistry for SLP native app ?
399 // Try to searach SLP data control.
400 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
401 // pDc = pReg->GetNativeMapDataControlN(providerId);
407 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
408 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
410 pAppId = dynamic_cast< String* >(pList->GetAt(0));
411 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
412 pAccess = dynamic_cast< String* >(pList->GetAt(1));
413 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
416 if (pAppId->StartsWith(L"org.tizen.", 0))
418 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
419 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
420 for (int i = 0; i < 3; ++i)
422 pStrTok->GetNextToken(appId);
425 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
429 appId.Append(*pAppId);
432 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
433 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
439 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
441 String pkgId = PackageManager::GetPackageIdByAppId(appId);
442 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
443 "The application is not installed. (app: %ls)", appId.GetPointer());
445 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
446 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
447 "Failed to get _PackageManagerImpl instance.");
449 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
450 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
451 "The application is not installed. (app: %ls)", appId.GetPointer());
453 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
454 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
455 "Failed to get _PackageInfoImpl instance.");
457 appRootPath = pPkgInfoImpl->GetAppRootPath();
458 appRootPath.Append(L"/");
464 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
466 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
467 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
468 "The target application(%ls) is not installed.", appId.GetPointer());
469 // SysTryReturnResult(NID_APP,
470 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
471 // "The length of appid exceeded the limit(%d).",
472 // WIDGET_APP_MAX_APPID_LENGTH);
474 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
475 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
480 argSize = GetTotalSize(*pArguments);
482 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
484 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
485 "The size of pDataList exceeded the limit(%d).",
486 MAX_APPCONTROL_ARGUMENT);
489 result r = pAc->Start(pArguments, null);
491 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
497 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
499 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
501 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
503 int ret = aul_open_app(pName.get());
507 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
512 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
513 "The target application(%ls) is not installed.", appId.GetPointer());
519 r = E_APP_NOT_INSTALLED;
529 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
535 _AppManagerImpl::TerminateApplication(const AppId& appId)
537 _IAppManager* pMgr = _AppManagerProxy::GetService();
538 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
540 return pMgr->TerminateApplication(appId);
545 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
547 const char* pStr = static_cast<const char*>(pData);
549 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
551 aul_terminate_pid(pAppInfo->pid);
552 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
558 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
560 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
561 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
563 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
568 _AppManagerImpl::IsRunning(const AppId& appId) const
570 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
572 return aul_app_is_running(pAppId.get());
576 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
578 ArrayList* pList = static_cast<ArrayList*>(pData);
579 if (pList && pAppInfo && pAppInfo->appid)
581 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
588 _AppManagerImpl::GetRunningAppListN(void) const
590 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
591 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
592 GetErrorMessage(E_OUT_OF_MEMORY));
593 pRunningAppList->Construct();
595 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
596 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
598 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
599 return pRunningAppList;
604 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
605 AppManager::LaunchOption option)
607 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
608 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
610 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
611 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
614 result r = strTok.GetNextToken(key);
615 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
618 if (key == L"Serial")
620 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
622 else if (key == L"NFC")
624 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
626 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
628 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
632 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
634 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
635 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
639 SysTryReturnResult(NID_APP,
640 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
641 "No such a condition.");
644 return pProxy->UnregisterAppLaunch(appId, pCondition);
648 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
650 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
651 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
652 GetErrorMessage(E_INVALID_STATE));
655 return pProxy->IsAppLaunchRegistered(appId, pCondition);
659 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
661 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
665 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
667 SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
669 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
670 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
671 __appManagerEvent.FireAsync(*pArg);
676 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
684 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
686 _IAppManager* pMgr = _AppManagerProxy::GetService();
687 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
689 result r = __appManagerEvent.AddListener(listener);
690 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
692 const int count = __appManagerEvent.GetListenerCount();
693 SysLog(NID_APP, "registered event listener(s) # %d", count);
700 return pMgr->AddEventListener(-1);
704 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
706 _IAppManager* pMgr = _AppManagerProxy::GetService();
707 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
709 result r = __appManagerEvent.RemoveListener(listener);
710 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
712 const int count = __appManagerEvent.GetListenerCount();
713 SysLog(NID_APP, "registered event listener(s) # %d", count);
717 return pMgr->RemoveEventListener(-1);
724 ///////////////////////////////////////////////////////////////////////////////
725 // LifecycleManager begins.
726 ///////////////////////////////////////////////////////////////////////////////
729 _AppLifecycleManager::Init(void)
731 aul_listen_app_launch_signal(LaunchCallback, this);
732 aul_listen_app_dead_signal(TerminateCallback, this);
736 _AppLifecycleManager::Fini(void)
738 aul_listen_app_launch_signal(NULL, NULL);
739 aul_listen_app_dead_signal(NULL, NULL);
743 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
745 if (__pEventList == null)
747 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
748 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
752 __pEventList = pAppEventList.release();
753 SysLog(NID_APP, "Registered app event listener.");
756 return __pEventList->Add(&listener);
760 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
762 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
764 result r = __pEventList->Remove(&listener);
766 if (__pEventList->GetCount() == 0)
778 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
780 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
781 if (pImpl == null || pImpl->__pEventList == null)
783 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
788 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
791 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
795 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
798 const String tmp = appId;
799 pImpl->__map.Add(pid, tmp);
801 while (pEnum->MoveNext() == E_SUCCESS)
803 _IAppEventListener* pListener = null;
804 pEnum->GetCurrent(pListener);
806 pListener->OnApplicationLaunched(tmp, pid);
810 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
816 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
818 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
819 if (pImpl == null || pImpl->__pEventList == null)
821 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
825 // terminate callback cannot acquire appId from pid
827 result r = pImpl->__map.GetValue(pid, tmp);
830 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
834 pImpl->__map.Remove(pid);
836 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
839 while (pEnum->MoveNext() == E_SUCCESS)
841 _IAppEventListener* pListener = null;
842 pEnum->GetCurrent(pListener);
844 pListener->OnApplicationTerminated(tmp, pid);
848 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
853 ///////////////////////////////////////////////////////////////////////////////
854 // LifecycleManager ends.
855 ///////////////////////////////////////////////////////////////////////////////
859 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
861 return __lifeManager.AddListener(listener);
865 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
867 return __lifeManager.RemoveListener(listener);
872 _AppManagerImpl::GetUiLibraryImpl(void)
874 if (__pUiLibrary == null)
876 __pUiLibrary = new (std::nothrow) _LibraryImpl;
877 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
879 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
880 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
883 return *__pUiLibrary;
888 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
890 return __pActiveWindowManager->AddActiveAppEventListener(listener);
895 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
897 return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
902 _AppManagerImpl::GetActiveApp(AppId& appId)
904 return __pActiveWindowManager->GetActiveApp(appId);
909 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
911 _IAppManager* pMgr = _AppManagerProxy::GetService();
912 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
914 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
918 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
920 _IAppManager* pMgr = _AppManagerProxy::GetService();
921 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
923 return pMgr->ClearUserPreferenceForAppControlResolution(appId);
927 _AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
929 return __appLifecycleEvent.AddListener(listener, false);
933 _AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
935 return __appLifecycleEvent.RemoveListener(listener);
939 _AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
941 SysLog(NID_APP, "Enter");
943 result res = __mutex.Acquire();
944 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
946 bool isContained = false;
947 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
949 int currentRefCnt = 0;
952 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
953 r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
957 r = __appListForAppLifecycle.Add(appId, currentRefCnt);
959 _IAppManager* pMgr = _AppManagerProxy::GetService();
960 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
962 r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
963 SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
965 res = __mutex.Release();
966 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
968 SysLog(NID_APP, "Exit");
974 _AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
976 SysLog(NID_APP, "Enter");
978 result res = __mutex.Acquire();
979 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
981 bool isContained = false;
982 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
986 int currentRefCnt = 0;
987 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
991 if (currentRefCnt < 0)
993 r = __appListForAppLifecycle.Remove(appId);
995 _IAppManager* pMgr = _AppManagerProxy::GetService();
996 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
998 r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
1000 SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
1004 r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
1009 SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
1012 res = __mutex.Release();
1013 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
1015 SysLog(NID_APP, "Exit");
1022 _AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
1024 SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
1026 _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
1027 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1029 __appLifecycleEvent.FireAsync(*pArg);