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