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 audio from '@ohos.multimedia.audio'; 17import { BusinessError } from '@ohos.base'; 18import Log from '../../../../../../../../common/src/main/ets/default/Log'; 19import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 20import { getAudioManager } from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 21import { AudioRingMode } from '../common/Constants'; 22 23const TAG = 'RingModeModel'; 24 25export interface RingModeStatusListener { 26 updateRingerMode(status: AudioRingMode): void; 27} 28 29export class RingModeService { 30 mIsStart = false; 31 mListeners = new Set<RingModeStatusListener>(); 32 mAudioManager: any; 33 34 async startService(): Promise<void> { 35 if (this.mIsStart) { 36 return; 37 } 38 Log.showInfo(TAG, 'startService'); 39 this.mIsStart = true; 40 41 this.mAudioManager = await getAudioManager().getVolumeManager().getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID); 42 43 this.getRingerMode(); 44 45 this.mAudioManager.on('ringerModeChange', (data: AudioRingMode) => { 46 Log.showInfo(TAG, `startService->ringerModeChange, data: ${JSON.stringify(data)}`); 47 this.mListeners.forEach(listener => listener.updateRingerMode(data)); 48 }); 49 } 50 51 stopService(): void { 52 if (!this.mIsStart) { 53 return; 54 } 55 Log.showInfo(TAG, 'stopService'); 56 this.mIsStart = false; 57 58 this.mAudioManager = null; 59 } 60 61 registerListener(listener: RingModeStatusListener): void { 62 let res = this.mListeners.add(listener); 63 Log.showInfo(TAG, `registser ringMode Listener ${res}`); 64 } 65 66 unregisterListener(listener: RingModeStatusListener): void { 67 let res = this.mListeners.delete(listener); 68 Log.showInfo(TAG, `unregistser ringMode Listener ${res}`); 69 } 70 71 getRingerMode(): void { 72 this.mAudioManager?.getRingerMode((error: BusinessError, action: AudioRingMode) => { 73 Log.showInfo(TAG, `getRingerMode, error: ${JSON.stringify(error)} action: ${JSON.stringify(action)}`); 74 if (error) { 75 return; 76 } 77 this.mListeners.forEach(listener => listener.updateRingerMode(action)); 78 }); 79 } 80 81 setRingerMode(mode: AudioRingMode): void { 82 Log.showInfo(TAG, `setRingerMode, mode: ${JSON.stringify(mode)}`); 83 this.mAudioManager?.setRingerMode(mode, (err: BusinessError) => { 84 Log.showInfo(TAG, 'mAudioManager.setRingerMode'); 85 }); 86 } 87} 88 89let sRingModeService = createOrGet(RingModeService, TAG); 90 91export default sRingModeService;