Sync with tizen_2.2.1 appfw spec
[platform/framework/native/appfw.git] / src / app / FApp_AppManagerImpl.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 9151e72..1fab392
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
 
 #include <aul/aul.h>
 #include <bundle.h>
-#include <app_manager.h>
-#include <Ecore.h>
-#include <Ecore_X.h>
-#include <X11/Xlib.h>
 
-#include <FBaseInteger.h>
 #include <FBaseObject.h>
 #include <FBaseString.h>
 #include <FBaseColArrayList.h>
 
 #include <FBase_StringConverter.h>
 #include <FBaseRt_LibraryImpl.h>
-#include <FIo_DataControlResultSetImpl.h>
+#include <FSys_SystemInfoImpl.h>
 
 #include "FApp_AppControlRegistry.h"
 #include "FApp_AppImpl.h"
 #include "FApp_AppInfo.h"
-#include "FApp_AppManagerEventArg.h"
 #include "FApp_AppManagerImpl.h"
 #include "FApp_AppManagerProxy.h"
 #include "FApp_AppMessageImpl.h"
 #include "FApp_AppControlImpl.h"
 #include "FApp_ConditionManagerProxy.h"
-#include "FApp_IAppManagerEventListener.h"
 #include "FApp_IAppEventListener.h"
 #include "FApp_MapDataControlImpl.h"
 #include "FApp_SqlDataControlImpl.h"
 #include "FAppPkg_PackageManagerImpl.h"
 #include "FAppPkg_PackageInfoImpl.h"
 #include "FApp_AppControlManager.h"
+#include "FApp_TemplateUtil.h"
 #include "FApp_Aul.h"
-#include "FSys_SystemInfoImpl.h"
+#include "FApp_AppLifecycleEvent.h"
+#include "FApp_AppLifecycleEventArg.h"
+#include "FApp_IAppLifecycleEventListener.h"
+#include "FApp_ActiveWindowManager.h"
 
 using namespace Tizen::App::Package;
 using namespace Tizen::Base;
@@ -76,8 +72,6 @@ using namespace Tizen::Base::Runtime;
 using namespace Tizen::Base::Utility;
 using namespace Tizen::Io;
 
-//extern const char* _DATACONTROL_RESULTSET_DIR;
-
 namespace
 {
 
@@ -85,35 +79,6 @@ namespace
 const long MAX_APPCONTROL_ARGUMENT = 1024;
 const long MAX_CONDITION_LENGTH = 400;
 
-// ActiveWindow related function and variable from shared library.
-// libecore_x.so.1
-static Ecore_X_Atom(* p_ecore_x_atom_get)(const char* name) = null;
-static int(* p_ecore_x_window_prop_window_get)(Ecore_X_Window win, Ecore_X_Atom atom, Ecore_X_Window* val, unsigned int len) = null;
-static Eina_Bool(* p_ecore_x_netwm_pid_get)(Ecore_X_Window win, int* pid) = null;
-static int(* p_ecore_x_netwm_name_get)(Ecore_X_Window win, char** name) = null;
-static Ecore_X_Window* (* p_ecore_x_window_root_list)(int* num_ret) = null;
-static int* p_ECORE_X_EVENT_WINDOW_PROPERTY = null;
-// libecore.so.1
-static Ecore_Event_Handler* (* p_ecore_event_handler_add)(int type, Ecore_Event_Handler_Cb func, const void* data) = null;
-static void* (* p_ecore_event_handler_del)(Ecore_Event_Handler* event_handler) = null;
-// libX11.so
-static int (* p_XSelectInput)(Display* display, Window w, long event_mask) = null;
-static Display* (* p_XOpenDisplay)(_Xconst char* display_name) = null;
-static int (* p_XCloseDisplay)(Display* display) = null;
-
-Ecore_Event_Handler* pWindowPropertyChanged = null;
-
-struct _DisplayDeleter
-{
-       void operator()(Display* pDisplay)
-       {
-               if (p_XOpenDisplay)
-               {
-                       p_XCloseDisplay(pDisplay);
-               }
-       }
-};
-
 int
 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
 {
@@ -136,81 +101,6 @@ GetTotalSize(const Tizen::Base::Collection::ICollection& col)
        return size;
 }
 
-Eina_Bool
-OnPropertyChanged(void* pData, int type, void* pEvent)
-{
-       using namespace Tizen::App;
-
-       if (p_ecore_x_atom_get == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, EINA_FALSE,
-                                                  "A system error has been occurred. Failed to get ecore_x_atom_get.");
-       }
-       if (p_ecore_x_window_prop_window_get == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_window_prop_window_get = reinterpret_cast<int(*)(Ecore_X_Window win, Ecore_X_Atom atom, Ecore_X_Window* val, unsigned int len)>(lib.GetProcAddress(L"ecore_x_window_prop_window_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
-                                                  "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
-       }
-       if (p_ecore_x_netwm_pid_get == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
-                                                  "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
-       }
-       if (p_ecore_x_netwm_name_get == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_netwm_name_get = reinterpret_cast<int(*)(Ecore_X_Window win, char** name)>(lib.GetProcAddress(L"ecore_x_netwm_name_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_netwm_name_get != null, EINA_FALSE,
-                                                  "A system error has been occurred. Failed to get ecore_x_netwm_name_get.");
-       }
-
-       if (!pData)
-       {
-               SysLog(NID_UI, "The data is not valid.");
-               return EINA_FALSE;
-       }
-       if (!pEvent)
-       {
-               SysLog(NID_UI, "The event is not valid.");
-               return EINA_FALSE;
-       }
-
-       Ecore_X_Event_Window_Property* pE = (Ecore_X_Event_Window_Property*) pEvent;
-       Ecore_X_Atom atom = pE->atom;
-       Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
-
-       if (atom != activeAtom)
-       {
-               return ECORE_CALLBACK_PASS_ON;
-       }
-
-       Ecore_X_Window win = pE->win;
-       Ecore_X_Window activeWin = 0;
-       p_ecore_x_window_prop_window_get(win, activeAtom, &activeWin, 1);
-
-       int pid = 0;
-       p_ecore_x_netwm_pid_get(activeWin, &pid);
-
-       char* pAppName = null;
-       p_ecore_x_netwm_name_get(activeWin, &pAppName);
-
-       Tizen::App::_AppManagerImpl* pAppManagerImpl = static_cast<Tizen::App::_AppManagerImpl*>(pData);
-       pAppManagerImpl->FireActiveAppEvent(activeWin, pid, pAppName);
-
-       if (pAppName)
-       {
-               free(pAppName);
-       }
-
-       return ECORE_CALLBACK_PASS_ON;
-}
-
 } // anonymous name-space
 
 
@@ -220,19 +110,13 @@ namespace Tizen { namespace App
 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
-const wchar_t OSP_ECORE_X_SONAME[] = L"libecore_x.so.1";
-const wchar_t OSP_ECORE_SONAME[] = L"libecore.so.1";
-const wchar_t OSP_X11_SONAME[] = L"libX11.so.6";
 const int _MAX_PACKAGE_ID_LENGTH = 10;
 
-_LibraryImpl* _AppManagerImpl::__pEcoreLibrary = null;
-_LibraryImpl* _AppManagerImpl::__pEcoreXLibrary = null;
 
 _AppManagerImpl::_AppManagerImpl(void)
        : __pConditionManager(null)
-       , __eventListenerCount(0)
+       , __pActiveWindowManager(new (std::nothrow) _ActiveWindowManager)
        , __pUiLibrary(null)
-       , __pX11Library(null)
 {
        SysLog(NID_APP, "");
 }
@@ -241,37 +125,18 @@ _AppManagerImpl::~_AppManagerImpl(void)
 {
        SysLog(NID_APP, "");
 
-       if (__activeAppEventListenerList.GetCount() > 0)
-       {
-               if (p_ecore_event_handler_del == null)
-               {
-                       _LibraryImpl& lib = GetEcoreLibraryImpl();
-                       p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
-               }
-               if (p_ecore_event_handler_del)
-               {
-                       if (pWindowPropertyChanged)
-                       {
-                               p_ecore_event_handler_del(pWindowPropertyChanged);
-                               pWindowPropertyChanged = null;
-                       }
-               }
-       }
-
        delete __pConditionManager;
        delete __pUiLibrary;
-       delete __pX11Library;
-       delete __pEcoreXLibrary;
-       __pEcoreXLibrary = null;
-       delete __pEcoreLibrary;
-       __pEcoreLibrary = null;
 }
 
 
 result
 _AppManagerImpl::Construct(void)
 {
-       __appManagerEvent.Construct();
+       __appLifecycleEvent.Construct();
+       __appListForAppLifecycle.Construct();
+       result r = __mutex.Create();
+        SysTryLog(NID_APP, r == E_SUCCESS, "Creating mutex failed.");
 
        _IAppManager* pMgr = _AppManagerProxy::GetService();
        //todo : uncomment following _SysTryReturn or put assert.
@@ -293,11 +158,31 @@ _AppManagerImpl::GetConditionManagerProxy(void)
 {
        if (__pConditionManager == null)
        {
+               const int MAX_TRY_COUNT = 5;
+               const int TRY_SLEEP_TIME = 250;
+
                __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
                SysAssert(__pConditionManager != null);
 
-               result r = __pConditionManager->Construct();
-               SysAssertf(r == E_SUCCESS, "__pConditionManager->Construct() faliied [%s].", GetErrorMessage(r));
+               int count = 0;
+               while (true)
+               {
+                       result r = __pConditionManager->Construct();
+                       if (r == E_SUCCESS)
+                       {
+                               SysLog(NID_APP, "Succeeded in connecting condition manager.");
+                               break;
+                       }
+
+                       if (count >= MAX_TRY_COUNT)
+                       {
+                               SysLog(NID_APP, "Failed to connecting condition manager.");
+                               break;
+                       }
+
+                       count++;
+                       Thread::Sleep(TRY_SLEEP_TIME);
+               }
        }
 
        return __pConditionManager;
@@ -311,14 +196,6 @@ _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
        AppControl* pAc = null;
 
        pAc = pRegs->GetTizenAppControlN(aId, oId);
-       if (pAc != null)
-       {
-               SetLastResult(E_SUCCESS);
-               return pAc;
-       }
-
-       pAc = pRegs->GetAppControlN(aId, oId);
-
        SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
                                 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
        SetLastResult(E_SUCCESS);
@@ -337,21 +214,7 @@ _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCat
 
        if (pDataType)
        {
-               if ((*pDataType)[0] == L'.')
-               {
-                       SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
-
-                       String ext;
-                       pDataType->SubString(1, ext);
-
-                       result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
-
-                       SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
-
-                       pMimeType = &mimeType;
-
-                       SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
-               }
+               mimeType = _AppControlManager::GetMimeTypeFromDataType(*pDataType);
        }
 
        if (pUriScheme)
@@ -360,16 +223,8 @@ _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCat
                                         GetErrorMessage(E_INVALID_FORMAT));
        }
 
-       ArrayList* pRetArg = null;
-       pRetArg = new (std::nothrow) ArrayList();
-       SysTryReturn(NID_APP, pRetArg != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
-                                GetErrorMessage(E_OUT_OF_MEMORY));
-
-       pRetArg->Construct();
-
        _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
-
-       pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
+       ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
 
        if ((pRetArg == null) || (pRetArg->GetCount() == 0))
        {
@@ -426,25 +281,7 @@ _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCate
 
        if (pDataType)
        {
-               String mimeType = *pDataType;
-
-               if ((*pDataType)[0] == L'.')
-               {
-                       SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
-
-#if 0
-                       String ext;
-                       pDataType->SubString(1, ext);
-
-                       result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
-
-                       SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
-
-                       pMimeType = &mimeType;
-
-                       SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
-#endif
-               }
+               const String& mimeType = _AppControlManager::GetMimeTypeFromDataType(*pDataType);
 
                _AppMessageImpl::SetMime(pBundle.get(), mimeType);
        }
@@ -592,8 +429,8 @@ _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments,
 //                                       "The length of appid exceeded the limit(%d).",
 //                                       WIDGET_APP_MAX_APPID_LENGTH);
 
-       AppControl* pAc = _AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN);
-       SysTryReturnResult(NID_APP, pAc != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
+       std::unique_ptr<AppControl> pAc(FindAppControlN(appId, TIZEN_OPERATION_MAIN));
+       SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
 
        if (pArguments)
        {
@@ -782,16 +619,6 @@ _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEve
        return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
 }
 
-result
-_AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
-{
-       SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
-
-       _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
-       SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
-       __appManagerEvent.FireAsync(*pArg);
-       return E_SUCCESS;
-}
 
 result
 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
@@ -801,46 +628,6 @@ _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
        return E_SUCCESS;
 }
 
-result
-_AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
-{
-       _IAppManager* pMgr = _AppManagerProxy::GetService();
-       SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
-
-       result r = __appManagerEvent.AddListener(listener);
-       SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
-
-       __eventListenerCount++;
-       SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
-
-       if( __eventListenerCount > 1)
-       {
-               return E_SUCCESS;
-       }
-
-       return pMgr->AddEventListener(-1);
-}
-
-result
-_AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
-{
-       _IAppManager* pMgr = _AppManagerProxy::GetService();
-       SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
-
-       result r = __appManagerEvent.RemoveListener(listener);
-       SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
-
-       __eventListenerCount--;
-       SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
-
-       if (__eventListenerCount == 0)
-       {
-               return pMgr->RemoveEventListener(-1);
-       }
-
-       return E_SUCCESS;
-}
-
 
 ///////////////////////////////////////////////////////////////////////////////
 // LifecycleManager begins.
@@ -924,7 +711,10 @@ _AppLifecycleManager::LaunchCallback(int pid, void* pData)
                        _IAppEventListener* pListener = null;
                        pEnum->GetCurrent(pListener);
 
-                       pListener->OnApplicationLaunched(tmp, pid);
+                       if (pListener && _AppInfo::GetAppState() == RUNNING)
+                       {
+                               pListener->OnApplicationLaunched(tmp, pid);
+                       }
                }
        }
 
@@ -948,7 +738,7 @@ _AppLifecycleManager::TerminateCallback(int pid, void* pData)
        result r = pImpl->__map.GetValue(pid, tmp);
        if (r != E_SUCCESS)
        {
-               SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
+               SysLog(NID_APP, "[%s] Cannot acquire app from pid %d.", GetErrorMessage(r), pid);
                return -1;
        }
 
@@ -962,7 +752,10 @@ _AppLifecycleManager::TerminateCallback(int pid, void* pData)
                        _IAppEventListener* pListener = null;
                        pEnum->GetCurrent(pListener);
 
-                       pListener->OnApplicationTerminated(tmp, pid);
+                       if (pListener && _AppInfo::GetAppState() == RUNNING)
+                       {
+                               pListener->OnApplicationTerminated(tmp, pid);
+                       }
                }
        }
 
@@ -1004,235 +797,150 @@ _AppManagerImpl::GetUiLibraryImpl(void)
        return *__pUiLibrary;
 }
 
-_LibraryImpl&
-_AppManagerImpl::GetEcoreXLibraryImpl(void)
+
+result
+_AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
 {
-       if (__pEcoreXLibrary == null)
-       {
-               __pEcoreXLibrary = new (std::nothrow) _LibraryImpl;
-               SysAssertf(__pEcoreXLibrary != null, "_LibraryImpl allocation failure.");
+       return __pActiveWindowManager->AddActiveAppEventListener(listener);
+}
 
-               result r = __pEcoreXLibrary->Construct(OSP_ECORE_X_SONAME);
-               SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
-       }
-       return *__pEcoreXLibrary;
+
+result
+_AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
+{
+       return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
 }
 
-_LibraryImpl&
-_AppManagerImpl::GetEcoreLibraryImpl(void)
+
+result
+_AppManagerImpl::GetActiveApp(AppId& appId)
 {
-       if (__pEcoreLibrary == null)
-       {
-               __pEcoreLibrary = new (std::nothrow) _LibraryImpl;
-               SysAssertf(__pEcoreLibrary != null, "_LibraryImpl allocation failure.");
+       return __pActiveWindowManager->GetActiveApp(appId);
+}
 
-               result r = __pEcoreLibrary->Construct(OSP_ECORE_SONAME);
-               SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
-       }
-       return *__pEcoreLibrary;
+
+bool 
+_AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
+{
+       _IAppManager* pMgr = _AppManagerProxy::GetService();
+       SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
+
+       return pMgr->IsUserPreferredAppForAppControlResolution(appId);
 }
 
-_LibraryImpl&
-_AppManagerImpl::GetX11LibraryImpl(void)
+result 
+_AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
 {
-       if (__pX11Library == null)
-       {
-               __pX11Library = new (std::nothrow) _LibraryImpl;
-               SysAssertf(__pX11Library != null, "_LibraryImpl allocation failure.");
+       _IAppManager* pMgr = _AppManagerProxy::GetService();
+       SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
 
-               result r = __pX11Library->Construct(OSP_X11_SONAME);
-               SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
-       }
-       return *__pX11Library;
+       return pMgr->ClearUserPreferenceForAppControlResolution(appId);
 }
 
-void
-_AppManagerImpl::FireActiveAppEvent(unsigned int xid, int pid, char* pAppName)
+result
+_AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
 {
-       static int oldPid = 0;
-       if (oldPid != pid)
-       {
-               oldPid = pid;
-               std::unique_ptr<IEnumeratorT<Tizen::App::IActiveAppEventListener* > > pEnum(__activeAppEventListenerList.GetEnumeratorN());
-               if (pEnum.get())
-               {
-                       while (pEnum->MoveNext() == E_SUCCESS)
-                       {
-                               Tizen::App::IActiveAppEventListener* pListener = null;
-                               pEnum->GetCurrent(pListener);
-                               if (pListener)
-                               {
-                                       char pkgname[255] = {0, };
-                                       aul_app_get_pkgname_bypid(pid, pkgname, 255);
-                                       // TODO: Translate it to package name --> AppId
-                                       pListener->OnActiveAppChanged(AppId(pkgname));
-                               }
-                       }
-               }
-       }
+       return __appLifecycleEvent.AddListener(listener, false);
 }
 
-unsigned int
-_AppManagerImpl::GetActiveWindow(void)
+result
+_AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
 {
-       if (p_ecore_x_window_root_list == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
-               SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, 0,
-                                                  "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
-       }
-       if (p_ecore_x_atom_get == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, 0,
-                                                  "A system error has been occurred. Failed to get ecore_x_atom_get.");
-       }
-       if (p_ecore_x_window_prop_window_get == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_window_prop_window_get = reinterpret_cast<int(*)(Ecore_X_Window win, Ecore_X_Atom atom, Ecore_X_Window* val, unsigned int len)>(lib.GetProcAddress(L"ecore_x_window_prop_window_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
-                                                  "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
-       }
+       return __appLifecycleEvent.RemoveListener(listener);
+}
 
-       int num = 0;
-       Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
+result
+_AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
+{
+       SysLog(NID_APP, "Enter");
 
-       Ecore_X_Window activeWin = 0;
-       if (pRoots)
-       {
-               Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
-               p_ecore_x_window_prop_window_get(pRoots[0], activeAtom, &activeWin, 1);
-               free(pRoots);
-       }
+       result res = __mutex.Acquire();
+        SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
 
-       return activeWin;
-}
+       bool isContained = false;
+       result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
 
-int
-_AppManagerImpl::GetProcessId(unsigned int window)
-{
-       if (p_ecore_x_netwm_pid_get == null)
+       int currentRefCnt = 0;
+       if(isContained)
        {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
-               SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
-                                                  "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
+               r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
+               r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
        }
+       else
+       {
+               r = __appListForAppLifecycle.Add(appId, currentRefCnt);
+               
+               _IAppManager* pMgr = _AppManagerProxy::GetService();
+               SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
 
-       int pid = 0;
-       p_ecore_x_netwm_pid_get(window, &pid);
+               r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
+               SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
+       }
+       res = __mutex.Release();
+        SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
+       
+       SysLog(NID_APP, "Exit");
 
-       return pid;
+       return r;
 }
 
 result
-_AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
+_AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
 {
-       if (p_ecore_x_window_root_list == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
-               SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
-       }
-       if (p_ECORE_X_EVENT_WINDOW_PROPERTY == null)
-       {
-               _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
-               p_ECORE_X_EVENT_WINDOW_PROPERTY = reinterpret_cast<int*>(lib.GetProcAddress(L"ECORE_X_EVENT_WINDOW_PROPERTY"));
-               SysTryReturnResult(NID_APP, p_ECORE_X_EVENT_WINDOW_PROPERTY != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_ECORE_X_EVENT_WINDOW_PROPERTY.");
-       }
-       if (p_XOpenDisplay == null)
-       {
-               _LibraryImpl& lib = GetX11LibraryImpl();
-               p_XOpenDisplay = reinterpret_cast<Display*(*)(_Xconst char* display_name)>(lib.GetProcAddress(L"XOpenDisplay"));
-               SysTryReturnResult(NID_APP, p_XOpenDisplay != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_XOpenDisplay.");
-       }
-       if (p_XCloseDisplay == null)
-       {
-               _LibraryImpl& lib = GetX11LibraryImpl();
-               p_XCloseDisplay = reinterpret_cast<int(*)(Display* display)>(lib.GetProcAddress(L"XCloseDisplay"));
-               SysTryReturnResult(NID_APP, p_XCloseDisplay != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_XCloseDisplay.");
-       }
-       if (p_XSelectInput == null)
-       {
-               _LibraryImpl& lib = GetX11LibraryImpl();
-               p_XSelectInput = reinterpret_cast<int(*)(Display* display, Window w, long event_mask)>(lib.GetProcAddress(L"XSelectInput"));
-               SysTryReturnResult(NID_APP, p_XSelectInput != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_XSelectInput.");
-       }
-       if (p_ecore_event_handler_add == null)
-       {
-               _LibraryImpl& lib = GetEcoreLibraryImpl();
-               p_ecore_event_handler_add = reinterpret_cast<Ecore_Event_Handler*(*)(int type, Ecore_Event_Handler_Cb func, const void* data)>(lib.GetProcAddress(L"ecore_event_handler_add"));
-               SysTryReturnResult(NID_APP, p_ecore_event_handler_add != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_ecore_event_handler_add.");
-       }
+       SysLog(NID_APP, "Enter");
 
-       bool alreadyExist = __activeAppEventListenerList.Contains(&listener);
-       SysTryReturnResult(NID_APP, !alreadyExist, E_OBJ_ALREADY_EXIST, "The event listener already exist.");
-       result r = __activeAppEventListenerList.Add(&listener);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
+       result res = __mutex.Acquire();
+        SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
+       
+       bool isContained = false;
+       result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
 
-       if (!pWindowPropertyChanged)
+       if(isContained)
        {
-               std::unique_ptr<Display, _DisplayDeleter> pDisplay(p_XOpenDisplay(NULL));
-               SysTryReturnResult(NID_APP, pDisplay != null, E_SYSTEM, "A system error has been occurred. Failed to XOpenDisplay.");
+               int currentRefCnt = 0;
+               r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
 
-               int num = 0;
-               Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
-               SysTryReturnResult(NID_APP, pRoots != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
+               currentRefCnt--;
 
-               for (int i = 0; i < num; i++)
+               if (currentRefCnt < 0)
                {
-                       p_XSelectInput(pDisplay.get(), pRoots[i], PropertyChangeMask);
-               }
-
-               pWindowPropertyChanged = p_ecore_event_handler_add(*p_ECORE_X_EVENT_WINDOW_PROPERTY, OnPropertyChanged, (void*) this);
-               free(pRoots);
-               SysTryReturnResult(NID_APP, pWindowPropertyChanged, E_SYSTEM, "A system error has been occurred.");
-       }
+                       r = __appListForAppLifecycle.Remove(appId);
 
-       return r;
-}
+                       _IAppManager* pMgr = _AppManagerProxy::GetService();
+                       SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
 
-result
-_AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
-{
-       if (p_ecore_event_handler_del == null)
-       {
-               _LibraryImpl& lib = GetEcoreLibraryImpl();
-               p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
-               SysTryReturnResult(NID_APP, p_ecore_event_handler_del != null, E_SYSTEM,
-                                                  "A system error has been occurred. Failed to get p_ecore_event_handler_del.");
+                       r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
+                       
+                       SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
+               }
+               else
+               {
+                       r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
+               }
        }
-
-       result r = __activeAppEventListenerList.Remove(&listener);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
-
-       if (__activeAppEventListenerList.GetCount() == 0)
+       else
        {
-               p_ecore_event_handler_del(pWindowPropertyChanged);
-               pWindowPropertyChanged = null;
+               SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
        }
+       
+       res = __mutex.Release();
+        SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
+       
+       SysLog(NID_APP, "Exit");
 
        return r;
+
 }
 
 result
-_AppManagerImpl::GetActiveApp(AppId& appId)
+_AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
 {
-       unsigned int windowId = GetActiveWindow();
-       int processId = GetProcessId(windowId);
-       char pkgname[255] = {0, };
-       aul_app_get_pkgname_bypid(processId, pkgname, 255);
-       // TODO: Translate it to package name --> AppId
-       appId = pkgname;
+       SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
+       
+       _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
+       SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
+       
+       __appLifecycleEvent.FireAsync(*pArg);
        return E_SUCCESS;
 }