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

Commit 5f46e1db authored by Caitlin Shkuratov's avatar Caitlin Shkuratov Committed by Android (Google) Code Review
Browse files

Merge "Update WallpaperRepository to fetch wallpaper info in the background." into main

parents e018b4cf 6181f6bf
Loading
Loading
Loading
Loading
+13 −6
Original line number Original line 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.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
@@ -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 {
@@ -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,
@@ -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,
                )
                )
        }
        }


@@ -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)
        }
    }
    }
}
}
+12 −3
Original line number Original line Diff line number Diff line
@@ -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
@@ -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,
@@ -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)
        }
        }


@@ -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()