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>
34 #include <FBaseInteger.h>
35 #include <FBaseObject.h>
36 #include <FBaseString.h>
37 #include <FBaseColArrayList.h>
38 #include <FBaseErrors.h>
39 #include <FAppAppControl.h>
40 #include <FAppAppManager.h>
41 #include <FAppSqlDataControl.h>
42 #include <FAppMapDataControl.h>
43 #include <FAppPkgPackageInfo.h>
44 #include <FAppIActiveAppEventListener.h>
45 #include <FBaseSysLog.h>
46 #include <FAppIAppControlListener.h>
48 #include <FBase_StringConverter.h>
49 #include <FBaseRt_LibraryImpl.h>
50 #include <FIo_DataControlResultSetImpl.h>
52 #include "FApp_AppControlRegistry.h"
53 #include "FApp_AppImpl.h"
54 #include "FApp_AppInfo.h"
55 #include "FApp_AppManagerEventArg.h"
56 #include "FApp_AppManagerImpl.h"
57 #include "FApp_AppManagerProxy.h"
58 #include "FApp_AppMessageImpl.h"
59 #include "FApp_AppControlImpl.h"
60 #include "FApp_ConditionManagerProxy.h"
61 #include "FApp_IAppManagerEventListener.h"
62 #include "FApp_IAppEventListener.h"
63 #include "FApp_MapDataControlImpl.h"
64 #include "FApp_SqlDataControlImpl.h"
65 #include "FAppPkg_PackageManagerImpl.h"
66 #include "FAppPkg_PackageInfoImpl.h"
67 #include "FApp_AppControlManager.h"
69 #include "FSys_SystemInfoImpl.h"
71 using namespace Tizen::App::Package;
72 using namespace Tizen::Base;
73 using namespace Tizen::Base::Collection;
74 using namespace Tizen::Base::Runtime;
75 using namespace Tizen::Base::Utility;
76 using namespace Tizen::Io;
81 //const long MAX_APPCONTROL_ARGUMENT = 4096;
82 const long MAX_APPCONTROL_ARGUMENT = 1024;
83 const long MAX_CONDITION_LENGTH = 400;
85 // ActiveWindow related function and variable from shared library.
87 static Ecore_X_Atom(* p_ecore_x_atom_get)(const char* name) = null;
88 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;
89 static Eina_Bool(* p_ecore_x_netwm_pid_get)(Ecore_X_Window win, int* pid) = null;
90 static int(* p_ecore_x_netwm_name_get)(Ecore_X_Window win, char** name) = null;
91 static Ecore_X_Window* (* p_ecore_x_window_root_list)(int* num_ret) = null;
92 static int* p_ECORE_X_EVENT_WINDOW_PROPERTY = null;
94 static Ecore_Event_Handler* (* p_ecore_event_handler_add)(int type, Ecore_Event_Handler_Cb func, const void* data) = null;
95 static void* (* p_ecore_event_handler_del)(Ecore_Event_Handler* event_handler) = null;
97 static int (* p_XSelectInput)(Display* display, Window w, long event_mask) = null;
98 static Display* (* p_XOpenDisplay)(_Xconst char* display_name) = null;
99 static int (* p_XCloseDisplay)(Display* display) = null;
101 Ecore_Event_Handler* pWindowPropertyChanged = null;
103 struct _DisplayDeleter
105 void operator()(Display* pDisplay)
109 p_XCloseDisplay(pDisplay);
115 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
118 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
122 while (pEnum->MoveNext() == E_SUCCESS)
124 String* pStr = static_cast<String*>(pEnum->GetCurrent());
129 size += pStr->GetLength();
137 OnPropertyChanged(void* pData, int type, void* pEvent)
139 using namespace Tizen::App;
141 if (p_ecore_x_atom_get == null)
143 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
144 p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
145 SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, EINA_FALSE,
146 "A system error has been occurred. Failed to get ecore_x_atom_get.");
148 if (p_ecore_x_window_prop_window_get == null)
150 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
151 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"));
152 SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
153 "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
155 if (p_ecore_x_netwm_pid_get == null)
157 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
158 p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
159 SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
160 "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
162 if (p_ecore_x_netwm_name_get == null)
164 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
165 p_ecore_x_netwm_name_get = reinterpret_cast<int(*)(Ecore_X_Window win, char** name)>(lib.GetProcAddress(L"ecore_x_netwm_name_get"));
166 SysTryReturnResult(NID_APP, p_ecore_x_netwm_name_get != null, EINA_FALSE,
167 "A system error has been occurred. Failed to get ecore_x_netwm_name_get.");
172 SysLog(NID_UI, "The data is not valid.");
177 SysLog(NID_UI, "The event is not valid.");
181 Ecore_X_Event_Window_Property* pE = (Ecore_X_Event_Window_Property*) pEvent;
182 Ecore_X_Atom atom = pE->atom;
183 Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
185 if (atom != activeAtom)
187 return ECORE_CALLBACK_PASS_ON;
190 Ecore_X_Window win = pE->win;
191 Ecore_X_Window activeWin = 0;
192 p_ecore_x_window_prop_window_get(win, activeAtom, &activeWin, 1);
195 p_ecore_x_netwm_pid_get(activeWin, &pid);
197 char* pAppName = null;
198 p_ecore_x_netwm_name_get(activeWin, &pAppName);
200 Tizen::App::_AppManagerImpl* pAppManagerImpl = static_cast<Tizen::App::_AppManagerImpl*>(pData);
201 pAppManagerImpl->FireActiveAppEvent(activeWin, pid, pAppName);
208 return ECORE_CALLBACK_PASS_ON;
211 } // anonymous name-space
214 namespace Tizen { namespace App
217 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
218 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
219 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
220 const wchar_t OSP_ECORE_X_SONAME[] = L"libecore_x.so.1";
221 const wchar_t OSP_ECORE_SONAME[] = L"libecore.so.1";
222 const wchar_t OSP_X11_SONAME[] = L"libX11.so.6";
223 const int _MAX_PACKAGE_ID_LENGTH = 10;
225 _LibraryImpl* _AppManagerImpl::__pEcoreLibrary = null;
226 _LibraryImpl* _AppManagerImpl::__pEcoreXLibrary = null;
228 _AppManagerImpl::_AppManagerImpl(void)
229 : __pConditionManager(null)
230 , __eventListenerCount(0)
232 , __pX11Library(null)
237 _AppManagerImpl::~_AppManagerImpl(void)
241 if (__activeAppEventListenerList.GetCount() > 0)
243 if (p_ecore_event_handler_del == null)
245 _LibraryImpl& lib = GetEcoreLibraryImpl();
246 p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
248 if (p_ecore_event_handler_del)
250 if (pWindowPropertyChanged)
252 p_ecore_event_handler_del(pWindowPropertyChanged);
253 pWindowPropertyChanged = null;
258 delete __pConditionManager;
260 delete __pX11Library;
261 delete __pEcoreXLibrary;
262 __pEcoreXLibrary = null;
263 delete __pEcoreLibrary;
264 __pEcoreLibrary = null;
269 _AppManagerImpl::Construct(void)
271 __appManagerEvent.Construct();
273 _IAppManager* pMgr = _AppManagerProxy::GetService();
274 //todo : uncomment following _SysTryReturn or put assert.
275 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
276 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
277 pMgr->InitEventListener(this);
283 _AppManagerImpl::GetInstance(void)
285 return AppManager::GetInstance()->__pAppManagerImpl;
288 _ConditionManagerProxy*
289 _AppManagerImpl::GetConditionManagerProxy(void)
291 if (__pConditionManager == null)
293 const int MAX_TRY_COUNT = 5;
294 const int TRY_SLEEP_TIME = 250;
296 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
297 SysAssert(__pConditionManager != null);
302 result r = __pConditionManager->Construct();
305 SysLog(NID_APP, "Succeeded in connecting condition manager.");
309 if (count >= MAX_TRY_COUNT)
311 SysLog(NID_APP, "Failed to connecting condition manager.");
316 Thread::Sleep(TRY_SLEEP_TIME);
320 return __pConditionManager;
324 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
326 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
328 AppControl* pAc = null;
330 pAc = pRegs->GetTizenAppControlN(aId, oId);
333 SetLastResult(E_SUCCESS);
337 pAc = pRegs->GetAppControlN(aId, oId);
339 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
340 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
341 SetLastResult(E_SUCCESS);
346 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
347 const String* pUriScheme)
349 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
350 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
353 const String* pMimeType = pDataType;
357 if ((*pDataType)[0] == L'.')
359 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
362 pDataType->SubString(1, ext);
364 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
366 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
368 pMimeType = &mimeType;
370 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
376 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
377 GetErrorMessage(E_INVALID_FORMAT));
380 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
381 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
383 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
387 SetLastResult(E_OBJ_NOT_FOUND);
391 SetLastResult(E_SUCCESS);
392 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
399 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
400 IAppControlListener* pListener)
402 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
407 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
408 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
410 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
411 "At least one of the specified argument must not be null.");
412 if (pDataList != null)
415 argSize = GetTotalSize(*pDataList);
417 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
419 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
420 "The size of pDataList exceeded the limit(%d).",
421 MAX_APPCONTROL_ARGUMENT);
424 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
426 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
427 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
429 _AppMessageImpl::SetOperation(pBundle.get(), operation);
433 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
438 String mimeType = *pDataType;
440 if ((*pDataType)[0] == L'.')
442 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
446 pDataType->SubString(1, ext);
448 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
450 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
452 pMimeType = &mimeType;
454 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
458 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
463 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
466 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
471 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
473 SqlDataControl* pDc = null;
475 String* pAppId = null;
476 String* pAccess = null;
479 // XXX: Need _NativeDataControlRegistry for SLP native app ?
480 // Try to searach SLP data control.
481 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
482 // pDc = pReg->GetNativeSqlDataControlN(providerId);
488 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
489 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
491 pAppId = dynamic_cast< String* >(pList->GetAt(0));
492 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
493 pAccess = dynamic_cast< String* >(pList->GetAt(1));
494 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
497 if (pAppId->StartsWith(L"org.tizen.", 0))
499 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
500 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
501 for (int i = 0; i < 3; ++i)
503 pStrTok->GetNextToken(appId);
506 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
510 appId.Append(*pAppId);
513 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
514 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
520 _AppManagerImpl::GetMapDataControlN(const String& providerId)
522 MapDataControl* pDc = null;
524 String* pAppId = null;
525 String* pAccess = null;
528 // XXX: Need _NativeDataControlRegistry for SLP native app ?
529 // Try to searach SLP data control.
530 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
531 // pDc = pReg->GetNativeMapDataControlN(providerId);
537 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
538 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
540 pAppId = dynamic_cast< String* >(pList->GetAt(0));
541 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
542 pAccess = dynamic_cast< String* >(pList->GetAt(1));
543 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
546 if (pAppId->StartsWith(L"org.tizen.", 0))
548 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
549 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
550 for (int i = 0; i < 3; ++i)
552 pStrTok->GetNextToken(appId);
555 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
559 appId.Append(*pAppId);
562 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
563 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
569 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
571 String pkgId = PackageManager::GetPackageIdByAppId(appId);
572 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
573 "The application is not installed. (app: %ls)", appId.GetPointer());
575 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
576 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
577 "Failed to get _PackageManagerImpl instance.");
579 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
580 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
581 "The application is not installed. (app: %ls)", appId.GetPointer());
583 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
584 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
585 "Failed to get _PackageInfoImpl instance.");
587 appRootPath = pPkgInfoImpl->GetAppRootPath();
588 appRootPath.Append(L"/");
594 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
596 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
597 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
598 "The target application(%ls) is not installed.", appId.GetPointer());
599 // SysTryReturnResult(NID_APP,
600 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
601 // "The length of appid exceeded the limit(%d).",
602 // WIDGET_APP_MAX_APPID_LENGTH);
604 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
605 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
610 argSize = GetTotalSize(*pArguments);
612 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
614 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
615 "The size of pDataList exceeded the limit(%d).",
616 MAX_APPCONTROL_ARGUMENT);
619 result r = pAc->Start(pArguments, null);
621 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
627 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
629 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
631 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
633 int ret = aul_open_app(pName.get());
637 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
642 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
643 "The target application(%ls) is not installed.", appId.GetPointer());
649 r = E_APP_NOT_INSTALLED;
659 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
665 _AppManagerImpl::TerminateApplication(const AppId& appId)
667 _IAppManager* pMgr = _AppManagerProxy::GetService();
668 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
670 return pMgr->TerminateApplication(appId);
675 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
677 const char* pStr = static_cast<const char*>(pData);
679 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
681 aul_terminate_pid(pAppInfo->pid);
682 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
688 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
690 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
691 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
693 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
698 _AppManagerImpl::IsRunning(const AppId& appId) const
700 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
702 return aul_app_is_running(pAppId.get());
706 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
708 ArrayList* pList = static_cast<ArrayList*>(pData);
709 if (pList && pAppInfo && pAppInfo->appid)
711 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
718 _AppManagerImpl::GetRunningAppListN(void) const
720 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
721 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
722 GetErrorMessage(E_OUT_OF_MEMORY));
723 pRunningAppList->Construct();
725 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
726 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
728 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
729 return pRunningAppList;
734 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
735 AppManager::LaunchOption option)
737 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
738 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
740 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
741 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
744 result r = strTok.GetNextToken(key);
745 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
748 if (key == L"Serial")
750 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
752 else if (key == L"NFC")
754 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
756 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
758 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
762 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
764 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
765 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
769 SysTryReturnResult(NID_APP,
770 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
771 "No such a condition.");
774 return pProxy->UnregisterAppLaunch(appId, pCondition);
778 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
780 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
781 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
782 GetErrorMessage(E_INVALID_STATE));
785 return pProxy->IsAppLaunchRegistered(appId, pCondition);
789 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
791 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
795 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
797 SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
799 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
800 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
801 __appManagerEvent.FireAsync(*pArg);
806 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
814 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
816 _IAppManager* pMgr = _AppManagerProxy::GetService();
817 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
819 result r = __appManagerEvent.AddListener(listener);
820 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
822 __eventListenerCount++;
823 SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
825 if( __eventListenerCount > 1)
830 return pMgr->AddEventListener(-1);
834 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
836 _IAppManager* pMgr = _AppManagerProxy::GetService();
837 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
839 result r = __appManagerEvent.RemoveListener(listener);
840 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
842 __eventListenerCount--;
843 SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
845 if (__eventListenerCount == 0)
847 return pMgr->RemoveEventListener(-1);
854 ///////////////////////////////////////////////////////////////////////////////
855 // LifecycleManager begins.
856 ///////////////////////////////////////////////////////////////////////////////
859 _AppLifecycleManager::Init(void)
861 aul_listen_app_launch_signal(LaunchCallback, this);
862 aul_listen_app_dead_signal(TerminateCallback, this);
866 _AppLifecycleManager::Fini(void)
868 aul_listen_app_launch_signal(NULL, NULL);
869 aul_listen_app_dead_signal(NULL, NULL);
873 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
875 if (__pEventList == null)
877 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
878 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
882 __pEventList = pAppEventList.release();
883 SysLog(NID_APP, "Registered app event listener.");
886 return __pEventList->Add(&listener);
890 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
892 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
894 result r = __pEventList->Remove(&listener);
896 if (__pEventList->GetCount() == 0)
908 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
910 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
911 if (pImpl == null || pImpl->__pEventList == null)
913 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
918 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
921 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
925 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
928 const String tmp = appId;
929 pImpl->__map.Add(pid, tmp);
931 while (pEnum->MoveNext() == E_SUCCESS)
933 _IAppEventListener* pListener = null;
934 pEnum->GetCurrent(pListener);
936 pListener->OnApplicationLaunched(tmp, pid);
940 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
946 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
948 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
949 if (pImpl == null || pImpl->__pEventList == null)
951 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
955 // terminate callback cannot acquire appId from pid
957 result r = pImpl->__map.GetValue(pid, tmp);
960 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
964 pImpl->__map.Remove(pid);
966 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
969 while (pEnum->MoveNext() == E_SUCCESS)
971 _IAppEventListener* pListener = null;
972 pEnum->GetCurrent(pListener);
974 pListener->OnApplicationTerminated(tmp, pid);
978 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
983 ///////////////////////////////////////////////////////////////////////////////
984 // LifecycleManager ends.
985 ///////////////////////////////////////////////////////////////////////////////
989 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
991 return __lifeManager.AddListener(listener);
995 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
997 return __lifeManager.RemoveListener(listener);
1002 _AppManagerImpl::GetUiLibraryImpl(void)
1004 if (__pUiLibrary == null)
1006 __pUiLibrary = new (std::nothrow) _LibraryImpl;
1007 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
1009 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
1010 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1013 return *__pUiLibrary;
1017 _AppManagerImpl::GetEcoreXLibraryImpl(void)
1019 if (__pEcoreXLibrary == null)
1021 __pEcoreXLibrary = new (std::nothrow) _LibraryImpl;
1022 SysAssertf(__pEcoreXLibrary != null, "_LibraryImpl allocation failure.");
1024 result r = __pEcoreXLibrary->Construct(OSP_ECORE_X_SONAME);
1025 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1027 return *__pEcoreXLibrary;
1031 _AppManagerImpl::GetEcoreLibraryImpl(void)
1033 if (__pEcoreLibrary == null)
1035 __pEcoreLibrary = new (std::nothrow) _LibraryImpl;
1036 SysAssertf(__pEcoreLibrary != null, "_LibraryImpl allocation failure.");
1038 result r = __pEcoreLibrary->Construct(OSP_ECORE_SONAME);
1039 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1041 return *__pEcoreLibrary;
1045 _AppManagerImpl::GetX11LibraryImpl(void)
1047 if (__pX11Library == null)
1049 __pX11Library = new (std::nothrow) _LibraryImpl;
1050 SysAssertf(__pX11Library != null, "_LibraryImpl allocation failure.");
1052 result r = __pX11Library->Construct(OSP_X11_SONAME);
1053 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1055 return *__pX11Library;
1059 _AppManagerImpl::FireActiveAppEvent(unsigned int xid, int pid, char* pAppName)
1061 static int oldPid = 0;
1065 std::unique_ptr<IEnumeratorT<Tizen::App::IActiveAppEventListener* > > pEnum(__activeAppEventListenerList.GetEnumeratorN());
1068 while (pEnum->MoveNext() == E_SUCCESS)
1070 Tizen::App::IActiveAppEventListener* pListener = null;
1071 pEnum->GetCurrent(pListener);
1074 char pkgname[255] = {0, };
1075 aul_app_get_pkgname_bypid(pid, pkgname, 255);
1076 // TODO: Translate it to package name --> AppId
1077 pListener->OnActiveAppChanged(AppId(pkgname));
1085 _AppManagerImpl::GetActiveWindow(void)
1087 if (p_ecore_x_window_root_list == null)
1089 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1090 p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
1091 SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, 0,
1092 "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
1094 if (p_ecore_x_atom_get == null)
1096 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1097 p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
1098 SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, 0,
1099 "A system error has been occurred. Failed to get ecore_x_atom_get.");
1101 if (p_ecore_x_window_prop_window_get == null)
1103 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1104 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"));
1105 SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
1106 "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
1110 Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
1112 Ecore_X_Window activeWin = 0;
1115 Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
1116 p_ecore_x_window_prop_window_get(pRoots[0], activeAtom, &activeWin, 1);
1124 _AppManagerImpl::GetProcessId(unsigned int window)
1126 if (p_ecore_x_netwm_pid_get == null)
1128 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1129 p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
1130 SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
1131 "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
1135 p_ecore_x_netwm_pid_get(window, &pid);
1141 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
1143 if (p_ecore_x_window_root_list == null)
1145 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1146 p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
1147 SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, E_SYSTEM,
1148 "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
1150 if (p_ECORE_X_EVENT_WINDOW_PROPERTY == null)
1152 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1153 p_ECORE_X_EVENT_WINDOW_PROPERTY = reinterpret_cast<int*>(lib.GetProcAddress(L"ECORE_X_EVENT_WINDOW_PROPERTY"));
1154 SysTryReturnResult(NID_APP, p_ECORE_X_EVENT_WINDOW_PROPERTY != null, E_SYSTEM,
1155 "A system error has been occurred. Failed to get p_ECORE_X_EVENT_WINDOW_PROPERTY.");
1157 if (p_XOpenDisplay == null)
1159 _LibraryImpl& lib = GetX11LibraryImpl();
1160 p_XOpenDisplay = reinterpret_cast<Display*(*)(_Xconst char* display_name)>(lib.GetProcAddress(L"XOpenDisplay"));
1161 SysTryReturnResult(NID_APP, p_XOpenDisplay != null, E_SYSTEM,
1162 "A system error has been occurred. Failed to get p_XOpenDisplay.");
1164 if (p_XCloseDisplay == null)
1166 _LibraryImpl& lib = GetX11LibraryImpl();
1167 p_XCloseDisplay = reinterpret_cast<int(*)(Display* display)>(lib.GetProcAddress(L"XCloseDisplay"));
1168 SysTryReturnResult(NID_APP, p_XCloseDisplay != null, E_SYSTEM,
1169 "A system error has been occurred. Failed to get p_XCloseDisplay.");
1171 if (p_XSelectInput == null)
1173 _LibraryImpl& lib = GetX11LibraryImpl();
1174 p_XSelectInput = reinterpret_cast<int(*)(Display* display, Window w, long event_mask)>(lib.GetProcAddress(L"XSelectInput"));
1175 SysTryReturnResult(NID_APP, p_XSelectInput != null, E_SYSTEM,
1176 "A system error has been occurred. Failed to get p_XSelectInput.");
1178 if (p_ecore_event_handler_add == null)
1180 _LibraryImpl& lib = GetEcoreLibraryImpl();
1181 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"));
1182 SysTryReturnResult(NID_APP, p_ecore_event_handler_add != null, E_SYSTEM,
1183 "A system error has been occurred. Failed to get p_ecore_event_handler_add.");
1186 bool alreadyExist = __activeAppEventListenerList.Contains(&listener);
1187 SysTryReturnResult(NID_APP, !alreadyExist, E_OBJ_ALREADY_EXIST, "The event listener already exist.");
1188 result r = __activeAppEventListenerList.Add(&listener);
1189 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1191 if (!pWindowPropertyChanged)
1193 std::unique_ptr<Display, _DisplayDeleter> pDisplay(p_XOpenDisplay(NULL));
1194 SysTryReturnResult(NID_APP, pDisplay != null, E_SYSTEM, "A system error has been occurred. Failed to XOpenDisplay.");
1197 Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
1198 SysTryReturnResult(NID_APP, pRoots != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1200 for (int i = 0; i < num; i++)
1202 p_XSelectInput(pDisplay.get(), pRoots[i], PropertyChangeMask);
1205 pWindowPropertyChanged = p_ecore_event_handler_add(*p_ECORE_X_EVENT_WINDOW_PROPERTY, OnPropertyChanged, (void*) this);
1207 SysTryReturnResult(NID_APP, pWindowPropertyChanged, E_SYSTEM, "A system error has been occurred.");
1214 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
1216 if (p_ecore_event_handler_del == null)
1218 _LibraryImpl& lib = GetEcoreLibraryImpl();
1219 p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
1220 SysTryReturnResult(NID_APP, p_ecore_event_handler_del != null, E_SYSTEM,
1221 "A system error has been occurred. Failed to get p_ecore_event_handler_del.");
1224 result r = __activeAppEventListenerList.Remove(&listener);
1225 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1227 if (__activeAppEventListenerList.GetCount() == 0)
1229 p_ecore_event_handler_del(pWindowPropertyChanged);
1230 pWindowPropertyChanged = null;
1237 _AppManagerImpl::GetActiveApp(AppId& appId)
1239 const unsigned int windowId = GetActiveWindow();
1240 const int processId = GetProcessId(windowId);
1241 char pkgname[255] = {0, };
1242 aul_app_get_pkgname_bypid(processId, pkgname, 255);
1244 appId = _Aul::GetRealAppId(String(pkgname));
1249 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
1251 _IAppManager* pMgr = _AppManagerProxy::GetService();
1252 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
1254 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
1258 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
1260 _IAppManager* pMgr = _AppManagerProxy::GetService();
1261 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
1263 return pMgr->ClearUserPreferenceForAppControlResolution(appId);