1 /* 2 * Copyright (c) 2022-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 "multiple_user_connector.h" 17 18 #include "dm_log.h" 19 20 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 21 #include "account_info.h" 22 #include "ohos_account_kits.h" 23 #ifdef OS_ACCOUNT_PART_EXISTS 24 #include "os_account_manager.h" 25 using namespace OHOS::AccountSA; 26 #endif // OS_ACCOUNT_PART_EXISTS 27 #endif 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 int32_t MultipleUserConnector::oldUserId_ = -1; 32 std::string MultipleUserConnector::accountId_ = ""; 33 std::string MultipleUserConnector::accountName_ = ""; 34 std::mutex MultipleUserConnector::lock_; 35 #ifndef OS_ACCOUNT_PART_EXISTS 36 const int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part 37 #endif // OS_ACCOUNT_PART_EXISTS 38 GetCurrentAccountUserID(void)39int32_t MultipleUserConnector::GetCurrentAccountUserID(void) 40 { 41 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) 42 return 0; 43 #elif OS_ACCOUNT_PART_EXISTS 44 std::vector<int> ids; 45 ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids); 46 if (ret != 0 || ids.empty()) { 47 LOGE("GetCurrentAccountUserID error ret: %{public}d", ret); 48 return -1; 49 } 50 return ids[0]; 51 #else // OS_ACCOUNT_PART_EXISTS 52 return DEFAULT_OS_ACCOUNT_ID; 53 #endif 54 } 55 GetOhosAccountId(void)56std::string MultipleUserConnector::GetOhosAccountId(void) 57 { 58 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) 59 return ""; 60 #elif OS_ACCOUNT_PART_EXISTS 61 OhosAccountInfo accountInfo; 62 ErrCode ret = OhosAccountKits::GetInstance().GetOhosAccountInfo(accountInfo); 63 if (ret != 0 || accountInfo.uid_ == "") { 64 LOGE("GetOhosAccountId error ret: %{public}d", ret); 65 return ""; 66 } 67 return accountInfo.uid_; 68 #else // OS_ACCOUNT_PART_EXISTS 69 return ""; 70 #endif 71 } 72 GetOhosAccountName(void)73std::string MultipleUserConnector::GetOhosAccountName(void) 74 { 75 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) 76 return ""; 77 #elif OS_ACCOUNT_PART_EXISTS 78 auto accountInfo = OhosAccountKits::GetInstance().QueryOhosAccountInfo(); 79 if (!accountInfo.first) { 80 LOGE("QueryOhosAccountInfo failed."); 81 return ""; 82 } 83 if (accountInfo.second.name_.empty()) { 84 LOGE("QueryOhosAccountInfo name empty."); 85 return ""; 86 } 87 return accountInfo.second.name_; 88 #else 89 return ""; 90 #endif 91 } 92 SetSwitchOldUserId(int32_t userId)93void MultipleUserConnector::SetSwitchOldUserId(int32_t userId) 94 { 95 std::lock_guard<std::mutex> lock(lock_); 96 oldUserId_ = userId; 97 } 98 GetSwitchOldUserId(void)99int32_t MultipleUserConnector::GetSwitchOldUserId(void) 100 { 101 std::lock_guard<std::mutex> lock(lock_); 102 return oldUserId_; 103 } 104 SetSwitchOldAccountId(std::string accountId)105void MultipleUserConnector::SetSwitchOldAccountId(std::string accountId) 106 { 107 std::lock_guard<std::mutex> lock(lock_); 108 accountId_ = accountId; 109 } 110 GetSwitchOldAccountId(void)111std::string MultipleUserConnector::GetSwitchOldAccountId(void) 112 { 113 std::lock_guard<std::mutex> lock(lock_); 114 return accountId_; 115 } 116 SetSwitchOldAccountName(std::string accountName)117void MultipleUserConnector::SetSwitchOldAccountName(std::string accountName) 118 { 119 std::lock_guard<std::mutex> lock(lock_); 120 accountName_ = accountName; 121 } 122 GetSwitchOldAccountName(void)123std::string MultipleUserConnector::GetSwitchOldAccountName(void) 124 { 125 std::lock_guard<std::mutex> lock(lock_); 126 return accountName_; 127 } 128 } // namespace DistributedHardware 129 } // namespace OHOS 130