blob: 96646b2e684a4a4b5078320e6e1875050e38ced6 [file] [log] [blame]
Hall Liuf62630a2015-10-27 14:53:39 -07001/*
2 * Copyright (C) 2015 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.server.telecom;
18
Hall Liu486cb192016-10-21 18:23:33 -070019import com.android.server.telecom.bluetooth.BluetoothRouteManager;
20
Hall Liuf62630a2015-10-27 14:53:39 -070021/**
22 * A class that acts as a listener to things that could change call audio routing, namely
23 * bluetooth status, wired headset status, and dock status.
24 */
Hall Liue74af082016-02-10 16:12:47 -080025public class CallAudioRoutePeripheralAdapter implements WiredHeadsetManager.Listener,
Hall Liu486cb192016-10-21 18:23:33 -070026 DockManager.Listener, BluetoothRouteManager.BluetoothStateListener {
Hall Liue74af082016-02-10 16:12:47 -080027
Hall Liuf62630a2015-10-27 14:53:39 -070028 private final CallAudioRouteStateMachine mCallAudioRouteStateMachine;
Hall Liu486cb192016-10-21 18:23:33 -070029 private final BluetoothRouteManager mBluetoothRouteManager;
Hall Liuf62630a2015-10-27 14:53:39 -070030
31 public CallAudioRoutePeripheralAdapter(
32 CallAudioRouteStateMachine callAudioRouteStateMachine,
Hall Liu486cb192016-10-21 18:23:33 -070033 BluetoothRouteManager bluetoothManager,
Hall Liuf62630a2015-10-27 14:53:39 -070034 WiredHeadsetManager wiredHeadsetManager,
35 DockManager dockManager) {
36 mCallAudioRouteStateMachine = callAudioRouteStateMachine;
Hall Liu486cb192016-10-21 18:23:33 -070037 mBluetoothRouteManager = bluetoothManager;
Hall Liuf62630a2015-10-27 14:53:39 -070038
Hall Liu486cb192016-10-21 18:23:33 -070039 mBluetoothRouteManager.setListener(this);
Hall Liuf62630a2015-10-27 14:53:39 -070040 wiredHeadsetManager.addListener(this);
41 dockManager.addListener(this);
42 }
43
Hall Liuf62630a2015-10-27 14:53:39 -070044 public boolean isBluetoothAudioOn() {
Hall Liu486cb192016-10-21 18:23:33 -070045 return mBluetoothRouteManager.isBluetoothAudioConnectedOrPending();
Hall Liuf62630a2015-10-27 14:53:39 -070046 }
47
Hall Liue74af082016-02-10 16:12:47 -080048 @Override
49 public void onBluetoothStateChange(int oldState, int newState) {
50 switch (oldState) {
Hall Liu486cb192016-10-21 18:23:33 -070051 case BluetoothRouteManager.BLUETOOTH_DISCONNECTED:
52 case BluetoothRouteManager.BLUETOOTH_UNINITIALIZED:
Hall Liue74af082016-02-10 16:12:47 -080053 switch (newState) {
Hall Liu486cb192016-10-21 18:23:33 -070054 case BluetoothRouteManager.BLUETOOTH_DEVICE_CONNECTED:
55 case BluetoothRouteManager.BLUETOOTH_AUDIO_CONNECTED:
Hall Liue74af082016-02-10 16:12:47 -080056 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
57 CallAudioRouteStateMachine.CONNECT_BLUETOOTH);
58 break;
59 }
60 break;
Hall Liu486cb192016-10-21 18:23:33 -070061 case BluetoothRouteManager.BLUETOOTH_DEVICE_CONNECTED:
Hall Liue74af082016-02-10 16:12:47 -080062 switch (newState) {
Hall Liu486cb192016-10-21 18:23:33 -070063 case BluetoothRouteManager.BLUETOOTH_DISCONNECTED:
Hall Liue74af082016-02-10 16:12:47 -080064 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
65 CallAudioRouteStateMachine.DISCONNECT_BLUETOOTH);
66 break;
Hall Liu486cb192016-10-21 18:23:33 -070067 case BluetoothRouteManager.BLUETOOTH_AUDIO_CONNECTED:
Hall Liue74af082016-02-10 16:12:47 -080068 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
69 CallAudioRouteStateMachine.SWITCH_BLUETOOTH);
70 break;
71 }
72 break;
Hall Liu486cb192016-10-21 18:23:33 -070073 case BluetoothRouteManager.BLUETOOTH_AUDIO_CONNECTED:
74 case BluetoothRouteManager.BLUETOOTH_AUDIO_PENDING:
Hall Liue74af082016-02-10 16:12:47 -080075 switch (newState) {
Hall Liu486cb192016-10-21 18:23:33 -070076 case BluetoothRouteManager.BLUETOOTH_DISCONNECTED:
Hall Liue74af082016-02-10 16:12:47 -080077 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
78 CallAudioRouteStateMachine.DISCONNECT_BLUETOOTH);
79 break;
Hall Liu486cb192016-10-21 18:23:33 -070080 case BluetoothRouteManager.BLUETOOTH_DEVICE_CONNECTED:
Hall Liue74af082016-02-10 16:12:47 -080081 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
82 CallAudioRouteStateMachine.BT_AUDIO_DISCONNECT);
83 break;
84 }
85 break;
86 }
87 }
Hall Liuf62630a2015-10-27 14:53:39 -070088 /**
89 * Updates the audio route when the headset plugged in state changes. For example, if audio is
90 * being routed over speakerphone and a headset is plugged in then switch to wired headset.
91 */
92 @Override
93 public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) {
94 if (!oldIsPluggedIn && newIsPluggedIn) {
Brad Ebinger72930a82015-11-23 10:06:40 -080095 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
Hall Liuf62630a2015-10-27 14:53:39 -070096 CallAudioRouteStateMachine.CONNECT_WIRED_HEADSET);
97 } else if (oldIsPluggedIn && !newIsPluggedIn){
Brad Ebinger72930a82015-11-23 10:06:40 -080098 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
Hall Liuf62630a2015-10-27 14:53:39 -070099 CallAudioRouteStateMachine.DISCONNECT_WIRED_HEADSET);
100 }
101 }
102
103 @Override
104 public void onDockChanged(boolean isDocked) {
Brad Ebinger72930a82015-11-23 10:06:40 -0800105 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
Hall Liuf62630a2015-10-27 14:53:39 -0700106 isDocked ? CallAudioRouteStateMachine.CONNECT_DOCK
107 : CallAudioRouteStateMachine.DISCONNECT_DOCK
108 );
109 }
110}