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} from '@ohos/common'; 23import GestureNavigationExecutors from './GestureNavigationExecutors'; 24 25const TAG = 'GestureNavigationManage'; 26 27export class GestureNavigationManager { 28 private readonly uri: string | null = null; 29 private helper: any = null; 30 private readonly sGestureNavigationExecutors: GestureNavigationExecutors = GestureNavigationExecutors.getInstance(); 31 private touchEventCallback: inputMonitor.TouchEventReceiver | null = null; 32 33 private constructor() { 34 this.uri = settingsDataManager.getUri(CommonConstants.NAVIGATION_BAR_STATUS_KEY); 35 this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri); 36 } 37 38 private setValue(value: string) { 39 settingsDataManager.setValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, value); 40 } 41 42 private getValue() { 43 return settingsDataManager.getValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, '1'); 44 } 45 46 /** 47 * Monitor data changes. 48 * @param callback 49 */ 50 private registerListenForDataChanges(callback: AsyncCallback<void>) { 51 this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri); 52 Log.showInfo(TAG, "helper:" + this.helper + " registerListenForDataChanges uri:" + this.uri); 53 this.helper.on('dataChange', this.uri, callback); 54 } 55 56 initWindowSize(display: any) { 57 if (globalThis.sGestureNavigationExecutors) { 58 globalThis.sGestureNavigationExecutors.setScreenWidth(display.width); 59 globalThis.sGestureNavigationExecutors.setScreenHeight(display.height); 60 this.touchEventCallback = globalThis.sGestureNavigationExecutors.touchEventCallback 61 .bind(globalThis.sGestureNavigationExecutors); 62 settingsDataManager.createDataShareHelper(); 63 } 64 } 65 66 private getGestureNavigationStatus() { 67 Log.showDebug(TAG, 'getGestureNavigationStatus enter'); 68 let gestureNavigationStatus = null; 69 try{ 70 gestureNavigationStatus = this.getValue(); 71 Log.showDebug(TAG, `getGestureNavigationStatus gestureNavigationStatus: ${gestureNavigationStatus}`); 72 this.handleEventSwitches(gestureNavigationStatus); 73 this.registerListenForDataChanges(this.dataChangesCallback.bind(this)); 74 }catch (error) { 75 Log.showError(TAG, `getGestureNavigationStatus error: ${JSON.stringify(error)}`); 76 } 77 } 78 79 private dataChangesCallback(data: any) { 80 Log.showInfo(TAG, "dataChangesCallback data:" + data); 81 const getRetValue = this.getValue(); 82 this.handleEventSwitches(getRetValue); 83 AppStorage.setOrCreate('NavigationBarStatusValue', getRetValue == '0' ? true : false); 84 } 85 86 private turnOnTouchEventCallback() { 87 Log.showWarn(TAG, 'turnOnTouchEventCallback start'); 88 inputMonitor.on('touch', this.touchEventCallback); 89 } 90 91 private turnOffTouchEventCallback() { 92 Log.showWarn(TAG, 'turnOffTouchEventCallback start'); 93 inputMonitor.off('touch', this.touchEventCallback); 94 } 95 96 private handleEventSwitches(gestureNavigationStatus: string) { 97 if (gestureNavigationStatus == '0') { 98 this.turnOnTouchEventCallback(); 99 } else { 100 this.turnOffTouchEventCallback(); 101 } 102 } 103 104 /** 105 * Get the GestureNavigationManage instance. 106 */ 107 static getInstance(): GestureNavigationManager { 108 if (globalThis.sGestureNavigationManager == null) { 109 globalThis.sGestureNavigationManager = new GestureNavigationManager(); 110 } 111 return globalThis.sGestureNavigationManager; 112 } 113}