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"
20 #include <dpl/log/log.h>
21 #include <dpl/singleton_impl.h>
22 #include <Commons/Exception.h>
23 #include <Commons/Regex.h>
25 // To get package id from appId
26 #include <package_manager.h>
28 IMPLEMENT_SINGLETON(DeviceAPI::Application::AppManagerWrapper)
31 namespace Application {
34 using namespace WrtDeviceApis::Commons;
37 AppManagerWrapper::AppManagerWrapper() :
38 m_registeredCallback(false),
43 AppManagerWrapper::~AppManagerWrapper()
45 if(m_registeredCallback)
47 unregisterAppListChangedCallbacks();
51 void AppManagerWrapper::registerAppListChangedCallbacks(IAppManagerAppListChangedCallbacks *callbacks)
55 LogWarning("Parameter is NULL.");
59 if(!m_registeredCallback)
61 registerAppListChangedCallbacks();
64 m_callbacks.insert(callbacks);
67 void AppManagerWrapper::unregisterAppListChangedCallbacks(IAppManagerAppListChangedCallbacks *callbacks)
71 LogWarning("Parameter is NULL.");
75 if(m_callbacks.size() == 0)
77 LogWarning("No callbacks are registered.");
81 AppListChangedCallbacksSet::iterator iter = m_callbacks.find(callbacks);
82 if(iter == m_callbacks.end())
84 LogWarning("Callbacks " << callbacks << " are not registered.");
88 m_callbacks.erase(iter);
90 if(m_callbacks.size() == 0)
92 unregisterAppListChangedCallbacks();
97 long AppManagerWrapper::getWatchIdAndInc()
99 return ++m_watchIdAcc;
102 void AppManagerWrapper::appListChangedCallback(app_manger_event_type_e event_type,
106 if(user_data == NULL)
108 LogWarning("app_manager listener passed NULL user_data");
112 AppManagerWrapper *appManager = (AppManagerWrapper *)user_data;
114 appManager->appListChangedCallback(event_type, appId);
117 void AppManagerWrapper::appListChangedCallback(app_manger_event_type_e event_type,
122 LogWarning("app_manager listener gave a NULL package name.");
128 case APP_MANAGER_EVENT_INSTALLED:
129 appListAppInstalled(appId);
131 case APP_MANAGER_EVENT_UNINSTALLED:
132 appListAppUninstalled(appId);
134 case APP_MANAGER_EVENT_UPDATED:
135 appListAppUpdated(appId);
138 LogWarning("app_manager listener gave wrong event_type : " << event_type);
143 void AppManagerWrapper::appListAppInstalled(const char *appId)
145 LogDebug("app installed : " << appId);
147 AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
148 for(; iter != m_callbacks.end(); iter++)
150 (*iter)->onAppManagerEventInstalled(appId);
154 void AppManagerWrapper::appListAppUninstalled(const char *appId)
156 LogDebug("app uninstalled : " << appId);
158 AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
159 for(; iter != m_callbacks.end(); iter++)
161 (*iter)->onAppManagerEventUninstalled(appId);
165 void AppManagerWrapper::appListAppUpdated(const char *appId)
167 LogDebug("app updated : " << appId);
169 AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
170 for(; iter != m_callbacks.end(); iter++)
172 (*iter)->onAppManagerEventUpdated(appId);
176 void AppManagerWrapper::registerAppListChangedCallbacks()
178 if(m_registeredCallback)
180 LogWarning("Callback already registered.");
184 // AppManagerWrapper will be used as a singleton object,
185 // so this pointer of it can be passed as a parameter of a async function.
186 int result = app_manager_set_app_list_changed_cb(appListChangedCallback, this);
187 if (result != APP_MANAGER_ERROR_NONE)
189 LogWarning("app_manager_set_app_list_changed_cb() returns APP_MANAGER_ERROR_INVALID_PARAMETER");
191 ThrowMsg(InvalidArgumentException, "Error while registering listener to app_manager");
194 m_registeredCallback = true;
197 void AppManagerWrapper::unregisterAppListChangedCallbacks()
199 if(!m_registeredCallback)
201 LogWarning("Callback already unregistered.");
205 app_manager_unset_app_list_changed_cb();
207 m_registeredCallback = false;
210 // @20130125-wscho: current pkgmanager has a problem while db operation.
211 // So, below function is temporal function. all attribute should be filled to appInfoObject
212 // when that is created.
213 // See ApplicationManager.cpp
214 bool AppManagerWrapper::initializeAppInfo(ApplicationInformationPtr &appInfo)
216 std::string appId = appInfo->getAppId();
217 char* packageId = NULL;
220 //LogDebug("InitializeAppInfo for " << appInfo->getAppId().c_str());
222 ret = package_manager_get_package_id_by_app_id(appId.c_str(), &packageId);
223 if (ret != PACKAGE_MANAGER_ERROR_NONE)
225 LogDebug("Fail to get package id with appId : " << appId.c_str());
228 // Below code is already run on ApplicationManager.cpp
231 char* iconPath = NULL;
232 bool noDisplay = false;
233 pkgmgrinfo_appinfo_h appinfo_h;
235 ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &appinfo_h);
236 if (ret != PMINFO_R_OK) {
237 LogDebug("Fail to get appinfo by " << appId.c_str());
240 ret = pkgmgrinfo_appinfo_get_label(appinfo_h, &name);
241 if (ret != PMINFO_R_OK) {
242 LogDebug("Fail to get name by " << appId.c_str());
244 appInfo->setName(name);
246 ret = pkgmgrinfo_appinfo_get_icon(appinfo_h, &iconPath);
247 if (ret != PMINFO_R_OK) {
248 LogDebug("Fail to get icon by " << appId.c_str());
250 appInfo->setIconPath(iconPath);
252 ret = pkgmgrinfo_appinfo_is_nodisplay(appinfo_h, &noDisplay);
253 if (ret != PMINFO_R_OK) {
254 LogDebug("Fail to get nodisplay by " << appId.c_str());
256 appInfo->setShow(!noDisplay);
258 ret = pkgmgrinfo_appinfo_foreach_category(appinfo_h, category_cb, (void*)appInfo.Get());
261 LogDebug("Fail to get categories " << appId.c_str());
264 ret = pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
267 LogDebug("Fail to destroy " << appId.c_str());
271 char *version = NULL;
273 int installed_time = 0;
274 pkgmgrinfo_pkginfo_h pkginfo_h;
276 ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &pkginfo_h);
277 if (ret != PMINFO_R_OK) {
278 LogDebug("Fail to get package info");
281 ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
282 if (ret != PMINFO_R_OK) {
283 LogDebug("Fail to get version");
285 appInfo->setVersion(version);
287 ret = pkgmgrinfo_pkginfo_get_total_size(pkginfo_h, &total_size);
288 if (ret == PMINFO_R_OK) {
289 appInfo->setInstallSize(total_size);
292 ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
293 if (ret == PMINFO_R_OK) {
294 appInfo->setInstallDate(installed_time);
297 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
299 appInfo->setInitialize();