Add ability to connect to only headset profile.

Fix some SDP records issues.

Change-Id: I42816527b1ae0749b4b59b7947f1acb9d2e6f001
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index f89cc89..aae1311 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -353,7 +353,7 @@
         case BluetoothDevicePicker.FILTER_TYPE_AUDIO:
             if (uuids != null) {
                 if (BluetoothUuid.containsAnyUuid(uuids,
-                        LocalBluetoothProfileManager.A2DP_PROFILE_UUIDS))  return true;
+                        LocalBluetoothProfileManager.A2DP_SINK_PROFILE_UUIDS))  return true;
 
                 if (BluetoothUuid.containsAnyUuid(uuids,
                         LocalBluetoothProfileManager.HEADSET_PROFILE_UUIDS))  return true;
diff --git a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
index 76a5b99..df455b3 100644
--- a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
+++ b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
@@ -589,7 +589,11 @@
         ParcelUuid[] uuids = mDevice.getUuids();
         if (uuids == null) return false;
 
-        LocalBluetoothProfileManager.updateProfiles(uuids, mProfiles);
+        BluetoothAdapter adapter = mLocalManager.getBluetoothAdapter();
+        ParcelUuid[] localUuids = adapter.getUuids();
+        if (localUuids == null) return false;
+
+        LocalBluetoothProfileManager.updateProfiles(uuids, localUuids, mProfiles);
 
         if (DEBUG) {
             Log.e(TAG, "updating profiles for " + mDevice.getName());
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index 885706f..f177ed0 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -48,11 +48,15 @@
         BluetoothUuid.Handsfree,
     };
 
-    /* package */ static final ParcelUuid[] A2DP_PROFILE_UUIDS = new ParcelUuid[] {
+    /* package */ static final ParcelUuid[] A2DP_SINK_PROFILE_UUIDS = new ParcelUuid[] {
         BluetoothUuid.AudioSink,
         BluetoothUuid.AdvAudioDist,
     };
 
+    /* package */ static final ParcelUuid[] A2DP_SRC_PROFILE_UUIDS = new ParcelUuid[] {
+        BluetoothUuid.AudioSource
+    };
+
     /* package */ static final ParcelUuid[] OPP_PROFILE_UUIDS = new ParcelUuid[] {
         BluetoothUuid.ObexObjectPush
     };
@@ -124,25 +128,24 @@
     // TODO(): Combine the init and updateLocalProfiles codes.
     // init can get called from various paths, it makes no sense to add and then delete.
     public static void updateLocalProfiles(LocalBluetoothManager localManager, ParcelUuid[] uuids) {
-        if (!BluetoothUuid.containsAnyUuid(uuids, HEADSET_PROFILE_UUIDS)) {
+        if (!BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree_AG) &&
+            !BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP_AG)) {
             sProfileMap.remove(Profile.HEADSET);
         }
 
-        if (BluetoothUuid.containsAnyUuid(uuids, A2DP_PROFILE_UUIDS)) {
+        if (!BluetoothUuid.containsAnyUuid(uuids, A2DP_SRC_PROFILE_UUIDS)) {
             sProfileMap.remove(Profile.A2DP);
         }
 
-        if (BluetoothUuid.containsAnyUuid(uuids, OPP_PROFILE_UUIDS)) {
+        if (!BluetoothUuid.containsAnyUuid(uuids, OPP_PROFILE_UUIDS)) {
             sProfileMap.remove(Profile.OPP);
         }
 
-        if (BluetoothUuid.containsAnyUuid(uuids, HID_PROFILE_UUIDS)) {
-            sProfileMap.remove(Profile.HID);
-        }
-
-        if (BluetoothUuid.containsAnyUuid(uuids, PANU_PROFILE_UUIDS)) {
+        if (!BluetoothUuid.containsAnyUuid(uuids, PANU_PROFILE_UUIDS)) {
             sProfileMap.remove(Profile.PAN);
         }
+
+        // There is no local SDP record for HID and Settings app doesn't control PBAP
     }
 
     private static LinkedList<ServiceListener> mServiceListeners =
@@ -186,21 +189,28 @@
      * NOTE: This list happens to define the connection order. We should put this logic in a more
      * well known place when this method is no longer temporary.
      * @param uuids of the remote device
+     * @param localUuids UUIDs of the local device
      * @param profiles The list of profiles to fill
      */
-    public static void updateProfiles(ParcelUuid[] uuids, List<Profile> profiles) {
+    public static void updateProfiles(ParcelUuid[] uuids, ParcelUuid[] localUuids,
+        List<Profile> profiles) {
         profiles.clear();
 
         if (uuids == null) {
             return;
         }
 
-        if (BluetoothUuid.containsAnyUuid(uuids, HEADSET_PROFILE_UUIDS) &&
-            sProfileMap.containsKey(Profile.HEADSET)) {
-            profiles.add(Profile.HEADSET);
+        if (sProfileMap.containsKey(Profile.HEADSET)) {
+            if ((BluetoothUuid.isUuidPresent(localUuids, BluetoothUuid.HSP_AG) &&
+                BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP)) ||
+                (BluetoothUuid.isUuidPresent(localUuids, BluetoothUuid.Handsfree_AG) &&
+                BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree))) {
+                    profiles.add(Profile.HEADSET);
+            }
         }
 
-        if (BluetoothUuid.containsAnyUuid(uuids, A2DP_PROFILE_UUIDS) &&
+
+        if (BluetoothUuid.containsAnyUuid(uuids, A2DP_SINK_PROFILE_UUIDS) &&
             sProfileMap.containsKey(Profile.A2DP)) {
             profiles.add(Profile.A2DP);
         }