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 | |
| 40 | private final LocalBluetoothAdapter mLocalAdapter; |
| 41 | private final CachedBluetoothDeviceManager mDeviceManager; |
| 42 | |
| 43 | static final ParcelUuid[] SRC_UUIDS = { |
| 44 | BluetoothUuid.AudioSource, |
| 45 | BluetoothUuid.AdvAudioDist, |
| 46 | }; |
| 47 | |
| 48 | static final String NAME = "A2DPSink"; |
| 49 | private final LocalBluetoothProfileManager mProfileManager; |
| 50 | |
| 51 | // Order of this profile in device profiles list |
| 52 | private static final int ORDINAL = 5; |
| 53 | |
| 54 | // These callbacks run on the main thread. |
| 55 | private final class A2dpSinkServiceListener |
| 56 | implements BluetoothProfile.ServiceListener { |
| 57 | |
| 58 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 59 | Log.d(TAG, "Bluetooth service connected, profile:" + profile); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 60 | mService = (BluetoothA2dpSink) proxy; |
| 61 | // We just bound to the service, so refresh the UI for any connected A2DP devices. |
| 62 | List<BluetoothDevice> deviceList = mService.getConnectedDevices(); |
| 63 | while (!deviceList.isEmpty()) { |
| 64 | BluetoothDevice nextDevice = deviceList.remove(0); |
| 65 | CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice); |
| 66 | // we may add a new device here, but generally this should not happen |
| 67 | if (device == null) { |
| 68 | Log.w(TAG, "A2dpSinkProfile found new device: " + nextDevice); |
| 69 | device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice); |
| 70 | } |
| 71 | device.onProfileStateChanged(A2dpSinkProfile.this, BluetoothProfile.STATE_CONNECTED); |
| 72 | device.refresh(); |
| 73 | } |
| 74 | mIsProfileReady=true; |
| 75 | } |
| 76 | |
| 77 | public void onServiceDisconnected(int profile) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 78 | Log.d(TAG, "Bluetooth service disconnected, profile:" + profile); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 79 | mIsProfileReady=false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public boolean isProfileReady() { |
| 84 | return mIsProfileReady; |
| 85 | } |
| 86 | |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame] | 87 | @Override |
| 88 | public int getProfileId() { |
| 89 | return BluetoothProfile.A2DP_SINK; |
| 90 | } |
| 91 | |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 92 | A2dpSinkProfile(Context context, LocalBluetoothAdapter adapter, |
| 93 | CachedBluetoothDeviceManager deviceManager, |
| 94 | LocalBluetoothProfileManager profileManager) { |
| 95 | mLocalAdapter = adapter; |
| 96 | mDeviceManager = deviceManager; |
| 97 | mProfileManager = profileManager; |
| 98 | mLocalAdapter.getProfileProxy(context, new A2dpSinkServiceListener(), |
| 99 | BluetoothProfile.A2DP_SINK); |
| 100 | } |
| 101 | |
Leon Liao | 3b5e6bd | 2018-09-18 12:45:45 +0800 | [diff] [blame^] | 102 | public boolean accessProfileEnabled() { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | public boolean isAutoConnectable() { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | public List<BluetoothDevice> getConnectedDevices() { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 111 | if (mService == null) { |
| 112 | return new ArrayList<BluetoothDevice>(0); |
| 113 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 114 | return mService.getDevicesMatchingConnectionStates( |
| 115 | new int[] {BluetoothProfile.STATE_CONNECTED, |
| 116 | BluetoothProfile.STATE_CONNECTING, |
| 117 | BluetoothProfile.STATE_DISCONNECTING}); |
| 118 | } |
| 119 | |
| 120 | public boolean connect(BluetoothDevice device) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 121 | if (mService == null) { |
| 122 | return false; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 123 | } |
| 124 | return mService.connect(device); |
| 125 | } |
| 126 | |
| 127 | public boolean disconnect(BluetoothDevice device) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 128 | if (mService == null) { |
| 129 | return false; |
| 130 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 131 | // Downgrade priority as user is disconnecting the headset. |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 132 | if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 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) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 146 | if (mService == null) { |
| 147 | return false; |
| 148 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 149 | return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; |
| 150 | } |
| 151 | |
| 152 | public int getPreferred(BluetoothDevice device) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 153 | if (mService == null) { |
| 154 | return BluetoothProfile.PRIORITY_OFF; |
| 155 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 156 | return mService.getPriority(device); |
| 157 | } |
| 158 | |
| 159 | public void setPreferred(BluetoothDevice device, boolean preferred) { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 160 | if (mService == null) { |
| 161 | return; |
| 162 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 163 | if (preferred) { |
| 164 | if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { |
| 165 | mService.setPriority(device, BluetoothProfile.PRIORITY_ON); |
| 166 | } |
| 167 | } else { |
| 168 | mService.setPriority(device, BluetoothProfile.PRIORITY_OFF); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | boolean isA2dpPlaying() { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 173 | if (mService == null) { |
| 174 | return false; |
| 175 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 176 | List<BluetoothDevice> srcs = mService.getConnectedDevices(); |
| 177 | if (!srcs.isEmpty()) { |
| 178 | if (mService.isA2dpPlaying(srcs.get(0))) { |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | public String toString() { |
| 186 | return NAME; |
| 187 | } |
| 188 | |
| 189 | public int getOrdinal() { |
| 190 | return ORDINAL; |
| 191 | } |
| 192 | |
| 193 | public int getNameResource(BluetoothDevice device) { |
| 194 | // we need to have same string in UI for even SINK Media Audio. |
| 195 | return R.string.bluetooth_profile_a2dp; |
| 196 | } |
| 197 | |
| 198 | public int getSummaryResourceForDevice(BluetoothDevice device) { |
| 199 | int state = getConnectionStatus(device); |
| 200 | switch (state) { |
| 201 | case BluetoothProfile.STATE_DISCONNECTED: |
| 202 | return R.string.bluetooth_a2dp_profile_summary_use_for; |
| 203 | |
| 204 | case BluetoothProfile.STATE_CONNECTED: |
| 205 | return R.string.bluetooth_a2dp_profile_summary_connected; |
| 206 | |
| 207 | default: |
| 208 | return Utils.getConnectionStateSummary(state); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public int getDrawableResource(BluetoothClass btClass) { |
| 213 | return R.drawable.ic_bt_headphones_a2dp; |
| 214 | } |
| 215 | |
| 216 | protected void finalize() { |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 217 | Log.d(TAG, "finalize()"); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 218 | if (mService != null) { |
| 219 | try { |
| 220 | BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP_SINK, |
Chienyuan | dcbc7de | 2018-09-05 21:28:59 +0800 | [diff] [blame] | 221 | mService); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 222 | mService = null; |
| 223 | }catch (Throwable t) { |
| 224 | Log.w(TAG, "Error cleaning up A2DP proxy", t); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |