summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothCallback.java10
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java19
-rw-r--r--packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java14
-rw-r--r--packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractor.kt19
-rw-r--r--packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepository.kt125
-rw-r--r--packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModel.kt3
-rw-r--r--packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractorTest.kt62
-rw-r--r--packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepositoryTest.kt73
-rw-r--r--packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModelTest.kt39
9 files changed, 192 insertions, 172 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothCallback.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothCallback.java
index 416b36981a4c..baccda7e3cc4 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothCallback.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothCallback.java
@@ -163,6 +163,16 @@ public interface BluetoothCallback {
default void onAclConnectionStateChanged(
@NonNull CachedBluetoothDevice cachedDevice, int state) {}
+ /**
+ * Called when the Auto-on state is changed for any user. Listens to intent
+ * {@link android.bluetooth.BluetoothAdapter#ACTION_AUTO_ON_STATE_CHANGED }
+ *
+ * @param state the Auto-on state, the possible values are:
+ * {@link android.bluetooth.BluetoothAdapter#AUTO_ON_STATE_ENABLED},
+ * {@link android.bluetooth.BluetoothAdapter#AUTO_ON_STATE_DISABLED}
+ */
+ default void onAutoOnStateChanged(int state) {}
+
@Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = { "STATE_" }, value = {
STATE_DISCONNECTED,
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
index 647fcb9f67fa..0996d52b0e30 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
@@ -133,6 +133,8 @@ public class BluetoothEventManager {
addHandler(BluetoothDevice.ACTION_ACL_CONNECTED, new AclStateChangedHandler());
addHandler(BluetoothDevice.ACTION_ACL_DISCONNECTED, new AclStateChangedHandler());
+ addHandler(BluetoothAdapter.ACTION_AUTO_ON_STATE_CHANGED, new AutoOnStateChangedHandler());
+
registerAdapterIntentReceiver();
}
@@ -552,4 +554,21 @@ public class BluetoothEventManager {
dispatchAudioModeChanged();
}
}
+
+ private class AutoOnStateChangedHandler implements Handler {
+
+ @Override
+ public void onReceive(Context context, Intent intent, BluetoothDevice device) {
+ String action = intent.getAction();
+ if (action == null) {
+ Log.w(TAG, "AutoOnStateChangedHandler() action is null");
+ return;
+ }
+ int state = intent.getIntExtra(BluetoothAdapter.EXTRA_AUTO_ON_STATE,
+ BluetoothAdapter.ERROR);
+ for (BluetoothCallback callback : mCallbacks) {
+ callback.onAutoOnStateChanged(state);
+ }
+ }
+ }
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
index 13635c3a8256..48bbf4ea6a65 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
@@ -18,6 +18,7 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -489,4 +490,17 @@ public class BluetoothEventManagerTest {
verify(mErrorListener).onShowError(any(Context.class), eq(DEVICE_NAME),
eq(R.string.bluetooth_pairing_pin_error_message));
}
+
+ /**
+ * Intent ACTION_AUTO_ON_STATE_CHANGED should dispatch to callback.
+ */
+ @Test
+ public void intentWithExtraState_autoOnStateChangedShouldDispatchToRegisterCallback() {
+ mBluetoothEventManager.registerCallback(mBluetoothCallback);
+ mIntent = new Intent(BluetoothAdapter.ACTION_AUTO_ON_STATE_CHANGED);
+
+ mContext.sendBroadcast(mIntent);
+
+ verify(mBluetoothCallback).onAutoOnStateChanged(anyInt());
+ }
}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractor.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractor.kt
index 1247854da61d..59fc81c82df0 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractor.kt
@@ -19,8 +19,6 @@ package com.android.systemui.qs.tiles.dialog.bluetooth
import android.util.Log
import com.android.systemui.dagger.SysUISingleton
import javax.inject.Inject
-import kotlinx.coroutines.flow.distinctUntilChanged
-import kotlinx.coroutines.flow.map
/** Interactor class responsible for interacting with the Bluetooth Auto-On feature. */
@SysUISingleton
@@ -30,14 +28,10 @@ constructor(
private val bluetoothAutoOnRepository: BluetoothAutoOnRepository,
) {
- val isEnabled = bluetoothAutoOnRepository.isAutoOn.map { it == ENABLED }.distinctUntilChanged()
+ val isEnabled = bluetoothAutoOnRepository.isAutoOn
- /**
- * Checks if the auto on value is present in the repository.
- *
- * @return `true` if a value is present (i.e, the feature is enabled by the Bluetooth server).
- */
- suspend fun isValuePresent(): Boolean = bluetoothAutoOnRepository.isValuePresent()
+ /** Checks if the auto on feature is supported. */
+ suspend fun isAutoOnSupported(): Boolean = bluetoothAutoOnRepository.isAutoOnSupported()
/**
* Sets enabled or disabled based on the provided value.
@@ -45,17 +39,14 @@ constructor(
* @param value `true` to enable the feature, `false` to disable it.
*/
suspend fun setEnabled(value: Boolean) {
- if (!isValuePresent()) {
+ if (!isAutoOnSupported()) {
Log.e(TAG, "Trying to set toggle value while feature not available.")
} else {
- val newValue = if (value) ENABLED else DISABLED
- bluetoothAutoOnRepository.setAutoOn(newValue)
+ bluetoothAutoOnRepository.setAutoOn(value)
}
}
companion object {
private const val TAG = "BluetoothAutoOnInteractor"
- const val DISABLED = 0
- const val ENABLED = 1
}
}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepository.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepository.kt
index f97fc389b12c..9ee582a77862 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepository.kt
@@ -16,22 +16,23 @@
package com.android.systemui.qs.tiles.dialog.bluetooth
+import android.bluetooth.BluetoothAdapter
+import android.util.Log
+import com.android.settingslib.bluetooth.BluetoothCallback
+import com.android.settingslib.bluetooth.LocalBluetoothManager
+import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
+import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
-import com.android.systemui.user.data.repository.UserRepository
-import com.android.systemui.util.settings.SecureSettings
-import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.channels.awaitClose
+import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
-import kotlinx.coroutines.flow.StateFlow
-import kotlinx.coroutines.flow.distinctUntilChanged
-import kotlinx.coroutines.flow.flatMapLatest
+import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
-import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
@@ -44,61 +45,87 @@ import kotlinx.coroutines.withContext
class BluetoothAutoOnRepository
@Inject
constructor(
- private val secureSettings: SecureSettings,
- private val userRepository: UserRepository,
+ localBluetoothManager: LocalBluetoothManager?,
+ private val bluetoothAdapter: BluetoothAdapter?,
@Application private val coroutineScope: CoroutineScope,
@Background private val backgroundDispatcher: CoroutineDispatcher,
) {
- // 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(userInfo.id, SETTING_NAME)
- .onStart { emit(Unit) }
- .map { secureSettings.getIntForUser(SETTING_NAME, UNSET, userInfo.id) }
- }
- .distinctUntilChanged()
- .flowOn(backgroundDispatcher)
- .stateIn(
- coroutineScope,
- SharingStarted.WhileSubscribed(replayExpirationMillis = 0),
- UNSET
- )
+ // Flow representing the auto on state for the current user
+ internal val isAutoOn: Flow<Boolean> =
+ localBluetoothManager?.eventManager?.let { eventManager ->
+ conflatedCallbackFlow {
+ val listener =
+ object : BluetoothCallback {
+ override fun onAutoOnStateChanged(autoOnState: Int) {
+ super.onAutoOnStateChanged(autoOnState)
+ if (
+ autoOnState == BluetoothAdapter.AUTO_ON_STATE_ENABLED ||
+ autoOnState == BluetoothAdapter.AUTO_ON_STATE_DISABLED
+ ) {
+ trySendWithFailureLogging(
+ autoOnState == BluetoothAdapter.AUTO_ON_STATE_ENABLED,
+ TAG,
+ "onAutoOnStateChanged"
+ )
+ }
+ }
+ }
+ eventManager.registerCallback(listener)
+ awaitClose { eventManager.unregisterCallback(listener) }
+ }
+ .onStart { emit(isAutoOnEnabled()) }
+ .flowOn(backgroundDispatcher)
+ .stateIn(
+ coroutineScope,
+ SharingStarted.WhileSubscribed(replayExpirationMillis = 0),
+ initialValue = false
+ )
+ }
+ ?: flowOf(false)
/**
- * Checks if the auto on setting value is ever set for the current user.
+ * Checks if the auto on feature is supported for the current user.
*
- * @return `true` if the setting value is not UNSET, `false` otherwise.
+ * @throws Exception if an error occurs while checking auto-on support.
*/
- suspend fun isValuePresent(): Boolean =
+ suspend fun isAutoOnSupported(): Boolean =
withContext(backgroundDispatcher) {
- secureSettings.getIntForUser(
- SETTING_NAME,
- UNSET,
- userRepository.getSelectedUserInfo().id
- ) != UNSET
+ try {
+ bluetoothAdapter?.isAutoOnSupported ?: false
+ } catch (e: Exception) {
+ // Server could throw TimeoutException, InterruptedException or ExecutionException
+ Log.e(TAG, "Error calling isAutoOnSupported", e)
+ false
+ }
}
- /**
- * Sets the Bluetooth Auto-On setting value for the current user.
- *
- * @param value The new setting value to be applied.
- */
- suspend fun setAutoOn(value: Int) {
+ /** Sets the Bluetooth Auto-On for the current user. */
+ suspend fun setAutoOn(value: Boolean) {
withContext(backgroundDispatcher) {
- secureSettings.putIntForUser(
- SETTING_NAME,
- value,
- userRepository.getSelectedUserInfo().id
- )
+ try {
+ bluetoothAdapter?.setAutoOnEnabled(value)
+ } catch (e: Exception) {
+ // Server could throw IllegalStateException, TimeoutException, InterruptedException
+ // or ExecutionException
+ Log.e(TAG, "Error calling setAutoOnEnabled", e)
+ }
}
}
- companion object {
- const val SETTING_NAME = "bluetooth_automatic_turn_on"
- const val UNSET = -1
+ private suspend fun isAutoOnEnabled() =
+ withContext(backgroundDispatcher) {
+ try {
+ bluetoothAdapter?.isAutoOnEnabled ?: false
+ } catch (e: Exception) {
+ // Server could throw IllegalStateException, TimeoutException, InterruptedException
+ // or ExecutionException
+ Log.e(TAG, "Error calling isAutoOnEnabled", e)
+ false
+ }
+ }
+
+ private companion object {
+ const val TAG = "BluetoothAutoOnRepository"
}
}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModel.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModel.kt
index e4f3c199371e..163df6d7f750 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModel.kt
@@ -29,7 +29,6 @@ import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import com.android.internal.jank.InteractionJankMonitor
import com.android.internal.logging.UiEventLogger
-import com.android.settingslib.flags.Flags.bluetoothQsTileDialogAutoOnToggle
import com.android.systemui.Prefs
import com.android.systemui.animation.DialogCuj
import com.android.systemui.animation.DialogTransitionAnimator
@@ -277,7 +276,7 @@ constructor(
@VisibleForTesting
internal suspend fun isAutoOnToggleFeatureAvailable() =
- bluetoothQsTileDialogAutoOnToggle() && bluetoothAutoOnInteractor.isValuePresent()
+ bluetoothAutoOnInteractor.isAutoOnSupported()
companion object {
private const val INTERACTION_JANK_TAG = "bluetooth_tile_dialog"
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractorTest.kt
index 37107135c6d8..036d3c862ae0 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnInteractorTest.kt
@@ -16,22 +16,25 @@
package com.android.systemui.qs.tiles.dialog.bluetooth
-import android.content.pm.UserInfo
+import android.bluetooth.BluetoothAdapter
import android.testing.AndroidTestingRunner
import androidx.test.filters.SmallTest
+import com.android.settingslib.bluetooth.LocalBluetoothManager
import com.android.systemui.SysuiTestCase
-import com.android.systemui.coroutines.collectLastValue
-import com.android.systemui.user.data.repository.FakeUserRepository
-import com.android.systemui.util.settings.FakeSettings
-import com.google.common.truth.Truth
+import com.android.systemui.util.mockito.mock
+import com.android.systemui.util.mockito.whenever
import kotlin.test.Test
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith
+import org.mockito.ArgumentMatchers.anyBoolean
+import org.mockito.Mock
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
@@ -41,8 +44,17 @@ class BluetoothAutoOnInteractorTest : SysuiTestCase() {
@get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()
private val testDispatcher = StandardTestDispatcher()
private val testScope = TestScope(testDispatcher)
- private var secureSettings: FakeSettings = FakeSettings()
- private val userRepository: FakeUserRepository = FakeUserRepository()
+ private val bluetoothAdapter =
+ mock<BluetoothAdapter> {
+ var autoOn = false
+ whenever(isAutoOnEnabled).thenAnswer { autoOn }
+
+ whenever(setAutoOnEnabled(anyBoolean())).thenAnswer { invocation ->
+ autoOn = invocation.getArgument(0) as Boolean
+ autoOn
+ }
+ }
+ @Mock private lateinit var localBluetoothManager: LocalBluetoothManager
private lateinit var bluetoothAutoOnInteractor: BluetoothAutoOnInteractor
@Before
@@ -50,49 +62,35 @@ class BluetoothAutoOnInteractorTest : SysuiTestCase() {
bluetoothAutoOnInteractor =
BluetoothAutoOnInteractor(
BluetoothAutoOnRepository(
- secureSettings,
- userRepository,
+ localBluetoothManager,
+ bluetoothAdapter,
testScope.backgroundScope,
- testDispatcher
+ testDispatcher,
)
)
}
@Test
- fun testSet_bluetoothAutoOnUnset_doNothing() {
+ fun testSetEnabled_bluetoothAutoOnUnsupported_doNothing() {
testScope.runTest {
- bluetoothAutoOnInteractor.setEnabled(true)
-
- val actualValue by collectLastValue(bluetoothAutoOnInteractor.isEnabled)
+ whenever(bluetoothAdapter.isAutoOnSupported).thenReturn(false)
+ bluetoothAutoOnInteractor.setEnabled(true)
runCurrent()
- Truth.assertThat(actualValue).isEqualTo(false)
+ assertFalse(bluetoothAdapter.isAutoOnEnabled)
}
}
@Test
- fun testSet_bluetoothAutoOnSet_setNewValue() {
+ fun testSetEnabled_bluetoothAutoOnSupported_setNewValue() {
testScope.runTest {
- userRepository.setUserInfos(listOf(SYSTEM_USER))
- secureSettings.putIntForUser(
- BluetoothAutoOnRepository.SETTING_NAME,
- BluetoothAutoOnInteractor.DISABLED,
- SYSTEM_USER_ID
- )
- bluetoothAutoOnInteractor.setEnabled(true)
-
- val actualValue by collectLastValue(bluetoothAutoOnInteractor.isEnabled)
+ whenever(bluetoothAdapter.isAutoOnSupported).thenReturn(true)
+ bluetoothAutoOnInteractor.setEnabled(true)
runCurrent()
- Truth.assertThat(actualValue).isEqualTo(true)
+ assertTrue(bluetoothAdapter.isAutoOnEnabled)
}
}
-
- companion object {
- private const val SYSTEM_USER_ID = 0
- private val SYSTEM_USER =
- UserInfo(/* id= */ SYSTEM_USER_ID, /* name= */ "system user", /* flags= */ 0)
- }
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepositoryTest.kt
index cd1452a6bf84..31192841ec77 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepositoryTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothAutoOnRepositoryTest.kt
@@ -16,18 +16,14 @@
package com.android.systemui.qs.tiles.dialog.bluetooth
-import android.content.pm.UserInfo
-import android.os.UserHandle
+import android.bluetooth.BluetoothAdapter
import android.testing.AndroidTestingRunner
import androidx.test.filters.SmallTest
+import com.android.settingslib.bluetooth.BluetoothEventManager
+import com.android.settingslib.bluetooth.LocalBluetoothManager
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
-import com.android.systemui.qs.tiles.dialog.bluetooth.BluetoothAutoOnInteractor.Companion.DISABLED
-import com.android.systemui.qs.tiles.dialog.bluetooth.BluetoothAutoOnInteractor.Companion.ENABLED
-import com.android.systemui.qs.tiles.dialog.bluetooth.BluetoothAutoOnRepository.Companion.SETTING_NAME
-import com.android.systemui.qs.tiles.dialog.bluetooth.BluetoothAutoOnRepository.Companion.UNSET
-import com.android.systemui.user.data.repository.FakeUserRepository
-import com.android.systemui.util.settings.FakeSettings
+import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
@@ -37,6 +33,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
+import org.mockito.Mock
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
@@ -46,83 +43,57 @@ class BluetoothAutoOnRepositoryTest : SysuiTestCase() {
@get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()
private val testDispatcher = StandardTestDispatcher()
private val testScope = TestScope(testDispatcher)
- private var secureSettings: FakeSettings = FakeSettings()
- private val userRepository: FakeUserRepository = FakeUserRepository()
+ @Mock private lateinit var bluetoothAdapter: BluetoothAdapter
+ @Mock private lateinit var localBluetoothManager: LocalBluetoothManager
+ @Mock private lateinit var eventManager: BluetoothEventManager
private lateinit var bluetoothAutoOnRepository: BluetoothAutoOnRepository
@Before
fun setUp() {
+ whenever(localBluetoothManager.eventManager).thenReturn(eventManager)
bluetoothAutoOnRepository =
BluetoothAutoOnRepository(
- secureSettings,
- userRepository,
+ localBluetoothManager,
+ bluetoothAdapter,
testScope.backgroundScope,
- testDispatcher
+ testDispatcher,
)
-
- userRepository.setUserInfos(listOf(SECONDARY_USER, SYSTEM_USER))
}
@Test
- fun testGetValue_valueUnset() {
+ fun testIsAutoOn_returnFalse() {
testScope.runTest {
- userRepository.setSelectedUserInfo(SYSTEM_USER)
+ whenever(bluetoothAdapter.isAutoOnEnabled).thenReturn(false)
val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)
runCurrent()
- assertThat(actualValue).isEqualTo(UNSET)
- assertThat(bluetoothAutoOnRepository.isValuePresent()).isFalse()
+ assertThat(actualValue).isEqualTo(false)
}
}
@Test
- fun testGetValue_valueFalse() {
+ fun testIsAutoOn_returnTrue() {
testScope.runTest {
- userRepository.setSelectedUserInfo(SYSTEM_USER)
+ whenever(bluetoothAdapter.isAutoOnEnabled).thenReturn(true)
val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)
- secureSettings.putIntForUser(SETTING_NAME, DISABLED, UserHandle.USER_SYSTEM)
runCurrent()
- assertThat(actualValue).isEqualTo(DISABLED)
+ assertThat(actualValue).isEqualTo(true)
}
}
@Test
- fun testGetValue_valueTrue() {
+ fun testIsAutoOnSupported_returnTrue() {
testScope.runTest {
- userRepository.setSelectedUserInfo(SYSTEM_USER)
- val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn)
+ whenever(bluetoothAdapter.isAutoOnSupported).thenReturn(true)
+ val actualValue = bluetoothAutoOnRepository.isAutoOnSupported()
- secureSettings.putIntForUser(SETTING_NAME, ENABLED, UserHandle.USER_SYSTEM)
runCurrent()
- assertThat(actualValue).isEqualTo(ENABLED)
+ assertThat(actualValue).isEqualTo(true)
}
}
-
- @Test
- fun testGetValue_valueTrue_secondaryUser_returnTrue() {
- testScope.runTest {
- userRepository.setSelectedUserInfo(SECONDARY_USER)
- 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(ENABLED)
- }
- }
-
- companion object {
- private const val SYSTEM_USER_ID = 0
- private const val SECONDARY_USER_ID = 1
- private val SYSTEM_USER =
- UserInfo(/* id= */ SYSTEM_USER_ID, /* name= */ "system user", /* flags= */ 0)
- private val SECONDARY_USER =
- UserInfo(/* id= */ SECONDARY_USER_ID, /* name= */ "secondary user", /* flags= */ 0)
- }
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModelTest.kt
index 39e2413be40e..adea362b2ef4 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModelTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/bluetooth/BluetoothTileDialogViewModelTest.kt
@@ -16,7 +16,7 @@
package com.android.systemui.qs.tiles.dialog.bluetooth
-import android.content.pm.UserInfo
+import android.bluetooth.BluetoothAdapter
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper
import android.view.View
@@ -26,19 +26,18 @@ import android.widget.LinearLayout
import androidx.test.filters.SmallTest
import com.android.internal.logging.UiEventLogger
import com.android.settingslib.bluetooth.CachedBluetoothDevice
+import com.android.settingslib.bluetooth.LocalBluetoothManager
import com.android.settingslib.flags.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.DialogTransitionAnimator
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.statusbar.phone.SystemUIDialog
-import com.android.systemui.user.data.repository.FakeUserRepository
import com.android.systemui.util.FakeSharedPreferences
import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.kotlin.getMutableStateFlow
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.nullable
import com.android.systemui.util.mockito.whenever
-import com.android.systemui.util.settings.FakeSettings
import com.android.systemui.util.time.FakeSystemClock
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.CoroutineDispatcher
@@ -75,6 +74,8 @@ class BluetoothTileDialogViewModelTest : SysuiTestCase() {
@Mock private lateinit var bluetoothStateInteractor: BluetoothStateInteractor
+ @Mock private lateinit var bluetoothAutoOnInteractor: BluetoothAutoOnInteractor
+
@Mock private lateinit var deviceItemInteractor: DeviceItemInteractor
@Mock private lateinit var activityStarter: ActivityStarter
@@ -87,6 +88,10 @@ class BluetoothTileDialogViewModelTest : SysuiTestCase() {
@Mock private lateinit var uiEventLogger: UiEventLogger
+ @Mock private lateinit var bluetoothAdapter: BluetoothAdapter
+
+ @Mock private lateinit var localBluetoothManager: LocalBluetoothManager
+
@Mock
private lateinit var mBluetoothTileDialogDelegateDelegateFactory:
BluetoothTileDialogDelegate.Factory
@@ -100,8 +105,6 @@ class BluetoothTileDialogViewModelTest : SysuiTestCase() {
private lateinit var scheduler: TestCoroutineScheduler
private lateinit var dispatcher: CoroutineDispatcher
private lateinit var testScope: TestScope
- private lateinit var secureSettings: FakeSettings
- private lateinit var userRepository: FakeUserRepository
@Before
fun setUp() {
@@ -109,14 +112,6 @@ class BluetoothTileDialogViewModelTest : SysuiTestCase() {
scheduler = TestCoroutineScheduler()
dispatcher = UnconfinedTestDispatcher(scheduler)
testScope = TestScope(dispatcher)
- secureSettings = FakeSettings()
- userRepository = FakeUserRepository()
- userRepository.setUserInfos(listOf(SYSTEM_USER))
- secureSettings.putIntForUser(
- BluetoothAutoOnRepository.SETTING_NAME,
- BluetoothAutoOnInteractor.ENABLED,
- SYSTEM_USER_ID
- )
bluetoothTileDialogViewModel =
BluetoothTileDialogViewModel(
deviceItemInteractor,
@@ -124,8 +119,8 @@ class BluetoothTileDialogViewModelTest : SysuiTestCase() {
// TODO(b/316822488): Create FakeBluetoothAutoOnInteractor.
BluetoothAutoOnInteractor(
BluetoothAutoOnRepository(
- secureSettings,
- userRepository,
+ localBluetoothManager,
+ bluetoothAdapter,
testScope.backgroundScope,
dispatcher
)
@@ -265,26 +260,22 @@ class BluetoothTileDialogViewModelTest : SysuiTestCase() {
}
@Test
- fun testIsAutoOnToggleFeatureAvailable_flagOn_settingValueSet_returnTrue() {
+ fun testIsAutoOnToggleFeatureAvailable_returnTrue() {
testScope.runTest {
+ whenever(bluetoothAdapter.isAutoOnSupported).thenReturn(true)
+
val actual = bluetoothTileDialogViewModel.isAutoOnToggleFeatureAvailable()
assertThat(actual).isTrue()
}
}
@Test
- fun testIsAutoOnToggleFeatureAvailable_flagOff_settingValueSet_returnFalse() {
+ fun testIsAutoOnToggleFeatureAvailable_returnFalse() {
testScope.runTest {
- mSetFlagsRule.disableFlags(Flags.FLAG_BLUETOOTH_QS_TILE_DIALOG_AUTO_ON_TOGGLE)
+ whenever(bluetoothAdapter.isAutoOnSupported).thenReturn(false)
val actual = bluetoothTileDialogViewModel.isAutoOnToggleFeatureAvailable()
assertThat(actual).isFalse()
}
}
-
- companion object {
- private const val SYSTEM_USER_ID = 0
- private val SYSTEM_USER =
- UserInfo(/* id= */ SYSTEM_USER_ID, /* name= */ "system user", /* flags= */ 0)
- }
}