blob: 164bedb60da32ef112503be6158e997503a48a75 [file] [log] [blame]
Sailesh Nepal810735e2014-03-18 18:15:46 -07001/*
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
17package com.android.telecomm;
18
19import android.content.Context;
20import android.media.AudioManager;
Ihab Awad6fb37c82014-08-07 19:48:57 -070021import android.telecomm.AudioState;
Sailesh Nepal810735e2014-03-18 18:15:46 -070022import android.telecomm.CallState;
23
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070024import com.google.common.base.Preconditions;
Santos Cordon1ae2b852014-03-19 03:03:10 -070025
Santos Cordon14ff8382014-08-05 20:44:27 -070026import java.util.Objects;
27
Sailesh Nepal810735e2014-03-18 18:15:46 -070028/**
29 * This class manages audio modes, streams and other properties.
30 */
Sailesh Nepalb88795a2014-07-15 14:53:27 -070031final class CallAudioManager extends CallsManagerListenerBase
32 implements WiredHeadsetManager.Listener {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070033 private static final int STREAM_NONE = -1;
Santos Cordon1ae2b852014-03-19 03:03:10 -070034
Santos Cordondeb8c892014-05-30 01:38:03 -070035 private final StatusBarNotifier mStatusBarNotifier;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070036 private final AudioManager mAudioManager;
Santos Cordonc7e85d42014-05-22 02:51:48 -070037 private final BluetoothManager mBluetoothManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070038 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordondeb8c892014-05-30 01:38:03 -070039
Ihab Awad6fb37c82014-08-07 19:48:57 -070040 private AudioState mAudioState;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070041 private int mAudioFocusStreamType;
42 private boolean mIsRinging;
Santos Cordona56f2762014-03-24 15:55:53 -070043 private boolean mIsTonePlaying;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070044 private boolean mWasSpeakerOn;
Santos Cordon14ff8382014-08-05 20:44:27 -070045 private int mMostRecentlyUsedMode = AudioManager.MODE_IN_CALL;
Santos Cordon1ae2b852014-03-19 03:03:10 -070046
Sailesh Nepalb88795a2014-07-15 14:53:27 -070047 CallAudioManager(Context context, StatusBarNotifier statusBarNotifier,
48 WiredHeadsetManager wiredHeadsetManager) {
Santos Cordondeb8c892014-05-30 01:38:03 -070049 mStatusBarNotifier = statusBarNotifier;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070050 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Santos Cordonc7e85d42014-05-22 02:51:48 -070051 mBluetoothManager = new BluetoothManager(context, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -070052 mWiredHeadsetManager = wiredHeadsetManager;
Sailesh Nepald0a76aa2014-07-16 22:12:23 -070053 mWiredHeadsetManager.addListener(this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -070054
Santos Cordondeb8c892014-05-30 01:38:03 -070055 saveAudioState(getInitialAudioState(null));
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070056 mAudioFocusStreamType = STREAM_NONE;
57 }
Santos Cordon1ae2b852014-03-19 03:03:10 -070058
Ihab Awad6fb37c82014-08-07 19:48:57 -070059 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070060 return mAudioState;
61 }
Santos Cordon1ae2b852014-03-19 03:03:10 -070062
63 @Override
64 public void onCallAdded(Call call) {
Santos Cordon14ff8382014-08-05 20:44:27 -070065 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 Cordon1ae2b852014-03-19 03:03:10 -070072 }
Santos Cordon1ae2b852014-03-19 03:03:10 -070073 }
74
75 @Override
76 public void onCallRemoved(Call call) {
Santos Cordon14ff8382014-08-05 20:44:27 -070077 // 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 Nepal6aca10a2014-03-24 16:11:02 -070085 }
Santos Cordon1ae2b852014-03-19 03:03:10 -070086 }
87
Sailesh Nepal810735e2014-03-18 18:15:46 -070088 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -070089 public void onCallStateChanged(Call call, int oldState, int newState) {
Santos Cordon14ff8382014-08-05 20:44:27 -070090 onCallUpdated(call);
Santos Cordon1ae2b852014-03-19 03:03:10 -070091 }
92
93 @Override
94 public void onIncomingCallAnswered(Call call) {
Santos Cordonc7e85d42014-05-22 02:51:48 -070095 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 Awad6fb37c82014-08-07 19:48:57 -0700103 route = AudioState.ROUTE_BLUETOOTH;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700104 }
105
106 setSystemAudioState(false /* isMute */, route, mAudioState.supportedRouteMask);
Santos Cordon1ae2b852014-03-19 03:03:10 -0700107 }
108
109 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700110 public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700111 onCallUpdated(newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700112 // Ensure that the foreground call knows about the latest audio state.
113 updateAudioForForegroundCall();
Santos Cordon1ae2b852014-03-19 03:03:10 -0700114 }
115
Sailesh Nepal7e669572014-07-08 21:29:12 -0700116 @Override
117 public void onAudioModeIsVoipChanged(Call call) {
118 updateAudioStreamAndMode();
119 }
120
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700121 /**
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 Cordon14ff8382014-08-05 20:44:27 -0700127 // This can happen even when there are no calls and we don't have focus.
128 if (!hasFocus()) {
129 return;
130 }
131
Ihab Awad6fb37c82014-08-07 19:48:57 -0700132 int newRoute = AudioState.ROUTE_EARPIECE;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700133 if (newIsPluggedIn) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700134 newRoute = AudioState.ROUTE_WIRED_HEADSET;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700135 } else if (mWasSpeakerOn) {
136 Call call = getForegroundCall();
137 if (call != null && call.isAlive()) {
138 // Restore the speaker state.
Ihab Awad6fb37c82014-08-07 19:48:57 -0700139 newRoute = AudioState.ROUTE_SPEAKER;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700140 }
141 }
142 setSystemAudioState(mAudioState.isMuted, newRoute, calculateSupportedRoutes());
143 }
144
Santos Cordondeb8c892014-05-30 01:38:03 -0700145 void toggleMute() {
146 mute(!mAudioState.isMuted);
147 }
148
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700149 void mute(boolean shouldMute) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700150 if (!hasFocus()) {
151 return;
152 }
153
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700154 Log.v(this, "mute, shouldMute: %b", shouldMute);
Santos Cordon1ae2b852014-03-19 03:03:10 -0700155
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700156 // 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 Cordon1ae2b852014-03-19 03:03:10 -0700160 }
161
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700162 if (mAudioState.isMuted != shouldMute) {
163 setSystemAudioState(shouldMute, mAudioState.route, mAudioState.supportedRouteMask);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700164 }
165 }
166
Santos Cordon1ae2b852014-03-19 03:03:10 -0700167 /**
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700168 * Changed the audio route, for example from earpiece to speaker phone.
Santos Cordon1ae2b852014-03-19 03:03:10 -0700169 *
Ihab Awad6fb37c82014-08-07 19:48:57 -0700170 * @param route The new audio route to use. See {@link AudioState}.
Santos Cordon1ae2b852014-03-19 03:03:10 -0700171 */
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700172 void setAudioRoute(int route) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700173 // This can happen even when there are no calls and we don't have focus.
174 if (!hasFocus()) {
175 return;
176 }
177
Ihab Awad6fb37c82014-08-07 19:48:57 -0700178 Log.v(this, "setAudioRoute, route: %s", AudioState.audioRouteToString(route));
Santos Cordon1ae2b852014-03-19 03:03:10 -0700179
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700180 // 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 Awad6fb37c82014-08-07 19:48:57 -0700192 mWasSpeakerOn = newRoute == AudioState.ROUTE_SPEAKER;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700193 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 Cordon1ae2b852014-03-19 03:03:10 -0700202 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700203 }
204
Santos Cordon1ae2b852014-03-19 03:03:10 -0700205 /**
Santos Cordona56f2762014-03-24 15:55:53 -0700206 * 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 Cordonc7e85d42014-05-22 02:51:48 -0700223 * Updates the audio routing according to the bluetooth state.
224 */
225 void onBluetoothStateChange(BluetoothManager bluetoothManager) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700226 // This can happen even when there are no calls and we don't have focus.
227 if (!hasFocus()) {
228 return;
229 }
230
Santos Cordonc7e85d42014-05-22 02:51:48 -0700231 int newRoute = mAudioState.route;
232 if (bluetoothManager.isBluetoothAudioConnectedOrPending()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700233 newRoute = AudioState.ROUTE_BLUETOOTH;
234 } else if (mAudioState.route == AudioState.ROUTE_BLUETOOTH) {
235 newRoute = AudioState.ROUTE_WIRED_OR_EARPIECE;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700236 // 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
Ihab Awad6fb37c82014-08-07 19:48:57 -0700251 private void saveAudioState(AudioState audioState) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700252 mAudioState = audioState;
253 mStatusBarNotifier.notifyMute(mAudioState.isMuted);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700254 mStatusBarNotifier.notifySpeakerphone(mAudioState.route == AudioState.ROUTE_SPEAKER);
Santos Cordondeb8c892014-05-30 01:38:03 -0700255 }
256
Santos Cordon14ff8382014-08-05 20:44:27 -0700257 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 Nepal6aca10a2014-03-24 16:11:02 -0700267 private void setSystemAudioState(boolean isMuted, int route, int supportedRouteMask) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700268 if (!hasFocus()) {
269 return;
270 }
271
Ihab Awad6fb37c82014-08-07 19:48:57 -0700272 AudioState oldAudioState = mAudioState;
273 saveAudioState(new AudioState(isMuted, route, supportedRouteMask));
Santos Cordon14ff8382014-08-05 20:44:27 -0700274 if (Objects.equals(oldAudioState, mAudioState)) {
275 return;
276 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700277 Log.i(this, "changing audio state from %s to %s", oldAudioState, mAudioState);
Santos Cordon1ae2b852014-03-19 03:03:10 -0700278
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700279 // 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 Cordon1ae2b852014-03-19 03:03:10 -0700283 }
284
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700285 // Audio route.
Ihab Awad6fb37c82014-08-07 19:48:57 -0700286 if (mAudioState.route == AudioState.ROUTE_BLUETOOTH) {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700287 turnOnSpeaker(false);
288 turnOnBluetooth(true);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700289 } else if (mAudioState.route == AudioState.ROUTE_SPEAKER) {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700290 turnOnBluetooth(false);
291 turnOnSpeaker(true);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700292 } else if (mAudioState.route == AudioState.ROUTE_EARPIECE ||
293 mAudioState.route == AudioState.ROUTE_WIRED_HEADSET) {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700294 turnOnBluetooth(false);
295 turnOnSpeaker(false);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700296 }
297
298 if (!oldAudioState.equals(mAudioState)) {
299 CallsManager.getInstance().onAudioStateChanged(oldAudioState, mAudioState);
300 updateAudioForForegroundCall();
301 }
302 }
303
Santos Cordonc7e85d42014-05-22 02:51:48 -0700304 private void turnOnSpeaker(boolean on) {
305 // Wired headset and earpiece work the same way
306 if (mAudioManager.isSpeakerphoneOn() != on) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700307 Log.i(this, "turning speaker phone %s", on);
Santos Cordonc7e85d42014-05-22 02:51:48 -0700308 mAudioManager.setSpeakerphoneOn(on);
309 }
310 }
311
312 private void turnOnBluetooth(boolean on) {
313 if (mBluetoothManager.isBluetoothAvailable()) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700314 boolean isAlreadyOn = mBluetoothManager.isBluetoothAudioConnectedOrPending();
Santos Cordonc7e85d42014-05-22 02:51:48 -0700315 if (on != isAlreadyOn) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700316 Log.i(this, "connecting bluetooth %s", on);
Santos Cordonc7e85d42014-05-22 02:51:48 -0700317 if (on) {
318 mBluetoothManager.connectBluetoothAudio();
319 } else {
320 mBluetoothManager.disconnectBluetoothAudio();
321 }
322 }
323 }
324 }
325
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700326 private void updateAudioStreamAndMode() {
Santos Cordona56f2762014-03-24 15:55:53 -0700327 Log.v(this, "updateAudioStreamAndMode, mIsRinging: %b, mIsTonePlaying: %b", mIsRinging,
328 mIsTonePlaying);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700329 if (mIsRinging) {
330 requestAudioFocusAndSetMode(AudioManager.STREAM_RING, AudioManager.MODE_RINGTONE);
331 } else {
Santos Cordon5ba7f272014-05-28 13:59:49 -0700332 Call call = getForegroundCall();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700333 if (call != null) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700334 int mode = call.getAudioModeIsVoip() ?
335 AudioManager.MODE_IN_COMMUNICATION : AudioManager.MODE_IN_CALL;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700336 requestAudioFocusAndSetMode(AudioManager.STREAM_VOICE_CALL, mode);
Santos Cordona56f2762014-03-24 15:55:53 -0700337 } else if (mIsTonePlaying) {
338 // There is no call, however, we are still playing a tone, so keep focus.
Santos Cordon14ff8382014-08-05 20:44:27 -0700339 // Since there is no call from which to determine the mode, use the most
340 // recently used mode instead.
Santos Cordona56f2762014-03-24 15:55:53 -0700341 requestAudioFocusAndSetMode(
Santos Cordon14ff8382014-08-05 20:44:27 -0700342 AudioManager.STREAM_VOICE_CALL, mMostRecentlyUsedMode);
Yorke Lee42afb972014-09-08 09:47:21 -0700343 } else if (!hasRingingForegroundCall()) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700344 abandonAudioFocus();
Yorke Lee42afb972014-09-08 09:47:21 -0700345 } else {
346 // mIsRinging is false, but there is a foreground ringing call present. Don't
347 // abandon audio focus immediately to prevent audio focus from getting lost between
348 // the time it takes for the foreground call to transition from RINGING to ACTIVE/
349 // DISCONNECTED. When the call eventually transitions to the next state, audio
350 // focus will be correctly abandoned by the if clause above.
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700351 }
352 }
353 }
354
355 private void requestAudioFocusAndSetMode(int stream, int mode) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700356 Log.v(this, "requestAudioFocusAndSetMode, stream: %d -> %d", mAudioFocusStreamType, stream);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700357 Preconditions.checkState(stream != STREAM_NONE);
358
Santos Cordon5ba7f272014-05-28 13:59:49 -0700359 // Even if we already have focus, if the stream is different we update audio manager to give
360 // it a hint about the purpose of our focus.
361 if (mAudioFocusStreamType != stream) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700362 Log.v(this, "requesting audio focus for stream: %d", stream);
363 mAudioManager.requestAudioFocusForCall(stream,
364 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
365 }
366 mAudioFocusStreamType = stream;
Santos Cordon14ff8382014-08-05 20:44:27 -0700367
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700368 setMode(mode);
369 }
370
371 private void abandonAudioFocus() {
Santos Cordon14ff8382014-08-05 20:44:27 -0700372 if (hasFocus()) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700373 setMode(AudioManager.MODE_NORMAL);
374 Log.v(this, "abandoning audio focus");
375 mAudioManager.abandonAudioFocusForCall();
376 mAudioFocusStreamType = STREAM_NONE;
377 }
Santos Cordon1ae2b852014-03-19 03:03:10 -0700378 }
379
380 /**
381 * Sets the audio mode.
382 *
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700383 * @param newMode Mode constant from AudioManager.MODE_*.
Santos Cordon1ae2b852014-03-19 03:03:10 -0700384 */
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700385 private void setMode(int newMode) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700386 Preconditions.checkState(hasFocus());
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700387 int oldMode = mAudioManager.getMode();
388 Log.v(this, "Request to change audio mode from %d to %d", oldMode, newMode);
389 if (oldMode != newMode) {
390 mAudioManager.setMode(newMode);
Santos Cordon14ff8382014-08-05 20:44:27 -0700391 mMostRecentlyUsedMode = newMode;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700392 }
393 }
394
395 private int selectWiredOrEarpiece(int route, int supportedRouteMask) {
396 // Since they are mutually exclusive and one is ALWAYS valid, we allow a special input of
397 // ROUTE_WIRED_OR_EARPIECE so that callers dont have to make a call to check which is
398 // supported before calling setAudioRoute.
Ihab Awad6fb37c82014-08-07 19:48:57 -0700399 if (route == AudioState.ROUTE_WIRED_OR_EARPIECE) {
400 route = AudioState.ROUTE_WIRED_OR_EARPIECE & supportedRouteMask;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700401 if (route == 0) {
402 Log.wtf(this, "One of wired headset or earpiece should always be valid.");
403 // assume earpiece in this case.
Ihab Awad6fb37c82014-08-07 19:48:57 -0700404 route = AudioState.ROUTE_EARPIECE;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700405 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700406 }
407 return route;
408 }
409
410 private int calculateSupportedRoutes() {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700411 int routeMask = AudioState.ROUTE_SPEAKER;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700412
413 if (mWiredHeadsetManager.isPluggedIn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700414 routeMask |= AudioState.ROUTE_WIRED_HEADSET;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700415 } else {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700416 routeMask |= AudioState.ROUTE_EARPIECE;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700417 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700418
Santos Cordonc7e85d42014-05-22 02:51:48 -0700419 if (mBluetoothManager.isBluetoothAvailable()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700420 routeMask |= AudioState.ROUTE_BLUETOOTH;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700421 }
422
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700423 return routeMask;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700424 }
425
Ihab Awad6fb37c82014-08-07 19:48:57 -0700426 private AudioState getInitialAudioState(Call call) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700427 int supportedRouteMask = calculateSupportedRoutes();
Santos Cordonc7e85d42014-05-22 02:51:48 -0700428 int route = selectWiredOrEarpiece(
Ihab Awad6fb37c82014-08-07 19:48:57 -0700429 AudioState.ROUTE_WIRED_OR_EARPIECE, supportedRouteMask);
Santos Cordonc7e85d42014-05-22 02:51:48 -0700430
431 // We want the UI to indicate that "bluetooth is in use" in two slightly different cases:
432 // (a) The obvious case: if a bluetooth headset is currently in use for an ongoing call.
433 // (b) The not-so-obvious case: if an incoming call is ringing, and we expect that audio
434 // *will* be routed to a bluetooth headset once the call is answered. In this case, just
435 // check if the headset is available. Note this only applies when we are dealing with
436 // the first call.
437 if (call != null && mBluetoothManager.isBluetoothAvailable()) {
438 switch(call.getState()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700439 case CallState.ACTIVE:
440 case CallState.ON_HOLD:
441 case CallState.DIALING:
442 case CallState.RINGING:
443 route = AudioState.ROUTE_BLUETOOTH;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700444 break;
445 default:
446 break;
447 }
448 }
449
Ihab Awad6fb37c82014-08-07 19:48:57 -0700450 return new AudioState(false, route, supportedRouteMask);
Santos Cordon1ae2b852014-03-19 03:03:10 -0700451 }
452
Santos Cordonc7e85d42014-05-22 02:51:48 -0700453 private void setInitialAudioState(Call call) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700454 AudioState audioState = getInitialAudioState(call);
Santos Cordon14ff8382014-08-05 20:44:27 -0700455 Log.v(this, "setInitialAudioState %s, %s", audioState, call);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700456 setSystemAudioState(audioState.isMuted, audioState.route, audioState.supportedRouteMask);
457 }
458
459 private void updateAudioForForegroundCall() {
460 Call call = CallsManager.getInstance().getForegroundCall();
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700461 if (call != null && call.getConnectionService() != null) {
462 call.getConnectionService().onAudioStateChanged(call, mAudioState);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700463 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700464 }
Santos Cordon5ba7f272014-05-28 13:59:49 -0700465
466 /**
467 * Returns the current foreground call in order to properly set the audio mode.
468 */
469 private Call getForegroundCall() {
470 Call call = CallsManager.getInstance().getForegroundCall();
471
472 // We ignore any foreground call that is in the ringing state because we deal with ringing
473 // calls exclusively through the mIsRinging variable set by {@link Ringer}.
474 if (call != null && call.getState() == CallState.RINGING) {
475 call = null;
476 }
477 return call;
478 }
Santos Cordon14ff8382014-08-05 20:44:27 -0700479
Yorke Lee42afb972014-09-08 09:47:21 -0700480 private boolean hasRingingForegroundCall() {
481 Call call = CallsManager.getInstance().getForegroundCall();
482 return call != null && call.getState() == CallState.RINGING;
483 }
484
Santos Cordon14ff8382014-08-05 20:44:27 -0700485 private boolean hasFocus() {
486 return mAudioFocusStreamType != STREAM_NONE;
487 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700488}