1 /*
2  * Copyright (c) 2021-2024 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 {
35 namespace {
36 sptr<SettingObserver> g_autoAdjustBrightnessObserver;
37 sptr<SettingObserver> g_autoWindowRotationObserver;
38 sptr<SettingObserver> g_vibratorsStateObserver;
39 sptr<SettingObserver> g_intellVoiceObserver;
40 }
41 
PowerModeModule()42 PowerModeModule::PowerModeModule()
43     : mode_(PowerMode::NORMAL_MODE), lastMode_(LAST_MODE_FLAG), started_(false)
44 {
45     POWER_HILOGI(FEATURE_POWER_MODE, "Instance create");
46     callbackMgr_ = new CallbackManager();
47     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
48     PowerModePolicy::ModeAction displayOffTimeAction = [&](bool isInit) { SetDisplayOffTime(isInit); };
49     policy->AddAction(PowerModePolicy::ServiceType::DISPLAY_OFFTIME, displayOffTimeAction);
50     PowerModePolicy::ModeAction sleepTimeAction = [&](bool isInit) { SetSleepTime(isInit); };
51     policy->AddAction(PowerModePolicy::ServiceType::SLEEPTIME, sleepTimeAction);
52     PowerModePolicy::ModeAction autoAdjustBrightnessAction = [&](bool isInit) { SetAutoAdjustBrightness(isInit); };
53     policy->AddAction(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS, autoAdjustBrightnessAction);
54     PowerModePolicy::ModeAction lcdBrightnessAction = [&](bool isInit) { SetLcdBrightness(isInit); };
55     policy->AddAction(PowerModePolicy::ServiceType::LCD_BRIGHTNESS, lcdBrightnessAction);
56     PowerModePolicy::ModeAction vibrationAction = [&](bool isInit) { SetVibration(isInit); };
57     policy->AddAction(PowerModePolicy::ServiceType::VIBRATORS_STATE, vibrationAction);
58     PowerModePolicy::ModeAction onOffRotationAction = [&](bool isInit) { SetWindowRotation(isInit); };
59     policy->AddAction(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION, onOffRotationAction);
60     PowerModePolicy::ModeAction intellVoiceAction = [&](bool isInit) { SetIntellVoiceState(isInit); };
61     policy->AddAction(PowerModePolicy::ServiceType::INTELL_VOICE, intellVoiceAction);
62 }
63 
InitPowerMode()64 void PowerModeModule::InitPowerMode()
65 {
66     POWER_HILOGI(FEATURE_POWER_MODE, "Start to init power mode.");
67     int32_t saveMode = SettingHelper::ReadCurrentMode(static_cast<int32_t>(this->mode_));
68     this->mode_ = static_cast<PowerMode>(saveMode);
69     Prepare();
70     if (!DelayedSingleton<PowerModePolicy>::GetInstance()->InitRecoverMap()) {
71         UpdateModepolicy();
72         RunAction(false);
73     }
74 }
75 
SetModeItem(PowerMode mode)76 void PowerModeModule::SetModeItem(PowerMode mode)
77 {
78     POWER_HILOGI(FEATURE_POWER_MODE, "SetModeItem mode_: %{public}u, mode: %{public}u", mode_, mode);
79 
80     /* Same as the previous mode */
81     if (this->mode_ == mode && this->mode_ != PowerMode::PERFORMANCE_MODE) {
82         return;
83     }
84 
85     /* If it's a valid mode */
86     if (mode < PowerMode::POWER_MODE_MIN || mode > PowerMode::POWER_MODE_MAX) {
87         POWER_HILOGW(FEATURE_POWER_MODE, "Invalid mode %{public}d", mode);
88         return;
89     }
90 
91     /* unregister setting observer for current mode */
92     UnregisterSaveModeObserver();
93 
94     /* start set mode thread */
95     EnableMode(mode);
96 
97     /* register setting observer for save mode */
98     RegisterSaveModeObserver();
99 }
100 
GetModeItem()101 PowerMode PowerModeModule::GetModeItem()
102 {
103     POWER_HILOGD(FEATURE_POWER_MODE, "GetModeItem mode_: %{public}u", mode_);
104     /* get power mode */
105     return mode_;
106 }
107 
UnregisterSaveModeObserver()108 void PowerModeModule::UnregisterSaveModeObserver()
109 {
110     if (!this->observerRegisted_) {
111         POWER_HILOGD(FEATURE_POWER_MODE, "current power mode is normal mode");
112         return;
113     }
114 
115     POWER_HILOGI(FEATURE_POWER_MODE, "unregister setting observer for save mode");
116     SettingHelper::UnregisterSettingObserver(g_autoAdjustBrightnessObserver);
117     SettingHelper::UnregisterSettingObserver(g_autoWindowRotationObserver);
118     SettingHelper::UnregisterSettingObserver(g_vibratorsStateObserver);
119     SettingHelper::UnregisterSettingObserver(g_intellVoiceObserver);
120     g_autoAdjustBrightnessObserver = nullptr;
121     g_autoWindowRotationObserver = nullptr;
122     g_vibratorsStateObserver = nullptr;
123     g_intellVoiceObserver = nullptr;
124     observerRegisted_ = false;
125 }
126 
RegisterSaveModeObserver()127 void PowerModeModule::RegisterSaveModeObserver()
128 {
129     if (this->mode_ == PowerMode::POWER_SAVE_MODE || this->mode_ == PowerMode::EXTREME_POWER_SAVE_MODE) {
130         POWER_HILOGD(FEATURE_POWER_MODE, "register setting observer in save mode");
131         RegisterAutoAdjustBrightnessObserver();
132         RegisterAutoWindowRotationObserver();
133         RegisterVibrateStateObserver();
134         RegisterIntellVoiceObserver();
135         observerRegisted_ = true;
136     }
137 }
138 
AutoAdjustBrightnessUpdateFunc()139 static void AutoAdjustBrightnessUpdateFunc()
140 {
141     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
142     int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS);
143     auto setVal = SettingHelper::GetSettingAutoAdjustBrightness(switchVal);
144     if (setVal == switchVal) {
145         return;
146     }
147     policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS);
148 }
149 
RegisterAutoAdjustBrightnessObserver()150 void PowerModeModule::RegisterAutoAdjustBrightnessObserver()
151 {
152     if (g_autoAdjustBrightnessObserver) {
153         POWER_HILOGD(FEATURE_POWER_MODE, "auto adjust brightness observer already registed");
154         return;
155     }
156     SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {
157         AutoAdjustBrightnessUpdateFunc();
158     };
159     g_autoAdjustBrightnessObserver = SettingHelper::RegisterSettingAutoAdjustBrightnessObserver(updateFunc);
160 }
161 
WindowRotationUpdateFunc()162 static void WindowRotationUpdateFunc()
163 {
164     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
165     int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION);
166     auto setVal = SettingHelper::GetSettingWindowRotation(switchVal);
167     if (setVal == switchVal) {
168         return;
169     }
170     policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION);
171 }
172 
RegisterAutoWindowRotationObserver()173 void PowerModeModule::RegisterAutoWindowRotationObserver()
174 {
175     if (g_autoWindowRotationObserver) {
176         POWER_HILOGD(FEATURE_POWER_MODE, "auto window rotation observer already registed");
177         return;
178     }
179     SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {
180         WindowRotationUpdateFunc();
181     };
182     g_autoWindowRotationObserver = SettingHelper::RegisterSettingWindowRotationObserver(updateFunc);
183 }
184 
VibrateStateUpdateFunc()185 static void VibrateStateUpdateFunc()
186 {
187     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
188     int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::VIBRATORS_STATE);
189     auto setVal = SettingHelper::GetSettingVibration(switchVal);
190     if (setVal == switchVal) {
191         return;
192     }
193     policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::VIBRATORS_STATE);
194 }
195 
RegisterVibrateStateObserver()196 void PowerModeModule::RegisterVibrateStateObserver()
197 {
198     if (g_vibratorsStateObserver) {
199         POWER_HILOGD(FEATURE_POWER_MODE, "vibrate state observer already registed");
200         return;
201     }
202     SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {
203         VibrateStateUpdateFunc();
204     };
205     g_vibratorsStateObserver = SettingHelper::RegisterSettingVibrationObserver(updateFunc);
206 }
207 
IntellVoiceUpdateFunc()208 static void IntellVoiceUpdateFunc()
209 {
210     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
211     int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::INTELL_VOICE);
212     auto setVal = SettingHelper::GetSettingIntellVoice(switchVal);
213     if (setVal == switchVal) {
214         return;
215     }
216     policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::INTELL_VOICE);
217 }
218 
RegisterIntellVoiceObserver()219 void PowerModeModule::RegisterIntellVoiceObserver()
220 {
221     if (g_intellVoiceObserver) {
222         POWER_HILOGD(FEATURE_POWER_MODE, "intell voice observer already registed");
223         return;
224     }
225     SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {
226         IntellVoiceUpdateFunc();
227     };
228     g_intellVoiceObserver = SettingHelper::RegisterSettingIntellVoiceObserver(updateFunc);
229 }
230 
EnableMode(PowerMode mode,bool isBoot)231 void PowerModeModule::EnableMode(PowerMode mode, bool isBoot)
232 {
233     if (started_) {
234         POWER_HILOGW(FEATURE_POWER_MODE, "Power Mode is already running");
235         return;
236     }
237 
238     started_ = true;
239     mode_ = mode;
240 
241     /* Update power mode policy */
242     UpdateModepolicy();
243 
244     /* Send state change */
245     Prepare();
246 
247     /* Set action */
248     RunAction(isBoot);
249 
250     /* Save power mode status to setting data*/
251     SettingHelper::SaveCurrentMode(static_cast<int32_t>(mode));
252 
253     this->lastMode_ = static_cast<uint32_t>(mode);
254     started_ = false;
255 }
256 
UpdateModepolicy()257 void PowerModeModule::UpdateModepolicy()
258 {
259     /* update policy */
260     DelayedSingleton<PowerModePolicy>::GetInstance()->UpdatePowerModePolicy(static_cast<uint32_t>(this->mode_));
261 }
262 
AddPowerModeCallback(const sptr<IPowerModeCallback> & callback)263 void PowerModeModule::AddPowerModeCallback(const sptr<IPowerModeCallback>& callback)
264 {
265     if (callbackMgr_) {
266         callbackMgr_->AddCallback(callback);
267     }
268 }
269 
DelPowerModeCallback(const sptr<IPowerModeCallback> & callback)270 void PowerModeModule::DelPowerModeCallback(const sptr<IPowerModeCallback>& callback)
271 {
272     if (callbackMgr_) {
273         callbackMgr_->RemoveCallback(callback);
274     }
275 }
276 
Prepare()277 void PowerModeModule::Prepare()
278 {
279     PublishPowerModeEvent();
280     if (callbackMgr_) {
281         callbackMgr_->WaitingCallback();
282     }
283 }
284 
AddCallback(const sptr<IPowerModeCallback> & callback)285 void PowerModeModule::CallbackManager::AddCallback(const sptr<IPowerModeCallback>& callback)
286 {
287     unique_lock<mutex> lock(mutex_);
288     RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr));
289     auto object = callback->AsObject();
290     auto retIt = callbacks_.insert(object);
291     if (retIt.second) {
292         object->AddDeathRecipient(this);
293     }
294     POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu, insertOk = %{public}d",
295         callbacks_.size(), retIt.second);
296 }
297 
RemoveCallback(const sptr<IPowerModeCallback> & callback)298 void PowerModeModule::CallbackManager::RemoveCallback(const sptr<IPowerModeCallback>& callback)
299 {
300     unique_lock<mutex> lock(mutex_);
301     RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr));
302     auto object = callback->AsObject();
303     auto it = find(callbacks_.begin(), callbacks_.end(), object);
304     if (it != callbacks_.end()) {
305         callbacks_.erase(it);
306         object->RemoveDeathRecipient(this);
307     }
308     POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu", callbacks_.size());
309 }
310 
OnRemoteDied(const wptr<IRemoteObject> & remote)311 void PowerModeModule::CallbackManager::OnRemoteDied(const wptr<IRemoteObject>& remote)
312 {
313     POWER_HILOGW(FEATURE_POWER_MODE, "On remote died");
314     RETURN_IF(remote.promote() == nullptr);
315     RemoveCallback(iface_cast<IPowerModeCallback>(remote.promote()));
316 }
317 
WaitingCallback()318 void PowerModeModule::CallbackManager::WaitingCallback()
319 {
320     POWER_HILOGD(FEATURE_POWER_MODE, "Mode callback started");
321     unique_lock<mutex> lock(mutex_);
322     for (auto& obj: callbacks_) {
323         sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
324         if (callback != nullptr) {
325             POWER_HILOGD(FEATURE_POWER_MODE, "Call IPowerModeCallback");
326             PowerMode mode = PowerMode::NORMAL_MODE;
327             callback->OnPowerModeChanged(mode);
328         }
329     }
330 }
331 
PublishPowerModeEvent()332 void PowerModeModule::PublishPowerModeEvent()
333 {
334     POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event");
335     /* send event */
336     CommonEventPublishInfo publishInfo;
337     publishInfo.SetOrdered(false);
338     std::string action;
339     uint32_t code;
340     std::string data;
341     switch (mode_) {
342         case PowerMode::PERFORMANCE_MODE:
343             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
344             code = static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE);
345             data = ToString(static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE));
346             break;
347         case PowerMode::NORMAL_MODE:
348             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
349             code = static_cast<uint32_t>(PowerMode::NORMAL_MODE);
350             data = ToString(static_cast<uint32_t>(PowerMode::NORMAL_MODE));
351             break;
352         case PowerMode::POWER_SAVE_MODE:
353             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
354             code = static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE);
355             data = ToString(static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE));
356             break;
357         case PowerMode::EXTREME_POWER_SAVE_MODE:
358             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
359             code = static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE);
360             data = ToString(static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE));
361             break;
362         default:
363             POWER_HILOGW(FEATURE_POWER_MODE, "Unknown mode");
364             return;
365     }
366     IntentWant setModeWant;
367     setModeWant.SetAction(action);
368     CommonEventData event(setModeWant);
369     event.SetCode(code);
370     event.SetData(data);
371     if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
372         POWER_HILOGE(FEATURE_POWER_MODE, "Failed to publish the mode event");
373         return;
374     }
375     POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event end");
376 }
377 
RunAction(bool isBoot)378 void PowerModeModule::RunAction(bool isBoot)
379 {
380     POWER_HILOGD(FEATURE_POWER_MODE, "Run action");
381     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
382     policy->TriggerAllActions(isBoot);
383 }
384 
SetDisplayOffTime(bool isBoot)385 void PowerModeModule::SetDisplayOffTime(bool isBoot)
386 {
387     if (isBoot && SettingHelper::IsDisplayOffTimeSettingValid()) {
388         return;
389     }
390     int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance()
391         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::DISPLAY_OFFTIME);
392     if (time == INIT_VALUE_FALSE) {
393         return;
394     }
395     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
396     POWER_HILOGI(FEATURE_POWER_MODE, "Set default display off timeout: %{public}d", time);
397     bool needUpdateSetting = time > 0;
398     pms->GetPowerStateMachine()->SetDisplayOffTime(static_cast<int64_t>(time), needUpdateSetting);
399 }
400 
401 void PowerModeModule::SetSleepTime([[maybe_unused]] bool isBoot)
402 {
403     int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance()
404         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::SLEEPTIME);
405     if (time == INIT_VALUE_FALSE) {
406         return;
407     }
408     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
409     POWER_HILOGD(FEATURE_POWER_MODE, "Set sleep timeout: %{public}d", time);
410     pms->GetPowerStateMachine()->SetSleepTime(static_cast<int64_t>(time));
411 }
412 
SetAutoAdjustBrightness(bool isBoot)413 void PowerModeModule::SetAutoAdjustBrightness(bool isBoot)
414 {
415     if (isBoot && SettingHelper::IsAutoAdjustBrightnessSettingValid()) {
416         return;
417     }
418     int32_t value = DelayedSingleton<PowerModePolicy>::GetInstance()
419         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS);
420     auto status = static_cast<SettingHelper::SwitchStatus>(value);
421     POWER_HILOGD(FEATURE_POWER_MODE, "Set auto adjust brightness status: %{public}d", status);
422     if (value == INIT_VALUE_FALSE) {
423         return;
424     }
425     SettingHelper::SetSettingAutoAdjustBrightness(status);
426 }
427 
SetLcdBrightness(bool isBoot)428 void PowerModeModule::SetLcdBrightness(bool isBoot)
429 {
430     if (isBoot && SettingHelper::IsBrightnessSettingValid()) {
431         return;
432     }
433     int32_t lcdBrightness = DelayedSingleton<PowerModePolicy>::GetInstance()
434         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::LCD_BRIGHTNESS);
435     POWER_HILOGD(FEATURE_POWER_MODE, "Set lcd Brightness: %{public}d", lcdBrightness);
436     if (lcdBrightness == INIT_VALUE_FALSE) {
437         return;
438     }
439     SettingHelper::SetSettingBrightness(lcdBrightness);
440 #ifdef HAS_DISPLAY_MANAGER
441     OHOS::DisplayPowerMgr::DisplayPowerMgrClient::GetInstance().SetBrightness(lcdBrightness);
442 #endif
443 }
444 
SetVibration(bool isBoot)445 void PowerModeModule::SetVibration(bool isBoot)
446 {
447     if (isBoot && SettingHelper::IsVibrationSettingValid()) {
448         return;
449     }
450     int32_t vibration = DelayedSingleton<PowerModePolicy>::GetInstance()
451         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::VIBRATORS_STATE);
452     POWER_HILOGI(FEATURE_POWER_MODE, "Set vibrate state %{public}d", vibration);
453     if (vibration == INIT_VALUE_FALSE) {
454         return;
455     }
456     SettingHelper::SetSettingVibration(static_cast<SettingHelper::SwitchStatus>(vibration));
457 }
458 
SetWindowRotation(bool isBoot)459 void PowerModeModule::SetWindowRotation(bool isBoot)
460 {
461     if (isBoot && SettingHelper::IsWindowRotationSettingValid()) {
462         return;
463     }
464     int32_t rotation = DelayedSingleton<PowerModePolicy>::GetInstance()
465         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION);
466     POWER_HILOGD(FEATURE_POWER_MODE, "Set window rotation state %{public}d", rotation);
467     if (rotation == INIT_VALUE_FALSE) {
468         return;
469     }
470     SettingHelper::SetSettingWindowRotation(static_cast<SettingHelper::SwitchStatus>(rotation));
471 }
472 
SetIntellVoiceState(bool isBoot)473 void PowerModeModule::SetIntellVoiceState(bool isBoot)
474 {
475     if (isBoot && SettingHelper::IsIntellVoiceSettingValid()) {
476         return;
477     }
478     int32_t state = DelayedSingleton<PowerModePolicy>::GetInstance()
479         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::INTELL_VOICE);
480     POWER_HILOGD(FEATURE_POWER_MODE, "Set intell voice state %{public}d", state);
481     if (state == INIT_VALUE_FALSE) {
482         return;
483     }
484     SettingHelper::SetSettingIntellVoice(static_cast<SettingHelper::SwitchStatus>(state));
485 }
486 } // namespace PowerMgr
487 } // namespace OHOS
488