16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport { Log } from '../utils/Log';
176e80583aSopenharmony_ciimport { RdbStoreManager } from './RdbStoreManager';
186e80583aSopenharmony_ciimport { localEventManager } from './LocalEventManager';
196e80583aSopenharmony_ciimport { launcherAbilityManager } from './LauncherAbilityManager';
206e80583aSopenharmony_ciimport BadgeItemInfo from '../bean/BadgeItemInfo';
216e80583aSopenharmony_ciimport { EventConstants } from '../constants/EventConstants';
226e80583aSopenharmony_ci
236e80583aSopenharmony_ciconst TAG = 'BadgeManager';
246e80583aSopenharmony_ci
256e80583aSopenharmony_ci/**
266e80583aSopenharmony_ci * badge manager
276e80583aSopenharmony_ci */
286e80583aSopenharmony_ciexport class BadgeManager {
296e80583aSopenharmony_ci  private readonly mDbStoreManager: RdbStoreManager;
306e80583aSopenharmony_ci
316e80583aSopenharmony_ci  static UPDATE_BADGE = 'updateBadge';
326e80583aSopenharmony_ci
336e80583aSopenharmony_ci  private readonly listener;
346e80583aSopenharmony_ci
356e80583aSopenharmony_ci  private constructor() {
366e80583aSopenharmony_ci    this.mDbStoreManager = RdbStoreManager.getInstance();
376e80583aSopenharmony_ci    this.listener = this.appRemovedCallBack.bind(this);
386e80583aSopenharmony_ci    this.registerAppListEvent();
396e80583aSopenharmony_ci  }
406e80583aSopenharmony_ci  /**
416e80583aSopenharmony_ci   * badge manager instance
426e80583aSopenharmony_ci   *
436e80583aSopenharmony_ci   * @return badgeManager instance
446e80583aSopenharmony_ci   */
456e80583aSopenharmony_ci  static getInstance(): BadgeManager {
466e80583aSopenharmony_ci    if (globalThis.BadgeManagerInstance == null) {
476e80583aSopenharmony_ci      globalThis.BadgeManagerInstance = new BadgeManager();
486e80583aSopenharmony_ci    }
496e80583aSopenharmony_ci    return globalThis.BadgeManagerInstance;
506e80583aSopenharmony_ci  }
516e80583aSopenharmony_ci
526e80583aSopenharmony_ci  async getAllBadge(): Promise<BadgeItemInfo[]> {
536e80583aSopenharmony_ci    const badgeList = await this.mDbStoreManager.getAllBadge();
546e80583aSopenharmony_ci    return badgeList;
556e80583aSopenharmony_ci  }
566e80583aSopenharmony_ci
576e80583aSopenharmony_ci  async getBadgeByBundle(bundleName: string, callback: ((badgeNumber: number) => void)): Promise<void> {
586e80583aSopenharmony_ci    const badgeList = await this.mDbStoreManager.getBadgeByBundle(bundleName);
596e80583aSopenharmony_ci    if (badgeList.length > 0) {
606e80583aSopenharmony_ci      callback(badgeList[0].badgeNumber);
616e80583aSopenharmony_ci    } else {
626e80583aSopenharmony_ci      callback(0);
636e80583aSopenharmony_ci    }
646e80583aSopenharmony_ci  }
656e80583aSopenharmony_ci
666e80583aSopenharmony_ci  async getBadgeByBundleSync(bundleName: string): Promise<number> {
676e80583aSopenharmony_ci    const badgeList = await this.mDbStoreManager.getBadgeByBundle(bundleName);
686e80583aSopenharmony_ci    if (badgeList.length > 0) {
696e80583aSopenharmony_ci      return badgeList[0].badgeNumber;
706e80583aSopenharmony_ci    } else {
716e80583aSopenharmony_ci      return 0;
726e80583aSopenharmony_ci    }
736e80583aSopenharmony_ci  }
746e80583aSopenharmony_ci
756e80583aSopenharmony_ci  async updateBadgeNumber(bundleName: string, badgeNum: number): Promise<boolean> {
766e80583aSopenharmony_ci    Log.showDebug(TAG, `updateBadgeNumber, bundleName:${bundleName}, badgeNum:${badgeNum}`);
776e80583aSopenharmony_ci    let result = false;
786e80583aSopenharmony_ci    if (badgeNum < 0 || this.ifStringIsNull(bundleName)) {
796e80583aSopenharmony_ci      return result;
806e80583aSopenharmony_ci    }
816e80583aSopenharmony_ci
826e80583aSopenharmony_ci    result = await this.mDbStoreManager.updateBadgeByBundle(bundleName, badgeNum);
836e80583aSopenharmony_ci    if (result) {
846e80583aSopenharmony_ci      const badgeInfo: BadgeItemInfo = new BadgeItemInfo();
856e80583aSopenharmony_ci      badgeInfo.bundleName = bundleName;
866e80583aSopenharmony_ci      badgeInfo.badgeNumber = badgeNum;
876e80583aSopenharmony_ci      localEventManager.sendLocalEventSticky(EventConstants.EVENT_BADGE_UPDATE, badgeInfo);
886e80583aSopenharmony_ci    }
896e80583aSopenharmony_ci    return result;
906e80583aSopenharmony_ci  }
916e80583aSopenharmony_ci
926e80583aSopenharmony_ci  /**
936e80583aSopenharmony_ci   * register app listener.
946e80583aSopenharmony_ci   */
956e80583aSopenharmony_ci  registerAppListEvent(): void {
966e80583aSopenharmony_ci    launcherAbilityManager.registerLauncherAbilityChangeListener(this.listener);
976e80583aSopenharmony_ci  }
986e80583aSopenharmony_ci
996e80583aSopenharmony_ci  /**
1006e80583aSopenharmony_ci   * unregister app listener.
1016e80583aSopenharmony_ci   */
1026e80583aSopenharmony_ci  unregisterAppListEvent(): void {
1036e80583aSopenharmony_ci    launcherAbilityManager.unregisterLauncherAbilityChangeListener(this.listener);
1046e80583aSopenharmony_ci  }
1056e80583aSopenharmony_ci
1066e80583aSopenharmony_ci  private async appRemovedCallBack(event, bundleName: string, userId): Promise<void> {
1076e80583aSopenharmony_ci    Log.showDebug(TAG, 'Launcher AppModel installationSubscriberCallBack event = ' + event);
1086e80583aSopenharmony_ci    if (event == EventConstants.EVENT_PACKAGE_REMOVED) {
1096e80583aSopenharmony_ci      this.mDbStoreManager.deleteBadgeByBundle(bundleName);
1106e80583aSopenharmony_ci    }
1116e80583aSopenharmony_ci  }
1126e80583aSopenharmony_ci
1136e80583aSopenharmony_ci  private ifStringIsNull(str: string | null | undefined): boolean {
1146e80583aSopenharmony_ci    if (str == undefined || str == '' || str == null) {
1156e80583aSopenharmony_ci      return true;
1166e80583aSopenharmony_ci    }
1176e80583aSopenharmony_ci    return false;
1186e80583aSopenharmony_ci  }
1196e80583aSopenharmony_ci}