• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "net_supplier_info.h"
27 #include "system_ability_definition.h"
28 
29 #include "ressched_utils.h"
30 #include "res_sched_log.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         return;
47     }
48     sptr<ISystemAbilityManager> systemAbilityManager
49         = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50     if (systemAbilityManager == nullptr) {
51         RESSCHED_LOGE("systemAbilityManager is null");
52         sysAbilityListener_ = nullptr;
53         return;
54     }
55     int32_t ret = systemAbilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, sysAbilityListener_);
56     if (ret != ERR_OK) {
57         RESSCHED_LOGE("subscribe system ability id: %{public}d failed", COMMON_EVENT_SERVICE_ID);
58         sysAbilityListener_ = nullptr;
59     }
60 }
61 
HandlePkgAddRemove(const EventFwk::Want & want,nlohmann::json & payload) const62 void EventController::HandlePkgAddRemove(const EventFwk::Want &want, nlohmann::json &payload) const
63 {
64     AppExecFwk::ElementName ele = want.GetElement();
65     std::string bundleName = ele.GetBundleName();
66     int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
67     payload["bundleName"] = bundleName;
68     payload["uid"] = uid;
69 }
70 
GetUid(const int32_t & userId,const std::string & bundleName) const71 int32_t EventController::GetUid(const int32_t &userId, const std::string &bundleName) const
72 {
73     AppExecFwk::ApplicationInfo info;
74     sptr<ISystemAbilityManager> systemAbilityManager
75         = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76     if (systemAbilityManager == nullptr) {
77         RESSCHED_LOGE("Failed to get uid due to get systemAbilityManager is null.");
78         return -1;
79     }
80     sptr<IRemoteObject> remoteObject  = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
81     if (remoteObject == nullptr) {
82         RESSCHED_LOGE("Failed to get uid due to get BMS is null.");
83         return -1;
84     }
85     sptr<AppExecFwk::IBundleMgr> bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
86     bundleMgr->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, info);
87     return static_cast<int32_t>(info.uid);
88 }
89 
HandleConnectivityChange(const EventFwk::Want & want,const int32_t & code,nlohmann::json & payload)90 void EventController::HandleConnectivityChange(
91     const EventFwk::Want &want, const int32_t &code, nlohmann::json &payload)
92 {
93     int32_t netType = want.GetIntParam("NetType", -1);
94     if (netType != 1) {
95         return;
96     }
97     ReportDataInProcess(ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE, code, payload);
98 }
99 
ReportDataInProcess(const uint32_t & resType,const int64_t & value,const nlohmann::json & payload)100 void EventController::ReportDataInProcess(const uint32_t &resType, const int64_t &value, const nlohmann::json& payload)
101 {
102     ResSchedUtils::GetInstance().ReportDataInProcess(resType, value, payload);
103     resType_ = resType;
104     value_ = value;
105     payload_ = payload;
106 }
107 
Stop()108 void EventController::Stop()
109 {
110     if (sysAbilityListener_ == nullptr) {
111         return;
112     }
113     sysAbilityListener_->Stop();
114 }
115 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)116 void EventController::SystemAbilityStatusChangeListener::OnAddSystemAbility(
117     int32_t systemAbilityId, const std::string& deviceId)
118 {
119     MatchingSkills matchingSkills;
120     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
121     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
122     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
123     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
124     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
125     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
126     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
127     matchingSkills.AddEvent("common.event.UNLOCK_SCREEN");
128     matchingSkills.AddEvent("common.event.LOCK_SCREEN");
129     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
130     subscriber_ = std::make_shared<EventController>(subscriberInfo);
131     if (CommonEventManager::SubscribeCommonEvent(subscriber_)) {
132         RESSCHED_LOGD("SubscribeCommonEvent ok");
133     } else {
134         RESSCHED_LOGW("SubscribeCommonEvent fail");
135     }
136 }
137 
OnReceiveEvent(const EventFwk::CommonEventData & data)138 void EventController::OnReceiveEvent(const EventFwk::CommonEventData &data)
139 {
140     Want want = data.GetWant();
141     std::string action = want.GetAction();
142     RESSCHED_LOGD("Recieved common event:%{public}s", action.c_str());
143 
144     nlohmann::json payload = nlohmann::json::object();
145     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
146         HandlePkgAddRemove(want, payload);
147         ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_UNINSTALL, payload);
148         return;
149     }
150     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
151         HandlePkgAddRemove(want, payload);
152         ReportDataInProcess(ResType::RES_TYPE_APP_INSTALL_UNINSTALL, ResType::AppInstallStatus::APP_INSTALL, payload);
153         return;
154     }
155     if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
156         ReportDataInProcess(ResType::RES_TYPE_SCREEN_STATUS, ResType::ScreenStatus::SCREEN_ON, payload);
157         return;
158     }
159     if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
160         ReportDataInProcess(ResType::RES_TYPE_SCREEN_STATUS, ResType::ScreenStatus::SCREEN_OFF, payload);
161         return;
162     }
163     if (action == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
164         int32_t code = data.GetCode();
165         HandleConnectivityChange(want, code, payload);
166         return;
167     }
168     if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
169         int32_t userId = data.GetCode();
170         ReportDataInProcess(ResType::RES_TYPE_USER_SWITCH, static_cast<int64_t>(userId), payload);
171         return;
172     }
173     if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
174         int32_t userId = data.GetCode();
175         ReportDataInProcess(ResType::RES_TYPE_USER_REMOVE, static_cast<int64_t>(userId), payload);
176         return;
177     }
178     if (action == "common.event.UNLOCK_SCREEN") {
179         ReportDataInProcess(ResType::RES_TYPE_SCREEN_LOCK, ResType::ScreenLockStatus::SCREEN_UNLOCK, payload);
180         return;
181     }
182     if (action == "common.event.LOCK_SCREEN") {
183         ReportDataInProcess(ResType::RES_TYPE_SCREEN_LOCK, ResType::ScreenLockStatus::SCREEN_LOCK, payload);
184         return;
185     }
186 }
187 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)188 void EventController::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
189     int32_t systemAbilityId, const std::string& deviceId)
190 {
191     RESSCHED_LOGW("common event service is removed.");
192     subscriber_ = nullptr;
193 }
194 
Stop()195 void EventController::SystemAbilityStatusChangeListener::Stop()
196 {
197     if (subscriber_ == nullptr) {
198         return;
199     }
200     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
201     subscriber_ = nullptr;
202 }
203 
EventControllerInit()204 extern "C" void EventControllerInit()
205 {
206     EventController::GetInstance().Init();
207 }
208 
EventControllerStop()209 extern "C" void EventControllerStop()
210 {
211     EventController::GetInstance().Stop();
212 }
213 }
214 }