From 62af0d580af4afcfac8f62fc790c493a86a5899a Mon Sep 17 00:00:00 2001 From: cjw123qq Date: Fri, 12 Sep 2025 16:19:40 +0800 Subject: [PATCH] fix: powerkey down_up issue modify Signed-off-by: cjw123qq --- .../native/src/suspend/suspend_controller.cpp | 21 +++-------- .../native/src/suspend/suspend_controller.h | 4 +-- .../native/src/wakeup/wakeup_controller.cpp | 11 ++---- .../native/src/wakeup/wakeup_controller.h | 2 +- .../power_suspend_controller_test.cpp | 35 ------------------- 5 files changed, 9 insertions(+), 64 deletions(-) diff --git a/services/native/src/suspend/suspend_controller.cpp b/services/native/src/suspend/suspend_controller.cpp index 6a276b5d..f0afcfac 100644 --- a/services/native/src/suspend/suspend_controller.cpp +++ b/services/native/src/suspend/suspend_controller.cpp @@ -843,16 +843,9 @@ bool PowerKeySuspendMonitor::Init() POWER_HILOGE(FEATURE_SUSPEND, "PowerKeySuspendMonitorInit inputManager is null"); return false; } - std::weak_ptr weak = weak_from_this(); powerkeyReleaseId_ = inputManager->SubscribeKeyEvent( - keyOption, [weak](std::shared_ptr keyEvent) { - std::shared_ptr strong = weak.lock(); - if (!strong) { - POWER_HILOGE(FEATURE_SUSPEND, "[UL_POWER] PowerKeySuspendMonitor is invaild, return"); - return; - } - - strong->ReceivePowerkeyCallback(keyEvent); + keyOption, [this](std::shared_ptr keyEvent) { + this->ReceivePowerkeyCallback(keyEvent); }); POWER_HILOGI(FEATURE_SUSPEND, "powerkeyReleaseId_=%{public}d", powerkeyReleaseId_); return powerkeyReleaseId_ >= 0 ? true : false; @@ -1016,16 +1009,10 @@ bool TPCoverSuspendMonitor::Init() POWER_HILOGE(FEATURE_SUSPEND, "TPCoverSuspendMonitorInit inputManager is null"); return false; } - std::weak_ptr weak = weak_from_this(); TPCoverReleaseId_ = inputManager->SubscribeKeyEvent( - keyOption, [weak](std::shared_ptr keyEvent) { - std::shared_ptr strong = weak.lock(); - if (!strong) { - POWER_HILOGE(FEATURE_SUSPEND, "[UL_POWER] TPCoverSuspendMonitor is invaild, return"); - return; - } + keyOption, [this](std::shared_ptr keyEvent) { POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Received TPCover event"); - strong->Notify(); + this->Notify(); }); POWER_HILOGI(FEATURE_SUSPEND, "TPCoverReleaseId_=%{public}d", TPCoverReleaseId_); return TPCoverReleaseId_ >= 0 ? true : false; diff --git a/services/native/src/suspend/suspend_controller.h b/services/native/src/suspend/suspend_controller.h index 78dd50ba..3205570c 100644 --- a/services/native/src/suspend/suspend_controller.h +++ b/services/native/src/suspend/suspend_controller.h @@ -198,7 +198,7 @@ protected: SuspendListener listener_; }; -class PowerKeySuspendMonitor : public SuspendMonitor, public std::enable_shared_from_this { +class PowerKeySuspendMonitor : public SuspendMonitor { public: explicit PowerKeySuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} ~PowerKeySuspendMonitor() override = default; @@ -239,7 +239,7 @@ public: void Cancel() override; }; -class TPCoverSuspendMonitor : public SuspendMonitor, public std::enable_shared_from_this { +class TPCoverSuspendMonitor : public SuspendMonitor { public: explicit TPCoverSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} ~TPCoverSuspendMonitor() override = default; diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index 2c802936..a57c670f 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -959,16 +959,9 @@ bool PowerkeyWakeupMonitor::Init() POWER_HILOGE(FEATURE_WAKEUP, "PowerkeyWakeupMonitorInit inputManager is null"); return false; } - std::weak_ptr weak = weak_from_this(); powerkeyShortPressId_ = inputManager->SubscribeKeyEvent( - keyOption, [weak](std::shared_ptr keyEvent) { - std::shared_ptr strong = weak.lock(); - if (!strong) { - POWER_HILOGE(FEATURE_WAKEUP, "[UL_POWER] PowerkeyWakeupMonitor is invaild, return"); - return; - } - - strong->ReceivePowerkeyCallback(keyEvent); + keyOption, [this](std::shared_ptr keyEvent) { + this->ReceivePowerkeyCallback(keyEvent); }); POWER_HILOGI(FEATURE_WAKEUP, "powerkey register powerkeyShortPressId_=%{public}d", powerkeyShortPressId_); diff --git a/services/native/src/wakeup/wakeup_controller.h b/services/native/src/wakeup/wakeup_controller.h index 863fef83..60c5abe3 100644 --- a/services/native/src/wakeup/wakeup_controller.h +++ b/services/native/src/wakeup/wakeup_controller.h @@ -167,7 +167,7 @@ protected: WakeupListener listener_; }; -class PowerkeyWakeupMonitor : public WakeupMonitor, public std::enable_shared_from_this { +class PowerkeyWakeupMonitor : public WakeupMonitor { public: explicit PowerkeyWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} ~PowerkeyWakeupMonitor() override = default; diff --git a/test/unittest/src/interface_test/power_suspend_controller_test.cpp b/test/unittest/src/interface_test/power_suspend_controller_test.cpp index ae192325..164e614f 100644 --- a/test/unittest/src/interface_test/power_suspend_controller_test.cpp +++ b/test/unittest/src/interface_test/power_suspend_controller_test.cpp @@ -417,41 +417,6 @@ HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest016, TestSize.Lev POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest016 function end!"); } -/** - * @tc.name: PowerSuspendControllerTest017 - * @tc.desc: test simulate powerkey event when screenon - * @tc.type: FUNC - */ -HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest017, TestSize.Level0) -{ - POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest017 function start!"); - GTEST_LOG_(INFO) << "PowerSuspendControllerTest017: start"; - - g_service->WakeupControllerInit(); - g_service->SuspendControllerInit(); - g_service->WakeupDevice( - static_cast(time(nullptr)), WakeupDeviceType::WAKEUP_DEVICE_PLUG_CHANGE, "plug change"); - EXPECT_TRUE(g_service->IsScreenOn()); - std::shared_ptr suspendController = g_service->GetSuspendController(); - suspendController->monitorMap_.clear(); - - auto inputManager = MMI::InputManager::GetInstance(); - std::shared_ptr keyEventPowerkeyDown = MMI::KeyEvent::Create(); - keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN); - keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); - std::shared_ptr keyEventPowerkeyUp = MMI::KeyEvent::Create(); - keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP); - keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); - - inputManager->SimulateInputEvent(keyEventPowerkeyDown); - inputManager->SimulateInputEvent(keyEventPowerkeyUp); - sleep(2); - EXPECT_FALSE(g_service->IsScreenOn()); - - GTEST_LOG_(INFO) << "PowerSuspendControllerTest017: end"; - POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest017 function end!"); -} - #ifdef POWER_MANAGER_TAKEOVER_SUSPEND class TestTakeOverSuspendCallback : public ITakeOverSuspendCallback { public: -- Gitee