diff --git a/bundle.json b/bundle.json index c9cb08cec11f1a6de77eca0aaf28db5fa71c6900..bca79421240eff1563a00b564ad9579ee0c517e1 100644 --- a/bundle.json +++ b/bundle.json @@ -32,7 +32,8 @@ "power_manager_feature_doubleclick_or_pickup", "power_manager_feature_force_sleep_broadcast", "power_manager_feature_screen_on_timeout_check", - "power_manager_feature_dialog_path" + "power_manager_feature_dialog_path", + "power_manager_feature_charging_type_setting" ], "adapted_system_type": [ "standard" diff --git a/powermgr.gni b/powermgr.gni index 88770c94fba742658cc9bb89f6c73aeb5e4f3ad8..07a1c1e13aae2e1e2ec28645df4898fe85cf3e77 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -26,6 +26,7 @@ declare_args() { power_manager_feature_screen_on_timeout_check = false power_manager_feature_doubleclick_or_pickup = true power_manager_feature_force_sleep_broadcast = false + power_manager_feature_charging_type_setting = false } cflags_cc = [] diff --git a/services/BUILD.gn b/services/BUILD.gn index 64fac59a4546155b13c88d7e896e605d02740811..562c193408af874f89ffc7cb5c3e43afbd7ebc6b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -197,6 +197,16 @@ ohos_shared_library("powermgrservice") { defines += [ "POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST" ] } + if (power_manager_feature_charging_type_setting) { + defines += [ "POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING" ] + } + + if (power_manager_feature_charging_type_setting && + defined(global_parts_info) && + defined(global_parts_info.powermgr_battery_manager)) { + external_deps += [ "battery_manager:batterysrv_client" ] + } + if (factory == true) { defines += [ "CONFIG_FACTORY_MODE" ] } diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index a80ab5c84e02df3225fdab02840f996a36c40932..127541c5d57bf94fc781ce3dfb89688c65c0d447 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -42,6 +42,13 @@ namespace OHOS { namespace PowerMgr { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +typedef enum { + POWER_CONNECT_INVALID = -1, + POWER_CONNECT_DC = 0, // DC for Direct Current, means battery supply + POWER_CONNECT_AC = 1, // AC for Alternating Current, means charing supply +} PowerConnectStatus; +#endif class RunningLockMgr; class PowerMgrService final : public SystemAbility, public PowerMgrStub { DECLARE_SYSTEM_ABILITY(PowerMgrService) @@ -137,7 +144,6 @@ public: void QueryRunningLockListsInner(std::map& runningLockLists); static void RegisterSettingWakeupPickupGestureObserver(); static void WakeupPickupGestureSettingUpdateFunc(const std::string& key); - static void RegisterSettingObservers(); static void RegisterSettingWakeupDoubleClickObservers(); static void WakeupDoubleClickSettingUpdateFunc(const std::string& key); static bool GetSettingWakeupDoubleClick(const std::string& key = SETTING_POWER_WAKEUP_DOUBLE_KEY); @@ -152,6 +158,22 @@ public: void Reset(); void KeepScreenOnInit(); void KeepScreenOn(bool isOpenOn); + void UnregisterAllSettingObserver(); + void RegisterAllSettingObserver(); + int64_t GetSettingDisplayOffTime(int64_t defaultTime); +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + PowerConnectStatus GetPowerConnectStatus() const + { + return powerConnectStatus_; + } + void SetPowerConnectStatus(PowerConnectStatus status) + { + powerConnectStatus_ = status; + } + void PowerConnectStatusInit(); + bool IsPowerConnected() const; + void UpdateSettingInvalidDisplayOffTime(); +#endif std::shared_ptr GetRunningLockMgr() const { @@ -324,6 +346,9 @@ private: int32_t monitorId_ {0}; int32_t inputMonitorId_ {-1}; sptr ptoken_; +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + PowerConnectStatus powerConnectStatus_ {PowerConnectStatus::POWER_CONNECT_INVALID}; +#endif void SubscribeCommonEvent(); std::shared_ptr subscriberPtr_; }; @@ -343,6 +368,10 @@ public: : EventFwk::CommonEventSubscriber(subscribeInfo) {} virtual ~PowerCommonEventSubscriber() {} void OnReceiveEvent(const EventFwk::CommonEventData &data) override; +private: +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + void OnPowerConnectStatusChanged(PowerConnectStatus status); +#endif }; } // namespace PowerMgr diff --git a/services/native/include/power_mode_policy.h b/services/native/include/power_mode_policy.h index 66a0415e63511d3b012c5cb861c02e8c37fef9d0..fd46f943dbc922a8c52969713c749f8e8edb09a0 100644 --- a/services/native/include/power_mode_policy.h +++ b/services/native/include/power_mode_policy.h @@ -61,6 +61,8 @@ private: void GetSettingSwitchState(uint32_t& switchId, int32_t& value); // from setting int32_t GetPolicyFromMap(uint32_t type); void SavePowerModeRecoverMap(); + int64_t GetSettingDisplayOffTime(int64_t defaultVal); + std::mutex policyMutex_; std::mutex actionMapMutex_; }; diff --git a/services/native/include/power_state_machine.h b/services/native/include/power_state_machine.h index 43796d628d43a6c9ef365f8009c016f6c420db1f..430290be8542f3abf8f45859bc1bce0eaab1359e 100644 --- a/services/native/include/power_state_machine.h +++ b/services/native/include/power_state_machine.h @@ -126,6 +126,7 @@ public: StateChangeReason GetReasonByWakeType(WakeupDeviceType type); StateChangeReason GetReasionBySuspendType(SuspendDeviceType type); WakeupDeviceType ParseWakeupDeviceType(const std::string& details); + static void DisplayOffTimeUpdateFunc(); // only use for test int64_t GetLastSuspendDeviceTime() const diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 258c8a25039978c3c8693c9a6da1259b8f02fe1f..5900409958d650f15c635442bb16edb4bb4787b7 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -47,6 +47,9 @@ #ifdef HAS_DEVICE_STANDBY_PART #include "standby_service_client.h" #endif +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +#include "battery_srv_client.h" +#endif using namespace OHOS::AppExecFwk; using namespace OHOS::AAFwk; @@ -131,6 +134,10 @@ void PowerMgrService::RegisterBootCompletedCallback() return; } auto powerStateMachine = power->GetPowerStateMachine(); + SettingHelper::UpdateCurrentUserId(); // update setting user id before get setting values +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + power->PowerConnectStatusInit(); +#endif powerStateMachine->RegisterDisplayOffTimeObserver(); powerStateMachine->InitState(); #ifdef POWER_MANAGER_POWER_DIALOG @@ -139,7 +146,6 @@ void PowerMgrService::RegisterBootCompletedCallback() #endif power->SwitchSubscriberInit(); power->InputMonitorInit(); - SettingHelper::UpdateCurrentUserId(); power->SuspendControllerInit(); power->WakeupControllerInit(); power->SubscribeCommonEvent(); @@ -148,7 +154,7 @@ void PowerMgrService::RegisterBootCompletedCallback() #endif power->VibratorInit(); #ifdef POWER_WAKEUPDOUBLE_OR_PICKUP_ENABLE - power->RegisterSettingObservers(); + power->RegisterSettingWakeupDoubleClickObservers(); power->RegisterSettingWakeupPickupGestureObserver(); #endif #ifndef CONFIG_FACTORY_MODE @@ -227,11 +233,6 @@ void PowerMgrService::KeepScreenOn(bool isOpenOn) } #ifdef POWER_WAKEUPDOUBLE_OR_PICKUP_ENABLE -void PowerMgrService::RegisterSettingObservers() -{ - RegisterSettingWakeupDoubleClickObservers(); -} - void PowerMgrService::RegisterSettingWakeupDoubleClickObservers() { SettingObserver::UpdateFunc updateFunc = [&](const std::string& key) {WakeupDoubleClickSettingUpdateFunc(key); }; @@ -1412,6 +1413,31 @@ void PowerMgrService::WakeupActionControllerInit() } #endif +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +void PowerMgrService::PowerConnectStatusInit() +{ + auto pluggedType = BatterySrvClient::GetInstance().GetPluggedType(); + if (pluggedType == BatteryPluggedType::PLUGGED_TYPE_BUTT) { + POWER_HILOGE(COMP_SVC, "BatterySrvClient GetPluggedType error"); + SetPowerConnectStatus(PowerConnectStatus::POWER_CONNECT_INVALID); + } else if ((pluggedType == BatteryPluggedType::PLUGGED_TYPE_AC) || + (pluggedType == BatteryPluggedType::PLUGGED_TYPE_USB) || + (pluggedType == BatteryPluggedType::PLUGGED_TYPE_WIRELESS)) { + SetPowerConnectStatus(PowerConnectStatus::POWER_CONNECT_AC); + } else { + SetPowerConnectStatus(PowerConnectStatus::POWER_CONNECT_DC); + } +} + +bool PowerMgrService::IsPowerConnected() +{ + if (GetPowerConnectStatus() == PowerConnectStatus::POWER_CONNECT_INVALID) { + PowerConnectStatusInit(); // try to init again if invalid + } + return GetPowerConnectStatus() == PowerConnectStatus::POWER_CONNECT_AC; +} +#endif + void PowerMgrService::VibratorInit() { std::shared_ptr vibrator = PowerVibrator::GetInstance(); @@ -1573,6 +1599,10 @@ void PowerMgrService::SubscribeCommonEvent() using namespace OHOS::EventFwk; MatchingSkills matchingSkills; matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED); +#endif CommonEventSubscribeInfo subscribeInfo(matchingSkills); subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::COMMON); if (!subscriberPtr_) { @@ -1580,30 +1610,143 @@ void PowerMgrService::SubscribeCommonEvent() } bool result = CommonEventManager::SubscribeCommonEvent(subscriberPtr_); if (!result) { - POWER_HILOGE(COMP_SVC, "Subscribe COMMON_EVENT_USER_SWITCHED failed"); + POWER_HILOGE(COMP_SVC, "Subscribe COMMON_EVENT failed"); } } -void PowerCommonEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +void PowerMgrService::UnregisterAllSettingObserver() { - std::string action = data.GetWant().GetAction(); - auto power = DelayedSpSingleton::GetInstance(); - if (power == nullptr) { + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { POWER_HILOGI(COMP_SVC, "get PowerMgrService fail"); return; } - if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + #ifdef POWER_WAKEUPDOUBLE_OR_PICKUP_ENABLE - SettingHelper::UnregisterSettingWakeupDoubleObserver(); - SettingHelper::UnregisterSettingWakeupPickupObserver(); + SettingHelper::UnregisterSettingWakeupDoubleObserver(); + SettingHelper::UnregisterSettingWakeupPickupObserver(); #endif - power->GetWakeupController()->UnregisterSettingsObserver(); - SettingHelper::UpdateCurrentUserId(); + pms->GetWakeupController()->UnregisterSettingsObserver(); +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + pms->GetSuspendController()->UnregisterSettingsObserver(); + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerStateMachine fail"); + return; + } + stateMachine->UnregisterDisplayOffTimeObserver(); +#endif +} + +void PowerMgrService::RegisterAllSettingObserver() +{ + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGI(COMP_SVC, "get PowerMgrService fail"); + return; + } + #ifdef POWER_WAKEUPDOUBLE_OR_PICKUP_ENABLE - power->RegisterSettingObservers(); - power->RegisterSettingWakeupPickupGestureObserver(); + pms->RegisterSettingWakeupDoubleClickObservers(); + pms->RegisterSettingWakeupPickupGestureObserver(); +#endif + pms->WakeupControllerInit(); +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + pms->SuspendControllerInit(); + pms->UpdateSettingInvalidDisplayOffTime(); // update setting value if invalid before register + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerStateMachine fail"); + return; + } + stateMachine->RegisterDisplayOffTimeObserver(); +#endif +} + +int64_t PowerMgrService::GetSettingDisplayOffTime(int64_t defaultTime) +{ + int64_t settingTime = defaultTime; +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(FEATURE_POWER_MODE, "get PowerMgrService fail"); + return settingTime; + } + if (pms->IsPowerConnected()) { + settingTime = SettingHelper::GetSettingDisplayAcScreenOffTime(defaultTime); + } else { + settingTime = SettingHelper::GetSettingDisplayDcScreenOffTime(defaultTime); + } +#else + settingTime = SettingHelper::GetSettingDisplayOffTime(defaultTime); +#endif + return settingTime; +} + +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +void PowerMgrService::UpdateSettingInvalidDisplayOffTime() +{ + if (SettingHelper::IsSettingDisplayAcScreenOffTimeValid() && + SettingHelper::IsSettingDisplayDcScreenOffTimeValid()) { + return; + } + + auto power = DelayedSpSingleton::GetInstance(); + if (power == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerMgrService fail"); + return; + } + auto stateMachine = power->GetPowerStateMachine(); + if (stateMachine == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerStateMachine fail"); + return; + } + stateMachine->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME, true); +} + +void PowerCommonEventSubscriber::OnPowerConnectStatusChanged(PowerConnectStatus status) +{ + auto power = DelayedSpSingleton::GetInstance(); + if (power == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerMgrService fail"); + return; + } + power->SetPowerConnectStatus(status); + + auto suspendController = power->GetSuspendController(); + if (suspendController == nullptr) { + POWER_HILOGE(COMP_SVC, "get suspendController fail"); + return; + } + suspendController->UpdateSuspendSources(); + + auto stateMachine = power->GetPowerStateMachine(); + if (stateMachine == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerStateMachine fail"); + return; + } + stateMachine->DisplayOffTimeUpdateFunc(); +} +#endif + +void PowerCommonEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGI(COMP_SVC, "get PowerMgrService fail"); + return; + } + if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + pms->UnregisterAllSettingObserver(); // unregister old user observer + SettingHelper::UpdateCurrentUserId(); // update user Id + pms->RegisterAllSettingObserver(); // register new user observer +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) { + OnPowerConnectStatusChanged(PowerConnectStatus::POWER_CONNECT_AC); + } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) { + OnPowerConnectStatusChanged(PowerConnectStatus::POWER_CONNECT_DC); #endif - power->WakeupControllerInit(); } } } // namespace PowerMgr diff --git a/services/native/src/power_mode_module.cpp b/services/native/src/power_mode_module.cpp index b84ca38e1035cb721c0e709a67d7046b1fdaab57..833f676d807f139bb57fd7ece7f32e7a411b0782 100644 --- a/services/native/src/power_mode_module.cpp +++ b/services/native/src/power_mode_module.cpp @@ -393,7 +393,7 @@ void PowerModeModule::SetDisplayOffTime(bool isBoot) return; } auto pms = DelayedSpSingleton::GetInstance(); - POWER_HILOGD(FEATURE_POWER_MODE, "Set display off timeout: %{public}d", time); + POWER_HILOGI(FEATURE_POWER_MODE, "Set default display off timeout: %{public}d", time); bool needUpdateSetting = time > 0; pms->GetPowerStateMachine()->SetDisplayOffTime(static_cast(time), needUpdateSetting); } diff --git a/services/native/src/power_mode_policy.cpp b/services/native/src/power_mode_policy.cpp index 773535cd7d82812b07f18f20a746570a818d5eb5..469871e99fee7fa5edff3d5b6e04b7cea9ea9ac7 100644 --- a/services/native/src/power_mode_policy.cpp +++ b/services/native/src/power_mode_policy.cpp @@ -14,7 +14,7 @@ */ #include "power_mode_policy.h" - +#include "power_mgr_service.h" #include "power_log.h" #include "power_save_mode.h" #include "singleton.h" @@ -105,6 +105,16 @@ void PowerModePolicy::ReadPowerModePolicy(uint32_t mode) } } +int64_t PowerModePolicy::GetSettingDisplayOffTime(int64_t defaultVal) +{ + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(FEATURE_POWER_MODE, "get PowerMgrService fail"); + return defaultVal; + } + return pms->GetSettingDisplayOffTime(defaultVal); +} + void PowerModePolicy::GetSettingSwitchState(uint32_t& switchId, int32_t& value) { int32_t defaultVal = INIT_VALUE_FALSE; @@ -122,8 +132,7 @@ void PowerModePolicy::GetSettingSwitchState(uint32_t& switchId, int32_t& value) defaultVal = SettingHelper::GetSettingIntellVoice(defaultVal); break; case PowerModePolicy::ServiceType::DISPLAY_OFFTIME: { - int64_t displayOfftime = INIT_VALUE_FALSE; - displayOfftime = SettingHelper::GetSettingDisplayOffTime(INIT_VALUE_FALSE); + int64_t displayOfftime = GetSettingDisplayOffTime(static_cast(defaultVal)); defaultVal = static_cast(displayOfftime); break; } diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index 97845de42f087106421488e2c025edd3b69fa5cd..4a9529483e6909fc9e81a8806124194c77eb80d2 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -36,7 +36,12 @@ namespace OHOS { namespace PowerMgr { namespace { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +sptr g_displayOffTimeAcObserver; +sptr g_displayOffTimeDcObserver; +#else sptr g_displayOffTimeObserver; +#endif static int64_t g_beforeOverrideTime {-1}; constexpr int32_t DISPLAY_OFF = 0; constexpr int32_t DISPLAY_ON = 2; @@ -1279,47 +1284,90 @@ void PowerStateMachine::SetDisplayOffTime(int64_t time, bool needUpdateSetting) } } if (needUpdateSetting) { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + SettingHelper::SetSettingDisplayAcScreenOffTime(displayOffTime_); + SettingHelper::SetSettingDisplayDcScreenOffTime(displayOffTime_); +#else SettingHelper::SetSettingDisplayOffTime(displayOffTime_); +#endif } } -static void DisplayOffTimeUpdateFunc() +void PowerStateMachine::DisplayOffTimeUpdateFunc() { - auto stateMachine = DelayedSpSingleton::GetInstance()->GetPowerStateMachine(); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(FEATURE_POWER_STATE, "get PowerMgrService fail"); + return; + } + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + POWER_HILOGE(FEATURE_POWER_STATE, "get PowerStateMachine fail"); + return; + } + int64_t systemTime = stateMachine->GetDisplayOffTime(); - auto settingTime = SettingHelper::GetSettingDisplayOffTime(systemTime); + int64_t settingTime = pms->GetSettingDisplayOffTime(systemTime); if (settingTime == systemTime) { return; } - POWER_HILOGD(FEATURE_POWER_STATE, "setting update display off time %{public}" PRId64 " -> %{public}" PRId64 "", + POWER_HILOGI(FEATURE_POWER_STATE, "setting update display off time %{public}" PRId64 " -> %{public}" PRId64 "", systemTime, settingTime); g_beforeOverrideTime = settingTime; auto policy = DelayedSingleton::GetInstance(); + if (policy == nullptr) { + POWER_HILOGE(FEATURE_POWER_STATE, "get PowerModePolicy fail"); + return; + } policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::DISPLAY_OFFTIME); stateMachine->SetDisplayOffTime(settingTime, false); } void PowerStateMachine::RegisterDisplayOffTimeObserver() { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (g_displayOffTimeAcObserver && g_displayOffTimeDcObserver) { +#else if (g_displayOffTimeObserver) { +#endif POWER_HILOGI(FEATURE_POWER_STATE, "setting display off time observer is already registered"); return; } DisplayOffTimeUpdateFunc(); SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { - DisplayOffTimeUpdateFunc(); + PowerStateMachine::DisplayOffTimeUpdateFunc(); }; +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (g_displayOffTimeAcObserver == nullptr) { + g_displayOffTimeAcObserver = SettingHelper::RegisterSettingDisplayAcScreenOffTimeObserver(updateFunc); + } + if (g_displayOffTimeDcObserver == nullptr) { + g_displayOffTimeDcObserver = SettingHelper::RegisterSettingDisplayDcScreenOffTimeObserver(updateFunc); + } +#else g_displayOffTimeObserver = SettingHelper::RegisterSettingDisplayOffTimeObserver(updateFunc); +#endif } void PowerStateMachine::UnregisterDisplayOffTimeObserver() { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (g_displayOffTimeAcObserver) { + SettingHelper::UnregisterSettingObserver(g_displayOffTimeAcObserver); + g_displayOffTimeAcObserver = nullptr; + } + if (g_displayOffTimeDcObserver) { + SettingHelper::UnregisterSettingObserver(g_displayOffTimeDcObserver); + g_displayOffTimeDcObserver = nullptr; + } +#else if (g_displayOffTimeObserver == nullptr) { POWER_HILOGD(FEATURE_POWER_STATE, "g_displayOffTimeObserver is nullptr, no need to unregister"); return; } SettingHelper::UnregisterSettingObserver(g_displayOffTimeObserver); g_displayOffTimeObserver = nullptr; +#endif } void PowerStateMachine::SetSleepTime(int64_t time) diff --git a/services/native/src/screenoffpre/screen_off_pre_controller.cpp b/services/native/src/screenoffpre/screen_off_pre_controller.cpp index 30bb127f8500e199419dde7a3488b032ada7adc1..6e2ef6c7e506c538e472d3ee58cde5595ca875b4 100644 --- a/services/native/src/screenoffpre/screen_off_pre_controller.cpp +++ b/services/native/src/screenoffpre/screen_off_pre_controller.cpp @@ -54,7 +54,12 @@ void ScreenOffPreController::AddScreenStateCallback(int32_t remainTime, const sp isRegistered_ = true; if (powerStateMachine_ != nullptr) { int64_t systemTime = powerStateMachine_->GetDisplayOffTime(); - auto settingTime = SettingHelper::GetSettingDisplayOffTime(systemTime); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(FEATURE_SCREEN_OFF_PRE, "get PowerMgrService fail"); + return; + } + int64_t settingTime = pms->GetSettingDisplayOffTime(systemTime); int64_t dimTime = powerStateMachine_->GetDimTime(systemTime); POWER_HILOGD(FEATURE_SCREEN_OFF_PRE, "systemTime=%{public}lld,settingTime=%{public}lld,dimTime=%{public}lld", diff --git a/services/native/src/setting/setting_helper.cpp b/services/native/src/setting/setting_helper.cpp index 661ea0e2bef5185dbf98e26c89c79d201fb2f4c5..5fe171e09bb769a85d9d8bdfee544bba249f964d 100644 --- a/services/native/src/setting/setting_helper.cpp +++ b/services/native/src/setting/setting_helper.cpp @@ -37,9 +37,102 @@ void SettingHelper::UpdateCurrentUserId() settingProvider.UpdateCurrentUserId(); } +bool SettingHelper::IsSettingKeyValid(const std::string& key) +{ + return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(key); +} + +int32_t SettingHelper::GetSettingIntValue(const std::string& key, int32_t defaultVal) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + int32_t value = defaultVal; + ErrCode ret = settingProvider.GetIntValue(key, value); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, "get setting key=%{public}s failed, ret=%{public}d", key.c_str(), ret); + } + return value; +} + +void SettingHelper::SetSettingIntValue(const std::string& key, int32_t value) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + ErrCode ret = settingProvider.PutIntValue(key, value); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, + "set setting key=%{public}s value=%{public}d failed, ret=%{public}d", + key.c_str(), value, ret); + } +} + +int64_t SettingHelper::GetSettingLongValue(const std::string& key, int64_t defaultVal) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + int64_t value = defaultVal; + ErrCode ret = settingProvider.GetLongValue(key, value); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, "get setting key=%{public}s failed, ret=%{public}d", key.c_str(), ret); + } + return value; +} + +void SettingHelper::SetSettingLongValue(const std::string& key, int64_t value) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + ErrCode ret = settingProvider.PutLongValue(key, value); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, + "set setting key=%{public}s value=%{public}" PRId64 " failed, ret=%{public}d", + key.c_str(), value, ret); + } +} + +const std::string SettingHelper::GetSettingStringValue(const std::string& key) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + std::string value; + ErrCode ret = settingProvider.GetStringValue(key, value); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, "get setting key=%{public}s failed, ret=%{public}d", key.c_str(), ret); + } + return value; +} + +void SettingHelper::SetSettingJsonStringValue(const std::string& key, const std::string& jsonConfig) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + ErrCode ret = settingProvider.PutStringValue(key, jsonConfig); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, + "set setting key=%{public}s failed, jsonConfig=%{public}s, ret=%{public}d", + key.c_str(), PowerUtils::JsonToSimpleStr(jsonConfig).c_str(), ret); + } +} + +sptr SettingHelper::RegisterSettingKeyObserver( + const std::string& key, SettingObserver::UpdateFunc& func) +{ + SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); + auto settingObserver = settingProvider.CreateObserver(key, func); + ErrCode ret = settingProvider.RegisterObserver(settingObserver); + if (ret != ERR_OK) { + POWER_HILOGW(COMP_UTILS, "register setting key=%{public}s observer failed, ret=%{public}d", + key.c_str(), ret); + return nullptr; + } + return settingObserver; +} + +void SettingHelper::UnregisterSettingObserver(sptr& observer) +{ + if (observer == nullptr) { + return; + } + SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(observer); +} + bool SettingHelper::IsWakeupPickupSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_WAKEUP_PICKUP_KEY); + return IsSettingKeyValid(SETTING_POWER_WAKEUP_PICKUP_KEY); } bool SettingHelper::GetSettingWakeupPickup(const std::string& key) @@ -56,13 +149,8 @@ bool SettingHelper::GetSettingWakeupPickup(const std::string& key) void SettingHelper::SetSettingWakeupPickup(bool enable) { POWER_HILOGI(COMP_UTILS, "SetSettingWakeupPickup switch, enable=%{public}d", enable); - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); int32_t value = enable ? WAKEUP_SOURCE_OPEN : WAKEUP_SOURCE_CLOSE; - ErrCode ret = settingProvider.PutIntValue(SETTING_POWER_WAKEUP_PICKUP_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "set setting power wakeup pickup key failed, enable=%{public}d, ret=%{public}d", - enable, ret); - } + SetSettingIntValue(SETTING_POWER_WAKEUP_PICKUP_KEY, value); } void SettingHelper::RegisterSettingWakeupPickupObserver(SettingObserver::UpdateFunc& func) @@ -71,13 +159,7 @@ void SettingHelper::RegisterSettingWakeupPickupObserver(SettingObserver::UpdateF POWER_HILOGI(COMP_UTILS, "setting wakeup pickup observer is already registered"); return; } - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - pickUpObserver_ = settingProvider.CreateObserver(SETTING_POWER_WAKEUP_PICKUP_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(pickUpObserver_); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "register setting wakeup pickup failed, ret=%{public}d", ret); - pickUpObserver_ = nullptr; - } + pickUpObserver_ = RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_PICKUP_KEY, func); } void SettingHelper::UnregisterSettingWakeupPickupObserver() @@ -86,294 +168,259 @@ void SettingHelper::UnregisterSettingWakeupPickupObserver() POWER_HILOGI(COMP_UTILS, "pickUpObserver_ is nullptr, no need to unregister"); return; } - auto ret = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(pickUpObserver_); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "unregister setting wakeup pickup observer failed, ret=%{public}d", ret); - } + UnregisterSettingObserver(pickUpObserver_); pickUpObserver_ = nullptr; } +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING bool SettingHelper::IsDisplayOffTimeSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_DISPLAY_OFF_TIME_KEY); + return IsSettingKeyValid(SETTING_DISPLAY_AC_OFF_TIME_KEY) && + IsSettingKeyValid(SETTING_DISPLAY_DC_OFF_TIME_KEY); +} + +bool SettingHelper::IsSettingDisplayAcScreenOffTimeValid() +{ + return IsSettingKeyValid(SETTING_DISPLAY_AC_OFF_TIME_KEY); +} + +int64_t SettingHelper::GetSettingDisplayAcScreenOffTime(int64_t defaultVal) +{ + return GetSettingLongValue(SETTING_DISPLAY_AC_OFF_TIME_KEY, defaultVal); +} + +void SettingHelper::SetSettingDisplayAcScreenOffTime(int64_t time) +{ + SetSettingLongValue(SETTING_DISPLAY_AC_OFF_TIME_KEY, time); +} + +sptr SettingHelper::RegisterSettingDisplayAcScreenOffTimeObserver(SettingObserver::UpdateFunc& func) +{ + return RegisterSettingKeyObserver(SETTING_DISPLAY_AC_OFF_TIME_KEY, func); +} + +bool SettingHelper::IsSettingDisplayDcScreenOffTimeValid() +{ + return IsSettingKeyValid(SETTING_DISPLAY_DC_OFF_TIME_KEY); +} + +int64_t SettingHelper::GetSettingDisplayDcScreenOffTime(int64_t defaultVal) +{ + return GetSettingLongValue(SETTING_DISPLAY_DC_OFF_TIME_KEY, defaultVal); +} + +void SettingHelper::SetSettingDisplayDcScreenOffTime(int64_t time) +{ + SetSettingLongValue(SETTING_DISPLAY_DC_OFF_TIME_KEY, time); +} + +sptr SettingHelper::RegisterSettingDisplayDcScreenOffTimeObserver(SettingObserver::UpdateFunc& func) +{ + return RegisterSettingKeyObserver(SETTING_DISPLAY_DC_OFF_TIME_KEY, func); +} +#else +bool SettingHelper::IsDisplayOffTimeSettingValid() +{ + return IsSettingKeyValid(SETTING_DISPLAY_OFF_TIME_KEY); } int64_t SettingHelper::GetSettingDisplayOffTime(int64_t defaultVal) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - int64_t value = defaultVal; - ErrCode ret = settingProvider.GetLongValue(SETTING_DISPLAY_OFF_TIME_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "get setting display off time failed, ret=%{public}d", ret); - } - return value; + return GetSettingLongValue(SETTING_DISPLAY_OFF_TIME_KEY, defaultVal); } void SettingHelper::SetSettingDisplayOffTime(int64_t time) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutLongValue(SETTING_DISPLAY_OFF_TIME_KEY, time); - if (ret != ERR_OK) { - POWER_HILOGW( - COMP_UTILS, "set setting display off time failed, time=%{public}" PRId64 ", ret=%{public}d", time, ret); - } + SetSettingLongValue(SETTING_DISPLAY_OFF_TIME_KEY, time); } sptr SettingHelper::RegisterSettingDisplayOffTimeObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_DISPLAY_OFF_TIME_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "register setting display off time observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_DISPLAY_OFF_TIME_KEY, func); } +#endif bool SettingHelper::IsAutoAdjustBrightnessSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY); + return IsSettingKeyValid(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY); } int32_t SettingHelper::GetSettingAutoAdjustBrightness(int32_t defaultVal) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - int32_t value = defaultVal; - ErrCode ret = settingProvider.GetIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "get setting auto adjust brightness failed, ret=%{public}d", ret); - } - return value; + return GetSettingIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, defaultVal); } void SettingHelper::SetSettingAutoAdjustBrightness(SwitchStatus status) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, static_cast(status)); - if (ret != ERR_OK) { - POWER_HILOGW( - COMP_UTILS, "set setting auto adjust brightness failed, status=%{public}d, ret=%{public}d", status, ret); - } + SetSettingIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, static_cast(status)); } sptr SettingHelper::RegisterSettingAutoAdjustBrightnessObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "register setting auto adjust brightness observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, func); } bool SettingHelper::IsBrightnessSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_BRIGHTNESS_KEY); + return IsSettingKeyValid(SETTING_BRIGHTNESS_KEY); } void SettingHelper::SetSettingBrightness(int32_t brightness) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutIntValue(SETTING_BRIGHTNESS_KEY, brightness); - if (ret != ERR_OK) { - POWER_HILOGW( - COMP_UTILS, "set setting brightness failed, brightness=%{public}d, ret=%{public}d", brightness, ret); - } + SetSettingIntValue(SETTING_BRIGHTNESS_KEY, brightness); } bool SettingHelper::IsVibrationSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_VIBRATION_KEY); + return IsSettingKeyValid(SETTING_VIBRATION_KEY); } int32_t SettingHelper::GetSettingVibration(int32_t defaultVal) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - int32_t value = defaultVal; - ErrCode ret = settingProvider.GetIntValue(SETTING_VIBRATION_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "get setting vibration failed, ret=%{public}d", ret); - } - return value; + return GetSettingIntValue(SETTING_VIBRATION_KEY, defaultVal); } void SettingHelper::SetSettingVibration(SwitchStatus status) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutIntValue(SETTING_VIBRATION_KEY, static_cast(status)); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "set setting vibration failed, status=%{public}d, ret=%{public}d", status, ret); - } + SetSettingIntValue(SETTING_VIBRATION_KEY, static_cast(status)); } sptr SettingHelper::RegisterSettingVibrationObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_VIBRATION_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "register setting vibration observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_VIBRATION_KEY, func); } bool SettingHelper::IsWindowRotationSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_WINDOW_ROTATION_KEY); + return IsSettingKeyValid(SETTING_WINDOW_ROTATION_KEY); } int32_t SettingHelper::GetSettingWindowRotation(int32_t defaultVal) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - int32_t value = defaultVal; - ErrCode ret = settingProvider.GetIntValue(SETTING_WINDOW_ROTATION_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "get setting window rotation failed, ret=%{public}d", ret); - } - return value; + return GetSettingIntValue(SETTING_WINDOW_ROTATION_KEY, defaultVal); } void SettingHelper::SetSettingWindowRotation(SwitchStatus status) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutIntValue(SETTING_WINDOW_ROTATION_KEY, static_cast(status)); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "set setting window rotation failed, status=%{public}d, ret=%{public}d", status, ret); - } + SetSettingIntValue(SETTING_WINDOW_ROTATION_KEY, static_cast(status)); } sptr SettingHelper::RegisterSettingWindowRotationObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_WINDOW_ROTATION_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "register setting window rotation observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_WINDOW_ROTATION_KEY, func); } bool SettingHelper::IsIntellVoiceSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_INTELL_VOICE_KEY); + return IsSettingKeyValid(SETTING_INTELL_VOICE_KEY); } int32_t SettingHelper::GetSettingIntellVoice(int32_t defaultVal) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - int32_t value = defaultVal; - ErrCode ret = settingProvider.GetIntValue(SETTING_INTELL_VOICE_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "get setting intell voice failed, ret=%{public}d", ret); - } - return value; + return GetSettingIntValue(SETTING_INTELL_VOICE_KEY, defaultVal); } void SettingHelper::SetSettingIntellVoice(SwitchStatus status) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutIntValue(SETTING_INTELL_VOICE_KEY, static_cast(status)); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "set setting intell voice failed, status=%{public}d, ret=%{public}d", status, ret); - } + SetSettingIntValue(SETTING_INTELL_VOICE_KEY, static_cast(status)); } sptr SettingHelper::RegisterSettingIntellVoiceObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_INTELL_VOICE_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "register setting intell voice observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_INTELL_VOICE_KEY, func); +} + +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +sptr SettingHelper::RegisterSettingAcSuspendSourcesObserver(SettingObserver::UpdateFunc& func) +{ + return RegisterSettingKeyObserver(SETTING_POWER_AC_SUSPEND_SOURCES_KEY, func); +} + +bool SettingHelper::IsSuspendSourcesSettingValid() +{ + return IsSettingKeyValid(SETTING_POWER_AC_SUSPEND_SOURCES_KEY) && + IsSettingKeyValid(SETTING_POWER_DC_SUSPEND_SOURCES_KEY); +} + +bool SettingHelper::IsSettingAcSuspendSourcesValid() +{ + return IsSettingKeyValid(SETTING_POWER_AC_SUSPEND_SOURCES_KEY); +} + +const std::string SettingHelper::GetSettingAcSuspendSources() +{ + return GetSettingStringValue(SETTING_POWER_AC_SUSPEND_SOURCES_KEY); } +void SettingHelper::SetSettingAcSuspendSources(const std::string& jsonConfig) +{ + SetSettingJsonStringValue(SETTING_POWER_AC_SUSPEND_SOURCES_KEY, jsonConfig); +} + +sptr SettingHelper::RegisterSettingDcSuspendSourcesObserver(SettingObserver::UpdateFunc& func) +{ + return RegisterSettingKeyObserver(SETTING_POWER_DC_SUSPEND_SOURCES_KEY, func); +} + +bool SettingHelper::IsSettingDcSuspendSourcesValid() +{ + return IsSettingKeyValid(SETTING_POWER_DC_SUSPEND_SOURCES_KEY); +} + +const std::string SettingHelper::GetSettingDcSuspendSources() +{ + return GetSettingStringValue(SETTING_POWER_DC_SUSPEND_SOURCES_KEY); +} + +void SettingHelper::SetSettingDcSuspendSources(const std::string& jsonConfig) +{ + SetSettingJsonStringValue(SETTING_POWER_DC_SUSPEND_SOURCES_KEY, jsonConfig); +} +#else sptr SettingHelper::RegisterSettingSuspendSourcesObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_POWER_SUSPEND_SOURCES_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "register setting brightness observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_POWER_SUSPEND_SOURCES_KEY, func); } bool SettingHelper::IsSuspendSourcesSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_SUSPEND_SOURCES_KEY); + return IsSettingKeyValid(SETTING_POWER_SUSPEND_SOURCES_KEY); } const std::string SettingHelper::GetSettingSuspendSources() { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - std::string value; - ErrCode ret = settingProvider.GetStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "get setting power suspend sources key failed, ret=%{public}d", ret); - } - return value; + return GetSettingStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY); } void SettingHelper::SetSettingSuspendSources(const std::string& jsonConfig) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY, jsonConfig); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "set setting power suspend sources key failed, jsonConfig=%{public}s ret=%{public}d", - PowerUtils::JsonToSimpleStr(jsonConfig).c_str(), ret); - } + SetSettingJsonStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY, jsonConfig); } +#endif sptr SettingHelper::RegisterSettingWakeupSourcesObserver(SettingObserver::UpdateFunc& func) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - auto settingObserver = settingProvider.CreateObserver(SETTING_POWER_WAKEUP_SOURCES_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(settingObserver); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "register setting brightness observer failed, ret=%{public}d", ret); - return nullptr; - } - return settingObserver; + return RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_SOURCES_KEY, func); } bool SettingHelper::IsWakeupSourcesSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_WAKEUP_SOURCES_KEY); + return IsSettingKeyValid(SETTING_POWER_WAKEUP_SOURCES_KEY); } const std::string SettingHelper::GetSettingWakeupSources() { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - std::string value; - ErrCode ret = settingProvider.GetStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "get setting power Wakeup sources key failed, ret=%{public}d", ret); - } - return value; + return GetSettingStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY); } void SettingHelper::SetSettingWakeupSources(const std::string& jsonConfig) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, jsonConfig); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "set setting power Wakeup sources key failed, jsonConfig=%{public}s ret=%{public}d", - PowerUtils::JsonToSimpleStr(jsonConfig).c_str(), ret); - } + SetSettingJsonStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, jsonConfig); } bool SettingHelper::IsWakeupDoubleSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_WAKEUP_DOUBLE_KEY); + return IsSettingKeyValid(SETTING_POWER_WAKEUP_DOUBLE_KEY); } bool SettingHelper::GetSettingWakeupDouble(const std::string& key) @@ -390,13 +437,8 @@ bool SettingHelper::GetSettingWakeupDouble(const std::string& key) void SettingHelper::SetSettingWakeupDouble(bool enable) { POWER_HILOGI(COMP_UTILS, "SetSettingWakeupDouble switch, enable=%{public}d", enable); - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); int32_t value = enable ? WAKEUP_SOURCE_OPEN : WAKEUP_SOURCE_CLOSE; - ErrCode ret = settingProvider.PutIntValue(SETTING_POWER_WAKEUP_DOUBLE_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "set setting power Wakeup double key failed, enable=%{public}d, ret=%{public}d", - enable, ret); - } + SetSettingIntValue(SETTING_POWER_WAKEUP_DOUBLE_KEY, value); } void SettingHelper::RegisterSettingWakeupDoubleObserver(SettingObserver::UpdateFunc& func) @@ -405,13 +447,7 @@ void SettingHelper::RegisterSettingWakeupDoubleObserver(SettingObserver::UpdateF POWER_HILOGI(COMP_UTILS, "setting wakeup double click observer is already registered"); return; } - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - doubleClickObserver_ = settingProvider.CreateObserver(SETTING_POWER_WAKEUP_DOUBLE_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(doubleClickObserver_); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "register setting wakeup double click failed, ret=%{public}d", ret); - doubleClickObserver_ = nullptr; - } + doubleClickObserver_ = RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_DOUBLE_KEY, func); } void SettingHelper::UnregisterSettingWakeupDoubleObserver() @@ -420,39 +456,18 @@ void SettingHelper::UnregisterSettingWakeupDoubleObserver() POWER_HILOGI(COMP_UTILS, "doubleClickObserver_ is nullptr, no need to unregister"); return; } - auto ret = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(doubleClickObserver_); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "unregister setting wakeup double click observer failed, ret=%{public}d", ret); - } + UnregisterSettingObserver(doubleClickObserver_); doubleClickObserver_ = nullptr; } -void SettingHelper::UnregisterSettingObserver(sptr& observer) -{ - if (observer == nullptr) { - return; - } - SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(observer); -} - void SettingHelper::SaveCurrentMode(int32_t mode) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutIntValue(SETTING_POWER_MODE_KEY, mode); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "save power mode key failed, mode=%{public}d ret=%{public}d", mode, ret); - } + SetSettingIntValue(SETTING_POWER_MODE_KEY, mode); } int32_t SettingHelper::ReadCurrentMode(int32_t defaultMode) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - int32_t mode = defaultMode; - ErrCode ret = settingProvider.GetIntValue(SETTING_POWER_MODE_KEY, mode); - if (ret != ERR_OK) { - POWER_HILOGW(COMP_UTILS, "read power mode key failed, ret=%{public}d", ret); - } - return mode; + return GetSettingIntValue(SETTING_POWER_MODE_KEY, defaultMode); } void SettingHelper::RegisterSettingPowerModeObserver(SettingObserver::UpdateFunc& func) @@ -461,13 +476,7 @@ void SettingHelper::RegisterSettingPowerModeObserver(SettingObserver::UpdateFunc POWER_HILOGI(COMP_UTILS, "setting power mode observer is already registered"); return; } - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - powerModeObserver_ = settingProvider.CreateObserver(SETTING_POWER_MODE_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(powerModeObserver_); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "register setting power mode observer failed, ret=%{public}d", ret); - powerModeObserver_ = nullptr; - } + powerModeObserver_ = RegisterSettingKeyObserver(SETTING_POWER_MODE_KEY, func); } void SettingHelper::UnRegisterSettingPowerModeObserver() @@ -485,27 +494,17 @@ void SettingHelper::UnRegisterSettingPowerModeObserver() const std::string SettingHelper::ReadPowerModeRecoverMap() { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - std::string value; - ErrCode ret = settingProvider.GetStringValue(SETTING_POWER_MODE_BACKUP_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "get back up power mode policy failed, ret=%{public}d", ret); - } - return value; + return GetSettingStringValue(SETTING_POWER_MODE_BACKUP_KEY); } void SettingHelper::SavePowerModeRecoverMap(const std::string& jsonConfig) { - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - ErrCode ret = settingProvider.PutStringValue(SETTING_POWER_MODE_BACKUP_KEY, jsonConfig); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "save back up power mode policy failed, jsonConfig=%{public}s ret=%{public}d", - jsonConfig.c_str(), ret); - } + SetSettingJsonStringValue(SETTING_POWER_MODE_BACKUP_KEY, jsonConfig); } + bool SettingHelper::IsWakeupLidSettingValid() { - return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_WAKEUP_LID_KEY); + return IsSettingKeyValid(SETTING_POWER_WAKEUP_LID_KEY); } void SettingHelper::RegisterSettingWakeupLidObserver(SettingObserver::UpdateFunc& func) @@ -514,13 +513,7 @@ void SettingHelper::RegisterSettingWakeupLidObserver(SettingObserver::UpdateFunc POWER_HILOGE(COMP_UTILS, "settings.power.wakeup_lid is valid."); return; } - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); - lidObserver_ = settingProvider.CreateObserver(SETTING_POWER_WAKEUP_LID_KEY, func); - ErrCode ret = settingProvider.RegisterObserver(lidObserver_); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "register setting wakeup lid failed, ret=%{public}d", ret); - lidObserver_ = nullptr; - } + lidObserver_ = RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_LID_KEY, func); } void SettingHelper::UnRegisterSettingWakeupLidObserver() @@ -558,13 +551,8 @@ bool SettingHelper::GetSettingWakeupLid(const std::string& key) void SettingHelper::SetSettingWakeupLid(bool enable) { POWER_HILOGI(COMP_UTILS, "SetSettingWakeupLid, enable=%{public}d", enable); - SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID); int32_t value = enable ? WAKEUP_SOURCE_OPEN : WAKEUP_SOURCE_CLOSE; - ErrCode ret = settingProvider.PutIntValue(SETTING_POWER_WAKEUP_LID_KEY, value); - if (ret != ERR_OK) { - POWER_HILOGE(COMP_UTILS, "set setting power wakeup lid key failed, enable=%{public}d, ret=%{public}d", - enable, ret); - } + SetSettingIntValue(SETTING_POWER_WAKEUP_LID_KEY, value); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/setting/setting_helper.h b/services/native/src/setting/setting_helper.h index 3c875b661111d72a848aefe28df76f1a18694b39..790639e6dbc56ca00dcfbe45783182955534d593 100644 --- a/services/native/src/setting/setting_helper.h +++ b/services/native/src/setting/setting_helper.h @@ -31,10 +31,34 @@ public: ENABLE = 1, }; static void UpdateCurrentUserId(); + static void UnregisterSettingObserver(sptr& observer); static bool IsDisplayOffTimeSettingValid(); + static bool IsSuspendSourcesSettingValid(); +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + static bool IsSettingDisplayAcScreenOffTimeValid(); + static int64_t GetSettingDisplayAcScreenOffTime(int64_t defaultVal); + static void SetSettingDisplayAcScreenOffTime(int64_t time); + static sptr RegisterSettingDisplayAcScreenOffTimeObserver(SettingObserver::UpdateFunc& func); + static bool IsSettingDisplayDcScreenOffTimeValid(); + static int64_t GetSettingDisplayDcScreenOffTime(int64_t defaultVal); + static void SetSettingDisplayDcScreenOffTime(int64_t time); + static sptr RegisterSettingDisplayDcScreenOffTimeObserver(SettingObserver::UpdateFunc& func); + static bool IsSettingAcSuspendSourcesValid(); + static const std::string GetSettingAcSuspendSources(); + static void SetSettingAcSuspendSources(const std::string& jsonConfig); + static sptr RegisterSettingAcSuspendSourcesObserver(SettingObserver::UpdateFunc& func); + static bool IsSettingDcSuspendSourcesValid(); + static const std::string GetSettingDcSuspendSources(); + static void SetSettingDcSuspendSources(const std::string& jsonConfig); + static sptr RegisterSettingDcSuspendSourcesObserver(SettingObserver::UpdateFunc& func); +#else static int64_t GetSettingDisplayOffTime(int64_t defaultVal); static void SetSettingDisplayOffTime(int64_t time); static sptr RegisterSettingDisplayOffTimeObserver(SettingObserver::UpdateFunc& func); + static sptr RegisterSettingSuspendSourcesObserver(SettingObserver::UpdateFunc& func); + static const std::string GetSettingSuspendSources(); + static void SetSettingSuspendSources(const std::string& jsonConfig); +#endif static bool IsAutoAdjustBrightnessSettingValid(); static int32_t GetSettingAutoAdjustBrightness(int32_t defaultVal); static void SetSettingAutoAdjustBrightness(SwitchStatus status); @@ -53,10 +77,6 @@ public: static int32_t GetSettingIntellVoice(int32_t defaultVal); static void SetSettingIntellVoice(SwitchStatus status); static sptr RegisterSettingIntellVoiceObserver(SettingObserver::UpdateFunc& func); - static sptr RegisterSettingSuspendSourcesObserver(SettingObserver::UpdateFunc& func); - static bool IsSuspendSourcesSettingValid(); - static const std::string GetSettingSuspendSources(); - static void SetSettingSuspendSources(const std::string& jsonConfig); static sptr RegisterSettingWakeupSourcesObserver(SettingObserver::UpdateFunc& func); static bool IsWakeupSourcesSettingValid(); static const std::string GetSettingWakeupSources(); @@ -71,7 +91,6 @@ public: static void SetSettingWakeupPickup(bool enable); static void RegisterSettingWakeupPickupObserver(SettingObserver::UpdateFunc& func); static void UnregisterSettingWakeupPickupObserver(); - static void UnregisterSettingObserver(sptr& observer); static void SaveCurrentMode(int32_t mode); static int32_t ReadCurrentMode(int32_t defaultMode); static const std::string ReadPowerModeRecoverMap(); @@ -85,12 +104,30 @@ public: static bool IsWakeupLidSettingValid(); private: + static bool IsSettingKeyValid(const std::string& key); + static int32_t GetSettingIntValue(const std::string& key, int32_t defaultVal); + static void SetSettingIntValue(const std::string& key, int32_t value); + static int64_t GetSettingLongValue(const std::string& key, int64_t defaultVal); + static void SetSettingLongValue(const std::string& key, int64_t value); + static const std::string GetSettingStringValue(const std::string& key); + static void SetSettingJsonStringValue(const std::string& key, const std::string& jsonConfig); + static sptr RegisterSettingKeyObserver(const std::string& key, SettingObserver::UpdateFunc& func); + +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + // AC for Alternating Current, means charing supply + // DC for Direct Current, means battery supply + static constexpr const char* SETTING_DISPLAY_AC_OFF_TIME_KEY {"settings.display.ac.screen_off_timeout"}; + static constexpr const char* SETTING_DISPLAY_DC_OFF_TIME_KEY {"settings.display.dc.screen_off_timeout"}; + static constexpr const char* SETTING_POWER_AC_SUSPEND_SOURCES_KEY {"settings.power.ac.suspend_sources"}; + static constexpr const char* SETTING_POWER_DC_SUSPEND_SOURCES_KEY {"settings.power.dc.suspend_sources"}; +#else static constexpr const char* SETTING_DISPLAY_OFF_TIME_KEY {"settings.display.screen_off_timeout"}; + static constexpr const char* SETTING_POWER_SUSPEND_SOURCES_KEY {"settings.power.suspend_sources"}; +#endif static constexpr const char* SETTING_AUTO_ADJUST_BRIGHTNESS_KEY {"settings.display.auto_screen_brightness"}; static constexpr const char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"}; static constexpr const char* SETTING_VIBRATION_KEY {"physic_navi_haptic_feedback_enabled"}; static constexpr const char* SETTING_WINDOW_ROTATION_KEY {"settings.general.accelerometer_rotation_status"}; - static constexpr const char* SETTING_POWER_SUSPEND_SOURCES_KEY {"settings.power.suspend_sources"}; static constexpr const char* SETTING_POWER_WAKEUP_SOURCES_KEY {"settings.power.wakeup_sources"}; static constexpr const char* SETTING_INTELL_VOICE_KEY {"intell_voice_trigger_enabled"}; static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"}; diff --git a/services/native/src/suspend/suspend_controller.cpp b/services/native/src/suspend/suspend_controller.cpp index f6104c86621ac7858f49e3b893e08965989d94f8..95fa601c56159ee59d0295ee56abd1dbfdde51fd 100644 --- a/services/native/src/suspend/suspend_controller.cpp +++ b/services/native/src/suspend/suspend_controller.cpp @@ -32,7 +32,12 @@ namespace OHOS { namespace PowerMgr { using namespace OHOS::MMI; namespace { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +sptr g_suspendSourcesKeyAcObserver = nullptr; +sptr g_suspendSourcesKeyDcObserver = nullptr; +#else sptr g_suspendSourcesKeyObserver = nullptr; +#endif FFRTMutex g_monitorMutex; const uint32_t SLEEP_DELAY_MS = 5000; constexpr int64_t POWERKEY_MIN_INTERVAL = 350; // ms @@ -50,9 +55,21 @@ SuspendController::SuspendController( SuspendController::~SuspendController() { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (g_suspendSourcesKeyAcObserver) { + SettingHelper::UnregisterSettingObserver(g_suspendSourcesKeyAcObserver); + g_suspendSourcesKeyAcObserver = nullptr; + } + if (g_suspendSourcesKeyDcObserver) { + SettingHelper::UnregisterSettingObserver(g_suspendSourcesKeyDcObserver); + g_suspendSourcesKeyDcObserver = nullptr; + } +#else if (g_suspendSourcesKeyObserver) { SettingHelper::UnregisterSettingObserver(g_suspendSourcesKeyObserver); + g_suspendSourcesKeyObserver = nullptr; } +#endif ffrtTimer_.reset(); } @@ -179,40 +196,73 @@ void SuspendController::ExecSuspendMonitorByReason(SuspendDeviceType reason) }); } +void SuspendController::UpdateSuspendSources() +{ + POWER_HILOGI(COMP_SVC, "start setting string update"); + std::lock_guard lock(mutex_); + + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(COMP_SVC, "get PowerMgrService fail"); + return; + } + std::string jsonStr; +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (pms->IsPowerConnected()) { + jsonStr = SettingHelper::GetSettingAcSuspendSources(); + } else { + jsonStr = SettingHelper::GetSettingDcSuspendSources(); + } +#else + jsonStr = SettingHelper::GetSettingSuspendSources(); +#endif + std::shared_ptr sources = SuspendSourceParser::ParseSources(jsonStr); + std::vector updateSourceList = sources->GetSourceList(); + if (updateSourceList.size() == 0) { + return; + } + sourceList_ = updateSourceList; + POWER_HILOGI(COMP_SVC, "start updateListener"); + Cancel(); + uint32_t id = 0; + for (auto source = sourceList_.begin(); source != sourceList_.end(); source++, id++) { + std::shared_ptr monitor = SuspendMonitor::CreateMonitor(*source); + POWER_HILOGI(FEATURE_SUSPEND, "UpdateFunc CreateMonitor[%{public}u] reason=%{public}d", + id, source->GetReason()); + if (monitor != nullptr && monitor->Init()) { + monitor->RegisterListener([this](SuspendDeviceType reason, uint32_t action, uint32_t delay) { + this->ControlListener(reason, action, delay); + }); + g_monitorMutex.lock(); + monitorMap_.emplace(monitor->GetReason(), monitor); + g_monitorMutex.unlock(); + } + } +} + void SuspendController::RegisterSettingsObserver() { +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (g_suspendSourcesKeyAcObserver && g_suspendSourcesKeyDcObserver) { +#else if (g_suspendSourcesKeyObserver) { +#endif POWER_HILOGE(FEATURE_POWER_STATE, "suspend sources key observer is already registered"); return; } SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { - POWER_HILOGI(COMP_SVC, "start setting string update"); - std::lock_guard lock(mutex_); - std::string jsonStr = SettingHelper::GetSettingSuspendSources(); - std::shared_ptr sources = SuspendSourceParser::ParseSources(jsonStr); - std::vector updateSourceList = sources->GetSourceList(); - if (updateSourceList.size() == 0) { - return; - } - sourceList_ = updateSourceList; - POWER_HILOGI(COMP_SVC, "start updateListener"); - Cancel(); - uint32_t id = 0; - for (auto source = sourceList_.begin(); source != sourceList_.end(); source++, id++) { - std::shared_ptr monitor = SuspendMonitor::CreateMonitor(*source); - POWER_HILOGI(FEATURE_SUSPEND, "UpdateFunc CreateMonitor[%{public}u] reason=%{public}d", - id, source->GetReason()); - if (monitor != nullptr && monitor->Init()) { - monitor->RegisterListener([this](SuspendDeviceType reason, uint32_t action, uint32_t delay) { - this->ControlListener(reason, action, delay); - }); - g_monitorMutex.lock(); - monitorMap_.emplace(monitor->GetReason(), monitor); - g_monitorMutex.unlock(); - } - } + SuspendController::UpdateSuspendSources(); }; +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + if (g_suspendSourcesKeyAcObserver == nullptr) { + g_suspendSourcesKeyAcObserver = SettingHelper::RegisterSettingAcSuspendSourcesObserver(updateFunc); + } + if (g_suspendSourcesKeyDcObserver == nullptr) { + g_suspendSourcesKeyDcObserver = SettingHelper::RegisterSettingDcSuspendSourcesObserver(updateFunc); + } +#else g_suspendSourcesKeyObserver = SettingHelper::RegisterSettingSuspendSourcesObserver(updateFunc); +#endif POWER_HILOGI(FEATURE_POWER_STATE, "register setting observer fin"); } @@ -653,6 +703,5 @@ bool SwitchSuspendMonitor::Init() } void SwitchSuspendMonitor::Cancel() {} - } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/suspend/suspend_controller.h b/services/native/src/suspend/suspend_controller.h index fc800e8910729534b5ed09b869ab2b7f5a495023..0b69b4c7bdca612cbfd66da00f6cf675110f9ee6 100644 --- a/services/native/src/suspend/suspend_controller.h +++ b/services/native/src/suspend/suspend_controller.h @@ -58,6 +58,7 @@ public: void AddCallback(const sptr& callback, SleepPriority priority); void RemoveCallback(const sptr& callback); void TriggerSyncSleepCallback(bool isWakeup); + void UpdateSuspendSources(); std::shared_ptr GetStateMachine() const { diff --git a/services/native/src/suspend/suspend_source_parser.cpp b/services/native/src/suspend/suspend_source_parser.cpp index 34ad21bd66fe33a4597bf7adbcfa297c849d8ee7..a76fb458d4f5dfb13a2fb89e8b529213fcce6d52 100644 --- a/services/native/src/suspend/suspend_source_parser.cpp +++ b/services/native/src/suspend/suspend_source_parser.cpp @@ -23,6 +23,9 @@ #include "setting_helper.h" #include "json/reader.h" #include "json/value.h" +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING +#include "power_mgr_service.h" +#endif namespace OHOS { namespace PowerMgr { @@ -34,9 +37,53 @@ static const std::string SYSTEM_POWER_SUSPEND_CONFIG_FILE = "/system/etc/power_c static const uint32_t ILLEGAL_ACTION = static_cast(SuspendAction::ACTION_INVALID); } // namespace +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING std::shared_ptr SuspendSourceParser::ParseSources() { - std::shared_ptr parseSources; + std::shared_ptr parseSources{nullptr}; + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(FEATURE_SUSPEND, "get PowerMgrService fail"); + return parseSources; + } + bool isPowerConnected = pms->IsPowerConnected(); + bool isSettingAcValid = SettingHelper::IsSettingAcSuspendSourcesValid(); + bool isSettingDcValid = SettingHelper::IsSettingDcSuspendSourcesValid(); + std::string configJsonStr; + + if (!isSettingAcValid || !isSettingDcValid) { + std::string targetPath; + bool ret = GetTargetPath(targetPath); + if (ret == false) { + POWER_HILOGE(FEATURE_SUSPEND, "GetTargetPath fail"); + return parseSources; + } + POWER_HILOGI(FEATURE_SUSPEND, "use targetPath=%{public}s", targetPath.c_str()); + std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary); + std::string fileStringStr(std::istreambuf_iterator {inputStream}, std::istreambuf_iterator {}); + + if (!isSettingAcValid) { + SettingHelper::SetSettingAcSuspendSources(fileStringStr); + } + if (!isSettingDcValid) { + SettingHelper::SetSettingDcSuspendSources(fileStringStr); + } + configJsonStr = fileStringStr; + } + + if (isPowerConnected && isSettingAcValid) { + configJsonStr = SettingHelper::GetSettingAcSuspendSources(); + } else if (!isPowerConnected && isSettingDcValid) { + configJsonStr = SettingHelper::GetSettingDcSuspendSources(); + } + + parseSources = ParseSources(configJsonStr); + return parseSources; +} +#else +std::shared_ptr SuspendSourceParser::ParseSources() +{ + std::shared_ptr parseSources{nullptr}; bool isSettingUpdated = SettingHelper::IsSuspendSourcesSettingValid(); POWER_HILOGI(FEATURE_SUSPEND, "ParseSources setting=%{public}d", isSettingUpdated); std::string configJsonStr; @@ -59,6 +106,7 @@ std::shared_ptr SuspendSourceParser::ParseSources() } return parseSources; } +#endif bool SuspendSourceParser::GetTargetPath(std::string& targetPath) { @@ -132,14 +180,11 @@ bool SuspendSourceParser::ParseSourcesProc( } } - POWER_HILOGI(FEATURE_SUSPEND, "key=%{public}s, type=%{public}u, action=%{public}u, delayMs=%{public}u", + POWER_HILOGI(FEATURE_SUSPEND, + "ParseSourcesProc key=%{public}s, type=%{public}u, action=%{public}u, delayMs=%{public}u", key.c_str(), suspendDeviceType, action, delayMs); - - if (action != 0) { - SuspendSource suspendSource = SuspendSource(suspendDeviceType, action, delayMs); - parseSources->PutSource(suspendSource); - } - + SuspendSource suspendSource = SuspendSource(suspendDeviceType, action, delayMs); + parseSources->PutSource(suspendSource); return true; } diff --git a/utils/setting/include/setting_provider.h b/utils/setting/include/setting_provider.h index c697000ffcb0330bbbbea01210f175c5a94846c1..f511c8c6c2719475aa93658cb85cc7ab467d5103 100644 --- a/utils/setting/include/setting_provider.h +++ b/utils/setting/include/setting_provider.h @@ -45,12 +45,21 @@ protected: ~SettingProvider() override; private: +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + // AC for Alternating Current, means charing supply + // DC for Direct Current, means battery supply + static constexpr const char* SETTING_DISPLAY_AC_OFF_TIME_KEY {"settings.display.ac.screen_off_timeout"}; + static constexpr const char* SETTING_DISPLAY_DC_OFF_TIME_KEY {"settings.display.dc.screen_off_timeout"}; + static constexpr const char* SETTING_POWER_AC_SUSPEND_SOURCES_KEY {"settings.power.ac.suspend_sources"}; + static constexpr const char* SETTING_POWER_DC_SUSPEND_SOURCES_KEY {"settings.power.dc.suspend_sources"}; +#else static constexpr const char* SETTING_DISPLAY_OFF_TIME_KEY {"settings.display.screen_off_timeout"}; + static constexpr const char* SETTING_POWER_SUSPEND_SOURCES_KEY {"settings.power.suspend_sources"}; +#endif static constexpr const char* SETTING_AUTO_ADJUST_BRIGHTNESS_KEY {"settings.display.auto_screen_brightness"}; static constexpr const char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"}; static constexpr const char* SETTING_VIBRATION_KEY {"physic_navi_haptic_feedback_enabled"}; static constexpr const char* SETTING_WINDOW_ROTATION_KEY {"settings.general.accelerometer_rotation_status"}; - static constexpr const char* SETTING_POWER_SUSPEND_SOURCES_KEY {"settings.power.suspend_sources"}; static constexpr const char* SETTING_POWER_WAKEUP_SOURCES_KEY {"settings.power.wakeup_sources"}; static constexpr const char* SETTING_INTELL_VOICE_KEY {"intell_voice_trigger_enabled"}; static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"}; diff --git a/utils/setting/src/setting_provider.cpp b/utils/setting/src/setting_provider.cpp index abc74e77072caa43ad08cb8b5f321fd3f36d58b9..368f591562f900c2fa0d3f4334b353d869ea10bb 100644 --- a/utils/setting/src/setting_provider.cpp +++ b/utils/setting/src/setting_provider.cpp @@ -298,8 +298,22 @@ Uri SettingProvider::AssembleUri(const std::string& key) bool SettingProvider::IsNeedMultiUser(const std::string& key) { - return key == SETTING_POWER_WAKEUP_DOUBLE_KEY || key == SETTING_POWER_WAKEUP_PICKUP_KEY || - key == SETTING_POWER_WAKEUP_SOURCES_KEY; + std::vector needMultiUserStrVec { + SETTING_POWER_WAKEUP_DOUBLE_KEY, + SETTING_POWER_WAKEUP_PICKUP_KEY, + SETTING_POWER_WAKEUP_SOURCES_KEY, +#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING + SETTING_DISPLAY_AC_OFF_TIME_KEY, + SETTING_DISPLAY_DC_OFF_TIME_KEY, + SETTING_POWER_AC_SUSPEND_SOURCES_KEY, + SETTING_POWER_DC_SUSPEND_SOURCES_KEY, +#endif + }; + + if (std::count(needMultiUserStrVec.begin(), needMultiUserStrVec.end(), key)) { + return true; + } + return false; } std::string SettingProvider::ReplaceUserIdForUri(int32_t userId)