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 commonEventManager from '@ohos.commonEventManager';
18import { Subscriber } from '@ohos/common/src/main/ets/subscriber/Subscriber'
19import { Log } from '@ohos/common/src/main/ets/utils/Log';
20import { runScheduleNextAlarm } from './AlertsProcessor'
21import { CommonEventConstants } from '../../commonevents/CommonEventConstants';
22import { BusinessError } from '@ohos.base';
23
24const TAG = 'AlertsSubscriber';
25
26/**
27 * the Alerts Subscriber
28 *
29 * @since 2022-10-08
30 */
31export class AlertsSubscriber implements Subscriber {
32  // AlertsSubscriber 的单例
33  private static sInstance: AlertsSubscriber;
34  // AlertsSubscriber 持有的订阅者
35  private subscriber: commonEventManager.CommonEventSubscriber | null = null;
36
37  private constructor() {
38    this.subscriber;
39  }
40
41  /**
42   * 获取 AlertsSubscriber 的单例
43   */
44  public static getInstance() {
45    if (!AlertsSubscriber.sInstance) {
46      Log.log(TAG, 'call getInstance init');
47      AlertsSubscriber.sInstance = new AlertsSubscriber();
48    }
49    return AlertsSubscriber.sInstance;
50  }
51
52  /**
53   * 初始化 AlertsSubscriber 持有的 subscriber
54   */
55  public initSubscriber() {
56    if (this.subscriber === null || this.subscriber === undefined) {
57      AlertsSubscriber.sInstance.subscribe();
58    }
59  }
60
61  /**
62   * 订阅公共事件
63   */
64  subscribe() {
65    // 订阅的公共事件集合
66
67    // 创建订阅对象
68    commonEventManager.createSubscriber(
69      { events: [CommonEventConstants.CHECK_NEXT_ALARM, CommonEventConstants.EVENT_REMINDER] }, (err, subscriber) => {
70      if (err?.code) {
71        Log.error(TAG, `CreateSubscriberCallBack err = ${JSON.stringify(err)}`);
72      } else {
73        this.subscriber = subscriber;
74        Log.info(TAG, "Create subscriber succeed");
75        if (this.subscriber != null) {
76          Log.info(TAG, "subscriber subscribe commonEvent");
77          commonEventManager.subscribe(subscriber, (err, data) => {
78            if (err?.code) {
79              Log.error(TAG, `SubscribeCallBack err= ${JSON.stringify(err)}`);
80            } else {
81              Log.info(TAG, `SubscribeCallBack data= ${JSON.stringify(data)}`);
82              // 接到广播后,此处会被回调
83              runScheduleNextAlarm();
84            }
85          })
86        } else {
87          Log.info(TAG, "Need create subscriber");
88        }
89      }
90    })
91  }
92
93  /**
94   * 取消订阅公共事件
95   */
96  unSubscribe() {
97    if (this.subscriber != null) {
98      commonEventManager.unsubscribe(this.subscriber, (err: BusinessError) => {
99        if (err?.code) {
100          Log.error(TAG, `UnsubscribeCallBack err= = ${JSON.stringify(err)}`);
101        } else {
102          Log.info(TAG, "Unsubscribe");
103          this.subscriber = null;
104          Log.info(TAG, "Unsubscribe succeed");
105        }
106      })
107    }
108  }
109}
110