• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 inputMonitor from '@ohos.multimodalInput.inputMonitor';
18import {
19  Log,
20  CommonConstants,
21  settingsDataManager,
22  localEventManager,
23  EventConstants
24} from '@ohos/common';
25import dataShare from '@ohos.data.dataShare';
26import GestureNavigationExecutors from './GestureNavigationExecutors';
27import display from '@ohos.display';
28
29const TAG = 'GestureNavigationManage';
30
31export class GestureNavigationManager {
32  private readonly uri: string | null = null;
33  private helper: dataShare.DataShareHelper;
34  private readonly sGestureNavigationExecutors: GestureNavigationExecutors = GestureNavigationExecutors.getInstance();
35  private touchEventCallback: inputMonitor.TouchEventReceiver | null = null;
36
37  private constructor() {
38    this.uri = settingsDataManager.getUri(CommonConstants.NAVIGATION_BAR_STATUS_KEY);
39    this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri);
40  }
41
42  private setValue(value: string) {
43    settingsDataManager.setValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, value);
44  }
45
46  private getValue() {
47    return settingsDataManager.getValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, '1');
48  }
49
50  /**
51   * Monitor data changes.
52   * @param callback
53   */
54  private registerListenForDataChanges(callback: AsyncCallback<void>) {
55    this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri);
56    Log.showInfo(TAG, "helper:" + this.helper +  "  registerListenForDataChanges uri:" + this.uri);
57    this.helper.on('dataChange', this.uri, callback);
58  }
59
60  initWindowSize(display: display.Display) {
61    if (globalThis.sGestureNavigationExecutors) {
62      globalThis.sGestureNavigationExecutors.setScreenWidth(display.width);
63      globalThis.sGestureNavigationExecutors.setScreenHeight(display.height);
64      this.touchEventCallback = globalThis.sGestureNavigationExecutors.touchEventCallback
65        .bind(globalThis.sGestureNavigationExecutors);
66      settingsDataManager.createDataShareHelper();
67    }
68  }
69
70  updateWindowSize(display: any) {
71    if (globalThis.sGestureNavigationExecutors) {
72      globalThis.sGestureNavigationExecutors.setScreenWidth(display.width);
73      globalThis.sGestureNavigationExecutors.setScreenHeight(display.height);
74    }
75  }
76
77  private getGestureNavigationStatus() {
78    Log.showDebug(TAG, 'getGestureNavigationStatus enter');
79    let gestureNavigationStatus = null;
80    try {
81      gestureNavigationStatus = this.getValue();
82      Log.showDebug(TAG, `getGestureNavigationStatus gestureNavigationStatus:  ${gestureNavigationStatus}`);
83      this.handleEventSwitches(gestureNavigationStatus);
84
85      // 初始化时保持弹窗的距离底部的位置和(打开/关闭)三键时的位置一致
86      AppStorage.setOrCreate('NavigationBarStatusValue', gestureNavigationStatus === '0' ? true : false);
87
88      this.registerListenForDataChanges(this.dataChangesCallback.bind(this));
89    } catch (error) {
90      Log.showError(TAG, `getGestureNavigationStatus error: ${JSON.stringify(error)}`);
91    }
92  }
93
94  private dataChangesCallback(data: any) {
95    Log.showInfo(TAG, "dataChangesCallback data:" + data);
96    const getRetValue = this.getValue();
97    this.handleEventSwitches(getRetValue);
98    AppStorage.setOrCreate('NavigationBarStatusValue', getRetValue === '0' ? true : false);
99    localEventManager.sendLocalEventSticky(EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE, getRetValue);
100  }
101
102  private turnOnTouchEventCallback() {
103    Log.showWarn(TAG, 'turnOnTouchEventCallback start');
104    inputMonitor.on('touch', this.touchEventCallback);
105  }
106
107  private turnOffTouchEventCallback() {
108    Log.showWarn(TAG, 'turnOffTouchEventCallback start');
109    inputMonitor.off('touch', this.touchEventCallback);
110  }
111
112  private handleEventSwitches(gestureNavigationStatus: string) {
113    if (gestureNavigationStatus == '0') {
114      this.turnOnTouchEventCallback();
115    } else {
116      this.turnOffTouchEventCallback();
117    }
118  }
119
120  /**
121   * Get the GestureNavigationManage instance.
122   */
123  static getInstance(): GestureNavigationManager {
124    if (globalThis.sGestureNavigationManager == null) {
125      globalThis.sGestureNavigationManager = new GestureNavigationManager();
126    }
127    return globalThis.sGestureNavigationManager;
128  }
129}