• 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 "device_state_handler.h"
17 
18 #include "battery_srv_client.h"
19 #include "network_search_manager.h"
20 #include "power_mgr_client.h"
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 namespace {
26 const uint32_t CELL_REQUEST_SHORT_INTERVAL = 2; // This is the minimum interval in seconds for cell requests
27 const uint32_t CELL_REQUEST_LONG_INTERVAL = 10; // This is the maximum interval in seconds for cell requests
28 } // namespace
29 
DeviceStateHandler(const std::weak_ptr<NetworkSearchManager> & networkSearchManager,const std::weak_ptr<ITelRilManager> & telRilManager,int32_t slotId)30 DeviceStateHandler::DeviceStateHandler(
31     const std::weak_ptr<NetworkSearchManager> &networkSearchManager,
32     const std::weak_ptr<ITelRilManager> &telRilManager, int32_t slotId)
33     : networkSearchManager_(networkSearchManager), telRilManager_(telRilManager), slotId_(slotId)
34 {
35     isCharging_ = true;
36     auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
37     isScreenOn_ = true;
38     auto powerSaveMode = powerMgrClient.GetDeviceMode();
39     isPowerSaveModeOn_ = powerSaveMode == PowerMgr::PowerMode::POWER_SAVE_MODE ||
40         powerSaveMode == PowerMgr::PowerMode::EXTREME_POWER_SAVE_MODE;
41     TELEPHONY_LOGI("DeviceStateHandler isCharging_=%{public}d, isScreenOn_=%{public}d, isPowerSaveModeOn_=%{public}d",
42         isCharging_, isScreenOn_, isPowerSaveModeOn_);
43 }
44 
ProcessWifiState(bool isWifiConnected)45 void DeviceStateHandler::ProcessWifiState(bool isWifiConnected)
46 {
47     isWifiConnected_ = isWifiConnected;
48     ProcessDeviceState();
49 }
50 
ProcessScreenDisplay(bool isScreenOn)51 void DeviceStateHandler::ProcessScreenDisplay(bool isScreenOn)
52 {
53     isScreenOn_ = isScreenOn;
54     ProcessDeviceState();
55 }
56 
ProcessPowerSaveMode(bool isPowerSaveModeOn)57 void DeviceStateHandler::ProcessPowerSaveMode(bool isPowerSaveModeOn)
58 {
59     isPowerSaveModeOn_ = isPowerSaveModeOn;
60     SetDeviceState(POWER_SAVE_MODE, isPowerSaveModeOn_);
61     ProcessDeviceState();
62 }
63 
ProcessChargingState(bool isCharging)64 void DeviceStateHandler::ProcessChargingState(bool isCharging)
65 {
66     isCharging_ = isCharging;
67     SetDeviceState(CHARGING_STATE, isCharging_);
68     ProcessDeviceState();
69 }
70 
ProcessNetSharingState(bool isNetSharingOn)71 void DeviceStateHandler::ProcessNetSharingState(bool isNetSharingOn)
72 {
73     isNetSharingOn_ = isNetSharingOn;
74     ProcessDeviceState();
75 }
76 
ProcessRadioState()77 void DeviceStateHandler::ProcessRadioState()
78 {
79     SyncSettings();
80 }
81 
ProcessDeviceState()82 void DeviceStateHandler::ProcessDeviceState()
83 {
84     uint32_t newCellRequestMinInterval = GetCellRequestMinInterval();
85     TELEPHONY_LOGI(
86         "ProcessDeviceState isCharging_=%{public}d, isPowerSaveModeOn_=%{public}d, isNetSharingOn_=%{public}d, "
87         "isScreenOn_=%{public}d, isWifiConnected_=%{public}d, newCellRequestMinInterval=%{public}d",
88         isCharging_, isPowerSaveModeOn_, isNetSharingOn_, isScreenOn_, isWifiConnected_, newCellRequestMinInterval);
89     if (cellRequestMinInterval_ != newCellRequestMinInterval) {
90         cellRequestMinInterval_ = newCellRequestMinInterval;
91         SetCellRequestMinInterval(cellRequestMinInterval_);
92     }
93 
94     if (isLowData_ != IsLowPowerConsumption()) {
95         isLowData_ = !isLowData_;
96         SetDeviceState(LOW_DATA_STATE, isLowData_);
97     }
98 
99     int32_t newFilter = NOTIFICATION_FILTER_NONE;
100     if (IsSignalStrengthNotificationExpected()) {
101         newFilter |= NOTIFICATION_FILTER_SIGNAL_STRENGTH;
102     }
103 
104     if (!IsLowPowerConsumption()) {
105         newFilter |= NOTIFICATION_FILTER_NETWORK_STATE;
106         newFilter |= NOTIFICATION_FILTER_DATA_CALL;
107         newFilter |= NOTIFICATION_FILTER_LINK_CAPACITY;
108         newFilter |= NOTIFICATION_FILTER_PHYSICAL_CHANNEL_CONFIG;
109     }
110 
111     SetNotificationFilter(newFilter, false);
112 }
113 
IsSignalStrengthNotificationExpected() const114 bool DeviceStateHandler::IsSignalStrengthNotificationExpected() const
115 {
116     return isCharging_ || isScreenOn_;
117 }
118 
IsLowPowerConsumption() const119 bool DeviceStateHandler::IsLowPowerConsumption() const
120 {
121     return !isCharging_ && !isScreenOn_ && !isNetSharingOn_;
122 }
123 
GetCellRequestMinInterval() const124 uint32_t DeviceStateHandler::GetCellRequestMinInterval() const
125 {
126     if (isScreenOn_ && (!isWifiConnected_ || isCharging_)) {
127         return CELL_REQUEST_SHORT_INTERVAL;
128     } else {
129         return CELL_REQUEST_LONG_INTERVAL;
130     }
131 }
132 
SetCellRequestMinInterval(uint32_t minInterval) const133 void DeviceStateHandler::SetCellRequestMinInterval(uint32_t minInterval) const
134 {
135     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
136     if (nsm == nullptr) {
137         TELEPHONY_LOGE("DeviceStateHandler::SetCellRequestMinInterval nsm is null");
138         return;
139     }
140     auto inner = nsm->FindManagerInner(slotId_);
141     if (inner == nullptr) {
142         TELEPHONY_LOGE("DeviceStateHandler::SetCellRequestMinInterval inner is null");
143         return;
144     }
145     if (inner->networkSearchHandler_ != nullptr) {
146         TELEPHONY_LOGD("DeviceStateHandler::SetCellRequestMinInterval %{public}d", minInterval);
147         inner->networkSearchHandler_->SetCellRequestMinInterval(minInterval);
148     }
149 }
150 
SetNotificationFilter(int32_t newFilter,bool force)151 void DeviceStateHandler::SetNotificationFilter(int32_t newFilter, bool force)
152 {
153     if (!force && newFilter == notificationFilter_) {
154         TELEPHONY_LOGE("DeviceStateHandler::SetNotificationFilter is not necessary");
155         return;
156     }
157     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_NOTIFICATION_FILTER);
158     if (event == nullptr) {
159         TELEPHONY_LOGE("DeviceStateHandler::SetNotificationFilter event is null");
160         return;
161     }
162     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
163     if (telRilManager != nullptr) {
164         TELEPHONY_LOGI("DeviceStateHandler::SetNotificationFilter old filter:%{public}d, new filter:%{public}d,"
165             " slotId_:%{public}d", notificationFilter_, newFilter, slotId_);
166         telRilManager->SetNotificationFilter(slotId_, newFilter, event);
167         notificationFilter_ = newFilter;
168     }
169 }
170 
SetDeviceState(int32_t deviceStateType,bool deviceStateOn)171 void DeviceStateHandler::SetDeviceState(int32_t deviceStateType, bool deviceStateOn)
172 {
173     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_DEVICE_STATE);
174     if (event == nullptr) {
175         TELEPHONY_LOGE("DeviceStateHandler::SetDeviceState event is null");
176         return;
177     }
178     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
179     if (telRilManager != nullptr) {
180         TELEPHONY_LOGD("DeviceStateHandler::SetDeviceState type:%{public}d state:%{public}d, slotId_:%{public}d",
181             deviceStateType, deviceStateOn, slotId_);
182         telRilManager->SetDeviceState(slotId_, deviceStateType, deviceStateOn, event);
183     }
184 }
185 
SyncSettings()186 void DeviceStateHandler::SyncSettings()
187 {
188     TELEPHONY_LOGI("DeviceStateHandler::SyncSettings isCharging_=%{public}d, isLowData_=%{public}d,"
189         " isPowerSaveModeOn_=%{public}d, notificationFilter_=%{public}d",
190         isCharging_, isLowData_, isPowerSaveModeOn_, notificationFilter_);
191     SetDeviceState(CHARGING_STATE, isCharging_);
192     SetDeviceState(LOW_DATA_STATE, isLowData_);
193     SetDeviceState(POWER_SAVE_MODE, isPowerSaveModeOn_);
194     SetNotificationFilter(notificationFilter_, true);
195 }
196 } // namespace Telephony
197 } // namespace OHOS
198