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();
85 if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL) {
86 apphistory_stmt hstmt = NULL;
87 char query[APPHISTORY_SQL_LEN_MAX] = {0, };
89 APPHISTORY_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId) VALUES " "(?) ", APP_TABLE_REMOVABLE_APP);
91 hstmt = database->prepare_query(query);
92 database->query_bind_text(hstmt, count, appId);
93 database->execute(hstmt);
95 } else if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL) {
96 database->execute(__createDeletionQuery(APP_TABLE_REMOVABLE_APP, appId));
97 database->execute(__createDeletionQuery(APP_TABLE_USAGE_LOG, appId));
103 apphistory_stmt ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId)
105 Database *database = DatabaseManager::getInstance();
108 apphistory_stmt hstmt = NULL;
109 char query[APPHISTORY_SQL_LEN_MAX] = {0, };
111 APPHISTORY_SNPRINTF(query, sizeof(query), "DELETE FROM %s(AppId) WHERE KEY_APP_ID = ? ", tableName);
113 hstmt = database->prepare_query(query);
114 database->query_bind_text(hstmt, count, appId);