From c267643e14d9ac6c6982efeefa895663ceb1414c Mon Sep 17 00:00:00 2001 From: chenming Date: Tue, 15 Feb 2022 13:01:51 +0800 Subject: [PATCH 1/3] add account get method Signed-off-by: chenming --- services/native/src/work_status.cpp | 2 +- utils/native/include/work_sched_utils.h | 1 + utils/native/src/work_sched_utils.cpp | 41 +++++++++++++++++++------ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index d6da3df..a7cd34c 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 afd70c5..f96a811 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 c83b956..6aeb225 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 false; } - 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 == uid) { + return true; + } + } + WS_HILOGE("IsIdActive failed, no Actived now."); + return false; +} + int32_t WorkSchedUtils::GetUserIdByUid(int32_t uid) { if (uid <= INVALID_DATA) { -- Gitee From 49658a22e0a542aac95544ca6b0d75586b13f6d1 Mon Sep 17 00:00:00 2001 From: chenming Date: Tue, 15 Feb 2022 13:03:38 +0800 Subject: [PATCH 2/3] add account get method Signed-off-by: chenming --- utils/native/src/work_sched_utils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/native/src/work_sched_utils.cpp b/utils/native/src/work_sched_utils.cpp index 6aeb225..c1b7e26 100644 --- a/utils/native/src/work_sched_utils.cpp +++ b/utils/native/src/work_sched_utils.cpp @@ -32,11 +32,11 @@ int WorkSchedUtils::GetCurrentAccountId() if (osAccountIds.empty()) { WS_HILOGE("osAccountInfos is empty, no accounts."); - return false; + return -1; } for (const auto& accountId : osAccountIds) { - if (accountId > 0) { + if (accountId >= 0) { return accountId; } } -- Gitee From 7d420da827a556017679e14dd5840629067a8baa Mon Sep 17 00:00:00 2001 From: chenming Date: Tue, 15 Feb 2022 14:39:00 +0800 Subject: [PATCH 3/3] add account get method Signed-off-by: chenming --- utils/native/include/work_sched_utils.h | 2 +- utils/native/src/work_sched_utils.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/native/include/work_sched_utils.h b/utils/native/include/work_sched_utils.h index f96a811..09af07d 100644 --- a/utils/native/include/work_sched_utils.h +++ b/utils/native/include/work_sched_utils.h @@ -27,7 +27,7 @@ public: ~WorkSchedUtils() = delete; static int GetCurrentAccountId(); - static bool IsIdActive(int id) + 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 c1b7e26..9f16ef8 100644 --- a/utils/native/src/work_sched_utils.cpp +++ b/utils/native/src/work_sched_utils.cpp @@ -59,7 +59,7 @@ bool WorkSchedUtils::IsIdActive(int id) } for (const auto& accountId : osAccountIds) { - if (accountId == uid) { + if (accountId == id) { return true; } } -- Gitee