• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021 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 BaseModel from '../../../../../../../common/utils/src/main/ets/default/model/BaseModel';
17import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
18import { GlobalContext } from '../../../../../../../common/utils/src/main/ets/default/baseUtil/GlobalContext';
19import Log from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogDecorator';
20import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData';
21import Audio from '@ohos.multimedia.audio';
22
23const groupId = Audio.DEFAULT_VOLUME_GROUP_ID;
24
25function getAudioManager() {
26  let context = GlobalContext.getContext().getObject(GlobalContext.globalKeyAudioManager);
27  if (!context) {
28    GlobalContext.getContext().setObject(GlobalContext.globalKeyAudioManager, Audio.getAudioManager());
29  }
30  return GlobalContext.getContext().getObject(GlobalContext.globalKeyAudioManager) as Audio.AudioManager;
31}
32
33function audioVolumeGroupManager() {
34  let context = GlobalContext.getContext().getObject(GlobalContext.globalKeyAudioVolumeGroupManager);
35  if (!context) {
36    GlobalContext.getContext().setObject(GlobalContext.globalKeyAudioVolumeGroupManager,
37      getAudioManager().getVolumeManager().getVolumeGroupManager(groupId));
38  }
39  return GlobalContext.getContext().getObject(GlobalContext.globalKeyAudioVolumeGroupManager) as Audio.AudioVolumeGroupManager;
40}
41
42export async function registerObserver() {
43  let audioManager = getAudioManager();
44  let getaudioVolumeGroupManager = await audioVolumeGroupManager();
45  getaudioVolumeGroupManager.on('ringerModeChange', (mode) => {
46    AppStorage.SetOrCreate('ringerModeSilent', mode === Audio.AudioRingMode.RINGER_MODE_SILENT);
47    AppStorage.SetOrCreate('ringerModeNormal', mode === Audio.AudioRingMode.RINGER_MODE_NORMAL);
48  })
49
50  Audio?.getAudioManager()?.getVolumeManager()?.on('volumeChange', (data) => {
51    if (data.volumeType === Audio.AudioVolumeType.RINGTONE) {
52      AppStorage.SetOrCreate('volume_ringtone', data.volume);
53    } else if (data.volumeType === Audio.AudioVolumeType.VOICE_CALL) {
54      AppStorage.SetOrCreate('volume_voicecall', data.volume);
55    } else if (data.volumeType === Audio.AudioVolumeType.MEDIA) {
56      AppStorage.SetOrCreate('volume_media', data.volume);
57    }
58  })
59
60  audioManager.on('deviceChange', () => {
61    audioManager.getVolume(Audio.AudioVolumeType.RINGTONE, (err, data) => {
62      AppStorage.SetOrCreate('volume_ringtone', data);
63    })
64    audioManager.getVolume(Audio.AudioVolumeType.VOICE_CALL, (err, data) => {
65      AppStorage.SetOrCreate('volume_voicecall', data);
66    })
67    audioManager.getVolume(Audio.AudioVolumeType.MEDIA, (err, data) => {
68      AppStorage.SetOrCreate('volume_media', data);
69    })
70  })
71}
72
73
74export class VolumeModel extends BaseModel {
75  private volumeType;
76  private audioManager;
77  private volume;
78  private TAG = ConfigData.TAG + 'VolumeModel ';
79  private oldVolume;
80
81  constructor(volumeType) {
82    super();
83    this.volumeType = volumeType;
84    this.audioManager = getAudioManager();
85    this.audioManager.getVolume(this.volumeType, (err, data) => {
86      this.initState(data);
87      this.oldVolume = data;
88    })
89  }
90
91  /**
92   * Get volume value in the VolumeModel
93   */
94  @Log
95  public initState(volume) {
96    if (this.volumeType === Audio.AudioVolumeType.RINGTONE) {
97      AppStorage.SetOrCreate('volume_ringtone', volume);
98    } else if (this.volumeType === Audio.AudioVolumeType.MEDIA) {
99      AppStorage.SetOrCreate('volume_media', volume);
100    } else if (this.volumeType === Audio.AudioVolumeType.VOICE_CALL) {
101      AppStorage.SetOrCreate('volume_voicecall', volume);
102    }
103    return;
104  }
105
106  /**
107   * Set value
108   */
109  @Log
110  public setVolume(volume: number) {
111    if (volume === this.oldVolume) {
112      return;
113    } else {
114      LogUtil.info(`${this.TAG} setVolume start, volume: ${volume}`);
115      this.audioManager.setVolume(this.volumeType, volume).then(() => {
116        LogUtil.info(`${this.TAG} setVolume callback in, volume: ${volume}`);
117      });
118      this.oldVolume = volume;
119      LogUtil.info(`${this.TAG} setVolume end, volume: ${volume}`);
120      return;
121    }
122  }
123}
124
125export class RingerModel extends BaseModel {
126  private modeTag;
127  private TAG = ConfigData.TAG + 'RingerModel ';
128
129  constructor(mode) {
130    super();
131    this.modeTag = mode;
132    this.initState();
133  }
134
135  /**
136   * Update ringer Mode
137   */
138  @Log
139  public async initState() {
140    let getaudioVolumeGroupManager = await audioVolumeGroupManager();
141    getaudioVolumeGroupManager.getRingerMode((error, action) => {
142      LogUtil.info(`${this.TAG} updateMode.`);
143      if (error) {
144        return;
145      }
146      AppStorage.SetOrCreate('ringerModeSilent', action === Audio.AudioRingMode.RINGER_MODE_SILENT);
147      AppStorage.SetOrCreate('ringerModeNormal', action === Audio.AudioRingMode.RINGER_MODE_NORMAL);
148      LogUtil.info(`${this.TAG} updateMode sucess.`);
149    });
150  }
151
152  /**
153   * Set  ringer mode
154   */
155  @Log
156  public async setRingerMode() {
157    let audioVolumeGroupManager = await Audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupId);
158    await audioVolumeGroupManager.setRingerMode(this.modeTag);
159    let volume = await audioVolumeGroupManager.getVolume(Audio.AudioVolumeType.RINGTONE);
160    AppStorage.SetOrCreate('volume_ringtone', volume);
161    LogUtil.info(`${this.TAG} setRingerMode ${this.modeTag} volume: ${volume}.`);
162    return;
163  }
164}
165
166