• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "power_mode_module.h"
17 
18 #ifdef HAS_DISPLAY_MANAGER
19 #include "display_power_mgr_client.h"
20 #endif
21 #include "power_log.h"
22 #include "power_mode_policy.h"
23 #include "power_mgr_service.h"
24 #include "setting_helper.h"
25 
26 #include "singleton.h"
27 
28 using namespace std;
29 using namespace OHOS;
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::EventFwk;
32 
33 namespace OHOS {
34 namespace PowerMgr {
PowerModeModule()35 PowerModeModule::PowerModeModule()
36     : mode_(PowerMode::NORMAL_MODE), lastMode_(LAST_MODE_FLAG), started_(false)
37 {
38     POWER_HILOGI(FEATURE_POWER_MODE, "Instance create");
39     callbackMgr_ = new CallbackManager();
40     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
41     PowerModePolicy::ModeAction displayOffTimeAction = [&](bool isInit) { SetDisplayOffTime(isInit); };
42     policy->AddAction(PowerModePolicy::ServiceType::DISPLAY_OFFTIME, displayOffTimeAction);
43     PowerModePolicy::ModeAction sleepTimeAction = [&](bool isInit) { SetSleepTime(isInit); };
44     policy->AddAction(PowerModePolicy::ServiceType::SLEEPTIME, sleepTimeAction);
45     PowerModePolicy::ModeAction autoAdjustBrightnessAction = [&](bool isInit) { SetAutoAdjustBrightness(isInit); };
46     policy->AddAction(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS, autoAdjustBrightnessAction);
47     PowerModePolicy::ModeAction lcdBrightnessAction = [&](bool isInit) { SetLcdBrightness(isInit); };
48     policy->AddAction(PowerModePolicy::ServiceType::SMART_BACKLIGHT, lcdBrightnessAction);
49     PowerModePolicy::ModeAction vibrationAction = [&](bool isInit) { SetVibration(isInit); };
50     policy->AddAction(PowerModePolicy::ServiceType::VIBRATORS_STATE, vibrationAction);
51     PowerModePolicy::ModeAction onOffRotationAction = [&](bool isInit) { SetWindowRotation(isInit); };
52     policy->AddAction(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION, onOffRotationAction);
53 }
54 
SetModeItem(PowerMode mode)55 void PowerModeModule::SetModeItem(PowerMode mode)
56 {
57     POWER_HILOGI(FEATURE_POWER_MODE, "mode_: %{public}u, mode: %{public}u", mode_, mode);
58 
59     /* Same as the previous mode */
60     if (this->mode_ == mode) {
61         return;
62     }
63 
64     /* If it's a valid mode */
65     if (mode < PowerMode::POWER_MODE_MIN || mode > PowerMode::POWER_MODE_MAX) {
66         POWER_HILOGW(FEATURE_POWER_MODE, "Invalid mode %{public}d", mode);
67         return;
68     }
69 
70     /* start set mode thread */
71     EnableMode(mode);
72 }
73 
GetModeItem()74 PowerMode PowerModeModule::GetModeItem()
75 {
76     POWER_HILOGD(FEATURE_POWER_MODE, "mode_: %{public}u", mode_);
77     /* get power mode */
78     return mode_;
79 }
80 
EnableMode(PowerMode mode,bool isBoot)81 void PowerModeModule::EnableMode(PowerMode mode, bool isBoot)
82 {
83     if (started_) {
84         POWER_HILOGW(FEATURE_POWER_MODE, "Power Mode is already running");
85         return;
86     }
87 
88     started_ = true;
89     mode_ = mode;
90 
91     /* Update power mode policy */
92     UpdateModepolicy();
93 
94     /* Send state change */
95     Prepare();
96 
97     /* Set action */
98     RunAction(isBoot);
99 
100     this->lastMode_ = static_cast<uint32_t>(mode);
101     started_ = false;
102 }
103 
UpdateModepolicy()104 void PowerModeModule::UpdateModepolicy()
105 {
106     /* update policy */
107     DelayedSingleton<PowerModePolicy>::GetInstance()->SetPowerModePolicy(static_cast<uint32_t>(this->mode_),
108         this->lastMode_);
109 }
110 
AddPowerModeCallback(const sptr<IPowerModeCallback> & callback)111 void PowerModeModule::AddPowerModeCallback(const sptr<IPowerModeCallback>& callback)
112 {
113     if (callbackMgr_) {
114         callbackMgr_->AddCallback(callback);
115     }
116 }
117 
DelPowerModeCallback(const sptr<IPowerModeCallback> & callback)118 void PowerModeModule::DelPowerModeCallback(const sptr<IPowerModeCallback>& callback)
119 {
120     if (callbackMgr_) {
121         callbackMgr_->RemoveCallback(callback);
122     }
123 }
124 
Prepare()125 void PowerModeModule::Prepare()
126 {
127     PublishPowerModeEvent();
128     if (callbackMgr_) {
129         callbackMgr_->WaitingCallback();
130     }
131 }
132 
AddCallback(const sptr<IPowerModeCallback> & callback)133 void PowerModeModule::CallbackManager::AddCallback(const sptr<IPowerModeCallback>& callback)
134 {
135     unique_lock<mutex> lock(mutex_);
136     RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr));
137     auto object = callback->AsObject();
138     auto retIt = callbacks_.insert(object);
139     if (retIt.second) {
140         object->AddDeathRecipient(this);
141     }
142     POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu, insertOk = %{public}d",
143         callbacks_.size(), retIt.second);
144 }
145 
RemoveCallback(const sptr<IPowerModeCallback> & callback)146 void PowerModeModule::CallbackManager::RemoveCallback(const sptr<IPowerModeCallback>& callback)
147 {
148     unique_lock<mutex> lock(mutex_);
149     RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr));
150     auto object = callback->AsObject();
151     auto it = find(callbacks_.begin(), callbacks_.end(), object);
152     if (it != callbacks_.end()) {
153         callbacks_.erase(it);
154         object->RemoveDeathRecipient(this);
155     }
156     POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu", callbacks_.size());
157 }
158 
OnRemoteDied(const wptr<IRemoteObject> & remote)159 void PowerModeModule::CallbackManager::OnRemoteDied(const wptr<IRemoteObject>& remote)
160 {
161     POWER_HILOGW(FEATURE_POWER_MODE, "On remote died");
162     RETURN_IF(remote.promote() == nullptr);
163     RemoveCallback(iface_cast<IPowerModeCallback>(remote.promote()));
164 }
165 
WaitingCallback()166 void PowerModeModule::CallbackManager::WaitingCallback()
167 {
168     POWER_HILOGD(FEATURE_POWER_MODE, "Mode callback started");
169     unique_lock<mutex> lock(mutex_);
170     for (auto& obj: callbacks_) {
171         sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
172         if (callback != nullptr) {
173             POWER_HILOGD(FEATURE_POWER_MODE, "Call IPowerModeCallback");
174             PowerMode mode = PowerMode::NORMAL_MODE;
175             callback->OnPowerModeChanged(mode);
176         }
177     }
178 }
179 
PublishPowerModeEvent()180 void PowerModeModule::PublishPowerModeEvent()
181 {
182     POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event");
183     /* send event */
184     CommonEventPublishInfo publishInfo;
185     publishInfo.SetOrdered(false);
186     std::string action;
187     uint32_t code;
188     std::string data;
189     switch (mode_) {
190         case PowerMode::PERFORMANCE_MODE:
191             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
192             code = static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE);
193             data = ToString(static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE));
194             break;
195         case PowerMode::NORMAL_MODE:
196             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
197             code = static_cast<uint32_t>(PowerMode::NORMAL_MODE);
198             data = ToString(static_cast<uint32_t>(PowerMode::NORMAL_MODE));
199             break;
200         case PowerMode::POWER_SAVE_MODE:
201             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
202             code = static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE);
203             data = ToString(static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE));
204             break;
205         case PowerMode::EXTREME_POWER_SAVE_MODE:
206             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
207             code = static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE);
208             data = ToString(static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE));
209             break;
210         default:
211             POWER_HILOGW(FEATURE_POWER_MODE, "Unknown mode");
212             return;
213     }
214     IntentWant setModeWant;
215     setModeWant.SetAction(action);
216     CommonEventData event(setModeWant);
217     event.SetCode(code);
218     event.SetData(data);
219     if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
220         POWER_HILOGE(FEATURE_POWER_MODE, "Failed to publish the mode event");
221         return;
222     }
223     POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event end");
224 }
225 
RunAction(bool isBoot)226 void PowerModeModule::RunAction(bool isBoot)
227 {
228     POWER_HILOGD(FEATURE_POWER_MODE, "Run action");
229     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
230     policy->TriggerAllActions(isBoot);
231 }
232 
SetDisplayOffTime(bool isBoot)233 void PowerModeModule::SetDisplayOffTime(bool isBoot)
234 {
235     if (isBoot && SettingHelper::IsDisplayOffTimeSettingValid()) {
236         return;
237     }
238     int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance()
239         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::DISPLAY_OFFTIME);
240     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
241     POWER_HILOGD(FEATURE_POWER_MODE, "Set display off timeout: %{public}d", time);
242     pms->GetPowerStateMachine()->SetDisplayOffTime(static_cast<int64_t>(time));
243 }
244 
SetSleepTime(bool isBoot)245 void PowerModeModule::SetSleepTime([[maybe_unused]] bool isBoot)
246 {
247     int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance()
248         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::SLEEPTIME);
249     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
250     POWER_HILOGD(FEATURE_POWER_MODE, "Set sleep timeout: %{public}d", time);
251     pms->GetPowerStateMachine()->SetSleepTime(static_cast<int64_t>(time));
252 }
253 
SetAutoAdjustBrightness(bool isBoot)254 void PowerModeModule::SetAutoAdjustBrightness(bool isBoot)
255 {
256     if (isBoot && SettingHelper::IsAutoAdjustBrightnessSettingValid()) {
257         return;
258     }
259     int32_t value = DelayedSingleton<PowerModePolicy>::GetInstance()
260         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS);
261     auto status = static_cast<SettingHelper::SwitchStatus>(value);
262     POWER_HILOGI(FEATURE_POWER_MODE, "status: %{public}d", status);
263     SettingHelper::SetSettingAutoAdjustBrightness(status);
264 }
265 
SetLcdBrightness(bool isBoot)266 void PowerModeModule::SetLcdBrightness(bool isBoot)
267 {
268     if (isBoot && SettingHelper::IsBrightnessSettingValid()) {
269         return;
270     }
271     int32_t lcdBrightness = DelayedSingleton<PowerModePolicy>::GetInstance()
272         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::SMART_BACKLIGHT);
273     POWER_HILOGD(FEATURE_POWER_MODE, "lcdBrightness: %{public}d", lcdBrightness);
274     SettingHelper::SetSettingBrightness(lcdBrightness);
275 #ifdef HAS_DISPLAY_MANAGER
276     OHOS::DisplayPowerMgr::DisplayPowerMgrClient::GetInstance().SetBrightness(lcdBrightness);
277 #endif
278 }
279 
SetVibration(bool isBoot)280 void PowerModeModule::SetVibration(bool isBoot)
281 {
282     if (isBoot && SettingHelper::IsVibrationSettingValid()) {
283         return;
284     }
285     int32_t vibration = DelayedSingleton<PowerModePolicy>::GetInstance()
286         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::VIBRATORS_STATE);
287     POWER_HILOGD(FEATURE_POWER_MODE, "GetPowerModeValuePolicy vibrate=%{public}d", vibration);
288     SettingHelper::SetSettingVibration(static_cast<SettingHelper::SwitchStatus>(vibration));
289 }
290 
SetWindowRotation(bool isBoot)291 void PowerModeModule::SetWindowRotation(bool isBoot)
292 {
293     if (isBoot && SettingHelper::IsWindowRotationSettingValid()) {
294         return;
295     }
296     int32_t rotation = DelayedSingleton<PowerModePolicy>::GetInstance()
297         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION);
298     POWER_HILOGD(FEATURE_POWER_MODE, "GetPowerModeValuePolicy rotation=%{public}d", rotation);
299     SettingHelper::SetSettingWindowRotation(static_cast<SettingHelper::SwitchStatus>(rotation));
300 }
301 } // namespace PowerMgr
302 } // namespace OHOS
303