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