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

Commit c7547feb authored by Sherry Zhou's avatar Sherry Zhou
Browse files

Send wallpaper focal area when lockscreen wallpaper is Magic Portrait

Test: atest WallpaperRepositoryImplTest
Bug: 400803879
Bug: 399850370
Flag: com.android.systemui.shared.extended_wallpaper_effects

Change-Id: Iaa0b9b94c6d56cfb63957b0ccf3e497859b0dcf5
parent ccb979d4
Loading
Loading
Loading
Loading
+51 −4
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.wallpapers.data.repository

import android.app.WallpaperInfo
import android.app.WallpaperManager
import android.app.WallpaperManager.FLAG_LOCK
import android.content.ComponentName
import android.content.Intent
import android.content.pm.UserInfo
@@ -44,6 +45,7 @@ import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.eq
import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.mock
@@ -275,17 +277,17 @@ class WallpaperRepositoryImplTest : SysuiTestCase() {

    @Test
    @EnableFlags(SharedFlags.FLAG_EXTENDED_WALLPAPER_EFFECTS)
    fun shouldSendNotificationLayout_setExtendedEffectsWallpaper_launchSendLayoutJob() =
    fun shouldSendNotificationLayout_setExtendedEffectsWallpaper() =
        testScope.runTest {
            underTest = kosmos.wallpaperRepository
            val latest by collectLastValue(underTest.shouldSendFocalArea)
            val extedendEffectsWallpaper =
            val extendedEffectsWallpaper =
                mock<WallpaperInfo>().apply {
                    whenever(this.component).thenReturn(ComponentName(context, focalAreaTarget))
                }

            whenever(kosmos.wallpaperManager.getWallpaperInfoForUser(any()))
                .thenReturn(extedendEffectsWallpaper)
                .thenReturn(extendedEffectsWallpaper)
            broadcastDispatcher.sendIntentToMatchingReceiversOnly(
                context,
                Intent(Intent.ACTION_WALLPAPER_CHANGED),
@@ -295,7 +297,7 @@ class WallpaperRepositoryImplTest : SysuiTestCase() {

    @Test
    @EnableFlags(SharedFlags.FLAG_EXTENDED_WALLPAPER_EFFECTS)
    fun shouldSendNotificationLayout_setNotExtendedEffectsWallpaper_cancelSendLayoutJob() =
    fun shouldSendNotificationLayout_setNotExtendedEffectsWallpaper() =
        testScope.runTest {
            underTest = kosmos.wallpaperRepository
            val latest by collectLastValue(underTest.shouldSendFocalArea)
@@ -322,6 +324,51 @@ class WallpaperRepositoryImplTest : SysuiTestCase() {
            assertThat(latest).isFalse()
        }

    @Test
    @EnableFlags(SharedFlags.FLAG_EXTENDED_WALLPAPER_EFFECTS)
    fun shouldSendNotificationLayout_setExtendedEffectsWallpaperOnlyForHomescreen() =
        testScope.runTest {
            underTest = kosmos.wallpaperRepository
            val latest by collectLastValue(underTest.shouldSendFocalArea)
            val extendedEffectsWallpaper =
                mock<WallpaperInfo>().apply {
                    whenever(this.component).thenReturn(ComponentName("", focalAreaTarget))
                }

            whenever(kosmos.wallpaperManager.lockScreenWallpaperExists()).thenReturn(true)
            whenever(kosmos.wallpaperManager.getWallpaperInfoForUser(any()))
                .thenReturn(extendedEffectsWallpaper)
            whenever(kosmos.wallpaperManager.getWallpaperInfo(eq(FLAG_LOCK), any()))
                .thenReturn(UNSUPPORTED_WP)
            broadcastDispatcher.sendIntentToMatchingReceiversOnly(
                context,
                Intent(Intent.ACTION_WALLPAPER_CHANGED),
            )
            assertThat(latest).isFalse()
        }

    @Test
    @EnableFlags(SharedFlags.FLAG_EXTENDED_WALLPAPER_EFFECTS)
    fun shouldSendNotificationLayout_setExtendedEffectsWallpaperOnlyForLockscreen() =
        testScope.runTest {
            underTest = kosmos.wallpaperRepository
            val latest by collectLastValue(underTest.shouldSendFocalArea)
            val extendedEffectsWallpaper =
                mock<WallpaperInfo>().apply {
                    whenever(this.component).thenReturn(ComponentName("", focalAreaTarget))
                }
            whenever(kosmos.wallpaperManager.lockScreenWallpaperExists()).thenReturn(true)
            whenever(kosmos.wallpaperManager.getWallpaperInfoForUser(any()))
                .thenReturn(UNSUPPORTED_WP)
            whenever(kosmos.wallpaperManager.getWallpaperInfo(eq(FLAG_LOCK), any()))
                .thenReturn(extendedEffectsWallpaper)
            broadcastDispatcher.sendIntentToMatchingReceiversOnly(
                context,
                Intent(Intent.ACTION_WALLPAPER_CHANGED),
            )
            assertThat(latest).isTrue()
        }

    private companion object {
        val USER_WITH_UNSUPPORTED_WP = UserInfo(/* id= */ 3, /* name= */ "user3", /* flags= */ 0)
        val UNSUPPORTED_WP =
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import kotlinx.coroutines.flow.flowOf
@SysUISingleton
class NoopWallpaperRepository @Inject constructor() : WallpaperRepository {
    override val wallpaperInfo: StateFlow<WallpaperInfo?> = MutableStateFlow(null).asStateFlow()
    override val lockscreenWallpaperInfo: StateFlow<WallpaperInfo?> =
        MutableStateFlow(null).asStateFlow()
    override val wallpaperSupportsAmbientMode = flowOf(false)
    override var rootView: View? = null
    override val shouldSendFocalArea: StateFlow<Boolean> = MutableStateFlow(false).asStateFlow()
+37 −20
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.systemui.wallpapers.data.repository

import android.app.WallpaperInfo
import android.app.WallpaperManager
import android.app.WallpaperManager.FLAG_LOCK
import android.app.WallpaperManager.FLAG_SYSTEM
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
@@ -63,6 +65,12 @@ interface WallpaperRepository {
    /** Emits the current user's current wallpaper. */
    val wallpaperInfo: StateFlow<WallpaperInfo?>

    /**
     * Emits the current user's lockscreen wallpaper. This will emit the same value as
     * [wallpaperInfo] if the wallpaper is shared between home and lock screen.
     */
    val lockscreenWallpaperInfo: StateFlow<WallpaperInfo?>

    /** Emits true if the current user's current wallpaper supports ambient mode. */
    val wallpaperSupportsAmbientMode: Flow<Boolean>

@@ -81,7 +89,7 @@ interface WallpaperRepository {
class WallpaperRepositoryImpl
@Inject
constructor(
    @Background scope: CoroutineScope,
    @Background private val scope: CoroutineScope,
    @Background private val bgDispatcher: CoroutineDispatcher,
    broadcastDispatcher: BroadcastDispatcher,
    userRepository: UserRepository,
@@ -106,22 +114,8 @@ constructor(
            // Only update the wallpaper status once the user selection has finished.
            .filter { it.selectionStatus == SelectionStatus.SELECTION_COMPLETE }

    override val wallpaperInfo: StateFlow<WallpaperInfo?> =
        if (!wallpaperManager.isWallpaperSupported) {
            MutableStateFlow(null).asStateFlow()
        } else {
            combine(wallpaperChanged, selectedUser, ::Pair)
                .mapLatestConflated { (_, selectedUser) -> getWallpaper(selectedUser) }
                .stateIn(
                    scope,
                    // Always be listening for wallpaper changes.
                    SharingStarted.Eagerly,
                    // The initial value is null, but it should get updated pretty quickly because
                    // the `combine` should immediately kick off a fetch.
                    initialValue = null,
                )
        }

    override val wallpaperInfo: StateFlow<WallpaperInfo?> = getWallpaperInfo(FLAG_SYSTEM)
    override val lockscreenWallpaperInfo: StateFlow<WallpaperInfo?> = getWallpaperInfo(FLAG_LOCK)
    override val wallpaperSupportsAmbientMode: Flow<Boolean> =
        secureSettings
            .observerFlow(UserHandle.USER_ALL, Settings.Secure.DOZE_ALWAYS_ON_WALLPAPER_ENABLED)
@@ -172,7 +166,7 @@ constructor(
    }

    override val shouldSendFocalArea =
        wallpaperInfo
        lockscreenWallpaperInfo
            .map {
                val focalAreaTarget = context.resources.getString(SysUIR.string.focal_area_target)
                val shouldSendNotificationLayout = it?.component?.className == focalAreaTarget
@@ -184,11 +178,34 @@ constructor(
                initialValue = extendedWallpaperEffects(),
            )

    private suspend fun getWallpaper(selectedUser: SelectedUserModel): WallpaperInfo? {
    private suspend fun getWallpaper(
        selectedUser: SelectedUserModel,
        which: Int = FLAG_SYSTEM,
    ): WallpaperInfo? {
        return withContext(bgDispatcher) {
            if (which == FLAG_LOCK && wallpaperManager.lockScreenWallpaperExists()) {
                wallpaperManager.getWallpaperInfo(FLAG_LOCK, selectedUser.userInfo.id)
            } else {
                wallpaperManager.getWallpaperInfoForUser(selectedUser.userInfo.id)
            }
        }
    }

    private fun getWallpaperInfo(which: Int): StateFlow<WallpaperInfo?> =
        if (!wallpaperManager.isWallpaperSupported) {
            MutableStateFlow(null).asStateFlow()
        } else {
            combine(wallpaperChanged, selectedUser, ::Pair)
                .mapLatestConflated { (_, selectedUser) -> getWallpaper(selectedUser, which) }
                .stateIn(
                    scope,
                    // Always be listening for wallpaper changes.
                    SharingStarted.Eagerly,
                    // The initial value is null, but it should get updated pretty quickly because
                    // the `combine` should immediately kick off a fetch.
                    initialValue = null,
                )
        }

    companion object {
        private val TAG = WallpaperRepositoryImpl::class.simpleName
+3 −0
Original line number Diff line number Diff line
@@ -29,6 +29,9 @@ import kotlinx.coroutines.flow.asStateFlow
class FakeWallpaperRepository : WallpaperRepository {
    private val _wallpaperInfo: MutableStateFlow<WallpaperInfo?> = MutableStateFlow(null)
    override val wallpaperInfo: StateFlow<WallpaperInfo?> = _wallpaperInfo.asStateFlow()
    private val _lockscreenWallpaperInfo: MutableStateFlow<WallpaperInfo?> = MutableStateFlow(null)
    override val lockscreenWallpaperInfo: StateFlow<WallpaperInfo?> =
        _lockscreenWallpaperInfo.asStateFlow()
    private val _wallpaperSupportsAmbientMode = MutableStateFlow(false)
    override val wallpaperSupportsAmbientMode: Flow<Boolean> =
        _wallpaperSupportsAmbientMode.asStateFlow()