Merge "Update Bluetooth Profile defaults only while bonding." into rvc-dev
diff --git a/service/src/com/android/car/BluetoothProfileDeviceManager.java b/service/src/com/android/car/BluetoothProfileDeviceManager.java
index cfd45e5..2fd17d0 100644
--- a/service/src/com/android/car/BluetoothProfileDeviceManager.java
+++ b/service/src/com/android/car/BluetoothProfileDeviceManager.java
@@ -51,6 +51,7 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
@@ -65,6 +66,7 @@
     private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
     private final Context mContext;
     private final int mUserId;
+    private Set<String> mBondingDevices = new HashSet<>();
 
     private static final String SETTINGS_DELIMITER = ",";
 
@@ -217,16 +219,20 @@
         logd("Bond state has changed [device: " + device + ", state: "
                 + Utils.getBondStateName(state) + "]");
         if (state == BluetoothDevice.BOND_NONE) {
+            mBondingDevices.remove(device.getAddress());
             // Note: We have seen cases of unbonding events being sent without actually
             // unbonding the device.
             removeDevice(device);
+        } else if (state == BluetoothDevice.BOND_BONDING) {
+            mBondingDevices.add(device.getAddress());
         } else if (state == BluetoothDevice.BOND_BONDED) {
             addBondedDeviceIfSupported(device);
+            mBondingDevices.remove(device.getAddress());
         }
     }
 
     /**
-     * Handles an incoming device UUID set update event.
+     * Handles an incoming device UUID set update event for bonding devices.
      *
      * On BluetoothDevice.ACTION_UUID:
      *    If the UUID is one this profile cares about, set the profile priority for the device that
@@ -238,6 +244,7 @@
      */
     private void handleDeviceUuidEvent(BluetoothDevice device, Parcelable[] uuids) {
         logd("UUIDs found, device: " + device);
+        if (!mBondingDevices.remove(device.getAddress())) return;
         if (uuids != null) {
             ParcelUuid[] uuidsToSend = new ParcelUuid[uuids.length];
             for (int i = 0; i < uuidsToSend.length; i++) {
diff --git a/tests/carservice_unit_test/src/com/android/car/BluetoothProfileDeviceManagerTest.java b/tests/carservice_unit_test/src/com/android/car/BluetoothProfileDeviceManagerTest.java
index 0cc684f..be8f8f3 100644
--- a/tests/carservice_unit_test/src/com/android/car/BluetoothProfileDeviceManagerTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/BluetoothProfileDeviceManagerTest.java
@@ -1176,6 +1176,30 @@
      * - The device manager is initialized, there are no devices in the list.
      *
      * Actions:
+     * - A Bonding state change with state == BOND_BONDING is received
+     * - A Uuid set is received for a device that has PRIORITY_UNDEFINED
+     *
+     * Outcome:
+     * - The device has its priority updated to PRIORITY_ON.
+     */
+    @Test
+    public void testReceiveUuidDevicePriorityUndefinedBonding_setPriorityOn() throws Exception {
+        setPreconditionsAndStart(ADAPTER_STATE_ANY, EMPTY_SETTINGS_STRING, EMPTY_DEVICE_LIST);
+        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(SINGLE_DEVICE_LIST.get(0));
+        mockDevicePriority(device, BluetoothProfile.PRIORITY_UNDEFINED);
+        sendBondStateChanged(device, BluetoothDevice.BOND_BONDING);
+        sendDeviceUuids(device, mUuids);
+        assertDeviceList(EMPTY_DEVICE_LIST);
+        verify(mMockProxies, times(1)).setProfilePriority(mProfileId, device,
+                BluetoothProfile.PRIORITY_ON);
+    }
+
+        /**
+     * Preconditions:
+     * - The device manager is initialized, there are no devices in the list.
+     * - The designated device is not in a bonding state.
+     *
+     * Actions:
      * - A Uuid set is received for a device that has PRIORITY_UNDEFINED
      *
      * Outcome:
@@ -1188,7 +1212,7 @@
         mockDevicePriority(device, BluetoothProfile.PRIORITY_UNDEFINED);
         sendDeviceUuids(device, mUuids);
         assertDeviceList(EMPTY_DEVICE_LIST);
-        verify(mMockProxies, times(1)).setProfilePriority(mProfileId, device,
+        verify(mMockProxies, times(0)).setProfilePriority(mProfileId, device,
                 BluetoothProfile.PRIORITY_ON);
     }