From 6c7af5c5b922f35b253ac369f170f202b3e26f39 Mon Sep 17 00:00:00 2001 From: wangyantian Date: Tue, 6 Aug 2024 14:03:04 +0800 Subject: [PATCH] feat: Publish sleep broadcast cherry pick a7eb3ff from https://gitee.com/wangyantian/powermgr_power_manager/pulls/1182 Signed-off-by: wangyantian --- bundle.json | 3 +- powermgr.gni | 1 + services/BUILD.gn | 4 +++ services/native/include/power_mgr_notify.h | 8 +++++ services/native/src/power_mgr_notify.cpp | 25 +++++++++++++ services/native/src/power_state_machine.cpp | 36 +++++++++++++++---- .../native/src/suspend/suspend_controller.cpp | 10 ++++++ .../native/src/suspend/suspend_controller.h | 14 ++++++++ 8 files changed, 94 insertions(+), 7 deletions(-) diff --git a/bundle.json b/bundle.json index fb2c8c44..c542fd41 100644 --- a/bundle.json +++ b/bundle.json @@ -29,7 +29,8 @@ "power_manager_feature_wakeup_action", "power_manager_feature_power_dialog", "power_manager_feature_enable_s4", - "power_manager_feature_doubleclick_or_pickup" + "power_manager_feature_doubleclick_or_pickup", + "power_manager_feature_force_sleep_broadcast" ], "adapted_system_type": [ "standard" diff --git a/powermgr.gni b/powermgr.gni index c740a5cb..88770c94 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -25,6 +25,7 @@ declare_args() { power_manager_feature_enable_s4 = false power_manager_feature_screen_on_timeout_check = false power_manager_feature_doubleclick_or_pickup = true + power_manager_feature_force_sleep_broadcast = false } cflags_cc = [] diff --git a/services/BUILD.gn b/services/BUILD.gn index 98c4899c..35ffe01d 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -183,6 +183,10 @@ ohos_shared_library("powermgrservice") { ] } + if (power_manager_feature_force_sleep_broadcast) { + defines += [ "POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST" ] + } + if (factory == true) { defines += [ "CONFIG_FACTORY_MODE" ] } diff --git a/services/native/include/power_mgr_notify.h b/services/native/include/power_mgr_notify.h index 3caa601d..db465621 100644 --- a/services/native/include/power_mgr_notify.h +++ b/services/native/include/power_mgr_notify.h @@ -30,6 +30,10 @@ public: void RegisterPublishEvents(); void PublishScreenOffEvents(int64_t eventTime); void PublishScreenOnEvents(int64_t eventTime); +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + void PublishEnterForceSleepEvents(int64_t eventTime); + void PublishExitForceSleepEvents(int64_t eventTime); +#endif private: using IntentWant = OHOS::AAFwk::Want; @@ -38,6 +42,10 @@ private: sptr screenOffWant_ {nullptr}; sptr screenOnWant_ {nullptr}; +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + sptr enterForceSleepWant_ {nullptr}; + sptr exitForceSleepWant_ {nullptr}; +#endif sptr publishInfo_ {nullptr}; }; } // namespace PowerMgr diff --git a/services/native/src/power_mgr_notify.cpp b/services/native/src/power_mgr_notify.cpp index 3599c4cb..b9ba2207 100644 --- a/services/native/src/power_mgr_notify.cpp +++ b/services/native/src/power_mgr_notify.cpp @@ -37,6 +37,12 @@ void PowerMgrNotify::RegisterPublishEvents() screenOffWant_->SetAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF); screenOnWant_ = new (std::nothrow)IntentWant(); screenOnWant_->SetAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON); +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + enterForceSleepWant_ = new (std::nothrow)IntentWant(); + enterForceSleepWant_->SetAction(CommonEventSupport::COMMON_EVENT_ENTER_FORCE_SLEEP); + exitForceSleepWant_ = new (std::nothrow)IntentWant(); + exitForceSleepWant_->SetAction(CommonEventSupport::COMMON_EVENT_EXIT_FORCE_SLEEP); +#endif } void PowerMgrNotify::PublishEvents(int64_t eventTime, sptr want) @@ -64,5 +70,24 @@ void PowerMgrNotify::PublishScreenOnEvents(int64_t eventTime) PublishEvents(eventTime, screenOnWant_); POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", screenOnWant_->GetAction().c_str()); } + +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST +void PowerMgrNotify::PublishEnterForceSleepEvents(int64_t eventTime) +{ + POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld", + enterForceSleepWant_->GetAction().c_str(), static_cast(eventTime)); + PublishEvents(eventTime, enterForceSleepWant_); + POWER_HILOGI( + FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", enterForceSleepWant_->GetAction().c_str()); +} + +void PowerMgrNotify::PublishExitForceSleepEvents(int64_t eventTime) +{ + POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld", + exitForceSleepWant_->GetAction().c_str(), static_cast(eventTime)); + PublishEvents(eventTime, exitForceSleepWant_); + POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", exitForceSleepWant_->GetAction().c_str()); +} +#endif } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index 26aba4d6..e0496aae 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -849,12 +849,36 @@ void PowerStateMachine::SendEventToPowerMgrNotify(PowerState state, int64_t call POWER_HILOGE(FEATURE_POWER_STATE, "Notify is null"); return; } - if (state == PowerState::AWAKE) { - notify->PublishScreenOnEvents(callTime); - } else if (state == PowerState::INACTIVE) { - notify->PublishScreenOffEvents(callTime); - } else { - POWER_HILOGI(FEATURE_POWER_STATE, "No need to publish event, state:%{public}u", state); + + switch (state) { + case PowerState::AWAKE: { + notify->PublishScreenOnEvents(callTime); +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + auto suspendController = pms->GetSuspendController(); + if (suspendController != nullptr && suspendController->GetForceSleepingFlag()) { + POWER_HILOGI(FEATURE_POWER_STATE, "Set flag of force sleeping to false"); + notify->PublishExitForceSleepEvents(callTime); + suspendController->SetForceSleepingFlag(false); + } +#endif + break; + } + case PowerState::INACTIVE: { + notify->PublishScreenOffEvents(callTime); + break; + } + case PowerState::SLEEP: { +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + auto suspendController = pms->GetSuspendController(); + if (suspendController != nullptr && suspendController->GetForceSleepingFlag()) { + notify->PublishEnterForceSleepEvents(callTime); + } + break; +#endif + break; + } + default: + POWER_HILOGI(FEATURE_POWER_STATE, "No need to publish event, state:%{public}u", state); } } diff --git a/services/native/src/suspend/suspend_controller.cpp b/services/native/src/suspend/suspend_controller.cpp index 9222f055..be2037f6 100644 --- a/services/native/src/suspend/suspend_controller.cpp +++ b/services/native/src/suspend/suspend_controller.cpp @@ -487,6 +487,16 @@ void SuspendController::HandleForceSleep(SuspendDeviceType reason) POWER_HILOGE(FEATURE_SUSPEND, "Can't get PowerStateMachine"); return; } + +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + auto pms = DelayedSpSingleton::GetInstance(); + if (pms != nullptr && pms->GetSuspendController() != nullptr) { + pms->GetSuspendController()->SetForceSleepingFlag(true); + POWER_HILOGI(FEATURE_SUSPEND, "Set flag of force sleeping to true"); + } else { + POWER_HILOGE(FEATURE_SUSPEND, "Failed to set flag of force sleeping, pms or suspendController is nullptr"); + } +#endif bool ret = stateMachine_->SetState(PowerState::SLEEP, stateMachine_->GetReasionBySuspendType(reason), true); if (ret) { diff --git a/services/native/src/suspend/suspend_controller.h b/services/native/src/suspend/suspend_controller.h index ad744788..7b712062 100644 --- a/services/native/src/suspend/suspend_controller.h +++ b/services/native/src/suspend/suspend_controller.h @@ -75,6 +75,17 @@ public: void StartSleepTimer(SuspendDeviceType reason, uint32_t action, uint32_t delay); void Reset(); +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + void SetForceSleepingFlag(bool isForceSleeping) + { + forceSleeping_.store(isForceSleeping, std::memory_order_relaxed); + } + bool GetForceSleepingFlag() + { + return forceSleeping_.load(); + } +#endif + private: void ControlListener(SuspendDeviceType reason, uint32_t action, uint32_t delay); void HandleAutoSleep(SuspendDeviceType reason); @@ -99,6 +110,9 @@ private: std::mutex mutex_; std::shared_ptr ffrtTimer_; FFRTMutexMap ffrtMutexMap_; +#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST + std::atomic forceSleeping_ {false}; +#endif }; class SuspendMonitor { -- Gitee