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

This commit is contained in:
Angela Wang
2023-07-19 23:58:54 +00:00
committed by Android (Google) Code Review

View File

@@ -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
// Prevent updates while the list shows one of the state messages
if (mBluetoothAdapter!!.state == BluetoothAdapter.STATE_ON && filterMatched) {
if (mBluetoothAdapter!!.state != BluetoothAdapter.STATE_ON) {
// Prevent updates while the list shows one of the state messages
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()
}
}