diff --git a/services/bundlemgr/include/account_helper.h b/services/bundlemgr/include/account_helper.h index 865e0718ca27789152caeda7629c1ce96a08310a..d720de1f55e6353feb584ae6aac7aee59093a479 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 dc5a6b66ea8a997cd9828349d6508c60cdaa0903..9841f9a680123682d7d7f0e84eceb4e39476dcfb 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 d4471f7db5f220e8490f839e27ffbd3e7948b55d..2e1c222e08c0cf5c37a0c5a4fd0d0c70e5823f5a 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 99907e641e4e4c3e1ae6c8858584de2531861be3..57b19b02ea855671673c4d19bb429b4fce1eea2a 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