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

Commit fc0e80a2 authored by Chelsea Hao's avatar Chelsea Hao Committed by Android (Google) Code Review
Browse files

Merge "Allow changing auto on value for all users." into main

parents d0fa4222 a35072b7
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ constructor(
    private val bluetoothAutoOnRepository: BluetoothAutoOnRepository,
) {

    val isEnabled = bluetoothAutoOnRepository.getValue.map { it == ENABLED }.distinctUntilChanged()
    val isEnabled = bluetoothAutoOnRepository.isAutoOn.map { it == ENABLED }.distinctUntilChanged()

    /**
     * Checks if the auto on value is present in the repository.
@@ -49,7 +49,7 @@ constructor(
            Log.e(TAG, "Trying to set toggle value while feature not available.")
        } else {
            val newValue = if (value) ENABLED else DISABLED
            bluetoothAutoOnRepository.setValue(newValue)
            bluetoothAutoOnRepository.setAutoOn(newValue)
        }
    }

+34 −31
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.systemui.qs.tiles.dialog.bluetooth

import android.os.UserHandle
import android.util.Log
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
@@ -27,17 +25,21 @@ import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext

/** Repository class responsible for managing the Bluetooth Auto-On feature settings. */
// TODO(b/316822488): Handle multi-user
/**
 * Repository class responsible for managing the Bluetooth Auto-On feature settings for the current
 * user.
 */
@SysUISingleton
class BluetoothAutoOnRepository
@Inject
@@ -47,21 +49,24 @@ constructor(
    @Application private val coroutineScope: CoroutineScope,
    @Background private val backgroundDispatcher: CoroutineDispatcher,
) {
    // Flow representing the auto on setting value
    internal val getValue: Flow<Int> =

    // Flow representing the auto on setting value for the current user
    @OptIn(ExperimentalCoroutinesApi::class)
    internal val isAutoOn: StateFlow<Int> =
        userRepository.selectedUserInfo
            .flatMapLatest { userInfo ->
                secureSettings
            .observerFlow(UserHandle.USER_SYSTEM, SETTING_NAME)
                    .observerFlow(userInfo.id, SETTING_NAME)
                    .onStart { emit(Unit) }
            .map {
                if (userRepository.getSelectedUserInfo().id != UserHandle.USER_SYSTEM) {
                    Log.i(TAG, "Current user is not USER_SYSTEM. Multi-user is not supported")
                    return@map UNSET
                }
                secureSettings.getIntForUser(SETTING_NAME, UNSET, UserHandle.USER_SYSTEM)
                    .map { secureSettings.getIntForUser(SETTING_NAME, UNSET, userInfo.id) }
            }
            .distinctUntilChanged()
            .flowOn(backgroundDispatcher)
            .shareIn(coroutineScope, SharingStarted.WhileSubscribed(replayExpirationMillis = 0))
            .stateIn(
                coroutineScope,
                SharingStarted.WhileSubscribed(replayExpirationMillis = 0),
                UNSET
            )

    /**
     * Checks if the auto on setting value is ever set for the current user.
@@ -70,12 +75,11 @@ constructor(
     */
    suspend fun isValuePresent(): Boolean =
        withContext(backgroundDispatcher) {
            if (userRepository.getSelectedUserInfo().id != UserHandle.USER_SYSTEM) {
                Log.i(TAG, "Current user is not USER_SYSTEM. Multi-user is not supported")
                false
            } else {
                secureSettings.getIntForUser(SETTING_NAME, UNSET, UserHandle.USER_SYSTEM) != UNSET
            }
            secureSettings.getIntForUser(
                SETTING_NAME,
                UNSET,
                userRepository.getSelectedUserInfo().id
            ) != UNSET
        }

    /**
@@ -83,18 +87,17 @@ constructor(
     *
     * @param value The new setting value to be applied.
     */
    suspend fun setValue(value: Int) {
    suspend fun setAutoOn(value: Int) {
        withContext(backgroundDispatcher) {
            if (userRepository.getSelectedUserInfo().id != UserHandle.USER_SYSTEM) {
                Log.i(TAG, "Current user is not USER_SYSTEM. Multi-user is not supported")
            } else {
                secureSettings.putIntForUser(SETTING_NAME, value, UserHandle.USER_SYSTEM)
            }
            secureSettings.putIntForUser(
                SETTING_NAME,
                value,
                userRepository.getSelectedUserInfo().id
            )
        }
    }

    companion object {
        private const val TAG = "BluetoothAutoOnRepository"
        const val SETTING_NAME = "bluetooth_automatic_turn_on"
        const val UNSET = -1
    }
+7 −6
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ class BluetoothAutoOnRepositoryTest : SysuiTestCase() {
    fun testGetValue_valueUnset() {
        testScope.runTest {
            userRepository.setSelectedUserInfo(SYSTEM_USER)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.getValue)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)

            runCurrent()

@@ -81,7 +81,7 @@ class BluetoothAutoOnRepositoryTest : SysuiTestCase() {
    fun testGetValue_valueFalse() {
        testScope.runTest {
            userRepository.setSelectedUserInfo(SYSTEM_USER)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.getValue)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)

            secureSettings.putIntForUser(SETTING_NAME, DISABLED, UserHandle.USER_SYSTEM)
            runCurrent()
@@ -94,7 +94,7 @@ class BluetoothAutoOnRepositoryTest : SysuiTestCase() {
    fun testGetValue_valueTrue() {
        testScope.runTest {
            userRepository.setSelectedUserInfo(SYSTEM_USER)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.getValue)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)

            secureSettings.putIntForUser(SETTING_NAME, ENABLED, UserHandle.USER_SYSTEM)
            runCurrent()
@@ -104,15 +104,16 @@ class BluetoothAutoOnRepositoryTest : SysuiTestCase() {
    }

    @Test
    fun testGetValue_valueTrue_secondaryUser_returnUnset() {
    fun testGetValue_valueTrue_secondaryUser_returnTrue() {
        testScope.runTest {
            userRepository.setSelectedUserInfo(SECONDARY_USER)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.getValue)
            val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)

            secureSettings.putIntForUser(SETTING_NAME, DISABLED, SYSTEM_USER_ID)
            secureSettings.putIntForUser(SETTING_NAME, ENABLED, SECONDARY_USER_ID)
            runCurrent()

            assertThat(actualValue).isEqualTo(UNSET)
            assertThat(actualValue).isEqualTo(ENABLED)
        }
    }