blob: e13e566ec901a3a2ca820347a9d29c978f165ff2 [file] [log] [blame]
Sanket Agarwal1bec6a52015-10-21 18:23:27 -07001/*
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
17package com.android.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothA2dpSink;
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
34final class A2dpSinkProfile implements LocalBluetoothProfile {
35 private static final String TAG = "A2dpSinkProfile";
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070036
37 private BluetoothA2dpSink mService;
38 private boolean mIsProfileReady;
39
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070040 private final CachedBluetoothDeviceManager mDeviceManager;
41
42 static final ParcelUuid[] SRC_UUIDS = {
43 BluetoothUuid.AudioSource,
44 BluetoothUuid.AdvAudioDist,
45 };
46
47 static final String NAME = "A2DPSink";
48 private final LocalBluetoothProfileManager mProfileManager;
49
50 // Order of this profile in device profiles list
51 private static final int ORDINAL = 5;
52
53 // These callbacks run on the main thread.
54 private final class A2dpSinkServiceListener
55 implements BluetoothProfile.ServiceListener {
56
57 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Chienyuan447fbde2018-09-05 21:28:59 +080058 Log.d(TAG, "Bluetooth service connected");
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070059 mService = (BluetoothA2dpSink) proxy;
60 // We just bound to the service, so refresh the UI for any connected A2DP devices.
61 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
62 while (!deviceList.isEmpty()) {
63 BluetoothDevice nextDevice = deviceList.remove(0);
64 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
65 // we may add a new device here, but generally this should not happen
66 if (device == null) {
67 Log.w(TAG, "A2dpSinkProfile found new device: " + nextDevice);
timhypengf64f6902018-07-31 15:40:15 +080068 device = mDeviceManager.addDevice(nextDevice);
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070069 }
70 device.onProfileStateChanged(A2dpSinkProfile.this, BluetoothProfile.STATE_CONNECTED);
71 device.refresh();
72 }
73 mIsProfileReady=true;
74 }
75
76 public void onServiceDisconnected(int profile) {
Chienyuan447fbde2018-09-05 21:28:59 +080077 Log.d(TAG, "Bluetooth service disconnected");
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070078 mIsProfileReady=false;
79 }
80 }
81
82 public boolean isProfileReady() {
83 return mIsProfileReady;
84 }
85
ryanywlin44de3a02018-05-02 15:15:37 +080086 @Override
87 public int getProfileId() {
88 return BluetoothProfile.A2DP_SINK;
89 }
90
timhypengf64f6902018-07-31 15:40:15 +080091 A2dpSinkProfile(Context context, CachedBluetoothDeviceManager deviceManager,
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070092 LocalBluetoothProfileManager profileManager) {
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070093 mDeviceManager = deviceManager;
94 mProfileManager = profileManager;
timhypengf64f6902018-07-31 15:40:15 +080095 BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new A2dpSinkServiceListener(),
Sanket Agarwal1bec6a52015-10-21 18:23:27 -070096 BluetoothProfile.A2DP_SINK);
97 }
98
99 public boolean isConnectable() {
100 return true;
101 }
102
103 public boolean isAutoConnectable() {
104 return true;
105 }
106
107 public List<BluetoothDevice> getConnectedDevices() {
Chienyuan447fbde2018-09-05 21:28:59 +0800108 if (mService == null) {
109 return new ArrayList<BluetoothDevice>(0);
110 }
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700111 return mService.getDevicesMatchingConnectionStates(
112 new int[] {BluetoothProfile.STATE_CONNECTED,
113 BluetoothProfile.STATE_CONNECTING,
114 BluetoothProfile.STATE_DISCONNECTING});
115 }
116
117 public boolean connect(BluetoothDevice device) {
Chienyuan447fbde2018-09-05 21:28:59 +0800118 if (mService == null) {
119 return false;
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700120 }
121 return mService.connect(device);
122 }
123
124 public boolean disconnect(BluetoothDevice device) {
Chienyuan447fbde2018-09-05 21:28:59 +0800125 if (mService == null) {
126 return false;
127 }
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700128 // Downgrade priority as user is disconnecting the headset.
Chienyuan447fbde2018-09-05 21:28:59 +0800129 if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700130 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
131 }
132 return mService.disconnect(device);
133 }
134
135 public int getConnectionStatus(BluetoothDevice device) {
136 if (mService == null) {
137 return BluetoothProfile.STATE_DISCONNECTED;
138 }
139 return mService.getConnectionState(device);
140 }
141
142 public boolean isPreferred(BluetoothDevice device) {
Chienyuan447fbde2018-09-05 21:28:59 +0800143 if (mService == null) {
144 return false;
145 }
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700146 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
147 }
148
149 public int getPreferred(BluetoothDevice device) {
Chienyuan447fbde2018-09-05 21:28:59 +0800150 if (mService == null) {
151 return BluetoothProfile.PRIORITY_OFF;
152 }
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700153 return mService.getPriority(device);
154 }
155
156 public void setPreferred(BluetoothDevice device, boolean preferred) {
Chienyuan447fbde2018-09-05 21:28:59 +0800157 if (mService == null) {
158 return;
159 }
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700160 if (preferred) {
161 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
162 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
163 }
164 } else {
165 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
166 }
167 }
168
169 boolean isA2dpPlaying() {
Chienyuan447fbde2018-09-05 21:28:59 +0800170 if (mService == null) {
171 return false;
172 }
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700173 List<BluetoothDevice> srcs = mService.getConnectedDevices();
174 if (!srcs.isEmpty()) {
175 if (mService.isA2dpPlaying(srcs.get(0))) {
176 return true;
177 }
178 }
179 return false;
180 }
181
182 public String toString() {
183 return NAME;
184 }
185
186 public int getOrdinal() {
187 return ORDINAL;
188 }
189
190 public int getNameResource(BluetoothDevice device) {
191 // we need to have same string in UI for even SINK Media Audio.
192 return R.string.bluetooth_profile_a2dp;
193 }
194
195 public int getSummaryResourceForDevice(BluetoothDevice device) {
196 int state = getConnectionStatus(device);
197 switch (state) {
198 case BluetoothProfile.STATE_DISCONNECTED:
199 return R.string.bluetooth_a2dp_profile_summary_use_for;
200
201 case BluetoothProfile.STATE_CONNECTED:
202 return R.string.bluetooth_a2dp_profile_summary_connected;
203
204 default:
Kunhung Lie7b7cb82018-05-17 11:04:54 +0800205 return BluetoothUtils.getConnectionStateSummary(state);
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700206 }
207 }
208
209 public int getDrawableResource(BluetoothClass btClass) {
210 return R.drawable.ic_bt_headphones_a2dp;
211 }
212
213 protected void finalize() {
Chienyuan447fbde2018-09-05 21:28:59 +0800214 Log.d(TAG, "finalize()");
Sanket Agarwal1bec6a52015-10-21 18:23:27 -0700215 if (mService != null) {
216 try {
217 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP_SINK,
218 mService);
219 mService = null;
220 }catch (Throwable t) {
221 Log.w(TAG, "Error cleaning up A2DP proxy", t);
222 }
223 }
224 }
225}