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