eda93cf8c808e166ef8fb3274790aeee7788674e
[platform/core/context/app-history.git] / src / server / usage-stats / InstallMonitor.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <sstream>
18 #include <AppHistoryTypes.h>
19 #include <AppHistoryTypesPrivate.h>
20 #include "../DatabaseManager.h"
21 #include "AppStatisticsTypes.h"
22 #include "InstallMonitor.h"
23
24 static package_manager_event_type_e __lastEventType;
25
26 ctx::AppInstallMonitor::AppInstallMonitor() :
27         __pkgmgrHandle(NULL)
28 {
29         __startMonitoring();
30 }
31
32 ctx::AppInstallMonitor::~AppInstallMonitor()
33 {
34         __stopMonitoring();
35 }
36
37 bool ctx::AppInstallMonitor::__startMonitoring()
38 {
39         int err = package_manager_create(&__pkgmgrHandle);
40         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_create() failed");
41
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");
44
45         return true;
46 }
47
48 void ctx::AppInstallMonitor::__stopMonitoring()
49 {
50         if(__pkgmgrHandle) {
51                 package_manager_unset_event_cb(__pkgmgrHandle);
52                 package_manager_destroy(__pkgmgrHandle);
53                 __pkgmgrHandle = NULL;
54         }
55 }
56
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)
58 {
59         IF_FAIL_VOID_TAG(error == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager error: %d", error);
60
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);
64                 return;
65         }
66
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");
70
71         __lastEventType = eventType;
72
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");
76
77         package_info_destroy(pkgInfo);
78 }
79
80 bool ctx::AppInstallMonitor::__appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData)
81 {
82         Database* database = DatabaseManager::getInstance();
83
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);
91         }
92
93         return true;
94 }
95
96 std::string ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId)
97 {
98         std::stringstream query;
99         query << "DELETE FROM " << tableName;
100         query << " WHERE " KEY_APP_ID " = '" << appId << "'";
101         return query.str();
102 }