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