From 05a872e8259e2222cd1df7317059d1eb7cf3d0ca Mon Sep 17 00:00:00 2001 From: yanghang Date: Tue, 2 Sep 2025 15:09:48 +0800 Subject: [PATCH] fix: The proximity sensor function is optimized Signed-off-by: yanghang --- .../src/runninglock/running_lock_mgr.cpp | 17 ++++++++++-- .../native/src/runninglock/running_lock_mgr.h | 1 + test/unittest/src/app_manager_utils_test.cpp | 4 ++- .../unittest/src/running_lock_native_test.cpp | 27 +++++++++++++++++++ .../running_lock_mgr_abnormal_test.cpp | 15 +++++++++++ utils/ability/power_ability.cpp | 4 +-- utils/appmgr/include/app_manager_utils.h | 2 +- utils/appmgr/src/app_manager_utils.cpp | 7 ++--- utils/native/include/power_utils.h | 4 ++- utils/native/src/power_utils.cpp | 19 ++++++++++--- 10 files changed, 87 insertions(+), 13 deletions(-) diff --git a/services/native/src/runninglock/running_lock_mgr.cpp b/services/native/src/runninglock/running_lock_mgr.cpp index 73f6619d..9643aa4b 100644 --- a/services/native/src/runninglock/running_lock_mgr.cpp +++ b/services/native/src/runninglock/running_lock_mgr.cpp @@ -32,6 +32,7 @@ #include "power_utils.h" #include "system_suspend_controller.h" #include "power_hookmgr.h" +#include "parameters.h" using namespace std; @@ -41,7 +42,7 @@ namespace { const string TASK_RUNNINGLOCK_FORCEUNLOCK = "RunningLock_ForceUnLock"; constexpr int32_t VALID_PID_LIMIT = 1; sptr g_runningLockCallback = nullptr; -const string INCALL_APP_BUNDLE_NAME = "com.ohos.callui"; +const string FOREGROUND_APP_LIST = "const.power.prox_dly_off_fg_apps"; #ifdef HAS_SENSORS_SENSOR_PART constexpr uint32_t FOREGROUND_INCALL_DELAY_TIME_MS = 300; constexpr uint32_t BACKGROUND_INCALL_DELAY_TIME_MS = 800; @@ -944,6 +945,18 @@ void RunningLockMgr::ProximityController::OnAway() runningLock->HandleProximityAwayEvent(); } +bool RunningLockMgr::IsVoiceAppForeground() +{ + std::string foregroundApp = OHOS::system::GetParameter(FOREGROUND_APP_LIST, ""); + if (PowerUtils::IsForegroundApplication(PowerUtils::Split(foregroundApp, ';'))) { + POWER_HILOGI(FEATURE_RUNNING_LOCK, "call in foreground"); + return true; + } + + POWER_HILOGI(FEATURE_RUNNING_LOCK, "No voice app is in foreground"); + return false; +} + void RunningLockMgr::HandleProximityCloseEvent() { auto pms = DelayedSpSingleton::GetInstance(); @@ -959,7 +972,7 @@ void RunningLockMgr::HandleProximityCloseEvent() if (GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) { POWER_HILOGI(FEATURE_RUNNING_LOCK, "Change state to INACITVE when holding PROXIMITY LOCK"); uint32_t delayTime = FOREGROUND_INCALL_DELAY_TIME_MS; - if (!PowerUtils::IsForegroundApplication(INCALL_APP_BUNDLE_NAME)) { + if (!IsVoiceAppForeground()) { delayTime = BACKGROUND_INCALL_DELAY_TIME_MS; } if (pms->IsDuringCallStateEnable() && stateMachine->IsDuringCall() && stateMachine->IsScreenOn()) { diff --git a/services/native/src/runninglock/running_lock_mgr.h b/services/native/src/runninglock/running_lock_mgr.h index ef94ff26..ca317018 100644 --- a/services/native/src/runninglock/running_lock_mgr.h +++ b/services/native/src/runninglock/running_lock_mgr.h @@ -90,6 +90,7 @@ private: bool InitProximityController(); void HandleProximityCloseEvent(); void HandleProximityAwayEvent(); + bool IsVoiceAppForeground(); #endif class LockCounter { diff --git a/test/unittest/src/app_manager_utils_test.cpp b/test/unittest/src/app_manager_utils_test.cpp index 5eb6c0d6..2e944930 100644 --- a/test/unittest/src/app_manager_utils_test.cpp +++ b/test/unittest/src/app_manager_utils_test.cpp @@ -46,8 +46,10 @@ HWTEST_F(AppManagerUtilsTest, AppManagerUtilsTest001, TestSize.Level0) HWTEST_F(AppManagerUtilsTest, AppManagerUtilsTest002, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "AppManagerUtilsTest002 function start!"); - std::string unExistApp = "com.example.UNEXISTED_APP"; + std::set unExistApp; bool res = AppManagerUtils::IsForegroundApplication(unExistApp); + unExistApp.insert("com.example.UNEXISTED_APP"); + res = AppManagerUtils::IsForegroundApplication(unExistApp); EXPECT_FALSE(res); POWER_HILOGI(LABEL_TEST, "AppManagerUtilsTest002 function end!"); } diff --git a/test/unittest/src/running_lock_native_test.cpp b/test/unittest/src/running_lock_native_test.cpp index a2699c39..722efe3e 100644 --- a/test/unittest/src/running_lock_native_test.cpp +++ b/test/unittest/src/running_lock_native_test.cpp @@ -16,6 +16,7 @@ #include "running_lock_native_test.h" #include +#include "power_utils.h" #include "actions/irunning_lock_action.h" @@ -44,6 +45,11 @@ void PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message) POWER_HILOGI(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage, %{public}s", message.c_str()); } + +bool PowerUtils::IsForegroundApplication(const std::set& appNames) +{ + return true; +} namespace { /** * @tc.name: RunningLockNative001 @@ -732,4 +738,25 @@ HWTEST_F (RunningLockNativeTest, RunningLockNative023, TestSize.Level1) POWER_HILOGI(LABEL_TEST, "RunningLockNative023 function end!"); } + +/** + * @tc.name: RunningLockNative024 + * @tc.desc: test IsVoiceAppForeground + * @tc.type: FUNC + * @tc.require: issueI7MNRN + */ +HWTEST_F(RunningLockNativeTest, RunningLockNative024, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockNative024 function start!"); + auto pmsTest = DelayedSpSingleton::GetInstance(); + auto runningLockMgr = std::make_shared(pmsTest); + std::string str = "app.bundlename1;app.bundlename2"; + auto appList = PowerUtils::Split(str, ';'); + + EXPECT_TRUE(runningLockMgr->Init()); + sptr remoteObject = new RunningLockTokenStub(); + bool ret = runningLockMgr->IsVoiceAppForeground(); + EXPECT_TRUE(ret); + POWER_HILOGI(LABEL_TEST, "RunningLockNative024 function end!"); +} } // namespace diff --git a/test/unittest/src/scenario_test/proximity_controller/running_lock_mgr_abnormal_test.cpp b/test/unittest/src/scenario_test/proximity_controller/running_lock_mgr_abnormal_test.cpp index 81f75bb9..f7439163 100644 --- a/test/unittest/src/scenario_test/proximity_controller/running_lock_mgr_abnormal_test.cpp +++ b/test/unittest/src/scenario_test/proximity_controller/running_lock_mgr_abnormal_test.cpp @@ -365,4 +365,19 @@ HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest009, TestSize.Lev SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, 0, 0)); POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest009 function end!"); } + +/** + * @tc.name: RunningLockMgrAbnormalTest010 + * @tc.desc: Test IsVoiceAppForeground + * @tc.type: FUNC + * @tc.require: ICGV1M + */ +HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest010, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest010 function start!"); + std::shared_ptr lockMgr = std::make_shared(nullptr); + bool ret = lockMgr->IsVoiceAppForeground(); + EXPECT_FALSE(ret); + POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest010 function end!"); +} } // namespace \ No newline at end of file diff --git a/utils/ability/power_ability.cpp b/utils/ability/power_ability.cpp index 5ed60895..a85112ab 100644 --- a/utils/ability/power_ability.cpp +++ b/utils/ability/power_ability.cpp @@ -62,9 +62,9 @@ void PowerGetForegroundApplications(std::vector& AppManagerUtils::GetForegroundApplications(appsData); } -bool PowerIsForegroundApplication(const std::string& appName) +bool PowerIsForegroundApplication(const std::set& appNames) { - return AppManagerUtils::IsForegroundApplication(appName); + return AppManagerUtils::IsForegroundApplication(appNames); } #ifdef __cplusplus diff --git a/utils/appmgr/include/app_manager_utils.h b/utils/appmgr/include/app_manager_utils.h index 46605094..b1bfed30 100644 --- a/utils/appmgr/include/app_manager_utils.h +++ b/utils/appmgr/include/app_manager_utils.h @@ -27,7 +27,7 @@ class AppManagerUtils final { public: static sptr GetAppManagerInstance(); static void GetForegroundApplications(std::vector& appsData); - static bool IsForegroundApplication(const std::string& appName); + static bool IsForegroundApplication(const std::set& appNames); static int32_t GetApiTargetVersion(); private: diff --git a/utils/appmgr/src/app_manager_utils.cpp b/utils/appmgr/src/app_manager_utils.cpp index ef8a2ebc..0923f864 100644 --- a/utils/appmgr/src/app_manager_utils.cpp +++ b/utils/appmgr/src/app_manager_utils.cpp @@ -70,9 +70,9 @@ void AppManagerUtils::GetForegroundApplications(std::vector& appNames) { - if (appName.empty()) { + if (appNames.empty()) { POWER_HILOGW(FEATURE_UTIL, "IsForegroundApplication: app name is empty"); return false; } @@ -81,8 +81,9 @@ bool AppManagerUtils::IsForegroundApplication(const std::string& appName) std::vector appsData; GetForegroundApplications(appsData); for (const auto& curApp : appsData) { - if (curApp.bundleName == appName) { + if (appNames.find(curApp.bundleName) != appNames.end()) { IsForeground = true; + POWER_HILOGI(FEATURE_UTIL, "Current ForegroundApp is %{public}s", curApp.bundleName.c_str()); break; } } diff --git a/utils/native/include/power_utils.h b/utils/native/include/power_utils.h index 925e5a6d..e7d425f9 100644 --- a/utils/native/include/power_utils.h +++ b/utils/native/include/power_utils.h @@ -16,6 +16,7 @@ #ifndef POWER_UTILS_H #define POWER_UTILS_H +#include #include "dm_common.h" #include "power_state_machine_info.h" #include "running_lock_info.h" @@ -32,7 +33,8 @@ public: static StateChangeReason GetReasonByUserActivity(UserActivityType type); static WakeupDeviceType ParseWakeupDeviceType(const std::string& details); static const std::string JsonToSimpleStr(const std::string& json); - static bool IsForegroundApplication(const std::string& appName); + static bool IsForegroundApplication(const std::set& appNames); + static std::set Split(const std::string& str, char delimiter); }; } // namespace PowerMgr } // namespace OHOS diff --git a/utils/native/src/power_utils.cpp b/utils/native/src/power_utils.cpp index 24322ef9..d65733c1 100644 --- a/utils/native/src/power_utils.cpp +++ b/utils/native/src/power_utils.cpp @@ -15,6 +15,7 @@ #include #include +#include #include "power_log.h" #include "power_utils.h" @@ -327,7 +328,7 @@ const std::string PowerUtils::JsonToSimpleStr(const std::string& json) return str; } -bool PowerUtils::IsForegroundApplication(const std::string& appName) +bool PowerUtils::IsForegroundApplication(const std::set& appNames) { void* handler = dlopen("libpower_ability.z.so", RTLD_NOW | RTLD_NODELETE); if (handler == nullptr) { @@ -336,7 +337,7 @@ bool PowerUtils::IsForegroundApplication(const std::string& appName) } auto powerIsForegroundApplicationFunc = - reinterpret_cast(dlsym(handler, "PowerIsForegroundApplication")); + reinterpret_cast&)>(dlsym(handler, "PowerIsForegroundApplication")); if (powerIsForegroundApplicationFunc == nullptr) { POWER_HILOGE(FEATURE_UTIL, "find PowerIsForegroundApplication function failed, reason : %{public}s", dlerror()); #ifndef FUZZ_TEST @@ -345,12 +346,24 @@ bool PowerUtils::IsForegroundApplication(const std::string& appName) handler = nullptr; return false; } - bool isForeground = powerIsForegroundApplicationFunc(appName); + bool isForeground = powerIsForegroundApplicationFunc(appNames); #ifndef FUZZ_TEST dlclose(handler); #endif handler = nullptr; return isForeground; } + +std::set PowerUtils::Split(const std::string& str, char delimiter) +{ + POWER_HILOGI(FEATURE_UTIL, "split str is: %{public}s", str.c_str()); + std::set tokens; + std::istringstream tokenStream(str); + std::string token; + while (std::getline(tokenStream, token, delimiter)) { + tokens.insert(token); + } + return tokens; +} } // namespace PowerMgr } // namespace OHOS -- Gitee