1 /*
2  * Copyright (C) 2020 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.bluetooth.hfp;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.bluetooth.BluetoothHeadset;
21 import android.bluetooth.BluetoothManager;
22 import android.bluetooth.BluetoothProfile;
23 import android.content.Context;
24 
25 import java.util.List;
26 
27 /**
28  * A proxy class that facilitates testing of the BluetoothInCallService class.
29  *
30  * <p>This is necessary due to the "final" attribute of the BluetoothHeadset class. In order to test
31  * the correct functioning of the BluetoothInCallService class, the final class must be put into a
32  * container that can be mocked correctly.
33  */
34 public class BluetoothHeadsetProxy {
35 
36     private BluetoothHeadset mBluetoothHeadset;
37 
BluetoothHeadsetProxy(BluetoothHeadset headset)38     public BluetoothHeadsetProxy(BluetoothHeadset headset) {
39         mBluetoothHeadset = headset;
40     }
41 
closeBluetoothHeadsetProxy(Context context)42     public void closeBluetoothHeadsetProxy(Context context) {
43         final BluetoothManager btManager = context.getSystemService(BluetoothManager.class);
44         if (btManager != null) {
45             btManager.getAdapter().closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
46         }
47     }
48 
clccResponse( int index, int direction, int status, int mode, boolean mpty, String number, int type)49     public void clccResponse(
50             int index, int direction, int status, int mode, boolean mpty, String number, int type) {
51 
52         mBluetoothHeadset.clccResponse(index, direction, status, mode, mpty, number, type);
53     }
54 
phoneStateChanged( int numActive, int numHeld, int callState, String number, int type, String name)55     public void phoneStateChanged(
56             int numActive, int numHeld, int callState, String number, int type, String name) {
57 
58         mBluetoothHeadset.phoneStateChanged(numActive, numHeld, callState, number, type, name);
59     }
60 
getConnectedDevices()61     public List<BluetoothDevice> getConnectedDevices() {
62         return mBluetoothHeadset.getConnectedDevices();
63     }
64 
getConnectionState(BluetoothDevice device)65     public int getConnectionState(BluetoothDevice device) {
66         return mBluetoothHeadset.getConnectionState(device);
67     }
68 
getAudioState(BluetoothDevice device)69     public int getAudioState(BluetoothDevice device) {
70         return mBluetoothHeadset.getAudioState(device);
71     }
72 
73     /**
74      * Proxy function that calls {@link BluetoothHeadset#connectAudio()}.
75      *
76      * @return whether the connection request was successful
77      */
connectAudio()78     public int connectAudio() {
79         return mBluetoothHeadset.connectAudio();
80     }
81 
setActiveDevice(BluetoothDevice device)82     public boolean setActiveDevice(BluetoothDevice device) {
83         return mBluetoothHeadset.setActiveDevice(device);
84     }
85 
getActiveDevice()86     public BluetoothDevice getActiveDevice() {
87         return mBluetoothHeadset.getActiveDevice();
88     }
89 
90     /**
91      * Proxy function that calls {@link BluetoothHeadset#disconnectAudio()}.
92      *
93      * @return whether the disconnection request was successful
94      */
disconnectAudio()95     public int disconnectAudio() {
96         return mBluetoothHeadset.disconnectAudio();
97     }
98 
isInbandRingingEnabled()99     public boolean isInbandRingingEnabled() {
100         return mBluetoothHeadset.isInbandRingingEnabled();
101     }
102 }
103