[BT] Correct the filter when addCachedDevices

In Change Ia9750adb6b4c1424d084381e9d7c2ca8e7912391, addCachedDevices()
becomes async, but the filter is set outside of addCachedDevices(),
which makes the filter not apply to addCachedDevices().

Direct pass the filter to addCachedDevices() to fix this issue.

Also migrate the test to Kotlin so we can test coroutine.

Fix: 289876965
Test: manual - check BT pairing page
Test: m RunSettingsRoboTests
Change-Id: I95b16840881747ec9f69e5cd778e456bcc8a7626
This commit is contained in:
Chaohui Wang
2023-07-11 17:49:18 +08:00
parent 9416328ab7
commit 8dd32ab07d
4 changed files with 272 additions and 252 deletions

View File

@@ -101,10 +101,8 @@ public class BluetoothPairingDetail extends BluetoothDevicePairingDetailBase imp
if (bluetoothState == BluetoothAdapter.STATE_ON) {
if (mInitialScanStarted) {
// Don't show bonded devices when screen turned back on
setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
addCachedDevices();
addCachedDevices(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
}
setFilter(BluetoothDeviceFilter.ALL_FILTER);
updateFooterPreference(mFooterPreference);
mAlwaysDiscoverable.start();
}

View File

@@ -28,7 +28,6 @@ import android.text.BidiFormatter
import android.util.Log
import android.view.View
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
@@ -41,6 +40,7 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager
import com.android.settingslib.bluetooth.LocalBluetoothManager
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -85,11 +85,10 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
@JvmField
val mSelectedList: MutableList<BluetoothDevice> = ArrayList()
private var showDevicesWithoutNames = false
@VisibleForTesting
var lifecycleScope: CoroutineScope? = null
protected fun setFilter(filter: BluetoothDeviceFilter.Filter?) {
this.filter = filter
}
private var showDevicesWithoutNames = false
protected fun setFilter(filterType: Int) {
filter = BluetoothDeviceFilter.getFilter(filterType)
@@ -125,8 +124,6 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
/** find and update preference that already existed in preference screen */
protected abstract fun initPreferencesFromPreferenceScreen()
private var lifecycleScope: LifecycleCoroutineScope? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
lifecycleScope = viewLifecycleOwner.lifecycleScope
@@ -154,13 +151,15 @@ abstract class DeviceListPreferenceFragment(restrictedKey: String?) :
mDeviceListGroup!!.removeAll()
}
fun addCachedDevices() {
@JvmOverloads
fun addCachedDevices(filterForCachedDevices: BluetoothDeviceFilter.Filter? = null) {
lifecycleScope?.launch {
withContext(Dispatchers.Default) {
val cachedDevices = mCachedDeviceManager!!.cachedDevicesCopy
for (cachedDevice in cachedDevices) {
onDeviceAdded(cachedDevice)
}
mCachedDeviceManager!!.cachedDevicesCopy
.filter {
filterForCachedDevices == null || filterForCachedDevices.matches(it.device)
}
.forEach(::onDeviceAdded)
}
}
}