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 19 import com.android.server.telecom.bluetooth.BluetoothRouteManager; 20 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 */ 25 public class CallAudioRoutePeripheralAdapter implements WiredHeadsetManager.Listener, 26 DockManager.Listener, BluetoothRouteManager.BluetoothStateListener { 27 28 private final CallAudioRouteStateMachine mCallAudioRouteStateMachine; 29 private final BluetoothRouteManager mBluetoothRouteManager; 30 CallAudioRoutePeripheralAdapter( CallAudioRouteStateMachine callAudioRouteStateMachine, BluetoothRouteManager bluetoothManager, WiredHeadsetManager wiredHeadsetManager, DockManager dockManager)31 public CallAudioRoutePeripheralAdapter( 32 CallAudioRouteStateMachine callAudioRouteStateMachine, 33 BluetoothRouteManager bluetoothManager, 34 WiredHeadsetManager wiredHeadsetManager, 35 DockManager dockManager) { 36 mCallAudioRouteStateMachine = callAudioRouteStateMachine; 37 mBluetoothRouteManager = bluetoothManager; 38 39 mBluetoothRouteManager.setListener(this); 40 wiredHeadsetManager.addListener(this); 41 dockManager.addListener(this); 42 } 43 isBluetoothAudioOn()44 public boolean isBluetoothAudioOn() { 45 return mBluetoothRouteManager.isBluetoothAudioConnectedOrPending(); 46 } 47 isHearingAidDeviceOn()48 public boolean isHearingAidDeviceOn() { 49 return mBluetoothRouteManager.isCachedHearingAidDevice( 50 mBluetoothRouteManager.getBluetoothAudioConnectedDevice()); 51 } 52 isLeAudioDeviceOn()53 public boolean isLeAudioDeviceOn() { 54 return mBluetoothRouteManager.isCachedLeAudioDevice( 55 mBluetoothRouteManager.getBluetoothAudioConnectedDevice()); 56 } 57 58 @Override onBluetoothDeviceListChanged()59 public void onBluetoothDeviceListChanged() { 60 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 61 CallAudioRouteStateMachine.BLUETOOTH_DEVICE_LIST_CHANGED); 62 } 63 64 @Override onBluetoothActiveDevicePresent()65 public void onBluetoothActiveDevicePresent() { 66 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 67 CallAudioRouteStateMachine.BT_ACTIVE_DEVICE_PRESENT); 68 } 69 70 @Override onBluetoothActiveDeviceGone()71 public void onBluetoothActiveDeviceGone() { 72 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 73 CallAudioRouteStateMachine.BT_ACTIVE_DEVICE_GONE); 74 } 75 76 @Override onBluetoothAudioConnected()77 public void onBluetoothAudioConnected() { 78 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 79 CallAudioRouteStateMachine.BT_AUDIO_CONNECTED); 80 } 81 82 @Override onBluetoothAudioDisconnected()83 public void onBluetoothAudioDisconnected() { 84 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 85 CallAudioRouteStateMachine.BT_AUDIO_DISCONNECTED); 86 } 87 88 @Override onUnexpectedBluetoothStateChange()89 public void onUnexpectedBluetoothStateChange() { 90 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 91 CallAudioRouteStateMachine.UPDATE_SYSTEM_AUDIO_ROUTE); 92 } 93 94 /** 95 * Updates the audio route when the headset plugged in state changes. For example, if audio is 96 * being routed over speakerphone and a headset is plugged in then switch to wired headset. 97 */ 98 @Override onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn)99 public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) { 100 if (!oldIsPluggedIn && newIsPluggedIn) { 101 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 102 CallAudioRouteStateMachine.CONNECT_WIRED_HEADSET); 103 } else if (oldIsPluggedIn && !newIsPluggedIn){ 104 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 105 CallAudioRouteStateMachine.DISCONNECT_WIRED_HEADSET); 106 } 107 } 108 109 @Override onDockChanged(boolean isDocked)110 public void onDockChanged(boolean isDocked) { 111 mCallAudioRouteStateMachine.sendMessageWithSessionInfo( 112 isDocked ? CallAudioRouteStateMachine.CONNECT_DOCK 113 : CallAudioRouteStateMachine.DISCONNECT_DOCK 114 ); 115 } 116 } 117