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 CallAudioRouteAdapter mCallAudioAdapter;
29     private final BluetoothRouteManager mBluetoothRouteManager;
30     private final AsyncRingtonePlayer mRingtonePlayer;
31 
CallAudioRoutePeripheralAdapter( CallAudioRouteAdapter callAudioRouteAdapter, BluetoothRouteManager bluetoothManager, WiredHeadsetManager wiredHeadsetManager, DockManager dockManager, AsyncRingtonePlayer ringtonePlayer)32     public CallAudioRoutePeripheralAdapter(
33             CallAudioRouteAdapter callAudioRouteAdapter,
34             BluetoothRouteManager bluetoothManager,
35             WiredHeadsetManager wiredHeadsetManager,
36             DockManager dockManager,
37             AsyncRingtonePlayer ringtonePlayer) {
38         mCallAudioAdapter = callAudioRouteAdapter;
39         mBluetoothRouteManager = bluetoothManager;
40         mRingtonePlayer = ringtonePlayer;
41 
42         mBluetoothRouteManager.setListener(this);
43         wiredHeadsetManager.addListener(this);
44         dockManager.addListener(this);
45     }
46 
isBluetoothAudioOn()47     public boolean isBluetoothAudioOn() {
48         return mBluetoothRouteManager.isBluetoothAudioConnectedOrPending();
49     }
50 
isHearingAidDeviceOn()51     public boolean isHearingAidDeviceOn() {
52         return mBluetoothRouteManager.isCachedHearingAidDevice(
53                 mBluetoothRouteManager.getBluetoothAudioConnectedDevice());
54     }
55 
isLeAudioDeviceOn()56     public boolean isLeAudioDeviceOn() {
57         return mBluetoothRouteManager.isCachedLeAudioDevice(
58                 mBluetoothRouteManager.getBluetoothAudioConnectedDevice());
59     }
60 
61     @Override
onBluetoothDeviceListChanged()62     public void onBluetoothDeviceListChanged() {
63         mCallAudioAdapter.sendMessageWithSessionInfo(
64                 CallAudioRouteStateMachine.BLUETOOTH_DEVICE_LIST_CHANGED);
65     }
66 
67     @Override
onBluetoothActiveDevicePresent()68     public void onBluetoothActiveDevicePresent() {
69         mCallAudioAdapter.sendMessageWithSessionInfo(
70                 CallAudioRouteStateMachine.BT_ACTIVE_DEVICE_PRESENT);
71     }
72 
73     @Override
onBluetoothActiveDeviceGone()74     public void onBluetoothActiveDeviceGone() {
75         mCallAudioAdapter.sendMessageWithSessionInfo(
76                 CallAudioRouteStateMachine.BT_ACTIVE_DEVICE_GONE);
77     }
78 
79     @Override
onBluetoothAudioConnected()80     public void onBluetoothAudioConnected() {
81         mRingtonePlayer.updateBtActiveState(true);
82         mCallAudioAdapter.sendMessageWithSessionInfo(
83                 CallAudioRouteStateMachine.BT_AUDIO_CONNECTED);
84     }
85 
86     @Override
onBluetoothAudioConnecting()87     public void onBluetoothAudioConnecting() {
88         mRingtonePlayer.updateBtActiveState(false);
89         // Pretend like audio is connected when communicating w/ CARSM.
90         mCallAudioAdapter.sendMessageWithSessionInfo(
91                 CallAudioRouteStateMachine.BT_AUDIO_CONNECTED);
92     }
93 
94     @Override
onBluetoothAudioDisconnected()95     public void onBluetoothAudioDisconnected() {
96         mRingtonePlayer.updateBtActiveState(false);
97         mCallAudioAdapter.sendMessageWithSessionInfo(
98                 CallAudioRouteStateMachine.BT_AUDIO_DISCONNECTED);
99     }
100 
101     @Override
onUnexpectedBluetoothStateChange()102     public void onUnexpectedBluetoothStateChange() {
103         mCallAudioAdapter.sendMessageWithSessionInfo(
104                 CallAudioRouteStateMachine.UPDATE_SYSTEM_AUDIO_ROUTE);
105     }
106 
107     /**
108       * Updates the audio route when the headset plugged in state changes. For example, if audio is
109       * being routed over speakerphone and a headset is plugged in then switch to wired headset.
110       */
111     @Override
onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn)112     public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) {
113         if (!oldIsPluggedIn && newIsPluggedIn) {
114             mCallAudioAdapter.sendMessageWithSessionInfo(
115                     CallAudioRouteStateMachine.CONNECT_WIRED_HEADSET);
116         } else if (oldIsPluggedIn && !newIsPluggedIn){
117             mCallAudioAdapter.sendMessageWithSessionInfo(
118                     CallAudioRouteStateMachine.DISCONNECT_WIRED_HEADSET);
119         }
120     }
121 
122     @Override
onDockChanged(boolean isDocked)123     public void onDockChanged(boolean isDocked) {
124         mCallAudioAdapter.sendMessageWithSessionInfo(
125                 isDocked ? CallAudioRouteStateMachine.CONNECT_DOCK
126                         : CallAudioRouteStateMachine.DISCONNECT_DOCK
127         );
128     }
129 }
130