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 'basic'; 17import { DataAbilityHelper } from 'ability/dataAbilityHelper'; 18import inputMonitor from '@ohos.multimodalInput.inputMonitor'; 19 20import { Log } from '@ohos/common'; 21import { CommonConstants } from '@ohos/common'; 22import { settingsDataManager } 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 inputMonitor.on('touch', this.touchEventCallback); 88 } 89 90 private turnOffTouchEventCallback() { 91 inputMonitor.off('touch', this.touchEventCallback); 92 } 93 94 private handleEventSwitches(gestureNavigationStatus: string) { 95 if (gestureNavigationStatus == '0') { 96 this.turnOnTouchEventCallback(); 97 } else { 98 this.turnOffTouchEventCallback(); 99 } 100 } 101 102 /** 103 * Get the GestureNavigationManage instance. 104 */ 105 static getInstance(): GestureNavigationManager { 106 if (globalThis.sGestureNavigationManager == null) { 107 globalThis.sGestureNavigationManager = new GestureNavigationManager(); 108 } 109 return globalThis.sGestureNavigationManager; 110 } 111}