1/**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { Log } from '../utils/Log';
17import { RdbStoreManager } from './RdbStoreManager';
18import { localEventManager } from './LocalEventManager';
19import { launcherAbilityManager } from './LauncherAbilityManager';
20import BadgeItemInfo from '../bean/BadgeItemInfo';
21import { EventConstants } from '../constants/EventConstants';
22
23const TAG = 'BadgeManager';
24
25/**
26 * badge manager
27 */
28export class BadgeManager {
29  private readonly mDbStoreManager: RdbStoreManager;
30
31  static UPDATE_BADGE = 'updateBadge';
32
33  private readonly listener;
34
35  private constructor() {
36    this.mDbStoreManager = RdbStoreManager.getInstance();
37    this.listener = this.appRemovedCallBack.bind(this);
38    this.registerAppListEvent();
39  }
40  /**
41   * badge manager instance
42   *
43   * @return badgeManager instance
44   */
45  static getInstance(): BadgeManager {
46    if (globalThis.BadgeManagerInstance == null) {
47      globalThis.BadgeManagerInstance = new BadgeManager();
48    }
49    return globalThis.BadgeManagerInstance;
50  }
51
52  async getAllBadge(): Promise<BadgeItemInfo[]> {
53    const badgeList = await this.mDbStoreManager.getAllBadge();
54    return badgeList;
55  }
56
57  async getBadgeByBundle(bundleName: string, callback: ((badgeNumber: number) => void)): Promise<void> {
58    const badgeList = await this.mDbStoreManager.getBadgeByBundle(bundleName);
59    if (badgeList.length > 0) {
60      callback(badgeList[0].badgeNumber);
61    } else {
62      callback(0);
63    }
64  }
65
66  async getBadgeByBundleSync(bundleName: string): Promise<number> {
67    const badgeList = await this.mDbStoreManager.getBadgeByBundle(bundleName);
68    if (badgeList.length > 0) {
69      return badgeList[0].badgeNumber;
70    } else {
71      return 0;
72    }
73  }
74
75  async updateBadgeNumber(bundleName: string, badgeNum: number): Promise<boolean> {
76    Log.showDebug(TAG, `updateBadgeNumber, bundleName:${bundleName}, badgeNum:${badgeNum}`);
77    let result = false;
78    if (badgeNum < 0 || this.ifStringIsNull(bundleName)) {
79      return result;
80    }
81
82    result = await this.mDbStoreManager.updateBadgeByBundle(bundleName, badgeNum);
83    if (result) {
84      const badgeInfo: BadgeItemInfo = new BadgeItemInfo();
85      badgeInfo.bundleName = bundleName;
86      badgeInfo.badgeNumber = badgeNum;
87      localEventManager.sendLocalEventSticky(EventConstants.EVENT_BADGE_UPDATE, badgeInfo);
88    }
89    return result;
90  }
91
92  /**
93   * register app listener.
94   */
95  registerAppListEvent(): void {
96    launcherAbilityManager.registerLauncherAbilityChangeListener(this.listener);
97  }
98
99  /**
100   * unregister app listener.
101   */
102  unregisterAppListEvent(): void {
103    launcherAbilityManager.unregisterLauncherAbilityChangeListener(this.listener);
104  }
105
106  private async appRemovedCallBack(event, bundleName: string, userId): Promise<void> {
107    Log.showDebug(TAG, 'Launcher AppModel installationSubscriberCallBack event = ' + event);
108    if (event == EventConstants.EVENT_PACKAGE_REMOVED) {
109      this.mDbStoreManager.deleteBadgeByBundle(bundleName);
110    }
111  }
112
113  private ifStringIsNull(str: string | null | undefined): boolean {
114    if (str == undefined || str == '' || str == null) {
115      return true;
116    }
117    return false;
118  }
119}