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, BusinessError} from '@ohos.base'; 17import commonEventMgr from '@ohos.commonEventManager'; 18import { EventConstants } from '../constants/EventConstants'; 19import { localEventManager } from './LocalEventManager'; 20import commonEventManager from './CommonEventManager'; 21import { Log } from '../utils/Log'; 22 23const TAG = 'NavigationBarCommonEventManager'; 24 25/** 26 * Wrapper class for NavigationBarCommonEvent interfaces. 27 */ 28class NavigationBarCommonEventManager { 29 private static NAVIGATION_BAR_HIDE = 'systemui.event.NAVIGATIONBAR_HIDE'; 30 private static subscriber: commonEventMgr.CommonEventSubscriber; 31 private static eventCallback: AsyncCallback<commonEventMgr.CommonEventData>; 32 33 /** 34 * get NavigationBarCommonEvent instance 35 * 36 * @return NavigationBarCommonEvent singleton 37 */ 38 static getInstance(): NavigationBarCommonEventManager { 39 if (globalThis.NavigationBarCommonEvent == null) { 40 globalThis.NavigationBarCommonEvent = new NavigationBarCommonEventManager(); 41 this.eventCallback = this.navigationBarEventCallback.bind(this); 42 this.initSubscriber(); 43 } 44 return globalThis.NavigationBarCommonEvent; 45 } 46 47 private static initSubscriber() { 48 if (NavigationBarCommonEventManager.subscriber != null) { 49 return; 50 } 51 const subscribeInfo: commonEventMgr.CommonEventSubscribeInfo = { 52 events: [NavigationBarCommonEventManager.NAVIGATION_BAR_HIDE] 53 }; 54 commonEventMgr.createSubscriber(subscribeInfo).then( 55 (commonEventSubscriber: commonEventMgr.CommonEventSubscriber) => { 56 Log.showDebug(TAG, 'init SPLIT_SCREEN subscriber success'); 57 NavigationBarCommonEventManager.subscriber = commonEventSubscriber; 58 }, (err) => { 59 Log.showError(TAG, `Failed to createSubscriber ${err}`); 60 }) 61 } 62 63 /** 64 * Register navigationBar event listener. 65 */ 66 public registerNavigationBarEvent() { 67 commonEventManager.registerCommonEvent(NavigationBarCommonEventManager.subscriber, 68 NavigationBarCommonEventManager.eventCallback); 69 } 70 71 /** 72 * Unregister navigationBar event listener. 73 */ 74 public unregisterNavigationBarEvent() { 75 commonEventManager.unregisterCommonEvent(NavigationBarCommonEventManager.subscriber, 76 NavigationBarCommonEventManager.eventCallback); 77 } 78 79 /** 80 * navigationBar event handler. 81 */ 82 private static async navigationBarEventCallback(error: BusinessError, data: commonEventMgr.CommonEventData) { 83 Log.showDebug(TAG,`navigationBarEventCallback receive data: ${JSON.stringify(data)}.`); 84 if (data.code !== 0) { 85 Log.showError(TAG, `navigationBarEventCallback error: ${JSON.stringify(error)}`); 86 return; 87 } 88 switch (data.event) { 89 case NavigationBarCommonEventManager.NAVIGATION_BAR_HIDE: 90 setTimeout(() => { 91 localEventManager.sendLocalEventSticky(EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE, '0'); 92 }, 30) 93 default: 94 break; 95 } 96 } 97} 98 99export const navigationBarCommonEventManager = NavigationBarCommonEventManager.getInstance();