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