blob: 9b62443ede748f23dd430079276982bee0c62dce [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
19/**
20 * A class that acts as a listener to things that could change call audio routing, namely
21 * bluetooth status, wired headset status, and dock status.
22 */
Hall Liue74af082016-02-10 16:12:47 -080023public class CallAudioRoutePeripheralAdapter implements WiredHeadsetManager.Listener,
24 DockManager.Listener, BluetoothManager.BluetoothStateListener {
25
Hall Liuf62630a2015-10-27 14:53:39 -070026 private final CallAudioRouteStateMachine mCallAudioRouteStateMachine;
27 private final BluetoothManager mBluetoothManager;
28
29 public CallAudioRoutePeripheralAdapter(
30 CallAudioRouteStateMachine callAudioRouteStateMachine,
31 BluetoothManager bluetoothManager,
32 WiredHeadsetManager wiredHeadsetManager,
33 DockManager dockManager) {
34 mCallAudioRouteStateMachine = callAudioRouteStateMachine;
35 mBluetoothManager = bluetoothManager;
36
37 mBluetoothManager.setBluetoothStateListener(this);
38 wiredHeadsetManager.addListener(this);
39 dockManager.addListener(this);
40 }
41
Hall Liuf62630a2015-10-27 14:53:39 -070042 public boolean isBluetoothAudioOn() {
43 return mBluetoothManager.isBluetoothAudioConnected();
44 }
45
Hall Liue74af082016-02-10 16:12:47 -080046 @Override
47 public void onBluetoothStateChange(int oldState, int newState) {
48 switch (oldState) {
49 case BluetoothManager.BLUETOOTH_DISCONNECTED:
50 case BluetoothManager.BLUETOOTH_UNINITIALIZED:
51 switch (newState) {
52 case BluetoothManager.BLUETOOTH_DEVICE_CONNECTED:
53 case BluetoothManager.BLUETOOTH_AUDIO_CONNECTED:
54 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
55 CallAudioRouteStateMachine.CONNECT_BLUETOOTH);
56 break;
57 }
58 break;
59 case BluetoothManager.BLUETOOTH_DEVICE_CONNECTED:
60 switch (newState) {
61 case BluetoothManager.BLUETOOTH_DISCONNECTED:
62 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
63 CallAudioRouteStateMachine.DISCONNECT_BLUETOOTH);
64 break;
65 case BluetoothManager.BLUETOOTH_AUDIO_CONNECTED:
66 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
67 CallAudioRouteStateMachine.SWITCH_BLUETOOTH);
68 break;
69 }
70 break;
71 case BluetoothManager.BLUETOOTH_AUDIO_CONNECTED:
72 case BluetoothManager.BLUETOOTH_AUDIO_PENDING:
73 switch (newState) {
74 case BluetoothManager.BLUETOOTH_DISCONNECTED:
75 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
76 CallAudioRouteStateMachine.DISCONNECT_BLUETOOTH);
77 break;
78 case BluetoothManager.BLUETOOTH_DEVICE_CONNECTED:
79 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
80 CallAudioRouteStateMachine.BT_AUDIO_DISCONNECT);
81 break;
82 }
83 break;
84 }
85 }
Hall Liuf62630a2015-10-27 14:53:39 -070086 /**
87 * Updates the audio route when the headset plugged in state changes. For example, if audio is
88 * being routed over speakerphone and a headset is plugged in then switch to wired headset.
89 */
90 @Override
91 public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) {
92 if (!oldIsPluggedIn && newIsPluggedIn) {
Brad Ebinger72930a82015-11-23 10:06:40 -080093 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
Hall Liuf62630a2015-10-27 14:53:39 -070094 CallAudioRouteStateMachine.CONNECT_WIRED_HEADSET);
95 } else if (oldIsPluggedIn && !newIsPluggedIn){
Brad Ebinger72930a82015-11-23 10:06:40 -080096 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
Hall Liuf62630a2015-10-27 14:53:39 -070097 CallAudioRouteStateMachine.DISCONNECT_WIRED_HEADSET);
98 }
99 }
100
101 @Override
102 public void onDockChanged(boolean isDocked) {
Brad Ebinger72930a82015-11-23 10:06:40 -0800103 mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
Hall Liuf62630a2015-10-27 14:53:39 -0700104 isDocked ? CallAudioRouteStateMachine.CONNECT_DOCK
105 : CallAudioRouteStateMachine.DISCONNECT_DOCK
106 );
107 }
108}