Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioManager; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 21 | import android.telecomm.CallAudioState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 22 | import android.telecomm.CallState; |
| 23 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 24 | import com.google.common.base.Preconditions; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 25 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 26 | /** |
| 27 | * This class manages audio modes, streams and other properties. |
| 28 | */ |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 29 | final class CallAudioManager extends CallsManagerListenerBase |
| 30 | implements WiredHeadsetManager.Listener { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 31 | private static final int STREAM_NONE = -1; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 32 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 33 | private final StatusBarNotifier mStatusBarNotifier; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 34 | private final AudioManager mAudioManager; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 35 | private final BluetoothManager mBluetoothManager; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 36 | private final WiredHeadsetManager mWiredHeadsetManager; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 37 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 38 | private CallAudioState mAudioState; |
| 39 | private int mAudioFocusStreamType; |
| 40 | private boolean mIsRinging; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 41 | private boolean mIsTonePlaying; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 42 | private boolean mWasSpeakerOn; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 43 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 44 | CallAudioManager(Context context, StatusBarNotifier statusBarNotifier, |
| 45 | WiredHeadsetManager wiredHeadsetManager) { |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 46 | mStatusBarNotifier = statusBarNotifier; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 47 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 48 | mBluetoothManager = new BluetoothManager(context, this); |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 49 | mWiredHeadsetManager = wiredHeadsetManager; |
Sailesh Nepal | d0a76aa | 2014-07-16 22:12:23 -0700 | [diff] [blame] | 50 | mWiredHeadsetManager.addListener(this); |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 51 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 52 | saveAudioState(getInitialAudioState(null)); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 53 | mAudioFocusStreamType = STREAM_NONE; |
| 54 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 55 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 56 | CallAudioState getAudioState() { |
| 57 | return mAudioState; |
| 58 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | public void onCallAdded(Call call) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 62 | updateAudioStreamAndMode(); |
| 63 | if (CallsManager.getInstance().getCalls().size() == 1) { |
| 64 | Log.v(this, "first call added, reseting system audio to default state"); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 65 | setInitialAudioState(call); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 66 | } else if (!call.isIncoming()) { |
| 67 | // Unmute new outgoing call. |
| 68 | setSystemAudioState(false, mAudioState.route, mAudioState.supportedRouteMask); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 69 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onCallRemoved(Call call) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 74 | if (CallsManager.getInstance().getCalls().isEmpty()) { |
| 75 | Log.v(this, "all calls removed, reseting system audio to default state"); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 76 | setInitialAudioState(null); |
Santos Cordon | a343b76 | 2014-08-06 05:01:53 -0700 | [diff] [blame] | 77 | mWasSpeakerOn = false; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 78 | } |
| 79 | updateAudioStreamAndMode(); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 84 | updateAudioStreamAndMode(); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onIncomingCallAnswered(Call call) { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 89 | int route = mAudioState.route; |
| 90 | |
| 91 | // We do two things: |
| 92 | // (1) If this is the first call, then we can to turn on bluetooth if available. |
| 93 | // (2) Unmute the audio for the new incoming call. |
| 94 | boolean isOnlyCall = CallsManager.getInstance().getCalls().size() == 1; |
| 95 | if (isOnlyCall && mBluetoothManager.isBluetoothAvailable()) { |
| 96 | mBluetoothManager.connectBluetoothAudio(); |
| 97 | route = CallAudioState.ROUTE_BLUETOOTH; |
| 98 | } |
| 99 | |
| 100 | setSystemAudioState(false /* isMute */, route, mAudioState.supportedRouteMask); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 104 | public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) { |
| 105 | updateAudioStreamAndMode(); |
| 106 | // Ensure that the foreground call knows about the latest audio state. |
| 107 | updateAudioForForegroundCall(); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public void onAudioModeIsVoipChanged(Call call) { |
| 112 | updateAudioStreamAndMode(); |
| 113 | } |
| 114 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Updates the audio route when the headset plugged in state changes. For example, if audio is |
| 117 | * being routed over speakerphone and a headset is plugged in then switch to wired headset. |
| 118 | */ |
| 119 | @Override |
| 120 | public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) { |
| 121 | int newRoute = CallAudioState.ROUTE_EARPIECE; |
| 122 | if (newIsPluggedIn) { |
| 123 | newRoute = CallAudioState.ROUTE_WIRED_HEADSET; |
| 124 | } else if (mWasSpeakerOn) { |
| 125 | Call call = getForegroundCall(); |
| 126 | if (call != null && call.isAlive()) { |
| 127 | // Restore the speaker state. |
| 128 | newRoute = CallAudioState.ROUTE_SPEAKER; |
| 129 | } |
| 130 | } |
| 131 | setSystemAudioState(mAudioState.isMuted, newRoute, calculateSupportedRoutes()); |
| 132 | } |
| 133 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 134 | void toggleMute() { |
| 135 | mute(!mAudioState.isMuted); |
| 136 | } |
| 137 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 138 | void mute(boolean shouldMute) { |
| 139 | Log.v(this, "mute, shouldMute: %b", shouldMute); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 140 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 141 | // Don't mute if there are any emergency calls. |
| 142 | if (CallsManager.getInstance().hasEmergencyCall()) { |
| 143 | shouldMute = false; |
| 144 | Log.v(this, "ignoring mute for emergency call"); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 147 | if (mAudioState.isMuted != shouldMute) { |
| 148 | setSystemAudioState(shouldMute, mAudioState.route, mAudioState.supportedRouteMask); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 152 | /** |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 153 | * Changed the audio route, for example from earpiece to speaker phone. |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 154 | * |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 155 | * @param route The new audio route to use. See {@link CallAudioState}. |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 156 | */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 157 | void setAudioRoute(int route) { |
| 158 | Log.v(this, "setAudioRoute, route: %s", CallAudioState.audioRouteToString(route)); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 159 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 160 | // Change ROUTE_WIRED_OR_EARPIECE to a single entry. |
| 161 | int newRoute = selectWiredOrEarpiece(route, mAudioState.supportedRouteMask); |
| 162 | |
| 163 | // If route is unsupported, do nothing. |
| 164 | if ((mAudioState.supportedRouteMask | newRoute) == 0) { |
| 165 | Log.wtf(this, "Asking to set to a route that is unsupported: %d", newRoute); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (mAudioState.route != newRoute) { |
| 170 | // Remember the new speaker state so it can be restored when the user plugs and unplugs |
| 171 | // a headset. |
| 172 | mWasSpeakerOn = newRoute == CallAudioState.ROUTE_SPEAKER; |
| 173 | setSystemAudioState(mAudioState.isMuted, newRoute, mAudioState.supportedRouteMask); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void setIsRinging(boolean isRinging) { |
| 178 | if (mIsRinging != isRinging) { |
| 179 | Log.v(this, "setIsRinging %b -> %b", mIsRinging, isRinging); |
| 180 | mIsRinging = isRinging; |
| 181 | updateAudioStreamAndMode(); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 182 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 185 | /** |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 186 | * Sets the tone playing status. Some tones can play even when there are no live calls and this |
| 187 | * status indicates that we should keep audio focus even for tones that play beyond the life of |
| 188 | * calls. |
| 189 | * |
| 190 | * @param isPlayingNew The status to set. |
| 191 | */ |
| 192 | void setIsTonePlaying(boolean isPlayingNew) { |
| 193 | ThreadUtil.checkOnMainThread(); |
| 194 | |
| 195 | if (mIsTonePlaying != isPlayingNew) { |
| 196 | Log.v(this, "mIsTonePlaying %b -> %b.", mIsTonePlaying, isPlayingNew); |
| 197 | mIsTonePlaying = isPlayingNew; |
| 198 | updateAudioStreamAndMode(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 203 | * Updates the audio routing according to the bluetooth state. |
| 204 | */ |
| 205 | void onBluetoothStateChange(BluetoothManager bluetoothManager) { |
| 206 | int newRoute = mAudioState.route; |
| 207 | if (bluetoothManager.isBluetoothAudioConnectedOrPending()) { |
| 208 | newRoute = CallAudioState.ROUTE_BLUETOOTH; |
| 209 | } else if (mAudioState.route == CallAudioState.ROUTE_BLUETOOTH) { |
| 210 | newRoute = CallAudioState.ROUTE_WIRED_OR_EARPIECE; |
| 211 | // Do not switch to speaker when bluetooth disconnects. |
| 212 | mWasSpeakerOn = false; |
| 213 | } |
| 214 | |
| 215 | setSystemAudioState(mAudioState.isMuted, newRoute, calculateSupportedRoutes()); |
| 216 | } |
| 217 | |
| 218 | boolean isBluetoothAudioOn() { |
| 219 | return mBluetoothManager.isBluetoothAudioConnected(); |
| 220 | } |
| 221 | |
| 222 | boolean isBluetoothDeviceAvailable() { |
| 223 | return mBluetoothManager.isBluetoothAvailable(); |
| 224 | } |
| 225 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 226 | private void saveAudioState(CallAudioState audioState) { |
| 227 | mAudioState = audioState; |
| 228 | mStatusBarNotifier.notifyMute(mAudioState.isMuted); |
| 229 | mStatusBarNotifier.notifySpeakerphone(mAudioState.route == CallAudioState.ROUTE_SPEAKER); |
| 230 | } |
| 231 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 232 | private void setSystemAudioState(boolean isMuted, int route, int supportedRouteMask) { |
| 233 | CallAudioState oldAudioState = mAudioState; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 234 | saveAudioState(new CallAudioState(isMuted, route, supportedRouteMask)); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 235 | Log.i(this, "changing audio state from %s to %s", oldAudioState, mAudioState); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 236 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 237 | // Mute. |
| 238 | if (mAudioState.isMuted != mAudioManager.isMicrophoneMute()) { |
| 239 | Log.i(this, "changing microphone mute state to: %b", mAudioState.isMuted); |
| 240 | mAudioManager.setMicrophoneMute(mAudioState.isMuted); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 243 | // Audio route. |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 244 | if (mAudioState.route == CallAudioState.ROUTE_BLUETOOTH) { |
| 245 | turnOnSpeaker(false); |
| 246 | turnOnBluetooth(true); |
| 247 | } else if (mAudioState.route == CallAudioState.ROUTE_SPEAKER) { |
| 248 | turnOnBluetooth(false); |
| 249 | turnOnSpeaker(true); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 250 | } else if (mAudioState.route == CallAudioState.ROUTE_EARPIECE || |
| 251 | mAudioState.route == CallAudioState.ROUTE_WIRED_HEADSET) { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 252 | turnOnBluetooth(false); |
| 253 | turnOnSpeaker(false); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (!oldAudioState.equals(mAudioState)) { |
| 257 | CallsManager.getInstance().onAudioStateChanged(oldAudioState, mAudioState); |
| 258 | updateAudioForForegroundCall(); |
| 259 | } |
| 260 | } |
| 261 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 262 | private void turnOnSpeaker(boolean on) { |
| 263 | // Wired headset and earpiece work the same way |
| 264 | if (mAudioManager.isSpeakerphoneOn() != on) { |
| 265 | Log.i(this, "turning speaker phone off"); |
| 266 | mAudioManager.setSpeakerphoneOn(on); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | private void turnOnBluetooth(boolean on) { |
| 271 | if (mBluetoothManager.isBluetoothAvailable()) { |
| 272 | boolean isAlreadyOn = mBluetoothManager.isBluetoothAudioConnected(); |
| 273 | if (on != isAlreadyOn) { |
| 274 | if (on) { |
| 275 | mBluetoothManager.connectBluetoothAudio(); |
| 276 | } else { |
| 277 | mBluetoothManager.disconnectBluetoothAudio(); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 283 | private void updateAudioStreamAndMode() { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 284 | Log.v(this, "updateAudioStreamAndMode, mIsRinging: %b, mIsTonePlaying: %b", mIsRinging, |
| 285 | mIsTonePlaying); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 286 | if (mIsRinging) { |
| 287 | requestAudioFocusAndSetMode(AudioManager.STREAM_RING, AudioManager.MODE_RINGTONE); |
| 288 | } else { |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 289 | Call call = getForegroundCall(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 290 | if (call != null) { |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 291 | int mode = call.getAudioModeIsVoip() ? |
| 292 | AudioManager.MODE_IN_COMMUNICATION : AudioManager.MODE_IN_CALL; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 293 | requestAudioFocusAndSetMode(AudioManager.STREAM_VOICE_CALL, mode); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 294 | } else if (mIsTonePlaying) { |
| 295 | // There is no call, however, we are still playing a tone, so keep focus. |
| 296 | requestAudioFocusAndSetMode( |
| 297 | AudioManager.STREAM_VOICE_CALL, AudioManager.MODE_IN_COMMUNICATION); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 298 | } else { |
| 299 | abandonAudioFocus(); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | private void requestAudioFocusAndSetMode(int stream, int mode) { |
| 305 | Log.v(this, "setSystemAudioStreamAndMode, stream: %d -> %d", mAudioFocusStreamType, stream); |
| 306 | Preconditions.checkState(stream != STREAM_NONE); |
| 307 | |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 308 | // Even if we already have focus, if the stream is different we update audio manager to give |
| 309 | // it a hint about the purpose of our focus. |
| 310 | if (mAudioFocusStreamType != stream) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 311 | Log.v(this, "requesting audio focus for stream: %d", stream); |
| 312 | mAudioManager.requestAudioFocusForCall(stream, |
| 313 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 314 | } |
| 315 | mAudioFocusStreamType = stream; |
| 316 | setMode(mode); |
| 317 | } |
| 318 | |
| 319 | private void abandonAudioFocus() { |
| 320 | if (mAudioFocusStreamType != STREAM_NONE) { |
| 321 | setMode(AudioManager.MODE_NORMAL); |
| 322 | Log.v(this, "abandoning audio focus"); |
| 323 | mAudioManager.abandonAudioFocusForCall(); |
| 324 | mAudioFocusStreamType = STREAM_NONE; |
| 325 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Sets the audio mode. |
| 330 | * |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 331 | * @param newMode Mode constant from AudioManager.MODE_*. |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 332 | */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 333 | private void setMode(int newMode) { |
| 334 | Preconditions.checkState(mAudioFocusStreamType != STREAM_NONE); |
| 335 | int oldMode = mAudioManager.getMode(); |
| 336 | Log.v(this, "Request to change audio mode from %d to %d", oldMode, newMode); |
| 337 | if (oldMode != newMode) { |
| 338 | mAudioManager.setMode(newMode); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | private int selectWiredOrEarpiece(int route, int supportedRouteMask) { |
| 343 | // Since they are mutually exclusive and one is ALWAYS valid, we allow a special input of |
| 344 | // ROUTE_WIRED_OR_EARPIECE so that callers dont have to make a call to check which is |
| 345 | // supported before calling setAudioRoute. |
| 346 | if (route == CallAudioState.ROUTE_WIRED_OR_EARPIECE) { |
| 347 | route = CallAudioState.ROUTE_WIRED_OR_EARPIECE & supportedRouteMask; |
| 348 | if (route == 0) { |
| 349 | Log.wtf(this, "One of wired headset or earpiece should always be valid."); |
| 350 | // assume earpiece in this case. |
| 351 | route = CallAudioState.ROUTE_EARPIECE; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 352 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 353 | } |
| 354 | return route; |
| 355 | } |
| 356 | |
| 357 | private int calculateSupportedRoutes() { |
| 358 | int routeMask = CallAudioState.ROUTE_SPEAKER; |
| 359 | |
| 360 | if (mWiredHeadsetManager.isPluggedIn()) { |
| 361 | routeMask |= CallAudioState.ROUTE_WIRED_HEADSET; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 362 | } else { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 363 | routeMask |= CallAudioState.ROUTE_EARPIECE; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 364 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 365 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 366 | if (mBluetoothManager.isBluetoothAvailable()) { |
| 367 | routeMask |= CallAudioState.ROUTE_BLUETOOTH; |
| 368 | } |
| 369 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 370 | return routeMask; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 373 | private CallAudioState getInitialAudioState(Call call) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 374 | int supportedRouteMask = calculateSupportedRoutes(); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 375 | int route = selectWiredOrEarpiece( |
| 376 | CallAudioState.ROUTE_WIRED_OR_EARPIECE, supportedRouteMask); |
| 377 | |
| 378 | // We want the UI to indicate that "bluetooth is in use" in two slightly different cases: |
| 379 | // (a) The obvious case: if a bluetooth headset is currently in use for an ongoing call. |
| 380 | // (b) The not-so-obvious case: if an incoming call is ringing, and we expect that audio |
| 381 | // *will* be routed to a bluetooth headset once the call is answered. In this case, just |
| 382 | // check if the headset is available. Note this only applies when we are dealing with |
| 383 | // the first call. |
| 384 | if (call != null && mBluetoothManager.isBluetoothAvailable()) { |
| 385 | switch(call.getState()) { |
| 386 | case ACTIVE: |
| 387 | case ON_HOLD: |
| 388 | if (mBluetoothManager.isBluetoothAudioConnectedOrPending()) { |
| 389 | route = CallAudioState.ROUTE_BLUETOOTH; |
| 390 | } |
| 391 | break; |
| 392 | case RINGING: |
| 393 | route = CallAudioState.ROUTE_BLUETOOTH; |
| 394 | break; |
| 395 | default: |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | return new CallAudioState(false, route, supportedRouteMask); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 403 | private void setInitialAudioState(Call call) { |
| 404 | CallAudioState audioState = getInitialAudioState(call); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 405 | setSystemAudioState(audioState.isMuted, audioState.route, audioState.supportedRouteMask); |
| 406 | } |
| 407 | |
| 408 | private void updateAudioForForegroundCall() { |
| 409 | Call call = CallsManager.getInstance().getForegroundCall(); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 410 | if (call != null && call.getConnectionService() != null) { |
| 411 | call.getConnectionService().onAudioStateChanged(call, mAudioState); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 412 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 413 | } |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 414 | |
| 415 | /** |
| 416 | * Returns the current foreground call in order to properly set the audio mode. |
| 417 | */ |
| 418 | private Call getForegroundCall() { |
| 419 | Call call = CallsManager.getInstance().getForegroundCall(); |
| 420 | |
| 421 | // We ignore any foreground call that is in the ringing state because we deal with ringing |
| 422 | // calls exclusively through the mIsRinging variable set by {@link Ringer}. |
| 423 | if (call != null && call.getState() == CallState.RINGING) { |
| 424 | call = null; |
| 425 | } |
| 426 | return call; |
| 427 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 428 | } |