1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "os_account_adapter.h"
17 
18 #include "global.h"
19 #include "os_account_manager.h"
20 namespace OHOS {
21 namespace MiscServices {
22 using namespace AccountSA;
IsOsAccountForeground(int32_t userId)23 bool OsAccountAdapter::IsOsAccountForeground(int32_t userId)
24 {
25     bool isForeground = false;
26     auto errCode = OsAccountManager::IsOsAccountForeground(userId, isForeground);
27     if (errCode != ERR_OK) {
28         IMSA_HILOGE("IsOsAccountForeground failed, userId:%{public}d.", userId);
29     }
30     return isForeground;
31 }
32 
QueryActiveOsAccountIds()33 std::vector<int32_t> OsAccountAdapter::QueryActiveOsAccountIds()
34 {
35     std::vector<int32_t> userIds;
36     int errCode = OsAccountManager::QueryActiveOsAccountIds(userIds);
37     if (errCode != ERR_OK) {
38         IMSA_HILOGE("QueryActiveOsAccountIds failed.");
39     }
40     return userIds;
41 }
42 
GetForegroundOsAccountLocalId()43 int32_t OsAccountAdapter::GetForegroundOsAccountLocalId()
44 {
45     int32_t userId = MAIN_USER_ID;
46     if (!BlockRetry(RETRY_INTERVAL, BLOCK_RETRY_TIMES,
47         [&userId]() -> bool { return OsAccountManager::GetForegroundOsAccountLocalId(userId) == ERR_OK; })) {
48         IMSA_HILOGE("get foreground userId failed!");
49     }
50     return userId;
51 }
52 
GetOsAccountLocalIdFromUid(int32_t uid)53 int32_t OsAccountAdapter::GetOsAccountLocalIdFromUid(int32_t uid)
54 {
55     int32_t userId = INVALID_USER_ID;
56     auto errCode = OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
57     if (errCode != ERR_OK) {
58         IMSA_HILOGE("GetOsAccountLocalIdFromUid failed, uid:%{public}d", uid);
59     }
60     return userId;
61 }
62 
IsOsAccountVerified(int32_t userId,bool & isUnlocked)63 int32_t OsAccountAdapter::IsOsAccountVerified(int32_t userId, bool &isUnlocked)
64 {
65     auto errCode = OsAccountManager::IsOsAccountVerified(userId, isUnlocked);
66     if (errCode != ERR_OK) {
67         IMSA_HILOGE("IsOsAccountVerified failed, userId: %{public}d, errCode: %{public}d", userId, errCode);
68         return ErrorCode::ERROR_OS_ACCOUNT;
69     }
70     return ErrorCode::NO_ERROR;
71 }
72 } // namespace MiscServices
73 } // namespace OHOS