Code cleanup: merge common, system, social types into one provider types header
[platform/core/context/context-provider.git] / src / app-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 <glib.h>
19 #include <Json.h>
20 #include <Types.h>
21 #include <DatabaseManager.h>
22 #include "AppStatisticsTypes.h"
23 #include "InstallMonitor.h"
24
25 static package_manager_event_type_e __lastEventType;
26
27 ctx::AppInstallMonitor::AppInstallMonitor() :
28         __pkgmgrHandle(NULL)
29 {
30         __startMonitoring();
31 }
32
33 ctx::AppInstallMonitor::~AppInstallMonitor()
34 {
35         __stopMonitoring();
36 }
37
38 bool ctx::AppInstallMonitor::__startMonitoring()
39 {
40         int err = package_manager_create(&__pkgmgrHandle);
41         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_create() failed");
42
43         err = package_manager_set_event_cb(__pkgmgrHandle, __packageEventCb, this);
44         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_set_event_cb() failed");
45
46         return true;
47 }
48
49 void ctx::AppInstallMonitor::__stopMonitoring()
50 {
51         if(__pkgmgrHandle) {
52                 package_manager_unset_event_cb(__pkgmgrHandle);
53                 package_manager_destroy(__pkgmgrHandle);
54                 __pkgmgrHandle = NULL;
55         }
56 }
57
58 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 {
60         IF_FAIL_VOID_TAG(error == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager error: %d", error);
61
62         if (!(eventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL && eventState == PACKAGE_MANAGER_EVENT_STATE_COMPLETED) &&
63                 !(eventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL && eventState == PACKAGE_MANAGER_EVENT_STATE_STARTED)) {
64                 _D("Skipping event type-%d / state-%d", eventType, eventState);
65                 return;
66         }
67
68         package_info_h pkgInfo;
69         int err = package_manager_get_package_info(package, &pkgInfo);
70         IF_FAIL_VOID_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_get_package_info() failed");
71
72         __lastEventType = eventType;
73
74         err = package_info_foreach_app_from_package(pkgInfo, PACKAGE_INFO_UIAPP, __appInfoCb, userData);
75         if (err != PACKAGE_MANAGER_ERROR_NONE)
76                 _E("package_info_foreach_app_from_package() failed");
77
78         package_info_destroy(pkgInfo);
79 }
80
81 bool ctx::AppInstallMonitor::__appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData)
82 {
83         DatabaseManager dbManager;
84
85         if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL) {
86                 Json data;
87                 data.set(NULL, KEY_APP_ID, appId);
88                 dbManager.insert(0, APP_TABLE_REMOVABLE_APP, data, NULL);
89         } else if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL) {
90                 dbManager.execute(0, __createDeletionQuery(APP_TABLE_REMOVABLE_APP, appId).c_str(), NULL);
91                 dbManager.execute(0, __createDeletionQuery(APP_TABLE_USAGE_LOG, appId).c_str(), NULL);
92         }
93
94         return true;
95 }
96
97 std::string ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId)
98 {
99         std::stringstream query;
100         query << "DELETE FROM " << tableName;
101         query << " WHERE " KEY_APP_ID " = '" << appId << "'";
102         return query.str();
103 }