15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include "power_mode_module.h" 175ccb8f90Sopenharmony_ci 185ccb8f90Sopenharmony_ci#ifdef HAS_DISPLAY_MANAGER 195ccb8f90Sopenharmony_ci#include "display_power_mgr_client.h" 205ccb8f90Sopenharmony_ci#endif 215ccb8f90Sopenharmony_ci#include "power_log.h" 225ccb8f90Sopenharmony_ci#include "power_mode_policy.h" 235ccb8f90Sopenharmony_ci#include "power_mgr_service.h" 245ccb8f90Sopenharmony_ci#include "setting_helper.h" 255ccb8f90Sopenharmony_ci 265ccb8f90Sopenharmony_ci#include "singleton.h" 275ccb8f90Sopenharmony_ci 285ccb8f90Sopenharmony_ciusing namespace std; 295ccb8f90Sopenharmony_ciusing namespace OHOS; 305ccb8f90Sopenharmony_ciusing namespace OHOS::AAFwk; 315ccb8f90Sopenharmony_ciusing namespace OHOS::EventFwk; 325ccb8f90Sopenharmony_ci 335ccb8f90Sopenharmony_cinamespace OHOS { 345ccb8f90Sopenharmony_cinamespace PowerMgr { 355ccb8f90Sopenharmony_cinamespace { 365ccb8f90Sopenharmony_cisptr<SettingObserver> g_autoAdjustBrightnessObserver; 375ccb8f90Sopenharmony_cisptr<SettingObserver> g_autoWindowRotationObserver; 385ccb8f90Sopenharmony_cisptr<SettingObserver> g_vibratorsStateObserver; 395ccb8f90Sopenharmony_cisptr<SettingObserver> g_intellVoiceObserver; 405ccb8f90Sopenharmony_ci} 415ccb8f90Sopenharmony_ci 425ccb8f90Sopenharmony_ciPowerModeModule::PowerModeModule() 435ccb8f90Sopenharmony_ci : mode_(PowerMode::NORMAL_MODE), lastMode_(LAST_MODE_FLAG), started_(false) 445ccb8f90Sopenharmony_ci{ 455ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_MODE, "Instance create"); 465ccb8f90Sopenharmony_ci callbackMgr_ = new CallbackManager(); 475ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 485ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction displayOffTimeAction = [&](bool isInit) { SetDisplayOffTime(isInit); }; 495ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::DISPLAY_OFFTIME, displayOffTimeAction); 505ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction sleepTimeAction = [&](bool isInit) { SetSleepTime(isInit); }; 515ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::SLEEPTIME, sleepTimeAction); 525ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction autoAdjustBrightnessAction = [&](bool isInit) { SetAutoAdjustBrightness(isInit); }; 535ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS, autoAdjustBrightnessAction); 545ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction lcdBrightnessAction = [&](bool isInit) { SetLcdBrightness(isInit); }; 555ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::LCD_BRIGHTNESS, lcdBrightnessAction); 565ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction vibrationAction = [&](bool isInit) { SetVibration(isInit); }; 575ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::VIBRATORS_STATE, vibrationAction); 585ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction onOffRotationAction = [&](bool isInit) { SetWindowRotation(isInit); }; 595ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION, onOffRotationAction); 605ccb8f90Sopenharmony_ci PowerModePolicy::ModeAction intellVoiceAction = [&](bool isInit) { SetIntellVoiceState(isInit); }; 615ccb8f90Sopenharmony_ci policy->AddAction(PowerModePolicy::ServiceType::INTELL_VOICE, intellVoiceAction); 625ccb8f90Sopenharmony_ci} 635ccb8f90Sopenharmony_ci 645ccb8f90Sopenharmony_civoid PowerModeModule::InitPowerMode() 655ccb8f90Sopenharmony_ci{ 665ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_MODE, "Start to init power mode."); 675ccb8f90Sopenharmony_ci int32_t saveMode = SettingHelper::ReadCurrentMode(static_cast<int32_t>(this->mode_)); 685ccb8f90Sopenharmony_ci this->mode_ = static_cast<PowerMode>(saveMode); 695ccb8f90Sopenharmony_ci Prepare(); 705ccb8f90Sopenharmony_ci if (!DelayedSingleton<PowerModePolicy>::GetInstance()->InitRecoverMap()) { 715ccb8f90Sopenharmony_ci UpdateModepolicy(); 725ccb8f90Sopenharmony_ci RunAction(false); 735ccb8f90Sopenharmony_ci } 745ccb8f90Sopenharmony_ci} 755ccb8f90Sopenharmony_ci 765ccb8f90Sopenharmony_civoid PowerModeModule::SetModeItem(PowerMode mode) 775ccb8f90Sopenharmony_ci{ 785ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_MODE, "SetModeItem mode_: %{public}u, mode: %{public}u", mode_, mode); 795ccb8f90Sopenharmony_ci 805ccb8f90Sopenharmony_ci /* Same as the previous mode */ 815ccb8f90Sopenharmony_ci if (this->mode_ == mode && this->mode_ != PowerMode::PERFORMANCE_MODE) { 825ccb8f90Sopenharmony_ci return; 835ccb8f90Sopenharmony_ci } 845ccb8f90Sopenharmony_ci 855ccb8f90Sopenharmony_ci /* If it's a valid mode */ 865ccb8f90Sopenharmony_ci if (mode < PowerMode::POWER_MODE_MIN || mode > PowerMode::POWER_MODE_MAX) { 875ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_MODE, "Invalid mode %{public}d", mode); 885ccb8f90Sopenharmony_ci return; 895ccb8f90Sopenharmony_ci } 905ccb8f90Sopenharmony_ci 915ccb8f90Sopenharmony_ci /* unregister setting observer for current mode */ 925ccb8f90Sopenharmony_ci UnregisterSaveModeObserver(); 935ccb8f90Sopenharmony_ci 945ccb8f90Sopenharmony_ci /* start set mode thread */ 955ccb8f90Sopenharmony_ci EnableMode(mode); 965ccb8f90Sopenharmony_ci 975ccb8f90Sopenharmony_ci /* register setting observer for save mode */ 985ccb8f90Sopenharmony_ci RegisterSaveModeObserver(); 995ccb8f90Sopenharmony_ci} 1005ccb8f90Sopenharmony_ci 1015ccb8f90Sopenharmony_ciPowerMode PowerModeModule::GetModeItem() 1025ccb8f90Sopenharmony_ci{ 1035ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "GetModeItem mode_: %{public}u", mode_); 1045ccb8f90Sopenharmony_ci /* get power mode */ 1055ccb8f90Sopenharmony_ci return mode_; 1065ccb8f90Sopenharmony_ci} 1075ccb8f90Sopenharmony_ci 1085ccb8f90Sopenharmony_civoid PowerModeModule::UnregisterSaveModeObserver() 1095ccb8f90Sopenharmony_ci{ 1105ccb8f90Sopenharmony_ci if (!this->observerRegisted_) { 1115ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "current power mode is normal mode"); 1125ccb8f90Sopenharmony_ci return; 1135ccb8f90Sopenharmony_ci } 1145ccb8f90Sopenharmony_ci 1155ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_MODE, "unregister setting observer for save mode"); 1165ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_autoAdjustBrightnessObserver); 1175ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_autoWindowRotationObserver); 1185ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_vibratorsStateObserver); 1195ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_intellVoiceObserver); 1205ccb8f90Sopenharmony_ci g_autoAdjustBrightnessObserver = nullptr; 1215ccb8f90Sopenharmony_ci g_autoWindowRotationObserver = nullptr; 1225ccb8f90Sopenharmony_ci g_vibratorsStateObserver = nullptr; 1235ccb8f90Sopenharmony_ci g_intellVoiceObserver = nullptr; 1245ccb8f90Sopenharmony_ci observerRegisted_ = false; 1255ccb8f90Sopenharmony_ci} 1265ccb8f90Sopenharmony_ci 1275ccb8f90Sopenharmony_civoid PowerModeModule::RegisterSaveModeObserver() 1285ccb8f90Sopenharmony_ci{ 1295ccb8f90Sopenharmony_ci if (this->mode_ == PowerMode::POWER_SAVE_MODE || this->mode_ == PowerMode::EXTREME_POWER_SAVE_MODE) { 1305ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "register setting observer in save mode"); 1315ccb8f90Sopenharmony_ci RegisterAutoAdjustBrightnessObserver(); 1325ccb8f90Sopenharmony_ci RegisterAutoWindowRotationObserver(); 1335ccb8f90Sopenharmony_ci RegisterVibrateStateObserver(); 1345ccb8f90Sopenharmony_ci RegisterIntellVoiceObserver(); 1355ccb8f90Sopenharmony_ci observerRegisted_ = true; 1365ccb8f90Sopenharmony_ci } 1375ccb8f90Sopenharmony_ci} 1385ccb8f90Sopenharmony_ci 1395ccb8f90Sopenharmony_cistatic void AutoAdjustBrightnessUpdateFunc() 1405ccb8f90Sopenharmony_ci{ 1415ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 1425ccb8f90Sopenharmony_ci int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS); 1435ccb8f90Sopenharmony_ci auto setVal = SettingHelper::GetSettingAutoAdjustBrightness(switchVal); 1445ccb8f90Sopenharmony_ci if (setVal == switchVal) { 1455ccb8f90Sopenharmony_ci return; 1465ccb8f90Sopenharmony_ci } 1475ccb8f90Sopenharmony_ci policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS); 1485ccb8f90Sopenharmony_ci} 1495ccb8f90Sopenharmony_ci 1505ccb8f90Sopenharmony_civoid PowerModeModule::RegisterAutoAdjustBrightnessObserver() 1515ccb8f90Sopenharmony_ci{ 1525ccb8f90Sopenharmony_ci if (g_autoAdjustBrightnessObserver) { 1535ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "auto adjust brightness observer already registed"); 1545ccb8f90Sopenharmony_ci return; 1555ccb8f90Sopenharmony_ci } 1565ccb8f90Sopenharmony_ci SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { 1575ccb8f90Sopenharmony_ci AutoAdjustBrightnessUpdateFunc(); 1585ccb8f90Sopenharmony_ci }; 1595ccb8f90Sopenharmony_ci g_autoAdjustBrightnessObserver = SettingHelper::RegisterSettingAutoAdjustBrightnessObserver(updateFunc); 1605ccb8f90Sopenharmony_ci} 1615ccb8f90Sopenharmony_ci 1625ccb8f90Sopenharmony_cistatic void WindowRotationUpdateFunc() 1635ccb8f90Sopenharmony_ci{ 1645ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 1655ccb8f90Sopenharmony_ci int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION); 1665ccb8f90Sopenharmony_ci auto setVal = SettingHelper::GetSettingWindowRotation(switchVal); 1675ccb8f90Sopenharmony_ci if (setVal == switchVal) { 1685ccb8f90Sopenharmony_ci return; 1695ccb8f90Sopenharmony_ci } 1705ccb8f90Sopenharmony_ci policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION); 1715ccb8f90Sopenharmony_ci} 1725ccb8f90Sopenharmony_ci 1735ccb8f90Sopenharmony_civoid PowerModeModule::RegisterAutoWindowRotationObserver() 1745ccb8f90Sopenharmony_ci{ 1755ccb8f90Sopenharmony_ci if (g_autoWindowRotationObserver) { 1765ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "auto window rotation observer already registed"); 1775ccb8f90Sopenharmony_ci return; 1785ccb8f90Sopenharmony_ci } 1795ccb8f90Sopenharmony_ci SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { 1805ccb8f90Sopenharmony_ci WindowRotationUpdateFunc(); 1815ccb8f90Sopenharmony_ci }; 1825ccb8f90Sopenharmony_ci g_autoWindowRotationObserver = SettingHelper::RegisterSettingWindowRotationObserver(updateFunc); 1835ccb8f90Sopenharmony_ci} 1845ccb8f90Sopenharmony_ci 1855ccb8f90Sopenharmony_cistatic void VibrateStateUpdateFunc() 1865ccb8f90Sopenharmony_ci{ 1875ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 1885ccb8f90Sopenharmony_ci int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::VIBRATORS_STATE); 1895ccb8f90Sopenharmony_ci auto setVal = SettingHelper::GetSettingVibration(switchVal); 1905ccb8f90Sopenharmony_ci if (setVal == switchVal) { 1915ccb8f90Sopenharmony_ci return; 1925ccb8f90Sopenharmony_ci } 1935ccb8f90Sopenharmony_ci policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::VIBRATORS_STATE); 1945ccb8f90Sopenharmony_ci} 1955ccb8f90Sopenharmony_ci 1965ccb8f90Sopenharmony_civoid PowerModeModule::RegisterVibrateStateObserver() 1975ccb8f90Sopenharmony_ci{ 1985ccb8f90Sopenharmony_ci if (g_vibratorsStateObserver) { 1995ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "vibrate state observer already registed"); 2005ccb8f90Sopenharmony_ci return; 2015ccb8f90Sopenharmony_ci } 2025ccb8f90Sopenharmony_ci SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { 2035ccb8f90Sopenharmony_ci VibrateStateUpdateFunc(); 2045ccb8f90Sopenharmony_ci }; 2055ccb8f90Sopenharmony_ci g_vibratorsStateObserver = SettingHelper::RegisterSettingVibrationObserver(updateFunc); 2065ccb8f90Sopenharmony_ci} 2075ccb8f90Sopenharmony_ci 2085ccb8f90Sopenharmony_cistatic void IntellVoiceUpdateFunc() 2095ccb8f90Sopenharmony_ci{ 2105ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 2115ccb8f90Sopenharmony_ci int32_t switchVal = policy->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::INTELL_VOICE); 2125ccb8f90Sopenharmony_ci auto setVal = SettingHelper::GetSettingIntellVoice(switchVal); 2135ccb8f90Sopenharmony_ci if (setVal == switchVal) { 2145ccb8f90Sopenharmony_ci return; 2155ccb8f90Sopenharmony_ci } 2165ccb8f90Sopenharmony_ci policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::INTELL_VOICE); 2175ccb8f90Sopenharmony_ci} 2185ccb8f90Sopenharmony_ci 2195ccb8f90Sopenharmony_civoid PowerModeModule::RegisterIntellVoiceObserver() 2205ccb8f90Sopenharmony_ci{ 2215ccb8f90Sopenharmony_ci if (g_intellVoiceObserver) { 2225ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "intell voice observer already registed"); 2235ccb8f90Sopenharmony_ci return; 2245ccb8f90Sopenharmony_ci } 2255ccb8f90Sopenharmony_ci SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { 2265ccb8f90Sopenharmony_ci IntellVoiceUpdateFunc(); 2275ccb8f90Sopenharmony_ci }; 2285ccb8f90Sopenharmony_ci g_intellVoiceObserver = SettingHelper::RegisterSettingIntellVoiceObserver(updateFunc); 2295ccb8f90Sopenharmony_ci} 2305ccb8f90Sopenharmony_ci 2315ccb8f90Sopenharmony_civoid PowerModeModule::EnableMode(PowerMode mode, bool isBoot) 2325ccb8f90Sopenharmony_ci{ 2335ccb8f90Sopenharmony_ci if (started_) { 2345ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_MODE, "Power Mode is already running"); 2355ccb8f90Sopenharmony_ci return; 2365ccb8f90Sopenharmony_ci } 2375ccb8f90Sopenharmony_ci 2385ccb8f90Sopenharmony_ci started_ = true; 2395ccb8f90Sopenharmony_ci mode_ = mode; 2405ccb8f90Sopenharmony_ci 2415ccb8f90Sopenharmony_ci /* Update power mode policy */ 2425ccb8f90Sopenharmony_ci UpdateModepolicy(); 2435ccb8f90Sopenharmony_ci 2445ccb8f90Sopenharmony_ci /* Send state change */ 2455ccb8f90Sopenharmony_ci Prepare(); 2465ccb8f90Sopenharmony_ci 2475ccb8f90Sopenharmony_ci /* Set action */ 2485ccb8f90Sopenharmony_ci RunAction(isBoot); 2495ccb8f90Sopenharmony_ci 2505ccb8f90Sopenharmony_ci /* Save power mode status to setting data*/ 2515ccb8f90Sopenharmony_ci SettingHelper::SaveCurrentMode(static_cast<int32_t>(mode)); 2525ccb8f90Sopenharmony_ci 2535ccb8f90Sopenharmony_ci this->lastMode_ = static_cast<uint32_t>(mode); 2545ccb8f90Sopenharmony_ci started_ = false; 2555ccb8f90Sopenharmony_ci} 2565ccb8f90Sopenharmony_ci 2575ccb8f90Sopenharmony_civoid PowerModeModule::UpdateModepolicy() 2585ccb8f90Sopenharmony_ci{ 2595ccb8f90Sopenharmony_ci /* update policy */ 2605ccb8f90Sopenharmony_ci DelayedSingleton<PowerModePolicy>::GetInstance()->UpdatePowerModePolicy(static_cast<uint32_t>(this->mode_)); 2615ccb8f90Sopenharmony_ci} 2625ccb8f90Sopenharmony_ci 2635ccb8f90Sopenharmony_civoid PowerModeModule::AddPowerModeCallback(const sptr<IPowerModeCallback>& callback) 2645ccb8f90Sopenharmony_ci{ 2655ccb8f90Sopenharmony_ci if (callbackMgr_) { 2665ccb8f90Sopenharmony_ci callbackMgr_->AddCallback(callback); 2675ccb8f90Sopenharmony_ci } 2685ccb8f90Sopenharmony_ci} 2695ccb8f90Sopenharmony_ci 2705ccb8f90Sopenharmony_civoid PowerModeModule::DelPowerModeCallback(const sptr<IPowerModeCallback>& callback) 2715ccb8f90Sopenharmony_ci{ 2725ccb8f90Sopenharmony_ci if (callbackMgr_) { 2735ccb8f90Sopenharmony_ci callbackMgr_->RemoveCallback(callback); 2745ccb8f90Sopenharmony_ci } 2755ccb8f90Sopenharmony_ci} 2765ccb8f90Sopenharmony_ci 2775ccb8f90Sopenharmony_civoid PowerModeModule::Prepare() 2785ccb8f90Sopenharmony_ci{ 2795ccb8f90Sopenharmony_ci PublishPowerModeEvent(); 2805ccb8f90Sopenharmony_ci if (callbackMgr_) { 2815ccb8f90Sopenharmony_ci callbackMgr_->WaitingCallback(); 2825ccb8f90Sopenharmony_ci } 2835ccb8f90Sopenharmony_ci} 2845ccb8f90Sopenharmony_ci 2855ccb8f90Sopenharmony_civoid PowerModeModule::CallbackManager::AddCallback(const sptr<IPowerModeCallback>& callback) 2865ccb8f90Sopenharmony_ci{ 2875ccb8f90Sopenharmony_ci unique_lock<mutex> lock(mutex_); 2885ccb8f90Sopenharmony_ci RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr)); 2895ccb8f90Sopenharmony_ci auto object = callback->AsObject(); 2905ccb8f90Sopenharmony_ci auto retIt = callbacks_.insert(object); 2915ccb8f90Sopenharmony_ci if (retIt.second) { 2925ccb8f90Sopenharmony_ci object->AddDeathRecipient(this); 2935ccb8f90Sopenharmony_ci } 2945ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu, insertOk = %{public}d", 2955ccb8f90Sopenharmony_ci callbacks_.size(), retIt.second); 2965ccb8f90Sopenharmony_ci} 2975ccb8f90Sopenharmony_ci 2985ccb8f90Sopenharmony_civoid PowerModeModule::CallbackManager::RemoveCallback(const sptr<IPowerModeCallback>& callback) 2995ccb8f90Sopenharmony_ci{ 3005ccb8f90Sopenharmony_ci unique_lock<mutex> lock(mutex_); 3015ccb8f90Sopenharmony_ci RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr)); 3025ccb8f90Sopenharmony_ci auto object = callback->AsObject(); 3035ccb8f90Sopenharmony_ci auto it = find(callbacks_.begin(), callbacks_.end(), object); 3045ccb8f90Sopenharmony_ci if (it != callbacks_.end()) { 3055ccb8f90Sopenharmony_ci callbacks_.erase(it); 3065ccb8f90Sopenharmony_ci object->RemoveDeathRecipient(this); 3075ccb8f90Sopenharmony_ci } 3085ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu", callbacks_.size()); 3095ccb8f90Sopenharmony_ci} 3105ccb8f90Sopenharmony_ci 3115ccb8f90Sopenharmony_civoid PowerModeModule::CallbackManager::OnRemoteDied(const wptr<IRemoteObject>& remote) 3125ccb8f90Sopenharmony_ci{ 3135ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_MODE, "On remote died"); 3145ccb8f90Sopenharmony_ci RETURN_IF(remote.promote() == nullptr); 3155ccb8f90Sopenharmony_ci RemoveCallback(iface_cast<IPowerModeCallback>(remote.promote())); 3165ccb8f90Sopenharmony_ci} 3175ccb8f90Sopenharmony_ci 3185ccb8f90Sopenharmony_civoid PowerModeModule::CallbackManager::WaitingCallback() 3195ccb8f90Sopenharmony_ci{ 3205ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Mode callback started"); 3215ccb8f90Sopenharmony_ci unique_lock<mutex> lock(mutex_); 3225ccb8f90Sopenharmony_ci for (auto& obj: callbacks_) { 3235ccb8f90Sopenharmony_ci sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj); 3245ccb8f90Sopenharmony_ci if (callback != nullptr) { 3255ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Call IPowerModeCallback"); 3265ccb8f90Sopenharmony_ci PowerMode mode = PowerMode::NORMAL_MODE; 3275ccb8f90Sopenharmony_ci callback->OnPowerModeChanged(mode); 3285ccb8f90Sopenharmony_ci } 3295ccb8f90Sopenharmony_ci } 3305ccb8f90Sopenharmony_ci} 3315ccb8f90Sopenharmony_ci 3325ccb8f90Sopenharmony_civoid PowerModeModule::PublishPowerModeEvent() 3335ccb8f90Sopenharmony_ci{ 3345ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event"); 3355ccb8f90Sopenharmony_ci /* send event */ 3365ccb8f90Sopenharmony_ci CommonEventPublishInfo publishInfo; 3375ccb8f90Sopenharmony_ci publishInfo.SetOrdered(false); 3385ccb8f90Sopenharmony_ci std::string action; 3395ccb8f90Sopenharmony_ci uint32_t code; 3405ccb8f90Sopenharmony_ci std::string data; 3415ccb8f90Sopenharmony_ci switch (mode_) { 3425ccb8f90Sopenharmony_ci case PowerMode::PERFORMANCE_MODE: 3435ccb8f90Sopenharmony_ci action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED; 3445ccb8f90Sopenharmony_ci code = static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE); 3455ccb8f90Sopenharmony_ci data = ToString(static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE)); 3465ccb8f90Sopenharmony_ci break; 3475ccb8f90Sopenharmony_ci case PowerMode::NORMAL_MODE: 3485ccb8f90Sopenharmony_ci action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED; 3495ccb8f90Sopenharmony_ci code = static_cast<uint32_t>(PowerMode::NORMAL_MODE); 3505ccb8f90Sopenharmony_ci data = ToString(static_cast<uint32_t>(PowerMode::NORMAL_MODE)); 3515ccb8f90Sopenharmony_ci break; 3525ccb8f90Sopenharmony_ci case PowerMode::POWER_SAVE_MODE: 3535ccb8f90Sopenharmony_ci action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED; 3545ccb8f90Sopenharmony_ci code = static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE); 3555ccb8f90Sopenharmony_ci data = ToString(static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE)); 3565ccb8f90Sopenharmony_ci break; 3575ccb8f90Sopenharmony_ci case PowerMode::EXTREME_POWER_SAVE_MODE: 3585ccb8f90Sopenharmony_ci action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED; 3595ccb8f90Sopenharmony_ci code = static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE); 3605ccb8f90Sopenharmony_ci data = ToString(static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE)); 3615ccb8f90Sopenharmony_ci break; 3625ccb8f90Sopenharmony_ci default: 3635ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_MODE, "Unknown mode"); 3645ccb8f90Sopenharmony_ci return; 3655ccb8f90Sopenharmony_ci } 3665ccb8f90Sopenharmony_ci IntentWant setModeWant; 3675ccb8f90Sopenharmony_ci setModeWant.SetAction(action); 3685ccb8f90Sopenharmony_ci CommonEventData event(setModeWant); 3695ccb8f90Sopenharmony_ci event.SetCode(code); 3705ccb8f90Sopenharmony_ci event.SetData(data); 3715ccb8f90Sopenharmony_ci if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) { 3725ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_MODE, "Failed to publish the mode event"); 3735ccb8f90Sopenharmony_ci return; 3745ccb8f90Sopenharmony_ci } 3755ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event end"); 3765ccb8f90Sopenharmony_ci} 3775ccb8f90Sopenharmony_ci 3785ccb8f90Sopenharmony_civoid PowerModeModule::RunAction(bool isBoot) 3795ccb8f90Sopenharmony_ci{ 3805ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Run action"); 3815ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 3825ccb8f90Sopenharmony_ci policy->TriggerAllActions(isBoot); 3835ccb8f90Sopenharmony_ci} 3845ccb8f90Sopenharmony_ci 3855ccb8f90Sopenharmony_civoid PowerModeModule::SetDisplayOffTime(bool isBoot) 3865ccb8f90Sopenharmony_ci{ 3875ccb8f90Sopenharmony_ci if (isBoot && SettingHelper::IsDisplayOffTimeSettingValid()) { 3885ccb8f90Sopenharmony_ci return; 3895ccb8f90Sopenharmony_ci } 3905ccb8f90Sopenharmony_ci int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance() 3915ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::DISPLAY_OFFTIME); 3925ccb8f90Sopenharmony_ci if (time == INIT_VALUE_FALSE) { 3935ccb8f90Sopenharmony_ci return; 3945ccb8f90Sopenharmony_ci } 3955ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 3965ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_MODE, "Set default display off timeout: %{public}d", time); 3975ccb8f90Sopenharmony_ci bool needUpdateSetting = time > 0; 3985ccb8f90Sopenharmony_ci pms->GetPowerStateMachine()->SetDisplayOffTime(static_cast<int64_t>(time), needUpdateSetting); 3995ccb8f90Sopenharmony_ci} 4005ccb8f90Sopenharmony_ci 4015ccb8f90Sopenharmony_civoid PowerModeModule::SetSleepTime([[maybe_unused]] bool isBoot) 4025ccb8f90Sopenharmony_ci{ 4035ccb8f90Sopenharmony_ci int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance() 4045ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::SLEEPTIME); 4055ccb8f90Sopenharmony_ci if (time == INIT_VALUE_FALSE) { 4065ccb8f90Sopenharmony_ci return; 4075ccb8f90Sopenharmony_ci } 4085ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 4095ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Set sleep timeout: %{public}d", time); 4105ccb8f90Sopenharmony_ci pms->GetPowerStateMachine()->SetSleepTime(static_cast<int64_t>(time)); 4115ccb8f90Sopenharmony_ci} 4125ccb8f90Sopenharmony_ci 4135ccb8f90Sopenharmony_civoid PowerModeModule::SetAutoAdjustBrightness(bool isBoot) 4145ccb8f90Sopenharmony_ci{ 4155ccb8f90Sopenharmony_ci if (isBoot && SettingHelper::IsAutoAdjustBrightnessSettingValid()) { 4165ccb8f90Sopenharmony_ci return; 4175ccb8f90Sopenharmony_ci } 4185ccb8f90Sopenharmony_ci int32_t value = DelayedSingleton<PowerModePolicy>::GetInstance() 4195ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS); 4205ccb8f90Sopenharmony_ci auto status = static_cast<SettingHelper::SwitchStatus>(value); 4215ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Set auto adjust brightness status: %{public}d", status); 4225ccb8f90Sopenharmony_ci if (value == INIT_VALUE_FALSE) { 4235ccb8f90Sopenharmony_ci return; 4245ccb8f90Sopenharmony_ci } 4255ccb8f90Sopenharmony_ci SettingHelper::SetSettingAutoAdjustBrightness(status); 4265ccb8f90Sopenharmony_ci} 4275ccb8f90Sopenharmony_ci 4285ccb8f90Sopenharmony_civoid PowerModeModule::SetLcdBrightness(bool isBoot) 4295ccb8f90Sopenharmony_ci{ 4305ccb8f90Sopenharmony_ci if (isBoot && SettingHelper::IsBrightnessSettingValid()) { 4315ccb8f90Sopenharmony_ci return; 4325ccb8f90Sopenharmony_ci } 4335ccb8f90Sopenharmony_ci int32_t lcdBrightness = DelayedSingleton<PowerModePolicy>::GetInstance() 4345ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::LCD_BRIGHTNESS); 4355ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Set lcd Brightness: %{public}d", lcdBrightness); 4365ccb8f90Sopenharmony_ci if (lcdBrightness == INIT_VALUE_FALSE) { 4375ccb8f90Sopenharmony_ci return; 4385ccb8f90Sopenharmony_ci } 4395ccb8f90Sopenharmony_ci SettingHelper::SetSettingBrightness(lcdBrightness); 4405ccb8f90Sopenharmony_ci#ifdef HAS_DISPLAY_MANAGER 4415ccb8f90Sopenharmony_ci OHOS::DisplayPowerMgr::DisplayPowerMgrClient::GetInstance().SetBrightness(lcdBrightness); 4425ccb8f90Sopenharmony_ci#endif 4435ccb8f90Sopenharmony_ci} 4445ccb8f90Sopenharmony_ci 4455ccb8f90Sopenharmony_civoid PowerModeModule::SetVibration(bool isBoot) 4465ccb8f90Sopenharmony_ci{ 4475ccb8f90Sopenharmony_ci if (isBoot && SettingHelper::IsVibrationSettingValid()) { 4485ccb8f90Sopenharmony_ci return; 4495ccb8f90Sopenharmony_ci } 4505ccb8f90Sopenharmony_ci int32_t vibration = DelayedSingleton<PowerModePolicy>::GetInstance() 4515ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::VIBRATORS_STATE); 4525ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_MODE, "Set vibrate state %{public}d", vibration); 4535ccb8f90Sopenharmony_ci if (vibration == INIT_VALUE_FALSE) { 4545ccb8f90Sopenharmony_ci return; 4555ccb8f90Sopenharmony_ci } 4565ccb8f90Sopenharmony_ci SettingHelper::SetSettingVibration(static_cast<SettingHelper::SwitchStatus>(vibration)); 4575ccb8f90Sopenharmony_ci} 4585ccb8f90Sopenharmony_ci 4595ccb8f90Sopenharmony_civoid PowerModeModule::SetWindowRotation(bool isBoot) 4605ccb8f90Sopenharmony_ci{ 4615ccb8f90Sopenharmony_ci if (isBoot && SettingHelper::IsWindowRotationSettingValid()) { 4625ccb8f90Sopenharmony_ci return; 4635ccb8f90Sopenharmony_ci } 4645ccb8f90Sopenharmony_ci int32_t rotation = DelayedSingleton<PowerModePolicy>::GetInstance() 4655ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION); 4665ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Set window rotation state %{public}d", rotation); 4675ccb8f90Sopenharmony_ci if (rotation == INIT_VALUE_FALSE) { 4685ccb8f90Sopenharmony_ci return; 4695ccb8f90Sopenharmony_ci } 4705ccb8f90Sopenharmony_ci SettingHelper::SetSettingWindowRotation(static_cast<SettingHelper::SwitchStatus>(rotation)); 4715ccb8f90Sopenharmony_ci} 4725ccb8f90Sopenharmony_ci 4735ccb8f90Sopenharmony_civoid PowerModeModule::SetIntellVoiceState(bool isBoot) 4745ccb8f90Sopenharmony_ci{ 4755ccb8f90Sopenharmony_ci if (isBoot && SettingHelper::IsIntellVoiceSettingValid()) { 4765ccb8f90Sopenharmony_ci return; 4775ccb8f90Sopenharmony_ci } 4785ccb8f90Sopenharmony_ci int32_t state = DelayedSingleton<PowerModePolicy>::GetInstance() 4795ccb8f90Sopenharmony_ci ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::INTELL_VOICE); 4805ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_MODE, "Set intell voice state %{public}d", state); 4815ccb8f90Sopenharmony_ci if (state == INIT_VALUE_FALSE) { 4825ccb8f90Sopenharmony_ci return; 4835ccb8f90Sopenharmony_ci } 4845ccb8f90Sopenharmony_ci SettingHelper::SetSettingIntellVoice(static_cast<SettingHelper::SwitchStatus>(state)); 4855ccb8f90Sopenharmony_ci} 4865ccb8f90Sopenharmony_ci} // namespace PowerMgr 4875ccb8f90Sopenharmony_ci} // namespace OHOS 488