From f3390529b6dc1c60fd0a8cc384b5c4c56d26733e Mon Sep 17 00:00:00 2001 From: mamingxiao Date: Sat, 13 Sep 2025 11:10:19 +0800 Subject: [PATCH] mistouchPrevention add ffrt Signed-off-by: mamingxiao --- .../key_command/include/key_command_handler.h | 2 +- .../key_command/src/key_command_handler.cpp | 81 +++++++------------ 2 files changed, 28 insertions(+), 55 deletions(-) diff --git a/service/key_command/include/key_command_handler.h b/service/key_command/include/key_command_handler.h index 1ba74aee63..f0b947a70f 100755 --- a/service/key_command/include/key_command_handler.h +++ b/service/key_command/include/key_command_handler.h @@ -427,7 +427,7 @@ private: int32_t menuPressedTimerId_ { -1 }; std::shared_ptr tmpkeyEvent_ {nullptr}; bool gameForbidFingerKnuckle_ { false }; - bool hasRegisteredSensor_ { false }; + std::atomic hasRegisteredSensor_ { 0 }; uint32_t screenCapturePermission_ { ScreenCapturePermissionType::DEFAULT_PERMISSIONS }; void *mistouchLibHandle_ {nullptr}; IMistouchPrevention *mistouchPrevention_ {nullptr}; diff --git a/service/key_command/src/key_command_handler.cpp b/service/key_command/src/key_command_handler.cpp index 501d5e4cad..31cc36dfc7 100644 --- a/service/key_command/src/key_command_handler.cpp +++ b/service/key_command/src/key_command_handler.cpp @@ -39,6 +39,7 @@ #include "timer_manager.h" #include #include +#include "ffrt.h" #undef MMI_LOG_DOMAIN #define MMI_LOG_DOMAIN MMI_LOG_HANDLER @@ -87,6 +88,9 @@ const char* LOADMISTOUCH_LIBPATH = "libmistouch_prevention.z.so"; constexpr int32_t LIGHT_STAY_AWAY { 0 }; const std::string DEVICE_TYPE_TV = system::GetParameter("const.product.devicetype", "unknown"); const std::string PRODUCT_TYPE_TV = "tv"; +constexpr int32_t NOT_SUBSCRIBED = 0; +constexpr int32_t HAS_SUBSCRIBED = 1; +constexpr int32_t IN_SUBSCRIPTION = 2; } // namespace static void SensorDataCallbackImpl(SensorEvent *event) @@ -3063,35 +3067,6 @@ void KeyCommandHandler::MenuClickProcess(const std::string bundleName, LaunchAbility(ability, NO_DELAY); } -void KeyCommandHandler::RegisterProximitySensor() -{ - CALL_INFO_TRACE; - if (hasRegisteredSensor_) { - MMI_HILOGE("Has SubscribeSensor %{public}d", SENSOR_TYPE_ID_PROXIMITY); - return; - } - if (!KeyEventHdr->IsScreenFold()) { - MMI_HILOGD("Screen not fold"); - return; - } - g_user.callback = SensorDataCallbackImpl; - int32_t ret = SubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &g_user); - if (ret != 0) { - MMI_HILOGE("Failed to SubscribeSensor: %{public}d ret:%{public}d", SENSOR_TYPE_ID_PROXIMITY, ret); - return; - } - ret = SetBatch(SENSOR_TYPE_ID_PROXIMITY, &g_user, SENSOR_SAMPLING_INTERVAL, SENSOR_REPORT_INTERVAL); - if (ret != 0) { - MMI_HILOGE("Failed to SetBatch: %{public}d ret:%{public}d", SENSOR_TYPE_ID_PROXIMITY, ret); - return; - } - ret = ActivateSensor(SENSOR_TYPE_ID_PROXIMITY, &g_user); - if (ret != 0) { - MMI_HILOGE("Failed to ActivateSensor: %{public}d ret:%{public}d", SENSOR_TYPE_ID_PROXIMITY, ret); - return; - } - hasRegisteredSensor_ = true; -} int32_t KeyCommandHandler::SetKnuckleSwitch(bool knuckleSwitch) { @@ -3158,22 +3133,6 @@ int32_t KeyCommandHandler::LaunchAiScreenAbility(int32_t pid) return RET_OK; } -void KeyCommandHandler::UnregisterProximitySensor() -{ - if (!hasRegisteredSensor_) { - MMI_HILOGI("Has registered sensor: %{public}d", SENSOR_TYPE_ID_PROXIMITY); - return; - } - hasRegisteredSensor_ = false; - int32_t ret = DeactivateSensor(SENSOR_TYPE_ID_PROXIMITY, &g_user); - if (ret != 0) { - MMI_HILOGE("Failed to DeactiveSensor: %{public}d ret:%{public}d", SENSOR_TYPE_ID_PROXIMITY, ret); - } - ret = UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &g_user); - if (ret != 0) { - MMI_HILOGE("Failed to UnsubscribeSensor: %{public}d ret:%{public}d", SENSOR_TYPE_ID_PROXIMITY, ret); - } -} int32_t KeyCommandHandler::SwitchScreenCapturePermission(uint32_t permissionType, bool enable) { @@ -3218,7 +3177,7 @@ bool KeyCommandHandler::HasScreenCapturePermission(uint32_t permissionType) #ifdef OHOS_BUILD_ENABLE_MISTOUCH_PREVENTION void KeyCommandHandler::CallMistouchPrevention() { - if (hasRegisteredSensor_) { + if (hasRegisteredSensor_ != NOT_SUBSCRIBED) { MMI_HILOGE("Has SubscribeSensor %{public}d", SENSOR_TYPE_ID_PROXIMITY); return; } @@ -3254,10 +3213,17 @@ void KeyCommandHandler::CallMistouchPrevention() } }; CHKPV(mistouchPrevention_); - int ret = mistouchPrevention_->MistouchPreventionConnector(callback); - hasRegisteredSensor_ = true; - MMI_HILOGD("CallMistouchPrevention yes MistouchPreventionConnector:%{public}d", ret); + if (hasRegisteredSensor_ == NOT_SUBSCRIBED) { + ffrt::submit([this, callback]() { + this->hasRegisteredSensor_ = IN_SUBSCRIPTION; + int ret = mistouchPrevention_->MistouchPreventionConnector(callback); + MMI_HILOGI("CallMistouchPrevention yes MistouchPreventionConnector:%{public}d", ret); + this->hasRegisteredSensor_ = HAS_SUBSCRIBED; + }); + if (timerId_ >= 0) { + TimerMgr->RemoveTimer(timerId_); + } timerId_ = TimerMgr->AddTimer(FREQUENCY, 1, [weakPtr]() { if (auto sharedPtr = weakPtr.lock()) { sharedPtr->UnregisterMistouchPrevention(); @@ -3268,23 +3234,30 @@ void KeyCommandHandler::CallMistouchPrevention() if (timerId_ < 0) { MMI_HILOGE("Add timer failed"); } + MMI_HILOGI("hasRegisteredSensor_ is :%{public}d", hasRegisteredSensor_.load()); } #endif // OHOS_BUILD_ENABLE_MISTOUCH_PREVENTION #ifdef OHOS_BUILD_ENABLE_MISTOUCH_PREVENTION void KeyCommandHandler::UnregisterMistouchPrevention() { - if (!hasRegisteredSensor_) { - MMI_HILOGD("Has unregistered sensor: %{public}d", SENSOR_TYPE_ID_PROXIMITY); + if (hasRegisteredSensor_ != HAS_SUBSCRIBED) { + MMI_HILOGE("Has unregistered sensor: %{public}d", SENSOR_TYPE_ID_PROXIMITY); return; } CHKPV(mistouchPrevention_); - mistouchPrevention_->MistouchPreventionClose(); - hasRegisteredSensor_ = false; + ffrt::submit([this]() { + this->hasRegisteredSensor_ = IN_SUBSCRIPTION; + mistouchPrevention_->MistouchPreventionClose(); + this->hasRegisteredSensor_ = NOT_SUBSCRIBED; + }); + if (hasRegisteredSensor_ != NOT_SUBSCRIBED) { + MMI_HILOGE("UnregisterMistouchPrevention fail"); + } ret_ = -1; MMI_HILOGD("UnregisterMistouchPrevention:ret_ %{public}d", ret_.load()); if (timerId_ >= 0) { - MMI_HILOGD("lzc RemoveTimer:%{public}d", timerId_); + MMI_HILOGD("RemoveTimer:%{public}d", timerId_); TimerMgr->RemoveTimer(timerId_); timerId_ = -1; } -- Gitee