refinement of osp-appfw log messages
[platform/framework/native/appfw.git] / src / app / FApp_AppManagerImpl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 5aaf297..9151e72
@@ -67,6 +67,7 @@
 #include "FAppPkg_PackageInfoImpl.h"
 #include "FApp_AppControlManager.h"
 #include "FApp_Aul.h"
+#include "FSys_SystemInfoImpl.h"
 
 using namespace Tizen::App::Package;
 using namespace Tizen::Base;
@@ -232,7 +233,6 @@ _AppManagerImpl::_AppManagerImpl(void)
        , __eventListenerCount(0)
        , __pUiLibrary(null)
        , __pX11Library(null)
-       , __pAppEventList(null)
 {
        SysLog(NID_APP, "");
 }
@@ -310,14 +310,7 @@ _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
 
        AppControl* pAc = null;
 
-       pAc = pRegs->GetNativeAppControlN(aId, oId);
-       if (pAc != null)
-       {
-               SetLastResult(E_SUCCESS);
-               return pAc;
-       }
-
-       pAc = pRegs->GetAliasAppControlN(aId, oId);
+       pAc = pRegs->GetTizenAppControlN(aId, oId);
        if (pAc != null)
        {
                SetLastResult(E_SUCCESS);
@@ -568,7 +561,7 @@ _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
 {
        String pkgId = PackageManager::GetPackageIdByAppId(appId);
        SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
-                       "The application is not installed. (appId: %ls)", appId.GetPointer());
+                       "The application is not installed. (app: %ls)", appId.GetPointer());
 
        _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
        SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
@@ -576,7 +569,7 @@ _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
 
        std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
        SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
-                       "The application is not installed. (appId: %ls)", appId.GetPointer());
+                       "The application is not installed. (app: %ls)", appId.GetPointer());
 
        _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
        SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
@@ -735,6 +728,24 @@ _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition,
        _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
        SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
 
+       Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
+       SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
+
+       String key;
+       result r = strTok.GetNextToken(key);
+       SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
+
+       bool ret = true;
+       if (key == L"Serial")
+       {
+               r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
+       }
+       else if (key == L"NFC")
+       {
+               r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
+       }
+       SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
+
        return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
 }
 
@@ -774,7 +785,7 @@ _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEve
 result
 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
 {
-       SysLog(NID_APP, "appId:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
+       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.");
@@ -831,102 +842,150 @@ _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
 }
 
 
+///////////////////////////////////////////////////////////////////////////////
+// LifecycleManager begins.
+///////////////////////////////////////////////////////////////////////////////
+
 void
-_AppManagerImpl::AppEventCallback(app_context_h app_context, app_context_event_e event, void* pData)
+_AppLifecycleManager::Init(void)
 {
-       _AppManagerImpl* pImpl = static_cast<_AppManagerImpl*>(pData);
-       if (pImpl == null || pImpl->__pAppEventList == null)
-       {
-               SysLogException(NID_APP, E_SYSTEM, "Wrong _AppManagerImpl state.");
-               return;
-       }
+       aul_listen_app_launch_signal(LaunchCallback, this);
+       aul_listen_app_dead_signal(TerminateCallback, this);
+}
 
-       std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pAppEventList->GetEnumeratorN());
+void
+_AppLifecycleManager::Fini(void)
+{
+       aul_listen_app_launch_signal(NULL, NULL);
+       aul_listen_app_dead_signal(NULL, NULL);
+}
 
-       if (pEnum.get())
+result
+_AppLifecycleManager::AddListener(_IAppEventListener& listener)
+{
+       if (__pEventList == null)
        {
-               char* pAppId = NULL;
-               app_context_get_app_id(app_context, &pAppId);
-               if (pAppId == NULL)
-               {
-                       SysLogException(NID_APP, E_SYSTEM, "Cannot acquire appId.");
-                       return;
-               }
+               std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
+               SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
 
-               const String appId = pAppId;
+               Init();
 
-               free(pAppId);
+               __pEventList = pAppEventList.release();
+               SysLog(NID_APP, "Registered app event listener.");
+       }
 
-               pid_t pid = -1;
-               app_context_get_pid(app_context, &pid);
-               const int val = event;
+       return __pEventList->Add(&listener);
+}
 
-               // loop unfolding
-               switch (val)
-               {
-               case APP_CONTEXT_EVENT_LAUNCHED:
-                       while (pEnum->MoveNext() == E_SUCCESS)
-                       {
-                               _IAppEventListener* pListener = null;
-                               pEnum->GetCurrent(pListener);
+result
+_AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
+{
+       SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
 
-                               pListener->OnApplicationLaunched(appId, pid);
-                       }
-                       break;
-               case APP_CONTEXT_EVENT_TERMINATED:
-                       while (pEnum->MoveNext() == E_SUCCESS)
-                       {
-                               _IAppEventListener* pListener = null;
-                               pEnum->GetCurrent(pListener);
+       result r = __pEventList->Remove(&listener);
 
-                               pListener->OnApplicationTerminated(appId, pid);
-                       }
-                       break;
-               default:
-                       SysLog(NID_APP, "Invalid state.");
-                       break;
-               }
+       if (__pEventList->GetCount() == 0)
+       {
+               Fini();
+
+               delete __pEventList;
+               __pEventList = null;
        }
 
-       SysLog(NID_APP, "Finished invoking application event listener.");
+       return r;
 }
 
-result
-_AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
+int
+_AppLifecycleManager::LaunchCallback(int pid, void* pData)
 {
-       if (__pAppEventList == null)
+       _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
+       if (pImpl == null || pImpl->__pEventList == null)
        {
-               std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
-               SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
+               SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
+               return -1;
+       }
 
-               const int ret = app_manager_set_app_context_event_cb(_AppManagerImpl::AppEventCallback, this);
-               SysTryReturnResult(NID_APP, ret == APP_MANAGER_ERROR_NONE, E_SYSTEM, "Context registration failed.");
+       char appId[255];
+       int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
+       if (ret != AUL_R_OK)
+       {
+               SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
+               return -1;
+       }
 
-               __pAppEventList = pAppEventList.release();
-               SysLog(NID_APP, "Registered app event listener.");
+       std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
+       if (pEnum.get())
+       {
+               const String tmp = appId;
+               pImpl->__map.Add(pid, tmp);
+
+               while (pEnum->MoveNext() == E_SUCCESS)
+               {
+                       _IAppEventListener* pListener = null;
+                       pEnum->GetCurrent(pListener);
+
+                       pListener->OnApplicationLaunched(tmp, pid);
+               }
        }
 
-       return __pAppEventList->Add(&listener);
+       SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
+
+       return 0;
 }
 
-result
-_AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
+int
+_AppLifecycleManager::TerminateCallback(int pid, void* pData)
 {
-       result r = E_SUCCESS;
+       _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
+       if (pImpl == null || pImpl->__pEventList == null)
+       {
+               SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
+               return -1;
+       }
 
-       SysTryReturnResult(NID_APP,__pAppEventList != null, E_OBJ_NOT_FOUND, "_IAppEventListener list is empty.");
+       // terminate callback cannot acquire appId from pid
+       String tmp;
+       result r = pImpl->__map.GetValue(pid, tmp);
+       if (r != E_SUCCESS)
+       {
+               SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
+               return -1;
+       }
 
-       r = __pAppEventList->Remove(&listener);
+       pImpl->__map.Remove(pid);
 
-       if (__pAppEventList->GetCount() == 0)
+       std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
+       if (pEnum.get())
        {
-               app_manager_unset_app_context_event_cb();
+               while (pEnum->MoveNext() == E_SUCCESS)
+               {
+                       _IAppEventListener* pListener = null;
+                       pEnum->GetCurrent(pListener);
 
-               delete __pAppEventList;
-               __pAppEventList = null;
+                       pListener->OnApplicationTerminated(tmp, pid);
+               }
        }
 
-       return E_SUCCESS;
+       SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
+
+       return 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// LifecycleManager ends.
+///////////////////////////////////////////////////////////////////////////////
+
+
+result
+_AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
+{
+       return __lifeManager.AddListener(listener);
+}
+
+result
+_AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
+{
+       return __lifeManager.RemoveListener(listener);
 }