1/** 2 * @file Describe the file 3 * Copyright (c) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import commonEvent from '@ohos.commonEvent' 18 19import { Subscriber } from '@ohos/common/src/main/ets/subscriber/Subscriber' 20import { Log } from '@ohos/common/src/main/ets/utils/Log'; 21 22import { runScheduleNextAlarm } from './AlertsProcessor' 23import { CommonEventConstants } from '../../commonevents/CommonEventConstants'; 24 25const TAG = 'AlertsSubscriber'; 26 27/** 28 * the Alerts Subscriber 29 * 30 * @since 2022-10-08 31 */ 32export class AlertsSubscriber implements Subscriber { 33 // AlertsSubscriber 的单例 34 private static sInstance: AlertsSubscriber; 35 36 // AlertsSubscriber 持有的订阅者 37 private subscriber; 38 39 private constructor() { 40 this.subscriber; 41 } 42 43 /** 44 * 获取 AlertsSubscriber 的单例 45 */ 46 public static getInstance() { 47 if (!AlertsSubscriber.sInstance) { 48 Log.log(TAG, 'call getInstance init'); 49 AlertsSubscriber.sInstance = new AlertsSubscriber(); 50 } 51 return AlertsSubscriber.sInstance; 52 } 53 54 /** 55 * 初始化 AlertsSubscriber 持有的 subscriber 56 */ 57 public initSubscriber() { 58 if (this.subscriber === null || this.subscriber === undefined) { 59 AlertsSubscriber.sInstance.subscribe(); 60 } 61 } 62 63 /** 64 * 订阅公共事件 65 */ 66 subscribe() { 67 // 订阅的公共事件集合 68 var subscribeInfo = { 69 events: [CommonEventConstants.CHECK_NEXT_ALARM, CommonEventConstants.EVENT_REMINDER], 70 } 71 72 // 创建订阅对象 73 commonEvent.createSubscriber(subscribeInfo, (err, subscriber) => { 74 if (err?.code) { 75 Log.error(TAG, `CreateSubscriberCallBack err = ${JSON.stringify(err)}`); 76 } else { 77 this.subscriber = subscriber; 78 Log.info(TAG, "Create subscriber succeed"); 79 if (this.subscriber != null) { 80 Log.info(TAG, "subscriber subscribe commonEvent"); 81 commonEvent.subscribe(subscriber, (err, data) => { 82 if (err?.code) { 83 Log.error(TAG, `SubscribeCallBack err= ${JSON.stringify(err)}`); 84 } else { 85 Log.info(TAG, `SubscribeCallBack data= ${JSON.stringify(data)}`); 86 // 接到广播后,此处会被回调 87 runScheduleNextAlarm(); 88 } 89 }) 90 } else { 91 Log.info(TAG, "Need create subscriber"); 92 } 93 } 94 }) 95 } 96 97 /** 98 * 取消订阅公共事件 99 */ 100 unSubscribe() { 101 if (this.subscriber != null) { 102 commonEvent.unsubscribe(this.subscriber, (err) => { 103 if (err?.code) { 104 Log.error(TAG, `UnsubscribeCallBack err= = ${JSON.stringify(err)}`); 105 } else { 106 Log.info(TAG, "Unsubscribe"); 107 this.subscriber = null 108 Log.info(TAG, "Unsubscribe succeed"); 109 } 110 }) 111 } 112 } 113} 114