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