• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import Log from '../../../../../../../../common/src/main/ets/default/Log';
16import Notification from '@ohos.notification';
17import NotificationManager from '@ohos.notificationManager';
18import NotificationSubscribe from '@ohos.notificationSubscribe';
19import type { NotificationSubscriber } from 'notification/notificationSubscriber';
20
21const TAG = 'NotificationManagenment-NotificationListener';
22
23interface IOnEnableChanged {
24    (value: boolean): void;
25}
26
27interface EnableListener {
28    bundle: string;
29    onEnableChanged: IOnEnableChanged;
30}
31
32export interface BundleOption {
33    bundle: string;
34    uid?: number;
35}
36
37export class NotificationListener {
38    private readonly listeners = new Map<string, Set<IOnEnableChanged>>();
39    private subscriber: NotificationSubscriber = {
40        onEnabledNotificationChanged: this.handleEnabledNotificationChanged.bind(this)
41    };
42
43    subscribeEnableChanged(): void {
44        Log.showDebug(TAG, `subscribeEnableChanged ${this.subscriber}`);
45
46        NotificationSubscribe.subscribe(this.subscriber, err => {
47            if (err) {
48                Log.showInfo(TAG, `subscribeEnableChanged error ${JSON.stringify(err)}`);
49            } else {
50                Log.showInfo(TAG, 'subscribeEnableChanged finished');
51            }
52        });
53    }
54
55    unsubscribeEnableChanged(): void {
56        Log.showDebug(TAG, `unsubscribeEnableChanged start  ${this.subscriber}`);
57        this.unRegisterAll();
58
59        NotificationSubscribe.unsubscribe(this.subscriber, err => {
60            if (err) {
61                Log.showInfo(TAG, `unsubscribeEnableChanged error ${JSON.stringify(err)}`);
62            } else {
63                Log.showInfo(TAG, 'unsubscribeEnableChanged finished');
64            }
65        });
66    }
67
68    handleEnabledNotificationChanged(data: NotificationSubscribe.EnabledNotificationCallbackData): void {
69        Log.showDebug(TAG, `handleEnabledNotificationChanged data:${JSON.stringify(data)} `);
70
71        const callbacks = this.listeners.get(data.bundle);
72        if (callbacks && callbacks.size) {
73            callbacks.forEach(cb => cb(data.enable));
74        }
75    }
76
77    register(listener: EnableListener): void {
78        let callbacks: Set<IOnEnableChanged> = this.listeners.get(listener.bundle);
79        if (!callbacks) {
80            callbacks = new Set();
81            this.listeners.set(listener.bundle, callbacks);
82        }
83
84        callbacks.add(listener.onEnableChanged);
85        Log.showDebug(TAG, 'register finished');
86    }
87
88    unRegister(listener: EnableListener): void {
89        const callbacks = this.listeners.get(listener.bundle);
90        if (!callbacks) {
91            Log.showDebug(TAG, 'unRegister finished by empty');
92            return;
93        }
94
95        callbacks.delete(listener.onEnableChanged);
96        Log.showDebug(TAG, 'unRegister finished');
97    }
98
99    unRegisterAll(): void {
100        this.listeners.clear();
101        Log.showDebug(TAG, 'unRegisterAll finished');
102    }
103
104    async isNotificationEnabled(bundleOption: BundleOption, callback?: (data) => void): Promise<boolean> {
105        Log.showDebug(TAG, `isNotificationEnabled bundleOption:${JSON.stringify(bundleOption)} `);
106        return new Promise((resolve, reject) => {
107          NotificationManager.isNotificationEnabled(bundleOption, (err, data) => {
108                Log.showInfo(TAG, `isNotificationEnabled callback data:${JSON.stringify(data)} err:${JSON.stringify(err)}`);
109                if (!!data) {
110                    if (callback) {
111                        callback(data);
112                    }
113                    resolve(data);
114                } else {
115                    reject(err);
116                }
117            });
118        });
119    }
120
121    enableNotification(bundleOption: BundleOption, data: boolean): void {
122        Log.showDebug(TAG, `enableNotification bundleOption:${JSON.stringify(bundleOption)} data:${JSON.stringify(data)}`);
123        let enableValue: boolean = data ? true : false;
124        NotificationManager.setNotificationEnable(bundleOption, enableValue, (err, result) => {
125            Log.showInfo(TAG, `enableNotification err:${JSON.stringify(err)} result:${JSON.stringify(result)}`);
126        });
127    }
128
129    async isNotificationSlotEnabled(bundleOption: BundleOption, slotType: Notification.SlotType, callback?: (data) => void): Promise<boolean> {
130        Log.showDebug(TAG, `isNotificationSlotEnabled bundleOption:${JSON.stringify(bundleOption)} `);
131        return new Promise((resolve, reject) => {
132            NotificationManager.isNotificationSlotEnabled(bundleOption, slotType, (err, data) => {
133                Log.showInfo(TAG, `isNotificationSlotEnabled callback data:${JSON.stringify(data)} err:${JSON.stringify(err)}`);
134                if (!!data) {
135                    if (callback) {
136                        callback(data);
137                    }
138                    resolve(data);
139                } else {
140                    reject(err);
141                }
142            });
143        });
144    }
145
146    enableNotificationSlot(bundleOption: BundleOption, slotType: Notification.SlotType, data: boolean): void {
147        Log.showDebug(TAG, `enableNotificationSlot bundleOption:${JSON.stringify(bundleOption)} data:${JSON.stringify(data)}`);
148        let enableValue: boolean = data ? true : false;
149        NotificationManager.setNotificationEnableSlot(bundleOption, slotType, enableValue, (err, result) => {
150            Log.showInfo(TAG, `enableNotificationSlot err:${JSON.stringify(err)} result:${JSON.stringify(result)}`);
151        });
152    }
153
154    notificationSlotSet(bundleOption: BundleOption, data: NotificationManager.NotificationSlot): void {
155        Log.showDebug(TAG, `notificationSlotSet bundleOption:${JSON.stringify(bundleOption)} data:${JSON.stringify(data)}`);
156        try {
157            NotificationManager.setSlotByBundle(bundleOption, data, (err, result) => {
158                Log.showInfo(TAG, `notificationSlotSet err:${JSON.stringify(err)} result:${JSON.stringify(result)}`);
159            });
160        } catch (err) {
161            Log.showError(TAG, `notificationSlotSet error => ${JSON.stringify(err)}`);
162        }
163    }
164
165    async isDistributedEnabled(): Promise<boolean> {
166        Log.showInfo(TAG, 'isDistributedEnabled');
167        return new Promise((resolve, reject) => {
168            NotificationManager.isDistributedEnabled().then((data) => {
169                Log.showInfo(TAG, `isDistributedEnabled data:${data ? 'true' : 'false'}`);
170                resolve(data);
171            }).catch((err) => {
172                Log.showError(TAG, `isDistributedEnabled err:${JSON.stringify(err)}`);
173                reject(err);
174            });
175        });
176    }
177
178    enableDistributed(data: boolean): void {
179        Log.showDebug(TAG, `enableDistributed data:${JSON.stringify(data)}`);
180        let enableValue: boolean = data ? true : false;
181        NotificationManager.setDistributedEnable(enableValue, (err, result) => {
182            Log.showInfo(TAG, `enableDistributed err:${JSON.stringify(err)} result:${JSON.stringify(result)}`);
183        });
184    }
185}
186
187let notificationListener = new NotificationListener();
188
189export default notificationListener;