1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 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 package com.android.car.audio; 17 18 import android.annotation.Nullable; 19 import android.annotation.SuppressLint; 20 import android.car.builtin.media.AudioManagerHelper; 21 import android.media.AudioAttributes; 22 import android.media.AudioDeviceAttributes; 23 import android.media.AudioDeviceCallback; 24 import android.media.AudioDeviceInfo; 25 import android.media.AudioFocusInfo; 26 import android.media.AudioFocusRequest; 27 import android.media.AudioManager; 28 import android.media.AudioManager.AudioPlaybackCallback; 29 import android.media.AudioManager.AudioServerStateCallback; 30 import android.media.AudioPlaybackConfiguration; 31 import android.media.FadeManagerConfiguration; 32 import android.media.audiopolicy.AudioPolicy; 33 import android.media.audiopolicy.AudioProductStrategy; 34 import android.media.audiopolicy.AudioVolumeGroup; 35 import android.os.Handler; 36 37 import java.util.List; 38 import java.util.Objects; 39 import java.util.concurrent.Executor; 40 41 /** 42 * Class to wrap audio manager. This makes it easier to call to audio manager without the need 43 * for actually using an audio manager. 44 */ 45 public final class AudioManagerWrapper { 46 47 private final AudioManager mAudioManager; 48 AudioManagerWrapper(AudioManager audioManager)49 AudioManagerWrapper(AudioManager audioManager) { 50 mAudioManager = Objects.requireNonNull(audioManager, "Audio manager can not be null"); 51 } 52 getMinVolumeIndexForAttributes(AudioAttributes audioAttributes)53 int getMinVolumeIndexForAttributes(AudioAttributes audioAttributes) { 54 return mAudioManager.getMinVolumeIndexForAttributes(audioAttributes); 55 } 56 getMaxVolumeIndexForAttributes(AudioAttributes audioAttributes)57 int getMaxVolumeIndexForAttributes(AudioAttributes audioAttributes) { 58 return mAudioManager.getMaxVolumeIndexForAttributes(audioAttributes); 59 } 60 getVolumeIndexForAttributes(AudioAttributes audioAttributes)61 int getVolumeIndexForAttributes(AudioAttributes audioAttributes) { 62 return mAudioManager.getVolumeIndexForAttributes(audioAttributes); 63 } 64 isVolumeGroupMuted(int groupId)65 boolean isVolumeGroupMuted(int groupId) { 66 return AudioManagerHelper.isVolumeGroupMuted(mAudioManager, groupId); 67 } 68 getLastAudibleVolumeForVolumeGroup(int groupId)69 int getLastAudibleVolumeForVolumeGroup(int groupId) { 70 return AudioManagerHelper.getLastAudibleVolumeGroupVolume(mAudioManager, groupId); 71 } 72 setVolumeGroupVolumeIndex(int groupId, int gainIndex, int flags)73 void setVolumeGroupVolumeIndex(int groupId, int gainIndex, int flags) { 74 mAudioManager.setVolumeGroupVolumeIndex(groupId, gainIndex, flags); 75 } 76 adjustVolumeGroupVolume(int groupId, int adjustment, int flags)77 void adjustVolumeGroupVolume(int groupId, int adjustment, int flags) { 78 AudioManagerHelper.adjustVolumeGroupVolume(mAudioManager, groupId, adjustment, flags); 79 } 80 setPreferredDeviceForStrategy(AudioProductStrategy strategy, AudioDeviceAttributes audioDeviceAttributes)81 void setPreferredDeviceForStrategy(AudioProductStrategy strategy, 82 AudioDeviceAttributes audioDeviceAttributes) { 83 mAudioManager.setPreferredDeviceForStrategy(strategy, audioDeviceAttributes); 84 } 85 setAudioDeviceGain(String address, int gain, boolean isOutput)86 boolean setAudioDeviceGain(String address, int gain, boolean isOutput) { 87 return AudioManagerHelper.setAudioDeviceGain(mAudioManager, address, gain, isOutput); 88 } 89 90 /** 91 * {@link AudioManager#abandonAudioFocusRequest(AudioFocusRequest)} 92 */ abandonAudioFocusRequest(AudioFocusRequest audioFocusRequest)93 public int abandonAudioFocusRequest(AudioFocusRequest audioFocusRequest) { 94 return mAudioManager.abandonAudioFocusRequest(audioFocusRequest); 95 } 96 97 /** 98 * {@link AudioManager#requestAudioFocus(AudioFocusRequest)} 99 */ requestAudioFocus(AudioFocusRequest audioFocusRequest)100 public int requestAudioFocus(AudioFocusRequest audioFocusRequest) { 101 return mAudioManager.requestAudioFocus(audioFocusRequest); 102 } 103 isMasterMuted()104 boolean isMasterMuted() { 105 return AudioManagerHelper.isMasterMute(mAudioManager); 106 } 107 setMasterMute(boolean mute, int flags)108 void setMasterMute(boolean mute, int flags) { 109 AudioManagerHelper.setMasterMute(mAudioManager, mute, flags); 110 } 111 dispatchAudioFocusChange(AudioFocusInfo info, int focusChange, AudioPolicy policy)112 int dispatchAudioFocusChange(AudioFocusInfo info, int focusChange, AudioPolicy policy) { 113 return mAudioManager.dispatchAudioFocusChange(info, focusChange, policy); 114 } 115 dispatchAudioFocusChangeWithFade(AudioFocusInfo info, int changeType, AudioPolicy policy, List<AudioFocusInfo> activeAfis, FadeManagerConfiguration fadeConfig)116 int dispatchAudioFocusChangeWithFade(AudioFocusInfo info, int changeType, 117 AudioPolicy policy, List<AudioFocusInfo> activeAfis, 118 FadeManagerConfiguration fadeConfig) { 119 return mAudioManager.dispatchAudioFocusChangeWithFade(info, changeType, policy, activeAfis, 120 fadeConfig); 121 } 122 setFocusRequestResult(AudioFocusInfo info, int response, AudioPolicy policy)123 void setFocusRequestResult(AudioFocusInfo info, int response, AudioPolicy policy) { 124 mAudioManager.setFocusRequestResult(info, response, policy); 125 } 126 registerVolumeGroupCallback(Executor executor, AudioManager.VolumeGroupCallback coreAudioVolumeGroupCallback)127 void registerVolumeGroupCallback(Executor executor, 128 AudioManager.VolumeGroupCallback coreAudioVolumeGroupCallback) { 129 mAudioManager.registerVolumeGroupCallback(executor, coreAudioVolumeGroupCallback); 130 } 131 unregisterVolumeGroupCallback( AudioManager.VolumeGroupCallback coreAudioVolumeGroupCallback)132 void unregisterVolumeGroupCallback( 133 AudioManager.VolumeGroupCallback coreAudioVolumeGroupCallback) { 134 mAudioManager.unregisterVolumeGroupCallback(coreAudioVolumeGroupCallback); 135 } 136 isAudioServerRunning()137 boolean isAudioServerRunning() { 138 return mAudioManager.isAudioServerRunning(); 139 } 140 setAudioServerStateCallback(Executor executor, AudioServerStateCallback callback)141 void setAudioServerStateCallback(Executor executor, AudioServerStateCallback callback) { 142 mAudioManager.setAudioServerStateCallback(executor, callback); 143 } 144 145 @SuppressLint("WrongConstant") setSupportedSystemUsages(int[] systemUsages)146 void setSupportedSystemUsages(int[] systemUsages) { 147 mAudioManager.setSupportedSystemUsages(systemUsages); 148 } 149 registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler)150 void registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler) { 151 mAudioManager.registerAudioDeviceCallback(callback, handler); 152 } 153 unregisterAudioDeviceCallback(AudioDeviceCallback callback)154 void unregisterAudioDeviceCallback(AudioDeviceCallback callback) { 155 mAudioManager.unregisterAudioDeviceCallback(callback); 156 } 157 clearAudioServerStateCallback()158 void clearAudioServerStateCallback() { 159 mAudioManager.clearAudioServerStateCallback(); 160 } 161 unregisterAudioPolicy(AudioPolicy policy)162 void unregisterAudioPolicy(AudioPolicy policy) { 163 mAudioManager.unregisterAudioPolicy(policy); 164 } 165 unregisterAudioPolicyAsync(AudioPolicy policy)166 void unregisterAudioPolicyAsync(AudioPolicy policy) { 167 mAudioManager.unregisterAudioPolicyAsync(policy); 168 } 169 registerAudioPolicy(AudioPolicy policy)170 int registerAudioPolicy(AudioPolicy policy) { 171 return mAudioManager.registerAudioPolicy(policy); 172 } 173 setStreamVolume(int stream, int index, int flags)174 void setStreamVolume(int stream, int index, int flags) { 175 mAudioManager.setStreamVolume(stream, index, flags); 176 } 177 getStreamMaxVolume(int stream)178 int getStreamMaxVolume(int stream) { 179 return mAudioManager.getStreamMaxVolume(stream); 180 } 181 getStreamMinVolume(int stream)182 int getStreamMinVolume(int stream) { 183 return mAudioManager.getStreamMinVolume(stream); 184 } 185 getStreamVolume(int stream)186 int getStreamVolume(int stream) { 187 return mAudioManager.getStreamVolume(stream); 188 } 189 setParameters(String parameters)190 void setParameters(String parameters) { 191 mAudioManager.setParameters(parameters); 192 } 193 getDevices(int flags)194 AudioDeviceInfo[] getDevices(int flags) { 195 return mAudioManager.getDevices(flags); 196 } 197 registerAudioPlaybackCallback(AudioPlaybackCallback callback, @Nullable Handler handler)198 void registerAudioPlaybackCallback(AudioPlaybackCallback callback, @Nullable Handler handler) { 199 mAudioManager.registerAudioPlaybackCallback(callback, handler); 200 } 201 unregisterAudioPlaybackCallback(AudioPlaybackCallback callback)202 void unregisterAudioPlaybackCallback(AudioPlaybackCallback callback) { 203 mAudioManager.unregisterAudioPlaybackCallback(callback); 204 } 205 releaseAudioPatch(AudioManagerHelper.AudioPatchInfo audioPatchInfo)206 boolean releaseAudioPatch(AudioManagerHelper.AudioPatchInfo audioPatchInfo) { 207 return AudioManagerHelper.releaseAudioPatch(mAudioManager, audioPatchInfo); 208 } 209 getActivePlaybackConfigurations()210 List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { 211 return mAudioManager.getActivePlaybackConfigurations(); 212 } 213 getAudioProductStrategies()214 static List<AudioProductStrategy> getAudioProductStrategies() { 215 return AudioManager.getAudioProductStrategies(); 216 } 217 getAudioVolumeGroups()218 static List<AudioVolumeGroup> getAudioVolumeGroups() { 219 return AudioManager.getAudioVolumeGroups(); 220 } 221 } 222