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.settings.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 28 import com.android.settings.R; 29 30 import java.util.List; 31 32 /** 33 * A2dpProfile handles Bluetooth A2DP. 34 * TODO: add null checks around calls to mService object. 35 */ 36 final class A2dpProfile implements LocalBluetoothProfile { 37 private BluetoothA2dp mService; 38 39 static final ParcelUuid[] SINK_UUIDS = { 40 BluetoothUuid.AudioSink, 41 BluetoothUuid.AdvAudioDist, 42 }; 43 44 static final String NAME = "A2DP"; 45 46 // Order of this profile in device profiles list 47 private static final int ORDINAL = 1; 48 49 // These callbacks run on the main thread. 50 private final class A2dpServiceListener 51 implements BluetoothProfile.ServiceListener { 52 53 public void onServiceConnected(int profile, BluetoothProfile proxy) { 54 mService = (BluetoothA2dp) proxy; 55 } 56 57 public void onServiceDisconnected(int profile) { 58 mService = null; 59 } 60 } 61 62 A2dpProfile(Context context) { 63 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 64 adapter.getProfileProxy(context, new A2dpServiceListener(), 65 BluetoothProfile.A2DP); 66 } 67 68 public boolean isConnectable() { 69 return true; 70 } 71 72 public boolean isAutoConnectable() { 73 return true; 74 } 75 76 private List<BluetoothDevice> getConnectedDevices() { 77 return mService.getDevicesMatchingConnectionStates( 78 new int[] {BluetoothProfile.STATE_CONNECTED, 79 BluetoothProfile.STATE_CONNECTING, 80 BluetoothProfile.STATE_DISCONNECTING}); 81 } 82 83 public boolean connect(BluetoothDevice device) { 84 List<BluetoothDevice> sinks = getConnectedDevices(); 85 if (sinks != null) { 86 for (BluetoothDevice sink : sinks) { 87 mService.disconnect(sink); 88 } 89 } 90 return mService.connect(device); 91 } 92 93 public boolean disconnect(BluetoothDevice device) { 94 return mService.disconnect(device); 95 } 96 97 public int getConnectionStatus(BluetoothDevice device) { 98 return mService.getConnectionState(device); 99 } 100 101 public boolean isPreferred(BluetoothDevice device) { 102 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; 103 } 104 105 public int getPreferred(BluetoothDevice device) { 106 return mService.getPriority(device); 107 } 108 109 public void setPreferred(BluetoothDevice device, boolean preferred) { 110 if (preferred) { 111 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { 112 mService.setPriority(device, BluetoothProfile.PRIORITY_ON); 113 } 114 } else { 115 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF); 116 } 117 } 118 119 boolean isA2dpPlaying() { 120 List<BluetoothDevice> sinks = mService.getConnectedDevices(); 121 if (!sinks.isEmpty()) { 122 if (mService.isA2dpPlaying(sinks.get(0))) { 123 return true; 124 } 125 } 126 return false; 127 } 128 129 public boolean isProfileReady() { 130 return mService != null; 131 } 132 133 public String toString() { 134 return NAME; 135 } 136 137 public int getOrdinal() { 138 return ORDINAL; 139 } 140 141 public int getNameResource(BluetoothDevice device) { 142 return R.string.bluetooth_profile_a2dp; 143 } 144 145 public int getSummaryResourceForDevice(BluetoothDevice device) { 146 int state = mService.getConnectionState(device); 147 switch (state) { 148 case BluetoothProfile.STATE_DISCONNECTED: 149 return R.string.bluetooth_a2dp_profile_summary_use_for; 150 151 case BluetoothProfile.STATE_CONNECTED: 152 return R.string.bluetooth_a2dp_profile_summary_connected; 153 154 default: 155 return Utils.getConnectionStateSummary(state); 156 } 157 } 158 159 public int getDrawableResource(BluetoothClass btClass) { 160 return R.drawable.ic_bt_headphones_a2dp; 161 } 162 } 163