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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioManager; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 21 | import android.telecom.AudioState; |
| 22 | import android.telecom.CallState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 23 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 24 | import com.android.internal.util.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 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 40 | private AudioState mAudioState; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 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 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 59 | AudioState getAudioState() { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 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"); |
Santos Cordon | 7c6a5ec | 2014-09-11 19:47:23 -0700 | [diff] [blame] | 81 | setInitialAudioState(null, false /* force */); |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 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 |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 89 | public void onCallStateChanged(Call call, int oldState, int 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(); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 103 | route = AudioState.ROUTE_BLUETOOTH; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 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 |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 117 | public void onIsVoipAudioModeChanged(Call call) { |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 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 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 132 | int newRoute = AudioState.ROUTE_EARPIECE; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 133 | if (newIsPluggedIn) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 134 | newRoute = AudioState.ROUTE_WIRED_HEADSET; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 135 | } else if (mWasSpeakerOn) { |
| 136 | Call call = getForegroundCall(); |
| 137 | if (call != null && call.isAlive()) { |
| 138 | // Restore the speaker state. |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 139 | newRoute = AudioState.ROUTE_SPEAKER; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 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 | * |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 170 | * @param route The new audio route to use. See {@link AudioState}. |
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 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 178 | Log.v(this, "setAudioRoute, route: %s", AudioState.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. |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 192 | mWasSpeakerOn = newRoute == AudioState.ROUTE_SPEAKER; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 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 | 3195364 | 2014-09-12 03:23:59 -0700 | [diff] [blame] | 231 | int supportedRoutes = calculateSupportedRoutes(); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 232 | int newRoute = mAudioState.route; |
| 233 | if (bluetoothManager.isBluetoothAudioConnectedOrPending()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 234 | newRoute = AudioState.ROUTE_BLUETOOTH; |
| 235 | } else if (mAudioState.route == AudioState.ROUTE_BLUETOOTH) { |
Santos Cordon | 3195364 | 2014-09-12 03:23:59 -0700 | [diff] [blame] | 236 | newRoute = selectWiredOrEarpiece(AudioState.ROUTE_WIRED_OR_EARPIECE, supportedRoutes); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 237 | // Do not switch to speaker when bluetooth disconnects. |
| 238 | mWasSpeakerOn = false; |
| 239 | } |
| 240 | |
Santos Cordon | 3195364 | 2014-09-12 03:23:59 -0700 | [diff] [blame] | 241 | setSystemAudioState(mAudioState.isMuted, newRoute, supportedRoutes); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | boolean isBluetoothAudioOn() { |
| 245 | return mBluetoothManager.isBluetoothAudioConnected(); |
| 246 | } |
| 247 | |
| 248 | boolean isBluetoothDeviceAvailable() { |
| 249 | return mBluetoothManager.isBluetoothAvailable(); |
| 250 | } |
| 251 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 252 | private void saveAudioState(AudioState audioState) { |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 253 | mAudioState = audioState; |
| 254 | mStatusBarNotifier.notifyMute(mAudioState.isMuted); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 255 | mStatusBarNotifier.notifySpeakerphone(mAudioState.route == AudioState.ROUTE_SPEAKER); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 258 | private void onCallUpdated(Call call) { |
| 259 | boolean wasNotVoiceCall = mAudioFocusStreamType != AudioManager.STREAM_VOICE_CALL; |
| 260 | updateAudioStreamAndMode(); |
| 261 | |
| 262 | // If we transition from not voice call to voice call, we need to set an initial state. |
| 263 | if (wasNotVoiceCall && mAudioFocusStreamType == AudioManager.STREAM_VOICE_CALL) { |
Santos Cordon | 7c6a5ec | 2014-09-11 19:47:23 -0700 | [diff] [blame] | 264 | setInitialAudioState(call, true /* force */); |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 268 | private void setSystemAudioState(boolean isMuted, int route, int supportedRouteMask) { |
Santos Cordon | 7c6a5ec | 2014-09-11 19:47:23 -0700 | [diff] [blame] | 269 | setSystemAudioState(false /* force */, isMuted, route, supportedRouteMask); |
| 270 | } |
| 271 | |
| 272 | private void setSystemAudioState( |
| 273 | boolean force, boolean isMuted, int route, int supportedRouteMask) { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 274 | if (!hasFocus()) { |
| 275 | return; |
| 276 | } |
| 277 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 278 | AudioState oldAudioState = mAudioState; |
| 279 | saveAudioState(new AudioState(isMuted, route, supportedRouteMask)); |
Santos Cordon | 7c6a5ec | 2014-09-11 19:47:23 -0700 | [diff] [blame] | 280 | if (!force && Objects.equals(oldAudioState, mAudioState)) { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 283 | Log.i(this, "changing audio state from %s to %s", oldAudioState, mAudioState); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 284 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 285 | // Mute. |
| 286 | if (mAudioState.isMuted != mAudioManager.isMicrophoneMute()) { |
| 287 | Log.i(this, "changing microphone mute state to: %b", mAudioState.isMuted); |
| 288 | mAudioManager.setMicrophoneMute(mAudioState.isMuted); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 291 | // Audio route. |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 292 | if (mAudioState.route == AudioState.ROUTE_BLUETOOTH) { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 293 | turnOnSpeaker(false); |
| 294 | turnOnBluetooth(true); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 295 | } else if (mAudioState.route == AudioState.ROUTE_SPEAKER) { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 296 | turnOnBluetooth(false); |
| 297 | turnOnSpeaker(true); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 298 | } else if (mAudioState.route == AudioState.ROUTE_EARPIECE || |
| 299 | mAudioState.route == AudioState.ROUTE_WIRED_HEADSET) { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 300 | turnOnBluetooth(false); |
| 301 | turnOnSpeaker(false); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if (!oldAudioState.equals(mAudioState)) { |
| 305 | CallsManager.getInstance().onAudioStateChanged(oldAudioState, mAudioState); |
| 306 | updateAudioForForegroundCall(); |
| 307 | } |
| 308 | } |
| 309 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 310 | private void turnOnSpeaker(boolean on) { |
| 311 | // Wired headset and earpiece work the same way |
| 312 | if (mAudioManager.isSpeakerphoneOn() != on) { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 313 | Log.i(this, "turning speaker phone %s", on); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 314 | mAudioManager.setSpeakerphoneOn(on); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | private void turnOnBluetooth(boolean on) { |
| 319 | if (mBluetoothManager.isBluetoothAvailable()) { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 320 | boolean isAlreadyOn = mBluetoothManager.isBluetoothAudioConnectedOrPending(); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 321 | if (on != isAlreadyOn) { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 322 | Log.i(this, "connecting bluetooth %s", on); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 323 | if (on) { |
| 324 | mBluetoothManager.connectBluetoothAudio(); |
| 325 | } else { |
| 326 | mBluetoothManager.disconnectBluetoothAudio(); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 332 | private void updateAudioStreamAndMode() { |
Yorke Lee | e5a7c92 | 2014-10-16 16:50:51 -0700 | [diff] [blame^] | 333 | Log.i(this, "updateAudioStreamAndMode, mIsRinging: %b, mIsTonePlaying: %b", mIsRinging, |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 334 | mIsTonePlaying); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 335 | if (mIsRinging) { |
| 336 | requestAudioFocusAndSetMode(AudioManager.STREAM_RING, AudioManager.MODE_RINGTONE); |
| 337 | } else { |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 338 | Call call = getForegroundCall(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 339 | if (call != null) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 340 | int mode = call.getIsVoipAudioMode() ? |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 341 | AudioManager.MODE_IN_COMMUNICATION : AudioManager.MODE_IN_CALL; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 342 | requestAudioFocusAndSetMode(AudioManager.STREAM_VOICE_CALL, mode); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 343 | } else if (mIsTonePlaying) { |
| 344 | // 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] | 345 | // Since there is no call from which to determine the mode, use the most |
| 346 | // recently used mode instead. |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 347 | requestAudioFocusAndSetMode( |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 348 | AudioManager.STREAM_VOICE_CALL, mMostRecentlyUsedMode); |
Yorke Lee | 42afb97 | 2014-09-08 09:47:21 -0700 | [diff] [blame] | 349 | } else if (!hasRingingForegroundCall()) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 350 | abandonAudioFocus(); |
Yorke Lee | 42afb97 | 2014-09-08 09:47:21 -0700 | [diff] [blame] | 351 | } else { |
| 352 | // mIsRinging is false, but there is a foreground ringing call present. Don't |
| 353 | // abandon audio focus immediately to prevent audio focus from getting lost between |
| 354 | // the time it takes for the foreground call to transition from RINGING to ACTIVE/ |
| 355 | // DISCONNECTED. When the call eventually transitions to the next state, audio |
| 356 | // focus will be correctly abandoned by the if clause above. |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | private void requestAudioFocusAndSetMode(int stream, int mode) { |
Yorke Lee | e5a7c92 | 2014-10-16 16:50:51 -0700 | [diff] [blame^] | 362 | Log.i(this, "requestAudioFocusAndSetMode, stream: %d -> %d", mAudioFocusStreamType, stream); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 363 | Preconditions.checkState(stream != STREAM_NONE); |
| 364 | |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 365 | // Even if we already have focus, if the stream is different we update audio manager to give |
| 366 | // it a hint about the purpose of our focus. |
| 367 | if (mAudioFocusStreamType != stream) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 368 | Log.v(this, "requesting audio focus for stream: %d", stream); |
| 369 | mAudioManager.requestAudioFocusForCall(stream, |
| 370 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 371 | } |
| 372 | mAudioFocusStreamType = stream; |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 373 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 374 | setMode(mode); |
| 375 | } |
| 376 | |
| 377 | private void abandonAudioFocus() { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 378 | if (hasFocus()) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 379 | setMode(AudioManager.MODE_NORMAL); |
| 380 | Log.v(this, "abandoning audio focus"); |
| 381 | mAudioManager.abandonAudioFocusForCall(); |
| 382 | mAudioFocusStreamType = STREAM_NONE; |
| 383 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Sets the audio mode. |
| 388 | * |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 389 | * @param newMode Mode constant from AudioManager.MODE_*. |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 390 | */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 391 | private void setMode(int newMode) { |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 392 | Preconditions.checkState(hasFocus()); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 393 | int oldMode = mAudioManager.getMode(); |
| 394 | Log.v(this, "Request to change audio mode from %d to %d", oldMode, newMode); |
| 395 | if (oldMode != newMode) { |
| 396 | mAudioManager.setMode(newMode); |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 397 | mMostRecentlyUsedMode = newMode; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | private int selectWiredOrEarpiece(int route, int supportedRouteMask) { |
| 402 | // Since they are mutually exclusive and one is ALWAYS valid, we allow a special input of |
| 403 | // ROUTE_WIRED_OR_EARPIECE so that callers dont have to make a call to check which is |
| 404 | // supported before calling setAudioRoute. |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 405 | if (route == AudioState.ROUTE_WIRED_OR_EARPIECE) { |
| 406 | route = AudioState.ROUTE_WIRED_OR_EARPIECE & supportedRouteMask; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 407 | if (route == 0) { |
| 408 | Log.wtf(this, "One of wired headset or earpiece should always be valid."); |
| 409 | // assume earpiece in this case. |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 410 | route = AudioState.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 | } |
| 413 | return route; |
| 414 | } |
| 415 | |
| 416 | private int calculateSupportedRoutes() { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 417 | int routeMask = AudioState.ROUTE_SPEAKER; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 418 | |
| 419 | if (mWiredHeadsetManager.isPluggedIn()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 420 | routeMask |= AudioState.ROUTE_WIRED_HEADSET; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 421 | } else { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 422 | routeMask |= AudioState.ROUTE_EARPIECE; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 423 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 424 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 425 | if (mBluetoothManager.isBluetoothAvailable()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 426 | routeMask |= AudioState.ROUTE_BLUETOOTH; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 429 | return routeMask; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 432 | private AudioState getInitialAudioState(Call call) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 433 | int supportedRouteMask = calculateSupportedRoutes(); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 434 | int route = selectWiredOrEarpiece( |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 435 | AudioState.ROUTE_WIRED_OR_EARPIECE, supportedRouteMask); |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 436 | |
| 437 | // We want the UI to indicate that "bluetooth is in use" in two slightly different cases: |
| 438 | // (a) The obvious case: if a bluetooth headset is currently in use for an ongoing call. |
| 439 | // (b) The not-so-obvious case: if an incoming call is ringing, and we expect that audio |
| 440 | // *will* be routed to a bluetooth headset once the call is answered. In this case, just |
| 441 | // check if the headset is available. Note this only applies when we are dealing with |
| 442 | // the first call. |
| 443 | if (call != null && mBluetoothManager.isBluetoothAvailable()) { |
| 444 | switch(call.getState()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 445 | case CallState.ACTIVE: |
| 446 | case CallState.ON_HOLD: |
| 447 | case CallState.DIALING: |
Santos Cordon | 5753f30 | 2014-09-26 13:56:16 -0700 | [diff] [blame] | 448 | case CallState.CONNECTING: |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 449 | case CallState.RINGING: |
| 450 | route = AudioState.ROUTE_BLUETOOTH; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 451 | break; |
| 452 | default: |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 457 | return new AudioState(false, route, supportedRouteMask); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Santos Cordon | 7c6a5ec | 2014-09-11 19:47:23 -0700 | [diff] [blame] | 460 | private void setInitialAudioState(Call call, boolean force) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 461 | AudioState audioState = getInitialAudioState(call); |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 462 | Log.v(this, "setInitialAudioState %s, %s", audioState, call); |
Santos Cordon | 7c6a5ec | 2014-09-11 19:47:23 -0700 | [diff] [blame] | 463 | setSystemAudioState( |
| 464 | force, audioState.isMuted, audioState.route, audioState.supportedRouteMask); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | private void updateAudioForForegroundCall() { |
| 468 | Call call = CallsManager.getInstance().getForegroundCall(); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 469 | if (call != null && call.getConnectionService() != null) { |
| 470 | call.getConnectionService().onAudioStateChanged(call, mAudioState); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 471 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 472 | } |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Returns the current foreground call in order to properly set the audio mode. |
| 476 | */ |
| 477 | private Call getForegroundCall() { |
| 478 | Call call = CallsManager.getInstance().getForegroundCall(); |
| 479 | |
| 480 | // We ignore any foreground call that is in the ringing state because we deal with ringing |
| 481 | // calls exclusively through the mIsRinging variable set by {@link Ringer}. |
| 482 | if (call != null && call.getState() == CallState.RINGING) { |
| 483 | call = null; |
| 484 | } |
| 485 | return call; |
| 486 | } |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 487 | |
Yorke Lee | 42afb97 | 2014-09-08 09:47:21 -0700 | [diff] [blame] | 488 | private boolean hasRingingForegroundCall() { |
| 489 | Call call = CallsManager.getInstance().getForegroundCall(); |
| 490 | return call != null && call.getState() == CallState.RINGING; |
| 491 | } |
| 492 | |
Santos Cordon | 14ff838 | 2014-08-05 20:44:27 -0700 | [diff] [blame] | 493 | private boolean hasFocus() { |
| 494 | return mAudioFocusStreamType != STREAM_NONE; |
| 495 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 496 | } |