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 */ 15 16import { AsyncCallback } from '@ohos.base'; 17import commonEventMgr from '@ohos.commonEventManager'; 18 19const TAG = 'CommonEventManager'; 20 21/** 22 * Wrapper class for CommonEvent. 23 */ 24class CommonEventManager { 25 RECENT_FULL_SCREEN = 'CREATE_RECENT_WINDOW_EVENT'; 26 RECENT_SPLIT_SCREEN = 'common.event.SPLIT_SCREEN'; 27 28 private callbackList: AsyncCallback<commonEventMgr.CommonEventData>[] = []; 29 private subscriberList: commonEventMgr.CommonEventSubscriber[] = []; 30 31 /** 32 * get CommonEventManager instance 33 * 34 * @return CommonEventManager singleton 35 */ 36 static getInstance(): CommonEventManager { 37 if (globalThis.CommonEventManager == null) { 38 globalThis.CommonEventManager = new CommonEventManager(); 39 } 40 return globalThis.CommonEventManager; 41 } 42 43 private constructor() { 44 } 45 46 /** 47 * Register common event listener. 48 */ 49 public registerCommonEvent(subscriber: commonEventMgr.CommonEventSubscriber, 50 eventCallback: AsyncCallback<commonEventMgr.CommonEventData>): void { 51 if (this.subscriberList.indexOf(subscriber) !== -1) { 52 return; 53 } 54 commonEventMgr.subscribe(subscriber, eventCallback); 55 this.subscriberList.push(subscriber); 56 this.callbackList.push(eventCallback); 57 } 58 59 /** 60 * Unregister common event listener. 61 */ 62 public unregisterCommonEvent(subscriber: commonEventMgr.CommonEventSubscriber, 63 eventCallback: AsyncCallback<commonEventMgr.CommonEventData>): void { 64 const subscriberIndex: number = this.subscriberList.indexOf(subscriber); 65 const callbackIndex: number = this.callbackList.indexOf(eventCallback); 66 if (subscriberIndex !== -1) { 67 commonEventMgr.unsubscribe(subscriber); 68 this.subscriberList.splice(subscriberIndex, 1); 69 } 70 callbackIndex !== -1 && this.callbackList.splice(callbackIndex, 1); 71 } 72} 73 74const commonEventManager = CommonEventManager.getInstance(); 75export default commonEventManager; 76