1 /* 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 17 package com.android.settingslib.bluetooth; 18 19 import android.bluetooth.BluetoothA2dp; 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 public 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) { 119 mService.disconnect(sink); 120 } 121 } 122 return mService.connect(device); 123 } 124 125 public boolean disconnect(BluetoothDevice device) { 126 if (mService == null) return false; 127 // Downgrade priority as user is disconnecting the headset. 128 if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON){ 129 mService.setPriority(device, BluetoothProfile.PRIORITY_ON); 130 } 131 return mService.disconnect(device); 132 } 133 134 public int getConnectionStatus(BluetoothDevice device) { 135 if (mService == null) { 136 return BluetoothProfile.STATE_DISCONNECTED; 137 } 138 return mService.getConnectionState(device); 139 } 140 141 public boolean isPreferred(BluetoothDevice device) { 142 if (mService == null) return false; 143 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; 144 } 145 146 public int getPreferred(BluetoothDevice device) { 147 if (mService == null) return BluetoothProfile.PRIORITY_OFF; 148 return mService.getPriority(device); 149 } 150 151 public void setPreferred(BluetoothDevice device, boolean preferred) { 152 if (mService == null) return; 153 if (preferred) { 154 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { 155 mService.setPriority(device, BluetoothProfile.PRIORITY_ON); 156 } 157 } else { 158 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF); 159 } 160 } 161 boolean isA2dpPlaying() { 162 if (mService == null) return false; 163 List<BluetoothDevice> sinks = mService.getConnectedDevices(); 164 if (!sinks.isEmpty()) { 165 if (mService.isA2dpPlaying(sinks.get(0))) { 166 return true; 167 } 168 } 169 return false; 170 } 171 172 public String toString() { 173 return NAME; 174 } 175 176 public int getOrdinal() { 177 return ORDINAL; 178 } 179 180 public int getNameResource(BluetoothDevice device) { 181 return R.string.bluetooth_profile_a2dp; 182 } 183 184 public int getSummaryResourceForDevice(BluetoothDevice device) { 185 int state = getConnectionStatus(device); 186 switch (state) { 187 case BluetoothProfile.STATE_DISCONNECTED: 188 return R.string.bluetooth_a2dp_profile_summary_use_for; 189 190 case BluetoothProfile.STATE_CONNECTED: 191 return R.string.bluetooth_a2dp_profile_summary_connected; 192 193 default: 194 return Utils.getConnectionStateSummary(state); 195 } 196 } 197 198 public int getDrawableResource(BluetoothClass btClass) { 199 return R.drawable.ic_bt_headphones_a2dp; 200 } 201 202 protected void finalize() { 203 if (V) Log.d(TAG, "finalize()"); 204 if (mService != null) { 205 try { 206 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP, 207 mService); 208 mService = null; 209 }catch (Throwable t) { 210 Log.w(TAG, "Error cleaning up A2DP proxy", t); 211 } 212 } 213 } 214 } 215