1// @ts-nocheck 2/** 3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import osAccount from '@ohos.account.osAccount'; 18import type common from '@ohos.app.ability.common'; 19import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil'; 20import { GlobalContext } from '../../../../../../../common/utils/src/main/ets/default/baseUtil/GlobalContext'; 21import GlobalResourceManager 22 from '../../../../../../../common/utils/src/main/ets/default/baseUtil/GlobalResourceManager'; 23import SystemAccountModel, { MAX_ACCOUNT } from './systemAccountModel'; 24 25export class SystemAccountController { 26 private static instance: SystemAccountController; 27 private currentAccount: osAccount.OsAccountInfo; 28 private accountList: osAccount.OsAccountInfo[] = []; 29 30 constructor() { 31 } 32 33 public static getInstance(): SystemAccountController { 34 if (!SystemAccountController.instance) { 35 SystemAccountController.instance = new SystemAccountController(); 36 } 37 return SystemAccountController.instance; 38 } 39 40 /** 41 * Get all created system account name. 42 */ 43 async refreshSystemAccountList() { 44 LogUtil.info("Refresh system account list."); 45 AppStorage.SetOrCreate("systemAccountList", []); 46 this.accountList = await osAccount.getAccountManager().queryAllCreatedOsAccounts(); 47 LogUtil.info("before sort account list, length: " + this.accountList.length + ", list: " + JSON.stringify(this.accountList)); 48 this.currentAccount = SystemAccountModel.getCurrentAccount(this.accountList); 49 LogUtil.info("Current account localId: " + this.currentAccount.localId); 50 this.accountList.sort(SystemAccountModel.sortAccount.bind(this)); 51 LogUtil.info("Successfully sort account list, length: " + this.accountList.length); 52 AppStorage.SetOrCreate("systemAccountList", this.accountList); 53 AppStorage.SetOrCreate("isShowAddUser", this.isShowAddUser()); 54 AppStorage.SetOrCreate("isShowAddGuest", this.isShowAddQuest()); 55 AppStorage.SetOrCreate("isShowDelete",!this.isCurrentAdministrator()); 56 } 57 58 /** 59 * Whether to show system account type. 60 * 61 * @return true if current account is administrator. 62 */ 63 isShowIdentity(accountInfo) { 64 return accountInfo.type === osAccount.OsAccountType.ADMIN; 65 } 66 67 /** 68 * Whether account list has quest 69 * 70 * @return boolean.true if account list has quest,false if account list doesn't have quest 71 */ 72 isHasQuest() { 73 for (let index = 0; index < this.accountList.length; index++) { 74 LogUtil.info("Is show add quest, system account type: " + this.accountList[index].type); 75 if (this.accountList[index].type === osAccount.OsAccountType.GUEST) { 76 return true; 77 } 78 } 79 return false; 80 } 81 82 /** 83 * Whether to show add normal user item. 84 * 85 * @return true if current user is administrator. 86 */ 87 isShowAddUser() { 88 return this.currentAccount.type === osAccount.OsAccountType.ADMIN && this.accountList.length < (this.isHasQuest() ? MAX_ACCOUNT : (MAX_ACCOUNT - 1)); 89 } 90 91 /** 92 * Whether to show add create normal account list item. 93 * 94 * @return true when created account list no contains quest account. 95 */ 96 isShowAddQuest() { 97 return this.currentAccount.type === osAccount.OsAccountType.ADMIN && !this.isHasQuest() && this.accountList.length < MAX_ACCOUNT; 98 } 99 100 /** 101 * Whether is current system account. 102 * 103 * @return true when clicked item is system account. 104 */ 105 isCurrentUser(accountInfo: any) { 106 LogUtil.info("Is current user, account id: " + accountInfo.localId); 107 return accountInfo.localId === this.currentAccount.localId; 108 } 109 110 /** 111 * Whether current account is administrator type. 112 * 113 * @return true when current account type is administrator. 114 */ 115 isCurrentAdministrator() { 116 LogUtil.info("Is current user administrator.") 117 return this.currentAccount.type === osAccount.OsAccountType.ADMIN; 118 } 119 120 /** 121 * Whether current account is quest type. 122 * 123 * @return true when current account type is quest. 124 */ 125 isCurrentQuest() { 126 return this.currentAccount.type === osAccount.OsAccountType.GUEST; 127 } 128 129 /** 130 * Whether the account is quest. 131 * 132 * @param account input system account. 133 */ 134 isGuestAccount(account: any) { 135 return account.type === osAccount.OsAccountType.GUEST; 136 } 137 138 /** 139 * Get new created account. 140 * 141 * @return the local id of newly created system account. 142 */ 143 switchToCreatedAccount(): number { 144 this.accountList.sort((account1, account2) => { 145 return account2.serialNumber - account1.serialNumber; 146 }); 147 let newlyId = this.accountList[0].localId; 148 LogUtil.info("Newly created local id: " + newlyId); 149 this.switchUser(newlyId); 150 } 151 152 /** 153 * Create the quest system account. 154 */ 155 async createQuest(callback: (account: any) => void) { 156 LogUtil.info("Create quest account."); 157 let localName = GlobalResourceManager.getStringByResource($r("app.string.quest")); 158 localName.then(name => { 159 osAccount.getAccountManager().createOsAccount(name, osAccount.OsAccountType.GUEST).then((accountInfo) => { 160 LogUtil.info("Create quest system account."); 161 this.refreshSystemAccountList(); 162 callback(accountInfo); 163 }); 164 }) 165 } 166 167 /** 168 * Create the normal system account. 169 * 170 * @param localName create name of normal system account. 171 */ 172 async createSystemAccount(localName: string, callback?: (account: any) => void) { 173 LogUtil.info("Create system account."); 174 osAccount.getAccountManager().createOsAccount(localName, osAccount.OsAccountType.NORMAL).then(accountInfo => { 175 this.refreshSystemAccountList(); 176 callback(accountInfo); 177 }); 178 } 179 180 /** 181 * To check whether the user name is used. 182 * 183 * @param inputName name of input. 184 * @return true if created system accounts include the input name. 185 */ 186 isAlreadyCreated(inputName: string): boolean { 187 for (let index = 0; index < this.accountList.length; index++) { 188 if (this.accountList[index].localName === inputName) { 189 return true; 190 } 191 } 192 return false; 193 } 194 195 /** 196 * To set the system account name. 197 * 198 * @param localId local id of system account. 199 * @param name name of system account. 200 */ 201 async setAccountName(localId: number, name: string) { 202 LogUtil.info("Set system account name."); 203 osAccount.getAccountManager().setOsAccountName(localId, name).then(() => { 204 this.refreshSystemAccountList(); 205 }); 206 } 207 208 /** 209 * Switch to other system account. 210 * 211 * @param localId local id of object system account. 212 */ 213 switchUser(localId: number) { 214 LogUtil.info("Switch system account."); 215 osAccount.getAccountManager().activateOsAccount(localId).then(() => { 216 LogUtil.info("Successfully switched to account: " + localId); 217 this.refreshSystemAccountList(); 218 setTimeout(this.startLockScreenAbility(), 500); 219 }); 220 } 221 222 /** 223 * Remove system account by local id. 224 * 225 * @param localId local id of this system account, if not set, set it current local id. 226 */ 227 async removeAccount(localId?: number, callback?: () => void): Promise<void> { 228 let removeId = localId ? localId : this.currentAccount.localId; 229 LogUtil.info("Remove system account, local Id: " + removeId); 230 osAccount.getAccountManager().removeOsAccount(removeId).then(() => { 231 this.refreshSystemAccountList(); 232 callback(); 233 }); 234 } 235 236 private startLockScreenAbility() { 237 var abilityParam = { 238 "bundleName": "com.ohos.screenlock", 239 "abilityName": "com.ohos.screenlock.MainAbility", 240 "abilityStartSetting": {} 241 }; 242 let context = GlobalContext.getContext().getObject(GlobalContext.globalKeySettingsAbilityContext) as common.UIAbilityContext; 243 context.startAbility(abilityParam) 244 .then((data) => { 245 LogUtil.info('Start lockscreen successful. Data: ' + JSON.stringify(data)) 246 }).catch((error) => { 247 LogUtil.error('Start lockscreen failed. Cause: ' + JSON.stringify(error)); 248 }) 249 } 250} 251 252let systemAccountController = SystemAccountController.getInstance(); 253 254export default systemAccountController as SystemAccountController; 255