• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/**
3 * Copyright (c) 2021 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 */
16import osAccount from '@ohos.account.osAccount';
17import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
18
19export const MAX_LENGTH: number = 30;
20export const MAX_ACCOUNT: number = 5;
21
22export class SystemAccoutModel {
23  /**
24   * Get current account name and do callback.
25   *
26   * @param callback related callback when current system account is obtained.
27   */
28  updateAccountName(callback: (name: string) => void) {
29    LogUtil.info("Update account name.");
30    osAccount.getAccountManager().queryAllCreatedOsAccounts().then(list => {
31      let info = this.getCurrentAccount(list);
32      LogUtil.info("Current os name: " + info.localName);
33      callback(info.localName);
34    });
35  }
36
37  /**
38   * Get current account name from created account list.
39   *
40   * @param accountList created system account list.
41   */
42  getCurrentAccount(accountList: any[]): any {
43    for (let index = 0; index < accountList.length; index++) {
44      if (accountList[index].isActived) {
45        return accountList[index];
46      }
47    }
48    return {};
49  }
50
51  /**
52   * Sort the account info list according to following rules:
53   * 1. If this account is current user, then put it first.
54   * 2. If this administrator, then place it after the current user.
55   * 2. If this account is quest user, then put it last.
56   * 3. If this account is normal user, place it in chronological order of creation.
57   *
58   * @param info1 input param 1.
59   * @param info2 input param 2.
60   */
61  sortAccount(info1: any, info2: any): number {
62    if (info1.localId == this.currentAccount.localId || info2.localId == this.currentAccount.localId) {
63      LogUtil.info("Sort current account, info: " + info1.localName);
64      return info1.localId == this.currentAccount.localId ? -1 : 1;
65    } else if (info1.type == osAccount.OsAccountType.ADMIN || info2.type == osAccount.OsAccountType.ADMIN) {
66      LogUtil.info("Sort administrator account, info: " + info1.localName);
67      return info1.type == osAccount.OsAccountType.ADMIN ? -1 : 1;
68    } else if ( info1.type == osAccount.OsAccountType.GUEST || info2.type == osAccount.OsAccountType.GUEST) {
69      LogUtil.info("Sort quest account, info: " + info1.localName);
70      return info1.type == osAccount.OsAccountType.GUEST ? 1 : -1;
71    } else {
72      return info2.localId - info1.localId;
73    }
74  }
75
76  /**
77   * Get identity from account info type.
78   *
79   * @param type int type, like admin, quest.
80   * @return identity string of different account type.
81   */
82  getIdentityFromMap(accountType: any): string | Resource {
83    LogUtil.info("Get identity from map, type: " + JSON.stringify(accountType));
84    if (accountType == osAccount.OsAccountType.ADMIN) {
85      return $r("app.string.administrator");
86    } else if (accountType == osAccount.OsAccountType.NORMAL) {
87      return "";
88    } else if (accountType == osAccount.OsAccountType.GUEST) {
89      return $r("app.string.quest");
90    } else {
91      LogUtil.info("Unknown system account type.")
92      return "未知用户";
93    }
94  }
95}
96
97let systemAccountModel = new SystemAccoutModel();
98export default systemAccountModel as SystemAccoutModel;
99