Loading packages/SystemUI/src/com/android/systemui/wallpapers/data/repository/WallpaperRepository.kt +13 −6 Original line number Original line Diff line number Diff line Loading @@ -28,7 +28,9 @@ import com.android.systemui.dagger.qualifiers.Background import com.android.systemui.user.data.model.SelectedUserModel import com.android.systemui.user.data.model.SelectedUserModel import com.android.systemui.user.data.model.SelectionStatus import com.android.systemui.user.data.model.SelectionStatus import com.android.systemui.user.data.repository.UserRepository import com.android.systemui.user.data.repository.UserRepository import com.android.systemui.utils.coroutines.flow.mapLatestConflated import javax.inject.Inject import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow Loading @@ -40,6 +42,7 @@ import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.withContext /** A repository storing information about the current wallpaper. */ /** A repository storing information about the current wallpaper. */ interface WallpaperRepository { interface WallpaperRepository { Loading @@ -55,6 +58,7 @@ class WallpaperRepositoryImpl @Inject @Inject constructor( constructor( @Background scope: CoroutineScope, @Background scope: CoroutineScope, @Background private val bgDispatcher: CoroutineDispatcher, broadcastDispatcher: BroadcastDispatcher, broadcastDispatcher: BroadcastDispatcher, userRepository: UserRepository, userRepository: UserRepository, private val wallpaperManager: WallpaperManager, private val wallpaperManager: WallpaperManager, Loading Loading @@ -87,14 +91,15 @@ constructor( if (!wallpaperManager.isWallpaperSupported || !deviceSupportsAodWallpaper) { if (!wallpaperManager.isWallpaperSupported || !deviceSupportsAodWallpaper) { MutableStateFlow(null).asStateFlow() MutableStateFlow(null).asStateFlow() } else { } else { combine(wallpaperChanged, selectedUser) { _, selectedUser -> combine(wallpaperChanged, selectedUser, ::Pair) getWallpaper(selectedUser) .mapLatestConflated { (_, selectedUser) -> getWallpaper(selectedUser) } } .stateIn( .stateIn( scope, scope, // Always be listening for wallpaper changes. // Always be listening for wallpaper changes. SharingStarted.Eagerly, SharingStarted.Eagerly, initialValue = getWallpaper(userRepository.selectedUser.value), // The initial value is null, but it should get updated pretty quickly because // the `combine` should immediately kick off a fetch. initialValue = null, ) ) } } Loading @@ -111,7 +116,9 @@ constructor( initialValue = wallpaperInfo.value?.supportsAmbientMode() == true, initialValue = wallpaperInfo.value?.supportsAmbientMode() == true, ) ) private fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? { private suspend fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? { return wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id) return withContext(bgDispatcher) { wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id) } } } } } packages/SystemUI/tests/src/com/android/systemui/wallpapers/data/repository/WallpaperRepositoryImplTest.kt +12 −3 Original line number Original line Diff line number Diff line Loading @@ -33,6 +33,7 @@ import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Before import org.junit.Test import org.junit.Test Loading @@ -41,13 +42,15 @@ import org.junit.Test @OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class) class WallpaperRepositoryImplTest : SysuiTestCase() { class WallpaperRepositoryImplTest : SysuiTestCase() { private val testScope = TestScope(StandardTestDispatcher()) private val testDispatcher = StandardTestDispatcher() private val testScope = TestScope(testDispatcher) private val userRepository = FakeUserRepository() private val userRepository = FakeUserRepository() private val wallpaperManager: WallpaperManager = mock() private val wallpaperManager: WallpaperManager = mock() private val underTest: WallpaperRepositoryImpl by lazy { private val underTest: WallpaperRepositoryImpl by lazy { WallpaperRepositoryImpl( WallpaperRepositoryImpl( testScope.backgroundScope, testScope.backgroundScope, testDispatcher, fakeBroadcastDispatcher, fakeBroadcastDispatcher, userRepository, userRepository, wallpaperManager, wallpaperManager, Loading Loading @@ -102,8 +105,10 @@ class WallpaperRepositoryImplTest : SysuiTestCase() { userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) // WHEN the repo initially starts up (underTest is lazy), then it fetches the current // Start up the repo and let it run the initial fetch // value for the wallpaper underTest.wallpaperInfo runCurrent() assertThat(underTest.wallpaperInfo.value).isEqualTo(SUPPORTED_WP) assertThat(underTest.wallpaperInfo.value).isEqualTo(SUPPORTED_WP) } } Loading Loading @@ -282,6 +287,10 @@ class WallpaperRepositoryImplTest : SysuiTestCase() { userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) // Start up the repo and let it run the initial fetch underTest.wallpaperSupportsAmbientMode runCurrent() // WHEN the repo initially starts up (underTest is lazy), then it fetches the current // WHEN the repo initially starts up (underTest is lazy), then it fetches the current // value for the wallpaper // value for the wallpaper assertThat(underTest.wallpaperSupportsAmbientMode.value).isTrue() assertThat(underTest.wallpaperSupportsAmbientMode.value).isTrue() Loading Loading
packages/SystemUI/src/com/android/systemui/wallpapers/data/repository/WallpaperRepository.kt +13 −6 Original line number Original line Diff line number Diff line Loading @@ -28,7 +28,9 @@ import com.android.systemui.dagger.qualifiers.Background import com.android.systemui.user.data.model.SelectedUserModel import com.android.systemui.user.data.model.SelectedUserModel import com.android.systemui.user.data.model.SelectionStatus import com.android.systemui.user.data.model.SelectionStatus import com.android.systemui.user.data.repository.UserRepository import com.android.systemui.user.data.repository.UserRepository import com.android.systemui.utils.coroutines.flow.mapLatestConflated import javax.inject.Inject import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow Loading @@ -40,6 +42,7 @@ import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.withContext /** A repository storing information about the current wallpaper. */ /** A repository storing information about the current wallpaper. */ interface WallpaperRepository { interface WallpaperRepository { Loading @@ -55,6 +58,7 @@ class WallpaperRepositoryImpl @Inject @Inject constructor( constructor( @Background scope: CoroutineScope, @Background scope: CoroutineScope, @Background private val bgDispatcher: CoroutineDispatcher, broadcastDispatcher: BroadcastDispatcher, broadcastDispatcher: BroadcastDispatcher, userRepository: UserRepository, userRepository: UserRepository, private val wallpaperManager: WallpaperManager, private val wallpaperManager: WallpaperManager, Loading Loading @@ -87,14 +91,15 @@ constructor( if (!wallpaperManager.isWallpaperSupported || !deviceSupportsAodWallpaper) { if (!wallpaperManager.isWallpaperSupported || !deviceSupportsAodWallpaper) { MutableStateFlow(null).asStateFlow() MutableStateFlow(null).asStateFlow() } else { } else { combine(wallpaperChanged, selectedUser) { _, selectedUser -> combine(wallpaperChanged, selectedUser, ::Pair) getWallpaper(selectedUser) .mapLatestConflated { (_, selectedUser) -> getWallpaper(selectedUser) } } .stateIn( .stateIn( scope, scope, // Always be listening for wallpaper changes. // Always be listening for wallpaper changes. SharingStarted.Eagerly, SharingStarted.Eagerly, initialValue = getWallpaper(userRepository.selectedUser.value), // The initial value is null, but it should get updated pretty quickly because // the `combine` should immediately kick off a fetch. initialValue = null, ) ) } } Loading @@ -111,7 +116,9 @@ constructor( initialValue = wallpaperInfo.value?.supportsAmbientMode() == true, initialValue = wallpaperInfo.value?.supportsAmbientMode() == true, ) ) private fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? { private suspend fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? { return wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id) return withContext(bgDispatcher) { wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id) } } } } }
packages/SystemUI/tests/src/com/android/systemui/wallpapers/data/repository/WallpaperRepositoryImplTest.kt +12 −3 Original line number Original line Diff line number Diff line Loading @@ -33,6 +33,7 @@ import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Before import org.junit.Test import org.junit.Test Loading @@ -41,13 +42,15 @@ import org.junit.Test @OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class) class WallpaperRepositoryImplTest : SysuiTestCase() { class WallpaperRepositoryImplTest : SysuiTestCase() { private val testScope = TestScope(StandardTestDispatcher()) private val testDispatcher = StandardTestDispatcher() private val testScope = TestScope(testDispatcher) private val userRepository = FakeUserRepository() private val userRepository = FakeUserRepository() private val wallpaperManager: WallpaperManager = mock() private val wallpaperManager: WallpaperManager = mock() private val underTest: WallpaperRepositoryImpl by lazy { private val underTest: WallpaperRepositoryImpl by lazy { WallpaperRepositoryImpl( WallpaperRepositoryImpl( testScope.backgroundScope, testScope.backgroundScope, testDispatcher, fakeBroadcastDispatcher, fakeBroadcastDispatcher, userRepository, userRepository, wallpaperManager, wallpaperManager, Loading Loading @@ -102,8 +105,10 @@ class WallpaperRepositoryImplTest : SysuiTestCase() { userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) // WHEN the repo initially starts up (underTest is lazy), then it fetches the current // Start up the repo and let it run the initial fetch // value for the wallpaper underTest.wallpaperInfo runCurrent() assertThat(underTest.wallpaperInfo.value).isEqualTo(SUPPORTED_WP) assertThat(underTest.wallpaperInfo.value).isEqualTo(SUPPORTED_WP) } } Loading Loading @@ -282,6 +287,10 @@ class WallpaperRepositoryImplTest : SysuiTestCase() { userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP)) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP) // Start up the repo and let it run the initial fetch underTest.wallpaperSupportsAmbientMode runCurrent() // WHEN the repo initially starts up (underTest is lazy), then it fetches the current // WHEN the repo initially starts up (underTest is lazy), then it fetches the current // value for the wallpaper // value for the wallpaper assertThat(underTest.wallpaperSupportsAmbientMode.value).isTrue() assertThat(underTest.wallpaperSupportsAmbientMode.value).isTrue() Loading