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