diff --git a/services/BUILD.gn b/services/BUILD.gn index af4dd2247b6fda7ffeec8d150cc029527a28d9c1..181b670fa55b24809ab5d8106ad2ae77c8d2996b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -101,7 +101,6 @@ ohos_shared_library("powermgrservice") { "ability_runtime:ability_connect_callback_stub", "bundle_framework:appexecfwk_core", "c_utils:utils", - "c_utils:utils", "common_event_service:cesfwk_innerkits", "config_policy:configpolicy_util", "data_share:datashare_consumer", diff --git a/services/native/include/power_state_machine.h b/services/native/include/power_state_machine.h index e9315f3772b57cb450b5d1c17250f532d7c31bba..29413102404b4ff113bcc5634641f845c14e03ae 100644 --- a/services/native/include/power_state_machine.h +++ b/services/native/include/power_state_machine.h @@ -76,6 +76,7 @@ public: CHECK_USER_ACTIVITY_TIMEOUT_MSG = 0, CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG, CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG, + CHECK_PROXIMITY_SCREEN_OFF_MSG, }; static void onSuspend(); @@ -275,8 +276,8 @@ private: }; static std::string GetTransitResultString(TransitResult result); - void UpdateSettingStateFlag(const PowerState state, const StateChangeReason reason); - void RestoreSettingStateFlag(const PowerState state, const StateChangeReason reason); + void UpdateSettingStateFlag(PowerState state, StateChangeReason reason); + void RestoreSettingStateFlag(); void InitStateMap(); void EmplaceAwake(); void EmplaceFreeze(); @@ -299,6 +300,7 @@ private: std::shared_ptr GetStateController(PowerState state); void ResetScreenOffPreTimeForSwing(int64_t displayOffTime); void ShowCurrentScreenLocks(); + void HandleProximityScreenOffTimer(PowerState state, StateChangeReason reason); bool HandlePreBrightState(StateChangeReason reason); bool IsPreBrightAuthReason(StateChangeReason reason); bool IsPreBrightWakeUp(WakeupDeviceType type); @@ -344,6 +346,7 @@ private: std::atomic settingOnStateFlag_ {false}; std::atomic settingOffStateFlag_ {false}; std::atomic preBrightState_ {PRE_BRIGHT_UNSTART}; + std::atomic proximityScreenOffTimerStarted_ {false}; }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index 75b4e530f7720dd276c0f462236cda9dcd533140..cd437cb8ee0396756581b18832e5806ac47c41f7 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -146,7 +146,7 @@ bool PowerStateMachine::CanTransitTo(PowerState to, StateChangeReason reason) return false; } #ifdef HAS_SENSORS_SENSOR_PART - // prevent the unexpected double click to light up the screen when calling + // prevent the double click and pickup to light up the screen when calling if ((reason == StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK || reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) && IsProximityClose() && to == PowerState::AWAKE) { @@ -902,13 +902,15 @@ void PowerStateMachine::PowerStateCallbackDeathRecipient::OnRemoteDied(const wpt void PowerStateMachine::SetDelayTimer(int64_t delayTime, int32_t event) { + if (!ffrtTimer_) { + POWER_HILOGE(FEATURE_ACTIVITY, "Failed to set delay timer, the timer pointer is null"); + return; + } POWER_HILOGD(FEATURE_ACTIVITY, "Set delay timer, delayTime=%{public}s, event=%{public}d", std::to_string(delayTime).c_str(), event); + switch (event) { case CHECK_USER_ACTIVITY_TIMEOUT_MSG: { - if (!ffrtTimer_) { - return; - } FFRTTask task = [this] { this->HandleActivityTimeout(); }; ffrtTimer_->SetTimer(TIMER_ID_USER_ACTIVITY_TIMEOUT, task, delayTime); break; @@ -923,6 +925,27 @@ void PowerStateMachine::SetDelayTimer(int64_t delayTime, int32_t event) suspendController->HandleEvent(delayTime); break; } + case CHECK_PROXIMITY_SCREEN_OFF_MSG: { + FFRTTask delayScreenOffTask = [this] { + POWER_HILOGI(FEATURE_POWER_STATE, "proximity-screen-off timer task is triggered"); + proximityScreenOffTimerStarted_.store(false, std::memory_order_relaxed); + auto pms = DelayedSpSingleton::GetInstance(); + auto suspendController = pms->GetSuspendController(); + if (suspendController == nullptr) { + POWER_HILOGW( + FEATURE_POWER_STATE, "suspendController is nullptr, exit proximity-screen-off timer task"); + return; + } + bool ret = SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true); + if (ret) { + suspendController->StartSleepTimer(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, + static_cast(SuspendAction::ACTION_AUTO_SUSPEND), 0); + } + }; + ffrtTimer_->SetTimer(TIMER_ID_PROXIMITY_SCREEN_OFF, delayScreenOffTask, delayTime); + proximityScreenOffTimerStarted_.store(true, std::memory_order_relaxed); + break; + } default: { break; } @@ -931,12 +954,14 @@ void PowerStateMachine::SetDelayTimer(int64_t delayTime, int32_t event) void PowerStateMachine::CancelDelayTimer(int32_t event) { + if (!ffrtTimer_) { + POWER_HILOGE(FEATURE_ACTIVITY, "Failed to cancel delay timer, the timer pointer is null"); + return; + } POWER_HILOGD(FEATURE_ACTIVITY, "Cancel delay timer, event: %{public}d", event); + switch (event) { case CHECK_USER_ACTIVITY_TIMEOUT_MSG: { - if (!ffrtTimer_) { - return; - } ffrtTimer_->CancelTimer(TIMER_ID_USER_ACTIVITY_TIMEOUT); break; } @@ -951,12 +976,14 @@ void PowerStateMachine::CancelDelayTimer(int32_t event) break; } case CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG: { - if (!ffrtTimer_) { - return; - } ffrtTimer_->CancelTimer(TIMER_ID_PRE_BRIGHT_AUTH); break; } + case CHECK_PROXIMITY_SCREEN_OFF_MSG: { + ffrtTimer_->CancelTimer(TIMER_ID_PROXIMITY_SCREEN_OFF); + proximityScreenOffTimerStarted_.store(false, std::memory_order_relaxed); + break; + } default: { break; } @@ -1373,7 +1400,7 @@ bool PowerStateMachine::NeedShowScreenLocks(PowerState state) state == PowerState::INACTIVE || state == PowerState::DIM; } -void PowerStateMachine::UpdateSettingStateFlag(const PowerState state, const StateChangeReason reason) +void PowerStateMachine::UpdateSettingStateFlag(PowerState state, StateChangeReason reason) { if (reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT || reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF) { @@ -1385,12 +1412,36 @@ void PowerStateMachine::UpdateSettingStateFlag(const PowerState state, const Sta settingOffStateFlag_ = (state == PowerState::INACTIVE); } -void PowerStateMachine::RestoreSettingStateFlag(const PowerState state, const StateChangeReason reason) +void PowerStateMachine::RestoreSettingStateFlag() { settingOnStateFlag_ = false; settingOffStateFlag_ = false; } +void PowerStateMachine::HandleProximityScreenOffTimer(PowerState state, StateChangeReason reason) +{ + if (!proximityScreenOffTimerStarted_.load()) { + return; + } + if ((reason == StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK || + reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) && + IsProximityClose() && state == PowerState::AWAKE) { + POWER_HILOGI(FEATURE_POWER_STATE, "Double-click or pickup is allowed to cancel proximity-screen-off timer"); + return; + } + if (reason != StateChangeReason::STATE_CHANGE_REASON_PROXIMITY) { + POWER_HILOGI(FEATURE_POWER_STATE, "Cancel proximity-screen-off timer, reason:%{public}s", + PowerUtils::GetReasonTypeString(reason).c_str()); + CancelDelayTimer(PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG); + return; + } + if (state == PowerState::AWAKE) { + POWER_HILOGI(FEATURE_POWER_STATE, "Cancel proximity-screen-off timer, reason:%{public}s(away)", + PowerUtils::GetReasonTypeString(reason).c_str()); + CancelDelayTimer(PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG); + } +} + bool PowerStateMachine::HandlePreBrightState(StateChangeReason reason) { bool ret = false; @@ -1445,6 +1496,7 @@ bool PowerStateMachine::SetState(PowerState state, StateChangeReason reason, boo ShowCurrentScreenLocks(); } + HandleProximityScreenOffTimer(state, reason); if (!HandlePreBrightState(reason)) { timeoutCheck.Finish(TransitResult::OTHER_ERR); return false; @@ -1466,7 +1518,7 @@ bool PowerStateMachine::SetState(PowerState state, StateChangeReason reason, boo timeoutCheck.Finish(ret); POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER] StateController::TransitTo %{public}s ret: %{public}d", PowerUtils::GetPowerStateString(state).c_str(), ret); - RestoreSettingStateFlag(state, reason); + RestoreSettingStateFlag(); return (ret == TransitResult::SUCCESS || ret == TransitResult::ALREADY_IN_STATE); } diff --git a/services/native/src/runninglock/running_lock_mgr.cpp b/services/native/src/runninglock/running_lock_mgr.cpp index 291ede0d2f80de63af941b7e0c2f8d3663d72721..0d6de8a30b50e167cb1001c2693a4a2df6d9adc7 100644 --- a/services/native/src/runninglock/running_lock_mgr.cpp +++ b/services/native/src/runninglock/running_lock_mgr.cpp @@ -37,6 +37,9 @@ 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"; +constexpr uint32_t FOREGROUND_INCALL_DELAY_TIME_MS = 300; +constexpr uint32_t BACKGROUND_INCALL_DELAY_TIME_MS = 800; } RunningLockMgr::~RunningLockMgr() {} @@ -176,16 +179,15 @@ void RunningLockMgr::InitLocksTypeProximity() return RUNNINGLOCK_FAILURE; } auto stateMachine = pms->GetPowerStateMachine(); - if (stateMachine == nullptr) { + auto suspendController = pms->GetSuspendController(); + if (stateMachine == nullptr || suspendController == nullptr) { return RUNNINGLOCK_FAILURE; } if (active) { - ProximityLockOn(); + POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL active"); + proximityController_.Enable(); } else { POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL inactive"); - PreprocessBeforeAwake(); - stateMachine->SetState(PowerState::AWAKE, - StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK); proximityController_.Disable(); proximityController_.Clear(); } @@ -926,14 +928,14 @@ void RunningLockMgr::ProximityController::OnClose() isClose_ = true; POWER_HILOGD(FEATURE_RUNNING_LOCK, "PROXIMITY is closed"); auto runningLock = pms->GetRunningLockMgr(); - if (runningLock->GetValidRunningLockNum( - RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) { + if (runningLock->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) { POWER_HILOGD(FEATURE_RUNNING_LOCK, "Change state to INACITVE when holding PROXIMITY LOCK"); - bool ret = stateMachine->SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true); - if (ret) { - suspendController->StartSleepTimer(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, - static_cast(SuspendAction::ACTION_AUTO_SUSPEND), 0); + uint32_t delayTime = FOREGROUND_INCALL_DELAY_TIME_MS; + if (!PowerUtils::IsForegroundApplication(INCALL_APP_BUNDLE_NAME)) { + delayTime = BACKGROUND_INCALL_DELAY_TIME_MS; } + POWER_HILOGI(FEATURE_RUNNING_LOCK, "Start proximity-screen-off timer, delay time:%{public}u", delayTime); + stateMachine->SetDelayTimer(delayTime, PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG); } } diff --git a/services/native/src/shutdown/shutdown_dialog.cpp b/services/native/src/shutdown/shutdown_dialog.cpp index 78014d86a1ec93fd0fe7a199d764470e6c2bfbfe..eff3a9120a6ca6ef01a94e2b263628e3ebd06dc3 100644 --- a/services/native/src/shutdown/shutdown_dialog.cpp +++ b/services/native/src/shutdown/shutdown_dialog.cpp @@ -121,7 +121,7 @@ bool ShutdownDialog::ConnectSystemUi() POWER_HILOGW(FEATURE_SHUTDOWN, "power dialog has been show"); return true; } - + Want want; want.SetElementName("com.ohos.systemui", "com.ohos.systemui.dialog"); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index e8f45637eb4430a57de4b54f404619d8de5356e8..df79cf3f131aa056413a3c943cf93697bc824cca 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -39,7 +39,10 @@ ohos_source_set("power_utils") { public_configs = [ ":utils_config" ] - external_deps = [ "c_utils:utils" ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] subsystem_name = "powermgr" part_name = "${powermgr_part_name}" diff --git a/utils/ability/BUILD.gn b/utils/ability/BUILD.gn index c8cf08f3faa16fa26af6d73afb10b2da9a357a2d..26dc10c49f7f3adc0f4374f8f60dcc2c1eab49ba 100644 --- a/utils/ability/BUILD.gn +++ b/utils/ability/BUILD.gn @@ -27,7 +27,12 @@ ohos_shared_library("power_ability") { sources = [ "power_ability.cpp" ] - configs = [ ":private_config" ] + configs = [ + ":private_config", + "${powermgr_utils_path}:coverage_flags", + ] + + deps = [ "${powermgr_utils_path}/appmgr:power_appmgr" ] external_deps = [ "ability_runtime:ability_manager", diff --git a/utils/ability/power_ability.cpp b/utils/ability/power_ability.cpp index e47d426d20d51e99ea3c78058ba2a5e1775af5da..0ddae54ef621fa206ec5ab32636422708060a470 100644 --- a/utils/ability/power_ability.cpp +++ b/utils/ability/power_ability.cpp @@ -13,14 +13,22 @@ * limitations under the License. */ +#include +#include #include +#include "app_manager_utils.h" #include "power_log.h" + +#ifdef __cplusplus +extern "C" { +#endif + using namespace OHOS; using namespace PowerMgr; using namespace AAFwk; -extern "C" void PowerConnectAbility(const Want &want, const sptr& connect, int32_t userId) +void PowerConnectAbility(const Want& want, const sptr& connect, int32_t userId) { auto amsClient = AbilityManagerClient::GetInstance(); if (amsClient == nullptr) { @@ -35,7 +43,7 @@ extern "C" void PowerConnectAbility(const Want &want, const sptr& appsData) +{ + AppManagerUtils::GetForegroundApplications(appsData); +} + +bool PowerIsForegroundApplication(const std::string& appName) +{ + return AppManagerUtils::IsForegroundApplication(appName); +} + +#ifdef __cplusplus +} +#endif diff --git a/utils/appmgr/BUILD.gn b/utils/appmgr/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d4a9a7c4d6090f83213b55a52191b2277d89ce6a --- /dev/null +++ b/utils/appmgr/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/powermgr/power_manager/powermgr.gni") + +config("private_config") { + include_dirs = [ + "include", + "../native/include", + ] +} + +config("public_config") { + include_dirs = [ "include" ] +} + +ohos_source_set("power_appmgr") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + branch_protector_ret = "pac_ret" + + sources = [ "src/app_manager_utils.cpp" ] + + configs = [ + ":private_config", + "${powermgr_utils_path}:coverage_flags", + ] + + public_configs = [ ":public_config" ] + + external_deps = [ + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" +} diff --git a/utils/appmgr/include/app_manager_utils.h b/utils/appmgr/include/app_manager_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..fa74e4c26a763fa3ab43a8cab8b24bcbf4c1d622 --- /dev/null +++ b/utils/appmgr/include/app_manager_utils.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef POWERMGR_APP_MANAGER_UTILS_H +#define POWERMGR_APP_MANAGER_UTILS_H + +#include +#include + +#include "app_mgr_interface.h" + +namespace OHOS { +namespace PowerMgr { +class AppManagerUtils final { +public: + static sptr GetAppManagerInstance(); + static void GetForegroundApplications(std::vector& appsData); + static bool IsForegroundApplication(const std::string& appName); + +private: + static sptr appManagerInstance_; +}; + +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_APP_MANAGER_UTILS_H \ No newline at end of file diff --git a/utils/appmgr/src/app_manager_utils.cpp b/utils/appmgr/src/app_manager_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18c7e11c90a7054c8983479f215fa97b178b440d --- /dev/null +++ b/utils/appmgr/src/app_manager_utils.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "app_manager_utils.h" + +#include "power_log.h" +#include +#include +#include +#include + +namespace OHOS { +namespace PowerMgr { +static constexpr uint32_t APP_MGR_SERVICE_ID = 501; +sptr AppManagerUtils::appManagerInstance_ = nullptr; + +sptr AppManagerUtils::GetAppManagerInstance() +{ + if (appManagerInstance_) { + return appManagerInstance_; + } + + sptr abilityMgr = + OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (abilityMgr == nullptr) { + POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager"); + return nullptr; + } + sptr remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager service, id=%{public}u", APP_MGR_SERVICE_ID); + return nullptr; + } + sptr appMgrProxy = iface_cast(remoteObject); + if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) { + POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager proxy"); + return nullptr; + } + appManagerInstance_ = appMgrProxy; + return appManagerInstance_; +} + +void AppManagerUtils::GetForegroundApplications(std::vector& appsData) +{ + auto appMgr = GetAppManagerInstance(); + if (!appMgr) { + return; + } + int32_t ret = appMgr->GetForegroundApplications(appsData); + POWER_HILOGI( + FEATURE_UTIL, "GetForegroundApplications, ret: %{public}u, num of apps: %{public}zu", ret, appsData.size()); +} + +bool AppManagerUtils::IsForegroundApplication(const std::string& appName) +{ + if (appName.empty()) { + POWER_HILOGW(FEATURE_UTIL, "IsForegroundApplication: app name is empty"); + return false; + } + + bool IsForeground = false; + std::vector appsData; + GetForegroundApplications(appsData); + for (const auto& curApp : appsData) { + if (curApp.bundleName == appName) { + IsForeground = true; + break; + } + } + POWER_HILOGI(FEATURE_UTIL, "IsForegroundApplication, ret: %{public}u", static_cast(IsForeground)); + return IsForeground; +} + +} // namespace PowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/utils/ffrt/include/ffrt_utils.h b/utils/ffrt/include/ffrt_utils.h index 5d6f4626adbd6e64830996ff63ddf653762cc0c4..bb91f03bb5b21e432cad691e16f48a73dd7f01c4 100644 --- a/utils/ffrt/include/ffrt_utils.h +++ b/utils/ffrt/include/ffrt_utils.h @@ -130,6 +130,7 @@ enum FFRTTimerId { TIMER_ID_USER_ACTIVITY_TIMEOUT, TIMER_ID_SCREEN_TIMEOUT_CHECK, TIMER_ID_PRE_BRIGHT_AUTH, + TIMER_ID_PROXIMITY_SCREEN_OFF, }; class FFRTMutexMap { diff --git a/utils/native/include/power_utils.h b/utils/native/include/power_utils.h index 993ddf93c4d7e19b201264f239a521607c00b98e..d7f552aeef89d92a9182797c88c6e094a01f86ae 100644 --- a/utils/native/include/power_utils.h +++ b/utils/native/include/power_utils.h @@ -28,6 +28,7 @@ public: static const std::string GetDisplayStateString(DisplayState state); static const std::string GetRunningLockTypeString(RunningLockType type); static const std::string JsonToSimpleStr(const std::string& json); + static bool IsForegroundApplication(const std::string& appName); }; } // namespace PowerMgr } // namespace OHOS diff --git a/utils/native/src/power_utils.cpp b/utils/native/src/power_utils.cpp index de634627d78e66e4301fb80dd6d416ec11a2d01c..d98b5b4596683588150be5412214d47b0daf0433 100644 --- a/utils/native/src/power_utils.cpp +++ b/utils/native/src/power_utils.cpp @@ -13,6 +13,9 @@ * limitations under the License. */ +#include +#include +#include "power_log.h" #include "power_utils.h" namespace OHOS { @@ -189,5 +192,31 @@ const std::string PowerUtils::JsonToSimpleStr(const std::string& json) } return str; } + +bool PowerUtils::IsForegroundApplication(const std::string& appName) +{ + void* handler = dlopen("libpower_ability.z.so", RTLD_NOW); + if (handler == nullptr) { + POWER_HILOGE(FEATURE_UTIL, "dlopen libpower_ability.z.so failed, reason : %{public}s", dlerror()); + return false; + } + + auto powerIsForegroundApplicationFunc = + reinterpret_cast(dlsym(handler, "PowerIsForegroundApplication")); + if (powerIsForegroundApplicationFunc == nullptr) { + POWER_HILOGE(FEATURE_UTIL, "find PowerIsForegroundApplication function failed, reason : %{public}s", dlerror()); +#ifndef FUZZ_TEST + dlclose(handler); +#endif + handler = nullptr; + return false; + } + bool isForeground = powerIsForegroundApplicationFunc(appName); +#ifndef FUZZ_TEST + dlclose(handler); +#endif + handler = nullptr; + return isForeground; +} } // namespace PowerMgr } // namespace OHOS