1//@ts-nocheck 2/* 3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import Log from '../../../../../../../common/src/main/ets/default/Log'; 18import WindowManager, { WindowType } from '../../../../../../../common/src/main/ets/default/WindowManager'; 19import getSingleInstance from '../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 20import TintStateManager, { TintState, TintStateListener 21} from '../../../../../../../common/src/main/ets/default/TintStateManager'; 22import { NavigationBarComponentData, NAVIGATIONBAR_HIDE_EVENT } from '../common/constants'; 23import dataShare from '@ohos.data.dataShare'; 24import settings from '@ohos.settings'; 25import commonEvent from '@ohos.commonEvent'; 26import AbilityManager from '../../../../../../../common/src/main/ets/default/abilitymanager/abilityManager'; 27import Constants from '../../../../../../../common/src/main/ets/default/Constants'; 28 29const TAG = 'NavigationBarViewModel'; 30 31const NAVIGATION_BAE_VIEW_MODEL_KEY = 'AppStorage_NavigationBarViewModel'; 32 33const NAVIGATION_BAR_COMPONENT_DATA_KEY = 'AppStorage_NavigationBarComponentData'; 34 35export default class NavigationBarViewModel { 36 private readonly settingDataKey = 'settings.display.navigationbar_status'; 37 private readonly urivar: string; 38 private readonly helper: dataShare.DataShareHelper; 39 private readonly navigationBarStatusDefaultValue = '1'; 40 private isDisplay = true; 41 mNavigationBarComponentData: NavigationBarComponentData = { 42 ...new NavigationBarComponentData() 43 }; 44 mUseCount = 0; 45 46 static getInstance(): NavigationBarViewModel { 47 return getSingleInstance(NavigationBarViewModel, NAVIGATION_BAE_VIEW_MODEL_KEY); 48 } 49 50 constructor() { 51 Log.showInfo(TAG, 'constructor'); 52 this.mNavigationBarComponentData = 53 AppStorage.SetAndLink(NAVIGATION_BAR_COMPONENT_DATA_KEY, this.mNavigationBarComponentData).get() 54 if (AbilityManager.getContext(AbilityManager.ABILITY_NAME_NAVIGATION_BAR) == null) { 55 Log.showError(TAG, 'AbilityManager.getContext() is null'); 56 } else { 57 Log.showInfo(TAG, 'context: ' + AbilityManager.getContext(AbilityManager.ABILITY_NAME_NAVIGATION_BAR)); 58 } 59 this.initNavigationBarStatus(); 60 this.initHelper(this.dataChangesCallback.bind(this)); 61 } 62 63 private async initHelper(callback: () => void): Promise<void> { 64 this.urivar = Constants.getUriSync(Constants.KEY_NAVIGATIONBAR_STATUS); 65 this.helper = await dataShare.createDataShareHelper(AbilityManager.getContext(AbilityManager.ABILITY_NAME_NAVIGATION_BAR), this.urivar); 66 Log.showInfo(TAG, 'initHelper, helper: ' + this.helper + ', uri: ' + this.urivar); 67 this.helper.on('dataChange', this.urivar, () => { 68 Log.showInfo(TAG, 'onDataChange.'); 69 callback(); 70 }); 71 } 72 73 install(): void { 74 Log.showDebug(TAG, `install, useCount: ${this.mUseCount}`); 75 if (!this.mUseCount) { 76 TintStateManager.getInstance().registerListener('navigation', this as TintStateListener); 77 } 78 this.mUseCount++; 79 } 80 81 uninstall(): void { 82 Log.showDebug(TAG, `uninstall, useCount: ${this.mUseCount}`); 83 this.mUseCount--; 84 if (this.mUseCount) { 85 TintStateManager.getInstance().unregisterListener('navigation'); 86 } 87 } 88 89 getNavigationBarComponentData(): NavigationBarComponentData { 90 Log.showDebug(TAG, 'getNavigationBarComponentData'); 91 return this.mNavigationBarComponentData; 92 } 93 94 onTintStateChange(tintState: TintState): void { 95 Log.showDebug(TAG, `onTintStateChange, tintState: ${JSON.stringify(tintState)}`); 96 if (typeof (tintState.isEnable) == 'boolean') { 97 this.setWindowEnable(tintState.isEnable); 98 } 99 if (tintState.backgroundColor) { 100 this.mNavigationBarComponentData.backgroundColor = tintState.backgroundColor; 101 } 102 if (tintState.contentColor) { 103 this.mNavigationBarComponentData.contentColor = tintState.contentColor; 104 } 105 Log.showDebug(TAG, `onTintStateChange, backgroundColor ${this.mNavigationBarComponentData.backgroundColor}, 106 contentColor ${this.mNavigationBarComponentData.contentColor}`); 107 } 108 109 setWindowEnable(isEnable: boolean): void { 110 Log.showDebug(TAG, `setWindowEnable, isEnable ${String(isEnable)}`); 111 if (this.mNavigationBarComponentData.isEnable == isEnable) { 112 return; 113 } 114 this.mNavigationBarComponentData.isEnable = isEnable; 115 if (isEnable && this.isDisplay) { 116 WindowManager.showWindow(WindowType.NAVIGATION_BAR).then(() => { 117 }).catch((err) => { 118 }); 119 } else { 120 WindowManager.hideWindow(WindowType.NAVIGATION_BAR).then(() => { 121 }).catch((err) => { 122 }); 123 } 124 } 125 126 private setValue(value: string): void { 127 let context = AbilityManager.getContext(AbilityManager.ABILITY_NAME_NAVIGATION_BAR); 128 settings.setValueSync(context, this.settingDataKey, value); 129 } 130 131 private getValue(defaultValue?: string): string { 132 let context = AbilityManager.getContext(AbilityManager.ABILITY_NAME_NAVIGATION_BAR); 133 return settings.getValueSync( 134 context, this.settingDataKey, defaultValue ? defaultValue : this.navigationBarStatusDefaultValue 135 ); 136 } 137 138 /** 139 * Initialize the NavigationBar status. 140 */ 141 initNavigationBarStatus(): void { 142 try { 143 let initValue = this.getValue(); 144 Log.showInfo(TAG, `initNavigationBarStatus initValue ${initValue}`); 145 this.windowSwitches(initValue); 146 } catch (e) { 147 Log.showError(TAG, `initNavigationBarStatus error: ${e.toString()}`); 148 } 149 } 150 151 /** 152 * Get NavigationBar status data. 153 * @return 154 */ 155 dataChangesCallback(): void { 156 let getRetValue = this.getValue(); 157 Log.showInfo(TAG, `dataChangesCallback initValue ${getRetValue}`); 158 this.windowSwitches(getRetValue); 159 } 160 161 private windowSwitches(navigationBarStatusValue: string): void { 162 this.isDisplay = navigationBarStatusValue == '1' ? true : false; 163 if (!this.isDisplay || !this.mNavigationBarComponentData.isEnable) { 164 //For gesture navigation scenarios 165 //Systemui hides the navigation bar,and then notifies the launcher that it can start moving down the dock bar. 166 WindowManager.hideWindow(WindowType.NAVIGATION_BAR).then(() => { 167 if(!this.isDisplay){ 168 commonEvent.publish(NAVIGATIONBAR_HIDE_EVENT, (err) => { 169 if (err.code) { 170 Log.showError(TAG, `${NAVIGATIONBAR_HIDE_EVENT} PublishCallBack err: ${JSON.stringify(err)}`); 171 } else { 172 Log.showInfo(TAG, `${NAVIGATIONBAR_HIDE_EVENT} Publish sucess`); 173 } 174 }) 175 } 176 }).catch((err) => { 177 Log.showError(TAG, `${NAVIGATIONBAR_HIDE_EVENT} Publish catch err: ${JSON.stringify(err)}`); 178 }); 179 } else { 180 WindowManager.showWindow(WindowType.NAVIGATION_BAR).then(() => { 181 }).catch((err) => { 182 }); 183 } 184 } 185}