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

Commit 1b5f0143 authored by Tianfan Zhang's avatar Tianfan Zhang
Browse files

Add globallyFocusedTaskId stateflow. It will be used to determine

whether user is switching apps.

Bug: 403422950
Test: atest AmbientCueRepositoryTest
Flag: com.android.systemui.enable_underlay

Change-Id: I537fd04bb02ebb6e6e6d1377a7b0a174ecdb9f26
parent fa9d6da4
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.ambientcue.data.repository

import android.app.ActivityManager.RunningTaskInfo
import android.app.smartspace.SmartspaceAction
import android.app.smartspace.SmartspaceManager
import android.app.smartspace.SmartspaceSession
@@ -32,6 +33,7 @@ import com.android.systemui.kosmos.backgroundScope
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.shade.data.repository.fakeFocusedDisplayRepository
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Test
@@ -58,6 +60,7 @@ class AmbientCueRepositoryTest : SysuiTestCase() {
            smartSpaceManager = smartSpaceManager,
            executor = kosmos.fakeExecutor,
            applicationContext = kosmos.testableContext,
            focusdDisplayRepository = kosmos.fakeFocusedDisplayRepository,
        )

    @Test
@@ -81,9 +84,7 @@ class AmbientCueRepositoryTest : SysuiTestCase() {
            runCurrent()
            verify(smartSpaceSession)
                .addOnTargetsAvailableListener(any(), onTargetsAvailableListenerCaptor.capture())
            onTargetsAvailableListenerCaptor.firstValue.onTargetsAvailable(
                listOf(invalidTarget1)
            )
            onTargetsAvailableListenerCaptor.firstValue.onTargetsAvailable(listOf(invalidTarget1))
            advanceUntilIdle()
            assertThat(isVisible).isFalse()
        }
@@ -112,6 +113,17 @@ class AmbientCueRepositoryTest : SysuiTestCase() {
            }
        }

    @Test
    fun globallyFocusedTaskId_whenFocusedTaskChange_taskIdUpdated() =
        kosmos.runTest {
            val globallyFocusedTaskId by collectLastValue(underTest.globallyFocusedTaskId)
            runCurrent()

            fakeFocusedDisplayRepository.setGlobalTask(RunningTaskInfo().apply { taskId = TASK_ID })

            assertThat(globallyFocusedTaskId).isEqualTo(TASK_ID)
        }

    companion object {

        private const val TITLE_1 = "title 1"
@@ -140,5 +152,7 @@ class AmbientCueRepositoryTest : SysuiTestCase() {
            }

        private val allTargets = listOf(validTarget, invalidTarget1)

        private const val TASK_ID = 1
    }
}
+17 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.ambientcue.data.repository

import android.app.ActivityTaskManager
import android.app.smartspace.SmartspaceConfig
import android.app.smartspace.SmartspaceManager
import android.app.smartspace.SmartspaceSession.OnTargetsAvailableListener
@@ -26,6 +27,7 @@ import com.android.systemui.ambientcue.shared.model.ActionModel
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.display.data.repository.FocusedDisplayRepository
import com.android.systemui.res.R
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import java.util.concurrent.Executor
@@ -35,6 +37,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
@@ -49,6 +52,9 @@ interface AmbientCueRepository {

    /** If IME is visible or not. */
    val isImeVisible: MutableStateFlow<Boolean>

    /** Task Id which is globally focused on display. */
    val globallyFocusedTaskId: StateFlow<Int>
}

@SysUISingleton
@@ -59,6 +65,7 @@ constructor(
    private val smartSpaceManager: SmartspaceManager?,
    @Background executor: Executor,
    @Application applicationContext: Context,
    focusdDisplayRepository: FocusedDisplayRepository,
) : AmbientCueRepository {

    override val actions: StateFlow<List<ActionModel>> =
@@ -114,11 +121,21 @@ constructor(

    override val isImeVisible: MutableStateFlow<Boolean> = MutableStateFlow(false)

    override val globallyFocusedTaskId: StateFlow<Int> =
        focusdDisplayRepository.globallyFocusedTask
            .map { it?.taskId ?: INVALID_TASK_ID }
            .stateIn(
                scope = backgroundScope,
                started = SharingStarted.WhileSubscribed(),
                initialValue = INVALID_TASK_ID,
            )

    companion object {
        // Surface that PCC wants to push cards into
        @VisibleForTesting const val AMBIENT_CUE_SURFACE = "ambientcue"
        // Timeout to hide cuebar if it wasn't interacted with
        private const val TAG = "AmbientCueRepository"
        private const val DEBUG = false
        private const val INVALID_TASK_ID = ActivityTaskManager.INVALID_TASK_ID
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -33,7 +33,14 @@ class FakeAmbientCueRepository : AmbientCueRepository {

    override val isImeVisible: MutableStateFlow<Boolean> = MutableStateFlow(false)

    private val _globallyFocusedTaskId = MutableStateFlow(0)
    override val globallyFocusedTaskId: StateFlow<Int> = _globallyFocusedTaskId.asStateFlow()

    fun setActions(actions: List<ActionModel>) {
        _actions.update { actions }
    }

    fun setGloballyFocusedTaskId(taskId: Int) {
        _globallyFocusedTaskId.update { taskId }
    }
}