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 // LCOV_EXCL_START
23 using namespace AccountSA;
IsOsAccountForeground(int32_t userId)24 bool OsAccountAdapter::IsOsAccountForeground(int32_t userId)
25 {
26 bool isForeground = false;
27 auto errCode = OsAccountManager::IsOsAccountForeground(userId, isForeground);
28 if (errCode != ERR_OK) {
29 IMSA_HILOGE("IsOsAccountForeground failed, userId:%{public}d.", userId);
30 }
31 return isForeground;
32 }
33
QueryActiveOsAccountIds()34 std::vector<int32_t> OsAccountAdapter::QueryActiveOsAccountIds()
35 {
36 std::vector<int32_t> userIds;
37 int errCode = OsAccountManager::QueryActiveOsAccountIds(userIds);
38 if (errCode != ERR_OK) {
39 IMSA_HILOGE("QueryActiveOsAccountIds failed.");
40 }
41 return userIds;
42 }
43
GetForegroundOsAccountLocalId()44 int32_t OsAccountAdapter::GetForegroundOsAccountLocalId()
45 {
46 int32_t userId = MAIN_USER_ID;
47 if (!BlockRetry(RETRY_INTERVAL, BLOCK_RETRY_TIMES,
48 [&userId]() -> bool { return OsAccountManager::GetForegroundOsAccountLocalId(userId) == ERR_OK; })) {
49 IMSA_HILOGE("get foreground userId failed!");
50 }
51 return userId;
52 }
53
GetOsAccountLocalIdFromUid(int32_t uid)54 int32_t OsAccountAdapter::GetOsAccountLocalIdFromUid(int32_t uid)
55 {
56 int32_t userId = INVALID_USER_ID;
57 auto errCode = OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
58 if (errCode != ERR_OK) {
59 IMSA_HILOGE("GetOsAccountLocalIdFromUid failed, uid:%{public}d", uid);
60 }
61 return userId;
62 }
63
IsOsAccountVerified(int32_t userId,bool & isUnlocked)64 int32_t OsAccountAdapter::IsOsAccountVerified(int32_t userId, bool &isUnlocked)
65 {
66 auto errCode = OsAccountManager::IsOsAccountVerified(userId, isUnlocked);
67 if (errCode != ERR_OK) {
68 IMSA_HILOGE("IsOsAccountVerified failed, userId: %{public}d, errCode: %{public}d", userId, errCode);
69 return ErrorCode::ERROR_OS_ACCOUNT;
70 }
71 return ErrorCode::NO_ERROR;
72 }
73 // LCOV_EXCL_STOP
74 } // namespace MiscServices
75 } // namespace OHOS