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

Commit 653ff8a4 authored by Angela Wang's avatar Angela Wang
Browse files

Show connecting hearing devices in A11y hearing devices page

We only show connected hearing devices in the hearing devices page now.
When user pairing a device from pairing page and back to the hearing
devices page after the device is bonded, it's confusing no device shown
in the list because the device is still connecting to profiles. We
should show the connecting device to avoid confusion.

Bug: 283268686
Test: make RunSettingsRoboTests ROBOTEST_FILTER=DeviceListPreferenceFragmentTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=AvailableHearingDeviceUpdaterTest
Change-Id: Id3b29c12b80c282736a3e6ca73bcf317e0652b89
Merged-In: Id3b29c12b80c282736a3e6ca73bcf317e0652b89
parent bdd072d1
Loading
Loading
Loading
Loading
+3 −6
Original line number Original line Diff line number Diff line
@@ -16,7 +16,6 @@


package com.android.settings.accessibility;
package com.android.settings.accessibility;


import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Context;


import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
@@ -37,11 +36,9 @@ public class AvailableHearingDeviceUpdater extends AvailableMediaBluetoothDevice


    @Override
    @Override
    public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
    public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
        final BluetoothDevice device = cachedDevice.getDevice();
        return cachedDevice.isHearingAidDevice()
        final boolean isConnectedHearingAidDevice = (cachedDevice.isConnectedHearingAidDevice()
                && isDeviceConnected(cachedDevice)
                && (device.getBondState() == BluetoothDevice.BOND_BONDED));
                && isDeviceInCachedDevicesList(cachedDevice);

        return isConnectedHearingAidDevice && isDeviceInCachedDevicesList(cachedDevice);
    }
    }


    @Override
    @Override
+24 −15
Original line number Original line Diff line number Diff line
@@ -191,10 +191,11 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :


    private suspend fun addDevice(cachedDevice: CachedBluetoothDevice) =
    private suspend fun addDevice(cachedDevice: CachedBluetoothDevice) =
        withContext(Dispatchers.Default) {
        withContext(Dispatchers.Default) {
            // TODO(b/289189853): Replace checking if `filter` is null or not to decide which type
            // of Bluetooth scanning method will be used
            val filterMatched = filter == null || filter!!.matches(cachedDevice.device) == true
            // Prevent updates while the list shows one of the state messages
            // Prevent updates while the list shows one of the state messages
            if (mBluetoothAdapter!!.state == BluetoothAdapter.STATE_ON &&
            if (mBluetoothAdapter!!.state == BluetoothAdapter.STATE_ON && filterMatched) {
                filter?.matches(cachedDevice.device) == true
            ) {
                createDevicePreference(cachedDevice)
                createDevicePreference(cachedDevice)
            }
            }
        }
        }
@@ -304,17 +305,14 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
        }
        }
    }
    }


    private val scanCallback = object : ScanCallback() {
    private val leScanCallback = object : ScanCallback() {
        override fun onScanResult(callbackType: Int, result: ScanResult) {
        override fun onScanResult(callbackType: Int, result: ScanResult) {
            lifecycleScope?.launch {
            handleLeScanResult(result)
                withContext(Dispatchers.Default) {
                    if (mBluetoothAdapter!!.state == BluetoothAdapter.STATE_ON) {
                        val device = result.device
                        val cachedDevice = mCachedDeviceManager!!.findDevice(device)
                            ?: mCachedDeviceManager!!.addDevice(device)
                        createDevicePreference(cachedDevice)
                    }
        }
        }

        override fun onBatchScanResults(results: MutableList<ScanResult>?) {
            for (result in results.orEmpty()) {
                handleLeScanResult(result)
            }
            }
        }
        }


@@ -328,12 +326,23 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
        val settings = ScanSettings.Builder()
        val settings = ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .build()
            .build()
        scanner.startScan(leScanFilters, settings, scanCallback)
        scanner.startScan(leScanFilters, settings, leScanCallback)
    }
    }


    private fun stopLeScanning() {
    private fun stopLeScanning() {
        val scanner = mBluetoothAdapter!!.bluetoothLeScanner
        val scanner = mBluetoothAdapter!!.bluetoothLeScanner
        scanner?.stopScan(scanCallback)
        scanner?.stopScan(leScanCallback)
    }

    private fun handleLeScanResult(result: ScanResult) {
        lifecycleScope?.launch {
            withContext(Dispatchers.Default) {
                val device = result.device
                val cachedDevice = mCachedDeviceManager!!.findDevice(device)
                    ?: mCachedDeviceManager!!.addDevice(device, leScanFilters)
                addDevice(cachedDevice)
            }
        }
    }
    }


    companion object {
    companion object {
+10 −8
Original line number Original line Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.settings.accessibility;


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


import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
@@ -80,8 +79,9 @@ public class AvailableHearingDeviceUpdaterTest {
    @Test
    @Test
    public void isFilterMatch_connectedHearingDevice_returnTrue() {
    public void isFilterMatch_connectedHearingDevice_returnTrue() {
        CachedBluetoothDevice connectedHearingDevice = mCachedBluetoothDevice;
        CachedBluetoothDevice connectedHearingDevice = mCachedBluetoothDevice;
        when(connectedHearingDevice.isConnectedHearingAidDevice()).thenReturn(true);
        when(connectedHearingDevice.isHearingAidDevice()).thenReturn(true);
        doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
        when(mBluetoothDevice.isConnected()).thenReturn(true);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
                new ArrayList<>(List.of(connectedHearingDevice)));
                new ArrayList<>(List.of(connectedHearingDevice)));


@@ -91,8 +91,9 @@ public class AvailableHearingDeviceUpdaterTest {
    @Test
    @Test
    public void isFilterMatch_nonConnectedHearingDevice_returnFalse() {
    public void isFilterMatch_nonConnectedHearingDevice_returnFalse() {
        CachedBluetoothDevice nonConnectedHearingDevice = mCachedBluetoothDevice;
        CachedBluetoothDevice nonConnectedHearingDevice = mCachedBluetoothDevice;
        when(nonConnectedHearingDevice.isConnectedHearingAidDevice()).thenReturn(false);
        when(nonConnectedHearingDevice.isHearingAidDevice()).thenReturn(true);
        doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
        when(mBluetoothDevice.isConnected()).thenReturn(false);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
                new ArrayList<>(List.of(nonConnectedHearingDevice)));
                new ArrayList<>(List.of(nonConnectedHearingDevice)));


@@ -103,7 +104,8 @@ public class AvailableHearingDeviceUpdaterTest {
    public void isFilterMatch_connectedBondingHearingDevice_returnFalse() {
    public void isFilterMatch_connectedBondingHearingDevice_returnFalse() {
        CachedBluetoothDevice connectedBondingHearingDevice = mCachedBluetoothDevice;
        CachedBluetoothDevice connectedBondingHearingDevice = mCachedBluetoothDevice;
        when(connectedBondingHearingDevice.isHearingAidDevice()).thenReturn(true);
        when(connectedBondingHearingDevice.isHearingAidDevice()).thenReturn(true);
        doReturn(BluetoothDevice.BOND_BONDING).when(mBluetoothDevice).getBondState();
        when(mBluetoothDevice.isConnected()).thenReturn(true);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
                new ArrayList<>(List.of(connectedBondingHearingDevice)));
                new ArrayList<>(List.of(connectedBondingHearingDevice)));


@@ -114,8 +116,8 @@ public class AvailableHearingDeviceUpdaterTest {
    public void isFilterMatch_hearingDeviceNotInCachedDevicesList_returnFalse() {
    public void isFilterMatch_hearingDeviceNotInCachedDevicesList_returnFalse() {
        CachedBluetoothDevice notInCachedDevicesListDevice = mCachedBluetoothDevice;
        CachedBluetoothDevice notInCachedDevicesListDevice = mCachedBluetoothDevice;
        when(notInCachedDevicesListDevice.isHearingAidDevice()).thenReturn(true);
        when(notInCachedDevicesListDevice.isHearingAidDevice()).thenReturn(true);
        doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
        when(mBluetoothDevice.isConnected()).thenReturn(true);
        doReturn(false).when(mBluetoothDevice).isConnected();
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(new ArrayList<>());
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(new ArrayList<>());


        assertThat(mUpdater.isFilterMatched(notInCachedDevicesListDevice)).isEqualTo(false);
        assertThat(mUpdater.isFilterMatched(notInCachedDevicesListDevice)).isEqualTo(false);