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

Commit b84ec57b authored by Wes Okuhara's avatar Wes Okuhara Committed by Android (Google) Code Review
Browse files

Merge "Revert "Desktop screenshots: Temporary fix to ensure UI is exclu..."" into main

parents ef8622f2 5403e4ce
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -34,13 +34,13 @@ import kotlinx.coroutines.withContext
class ScreenshotInteractor
@Inject
constructor(
    @Background private val backgroundContext: CoroutineContext,
    @Background private val backgroundHandler: Handler,
    private val imageCapture: ImageCapture,
    private val screenshotHelper: ScreenshotHelper,
    @Background private val backgroundContext: CoroutineContext,
    @Background private val backgroundHandler: Handler,
    private val userRepository: UserRepository,
) {
    suspend fun requestFullscreenScreenshot(displayId: Int) {
    suspend fun takeFullscreenScreenshot(displayId: Int) {
        val request =
            ScreenshotRequest.Builder(
                    WindowManager.TAKE_SCREENSHOT_FULLSCREEN,
@@ -52,7 +52,7 @@ constructor(
        takeScreenshot(request)
    }

    suspend fun requestPartialScreenshot(regionBounds: Rect, displayId: Int) {
    suspend fun takePartialScreenshot(regionBounds: Rect, displayId: Int) {
        val bitmap =
            withContext(backgroundContext) {
                requireNotNull(imageCapture.captureDisplay(displayId, regionBounds))
+13 −14
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
@@ -127,32 +126,32 @@ constructor(

    private fun takeScreenshot() {
        when (captureRegionSource.value) {
            ScreenCaptureRegion.FULLSCREEN -> beginFullscreenScreenshot()
            ScreenCaptureRegion.PARTIAL -> beginPartialScreenshot()
            ScreenCaptureRegion.FULLSCREEN -> takeFullscreenScreenshot()
            ScreenCaptureRegion.PARTIAL -> takePartialScreenshot()
            ScreenCaptureRegion.APP_WINDOW -> {}
        }
    }

    private fun beginFullscreenScreenshot() {
        // Hide the UI to avoid the parent window closing animation.
    private fun takeFullscreenScreenshot() {
        // Finishing the activity is not guaranteed to complete before the screenshot is taken.
        // Since the pre-capture UI should not be included in the screenshot, hide the UI first.
        hideUi()
        backgroundScope.launch { screenshotInteractor.requestFullscreenScreenshot(displayId) }
        closeUi()

        backgroundScope.launch { screenshotInteractor.takeFullscreenScreenshot(displayId) }
    }

    private fun beginPartialScreenshot() {
    private fun takePartialScreenshot() {
        val regionBoxRect = requireNotNull(regionBoxSource.value)

        // Hide the UI to avoid the parent window closing animation.
        // Finishing the activity is not guaranteed to complete before the screenshot is taken.
        // Since the pre-capture UI should not be included in the screenshot, hide the UI first.
        hideUi()
        closeUi()

        backgroundScope.launch {
            // Temporary fix to allow enough time for the pre-capture UI to dismiss.
            // TODO(b/435225255) Implement a more reliable way to ensure the UI is hidden prior to
            // taking the screenshot.
            delay(100)
            screenshotInteractor.requestPartialScreenshot(regionBoxRect, displayId)
            screenshotInteractor.takePartialScreenshot(regionBoxRect, displayId)
        }
        closeUi()
    }

    /**
+4 −4
Original line number Diff line number Diff line
@@ -61,10 +61,10 @@ class ScreenshotInteractorTest : SysuiTestCase() {
    }

    @Test
    fun requestFullscreenScreenshot_callsScreenshotHelper_withCorrectRequest() {
    fun takeFullscreenScreenshot_callsScreenshotHelper_withCorrectRequest() {
        testScope.runTest {
            val displayId = 3
            interactor.requestFullscreenScreenshot(displayId)
            interactor.takeFullscreenScreenshot(displayId)

            val screenshotRequestCaptor = argumentCaptor<ScreenshotRequest>()
            verify(kosmos.mockScreenshotHelper, times(1))
@@ -79,7 +79,7 @@ class ScreenshotInteractorTest : SysuiTestCase() {
    }

    @Test
    fun requestPartialScreenshot_callsScreenshotHelper_withCorrectRequest() {
    fun takePartialScreenshot_callsScreenshotHelper_withCorrectRequest() {
        testScope.runTest {
            val bounds = Rect(0, 0, 100, 100)
            val displayId = 3
@@ -91,7 +91,7 @@ class ScreenshotInteractorTest : SysuiTestCase() {
            kosmos.fakeUserRepository.setUserInfos(listOf(mainUser, secondaryUser))
            kosmos.fakeUserRepository.setSelectedUserInfo(secondaryUser)

            interactor.requestPartialScreenshot(bounds, displayId)
            interactor.takePartialScreenshot(bounds, displayId)

            val screenshotRequestCaptor = argumentCaptor<ScreenshotRequest>()
            verify(kosmos.mockImageCapture, times(1)).captureDisplay(any(), eq(bounds))