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
This commit is contained in:
Angela Wang
2023-06-28 07:14:52 +00:00
parent 2281a64085
commit 14b419b100

View File

@@ -55,7 +55,12 @@ import kotlinx.coroutines.withContext
abstract class DeviceListPreferenceFragment(restrictedKey: String?) : abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
RestrictedDashboardFragment(restrictedKey), BluetoothCallback { 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 private var leScanFilters: List<ScanFilter>? = null
@JvmField @JvmField
@@ -91,7 +96,8 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
private var showDevicesWithoutNames = false private var showDevicesWithoutNames = false
protected fun setFilter(filterType: Int) { 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 * @param leScanFilters list of settings to filter scan result
*/ */
fun setFilter(leScanFilters: List<ScanFilter>?) { fun setFilter(leScanFilters: List<ScanFilter>?) {
filter = null this.scanType = ScanType.LE
this.leScanFilters = leScanFilters this.leScanFilters = leScanFilters
} }
@@ -191,11 +197,14 @@ 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 if (mBluetoothAdapter!!.state != BluetoothAdapter.STATE_ON) {
// 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 && 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) createDevicePreference(cachedDevice)
} }
} }
@@ -277,19 +286,19 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
@VisibleForTesting @VisibleForTesting
open fun startScanning() { open fun startScanning() {
if (filter != null) { if (scanType == ScanType.LE) {
startClassicScanning()
} else if (leScanFilters != null) {
startLeScanning() startLeScanning()
} else {
startClassicScanning()
} }
} }
@VisibleForTesting @VisibleForTesting
open fun stopScanning() { open fun stopScanning() {
if (filter != null) { if (scanType == ScanType.LE) {
stopClassicScanning()
} else if (leScanFilters != null) {
stopLeScanning() stopLeScanning()
} else {
stopClassicScanning()
} }
} }