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, BusinessError} from '@ohos.base';
176e80583aSopenharmony_ciimport commonEventMgr from '@ohos.commonEventManager';
186e80583aSopenharmony_ciimport { EventConstants } from '../constants/EventConstants';
196e80583aSopenharmony_ciimport { localEventManager } from './LocalEventManager';
206e80583aSopenharmony_ciimport commonEventManager from './CommonEventManager';
216e80583aSopenharmony_ciimport { Log } from '../utils/Log';
226e80583aSopenharmony_ci
236e80583aSopenharmony_ciconst TAG = 'NavigationBarCommonEventManager';
246e80583aSopenharmony_ci
256e80583aSopenharmony_ci/**
266e80583aSopenharmony_ci * Wrapper class for NavigationBarCommonEvent interfaces.
276e80583aSopenharmony_ci */
286e80583aSopenharmony_ciclass NavigationBarCommonEventManager {
296e80583aSopenharmony_ci  private static NAVIGATION_BAR_HIDE = 'systemui.event.NAVIGATIONBAR_HIDE';
306e80583aSopenharmony_ci  private static subscriber: commonEventMgr.CommonEventSubscriber;
316e80583aSopenharmony_ci  private static eventCallback: AsyncCallback<commonEventMgr.CommonEventData>;
326e80583aSopenharmony_ci
336e80583aSopenharmony_ci  /**
346e80583aSopenharmony_ci   * get NavigationBarCommonEvent instance
356e80583aSopenharmony_ci   *
366e80583aSopenharmony_ci   * @return NavigationBarCommonEvent singleton
376e80583aSopenharmony_ci   */
386e80583aSopenharmony_ci  static getInstance(): NavigationBarCommonEventManager {
396e80583aSopenharmony_ci    if (globalThis.NavigationBarCommonEvent == null) {
406e80583aSopenharmony_ci      globalThis.NavigationBarCommonEvent = new NavigationBarCommonEventManager();
416e80583aSopenharmony_ci      this.eventCallback = this.navigationBarEventCallback.bind(this);
426e80583aSopenharmony_ci      this.initSubscriber();
436e80583aSopenharmony_ci    }
446e80583aSopenharmony_ci    return globalThis.NavigationBarCommonEvent;
456e80583aSopenharmony_ci  }
466e80583aSopenharmony_ci
476e80583aSopenharmony_ci  private static initSubscriber() {
486e80583aSopenharmony_ci    if (NavigationBarCommonEventManager.subscriber != null) {
496e80583aSopenharmony_ci      return;
506e80583aSopenharmony_ci    }
516e80583aSopenharmony_ci    const subscribeInfo: commonEventMgr.CommonEventSubscribeInfo = {
526e80583aSopenharmony_ci      events: [NavigationBarCommonEventManager.NAVIGATION_BAR_HIDE]
536e80583aSopenharmony_ci    };
546e80583aSopenharmony_ci    commonEventMgr.createSubscriber(subscribeInfo).then(
556e80583aSopenharmony_ci      (commonEventSubscriber: commonEventMgr.CommonEventSubscriber) => {
566e80583aSopenharmony_ci        Log.showDebug(TAG, 'init SPLIT_SCREEN subscriber success');
576e80583aSopenharmony_ci        NavigationBarCommonEventManager.subscriber = commonEventSubscriber;
586e80583aSopenharmony_ci      }, (err) => {
596e80583aSopenharmony_ci      Log.showError(TAG, `Failed to createSubscriber ${err}`);
606e80583aSopenharmony_ci    })
616e80583aSopenharmony_ci  }
626e80583aSopenharmony_ci
636e80583aSopenharmony_ci  /**
646e80583aSopenharmony_ci   * Register navigationBar event listener.
656e80583aSopenharmony_ci   */
666e80583aSopenharmony_ci  public registerNavigationBarEvent() {
676e80583aSopenharmony_ci    commonEventManager.registerCommonEvent(NavigationBarCommonEventManager.subscriber,
686e80583aSopenharmony_ci      NavigationBarCommonEventManager.eventCallback);
696e80583aSopenharmony_ci  }
706e80583aSopenharmony_ci
716e80583aSopenharmony_ci  /**
726e80583aSopenharmony_ci   * Unregister navigationBar event listener.
736e80583aSopenharmony_ci   */
746e80583aSopenharmony_ci  public unregisterNavigationBarEvent() {
756e80583aSopenharmony_ci    commonEventManager.unregisterCommonEvent(NavigationBarCommonEventManager.subscriber,
766e80583aSopenharmony_ci      NavigationBarCommonEventManager.eventCallback);
776e80583aSopenharmony_ci  }
786e80583aSopenharmony_ci
796e80583aSopenharmony_ci  /**
806e80583aSopenharmony_ci   * navigationBar event handler.
816e80583aSopenharmony_ci   */
826e80583aSopenharmony_ci  private static async navigationBarEventCallback(error: BusinessError, data: commonEventMgr.CommonEventData) {
836e80583aSopenharmony_ci    Log.showDebug(TAG,`navigationBarEventCallback receive data: ${JSON.stringify(data)}.`);
846e80583aSopenharmony_ci    if (data.code !== 0) {
856e80583aSopenharmony_ci      Log.showError(TAG, `navigationBarEventCallback error: ${JSON.stringify(error)}`);
866e80583aSopenharmony_ci      return;
876e80583aSopenharmony_ci    }
886e80583aSopenharmony_ci    switch (data.event) {
896e80583aSopenharmony_ci      case NavigationBarCommonEventManager.NAVIGATION_BAR_HIDE:
906e80583aSopenharmony_ci        setTimeout(() => {
916e80583aSopenharmony_ci          localEventManager.sendLocalEventSticky(EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE, '0');
926e80583aSopenharmony_ci        }, 30)
936e80583aSopenharmony_ci      default:
946e80583aSopenharmony_ci        break;
956e80583aSopenharmony_ci    }
966e80583aSopenharmony_ci  }
976e80583aSopenharmony_ci}
986e80583aSopenharmony_ci
996e80583aSopenharmony_ciexport const navigationBarCommonEventManager = NavigationBarCommonEventManager.getInstance();