diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index d6da3dfa70854c60ec24c76b133db4dd447dd4d3..a7cd34c3f52e24306d3562fa8a4bc7cc153730d0 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -118,7 +118,7 @@ bool WorkStatus::NeedRemove() bool WorkStatus::IsSameUser() { - if (WorkSchedUtils::GetCurrentAccountId() != userId_) { + if (!WorkSchedUtils::IsIdActive(userId_)) { return false; } return true; diff --git a/utils/native/include/work_sched_utils.h b/utils/native/include/work_sched_utils.h index afd70c5d1d447dfe0f04b6f565c0af1ab38704c5..09af07da16d1a73d25b450be3a674f7370c084f8 100644 --- a/utils/native/include/work_sched_utils.h +++ b/utils/native/include/work_sched_utils.h @@ -27,6 +27,7 @@ public: ~WorkSchedUtils() = delete; static int GetCurrentAccountId(); + static bool IsIdActive(int id); static int32_t GetUserIdByUid(int32_t uid); static const int INVALID_DATA = -1; }; diff --git a/utils/native/src/work_sched_utils.cpp b/utils/native/src/work_sched_utils.cpp index c83b9565cc39859df4443557908b92d5da838494..9f16ef80d62d4d5f4f5103d96881f80934547f9b 100644 --- a/utils/native/src/work_sched_utils.cpp +++ b/utils/native/src/work_sched_utils.cpp @@ -23,27 +23,50 @@ namespace OHOS { namespace WorkScheduler { int WorkSchedUtils::GetCurrentAccountId() { - std::vector osAccountInfos; - ErrCode ret = AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + std::vector osAccountIds; + ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds); if (ret != ERR_OK) { - WS_HILOGE("QueryAllCreatedOsAccounts failed."); - return 0; + WS_HILOGE("QueryActiveOsAccountIds failed."); + return -1; } - if (osAccountInfos.empty()) { + if (osAccountIds.empty()) { WS_HILOGE("osAccountInfos is empty, no accounts."); - return 0; + return -1; } - for (const auto& account : osAccountInfos) { - if (account.GetIsActived()) { - return account.GetLocalId(); + for (const auto& accountId : osAccountIds) { + if (accountId >= 0) { + return accountId; } } WS_HILOGE("GetCurrentAccountId failed, no Actived now."); return -1; } +bool WorkSchedUtils::IsIdActive(int id) +{ + std::vector osAccountIds; + ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds); + if (ret != ERR_OK) { + WS_HILOGE("QueryActiveOsAccountIds failed."); + return false; + } + + if (osAccountIds.empty()) { + WS_HILOGE("osAccountIds is empty, no accounts."); + return false; + } + + for (const auto& accountId : osAccountIds) { + if (accountId == id) { + return true; + } + } + WS_HILOGE("IsIdActive failed, no Actived now."); + return false; +} + int32_t WorkSchedUtils::GetUserIdByUid(int32_t uid) { if (uid <= INVALID_DATA) {