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

Commit 6b016f2e authored by Bidhya Sharma's avatar Bidhya Sharma Committed by Automerger Merge Worker
Browse files

Merge "Remove duplicate device record" am: 0388531f am: 94549b0f

parents a56f5726 94549b0f
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -676,6 +676,7 @@ class AdapterProperties {
                if (!mBondedDevices.contains(device)) {
                    debugLog("Adding bonded device:" + device);
                    mBondedDevices.add(device);
                    cleanupPrevBondRecordsFor(device);
                }
            } else if (state == BluetoothDevice.BOND_NONE) {
                // remove device from list
@@ -691,6 +692,35 @@ class AdapterProperties {
        }
    }

    void cleanupPrevBondRecordsFor(BluetoothDevice currentDevice) {
        String currentAddress = currentDevice.getAddress();
        String currentIdentityAddress = mService.getIdentityAddress(currentAddress);
        debugLog("cleanupPrevBondRecordsFor: " + currentDevice);
        if (currentIdentityAddress == null) {
            return;
        }

        for (BluetoothDevice device : mBondedDevices) {
            String address = device.getAddress();
            String identityAddress = mService.getIdentityAddress(address);
            if (currentIdentityAddress.equals(identityAddress) && !currentAddress.equals(address)) {
                if (mService.removeBondNative(Utils.getBytesFromAddress(device.getAddress()))) {
                    mBondedDevices.remove(device);
                    infoLog("Removing old bond record: "
                                    + device
                                    + " for current device: "
                                    + currentDevice);
                } else {
                    Log.e(TAG, "Unexpected error while removing old bond record:"
                                    + device
                                    + " for current device: "
                                    + currentDevice);
                }
                break;
            }
        }
    }

    int getDiscoverableTimeout() {
        return mDiscoverableTimeout;
    }
+111 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.bluetooth.btservice;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.HandlerThread;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.Utils;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class AdapterPropertiesTest {
    private static final byte[] TEST_BT_ADDR_BYTES = {00, 11, 22, 33, 44, 55};
    private static final byte[] TEST_BT_ADDR_BYTES_2 = {00, 11, 22, 33, 44, 66};

    private BluetoothManager mBluetoothManager;
    private AdapterProperties mAdapterProperties;
    private RemoteDevices mRemoteDevices;
    private HandlerThread mHandlerThread;
    private Context mTargetContext;

    @Mock private AdapterService mAdapterService;

    @Before
    public void setUp() throws Exception {
        mTargetContext = InstrumentationRegistry.getTargetContext();

        MockitoAnnotations.initMocks(this);
        mHandlerThread = new HandlerThread("RemoteDevicesTestHandlerThread");
        mHandlerThread.start();

        mBluetoothManager = mTargetContext.getSystemService(BluetoothManager.class);
        when(mAdapterService.getSystemService(Context.BLUETOOTH_SERVICE))
                .thenReturn(mBluetoothManager);
        when(mAdapterService.getSystemServiceName(BluetoothManager.class))
                .thenReturn(Context.BLUETOOTH_SERVICE);

        when(mAdapterService.getIdentityAddress(Utils.getAddressStringFromByte(TEST_BT_ADDR_BYTES)))
                .thenReturn(Utils.getAddressStringFromByte(TEST_BT_ADDR_BYTES));
        when(mAdapterService.getIdentityAddress(
                        Utils.getAddressStringFromByte(TEST_BT_ADDR_BYTES_2)))
                .thenReturn(Utils.getAddressStringFromByte(TEST_BT_ADDR_BYTES));
        when(mAdapterService.removeBondNative(any(byte[].class))).thenReturn(true);

        mRemoteDevices = new RemoteDevices(mAdapterService, mHandlerThread.getLooper());
        verify(mAdapterService, times(1)).getSystemService(Context.BLUETOOTH_SERVICE);
        verify(mAdapterService, times(1)).getSystemService(BluetoothManager.class);

        mRemoteDevices.reset();

        doReturn(mHandlerThread.getLooper()).when(mAdapterService).getMainLooper();
        doReturn(true).when(mAdapterService).isMock();
        when(mAdapterService.getResources())
                .thenReturn(InstrumentationRegistry.getTargetContext().getResources());

        // Must be called to initialize services
        mAdapterProperties = new AdapterProperties(mAdapterService);
        mAdapterProperties.init(mRemoteDevices);
    }

    @Test
    public void testCleanupPrevBondRecordsFor() {
        mRemoteDevices.reset();
        mRemoteDevices.addDeviceProperties(TEST_BT_ADDR_BYTES);
        mRemoteDevices.addDeviceProperties(TEST_BT_ADDR_BYTES_2);
        BluetoothDevice device1, device2;
        device1 = mRemoteDevices.getDevice(TEST_BT_ADDR_BYTES);
        device2 = mRemoteDevices.getDevice(TEST_BT_ADDR_BYTES_2);

        // Bond record for device1 should be deleted when pairing with device2
        // as they are same device (have same identity address)
        mAdapterProperties.onBondStateChanged(device1, BluetoothDevice.BOND_BONDED);
        mAdapterProperties.onBondStateChanged(device2, BluetoothDevice.BOND_BONDED);
        assertThat(mAdapterProperties.getBondedDevices().length).isEqualTo(1);
        assertThat(mAdapterProperties.getBondedDevices()[0].getAddress())
                .isEqualTo(Utils.getAddressStringFromByte(TEST_BT_ADDR_BYTES_2));
    }
}