19b256929Sopenharmony_ci/*
29b256929Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49b256929Sopenharmony_ci * you may not use this file except in compliance with the License.
59b256929Sopenharmony_ci * You may obtain a copy of the License at
69b256929Sopenharmony_ci *
79b256929Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89b256929Sopenharmony_ci *
99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129b256929Sopenharmony_ci * See the License for the specific language governing permissions and
139b256929Sopenharmony_ci * limitations under the License.
149b256929Sopenharmony_ci */
159b256929Sopenharmony_ci
169b256929Sopenharmony_ciimport AccountManager from "@ohos.account.osAccount";
179b256929Sopenharmony_ciimport {Log} from "./Log";
189b256929Sopenharmony_ciimport {createOrGet} from "./SingleInstanceHelper";
199b256929Sopenharmony_ci
209b256929Sopenharmony_ciconst TAG = "SwitchUserManagerSc";
219b256929Sopenharmony_ciconst SUBSCRIBE_KEY = "SystemUiAccount";
229b256929Sopenharmony_ciconst USER_CHANGE_EVENT = "activate";
239b256929Sopenharmony_ciconst DELAY_TIME = 50 * 1000;
249b256929Sopenharmony_ciexport const INVALID_USER_ID = -1;
259b256929Sopenharmony_ci
269b256929Sopenharmony_citype AccountInfo = {
279b256929Sopenharmony_ci  localId: number;
289b256929Sopenharmony_ci  localName: string;
299b256929Sopenharmony_ci  photo: string;
309b256929Sopenharmony_ci};
319b256929Sopenharmony_ciexport type UserChangeListener = {
329b256929Sopenharmony_ci  userChange: (data: UserInfo) => void;
339b256929Sopenharmony_ci};
349b256929Sopenharmony_ci
359b256929Sopenharmony_ciexport class UserInfo {
369b256929Sopenharmony_ci  userId: number = INVALID_USER_ID;
379b256929Sopenharmony_ci  userName: string = "";
389b256929Sopenharmony_ci  userIcon: string | Resource = "";
399b256929Sopenharmony_ci  [key: string]: any;
409b256929Sopenharmony_ci}
419b256929Sopenharmony_ci
429b256929Sopenharmony_ciasync function getCurrentAccountInfo(): Promise<AccountInfo> {
439b256929Sopenharmony_ci  let accountInfos = await AccountManager.getAccountManager().queryAllCreatedOsAccounts();
449b256929Sopenharmony_ci  Log.showInfo(TAG, `accountInfos size:${accountInfos.length}`);
459b256929Sopenharmony_ci  for (let accountInfo of accountInfos) {
469b256929Sopenharmony_ci    Log.showDebug(TAG, `accountInfo: ${accountInfo.localId}, isActive: ${accountInfo.isActived}`);
479b256929Sopenharmony_ci    if (accountInfo.isActived) {
489b256929Sopenharmony_ci      return accountInfo;
499b256929Sopenharmony_ci    }
509b256929Sopenharmony_ci  }
519b256929Sopenharmony_ci  return Promise.reject("Can't get active userInfo.");
529b256929Sopenharmony_ci}
539b256929Sopenharmony_ci
549b256929Sopenharmony_cifunction parseAccountInfo(accountInfo: AccountInfo): UserInfo {
559b256929Sopenharmony_ci  return {
569b256929Sopenharmony_ci    userId: accountInfo.localId,
579b256929Sopenharmony_ci    userName: accountInfo.localName,
589b256929Sopenharmony_ci    userIcon: accountInfo.photo,
599b256929Sopenharmony_ci  };
609b256929Sopenharmony_ci}
619b256929Sopenharmony_ci
629b256929Sopenharmony_ciexport class SwitchUserManager {
639b256929Sopenharmony_ci  mUserInfo: UserInfo = new UserInfo();
649b256929Sopenharmony_ci  mListeners = new Set<UserChangeListener>();
659b256929Sopenharmony_ci  mHasWait: boolean = false;
669b256929Sopenharmony_ci
679b256929Sopenharmony_ci  static getInstance(): SwitchUserManager {
689b256929Sopenharmony_ci    return createOrGet(SwitchUserManager, TAG);
699b256929Sopenharmony_ci  }
709b256929Sopenharmony_ci
719b256929Sopenharmony_ci  constructor() {
729b256929Sopenharmony_ci    Log.showDebug(TAG, `SwitchUserManager constructor`);
739b256929Sopenharmony_ci    AccountManager.getAccountManager().on(USER_CHANGE_EVENT, SUBSCRIBE_KEY, this.handleUserChange.bind(this));
749b256929Sopenharmony_ci  }
759b256929Sopenharmony_ci
769b256929Sopenharmony_ci  public async getCurrentUserInfo(): Promise<UserInfo> {
779b256929Sopenharmony_ci    if (this.mUserInfo.userId == INVALID_USER_ID) {
789b256929Sopenharmony_ci      !this.mHasWait && (await new Promise((resolve) => setTimeout(resolve, DELAY_TIME)));
799b256929Sopenharmony_ci      this.mHasWait = true;
809b256929Sopenharmony_ci      this.mUserInfo = parseAccountInfo(await getCurrentAccountInfo());
819b256929Sopenharmony_ci    }
829b256929Sopenharmony_ci    Log.showInfo(TAG, `getCurrentUserInfo userId: ${this.mUserInfo.userId}`);
839b256929Sopenharmony_ci    return this.mUserInfo;
849b256929Sopenharmony_ci  }
859b256929Sopenharmony_ci
869b256929Sopenharmony_ci  public registerListener(listener: UserChangeListener) {
879b256929Sopenharmony_ci    this.mListeners.add(listener);
889b256929Sopenharmony_ci  }
899b256929Sopenharmony_ci
909b256929Sopenharmony_ci  public unregisterListener(listener: UserChangeListener) {
919b256929Sopenharmony_ci    this.mListeners.delete(listener);
929b256929Sopenharmony_ci  }
939b256929Sopenharmony_ci
949b256929Sopenharmony_ci  handleUserChange(accountId: number): void {
959b256929Sopenharmony_ci    AccountManager.getAccountManager()
969b256929Sopenharmony_ci      .queryOsAccountById(accountId)
979b256929Sopenharmony_ci      .then((accountInfo) => {
989b256929Sopenharmony_ci        Log.showInfo(TAG, `userChange, accountInfo: ${JSON.stringify(accountInfo)}`);
999b256929Sopenharmony_ci        this.mUserInfo = parseAccountInfo(accountInfo);
1009b256929Sopenharmony_ci        this.notifyUserChange();
1019b256929Sopenharmony_ci      })
1029b256929Sopenharmony_ci      .catch((err) => Log.showError(TAG, `Can't query account by ${accountId}, err: ${err}`));
1039b256929Sopenharmony_ci  }
1049b256929Sopenharmony_ci
1059b256929Sopenharmony_ci  notifyUserChange() {
1069b256929Sopenharmony_ci    this.mListeners.forEach((listener) => listener.userChange(this.mUserInfo));
1079b256929Sopenharmony_ci  }
1089b256929Sopenharmony_ci}