The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | /** |
| 18 | * TODO: Move this to services.jar |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 19 | * and make the constructor package private again. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | * @hide |
| 21 | */ |
| 22 | |
| 23 | package android.server; |
| 24 | |
| 25 | import android.bluetooth.BluetoothA2dp; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 26 | import android.bluetooth.BluetoothAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.bluetooth.BluetoothDevice; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 28 | import android.bluetooth.BluetoothProfile; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 29 | import android.bluetooth.BluetoothUuid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.bluetooth.IBluetoothA2dp; |
| 31 | import android.content.BroadcastReceiver; |
| 32 | import android.content.Context; |
| 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.media.AudioManager; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 36 | import android.os.ParcelUuid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.provider.Settings; |
| 38 | import android.util.Log; |
| 39 | |
| 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import java.util.HashMap; |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 44 | import java.util.List; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | public class BluetoothA2dpService extends IBluetoothA2dp.Stub { |
| 48 | private static final String TAG = "BluetoothA2dpService"; |
| 49 | private static final boolean DBG = true; |
| 50 | |
| 51 | public static final String BLUETOOTH_A2DP_SERVICE = "bluetooth_a2dp"; |
| 52 | |
| 53 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 54 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
| 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | private static final String BLUETOOTH_ENABLED = "bluetooth_enabled"; |
| 57 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 58 | private static final String PROPERTY_STATE = "State"; |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | private final Context mContext; |
| 61 | private final IntentFilter mIntentFilter; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 62 | private HashMap<BluetoothDevice, Integer> mAudioDevices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | private final AudioManager mAudioManager; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 64 | private final BluetoothService mBluetoothService; |
| 65 | private final BluetoothAdapter mAdapter; |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 66 | private int mTargetA2dpState; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 67 | private BluetoothDevice mPlayingA2dpDevice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 70 | @Override |
| 71 | public void onReceive(Context context, Intent intent) { |
| 72 | String action = intent.getAction(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 73 | BluetoothDevice device = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 74 | intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 75 | if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 76 | int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 77 | BluetoothAdapter.ERROR); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 78 | switch (state) { |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 79 | case BluetoothAdapter.STATE_ON: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 80 | onBluetoothEnable(); |
| 81 | break; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 82 | case BluetoothAdapter.STATE_TURNING_OFF: |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 83 | onBluetoothDisable(); |
| 84 | break; |
| 85 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 86 | } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { |
| 87 | int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 88 | BluetoothDevice.ERROR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | switch(bondState) { |
| 90 | case BluetoothDevice.BOND_BONDED: |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 91 | if (getPriority(device) == BluetoothA2dp.PRIORITY_UNDEFINED) { |
| 92 | setPriority(device, BluetoothA2dp.PRIORITY_ON); |
Jaikumar Ganesh | 551ed72 | 2009-12-11 12:00:31 -0800 | [diff] [blame] | 93 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | break; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 95 | case BluetoothDevice.BOND_NONE: |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 96 | setPriority(device, BluetoothA2dp.PRIORITY_UNDEFINED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | break; |
| 98 | } |
Jaikumar Ganesh | 4c9a29e | 2009-09-30 15:59:35 -0700 | [diff] [blame] | 99 | } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) { |
| 100 | synchronized (this) { |
| 101 | if (mAudioDevices.containsKey(device)) { |
| 102 | int state = mAudioDevices.get(device); |
| 103 | handleSinkStateChange(device, state, BluetoothA2dp.STATE_DISCONNECTED); |
| 104 | } |
| 105 | } |
Jaikumar Ganesh | 4f773a1 | 2010-02-16 11:57:06 -0800 | [diff] [blame] | 106 | } else if (action.equals(AudioManager.VOLUME_CHANGED_ACTION)) { |
| 107 | int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 108 | if (streamType == AudioManager.STREAM_MUSIC) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 109 | List<BluetoothDevice> sinks = getConnectedDevices(); |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 110 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 111 | if (sinks.size() != 0 && isPhoneDocked(sinks.get(0))) { |
| 112 | String address = sinks.get(0).getAddress(); |
Jaikumar Ganesh | 4f773a1 | 2010-02-16 11:57:06 -0800 | [diff] [blame] | 113 | int newVolLevel = |
| 114 | intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0); |
| 115 | int oldVolLevel = |
| 116 | intent.getIntExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, 0); |
| 117 | String path = mBluetoothService.getObjectPathFromAddress(address); |
| 118 | if (newVolLevel > oldVolLevel) { |
| 119 | avrcpVolumeUpNative(path); |
| 120 | } else if (newVolLevel < oldVolLevel) { |
| 121 | avrcpVolumeDownNative(path); |
| 122 | } |
| 123 | } |
| 124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
Jaikumar Ganesh | 4f773a1 | 2010-02-16 11:57:06 -0800 | [diff] [blame] | 129 | private boolean isPhoneDocked(BluetoothDevice device) { |
| 130 | // This works only because these broadcast intents are "sticky" |
| 131 | Intent i = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT)); |
| 132 | if (i != null) { |
| 133 | int state = i.getIntExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 134 | if (state != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 135 | BluetoothDevice dockDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 136 | if (dockDevice != null && device.equals(dockDevice)) { |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 144 | public BluetoothA2dpService(Context context, BluetoothService bluetoothService) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 145 | mContext = context; |
| 146 | |
| 147 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 148 | |
| 149 | mBluetoothService = bluetoothService; |
| 150 | if (mBluetoothService == null) { |
| 151 | throw new RuntimeException("Platform does not support Bluetooth"); |
| 152 | } |
| 153 | |
| 154 | if (!initNative()) { |
| 155 | throw new RuntimeException("Could not init BluetoothA2dpService"); |
| 156 | } |
| 157 | |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 158 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 159 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 160 | mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 161 | mIntentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); |
| 162 | mIntentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); |
Jaikumar Ganesh | 4c9a29e | 2009-09-30 15:59:35 -0700 | [diff] [blame] | 163 | mIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); |
Jaikumar Ganesh | 4f773a1 | 2010-02-16 11:57:06 -0800 | [diff] [blame] | 164 | mIntentFilter.addAction(AudioManager.VOLUME_CHANGED_ACTION); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 165 | mContext.registerReceiver(mReceiver, mIntentFilter); |
| 166 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 167 | mAudioDevices = new HashMap<BluetoothDevice, Integer>(); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 168 | |
| 169 | if (mBluetoothService.isEnabled()) |
| 170 | onBluetoothEnable(); |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 171 | mTargetA2dpState = -1; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 172 | mBluetoothService.setA2dpService(this); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | protected void finalize() throws Throwable { |
| 177 | try { |
| 178 | cleanupNative(); |
| 179 | } finally { |
| 180 | super.finalize(); |
| 181 | } |
| 182 | } |
| 183 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 184 | private int convertBluezSinkStringToState(String value) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 185 | if (value.equalsIgnoreCase("disconnected")) |
| 186 | return BluetoothA2dp.STATE_DISCONNECTED; |
| 187 | if (value.equalsIgnoreCase("connecting")) |
| 188 | return BluetoothA2dp.STATE_CONNECTING; |
| 189 | if (value.equalsIgnoreCase("connected")) |
| 190 | return BluetoothA2dp.STATE_CONNECTED; |
| 191 | if (value.equalsIgnoreCase("playing")) |
| 192 | return BluetoothA2dp.STATE_PLAYING; |
| 193 | return -1; |
| 194 | } |
| 195 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 196 | private boolean isSinkDevice(BluetoothDevice device) { |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 197 | ParcelUuid[] uuids = mBluetoothService.getRemoteUuids(device.getAddress()); |
| 198 | if (uuids != null && BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSink)) { |
| 199 | return true; |
Jaikumar Ganesh | 7f74d53 | 2009-07-14 12:21:26 -0700 | [diff] [blame] | 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 204 | private synchronized boolean addAudioSink(BluetoothDevice device) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 205 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 206 | String propValues[] = (String []) getSinkPropertiesNative(path); |
| 207 | if (propValues == null) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 208 | Log.e(TAG, "Error while getting AudioSink properties for device: " + device); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | Integer state = null; |
| 212 | // Properties are name-value pairs |
| 213 | for (int i = 0; i < propValues.length; i+=2) { |
| 214 | if (propValues[i].equals(PROPERTY_STATE)) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 215 | state = new Integer(convertBluezSinkStringToState(propValues[i+1])); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 219 | mAudioDevices.put(device, state); |
| 220 | handleSinkStateChange(device, BluetoothA2dp.STATE_DISCONNECTED, state); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | private synchronized void onBluetoothEnable() { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 225 | String devices = mBluetoothService.getProperty("Devices"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 226 | if (devices != null) { |
| 227 | String [] paths = devices.split(","); |
| 228 | for (String path: paths) { |
| 229 | String address = mBluetoothService.getAddressFromObjectPath(path); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 230 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 231 | ParcelUuid[] remoteUuids = mBluetoothService.getRemoteUuids(address); |
| 232 | if (remoteUuids != null) |
| 233 | if (BluetoothUuid.containsAnyUuid(remoteUuids, |
| 234 | new ParcelUuid[] {BluetoothUuid.AudioSink, |
| 235 | BluetoothUuid.AdvAudioDist})) { |
| 236 | addAudioSink(device); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 237 | } |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 238 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 240 | mAudioManager.setParameters(BLUETOOTH_ENABLED+"=true"); |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 241 | mAudioManager.setParameters("A2dpSuspended=false"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | private synchronized void onBluetoothDisable() { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 245 | if (!mAudioDevices.isEmpty()) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 246 | BluetoothDevice[] devices = new BluetoothDevice[mAudioDevices.size()]; |
| 247 | devices = mAudioDevices.keySet().toArray(devices); |
| 248 | for (BluetoothDevice device : devices) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 249 | int state = getConnectionState(device); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 250 | switch (state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | case BluetoothA2dp.STATE_CONNECTING: |
| 252 | case BluetoothA2dp.STATE_CONNECTED: |
| 253 | case BluetoothA2dp.STATE_PLAYING: |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 254 | disconnectSinkNative(mBluetoothService.getObjectPathFromAddress( |
| 255 | device.getAddress())); |
| 256 | handleSinkStateChange(device, state, BluetoothA2dp.STATE_DISCONNECTED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | break; |
| 258 | case BluetoothA2dp.STATE_DISCONNECTING: |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 259 | handleSinkStateChange(device, BluetoothA2dp.STATE_DISCONNECTING, |
| 260 | BluetoothA2dp.STATE_DISCONNECTED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | break; |
| 262 | } |
| 263 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 264 | mAudioDevices.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 266 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 267 | mAudioManager.setParameters(BLUETOOTH_ENABLED + "=false"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 270 | private synchronized boolean isConnectSinkFeasible(BluetoothDevice device) { |
| 271 | if (!mBluetoothService.isEnabled() || !isSinkDevice(device) || |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 272 | getPriority(device) == BluetoothA2dp.PRIORITY_OFF) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 273 | return false; |
| 274 | } |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 275 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 276 | if (mAudioDevices.get(device) == null && !addAudioSink(device)) { |
| 277 | return false; |
| 278 | } |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 279 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 280 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
| 281 | if (path == null) { |
| 282 | return false; |
| 283 | } |
| 284 | return true; |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 287 | public synchronized boolean isA2dpPlaying(BluetoothDevice device) { |
| 288 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 289 | "Need BLUETOOTH_ADMIN permission"); |
| 290 | if (DBG) log("isA2dpPlaying(" + device + ")"); |
| 291 | if (device.equals(mPlayingA2dpDevice)) return true; |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | public synchronized boolean connect(BluetoothDevice device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 297 | "Need BLUETOOTH_ADMIN permission"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 298 | if (DBG) log("connectSink(" + device + ")"); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 299 | if (!isConnectSinkFeasible(device)) return false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 300 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 301 | for (BluetoothDevice sinkDevice : mAudioDevices.keySet()) { |
| 302 | if (getConnectionState(sinkDevice) != BluetoothProfile.STATE_DISCONNECTED) { |
| 303 | disconnect(sinkDevice); |
| 304 | } |
| 305 | } |
| 306 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 307 | return mBluetoothService.connectSink(device.getAddress()); |
| 308 | } |
| 309 | |
| 310 | public synchronized boolean connectSinkInternal(BluetoothDevice device) { |
Jaikumar Ganesh | 4aae54c | 2010-04-07 17:28:08 -0700 | [diff] [blame] | 311 | if (!mBluetoothService.isEnabled()) return false; |
| 312 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 313 | int state = mAudioDevices.get(device); |
| 314 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 315 | // ignore if there are any active sinks |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 316 | if (getDevicesMatchingConnectionStates(new int[] { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 317 | BluetoothA2dp.STATE_CONNECTING, |
| 318 | BluetoothA2dp.STATE_CONNECTED, |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 319 | BluetoothA2dp.STATE_DISCONNECTING}).size() != 0) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 320 | return false; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 321 | } |
| 322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | switch (state) { |
| 324 | case BluetoothA2dp.STATE_CONNECTED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | case BluetoothA2dp.STATE_DISCONNECTING: |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 326 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | case BluetoothA2dp.STATE_CONNECTING: |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 328 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 331 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 332 | |
Jaikumar Ganesh | bcf5cf4 | 2010-10-12 15:15:55 -0700 | [diff] [blame] | 333 | // State is DISCONNECTED and we are connecting. |
Jaikumar Ganesh | aa4b235 | 2010-10-14 09:36:39 -0700 | [diff] [blame] | 334 | if (getPriority(device) < BluetoothA2dp.PRIORITY_AUTO_CONNECT) { |
| 335 | setPriority(device, BluetoothA2dp.PRIORITY_AUTO_CONNECT); |
Jaikumar Ganesh | bcf5cf4 | 2010-10-12 15:15:55 -0700 | [diff] [blame] | 336 | } |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 337 | handleSinkStateChange(device, state, BluetoothA2dp.STATE_CONNECTING); |
| 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | if (!connectSinkNative(path)) { |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 340 | // Restore previous state |
| 341 | handleSinkStateChange(device, mAudioDevices.get(device), state); |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 342 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | } |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 344 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 347 | private synchronized boolean isDisconnectSinkFeasible(BluetoothDevice device) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 348 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | if (path == null) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 350 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 352 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 353 | int state = getConnectionState(device); |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 354 | switch (state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | case BluetoothA2dp.STATE_DISCONNECTED: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | case BluetoothA2dp.STATE_DISCONNECTING: |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 357 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 362 | public synchronized boolean disconnect(BluetoothDevice device) { |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 363 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 364 | "Need BLUETOOTH_ADMIN permission"); |
| 365 | if (DBG) log("disconnectSink(" + device + ")"); |
| 366 | if (!isDisconnectSinkFeasible(device)) return false; |
| 367 | return mBluetoothService.disconnectSink(device.getAddress()); |
| 368 | } |
| 369 | |
| 370 | public synchronized boolean disconnectSinkInternal(BluetoothDevice device) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 371 | int state = getConnectionState(device); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 372 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | |
Jaikumar Ganesh | 5844f1d | 2010-10-14 12:11:25 -0700 | [diff] [blame] | 374 | switch (state) { |
| 375 | case BluetoothA2dp.STATE_DISCONNECTED: |
| 376 | case BluetoothA2dp.STATE_DISCONNECTING: |
| 377 | return false; |
| 378 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | // State is CONNECTING or CONNECTED or PLAYING |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 380 | handleSinkStateChange(device, state, BluetoothA2dp.STATE_DISCONNECTING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | if (!disconnectSinkNative(path)) { |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 382 | // Restore previous state |
| 383 | handleSinkStateChange(device, mAudioDevices.get(device), state); |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 384 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 386 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 389 | public synchronized boolean suspendSink(BluetoothDevice device) { |
| 390 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 391 | "Need BLUETOOTH_ADMIN permission"); |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 392 | if (DBG) log("suspendSink(" + device + "), mTargetA2dpState: "+mTargetA2dpState); |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 393 | if (device == null || mAudioDevices == null) { |
| 394 | return false; |
| 395 | } |
| 396 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
Nick Pelly | 62895a6 | 2009-09-30 15:30:43 -0700 | [diff] [blame] | 397 | Integer state = mAudioDevices.get(device); |
| 398 | if (path == null || state == null) { |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 399 | return false; |
| 400 | } |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 401 | |
| 402 | mTargetA2dpState = BluetoothA2dp.STATE_CONNECTED; |
| 403 | return checkSinkSuspendState(state.intValue()); |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | public synchronized boolean resumeSink(BluetoothDevice device) { |
| 407 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 408 | "Need BLUETOOTH_ADMIN permission"); |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 409 | if (DBG) log("resumeSink(" + device + "), mTargetA2dpState: "+mTargetA2dpState); |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 410 | if (device == null || mAudioDevices == null) { |
| 411 | return false; |
| 412 | } |
| 413 | String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); |
Nick Pelly | 62895a6 | 2009-09-30 15:30:43 -0700 | [diff] [blame] | 414 | Integer state = mAudioDevices.get(device); |
| 415 | if (path == null || state == null) { |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 416 | return false; |
| 417 | } |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 418 | mTargetA2dpState = BluetoothA2dp.STATE_PLAYING; |
| 419 | return checkSinkSuspendState(state.intValue()); |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 420 | } |
| 421 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 422 | public synchronized int getConnectionState(BluetoothDevice device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 424 | Integer state = mAudioDevices.get(device); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 425 | if (state == null) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | return BluetoothA2dp.STATE_DISCONNECTED; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 427 | return state; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 430 | public synchronized List<BluetoothDevice> getConnectedDevices() { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 431 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 432 | List<BluetoothDevice> sinks = getDevicesMatchingConnectionStates( |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 433 | new int[] {BluetoothA2dp.STATE_CONNECTED}); |
| 434 | return sinks; |
| 435 | } |
| 436 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 437 | public synchronized List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 438 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 439 | ArrayList<BluetoothDevice> sinks = new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 440 | for (BluetoothDevice device: mAudioDevices.keySet()) { |
| 441 | int sinkState = getConnectionState(device); |
| 442 | for (int state : states) { |
| 443 | if (state == sinkState) { |
| 444 | sinks.add(device); |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | } |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 449 | return sinks; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | public synchronized int getPriority(BluetoothDevice device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | return Settings.Secure.getInt(mContext.getContentResolver(), |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 455 | Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()), |
Jaikumar Ganesh | ccaebfc | 2010-01-08 18:26:14 -0800 | [diff] [blame] | 456 | BluetoothA2dp.PRIORITY_UNDEFINED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 459 | public synchronized boolean setPriority(BluetoothDevice device, int priority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 461 | "Need BLUETOOTH_ADMIN permission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | return Settings.Secure.putInt(mContext.getContentResolver(), |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 463 | Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()), priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 466 | /** |
| 467 | * Called by native code on a PropertyChanged signal from |
| 468 | * org.bluez.AudioSink. |
| 469 | * |
| 470 | * @param path the object path for the changed device |
| 471 | * @param propValues a string array containing the key and one or more |
| 472 | * values. |
| 473 | */ |
| 474 | private synchronized void onSinkPropertyChanged(String path, String[] propValues) { |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 475 | if (!mBluetoothService.isEnabled()) { |
| 476 | return; |
| 477 | } |
| 478 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 479 | String name = propValues[0]; |
| 480 | String address = mBluetoothService.getAddressFromObjectPath(path); |
| 481 | if (address == null) { |
| 482 | Log.e(TAG, "onSinkPropertyChanged: Address of the remote device in null"); |
| 483 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 486 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 487 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 488 | if (name.equals(PROPERTY_STATE)) { |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 489 | int state = convertBluezSinkStringToState(propValues[1]); |
Jaikumar Ganesh | 8febf88 | 2010-10-26 00:28:39 -0700 | [diff] [blame] | 490 | log("A2DP: onSinkPropertyChanged newState is: " + state + "mPlayingA2dpDevice: " + mPlayingA2dpDevice); |
| 491 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 492 | if (mAudioDevices.get(device) == null) { |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 493 | // This is for an incoming connection for a device not known to us. |
| 494 | // We have authorized it and bluez state has changed. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 495 | addAudioSink(device); |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 496 | } else { |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 497 | if (state == BluetoothA2dp.STATE_PLAYING && mPlayingA2dpDevice == null) { |
| 498 | mPlayingA2dpDevice = device; |
| 499 | handleSinkPlayingStateChange(device, state, BluetoothA2dp.STATE_NOT_PLAYING); |
| 500 | } else if (state == BluetoothA2dp.STATE_CONNECTED && mPlayingA2dpDevice != null) { |
| 501 | mPlayingA2dpDevice = null; |
| 502 | handleSinkPlayingStateChange(device, BluetoothA2dp.STATE_NOT_PLAYING, |
| 503 | BluetoothA2dp.STATE_PLAYING); |
| 504 | } else { |
Jaikumar Ganesh | b2ec4fa | 2010-10-26 11:22:48 -0700 | [diff] [blame] | 505 | mPlayingA2dpDevice = null; |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 506 | int prevState = mAudioDevices.get(device); |
| 507 | handleSinkStateChange(device, prevState, state); |
| 508 | } |
Jaikumar Ganesh | 9488cbd | 2009-08-03 17:17:37 -0700 | [diff] [blame] | 509 | } |
Mike Lockwood | a0de355 | 2009-03-24 21:08:33 -0700 | [diff] [blame] | 510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 513 | private void handleSinkStateChange(BluetoothDevice device, int prevState, int state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | if (state != prevState) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 515 | mAudioDevices.put(device, state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 517 | checkSinkSuspendState(state); |
| 518 | mTargetA2dpState = -1; |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 519 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 520 | if (getPriority(device) > BluetoothA2dp.PRIORITY_OFF && |
Jaikumar Ganesh | 721361f | 2009-11-20 15:21:47 -0800 | [diff] [blame] | 521 | state == BluetoothA2dp.STATE_CONNECTED) { |
| 522 | // We have connected or attempting to connect. |
| 523 | // Bump priority |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 524 | setPriority(device, BluetoothA2dp.PRIORITY_AUTO_CONNECT); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 525 | // We will only have 1 device with AUTO_CONNECT priority |
| 526 | // To be backward compatible set everyone else to have PRIORITY_ON |
| 527 | adjustOtherSinkPriorities(device); |
| 528 | } |
| 529 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 530 | Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 531 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 532 | intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState); |
| 533 | intent.putExtra(BluetoothProfile.EXTRA_STATE, state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 535 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 536 | if (DBG) log("A2DP state : device: " + device + " State:" + prevState + "->" + state); |
Jaikumar Ganesh | a46f2fb | 2010-10-21 14:55:05 -0700 | [diff] [blame] | 537 | |
| 538 | mBluetoothService.sendConnectionStateChange(device, state, prevState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 542 | private void handleSinkPlayingStateChange(BluetoothDevice device, int state, int prevState) { |
| 543 | Intent intent = new Intent(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); |
| 544 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); |
| 545 | intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState); |
| 546 | intent.putExtra(BluetoothProfile.EXTRA_STATE, state); |
| 547 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 548 | |
| 549 | if (DBG) log("A2DP Playing state : device: " + device + " State:" + prevState + "->" + state); |
| 550 | } |
| 551 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 552 | private void adjustOtherSinkPriorities(BluetoothDevice connectedDevice) { |
Jaikumar Ganesh | aa4b235 | 2010-10-14 09:36:39 -0700 | [diff] [blame] | 553 | for (BluetoothDevice device : mAdapter.getBondedDevices()) { |
| 554 | if (getPriority(device) >= BluetoothA2dp.PRIORITY_AUTO_CONNECT && |
| 555 | !device.equals(connectedDevice)) { |
| 556 | setPriority(device, BluetoothA2dp.PRIORITY_ON); |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 557 | } |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Eric Laurent | a47d153 | 2009-10-16 09:16:26 -0700 | [diff] [blame] | 561 | private boolean checkSinkSuspendState(int state) { |
| 562 | boolean result = true; |
| 563 | |
| 564 | if (state != mTargetA2dpState) { |
| 565 | if (state == BluetoothA2dp.STATE_PLAYING && |
| 566 | mTargetA2dpState == BluetoothA2dp.STATE_CONNECTED) { |
| 567 | mAudioManager.setParameters("A2dpSuspended=true"); |
| 568 | } else if (state == BluetoothA2dp.STATE_CONNECTED && |
| 569 | mTargetA2dpState == BluetoothA2dp.STATE_PLAYING) { |
| 570 | mAudioManager.setParameters("A2dpSuspended=false"); |
| 571 | } else { |
| 572 | result = false; |
| 573 | } |
| 574 | } |
| 575 | return result; |
| 576 | } |
| 577 | |
Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 578 | /** |
| 579 | * Called by native code for the async response to a Connect |
| 580 | * method call to org.bluez.AudioSink. |
| 581 | * |
| 582 | * @param deviceObjectPath the object path for the connecting device |
| 583 | * @param result true on success; false on error |
| 584 | */ |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 585 | private void onConnectSinkResult(String deviceObjectPath, boolean result) { |
| 586 | // If the call was a success, ignore we will update the state |
| 587 | // when we a Sink Property Change |
| 588 | if (!result) { |
| 589 | if (deviceObjectPath != null) { |
| 590 | String address = mBluetoothService.getAddressFromObjectPath(deviceObjectPath); |
Jaikumar Ganesh | c0cec62 | 2010-04-19 17:19:31 -0700 | [diff] [blame] | 591 | if (address == null) return; |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 592 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
Jaikumar Ganesh | 96a7983 | 2010-09-27 17:02:01 -0700 | [diff] [blame] | 593 | int state = getConnectionState(device); |
Jaikumar Ganesh | c0e32f1 | 2009-12-16 11:36:39 -0800 | [diff] [blame] | 594 | handleSinkStateChange(device, state, BluetoothA2dp.STATE_DISCONNECTED); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | @Override |
| 600 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 601 | if (mAudioDevices.isEmpty()) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | pw.println("Cached audio devices:"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 603 | for (BluetoothDevice device : mAudioDevices.keySet()) { |
| 604 | int state = mAudioDevices.get(device); |
| 605 | pw.println(device + " " + BluetoothA2dp.stateToString(state)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | private static void log(String msg) { |
| 610 | Log.d(TAG, msg); |
| 611 | } |
| 612 | |
| 613 | private native boolean initNative(); |
| 614 | private native void cleanupNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | private synchronized native boolean connectSinkNative(String path); |
| 616 | private synchronized native boolean disconnectSinkNative(String path); |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 617 | private synchronized native boolean suspendSinkNative(String path); |
| 618 | private synchronized native boolean resumeSinkNative(String path); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 619 | private synchronized native Object []getSinkPropertiesNative(String path); |
Jaikumar Ganesh | 4f773a1 | 2010-02-16 11:57:06 -0800 | [diff] [blame] | 620 | private synchronized native boolean avrcpVolumeUpNative(String path); |
| 621 | private synchronized native boolean avrcpVolumeDownNative(String path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } |