blob: 1e15559e24271a43e81d66b750271f98f1c081f5 [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;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070021import android.telecomm.CallAudioState;
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
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070040 private CallAudioState mAudioState;
41 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
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070059 CallAudioState getAudioState() {
60 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
89 public void onCallStateChanged(Call call, CallState oldState, CallState 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();
103 route = CallAudioState.ROUTE_BLUETOOTH;
104 }
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
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700132 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 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 *
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700170 * @param route The new audio route to use. See {@link CallAudioState}.
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
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700178 Log.v(this, "setAudioRoute, route: %s", CallAudioState.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.
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 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()) {
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 Cordondeb8c892014-05-30 01:38:03 -0700251 private void saveAudioState(CallAudioState audioState) {
252 mAudioState = audioState;
253 mStatusBarNotifier.notifyMute(mAudioState.isMuted);
254 mStatusBarNotifier.notifySpeakerphone(mAudioState.route == CallAudioState.ROUTE_SPEAKER);
255 }
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
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700272 CallAudioState oldAudioState = mAudioState;
Santos Cordondeb8c892014-05-30 01:38:03 -0700273 saveAudioState(new CallAudioState(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.
Santos Cordonc7e85d42014-05-22 02:51:48 -0700286 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 Nepal6aca10a2014-03-24 16:11:02 -0700292 } else if (mAudioState.route == CallAudioState.ROUTE_EARPIECE ||
293 mAudioState.route == CallAudioState.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);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700343 } else {
344 abandonAudioFocus();
345 }
346 }
347 }
348
349 private void requestAudioFocusAndSetMode(int stream, int mode) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700350 Log.v(this, "requestAudioFocusAndSetMode, stream: %d -> %d", mAudioFocusStreamType, stream);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700351 Preconditions.checkState(stream != STREAM_NONE);
352
Santos Cordon5ba7f272014-05-28 13:59:49 -0700353 // 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 Nepal6aca10a2014-03-24 16:11:02 -0700356 Log.v(this, "requesting audio focus for stream: %d", stream);
357 mAudioManager.requestAudioFocusForCall(stream,
358 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
359 }
360 mAudioFocusStreamType = stream;
Santos Cordon14ff8382014-08-05 20:44:27 -0700361
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700362 setMode(mode);
363 }
364
365 private void abandonAudioFocus() {
Santos Cordon14ff8382014-08-05 20:44:27 -0700366 if (hasFocus()) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700367 setMode(AudioManager.MODE_NORMAL);
368 Log.v(this, "abandoning audio focus");
369 mAudioManager.abandonAudioFocusForCall();
370 mAudioFocusStreamType = STREAM_NONE;
371 }
Santos Cordon1ae2b852014-03-19 03:03:10 -0700372 }
373
374 /**
375 * Sets the audio mode.
376 *
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700377 * @param newMode Mode constant from AudioManager.MODE_*.
Santos Cordon1ae2b852014-03-19 03:03:10 -0700378 */
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700379 private void setMode(int newMode) {
Santos Cordon14ff8382014-08-05 20:44:27 -0700380 Preconditions.checkState(hasFocus());
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700381 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 Cordon14ff8382014-08-05 20:44:27 -0700385 mMostRecentlyUsedMode = newMode;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700386 }
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 Cordon1ae2b852014-03-19 03:03:10 -0700399 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700400 }
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 Cordon1ae2b852014-03-19 03:03:10 -0700409 } else {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700410 routeMask |= CallAudioState.ROUTE_EARPIECE;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700411 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700412
Santos Cordonc7e85d42014-05-22 02:51:48 -0700413 if (mBluetoothManager.isBluetoothAvailable()) {
414 routeMask |= CallAudioState.ROUTE_BLUETOOTH;
415 }
416
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700417 return routeMask;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700418 }
419
Santos Cordonc7e85d42014-05-22 02:51:48 -0700420 private CallAudioState getInitialAudioState(Call call) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700421 int supportedRouteMask = calculateSupportedRoutes();
Santos Cordonc7e85d42014-05-22 02:51:48 -0700422 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 Cordon14ff8382014-08-05 20:44:27 -0700435 case DIALING:
Santos Cordonc7e85d42014-05-22 02:51:48 -0700436 case RINGING:
437 route = CallAudioState.ROUTE_BLUETOOTH;
438 break;
439 default:
440 break;
441 }
442 }
443
444 return new CallAudioState(false, route, supportedRouteMask);
Santos Cordon1ae2b852014-03-19 03:03:10 -0700445 }
446
Santos Cordonc7e85d42014-05-22 02:51:48 -0700447 private void setInitialAudioState(Call call) {
448 CallAudioState audioState = getInitialAudioState(call);
Santos Cordon14ff8382014-08-05 20:44:27 -0700449 Log.v(this, "setInitialAudioState %s, %s", audioState, call);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700450 setSystemAudioState(audioState.isMuted, audioState.route, audioState.supportedRouteMask);
451 }
452
453 private void updateAudioForForegroundCall() {
454 Call call = CallsManager.getInstance().getForegroundCall();
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700455 if (call != null && call.getConnectionService() != null) {
456 call.getConnectionService().onAudioStateChanged(call, mAudioState);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700457 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700458 }
Santos Cordon5ba7f272014-05-28 13:59:49 -0700459
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 Cordon14ff8382014-08-05 20:44:27 -0700473
474 private boolean hasFocus() {
475 return mAudioFocusStreamType != STREAM_NONE;
476 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700477}