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