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