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