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

Commit fd6495d0 authored by Charlie Boutier's avatar Charlie Boutier
Browse files

PandoraServer: Add WaitDisconnection Hashset

Test: atest pts-bot
Bug: 277190339
Change-Id: I1bd03f9cf03cf23592af254a0fcb658f741dcc52
parent b315d83f
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -171,6 +171,7 @@ class Host(

      initiatedConnection.clear()
      waitedAclConnection.clear()
      waitedAclDisconnection.clear()

      bluetoothAdapter.clearBluetooth()

@@ -190,6 +191,8 @@ class Host(
      Log.i(TAG, "reset")
      initiatedConnection.clear()
      waitedAclConnection.clear()
      waitedAclDisconnection.clear()

      rebootBluetooth()

      Empty.getDefaultInstance()
@@ -311,12 +314,18 @@ class Host(
      if (!bluetoothAdapter.isEnabled) {
        throw RuntimeException("Bluetooth is not enabled, cannot waitDisconnection")
      }
      if (bluetoothDevice.bondState != BluetoothDevice.BOND_NONE) {
      if (
        bluetoothDevice.bondState != BluetoothDevice.BOND_NONE &&
          !waitedAclDisconnection.contains(bluetoothDevice)
      ) {
        flow
          .filter { it.action == BluetoothDevice.ACTION_ACL_DISCONNECTED }
          .filter { it.getBluetoothDeviceExtra() == bluetoothDevice }
          .first()
      }

      waitedAclDisconnection.add(bluetoothDevice)

      Empty.getDefaultInstance()
    }
  }
+5 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ private val alphanumeric = ('A'..'Z') + ('a'..'z') + ('0'..'9')

val initiatedConnection = HashSet<BluetoothDevice>()
val waitedAclConnection = HashSet<BluetoothDevice>()
val waitedAclDisconnection = HashSet<BluetoothDevice>()

fun shell(cmd: String): String {
  val fd = InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(cmd)
@@ -85,9 +86,13 @@ fun intentFlow(context: Context, intentFilter: IntentFilter) = callbackFlow {
  val broadcastReceiver: BroadcastReceiver =
    object : BroadcastReceiver() {
      override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == BluetoothDevice.ACTION_ACL_CONNECTED) {
          waitedAclDisconnection.remove(intent.getBluetoothDeviceExtra())
        }
        if (intent.action == BluetoothDevice.ACTION_ACL_DISCONNECTED) {
          initiatedConnection.remove(intent.getBluetoothDeviceExtra())
          waitedAclConnection.remove(intent.getBluetoothDeviceExtra())
          waitedAclDisconnection.add(intent.getBluetoothDeviceExtra())
        }
        trySendBlocking(intent)
      }