Merge "Refactor for better readability of using different Bluetooth scanning" into main
diff --git a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.kt b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.kt
index f18ae46..77a80b8 100644
--- a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.kt
+++ b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.kt
@@ -55,7 +55,12 @@
 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 @@
     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 @@
      * @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 @@
 
     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 @@
 
     @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()
         }
     }