1/* 2 * Copyright (c) 2021-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 Log from '../../../../../../../../common/src/main/ets/default/Log'; 17import getSingleInstance from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 18import Trace from '../../../../../../../../common/src/main/ets/default/Trace' 19import VolumePanelService from '../model/VolumePanelService'; 20import { VolumeInfo } from '../common/Constants'; 21 22const TAG = 'VolumeWindowController'; 23const SINGLE_STANCE_KEY = 'VolumeWindowController'; 24const INTERVAL = 3 * 1000; 25 26export default class VolumeWindowController { 27 mWindowHandle: any; 28 mInterval: number = INTERVAL; 29 mLastActionTime: number; 30 mIsWindowShown: boolean; 31 32 static getInstance(): VolumeWindowController{ 33 return getSingleInstance(VolumeWindowController, SINGLE_STANCE_KEY); 34 } 35 36 constructor() { 37 VolumePanelService.startService(); 38 VolumePanelService.registerListener(this); 39 Log.showInfo(TAG, 'constructor done.'); 40 } 41 42 setInterval(interval: number): void { 43 this.mInterval = interval; 44 } 45 46 setWindowHandle(windowHandle): void { 47 Log.showInfo(TAG, `setWindowHandle windowHandle:${windowHandle}`); 48 this.mWindowHandle = windowHandle; 49 } 50 51 updateVolumeInfo(volumeInfo: VolumeInfo): void { 52 Log.showDebug(TAG, `updateVolumeInfo, volumeInfo: ${JSON.stringify(volumeInfo)} `); 53 if (volumeInfo && !volumeInfo.updateUi && !this.mIsWindowShown) { 54 return; 55 } 56 this.setWindowState(true); 57 this.mLastActionTime = (new Date()).getTime(); 58 setTimeout(() => { 59 Log.showInfo(TAG, 'check need hide window or not.'); 60 if ((new Date()).getTime() - this.mLastActionTime >= this.mInterval) { 61 this.setWindowState(false); 62 } 63 }, this.mInterval); 64 } 65 66 setWindowState(isShow: boolean): void { 67 Log.showInfo(TAG, `setWindowState ${isShow}.`); 68 if (isShow) { 69 Trace.start(Trace.CORE_METHOD_START_VOLUMEPANEL); 70 } 71 if (this.mIsWindowShown == isShow) { 72 Log.showInfo(TAG, `Neen't set volueme window state.`); 73 return; 74 } 75 if (this.mWindowHandle) { 76 this.mIsWindowShown = isShow; 77 (isShow ? this.mWindowHandle.show() : this.mWindowHandle.hide()) 78 .then(() => { 79 if (isShow) { 80 Trace.end(Trace.CORE_METHOD_START_VOLUMEPANEL); 81 } 82 Log.showInfo(TAG, `updateShowStatus ${isShow}.`); 83 }) 84 .catch((err) => Log.showError(TAG, `Can't set volueme window: ${JSON.stringify(err)}.`)); 85 } 86 } 87}