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

Commit ed656413 authored by David Duarte's avatar David Duarte
Browse files

PandoraServer: Format with kotlinlang style

external/ktfmt/ktfmt.py sets the kotlinlang-style and so
Bluetooth does the same

Bug: 288329780
Test: m PandoraServer
Change-Id: If9280a3120853300cedb89e38d4aa54fb84e3158
parent a62dc3ac
Loading
Loading
Loading
Loading
+238 −227
Original line number Diff line number Diff line
@@ -86,9 +86,12 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable {
                    flow
                        .filter { it.getAction() == BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED }
                        .filter { it.getBluetoothDeviceExtra() == device }
            .map { it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR) }
                        .map {
                            it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR)
                        }
                        .filter {
              it == BluetoothProfile.STATE_CONNECTED || it == BluetoothProfile.STATE_DISCONNECTED
                            it == BluetoothProfile.STATE_CONNECTED ||
                                it == BluetoothProfile.STATE_DISCONNECTED
                        }
                        .first()

@@ -97,7 +100,8 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable {
                }
            }

      // TODO: b/234891800, AVDTP start request sometimes never sent if playback starts too early.
            // TODO: b/234891800, AVDTP start request sometimes never sent if playback starts too
            // early.
            delay(2000L)

            val source = Source.newBuilder().setConnection(request.connection).build()
@@ -118,9 +122,12 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable {
                    flow
                        .filter { it.getAction() == BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED }
                        .filter { it.getBluetoothDeviceExtra() == device }
            .map { it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR) }
                        .map {
                            it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR)
                        }
                        .filter {
              it == BluetoothProfile.STATE_CONNECTED || it == BluetoothProfile.STATE_DISCONNECTED
                            it == BluetoothProfile.STATE_CONNECTED ||
                                it == BluetoothProfile.STATE_DISCONNECTED
                        }
                        .first()

@@ -129,7 +136,8 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable {
                }
            }

      // TODO: b/234891800, AVDTP start request sometimes never sent if playback starts too early.
            // TODO: b/234891800, AVDTP start request sometimes never sent if playback starts too
            // early.
            delay(2000L)

            val source = Source.newBuilder().setConnection(request.connection).build()
@@ -164,7 +172,10 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable {
        }
    }

  override fun suspend(request: SuspendRequest, responseObserver: StreamObserver<SuspendResponse>) {
    override fun suspend(
        request: SuspendRequest,
        responseObserver: StreamObserver<SuspendResponse>
    ) {
        grpcUnary<SuspendResponse>(scope, responseObserver) {
            val device = request.source.connection.toBluetoothDevice(bluetoothAdapter)
            Log.i(TAG, "suspend: device=$device")
+77 −67
Original line number Diff line number Diff line
@@ -75,11 +75,16 @@ class A2dpSink(val context: Context) : A2DPImplBase(), Closeable {
            if (bluetoothA2dpSink.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) {
                val state =
                    flow
            .filter { it.getAction() == BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED }
                        .filter {
                            it.getAction() == BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED
                        }
                        .filter { it.getBluetoothDeviceExtra() == device }
            .map { it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR) }
                        .map {
                            it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR)
                        }
                        .filter {
              it == BluetoothProfile.STATE_CONNECTED || it == BluetoothProfile.STATE_DISCONNECTED
                            it == BluetoothProfile.STATE_CONNECTED ||
                                it == BluetoothProfile.STATE_DISCONNECTED
                        }
                        .first()

@@ -107,8 +112,13 @@ class A2dpSink(val context: Context) : A2DPImplBase(), Closeable {
                    .filter { it.getBluetoothDeviceExtra() == device }
                    .map { it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR) }

      bluetoothA2dpSink.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN)
      a2dpConnectionStateChangedFlow.filter { it == BluetoothProfile.STATE_DISCONNECTED }.first()
            bluetoothA2dpSink.setConnectionPolicy(
                device,
                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
            )
            a2dpConnectionStateChangedFlow
                .filter { it == BluetoothProfile.STATE_DISCONNECTED }
                .first()

            CloseResponse.getDefaultInstance()
        }
+180 −170
Original line number Diff line number Diff line
@@ -68,7 +68,8 @@ class AndroidInternal(val context: Context) : AndroidImplBase(), Closeable {
    private val bluetoothAdapter = bluetoothManager.adapter
    private var telephonyManager = context.getSystemService(TelephonyManager::class.java)
    private val DEFAULT_MESSAGE_LEN = 130
  private var device: UiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
    private var device: UiDevice =
        UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

    init {
        createImageFile()
@@ -138,7 +139,10 @@ class AndroidInternal(val context: Context) : AndroidImplBase(), Closeable {
                .wait(Until.findObject(By.text(INCOMING_FILE_TITLE)), INCOMING_FILE_WAIT_TIMEOUT)
                .click()
            device
        .wait(Until.findObject(By.text(INCOMING_FILE_ACCEPT_BTN)), INCOMING_FILE_WAIT_TIMEOUT)
                .wait(
                    Until.findObject(By.text(INCOMING_FILE_ACCEPT_BTN)),
                    INCOMING_FILE_WAIT_TIMEOUT
                )
                .click()
            Empty.getDefaultInstance()
        }
@@ -163,7 +167,9 @@ class AndroidInternal(val context: Context) : AndroidImplBase(), Closeable {
    suspend private fun waitAndSelectBluetoothDevice(name: String) {
        var selectJob =
            scope.async {
        device.wait(Until.findObject(By.textContains(name)), BT_DEVICE_SELECT_WAIT_TIMEOUT).click()
                device
                    .wait(Until.findObject(By.textContains(name)), BT_DEVICE_SELECT_WAIT_TIMEOUT)
                    .click()
            }
        selectJob.await()
    }
@@ -178,13 +184,17 @@ class AndroidInternal(val context: Context) : AndroidImplBase(), Closeable {
                context.packageManager!!
                    .queryIntentActivities(
                        sendingIntent,
            PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
                        PackageManager.ResolveInfoFlags.of(
                            PackageManager.MATCH_DEFAULT_ONLY.toLong()
                        )
                    )
                    .filter { it!!.loadLabel(context.packageManager) == "Bluetooth" }
                    .first()
                    .activityInfo
            sendingIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
      sendingIntent.setComponent(ComponentName(activity.applicationInfo.packageName, activity.name))
            sendingIntent.setComponent(
                ComponentName(activity.applicationInfo.packageName, activity.name)
            )
            sendingIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
            context.startActivity(sendingIntent)
        } catch (e: Exception) {
+189 −172
Original line number Diff line number Diff line
@@ -87,10 +87,14 @@ class Asha(val context: Context) : AshaImplBase(), Closeable {
            val device = request.connection.toBluetoothDevice(bluetoothAdapter)
            Log.d(TAG, "connection address ${device.getAddress()}")

      if (bluetoothHearingAid.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) {
            if (
                bluetoothHearingAid.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED
            ) {
                Log.d(TAG, "wait for bluetoothHearingAid profile connection")
                flow
          .filter { it.getAction() == BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED }
                    .filter {
                        it.getAction() == BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED
                    }
                    .filter { it.getBluetoothDeviceExtra() == device }
                    .map { it.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR) }
                    .filter { it == BluetoothProfile.STATE_CONNECTED }
@@ -109,7 +113,9 @@ class Asha(val context: Context) : AshaImplBase(), Closeable {
            val device = request.connection.toBluetoothDevice(bluetoothAdapter)
            Log.d(TAG, "connection address ${device.getAddress()}")

      if (bluetoothHearingAid.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) {
            if (
                bluetoothHearingAid.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED
            ) {
                throw RuntimeException("Hearing aid device is not connected, cannot start")
            }

@@ -133,7 +139,10 @@ class Asha(val context: Context) : AshaImplBase(), Closeable {
                                    addedDevice.type == AudioDeviceInfo.TYPE_HEARING_AID &&
                                        addedDevice.address.equals(device.getAddress())
                                ) {
                  Log.d(TAG, "TYPE_HEARING_AID added with address: ${addedDevice.address}")
                                    Log.d(
                                        TAG,
                                        "TYPE_HEARING_AID added with address: ${addedDevice.address}"
                                    )
                                    trySendBlocking(null)
                                }
                            }
@@ -168,9 +177,13 @@ class Asha(val context: Context) : AshaImplBase(), Closeable {
                                Log.d(TAG, "Route to TYPE_HEARING_AID")
                                trySendBlocking(null)
                            } else {
                val outputDevices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
                                val outputDevices =
                                    audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
                                for (outputDevice in outputDevices) {
                  Log.d(TAG, "available output device in listener:${outputDevice.type}")
                                    Log.d(
                                        TAG,
                                        "available output device in listener:${outputDevice.type}"
                                    )
                                    if (outputDevice.type == AudioDeviceInfo.TYPE_HEARING_AID) {
                                        val result = router.setPreferredDevice(outputDevice)
                                        Log.d(TAG, "setPreferredDevice result:$result")
@@ -190,7 +203,11 @@ class Asha(val context: Context) : AshaImplBase(), Closeable {
            audioRoutingFlow.first()

            val minVolume = audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC)
      audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, minVolume, AudioManager.FLAG_SHOW_UI)
            audioManager.setStreamVolume(
                AudioManager.STREAM_MUSIC,
                minVolume,
                AudioManager.FLAG_SHOW_UI
            )

            StartResponse.getDefaultInstance()
        }
+374 −348
Original line number Diff line number Diff line
@@ -101,11 +101,20 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
                }
                val valueWrote =
                    gattInstance.writeDescriptorBlocking(descriptor, request.value.toByteArray())
        WriteResponse.newBuilder().setHandle(valueWrote.handle).setStatus(valueWrote.status).build()
                WriteResponse.newBuilder()
                    .setHandle(valueWrote.handle)
                    .setStatus(valueWrote.status)
                    .build()
            } else {
                val valueWrote =
          gattInstance.writeCharacteristicBlocking(characteristic, request.value.toByteArray())
        WriteResponse.newBuilder().setHandle(valueWrote.handle).setStatus(valueWrote.status).build()
                    gattInstance.writeCharacteristicBlocking(
                        characteristic,
                        request.value.toByteArray()
                    )
                WriteResponse.newBuilder()
                    .setHandle(valueWrote.handle)
                    .setStatus(valueWrote.status)
                    .build()
            }
        }
    }
@@ -119,7 +128,8 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
            Log.i(TAG, "discoverServiceByUuid uuid=${request.uuid}")
            // In some cases, GATT starts a discovery immediately after being connected, so
            // we need to wait until the service discovery is finished to be able to discover again.
      // This takes between 20s and 28s, and there is no way to know if the service is busy or not.
            // This takes between 20s and 28s, and there is no way to know if the service is busy or
            // not.
            // Delay was originally 30s, but due to flakyness increased to 32s.
            delay(32000L)
            check(gattInstance.mGatt.discoverServiceByUuid(UUID.fromString(request.uuid)))
@@ -191,7 +201,9 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
            checkNotNull(characteristic) { "Characteristic handle ${request.handle} not found." }
            val readValue = gattInstance.readCharacteristicBlocking(characteristic)
            ReadCharacteristicResponse.newBuilder()
        .setValue(AttValue.newBuilder().setHandle(readValue.handle).setValue(readValue.value))
                .setValue(
                    AttValue.newBuilder().setHandle(readValue.handle).setValue(readValue.value)
                )
                .setStatus(readValue.status)
                .build()
        }
@@ -229,7 +241,9 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
            checkNotNull(descriptor) { "Descriptor handle ${request.handle} not found." }
            val readValue = gattInstance.readDescriptorBlocking(descriptor)
            ReadCharacteristicDescriptorResponse.newBuilder()
        .setValue(AttValue.newBuilder().setHandle(readValue.handle).setValue(readValue.value))
                .setValue(
                    AttValue.newBuilder().setHandle(readValue.handle).setValue(readValue.value)
                )
                .setStatus(readValue.status)
                .build()
        }
@@ -244,15 +258,20 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
            val service =
                BluetoothGattService(UUID.fromString(request.service.uuid), SERVICE_TYPE_PRIMARY)
            for (characteristic_params in request.service.characteristicsList) {
        val characteristic = BluetoothGattCharacteristic(
                val characteristic =
                    BluetoothGattCharacteristic(
                        UUID.fromString(characteristic_params.uuid),
                        characteristic_params.properties,
          characteristic_params.permissions)
                        characteristic_params.permissions
                    )
                for (descriptor_params in characteristic_params.descriptorsList) {
          characteristic.addDescriptor(BluetoothGattDescriptor(
                    characteristic.addDescriptor(
                        BluetoothGattDescriptor(
                            UUID.fromString(descriptor_params.uuid),
                            descriptor_params.properties,
            descriptor_params.permissions))
                            descriptor_params.permissions
                        )
                    )
                }
                service.addCharacteristic(characteristic)
            }
@@ -286,21 +305,25 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
            val gattInstance = GattInstance.get(request.connection.address)
            val descriptor: BluetoothGattDescriptor? =
                getDescriptorWithHandle(request.handle, gattInstance)
      checkNotNull(descriptor) {
        "Found no descriptor with handle ${request.handle}"
      }
            checkNotNull(descriptor) { "Found no descriptor with handle ${request.handle}" }
            var characteristic = descriptor.getCharacteristic()
            gattInstance.mGatt.setCharacteristicNotification(characteristic, true)
            if (request.enableValue == EnableValue.ENABLE_INDICATION_VALUE) {
                val valueWrote =
          gattInstance.writeDescriptorBlocking(descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)
                    gattInstance.writeDescriptorBlocking(
                        descriptor,
                        BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
                    )
                SetCharacteristicNotificationFromHandleResponse.newBuilder()
                    .setHandle(valueWrote.handle)
                    .setStatus(valueWrote.status)
                    .build()
            } else {
                val valueWrote =
          gattInstance.writeDescriptorBlocking(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
                    gattInstance.writeDescriptorBlocking(
                        descriptor,
                        BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                    )
                SetCharacteristicNotificationFromHandleResponse.newBuilder()
                    .setHandle(valueWrote.handle)
                    .setStatus(valueWrote.status)
@@ -317,11 +340,10 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
            val gattInstance = GattInstance.get(request.connection.address)
            val descriptor: BluetoothGattDescriptor? =
                getDescriptorWithHandle(request.handle, gattInstance)
      checkNotNull(descriptor) {
        "Found no descriptor with handle ${request.handle}"
      }
            checkNotNull(descriptor) { "Found no descriptor with handle ${request.handle}" }
            var characteristic = descriptor.getCharacteristic()
      val characteristicNotificationReceived = gattInstance.waitForOnCharacteristicChanged(characteristic)
            val characteristicNotificationReceived =
                gattInstance.waitForOnCharacteristicChanged(characteristic)
            WaitCharacteristicNotificationResponse.newBuilder()
                .setCharacteristicNotificationReceived(characteristicNotificationReceived)
                .build()
@@ -380,7 +402,9 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
                    .setHandle(service.getInstanceId())
                    .setType(service.getType())
                    .setUuid(service.getUuid().toString().uppercase())
          .addAllIncludedServices(generateServicesList(service.getIncludedServices(), dpth + 1))
                    .addAllIncludedServices(
                        generateServicesList(service.getIncludedServices(), dpth + 1)
                    )
                    .addAllCharacteristics(generateCharacteristicsList(service.characteristics))
            newServicesList.add(serviceBuilder.build())
        }
@@ -429,7 +453,9 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable {
        for (readValue in readValuesList) {
            val readValueBuilder =
                ReadCharacteristicResponse.newBuilder()
          .setValue(AttValue.newBuilder().setHandle(readValue.handle).setValue(readValue.value))
                    .setValue(
                        AttValue.newBuilder().setHandle(readValue.handle).setValue(readValue.value)
                    )
                    .setStatus(readValue.status)
            newReadValuesList.add(readValueBuilder.build())
        }
Loading