2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "AppManagerWrapper.h"
19 #include "ApplicationUtil.h"
20 #include <Commons/Exception.h>
21 #include <Commons/Regex.h>
23 // To get package id from appId
24 #include <package_manager.h>
25 #include <TimeTracer.h>
30 namespace Application {
33 using namespace WrtDeviceApis::Commons;
36 AppManagerWrapper::AppManagerWrapper() :
37 m_manager_handle(NULL),
43 AppManagerWrapper::~AppManagerWrapper()
46 if (m_manager_handle != NULL)
48 unregisterAppListChangedCallbacks();
52 void AppManagerWrapper::registerAppListChangedCallbacks(
53 IAppManagerAppListChangedCallbacks *callbacks)
56 if (callbacks == NULL)
58 LOGE("callback cannot be set to NULL.");
62 if (!m_manager_handle)
64 registerAppListChangedCallbacks();
67 m_callbacks.insert(callbacks);
70 void AppManagerWrapper::unregisterAppListChangedCallbacks(
71 IAppManagerAppListChangedCallbacks *callbacks)
74 if (callbacks == NULL)
76 LOGE("callback cannot be set to NULL.");
80 if (m_callbacks.size() == 0)
82 LOGE("No callbacks are registered.");
86 AppListChangedCallbacksSet::iterator iter = m_callbacks.find(callbacks);
87 if (iter == m_callbacks.end())
89 LOGE("Callback is not registered.");
93 m_callbacks.erase(iter);
95 if (m_callbacks.size() == 0)
97 unregisterAppListChangedCallbacks();
102 long AppManagerWrapper::getWatchIdAndInc()
105 return ++m_watchIdAcc;
109 bool AppManagerWrapper::app_callback(package_info_app_component_type_e comp_type,
114 if (app_id == NULL) {
115 LOGE("Callback is called. but no package name is passed. skip this request");
119 if (user_data == NULL) {
120 LOGE("user data is not exist. skip this request");
124 AppManagerWrapper *appManager = (AppManagerWrapper *)user_data;
125 appManager->applist.push_back(app_id);
130 void AppManagerWrapper::appListChangedCallback(app_info_event_e event_type,
135 if (user_data == NULL) {
136 LOGE("user data is not exist. skip this request");
140 AppManagerWrapper *appManager = (AppManagerWrapper *)user_data;
142 if (event_type == APP_INFO_EVENT_UNINSTALLED) {
143 for (size_t i = 0; i < appManager->applist.size(); i++) {
144 appListAppUninstalled(appManager->applist.at(i).c_str());
147 package_info_h package_info;
149 int ret = package_info_create(pkgId, &package_info);
150 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
151 LOGE("%s", ApplicationUtil::getApplicationErrorMessage(ret,
152 "package_info_create()",
153 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
157 ret = package_info_foreach_app_from_package(package_info,
159 app_callback, user_data);
160 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
161 LOGE("%s", ApplicationUtil::getApplicationErrorMessage(ret,
162 "package_info_foreach_app_from_package()",
163 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
165 ret = package_info_destroy(package_info);
166 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
167 LOGW("%s", ApplicationUtil::getApplicationErrorMessage(ret,
168 "package_info_destroy()",
169 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
174 ret = package_info_destroy(package_info);
175 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
176 LOGW("%s", ApplicationUtil::getApplicationErrorMessage(ret,
177 "package_info_destroy()",
178 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
181 for (size_t i = 0; i < appManager->applist.size(); i++) {
184 case APP_INFO_EVENT_INSTALLED:
185 appListAppInstalled(appManager->applist.at(i).c_str());
187 case APP_INFO_EVENT_UPDATED:
188 appListAppUpdated(appManager->applist.at(i).c_str());
191 LOGE("app_manager listener gave wrong event_type : %d", event_type);
198 appManager->applist.clear();
202 void AppManagerWrapper::appListAppInstalled(const char *appId)
205 AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
206 for (; iter != m_callbacks.end(); iter++)
208 (*iter)->onAppManagerEventInstalled(appId);
212 void AppManagerWrapper::appListAppUninstalled(const char *appId)
215 AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
216 for (; iter != m_callbacks.end(); iter++)
218 (*iter)->onAppManagerEventUninstalled(appId);
222 void AppManagerWrapper::appListAppUpdated(const char *appId)
225 AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
226 for (; iter != m_callbacks.end(); iter++)
228 (*iter)->onAppManagerEventUpdated(appId);
233 int AppManagerWrapper::app_list_changed_cb_broker(int id,
242 static app_info_event_e event_type;
244 if (!strcasecmp(key, "start")) {
245 if (!strcasecmp(val, "install")) {
246 event_type = APP_INFO_EVENT_INSTALLED;
247 } else if (!strcasecmp(val, "uninstall")) {
248 // After uninstallation, we cannot get app ids from package name.
249 // So, we have to store app ids which is included to target package.
250 package_info_h package_info;
252 int ret = package_info_create(package, &package_info);
253 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
254 LOGW("%s", ApplicationUtil::getApplicationErrorMessage(ret,
255 "package_info_create()",
256 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
259 ret = package_info_foreach_app_from_package(package_info,
260 PACKAGE_INFO_ALLAPP, app_callback, data);
261 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
262 LOGW("%s", ApplicationUtil::getApplicationErrorMessage(ret,
263 "package_info_foreach_app_from_package()",
264 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
267 ret = package_info_destroy(package_info);
268 if (PACKAGE_MANAGER_ERROR_NONE != ret) {
269 LOGW("%s", ApplicationUtil::getApplicationErrorMessage(ret,
270 "package_info_destroy()",
271 ApplicationUtil::getApplicationPackageManagerMessage).c_str());
273 event_type = APP_INFO_EVENT_UNINSTALLED;
274 } else if (!strcasecmp(val, "update")) {
275 event_type = APP_INFO_EVENT_UPDATED;
277 } else if (!strcasecmp(key, "end") && !strcasecmp(val, "ok")) {
278 if (event_type >= 0) {
280 AppManagerWrapper *appManager = (AppManagerWrapper *)data;
281 appManager->appListChangedCallback(event_type, package, data);
286 return APP_MANAGER_ERROR_NONE;
290 void AppManagerWrapper::registerAppListChangedCallbacks()
293 if (m_manager_handle != NULL) {
294 LOGW("Callback is already registered.");
298 TIME_TRACER_ITEM_BEGIN("(addAppInfoEventListener)pkgmgr_client_new", 0);
299 m_manager_handle = pkgmgr_client_new(PC_LISTENING);
300 TIME_TRACER_ITEM_END("(addAppInfoEventListener)pkgmgr_client_new", 0);
301 if (NULL == m_manager_handle) {
302 LOGE("pkgmgr_client: NULL");
303 ApplicationUtil::throwApplicationException<InvalidArgumentException>(
305 "pkgmgr_client_new()",
306 ApplicationUtil::getApplicationPkgmgrinfoMessage);
309 TIME_TRACER_ITEM_BEGIN("(addAppInfoEventListener)pkgmgr_client_listen_status", 0);
310 int request_id = pkgmgr_client_listen_status(m_manager_handle,
311 app_list_changed_cb_broker,
313 TIME_TRACER_ITEM_END("(addAppInfoEventListener)pkgmgr_client_listen_status", 0);
314 if (request_id < 0) {
315 LOGE("ret: %d", request_id);
316 ApplicationUtil::throwApplicationException<PlatformException>(request_id,
317 "pkgmgr_client_listen_status()",
318 ApplicationUtil::getApplicationPkgmgrinfoMessage);
322 void AppManagerWrapper::unregisterAppListChangedCallbacks()
325 if (m_manager_handle == NULL) {
326 LOGE("No callback is registered");
330 TIME_TRACER_ITEM_BEGIN("(removeAppInfoEventListener)pkgmgr_client_free", 0);
331 int ret = pkgmgr_client_free(m_manager_handle);
332 if (PKGMGR_R_OK != ret) {
333 LOGW("%s", ApplicationUtil::getApplicationErrorMessage(ret,
334 "pkgmgr_client_free()",
335 ApplicationUtil::getApplicationPkgmgrinfoMessage).c_str());
337 TIME_TRACER_ITEM_END("(removeAppInfoEventListener)pkgmgr_client_free", 0);
338 m_manager_handle = NULL;
341 bool AppManagerWrapper::initializeAppInfo(ApplicationInformationPtr &appInfo)
344 pkgmgr_client *pc = NULL;
348 // get installed size from package server (to solve smack issue)
349 pc = pkgmgr_client_new(PC_REQUEST);
352 LOGE("%s", ApplicationUtil::getApplicationErrorMessage(PKGMGR_R_ERROR,
353 "pkgmgr_client_new()",
354 ApplicationUtil::getApplicationPkgmgrinfoMessage).c_str());
357 ret = pkgmgr_client_request_service(PM_REQUEST_GET_SIZE,
358 PM_GET_TOTAL_SIZE, pc, NULL, appInfo->getPackageId().c_str(),
361 LOGE("%s", ApplicationUtil::getApplicationErrorMessage(ret,
362 "pkgmgr_client_request_service()",
363 ApplicationUtil::getApplicationPkgmgrinfoMessage).c_str());
366 appInfo->setInstallSize(ret);
369 pkgmgr_client_free(pc);
370 appInfo->setInitialize();
374 SINGLETON_IMPLEMENTATION(AppManagerWrapper)