diff --git a/src/com/android/settings/bluetooth/A2dpProfile.java b/src/com/android/settings/bluetooth/A2dpProfile.java old mode 100644 new mode 100755 index b7ba44d93cd..98e45e2684c --- a/src/com/android/settings/bluetooth/A2dpProfile.java +++ b/src/com/android/settings/bluetooth/A2dpProfile.java @@ -42,6 +42,7 @@ final class A2dpProfile implements LocalBluetoothProfile { }; static final String NAME = "A2DP"; + private final LocalBluetoothProfileManager mProfileManager; // Order of this profile in device profiles list private static final int ORDINAL = 1; @@ -52,14 +53,18 @@ final class A2dpProfile implements LocalBluetoothProfile { public void onServiceConnected(int profile, BluetoothProfile proxy) { mService = (BluetoothA2dp) proxy; + mProfileManager.setA2dpServiceUp(true); } public void onServiceDisconnected(int profile) { mService = null; + mProfileManager.setA2dpServiceUp(false); } } - A2dpProfile(Context context) { + A2dpProfile(Context context, LocalBluetoothProfileManager profileManager) { + + mProfileManager = profileManager; BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); adapter.getProfileProxy(context, new A2dpServiceListener(), BluetoothProfile.A2DP); @@ -94,6 +99,19 @@ final class A2dpProfile implements LocalBluetoothProfile { return mService.disconnect(device); } + // This function is added as the AUTO CONNECT priority could not be set by using setPreferred(), + // as setPreferred() takes only boolean input but getPreferred() supports interger output. + // Also this need not implemented by all profiles so this has been added here. + public void enableAutoConnect(BluetoothDevice device, boolean enable) { + if (enable) { + mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT); + } else { + if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) { + mService.setPriority(device, BluetoothProfile.PRIORITY_ON); + } + } + } + public int getConnectionStatus(BluetoothDevice device) { return mService.getConnectionState(device); } diff --git a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java index cb2f6594f8d..2aa0566d0c7 100755 --- a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java +++ b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java @@ -25,6 +25,7 @@ import android.os.ParcelUuid; import android.os.SystemClock; import android.text.TextUtils; import android.util.Log; +import android.bluetooth.BluetoothAdapter; import java.util.ArrayList; import java.util.Collection; @@ -118,6 +119,29 @@ final class CachedBluetoothDevice implements Comparable { Log.d(TAG, "onProfileStateChanged: profile " + profile + " newProfileState " + newProfileState); } + if (profile instanceof HeadsetProfile) { + if (newProfileState == BluetoothProfile.STATE_CONNECTED) { + if (BluetoothProfile.PRIORITY_AUTO_CONNECT != profile.getPreferred(mDevice)) + mProfileManager.enableAutoConnectForHf(mDevice, true); + } else if (newProfileState == BluetoothProfile.STATE_DISCONNECTED) { + // dont reset auto connect priority when bluetooth turned off + if ((BluetoothAdapter.STATE_ON == mLocalAdapter.getBluetoothState()) + || (BluetoothAdapter.STATE_TURNING_ON == mLocalAdapter.getBluetoothState())) { + mProfileManager.enableAutoConnectForHf(mDevice, false); + } + } + } else if (profile instanceof A2dpProfile ) { + if (newProfileState == BluetoothProfile.STATE_CONNECTED) { + if (BluetoothProfile.PRIORITY_AUTO_CONNECT != profile.getPreferred(mDevice)) + mProfileManager.enableAutoConnectForA2dp(mDevice,true); + } else if (newProfileState == BluetoothProfile.STATE_DISCONNECTED) { + // dont reset auto connect priority when bluetooth turned off + if ((BluetoothAdapter.STATE_ON == mLocalAdapter.getBluetoothState()) + || (BluetoothAdapter.STATE_TURNING_ON == mLocalAdapter.getBluetoothState())) { + mProfileManager.enableAutoConnectForA2dp(mDevice, false); + } + } + } mProfileConnectionState.put(profile, newProfileState); if (newProfileState == BluetoothProfile.STATE_CONNECTED) { @@ -238,7 +262,7 @@ final class CachedBluetoothDevice implements Comparable { connectInt(profile); } - private void connectInt(LocalBluetoothProfile profile) { + synchronized void connectInt(LocalBluetoothProfile profile) { if (!ensurePaired()) { return; } diff --git a/src/com/android/settings/bluetooth/HeadsetProfile.java b/src/com/android/settings/bluetooth/HeadsetProfile.java old mode 100644 new mode 100755 index 99d070b9b27..34576f7e1ca --- a/src/com/android/settings/bluetooth/HeadsetProfile.java +++ b/src/com/android/settings/bluetooth/HeadsetProfile.java @@ -63,6 +63,7 @@ final class HeadsetProfile implements LocalBluetoothProfile { // headset device. List deviceList = mService.getConnectedDevices(); if (deviceList.isEmpty()) { + mProfileManager.setHfServiceUp(true); return; } BluetoothDevice firstDevice = deviceList.get(0); @@ -76,12 +77,14 @@ final class HeadsetProfile implements LocalBluetoothProfile { BluetoothProfile.STATE_CONNECTED); mProfileManager.callServiceConnectedListeners(); + mProfileManager.setHfServiceUp(true); } public void onServiceDisconnected(int profile) { mProfileReady = false; mService = null; mProfileManager.callServiceDisconnectedListeners(); + mProfileManager.setHfServiceUp(false); } } @@ -158,6 +161,19 @@ final class HeadsetProfile implements LocalBluetoothProfile { } } +// This function is added as the AUTO CONNECT priority could not be set by using setPreferred(), +// as setPreferred() takes only boolean input but getPreferred() supports interger output. +// Also this need not implemented by all profiles so this has been added here. + public void enableAutoConnect(BluetoothDevice device, boolean enable) { + if (enable) { + mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT); + } else { + if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) { + mService.setPriority(device, BluetoothProfile.PRIORITY_ON); + } + } + } + public synchronized boolean isProfileReady() { return mProfileReady; } diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java old mode 100644 new mode 100755 index f3143f0b8e7..de90de2628d --- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Set; /** * LocalBluetoothProfileManager provides access to the LocalBluetoothProfile @@ -77,6 +78,8 @@ final class LocalBluetoothProfileManager { private final HidProfile mHidProfile; private OppProfile mOppProfile; private final PanProfile mPanProfile; + private boolean isHfServiceUp; + private boolean isA2dpServiceUp; /** * Mapping from profile name, e.g. "HEADSET" to profile object. @@ -128,7 +131,7 @@ final class LocalBluetoothProfileManager { if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSource)) { if (mA2dpProfile == null) { Log.d(TAG, "Adding local A2DP profile"); - mA2dpProfile = new A2dpProfile(mContext); + mA2dpProfile = new A2dpProfile(mContext, this); addProfile(mA2dpProfile, A2dpProfile.NAME, BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); } @@ -264,6 +267,51 @@ final class LocalBluetoothProfileManager { } } + synchronized void setHfServiceUp(boolean isUp) { + isHfServiceUp = isUp; + if (isHfServiceUp && isA2dpServiceUp) { + // connect hf and then a2dp + // this order is maintained as per the white paper + handleAutoConnect(mHeadsetProfile); + handleAutoConnect(mA2dpProfile); + } + } + + synchronized void setA2dpServiceUp(boolean isUp) { + isA2dpServiceUp= isUp; + if (isHfServiceUp && isA2dpServiceUp) { + // connect hf and then a2dp + // this order is maintained as per the white paper + handleAutoConnect(mHeadsetProfile); + handleAutoConnect(mA2dpProfile); + } + } + + private void handleAutoConnect(LocalBluetoothProfile profile) { + Set bondedDevices = mLocalAdapter.getBondedDevices(); + for (BluetoothDevice device : bondedDevices) { + if (profile.getPreferred(device) == + BluetoothProfile.PRIORITY_AUTO_CONNECT) { + Log.d(TAG,"handleAutoConnect for device"); + CachedBluetoothDevice cacheDevice = mDeviceManager.findDevice(device); + if (null != cacheDevice) { + cacheDevice.connectInt(profile); + break; + } + else + Log.e(TAG,"Bluetooth cache devices mismatch with actual"); + } + } + } + + public void enableAutoConnectForHf(BluetoothDevice device,boolean enable) { + mHeadsetProfile.enableAutoConnect(device,enable); + } + + public void enableAutoConnectForA2dp(BluetoothDevice device,boolean enable) { + mA2dpProfile.enableAutoConnect(device,enable); + } + // This is called by DockService, so check Headset and A2DP. public synchronized boolean isManagerReady() { // Getting just the headset profile is fine for now. Will need to deal with A2DP