1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "event_controller.h"
17
18 #include "application_info.h"
19 #include "bundle_constants.h"
20 #include "bundle_mgr_interface.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "hisysevent.h"
24 #include "if_system_ability_manager.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28
29 #include "res_sched_log.h"
30 #include "res_sched_mgr.h"
31 #include "res_type.h"
32
33 using namespace OHOS::EventFwk;
34 namespace OHOS {
35 namespace ResourceSchedule {
36 IMPLEMENT_SINGLE_INSTANCE(EventController);
37
Init()38 void EventController::Init()
39 {
40 if (sysAbilityListener_ != nullptr) {
41 return;
42 }
43 sysAbilityListener_ = new (std::nothrow) SystemAbilityStatusChangeListener();
44 if (sysAbilityListener_ == nullptr) {
45 RESSCHED_LOGE("Failed to create statusChangeListener due to no memory.");
46 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
47 "COMPONENT_NAME", "MAIN",
48 "ERR_TYPE", "others",
49 "ERR_MSG", "EventController new statusChangeListener object failed!");
50 return;
51 }
52 sptr<ISystemAbilityManager> systemAbilityManager
53 = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54 if (systemAbilityManager == nullptr) {
55 RESSCHED_LOGE("systemAbilityManager is null");
56 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
57 "COMPONENT_NAME", "MAIN",
58 "ERR_TYPE", "register failure",
59 "ERR_MSG", "EventController get system ability manager failed!");
60 sysAbilityListener_ = nullptr;
61 return;
62 }
63 int32_t ret = systemAbilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, sysAbilityListener_);
64 if (ret != ERR_OK) {
65 RESSCHED_LOGE("subscribe system ability id: %{public}d failed", COMMON_EVENT_SERVICE_ID);
66 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
67 "COMPONENT_NAME", "MAIN",
68 "ERR_TYPE", "register failure",
69 "ERR_MSG", "EventController subscribe the event service SA failed!");
70 sysAbilityListener_ = nullptr;
71 }
72 }
73
HandlePkgAddRemove(const EventFwk::Want & want,nlohmann::json & payload) const74 void EventController::HandlePkgAddRemove(const EventFwk::Want &want, nlohmann::json &payload) const
75 {
76 AppExecFwk::ElementName ele = want.GetElement();
77 std::string bundleName = ele.GetBundleName();
78 int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
79 payload["bundleName"] = bundleName;
80 payload["uid"] = uid;
81 }
82
GetUid(const int32_t & userId,const std::string & bundleName) const83 int32_t EventController::GetUid(const int32_t &userId, const std::string &bundleName) const
84 {
85 AppExecFwk::ApplicationInfo info;
86 sptr<ISystemAbilityManager> systemAbilityManager
87 = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (systemAbilityManager == nullptr) {
89 RESSCHED_LOGE("Failed to get uid due to get systemAbilityManager is null.");
90 return -1;
91 }
92 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
93 if (remoteObject == nullptr) {
94 RESSCHED_LOGE("Failed to get uid due to get BMS is null.");
95 return -1;
96 }
97 sptr<AppExecFwk::IBundleMgr> bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
98 bundleMgr->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, info);
99 return static_cast<int32_t>(info.uid);
100 }
101
HandleConnectivityChange(const EventFwk::Want & want,const int32_t & code,nlohmann::json & payload)102 void EventController::HandleConnectivityChange(
103 const EventFwk::Want &want, const int32_t &code, nlohmann::json &payload)
104 {
105 int32_t netType = want.GetIntParam("NetType", -1);
106 if (netType != 1) {
107 return;
108 }
109 ReportDataInProcess(ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE, code, payload);
110 }
111
ReportDataInProcess(const uint32_t & resType,const int64_t & value,const nlohmann::json & payload)112 void EventController::ReportDataInProcess(const uint32_t &resType, const int64_t &value, const nlohmann::json& payload)
113 {
114 ResSchedMgr::GetInstance().ReportData(resType, value, payload);
115 resType_ = resType;
116 value_ = value;
117 payload_ = payload;
118 }
119
Stop()120 void EventController::Stop()
121 {
122 if (sysAbilityListener_ == nullptr) {
123 return;
124 }
125 sysAbilityListener_->Stop();
126 }
127
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)128 void EventController::SystemAbilityStatusChangeListener::OnAddSystemAbility(
129 int32_t systemAbilityId, const std::string& deviceId)
130 {
131 MatchingSkills matchingSkills;
132 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
133 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
134 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
135 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
136 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED);
137 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
138 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
139 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
140 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
141 matchingSkills.AddEvent("common.event.UNLOCK_SCREEN");
142 matchingSkills.AddEvent("common.event.LOCK_SCREEN");
143 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
144 subscriber_ = std::make_shared<EventController>(subscriberInfo);
145 if (CommonEventManager::SubscribeCommonEvent(subscriber_)) {
146 RESSCHED_LOGD("SubscribeCommonEvent ok");
147 } else {
148 RESSCHED_LOGW("SubscribeCommonEvent fail");
149 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
150 "COMPONENT_NAME", "MAIN",
151 "ERR_TYPE", "register failure",
152 "ERR_MSG", "EventController subscribe common events failed!");
153 }
154 }
155
OnReceiveEvent(const EventFwk::CommonEventData & data)156 void EventController::OnReceiveEvent(const EventFwk::CommonEventData &data)
157 {
158 Want want = data.GetWant();
159 std::string action = want.GetAction();
160 RESSCHED_LOGD("Recieved common event:%{public}s", action.c_str());
161
162 nlohmann::json payload = nlohmann::json::object();
163 if (HandlePkgCommonEvent(action, want, payload)) {
164 return;
165 }
166 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
167 ReportDataInProcess(ResType::RES_TYPE_SCREEN_STATUS, ResType::ScreenStatus::SCREEN_ON, payload);
168 return;
169 }
170 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
171 ReportDataInProcess(ResType::RES_TYPE_SCREEN_STATUS, ResType::ScreenStatus::SCREEN_OFF, payload);
172 return;
173 }
174 if (action == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
175 int32_t code = data.GetCode();
176 HandleConnectivityChange(want, code, payload);
177 return;
178 }
179 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
180 int32_t userId = data.GetCode();
181 ReportDataInProcess(ResType::RES_TYPE_USER_SWITCH, static_cast<int64_t>(userId), payload);
182 return;
183 }
184 if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
185 int32_t userId = data.GetCode();
186 ReportDataInProcess(ResType::RES_TYPE_USER_REMOVE, static_cast<int64_t>(userId), payload);
187 return;
188 }
189 if (action == "common.event.UNLOCK_SCREEN") {
190 ReportDataInProcess(ResType::RES_TYPE_SCREEN_LOCK, ResType::ScreenLockStatus::SCREEN_UNLOCK, payload);
191 return;
192 }
193 if (action == "common.event.LOCK_SCREEN") {
194 ReportDataInProcess(ResType::RES_TYPE_SCREEN_LOCK, ResType::ScreenLockStatus::SCREEN_LOCK, payload);
195 return;
196 }
197 }
198
HandlePkgCommonEvent(const std::string & action,Want & want,nlohmann::json & payload)199 bool EventController::HandlePkgCommonEvent(const std::string &action, Want &want, nlohmann::json &payload)
200 {
201 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
202 HandlePkgAddRemove(want, payload);
203 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_UNINSTALL, payload);
204 return true;
205 }
206 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
207 HandlePkgAddRemove(want, payload);
208 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_INSTALL, payload);
209 return true;
210 }
211 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
212 HandlePkgAddRemove(want, payload);
213 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_CHANGED, payload);
214 return true;
215 }
216 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED) {
217 HandlePkgAddRemove(want, payload);
218 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_REPLACED, payload);
219 return true;
220 }
221 return false;
222 }
223
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)224 void EventController::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
225 int32_t systemAbilityId, const std::string& deviceId)
226 {
227 RESSCHED_LOGW("common event service is removed.");
228 subscriber_ = nullptr;
229 }
230
Stop()231 void EventController::SystemAbilityStatusChangeListener::Stop()
232 {
233 if (subscriber_ == nullptr) {
234 return;
235 }
236 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
237 subscriber_ = nullptr;
238 }
239
EventControllerInit()240 extern "C" void EventControllerInit()
241 {
242 EventController::GetInstance().Init();
243 }
244
EventControllerStop()245 extern "C" void EventControllerStop()
246 {
247 EventController::GetInstance().Stop();
248 }
249 }
250 }