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 "device_state_observer.h"
17
18 #include "battery_srv_client.h"
19 #include "iservice_registry.h"
20 #include "networkshare_client.h"
21 #include "networkshare_constants.h"
22 #include "power_mgr_client.h"
23 #include "power_mode_info.h"
24 #include "system_ability_definition.h"
25 #include "telephony_log_wrapper.h"
26
27 namespace OHOS {
28 namespace Telephony {
29 using PowerMode = OHOS::PowerMgr::PowerMode;
30 namespace {
31 const std::string NET_TYPE = "NetType";
32 }
33
StartEventSubscriber(const std::shared_ptr<DeviceStateHandler> & deviceStateHandler)34 void DeviceStateObserver::StartEventSubscriber(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler)
35 {
36 MatchingSkills matchingSkills;
37 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
38 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
39 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
40 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
41 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
42 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISCHARGING);
43 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
44 subscriber_ = std::make_shared<DeviceStateEventSubscriber>(subscriberInfo);
45 subscriber_->SetEventHandler(deviceStateHandler);
46 subscriber_->InitEventMap();
47 sharingEventCallback_ = new (std::nothrow) SharingEventCallback(deviceStateHandler);
48
49 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_, sharingEventCallback_);
51 if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
52 TELEPHONY_LOGE("StartEventSubscriber samgrProxy or statusChangeListener_ is nullptr");
53 return;
54 }
55 int32_t commonEventResult = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
56 int32_t powerManagerResult = samgrProxy->SubscribeSystemAbility(POWER_MANAGER_SERVICE_ID, statusChangeListener_);
57 int32_t powerManagerBattResult =
58 samgrProxy->SubscribeSystemAbility(POWER_MANAGER_BATT_SERVICE_ID, statusChangeListener_);
59 int32_t netManagerResult =
60 samgrProxy->SubscribeSystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, statusChangeListener_);
61 TELEPHONY_LOGI(
62 "SubscribeSystemAbility COMMON_EVENT_SERVICE_ID(result:%{public}d) POWER_MANAGER_SERVICE_ID(result:%{public}d) "
63 "POWER_MANAGER_BATT_SERVICE_ID(result:%{public}d) COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID(result:%{public}d)",
64 commonEventResult, powerManagerResult, powerManagerBattResult, netManagerResult);
65 }
66
StopEventSubscriber()67 void DeviceStateObserver::StopEventSubscriber()
68 {
69 if (subscriber_ != nullptr) {
70 bool subscribeResult = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
71 subscriber_ = nullptr;
72 TELEPHONY_LOGI("DeviceStateObserver::StopEventSubscriber subscribeResult = %{public}d", subscribeResult);
73 }
74
75 if (sharingEventCallback_ == nullptr) {
76 TELEPHONY_LOGE("DeviceStateObserver::StopEventSubscriber sharingEventCallback_ is nullptr");
77 return;
78 }
79 auto networkShareClient = DelayedSingleton<NetManagerStandard::NetworkShareClient>::GetInstance();
80 if (networkShareClient == nullptr) {
81 TELEPHONY_LOGE("DeviceStateObserver::StopEventSubscriber networkShareClient is nullptr");
82 return;
83 }
84 networkShareClient->UnregisterSharingEvent(sharingEventCallback_);
85 sharingEventCallback_ = nullptr;
86 }
87
OnReceiveEvent(const CommonEventData & data)88 void DeviceStateEventSubscriber::OnReceiveEvent(const CommonEventData &data)
89 {
90 if (deviceStateHandler_ == nullptr) {
91 TELEPHONY_LOGE("DeviceStateEventSubscriber::OnReceiveEvent: networkSearchHandler_ is nullptr");
92 return;
93 }
94 std::string action = data.GetWant().GetAction();
95 TELEPHONY_LOGI("DeviceStateEventSubscriber::OnReceiveEvent: action = %{public}s", action.c_str());
96 switch (GetDeviceStateEventIntValue(action)) {
97 case COMMON_EVENT_CONNECTIVITY_CHANGE:
98 ProcessWifiState(data);
99 break;
100 case COMMON_EVENT_SCREEN_ON:
101 deviceStateHandler_->ProcessScreenDisplay(true);
102 break;
103 case COMMON_EVENT_SCREEN_OFF:
104 deviceStateHandler_->ProcessScreenDisplay(false);
105 break;
106 case COMMON_EVENT_POWER_SAVE_MODE_CHANGED:
107 ProcessPowerSaveMode(data);
108 break;
109 case COMMON_EVENT_CHARGING:
110 deviceStateHandler_->ProcessChargingState(true);
111 break;
112 case COMMON_EVENT_DISCHARGING:
113 deviceStateHandler_->ProcessChargingState(false);
114 break;
115 default:
116 TELEPHONY_LOGE("DeviceStateEventSubscriber::OnReceiveEvent: invalid event");
117 break;
118 }
119 }
120
ProcessWifiState(const CommonEventData & data)121 void DeviceStateEventSubscriber::ProcessWifiState(const CommonEventData &data)
122 {
123 if (deviceStateHandler_ == nullptr) {
124 TELEPHONY_LOGE("DeviceStateEventSubscriber::ProcessWifiState networkSearchHandler_ is nullptr");
125 return;
126 }
127 if (data.GetWant().GetIntParam(NET_TYPE, NetBearType::BEARER_DEFAULT) == NetBearType::BEARER_WIFI) {
128 bool isWifiConnected = data.GetCode() == NetConnState::NET_CONN_STATE_CONNECTED;
129 deviceStateHandler_->ProcessWifiState(isWifiConnected);
130 TELEPHONY_LOGI("DeviceStateEventSubscriber wifi %{public}s", isWifiConnected ? "connected" : "no connected");
131 }
132 }
133
ProcessPowerSaveMode(const CommonEventData & data)134 void DeviceStateEventSubscriber::ProcessPowerSaveMode(const CommonEventData &data)
135 {
136 if (deviceStateHandler_ == nullptr) {
137 TELEPHONY_LOGE("DeviceStateEventSubscriber::ProcessPowerSaveMode networkSearchHandler_ is nullptr");
138 return;
139 }
140 PowerMode powerModeCode = static_cast<PowerMode>(data.GetCode());
141 switch (powerModeCode) {
142 case PowerMode::POWER_SAVE_MODE:
143 case PowerMode::EXTREME_POWER_SAVE_MODE:
144 deviceStateHandler_->ProcessPowerSaveMode(true);
145 break;
146 case PowerMode::PERFORMANCE_MODE:
147 case PowerMode::NORMAL_MODE:
148 deviceStateHandler_->ProcessPowerSaveMode(false);
149 break;
150 default:
151 TELEPHONY_LOGE("DeviceStateEventSubscriber::ProcessPowerSaveMode invalid event");
152 break;
153 }
154 TELEPHONY_LOGI("ProcessPowerSaveMode powerModeCode %{public}d", static_cast<int32_t>(powerModeCode));
155 }
156
SetEventHandler(const std::shared_ptr<DeviceStateHandler> & deviceStateHandler)157 void DeviceStateEventSubscriber::SetEventHandler(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler)
158 {
159 deviceStateHandler_ = deviceStateHandler;
160 }
161
GetEventHandler()162 std::shared_ptr<DeviceStateHandler> DeviceStateEventSubscriber::GetEventHandler()
163 {
164 return deviceStateHandler_;
165 }
166
GetDeviceStateEventIntValue(std::string & event) const167 DeviceStateEventIntValue DeviceStateEventSubscriber::GetDeviceStateEventIntValue(std::string &event) const
168 {
169 auto iter = deviceStateEventMapIntValues_.find(event);
170 if (iter == deviceStateEventMapIntValues_.end()) {
171 return COMMON_EVENT_UNKNOWN;
172 }
173 return iter->second;
174 }
175
InitEventMap()176 void DeviceStateEventSubscriber::InitEventMap()
177 {
178 deviceStateEventMapIntValues_ = {
179 {CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE, COMMON_EVENT_CONNECTIVITY_CHANGE},
180 {CommonEventSupport::COMMON_EVENT_SCREEN_ON, COMMON_EVENT_SCREEN_ON},
181 {CommonEventSupport::COMMON_EVENT_SCREEN_OFF, COMMON_EVENT_SCREEN_OFF},
182 {CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED, COMMON_EVENT_POWER_SAVE_MODE_CHANGED},
183 {CommonEventSupport::COMMON_EVENT_CHARGING, COMMON_EVENT_CHARGING},
184 {CommonEventSupport::COMMON_EVENT_DISCHARGING, COMMON_EVENT_DISCHARGING},
185 };
186 }
187
SystemAbilityStatusChangeListener(std::shared_ptr<DeviceStateEventSubscriber> & sub,sptr<NetManagerStandard::ISharingEventCallback> & callback)188 DeviceStateObserver::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
189 std::shared_ptr<DeviceStateEventSubscriber> &sub, sptr<NetManagerStandard::ISharingEventCallback> &callback)
190 : sub_(sub), callback_(callback)
191 {}
192
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)193 void DeviceStateObserver::SystemAbilityStatusChangeListener::OnAddSystemAbility(
194 int32_t systemAbilityId, const std::string& deviceId)
195 {
196 if (sub_ == nullptr) {
197 TELEPHONY_LOGE("OnAddSystemAbility sub_ is nullptr");
198 return;
199 }
200 switch (systemAbilityId) {
201 case POWER_MANAGER_SERVICE_ID: {
202 TELEPHONY_LOGI("DeviceStateObserver systemAbilityId is POWER_MANAGER_SERVICE_ID");
203 if (sub_->GetEventHandler() == nullptr) {
204 TELEPHONY_LOGE("DeviceStateObserver OnAddSystemAbility eventHandler is nullptr");
205 return;
206 }
207 auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
208 sub_->GetEventHandler()->ProcessScreenDisplay(powerMgrClient.IsScreenOn());
209 auto powerSaveMode = powerMgrClient.GetDeviceMode();
210 sub_->GetEventHandler()->ProcessPowerSaveMode(
211 powerSaveMode == PowerMgr::PowerMode::POWER_SAVE_MODE ||
212 powerSaveMode == PowerMgr::PowerMode::EXTREME_POWER_SAVE_MODE);
213 break;
214 }
215 case POWER_MANAGER_BATT_SERVICE_ID: {
216 TELEPHONY_LOGI("DeviceStateObserver systemAbilityId is POWER_MANAGER_BATT_SERVICE_ID");
217 if (sub_->GetEventHandler() == nullptr) {
218 TELEPHONY_LOGE("DeviceStateObserver OnAddSystemAbility eventHandler is nullptr");
219 return;
220 }
221 auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance();
222 auto chargingStatus = batterySrvClient.GetChargingStatus();
223 sub_->GetEventHandler()->ProcessChargingState(
224 chargingStatus == PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE);
225 break;
226 }
227 case COMMON_EVENT_SERVICE_ID: {
228 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(sub_);
229 TELEPHONY_LOGI("DeviceStateObserver::OnAddSystemAbility subscribeResult = %{public}d", subscribeResult);
230 break;
231 }
232 case COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID: {
233 TELEPHONY_LOGI("DeviceStateObserver systemAbilityId is COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID");
234 auto networkShareClient = DelayedSingleton<NetManagerStandard::NetworkShareClient>::GetInstance();
235 if (networkShareClient == nullptr) {
236 TELEPHONY_LOGE("DeviceStateObserver OnAddSystemAbility networkShareClient is nullptr");
237 return;
238 }
239 int32_t isSharing = 0;
240 networkShareClient->IsSharing(isSharing);
241 sub_->GetEventHandler()->ProcessNetSharingState(isSharing == NetManagerStandard::NETWORKSHARE_IS_SHARING);
242 networkShareClient->RegisterSharingEvent(callback_);
243 break;
244 }
245 default:
246 TELEPHONY_LOGE("systemAbilityId is invalid");
247 break;
248 }
249 }
250
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)251 void DeviceStateObserver::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
252 int32_t systemAbilityId, const std::string& deviceId)
253 {
254 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
255 TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
256 return;
257 }
258 if (sub_ == nullptr) {
259 TELEPHONY_LOGE("DeviceStateObserver::OnRemoveSystemAbility sub_ is nullptr");
260 return;
261 }
262 bool subscribeResult = CommonEventManager::UnSubscribeCommonEvent(sub_);
263 TELEPHONY_LOGI("DeviceStateObserver::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult);
264 }
265
SharingEventCallback(const std::shared_ptr<DeviceStateHandler> & deviceStateHandler)266 SharingEventCallback::SharingEventCallback(
267 const std::shared_ptr<DeviceStateHandler> &deviceStateHandler) : handler_(deviceStateHandler)
268 {}
269
OnSharingStateChanged(const bool & isRunning)270 void SharingEventCallback::OnSharingStateChanged(const bool &isRunning)
271 {
272 if (handler_ == nullptr) {
273 TELEPHONY_LOGE("OnSharingStateChanged handler_ is nullptr");
274 return;
275 }
276 TELEPHONY_LOGI("DeviceStateObserver::OnSharingStateChanged: isSharing = %{public}d", isRunning);
277 handler_->ProcessNetSharingState(isRunning);
278 }
279 } // namespace Telephony
280 } // namespace OHOS
281