Home | History | Annotate | Download | only in telecom
      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 /**
     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  */
     23 public class CallAudioRoutePeripheralAdapter implements WiredHeadsetManager.Listener,
     24         DockManager.Listener, BluetoothManager.BluetoothStateListener {
     25 
     26     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 
     42     public boolean isBluetoothAudioOn() {
     43         return mBluetoothManager.isBluetoothAudioConnected();
     44     }
     45 
     46     @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     }
     86     /**
     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) {
     93             mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
     94                     CallAudioRouteStateMachine.CONNECT_WIRED_HEADSET);
     95         } else if (oldIsPluggedIn && !newIsPluggedIn){
     96             mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
     97                     CallAudioRouteStateMachine.DISCONNECT_WIRED_HEADSET);
     98         }
     99     }
    100 
    101     @Override
    102     public void onDockChanged(boolean isDocked) {
    103         mCallAudioRouteStateMachine.sendMessageWithSessionInfo(
    104                 isDocked ? CallAudioRouteStateMachine.CONNECT_DOCK
    105                         : CallAudioRouteStateMachine.DISCONNECT_DOCK
    106         );
    107     }
    108 }
    109