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

Commit a7a88aa9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Cts utils: stop depending on T+ api" into main

parents 5b5d86fc 1d2b5b67
Loading
Loading
Loading
Loading
+20 −6
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.bluetooth.BluetoothAdapter.ACTION_BLE_STATE_CHANGED
import android.bluetooth.BluetoothAdapter.STATE_BLE_ON
import android.bluetooth.BluetoothAdapter.STATE_OFF
import android.bluetooth.BluetoothAdapter.STATE_ON
import android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF
import android.bluetooth.BluetoothAdapter.STATE_TURNING_ON
import android.bluetooth.BluetoothManager
import android.bluetooth.test_utils.Permissions.withPermissions
import android.content.BroadcastReceiver
@@ -132,6 +134,8 @@ object BlockingBluetoothAdapter {

private class AdapterStateListener(context: Context, private val adapter: BluetoothAdapter) {
    private val STATE_UNKNOWN = -42
    private val STATE_BLE_TURNING_ON = 14 // BluetoothAdapter.STATE_BLE_TURNING_ON
    private val STATE_BLE_TURNING_OFF = 16 // BluetoothAdapter.STATE_BLE_TURNING_OFF

    // Set to true once a call to disable is made, in order to force the differentiation between the
    // various state hidden within STATE_OFF (OFF, BLE_TURNING_ON, BLE_TURNING_OFF)
@@ -151,7 +155,7 @@ private class AdapterStateListener(context: Context, private val adapter: Blueto
                awaitClose { context.unregisterReceiver(broadcastReceiver) }
            }
            .map { it.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) }
            .onEach { Log.d(TAG, "State changed to ${BluetoothAdapter.nameForState(it)}") }
            .onEach { Log.d(TAG, "State changed to ${nameForState(it)}") }
            .shareIn(CoroutineScope(Dispatchers.Default), SharingStarted.Eagerly, 1)

    private fun get(): Int =
@@ -171,11 +175,21 @@ private class AdapterStateListener(context: Context, private val adapter: Blueto
    fun eq(state: Int): Boolean = state == get()

    override fun toString(): String {
        val currentState = get()
        return if (currentState == STATE_UNKNOWN) {
            "UNKNOWN: State is uncertain, oneOf(OFF, BLE_TURNING_ON, BLE_TURNING_OFF)"
        } else {
            BluetoothAdapter.nameForState(currentState)
        return nameForState(get())
    }

    // Cts cannot use BluetoothAdapter.nameForState prior to T, some module test on R
    private fun nameForState(state: Int): String {
        return when (state) {
            STATE_UNKNOWN -> "UNKNOWN: State is oneOf(OFF, BLE_TURNING_ON, BLE_TURNING_OFF)"
            STATE_OFF -> "OFF"
            STATE_TURNING_ON -> "TURNING_ON"
            STATE_ON -> "ON"
            STATE_TURNING_OFF -> "TURNING_OFF"
            STATE_BLE_TURNING_ON -> "BLE_TURNING_ON"
            STATE_BLE_ON -> "BLE_ON"
            STATE_BLE_TURNING_OFF -> "BLE_TURNING_OFF"
            else -> "?!?!? ($state) ?!?!? "
        }
    }