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