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

Commit 6b2e287b authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[SB][Screen Chips] Add share-to-app and cast-to-other-device chips.

The MediaProjectionRepository receives callbacks for both the
share-to-app chip and the cast-to-other-device chip. This CL hooks up
that repository to a new MediaProjectionChipInteractor, and hooks that
interactor up into the existing OngoingActivityChipsViewModel.

These two use cases should actually have different icons. That will come
in the next CL.

Bug: 332662551
Flag: com.android.systemui.status_bar_screen_sharing_chips
Test: Use the Cast QS tile to cast to a device -> verify cast chip
appears with a timer. Stop casting -> verify cast chip disappears.
Test: Share your screen to an app -> verify cast chip appears with a
timer. Stop casting -> verify cast chip disappears.
Test: atest MediaProjectionChipInteractorTest
OngoingActivityChipsViewModelTest

Change-Id: Ic3ce1b9d8d8e5777ca855086bc0564a01a09e6f6
parent 426eecd7
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.chips.mediaprojection.domain.interactor

import com.android.systemui.common.shared.model.ContentDescription
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.mediaprojection.data.model.MediaProjectionState
import com.android.systemui.mediaprojection.data.repository.MediaProjectionRepository
import com.android.systemui.res.R
import com.android.systemui.statusbar.chips.domain.interactor.OngoingActivityChipInteractor
import com.android.systemui.statusbar.chips.domain.model.OngoingActivityChipModel
import com.android.systemui.util.time.SystemClock
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

/**
 * Interactor for media-projection-related chips in the status bar.
 *
 * There are two kinds of media projection events that will show chips in the status bar:
 * 1) Share-to-app: Sharing your phone screen content to another app on the same device. (Triggered
 *    from within each individual app.)
 * 2) Cast-to-other-device: Sharing your phone screen content to a different device. (Triggered from
 *    the Quick Settings Cast tile or from the Settings app.) This interactor handles both of those
 *    event types (though maybe not audio-only casting -- see b/342169876).
 */
@SysUISingleton
class MediaProjectionChipInteractor
@Inject
constructor(
    @Application scope: CoroutineScope,
    mediaProjectionRepository: MediaProjectionRepository,
    val systemClock: SystemClock,
) : OngoingActivityChipInteractor {
    override val chip: StateFlow<OngoingActivityChipModel> =
        mediaProjectionRepository.mediaProjectionState
            .map { state ->
                when (state) {
                    is MediaProjectionState.NotProjecting -> OngoingActivityChipModel.Hidden
                    is MediaProjectionState.EntireScreen,
                    is MediaProjectionState.SingleTask -> {
                        // TODO(b/332662551): Distinguish between cast-to-other-device and
                        // share-to-app.
                        OngoingActivityChipModel.Shown(
                            icon =
                                Icon.Resource(
                                    R.drawable.ic_cast_connected,
                                    ContentDescription.Resource(R.string.accessibility_casting)
                                ),
                            // TODO(b/332662551): See if we can use a MediaProjection API to fetch
                            // this time.
                            startTimeMs = systemClock.elapsedRealtime()
                        ) {
                            // TODO(b/332662551): Implement the pause dialog.
                        }
                    }
                }
            }
            .stateIn(scope, SharingStarted.WhileSubscribed(), OngoingActivityChipModel.Hidden)
}
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ import kotlinx.coroutines.flow.stateIn

/** Interactor for the screen recording chip shown in the status bar. */
@SysUISingleton
open class ScreenRecordChipInteractor
class ScreenRecordChipInteractor
@Inject
constructor(
    @Application scope: CoroutineScope,
+12 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.statusbar.chips.call.domain.interactor.CallChipInteractor
import com.android.systemui.statusbar.chips.domain.model.OngoingActivityChipModel
import com.android.systemui.statusbar.chips.mediaprojection.domain.interactor.MediaProjectionChipInteractor
import com.android.systemui.statusbar.chips.screenrecord.domain.interactor.ScreenRecordChipInteractor
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
@@ -40,6 +41,7 @@ class OngoingActivityChipsViewModel
constructor(
    @Application scope: CoroutineScope,
    screenRecordChipInteractor: ScreenRecordChipInteractor,
    mediaProjectionChipInteractor: MediaProjectionChipInteractor,
    callChipInteractor: CallChipInteractor,
) {

@@ -51,10 +53,19 @@ constructor(
     * actually displaying the chip.
     */
    val chip: StateFlow<OngoingActivityChipModel> =
        combine(screenRecordChipInteractor.chip, callChipInteractor.chip) { screenRecord, call ->
        combine(
                screenRecordChipInteractor.chip,
                mediaProjectionChipInteractor.chip,
                callChipInteractor.chip
            ) { screenRecord, mediaProjection, call ->
                // This `when` statement shows the priority order of the chips
                when {
                    // Screen recording also activates the media projection APIs, so whenever the
                    // screen recording chip is active, the media projection chip would also be
                    // active. We want the screen-recording-specific chip shown in this case, so we
                    // give the screen recording chip priority. See b/296461748.
                    screenRecord is OngoingActivityChipModel.Shown -> screenRecord
                    mediaProjection is OngoingActivityChipModel.Shown -> mediaProjection
                    else -> call
                }
            }
+1 −2
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import com.android.systemui.mediaprojection.taskswitcher.FakeActivityTaskManager
import com.android.systemui.mediaprojection.taskswitcher.FakeActivityTaskManager.Companion.createToken
import com.android.systemui.mediaprojection.taskswitcher.fakeActivityTaskManager
import com.android.systemui.mediaprojection.taskswitcher.fakeMediaProjectionManager
import com.android.systemui.mediaprojection.taskswitcher.mediaProjectionManagerRepository
import com.android.systemui.mediaprojection.taskswitcher.taskSwitcherKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
@@ -45,7 +44,7 @@ class MediaProjectionManagerRepositoryTest : SysuiTestCase() {
    private val fakeMediaProjectionManager = kosmos.fakeMediaProjectionManager
    private val fakeActivityTaskManager = kosmos.fakeActivityTaskManager

    private val repo = kosmos.mediaProjectionManagerRepository
    private val repo = kosmos.realMediaProjectionRepository

    @Test
    fun switchProjectedTask_stateIsUpdatedWithNewTask() =
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.chips.mediaprojection.domain.interactor

import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testScope
import com.android.systemui.mediaprojection.data.model.MediaProjectionState
import com.android.systemui.mediaprojection.data.repository.fakeMediaProjectionRepository
import com.android.systemui.mediaprojection.taskswitcher.FakeActivityTaskManager.Companion.createTask
import com.android.systemui.res.R
import com.android.systemui.statusbar.chips.domain.model.OngoingActivityChipModel
import com.android.systemui.statusbar.chips.ui.viewmodel.mediaProjectionChipInteractor
import com.android.systemui.util.time.fakeSystemClock
import com.google.common.truth.Truth.assertThat
import kotlin.test.Test
import kotlinx.coroutines.test.runTest

@SmallTest
class MediaProjectionChipInteractorTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val testScope = kosmos.testScope
    private val mediaProjectionRepo = kosmos.fakeMediaProjectionRepository
    private val systemClock = kosmos.fakeSystemClock

    private val underTest = kosmos.mediaProjectionChipInteractor

    @Test
    fun chip_notProjectingState_isHidden() =
        testScope.runTest {
            val latest by collectLastValue(underTest.chip)

            mediaProjectionRepo.mediaProjectionState.value = MediaProjectionState.NotProjecting

            assertThat(latest).isInstanceOf(OngoingActivityChipModel.Hidden::class.java)
        }

    @Test
    fun chip_singleTaskState_isShownWithIcon() =
        testScope.runTest {
            val latest by collectLastValue(underTest.chip)

            mediaProjectionRepo.mediaProjectionState.value =
                MediaProjectionState.SingleTask(createTask(taskId = 1))

            assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
            val icon = (latest as OngoingActivityChipModel.Shown).icon
            assertThat((icon as Icon.Resource).res).isEqualTo(R.drawable.ic_cast_connected)
        }

    @Test
    fun chip_entireScreenState_isShownWithIcon() =
        testScope.runTest {
            val latest by collectLastValue(underTest.chip)

            mediaProjectionRepo.mediaProjectionState.value = MediaProjectionState.EntireScreen

            assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
            val icon = (latest as OngoingActivityChipModel.Shown).icon
            assertThat((icon as Icon.Resource).res).isEqualTo(R.drawable.ic_cast_connected)
        }

    @Test
    fun chip_timeResetsOnEachNewShare() =
        testScope.runTest {
            val latest by collectLastValue(underTest.chip)

            systemClock.setElapsedRealtime(1234)
            mediaProjectionRepo.mediaProjectionState.value = MediaProjectionState.EntireScreen

            assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
            assertThat((latest as OngoingActivityChipModel.Shown).startTimeMs).isEqualTo(1234)

            mediaProjectionRepo.mediaProjectionState.value = MediaProjectionState.NotProjecting
            assertThat(latest).isInstanceOf(OngoingActivityChipModel.Hidden::class.java)

            systemClock.setElapsedRealtime(5678)
            mediaProjectionRepo.mediaProjectionState.value = MediaProjectionState.EntireScreen

            assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
            assertThat((latest as OngoingActivityChipModel.Shown).startTimeMs).isEqualTo(5678)
        }
}
Loading