1 /*
2  * Copyright (C) 2016 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.settingslib.bluetooth;
18 
19 import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
20 import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
21 
22 import android.bluetooth.BluetoothAdapter;
23 import android.bluetooth.BluetoothClass;
24 import android.bluetooth.BluetoothDevice;
25 import android.bluetooth.BluetoothHeadsetClient;
26 import android.bluetooth.BluetoothProfile;
27 import android.bluetooth.BluetoothUuid;
28 import android.content.Context;
29 import android.os.ParcelUuid;
30 import android.util.Log;
31 
32 import com.android.settingslib.R;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /**
38  * Handles the Handsfree HF role.
39  */
40 final class HfpClientProfile implements LocalBluetoothProfile {
41     private static final String TAG = "HfpClientProfile";
42 
43     private BluetoothHeadsetClient mService;
44     private boolean mIsProfileReady;
45 
46     private final CachedBluetoothDeviceManager mDeviceManager;
47 
48     static final ParcelUuid[] SRC_UUIDS = {
49         BluetoothUuid.HSP_AG,
50         BluetoothUuid.HFP_AG,
51     };
52 
53     static final String NAME = "HEADSET_CLIENT";
54     private final LocalBluetoothProfileManager mProfileManager;
55 
56     // Order of this profile in device profiles list
57     private static final int ORDINAL = 0;
58 
59     // These callbacks run on the main thread.
60     private final class HfpClientServiceListener
61             implements BluetoothProfile.ServiceListener {
62 
63         @Override
onServiceConnected(int profile, BluetoothProfile proxy)64         public void onServiceConnected(int profile, BluetoothProfile proxy) {
65             mService = (BluetoothHeadsetClient) proxy;
66             // We just bound to the service, so refresh the UI for any connected HFP devices.
67             List<BluetoothDevice> deviceList = mService.getConnectedDevices();
68             while (!deviceList.isEmpty()) {
69                 BluetoothDevice nextDevice = deviceList.remove(0);
70                 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
71                 // we may add a new device here, but generally this should not happen
72                 if (device == null) {
73                     Log.w(TAG, "HfpClient profile found new device: " + nextDevice);
74                     device = mDeviceManager.addDevice(nextDevice);
75                 }
76                 device.onProfileStateChanged(
77                     HfpClientProfile.this, BluetoothProfile.STATE_CONNECTED);
78                 device.refresh();
79             }
80             mIsProfileReady=true;
81         }
82 
83         @Override
onServiceDisconnected(int profile)84         public void onServiceDisconnected(int profile) {
85             mIsProfileReady=false;
86         }
87     }
88 
89     @Override
isProfileReady()90     public boolean isProfileReady() {
91         return mIsProfileReady;
92     }
93 
94     @Override
getProfileId()95     public int getProfileId() {
96         return BluetoothProfile.HEADSET_CLIENT;
97     }
98 
HfpClientProfile(Context context, CachedBluetoothDeviceManager deviceManager, LocalBluetoothProfileManager profileManager)99     HfpClientProfile(Context context, CachedBluetoothDeviceManager deviceManager,
100             LocalBluetoothProfileManager profileManager) {
101         mDeviceManager = deviceManager;
102         mProfileManager = profileManager;
103         BluetoothAdapter.getDefaultAdapter().getProfileProxy(context,
104                 new HfpClientServiceListener(), BluetoothProfile.HEADSET_CLIENT);
105     }
106 
107     @Override
accessProfileEnabled()108     public boolean accessProfileEnabled() {
109         return true;
110     }
111 
112     @Override
isAutoConnectable()113     public boolean isAutoConnectable() {
114         return true;
115     }
116 
getConnectedDevices()117     public List<BluetoothDevice> getConnectedDevices() {
118         if (mService == null) {
119             return new ArrayList<BluetoothDevice>(0);
120         }
121         return mService.getDevicesMatchingConnectionStates(
122               new int[] {BluetoothProfile.STATE_CONNECTED,
123                          BluetoothProfile.STATE_CONNECTING,
124                          BluetoothProfile.STATE_DISCONNECTING});
125     }
126 
127     @Override
getConnectionStatus(BluetoothDevice device)128     public int getConnectionStatus(BluetoothDevice device) {
129         if (mService == null) {
130             return BluetoothProfile.STATE_DISCONNECTED;
131         }
132         return mService.getConnectionState(device);
133     }
134 
135     @Override
isEnabled(BluetoothDevice device)136     public boolean isEnabled(BluetoothDevice device) {
137         if (mService == null) {
138             return false;
139         }
140         return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
141     }
142 
143     @Override
getConnectionPolicy(BluetoothDevice device)144     public int getConnectionPolicy(BluetoothDevice device) {
145         if (mService == null) {
146             return CONNECTION_POLICY_FORBIDDEN;
147         }
148         return mService.getConnectionPolicy(device);
149     }
150 
151     @Override
setEnabled(BluetoothDevice device, boolean enabled)152     public boolean setEnabled(BluetoothDevice device, boolean enabled) {
153         boolean isEnabled = false;
154         if (mService == null) {
155             return false;
156         }
157         if (enabled) {
158             if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
159                 isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
160             }
161         } else {
162             isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
163         }
164 
165         return isEnabled;
166     }
167 
168     @Override
toString()169     public String toString() {
170         return NAME;
171     }
172 
173     @Override
getOrdinal()174     public int getOrdinal() {
175         return ORDINAL;
176     }
177 
178     @Override
getNameResource(BluetoothDevice device)179     public int getNameResource(BluetoothDevice device) {
180         return R.string.bluetooth_profile_headset;
181     }
182 
183     @Override
getSummaryResourceForDevice(BluetoothDevice device)184     public int getSummaryResourceForDevice(BluetoothDevice device) {
185         int state = getConnectionStatus(device);
186         switch (state) {
187             case BluetoothProfile.STATE_DISCONNECTED:
188                 return R.string.bluetooth_headset_profile_summary_use_for;
189 
190             case BluetoothProfile.STATE_CONNECTED:
191                 return R.string.bluetooth_headset_profile_summary_connected;
192 
193             default:
194                 return BluetoothUtils.getConnectionStateSummary(state);
195         }
196     }
197 
198     @Override
getDrawableResource(BluetoothClass btClass)199     public int getDrawableResource(BluetoothClass btClass) {
200         return com.android.internal.R.drawable.ic_bt_headset_hfp;
201     }
202 
finalize()203     protected void finalize() {
204         Log.d(TAG, "finalize()");
205         if (mService != null) {
206             try {
207                 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(
208                     BluetoothProfile.HEADSET_CLIENT, mService);
209                 mService = null;
210             } catch (Throwable t) {
211                 Log.w(TAG, "Error cleaning up HfpClient proxy", t);
212             }
213         }
214     }
215 }
216