1 /*
2 * Copyright (c) 2022-2025 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 "oobe_datashare_utils.h"
28 #include "oobe_manager.h"
29 #include "system_ability_definition.h"
30
31 #include "res_sched_log.h"
32 #include "res_sched_mgr.h"
33 #include "res_type.h"
34
35 using namespace OHOS::EventFwk;
36 namespace OHOS {
37 namespace ResourceSchedule {
38 IMPLEMENT_SINGLE_INSTANCE(EventController);
39
40 static const char* COMMON_EVENT_CAMERA_STATUS = "usual.event.CAMERA_STATUS";
41 static const char* DATA_SHARE_READY = "usual.event.DATA_SHARE_READY";
42 static const char* DEVICE_MODE_PAYMODE_NAME = "deviceMode";
43 static const char* DEVICE_MODE_TYPE_KEY = "deviceModeType";
44 static const char* SCENE_BOARD_NAME = "com.ohos.sceneboard";
45 static const char* CAMERA_STATE = "cameraState";
46 static const char* CAMERA_TYPE = "cameraType";
47 static const char* IS_SYSTEM_CAMERA = "isSystemCamera";
Init()48 void EventController::Init()
49 {
50 if (sysAbilityListener_ != nullptr) {
51 return;
52 }
53 sysAbilityListener_ = new (std::nothrow) SystemAbilityStatusChangeListener();
54 if (sysAbilityListener_ == nullptr) {
55 RESSCHED_LOGE("Failed to create statusChangeListener due to no memory.");
56 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
57 "COMPONENT_NAME", "MAIN",
58 "ERR_TYPE", "others",
59 "ERR_MSG", "EventController new statusChangeListener object failed!");
60 return;
61 }
62 sptr<ISystemAbilityManager> systemAbilityManager
63 = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (systemAbilityManager == nullptr) {
65 RESSCHED_LOGE("systemAbilityManager is null");
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 get system ability manager failed!");
70 sysAbilityListener_ = nullptr;
71 return;
72 }
73 int32_t ret = systemAbilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, sysAbilityListener_);
74 if (ret != ERR_OK) {
75 RESSCHED_LOGE("subscribe system ability id: %{public}d failed", COMMON_EVENT_SERVICE_ID);
76 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
77 "COMPONENT_NAME", "MAIN",
78 "ERR_TYPE", "register failure",
79 "ERR_MSG", "EventController subscribe the event service SA failed!");
80 sysAbilityListener_ = nullptr;
81 } else {
82 RESSCHED_LOGI("subscribe system ability id: %{public}d succeed", COMMON_EVENT_SERVICE_ID);
83 }
84 }
85
HandlePkgAddRemove(const EventFwk::Want & want,nlohmann::json & payload) const86 void EventController::HandlePkgAddRemove(const EventFwk::Want &want, nlohmann::json &payload) const
87 {
88 AppExecFwk::ElementName ele = want.GetElement();
89 std::string bundleName = ele.GetBundleName();
90 int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
91 int32_t appIndex = want.GetIntParam("appIndex", -1);
92 payload["bundleName"] = bundleName;
93 payload["uid"] = uid;
94 payload["appIndex"] = appIndex;
95 }
96
GetUid(const int32_t & userId,const std::string & bundleName) const97 int32_t EventController::GetUid(const int32_t &userId, const std::string &bundleName) const
98 {
99 AppExecFwk::ApplicationInfo info;
100 sptr<ISystemAbilityManager> systemAbilityManager
101 = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 if (systemAbilityManager == nullptr) {
103 RESSCHED_LOGE("Failed to get uid due to get systemAbilityManager is null.");
104 return -1;
105 }
106 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
107 if (remoteObject == nullptr) {
108 RESSCHED_LOGE("Failed to get uid due to get BMS is null.");
109 return -1;
110 }
111 sptr<AppExecFwk::IBundleMgr> bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
112 if (bundleMgr == nullptr) {
113 RESSCHED_LOGE("Failed to get uid due to get bundleMgr is null.");
114 return -1;
115 }
116 bundleMgr->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, info);
117 return static_cast<int32_t>(info.uid);
118 }
119
HandleConnectivityChange(const EventFwk::Want & want,const int32_t & code,nlohmann::json & payload)120 void EventController::HandleConnectivityChange(
121 const EventFwk::Want &want, const int32_t &code, nlohmann::json &payload)
122 {
123 int32_t netType = want.GetIntParam("NetType", -1);
124 if (netType != 1) {
125 return;
126 }
127 ReportDataInProcess(ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE, code, payload);
128 }
129
ReportDataInProcess(const uint32_t & resType,const int64_t & value,const nlohmann::json & payload)130 void EventController::ReportDataInProcess(const uint32_t &resType, const int64_t &value, const nlohmann::json& payload)
131 {
132 ResSchedMgr::GetInstance().ReportData(resType, value, payload);
133 resType_ = resType;
134 value_ = value;
135 payload_ = payload;
136 }
137
Stop()138 void EventController::Stop()
139 {
140 if (sysAbilityListener_ == nullptr) {
141 return;
142 }
143 sysAbilityListener_->Stop();
144 }
145
DataShareIsReady()146 void EventController::DataShareIsReady()
147 {
148 RESSCHED_LOGI("Data_share is ready! Call back to create data_share helper");
149 ResourceSchedule::OOBEManager::GetInstance().OnReceiveDataShareReadyCallBack();
150 }
151
SubscribeCommonEvent(std::shared_ptr<EventController> subscriber)152 inline void SubscribeCommonEvent(std::shared_ptr<EventController> subscriber)
153 {
154 if (CommonEventManager::SubscribeCommonEvent(subscriber)) {
155 RESSCHED_LOGI("SubscribeCommonEvent ok");
156 } else {
157 RESSCHED_LOGW("SubscribeCommonEvent fail");
158 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
159 "COMPONENT_NAME", "MAIN",
160 "ERR_TYPE", "register failure",
161 "ERR_MSG", "EventController subscribe common events failed!");
162 }
163 }
164
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)165 void EventController::SystemAbilityStatusChangeListener::OnAddSystemAbility(
166 int32_t systemAbilityId, const std::string& deviceId)
167 {
168 RESSCHED_LOGI("common event service is added");
169 MatchingSkills matchingSkills;
170 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
171 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_INSTALLATION_STARTED);
172 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
173 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
174 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
175 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED);
176 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
177 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
178 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
179 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
180 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
181 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED);
182 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
183 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
184 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED);
185 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_NITZ_TIMEZONE_CHANGED);
186 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
187 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
188 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISCHARGING);
189 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
190 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
191 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_WIFI_P2P_STATE_CHANGED);
192 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
193 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
194 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
195 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
196 matchingSkills.AddEvent(DATA_SHARE_READY);
197 matchingSkills.AddEvent(COMMON_EVENT_CAMERA_STATUS);
198 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
199 subscriber_ = std::make_shared<EventController>(subscriberInfo);
200 SubscribeCommonEvent(subscriber_);
201 SubscribeLockScreenCommonEvent();
202 }
203
SubscribeLockScreenCommonEvent()204 void EventController::SystemAbilityStatusChangeListener::SubscribeLockScreenCommonEvent()
205 {
206 MatchingSkills lockScreenSkills;
207 lockScreenSkills.AddEvent("common.event.UNLOCK_SCREEN");
208 lockScreenSkills.AddEvent("common.event.LOCK_SCREEN");
209 CommonEventSubscribeInfo subscriberInfo(lockScreenSkills);
210 subscriberInfo.SetPublisherBundleName(SCENE_BOARD_NAME);
211 lockScreenSubscriber_ = std::make_shared<EventController>(subscriberInfo);
212 SubscribeCommonEvent(lockScreenSubscriber_);
213 }
214
OnReceiveEvent(const EventFwk::CommonEventData & data)215 void EventController::OnReceiveEvent(const EventFwk::CommonEventData &data)
216 {
217 Want want = data.GetWant();
218 std::string action = want.GetAction();
219 RESSCHED_LOGI("Recieved common event:%{public}s", action.c_str());
220
221 nlohmann::json payload = nlohmann::json::object();
222 if (HandlePkgCommonEvent(action, want, payload)) {
223 return;
224 }
225 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
226 ReportDataInProcess(ResType::RES_TYPE_SCREEN_STATUS, ResType::ScreenStatus::SCREEN_ON, payload);
227 return;
228 }
229 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
230 ReportDataInProcess(ResType::RES_TYPE_SCREEN_STATUS, ResType::ScreenStatus::SCREEN_OFF, payload);
231 return;
232 }
233 if (action == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
234 int32_t code = data.GetCode();
235 HandleConnectivityChange(want, code, payload);
236 return;
237 }
238 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
239 int32_t userId = data.GetCode();
240 ReportDataInProcess(ResType::RES_TYPE_USER_SWITCH, static_cast<int64_t>(userId), payload);
241 return;
242 }
243 if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
244 int32_t userId = data.GetCode();
245 ReportDataInProcess(ResType::RES_TYPE_USER_REMOVE, static_cast<int64_t>(userId), payload);
246 return;
247 }
248 if (action == "common.event.UNLOCK_SCREEN") {
249 ReportDataInProcess(ResType::RES_TYPE_SCREEN_LOCK, ResType::ScreenLockStatus::SCREEN_UNLOCK, payload);
250 return;
251 }
252 if (action == "common.event.LOCK_SCREEN") {
253 ReportDataInProcess(ResType::RES_TYPE_SCREEN_LOCK, ResType::ScreenLockStatus::SCREEN_LOCK, payload);
254 return;
255 }
256 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED) {
257 payload["state"] = want.GetIntParam("state", -1);
258 ReportDataInProcess(ResType::RES_TYPE_CALL_STATE_CHANGED, static_cast<int64_t>(data.GetCode()), payload);
259 return;
260 }
261 if (action == DATA_SHARE_READY) {
262 DataShareIsReady();
263 return;
264 }
265
266 handleEvent(data.GetCode(), action, payload, want);
267 }
268
handleEvent(int32_t userId,const std::string & action,nlohmann::json & payload,Want & want)269 void EventController::handleEvent(int32_t userId, const std::string &action, nlohmann::json &payload, Want &want)
270 {
271 if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) {
272 ReportDataInProcess(ResType::RES_TYPE_TIMEZONE_CHANGED, ResType::RES_TYPE_TIMEZONE_CHANGED, payload);
273 return;
274 }
275 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED) {
276 ReportDataInProcess(ResType::RES_TYPE_TIME_CHANGED, static_cast<int64_t>(userId), payload);
277 return;
278 }
279 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED) {
280 ReportDataInProcess(ResType::RES_TYPE_NITZ_TIME_CHANGED, static_cast<int64_t>(userId), payload);
281 return;
282 }
283 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIMEZONE_CHANGED) {
284 ReportDataInProcess(ResType::RES_TYPE_NITZ_TIMEZONE_CHANGED, static_cast<int64_t>(userId), payload);
285 return;
286 }
287 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING) {
288 ReportDataInProcess(ResType::RES_TYPE_CHARGING_DISCHARGING, ResType::ChargeStatus::EVENT_CHARGING, payload);
289 return;
290 }
291 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING) {
292 ReportDataInProcess(ResType::RES_TYPE_CHARGING_DISCHARGING, ResType::ChargeStatus::EVENT_DISCHARGING, payload);
293 return;
294 }
295 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED) {
296 ReportDataInProcess(ResType::RES_TYPE_USB_DEVICE, ResType::UsbDeviceStatus::USB_DEVICE_ATTACHED, payload);
297 return;
298 }
299 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED) {
300 ReportDataInProcess(ResType::RES_TYPE_USB_DEVICE, ResType::UsbDeviceStatus::USB_DEVICE_DETACHED, payload);
301 return;
302 }
303 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_P2P_STATE_CHANGED) {
304 ReportDataInProcess(ResType::RES_TYPE_WIFI_P2P_STATE_CHANGED, static_cast<int64_t>(userId), payload);
305 return;
306 }
307 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED) {
308 ReportDataInProcess(ResType::RES_TYPE_POWER_MODE_CHANGED, static_cast<int64_t>(userId), payload);
309 return;
310 }
311 handleOtherEvent(userId, action, payload, want);
312 }
313
handleOtherEvent(int32_t userId,const std::string & action,nlohmann::json & payload,Want & want)314 void EventController::handleOtherEvent(int32_t userId, const std::string &action, nlohmann::json &payload, Want &want)
315 {
316 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) {
317 RESSCHED_LOGI("report boot completed");
318 ReportDataInProcess(ResType::RES_TYPE_BOOT_COMPLETED,
319 ResType::BootCompletedStatus::START_BOOT_COMPLETED, payload);
320 return;
321 }
322 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) {
323 payload[DEVICE_MODE_TYPE_KEY] = "powerConnectStatus";
324 payload[DEVICE_MODE_PAYMODE_NAME] = "powerConnected";
325 ReportDataInProcess(ResType::RES_TYPE_DEVICE_MODE_STATUS,
326 ResType::DeviceModeStatus::MODE_ENTER, payload);
327 return;
328 }
329 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) {
330 payload[DEVICE_MODE_TYPE_KEY] = "powerConnectStatus";
331 payload[DEVICE_MODE_PAYMODE_NAME] = "powerDisConnected";
332 ReportDataInProcess(ResType::RES_TYPE_DEVICE_MODE_STATUS,
333 ResType::DeviceModeStatus::MODE_ENTER, payload);
334 return;
335 }
336 if (action == COMMON_EVENT_CAMERA_STATUS) {
337 RESSCHED_LOGI("report camera status completed");
338 payload[CAMERA_TYPE] = want.GetIntParam(IS_SYSTEM_CAMERA, 0);
339 ReportDataInProcess(ResType::RES_TYPE_REPORT_CAMERA_STATE,
340 static_cast<int64_t>(want.GetIntParam(CAMERA_STATE, 1)), payload);
341 return;
342 }
343 }
344
HandlePkgCommonEvent(const std::string & action,Want & want,nlohmann::json & payload)345 bool EventController::HandlePkgCommonEvent(const std::string &action, Want &want, nlohmann::json &payload)
346 {
347 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
348 HandlePkgAddRemove(want, payload);
349 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_UNINSTALL, payload);
350 return true;
351 }
352 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_INSTALLATION_STARTED) {
353 HandlePkgAddRemove(want, payload);
354 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL,
355 ResType::AppInstallStatus::APP_INSTALL_START, payload);
356 return true;
357 }
358 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
359 HandlePkgAddRemove(want, payload);
360 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL,
361 ResType::AppInstallStatus::APP_INSTALL_END, payload);
362 return true;
363 }
364 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
365 HandlePkgAddRemove(want, payload);
366 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_CHANGED, payload);
367 return true;
368 }
369 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED) {
370 HandlePkgAddRemove(want, payload);
371 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_REPLACED, payload);
372 return true;
373 }
374 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) {
375 HandlePkgAddRemove(want, payload);
376 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL,
377 ResType::AppInstallStatus::APP_FULLY_REMOVED, payload);
378 return true;
379 }
380 if (action == CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED) {
381 HandlePkgAddRemove(want, payload);
382 ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL,
383 ResType::AppInstallStatus::BUNDLE_REMOVED, payload);
384 return true;
385 }
386 return false;
387 }
388
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)389 void EventController::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
390 int32_t systemAbilityId, const std::string& deviceId)
391 {
392 RESSCHED_LOGW("common event service is removed.");
393 subscriber_ = nullptr;
394 lockScreenSubscriber_ = nullptr;
395 }
396
Stop()397 void EventController::SystemAbilityStatusChangeListener::Stop()
398 {
399 if (subscriber_ == nullptr || lockScreenSubscriber_ == nullptr) {
400 return;
401 }
402 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
403 CommonEventManager::UnSubscribeCommonEvent(lockScreenSubscriber_);
404 RESSCHED_LOGI("unsubscribe all common event.");
405 subscriber_ = nullptr;
406 lockScreenSubscriber_ = nullptr;
407 }
408
EventControllerInit()409 extern "C" void EventControllerInit()
410 {
411 EventController::GetInstance().Init();
412 }
413
EventControllerStop()414 extern "C" void EventControllerStop()
415 {
416 EventController::GetInstance().Stop();
417 }
418 }
419 }