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

Commit 6181f6bf authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

Update WallpaperRepository to fetch wallpaper info in the background.

Fixes: 324757468
Flag: NONE (bugfix)
Test: atest WallpaperRepositoryImplTest
Test: manual: change wallpapers successfully
Change-Id: I0cae1a1a16536497a6881a995e5465b4ffc492c5
parent 8e1ad154
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -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.SelectionStatus
import com.android.systemui.user.data.repository.UserRepository
import com.android.systemui.utils.coroutines.flow.mapLatestConflated
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@@ -40,6 +42,7 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext

/** A repository storing information about the current wallpaper. */
interface WallpaperRepository {
@@ -55,6 +58,7 @@ class WallpaperRepositoryImpl
@Inject
constructor(
    @Background scope: CoroutineScope,
    @Background private val bgDispatcher: CoroutineDispatcher,
    broadcastDispatcher: BroadcastDispatcher,
    userRepository: UserRepository,
    private val wallpaperManager: WallpaperManager,
@@ -87,14 +91,15 @@ constructor(
        if (!wallpaperManager.isWallpaperSupported || !deviceSupportsAodWallpaper) {
            MutableStateFlow(null).asStateFlow()
        } else {
            combine(wallpaperChanged, selectedUser) { _, selectedUser ->
                    getWallpaper(selectedUser)
                }
            combine(wallpaperChanged, selectedUser, ::Pair)
                .mapLatestConflated { (_, selectedUser) -> getWallpaper(selectedUser) }
                .stateIn(
                    scope,
                    // Always be listening for wallpaper changes.
                    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,
                )
        }

@@ -111,7 +116,9 @@ constructor(
                initialValue = wallpaperInfo.value?.supportsAmbientMode() == true,
            )

    private fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? {
        return wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id)
    private suspend fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? {
        return withContext(bgDispatcher) {
            wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id)
        }
    }
}
+12 −3
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@@ -41,13 +42,15 @@ import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class)
class WallpaperRepositoryImplTest : SysuiTestCase() {

    private val testScope = TestScope(StandardTestDispatcher())
    private val testDispatcher = StandardTestDispatcher()
    private val testScope = TestScope(testDispatcher)
    private val userRepository = FakeUserRepository()
    private val wallpaperManager: WallpaperManager = mock()

    private val underTest: WallpaperRepositoryImpl by lazy {
        WallpaperRepositoryImpl(
            testScope.backgroundScope,
            testDispatcher,
            fakeBroadcastDispatcher,
            userRepository,
            wallpaperManager,
@@ -102,8 +105,10 @@ class WallpaperRepositoryImplTest : SysuiTestCase() {
            userRepository.setUserInfos(listOf(USER_WITH_SUPPORTED_WP))
            userRepository.setSelectedUserInfo(USER_WITH_SUPPORTED_WP)

            // WHEN the repo initially starts up (underTest is lazy), then it fetches the current
            // value for the wallpaper
            // Start up the repo and let it run the initial fetch
            underTest.wallpaperInfo
            runCurrent()

            assertThat(underTest.wallpaperInfo.value).isEqualTo(SUPPORTED_WP)
        }

@@ -282,6 +287,10 @@ class WallpaperRepositoryImplTest : SysuiTestCase() {
            userRepository.setUserInfos(listOf(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
            // value for the wallpaper
            assertThat(underTest.wallpaperSupportsAmbientMode.value).isTrue()