16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport { AsyncCallback } from '@ohos.base';
176e80583aSopenharmony_ciimport commonEventMgr from '@ohos.commonEventManager';
186e80583aSopenharmony_ci
196e80583aSopenharmony_ciconst TAG = 'CommonEventManager';
206e80583aSopenharmony_ci
216e80583aSopenharmony_ci/**
226e80583aSopenharmony_ci * Wrapper class for CommonEvent.
236e80583aSopenharmony_ci */
246e80583aSopenharmony_ciclass CommonEventManager {
256e80583aSopenharmony_ci  RECENT_FULL_SCREEN = 'CREATE_RECENT_WINDOW_EVENT';
266e80583aSopenharmony_ci  RECENT_SPLIT_SCREEN = 'common.event.SPLIT_SCREEN';
276e80583aSopenharmony_ci
286e80583aSopenharmony_ci  private callbackList: AsyncCallback<commonEventMgr.CommonEventData>[] = [];
296e80583aSopenharmony_ci  private subscriberList: commonEventMgr.CommonEventSubscriber[] = [];
306e80583aSopenharmony_ci
316e80583aSopenharmony_ci  /**
326e80583aSopenharmony_ci   * get CommonEventManager instance
336e80583aSopenharmony_ci   *
346e80583aSopenharmony_ci   * @return CommonEventManager singleton
356e80583aSopenharmony_ci   */
366e80583aSopenharmony_ci  static getInstance(): CommonEventManager {
376e80583aSopenharmony_ci    if (globalThis.CommonEventManager == null) {
386e80583aSopenharmony_ci      globalThis.CommonEventManager = new CommonEventManager();
396e80583aSopenharmony_ci    }
406e80583aSopenharmony_ci    return globalThis.CommonEventManager;
416e80583aSopenharmony_ci  }
426e80583aSopenharmony_ci
436e80583aSopenharmony_ci  private constructor() {
446e80583aSopenharmony_ci  }
456e80583aSopenharmony_ci
466e80583aSopenharmony_ci  /**
476e80583aSopenharmony_ci   * Register common event listener.
486e80583aSopenharmony_ci   */
496e80583aSopenharmony_ci  public registerCommonEvent(subscriber: commonEventMgr.CommonEventSubscriber,
506e80583aSopenharmony_ci                             eventCallback: AsyncCallback<commonEventMgr.CommonEventData>): void {
516e80583aSopenharmony_ci    if (this.subscriberList.indexOf(subscriber) !== -1) {
526e80583aSopenharmony_ci      return;
536e80583aSopenharmony_ci    }
546e80583aSopenharmony_ci    commonEventMgr.subscribe(subscriber, eventCallback);
556e80583aSopenharmony_ci    this.subscriberList.push(subscriber);
566e80583aSopenharmony_ci    this.callbackList.push(eventCallback);
576e80583aSopenharmony_ci  }
586e80583aSopenharmony_ci
596e80583aSopenharmony_ci  /**
606e80583aSopenharmony_ci   * Unregister common event listener.
616e80583aSopenharmony_ci   */
626e80583aSopenharmony_ci  public unregisterCommonEvent(subscriber: commonEventMgr.CommonEventSubscriber,
636e80583aSopenharmony_ci                               eventCallback: AsyncCallback<commonEventMgr.CommonEventData>): void {
646e80583aSopenharmony_ci    const subscriberIndex: number = this.subscriberList.indexOf(subscriber);
656e80583aSopenharmony_ci    const callbackIndex: number = this.callbackList.indexOf(eventCallback);
666e80583aSopenharmony_ci    if (subscriberIndex !== -1) {
676e80583aSopenharmony_ci      commonEventMgr.unsubscribe(subscriber);
686e80583aSopenharmony_ci      this.subscriberList.splice(subscriberIndex, 1);
696e80583aSopenharmony_ci    }
706e80583aSopenharmony_ci    callbackIndex !== -1 && this.callbackList.splice(callbackIndex, 1);
716e80583aSopenharmony_ci  }
726e80583aSopenharmony_ci}
736e80583aSopenharmony_ci
746e80583aSopenharmony_ciconst commonEventManager = CommonEventManager.getInstance();
756e80583aSopenharmony_ciexport default commonEventManager;
76