From 5d420b215e1c1c1b6447fd0c2637d0732996313e Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Thu, 31 Jul 2025 17:33:10 +0800 Subject: [PATCH] IsUserIdInForegrounds Signed-off-by: lanhaoyu --- services/bundlemgr/include/account_helper.h | 4 +++- services/bundlemgr/src/account_helper.cpp | 24 ++++++++++++++++++- .../bundle_resource_callback.cpp | 7 +++--- .../src/mock_account_helper_constraint.cpp | 22 +++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/services/bundlemgr/include/account_helper.h b/services/bundlemgr/include/account_helper.h index 865e0718ca..d720de1f55 100644 --- a/services/bundlemgr/include/account_helper.h +++ b/services/bundlemgr/include/account_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -29,6 +29,8 @@ public: static bool IsOsAccountVerified(const int32_t userId); + static bool IsUserIdInForegrounds(const int32_t userId); + static int32_t GetOsAccountLocalIdFromUid(const int32_t callingUid); static int32_t GetCurrentActiveUserIdWithRetry(bool isOtaInstall = false); diff --git a/services/bundlemgr/src/account_helper.cpp b/services/bundlemgr/src/account_helper.cpp index dc5a6b66ea..9841f9a680 100644 --- a/services/bundlemgr/src/account_helper.cpp +++ b/services/bundlemgr/src/account_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -150,5 +150,27 @@ bool AccountHelper::CheckOsAccountConstraintEnabled(const int32_t userId, const return false; #endif } + +bool AccountHelper::IsUserIdInForegrounds(const int32_t userId) +{ +#ifdef ACCOUNT_ENABLE + std::vector accounts; + int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccounts(accounts); + if (ret != 0) { + APP_LOGE_NOFUNC("GetForegroundOsAccounts failed ret:%{public}d", ret); + return false; + } + + for (const auto& account : accounts) { + if (account.localId == userId) { + return true; + } + } + return false; +#else + APP_LOGI("ACCOUNT_ENABLE is false"); + return false; +#endif +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp b/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp index d4471f7db5..2e1c222e08 100644 --- a/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp +++ b/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -27,9 +27,8 @@ bool BundleResourceCallback::OnUserIdSwitched(const int32_t oldUserId, const int { APP_LOGI("start, oldUserId:%{public}d to newUserId:%{public}d", oldUserId, userId); if (userId != Constants::START_USERID) { - int32_t currentUserId = AccountHelper::GetCurrentActiveUserId(); - if (currentUserId != userId) { - APP_LOGE("userId:%{public}d current:%{public}d not same", userId, currentUserId); + if (!AccountHelper::IsUserIdInForegrounds(userId)) { + APP_LOGE("userId:%{public}d is not InForegrounds", userId); return false; } } diff --git a/services/bundlemgr/test/mock/src/mock_account_helper_constraint.cpp b/services/bundlemgr/test/mock/src/mock_account_helper_constraint.cpp index 99907e641e..57b19b02ea 100644 --- a/services/bundlemgr/test/mock/src/mock_account_helper_constraint.cpp +++ b/services/bundlemgr/test/mock/src/mock_account_helper_constraint.cpp @@ -139,5 +139,27 @@ bool AccountHelper::CheckOsAccountConstraintEnabled(const int32_t userId, const { return true; } + +bool AccountHelper::IsUserIdInForegrounds(const int32_t userId) +{ +#ifdef ACCOUNT_ENABLE + std::vector accounts; + int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccounts(accounts); + if (ret != 0) { + APP_LOGE_NOFUNC("GetForegroundOsAccounts failed ret:%{public}d", ret); + return false; + } + + for (const auto& account : accounts) { + if (account.localId == userId) { + return true; + } + } + return false; +#else + APP_LOGI("ACCOUNT_ENABLE is false"); + return false; +#endif +} } // namespace AppExecFwk } // namespace OHOS -- Gitee