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

Commit 14b419b1 authored by Angela Wang's avatar Angela Wang
Browse files

Refactor for better readability of using different Bluetooth scanning

We rely on using `filter` is null or not to determine which type of
Bluetooth scanning method is going to be used (Classic of LE). Add a
variable to clearly indicate current scan type for better readibility.

Bug:289189853
Test: make RunSettingsRoboTests ROBOTEST_FILTER=DeviceListPreferenceFragmentTest
Change-Id: I85a26d61410367bd617a0194ba3bfe7ed1b03356
parent 2281a640
Loading
Loading
Loading
Loading
+23 −14
Original line number Diff line number Diff line
@@ -55,7 +55,12 @@ import kotlinx.coroutines.withContext
abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
    RestrictedDashboardFragment(restrictedKey), BluetoothCallback {

    private var filter: BluetoothDeviceFilter.Filter? = BluetoothDeviceFilter.ALL_FILTER
    enum class ScanType {
        CLASSIC, LE
    }

    private var scanType = ScanType.CLASSIC
    private var filter: BluetoothDeviceFilter.Filter = BluetoothDeviceFilter.ALL_FILTER
    private var leScanFilters: List<ScanFilter>? = null

    @JvmField
@@ -91,7 +96,8 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
    private var showDevicesWithoutNames = false

    protected fun setFilter(filterType: Int) {
        filter = BluetoothDeviceFilter.getFilter(filterType)
        this.scanType = ScanType.CLASSIC
        this.filter = BluetoothDeviceFilter.getFilter(filterType)
    }

    /**
@@ -101,7 +107,7 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
     * @param leScanFilters list of settings to filter scan result
     */
    fun setFilter(leScanFilters: List<ScanFilter>?) {
        filter = null
        this.scanType = ScanType.LE
        this.leScanFilters = leScanFilters
    }

@@ -191,11 +197,14 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :

    private suspend fun addDevice(cachedDevice: CachedBluetoothDevice) =
        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
            if (mBluetoothAdapter!!.state != BluetoothAdapter.STATE_ON) {
                // Prevent updates while the list shows one of the state messages
            if (mBluetoothAdapter!!.state == BluetoothAdapter.STATE_ON && filterMatched) {
                return@withContext
            }
            // LE filters was already applied at scan time. We just need to check if the classic
            // filter matches
            if (scanType == ScanType.LE
                || (scanType == ScanType.CLASSIC && filter.matches(cachedDevice.device) == true)) {
                createDevicePreference(cachedDevice)
            }
        }
@@ -277,19 +286,19 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :

    @VisibleForTesting
    open fun startScanning() {
        if (filter != null) {
            startClassicScanning()
        } else if (leScanFilters != null) {
        if (scanType == ScanType.LE) {
            startLeScanning()
        } else {
            startClassicScanning()
        }
    }

    @VisibleForTesting
    open fun stopScanning() {
        if (filter != null) {
            stopClassicScanning()
        } else if (leScanFilters != null) {
        if (scanType == ScanType.LE) {
            stopLeScanning()
        } else {
            stopClassicScanning()
        }
    }