blob: ee1219126fe35af90be2ea3de84e83555b4adc58 [file] [log] [blame]
Jason Monk7ce96b92015-02-02 11:27:58 -05001/*
2 * Copyright (C) 2012 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
17package com.android.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothClass;
21import android.bluetooth.BluetoothDevice;
22import android.bluetooth.BluetoothHeadset;
23import android.bluetooth.BluetoothProfile;
24import android.bluetooth.BluetoothUuid;
25import android.content.Context;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.settingslib.R;
30
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * HeadsetProfile handles Bluetooth HFP and Headset profiles.
36 */
Jack He6258aae2017-06-29 17:01:23 -070037public class HeadsetProfile implements LocalBluetoothProfile {
Jason Monk7ce96b92015-02-02 11:27:58 -050038 private static final String TAG = "HeadsetProfile";
39 private static boolean V = true;
40
41 private BluetoothHeadset mService;
42 private boolean mIsProfileReady;
43
44 private final LocalBluetoothAdapter mLocalAdapter;
45 private final CachedBluetoothDeviceManager mDeviceManager;
46 private final LocalBluetoothProfileManager mProfileManager;
47
48 static final ParcelUuid[] UUIDS = {
49 BluetoothUuid.HSP,
50 BluetoothUuid.Handsfree,
51 };
52
53 static final String NAME = "HEADSET";
54
55 // Order of this profile in device profiles list
56 private static final int ORDINAL = 0;
57
58 // These callbacks run on the main thread.
59 private final class HeadsetServiceListener
60 implements BluetoothProfile.ServiceListener {
61
62 public void onServiceConnected(int profile, BluetoothProfile proxy) {
63 if (V) Log.d(TAG,"Bluetooth service connected");
64 mService = (BluetoothHeadset) proxy;
65 // We just bound to the service, so refresh the UI for any connected HFP devices.
66 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
67 while (!deviceList.isEmpty()) {
68 BluetoothDevice nextDevice = deviceList.remove(0);
69 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
70 // we may add a new device here, but generally this should not happen
71 if (device == null) {
72 Log.w(TAG, "HeadsetProfile found new device: " + nextDevice);
73 device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
74 }
75 device.onProfileStateChanged(HeadsetProfile.this,
76 BluetoothProfile.STATE_CONNECTED);
77 device.refresh();
78 }
79
80 mProfileManager.callServiceConnectedListeners();
81 mIsProfileReady=true;
82 }
83
84 public void onServiceDisconnected(int profile) {
85 if (V) Log.d(TAG,"Bluetooth service disconnected");
86 mProfileManager.callServiceDisconnectedListeners();
87 mIsProfileReady=false;
88 }
89 }
90
91 public boolean isProfileReady() {
92 return mIsProfileReady;
93 }
94
95 HeadsetProfile(Context context, LocalBluetoothAdapter adapter,
96 CachedBluetoothDeviceManager deviceManager,
97 LocalBluetoothProfileManager profileManager) {
98 mLocalAdapter = adapter;
99 mDeviceManager = deviceManager;
100 mProfileManager = profileManager;
101 mLocalAdapter.getProfileProxy(context, new HeadsetServiceListener(),
102 BluetoothProfile.HEADSET);
103 }
104
105 public boolean isConnectable() {
106 return true;
107 }
108
109 public boolean isAutoConnectable() {
110 return true;
111 }
112
113 public boolean connect(BluetoothDevice device) {
114 if (mService == null) return false;
115 List<BluetoothDevice> sinks = mService.getConnectedDevices();
116 if (sinks != null) {
117 for (BluetoothDevice sink : sinks) {
118 Log.d(TAG,"Not disconnecting device = " + sink);
119 }
120 }
121 return mService.connect(device);
122 }
123
124 public boolean disconnect(BluetoothDevice device) {
125 if (mService == null) return false;
126 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
127 if (!deviceList.isEmpty()) {
128 for (BluetoothDevice dev : deviceList) {
129 if (dev.equals(device)) {
130 if (V) Log.d(TAG,"Downgrade priority as user" +
131 "is disconnecting the headset");
132 // Downgrade priority as user is disconnecting the headset.
133 if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
134 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
135 }
136 return mService.disconnect(device);
137 }
138 }
139 }
140 return false;
141 }
142
143 public int getConnectionStatus(BluetoothDevice device) {
144 if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
145 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
146 if (!deviceList.isEmpty()){
147 for (BluetoothDevice dev : deviceList) {
148 if (dev.equals(device)) {
149 return mService.getConnectionState(device);
150 }
151 }
152 }
153 return BluetoothProfile.STATE_DISCONNECTED;
154 }
155
Pavlin Radoslavov03d44ce2018-01-21 02:59:15 -0800156 public boolean setActiveDevice(BluetoothDevice device) {
157 if (mService == null) return false;
158 return mService.setActiveDevice(device);
159 }
160
161 public BluetoothDevice getActiveDevice() {
162 if (mService == null) return null;
163 return mService.getActiveDevice();
164 }
165
Jason Monk7ce96b92015-02-02 11:27:58 -0500166 public boolean isPreferred(BluetoothDevice device) {
167 if (mService == null) return false;
168 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
169 }
170
171 public int getPreferred(BluetoothDevice device) {
172 if (mService == null) return BluetoothProfile.PRIORITY_OFF;
173 return mService.getPriority(device);
174 }
175
176 public void setPreferred(BluetoothDevice device, boolean preferred) {
177 if (mService == null) return;
178 if (preferred) {
179 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
180 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
181 }
182 } else {
183 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
184 }
185 }
186
187 public List<BluetoothDevice> getConnectedDevices() {
188 if (mService == null) return new ArrayList<BluetoothDevice>(0);
189 return mService.getDevicesMatchingConnectionStates(
190 new int[] {BluetoothProfile.STATE_CONNECTED,
191 BluetoothProfile.STATE_CONNECTING,
192 BluetoothProfile.STATE_DISCONNECTING});
193 }
194
195 public String toString() {
196 return NAME;
197 }
198
199 public int getOrdinal() {
200 return ORDINAL;
201 }
202
203 public int getNameResource(BluetoothDevice device) {
204 return R.string.bluetooth_profile_headset;
205 }
206
207 public int getSummaryResourceForDevice(BluetoothDevice device) {
208 int state = getConnectionStatus(device);
209 switch (state) {
210 case BluetoothProfile.STATE_DISCONNECTED:
211 return R.string.bluetooth_headset_profile_summary_use_for;
212
213 case BluetoothProfile.STATE_CONNECTED:
214 return R.string.bluetooth_headset_profile_summary_connected;
215
216 default:
217 return Utils.getConnectionStateSummary(state);
218 }
219 }
220
221 public int getDrawableResource(BluetoothClass btClass) {
222 return R.drawable.ic_bt_headset_hfp;
223 }
224
225 protected void finalize() {
226 if (V) Log.d(TAG, "finalize()");
227 if (mService != null) {
228 try {
229 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HEADSET,
230 mService);
231 mService = null;
232 }catch (Throwable t) {
233 Log.w(TAG, "Error cleaning up HID proxy", t);
234 }
235 }
236 }
237}