diff --git a/services/native/src/suspend/suspend_controller.cpp b/services/native/src/suspend/suspend_controller.cpp index 6a276b5d3058df8321336d64b31d23818d814031..f0afcfac120c08ae3d12f5930d31016f8d13ba49 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 78dd50ba67a2578cf91249f6d166f14e84ce0d0e..3205570c704fbc25fd83a6a87eec2ed566ad22de 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 2c80293624fd63c95c0a084a8ed0ac3de1ef51e1..a57c670ffeb6bf755ebe40e014f37f6162e27c69 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 863fef83fdc4365be217d87397ea536bbaf5831a..60c5abe395e2ac34717c34267db5bc6979854ccd 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 ae1923254c66c460e4292502f77f7bebf5d2baf3..164e614f80c75c85563ca898d5b10664ed5c6561 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: