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

Commit 2981b011 authored by Anton Potapov's avatar Anton Potapov
Browse files

Add infrastructure for screen recording parameters

Flag: com.android.systemui.new_screen_record_toolbar
Bug: 427682447
Test: atest ScreenCaptureRecordParametersInteractorTest
Change-Id: I6f5183df57d9962f1ab0a2a076fd867615f44a66
parent 36ef0423
Loading
Loading
Loading
Loading
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.screencapture.record.domain.interactor

import android.app.ActivityOptions
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.mediaprojection.MediaProjectionCaptureTarget
import com.android.systemui.screencapture.record.shared.model.RecordTargetModel
import com.android.systemui.screenrecord.ScreenRecordingAudioSource
import com.android.systemui.testKosmosNew
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.map
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@SmallTest
class ScreenCaptureRecordParametersModelInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmosNew()

    private val underTest by lazy { kosmos.screenCaptureRecordParametersInteractor }

    @Test
    fun testChangingAudioSource() =
        kosmos.runTest {
            val newAudioSource = ScreenRecordingAudioSource.MIC_AND_INTERNAL
            val audioSource by collectLastValue(underTest.parameters.map { it.audioSource })
            assertThat(audioSource).isNotEqualTo(newAudioSource)

            underTest.setAudioSource(newAudioSource)

            assertThat(audioSource).isEqualTo(newAudioSource)
        }

    @Test
    fun testChangingTarget() =
        kosmos.runTest {
            val newTarget =
                RecordTargetModel.App(
                    MediaProjectionCaptureTarget(ActivityOptions.LaunchCookie(), 1)
                )
            val target by collectLastValue(underTest.parameters.map { it.target })
            assertThat(target).isNotEqualTo(newTarget)

            underTest.setRecordTarget(newTarget)

            assertThat(target).isEqualTo(newTarget)
        }

    @Test
    fun testChangingShouldShowTaps() =
        kosmos.runTest {
            val newShouldShowTaps = true
            val shouldShowTaps by collectLastValue(underTest.parameters.map { it.shouldShowTaps })
            assertThat(shouldShowTaps).isNotEqualTo(newShouldShowTaps)

            underTest.setShouldShowTaps(newShouldShowTaps)

            assertThat(shouldShowTaps).isEqualTo(newShouldShowTaps)
        }
}
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.screencapture.record.data.repository

import com.android.systemui.screencapture.common.ScreenCapture
import com.android.systemui.screencapture.record.shared.model.RecordTargetModel
import com.android.systemui.screencapture.record.shared.model.ScreenCaptureRecordParametersModel
import com.android.systemui.screenrecord.ScreenRecordingAudioSource
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

@ScreenCapture
class ScreenCaptureRecordParametersRepository @Inject constructor() {

    private val _parameters =
        MutableStateFlow(
            ScreenCaptureRecordParametersModel(
                target = RecordTargetModel.WholeScreen,
                audioSource = ScreenRecordingAudioSource.NONE,
                shouldShowTaps = false,
            )
        )
    val parameters = _parameters.asStateFlow()

    fun updateParameters(
        update: (ScreenCaptureRecordParametersModel) -> ScreenCaptureRecordParametersModel
    ) {
        _parameters.update(update)
    }
}
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.screencapture.record.domain.interactor

import com.android.systemui.screencapture.common.ScreenCapture
import com.android.systemui.screencapture.record.data.repository.ScreenCaptureRecordParametersRepository
import com.android.systemui.screencapture.record.shared.model.RecordTargetModel
import com.android.systemui.screenrecord.ScreenRecordingAudioSource
import javax.inject.Inject

@ScreenCapture
class ScreenCaptureRecordParametersInteractor
@Inject
constructor(private val repository: ScreenCaptureRecordParametersRepository) {

    val parameters = repository.parameters

    fun setAudioSource(audioSource: ScreenRecordingAudioSource) {
        repository.updateParameters { it.copy(audioSource = audioSource) }
    }

    fun setRecordTarget(target: RecordTargetModel) {
        repository.updateParameters { it.copy(target = target) }
    }

    fun setShouldShowTaps(shouldShowTaps: Boolean) {
        repository.updateParameters { it.copy(shouldShowTaps = shouldShowTaps) }
    }
}
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.screencapture.record.shared.model

import android.graphics.Rect
import com.android.systemui.mediaprojection.MediaProjectionCaptureTarget

/** Models the target to be recorded. */
sealed interface RecordTargetModel {

    /** A single app should be recorded. */
    data class App(val target: MediaProjectionCaptureTarget) : RecordTargetModel

    /** The whole screen should be recorded. */
    data object WholeScreen : RecordTargetModel

    /** The region of the screen should be recorded. */
    data class Region(val rect: Rect) : RecordTargetModel
}
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.screencapture.record.shared.model

import com.android.systemui.screenrecord.ScreenRecordingAudioSource

/** Models a set of parameters necessary to start a screen recording. */
data class ScreenCaptureRecordParametersModel(
    val target: RecordTargetModel,
    val audioSource: ScreenRecordingAudioSource,
    val shouldShowTaps: Boolean,
)
Loading