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

Commit 48e3fce5 authored by Charlie Boutier's avatar Charlie Boutier Committed by Gerrit Code Review
Browse files

Merge "PandoraServer: Add WaitDisconnection Hashset"

parents f6ddaf93 fd6495d0
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)
      }