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