blob: 24ede164fbddaa2a6cb39b05485b602b481e00bf [file] [log] [blame]
Jason Monk7ce96b92015-02-02 11:27:58 -05001/*
2 * Copyright (C) 2011 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.BluetoothA2dp;
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.BluetoothClass;
22import android.bluetooth.BluetoothDevice;
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
34public final class A2dpProfile implements LocalBluetoothProfile {
35 private static final String TAG = "A2dpProfile";
36 private static boolean V = false;
37
38 private BluetoothA2dp mService;
39 private boolean mIsProfileReady;
40
41 private final LocalBluetoothAdapter mLocalAdapter;
42 private final CachedBluetoothDeviceManager mDeviceManager;
43
44 static final ParcelUuid[] SINK_UUIDS = {
45 BluetoothUuid.AudioSink,
46 BluetoothUuid.AdvAudioDist,
47 };
48
49 static final String NAME = "A2DP";
50 private final LocalBluetoothProfileManager mProfileManager;
51
52 // Order of this profile in device profiles list
53 private static final int ORDINAL = 1;
54
55 // These callbacks run on the main thread.
56 private final class A2dpServiceListener
57 implements BluetoothProfile.ServiceListener {
58
59 public void onServiceConnected(int profile, BluetoothProfile proxy) {
60 if (V) Log.d(TAG,"Bluetooth service connected");
61 mService = (BluetoothA2dp) proxy;
62 // We just bound to the service, so refresh the UI for any connected A2DP devices.
63 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
64 while (!deviceList.isEmpty()) {
65 BluetoothDevice nextDevice = deviceList.remove(0);
66 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
67 // we may add a new device here, but generally this should not happen
68 if (device == null) {
69 Log.w(TAG, "A2dpProfile found new device: " + nextDevice);
70 device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
71 }
72 device.onProfileStateChanged(A2dpProfile.this, BluetoothProfile.STATE_CONNECTED);
73 device.refresh();
74 }
75 mIsProfileReady=true;
76 }
77
78 public void onServiceDisconnected(int profile) {
79 if (V) Log.d(TAG,"Bluetooth service disconnected");
80 mIsProfileReady=false;
81 }
82 }
83
84 public boolean isProfileReady() {
85 return mIsProfileReady;
86 }
87
88 A2dpProfile(Context context, LocalBluetoothAdapter adapter,
89 CachedBluetoothDeviceManager deviceManager,
90 LocalBluetoothProfileManager profileManager) {
91 mLocalAdapter = adapter;
92 mDeviceManager = deviceManager;
93 mProfileManager = profileManager;
94 mLocalAdapter.getProfileProxy(context, new A2dpServiceListener(),
95 BluetoothProfile.A2DP);
96 }
97
98 public boolean isConnectable() {
99 return true;
100 }
101
102 public boolean isAutoConnectable() {
103 return true;
104 }
105
106 public List<BluetoothDevice> getConnectedDevices() {
107 if (mService == null) return new ArrayList<BluetoothDevice>(0);
108 return mService.getDevicesMatchingConnectionStates(
109 new int[] {BluetoothProfile.STATE_CONNECTED,
110 BluetoothProfile.STATE_CONNECTING,
111 BluetoothProfile.STATE_DISCONNECTING});
112 }
113
114 public boolean connect(BluetoothDevice device) {
115 if (mService == null) return false;
116 List<BluetoothDevice> sinks = getConnectedDevices();
117 if (sinks != null) {
118 for (BluetoothDevice sink : sinks) {
Pavlin Radoslavoveb067ba2016-11-16 20:51:06 -0800119 if (sink.equals(device)) {
120 Log.w(TAG, "Connecting to device " + device + " : disconnect skipped");
121 continue;
122 }
Jason Monk7ce96b92015-02-02 11:27:58 -0500123 mService.disconnect(sink);
124 }
125 }
126 return mService.connect(device);
127 }
128
129 public boolean disconnect(BluetoothDevice device) {
130 if (mService == null) return false;
131 // Downgrade priority as user is disconnecting the headset.
132 if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON){
133 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
134 }
135 return mService.disconnect(device);
136 }
137
138 public int getConnectionStatus(BluetoothDevice device) {
139 if (mService == null) {
140 return BluetoothProfile.STATE_DISCONNECTED;
141 }
142 return mService.getConnectionState(device);
143 }
144
145 public boolean isPreferred(BluetoothDevice device) {
146 if (mService == null) return false;
147 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
148 }
149
150 public int getPreferred(BluetoothDevice device) {
151 if (mService == null) return BluetoothProfile.PRIORITY_OFF;
152 return mService.getPriority(device);
153 }
154
155 public void setPreferred(BluetoothDevice device, boolean preferred) {
156 if (mService == null) return;
157 if (preferred) {
158 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
159 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
160 }
161 } else {
162 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
163 }
164 }
165 boolean isA2dpPlaying() {
166 if (mService == null) return false;
167 List<BluetoothDevice> sinks = mService.getConnectedDevices();
168 if (!sinks.isEmpty()) {
169 if (mService.isA2dpPlaying(sinks.get(0))) {
170 return true;
171 }
172 }
173 return false;
174 }
175
176 public String toString() {
177 return NAME;
178 }
179
180 public int getOrdinal() {
181 return ORDINAL;
182 }
183
184 public int getNameResource(BluetoothDevice device) {
185 return R.string.bluetooth_profile_a2dp;
186 }
187
188 public int getSummaryResourceForDevice(BluetoothDevice device) {
189 int state = getConnectionStatus(device);
190 switch (state) {
191 case BluetoothProfile.STATE_DISCONNECTED:
192 return R.string.bluetooth_a2dp_profile_summary_use_for;
193
194 case BluetoothProfile.STATE_CONNECTED:
195 return R.string.bluetooth_a2dp_profile_summary_connected;
196
197 default:
198 return Utils.getConnectionStateSummary(state);
199 }
200 }
201
202 public int getDrawableResource(BluetoothClass btClass) {
203 return R.drawable.ic_bt_headphones_a2dp;
204 }
205
206 protected void finalize() {
207 if (V) Log.d(TAG, "finalize()");
208 if (mService != null) {
209 try {
210 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP,
211 mService);
212 mService = null;
213 }catch (Throwable t) {
214 Log.w(TAG, "Error cleaning up A2DP proxy", t);
215 }
216 }
217 }
218}