Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settingslib.bluetooth; |
| 18 | |
| 19 | import android.bluetooth.BluetoothA2dpSink; |
| 20 | import android.bluetooth.BluetoothAdapter; |
| 21 | import android.bluetooth.BluetoothClass; |
| 22 | import android.bluetooth.BluetoothDevice; |
| 23 | import android.bluetooth.BluetoothProfile; |
| 24 | import android.bluetooth.BluetoothUuid; |
| 25 | import android.content.Context; |
| 26 | import android.os.ParcelUuid; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import com.android.settingslib.R; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | |
| 34 | final class A2dpSinkProfile implements LocalBluetoothProfile { |
| 35 | private static final String TAG = "A2dpSinkProfile"; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 36 | |
| 37 | private BluetoothA2dpSink mService; |
| 38 | private boolean mIsProfileReady; |
| 39 | |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 40 | 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) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 58 | Log.d(TAG, "Bluetooth service connected"); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 59 | 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); |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 68 | device = mDeviceManager.addDevice(nextDevice); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 69 | } |
| 70 | device.onProfileStateChanged(A2dpSinkProfile.this, BluetoothProfile.STATE_CONNECTED); |
| 71 | device.refresh(); |
| 72 | } |
| 73 | mIsProfileReady=true; |
| 74 | } |
| 75 | |
| 76 | public void onServiceDisconnected(int profile) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 77 | Log.d(TAG, "Bluetooth service disconnected"); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 78 | mIsProfileReady=false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public boolean isProfileReady() { |
| 83 | return mIsProfileReady; |
| 84 | } |
| 85 | |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame] | 86 | @Override |
| 87 | public int getProfileId() { |
| 88 | return BluetoothProfile.A2DP_SINK; |
| 89 | } |
| 90 | |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 91 | A2dpSinkProfile(Context context, CachedBluetoothDeviceManager deviceManager, |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 92 | LocalBluetoothProfileManager profileManager) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 93 | mDeviceManager = deviceManager; |
| 94 | mProfileManager = profileManager; |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 95 | BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new A2dpSinkServiceListener(), |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 96 | 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() { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 108 | if (mService == null) { |
| 109 | return new ArrayList<BluetoothDevice>(0); |
| 110 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 111 | 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) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 118 | if (mService == null) { |
| 119 | return false; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 120 | } |
| 121 | return mService.connect(device); |
| 122 | } |
| 123 | |
| 124 | public boolean disconnect(BluetoothDevice device) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 125 | if (mService == null) { |
| 126 | return false; |
| 127 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 128 | // Downgrade priority as user is disconnecting the headset. |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 129 | if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 130 | 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) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 143 | if (mService == null) { |
| 144 | return false; |
| 145 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 146 | return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; |
| 147 | } |
| 148 | |
| 149 | public int getPreferred(BluetoothDevice device) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 150 | if (mService == null) { |
| 151 | return BluetoothProfile.PRIORITY_OFF; |
| 152 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 153 | return mService.getPriority(device); |
| 154 | } |
| 155 | |
| 156 | public void setPreferred(BluetoothDevice device, boolean preferred) { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 157 | if (mService == null) { |
| 158 | return; |
| 159 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 160 | 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() { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 170 | if (mService == null) { |
| 171 | return false; |
| 172 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 173 | 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 Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 205 | return BluetoothUtils.getConnectionStateSummary(state); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | public int getDrawableResource(BluetoothClass btClass) { |
| 210 | return R.drawable.ic_bt_headphones_a2dp; |
| 211 | } |
| 212 | |
| 213 | protected void finalize() { |
Chienyuan | 447fbde | 2018-09-05 21:28:59 +0800 | [diff] [blame^] | 214 | Log.d(TAG, "finalize()"); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 215 | 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 | } |