Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.telecom; |
| 18 | |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 19 | import com.android.server.telecom.bluetooth.BluetoothRouteManager; |
| 20 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 21 | /** |
| 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 Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 25 | public class CallAudioRoutePeripheralAdapter implements WiredHeadsetManager.Listener, |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 26 | DockManager.Listener, BluetoothRouteManager.BluetoothStateListener { |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 27 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 28 | private final CallAudioRouteStateMachine mCallAudioRouteStateMachine; |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 29 | private final BluetoothRouteManager mBluetoothRouteManager; |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 30 | |
| 31 | public CallAudioRoutePeripheralAdapter( |
| 32 | CallAudioRouteStateMachine callAudioRouteStateMachine, |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 33 | BluetoothRouteManager bluetoothManager, |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 34 | WiredHeadsetManager wiredHeadsetManager, |
| 35 | DockManager dockManager) { |
| 36 | mCallAudioRouteStateMachine = callAudioRouteStateMachine; |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 37 | mBluetoothRouteManager = bluetoothManager; |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 38 | |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 39 | mBluetoothRouteManager.setListener(this); |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 40 | wiredHeadsetManager.addListener(this); |
| 41 | dockManager.addListener(this); |
| 42 | } |
| 43 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 44 | public boolean isBluetoothAudioOn() { |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 45 | return mBluetoothRouteManager.isBluetoothAudioConnectedOrPending(); |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 48 | @Override |
| 49 | public void onBluetoothStateChange(int oldState, int newState) { |
| 50 | switch (oldState) { |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 51 | case BluetoothRouteManager.BLUETOOTH_DISCONNECTED: |
| 52 | case BluetoothRouteManager.BLUETOOTH_UNINITIALIZED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 53 | switch (newState) { |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 54 | case BluetoothRouteManager.BLUETOOTH_DEVICE_CONNECTED: |
| 55 | case BluetoothRouteManager.BLUETOOTH_AUDIO_CONNECTED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 56 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
| 57 | CallAudioRouteStateMachine.CONNECT_BLUETOOTH); |
| 58 | break; |
| 59 | } |
| 60 | break; |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 61 | case BluetoothRouteManager.BLUETOOTH_DEVICE_CONNECTED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 62 | switch (newState) { |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 63 | case BluetoothRouteManager.BLUETOOTH_DISCONNECTED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 64 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
| 65 | CallAudioRouteStateMachine.DISCONNECT_BLUETOOTH); |
| 66 | break; |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 67 | case BluetoothRouteManager.BLUETOOTH_AUDIO_CONNECTED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 68 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
| 69 | CallAudioRouteStateMachine.SWITCH_BLUETOOTH); |
| 70 | break; |
| 71 | } |
| 72 | break; |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 73 | case BluetoothRouteManager.BLUETOOTH_AUDIO_CONNECTED: |
| 74 | case BluetoothRouteManager.BLUETOOTH_AUDIO_PENDING: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 75 | switch (newState) { |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 76 | case BluetoothRouteManager.BLUETOOTH_DISCONNECTED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 77 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
| 78 | CallAudioRouteStateMachine.DISCONNECT_BLUETOOTH); |
| 79 | break; |
Hall Liu | 486cb19 | 2016-10-21 18:23:33 -0700 | [diff] [blame] | 80 | case BluetoothRouteManager.BLUETOOTH_DEVICE_CONNECTED: |
Hall Liu | e74af08 | 2016-02-10 16:12:47 -0800 | [diff] [blame] | 81 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
| 82 | CallAudioRouteStateMachine.BT_AUDIO_DISCONNECT); |
| 83 | break; |
| 84 | } |
| 85 | break; |
| 86 | } |
| 87 | } |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 88 | /** |
| 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 Ebinger | 72930a8 | 2015-11-23 10:06:40 -0800 | [diff] [blame] | 95 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 96 | CallAudioRouteStateMachine.CONNECT_WIRED_HEADSET); |
| 97 | } else if (oldIsPluggedIn && !newIsPluggedIn){ |
Brad Ebinger | 72930a8 | 2015-11-23 10:06:40 -0800 | [diff] [blame] | 98 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 99 | CallAudioRouteStateMachine.DISCONNECT_WIRED_HEADSET); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onDockChanged(boolean isDocked) { |
Brad Ebinger | 72930a8 | 2015-11-23 10:06:40 -0800 | [diff] [blame] | 105 | mCallAudioRouteStateMachine.sendMessageWithSessionInfo( |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 106 | isDocked ? CallAudioRouteStateMachine.CONNECT_DOCK |
| 107 | : CallAudioRouteStateMachine.DISCONNECT_DOCK |
| 108 | ); |
| 109 | } |
| 110 | } |