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

Commit fc2f2352 authored by amehfooz's avatar amehfooz
Browse files

[SB][ComposeIcons] Add interactor for bluetooth connection status

This CL adds an interactor that can be used to determine if
there is currently an active bluetooth connection. This will be
used to determine if a bluetooth connected icon needs to be
shown in the status bar.

Bug: 414890231
Test: BluetoothConnectionStatusInteractorTest
Flag: com.android.systemui.status_bar_system_status_icons_in_compose

Change-Id: Ib4cd694968d2594c4de8678369738669aa4a3090
parent 38a622c1
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
    fun connectedDevices_whenDeviceDisconnects_isEmpty() =
        kosmos.runTest {
            val connectedDevices by collectLastValue(underTest.connectedDevices)
            bluetoothManager.eventManager?.let {
            bluetoothManager.eventManager.let {
                verify(it).registerCallback(callbackCaptor.capture())
            }
            val callback = callbackCaptor.value
@@ -156,8 +156,10 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
            whenever(cachedDevice.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTED)
            whenever(cachedDevice2.isConnected).thenReturn(true)
            whenever(cachedDevice2.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTED)

            whenever(cachedBluetoothDeviceManager.cachedDevicesCopy)
                .thenReturn(listOf(cachedDevice, cachedDevice2))

            callback.onConnectionStateChanged(cachedDevice, BluetoothProfile.STATE_CONNECTED)

            assertThat(connectedDevices).isEqualTo(listOf(cachedDevice, cachedDevice2))
@@ -199,12 +201,12 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
                mock<CachedBluetoothDevice>().also {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_DISCONNECTED)
                }
            val device2 =
            val cachedDevice2 =
                mock<CachedBluetoothDevice>().also {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_DISCONNECTED)
                }

            val status = fetchConnectionStatus(currentDevices = listOf(device1, device2))
            val status = fetchConnectionStatus(currentDevices = listOf(device1, cachedDevice2))

            assertThat(status.maxConnectionState).isEqualTo(BluetoothProfile.STATE_CONNECTING)
        }
@@ -235,13 +237,13 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTING)
                    whenever(it.isConnected).thenReturn(false)
                }
            val device2 =
            val cachedDevice2 =
                mock<CachedBluetoothDevice>().also {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTED)
                    whenever(it.isConnected).thenReturn(true)
                }

            val status = fetchConnectionStatus(currentDevices = listOf(device1, device2))
            val status = fetchConnectionStatus(currentDevices = listOf(device1, cachedDevice2))

            assertThat(status.maxConnectionState).isEqualTo(BluetoothProfile.STATE_CONNECTED)
        }
@@ -257,13 +259,13 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTED)
                    whenever(it.isConnected).thenReturn(false)
                }
            val device2 =
            val cachedDevice2 =
                mock<CachedBluetoothDevice>().also {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTED)
                    whenever(it.isConnected).thenReturn(false)
                }

            val status = fetchConnectionStatus(currentDevices = listOf(device1, device2))
            val status = fetchConnectionStatus(currentDevices = listOf(device1, cachedDevice2))

            // THEN the max state is DISCONNECTED
            assertThat(status.maxConnectionState).isEqualTo(BluetoothProfile.STATE_DISCONNECTED)
+83 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.policy.bluetooth.domain.interactor

import android.bluetooth.BluetoothProfile
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.bluetooth.CachedBluetoothDevice
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.policy.bluetooth.data.repository.bluetoothRepository
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4::class)
@ExperimentalCoroutinesApi
class BluetoothConnectionStatusInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val underTest = kosmos.bluetoothConnectionStatusInteractor

    @Test
    fun isBluetoothConnected_initialStateWithNoDevices_isFalse() =
        kosmos.runTest {
            val isConnected by collectLastValue(underTest.isBluetoothConnected)

            assertThat(isConnected).isFalse()
        }

    @Test
    fun isBluetoothConnected_whenDeviceConnects_isTrue() =
        kosmos.runTest {
            val isConnected by collectLastValue(underTest.isBluetoothConnected)

            // Simulate device connecting
            bluetoothRepository.setConnectedDevices(listOf(cachedDevice))

            assertThat(isConnected).isTrue()
        }

    @Test
    fun isBluetoothConnected_whenDeviceDisconnects_isFalse() =
        kosmos.runTest {
            val isConnected by collectLastValue(underTest.isBluetoothConnected)

            bluetoothRepository.setConnectedDevices(listOf(cachedDevice))
            assertThat(isConnected).isTrue()

            // Simulate device disconnecting
            bluetoothRepository.setConnectedDevices(emptyList())
            assertThat(isConnected).isFalse()
        }

    companion object {
        val cachedDevice =
            mock<CachedBluetoothDevice>().apply {
                whenever(this.isConnected).thenReturn(true)
                whenever(this.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTED)
            }
    }
}
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.policy.bluetooth.domain.interactor

import com.android.settingslib.bluetooth.CachedBluetoothDevice
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.policy.bluetooth.data.repository.BluetoothRepository
import javax.inject.Inject
import kotlinx.coroutines.flow.map

/** Interactor to manage and provide an observable state of Bluetooth connectivity. */
@SysUISingleton
class BluetoothConnectionStatusInteractor @Inject constructor(repository: BluetoothRepository) {
    private val maxConnectionState =
        repository.connectedDevices.map { devices -> calculateMaxConnectionState(devices) }

    val isBluetoothConnected =
        maxConnectionState.map { it == android.bluetooth.BluetoothProfile.STATE_CONNECTED }

    private fun calculateMaxConnectionState(devices: List<CachedBluetoothDevice>): Int {
        if (devices.isEmpty()) {
            return android.bluetooth.BluetoothProfile.STATE_DISCONNECTED
        }
        return devices.maxOfOrNull { it.maxConnectionState }
            ?: android.bluetooth.BluetoothProfile.STATE_DISCONNECTED
    }
}
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.policy.bluetooth.domain.interactor

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.statusbar.policy.bluetooth.data.repository.bluetoothRepository

val Kosmos.bluetoothConnectionStatusInteractor: BluetoothConnectionStatusInteractor by
    Kosmos.Fixture { BluetoothConnectionStatusInteractor(repository = bluetoothRepository) }