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