2 * Copyright (c) 2015 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 #include <AppHistoryTypes.h>
19 #include <AppHistoryTypesPrivate.h>
20 #include "../DatabaseManager.h"
21 #include "AppStatisticsTypes.h"
22 #include "InstallMonitor.h"
24 static package_manager_event_type_e __lastEventType;
26 ctx::AppInstallMonitor::AppInstallMonitor() :
32 ctx::AppInstallMonitor::~AppInstallMonitor()
37 bool ctx::AppInstallMonitor::__startMonitoring()
39 int err = package_manager_create(&__pkgmgrHandle);
40 IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_create() failed");
42 err = package_manager_set_event_cb(__pkgmgrHandle, __packageEventCb, this);
43 IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_set_event_cb() failed");
48 void ctx::AppInstallMonitor::__stopMonitoring()
51 package_manager_unset_event_cb(__pkgmgrHandle);
52 package_manager_destroy(__pkgmgrHandle);
53 __pkgmgrHandle = NULL;
57 void ctx::AppInstallMonitor::__packageEventCb(const char *type, const char *package, package_manager_event_type_e eventType, package_manager_event_state_e eventState, int progress, package_manager_error_e error, void *userData)
59 IF_FAIL_VOID_TAG(error == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager error: %d", error);
61 if (!(eventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL && eventState == PACKAGE_MANAGER_EVENT_STATE_COMPLETED) &&
62 !(eventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL && eventState == PACKAGE_MANAGER_EVENT_STATE_STARTED)) {
63 _D("Skipping event type-%d / state-%d", eventType, eventState);
67 package_info_h pkgInfo;
68 int err = package_manager_get_package_info(package, &pkgInfo);
69 IF_FAIL_VOID_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_get_package_info() failed");
71 __lastEventType = eventType;
73 err = package_info_foreach_app_from_package(pkgInfo, PACKAGE_INFO_UIAPP, __appInfoCb, userData);
74 if (err != PACKAGE_MANAGER_ERROR_NONE)
75 _E("package_info_foreach_app_from_package() failed");
77 package_info_destroy(pkgInfo);
80 bool ctx::AppInstallMonitor::__appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData)
82 Database* database = DatabaseManager::getInstance();
84 if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL) {
85 std::stringstream query;
86 query << "INSERT INTO " APP_TABLE_REMOVABLE_APP " (" KEY_APP_ID ") VALUES ('" << appId << "')";
87 database->execute(query.str(), NULL);
88 } else if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL) {
89 database->execute(__createDeletionQuery(APP_TABLE_REMOVABLE_APP, appId), NULL);
90 database->execute(__createDeletionQuery(APP_TABLE_USAGE_LOG, appId), NULL);
96 std::string ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId)
98 std::stringstream query;
99 query << "DELETE FROM " << tableName;
100 query << " WHERE " KEY_APP_ID " = '" << appId << "'";