Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3ffc2dae authored by SongFerng Wang's avatar SongFerng Wang Committed by Automerger Merge Worker
Browse files

Merge "[LE unicast] member device add PhonebookAccessPermission" into tm-qpr-dev am: 9db30ad5

parents 8fe25ff9 9db30ad5
Loading
Loading
Loading
Loading
+37 −3
Original line number Diff line number Diff line
@@ -356,7 +356,7 @@ public class CachedBluetoothDeviceManager {
     * @return {@code true}, if the device should pair automatically; Otherwise, return
     * {@code false}.
     */
    public synchronized boolean shouldPairByCsip(BluetoothDevice device, int groupId) {
    private synchronized boolean shouldPairByCsip(BluetoothDevice device, int groupId) {
        boolean isOngoingSetMemberPair = mOngoingSetMemberPair != null;
        int bondState = device.getBondState();
        if (isOngoingSetMemberPair || bondState != BluetoothDevice.BOND_NONE
@@ -365,10 +365,44 @@ public class CachedBluetoothDeviceManager {
                    + " , device.getBondState: " + bondState);
            return false;
        }
        return true;
    }

        Log.d(TAG, "Bond " + device.getName() + " by CSIP");
    /**
     * Called when we found a set member of a group. The function will check the {@code groupId} if
     * it exists and the bond state of the device is BOND_NONE, and if there isn't any ongoing pair
     * , and then pair the device automatically.
     *
     * @param device The found device
     * @param groupId The group id of the found device
     */
    public synchronized void pairDeviceByCsip(BluetoothDevice device, int groupId) {
        if (!shouldPairByCsip(device, groupId)) {
            return;
        }
        Log.d(TAG, "Bond " + device.getAnonymizedAddress() + " by CSIP");
        mOngoingSetMemberPair = device;
        return true;
        syncConfigFromMainDevice(device, groupId);
        device.createBond(BluetoothDevice.TRANSPORT_LE);
    }

    private void syncConfigFromMainDevice(BluetoothDevice device, int groupId) {
        if (!isOngoingPairByCsip(device)) {
            return;
        }
        CachedBluetoothDevice memberDevice = findDevice(device);
        CachedBluetoothDevice mainDevice = mCsipDeviceManager.findMainDevice(memberDevice);
        if (mainDevice == null) {
            mainDevice = mCsipDeviceManager.getCachedDevice(groupId);
        }

        if (mainDevice == null || mainDevice.equals(memberDevice)) {
            Log.d(TAG, "no mainDevice");
            return;
        }

        // The memberDevice set PhonebookAccessPermission
        device.setPhonebookAccessPermission(mainDevice.getDevice().getPhonebookAccessPermission());
    }

    /**
+8 −1
Original line number Diff line number Diff line
@@ -101,7 +101,14 @@ public class CsipDeviceManager {
        return groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
    }

    private CachedBluetoothDevice getCachedDevice(int groupId) {
    /**
     * To find the device with {@code groupId}.
     *
     * @param groupId The group id
     * @return if we could find a device with this {@code groupId} return this device. Otherwise,
     * return null.
     */
    public CachedBluetoothDevice getCachedDevice(int groupId) {
        log("getCachedDevice: groupId: " + groupId);
        for (int i = mCachedDevices.size() - 1; i >= 0; i--) {
            CachedBluetoothDevice cachedDevice = mCachedDevices.get(i);
+20 −0
Original line number Diff line number Diff line
@@ -582,4 +582,24 @@ public class CachedBluetoothDeviceManagerTest {
        assertThat(mCachedDeviceManager.isSubDevice(mDevice2)).isTrue();
        assertThat(mCachedDeviceManager.isSubDevice(mDevice3)).isFalse();
    }

    @Test
    public void pairDeviceByCsip_device2AndCapGroup1_device2StartsPairing() {
        doReturn(CAP_GROUP1).when(mCsipSetCoordinatorProfile).getGroupUuidMapByDevice(mDevice1);
        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice1.getPhonebookAccessPermission()).thenReturn(BluetoothDevice.ACCESS_ALLOWED);
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
        assertThat(cachedDevice1).isNotNull();
        when(mDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
        assertThat(cachedDevice2).isNotNull();

        int groupId = CAP_GROUP1.keySet().stream().findFirst().orElse(
                BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
        assertThat(groupId).isNotEqualTo(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
        mCachedDeviceManager.pairDeviceByCsip(mDevice2, groupId);

        verify(mDevice2).setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
        verify(mDevice2).createBond(BluetoothDevice.TRANSPORT_LE);
    }
}