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

Commit 1ac510e4 authored by Angela Wang's avatar Angela Wang Committed by Android (Google) Code Review
Browse files

Merge "Refactor for better readability of using different Bluetooth scanning" into main

parents 982a13a6 14b419b1
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()
        }
    }