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